def test_Mult_Div():

    SD = ShenDirichletBasis(N, "GC")
    SN = ShenNeumannBasis(N, "GC")
    SD.plan(N, 0, np.complex, {})
    SN.plan(N, 0, np.complex, {})

    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)

    #from IPython import embed; embed()
    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)
def test_Mult_Div():

    SD = ShenDirichletBasis(N, "GC")
    SN = ShenNeumannBasis(N, "GC")
    SD.plan(N, 0, np.complex, {})
    SN.plan(N, 0, np.complex, {})

    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)

    #from IPython import embed; embed()
    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)
Beispiel #3
0
def get_context():
    """Set up context for solver"""

    # Get points and weights for Chebyshev weighted integrals
    ST = ShenDirichletBasis(params.N[0],
                            quad=params.Dquad,
                            threads=params.threads,
                            planner_effort=params.planner_effort["dct"])
    SN = ShenNeumannBasis(params.N[0],
                          quad=params.Nquad,
                          threads=params.threads,
                          planner_effort=params.planner_effort["dct"])
    CT = ST.CT

    Nf = params.N[
        2] / 2 + 1  # Number of independent complex wavenumbers in z-direction
    Nu = params.N[0] - 2  # Number of velocity modes in Shen basis
    Nq = params.N[0] - 3  # Number of pressure modes in Shen basis
    u_slice = slice(0, Nu)
    p_slice = slice(1, Nu)

    FST = SlabShen_R2C(params.N,
                       params.L,
                       comm,
                       threads=params.threads,
                       communication=params.communication,
                       planner_effort=params.planner_effort,
                       dealias_cheb=params.dealias_cheb)

    float, complex, mpitype = datatypes("double")

    # Get grid for velocity points
    X = FST.get_local_mesh(ST)
    x0, x1, x2 = FST.get_mesh_dims(ST)

    U = zeros((3, ) + FST.real_shape(), dtype=float)
    U_hat = zeros((3, ) + FST.complex_shape(), dtype=complex)
    P = zeros(FST.real_shape(), dtype=float)
    P_hat = zeros(FST.complex_shape(), dtype=complex)
    Pcorr = zeros(FST.complex_shape(), dtype=complex)
    U0 = zeros((3, ) + FST.real_shape(), dtype=float)
    U_hat0 = zeros((3, ) + FST.complex_shape(), dtype=complex)
    U_hat1 = zeros((3, ) + FST.complex_shape(), dtype=complex)
    dU = zeros((3, ) + FST.complex_shape(), dtype=complex)
    H_hat = zeros((3, ) + FST.complex_shape(), dtype=complex)
    H_hat0 = zeros((3, ) + FST.complex_shape(), dtype=complex)
    H_hat1 = zeros((3, ) + FST.complex_shape(), dtype=complex)

    diff0 = zeros((3, ) + FST.complex_shape(), dtype=complex)
    Source = zeros((3, ) + FST.real_shape(), dtype=float)
    Sk = zeros((3, ) + FST.complex_shape(), dtype=complex)

    K = FST.get_local_wavenumbermesh(scaled=True)
    K2 = K[1] * K[1] + K[2] * K[2]
    K_over_K2 = zeros((3, ) + FST.complex_shape())
    for i in range(3):
        K_over_K2[i] = K[i] / np.where(K2 == 0, 1, K2)
    work = work_arrays()

    # Primary variable
    u = (U_hat, P_hat)

    nu, dt, N = params.nu, params.dt, params.N

    # Collect all linear algebra solvers
    la = config.AttributeDict(
        dict(HelmholtzSolverU=Helmholtz(N[0], np.sqrt(K2[0] + 2.0 / nu / dt),
                                        ST),
             HelmholtzSolverP=Helmholtz(N[0], np.sqrt(K2[0]), SN),
             TDMASolverD=TDMA(inner_product((ST, 0), (ST, 0))),
             TDMASolverN=TDMA(inner_product((SN, 0), (SN, 0)))))

    alfa = K2[0] - 2.0 / nu / dt

    # Collect all matrices
    kx = K[0][:, 0, 0]
    mat = config.AttributeDict(
        dict(CDN=inner_product((ST, 0), (SN, 1)),
             CND=inner_product((SN, 0), (ST, 1)),
             BDN=inner_product((ST, 0), (SN, 0)),
             CDD=inner_product((ST, 0), (ST, 1)),
             BDD=inner_product((ST, 0), (ST, 0)),
             BDT=inner_product((ST, 0), (CT, 0)),
             AB=HelmholtzCoeff(kx, -1.0, -alfa, ST.quad)))

    hdf5file = IPCSWriter({
        "U": U[0],
        "V": U[1],
        "W": U[2],
        "P": P
    },
                          chkpoint={
                              'current': {
                                  'U': U,
                                  'P': P
                              },
                              'previous': {
                                  'U': U0
                              }
                          },
                          filename=params.solver + ".h5",
                          mesh={
                              "x": x0,
                              "xp": FST.get_mesh_dim(SN, 0),
                              "y": x1,
                              "z": x2
                          })

    return config.AttributeDict(locals())