Ejemplo n.º 1
0
def test_rational_chebyshev_interpolation(show=False):
    """Test the inperpolation routine of ChebyshevRationalGrid"""
    import numpy as np
    from psecas import ChebyshevRationalGrid

    def psi(x, c):
        from numpy.polynomial.hermite import hermval

        return hermval(x, c) * np.exp(-x**2 / 2)

    N = 95
    grid = ChebyshevRationalGrid(N, C=4)

    grid_fine = ChebyshevRationalGrid(N * 4, C=1)
    z = grid_fine.zg

    y = psi(grid.zg, np.array(np.ones(4)))
    y_fine = psi(z, np.array(np.ones(4)))
    y_interpolated = grid.interpolate(z, y)

    if show:
        import matplotlib.pyplot as plt

        plt.figure(3)
        plt.clf()
        plt.title("Interpolation with rational Chebyshev")
        plt.plot(z, y_fine, "-")
        plt.plot(z, y_interpolated, "--")
        plt.plot(grid.zg, y, "+")
        plt.xlim(-15, 15)
        plt.show()

    np.testing.assert_allclose(y_fine, y_interpolated, atol=1e-12)
    return (y_fine, y_interpolated)
Ejemplo n.º 2
0
fig, axes = plt.subplots(num=1, ncols=2, nrows=modes, sharex=True,
    sharey='col')
for mode in range(modes):
    # List of resolutions to try
    Ns = np.hstack((np.arange(3, 6) * 32, np.arange(4, 12) * 64))
    # Solve the system to a given tolerance
    K2, vec, err = solver.iterate_solver(
        Ns, mode=mode, verbose=True, tol=1e-8, guess_tol=1e-3
    )
    # Plottting
    phi = np.arctan(vec[2].imag / vec[2].real)
    A = np.max(np.abs(vec))
    solver.keep_result(K2, vec * np.exp(-1j * phi)/A, mode=mode)
    axes[mode, 1].set_title(
        r"$K_n H = ${:1.4f}".format(np.sqrt(K2.real)), fontsize=10
    )
    zfine = np.linspace(-4, 4, 5000)
    axes[mode, 1].plot(zfine, grid.interpolate(zfine, system.result['G'].real))
    axes[mode, 1].plot(grid.zg, system.result['G'].real, '.')
    axes[mode, 1].set_xlim(-4, 4)
    axes[mode, 1].set_ylim(-1.3, 1.3)

    F = -1/(system.h*np.sqrt(K2.real))*grid.der(system.result['G'])
    axes[mode, 0].plot(grid.zg, F.real, '-')
    axes[mode, 0].set_xlim(-4, 4)
    axes[mode, 0].set_ylim(-3, 3)

axes[mode, 0].set_label(r'$z$')
axes[mode, 1].set_label(r'$z$')
plt.show()