示例#1
0
def test_clenshaw_vector(N, regtotal_in, k1, k2, ell):
    dtype = np.complex128
    c = coords.SphericalCoordinates('phi', 'theta', 'r')
    d = distributor.Distributor((c,))
    b = basis.BallBasis(c, (N, N, N), dtype=dtype, k=k1, radius=1)
    basis_in = basis.BallBasis(c, (N, N, N), dtype=dtype, k=k2, radius=1)
    phi, theta, r = b.local_grids((1, 1, 1))
    x = r * np.sin(theta) * np.cos(phi)
    y = r * np.sin(theta) * np.sin(phi)
    z = r * np.cos(theta)

    ncc = field.Field(dist=d, bases=(b.radial_basis,), tensorsig=(c,), dtype=dtype)
    ncc['g'][2] = r*(2*r**2-1)
    ncc_basis = ncc.domain.bases[0]
    a_ncc = ncc_basis.alpha + ncc_basis.k
    regtotal_ncc = 1
    b_ncc = 1/2 + regtotal_ncc

    n_size = b.n_size(ell)

    coeffs_filter = ncc['c'][1,0,0,:n_size]
    J = basis_in.operator_matrix('Z', ell, regtotal_in)
    I = basis_in.operator_matrix('Id', ell, regtotal_in)
    A, B = clenshaw.jacobi_recursion(n_size, a_ncc, b_ncc, J)
    f0 = dedalus_sphere.zernike.polynomials(3, 1, a_ncc, regtotal_ncc, 1)[0] * sparse.identity(n_size)
    matrix = clenshaw.matrix_clenshaw(coeffs_filter, A, B, f0, cutoff=1e-6)

    assert np.allclose(J.todense(), matrix.todense())
示例#2
0
def test_heat_ball_nlbvp(Nr, dtype, dealias):
    radius = 2
    ncc_cutoff = 1e-10
    tolerance = 1e-10
    # Bases
    c = coords.SphericalCoordinates('phi', 'theta', 'r')
    d = distributor.Distributor((c,))
    b = basis.BallBasis(c, (1, 1, Nr), radius=radius, dtype=dtype, dealias=dealias)
    bs = b.S2_basis(radius=radius)
    phi, theta, r = b.local_grids((1, 1, 1))
    # Fields
    u = field.Field(name='u', dist=d, bases=(b,), dtype=dtype)
    τ = field.Field(name='τ', dist=d, bases=(bs,), dtype=dtype)
    F = field.Field(name='F', dist=d, bases=(b,), dtype=dtype) # todo: make this constant
    F['g'] = 6
    # Problem
    Lap = lambda A: operators.Laplacian(A, c)
    Lift = lambda A: operators.Lift(A, b, -1)
    problem = problems.NLBVP([u, τ])
    problem.add_equation((Lap(u) + Lift(τ), F))
    problem.add_equation((u(r=radius), 0))
    # Solver
    solver = solvers.NonlinearBoundaryValueSolver(problem, ncc_cutoff=ncc_cutoff)
    # Initial guess
    u['g'] = 1
    # Iterations
    def error(perts):
        return np.sum([np.sum(np.abs(pert['c'])) for pert in perts])
    err = np.inf
    while err > tolerance:
        solver.newton_iteration()
        err = error(solver.perturbations)
    u_true = r**2 - radius**2
    u.change_scales(1)
    assert np.allclose(u['g'], u_true)
def build_shell(Nphi, Ntheta, Nr, dealias, dtype):
    c = coords.SphericalCoordinates('phi', 'theta', 'r')
    d = distributor.Distributor((c,))
    b = basis.ShellBasis(c, (Nphi, Ntheta, Nr), radii=radii_shell, dealias=(dealias, dealias, dealias), dtype=dtype)
    phi, theta, r = b.local_grids(b.domain.dealias)
    x, y, z = c.cartesian(phi, theta, r)
    return c, d, b, phi, theta, r, x, y, z
示例#4
0
def build_sphere_3d(Nphi, Ntheta, radius, dealias, dtype):
    c = coords.SphericalCoordinates('phi', 'theta', 'r')
    d = distributor.Distributor((c, ))
    b = basis.SphereBasis(c, (Nphi, Ntheta),
                          radius=radius,
                          dealias=(dealias, dealias),
                          dtype=dtype)
    phi, theta = b.local_grids((dealias, dealias, dealias))
    return c, d, b, phi, theta
示例#5
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])
示例#6
0
def build_ball(Nphi, Ntheta, Nr, dtype, dealias, radius=1):
    c = coords.SphericalCoordinates('phi', 'theta', 'r')
    d = distributor.Distributor((c, ))
    b = basis.BallBasis(c, (Nphi, Ntheta, Nr),
                        radius=radius,
                        dtype=dtype,
                        dealias=(dealias, dealias, dealias))
    phi, theta, r = b.local_grids()
    x, y, z = c.cartesian(phi, theta, r)
    return c, d, b, phi, theta, r, x, y, z
示例#7
0
def build_shell(Nphi, Ntheta, Nr, radii_shell, dealias, dtype, grid_scale=1):
    c = coords.SphericalCoordinates('phi', 'theta', 'r')
    d = distributor.Distributor((c, ))
    dealias_tuple = (dealias, dealias, dealias)
    b = basis.ShellBasis(c, (Nphi, Ntheta, Nr),
                         radii=radii_shell,
                         dealias=dealias_tuple,
                         dtype=dtype)
    grid_scale_tuple = (grid_scale, grid_scale, grid_scale)
    phi, theta, r = b.local_grids(grid_scale_tuple)
    x, y, z = c.cartesian(phi, theta, r)
    return c, d, b, phi, theta, r, x, y, z
示例#8
0
def build_ball(Nphi, Ntheta, Nr, radius, alpha, k, dealias, dtype):
    c = coords.SphericalCoordinates('phi', 'theta', 'r')
    d = distributor.Distributor((c, ))
    b = basis.BallBasis(c, (Nphi, Ntheta, Nr),
                        radius=radius,
                        alpha=alpha,
                        k=k,
                        dealias=(dealias, dealias, dealias),
                        dtype=dtype)
    phi, theta, r = b.local_grids((dealias, dealias, dealias))
    x = r * np.sin(theta) * np.cos(phi)
    y = r * np.sin(theta) * np.sin(phi)
    z = r * np.cos(theta)
    return c, d, b, phi, theta, r, x, y, z
示例#9
0
def test_lane_emden_floating_R(Nr, dtype, dealias):
    n = 3.0
    ncc_cutoff = 1e-10
    tolerance = 1e-10
    # Bases
    c = coords.SphericalCoordinates('phi', 'theta', 'r')
    d = distributor.Distributor((c,))
    b = basis.BallBasis(c, (1, 1, Nr), radius=1, dtype=dtype, dealias=dealias)
    bs = b.S2_basis(radius=1)
    bs0 = b.S2_basis(radius=0)
    phi, theta, r = b.local_grids((1, 1, 1))
    # Fields
    f = field.Field(dist=d, bases=(b,), dtype=dtype, name='f')
    R = field.Field(dist=d, dtype=dtype, name='R')
    τ = field.Field(dist=d, bases=(bs,), dtype=dtype, name='τ')
    one = field.Field(dist=d, bases=(bs0,), dtype=dtype)
    one['g'] = 1
    # Problem
    lap = lambda A: operators.Laplacian(A, c)
    Lift = lambda A: operators.Lift(A, b, -1)
    problem = problems.NLBVP([f, R, τ])
    problem.add_equation((lap(f) + Lift(τ), - R**2 * f**n))
    problem.add_equation((f(r=0), one))
    problem.add_equation((f(r=1), 0))
    # Solver
    solver = solvers.NonlinearBoundaryValueSolver(problem, ncc_cutoff=ncc_cutoff)
    # Initial guess
    f['g'] = np.cos(np.pi/2 * r)**2
    R['g'] = 5
    # Iterations
    def error(perts):
        return np.sum([np.sum(np.abs(pert['c'])) for pert in perts])
    err = np.inf
    while err > tolerance:
        solver.newton_iteration()
        err = error(solver.perturbations)
    # Compare to reference solutions from Boyd
    R_ref = {0.0: np.sqrt(6),
            0.5: 2.752698054065,
            1.0: np.pi,
            1.5: 3.65375373621912608,
            2.0: 4.3528745959461246769735700,
            2.5: 5.355275459010779,
            3.0: 6.896848619376960375454528,
            3.25: 8.018937527,
            3.5: 9.535805344244850444,
            4.0: 14.971546348838095097611066,
            4.5: 31.836463244694285264}
    assert np.allclose(R['g'].ravel(), R_ref[n])
示例#10
0
def test_ball_bessel_eigenfunction(Lmax, Nmax, Leig, Neig, radius, 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
    f = field.Field(dist=d, bases=(b, ), dtype=dtype)
    τ_f = field.Field(dist=d, bases=(b_S2, ), dtype=dtype)
    k2 = field.Field(name='k2', dist=d, dtype=dtype)
    # Parameters and operators
    lap = lambda A: operators.Laplacian(A, c)
    Lift = lambda A: operators.Lift(A, b, -1)
    # Bessel equation: k^2*f + lap(f) = 0
    problem = problems.EVP([f, τ_f], k2)
    problem.add_equation((k2 * f + lap(f) + Lift(τ_f), 0))
    problem.add_equation((f(r=radius), 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]
    solver.eigenvectors = solver.eigenvectors[:, i_sort]
    solver.set_state(Neig,
                     solver.subproblems[Leig].subsystems[0])  # m = 0 mode
    f.change_layout(d.layouts[1])
    local_m, local_ell, local_n = f.layout.local_group_arrays(
        f.domain, f.scales)
    radial_eigenfunction = f.data[(local_m == 0) * (local_ell == Leig)]
    i_max = np.argmax(np.abs(radial_eigenfunction))
    radial_eigenfunction /= radial_eigenfunction[i_max]
    k = np.sqrt(solver.eigenvalues[Neig])
    sol = spec.jv(Leig + 1 / 2, k * r) / np.sqrt(k * r)
    sol = sol.ravel()
    sol /= sol[i_max]
    assert np.allclose(radial_eigenfunction, sol)
示例#11
0
radius = 1
Lmax = 15
L_dealias = 1
Nmax = 15
N_dealias = 1
Om = 20.
u0 = np.sqrt(3 / (2 * np.pi))
nu = 1e-2
dt = 0.02
t_end = 20
ts = timesteppers.SBDF2
dtype = np.float64
Nphi = 2 * (Lmax + 1)

# Bases
c = coords.SphericalCoordinates('phi', 'theta', 'r')
d = distributor.Distributor((c, ))
b = basis.BallBasis(c, (Nphi, Lmax + 1, Nmax + 1), radius=radius, dtype=dtype)
b_S2 = b.S2_basis()
phi, theta, r = b.local_grids((1, 1, 1))

# Fields
u = field.Field(dist=d, bases=(b, ), tensorsig=(c, ), dtype=dtype)
p = field.Field(dist=d, bases=(b, ), dtype=dtype)
tau = field.Field(dist=d, bases=(b_S2, ), tensorsig=(c, ), dtype=dtype)

# Boundary conditions
u_BC = field.Field(dist=d, bases=(b_S2, ), tensorsig=(c, ), dtype=dtype)
u_BC['g'][2] = 0.  # u_r = 0
u_BC['g'][1] = -u0 * np.cos(theta) * np.cos(phi)
u_BC['g'][0] = u0 * np.sin(phi)
示例#12
0
def test_lane_emden_first_order(Nr, dtype, dealias):
    n = 3.0
    ncc_cutoff = 1e-10
    tolerance = 1e-10
    # Bases
    c = coords.SphericalCoordinates('phi', 'theta', 'r')
    d = distributor.Distributor((c,))
    b = basis.BallBasis(c, (1, 1, Nr), radius=1, dtype=dtype, dealias=dealias)
    br = b.radial_basis
    phi, theta, r = b.local_grids((1, 1, 1))
    # Fields
    p = field.Field(dist=d, bases=(br,), dtype=dtype, name='p')
    ρ = field.Field(dist=d, bases=(br,), dtype=dtype, name='ρ')
    φ = field.Field(dist=d, bases=(br,), dtype=dtype, name='φ')
    τ = field.Field(dist=d, dtype=dtype, name='τ')
    τ2 = field.Field(dist=d, dtype=dtype, name='τ2')
    rf = field.Field(dist=d, bases=(br,), tensorsig=(c,), dtype=dtype, name='r')
    rf['g'][2] = r
    # Problem
    lap = lambda A: operators.Laplacian(A, c)
    grad = lambda A: operators.Gradient(A, c)
    div = lambda A: operators.Divergence(A)
    Lift = lambda A: operators.Lift(A, br, -1)
    dot = lambda A, B: arithmetic.DotProduct(A, B)
    rdr = lambda A: dot(rf, grad(A))
    problem = problems.NLBVP([p, ρ, φ, τ, τ2])
    problem.add_equation((p, ρ**(1+1/n)))
    problem.add_equation((lap(φ) + Lift(τ), ρ))
    problem.add_equation((φ(r=1), 0))

    # This works
    # problem.add_equation((-φ, (n+1) * ρ**(1/n)))
    # problem.add_equation((τ2, 0))

    # Also works when near correct solution
    # problem.add_equation((-φ**n, (n+1)**n * ρ))
    # problem.add_equation((τ2, 0))

    # Doesn't work well
    problem.add_equation((rdr(p) + Lift(τ2), -ρ*rdr(φ)))
    problem.add_equation((p(r=1), 0))

    # Also doesn't work well
    # problem.add_equation((lap(p) + Lift(τ2), -div(ρ*grad(φ))))
    # problem.add_equation((p(r=1), 0))

    # Solver
    solver = solvers.NonlinearBoundaryValueSolver(problem, ncc_cutoff=ncc_cutoff)
    # Initial guess
    #φ['g'] = - 55 *  np.cos(np.pi/2 * r)
    #φ['g'] = - 50 *  (1 - r) * (1 + r)
    φ['g'] = np.array([[[-5.49184941e+01-2.10742982e-38j,
         -5.41628923e+01-5.32970546e-38j,
         -5.29461420e+01-5.04522267e-38j,
         -5.13265949e+01-2.97780743e-38j,
         -4.93761552e+01-2.61880274e-38j,
         -4.71730013e+01-3.43967627e-38j,
         -4.47948939e+01-3.04186813e-38j,
         -4.23139098e+01-1.79113018e-38j,
         -3.97929639e+01-1.43996160e-38j,
         -3.72840673e+01-1.63817277e-38j,
         -3.48280092e+01-9.99537738e-39j,
         -3.24550394e+01-3.17721047e-40j,
         -3.01861437e+01-5.81373831e-42j,
         -2.80345785e+01-3.10228717e-39j,
         -2.60074301e+01+1.28594534e-39j,
         -2.41070531e+01+7.60758754e-39j,
         -2.23323155e+01+7.97312927e-39j,
         -2.06796271e+01+5.81693170e-39j,
         -1.91437566e+01+6.56252079e-39j,
         -1.77184618e+01+1.10908840e-38j,
         -1.63969611e+01+1.53872437e-38j,
         -1.51722763e+01+1.39129399e-38j,
         -1.40374741e+01+9.43669477e-39j,
         -1.29858304e+01+9.30920868e-39j,
         -1.20109359e+01+1.23602737e-38j,
         -1.11067589e+01+1.41710050e-38j,
         -1.02676773e+01+1.60717088e-38j,
         -9.48848876e+00+1.77178302e-38j,
         -8.76440610e+00+1.48647842e-38j,
         -8.09104289e+00+1.01146628e-38j,
         -7.46439287e+00+1.11622279e-38j,
         -6.88080593e+00+1.66263627e-38j,
         -6.33696251e+00+1.79488585e-38j,
         -5.82984767e+00+1.46579657e-38j,
         -5.35672567e+00+1.34603496e-38j,
         -4.91511565e+00+1.50574167e-38j,
         -4.50276874e+00+1.54259944e-38j,
         -4.11764669e+00+1.52307339e-38j,
         -3.75790229e+00+1.61072571e-38j,
         -3.42186130e+00+1.52968997e-38j,
         -3.10800611e+00+1.33188351e-38j,
         -2.81496085e+00+1.46531686e-38j,
         -2.54147800e+00+1.65381249e-38j,
         -2.28642630e+00+1.48467159e-38j,
         -2.04877987e+00+1.49987605e-38j,
         -1.82760852e+00+1.83704612e-38j,
         -1.62206896e+00+1.68020109e-38j,
         -1.43139709e+00+1.17510410e-38j,
         -1.25490103e+00+1.25754442e-38j,
         -1.09195489e+00+1.71504952e-38j,
         -9.41993349e-01+1.76972495e-38j,
         -8.04506695e-01+1.53368883e-38j,
         -6.79036552e-01+1.46402303e-38j,
         -5.65172039e-01+1.54974386e-38j,
         -4.62546411e-01+1.60642465e-38j,
         -3.70834105e-01+1.59758147e-38j,
         -2.89748169e-01+1.49361039e-38j,
         -2.19038018e-01+1.32679253e-38j,
         -1.58487515e-01+1.40338570e-38j,
         -1.07913330e-01+1.83256446e-38j,
         -6.71635483e-02+2.05950514e-38j,
         -3.61164846e-02+1.65676365e-38j,
         -1.46794435e-02+1.02374473e-38j,
         -2.78432418e-03+6.69851727e-39j]]])
    ρ['g'] = (-φ['g']/(n+1))**n
    p['g'] = ρ['g']**(1+1/n)
    # Iterations
    def error(perts):
        return np.sum([np.sum(np.abs(pert['c'])) for pert in perts])
    err = np.inf
    while err > tolerance and solver.iteration < 20:
        solver.newton_iteration()
        err = error(solver.perturbations)
        φcen = φ(r=0).evaluate()['g'][0,0,0]
        R = -φcen  / (n+1)**(3/2)
        print(solver.iteration, φcen, R, err)
        dH = solver.subproblems[0].dH_min
        print('%.2e' %np.linalg.cond(dH.A))
    if err > tolerance:
        raise ValueError("Did not converge")
    # Compare to reference solutions from Boyd
    R_ref = {0.0: np.sqrt(6),
            0.5: 2.752698054065,
            1.0: np.pi,
            1.5: 3.65375373621912608,
            2.0: 4.3528745959461246769735700,
            2.5: 5.355275459010779,
            3.0: 6.896848619376960375454528,
            3.25: 8.018937527,
            3.5: 9.535805344244850444,
            4.0: 14.971546348838095097611066,
            4.5: 31.836463244694285264}
    assert np.allclose(R, R_ref[n])