Example #1
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
Example #2
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))))
Example #3
0
def test_calculus_2d_1():
    domain = Domain('Omega', dim=2)

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

    alpha, beta, gamma = [Constant(i) for i in ['alpha', 'beta', 'gamma']]

    f, g, h = elements_of(V, names='f, g, h')
    F, G, H = elements_of(W, names='F, G, H')

    # ... scalar gradient properties
    assert (grad(f + g) == grad(f) + grad(g))
    assert (grad(alpha * h) == alpha * grad(h))
    assert (grad(alpha * f + beta * g) == alpha * grad(f) + beta * grad(g))

    assert (grad(f * g) == f * grad(g) + g * grad(f))
    assert (grad(f / g) == -f * grad(g) / g**2 + grad(f) / g)

    assert (expand(grad(f * g * h)) == f * g * grad(h) + f * h * grad(g) +
            g * h * grad(f))
    # ...

    # ... vector gradient properties
    assert (grad(F + G) == grad(F) + grad(G))
    assert (grad(alpha * H) == alpha * grad(H))
    assert (grad(alpha * F + beta * G) == alpha * grad(F) + beta * grad(G))

    assert (grad(dot(F, G)) == convect(F, G) + convect(G, F) +
            cross(F, curl(G)) - cross(curl(F), G))
    # ...

    # ... curl properties
    assert (curl(f + g) == curl(f) + curl(g))
    assert (curl(alpha * h) == alpha * curl(h))
    assert (curl(alpha * f + beta * g) == alpha * curl(f) + beta * curl(g))
    # ...

    # ... laplace properties
    assert (laplace(f + g) == laplace(f) + laplace(g))
    assert (laplace(alpha * h) == alpha * laplace(h))
    assert (laplace(alpha * f + beta * g) == alpha * laplace(f) +
            beta * laplace(g))
    # ...

    # ... divergence properties
    assert (div(F + G) == div(F) + div(G))
    assert (div(alpha * H) == alpha * div(H))
    assert (div(alpha * F + beta * G) == alpha * div(F) + beta * div(G))

    assert (div(cross(F, G)) == -dot(F, curl(G)) + dot(G, curl(F)))
    # ...

    # ... rot properties
    assert (rot(F + G) == rot(F) + rot(G))
    assert (rot(alpha * H) == alpha * rot(H))
    assert (rot(alpha * F + beta * G) == alpha * rot(F) + beta * rot(G))
Example #4
0
def test_linearize_form_2d_1():
    domain = Domain('Omega', dim=2)

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

    v, F, u = elements_of(V, names='v, F, u')
    w, G, m = elements_of(W, names='w, G, m')

    int_0 = lambda expr: integral(domain, expr)

    # ...
    l = LinearForm(v, int_0(F**2 * v))
    a = linearize(l, F, trials=u)
    assert a(u, v) == int_0(2 * F * u * v)
    # ...

    # ...
    l = LinearForm(v, int_0(dot(grad(F), grad(F)) * v))
    a = linearize(l, F, trials=u)
    assert a(u, v) == int_0(2 * dot(grad(F), grad(u)) * v)
    # ...

    # ...
    l = LinearForm(v, int_0(exp(-F) * v))
    a = linearize(l, F, trials=u)
    assert a(u, v) == int_0(-exp(-F) * u * v)
    # ...

    # ...
    l = LinearForm(v, int_0(cos(F) * v))
    a = linearize(l, F, trials=u)
    assert a(u, v) == int_0(-sin(F) * u * v)
    # ...

    # ...
    l = LinearForm(v, int_0(cos(F**2) * v))
    a = linearize(l, F, trials=u)
    assert a(u, v) == int_0(-2 * F * sin(F**2) * u * v)
    # ...

    # ...
    l = LinearForm(v, int_0(F**2 * dot(grad(F), grad(v))))
    a = linearize(l, F, trials=u)
    assert a(u, v) == int_0(2 * F * u * dot(grad(F), grad(v)) +
                            F**2 * dot(grad(u), grad(v)))
    # ...

    # ...
    l = LinearForm(w, int_0(dot(rot(G), grad(G)) * w))
    a = linearize(l, G, trials=m)
    assert a(m, w) == int_0((dot(rot(m), grad(G)) + dot(rot(G), grad(m))) * w)
Example #5
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)
Example #6
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
Example #7
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))
Example #8
0
def test_interface_integral_2():

    # ...
    A = Square('A')
    B = Square('B')

    domain = A.join(B,
                    name='domain',
                    bnd_minus=A.get_boundary(axis=0, ext=1),
                    bnd_plus=B.get_boundary(axis=0, ext=-1))
    # ...

    x, y = domain.coordinates

    V = ScalarFunctionSpace('V', domain, kind=None)
    assert (V.is_broken)

    u, u1, u2, u3 = elements_of(V, names='u, u1, u2, u3')
    v, v1, v2, v3 = elements_of(V, names='v, v1, v2, v3')

    # ...
    I = domain.interfaces

    a = BilinearForm((u, v), integral(domain, dot(grad(u), grad(v))))
    b = BilinearForm((u, v), integral(I, jump(u) * jump(v)))

    A = BilinearForm(((u1, u2), (v1, v2)),
                     a(u1, v1) + a(u2, v2) + b(u1, v1) + b(u2, v2) + b(u1, v2))
    B = BilinearForm(
        ((u1, u2, u3), (v1, v2, v3)),
        a(u1, v1) + a(u2, v2) + a(u3, v3) + b(u1, v1) + b(u2, v2) + b(u1, v2))

    print(TerminalExpr(A))
    print(TerminalExpr(B))
    # ...

    # ... linear forms
    b = LinearForm(v, integral(I, jump(v)))

    b = LinearForm((v1, v2), b(v1) + b(v2))
    expr = TerminalExpr(b)
    print(expr)
Example #9
0
def test_logical_expr_3d_2():

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

    mapped_domain = M(domain)

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

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

    J = M.jacobian

    a = dot(grad(um), grad(vm))
    e = LogicalExpr(a, mapping=M, dim=dim)

    assert e == dot(J.inv().T * grad(u), J.inv().T * grad(v))
Example #10
0
def test_logical_expr_3d_3():

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

    mapped_domain = M(domain)

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

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

    J = M.jacobian

    a = dot(curl(um), curl(vm))
    e = LogicalExpr(a, mapping=M, dim=dim)

    assert e == dot(J / J.det() * curl(u), J / J.det() * curl(v))
Example #11
0
def test_interface_2d_1():

    # ...
    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

    # ...

    # create a domain with an interface
    domain = two_patches()
    interfaces = domain.interfaces

    V = ScalarFunctionSpace('V', domain)

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

    print(integral(interfaces, u * v))

    expr = integral(domain, dot(grad(v), grad(u)))
    expr += integral(interfaces, -avg(Dn(u)) * jump(v) + avg(Dn(v)) * jump(u))
    a = BilinearForm((u, v), expr)
    print(a)
Example #12
0
def test_logical_expr_3d_4():

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

    mapped_domain = M(domain)

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

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

    J = M.jacobian

    a = div(um) * div(vm)
    e = LogicalExpr(a, mapping=M, dim=dim)

    assert e == J.det()**-2 * div(u) * div(v)
Example #13
0
def test_interface_integral_1():

    # ...
    A = Square('A')
    B = Square('B')

    domain = A.join(B,
                    name='domain',
                    bnd_minus=A.get_boundary(axis=0, ext=1),
                    bnd_plus=B.get_boundary(axis=0, ext=-1))
    # ...

    x, y = domain.coordinates

    V = ScalarFunctionSpace('V', domain, kind=None)
    assert (V.is_broken)

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

    # ...
    I = domain.interfaces
    # ...

    #    expr = minus(Dn(u))
    #    print(expr)
    #    import sys; sys.exit(0)

    # ... bilinear forms
    #    a = BilinearForm((u,v), integral(domain, u*v))
    #    a = BilinearForm((u,v), integral(domain, dot(grad(u),grad(v))))
    #    a = BilinearForm((u,v), integral(I, jump(u) * jump(v)))
    #    a = BilinearForm((u,v), integral(I, jump(Dn(u)) * jump(v)))

    #    a = BilinearForm((u,v), integral(domain, dot(grad(u),grad(v)))
    #                          + integral(I,      jump(u) * jump(v)))

    # Nitsch
    kappa = Constant('kappa')
    expr_I = (-jump(u) * jump(Dn(v)) + kappa * jump(u) * jump(v) +
              plus(Dn(u)) * minus(v) + minus(Dn(u)) * plus(v))
    a = BilinearForm(
        (u, v),
        integral(domain, dot(grad(u), grad(v))) + integral(I, expr_I))

    #    # TODO BUG
    #    bnd_A = A.get_boundary(axis=0, ext=1)
    #
    #    a = BilinearForm((u,v), integral(domain, dot(grad(u),grad(v)))
    #                          + integral(I,      jump(u) * jump(v))
    #                          + integral(bnd_A,      dx(u)*v))

    expr = TerminalExpr(a)
    print(expr)
Example #14
0
def test_calculus_3d_4():
    domain = Domain('Omega', dim=3)

    W = VectorFunctionSpace('W', domain)

    alpha, beta, gamma = [Constant(i) for i in ['alpha', 'beta', 'gamma']]

    F, G, H = elements_of(W, names='F, G, H')

    # ...
    expected = alpha * inner(D(F), D(G)) + beta * inner(D(F), D(H))
    assert (inner(D(F), D(alpha * G + beta * H)) == expected)
Example #15
0
def test_calculus_3d_5():
    domain = Domain('Omega', dim=3)

    W = VectorFunctionSpace('W', domain)

    alpha, beta, gamma = [Constant(i) for i in ['alpha', 'beta', 'gamma']]

    F, G, H = elements_of(W, names='F, G, H')

    # ...
    expected = alpha * outer(F, G) + beta * outer(F, H)
    assert (outer(F, alpha * G + beta * H) == expected)
Example #16
0
def test_interface_integral_3():

    # ...
    A = Square('A')
    B = Square('B')
    C = Square('C')

    AB = A.join(B,
                name='AB',
                bnd_minus=A.get_boundary(axis=0, ext=1),
                bnd_plus=B.get_boundary(axis=0, ext=-1))

    domain = AB.join(C,
                     name='domain',
                     bnd_minus=B.get_boundary(axis=0, ext=1),
                     bnd_plus=C.get_boundary(axis=0, ext=-1))
    # ...

    x, y = domain.coordinates

    V = ScalarFunctionSpace('V', domain, kind=None)
    assert (V.is_broken)

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

    # ...
    I = domain.interfaces
    #    print(I)
    #    print(integral(I, jump(u) * jump(v)))

    #    a = BilinearForm((u,v), integral(domain, u*v))
    #    a = BilinearForm((u,v), integral(domain, dot(grad(u),grad(v))))
    #    a = BilinearForm((u,v), integral(I, jump(u) * jump(v)))

    a = BilinearForm((u, v),
                     integral(domain, dot(grad(u), grad(v))) +
                     integral(I,
                              jump(u) * jump(v)))

    expr = TerminalExpr(a)
    print(expr)
    # ...

    # ... linear forms
    b = LinearForm(
        v,
        integral(domain,
                 sin(x + y) * v) + integral(I,
                                            cos(x + y) * jump(v)))

    expr = TerminalExpr(b)
    print(expr)
Example #17
0
def test_calculus_2d_2():
    domain = Domain('Omega', dim=2)

    W = VectorFunctionSpace('W', domain)

    alpha, beta, gamma = [Constant(i) for i in ['alpha', 'beta', 'gamma']]

    F, G, H = elements_of(W, names='F, G, H')

    # ... vector gradient properties
    assert (convect(F + G, H) == convect(F, H) + convect(G, H))
    assert (convect(alpha * F, H) == alpha * convect(F, H))
    assert (convect(F, alpha * H) == alpha * convect(F, H))
Example #18
0
def test_tensorize_2d_2():

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

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

    int_0 = lambda expr: integral(domain , expr)
    # ...
#    a = BilinearForm((u,v), dot(u,v))
    a = BilinearForm((u,v), int_0(curl(u)*curl(v) + div(u)*div(v)))

    expr = TensorExpr(a, domain=domain)
    print(expr)
Example #19
0
def test_tensorize_2d_3():

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

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

    bx = Constant('bx')
    by = Constant('by')
    b = Tuple(bx, by)

    expr = integral(domain, dot(b, grad(v)) * dot(b, grad(u)))
    a = BilinearForm((u, v), expr)

    print(TensorExpr(a))
    print('')
Example #20
0
def test_tensorize_2d_2_mapping():

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

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

    c = Constant('c')

    int_0 = lambda expr: integral(domain, expr)

    a = BilinearForm((u, v), int_0(c * div(v) * div(u) + curl(v) * curl(u)))
    expr = TensorExpr(a, mapping=M)
    print(expr)
Example #21
0
def test_linearize_form_2d_2():
    domain = Domain('Omega', dim=2)

    V = ScalarFunctionSpace('V', domain)

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

    int_0 = lambda expr: integral(domain, expr)

    # ...
    l1 = LinearForm(v, int_0(F**2 * v))
    l = LinearForm(v, l1(v))

    a = linearize(l, F, trials=u)

    expected = linearize(l1, F, trials=u)
    assert a == expected
Example #22
0
def test_calculus_2d_3():
    domain = Domain('Omega', dim=2)
    x, y = domain.coordinates

    V = ScalarFunctionSpace('V', domain)

    alpha, beta, gamma = [Constant(i) for i in ['alpha', 'beta', 'gamma']]

    f, g = elements_of(V, names='f, g')
    K = Function('K')(x, y)
    S = Function('S')(x, y)

    # ... scalar gradient properties
    assert (conv(K, alpha * f + beta * g) == alpha * conv(K, f) +
            beta * conv(K, g))
    assert (conv(alpha * K + beta * S,
                 f) == alpha * conv(K, f) + beta * conv(S, f))
Example #23
0
def test_tensorize_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, v = elements_of(V, names='u, v')

    int_0 = lambda expr: integral(domain, expr)
    # ...
    #    a = BilinearForm((u,v), dot(u,v))
    a = BilinearForm((u, v), int_0(curl(u) * curl(v) + div(u) * div(v)))

    expr = TensorExpr(a)
    print(expr)
Example #24
0
def test_tensorize_2d_1():

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

    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), int_0(mu*u*v + dot(grad(u),grad(v))))
#    a = BilinearForm((u,v), dot(grad(u),grad(v)))
#    a = BilinearForm((u,v), dx(u)*v)
#    a = BilinearForm((u,v), laplace(u)*laplace(v))

    expr = TensorExpr(a, domain=domain)
    print(expr)
Example #25
0
def test_Outer(dim):

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

    # Not commutative
    assert outer(a, b) != outer(b, a)

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

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

    # Special case: null vector
    assert outer(a, 0) == 0
    assert outer(0, a) == 0
Example #26
0
def test_Cross(dim):

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

    # Anti-commutativity (anti-symmetry)
    assert cross(a, b) == -cross(b, a)

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

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

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

    # Special case: two arguments are the same
    assert cross(a, a) == 0
Example #27
0
def test_linearity_bilinear_form_2d_1():

    from sympde.expr.errors import UnconsistentLinearExpressionError

    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)
    eps = Constant('eps', real=True)
    nn = NormalVector('nn')

    V = ScalarFunctionSpace('V', domain)

    u, u1, u2 = elements_of(V, names='u, u1, u2')
    v, v1, v2 = elements_of(V, names='v, v1, v2')

    # ...

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

    # The following integral expressions are bilinear, hence it must be possible
    # to create BilinearForm objects from them

    _ = BilinearForm((u, v), int_0(u * v))
    _ = BilinearForm((u, v), int_0(dot(grad(u), grad(v))))
    _ = BilinearForm((u, v), int_0(u * v + dot(grad(u), grad(v))))
    _ = BilinearForm(
        (u, v),
        int_0(u * v + dot(grad(u), grad(v))) + int_1(v * dot(grad(u), nn)))
    _ = BilinearForm(((u1, u2), (v1, v2)), int_0(u1 * v1 + u2 * v2))

    a1 = BilinearForm((u1, v1), int_0(u1 * v1))
    _ = BilinearForm((u, v), a1(u, v))

    a1 = BilinearForm((u1, v1), int_0(u1 * v1))
    a2 = BilinearForm((u2, v2), int_0(dot(grad(u2), grad(v2))))
    _ = BilinearForm((u, v), a1(u, v) + a2(u, v))

    a1 = BilinearForm((u1, v1), int_0(u1 * v1))
    a2 = BilinearForm((u2, v2), int_0(dot(grad(u2), grad(v2))))
    _ = BilinearForm((u, v), a1(u, v) + kappa * a2(u, v))

    a1 = BilinearForm((u1, v1), int_0(u1 * v1))
    a2 = BilinearForm((u2, v2), int_0(dot(grad(u2), grad(v2))))
    a3 = BilinearForm((u, v), int_1(v * dot(grad(u), nn)))
    _ = BilinearForm((u, v), a1(u, v) + kappa * a2(u, v) + mu * a3(u, v))

    # ... Poisson with Nitsch method
    a0 = BilinearForm((u, v), int_0(dot(grad(u), grad(v))))
    a_B1 = BilinearForm((u, v),
                        int_1(-kappa * u * dot(grad(v), nn) -
                              v * dot(grad(u), nn) + u * v / eps))
    _ = BilinearForm((u, v), a0(u, v) + a_B1(u, v))
    # ...

    # The following integral expressions are not bilinear, hence BilinearForm must
    # raise an exception

    with pytest.raises(UnconsistentLinearExpressionError):
        _ = BilinearForm((u, v), int_0(x * y * dot(grad(u), grad(v)) + 1))

    with pytest.raises(UnconsistentLinearExpressionError):
        _ = BilinearForm((u, v), int_0(x * dot(grad(u), grad(v**2))))

    with pytest.raises(UnconsistentLinearExpressionError):
        _ = BilinearForm((u, v), int_0(u * v) + int_1(v * exp(u)))
Example #28
0
def test_linearity_linear_form_2d_1():

    from sympde.expr.errors import UnconsistentLinearExpressionError

    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 = ScalarFunctionSpace('V', domain)
    W = VectorFunctionSpace('W', domain)

    v, v1, v2 = elements_of(V, names='v, v1, v2')
    w = element_of(W, name='w')

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

    # The following integral expressions are linear, hence it must be possible
    # to create LinearForm objects from them

    _ = LinearForm(v, int_0(x * y * v))
    _ = LinearForm(v, int_0(x * y * v + v))

    g = Matrix((x**2, y**2))
    _ = LinearForm(v, int_0(v * dot(g, nn)))

    g = Matrix((x**2, y**2))
    _ = LinearForm(v, int_1(v * dot(g, nn)) + int_0(x * y * v))

    l1 = LinearForm(v1, int_0(x * y * v1))
    _ = LinearForm(v, l1(v))

    g = Matrix((x, y))
    l1 = LinearForm(v1, int_0(x * y * v1))
    l2 = LinearForm(v2, int_0(dot(grad(v2), g)))
    _ = LinearForm(v, l1(v) + l2(v))

    l1 = LinearForm(v1, int_0(x * y * v1))
    l2 = LinearForm(v1, int_0(v1))
    _ = LinearForm(v, l1(v) + kappa * l2(v))

    g = Matrix((x**2, y**2))
    l1 = LinearForm(v1, int_0(x * y * v1))
    l2 = LinearForm(v1, int_0(v1))
    l3 = LinearForm(v, int_1(v * dot(g, nn)))
    _ = LinearForm(v, l1(v) + kappa * l2(v) + mu * l3(v))

    l1 = LinearForm(w, int_0(5 * w * x))
    l2 = LinearForm(w, int_1(w * y))
    _ = LinearForm(w, l1(w) + kappa * l2(w))

    l0 = LinearForm(v, int_0(x * y * v))
    l1 = LinearForm(w, int_0(w[0] * y))
    l2 = LinearForm(w, int_1(w[1] * x))
    l3 = LinearForm(w, kappa * l1(w) + mu * l2(w))
    _ = LinearForm((v, w), l0(v) + l3(w))

    # The following integral expressions are not linear, hence LinearForm must
    # raise an exception

    with pytest.raises(UnconsistentLinearExpressionError):
        _ = LinearForm(v, int_0(x * y * v + 1))

    with pytest.raises(UnconsistentLinearExpressionError):
        _ = LinearForm(v, int_0(x * v**2))

    with pytest.raises(UnconsistentLinearExpressionError):
        _ = LinearForm(v, int_0(x * y * v) + int_1(v * exp(v)))

    with pytest.raises(UnconsistentLinearExpressionError):
        _ = LinearForm(w, int_0(w * w))

    with pytest.raises(UnconsistentLinearExpressionError):
        _ = LinearForm(w, int_0(w[0] * w[1]))

    with pytest.raises(UnconsistentLinearExpressionError):
        _ = LinearForm((v, w), int_0(x * w[0]) + int_1(v * w[1]))
Example #29
0
from nodes import StencilVectorGlobalBasis
from nodes import ElementOf
from nodes import WeightedVolumeQuadrature
from nodes import ComputeKernelExpr
from nodes import AST
from nodes import Block
from nodes import Reset

from parser import parse

# ... abstract model
domain = Square()
M      = Mapping('M', domain.dim)

V      = ScalarFunctionSpace('V', domain)
u,v    = elements_of(V, names='u,v')
expr   = dot(grad(v), grad(u))
# ...

# ...
grid    = Grid()
element = Element()
g_quad  = GlobalTensorQuadrature()
l_quad  = LocalTensorQuadrature()
quad    = TensorQuadrature()

g_basis   = GlobalTensorQuadratureTrialBasis(u)
l_basis   = LocalTensorQuadratureTrialBasis(u)
a_basis   = TensorQuadratureTrialBasis(u)
basis     = TensorTrialBasis(u)
g_basis_v = GlobalTensorQuadratureTestBasis(v)