def ext_helmholtz_convg(qbx_order, k):
    """
    Tests external Helmholtz solution by placing monopoles inside domain
    """
    alpha = k  # CFIE parameter
    sp, dp, qbx_exp_slp, qbx_exp_dlp, _, _, _, _ = eval_sp_dp_QBX(qbx_order, k)

    xs = 0.75
    ys = 1.75
    test_targets = np.array([[-1, 0], [4, 0], [0, -1], [0, 4]]).T
    # test_targets = np.array([[-5, 0], [5, 0], [0, -5], [0, 5]]).T
    exact_test = sp(test_targets[0], test_targets[1], xs, ys)
    plotset()
    n_panels = np.arange(10, 60, 5)
    convg_err = []
    for n in n_panels:
        domain = QuadratureInfo(n, show_bound=0)
        rhs = sp(domain.curve_nodes[0], domain.curve_nodes[1], xs, ys)
        soln = bvp(n, k, domain, alpha, qbx_exp_slp, qbx_exp_dlp, rhs)
        num_test = eval_target(
            test_targets,
            domain.curve_nodes,
            domain.curve_weights,
            domain.normals,
            soln,
            sp,
            dp,
            alpha,
        )
        er = la.norm(
            np.abs(num_test - exact_test) / la.norm(np.abs(exact_test)))
        convg_err.append(er)

    plt.figure(1)
    plt.loglog(n_panels,
               np.array(convg_err),
               "-o",
               label="QBX" + str(qbx_order))
    plt.loglog(
        n_panels,
        5e4 * np.power(n_panels.astype(float), -qbx_order - 1),
        label="$N^{-5}$",
    )
    plt.loglog(n_panels,
               2e3 * np.power(n_panels.astype(float), -qbx_order),
               label="$N^{-4}$")
    title = "Helmholtz exterior problem: target evaluation convergence"
    plt.xlabel("Number of panels", size=25)
    plt.ylabel("$|u - u_e| / |u_e|$", size=25)
    plt.title(title, size=25)
    plt.legend(loc="upper right", ncol=1, frameon=True)
    plt.show()
    slope, _, _, _, _ = stats.linregress(np.log(n_panels),
                                         np.log(np.array(convg_err)))
    print(slope)
def main():
    k = 10
    alpha = k  # CFIE parameter
    sp, dp, qbx_exp_slp, qbx_exp_dlp, _, _, _, _ = eval_sp_dp_QBX(4, k)
    npanels = 30
    domain = QuadratureInfo(npanels, show_bound=1)
    xs = 0.75
    ys = 1.75

    tg_size = 101
    x = np.linspace(-2, 5, tg_size)
    y = np.linspace(-2, 5, tg_size)
    X, Y = np.meshgrid(x, y)
    test_targets = np.array((X.reshape(-1), Y.reshape(-1)))
    rhs = sp(domain.curve_nodes[0], domain.curve_nodes[1], xs, ys)
    soln1 = bvp(npanels, k, domain, alpha, qbx_exp_slp, qbx_exp_dlp, rhs)
    num_test = eval_target(
        test_targets,
        domain.curve_nodes,
        domain.curve_weights,
        domain.normals,
        soln1,
        sp,
        dp,
        alpha,
    ).reshape(tg_size, -1)
    exact_test = sp(test_targets[0], test_targets[1], xs, ys).reshape(tg_size, -1)
    err = np.abs(num_test - exact_test) / np.abs(exact_test)
    # plt.contourf(X, Y, np.real(exact_test), cmap='magma', levels=100)
    v = np.linspace(-0.15, 0.15, 100, endpoint=True)
    plt.figure(1)
    plt.contourf(X, Y, np.real(num_test), v, cmap="twilight")
    plt.plot(xs, ys, "ro")
    plt.fill(domain.curve_nodes[0], domain.curve_nodes[1], "w")
    show_targets = np.array([[-1, 0], [4, 0], [0, -1], [0, 4]]).T
    plt.plot(show_targets[0], show_targets[1], "go")
    plt.xlabel("X", size=10)
    plt.ylabel("Y", size=10)
    plt.colorbar()
    plt.figure(2)
    plt.contourf(X, Y, np.log10(err), cmap="magma", levels=200)
    plt.fill(domain.curve_nodes[0], domain.curve_nodes[1], "w")
    plt.plot(xs, ys, "ro")
    plt.plot(show_targets[0], show_targets[1], "go")
    plt.xlabel("X", size=10)
    plt.ylabel("Y", size=10)
    plt.colorbar()
    plt.show()
y = np.linspace(0, 8, tg_size)
X, Y = np.meshgrid(x, y)
test_targets = np.array((X.reshape(-1), Y.reshape(-1)))
exact_test = (sp(test_targets[0], test_targets[1], xs,
                 ys)).reshape(tg_size, -1)
som_test = som_sp(test_targets[0], test_targets[1], xs,
                  ys).reshape(tg_size, -1)
imag_sp_test, _ = imgs.eval_integral(test_targets, (xs, ys), 0.0, 0.0)
exact_test += som_test + imag_sp_test.reshape(tg_size, -1)
num_test = eval_target(
    test_targets,
    domain.curve_nodes,
    domain.curve_weights,
    domain.normals,
    soln1,
    sp,
    dp,
    alpha,
    som_sp=som_sp,
    som_dp=som_dp,
    imgs=imgs,
).reshape(tg_size, -1)
v = np.linspace(-0.15, 0.15, 200, endpoint=True)
show_targets = np.array([[2, 4], [0, 5], [-2, 4]]).T
plt.figure(1)
plt.contourf(X, Y, np.real(exact_test), v, cmap="twilight")
plt.xlabel("X", size=10)
plt.ylabel("Y", size=10)
plt.colorbar()
plt.figure(2)
plt.contourf(X, Y, np.real(num_test), v, cmap="twilight")