Пример #1
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('')
Пример #2
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))
Пример #3
0
def test_boundary_2d_2():
    Omega_1 = InteriorDomain('Omega_1', dim=2)

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

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

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

    x, y = V.coordinates

    alpha = Constant('alpha')

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

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

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

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

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

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

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

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

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

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

    print(evaluate(l4, verbose=VERBOSE))
    print('')
Пример #4
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))
Пример #5
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('')
Пример #6
0
def test_projection_2d():
    domain = Domain('Omega', dim=DIM)

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

    alpha = Constant('alpha')

    u = Projection(x**2 + alpha * y, V, name='u')
Пример #7
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('')
Пример #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)
Пример #9
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 = FunctionSpace('V', domain)

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

    # ...
    a = BilinearForm((v, u), area * u * v)
Пример #10
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))
Пример #11
0
def test_expr_mapping_2d():

    F = Mapping('F', DIM)
    patch = Domain('Omega', dim=DIM)
    domain = F(patch)

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

    x, y = V.coordinates

    a = BilinearForm((v, u), dot(grad(v), grad(u)))
    assert (a.mapping is F)

    l = LinearForm(v, x * y * v)
    assert (l.mapping is F)
Пример #12
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))
Пример #13
0
def test_linearize_form_2d_3():
    """steady Euler equation."""
    domain = Domain('Omega', dim=2)
    x, y = domain.coordinates

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

    v = VectorTestFunction(U, name='v')
    phi = TestFunction(W, name='phi')
    q = TestFunction(W, name='q')

    U_0 = VectorField(U, name='U_0')
    Rho_0 = Field(W, name='Rho_0')
    P_0 = Field(W, name='P_0')

    # ...
    expr = div(Rho_0 * U_0) * phi
    l1 = LinearForm(phi, expr)

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

    expr = dot(U_0, grad(P_0)) * q + P_0 * div(U_0) * q
    l3 = LinearForm(q, expr)
    # ...

    a1 = linearize(l1, [Rho_0, U_0], trials=['d_rho', 'd_u'])
    print(a1)
    print('')

    a2 = linearize(l2, [Rho_0, U_0, P_0], trials=['d_rho', 'd_u', 'd_p'])
    print(a2)
    print('')

    a3 = linearize(l3, [P_0, U_0], trials=['d_p', 'd_u'])
    print(a3)
    print('')

    l = LinearForm((phi, v, q), l1(phi) + l2(v) + l3(q))
    a = linearize(l, [Rho_0, U_0, P_0], trials=['d_rho', 'd_u', 'd_p'])
    print(a)

    export(a, 'steady_euler.png')
Пример #14
0
def test_bilinear_form_2d_3():

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

    x, y = domain.coordinates

    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((u, v), inner(grad(v), grad(u)))
    b = BilinearForm((v, p), div(v) * p)
    A = BilinearForm(((u, p), (v, q)), a(v, u) - b(v, p) + b(u, q))

    export(A, 'stokes_2d.png')
Пример #15
0
def test_tensorize_2d_stokes():
    domain = Domain('Omega', dim=DIM)

    # ... abstract model
    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(tensorize(A))
    print('')
Пример #16
0
def test_linear_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']]

    g = Tuple(x**2, y**2)

    # ...
    d_forms = {}

    d_forms['l1'] = LinearForm(v, x * y * v)
    d_forms['l2'] = LinearForm(v, v * trace_1(g, B1))
    d_forms['l3'] = LinearForm(v, v * trace_1(g, B1) + x * y * v)

    # nitsche
    f = Function('f')
    g = Function('g')

    l0 = LinearForm(v, f(x, y) * v)
    l_B1 = LinearForm(
        v,
        g(x, y) * trace_0(v, B1) / eps -
        kappa * g(x, y) * trace_1(grad(v), B1))

    d_forms['l4'] = LinearForm(v, l0(v) + l_B1(v))
    # ...

    # ...
    for name, expr in d_forms.items():
        export(expr, 'linform_2d_{}.png'.format(name))
Пример #17
0
def test_calls_2d_2():

    domain = Square()

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

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

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

    expr = a(v, Un)
    print(evaluate(expr, verbose=True))
    # ...

    # ...
    l = LinearForm(v, a(v, Un))

    print(evaluate(l, verbose=True))
Пример #18
0
def test_linearize_2d_2():
    domain = Domain('Omega', dim=DIM)
    x, y = domain.coordinates

    V1 = FunctionSpace('V1', domain)

    v1 = TestFunction(V1, name='v1')

    alpha = Constant('alpha')

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

    # ...
    l1 = LinearForm(v1, F**2 * v1, check=True)
    l = LinearForm(v1, l1(v1))

    a = linearize(l, F, trials='u1')
    print(a)

    expected = linearize(l1, F, trials='u1')
    assert (linearize(l, F, trials='u1') == expected)
Пример #19
0
def test_evaluation_2d_2():
    domain = Square()
    x, y = domain.coordinates

    f0 = Tuple(2 * pi**2 * sin(pi * x) * sin(pi * y),
               2 * pi**2 * sin(pi * x) * sin(pi * y))

    f1 = cos(pi * x) * cos(pi * y)

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

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

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

    a0 = BilinearForm((v, u), inner(grad(v), grad(u)))
    a1 = BilinearForm((q, p), p * q)
    a = BilinearForm(((v, q), (u, p)), a0(v, u) + a1(q, p))

    l0 = LinearForm(v, dot(f0, v))
    l1 = LinearForm(q, f1 * q)
    l = LinearForm((v, q), l0(v) + l1(q))

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

    # ...
    print(l)
    print(evaluate(l))
    print('')
Пример #20
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)
Пример #21
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 = Tuple(b1, b2)

    # right hand side
    f = x * y

    e = ElementDomain()
    area = Area(e)

    V = FunctionSpace('V', domain)

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

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

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

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

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

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

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

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

    expr = -f * (dot(b, grad(v)) + kappa * laplace(v))
    l3 = LinearForm(v, 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(evaluate(a1, verbose=True))
    print('')

    print(a2)
    print(evaluate(a2, verbose=True))
    print('')

    print(a3)
    print(evaluate(a3, verbose=True))
    print('')
Пример #22
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))
Пример #23
0
def test_norm_2d():
    domain = Domain('Omega', dim=DIM)

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

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

    print('> l2 norm = ', evaluate(l2_norm_u))
    print('> h1 norm = ', evaluate(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 = ', evaluate(l2_norm_u))
    print('> h1 norm = ', evaluate(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 = ', evaluate(l2_norm_u))
    print('> h1 norm = ', evaluate(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 = ', evaluate(l2_norm_u))
    print('> h1 norm = ', evaluate(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 = ', evaluate(l2_norm_u))
    print('> h1 norm = ', evaluate(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 = ', evaluate(l2_norm_u))
    print('> h1 norm = ', evaluate(h1_norm_u))
    print('')
Пример #24
0
def test_boundary_2d_1():
    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')

    x, y = V1.coordinates

    alpha = Constant('alpha')

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

    # ...
    with pytest.raises(UnconsistentError):
        expr = dot(grad(v1), grad(u1)) + v1 * trace_0(u1, B1)
        a = BilinearForm((v1, u1), expr, name='a')
    # ...

    # ...
    with pytest.raises(UnconsistentError):
        expr = v1 * trace_0(u1, B3) + v1 * trace_1(grad(u1),
                                                   B3) + u1 * trace_0(v1, B2)
        a1 = BilinearForm((v1, u1), expr, name='a1')
    # ...

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

    expr = v1 * trace_0(u1, B1)
    a_bnd = BilinearForm((v1, u1), expr, name='a_bnd')

    expr = a_0(v1, u1) + a_bnd(v1, u1)
    a = BilinearForm((v1, u1), expr, name='a')
    print(a)
    print(evaluate(a, verbose=True))
    print('')
    #    import sys; sys.exit(0)
    # ...

    # ...
    expr = v1 * trace_0(u1, B1) + v1 * trace_1(grad(u1), B1)
    a1 = BilinearForm((v1, u1), expr, name='a1')

    expr = u1 * trace_1(grad(v1), B2)
    a2 = BilinearForm((v1, u1), expr, name='a2')

    expr = a1(v2, u2) + a2(v2, u2)
    # as expected, we can define the form call, but we cannot create a Bilinear
    # form out of it.
    # TODO add assert on exception type
    #    a = BilinearForm((v2, u2), expr, name='a')

    print(expr)
    print('')
    # ...

    # ...
    expr = v1 * trace_0(u1, B1)
    a0 = BilinearForm((v1, u1), expr, name='a0')

    expr = v1 * trace_1(grad(u1), B1)
    a1 = BilinearForm((v1, u1), expr, name='a1')

    expr = v1 * trace_1(grad(u1), B2)
    a2 = BilinearForm((v1, u1), expr, name='a2')

    expr = dot(grad(u1), grad(v1))
    a3 = BilinearForm((v1, u1), expr, name='a3')

    expr = u1 * v1
    a4 = BilinearForm((v1, u1), expr, name='a4')

    # TODO Mul not treated yet
    #    expr = a0(v2, u2) + a1(v2, u2) + alpha * a2(v2, u2) + a3(v2, u2) + alpha*a4(v2, u2)
    #    a = BilinearForm((v2, u2), expr, name='a')
    ##    print(expr)
    #    print(evaluate(expr, verbose=True))
    #    print('')
    #
    #    print(evaluate(a, verbose=True))
    #    print('')

    #    expr = a(v2, u2) + a1(v2, u2)
    #    b = BilinearForm((v2, u2), expr, name='b')
    #    print(b)
    #    print(evaluate(b, verbose=True))
    # ...

    # ...
    g = Tuple(x**2, y**2)
    expr = v1 * trace_1(g, B1)
    l1 = LinearForm(v1, expr, name='l1')
    print(l1)
    #    print(atomize(l1))
    #    print(evaluate(l1))
    print('')
Пример #25
0
def test_linearity_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)

    # ...
    l1 = LinearForm(v1, x * y * v1, check=True)

    l = LinearForm(v2, l1(v2), check=True)
    # ...

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

    l = LinearForm((u1, u2), l1(u1) + l2(u2), check=True)
    # ...

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

    l = LinearForm((u1, u2), l1(u1) + alpha * l2(u2), check=True)
    # ...

    # ...
    l1 = LinearForm(v1, x * y * v1, check=True)
    l2 = LinearForm(w1, div(w1), check=True)

    l = LinearForm((v2, w2), l1(v2) + l2(w2), check=True)
    # ...

    ################################
    #    non bilinear forms
    ################################
    # ...
    with pytest.raises(UnconsistentLinearExpressionError):
        l = LinearForm(v1, x * y * v1**2, check=True)
    # ...

    # ...
    with pytest.raises(UnconsistentLinearExpressionError):
        l = LinearForm(v1, x * y, check=True)