Пример #1
0
def test_boundary_3():
    Omega_1 = InteriorDomain('Omega_1', dim=2)

    Gamma_1 = Boundary(r'\Gamma_1', Omega_1, axis=0, ext=-1)
    Gamma_4 = Boundary(r'\Gamma_4', Omega_1, axis=1, ext=1)

    Omega = Domain('Omega',
                   interiors=[Omega_1],
                   boundaries=[Gamma_1, Gamma_4])

    assert(Omega.get_boundary(axis=0, ext=-1) == Gamma_1)
    assert(Omega.get_boundary(axis=1, ext=1) == Gamma_4)
Пример #2
0
def test_zero_derivative():

    assert grad(1) == 0  # native int
    assert grad(2.3) == 0  # native float
    assert grad(4 + 5j) == 0  # native complex
    assert grad(Integer(1)) == 0  # sympy Integer
    assert grad(Float(2.3)) == 0  # sympy Float
    assert grad(Rational(6, 7)) == 0  # sympy Rational
    assert grad(Constant('a')) == 0  # sympde Constant

    assert laplace(1) == 0  # native int
    assert laplace(2.3) == 0  # native float
    assert laplace(4 + 5j) == 0  # native complex
    assert laplace(Integer(1)) == 0  # sympy Integer
    assert laplace(Float(2.3)) == 0  # sympy Float
    assert laplace(Rational(6, 7)) == 0  # sympy Rational
    assert laplace(Constant('a')) == 0  # sympde Constant

    assert hessian(1) == 0  # native int
    assert hessian(2.3) == 0  # native float
    assert hessian(4 + 5j) == 0  # native complex
    assert hessian(Integer(1)) == 0  # sympy Integer
    assert hessian(Float(2.3)) == 0  # sympy Float
    assert hessian(Rational(6, 7)) == 0  # sympy Rational
    assert hessian(Constant('a')) == 0  # sympde Constant

    # 2D convection of constant scalar field
    domain = Domain('Omega', dim=2)
    W = VectorFunctionSpace('W', domain)
    F = element_of(W, name='F')

    assert convect(F, 1) == 0  # native int
    assert convect(F, 2.3) == 0  # native float
    assert convect(F, 4 + 5j) == 0  # native complex
    assert convect(F, Integer(1)) == 0  # sympy Integer
    assert convect(F, Float(2.3)) == 0  # sympy Float
    assert convect(F, Rational(6, 7)) == 0  # sympy Rational
    assert convect(F, Constant('a')) == 0  # sympde Constant

    # 3D convection of constant scalar field
    domain = Domain('Omega', dim=3)
    Z = VectorFunctionSpace('Z', domain)
    G = element_of(Z, name='G')

    assert convect(G, 1) == 0  # native int
    assert convect(G, 2.3) == 0  # native float
    assert convect(G, 4 + 5j) == 0  # native complex
    assert convect(G, Integer(1)) == 0  # sympy Integer
    assert convect(G, Float(2.3)) == 0  # sympy Float
    assert convect(G, Rational(6, 7)) == 0  # sympy Rational
    assert convect(G, Constant('a')) == 0  # sympde Constant
Пример #3
0
def test_target_mapping_2d_1():
    dim = 2

    x1, x2 = symbols('x1, x2')

    constants = ['c1', 'c2', 'D', 'k']
    c1, c2, D, k = [Constant(i) for i in constants]

    M = TargetMapping('M', dim=dim)

    domain = M(Domain('Omega', dim=dim))

    assert (not (M[0] == x1))
    assert (not (M[1] == x2))

    assert (LogicalExpr(M[0],
                        domain) == -D * x1**2 + c1 + x1 * (-k + 1) * cos(x2))
    assert (LogicalExpr(M[1], domain) == c2 + x1 * (k + 1) * sin(x2))

    assert (LogicalExpr(dx1(M[0]), domain) == -2 * D * x1 + (-k + 1) * cos(x2))
    assert (LogicalExpr(dx1(M[1]), domain) == (k + 1) * sin(x2))

    assert (LogicalExpr(dx2(M[0]), domain) == -x1 * (-k + 1) * sin(x2))
    assert (LogicalExpr(dx2(M[1]), domain) == x1 * (k + 1) * cos(x2))

    expected = Matrix(
        [[-2 * D * x1 + (-k + 1) * cos(x2), -x1 * (-k + 1) * sin(x2)],
         [(k + 1) * sin(x2), x1 * (k + 1) * cos(x2)]])

    assert (Jacobian(M) == expected)
Пример #4
0
def test_tensorize_2d_1_mapping():

    DIM = 2

    M = Mapping('Map', DIM)

    domain = Domain('Omega', dim=DIM)
    B1 = Boundary(r'\Gamma_1', domain)

    x, y = domain.coordinates

    kappa = Constant('kappa', is_real=True)
    mu = Constant('mu', is_real=True)

    V = ScalarFunctionSpace('V', domain)
    u, v = elements_of(V, names='u, v')

    int_0 = lambda expr: integral(domain, expr)
    # ...
    #    a = BilinearForm((u,v), u*v)
    #    a = BilinearForm((u,v), mu*u*v + dot(grad(u),grad(v)))
    a = BilinearForm((u, v), int_0(dot(grad(u), grad(v))))
    #    a = BilinearForm((u,v), dx(u)*v)
    #    a = BilinearForm((u,v), laplace(u)*laplace(v))

    expr = TensorExpr(a, mapping=M)
    print(expr)
Пример #5
0
def test_derivatives_2d_with_mapping():

    dim = 2
    M = Mapping('M', dim=dim)
    O = M(Domain('Omega', dim=dim))
    V = ScalarFunctionSpace('V', O, kind='h1')
    u = element_of(V, 'u')

    det_jac = Jacobian(M).det()
    J = Symbol('J')

    expr = M
    assert SymbolicExpr(expr) == Symbol('M')

    expr = M[0]
    assert SymbolicExpr(expr) == Symbol('x')

    expr = M[1]
    assert SymbolicExpr(expr) == Symbol('y')

    expr = dx2(M[0])
    assert SymbolicExpr(expr) == Symbol('x_x2')

    expr = dx1(M[1])
    assert SymbolicExpr(expr) == Symbol('y_x1')

    expr = LogicalExpr(dx(u), O).subs(det_jac, J)
    expected = '(u_x1 * y_x2 - u_x2 * y_x1)/J'
    difference = SymbolicExpr(expr) - sympify(expected)
    assert difference.simplify() == 0
Пример #6
0
def test_mapping_2d_2():
    print('============ test_mapping_2d_2 ==============')

    rdim   = 2
    F      = Mapping('F', rdim)
    domain = Domain('Omega', dim=rdim)
    D      = F(domain)
Пример #7
0
def test_Inner(dim):

    domain = Domain('Omega', dim=dim)
    W = VectorFunctionSpace('W', domain)
    a, b, c = elements_of(W, names='a, b, c')
    r = Constant('r')

    # Commutativity
    assert inner(a, b) == inner(b, a)

    # Bilinearity: vector addition
    assert inner(a, b + c) == inner(a, b) + inner(a, c)
    assert inner(a + b, c) == inner(a, c) + inner(b, c)

    # Bilinearity: scalar multiplication
    assert inner(a, r * b) == r * inner(a, b)
    assert inner(r * a, b) == r * inner(a, b)

    # Special case: null vector
    assert inner(a, 0) == 0
    assert inner(0, a) == 0

    # Special case: two arguments are the same
    assert inner(a, a).is_real
    assert inner(a, a).is_positive
Пример #8
0
def test_linearize_form_2d_4():
    domain = Domain('Omega', dim=2)
    Gamma_N = Boundary(r'\Gamma_N', domain)

    x, y = domain.coordinates

    V = ScalarFunctionSpace('V', domain)

    v = element_of(V, name='v')
    u = element_of(V, name='u')
    du = element_of(V, name='du')

    int_0 = lambda expr: integral(domain, expr)
    int_1 = lambda expr: integral(Gamma_N, expr)

    #    g = Matrix((cos(pi*x)*sin(pi*y),
    #              sin(pi*x)*cos(pi*y)))

    expr = dot(grad(v), grad(u)) - 4. * exp(-u) * v  # + v*trace_1(g, Gamma_N)

    l = LinearForm(v, int_0(expr))

    # linearising l around u, using du
    a = linearize(l, u, trials=du)

    assert a(du, v) == int_0(dot(grad(v), grad(du)) + 4. * exp(-u) * du * v)
Пример #9
0
def test_user_function_2d_1():

    domain = Domain('Omega', dim=2)
    x, y = domain.coordinates

    kappa = Constant('kappa', is_real=True)
    mu = Constant('mu', is_real=True)

    # right hand side
    f = Function('f')

    V = ScalarFunctionSpace('V', domain)

    u, v = [element_of(V, name=i) for i in ['u', 'v']]

    int_0 = lambda expr: integral(domain, expr)

    # ...
    expr = dot(grad(u), grad(v)) + f(x, y) * u * v
    a = BilinearForm((v, u), int_0(expr))

    print(a)
    print(TerminalExpr(a))
    print('')
    # ...

    # ...
    expr = f(x, y) * v
    l = LinearForm(v, int_0(expr))

    print(l)
    print(TerminalExpr(l))
    print('')
Пример #10
0
def test_latex_2d_2():

    DIM = 2
    domain = Domain('Omega', dim=DIM)

    V = VectorFunctionSpace('V', domain)

    x, y = V.coordinates

    v = element_of(V, name='v')
    u = element_of(V, name='u')
    #    F = element_of(V, name='F')

    int_0 = lambda expr: integral(domain, expr)

    assert (latex(v) == r'\mathbf{v}')
    assert (latex(inner(
        grad(v), grad(u))) == r'\nabla{\mathbf{u}} : \nabla{\mathbf{v}}')

    a = BilinearForm((v, u), int_0(inner(grad(v), grad(u))))
    print(latex(a))
    #    assert(latex(a) == r'\int_{0}^{1}\int_{0}^{1} \nabla{\mathbf{v}} : \nabla{\mathbf{u}} dxdy')

    b = LinearForm(v, int_0(sin(pi * x) * cos(pi * y) * div(v)))
    print(latex(b))
Пример #11
0
def test_linearize_form_2d_3():
    """steady Euler equation."""
    domain = Domain('Omega', dim=2)

    U = VectorFunctionSpace('U', domain)
    W = ScalarFunctionSpace('W', domain)

    # Test functions
    v = element_of(U, name='v')
    phi = element_of(W, name='phi')
    q = element_of(W, name='q')

    # Steady-state fields
    U_0 = element_of(U, name='U_0')
    Rho_0 = element_of(W, name='Rho_0')
    P_0 = element_of(W, name='P_0')

    # Trial functions (displacements from steady-state)
    d_u = element_of(U, name='d_u')
    d_rho = element_of(W, name='d_rho')
    d_p = element_of(W, name='d_p')

    # Shortcut
    int_0 = lambda expr: integral(domain, expr)

    # The Euler equations are a system of three non-linear equations; for each of
    # them we create a linear form in the test functions (phi, v, q) respectively.
    e1 = div(Rho_0 * U_0)
    l1 = LinearForm(phi, int_0(e1 * phi))

    e2 = Rho_0 * convect(U_0, U_0) + grad(P_0)
    l2 = LinearForm(v, int_0(dot(e2, v)))

    e3 = div(P_0 * U_0)
    l3 = LinearForm(q, int_0(e3 * q))
    # ...

    # Linearize l1, l2 and l3 separately
    a1 = linearize(l1, fields=[Rho_0, U_0], trials=[d_rho, d_u])
    a2 = linearize(l2, fields=[Rho_0, U_0, P_0], trials=[d_rho, d_u, d_p])
    a3 = linearize(l3, fields=[U_0, P_0], trials=[d_u, d_p])

    # Check individual bilinear forms
    d_e1 = div(U_0 * d_rho + Rho_0 * d_u)
    d_e2 = d_rho * convect(U_0, U_0) + \
           Rho_0 * convect(d_u, U_0) + \
           Rho_0 * convect(U_0, d_u) + grad(d_p)
    d_e3 = div(d_p * U_0 + P_0 * d_u)

    assert a1([d_rho, d_u], phi) == int_0(d_e1 * phi)
    assert a2([d_rho, d_u, d_p], v) == int_0(dot(d_e2, v))
    assert a3([d_u, d_p], q) == int_0(d_e3 * q)

    # Linearize linear form of system: l = l1 + l2 + l3
    l = LinearForm((phi, v, q), l1(phi) + l2(v) + l3(q))
    a = linearize(l, fields=[Rho_0, U_0, P_0], trials=[d_rho, d_u, d_p])

    # Check composite linear form
    assert a([d_rho, d_u, d_p], [phi, v, q]) == \
            int_0(d_e1 * phi + dot(d_e2, v) + d_e3 * q)
Пример #12
0
def test_latex_ec_3d_1():

    n = 3

    # ...
    u_0 = DifferentialForm('u_0', index=0, dim=n)
    v_0 = DifferentialForm('v_0', index=0, dim=n)

    u_1 = DifferentialForm('u_1', index=1, dim=n)
    v_1 = DifferentialForm('v_1', index=1, dim=n)

    u_2 = DifferentialForm('u_2', index=2, dim=n)
    v_2 = DifferentialForm('v_2', index=2, dim=n)

    u_3 = DifferentialForm('u_3', index=3, dim=n)
    v_3 = DifferentialForm('v_3', index=3, dim=n)
    # ...

    # ...
    domain = Domain('Omega', dim=3)
    V = VectorFunctionSpace('V', domain)

    beta = element_of(V, 'beta')
    # ...

    print(latex(u_0))
    print(latex(d(u_0)))
    print(latex(d(delta(u_3))))
    print(latex(d(delta(u_2)) + delta(d(u_2))))
    print(latex(wedge(u_0, u_1)))

    print(latex(ip(beta, u_1)))
    print(latex(hodge(u_1)))
    print(latex(jp(beta, u_1)))
Пример #13
0
def test_latex_2d_5():
    DIM = 2

    domain = Domain('Omega', dim=DIM)

    # ... abstract model
    W1 = VectorFunctionSpace('W1', domain)

    w1 = element_of(W1, name='w1')
    F = element_of(W1, 'F')

    int_0 = lambda expr: integral(domain, expr)

    # ...
    l1 = LinearForm(w1, int_0(dot(w1, F)))

    print(latex(l1))
    print('')
    # ...

    # ...
    l2 = LinearForm(w1, int_0(rot(w1) * rot(F) + div(w1) * div(F)))

    print(latex(l2))
    print('')
Пример #14
0
def test_latex_2d_3():
    DIM = 2

    domain = Domain('Omega', dim=DIM)

    B1 = Boundary(r'\Gamma_1', domain)
    B2 = Boundary(r'\Gamma_2', domain)
    B3 = Boundary(r'\Gamma_3', domain)

    V = ScalarFunctionSpace('V', domain)

    x = V.coordinates

    v = element_of(V, name='v')
    u = element_of(V, name='u')

    int_0 = lambda expr: integral(domain, expr)
    int_1 = lambda expr: integral(B1, expr)

    # ...
    expr = dot(grad(v), grad(u))
    a_0 = BilinearForm((v, u), int_0(expr))

    expr = v * u
    a_bnd = BilinearForm((v, u), int_1(expr))

    expr = a_0(v, u) + a_bnd(v, u)
    a = BilinearForm((v, u), expr)
    print(latex(a_0))
    print(latex(a_bnd))
    print(latex(a))
    #    print(a)
    print('')
Пример #15
0
def test_terminal_expr_bilinear_3d_1():

    domain = Domain('Omega', dim=3)
    M = Mapping('M', 3)

    mapped_domain = M(domain)

    V = ScalarFunctionSpace('V', domain)
    VM = ScalarFunctionSpace('VM', mapped_domain)

    u, v = elements_of(V, names='u,v')
    um, vm = elements_of(VM, names='u,v')

    int_0 = lambda expr: integral(domain, expr)
    int_1 = lambda expr: integral(mapped_domain, expr)

    J = M.det_jacobian
    det = dx1(M[0])*dx2(M[1])*dx3(M[2]) - dx1(M[0])*dx2(M[2])*dx3(M[1]) - dx1(M[1])*dx2(M[0])*dx3(M[2])\
        + dx1(M[1])*dx2(M[2])*dx3(M[0]) + dx1(M[2])*dx2(M[0])*dx3(M[1]) - dx1(M[2])*dx2(M[1])*dx3(M[0])

    a1 = BilinearForm((u, v), int_0(dot(grad(u), grad(v))))
    a2 = BilinearForm((um, vm), int_1(dot(grad(um), grad(vm))))
    a3 = BilinearForm((u, v), int_0(J * dot(grad(u), grad(v))))

    e1 = TerminalExpr(a1)
    e2 = TerminalExpr(a2)
    e3 = TerminalExpr(a3)

    assert e1[0].expr == dx1(u) * dx1(v) + dx2(u) * dx2(v) + dx3(u) * dx3(v)
    assert e2[0].expr == dx(um) * dx(vm) + dy(um) * dy(vm) + dz(um) * dz(vm)
    assert e3[0].expr.factor() == (dx1(u) * dx1(v) + dx2(u) * dx2(v) +
                                   dx3(u) * dx3(v)) * det
Пример #16
0
def test_functional_2d_1():

    domain = Domain('Omega', dim=2)
    x, y = domain.coordinates

    kappa = Constant('kappa', is_real=True)
    mu = Constant('mu', is_real=True)

    V = ScalarFunctionSpace('V', domain)
    F = element_of(V, name='F')

    int_0 = lambda expr: integral(domain, expr)

    # ...
    expr = x * y
    a = Functional(int_0(expr), domain)

    print(a)
    print(TerminalExpr(a))
    print('')
    # ...

    # ...
    expr = F - cos(2 * pi * x) * cos(3 * pi * y)
    expr = dot(grad(expr), grad(expr))
    a = Functional(int_0(expr), domain)

    print(a)
    print(TerminalExpr(a))
    print('')
Пример #17
0
def test_partial_derivatives_1():
    print('============ test_partial_derivatives_1 ==============')

    # ...
    domain = Domain('Omega', dim=2)
    x,y = domain.coordinates

    V = ScalarFunctionSpace('V', domain)

    F,u,v,w = [ScalarField(V, name=i) for i in ['F', 'u', 'v', 'w']]
    uvw = Tuple(u,v,w)

    alpha = Constant('alpha')
    beta = Constant('beta')
    # ...

    # ...
    assert(dx(x**2) == 2*x)
    assert(dy(x**2) == 0)
    assert(dz(x**2) == 0)

    assert(dx(x*F) == F + x*dx(F))
    assert(dx(uvw) == Matrix([[dx(u), dx(v), dx(w)]]))
    assert(dx(uvw) + dy(uvw) == Matrix([[dx(u) + dy(u),
                                         dx(v) + dy(v),
                                         dx(w) + dy(w)]]))

    expected = Matrix([[alpha*dx(u) + beta*dy(u),
                        alpha*dx(v) + beta*dy(v),
                        alpha*dx(w) + beta*dy(w)]])
    assert(alpha * dx(uvw) + beta * dy(uvw) == expected)
Пример #18
0
def test_bilinear_form_2d_4():

    domain = Domain('Omega', dim=2)
    B1 = Boundary(r'\Gamma_1', domain)

    x, y = domain.coordinates

    kappa = Constant('kappa', is_real=True)
    mu = Constant('mu', is_real=True)

    V = VectorFunctionSpace('V', domain)

    u, u1, u2 = [element_of(V, name=i) for i in ['u', 'u1', 'u2']]
    v, v1, v2 = [element_of(V, name=i) for i in ['v', 'v1', 'v2']]

    int_0 = lambda expr: integral(domain, expr)
    int_1 = lambda expr: integral(B1, expr)
    # ...
    a = BilinearForm((u, v), int_0(dot(u, v)))
    assert (a.is_symmetric)
    # ...

    # ...
    a = BilinearForm((u, v), int_0(inner(grad(u), grad(v))))
    assert (a.is_symmetric)
Пример #19
0
def test_calculus_2d_4():

    DIM = 2
    domain = Domain('Omega', dim=DIM)

    V = ScalarFunctionSpace('V', domain, kind=None)

    u, v = elements_of(V, names='u, v')

    a = Constant('a', is_real=True)

    # ... jump operator
    assert (jump(u + v) == jump(u) + jump(v))
    assert (jump(a * u) == a * jump(u))
    # ...

    # ... avg operator
    assert (avg(u + v) == avg(u) + avg(v))
    assert (avg(a * u) == a * avg(u))
    # ...

    # ... Dn operator
    assert (Dn(u + v) == Dn(u) + Dn(v))
    assert (Dn(a * u) == a * Dn(u))
    # ...

    # ... minus operator
    assert (minus(u + v) == minus(u) + minus(v))
    assert (minus(a * u) == a * minus(u))
    # ...

    # ... plus operator
    assert (plus(u + v) == plus(u) + plus(v))
    assert (plus(a * u) == a * plus(u))
Пример #20
0
    def two_patches():

        from sympde.topology import InteriorDomain
        from sympde.topology import Connectivity, Interface

        A = Square('A')
        B = Square('B')

        A = A.interior
        B = B.interior

        connectivity = Connectivity()

        bnd_A_1 = Boundary(r'\Gamma_1', A, axis=0, ext=-1)
        bnd_A_2 = Boundary(r'\Gamma_2', A, axis=0, ext=1)
        bnd_A_3 = Boundary(r'\Gamma_3', A, axis=1, ext=-1)
        bnd_A_4 = Boundary(r'\Gamma_4', A, axis=1, ext=1)

        bnd_B_1 = Boundary(r'\Gamma_1', B, axis=0, ext=-1)
        bnd_B_2 = Boundary(r'\Gamma_2', B, axis=0, ext=1)
        bnd_B_3 = Boundary(r'\Gamma_3', B, axis=1, ext=-1)
        bnd_B_4 = Boundary(r'\Gamma_4', B, axis=1, ext=1)

        connectivity['I'] = Interface('I', bnd_A_2, bnd_B_1)

        Omega = Domain('Omega',
                       interiors=[A, B],
                       boundaries=[
                           bnd_A_1, bnd_A_2, bnd_A_3, bnd_A_4, bnd_B_1,
                           bnd_B_2, bnd_B_3, bnd_B_4
                       ],
                       connectivity=connectivity)

        return Omega
Пример #21
0
def test_derivatives_2d_without_mapping():

    O = Domain('Omega', dim=2)
    V = ScalarFunctionSpace('V', O)
    u = element_of(V, 'u')

    expr = u
    assert SymbolicExpr(expr) == Symbol('u')

    expr = dx(u)
    assert SymbolicExpr(expr) == Symbol('u_x')

    expr = dx(dx(u))
    assert SymbolicExpr(expr) == Symbol('u_xx')

    expr = dx(dy(u))
    assert SymbolicExpr(expr) == Symbol('u_xy')

    expr = dy(dx(u))
    assert SymbolicExpr(expr) == Symbol('u_xy')

    expr = dy(dx(dz(u)))
    assert SymbolicExpr(expr) == Symbol('u_xyz')

    expr = dy(dy(dx(u)))
    assert SymbolicExpr(expr) == Symbol('u_xyy')

    expr = dy(dz(dy(u)))
    assert SymbolicExpr(expr) == Symbol('u_yyz')
Пример #22
0
def test_linear_expr_2d_1():

    domain = Domain('Omega', dim=2)
    x, y = domain.coordinates

    kappa = Constant('kappa', is_real=True)
    mu = Constant('mu', is_real=True)

    V = ScalarFunctionSpace('V', domain)

    u, u1, u2 = [element_of(V, name=i) for i in ['u', 'u1', 'u2']]
    v, v1, v2 = [element_of(V, name=i) for i in ['v', 'v1', 'v2']]

    # ...
    l = LinearExpr(v, x * y * v)
    print(l)
    print(l.expr)
    print(l(v1))
    # TODO
    #    print(l(v1+v2))
    print('')
    # ...

    # ...
    l = LinearExpr((v1, v2), x * v1 + y * v2)
    print(l)
    print(l.expr)
    print(l(u1, u2))
    # TODO
    #    print(l(u1+v1, u2+v2))
    print('')
Пример #23
0
def test_projector_2d_1():

    DIM = 2
    domain = Domain('Omega', dim=DIM)

    V = ScalarFunctionSpace('V', domain, kind=None)
    W = VectorFunctionSpace('W', domain, kind=None)

    v, w = element_of(V * W, ['v', 'w'])

    # ...
    P_V = Projector(V)
    assert (P_V.space == V)

    Pv = P_V(v)
    assert (isinstance(Pv, ScalarTestFunction))
    assert (Pv == v)
    assert (grad(Pv**2) == 2 * v * grad(v))

    Pdiv_w = P_V(div(w))
    assert (isinstance(Pdiv_w, ScalarTestFunction))
    # ...

    # ...
    P_W = Projector(W)
    assert (P_W.space == W)

    Pw = P_W(w)
    assert (isinstance(Pw, VectorTestFunction))
    assert (Pw == w)

    Pgrad_v = P_W(grad(v))
    assert (isinstance(Pgrad_v, VectorTestFunction))
    assert (P_W(Pgrad_v) == Pgrad_v)
Пример #24
0
def test_terminal_expr_linear_2d_2():

    domain = Domain('Omega', dim=2)
    B1 = Boundary(r'\Gamma_1', domain)

    x, y = domain.coordinates

    kappa = Constant('kappa', is_real=True)
    mu = Constant('mu', is_real=True)

    V = VectorFunctionSpace('V', domain)

    u, u1, u2 = [element_of(V, name=i) for i in ['u', 'u1', 'u2']]
    v, v1, v2 = [element_of(V, name=i) for i in ['v', 'v1', 'v2']]

    # ...
    int_0 = lambda expr: integral(domain, expr)
    int_1 = lambda expr: integral(B1, expr)

    g = Matrix((x, y))
    l = LinearForm(v, int_0(dot(g, v)))
    print(TerminalExpr(l))
    print('')
    # ...

    # ...
    g = Matrix((x, y))
    l = LinearForm(v, int_0(dot(g, v) + div(v)))
    print(TerminalExpr(l))
    print('')
Пример #25
0
def test_generic_line():

    # Create 1D domain (Line) from interval [-3, 4]
    domain = Line('line', bounds=(-3, 4))

    assert isinstance(domain, Line)

    # BasicDomain's attributes
    assert domain.dim == 1
    assert domain.name == 'line'
    assert domain.coordinates == x1

    # Domain's attributes
    assert isinstance(domain.interior, NCubeInterior)
    assert len(domain.boundary) == 2
    assert domain.dtype == {'type': 'Line', 'parameters': {'bounds': [-3, 4]}}

    # NCube's attributes
    assert domain.min_coords == (-3, )
    assert domain.max_coords == (4, )

    # Line's attributes
    assert domain.bounds == (-3, 4)

    # Export to file, read it again and compare with original domain
    domain.export('domain.h5')
    D = Domain.from_file('domain.h5')
    assert D.logical_domain == domain
Пример #26
0
def test_linear_expr_2d_2():

    domain = Domain('Omega', dim=2)
    x, y = domain.coordinates

    kappa = Constant('kappa', is_real=True)
    mu = Constant('mu', is_real=True)

    V = VectorFunctionSpace('V', domain)

    u, u1, u2 = [element_of(V, name=i) for i in ['u', 'u1', 'u2']]
    v, v1, v2 = [element_of(V, name=i) for i in ['v', 'v1', 'v2']]

    g = Tuple(x, y)
    l = LinearExpr(v, dot(g, v))
    print(l)
    print(l.expr)
    print(l(v1))
    # TODO
    #    print(l(v1+v2))
    print('')
    # ...

    # ...
    g1 = Tuple(x, 0)
    g2 = Tuple(0, y)
    l = LinearExpr((v1, v2), dot(g1, v1) + dot(g2, v2))
    print(l)
    print(l.expr)
    print(l(u1, u2))
    # TODO
    #    print(l(u1+v1, u2+v2))
    print('')
Пример #27
0
def test_polar_mapping_2d_1():
    dim = 2

    x1, x2 = symbols('x1, x2')

    constants = ['c1', 'c2', 'rmax', 'rmin']
    c1, c2, rmax, rmin = [Constant(i) for i in constants]

    M = PolarMapping('M', dim=dim)

    domain = M(Domain('Omega', dim=dim))

    assert (not (M[0] == x1))
    assert (not (M[1] == x2))

    assert (LogicalExpr(M[0], domain) == c1 + (rmax * x1 + rmin *
                                               (-x1 + 1)) * cos(x2))
    assert (LogicalExpr(M[1], domain) == c2 + (rmax * x1 + rmin *
                                               (-x1 + 1)) * sin(x2))

    assert (LogicalExpr(dx1(M[0]), domain) == (rmax - rmin) * cos(x2))
    assert (LogicalExpr(dx1(M[1]), domain) == (rmax - rmin) * sin(x2))

    expected = -(rmax * x1 + rmin * (-x1 + 1)) * sin(x2)
    assert (expand(LogicalExpr(dx2(M[0]), domain)) == expand(expected))
    assert (LogicalExpr(dx2(M[1]),
                        domain) == (rmax * x1 + rmin * (-x1 + 1)) * cos(x2))

    expected = Matrix([[(rmax - rmin) * cos(x2),
                        -(rmax * x1 + rmin * (-x1 + 1)) * sin(x2)],
                       [(rmax - rmin) * sin(x2),
                        (rmax * x1 + rmin * (-x1 + 1)) * cos(x2)]])
    assert (expand(LogicalExpr(Jacobian(M), domain)) == expand(expected))
Пример #28
0
def test_terminal_expr_bilinear_2d_2():

    domain = Domain('Omega', dim=2)
    B1 = Boundary(r'\Gamma_1', domain)

    x, y = domain.coordinates

    kappa = Constant('kappa', is_real=True)
    mu = Constant('mu', is_real=True)
    nn = NormalVector('nn')

    V = VectorFunctionSpace('V', domain)

    u, u1, u2 = [element_of(V, name=i) for i in ['u', 'u1', 'u2']]
    v, v1, v2 = [element_of(V, name=i) for i in ['v', 'v1', 'v2']]

    # ...
    int_0 = lambda expr: integral(domain, expr)
    int_1 = lambda expr: integral(B1, expr)

    a = BilinearForm((u, v), int_0(dot(u, v)))
    print(TerminalExpr(a))
    print('')

    # ...
    a = BilinearForm((u, v), int_0(inner(grad(u), grad(v))))
    print(TerminalExpr(a))
    print('')
    # ...

    # ...
    a = BilinearForm((u, v), int_0(dot(u, v) + inner(grad(u), grad(v))))
    print(TerminalExpr(a))
    print('')
Пример #29
0
def test_logical_expr_3d_5():

    dim = 3
    domain = Domain('Omega', dim=dim)
    M = Mapping('M', dim=dim)

    mapped_domain = M(domain)

    V = VectorFunctionSpace('V', domain, kind='hcurl')
    VM = VectorFunctionSpace('VM', mapped_domain, kind='hcurl')

    J = M.jacobian
    u, v = elements_of(V, names='u,v')
    um, vm = elements_of(VM, names='u,v')

    int_md = lambda expr: integral(mapped_domain, expr)
    int_ld = lambda expr: integral(domain, expr)

    am = BilinearForm((um, vm), int_md(dot(curl(vm), curl(um))))
    a = LogicalExpr(am)

    assert a == BilinearForm(
        (u, v),
        int_ld(
            sqrt((J.T * J).det()) *
            dot(J / J.det() * curl(u), J / J.det() * curl(v))))
Пример #30
0
def test_boundary_2d_2():
    Omega_1 = InteriorDomain('Omega_1', dim=2)

    B1 = Boundary('B1', Omega_1)
    B2 = Boundary('B2', Omega_1)
    B3 = Boundary('B3', Omega_1)

    domain = Domain('Omega', interiors=[Omega_1], boundaries=[B1, B2, B3])

    V = FunctionSpace('V', domain)
    v = TestFunction(V, name='v')
    u = TestFunction(V, name='u')

    x, y = V.coordinates

    alpha = Constant('alpha')

    # ...
    print('==== l0 ====')
    l0 = LinearForm(v, x * y * v, name='l0')

    print(evaluate(l0, verbose=VERBOSE))
    print('')
    # ...

    # ...
    print('==== l1 ====')
    g = Tuple(x**2, y**2)
    l1 = LinearForm(v, v * trace_1(g, domain.boundary))

    print(evaluate(l1, verbose=VERBOSE))
    print('')
    # ...

    # ...
    print('==== l2 ====')
    B_neumann = Union(B1, B2)
    g = Tuple(x**2, y**2)
    l2 = LinearForm(v, v * trace_1(g, B_neumann), name='l2')

    print(evaluate(l2, verbose=VERBOSE))
    print('')
    # ...

    # ...
    print('==== l3 ====')
    l3 = LinearForm(v, l2(v))

    assert (l3(v).__str__ == l2(v).__str__)

    print(evaluate(l3, verbose=VERBOSE))
    print('')
    # ...

    # ...
    print('==== l4 ====')
    l4 = LinearForm(v, l0(v) + l2(v))

    print(evaluate(l4, verbose=VERBOSE))
    print('')