Пример #1
0
def test_vector_heat_disk_dirichlet(Nr, Nphi, dtype):
    # Bases
    dealias = 1
    c, d, b, phi, r, x, y = build_disk(Nphi, Nr, dealias=dealias, dtype=dtype)
    # Fields
    u = field.Field(name='u',
                    dist=d,
                    bases=(b, ),
                    tensorsig=(c, ),
                    dtype=dtype)
    τu = field.Field(name='u',
                     dist=d,
                     bases=(b.S1_basis(), ),
                     tensorsig=(c, ),
                     dtype=dtype)
    v = field.Field(name='u',
                    dist=d,
                    bases=(b, ),
                    tensorsig=(c, ),
                    dtype=dtype)
    ex = np.array([-np.sin(phi), np.cos(phi)])
    ey = np.array([np.cos(phi), np.sin(phi)])
    v['g'] = (x + 4 * y) * ex
    vr = operators.RadialComponent(v(r=radius_disk))
    vph = operators.AzimuthalComponent(v(r=radius_disk))
    # Problem
    Lap = lambda A: operators.Laplacian(A, c)
    Lift = lambda A: operators.Lift(A, b, -1)
    problem = problems.LBVP([u, τu])
    problem.add_equation((Lap(u) + Lift(τu), 0))
    problem.add_equation((u(r=radius_disk), v(r=radius_disk)))
    # Solver
    solver = solvers.LinearBoundaryValueSolver(problem)
    solver.solve()
    assert np.allclose(u['g'], v['g'])
Пример #2
0
def test_radial_component_tensor(Nphi, Ntheta, Nr, k, dealias, dtype, basis,
                                 radius):
    c, d, b, phi, theta, r, x, y, z = basis(Nphi, Ntheta, Nr, k, dealias,
                                            dtype)
    T = field.Field(dist=d, bases=(b, ), tensorsig=(c, c), dtype=dtype)
    T.preset_scales(b.domain.dealias)
    T['g'][2, 2] = (6 * x**2 + 4 * y * z) / r**2
    T['g'][2, 1] = T['g'][1, 2] = -2 * (y**3 + x**2 * (y - 3 * z) -
                                        y * z**2) / (r**3 * np.sin(theta))
    T['g'][2, 0] = T['g'][0, 2] = 2 * x * (z - 3 * y) / (r**2 * np.sin(theta))
    T['g'][1, 1] = 6 * x**2 / (r**2 * np.sin(theta)**2) - (6 * x**2 +
                                                           4 * y * z) / r**2
    T['g'][1, 0] = T['g'][0,
                          1] = -2 * x * (x**2 + y**2 +
                                         3 * y * z) / (r**3 * np.sin(theta)**2)
    T['g'][0, 0] = 6 * y**2 / (x**2 + y**2)
    A = operators.RadialComponent(operators.interpolate(T,
                                                        r=radius)).evaluate()
    Ag = 0 * A['g']
    Ag[2] = 2 * np.sin(theta) * (3 * np.cos(phi)**2 * np.sin(theta) +
                                 2 * np.cos(theta) * np.sin(phi))
    Ag[1] = 6 * np.cos(theta) * np.cos(phi)**2 * np.sin(theta) + 2 * np.cos(
        2 * theta) * np.sin(phi)
    Ag[0] = 2 * np.cos(phi) * (np.cos(theta) - 3 * np.sin(theta) * np.sin(phi))
    assert np.allclose(A['g'], Ag)
Пример #3
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])
Пример #4
0
def test_radial_component_vector(Nphi, Nr, k, dealias, dtype, basis, radius):
    c, d, b, phi, r, x, y = basis(Nphi, Nr, k, dealias, dtype)
    cp, sp = np.cos(phi), np.sin(phi)
    u = field.Field(dist=d, bases=(b, ), tensorsig=(c, ), dtype=dtype)
    u.preset_scales(b.domain.dealias)
    ex = np.array([-np.sin(phi), np.cos(phi)])
    ey = np.array([np.cos(phi), np.sin(phi)])
    u['g'] = (x**2 * y - 2 * x * y**5) * ex + (x**2 * y + 7 * x**3 * y**2) * ey
    v = operators.RadialComponent(operators.interpolate(u,
                                                        r=radius)).evaluate()
    vg = (radius**3 * cp**2 * sp - 2 * radius**6 * cp * sp**5) * cp + (
        radius**3 * cp**2 * sp + 7 * radius**5 * cp**3 * sp**2) * sp
    assert np.allclose(v['g'], vg)
Пример #5
0
def test_radial_component_tensor(Nphi, Nr, k, dealias, dtype, basis, radius):
    c, d, b, phi, r, x, y = basis(Nphi, Nr, k, dealias, dtype)
    cp, sp = np.cos(phi), np.sin(phi)
    T = field.Field(dist=d, bases=(b, ), tensorsig=(c, c), dtype=dtype)
    T.preset_scales(b.domain.dealias)
    ex = np.array([-np.sin(phi), np.cos(phi)])
    ey = np.array([np.cos(phi), np.sin(phi)])
    exex = ex[:, None, ...] * ex[None, ...]
    exey = ex[:, None, ...] * ey[None, ...]
    eyex = ey[:, None, ...] * ex[None, ...]
    eyey = ey[:, None, ...] * ey[None, ...]
    T['g'] = (3 * x**2 + y) * exex + y**3 * exey + x**2 * y**2 * eyex + (
        y**5 - 2 * x * y) * eyey
    A = operators.RadialComponent(operators.interpolate(T,
                                                        r=radius)).evaluate()
    Ag = (
        3 * radius**2 * cp**2 + radius * sp
    ) * cp * ex + radius**3 * sp**3 * cp * ey + radius**4 * cp**2 * sp**2 * sp * ex + (
        radius**5 * sp**5 - 2 * radius**2 * cp * sp) * sp * ey
    assert np.allclose(A['g'], Ag)
Пример #6
0
def test_radial_component_vector(Nphi, Ntheta, Nr, k, dealias, dtype, basis,
                                 radius):
    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)
    v = operators.RadialComponent(operators.interpolate(u,
                                                        r=radius)).evaluate()
    vg = radius**2 * st * (2 * ct**2 * cp - radius * ct**3 * sp + radius**3 *
                           cp**3 * st**5 * sp**3 + radius * ct * st**2 *
                           (cp**3 + sp**3))
    assert np.allclose(v['g'], vg)
Пример #7
0
B['g'][1] = -3. / 2. * r * (-1 + 4 * r**2 - 6 * r**4 +
                            3 * r**6) * (np.cos(phi) + np.sin(phi))
B['g'][0] = -3./4.*r*(-1+r**2)*np.cos(theta)* \
                 ( 3*r*(2-5*r**2+4*r**4)*np.sin(theta)
                  +2*(1-3*r**2+3*r**4)*(np.cos(phi)-np.sin(phi)))

# Potential BC on A
ell_func = lambda ell: ell + 1

# Parameters and operators
div = lambda A: operators.Divergence(A)
grad = lambda A: operators.Gradient(A, c)
curl = lambda A: operators.Curl(A)
LiftTau = lambda A: operators.LiftTau(A, b, -1)
ellp1 = lambda A: operators.SphericalEllProduct(A, c, ell_func)
radial = lambda A: operators.RadialComponent(A)
angular = lambda A: operators.AngularComponent(A, index=1)

# BVP for initial A
BVP = problems.LBVP([φ, A, τ_A, τ_φ])
#BVP.add_equation((angular(τ_A),0))
BVP.add_equation((div(A) + LiftTau(τ_φ), 0))
BVP.add_equation((curl(A) + grad(φ) + LiftTau(τ_A), B))
BVP.add_equation((radial(grad(A)(r=radius)) + ellp1(A)(r=radius) / radius,
                  0))  #, condition = "ntheta != 0")
BVP.add_equation((φ(r=radius), 0))  #, condition = "ntheta == 0")
solver = solvers.LinearBoundaryValueSolver(BVP)
solver.solve()

plot_matrices = False
Пример #8
0
     (147 - 343 * r**2 + 217 * r**4 - 29 * r**6) * np.sin(phi)))
u['g'][1] = -10 * r**2 / 7 / np.sqrt(3) * np.cos(theta) * (
    3 * (-147 + 343 * r**2 - 217 * r**4 + 29 * r**6) * np.cos(phi) + 14 *
    (-9 - 125 * r**2 + 39 * r**4 + 27 * r**6) * np.sin(phi))

T_source = field.Field(dist=d, bases=(b, ), dtype=dtype)
T_source['g'] = Source

# initial toroidal magnetic field
# B['g'][0] = -3./4.*r*(-1+r**2)*np.cos(theta)* \
#                  ( 3*r*(2-5*r**2+4*r**4)*np.sin(theta)
#                   +2*(1-3*r**2+3*r**4)*(np.cos(phi)-np.sin(phi)))
# B['g'][1] = -3./2.*r*(-1+4*r**2-6*r**4+3*r**6)*(np.cos(phi)+np.sin(phi))

# Boundary conditions
u_r_bc = operators.RadialComponent(operators.interpolate(u, r=1))

stress = operators.Gradient(u, c) + operators.TransposeComponents(
    operators.Gradient(u, c))
u_perp_bc = operators.RadialComponent(
    operators.AngularComponent(operators.interpolate(stress, r=1), index=1))

# Potential BC on B
r_out = 1
ell_func = lambda ell: ell + 1
A_potential_bc = operators.RadialComponent(
    operators.interpolate(operators.Gradient(
        A, c), r=1)) + operators.interpolate(
            operators.SphericalEllProduct(A, c, ell_func), r=1) / r_out

# Parameters and operators
tau_T = field.Field(dist=d, bases=(b_S2, ), dtype=np.complex128)

r_vec = field.Field(dist=d, bases=(b, ), tensorsig=(c, ), dtype=np.complex128)
r_vec['g'][2] = r

T['g'] = 0.5 * (1 - r**2) + 0.1 / 8 * np.sqrt(35 / np.pi) * r**3 * (
    1 - r**2) * (np.cos(3 * phi) + np.sin(3 * phi)) * np.sin(theta)**3

T_source = field.Field(dist=d, bases=(b, ), dtype=np.complex128)
T_source['g'] = 3

# Potential BC on u
r_out = 1
ell_func = lambda ell: ell + 1
u_potential_bc = operators.RadialComponent(
    operators.interpolate(operators.Gradient(
        u, c), r=1)) + operators.interpolate(
            operators.SphericalEllProduct(u, c, ell_func), r=1) / r_out

# Parameters and operators
ez = field.Field(dist=d, bases=(b, ), tensorsig=(c, ), dtype=np.complex128)
ez['g'][1] = -np.sin(theta)
ez['g'][2] = np.cos(theta)
div = lambda A: operators.Divergence(A, index=0)
lap = lambda A: operators.Laplacian(A, c)
grad = lambda A: operators.Gradient(A, c)
dot = lambda A, B: arithmetic.DotProduct(A, B)
cross = lambda A, B: arithmetic.CrossProduct(A, B)
ddt = lambda A: operators.TimeDerivative(A)
LiftTau = lambda A: operators.LiftTau(A, b, -1)
Пример #10
0
B['g'][1] = 5 / 8 * (9 * r - 8 * r_outer - r_inner**4 / r**3) * np.sin(theta)
B['g'][2] = 5 / 8 * (8 * r_outer - 6 * r -
                     2 * r_inner**4 / r**3) * np.cos(theta)

# Parameters and operators
div = lambda A: operators.Divergence(A, index=0)
lap = lambda A: operators.Laplacian(A, c)
grad = lambda A: operators.Gradient(A, c)
dot = lambda A, B: arithmetic.DotProduct(A, B)
cross = lambda A, B: arithmetic.CrossProduct(A, B)
ddt = lambda A: operators.TimeDerivative(A)
curl = lambda A: operators.Curl(A)

ell_func = lambda ell: ell + 1
A_potential_bc_outer = operators.RadialComponent(
    operators.interpolate(operators.Gradient(
        A, c), r=r_outer)) + operators.interpolate(
            operators.SphericalEllProduct(A, c, ell_func), r=r_outer) / r_outer
A_potential_bc_inner = operators.RadialComponent(
    operators.interpolate(operators.Gradient(
        A, c), r=r_inner)) + operators.interpolate(
            operators.SphericalEllProduct(A, c, ell_func), r=r_inner) / r_inner


# Problem
def eq_eval(eq_str):
    return [eval(expr) for expr in split_equation(eq_str)]


V = de.field.Field(dist=d, bases=(b, ), dtype=np.complex128)