Ejemplo n.º 1
0
def test_tensor_prod_scalar(Nphi, Nr, basis, ncc_first, dealias, dtype):
    c, d, b, phi, r, x, y = basis(Nphi, Nr, dealias=dealias, dtype=dtype)

    f = field.Field(dist=d, bases=(b.radial_basis, ), dtype=dtype)
    g = field.Field(dist=d, bases=(b, ), dtype=dtype)

    f['g'] = r**4
    g['g'] = 3 * x**2 + 2 * y
    T = operators.Gradient(operators.Gradient(f, c), c).evaluate()

    vars = [g]
    if ncc_first:
        U0 = T * g
    else:
        U0 = g * T
    U1 = U0.reinitialize(ncc=True, ncc_vars=vars)

    problem = problems.LBVP(vars)
    problem.add_equation((f * g, 0))
    solver = solvers.LinearBoundaryValueSolver(
        problem,
        matsolver='SuperluNaturalSpsolve',
        matrix_coupling=[False, True])
    U1.store_ncc_matrices(vars, solver.subproblems)

    U0 = U0.evaluate()
    U0.change_scales(1)
    U1 = U1.evaluate_as_ncc()
    assert np.allclose(U0['g'], U1['g'])
Ejemplo n.º 2
0
def test_tensor_dot_vector(Nphi, Nr, basis, ncc_first, dealias, dtype):
    c, d, b, phi, r, x, y = basis(Nphi, Nr, dealias=dealias, dtype=dtype)

    f = field.Field(dist=d, bases=(b.radial_basis, ), dtype=dtype)
    g = field.Field(dist=d, bases=(b, ), dtype=dtype)

    f['g'] = r**4
    g['g'] = 3 * x**2 + 2 * y
    T = operators.Gradient(operators.Gradient(f, c), c).evaluate()
    u = operators.Gradient(g, c).evaluate()

    vars = [u]
    if ncc_first:
        w0 = dot(T, u)
    else:
        w0 = dot(u, T)
    w1 = w0.reinitialize(ncc=True, ncc_vars=vars)

    problem = problems.LBVP(vars)
    problem.add_equation((f * u, 0))
    solver = solvers.LinearBoundaryValueSolver(
        problem,
        matsolver='SuperluNaturalSpsolve',
        matrix_coupling=[False, True])
    w1.store_ncc_matrices(vars, solver.subproblems)

    w0 = w0.evaluate()
    w0.change_scales(1)
    w1 = w1.evaluate_as_ncc()
    assert np.allclose(w0['g'], w1['g'])
Ejemplo n.º 3
0
def test_multiply_tensor_tensor(Nphi, Ntheta, Nr, dealias, basis):
    c, d, b, phi, theta, r, x, y, z = basis(Nphi, Ntheta, Nr, dealias,
                                            np.complex128)
    f = field.Field(dist=d, bases=(b, ), dtype=np.complex128)
    f['g'] = x**3 + 2 * y**3 + 3 * z**3
    u = operators.Gradient(f, c).evaluate()
    T = operators.Gradient(u, c).evaluate()
    Q = (T * T).evaluate()
    Qg = T['g'][:, :, None, None, ...] * T['g'][None, None, ...]
    assert np.allclose(Q['g'], Qg)
Ejemplo n.º 4
0
def test_implicit_transpose_tensor(Nphi, Ntheta, Nr, k, dealias, dtype, basis):
    c, d, b, phi, theta, r, x, y, z = basis(Nphi, Ntheta, Nr, k, dealias,
                                            dtype)
    ct, st, cp, sp = np.cos(theta), np.sin(theta), np.cos(phi), np.sin(phi)
    u = field.Field(dist=d, bases=(b, ), tensorsig=(c, ), dtype=dtype)
    u.preset_scales(b.domain.dealias)
    u['g'][2] = r**2 * st * (2 * ct**2 * cp - r * ct**3 * sp +
                             r**3 * cp**3 * st**5 * sp**3 + r * ct * st**2 *
                             (cp**3 + sp**3))
    u['g'][1] = r**2 * (2 * ct**3 * cp - r * cp**3 * st**4 +
                        r**3 * ct * cp**3 * st**5 * sp**3 -
                        1 / 16 * r * np.sin(2 * theta)**2 *
                        (-7 * sp + np.sin(3 * phi)))
    u['g'][0] = r**2 * sp * (-2 * ct**2 + r * ct * cp * st**2 * sp -
                             r**3 * cp**2 * st**5 * sp**3)
    T = operators.Gradient(u, c).evaluate()
    Ttg = np.transpose(np.copy(T['g']), (1, 0, 2, 3, 4))
    Tt = field.Field(dist=d, bases=(b, ), tensorsig=(
        c,
        c,
    ), dtype=dtype)
    trans = lambda A: operators.TransposeComponents(A)
    problem = problems.LBVP([Tt])
    problem.add_equation((trans(Tt), T))
    # Solver
    solver = solvers.LinearBoundaryValueSolver(problem)
    solver.solve()
    assert np.allclose(Tt['g'], Ttg)
Ejemplo n.º 5
0
def test_interpolate_radius_tensor(Nphi, Nr, k, dealias, basis, dtype,
                                   r_interp):
    c, d, b, phi, r, x, y = basis(Nphi, Nr, k, dealias, dtype)
    f = field.Field(dist=d, bases=(b, ), dtype=dtype)
    f.preset_scales(b.domain.dealias)
    f['g'] = x**4 + 2 * y**4
    u = operators.Gradient(f, c)
    T = operators.Gradient(u, c)
    v = T(r=r_interp).evaluate()
    r = np.array([[r_interp]])
    x, y = c.cartesian(phi, r)
    ex = np.array([-np.sin(phi), np.cos(phi)])
    ey = np.array([np.cos(phi), np.sin(phi)])
    exex = ex[:, None, ...] * ex[None, ...]
    eyey = ey[:, None, ...] * ey[None, ...]
    vg = 12 * x**2 * exex + 24 * y**2 * eyey
    assert np.allclose(v['g'], vg)
Ejemplo n.º 6
0
def calculate2():
    op_dot = neg * operators.DotProduct(u, operators.Gradient(u, c))
    u_rhs.preset_layout(u_rhs.dist.grid_layout)
    u_rhs['g'] = op_dot.evaluate()['g']

    # R = ez cross u
    op = negOm * operators.CrossProduct(ez, u)
    u_rhs['g'] += op.evaluate()['g']
Ejemplo n.º 7
0
def test_divergence_radial_vector(Nr, dealias, basis, dtype):
    c, d, b, phi, theta, r, x, y, z = basis(1, 1, Nr, 1, dtype=dtype)
    f = field.Field(dist=d, bases=(b,), dtype=dtype)
    f.preset_scales(b.domain.dealias)
    f['g'] = r**4/3
    u = operators.Gradient(f, c)
    h = operators.Divergence(u).evaluate()
    hg = 20/3*r**2
    assert np.allclose(h['g'], hg)
Ejemplo n.º 8
0
def test_multiply_vector_vector(Nphi, Ntheta, Nr, dealias, basis):
    c, d, b, phi, theta, r, x, y, z = basis(Nphi, Ntheta, Nr, dealias,
                                            np.complex128)
    f = field.Field(dist=d, bases=(b, ), dtype=np.complex128)
    f['g'] = x**3 + 2 * y**3 + 3 * z**3
    u = operators.Gradient(f, c).evaluate()
    T = (u * u).evaluate()
    Tg = u['g'][None, ...] * u['g'][:, None, ...]
    assert np.allclose(T['g'], Tg)
Ejemplo n.º 9
0
def test_gradient_radial_scalar(Nr, dealias, basis, dtype):
    Nphi = 1
    c, d, b, phi, r, x, y = basis(Nphi, Nr, dealias, dtype)
    f = field.Field(dist=d, bases=(b, ), dtype=dtype)
    f.preset_scales(b.domain.dealias)
    f['g'] = r**4
    u = operators.Gradient(f, c).evaluate()
    ug = [0 * r * phi, 4 * r**3 + 0 * phi]
    assert np.allclose(u['g'], ug)
Ejemplo n.º 10
0
def test_divergence_vector(Nphi, Ntheta, Nr, dealias, basis, dtype):
    c, d, b, phi, theta, r, x, y, z = basis(Nphi, Ntheta, Nr, dealias, dtype)
    f = field.Field(dist=d, bases=(b,), dtype=dtype)
    f.preset_scales(b.domain.dealias)
    f['g'] = x**3 + 2*y**3 + 3*z**3
    u = operators.Gradient(f, c)
    h = operators.Divergence(u).evaluate()
    hg = 6*x + 12*y + 18*z
    assert np.allclose(h['g'], hg)
Ejemplo n.º 11
0
def test_ball_diffusion(Lmax, Nmax, Leig, radius, bc, dtype):
    # Bases
    c = coords.SphericalCoordinates('phi', 'theta', 'r')
    d = distributor.Distributor((c, ))
    b = basis.BallBasis(c, (2 * (Lmax + 1), Lmax + 1, Nmax + 1),
                        radius=radius,
                        dtype=dtype)
    b_S2 = b.S2_basis()
    phi, theta, r = b.local_grids((1, 1, 1))
    # Fields
    A = field.Field(dist=d, bases=(b, ), tensorsig=(c, ), dtype=dtype)
    φ = field.Field(dist=d, bases=(b, ), dtype=dtype)
    τ_A = field.Field(dist=d, bases=(b_S2, ), tensorsig=(c, ), dtype=dtype)
    λ = field.Field(name='λ', dist=d, dtype=dtype)
    # Parameters and operators
    div = lambda A: operators.Divergence(A)
    grad = lambda A: operators.Gradient(A, c)
    curl = lambda A: operators.Curl(A)
    lap = lambda A: operators.Laplacian(A, c)
    trans = lambda A: operators.TransposeComponents(A)
    radial = lambda A, index: operators.RadialComponent(A, index=index)
    angular = lambda A, index: operators.AngularComponent(A, index=index)
    Lift = lambda A: operators.Lift(A, b, -1)
    # Problem
    problem = problems.EVP([φ, A, τ_A], λ)
    problem.add_equation((div(A), 0))
    problem.add_equation((-λ * A + grad(φ) - lap(A) + Lift(τ_A), 0))
    if bc == 'no-slip':
        problem.add_equation((A(r=radius), 0))
    elif bc == 'stress-free':
        E = 1 / 2 * (grad(A) + trans(grad(A)))
        problem.add_equation((radial(A(r=radius), 0), 0))
        problem.add_equation((radial(angular(E(r=radius), 0), 1), 0))
    elif bc == 'potential':
        ell_func = lambda ell: ell + 1
        ell_1 = lambda A: operators.SphericalEllProduct(A, c, ell_func)
        problem.add_equation(
            (radial(grad(A)(r=radius), 0) + ell_1(A)(r=radius) / radius, 0))
    elif bc == 'conducting':
        problem.add_equation((φ(r=radius), 0))
        problem.add_equation((angular(A(r=radius), 0), 0))
    elif bc == 'pseudo':
        problem.add_equation((radial(A(r=radius), 0), 0))
        problem.add_equation((angular(curl(A)(r=radius), 0), 0))
    # Solver
    solver = solvers.EigenvalueSolver(problem)
    if not solver.subproblems[Leig].group[1] == Leig:
        raise ValueError("subproblems indexed in a strange way")
    solver.solve_dense(solver.subproblems[Leig])
    i_sort = np.argsort(solver.eigenvalues)
    solver.eigenvalues = solver.eigenvalues[i_sort]
    λ_analytic = analytic_eigenvalues(Leig, Nmax + 1, bc, r0=radius)
    if (bc == 'stress-free' and Leig == 1):
        # add null space solution
        λ_analytic = np.append(0, λ_analytic)
    assert np.allclose(solver.eigenvalues[:Nmax // 4], λ_analytic[:Nmax // 4])
Ejemplo n.º 12
0
def test_divergence_vector(Nphi, Nr, dealias, basis, dtype):
    c, d, b, phi, r, x, y = basis(Nphi, Nr, dealias, dtype)
    f = field.Field(dist=d, bases=(b, ), dtype=dtype)
    f.preset_scales(b.domain.dealias)
    f['g'] = 3 * x**4 + 2 * y * x
    grad = lambda A: operators.Gradient(A, c)
    div = lambda A: operators.Divergence(A)
    S = div(grad(f)).evaluate()
    Sg = 36 * x**2
    assert np.allclose(S['g'], Sg)
Ejemplo n.º 13
0
def test_gradient_radial_scalar(Nr, dealias, basis, dtype):
    Nphi = Ntheta = 1
    c, d, b, phi, theta, r, x, y, z = basis(Nphi, Ntheta, Nr, dealias, dtype)
    f = field.Field(dist=d, bases=(b,), dtype=dtype)
    f.preset_scales(b.domain.dealias)
    f['g'] = fg = r**4/3
    u = operators.Gradient(f, c).evaluate()
    ug = 0 * u['g']
    ug[2] = 4/3*r**3
    assert np.allclose(u['g'], ug)
Ejemplo n.º 14
0
def test_gradient_scalar(Nphi, Nr, dealias, basis, dtype):
    c, d, b, phi, r, x, y = basis(Nphi, Nr, dealias, dtype)
    f = field.Field(dist=d, bases=(b, ), dtype=dtype)
    f.preset_scales(b.domain.dealias)
    f['g'] = 3 * x**2 + 2 * y
    u = operators.Gradient(f, c).evaluate()
    ex = np.array([-np.sin(phi) + 0. * r, np.cos(phi) + 0. * r])
    ey = np.array([np.cos(phi) + 0. * r, np.sin(phi) + 0. * r])
    ug = 6 * x * ex + 2 * ey
    assert np.allclose(u['g'], ug)
Ejemplo n.º 15
0
def test_divergence_radial_vector(Nr, dealias, basis, dtype):
    Nphi = 1
    c, d, b, phi, r, x, y = basis(Nphi, Nr, dealias, dtype=dtype)
    f = field.Field(dist=d, bases=(b, ), dtype=dtype)
    f.preset_scales(b.domain.dealias)
    f['g'] = r**2
    grad = lambda A: operators.Gradient(A, c)
    div = lambda A: operators.Divergence(A)
    h = div(grad(f)).evaluate()
    hg = 4
    assert np.allclose(h['g'], hg)
Ejemplo n.º 16
0
def test_gradient_scalar(Nphi, Ntheta, Nr, dealias, basis, dtype):
    c, d, b, phi, theta, r, x, y, z = basis(Nphi, Ntheta, Nr, dealias, dtype)
    f = field.Field(dist=d, bases=(b,), dtype=dtype)
    f.preset_scales(b.domain.dealias)
    f['g'] = fg = 3*x**2 + 2*y*z
    u = operators.Gradient(f, c).evaluate()
    ug = 0 * u['g']
    ug[2] = (6*x**2+4*y*z)/r
    ug[1] = -2*(y**3+x**2*(y-3*z)-y*z**2)/(r**2*np.sin(theta))
    ug[0] = 2*x*(-3*y+z)/(r*np.sin(theta))
    assert np.allclose(u['g'], ug)
Ejemplo n.º 17
0
def test_divergence_tensor(Nphi, Nr, dealias, basis, dtype):
    c, d, b, phi, r, x, y = basis(Nphi, Nr, dealias, dtype)
    v = field.Field(dist=d, tensorsig=(c, ), bases=(b, ), dtype=dtype)
    v.preset_scales(b.domain.dealias)
    ex = np.array([-np.sin(phi) + 0. * r, np.cos(phi) + 0. * r])
    ey = np.array([np.cos(phi) + 0. * r, np.sin(phi) + 0. * r])
    v['g'] = 4 * x**3 * ey + 3 * y**2 * ey
    grad = lambda A: operators.Gradient(A, c)
    div = lambda A: operators.Divergence(A)
    U = div(grad(v)).evaluate()
    Ug = (24 * x + 6) * ey
    assert np.allclose(U['g'], Ug)
Ejemplo n.º 18
0
def test_explicit_trace_tensor(Nphi, Nr, k, dealias, basis, dtype, layout):
    c, d, b, phi, r, x, y = basis(Nphi, Nr, k, dealias, dtype)
    u = field.Field(dist=d, bases=(b, ), tensorsig=(c, ), dtype=dtype)
    u.preset_scales(b.domain.dealias)
    ex = np.array([-np.sin(phi) + 0. * r, np.cos(phi) + 0. * r])
    ey = np.array([np.cos(phi) + 0. * r, np.sin(phi) + 0. * r])
    u['g'] = 4 * x**3 * ey + 3 * y**2 * ey
    T = operators.Gradient(u, c).evaluate()
    fg = T['g'][0, 0] + T['g'][1, 1]
    T.change_layout(layout)
    f = operators.Trace(T).evaluate()
    assert np.allclose(f['g'], fg)
Ejemplo n.º 19
0
def test_multiply_scalar_vector(Nphi, Ntheta, Nr, dealias, basis):
    c, d, b, phi, theta, r, x, y, z = basis(Nphi, Ntheta, Nr, dealias,
                                            np.complex128)
    phi, theta, r = b.local_grids(b.domain.dealias)
    x, y, z = c.cartesian(phi, theta, r)
    f = field.Field(dist=d, bases=(b, ), dtype=np.complex128)
    f.preset_scales(b.domain.dealias)
    f['g'] = x**3 + 2 * y**3 + 3 * z**3
    u = operators.Gradient(f, c).evaluate()
    v = (f * u).evaluate()
    vg = f['g'][None, ...] * u['g']
    assert np.allclose(v['g'], vg)
Ejemplo n.º 20
0
def test_gradient_radial_vector(Nr, dealias, basis, dtype):
    Nphi = Ntheta = 1
    c, d, b, phi, theta, r, x, y, z = basis(Nphi, Ntheta, Nr, dealias, dtype=dtype)
    f = field.Field(dist=d, bases=(b,), dtype=dtype)
    f.preset_scales(b.domain.dealias)
    f['g'] = r**4 / 3
    grad = lambda A: operators.Gradient(A, c)
    T = grad(grad(f)).evaluate()
    Tg = 0 * T['g']
    Tg[0,0] = 4 / 3 * r**2
    Tg[1,1] = 4 / 3 * r**2
    Tg[2,2] = 4 * r**2
    assert np.allclose(T['g'], Tg)
Ejemplo n.º 21
0
def calculate1():
    Du.preset_layout(Du.dist.coeff_layout)
    Du['c'] = 0
    op = operators.Gradient(u, c)
    op.out = Du
    op.evaluate()
    op_dot = neg * operators.DotProduct(u, Du)
    u_rhs.preset_layout(u_rhs.dist.grid_layout)
    u_rhs['g'] = op_dot.evaluate()['g']

    # R = ez cross u
    op = negOm * operators.CrossProduct(ez, u)
    u_rhs['g'] += op.evaluate()['g']
Ejemplo n.º 22
0
def test_interpolate_radius_vector(Nphi, Nr, k, dealias, basis, dtype,
                                   r_interp):
    c, d, b, phi, r, x, y = basis(Nphi, Nr, k, dealias, dtype)
    f = field.Field(dist=d, bases=(b, ), dtype=dtype)
    f.preset_scales(b.domain.dealias)
    f['g'] = x**4 + 2 * y**4
    u = operators.Gradient(f, c)
    v = u(r=r_interp).evaluate()
    r = np.array([[r_interp]])
    x, y = c.cartesian(phi, r)
    ex = np.array([-np.sin(phi), np.cos(phi)])
    ey = np.array([np.cos(phi), np.sin(phi)])
    vg = 4 * x**3 * ex + 8 * y**3 * ey
    assert np.allclose(v['g'], vg)
Ejemplo n.º 23
0
def test_gradient_radial_vector(Nr, dealias, basis, dtype):
    Nphi = 1
    c, d, b, phi, r, x, y = basis(Nphi, Nr, dealias, dtype)
    f = field.Field(dist=d, bases=(b, ), dtype=dtype)
    f.preset_scales(b.domain.dealias)
    f['g'] = r**4
    grad = lambda A: operators.Gradient(A, c)
    T = grad(grad(f)).evaluate()
    er = np.array([[[0]], [[1]]])
    ephi = np.array([[[1]], [[0]]])
    erer = er[:, None, ...] * er[None, ...]
    ephiephi = ephi[:, None, ...] * ephi[None, ...]
    Tg = 12 * r**2 * erer + 4 * r**2 * ephiephi
    assert np.allclose(T['g'], Tg)
Ejemplo n.º 24
0
def test_dot_product_vector_vector(Nphi, Ntheta, Nr, dealias, basis):
    c, d, b, phi, theta, r, x, y, z = basis(Nphi, Ntheta, Nr, dealias,
                                            np.complex128)
    f = field.Field(dist=d, bases=(b, ), dtype=np.complex128)
    f['g'] = z
    ez = operators.Gradient(f, c).evaluate()
    u = field.Field(dist=d, bases=(b, ), tensorsig=(c, ), dtype=np.complex128)
    u['g'][2] = (6 * x**2 + 4 * y * z) / r
    u['g'][1] = -2 * (y**3 + x**2 *
                      (y - 3 * z) - y * z**2) / (r**2 * np.sin(theta))
    u['g'][0] = 2 * x * (-3 * y + z) / (r * np.sin(theta))
    h = arithmetic.DotProduct(ez, u).evaluate()
    hg = np.sum(ez['g'] * u['g'], axis=0)
    assert np.allclose(h['g'], hg)
Ejemplo n.º 25
0
def test_tensor_dot_tensor(Nphi, Nr, basis, ncc_first, dealias, dtype):
    c, d, b, phi, r, x, y = basis(Nphi, Nr, dealias=dealias, dtype=dtype)

    f = field.Field(dist=d, bases=(b.radial_basis, ), dtype=dtype)
    f['g'] = r**6
    U = operators.Gradient(operators.Gradient(f, c), c).evaluate()

    T = field.Field(dist=d, bases=(b, ), tensorsig=(
        c,
        c,
    ), dtype=dtype)
    z = 0
    theta = np.pi / 2.
    T['g'][1, 1] = (6 * x**2 + 4 * y * z) / r**2
    T['g'][1, 0] = T['g'][0, 1] = 2 * x * (z - 3 * y) / (r**2 * np.sin(theta))
    T['g'][0, 0] = 6 * y**2 / (x**2 + y**2)

    vars = [T]
    if ncc_first:
        W0 = dot(T, U)
    else:
        W0 = dot(U, T)
    W1 = W0.reinitialize(ncc=True, ncc_vars=vars)

    problem = problems.LBVP(vars)
    problem.add_equation((f * T, 0))
    solver = solvers.LinearBoundaryValueSolver(
        problem,
        matsolver='SuperluNaturalSpsolve',
        matrix_coupling=[False, True])
    W1.store_ncc_matrices(vars, solver.subproblems)

    W0 = W0.evaluate()
    W0.change_scales(1)
    W1 = W1.evaluate_as_ncc()
    assert np.allclose(W0['g'], W1['g'])
Ejemplo n.º 26
0
def test_gradient_vector(Nphi, Ntheta, Nr, dealias, basis, dtype):
    c, d, b, phi, theta, r, x, y, z = basis(Nphi, Ntheta, Nr, dealias, dtype)
    f = field.Field(dist=d, bases=(b,), dtype=dtype)
    f.preset_scales(b.domain.dealias)
    f['g'] = 3*x**2 + 2*y*z
    grad = lambda A: operators.Gradient(A, c)
    T = grad(grad(f)).evaluate()
    Tg = 0 * T['g']
    Tg[2,2] = (6*x**2+4*y*z)/r**2
    Tg[2,1] = Tg[1,2] = -2*(y**3+x**2*(y-3*z)-y*z**2)/(r**3*np.sin(theta))
    Tg[2,0] = Tg[0,2] = 2*x*(z-3*y)/(r**2*np.sin(theta))
    Tg[1,1] = 6*x**2/(r**2*np.sin(theta)**2) - (6*x**2+4*y*z)/r**2
    Tg[1,0] = Tg[0,1] = -2*x*(x**2+y**2+3*y*z)/(r**3*np.sin(theta)**2)
    Tg[0,0] = 6*y**2/(x**2+y**2)
    assert np.allclose(T['g'], Tg)
Ejemplo n.º 27
0
def test_gradient_vector(Nphi, Nr, dealias, basis, dtype):
    c, d, b, phi, r, x, y = basis(Nphi, Nr, dealias, dtype)
    f = field.Field(dist=d, bases=(b, ), dtype=dtype)
    f.preset_scales(b.domain.dealias)
    f['g'] = 3 * x**4 + 2 * y * x
    grad = lambda A: operators.Gradient(A, c)
    T = grad(grad(f)).evaluate()
    ex = np.array([-np.sin(phi) + 0. * r, np.cos(phi) + 0. * r])
    ey = np.array([np.cos(phi) + 0. * r, np.sin(phi) + 0. * r])
    exex = ex[:, None, ...] * ex[None, ...]
    eyex = ey[:, None, ...] * ex[None, ...]
    exey = ex[:, None, ...] * ey[None, ...]
    eyey = ey[:, None, ...] * ey[None, ...]
    Tg = 36 * x**2 * exex + 2 * (exey + eyex)
    assert np.allclose(T['g'], Tg)
Ejemplo n.º 28
0
def test_cross_product(Nphi, Ntheta, Nr, dealias, basis):
    c, d, b, phi, theta, r, x, y, z = basis(Nphi, Ntheta, Nr, dealias,
                                            np.complex128)
    f = field.Field(dist=d, bases=(b, ), dtype=np.complex128)
    f['g'] = z
    ez = operators.Gradient(f, c).evaluate()
    u = field.Field(dist=d, bases=(b, ), tensorsig=(c, ), dtype=np.complex128)
    u['g'][2] = (6 * x**2 + 4 * y * z) / r
    u['g'][1] = -2 * (y**3 + x**2 *
                      (y - 3 * z) - y * z**2) / (r**2 * np.sin(theta))
    u['g'][0] = 2 * x * (-3 * y + z) / (r * np.sin(theta))
    h = arithmetic.CrossProduct(ez, u).evaluate()
    hg = np.zeros(h['g'].shape, dtype=h['g'].dtype)
    hg[0] = -ez['g'][1] * u['g'][2] + ez['g'][2] * u['g'][1]
    hg[1] = -ez['g'][2] * u['g'][0] + ez['g'][0] * u['g'][2]
    hg[2] = -ez['g'][0] * u['g'][1] + ez['g'][1] * u['g'][0]
    assert np.allclose(h['g'], hg)
Ejemplo n.º 29
0
def test_disk_AdvectiveCFL(Nr, Nphi, timestepper, dtype, dealias):
    radius = 2
    # Bases
    c, d, db, phi, r, x, y = build_disk(Nphi,
                                        Nr,
                                        radius,
                                        dealias,
                                        dtype=dtype,
                                        grid_scale=1)
    # Fields
    f = field.Field(name='f', dist=d, bases=(db, ), dtype=dtype)
    f['g'] = x * y
    u = operators.Gradient(f, c).evaluate()
    # AdvectiveCFL initialization
    cfl = operators.AdvectiveCFL(u, c)
    cfl_freq = cfl.evaluate()['g']
    comparison_freq = np.abs(u['g'][0]) / cfl.cfl_spacing()[0]
    comparison_freq += np.abs(u['g'][1]) / cfl.cfl_spacing()[1]
    assert np.allclose(cfl_freq, comparison_freq)
Ejemplo n.º 30
0
def test_spherical_shell_AdvectiveCFL(Lmax, Nmax, timestepper, dtype, dealias):
    radii = (0.5, 2)
    # Bases
    c, d, b, phi, theta, r, x, y, z = build_shell(2 * (Lmax + 1), (Lmax + 1),
                                                  (Nmax + 1),
                                                  radii,
                                                  dealias,
                                                  dtype=dtype,
                                                  grid_scale=1)
    # Fields
    f = field.Field(name='f', dist=d, bases=(b, ), dtype=dtype)
    f['g'] = x * y * z
    u = operators.Gradient(f, c).evaluate()
    # AdvectiveCFL initialization
    cfl = operators.AdvectiveCFL(u, c)
    cfl_freq = cfl.evaluate()['g']
    comparison_freq = np.sqrt(u['g'][0]**2 +
                              u['g'][1]**2) / cfl.cfl_spacing()[0]
    comparison_freq += np.abs(u['g'][2]) / cfl.cfl_spacing()[1]
    assert np.allclose(cfl_freq, comparison_freq)