Beispiel #1
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('')
Beispiel #2
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('')
Beispiel #3
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('')
Beispiel #4
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('')
Beispiel #5
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
Beispiel #6
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)
Beispiel #7
0
def test_norm_2d_2():

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

    V = VectorFunctionSpace('V', domain)
    F = element_of(V, 'F')

    # ...
    f = Matrix((sin(pi * x) * sin(pi * y), sin(pi * x) * sin(pi * y)))
    expr = Matrix([F[0] - f[0], F[1] - f[1]])
    l2_norm_u = Norm(expr, domain, kind='l2')
    h1_norm_u = Norm(expr, domain, kind='h1')

    print('> l2 norm = ', TerminalExpr(l2_norm_u))
    print('> h1 norm = ', TerminalExpr(h1_norm_u))
    print('')
Beispiel #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)
Beispiel #9
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)
Beispiel #10
0
def test_area_2d_1():

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

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

    e = ElementDomain(domain)
    area = Area(e)

    V = ScalarFunctionSpace('V', domain)

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

    int_0 = lambda expr: integral(domain, expr)

    # ...
    a = BilinearForm((v, u), int_0(area * u * v))
    print(TerminalExpr(a))
Beispiel #11
0
def test_terminal_expr_linear_2d_5(boundary=[r'\Gamma_1', r'\Gamma_3']):

    # ... abstract model
    domain = Square()

    V = ScalarFunctionSpace('V', domain)

    B_neumann = [domain.get_boundary(i) for i in boundary]
    if len(B_neumann) == 1:
        B_neumann = B_neumann[0]

    else:
        B_neumann = Union(*B_neumann)

    x, y = domain.coordinates
    nn = NormalVector('nn')

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

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

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

    expr = dot(grad(v), grad(u))
    a = BilinearForm((v, u), int_0(expr))

    solution = cos(0.5 * pi * x) * cos(0.5 * pi * y)
    f = (1. / 2.) * pi**2 * solution

    expr = f * v
    l0 = LinearForm(v, int_0(expr))

    expr = v * dot(grad(solution), nn)
    l_B_neumann = LinearForm(v, int_1(expr))

    expr = l0(v) + l_B_neumann(v)
    l = LinearForm(v, expr)

    print(TerminalExpr(l))
    print('')
Beispiel #12
0
def test_terminal_expr_linear_2d_4():

    D1 = InteriorDomain('D1', dim=2)
    D2 = InteriorDomain('D2', dim=2)
    domain = Union(D1, D2)

    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']]

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

    l = LinearForm(v, int_0(x * y * v))
    print(TerminalExpr(l))
    print('')
Beispiel #13
0
def test_terminal_expr_linear_2d_3():

    domain = Square()
    B = domain.boundary

    x, y = domain.coordinates

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

    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']]

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

    l = LinearForm(v, int_1(dot(grad(v), nn)))
    print(TerminalExpr(l))
    print('')
Beispiel #14
0
def test_terminal_expr_bilinear_2d_4():

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

    x, y = domain.coordinates

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

    v = element_of(V, name='v')
    u = element_of(V, name='u')
    p = element_of(W, name='p')
    q = element_of(W, name='q')

    int_0 = lambda expr: integral(domain, expr)

    # stokes
    a = BilinearForm((u, v), int_0(inner(grad(v), grad(u))))
    b = BilinearForm((v, p), int_0(div(v) * p))
    a = BilinearForm(((u, p), (v, q)), a(v, u) - b(v, p) + b(u, q))

    print(TerminalExpr(a))
    print('')
Beispiel #15
0
def test_terminal_expr_bilinear_2d_3():

    domain = Square()

    V = ScalarFunctionSpace('V', domain)

    B = domain.boundary

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

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

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

    # nitsche
    a0 = BilinearForm((u, v), int_0(dot(grad(v), grad(u))))

    a_B = BilinearForm((u,v), int_1(-u*dot(grad(v), nn) \
                              -v*dot(grad(u), nn) \
                              +kappa*u*v))

    a = BilinearForm((u, v), a0(u, v) + a_B(u, v))

    print(TerminalExpr(a))
    print('')

    a = BilinearForm(
        (u, v),
        int_0(u * v + dot(grad(u), grad(v))) + int_1(v * dot(grad(u), nn)))
    print(TerminalExpr(a))
    print('')
    # ...

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

    # ...
    a1 = BilinearForm((u, v), int_0(u * v))
    a = BilinearForm((u, v), a1(u, v))
    print(TerminalExpr(a))
    print('')
    # ...

    # ...
    a1 = BilinearForm((u, v), int_0(u * v))
    a2 = BilinearForm((u, v), int_0(dot(grad(u), grad(v))))
    a = BilinearForm((u, v), a1(u, v) + a2(u, v))
    print(TerminalExpr(a))
    print('')
    # ...

    # ...
    a1 = BilinearForm((u, v), int_0(u * v))
    a2 = BilinearForm((u, v), int_0(dot(grad(u), grad(v))))
    a = BilinearForm((u, v), a1(u, v) + kappa * a2(u, v))
    print(TerminalExpr(a))
    print('')
    # ...

    # ...
    a1 = BilinearForm((u, v), int_0(u * v))
    a2 = BilinearForm((u, v), int_0(dot(grad(u), grad(v))))
    a3 = BilinearForm((u, v), int_1(v * dot(grad(u), nn)))
    a = BilinearForm((u, v), a1(u, v) + kappa * a2(u, v) + mu * a3(u, v))
    print(TerminalExpr(a))
    print('')
Beispiel #16
0
def test_stabilization_2d_1():

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

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

    b1 = 1.
    b2 = 0.
    b = Matrix((b1, b2))

    # right hand side
    f = x * y

    e = ElementDomain()
    area = Area(e)

    V = ScalarFunctionSpace('V', domain)

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

    int_0 = lambda expr: integral(domain, expr)

    # ...
    expr = kappa * dot(grad(u), grad(v)) + dot(b, grad(u)) * v
    a = BilinearForm((v, u), int_0(expr))
    # ...

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

    # ...
    expr = (-kappa * laplace(u) + dot(b, grad(u))) * dot(b, grad(v))
    s1 = BilinearForm((v, u), int_0(expr))

    expr = -f * dot(b, grad(v))
    l1 = LinearForm(v, int_0(expr))
    # ...

    # ...
    expr = (-kappa * laplace(u) + dot(b, grad(u))) * (dot(b, grad(v)) -
                                                      kappa * laplace(v))
    s2 = BilinearForm((v, u), int_0(expr))

    expr = -f * (dot(b, grad(v)) - kappa * laplace(v))
    l2 = LinearForm(v, int_0(expr))
    # ...

    # ...
    expr = (-kappa * laplace(u) + dot(b, grad(u))) * (dot(b, grad(v)) +
                                                      kappa * laplace(v))
    s3 = BilinearForm((v, u), int_0(expr))

    expr = -f * (dot(b, grad(v)) + kappa * laplace(v))
    l3 = LinearForm(v, int_0(expr))
    # ...

    # ...
    expr = a(v, u) + mu * area * s1(v, u)
    a1 = BilinearForm((v, u), expr)
    # ...

    # ...
    expr = a(v, u) + mu * area * s2(v, u)
    a2 = BilinearForm((v, u), expr)
    # ...

    # ...
    expr = a(v, u) + mu * area * s3(v, u)
    a3 = BilinearForm((v, u), expr)
    # ...

    print(a1)
    print(TerminalExpr(a1))
    print('')

    print(a2)
    print(TerminalExpr(a2))
    print('')

    print(a3)
    print(TerminalExpr(a3))
    print('')
Beispiel #17
0
def test_terminal_expr_bilinear_2d_1():

    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 = [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(u * v))
    print(a)
    print(TerminalExpr(a))
    print('')
    # ...

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

    # ...
    a = BilinearForm((u, v), int_0(u * v + dot(grad(u), grad(v))))
    print(a)
    print(TerminalExpr(a))
    print('')
    # ...

    # ...
    a = BilinearForm(
        (u, v),
        int_0(u * v + dot(grad(u), grad(v))) + int_1(v * dot(grad(u), nn)))
    print(a)
    print(TerminalExpr(a))
    print('')
    # ...

    # ...
    a = BilinearForm(((u1, u2), (v1, v2)), int_0(u1 * v1 + u2 * v2))
    print(a)
    print(TerminalExpr(a))
    print('')
    # ...

    # ...
    a1 = BilinearForm((u1, v1), int_0(u1 * v1))
    a = BilinearForm((u, v), a1(u, v))
    print(a)
    print(TerminalExpr(a))
    print('')
    # ...

    # ...
    a1 = BilinearForm((u1, v1), int_0(u1 * v1))
    a2 = BilinearForm((u2, v2), int_0(dot(grad(u2), grad(v2))))
    a = BilinearForm((u, v), a1(u, v) + a2(u, v))
    print(a)
    print(TerminalExpr(a))
    print('')
    # ...

    # ...
    a1 = BilinearForm((u1, v1), int_0(u1 * v1))
    a2 = BilinearForm((u2, v2), int_0(dot(grad(u2), grad(v2))))
    a = BilinearForm((u, v), a1(u, v) + kappa * a2(u, v))
    print(a)
    print(TerminalExpr(a))
    print('')
    # ...

    # ...
    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)))
    a = BilinearForm((u, v), a1(u, v) + kappa * a2(u, v) + mu * a3(u, v))
    print(a)
    print(TerminalExpr(a))
    print('')
    # ...

    # ... 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))
    a = BilinearForm((u, v), a0(u, v) + a_B1(u, v))
    print(a)
    print(TerminalExpr(a))
    print('')
Beispiel #18
0
def test_norm_2d_1():

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

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

    # ...
    expr = x * y
    l2_norm_u = Norm(expr, domain, kind='l2')
    h1_norm_u = Norm(expr, domain, kind='h1')

    print('> l2 norm = ', TerminalExpr(l2_norm_u))
    print('> h1 norm = ', TerminalExpr(h1_norm_u))
    print('')
    # ...

    # ...
    expr = sin(pi * x) * sin(pi * y)
    l2_norm_u = Norm(expr, domain, kind='l2')
    h1_norm_u = Norm(expr, domain, kind='h1')

    print('> l2 norm = ', TerminalExpr(l2_norm_u))
    print('> h1 norm = ', TerminalExpr(h1_norm_u))
    print('')
    # ...

    # ...
    expr = F - x * y
    l2_norm_u = Norm(expr, domain, kind='l2')
    h1_norm_u = Norm(expr, domain, kind='h1')

    print('> l2 norm = ', TerminalExpr(l2_norm_u))
    print('> h1 norm = ', TerminalExpr(h1_norm_u))
    print('')
    # ...

    # ...
    expr = F - sin(pi * x) * sin(pi * y)
    l2_norm_u = Norm(expr, domain, kind='l2')
    h1_norm_u = Norm(expr, domain, kind='h1')

    print('> l2 norm = ', TerminalExpr(l2_norm_u))
    print('> h1 norm = ', TerminalExpr(h1_norm_u))
    print('')
    # ...

    # ...
    expr = F - sin(0.5 * pi * (1. - x)) * sin(pi * y)
    l2_norm_u = Norm(expr, domain, kind='l2')
    h1_norm_u = Norm(expr, domain, kind='h1')

    print('> l2 norm = ', TerminalExpr(l2_norm_u))
    print('> h1 norm = ', TerminalExpr(h1_norm_u))
    print('')
    # ...

    # ...
    expr = F - cos(0.5 * pi * x) * sin(pi * y)
    l2_norm_u = Norm(expr, domain, kind='l2')
    h1_norm_u = Norm(expr, domain, kind='h1')

    print('> l2 norm = ', TerminalExpr(l2_norm_u))
    print('> h1 norm = ', TerminalExpr(h1_norm_u))
    print('')
Beispiel #19
0
def test_terminal_expr_linear_2d_1():

    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)

    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)

    l = LinearForm(v, int_0(x * y * v))

    print(TerminalExpr(l))
    print('')
    # ...

    # ...
    l = LinearForm(v, int_0(x * y * v + v))
    print(TerminalExpr(l))
    print('')
    # ...

    # ...
    g = Matrix((x**2, y**2))
    l = LinearForm(v, int_1(v * dot(g, nn)))
    print(TerminalExpr(l))
    print('')
    # ...

    # ...
    g = Matrix((x**2, y**2))
    l = LinearForm(v, int_1(v * dot(g, nn)) + int_0(x * y * v))
    print(TerminalExpr(l))
    print('')
    # ...

    # ...
    l1 = LinearForm(v1, int_0(x * y * v1))
    l = LinearForm(v, l1(v))
    print(TerminalExpr(l))
    print('')
    # ...

    # ...
    g = Matrix((x, y))
    l1 = LinearForm(v1, int_0(x * y * v1))
    l2 = LinearForm(v2, int_0(dot(grad(v2), g)))

    l = LinearForm(v, l1(v) + l2(v))
    print(TerminalExpr(l))
    print('')
    # ...

    # ...
    l1 = LinearForm(v1, int_0(x * y * v1))
    l2 = LinearForm(v1, int_0(v1))
    l = LinearForm(v, l1(v) + kappa * l2(v))
    print(TerminalExpr(l))
    print('')
    # ...

    # ...
    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)))
    l = LinearForm(v, l1(v) + kappa * l2(v) + mu * l3(v))
    print(TerminalExpr(l))
    print('')
Beispiel #20
0
    def eval(cls, expr, domain, **options):
        """."""

        from sympde.expr.evaluation import TerminalExpr, DomainExpression
        from sympde.expr.expr import BilinearForm, LinearForm, BasicForm, Norm
        from sympde.expr.expr import Integral

        types = (ScalarFunction, VectorFunction, DifferentialOperator, Trace,
                 Integral)

        mapping = domain.mapping
        dim = domain.dim
        assert mapping

        # TODO this is not the dim of the domain
        l_coords = ['x1', 'x2', 'x3'][:dim]
        ph_coords = ['x', 'y', 'z']

        if not has(expr, types):
            if has(expr, DiffOperator):
                return cls(expr, domain, evaluate=False)
            else:
                syms = symbols(ph_coords[:dim])
                if isinstance(mapping, InterfaceMapping):
                    mapping = mapping.minus
                    # here we assume that the two mapped domains
                    # are identical in the interface so we choose one of them
                Ms = [mapping[i] for i in range(dim)]
                expr = expr.subs(list(zip(syms, Ms)))

                if mapping.is_analytical:
                    expr = expr.subs(list(zip(Ms, mapping.expressions)))
                return expr

        if isinstance(expr, Symbol) and expr.name in l_coords:
            return expr

        if isinstance(expr, Symbol) and expr.name in ph_coords:
            return mapping[ph_coords.index(expr.name)]

        elif isinstance(expr, Add):
            args = [cls.eval(a, domain) for a in expr.args]
            v = S.Zero
            for i in args:
                v += i
            n, d = v.as_numer_denom()
            return n / d

        elif isinstance(expr, Mul):
            args = [cls.eval(a, domain) for a in expr.args]
            v = S.One
            for i in args:
                v *= i
            return v

        elif isinstance(expr, _logical_partial_derivatives):
            if mapping.is_analytical:
                Ms = [mapping[i] for i in range(dim)]
                expr = expr.subs(list(zip(Ms, mapping.expressions)))
            return expr

        elif isinstance(expr, IndexedVectorFunction):
            el = cls.eval(expr.base, domain)
            el = TerminalExpr(el, domain=domain.logical_domain)
            return el[expr.indices[0]]

        elif isinstance(expr, MinusInterfaceOperator):
            mapping = mapping.minus
            newexpr = PullBack(expr.args[0], mapping)
            test = newexpr.test
            newexpr = newexpr.expr.subs(test, MinusInterfaceOperator(test))
            return newexpr

        elif isinstance(expr, PlusInterfaceOperator):
            mapping = mapping.plus
            newexpr = PullBack(expr.args[0], mapping)
            test = newexpr.test
            newexpr = newexpr.expr.subs(test, PlusInterfaceOperator(test))
            return newexpr

        elif isinstance(expr, (VectorFunction, ScalarFunction)):
            return PullBack(expr, mapping).expr

        elif isinstance(expr, Transpose):
            arg = cls(expr.arg, domain)
            return Transpose(arg)

        elif isinstance(expr, grad):
            arg = expr.args[0]
            if isinstance(mapping, InterfaceMapping):
                if isinstance(arg, MinusInterfaceOperator):
                    a = arg.args[0]
                    mapping = mapping.minus
                elif isinstance(arg, PlusInterfaceOperator):
                    a = arg.args[0]
                    mapping = mapping.plus
                else:
                    raise TypeError(arg)

                arg = type(arg)(cls.eval(a, domain))
            else:
                arg = cls.eval(arg, domain)

            return mapping.jacobian.inv().T * grad(arg)

        elif isinstance(expr, curl):
            arg = expr.args[0]
            if isinstance(mapping, InterfaceMapping):
                if isinstance(arg, MinusInterfaceOperator):
                    arg = arg.args[0]
                    mapping = mapping.minus
                elif isinstance(arg, PlusInterfaceOperator):
                    arg = arg.args[0]
                    mapping = mapping.plus
                else:
                    raise TypeError(arg)

            if isinstance(arg, VectorFunction):
                arg = PullBack(arg, mapping)
            else:
                arg = cls.eval(arg, domain)

            if isinstance(arg, PullBack) and isinstance(
                    arg.kind, HcurlSpaceType):
                J = mapping.jacobian
                arg = arg.test
                if isinstance(expr.args[0],
                              (MinusInterfaceOperator, PlusInterfaceOperator)):
                    arg = type(expr.args[0])(arg)
                if expr.is_scalar:
                    return (1 / J.det()) * curl(arg)

                return (J / J.det()) * curl(arg)
            else:
                raise NotImplementedError('TODO')

        elif isinstance(expr, div):
            arg = expr.args[0]
            if isinstance(mapping, InterfaceMapping):
                if isinstance(arg, MinusInterfaceOperator):
                    arg = arg.args[0]
                    mapping = mapping.minus
                elif isinstance(arg, PlusInterfaceOperator):
                    arg = arg.args[0]
                    mapping = mapping.plus
                else:
                    raise TypeError(arg)

            if isinstance(arg, (ScalarFunction, VectorFunction)):
                arg = PullBack(arg, mapping)
            else:

                arg = cls.eval(arg, domain)

            if isinstance(arg, PullBack) and isinstance(
                    arg.kind, HdivSpaceType):
                J = mapping.jacobian
                arg = arg.test
                if isinstance(expr.args[0],
                              (MinusInterfaceOperator, PlusInterfaceOperator)):
                    arg = type(expr.args[0])(arg)
                return (1 / J.det()) * div(arg)
            elif isinstance(arg, PullBack):
                return SymbolicTrace(mapping.jacobian.inv().T * grad(arg.test))
            else:
                raise NotImplementedError('TODO')

        elif isinstance(expr, laplace):
            arg = expr.args[0]
            v = cls.eval(grad(arg), domain)
            v = mapping.jacobian.inv().T * grad(v)
            return SymbolicTrace(v)

#        elif isinstance(expr, hessian):
#           arg = expr.args[0]
#            if isinstance(mapping, InterfaceMapping):
#                if isinstance(arg, MinusInterfaceOperator):
#                    arg     = arg.args[0]
#                    mapping = mapping.minus
#                elif isinstance(arg, PlusInterfaceOperator):
#                    arg = arg.args[0]
#                    mapping = mapping.plus
#                else:
#                    raise TypeError(arg)
#            v   = cls.eval(grad(expr.args[0]), domain)
#            v   = mapping.jacobian.inv().T*grad(v)
#            return v

        elif isinstance(expr, (dot, inner, outer)):
            args = [cls.eval(arg, domain) for arg in expr.args]
            return type(expr)(*args)

        elif isinstance(expr, _diff_ops):
            raise NotImplementedError('TODO')

        # TODO MUST BE MOVED AFTER TREATING THE CASES OF GRAD, CURL, DIV IN FEEC
        elif isinstance(expr, (Matrix, ImmutableDenseMatrix)):
            n_rows, n_cols = expr.shape
            lines = []
            for i_row in range(0, n_rows):
                line = []
                for i_col in range(0, n_cols):
                    line.append(cls.eval(expr[i_row, i_col], domain))
                lines.append(line)
            return type(expr)(lines)

        elif isinstance(expr, dx):
            if expr.atoms(PlusInterfaceOperator):
                mapping = mapping.plus
            elif expr.atoms(MinusInterfaceOperator):
                mapping = mapping.minus

            arg = expr.args[0]
            arg = cls(arg, domain, evaluate=True)

            if isinstance(arg, PullBack):
                arg = TerminalExpr(arg, domain=domain.logical_domain)
            elif isinstance(arg, MatrixElement):
                arg = TerminalExpr(arg, domain=domain.logical_domain)
            # ...
            if dim == 1:
                lgrad_arg = LogicalGrad_1d(arg)

                if not isinstance(lgrad_arg, (list, tuple, Tuple, Matrix)):
                    lgrad_arg = Tuple(lgrad_arg)

            elif dim == 2:
                lgrad_arg = LogicalGrad_2d(arg)

            elif dim == 3:
                lgrad_arg = LogicalGrad_3d(arg)

            grad_arg = Covariant(mapping, lgrad_arg)
            expr = grad_arg[0]
            return expr

        elif isinstance(expr, dy):
            if expr.atoms(PlusInterfaceOperator):
                mapping = mapping.plus
            elif expr.atoms(MinusInterfaceOperator):
                mapping = mapping.minus

            arg = expr.args[0]
            arg = cls(arg, domain, evaluate=True)
            if isinstance(arg, PullBack):
                arg = TerminalExpr(arg, domain=domain.logical_domain)
            elif isinstance(arg, MatrixElement):
                arg = TerminalExpr(arg, domain=domain.logical_domain)

            # ..p
            if dim == 1:
                lgrad_arg = LogicalGrad_1d(arg)

            elif dim == 2:
                lgrad_arg = LogicalGrad_2d(arg)

            elif dim == 3:
                lgrad_arg = LogicalGrad_3d(arg)

            grad_arg = Covariant(mapping, lgrad_arg)

            expr = grad_arg[1]
            return expr

        elif isinstance(expr, dz):
            if expr.atoms(PlusInterfaceOperator):
                mapping = mapping.plus
            elif expr.atoms(MinusInterfaceOperator):
                mapping = mapping.minus

            arg = expr.args[0]
            arg = cls(arg, domain, evaluate=True)
            if isinstance(arg, PullBack):
                arg = TerminalExpr(arg, domain=domain.logical_domain)
            elif isinstance(arg, MatrixElement):
                arg = TerminalExpr(arg, domain=domain.logical_domain)
            # ...
            if dim == 1:
                lgrad_arg = LogicalGrad_1d(arg)

            elif dim == 2:
                lgrad_arg = LogicalGrad_2d(arg)

            elif dim == 3:
                lgrad_arg = LogicalGrad_3d(arg)

            grad_arg = Covariant(mapping, lgrad_arg)

            expr = grad_arg[2]

            return expr

        elif isinstance(expr, (Symbol, Indexed)):
            return expr

        elif isinstance(expr, NormalVector):
            return expr

        elif isinstance(expr, Pow):
            b = expr.base
            e = expr.exp
            expr = Pow(cls(b, domain), cls(e, domain))
            return expr

        elif isinstance(expr, Trace):
            e = cls.eval(expr.expr, domain)
            bd = expr.boundary.logical_domain
            order = expr.order
            return Trace(e, bd, order)

        elif isinstance(expr, Integral):
            domain = expr.domain
            mapping = domain.mapping

            assert domain is not None

            if expr.is_domain_integral:
                J = mapping.jacobian
                det = sqrt((J.T * J).det())
            else:
                axis = domain.axis
                J = JacobianSymbol(mapping, axis=axis)
                det = sqrt((J.T * J).det())

            body = cls.eval(expr.expr, domain) * det
            domain = domain.logical_domain
            return Integral(body, domain)

        elif isinstance(expr, BilinearForm):
            tests = [get_logical_test_function(a) for a in expr.test_functions]
            trials = [
                get_logical_test_function(a) for a in expr.trial_functions
            ]
            body = cls.eval(expr.expr, domain)
            return BilinearForm((trials, tests), body)

        elif isinstance(expr, LinearForm):
            tests = [get_logical_test_function(a) for a in expr.test_functions]
            body = cls.eval(expr.expr, domain)
            return LinearForm(tests, body)

        elif isinstance(expr, Norm):
            kind = expr.kind
            exponent = expr.exponent
            e = cls.eval(expr.expr, domain)
            domain = domain.logical_domain
            norm = Norm(e, domain, kind, evaluate=False)
            norm._exponent = exponent
            return norm

        elif isinstance(expr, DomainExpression):
            domain = expr.target
            J = domain.mapping.jacobian
            newexpr = cls.eval(expr.expr, domain)
            newexpr = TerminalExpr(newexpr, domain=domain)
            domain = domain.logical_domain
            det = TerminalExpr(sqrt((J.T * J).det()), domain=domain)
            return DomainExpression(domain,
                                    ImmutableDenseMatrix([[newexpr * det]]))

        elif isinstance(expr, Function):
            args = [cls.eval(a, domain) for a in expr.args]
            return type(expr)(*args)

        return cls(expr, domain, evaluate=False)