Пример #1
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)
Пример #2
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)
Пример #3
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('')
Пример #4
0
    def eval(cls, *_args):
        """."""

        if not _args:
            return

        if not len(_args) == 1:
            raise ValueError('Expecting one argument')

        expr = _args[0]
        if isinstance(expr, Add):
            args = [cls.eval(a) for a in expr.args]
            return Add(*args)

        elif isinstance(expr, Mul):
            coeffs  = [a for a in expr.args if isinstance(a, _coeffs_registery)]
            vectors = [a for a in expr.args if not(a in coeffs)]

            a = S.One
            if coeffs:
                a = Mul(*coeffs)

            b = S.One
            if vectors:
                try:
                    if len(vectors) == 1:
                        f = vectors[0]
                        b = cls(f)

                    elif len(vectors) == 2:
                        f,g = vectors
                        b = f*cls(g) + g*cls(f)

                    else:
                        left = vectors[0]
                        right = Mul(*vectors[1:])

                        f_left  = cls(left, evaluate=True)
                        f_right = cls(right, evaluate=True)

                        b = left * f_right + f_left * right

                except:
                    b = cls(Mul(*vectors), evaluate=False)

            return Mul(a, b)

        elif isinstance(expr, NormalDerivative):
            n = NormalVector('n')
            u = expr._args[0]

            return Dot(Grad(cls(u)), cls(n))

        elif isinstance(expr, NormalVector):
            return PlusNormalVector('n')

        return cls(expr, evaluate=False)
Пример #5
0
    def _print_NormalDerivative(self, expr):
        nn = self._print(NormalVector('n'))
        arg = expr.args[0]
        if isinstance(arg, (ScalarFunction, VectorFunction)):
            arg = r'\nabla ' + self._print(arg)

        else:
            arg = r'\nabla \left( ' + self._print(arg) + r' \right)'

        return arg + r' \cdot ' + nn
Пример #6
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])
Пример #7
0
def test_terminal_expr_linear_2d_5(boundary=[r'\Gamma_1', r'\Gamma_3']):

    # ... abstract model
    domain = Square()

    V = ScalarFunctionSpace('V', domain)

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

    else:
        B_neumann = Union(*B_neumann)

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

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

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

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

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

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

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

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

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

    print(TerminalExpr(l))
    print('')
Пример #8
0
def test_terminal_expr_linear_2d_3():

    domain = Square()
    B = domain.boundary

    x, y = domain.coordinates

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

    V = ScalarFunctionSpace('V', domain)

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

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

    l = LinearForm(v, int_1(dot(grad(v), nn)))
    print(TerminalExpr(l))
    print('')
Пример #9
0
    def eval(cls, *_args, **kwargs):
        """."""

        if not _args:
            return

        if not len(_args) == 1:
            raise ValueError('Expecting one argument')

        expr = _args[0]
        n_rows = kwargs.pop('n_rows', None)
        n_cols = kwargs.pop('n_cols', None)
        dim = kwargs.pop('dim', None)

        if isinstance(expr, Add):
            args = [cls.eval(a, dim=dim) for a in expr.args]
            o = args[0]
            for arg in args[1:]:
                o = o + arg
            return o

        elif isinstance(expr, Mul):
            args = [cls.eval(a, dim=dim) for a in expr.args]
            o = args[0]
            for arg in args[1:]:
                o = o * arg
            return o

        elif isinstance(expr, (ScalarTestFunction, VectorTestFunction)):
            return expr

        elif isinstance(expr, BasicForm):
            # ...
            dim = expr.ldim
            domain = expr.domain
            if isinstance(domain, Union):
                domain = list(domain._args)

            elif not is_sequence(domain):
                domain = [domain]
            # ...

            # ...
            d_expr = {}
            for d in domain:
                d_expr[d] = S.Zero
            # ...

            if isinstance(expr.expr, Add):
                for a in expr.expr.args:
                    newexpr = cls.eval(a, dim=dim)
                    newexpr = expand(newexpr)

                    # ...
                    try:
                        domain = _get_domain(a)
                        if isinstance(domain, Union):
                            domain = list(domain._args)

                        elif not is_sequence(domain):
                            domain = [domain]
                    except:
                        pass
                    # ...

                    # ...
                    for d in domain:
                        d_expr[d] += newexpr
                    # ...

            else:
                newexpr = cls.eval(expr.expr, dim=dim)
                newexpr = expand(newexpr)

                # ...
                if isinstance(expr, Functional):
                    domain = expr.domain

                else:
                    domain = _get_domain(expr.expr)

                if isinstance(domain, Union):
                    domain = list(domain._args)

                elif not is_sequence(domain):
                    domain = [domain]
                # ...

                # ...
                for d in domain:
                    d_expr[d] += newexpr
                # ...
            # ...

            # ...
            d_new = {}
            for domain, newexpr in d_expr.items():

                if newexpr != 0:
                    M, test_indices, trial_indices = _init_matrix(expr)
                    M = _to_matrix_form(newexpr, M, test_indices,
                                        trial_indices)

                    # TODO ARA make sure thre is no problem with psydac
                    #      we should always take the interior of a domain
                    if not isinstance(domain,
                                      (Boundary, Interface, InteriorDomain)):
                        domain = domain.interior

                    d_new[domain] = M
            # ...

            # ...
            ls = []
            d_all = {}
            # ...
            #            print(d_new)
            #            print([type(i) for i in d_new.keys()])

            # ... treating interfaces
            keys = [k for k in d_new.keys() if isinstance(k, Interface)]
            for interface in keys:
                # ...
                trials = None
                tests = None
                if expr.is_bilinear:
                    trials = list(expr.variables[0])
                    tests = list(expr.variables[1])

                elif expr.is_linear:
                    tests = list(expr.variables)
                # ...

                # ...
                newexpr = d_new[interface]
                ls_int, d = _split_expr_over_interface(newexpr,
                                                       interface,
                                                       tests=tests,
                                                       trials=trials)
                # ...

                # ...
                ls += ls_int
                # ...

                # ...
                for k, v in d.items():
                    if k in d_all.keys():
                        d_all[k] += v

                    else:
                        d_all[k] = v
                # ...
            # ...

            # ... treating subdomains
            keys = [k for k in d_new.keys() if isinstance(k, Union)]
            for domain in keys:
                # ...
                trials = None
                tests = None
                if expr.is_bilinear:
                    trials = list(expr.variables[0])
                    tests = list(expr.variables[1])

                elif expr.is_linear:
                    tests = list(expr.variables)

                else:
                    raise NotImplementedError(
                        'Only Bilinear and Linear forms are available')
                # ...

                # ...
                newexpr = d_new[domain]
                d = _split_expr_over_subdomains(newexpr,
                                                domain.as_tuple(),
                                                tests=tests,
                                                trials=trials)
                # ...

                # ...
                for k, v in d.items():
                    if k in d_all.keys():
                        d_all[k] += v

                    else:
                        d_all[k] = v
                # ...
            # ...

            # ...
            d = {}

            for k, v in d_new.items():
                if not isinstance(k, (Interface, Union)):
                    d[k] = d_new[k]

            for k, v in d_all.items():
                if k in d.keys():
                    d[k] += v

                else:
                    d[k] = v

            d_new = d
            # ...

            # ...
            for domain, newexpr in d_new.items():
                if isinstance(domain, Boundary):
                    ls += [BoundaryExpression(domain, newexpr)]

                elif isinstance(domain, Interface):
                    ls += [InterfaceExpression(domain, newexpr)]

                elif isinstance(domain, BasicDomain):
                    ls += [DomainExpression(domain, newexpr)]

                else:
                    raise TypeError('not implemented for {}'.format(
                        type(domain)))
            # ...
            return ls

        elif isinstance(expr,
                        (DomainIntegral, BoundaryIntegral, InterfaceIntegral)):
            if dim is None:
                domain = expr.domain
                dim = domain.dim

            return cls.eval(expr._args[0], dim=dim)

        elif isinstance(expr, NormalVector):
            lines = [[expr[i] for i in range(dim)]]
            return Matrix(lines)

        elif isinstance(expr, TangentVector):
            lines = [[expr[i] for i in range(dim)]]
            return Matrix(lines)

        elif isinstance(expr, BasicExpr):
            return cls.eval(expr.expr, dim=dim)

        elif isinstance(expr, _generic_ops):
            # if i = Dot(...) then type(i) is Grad
            op = type(expr)
            new = eval('{0}_{1}d'.format(op, dim))
            args = [cls.eval(i, dim=dim) for i in expr.args]
            return new(*args)

        elif isinstance(expr, Trace):
            # TODO treate different spaces
            if expr.order == 0:
                return cls.eval(expr.expr, dim=dim)

            elif expr.order == 1:
                # TODO give a name to normal vector
                normal_vector_name = 'n'
                n = NormalVector(normal_vector_name)
                M = cls.eval(expr.expr, dim=dim)

                if dim == 1:
                    return M
                else:
                    if isinstance(M, (Add, Mul)):
                        ls = M.atoms(Tuple)

                        for i in ls:
                            M = M.subs(i, Matrix(i))
                        M = simplify(M)
                    e = 0
                    for i in range(0, dim):
                        e += M[i] * n[i]
                    return e

            else:
                raise ValueError(
                    '> Only traces of order 0 and 1 are available')

        elif isinstance(expr, Matrix):
            n, m = expr.shape
            lines = []
            for i in range(0, n):
                line = []
                for j in range(0, m):
                    line.append(cls.eval(expr[i, j], dim=dim))
                lines.append(line)
            return Matrix(lines)

        return expr
Пример #10
0
def test_terminal_expr_bilinear_2d_3():

    domain = Square()

    V = ScalarFunctionSpace('V', domain)

    B = domain.boundary

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

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

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

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

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

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

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

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

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

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

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

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

    # ...
    a1 = BilinearForm((u, v), int_0(u * v))
    a2 = BilinearForm((u, v), int_0(dot(grad(u), grad(v))))
    a3 = BilinearForm((u, v), int_1(v * dot(grad(u), nn)))
    a = BilinearForm((u, v), a1(u, v) + kappa * a2(u, v) + mu * a3(u, v))
    print(TerminalExpr(a))
    print('')
Пример #11
0
def test_linearity_linear_form_2d_1():

    from sympde.expr.errors import UnconsistentLinearExpressionError

    domain = Domain('Omega', dim=2)
    B1 = Boundary(r'\Gamma_1', domain)

    x, y = domain.coordinates

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

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

    v, v1, v2 = elements_of(V, names='v, v1, v2')
    w = element_of(W, name='w')

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

    # The following integral expressions are linear, hence it must be possible
    # to create LinearForm objects from them

    _ = LinearForm(v, int_0(x * y * v))
    _ = LinearForm(v, int_0(x * y * v + v))

    g = Matrix((x**2, y**2))
    _ = LinearForm(v, int_0(v * dot(g, nn)))

    g = Matrix((x**2, y**2))
    _ = LinearForm(v, int_1(v * dot(g, nn)) + int_0(x * y * v))

    l1 = LinearForm(v1, int_0(x * y * v1))
    _ = LinearForm(v, l1(v))

    g = Matrix((x, y))
    l1 = LinearForm(v1, int_0(x * y * v1))
    l2 = LinearForm(v2, int_0(dot(grad(v2), g)))
    _ = LinearForm(v, l1(v) + l2(v))

    l1 = LinearForm(v1, int_0(x * y * v1))
    l2 = LinearForm(v1, int_0(v1))
    _ = LinearForm(v, l1(v) + kappa * l2(v))

    g = Matrix((x**2, y**2))
    l1 = LinearForm(v1, int_0(x * y * v1))
    l2 = LinearForm(v1, int_0(v1))
    l3 = LinearForm(v, int_1(v * dot(g, nn)))
    _ = LinearForm(v, l1(v) + kappa * l2(v) + mu * l3(v))

    l1 = LinearForm(w, int_0(5 * w * x))
    l2 = LinearForm(w, int_1(w * y))
    _ = LinearForm(w, l1(w) + kappa * l2(w))

    l0 = LinearForm(v, int_0(x * y * v))
    l1 = LinearForm(w, int_0(w[0] * y))
    l2 = LinearForm(w, int_1(w[1] * x))
    l3 = LinearForm(w, kappa * l1(w) + mu * l2(w))
    _ = LinearForm((v, w), l0(v) + l3(w))

    # The following integral expressions are not linear, hence LinearForm must
    # raise an exception

    with pytest.raises(UnconsistentLinearExpressionError):
        _ = LinearForm(v, int_0(x * y * v + 1))

    with pytest.raises(UnconsistentLinearExpressionError):
        _ = LinearForm(v, int_0(x * v**2))

    with pytest.raises(UnconsistentLinearExpressionError):
        _ = LinearForm(v, int_0(x * y * v) + int_1(v * exp(v)))

    with pytest.raises(UnconsistentLinearExpressionError):
        _ = LinearForm(w, int_0(w * w))

    with pytest.raises(UnconsistentLinearExpressionError):
        _ = LinearForm(w, int_0(w[0] * w[1]))

    with pytest.raises(UnconsistentLinearExpressionError):
        _ = LinearForm((v, w), int_0(x * w[0]) + int_1(v * w[1]))
Пример #12
0
def test_terminal_expr_bilinear_2d_1():

    domain = Domain('Omega', dim=2)
    B1 = Boundary(r'\Gamma_1', domain)

    x, y = domain.coordinates

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

    V = ScalarFunctionSpace('V', domain)

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

    # ...

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

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

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

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

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

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

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

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

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

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

    # ... Poisson with Nitsch method
    a0 = BilinearForm((u, v), int_0(dot(grad(u), grad(v))))
    a_B1 = BilinearForm((u, v),
                        int_1(-kappa * u * dot(grad(v), nn) -
                              v * dot(grad(u), nn) + u * v / eps))
    a = BilinearForm((u, v), a0(u, v) + a_B1(u, v))
    print(a)
    print(TerminalExpr(a))
    print('')
Пример #13
0
def test_terminal_expr_linear_2d_1():

    domain = Domain('Omega', dim=2)
    B1 = Boundary(r'\Gamma_1', domain)

    x, y = domain.coordinates

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

    V = ScalarFunctionSpace('V', domain)

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

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

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

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

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

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

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

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

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

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

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

    # ...
    g = Matrix((x**2, y**2))
    l1 = LinearForm(v1, int_0(x * y * v1))
    l2 = LinearForm(v1, int_0(v1))
    l3 = LinearForm(v, int_1(v * dot(g, nn)))
    l = LinearForm(v, l1(v) + kappa * l2(v) + mu * l3(v))
    print(TerminalExpr(l))
    print('')
Пример #14
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)
    nn = NormalVector('nn')

    V = ScalarFunctionSpace('V', domain)

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

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

    a = BilinearForm((u, v), int_0(u * v))

    assert (a.domain == domain.interior)
    assert (a(u1, v1) == int_0(u1 * v1))
    # ...

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

    assert (a.domain == domain.interior)
    assert (a(u1, v1) == int_0(u1 * v1) + int_0(dot(grad(u1), grad(v1))))
    # ...

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

    assert (a.domain == B1)
    assert (a(u1, v1) == int_1(v1 * trace_1(grad(u1), B1)))
    # ...

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

    # TODO a.domain are not ordered
    assert (len(a.domain.args) == 2)
    for i in a.domain.args:
        assert (i in [domain.interior, B1])

    assert (a(u1, v1) == int_0(u1 * v1) + int_1(v1 * trace_1(grad(u1), B1)))
    # ...

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

    assert (a.domain == domain.interior)
    assert (a(u2, v2) == int_0(u2 * v2))
    # ...

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

    assert (a.domain == domain.interior)
    assert (a(u, v) == int_0(u * v) + int_0(kappa * dot(grad(u), grad(v))))
Пример #15
0
def test_linearity_bilinear_form_2d_1():

    from sympde.expr.errors import UnconsistentLinearExpressionError

    domain = Domain('Omega', dim=2)
    B1 = Boundary(r'\Gamma_1', domain)

    x, y = domain.coordinates

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

    V = ScalarFunctionSpace('V', domain)

    u, u1, u2 = elements_of(V, names='u, u1, u2')
    v, v1, v2 = elements_of(V, names='v, v1, v2')

    # ...

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

    # The following integral expressions are bilinear, hence it must be possible
    # to create BilinearForm objects from them

    _ = BilinearForm((u, v), int_0(u * v))
    _ = BilinearForm((u, v), int_0(dot(grad(u), grad(v))))
    _ = BilinearForm((u, v), int_0(u * v + dot(grad(u), grad(v))))
    _ = BilinearForm(
        (u, v),
        int_0(u * v + dot(grad(u), grad(v))) + int_1(v * dot(grad(u), nn)))
    _ = BilinearForm(((u1, u2), (v1, v2)), int_0(u1 * v1 + u2 * v2))

    a1 = BilinearForm((u1, v1), int_0(u1 * v1))
    _ = BilinearForm((u, v), a1(u, v))

    a1 = BilinearForm((u1, v1), int_0(u1 * v1))
    a2 = BilinearForm((u2, v2), int_0(dot(grad(u2), grad(v2))))
    _ = BilinearForm((u, v), a1(u, v) + a2(u, v))

    a1 = BilinearForm((u1, v1), int_0(u1 * v1))
    a2 = BilinearForm((u2, v2), int_0(dot(grad(u2), grad(v2))))
    _ = BilinearForm((u, v), a1(u, v) + kappa * a2(u, v))

    a1 = BilinearForm((u1, v1), int_0(u1 * v1))
    a2 = BilinearForm((u2, v2), int_0(dot(grad(u2), grad(v2))))
    a3 = BilinearForm((u, v), int_1(v * dot(grad(u), nn)))
    _ = BilinearForm((u, v), a1(u, v) + kappa * a2(u, v) + mu * a3(u, v))

    # ... Poisson with Nitsch method
    a0 = BilinearForm((u, v), int_0(dot(grad(u), grad(v))))
    a_B1 = BilinearForm((u, v),
                        int_1(-kappa * u * dot(grad(v), nn) -
                              v * dot(grad(u), nn) + u * v / eps))
    _ = BilinearForm((u, v), a0(u, v) + a_B1(u, v))
    # ...

    # The following integral expressions are not bilinear, hence BilinearForm must
    # raise an exception

    with pytest.raises(UnconsistentLinearExpressionError):
        _ = BilinearForm((u, v), int_0(x * y * dot(grad(u), grad(v)) + 1))

    with pytest.raises(UnconsistentLinearExpressionError):
        _ = BilinearForm((u, v), int_0(x * dot(grad(u), grad(v**2))))

    with pytest.raises(UnconsistentLinearExpressionError):
        _ = BilinearForm((u, v), int_0(u * v) + int_1(v * exp(u)))
Пример #16
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)
Пример #17
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)
    nn = NormalVector('nn')

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

    u, u1, u2 = [element_of(V, name=i) for i in ['u', 'u1', 'u2']]
    v, v1, v2 = [element_of(V, name=i) for i in ['v', 'v1', 'v2']]
    # ...
    int_0 = lambda expr: integral(domain, expr)
    int_1 = lambda expr: integral(B1, expr)

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

    assert (l.domain == domain.interior)
    assert (l(v1) == int_0(x * y * v1))
    # ...

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

    assert (l.domain == B1)
    assert (l(v1) == int_1(v1 * trace_1(g, B1)))
    # ...

    # ...
    g = Tuple(x**2, y**2)
    l = LinearForm(v, int_1(v * dot(g, nn)) + int_0(x * y * v))

    assert (len(l.domain.args) == 2)
    for i in l.domain.args:
        assert (i in [domain.interior, B1])

    assert (l(v1) == int_1(v1 * trace_1(g, B1)) + int_0(x * y * v1))
    # ...

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

    assert (l.domain == domain.interior)
    assert (l(u1) == int_0(x * y * u1))
    # ...

    # ...
    g = Tuple(x, y)
    l1 = LinearForm(u1, int_0(x * y * u1))
    l2 = LinearForm(u2, int_0(dot(grad(u2), g)))

    l = LinearForm(v, l1(v) + l2(v))

    assert (l.domain == domain.interior)
    assert (l(v1) == int_0(x * y * v1) + int_0(dot(grad(v1), g)))
    # ...

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

    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)

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

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

    assert (l.domain == domain.interior)
    assert (l(u1,
              u2).expand() == int_0(-1.0 * dt * dot(grad(u1), grad(wn)) / Re) +
            int_0(dt * u1 * bracket(pn, wn)))