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))))
def test_bilinear_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']]

    # ...
    a = BilinearExpr((u,v), dot(u,v))
    print(a)
    print(a.expr)
    print(a(u1,v1))
    # TODO
#    print(a(u1+u2,v1+v2))
    print('')
    # ...

    # ...
    a1 = BilinearExpr((u,v), dot(u,v))
    a2 = BilinearExpr((u,v), inner(grad(u),grad(v)))
    print(a1(u1,v1) + a2(u2,v2))
    print('')
Exemple #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('')
Exemple #4
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)
Exemple #5
0
def test_newton_2d_1():

    # ... abstract model
    B1 = Boundary(r'\Gamma_1', domain)

    V = ScalarFunctionSpace('V', domain)

    x, y = domain.coordinates

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

    int_0 = lambda expr: integral(domain, expr)

    f = -4. * exp(-Un)
    l = LinearForm(v, int_0(dot(grad(v), grad(Un)) - f * v))

    u = element_of(V, name='u')
    eq = NewtonIteration(l, Un, trials=u)
    # ...

    # ...
    expected = int_0(-4.0 * u * v * exp(-Un) + dot(grad(u), grad(v)))

    assert (eq.lhs.expr == expected)
    # ...

    # ...
    bc = EssentialBC(u, 0, B1)
    eq = NewtonIteration(l, Un, bc=bc, trials=u)
Exemple #6
0
def test_equation_2d_3():

    V = ScalarFunctionSpace('V', domain)

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

    x, y = domain.coordinates

    B1 = Boundary(r'\Gamma_1', domain)

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

    # ... bilinear/linear forms
    a1 = BilinearForm((v, u), int_0(dot(grad(v), grad(u))))
    a2 = BilinearForm((v, u), int_0(v * u))

    l1 = LinearForm(v, int_0(x * y * v))
    l2 = LinearForm(v, int_0(cos(x + y) * v))
    # ...

    # ...
    bc = EssentialBC(u, 0, B1)
    eq = Equation(a1, l1, tests=v, trials=u, bc=bc)
    # ...

    # ...
    nn = NormalVector('nn')
    bc = EssentialBC(dot(grad(u), nn), 0, B1)
    eq = Equation(a1, l1, tests=v, trials=u, bc=bc)
Exemple #7
0
def test_equation_2d_4():

    V = VectorFunctionSpace('V', domain)

    v = element_of(V, name='v')
    u = element_of(V, name='u')
    x, y = domain.coordinates

    B1 = Boundary(r'\Gamma_1', domain)

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

    # ... bilinear/linear forms
    a1 = BilinearForm((v, u), int_0(inner(grad(v), grad(u))))

    f = Tuple(x * y, sin(pi * x) * sin(pi * y))
    l1 = LinearForm(v, int_0(dot(f, v)))
    # ...

    # ...
    bc = EssentialBC(u, 0, B1)
    eq = Equation(a1, l1, tests=v, trials=u, bc=bc)
    # ...

    # ...
    bc = EssentialBC(u[0], 0, B1)
    eq = Equation(a1, l1, tests=v, trials=u, bc=bc)
    # ...

    # ...
    nn = NormalVector('nn')
    bc = EssentialBC(dot(u, nn), 0, B1)
    eq = Equation(a1, l1, tests=v, trials=u, bc=bc)
Exemple #8
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('')
Exemple #9
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('')
Exemple #10
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)
Exemple #11
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
Exemple #12
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))
def test_linearize_expr_2d_1():
    domain = Domain('Omega', dim=2)
    x,y = domain.coordinates

    V1 = ScalarFunctionSpace('V1', domain)
    W1 = VectorFunctionSpace('W1', domain)

    v1 = element_of(V1, name='v1')
    w1 = element_of(W1, name='w1')

    alpha = Constant('alpha')

    F = element_of(V1, name='F')
    G = element_of(W1, 'G')


    # ...
    l = LinearExpr(v1, F**2*v1)
    a = linearize(l, F, trials='u1')
    print(a)
    # ...

    # ...
    l = LinearExpr(v1, dot(grad(F), grad(F))*v1)
    a = linearize(l, F, trials='u1')
    print(a)
    # ...

    # ...
    l = LinearExpr(v1, exp(-F)*v1)
    a = linearize(l, F, trials='u1')
    print(a)
    # ...

    # ...
    l = LinearExpr(v1, cos(F)*v1)
    a = linearize(l, F, trials='u1')
    print(a)
    # ...

    # ...
    l = LinearExpr(v1, cos(F**2)*v1)
    a = linearize(l, F, trials='u1')
    print(a)
    # ...

    # ...
    l = LinearExpr(v1, F**2*dot(grad(F), grad(v1)))
    a = linearize(l, F, trials='u1')
    print(a)
    # ...

    # ...
    l = LinearExpr(w1, dot(rot(G), grad(G))*w1)
    a = linearize(l, G, trials='u1')
    print(a)
Exemple #14
0
def test_linearize_2d_1():
    domain = Domain('Omega', dim=DIM)
    x, y = domain.coordinates

    V1 = FunctionSpace('V1', domain)
    W1 = VectorFunctionSpace('W1', domain)

    v1 = TestFunction(V1, name='v1')
    w1 = VectorTestFunction(W1, name='w1')

    alpha = Constant('alpha')

    F = Field('F', space=V1)
    G = VectorField(W1, 'G')

    # ...
    l = LinearForm(v1, F**2 * v1, check=True)
    a = linearize(l, F, trials='u1')
    print(a)
    # ...

    # ...
    l = LinearForm(v1, dot(grad(F), grad(F)) * v1, check=True)
    a = linearize(l, F, trials='u1')
    print(a)
    # ...

    # ...
    l = LinearForm(v1, exp(-F) * v1, check=True)
    a = linearize(l, F, trials='u1')
    print(a)
    # ...

    # ...
    l = LinearForm(v1, cos(F) * v1, check=True)
    a = linearize(l, F, trials='u1')
    print(a)
    # ...

    # ...
    l = LinearForm(v1, cos(F**2) * v1, check=True)
    a = linearize(l, F, trials='u1')
    print(a)
    # ...

    # ...
    l = LinearForm(v1, F**2 * dot(grad(F), grad(v1)), check=True)
    a = linearize(l, F, trials='u1')
    print(a)
    # ...

    # ...
    l = LinearForm(w1, dot(rot(G), grad(G)) * w1, check=True)
    a = linearize(l, G, trials='u1')
    print(a)
Exemple #15
0
def test_essential_bc_1():
    domain = Domain('Omega', dim=2)

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

    v = element_of(V, name='v')
    w = element_of(W, name='w')

    B1 = Boundary(r'\Gamma_1', domain)
    nn = NormalVector('nn')

    # ... scalar case
    bc = EssentialBC(v, 0, B1)

    assert (bc.variable == v)
    assert (bc.order == 0)
    assert (bc.normal_component == False)
    assert (bc.index_component == None)
    # ...

    # ... scalar case
    bc = EssentialBC(dot(grad(v), nn), 0, B1)

    assert (bc.variable == v)
    assert (bc.order == 1)
    assert (bc.normal_component == False)
    assert (bc.index_component == None)
    # ...

    # ... vector case
    bc = EssentialBC(w, 0, B1)
    assert (bc.variable == w)
    assert (bc.order == 0)
    assert (bc.normal_component == False)
    assert (bc.index_component == [0, 1])
    # ...

    # ... vector case
    bc = EssentialBC(dot(w, nn), 0, B1)

    assert (bc.variable == w)
    assert (bc.order == 0)
    assert (bc.normal_component == True)
    assert (bc.index_component == None)
    # ...

    # ... vector case
    bc = EssentialBC(w[0], 0, B1)
    assert (bc.variable == w)
    assert (bc.order == 0)
    assert (bc.normal_component == False)
    assert (bc.index_component == [0])
Exemple #16
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('')
Exemple #17
0
def test_system_2d():

    domain = Square()

    V = FunctionSpace('V', domain)
    x, y = V.coordinates

    u, v, p, q = [TestFunction(V, name=i) for i in ['u', 'v', 'p', 'q']]

    a1, a2, b1, b2 = [Constant(i, real=True) for i in ['a1', 'a2', 'b1', 'b2']]

    # ...
    a = BilinearForm((v, u), dot(grad(u), grad(v)))
    m = BilinearForm((v, u), u * v)

    expr = a(p, u) + a1 * m(p, u) + b1 * m(p, v) + a(
        q, v) + a2 * m(q, u) + b2 * m(q, v)
    b = BilinearForm(((p, q), (u, v)), expr)

    print(evaluate(b, verbose=True))
    # ...

    # ...
    f1 = x * y
    f2 = x + y
    l1 = LinearForm(p, f1 * p)
    l2 = LinearForm(q, f2 * q)

    expr = l1(p) + l2(q)
    l = LinearForm((p, q), expr)

    print(evaluate(l, verbose=True))
Exemple #18
0
def test_calls_2d_3():

    domain = Square()

    V = FunctionSpace('V', domain)

    x, y = domain.coordinates

    pn = Field('pn', V)
    wn = Field('wn', V)

    dp = TestFunction(V, name='dp')
    dw = TestFunction(V, name='dw')
    tau = TestFunction(V, name='tau')
    sigma = TestFunction(V, name='sigma')

    Re = Constant('Re', real=True)
    dt = Constant('dt', real=True)
    alpha = Constant('alpha', real=True)

    l1 = LinearForm(tau,
                    bracket(pn, wn) * tau - 1. / Re * dot(grad(tau), grad(wn)))

    # ...
    l = LinearForm((tau, sigma), dt * l1(tau))

    print(evaluate(l, verbose=True))
Exemple #19
0
def test_assembly_bilinear_form_2d_1():
    a = BilinearForm((u,v), integral(domain, dot(grad(u), grad(v))))
    ast = AST(a)

    stmt = parse(ast.expr, settings={'dim': ast.dim, 'nderiv': ast.nderiv})
    print(pycode(stmt))
    print()
Exemple #20
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)
Exemple #21
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 = FunctionSpace('V', domain)

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

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

    print(a)
    print(evaluate(a, verbose=True))
    print('')
    # ...

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

    print(l)
    print(evaluate(l, verbose=True))
    print('')
Exemple #22
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('')
Exemple #23
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('')
Exemple #24
0
def test_logical_expr_2d_3():
    dim = 2

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

    M1 = Mapping('M1', dim=dim)
    M2 = Mapping('M2', dim=dim)

    D1 = M1(A)
    D2 = M2(B)

    domain = D1.join(D2,
                     name='domain',
                     bnd_minus=D1.get_boundary(axis=0, ext=1),
                     bnd_plus=D2.get_boundary(axis=0, ext=-1))

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

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

    int_0 = lambda expr: integral(domain, expr)

    expr = LogicalExpr(int_0(dot(u, v)), domain)
    assert str(
        expr.args[0]
    ) == 'Integral(A, Dot((Jacobian(M1)**(-1)).T * u, (Jacobian(M1)**(-1)).T * v)*sqrt(det(Jacobian(M1).T * Jacobian(M1))))'
    assert str(
        expr.args[1]
    ) == 'Integral(B, Dot((Jacobian(M2)**(-1)).T * u, (Jacobian(M2)**(-1)).T * v)*sqrt(det(Jacobian(M2).T * Jacobian(M2))))'
Exemple #25
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('')
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)
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('')
Exemple #28
0
def test_bilinear_form_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)

    V = FunctionSpace('V', domain)

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

    # ...
    d_forms = {}

    d_forms['a1'] = BilinearForm((u, v), u * v)
    d_forms['a2'] = BilinearForm((u, v), u * v + dot(grad(u), grad(v)))
    d_forms['a3'] = BilinearForm((u, v), v * trace_1(grad(u), B1))

    # Poisson with Nitsch method
    a0 = BilinearForm((u, v), dot(grad(u), grad(v)))
    a_B1 = BilinearForm(
        (u, v), -kappa * u * trace_1(grad(v), B1) - v * trace_1(grad(u), B1) +
        trace_0(u, B1) * trace_0(v, B1) / eps)
    a = BilinearForm((u, v), a0(u, v) + a_B1(u, v))

    d_forms['a4'] = a
    # ...

    # ... calls
    d_calls = {}
    for name, a in d_forms.items():
        d_calls[name] = a(u1, v1)
    # ...

    # ... export forms
    for name, expr in d_forms.items():
        export(expr, 'biform_2d_{}.png'.format(name))
    # ...

    # ... export calls
    for name, expr in d_calls.items():
        export(expr, 'biform_2d_call_{}.png'.format(name))
Exemple #29
0
def test_tensorize_3d():

    V = FunctionSpace('V', domain)
    U = FunctionSpace('U', domain)
    W1 = VectorFunctionSpace('W1', domain)
    T1 = VectorFunctionSpace('T1', domain)

    v = TestFunction(V, name='v')
    u = TestFunction(U, name='u')
    w1 = VectorTestFunction(W1, name='w1')
    t1 = VectorTestFunction(T1, name='t1')

    x,y,z = domain.coordinates

    alpha = Constant('alpha')

    # ...
    expr = dot(grad(v), grad(u))
    a = BilinearForm((v,u), expr, name='a')
    print(a)
    print(tensorize(a))
    print('')
    # ...

    # ...
    expr = x*dx(v)*dx(u) + y*dy(v)*dy(u)
    a = BilinearForm((v,u), expr, name='a')
    print(a)
    print(tensorize(a))
    print('')
    # ...

    # ...
    expr = sin(x)*dx(v)*dx(u)
    a = BilinearForm((v,u), expr, name='a')
    print(a)
    print(tensorize(a))
    print('')
    # ...

    # ...
    expr = dot(curl(w1), curl(t1)) + div(w1)*div(t1)
    a = BilinearForm((w1, t1), expr, name='a')
    print(a)
    print(tensorize(a))
    print('')
Exemple #30
0
def test_bilinearity_2d_1():
    domain = Square()
    x, y = domain.coordinates

    alpha = Constant('alpha')
    beta = Constant('beta')

    f1 = x * y
    f2 = x + y
    f = Tuple(f1, f2)

    V = FunctionSpace('V', domain)

    # TODO improve: naming are not given the same way
    G = Field('G', V)

    p, q = [TestFunction(V, name=i) for i in ['p', 'q']]

    #####################################
    # linear expressions
    #####################################
    # ...
    expr = p * q
    assert (is_bilinear_form(expr, (p, q)))
    # ...

    # ...
    expr = dot(grad(p), grad(q))
    assert (is_bilinear_form(expr, (p, q)))
    # ...

    # ...
    expr = alpha * dot(grad(p),
                       grad(q)) + beta * p * q + laplace(p) * laplace(q)
    assert (is_bilinear_form(expr, (p, q)))
    # ...
    #####################################

    #####################################
    # nonlinear expressions
    #####################################
    # ...
    with pytest.raises(UnconsistentLinearExpressionError):
        expr = alpha * dot(grad(p**2), grad(q)) + beta * p * q
        is_bilinear_form(expr, (p, q))