def test_linearize_form_2d_3(): """steady Euler equation.""" domain = Domain('Omega', dim=2) U = VectorFunctionSpace('U', domain) W = ScalarFunctionSpace('W', domain) # Test functions v = element_of(U, name='v') phi = element_of(W, name='phi') q = element_of(W, name='q') # Steady-state fields U_0 = element_of(U, name='U_0') Rho_0 = element_of(W, name='Rho_0') P_0 = element_of(W, name='P_0') # Trial functions (displacements from steady-state) d_u = element_of(U, name='d_u') d_rho = element_of(W, name='d_rho') d_p = element_of(W, name='d_p') # Shortcut int_0 = lambda expr: integral(domain, expr) # The Euler equations are a system of three non-linear equations; for each of # them we create a linear form in the test functions (phi, v, q) respectively. e1 = div(Rho_0 * U_0) l1 = LinearForm(phi, int_0(e1 * phi)) e2 = Rho_0 * convect(U_0, U_0) + grad(P_0) l2 = LinearForm(v, int_0(dot(e2, v))) e3 = div(P_0 * U_0) l3 = LinearForm(q, int_0(e3 * q)) # ... # Linearize l1, l2 and l3 separately a1 = linearize(l1, fields=[Rho_0, U_0], trials=[d_rho, d_u]) a2 = linearize(l2, fields=[Rho_0, U_0, P_0], trials=[d_rho, d_u, d_p]) a3 = linearize(l3, fields=[U_0, P_0], trials=[d_u, d_p]) # Check individual bilinear forms d_e1 = div(U_0 * d_rho + Rho_0 * d_u) d_e2 = d_rho * convect(U_0, U_0) + \ Rho_0 * convect(d_u, U_0) + \ Rho_0 * convect(U_0, d_u) + grad(d_p) d_e3 = div(d_p * U_0 + P_0 * d_u) assert a1([d_rho, d_u], phi) == int_0(d_e1 * phi) assert a2([d_rho, d_u, d_p], v) == int_0(dot(d_e2, v)) assert a3([d_u, d_p], q) == int_0(d_e3 * q) # Linearize linear form of system: l = l1 + l2 + l3 l = LinearForm((phi, v, q), l1(phi) + l2(v) + l3(q)) a = linearize(l, fields=[Rho_0, U_0, P_0], trials=[d_rho, d_u, d_p]) # Check composite linear form assert a([d_rho, d_u, d_p], [phi, v, q]) == \ int_0(d_e1 * phi + dot(d_e2, v) + d_e3 * q)
def test_terminal_expr_linear_2d_2(): domain = Domain('Omega', dim=2) B1 = Boundary(r'\Gamma_1', domain) x, y = domain.coordinates kappa = Constant('kappa', is_real=True) mu = Constant('mu', is_real=True) V = VectorFunctionSpace('V', domain) u, u1, u2 = [element_of(V, name=i) for i in ['u', 'u1', 'u2']] v, v1, v2 = [element_of(V, name=i) for i in ['v', 'v1', 'v2']] # ... int_0 = lambda expr: integral(domain, expr) int_1 = lambda expr: integral(B1, expr) g = Matrix((x, y)) l = LinearForm(v, int_0(dot(g, v))) print(TerminalExpr(l)) print('') # ... # ... g = Matrix((x, y)) l = LinearForm(v, int_0(dot(g, v) + div(v))) print(TerminalExpr(l)) print('')
def test_user_function_2d_1(): domain = Domain('Omega', dim=2) x, y = domain.coordinates kappa = Constant('kappa', is_real=True) mu = Constant('mu', is_real=True) # right hand side f = Function('f') V = ScalarFunctionSpace('V', domain) u, v = [element_of(V, name=i) for i in ['u', 'v']] int_0 = lambda expr: integral(domain, expr) # ... expr = dot(grad(u), grad(v)) + f(x, y) * u * v a = BilinearForm((v, u), int_0(expr)) print(a) print(TerminalExpr(a)) print('') # ... # ... expr = f(x, y) * v l = LinearForm(v, int_0(expr)) print(l) print(TerminalExpr(l)) print('')
def test_linearize_form_2d_4(): domain = Domain('Omega', dim=2) Gamma_N = Boundary(r'\Gamma_N', domain) x, y = domain.coordinates V = ScalarFunctionSpace('V', domain) v = element_of(V, name='v') u = element_of(V, name='u') du = element_of(V, name='du') int_0 = lambda expr: integral(domain, expr) int_1 = lambda expr: integral(Gamma_N, expr) # g = Matrix((cos(pi*x)*sin(pi*y), # sin(pi*x)*cos(pi*y))) expr = dot(grad(v), grad(u)) - 4. * exp(-u) * v # + v*trace_1(g, Gamma_N) l = LinearForm(v, int_0(expr)) # linearising l around u, using du a = linearize(l, u, trials=du) assert a(du, v) == int_0(dot(grad(v), grad(du)) + 4. * exp(-u) * du * v)
def test_linearize_form_2d_2(): domain = Domain('Omega', dim=2) V = ScalarFunctionSpace('V', domain) v, F, u = elements_of(V, names='v, F, u') int_0 = lambda expr: integral(domain, expr) # ... l1 = LinearForm(v, int_0(F**2 * v)) l = LinearForm(v, l1(v)) a = linearize(l, F, trials=u) expected = linearize(l1, F, trials=u) assert a == expected
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')
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('')
def test_interface_integral_2(): # ... A = Square('A') B = Square('B') domain = A.join(B, name='domain', bnd_minus=A.get_boundary(axis=0, ext=1), bnd_plus=B.get_boundary(axis=0, ext=-1)) # ... x, y = domain.coordinates V = ScalarFunctionSpace('V', domain, kind=None) assert (V.is_broken) u, u1, u2, u3 = elements_of(V, names='u, u1, u2, u3') v, v1, v2, v3 = elements_of(V, names='v, v1, v2, v3') # ... I = domain.interfaces a = BilinearForm((u, v), integral(domain, dot(grad(u), grad(v)))) b = BilinearForm((u, v), integral(I, jump(u) * jump(v))) A = BilinearForm(((u1, u2), (v1, v2)), a(u1, v1) + a(u2, v2) + b(u1, v1) + b(u2, v2) + b(u1, v2)) B = BilinearForm( ((u1, u2, u3), (v1, v2, v3)), a(u1, v1) + a(u2, v2) + a(u3, v3) + b(u1, v1) + b(u2, v2) + b(u1, v2)) print(TerminalExpr(A)) print(TerminalExpr(B)) # ... # ... linear forms b = LinearForm(v, integral(I, jump(v))) b = LinearForm((v1, v2), b(v1) + b(v2)) expr = TerminalExpr(b) print(expr)
def test_interface_integral_3(): # ... A = Square('A') B = Square('B') C = Square('C') AB = A.join(B, name='AB', bnd_minus=A.get_boundary(axis=0, ext=1), bnd_plus=B.get_boundary(axis=0, ext=-1)) domain = AB.join(C, name='domain', bnd_minus=B.get_boundary(axis=0, ext=1), bnd_plus=C.get_boundary(axis=0, ext=-1)) # ... x, y = domain.coordinates V = ScalarFunctionSpace('V', domain, kind=None) assert (V.is_broken) u, v = elements_of(V, names='u, v') # ... I = domain.interfaces # print(I) # print(integral(I, jump(u) * jump(v))) # a = BilinearForm((u,v), integral(domain, u*v)) # a = BilinearForm((u,v), integral(domain, dot(grad(u),grad(v)))) # a = BilinearForm((u,v), integral(I, jump(u) * jump(v))) a = BilinearForm((u, v), integral(domain, dot(grad(u), grad(v))) + integral(I, jump(u) * jump(v))) expr = TerminalExpr(a) print(expr) # ... # ... linear forms b = LinearForm( v, integral(domain, sin(x + y) * v) + integral(I, cos(x + y) * jump(v))) expr = TerminalExpr(b) print(expr)
def test_linearize_form_2d_1(): domain = Domain('Omega', dim=2) V = ScalarFunctionSpace('V', domain) W = VectorFunctionSpace('W', domain) v, F, u = elements_of(V, names='v, F, u') w, G, m = elements_of(W, names='w, G, m') int_0 = lambda expr: integral(domain, expr) # ... l = LinearForm(v, int_0(F**2 * v)) a = linearize(l, F, trials=u) assert a(u, v) == int_0(2 * F * u * v) # ... # ... l = LinearForm(v, int_0(dot(grad(F), grad(F)) * v)) a = linearize(l, F, trials=u) assert a(u, v) == int_0(2 * dot(grad(F), grad(u)) * v) # ... # ... l = LinearForm(v, int_0(exp(-F) * v)) a = linearize(l, F, trials=u) assert a(u, v) == int_0(-exp(-F) * u * v) # ... # ... l = LinearForm(v, int_0(cos(F) * v)) a = linearize(l, F, trials=u) assert a(u, v) == int_0(-sin(F) * u * v) # ... # ... l = LinearForm(v, int_0(cos(F**2) * v)) a = linearize(l, F, trials=u) assert a(u, v) == int_0(-2 * F * sin(F**2) * u * v) # ... # ... l = LinearForm(v, int_0(F**2 * dot(grad(F), grad(v)))) a = linearize(l, F, trials=u) assert a(u, v) == int_0(2 * F * u * dot(grad(F), grad(v)) + F**2 * dot(grad(u), grad(v))) # ... # ... l = LinearForm(w, int_0(dot(rot(G), grad(G)) * w)) a = linearize(l, G, trials=m) assert a(m, w) == int_0((dot(rot(m), grad(G)) + dot(rot(G), grad(m))) * w)
def test_terminal_expr_linear_2d_4(): D1 = InteriorDomain('D1', dim=2) D2 = InteriorDomain('D2', dim=2) domain = Union(D1, D2) x, y = domain.coordinates kappa = Constant('kappa', is_real=True) mu = Constant('mu', is_real=True) 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) l = LinearForm(v, int_0(x * y * v)) print(TerminalExpr(l)) print('')
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('')
def test_linear_form_2d_2(): domain = Domain('Omega', dim=2) B1 = Boundary(r'\Gamma_1', domain) x, y = domain.coordinates kappa = Constant('kappa', is_real=True) mu = Constant('mu', is_real=True) V = VectorFunctionSpace('V', domain) u, u1, u2 = [element_of(V, name=i) for i in ['u', 'u1', 'u2']] v, v1, v2 = [element_of(V, name=i) for i in ['v', 'v1', 'v2']] # ... int_0 = lambda expr: integral(domain, expr) int_1 = lambda expr: integral(B1, expr) g = Matrix((x, y)) l = LinearForm(v, int_0(dot(g, v))) assert (l.domain == domain.interior) assert (l(v1) == int_0(dot(g, v1))) # ... # ... g = Matrix((x, y)) l1 = LinearForm(v1, int_0(dot(g, v1))) l = LinearForm(v, l1(v)) assert (l.domain == domain.interior) assert (l(u1) == int_0(dot(g, u1))) # ... # ... g1 = Matrix((x, 0)) g2 = Matrix((0, y)) l1 = LinearForm(v1, int_0(dot(v1, g1))) l2 = LinearForm(v2, int_0(dot(v2, g2))) l = LinearForm(v, l1(v) + l2(v)) assert (l.domain == domain.interior) assert (l(u) == int_0(dot(u, g1)) + int_0(dot(u, g2)))
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))
def eval(cls, expr, domain, **options): """.""" from sympde.expr.evaluation import TerminalExpr, DomainExpression from sympde.expr.expr import BilinearForm, LinearForm, BasicForm, Norm from sympde.expr.expr import Integral types = (ScalarFunction, VectorFunction, DifferentialOperator, Trace, Integral) mapping = domain.mapping dim = domain.dim assert mapping # TODO this is not the dim of the domain l_coords = ['x1', 'x2', 'x3'][:dim] ph_coords = ['x', 'y', 'z'] if not has(expr, types): if has(expr, DiffOperator): return cls(expr, domain, evaluate=False) else: syms = symbols(ph_coords[:dim]) if isinstance(mapping, InterfaceMapping): mapping = mapping.minus # here we assume that the two mapped domains # are identical in the interface so we choose one of them Ms = [mapping[i] for i in range(dim)] expr = expr.subs(list(zip(syms, Ms))) if mapping.is_analytical: expr = expr.subs(list(zip(Ms, mapping.expressions))) return expr if isinstance(expr, Symbol) and expr.name in l_coords: return expr if isinstance(expr, Symbol) and expr.name in ph_coords: return mapping[ph_coords.index(expr.name)] elif isinstance(expr, Add): args = [cls.eval(a, domain) for a in expr.args] v = S.Zero for i in args: v += i n, d = v.as_numer_denom() return n / d elif isinstance(expr, Mul): args = [cls.eval(a, domain) for a in expr.args] v = S.One for i in args: v *= i return v elif isinstance(expr, _logical_partial_derivatives): if mapping.is_analytical: Ms = [mapping[i] for i in range(dim)] expr = expr.subs(list(zip(Ms, mapping.expressions))) return expr elif isinstance(expr, IndexedVectorFunction): el = cls.eval(expr.base, domain) el = TerminalExpr(el, domain=domain.logical_domain) return el[expr.indices[0]] elif isinstance(expr, MinusInterfaceOperator): mapping = mapping.minus newexpr = PullBack(expr.args[0], mapping) test = newexpr.test newexpr = newexpr.expr.subs(test, MinusInterfaceOperator(test)) return newexpr elif isinstance(expr, PlusInterfaceOperator): mapping = mapping.plus newexpr = PullBack(expr.args[0], mapping) test = newexpr.test newexpr = newexpr.expr.subs(test, PlusInterfaceOperator(test)) return newexpr elif isinstance(expr, (VectorFunction, ScalarFunction)): return PullBack(expr, mapping).expr elif isinstance(expr, Transpose): arg = cls(expr.arg, domain) return Transpose(arg) elif isinstance(expr, grad): arg = expr.args[0] if isinstance(mapping, InterfaceMapping): if isinstance(arg, MinusInterfaceOperator): a = arg.args[0] mapping = mapping.minus elif isinstance(arg, PlusInterfaceOperator): a = arg.args[0] mapping = mapping.plus else: raise TypeError(arg) arg = type(arg)(cls.eval(a, domain)) else: arg = cls.eval(arg, domain) return mapping.jacobian.inv().T * grad(arg) elif isinstance(expr, curl): arg = expr.args[0] if isinstance(mapping, InterfaceMapping): if isinstance(arg, MinusInterfaceOperator): arg = arg.args[0] mapping = mapping.minus elif isinstance(arg, PlusInterfaceOperator): arg = arg.args[0] mapping = mapping.plus else: raise TypeError(arg) if isinstance(arg, VectorFunction): arg = PullBack(arg, mapping) else: arg = cls.eval(arg, domain) if isinstance(arg, PullBack) and isinstance( arg.kind, HcurlSpaceType): J = mapping.jacobian arg = arg.test if isinstance(expr.args[0], (MinusInterfaceOperator, PlusInterfaceOperator)): arg = type(expr.args[0])(arg) if expr.is_scalar: return (1 / J.det()) * curl(arg) return (J / J.det()) * curl(arg) else: raise NotImplementedError('TODO') elif isinstance(expr, div): arg = expr.args[0] if isinstance(mapping, InterfaceMapping): if isinstance(arg, MinusInterfaceOperator): arg = arg.args[0] mapping = mapping.minus elif isinstance(arg, PlusInterfaceOperator): arg = arg.args[0] mapping = mapping.plus else: raise TypeError(arg) if isinstance(arg, (ScalarFunction, VectorFunction)): arg = PullBack(arg, mapping) else: arg = cls.eval(arg, domain) if isinstance(arg, PullBack) and isinstance( arg.kind, HdivSpaceType): J = mapping.jacobian arg = arg.test if isinstance(expr.args[0], (MinusInterfaceOperator, PlusInterfaceOperator)): arg = type(expr.args[0])(arg) return (1 / J.det()) * div(arg) elif isinstance(arg, PullBack): return SymbolicTrace(mapping.jacobian.inv().T * grad(arg.test)) else: raise NotImplementedError('TODO') elif isinstance(expr, laplace): arg = expr.args[0] v = cls.eval(grad(arg), domain) v = mapping.jacobian.inv().T * grad(v) return SymbolicTrace(v) # elif isinstance(expr, hessian): # arg = expr.args[0] # if isinstance(mapping, InterfaceMapping): # if isinstance(arg, MinusInterfaceOperator): # arg = arg.args[0] # mapping = mapping.minus # elif isinstance(arg, PlusInterfaceOperator): # arg = arg.args[0] # mapping = mapping.plus # else: # raise TypeError(arg) # v = cls.eval(grad(expr.args[0]), domain) # v = mapping.jacobian.inv().T*grad(v) # return v elif isinstance(expr, (dot, inner, outer)): args = [cls.eval(arg, domain) for arg in expr.args] return type(expr)(*args) elif isinstance(expr, _diff_ops): raise NotImplementedError('TODO') # TODO MUST BE MOVED AFTER TREATING THE CASES OF GRAD, CURL, DIV IN FEEC elif isinstance(expr, (Matrix, ImmutableDenseMatrix)): n_rows, n_cols = expr.shape lines = [] for i_row in range(0, n_rows): line = [] for i_col in range(0, n_cols): line.append(cls.eval(expr[i_row, i_col], domain)) lines.append(line) return type(expr)(lines) elif isinstance(expr, dx): if expr.atoms(PlusInterfaceOperator): mapping = mapping.plus elif expr.atoms(MinusInterfaceOperator): mapping = mapping.minus arg = expr.args[0] arg = cls(arg, domain, evaluate=True) if isinstance(arg, PullBack): arg = TerminalExpr(arg, domain=domain.logical_domain) elif isinstance(arg, MatrixElement): arg = TerminalExpr(arg, domain=domain.logical_domain) # ... if dim == 1: lgrad_arg = LogicalGrad_1d(arg) if not isinstance(lgrad_arg, (list, tuple, Tuple, Matrix)): lgrad_arg = Tuple(lgrad_arg) elif dim == 2: lgrad_arg = LogicalGrad_2d(arg) elif dim == 3: lgrad_arg = LogicalGrad_3d(arg) grad_arg = Covariant(mapping, lgrad_arg) expr = grad_arg[0] return expr elif isinstance(expr, dy): if expr.atoms(PlusInterfaceOperator): mapping = mapping.plus elif expr.atoms(MinusInterfaceOperator): mapping = mapping.minus arg = expr.args[0] arg = cls(arg, domain, evaluate=True) if isinstance(arg, PullBack): arg = TerminalExpr(arg, domain=domain.logical_domain) elif isinstance(arg, MatrixElement): arg = TerminalExpr(arg, domain=domain.logical_domain) # ..p if dim == 1: lgrad_arg = LogicalGrad_1d(arg) elif dim == 2: lgrad_arg = LogicalGrad_2d(arg) elif dim == 3: lgrad_arg = LogicalGrad_3d(arg) grad_arg = Covariant(mapping, lgrad_arg) expr = grad_arg[1] return expr elif isinstance(expr, dz): if expr.atoms(PlusInterfaceOperator): mapping = mapping.plus elif expr.atoms(MinusInterfaceOperator): mapping = mapping.minus arg = expr.args[0] arg = cls(arg, domain, evaluate=True) if isinstance(arg, PullBack): arg = TerminalExpr(arg, domain=domain.logical_domain) elif isinstance(arg, MatrixElement): arg = TerminalExpr(arg, domain=domain.logical_domain) # ... if dim == 1: lgrad_arg = LogicalGrad_1d(arg) elif dim == 2: lgrad_arg = LogicalGrad_2d(arg) elif dim == 3: lgrad_arg = LogicalGrad_3d(arg) grad_arg = Covariant(mapping, lgrad_arg) expr = grad_arg[2] return expr elif isinstance(expr, (Symbol, Indexed)): return expr elif isinstance(expr, NormalVector): return expr elif isinstance(expr, Pow): b = expr.base e = expr.exp expr = Pow(cls(b, domain), cls(e, domain)) return expr elif isinstance(expr, Trace): e = cls.eval(expr.expr, domain) bd = expr.boundary.logical_domain order = expr.order return Trace(e, bd, order) elif isinstance(expr, Integral): domain = expr.domain mapping = domain.mapping assert domain is not None if expr.is_domain_integral: J = mapping.jacobian det = sqrt((J.T * J).det()) else: axis = domain.axis J = JacobianSymbol(mapping, axis=axis) det = sqrt((J.T * J).det()) body = cls.eval(expr.expr, domain) * det domain = domain.logical_domain return Integral(body, domain) elif isinstance(expr, BilinearForm): tests = [get_logical_test_function(a) for a in expr.test_functions] trials = [ get_logical_test_function(a) for a in expr.trial_functions ] body = cls.eval(expr.expr, domain) return BilinearForm((trials, tests), body) elif isinstance(expr, LinearForm): tests = [get_logical_test_function(a) for a in expr.test_functions] body = cls.eval(expr.expr, domain) return LinearForm(tests, body) elif isinstance(expr, Norm): kind = expr.kind exponent = expr.exponent e = cls.eval(expr.expr, domain) domain = domain.logical_domain norm = Norm(e, domain, kind, evaluate=False) norm._exponent = exponent return norm elif isinstance(expr, DomainExpression): domain = expr.target J = domain.mapping.jacobian newexpr = cls.eval(expr.expr, domain) newexpr = TerminalExpr(newexpr, domain=domain) domain = domain.logical_domain det = TerminalExpr(sqrt((J.T * J).det()), domain=domain) return DomainExpression(domain, ImmutableDenseMatrix([[newexpr * det]])) elif isinstance(expr, Function): args = [cls.eval(a, domain) for a in expr.args] return type(expr)(*args) return cls(expr, domain, evaluate=False)
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)))
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]))
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 = Matrix((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 l = 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) # ... print(a1) print(TerminalExpr(a1)) print('') print(a2) print(TerminalExpr(a2)) print('') print(a3) print(TerminalExpr(a3)) print('')
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('')