def test_terminal_expr_bilinear_3d_1(): domain = Domain('Omega', dim=3) M = Mapping('M', 3) mapped_domain = M(domain) V = ScalarFunctionSpace('V', domain) VM = ScalarFunctionSpace('VM', mapped_domain) u, v = elements_of(V, names='u,v') um, vm = elements_of(VM, names='u,v') int_0 = lambda expr: integral(domain, expr) int_1 = lambda expr: integral(mapped_domain, expr) J = M.det_jacobian det = dx1(M[0])*dx2(M[1])*dx3(M[2]) - dx1(M[0])*dx2(M[2])*dx3(M[1]) - dx1(M[1])*dx2(M[0])*dx3(M[2])\ + dx1(M[1])*dx2(M[2])*dx3(M[0]) + dx1(M[2])*dx2(M[0])*dx3(M[1]) - dx1(M[2])*dx2(M[1])*dx3(M[0]) a1 = BilinearForm((u, v), int_0(dot(grad(u), grad(v)))) a2 = BilinearForm((um, vm), int_1(dot(grad(um), grad(vm)))) a3 = BilinearForm((u, v), int_0(J * dot(grad(u), grad(v)))) e1 = TerminalExpr(a1) e2 = TerminalExpr(a2) e3 = TerminalExpr(a3) assert e1[0].expr == dx1(u) * dx1(v) + dx2(u) * dx2(v) + dx3(u) * dx3(v) assert e2[0].expr == dx(um) * dx(vm) + dy(um) * dy(vm) + dz(um) * dz(vm) assert e3[0].expr.factor() == (dx1(u) * dx1(v) + dx2(u) * dx2(v) + dx3(u) * dx3(v)) * det
def test_global_quad_basis_span_2d_2(): # ... nderiv = 2 stmts = construct_logical_expressions(u, nderiv) expressions = [dx(u), dx(dy(u)), dy(dy(u))] stmts += [ComputePhysicalBasis(i) for i in expressions] # ... # ... stmts += [Reduction('+', ComputePhysicalBasis(dx(u)*dx(v)))] # ... # ... loop = Loop((l_quad, a_basis), index_quad, stmts) # ... # ... stmts = [loop] loop = Loop(l_basis, index_dof, stmts) # ... # ... stmts = [loop] loop = Loop((g_quad, g_basis, g_span), index_element, stmts) # ... stmt = parse(loop, settings={'dim': domain.dim, 'nderiv': nderiv}) print(pycode(stmt)) print()
def test_logical_expr_2d_1(): rdim = 2 M = Mapping('M', rdim) domain = M(Domain('Omega', dim=rdim)) alpha = Constant('alpha') V = ScalarFunctionSpace('V', domain, kind='h1') W = VectorFunctionSpace('V', domain, kind='h1') u, v = [element_of(V, name=i) for i in ['u', 'v']] w = element_of(W, name='w') det_M = Jacobian(M).det() #print('det = ', det_M) det = Symbol('det') # ... expr = 2 * u + alpha * v expr = LogicalExpr(expr, mapping=M, dim=rdim) #print(expr) #print('') # ... # ... expr = dx(u) expr = LogicalExpr(expr, mapping=M, dim=rdim) #print(expr.subs(det_M, det)) #print('') # ... # ... expr = dy(u) expr = LogicalExpr(expr, mapping=M, dim=rdim) #print(expr.subs(det_M, det)) #print('') # ... # ... expr = dx(det_M) expr = LogicalExpr(expr, mapping=M, dim=rdim) expr = expr.subs(det_M, det) expr = expand(expr) #print(expr) #print('') # ... # ... expr = dx(dx(u)) expr = LogicalExpr(expr, mapping=M, dim=rdim) #print(expr.subs(det_M, det)) #print('') # ... # ... expr = dx(w[0]) expr = LogicalExpr(expr, mapping=M, dim=rdim)
def test_partial_derivatives_2(): print('============ test_partial_derivatives_2 ==============') # ... domain = Domain('Omega', dim=2) M = Mapping('M', dim=2) mapped_domain = M(domain) V = ScalarFunctionSpace('V', mapped_domain) F = element_of(V, name='F') alpha = Constant('alpha') beta = Constant('beta') # ... # ... expr = alpha * dx(F) indices = get_index_derivatives_atom(expr, F)[0] assert (indices_as_str(indices) == 'x') # ... # ... expr = dy(dx(F)) indices = get_index_derivatives_atom(expr, F)[0] assert (indices_as_str(indices) == 'xy') # ... # ... expr = alpha * dx(dy(dx(F))) indices = get_index_derivatives_atom(expr, F)[0] assert (indices_as_str(indices) == 'xxy') # ... # ... expr = alpha * dx(dx(F)) + beta * dy(F) + dx(dy(F)) indices = get_index_derivatives_atom(expr, F) indices = [indices_as_str(i) for i in indices] assert (sorted(indices) == ['xx', 'xy', 'y']) # ... # ... expr = alpha * dx(dx(F)) + beta * dy(F) + dx(dy(F)) d = get_max_partial_derivatives(expr, F) assert (indices_as_str(d) == 'xxy') d = get_max_partial_derivatives(expr) assert (indices_as_str(d) == 'xxy')
def test_logical_expr_3d_1(): rdim = 3 M = Mapping('M', rdim) domain = Domain('Omega', dim=rdim) alpha = Constant('alpha') V = ScalarFunctionSpace('V', domain) u, v = [element_of(V, name=i) for i in ['u', 'v']] det_M = DetJacobian(M) #print('det = ', det_M) det = Symbol('det') # ... expr = 2 * u + alpha * v expr = LogicalExpr(M, expr) #print(expr) #print('') # ... # ... expr = dx(u) expr = LogicalExpr(M, expr) #print(expr.subs(det_M, det)) #print('') # ... # ... expr = dy(u) expr = LogicalExpr(M, expr) #print(expr.subs(det_M, det)) #print('') # ... # ... expr = dx(det_M) expr = LogicalExpr(M, expr) expr = expr.subs(det_M, det) expr = expand(expr) #print(expr) #print('') # ... # ... expr = dx(dx(u)) expr = LogicalExpr(M, expr)
def test_field_2d_1(): print('============ test_field_2d_1 =============') # x, y = domain.coordinates W = VectorFunctionSpace('W', domain) F = element_of(W, 'F') assert (dx(F) == Matrix([[dx(F[0]), dx(F[1])]])) # TODO not working yet => check it for VectorFunction also # print(dx(x*F)) expr = inner(grad(F), grad(F)) print(expr)
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('')
def test_logical_expr_3d_1(): dim = 3 M = Mapping('M', dim=dim) domain = M(Domain('Omega', dim=dim)) alpha = Constant('alpha') V = ScalarFunctionSpace('V', domain, kind='h1') u, v = [element_of(V, name=i) for i in ['u', 'v']] det_M = Jacobian(M).det() #print('det = ', det_M) det = Symbol('det') # ... expr = 2 * u + alpha * v expr = LogicalExpr(expr, domain) #print(expr) #print('') # ... # ... expr = dx(u) expr = LogicalExpr(expr, domain) #print(expr.subs(det_M, det)) #print('') # ... # ... expr = dy(u) expr = LogicalExpr(expr, domain) #print(expr.subs(det_M, det)) #print('') # ... # ... expr = dx(det_M) expr = LogicalExpr(expr, domain) expr = expr.subs(det_M, det) #print(expr) #print('') # ... # ... expr = dx(dx(u)) expr = LogicalExpr(expr, domain)
def test_derivatives_2d_with_mapping(): dim = 2 M = Mapping('M', dim=dim) O = M(Domain('Omega', dim=dim)) V = ScalarFunctionSpace('V', O, kind='h1') u = element_of(V, 'u') det_jac = Jacobian(M).det() J = Symbol('J') expr = M assert SymbolicExpr(expr) == Symbol('M') expr = M[0] assert SymbolicExpr(expr) == Symbol('x') expr = M[1] assert SymbolicExpr(expr) == Symbol('y') expr = dx2(M[0]) assert SymbolicExpr(expr) == Symbol('x_x2') expr = dx1(M[1]) assert SymbolicExpr(expr) == Symbol('y_x1') expr = LogicalExpr(dx(u), O).subs(det_jac, J) expected = '(u_x1 * y_x2 - u_x2 * y_x1)/J' difference = SymbolicExpr(expr) - sympify(expected) assert difference.simplify() == 0
def test_derivatives_2d_without_mapping(): O = Domain('Omega', dim=2) V = ScalarFunctionSpace('V', O) u = element_of(V, 'u') expr = u assert SymbolicExpr(expr) == Symbol('u') expr = dx(u) assert SymbolicExpr(expr) == Symbol('u_x') expr = dx(dx(u)) assert SymbolicExpr(expr) == Symbol('u_xx') expr = dx(dy(u)) assert SymbolicExpr(expr) == Symbol('u_xy') expr = dy(dx(u)) assert SymbolicExpr(expr) == Symbol('u_xy') expr = dy(dx(dz(u))) assert SymbolicExpr(expr) == Symbol('u_xyz') expr = dy(dy(dx(u))) assert SymbolicExpr(expr) == Symbol('u_xyy') expr = dy(dz(dy(u))) assert SymbolicExpr(expr) == Symbol('u_yyz')
def test_global_quad_basis_span_2d_matrix_2(): # ... nderiv = 1 stmts = construct_logical_expressions(u, nderiv) expressions = [dx(v), dy(v), dx(u), dy(u)] stmts += [ComputePhysicalBasis(i) for i in expressions] # ... # ... loop = Loop((l_quad, a_basis, GeometryExpressions(M, nderiv)), index_quad, stmts) # ... # ... loop = Reduce('+', ComputeKernelExpr(dx(u)*dx(v)), ElementOf(l_mat), loop) # ... # ... loop over trials stmts = [loop] loop = Loop(l_basis, index_dof_trial, stmts) # ... # ... loop over tests stmts = [loop] loop = Loop(l_basis_v, index_dof_test, stmts) # ... # ... body = (Reset(l_mat), loop) stmts = Block(body) # ... # ... loop = Loop((g_quad, g_basis, g_basis_v, g_span), index_element, stmts) # ... # ... body = (Reset(g_mat), Reduce('+', l_mat, g_mat, loop)) stmt = Block(body) # ... stmt = parse(stmt, settings={'dim': domain.dim, 'nderiv': nderiv, 'mapping': M}) print(pycode(stmt)) print()
def test_loop_local_dof_quad_2d_2(): # ... args = [dx(u), dx(dy(u)), dy(dy(u)), dx(u) + dy(u)] stmts = [ComputePhysicalBasis(i) for i in args] # ... # ... loop = Loop((l_quad, a_basis), index_quad, stmts) # ... # ... stmts = [loop] loop = Loop(l_basis, index_dof, stmts) # ... stmt = parse(loop, settings={'dim': domain.dim, 'nderiv': 3}) print() print(pycode(stmt)) print()
def test_identity_mapping_2d_2(): dim = 2 M = IdentityMapping('F', dim=dim) domain = M(Domain('Omega', dim=dim)) V = ScalarFunctionSpace('V', domain, kind='h1') u = element_of(V, name='u') # ... assert (LogicalExpr(dx(u), domain) == dx1(u)) assert (LogicalExpr(dy(u), domain) == dx2(u))
def test_global_quad_basis_span_2d_vector_2(): # ... nderiv = 1 stmts = construct_logical_expressions(v, nderiv) # expressions = [dx(v), v] # TODO Wrong result expressions = [dx(v), dy(v)] stmts += [ComputePhysicalBasis(i) for i in expressions] # ... # ... case with mapping <> identity loop = Loop((l_quad, a_basis, GeometryExpressions(M, nderiv)), index_quad, stmts) # ... # ... loop = Reduce('+', ComputeKernelExpr(dx(v)*cos(x+y)), ElementOf(l_vec), loop) # ... # ... loop over tests stmts = [loop] loop = Loop(l_basis_v, index_dof_test, stmts) # ... # ... body = (Reset(l_vec), loop) stmts = Block(body) # ... # ... loop = Loop((g_quad, g_basis_v, g_span), index_element, stmts) # ... # ... body = (Reset(g_vec), Reduce('+', l_vec, g_vec, loop)) stmt = Block(body) # ... stmt = parse(stmt, settings={'dim': domain.dim, 'nderiv': nderiv, 'mapping': M}) print(pycode(stmt)) print()
def test_global_quad_basis_span_2d_vector_1(): # ... nderiv = 2 stmts = construct_logical_expressions(v, nderiv) expressions = [dx(v), dx(dy(v)), dy(dy(v))] stmts += [ComputePhysicalBasis(i) for i in expressions] # ... # ... loop = Loop((l_quad, a_basis), index_quad, stmts) # ... # ... loop = Reduce('+', ComputeKernelExpr(dx(v)*cos(x+y)), ElementOf(l_vec), loop) # ... # ... loop over tests stmts = [loop] loop = Loop(l_basis_v, index_dof_test, stmts) # ... # ... body = (Reset(l_vec), loop) stmts = Block(body) # ... # ... loop = Loop((g_quad, g_basis_v, g_span), index_element, stmts) # ... # ... body = (Reset(g_vec), Reduce('+', l_vec, g_vec, loop)) stmt = Block(body) # ... stmt = parse(stmt, settings={'dim': domain.dim, 'nderiv': nderiv}) print(pycode(stmt)) print()
def test_identity_mapping_2d_2(): rdim = 2 x1, x2 = symbols('x1, x2') domain = Domain('Omega', dim=rdim) M = IdentityMapping('F', rdim) V = ScalarFunctionSpace('V', domain) u = element_of(V, name='u') # ... assert (LogicalExpr(M, dx(u)) == dx1(u)) assert (LogicalExpr(M, dy(u)) == dx2(u))
def test_identity_mapping_2d_2(): rdim = 2 x1, x2 = symbols('x1, x2') M = IdentityMapping('F', rdim) domain = M(Domain('Omega', dim=rdim)) V = ScalarFunctionSpace('V', domain, kind='h1') u = element_of(V, name='u') # ... assert (LogicalExpr(dx(u), mapping=M, dim=rdim, subs=True) == dx1(u)) assert (LogicalExpr(dy(u), mapping=M, dim=rdim, subs=True) == dx2(u))
def test_basis_atom_2d_2(): expr = dy(dx(u)) lhs = BasisAtom(expr) rhs = PhysicalBasisValue(expr) settings = {'dim': domain.dim, 'nderiv': 1} _parse = lambda expr: parse(expr, settings=settings) u_xy = Symbol('u_xy') u_x1x2 = Symbol('u_x1x2') assert(lhs.atom == u) assert(_parse(lhs) == u_xy) assert(_parse(rhs) == u_x1x2)
def test_loop_local_dof_quad_2d_4(): # ... stmts = [] expressions = [dx1(u), dx2(u)] stmts += [ComputeLogicalBasis(i) for i in expressions] expressions = [dx(u)] stmts += [ComputePhysicalBasis(i) for i in expressions] # ... # ... loop = Loop((l_quad, a_basis), index_quad, stmts) # ... # ... stmts = [loop] loop = Loop(l_basis, index_dof, stmts) # ... stmt = parse(loop, settings={'dim': domain.dim, 'nderiv': 3}) print() print(pycode(stmt)) print()
def test_symbolic_expr_3d_1(): rdim = 3 M = Mapping('M', rdim) domain = Domain('Omega', dim=rdim) alpha = Constant('alpha') V = ScalarFunctionSpace('V', domain) u = element_of(V, 'u') det_M = DetJacobian(M) det_M = SymbolicExpr(det_M) #print('>>> ', det_M) det = Symbol('det') # ... expr = u expr = LogicalExpr(M, expr) expr = SymbolicExpr(expr) #print(expr) # ... # ... expr = dx1(u) expr = LogicalExpr(M, expr) expr = SymbolicExpr(expr) #print(expr) # ... # ... expr = dx1(dx2(u)) expr = LogicalExpr(M, expr) expr = SymbolicExpr(expr) #print(expr) # ... # ... expr = dx1(M[0]) expr = LogicalExpr(M, expr) expr = SymbolicExpr(expr) #print(expr) # ... # ... expr = dx(u) expr = LogicalExpr(M, expr) expr = SymbolicExpr(expr) expr = expr.subs(det_M, det) #print(expr) # ... # ... expr = dx(DetJacobian(M)) expr = LogicalExpr(M, expr) expr = SymbolicExpr(expr) expr = expr.subs(det_M, det) #print(expand(expr)) # ... # ... expr = dx(dx(u)) expr = LogicalExpr(M, expr) expr = SymbolicExpr(expr) expr = expr.subs(det_M, det) #print(expand(expr)) # ... # ... expr = dx(dx(dx(u))) expr = LogicalExpr(M, expr) expr = SymbolicExpr(expr) expr = expr.subs(det_M, det)
def test_linearity_2d_1(): domain = Square() x, y = domain.coordinates alpha = Constant('alpha') f1 = x * y f2 = x + y f = Tuple(f1, f2) V = FunctionSpace('V', domain) # TODO improve: naming are not given the same way G = Field('G', V) p, q = [TestFunction(V, name=i) for i in ['p', 'q']] ##################################### # linear expressions ##################################### # ... expr = p assert (is_linear_form(expr, p)) # ... # ... expr = alpha * p assert (is_linear_form(expr, p)) # ... # ... expr = dx(p) assert (is_linear_form(expr, p)) # ... # ... expr = dot(grad(p), f) assert (is_linear_form(expr, p)) # ... # ... expr = laplace(p) assert (is_linear_form(expr, p)) # ... # ... expr = alpha * p + dot(grad(p), f) + dx(p) + laplace(p) assert (is_linear_form(expr, p)) # ... ##################################### ##################################### # nonlinear expressions ##################################### # ... with pytest.raises(UnconsistentLinearExpressionError): expr = p**2 is_linear_form(expr, p) # ... # ... with pytest.raises(UnconsistentLinearExpressionError): expr = dot(grad(p), grad(p)) is_linear_form(expr, p)
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))
def test_partial_derivatives_1(): print('============ test_partial_derivatives_1 ==============') # ... domain = Domain('Omega', dim=2) M = Mapping('M', dim=2) mapped_domain = M(domain) x, y = mapped_domain.coordinates V = ScalarFunctionSpace('V', mapped_domain) F, u, v, w = [element_of(V, name=i) for i in ['F', 'u', 'v', 'w']] uvw = Tuple(u, v, w) alpha = Constant('alpha') beta = Constant('beta') # ... assert (dx(x**2) == 2 * x) assert (dy(x**2) == 0) assert (dz(x**2) == 0) assert (dx(y**2) == 0) assert (dy(y**2) == 2 * y) assert (dz(y**2) == 0) assert (dx(x * F) == F + x * dx(F)) assert (dx(uvw) == Matrix([[dx(u), dx(v), dx(w)]])) assert (dx(uvw) + dy(uvw) == Matrix( [[dx(u) + dy(u), dx(v) + dy(v), dx(w) + dy(w)]])) expected = Matrix([[ alpha * dx(u) + beta * dy(u), alpha * dx(v) + beta * dy(v), alpha * dx(w) + beta * dy(w) ]]) assert (alpha * dx(uvw) + beta * dy(uvw) == expected)
def test_symbolic_expr_3d_1(): dim = 3 M = Mapping('M', dim=dim) domain = M(Domain('Omega', dim=dim)) V = ScalarFunctionSpace('V', domain, kind='h1') u = element_of(V, 'u') det_M = Jacobian(M).det() det_M = SymbolicExpr(det_M) #print('>>> ', det_M) det = Symbol('det') # ... expr = u expr = LogicalExpr(expr, domain) expr = SymbolicExpr(expr) #print(expr) # ... # ... expr = dx1(u) expr = LogicalExpr(expr, domain) expr = SymbolicExpr(expr) #print(expr) # ... # ... expr = dx1(dx2(u)) expr = LogicalExpr(expr, domain) expr = SymbolicExpr(expr) #print(expr) # ... # ... expr = dx1(M[0]) expr = LogicalExpr(expr, domain) expr = SymbolicExpr(expr) #print(expr) # ... # ... expr = dx(u) expr = LogicalExpr(expr, domain) expr = SymbolicExpr(expr) expr = expr.subs(det_M, det) #print(expr) # ... # ... expr = dx(Jacobian(M).det()) expr = LogicalExpr(expr, domain) expr = SymbolicExpr(expr) expr = expr.subs(det_M, det) #print(expand(expr)) # ... # ... expr = dx(dx(u)) expr = LogicalExpr(expr, domain) expr = SymbolicExpr(expr) expr = expr.subs(det_M, det) #print(expand(expr)) # ... # ... expr = dx(dx(dx(u))) expr = LogicalExpr(expr, domain) expr = SymbolicExpr(expr) expr = expr.subs(det_M, det)
def test_symbolic_expr_1d_1(): rdim = 1 M = Mapping('M', rdim) domain = M(Domain('Omega', dim=rdim)) alpha = Constant('alpha') V = ScalarFunctionSpace('V', domain, kind='h1') u = element_of(V, name='u') det_M = Jacobian(M).det() det_M = SymbolicExpr(det_M) #print('>>> ', det_M) det = Symbol('det') # ... expr = u expr = LogicalExpr(expr, mapping=M, dim=rdim) expr = SymbolicExpr(expr) #print(expr) # ... # ... expr = dx1(u) expr = LogicalExpr(expr, mapping=M, dim=rdim) expr = SymbolicExpr(expr) #print(expr) # ... # ... expr = dx1(M[0]) expr = LogicalExpr(expr, mapping=M, dim=rdim) expr = SymbolicExpr(expr) #print(expr) # ... # ... expr = dx(u) expr = LogicalExpr(expr, mapping=M, dim=rdim) expr = SymbolicExpr(expr) expr = expr.subs(det_M, det) #print(expr) # ... # ... expr = dx(Jacobian(M).det()) expr = LogicalExpr(expr, mapping=M, dim=rdim) expr = SymbolicExpr(expr) expr = expr.subs(det_M, det) #print(expand(expr)) # ... # ... expr = dx(dx(u)) expr = LogicalExpr(expr, mapping=M, dim=rdim) expr = SymbolicExpr(expr) expr = expr.subs(det_M, det) #print(expand(expr)) # ... # ... expr = dx(dx(dx(u))) expr = LogicalExpr(expr, mapping=M, dim=rdim) expr = SymbolicExpr(expr) expr = expr.subs(det_M, det)
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)