コード例 #1
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))
コード例 #2
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('')
コード例 #3
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)
コード例 #4
0
def test_equation_2d_5():
    domain = Square()
    x, y = domain.coordinates

    f0 = Matrix([
        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 = ScalarFunctionSpace('V', domain)
    X = ProductSpace(W, V)

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

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

    int_0 = lambda expr: integral(domain, expr)

    a0 = BilinearForm((v, u), int_0(inner(grad(v), grad(u))))
    print('     a0 done.')
    a1 = BilinearForm((q, p), int_0(p * q))
    print('     a1 done.')
    a = BilinearForm(((v, q), (u, p)), a0(v, u) + a1(q, p))
    print('     a  done.')

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

    print('****************************')
    bc = EssentialBC(u, 0, domain.boundary)
    equation = Equation(a, l, tests=[v, q], trials=[u, p], bc=bc)

    # ...
    print('=======')
    print(equation.lhs.expr)
    print('')
    # ...

    # ...
    print('=======')
    print(equation.rhs.expr)
    print('')
コード例 #5
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('')
コード例 #6
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()
コード例 #7
0
def test_latex_2d_2():

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

    V = VectorFunctionSpace('V', domain)

    x, y = V.coordinates

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

    int_0 = lambda expr: integral(domain, expr)

    assert (latex(v) == r'\mathbf{v}')
    assert (latex(inner(
        grad(v), grad(u))) == r'\nabla{\mathbf{u}} : \nabla{\mathbf{v}}')

    a = BilinearForm((v, u), int_0(inner(grad(v), grad(u))))
    print(latex(a))
    #    assert(latex(a) == r'\int_{0}^{1}\int_{0}^{1} \nabla{\mathbf{v}} : \nabla{\mathbf{u}} dxdy')

    b = LinearForm(v, int_0(sin(pi * x) * cos(pi * y) * div(v)))
    print(latex(b))
コード例 #8
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('')
コード例 #9
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)
コード例 #10
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('')
コード例 #11
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('')
コード例 #12
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('')
コード例 #13
0
def test_equation_2d_2():

    domain = Square()

    V = ScalarFunctionSpace('V', domain)

    x, y = domain.coordinates

    pn, wn = [element_of(V, name=i) for i in ['pn', 'wn']]

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

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

    int_0 = lambda expr: integral(domain, expr)

    s = BilinearForm((tau, sigma), int_0(dot(grad(tau), grad(sigma))))
    m = BilinearForm((tau, sigma), int_0(tau * sigma))
    b1 = BilinearForm((tau, dw), int_0(bracket(pn, dw) * tau))
    b2 = BilinearForm((tau, dp), int_0(bracket(dp, wn) * tau))

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

    expr = m(tau, dw) - alpha * dt * b1(tau, dw) - dt * b2(
        tau, dp) - (alpha * dt / Re) * s(tau, dw)
    a = BilinearForm(((tau, sigma), (dp, dw)), int_0(expr))

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

    bc = [EssentialBC(dp, 0, domain.boundary)]
    bc += [EssentialBC(dw, 0, domain.boundary)]
    equation = Equation(a, l, tests=[tau, sigma], trials=[dp, dw], bc=bc)
コード例 #14
0
def test_latex_2d_4():
    DIM = 2

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

    # ... abstract model
    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)

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

    print(latex(A))
    #    print(latex(tensorize(A)))
    print('')
コード例 #15
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)
コード例 #16
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)
コード例 #17
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))
コード例 #18
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))
コード例 #19
0
def test_latex_2d_1():

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

    V = ScalarFunctionSpace('V', domain)

    x, y = V.coordinates

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

    int_0 = lambda expr: integral(domain, expr)

    assert (latex(grad(v)) == r'\nabla{v}')
    assert (latex(dot(grad(v), grad(u))) == r'\nabla{u} \cdot \nabla{v}')

    a = BilinearForm((v, u), int_0(dot(grad(v), grad(u))))
    print(latex(a))
    #    assert(latex(a) == r'\int_{0}^{1}\int_{0}^{1} \nabla{v} \cdot \nabla{u} dxdy')

    b = LinearForm(v, int_0(sin(pi * x) * cos(pi * y) * v))
    print(latex(b))
コード例 #20
0
def test_equation_2d_6():

    domain = Square()
    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 = 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
    l0 = 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)
    # ...

    bc = EssentialBC(u, 0, domain.boundary)

    # ...
    l = LinearForm(v, l0(v) + mu * area * l1(v))

    eq_1 = Equation(a1, l, tests=v, trials=u, bc=bc)
    # ...

    # ...
    l = LinearForm(v, l0(v) + mu * area * l2(v))

    eq_2 = Equation(a2, l, tests=v, trials=u, bc=bc)
    # ...

    # ...
    l = LinearForm(v, l0(v) + mu * area * l3(v))

    eq_3 = Equation(a3, l, tests=v, trials=u, bc=bc)
コード例 #21
0
def test_equation_2d_1():

    V = ScalarFunctionSpace('V', domain)
    U = ScalarFunctionSpace('U', domain)

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

    x, y = domain.coordinates

    alpha = Constant('alpha')
    kappa = Constant('kappa', real=True)
    eps = Constant('eps', real=True)
    nn = NormalVector('nn')

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

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

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

    expr = v * u
    a2 = BilinearForm((v, u), int_0(expr))

    expr = v * u
    a_B1 = BilinearForm((v, u), int_1(expr))

    expr = x * y * v
    l1 = LinearForm(v, int_0(expr))

    expr = cos(x + y) * v
    l2 = LinearForm(v, int_0(expr))

    expr = x * y * v
    l_B2 = LinearForm(v, int_2(expr))
    # ...

    #    # ...
    #    with pytest.raises(UnconsistentLhsError):
    #        equation = Equation(a1, l1(v))
    #
    #    with pytest.raises(UnconsistentLhsError):
    #        equation = Equation(l1(v), l1(v))
    #
    #    with pytest.raises(UnconsistentLhsError):
    #        equation = Equation(a1(v,u) + alpha*a2(v,u), l1(v))
    #    # ...
    #
    #    # ...
    #    with pytest.raises(UnconsistentRhsError):
    #        equation = Equation(a1(v,u), l1)
    #
    #    with pytest.raises(UnconsistentRhsError):
    #        equation = Equation(a1(v,u), a1(v,u))
    #
    #    with pytest.raises(UnconsistentRhsError):
    #        equation = Equation(a1(v,u), l1(v) + l2(v))
    #    # ...

    # ...
    equation = Equation(a1, l1, tests=v, trials=u)
    # ...

    # ...
    a = BilinearForm((v, u), a1(v, u) + alpha * a2(v, u))
    equation = Equation(a, l1, tests=v, trials=u)
    # ...

    # ...
    a = BilinearForm((v, u), a1(v, u) + a_B1(v, u))
    equation = Equation(a, l1, tests=v, trials=u)
    # ...

    # ...
    a = BilinearForm((v, u), a1(v, u) + a_B1(v, u))
    l = LinearForm(v, l1(v) + l2(v))
    equation = Equation(a, l, tests=v, trials=u)
    # ...

    # ...
    a = BilinearForm((v, u), a1(v, u) + a_B1(v, u))
    l = LinearForm(v, l1(v) + alpha * l_B2(v))
    equation = Equation(a, l, tests=v, trials=u)
    # ...

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

    # ... Poisson with Nitsch method
    g = cos(pi * x) * cos(pi * y)
    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))
    l = LinearForm(v, int_0(g * v / eps) - int_1(kappa * v * g))
    equation = Equation(a, l, tests=v, trials=u)
コード例 #22
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)
コード例 #23
0
ファイル: test_find_2d.py プロジェクト: yellalena/sympde
def test_find_2d_1():

    V = ScalarFunctionSpace('V', domain)
    U = ScalarFunctionSpace('U', domain)

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

    x,y = domain.coordinates

    alpha = Constant('alpha')
    kappa = Constant('kappa', real=True)
    eps   = Constant('eps', real=True)

    B1 = Boundary(r'\Gamma_1', domain)
    B2 = Boundary(r'\Gamma_2', domain)
    B3 = Boundary(r'\Gamma_3', domain)
    
    int_0 = lambda expr: integral(domain , expr)
    int_1 = lambda expr: integral(B1, expr)
    int_2 = lambda expr: integral(B2, expr)
    # ... bilinear/linear forms
    expr = dot(grad(v), grad(u))
    a1 = BilinearForm((v,u), int_0(expr))

    expr = v*u
    a2 = BilinearForm((v,u), int_0(expr))

    expr = v*u
    a_B1 = BilinearForm((v, u), int_1(expr))

    expr = x*y*v
    l1 = LinearForm(v, int_0(expr))

    expr = cos(x+y)*v
    l2 = LinearForm(v, int_0(expr))

    expr = x*y*v
    l_B2 = LinearForm(v, int_1(expr))
    # ...

    # ...
    equation = find(u, forall=v, lhs=a1(u,v), rhs=l1(v))
    # ...

    # ...
    a = BilinearForm((v,u), a1(v,u) + alpha*a2(v,u))
    equation = find(u, forall=v, lhs=a(u,v), rhs=l1(v))
    # ...

    # ...
    a = BilinearForm((v,u), a1(v,u) + a_B1(v,u))
    equation = find(u, forall=v, lhs=a(u,v), rhs=l1(v))
    # ...

    # ...
    a = BilinearForm((v,u), a1(v,u) + a_B1(v,u))
    l = LinearForm(v, l1(v) + l2(v))
    equation = find(u, forall=v, lhs=a(u,v), rhs=l(v))
    # ...

    # ...
    a = BilinearForm((v,u), a1(v,u) + a_B1(v,u))
    l = LinearForm(v, l1(v) + alpha*l_B2(v))
    equation = find(u, forall=v, lhs=a(u,v), rhs=l(v))
    # ...

    # ... using bc
    equation = find(u, forall=v, lhs=a(u,v), rhs=l(v), bc=EssentialBC(u, 0, B1))
コード例 #24
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('')
コード例 #25
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))
コード例 #26
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('')