Example #1
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('')
Example #2
0
def test_evaluation_2d_1():
    domain = Domain('Omega', dim=2)
    B_neumann = Boundary(r'\Gamma_1', domain)

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

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

    alpha = Constant('alpha')

    x, y = V.coordinates
    F = Field('F', space=V)

    a1 = BilinearForm((p, q), dot(grad(p), grad(q)))
    m = BilinearForm((p, q), p * q)
    a2 = BilinearForm((p, q), a1(p, q) + alpha * m(p, q))
    a3 = BilinearForm((u, v), rot(u) * rot(v) + alpha * div(u) * div(v))

    a11 = BilinearForm((v, u), inner(grad(v), grad(u)))
    a12 = BilinearForm((v, p), div(v) * p)
    a4 = BilinearForm(((v, q), (u, p)), a11(v, u) - a12(v, p) + a12(u, q))

    l0 = LinearForm(p, F * p)
    l_neu = LinearForm(p, p * trace_1(grad(F), B_neumann))
    l = LinearForm(p, l0(p) + l_neu(p))

    # ...
    print(a1)
    print(evaluate(a1))
    print('')
    # ...

    # ...
    print(a2)
    print(evaluate(a2))
    print('')
    # ...

    # ...
    print(a3)
    print(evaluate(a3))
    print('')
    # ...

    # ...
    print(a4)
    print(evaluate(a4))
    print('')
    # ...

    # ...
    print(l)
    print(evaluate(l))
    print('')
Example #3
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 #4
0
def test_tensorize_2d():
    domain = Domain('Omega', dim=DIM)

    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 = 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 = rot(w1)*rot(t1) + div(w1)*div(t1)
    expr = rot(w1) * rot(t1)  #+ div(w1)*div(t1)
    a = BilinearForm((w1, t1), expr, name='a')
    print(a)
    print(tensorize(a))
    print('')
Example #5
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 #6
0
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)
Example #7
0
def test_curldiv_2d():

    domain = Square()

    W1 = VectorFunctionSpace('W1', domain)
    T1 = VectorFunctionSpace('T1', domain)

    w1 = VectorTestFunction(W1, name='w1')
    t1 = VectorTestFunction(T1, name='t1')

    mu = Constant('mu')

    # ...
    a = BilinearForm((w1, t1),
                     rot(w1) * rot(t1) + mu * div(w1) * div(t1),
                     name='a')
    print(a)
    print(atomize(a))
    print(evaluate(a))
Example #8
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)
Example #9
0
def test_calls_2d():
    domain = Domain('Omega', dim=DIM)

    V1 = FunctionSpace('V1', domain)
    V2 = FunctionSpace('V2', domain)
    U1 = FunctionSpace('U1', domain)
    U2 = FunctionSpace('U2', domain)
    W1 = VectorFunctionSpace('W1', domain)
    W2 = VectorFunctionSpace('W2', domain)
    T1 = VectorFunctionSpace('T1', domain)
    T2 = VectorFunctionSpace('T2', domain)

    v1 = TestFunction(V1, name='v1')
    v2 = TestFunction(V2, name='v2')
    u1 = TestFunction(U1, name='u1')
    u2 = TestFunction(U2, name='u2')
    w1 = VectorTestFunction(W1, name='w1')
    w2 = VectorTestFunction(W2, name='w2')
    t1 = VectorTestFunction(T1, name='t1')
    t2 = VectorTestFunction(T2, name='t2')

    V = ProductSpace(V1, V2)
    U = ProductSpace(U1, U2)

    x, y = V1.coordinates

    alpha = Constant('alpha')

    F = Field('F', space=V1)

    # ...
    a1 = BilinearForm((v1, u1), u1 * v1, name='a1')
    print(a1)
    print(atomize(a1))
    print(evaluate(a1))
    print('')

    expr = a1(v2, u2)
    a = BilinearForm((v2, u2), expr, name='a')
    print(a)
    print(atomize(a))
    print(evaluate(a))
    print('')
    # ...

    # ...
    a = BilinearForm((v1, u1), dot(grad(v1), grad(u1)), name='a')

    print(a)
    print(atomize(a))
    print(evaluate(a))
    print('')
    # ...

    # ...
    a1 = BilinearForm((v1, u1), dot(grad(v1), grad(u1)), name='a1')

    expr = a1(v2, u2)
    a = BilinearForm((v2, u2), expr, name='a')
    print(a)
    print(atomize(a))
    print(evaluate(a))
    print('')
    # ...

    # ...
    a1 = BilinearForm((v1, u1), u1 * v1, name='a1')
    a2 = BilinearForm((v1, u1), dx(u1) * dx(v1), name='a2')

    expr = a1(v2, u2) + a2(v2, u2)
    a = BilinearForm((v2, u2), expr, name='a')
    print(a)
    print(atomize(a))
    print(evaluate(a))
    print('')
    # ...

    # ...
    a1 = BilinearForm((v1, u1), u1 * v1, name='a1')
    a2 = BilinearForm((v1, u1), dx(u1) * dx(v1), name='a2')

    expr = a1(v1, u2)
    print('> before = ', expr)
    expr = expr.subs(u2, u1)
    print('> after  = ', expr)
    print('')

    expr = a1(v1, u2) + a1(v2, u2)
    print('> before = ', expr)
    expr = expr.subs(u2, u1)
    print('> after  = ', expr)
    print('')
    # ...

    # ...
    a1 = BilinearForm((v1, u1), u1 * v1, name='a1')
    a2 = BilinearForm((v1, u1), dx(u1) * dx(v1), name='a2')

    expr = a1(v1, u2) + a2(v2, u1)
    a = BilinearForm(((v1, v2), (u1, u2)), expr, name='a')
    print(a)
    print(atomize(a))
    print(evaluate(a))
    print('')
    # ...

    # ...
    a = BilinearForm((w1, t1), rot(w1) * rot(t1) + div(w1) * div(t1), name='a')
    print(a)
    print(atomize(a))
    print(evaluate(a))
    # ...

    # ...
    a1 = BilinearForm((v1, u1), u1 * v1, name='a1')
    a2 = BilinearForm((v1, u1), dx(u1) * dx(v1), name='a2')
    a3 = BilinearForm((w1, t1),
                      rot(w1) * rot(t1) + div(w1) * div(t1),
                      name='a3')
    a4 = BilinearForm((w1, u1), div(w1) * u1, name='a4')

    expr = a3(w2, t2) + a2(v2, u2) + a4(w2, u2)
    a = BilinearForm(((w2, v2), (t2, u2)), expr, name='a')
    print(a)
    print(atomize(a))
    print(evaluate(a))
    # ...

    # ...
    a1 = BilinearForm((v1, u1), laplace(u1) * laplace(v1), name='a1')
    print(a1)
    print(atomize(a1))
    print(evaluate(a1))
    print('')
    # ...

    # ...
    a1 = BilinearForm((v1, u1), inner(hessian(u1), hessian(v1)), name='a1')
    print('================================')
    print(a1)
    print(atomize(a1))
    print(evaluate(a1))
    print('')
    # ...

    # ...
    l1 = LinearForm(v1, x * y * v1, name='11')

    expr = l1(v2)
    l = LinearForm(v2, expr, name='1')
    print(l)
    print(atomize(l))
    print(evaluate(l))
    # ...

    # ...
    l1 = LinearForm(v1, x * y * v1, name='l1')
    l2 = LinearForm(v2, cos(x + y) * v2, name='l2')

    expr = l1(u1) + l2(u2)
    l = LinearForm((u1, u2), expr, name='1')
    print(l)
    print(atomize(l))
    print(evaluate(l))
    # ...

    # ...
    l1 = LinearForm(v1, x * y * v1, name='l1')
    l2 = LinearForm(v2, cos(x + y) * v2, name='l2')

    expr = l1(u1) + alpha * l2(u2)
    l = LinearForm((u1, u2), expr, name='1')
    print(l)
    print(atomize(l))
    print(evaluate(l))
    # ...

    # ...
    l1 = LinearForm(v1, x * y * v1, name='l1')
    l2 = LinearForm(w1, div(w1), name='l2')

    expr = l1(v2) + l2(w2)
    l = LinearForm((v2, w2), expr, name='1')
    print(l)
    print(atomize(l))
    print(evaluate(l))
    # ...

    # ...
    I1 = Integral(x * y, domain, name='I1')

    print(I1)
    print(atomize(I1))
    print(evaluate(I1))
    # ...

    # ...
    expr = F - cos(2 * pi * x) * cos(3 * pi * y)
    expr = dot(grad(expr), grad(expr))
    I2 = Integral(expr, domain, name='I2')

    print(I2)
    print(atomize(I2))
    print(evaluate(I2))
    # ...

    # ...
    expr = F - cos(2 * pi * x) * cos(3 * pi * y)
    expr = dot(grad(expr), grad(expr))
    I2 = Integral(expr, domain, name='I2')

    print(I2)
    print(atomize(I2))
    print(evaluate(I2))
    # ...

    # ... stokes
    V = VectorFunctionSpace('V', domain)
    W = FunctionSpace('W', domain)

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

    a = BilinearForm((v, u), inner(grad(v), grad(u)), name='a')
    b = BilinearForm((v, p), div(v) * p, name='b')
    A = BilinearForm(((v, q), (u, p)), a(v, u) - b(v, p) + b(u, q), name='A')

    print(A)
    print(atomize(A))
    print(evaluate(A))
Example #10
0
def test_bilinearity_2d_2():
    domain = Domain('Omega', dim=DIM)

    V1 = FunctionSpace('V1', domain)
    V2 = FunctionSpace('V2', domain)
    U1 = FunctionSpace('U1', domain)
    U2 = FunctionSpace('U2', domain)
    W1 = VectorFunctionSpace('W1', domain)
    W2 = VectorFunctionSpace('W2', domain)
    T1 = VectorFunctionSpace('T1', domain)
    T2 = VectorFunctionSpace('T2', domain)

    v1 = TestFunction(V1, name='v1')
    v2 = TestFunction(V2, name='v2')
    u1 = TestFunction(U1, name='u1')
    u2 = TestFunction(U2, name='u2')
    w1 = VectorTestFunction(W1, name='w1')
    w2 = VectorTestFunction(W2, name='w2')
    t1 = VectorTestFunction(T1, name='t1')
    t2 = VectorTestFunction(T2, name='t2')

    V = ProductSpace(V1, V2)
    U = ProductSpace(U1, U2)

    x, y = V1.coordinates

    alpha = Constant('alpha')

    F = Field('F', space=V1)

    # ...
    a1 = BilinearForm((v1, u1), u1 * v1, check=True)
    a = BilinearForm((v2, u2), a1(v2, u2), check=True)
    # ...

    # ...
    a = BilinearForm((v1, u1), dot(grad(v1), grad(u1)), check=True)
    # ...

    # ...
    a1 = BilinearForm((v1, u1), dot(grad(v1), grad(u1)), check=True)
    a = BilinearForm((v2, u2), a1(v2, u2), check=True)
    # ...

    # ...
    a1 = BilinearForm((v1, u1), u1 * v1)
    a2 = BilinearForm((v1, u1), dx(u1) * dx(v1), check=True)
    a = BilinearForm((v2, u2), a1(v2, u2) + a2(v2, u2), check=True)
    # ...

    # ...
    a1 = BilinearForm((v1, u1), u1 * v1, check=True)
    a2 = BilinearForm((v1, u1), dx(u1) * dx(v1), check=True)
    # ...

    # ...
    a1 = BilinearForm((v1, u1), u1 * v1, check=True)
    a2 = BilinearForm((v1, u1), dx(u1) * dx(v1), check=True)
    a = BilinearForm(((v1, v2), (u1, u2)), a1(v1, u2) + a2(v2, u1), check=True)
    # ...

    # ...
    a = BilinearForm((w1, t1),
                     rot(w1) * rot(t1) + div(w1) * div(t1),
                     check=True)
    # ...

    # ...
    a1 = BilinearForm((v1, u1), u1 * v1, check=True)
    a2 = BilinearForm((v1, u1), dx(u1) * dx(v1), check=True)
    a3 = BilinearForm((w1, t1),
                      rot(w1) * rot(t1) + div(w1) * div(t1),
                      check=True)
    a4 = BilinearForm((w1, u1), div(w1) * u1, check=True)

    a = BilinearForm(((w2, v2), (t2, u2)),
                     a3(w2, t2) + a2(v2, u2) + a4(w2, u2),
                     check=True)
    # ...

    # ...
    a1 = BilinearForm((v1, u1), laplace(u1) * laplace(v1), check=True)
    # ...

    # ...
    a1 = BilinearForm((v1, u1), inner(hessian(u1), hessian(v1)), check=True)
    # ...

    # ... stokes
    V = VectorFunctionSpace('V', domain)
    W = FunctionSpace('W', domain)

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

    a = BilinearForm((v, u), inner(grad(v), grad(u)), check=True)
    b = BilinearForm((v, p), div(v) * p, check=True)
    A = BilinearForm(((v, q), (u, p)), a(v, u) - b(v, p) + b(u, q), check=True)
    # ...

    ################################
    #    non bilinear forms
    ################################
    # ...
    with pytest.raises(UnconsistentLinearExpressionError):
        a = BilinearForm((v1, u1), dot(grad(v1), grad(u1)) + v1, check=True)
    # ...

    # ...
    with pytest.raises(UnconsistentLinearExpressionError):
        a = BilinearForm((v1, u1), v1**2 * u1, check=True)
    # ...

    # ...
    with pytest.raises(UnconsistentLinearExpressionError):
        a = BilinearForm((v1, u1), dot(grad(v1), grad(v1)), check=True)