Esempio n. 1
0
v = TestFunction(ST)

# Get f on quad points and exact solution
fj = Array(ST, buffer=fe)
uj = Array(ST, buffer=ue)

# Compute right hand side
f_hat = Function(ST)
f_hat = inner(v, fj, output_array=f_hat)

# Solve Poisson equation
A = inner(grad(v), grad(u))
u_hat = Function(ST)
u_hat = A.solve(-f_hat, u_hat)

uq = ST.backward(u_hat)
u_hat = ST.forward(uq, u_hat, fast_transform=False)
uq = ST.backward(u_hat, uq, fast_transform=False)

assert np.allclose(uj, uq)

point = np.array([0.1, 0.2])
p = ST.eval(point, u_hat)
assert np.allclose(p, lambdify(x, ue)(point))

if 'pytest' not in os.environ:
    import matplotlib.pyplot as plt
    plt.figure()
    X = ST.mesh()
    plt.plot(X, uj.real)
    plt.title("U")
Esempio n. 2
0
def test_Mult_Div():

    #SD = ShenDirichlet(N, "GC")
    #SN = ShenNeumann(N, "GC")
    #SD.plan(N, 0, np.complex, {})
    #SN.plan(N, 0, np.complex, {})
    SD = FunctionSpace(N, 'C', bc=(0, 0), dtype='D')
    SN = FunctionSpace(N, 'C', bc='Neumann', dtype='D')

    Cm = inner_product((SN, 0), (SD, 1))
    Bm = inner_product((SN, 0), (SD, 0))

    uk = np.random.randn((N)) + np.random.randn((N)) * 1j
    vk = np.random.randn((N)) + np.random.randn((N)) * 1j
    wk = np.random.randn((N)) + np.random.randn((N)) * 1j

    b = np.zeros(N, dtype=np.complex)
    uk0 = np.zeros(N, dtype=np.complex)
    vk0 = np.zeros(N, dtype=np.complex)
    wk0 = np.zeros(N, dtype=np.complex)

    uk0 = SD.forward(uk, uk0)
    uk = SD.backward(uk0, uk)
    uk0 = SD.forward(uk, uk0)
    vk0 = SD.forward(vk, vk0)
    vk = SD.backward(vk0, vk)
    vk0 = SD.forward(vk, vk0)
    wk0 = SD.forward(wk, wk0)
    wk = SD.backward(wk0, wk)
    wk0 = SD.forward(wk, wk0)

    LUsolve.Mult_Div_1D(N, 7, 7, uk0[:N - 2], vk0[:N - 2], wk0[:N - 2],
                        b[1:N - 2])

    uu = np.zeros_like(uk0)
    v0 = np.zeros_like(vk0)
    w0 = np.zeros_like(wk0)
    uu = Cm.matvec(uk0, uu)
    uu += 1j * 7 * Bm.matvec(vk0, v0) + 1j * 7 * Bm.matvec(wk0, w0)

    assert np.allclose(uu, b)

    uk0 = uk0.repeat(4 * 4).reshape(
        (N, 4, 4)) + 1j * uk0.repeat(4 * 4).reshape((N, 4, 4))
    vk0 = vk0.repeat(4 * 4).reshape(
        (N, 4, 4)) + 1j * vk0.repeat(4 * 4).reshape((N, 4, 4))
    wk0 = wk0.repeat(4 * 4).reshape(
        (N, 4, 4)) + 1j * wk0.repeat(4 * 4).reshape((N, 4, 4))
    b = np.zeros((N, 4, 4), dtype=np.complex)
    m = np.zeros((4, 4)) + 7
    n = np.zeros((4, 4)) + 7
    LUsolve.Mult_Div_3D(N, m, n, uk0[:N - 2], vk0[:N - 2], wk0[:N - 2],
                        b[1:N - 2])

    uu = np.zeros_like(uk0)
    v0 = np.zeros_like(vk0)
    w0 = np.zeros_like(wk0)
    uu = Cm.matvec(uk0, uu)
    uu += 1j * 7 * Bm.matvec(vk0, v0) + 1j * 7 * Bm.matvec(wk0, w0)

    assert np.allclose(uu, b)
Esempio n. 3
0
fj = Array(SD, buffer=fe)

# Compute right hand side of Poisson equation
f_hat = Function(SD, buffer=inner(v, fj))

# Get left hand side of Poisson equation
A = inner(v, div(grad(u)))
B = extract_bc_matrices([A])[0]
A = A[0]
u_hat = Function(SD).set_boundary_dofs()
f_hat -= B.matvec(u_hat, Function(SD))
u_hat = A.solve(f_hat, u_hat)

# Solve and transform to real space
u = np.zeros(N)  # Solution real space
u = SD.backward(u_hat, u)

# Compare with analytical solution
uj = Array(SD, buffer=ue)
print(abs(uj - u).max())
assert np.allclose(uj, u)

if 'pytest' not in os.environ:
    import matplotlib.pyplot as plt
    plt.figure()
    X = SD.mesh()
    plt.plot(X, u)

    plt.figure()
    plt.plot(X, uj)
Esempio n. 4
0
# Size of discretization
N = int(sys.argv[-2])

SD = FunctionSpace(N, family=family, bc=(0, 0))
u = TrialFunction(SD)
v = TestFunction(SD)

# Get f on quad points
fj = Array(SD, buffer=fe)

# Compute right hand side of Poisson equation
f_hat = Array(SD)
f_hat = inner(v, fj, output_array=f_hat)

# Get left hand side of Poisson equation
A = inner(v, -div(grad(u)))
B = inner(v, alfa * u)

H = Solver(A, B)
u_hat = Function(SD)  # Solution spectral space
u_hat = H(u_hat, f_hat)
uj = SD.backward(u_hat)

# Compare with analytical solution
ua = Array(SD, buffer=ue)

error = dx((uj - ua)**2)
print('Error=%2.6e' % (np.sqrt(error)))
assert np.allclose(uj, ua)
Esempio n. 5
0
u_hat = Function(SD).set_boundary_dofs()

# Some work required for inhomogeneous boundary conditions only
if SD.has_nonhomogeneous_bcs:
    bc_mats = extract_bc_matrices([matrices])

    w0 = np.zeros_like(u_hat)
    for m in bc_mats:
        f_hat -= m.matvec(u_hat, w0)

# Create linear algebra solver
H = Solver(*matrices)
u_hat = H(u_hat, f_hat)

uj = Array(SD)
uj = SD.backward(u_hat, uj)
uh = uj.forward()

# Compare with analytical solution
uq = Array(SD, buffer=ue)
print("Error=%2.16e" % (np.linalg.norm(uj - uq)))
assert np.linalg.norm(uj - uq) < 1e-8

if 'pytest' not in os.environ:
    import matplotlib.pyplot as plt
    plt.figure()
    plt.plot(X, uq)

    plt.figure()
    plt.plot(X, uj)
Esempio n. 6
0
def main(N, dt=0.005, end_time=2, dealias_initial=True, plot_result=False):
    SD = FunctionSpace(N, 'F', dtype='D')
    X = SD.points_and_weights()[0]
    v = TestFunction(SD)
    U = Array(SD)
    dU = Function(SD)
    U_hat = Function(SD)
    U_hat0 = Function(SD)
    U_hat1 = Function(SD)
    w0 = Function(SD)
    a = [1. / 6., 1. / 3., 1. / 3., 1. / 6.]  # Runge-Kutta parameter
    b = [0.5, 0.5, 1.]  # Runge-Kutta parameter

    nu = 0.2
    k = SD.wavenumbers().astype(float)

    # initialize
    U[:] = 3. / (5. - 4. * np.cos(X))
    if not dealias_initial:
        U_hat = SD.forward(U, U_hat)
    else:
        U_hat[:] = 2**(-abs(k))

    def compute_rhs(rhs, u_hat, w0):
        rhs.fill(0)
        w0.fill(0)
        rhs = inner(v, nu * div(grad(u_hat)), output_array=rhs)
        rhs -= inner(v, grad(u_hat), output_array=w0)
        return rhs

    # Integrate using a 4th order Rung-Kutta method
    t = 0.0
    tstep = 0
    if plot_result is True:
        im = plt.figure()
        ca = im.gca()
        ca.plot(X, U.real, 'b')
        plt.draw()
        plt.pause(1e-6)
    while t < end_time - 1e-8:
        t += dt
        tstep += 1
        U_hat1[:] = U_hat0[:] = U_hat
        for rk in range(4):
            dU = compute_rhs(dU, U_hat, w0)
            if rk < 3:
                U_hat[:] = U_hat0 + b[rk] * dt * dU
            U_hat1 += a[rk] * dt * dU
        U_hat[:] = U_hat1

        if tstep % (200) == 0 and plot_result is True:
            ca.plot(X, U.real)
            plt.pause(1e-6)
            #plt.savefig('Ginzburg_Landau_pad_{}_real_{}.png'.format(N[0], int(np.round(t))))

    U = SD.backward(U_hat, U)
    Ue = np.zeros_like(U)
    for k in range(-100, 101):
        Ue += 2**(-abs(k)) * np.exp(1j * k * (X - t) - nu * k**2 * t)

    err = np.sqrt(2 * np.pi / N * np.linalg.norm(U - Ue.real)**2)
    return 1. / N, err
Esempio n. 7
0
v = TestFunction(SD)

# Get f on quad points
fj = Array(SD, buffer=fe)

# Compute right hand side of Poisson equation
f_hat = Function(SD, buffer=inner(v, fj))

# Get left hand side of Poisson equation
A = inner(v, div(grad(u)))

f_hat = A.solve(f_hat)

# Solve and transform to real space
u = np.zeros(N)               # Solution real space
u = SD.backward(f_hat, u)

# Compare with analytical solution
uj = Array(SD, buffer=ue)
print(abs(uj-u).max())
assert np.allclose(uj, u)

if 'pytest' not in os.environ:
    import matplotlib.pyplot as plt
    plt.figure()
    X = SD.mesh()
    plt.plot(X, u)

    plt.figure()
    plt.plot(X, uj)