def test_logical_expr_2d_3(): dim = 2 A = Square('A') B = Square('B') M1 = Mapping('M1', dim=dim) M2 = Mapping('M2', dim=dim) D1 = M1(A) D2 = M2(B) domain = D1.join(D2, name='domain', bnd_minus=D1.get_boundary(axis=0, ext=1), bnd_plus=D2.get_boundary(axis=0, ext=-1)) V = VectorFunctionSpace('V', domain, kind='hcurl') u, v = [element_of(V, name=i) for i in ['u', 'v']] int_0 = lambda expr: integral(domain, expr) expr = LogicalExpr(int_0(dot(u, v)), domain) assert str( expr.args[0] ) == 'Integral(A, Dot((Jacobian(M1)**(-1)).T * u, (Jacobian(M1)**(-1)).T * v)*sqrt(det(Jacobian(M1).T * Jacobian(M1))))' assert str( expr.args[1] ) == 'Integral(B, Dot((Jacobian(M2)**(-1)).T * u, (Jacobian(M2)**(-1)).T * v)*sqrt(det(Jacobian(M2).T * Jacobian(M2))))'
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_tensorize_2d_1_mapping(): DIM = 2 M = Mapping('Map', DIM) domain = Domain('Omega', dim=DIM) B1 = Boundary(r'\Gamma_1', domain) x, y = domain.coordinates kappa = Constant('kappa', is_real=True) mu = Constant('mu', is_real=True) V = ScalarFunctionSpace('V', domain) u, v = elements_of(V, names='u, v') int_0 = lambda expr: integral(domain, expr) # ... # a = BilinearForm((u,v), u*v) # a = BilinearForm((u,v), mu*u*v + dot(grad(u),grad(v))) a = BilinearForm((u, v), int_0(dot(grad(u), grad(v)))) # a = BilinearForm((u,v), dx(u)*v) # a = BilinearForm((u,v), laplace(u)*laplace(v)) expr = TensorExpr(a, mapping=M) print(expr)
def test_mapping_2d_2(): print('============ test_mapping_2d_2 ==============') rdim = 2 F = Mapping('F', rdim) domain = Domain('Omega', dim=rdim) D = F(domain)
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_logical_expr_3d_5(): dim = 3 domain = Domain('Omega', dim=dim) M = Mapping('M', dim=dim) mapped_domain = M(domain) V = VectorFunctionSpace('V', domain, kind='hcurl') VM = VectorFunctionSpace('VM', mapped_domain, kind='hcurl') J = M.jacobian u, v = elements_of(V, names='u,v') um, vm = elements_of(VM, names='u,v') int_md = lambda expr: integral(mapped_domain, expr) int_ld = lambda expr: integral(domain, expr) am = BilinearForm((um, vm), int_md(dot(curl(vm), curl(um)))) a = LogicalExpr(am) assert a == BilinearForm( (u, v), int_ld( sqrt((J.T * J).det()) * dot(J / J.det() * curl(u), J / J.det() * curl(v))))
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_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_tensorize_2d_2_mapping(): DIM = 2 M = Mapping('M', DIM) domain = Domain('Omega', dim=DIM) V = VectorFunctionSpace('V', domain) u, v = elements_of(V, names='u, v') c = Constant('c') int_0 = lambda expr: integral(domain, expr) a = BilinearForm((u, v), int_0(c * div(v) * div(u) + curl(v) * curl(u))) expr = TensorExpr(a, mapping=M) print(expr)
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)
def test_mapping_1d(): print('============ test_mapping_1d ==============') rdim = 1 F = Mapping('F', rdim) assert(F.name == 'F') # ... expected = Matrix([[dx1(F[0])]]) assert(F.jacobian == expected) # ... # ... expected = dx1(F[0]) assert(F.det_jacobian == expected)
def test_mapping_1d(): print('============ test_mapping_1d ==============') dim = 1 F = Mapping('F', dim=dim) assert(F.name == 'F') # ... expected = Matrix([[dx1(F[0])]]) assert(Jacobian(F) == expected) # ... # ... expected = dx1(F[0]) assert(Jacobian(F).det() == expected)
def test_logical_expr_3d_2(): dim = 3 domain = Domain('Omega', dim=dim) M = Mapping('M', dim=dim) mapped_domain = M(domain) V = ScalarFunctionSpace('V', domain, kind='h1') VM = ScalarFunctionSpace('VM', mapped_domain, kind='h1') u, v = elements_of(V, names='u,v') um, vm = elements_of(VM, names='u,v') J = M.jacobian a = dot(grad(um), grad(vm)) e = LogicalExpr(a, mapping=M, dim=dim) assert e == dot(J.inv().T * grad(u), J.inv().T * grad(v))
def test_logical_expr_3d_3(): dim = 3 domain = Domain('Omega', dim=dim) M = Mapping('M', dim=3) mapped_domain = M(domain) V = VectorFunctionSpace('V', domain, kind='hcurl') VM = VectorFunctionSpace('VM', mapped_domain, kind='hcurl') u, v = elements_of(V, names='u,v') um, vm = elements_of(VM, names='u,v') J = M.jacobian a = dot(curl(um), curl(vm)) e = LogicalExpr(a, mapping=M, dim=dim) assert e == dot(J / J.det() * curl(u), J / J.det() * curl(v))
def test_logical_expr_3d_4(): dim = 3 domain = Domain('Omega', dim=dim) M = Mapping('M', dim=3) mapped_domain = M(domain) V = VectorFunctionSpace('V', domain, kind='hdiv') VM = VectorFunctionSpace('VM', mapped_domain, kind='hdiv') u, v = elements_of(V, names='u,v') um, vm = elements_of(VM, names='u,v') J = M.jacobian a = div(um) * div(vm) e = LogicalExpr(a, mapping=M, dim=dim) assert e == J.det()**-2 * div(u) * div(v)
def test_mapping_3d(): print('============ test_mapping_3d ==============') rdim = 3 F = Mapping('F', rdim) a,b,c = symbols('a b c') abc = Tuple(a, b, c) assert(F.name == 'F') # ... expected = Matrix([[dx1(F[0]), dx2(F[0]), dx3(F[0])], [dx1(F[1]), dx2(F[1]), dx3(F[1])], [dx1(F[2]), dx2(F[2]), dx3(F[2])]]) assert(Jacobian(F) == expected) # ... # ... expected = (dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) assert(Jacobian(F).det() == expected) # ... # ... expected = Tuple (a*(dx2(F[1])*dx3(F[2]) - dx2(F[2])*dx3(F[1]))/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + b*(-dx1(F[1])*dx3(F[2]) + dx1(F[2])*dx3(F[1]))/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + c*(dx1(F[1])*dx2(F[2]) - dx1(F[2])*dx2(F[1]))/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])), a*(-dx2(F[0])*dx3(F[2]) + dx2(F[2])*dx3(F[0]))/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + b*(dx1(F[0])*dx3(F[2]) - dx1(F[2])*dx3(F[0]))/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + c*(-dx1(F[0])*dx2(F[2]) + dx1(F[2])*dx2(F[0]))/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])), a*(dx2(F[0])*dx3(F[1]) - dx2(F[1])*dx3(F[0]))/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + b*(-dx1(F[0])*dx3(F[1]) + dx1(F[1])*dx3(F[0]))/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + c*(dx1(F[0])*dx2(F[1]) - dx1(F[1])*dx2(F[0]))/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0]))) cov = Covariant(F, abc) cov = Matrix(cov) expected = Matrix(expected) diff = cov-expected diff.simplify() assert(diff.dot(diff).is_zero) # ... # ... expected = Tuple (a*dx1(F[0])/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + b*dx2(F[0])/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + c*dx3(F[0])/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])), a*dx1(F[1])/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + b*dx2(F[1])/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + c*dx3(F[1])/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])), a*dx1(F[2])/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + b*dx2(F[2])/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + c*dx3(F[2])/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0]))) cov = Contravariant(F, abc) assert(simplify(cov) == simplify(expected))
def test_topology_1(): # ... create a domain with 2 subdomains A and B A = InteriorDomain('A', dim=2) B = InteriorDomain('B', dim=2) connectivity = Connectivity() bnd_A_1 = Boundary('Gamma_1', A) bnd_A_2 = Boundary('Gamma_2', A) bnd_A_3 = Boundary('Gamma_3', A) bnd_B_1 = Boundary('Gamma_1', B) bnd_B_2 = Boundary('Gamma_2', B) bnd_B_3 = Boundary('Gamma_3', B) connectivity['I'] = Interface('I', bnd_A_1, bnd_B_2) mapping = Mapping('M', dim=2) logical_domain = Domain('D', dim=2) interiors = [A, B] boundaries = [bnd_A_2, bnd_A_3, bnd_B_1, bnd_B_3] Omega = Domain('Omega', interiors=interiors, boundaries=boundaries, connectivity=connectivity, mapping=mapping, logical_domain=logical_domain) interfaces = Omega.interfaces assert(isinstance(interfaces, Interface)) # export Omega.export('omega.h5') # ... # read it again and check that it has the same description as Omega D = Domain.from_file('omega.h5') assert( D.todict() == Omega.todict() )
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_tensorize_2d_1_mapping(): DIM = 2 M = Mapping('Map', dim=DIM) domain = M(Domain('Omega', dim=DIM)) # mu = Constant('mu' , is_real=True) V = ScalarFunctionSpace('V', domain) u, v = elements_of(V, names='u, v') int_0 = lambda expr: integral(domain , expr) # ... # a = BilinearForm((u,v), u*v) # a = BilinearForm((u,v), mu*u*v + dot(grad(u),grad(v))) a = BilinearForm((u,v), int_0(dot(grad(u),grad(v)))) # a = BilinearForm((u,v), dx(u)*v) # a = BilinearForm((u,v), laplace(u)*laplace(v)) expr = TensorExpr(a, domain=domain) print(expr)
def test_mapping_2d(): print('============ test_mapping_2d ==============') rdim = 2 F = Mapping('F', rdim) a,b = symbols('a b') ab = Tuple(a, b) assert(F.name == 'F') # ... expected = Matrix([[dx1(F[0]), dx2(F[0])], [dx1(F[1]), dx2(F[1])]]) assert(F.jacobian == expected) # ... # ... expected = dx1(F[0])*dx2(F[1]) - dx1(F[1])*dx2(F[0]) assert(F.det_jacobian == expected) # ... # ... expected = Tuple(a*dx2(F[1])/(dx1(F[0])*dx2(F[1]) - dx1(F[1])*dx2(F[0])) - b*dx1(F[1])/(dx1(F[0])*dx2(F[1]) - dx1(F[1])*dx2(F[0])), - a*dx2(F[0])/(dx1(F[0])*dx2(F[1]) - dx1(F[1])*dx2(F[0])) + b*dx1(F[0])/(dx1(F[0])*dx2(F[1]) - dx1(F[1])*dx2(F[0]))) assert(Covariant(F, ab) == expected) # ... # ... expected = Tuple(a*dx1(F[0])/(dx1(F[0])*dx2(F[1]) - dx1(F[1])*dx2(F[0])) + b*dx2(F[0])/(dx1(F[0])*dx2(F[1]) - dx1(F[1])*dx2(F[0])), a*dx1(F[1])/(dx1(F[0])*dx2(F[1]) - dx1(F[1])*dx2(F[0])) + b*dx2(F[1])/(dx1(F[0])*dx2(F[1]) - dx1(F[1])*dx2(F[0]))) assert(simplify(Contravariant(F, ab)) == simplify(expected))
def test_mapping_3d(): print('============ test_mapping_3d ==============') rdim = 3 F = Mapping('F', rdim) a,b,c = symbols('a b c') abc = Tuple(a, b, c) assert(F.name == 'F') # ... expected = Matrix([[dx1(F[0]), dx2(F[0]), dx3(F[0])], [dx1(F[1]), dx2(F[1]), dx3(F[1])], [dx1(F[2]), dx2(F[2]), dx3(F[2])]]) assert(F.jacobian == expected) # ... # ... expected = (dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) assert(F.det_jacobian == expected) # ... # ... expected = Tuple (a*(dx2(F[1])*dx3(F[2]) - dx2(F[2])*dx3(F[1]))/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + b*(-dx1(F[1])*dx3(F[2]) + dx1(F[2])*dx3(F[1]))/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + c*(dx1(F[1])*dx2(F[2]) - dx1(F[2])*dx2(F[1]))/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])), a*(-dx2(F[0])*dx3(F[2]) + dx2(F[2])*dx3(F[0]))/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + b*(dx1(F[0])*dx3(F[2]) - dx1(F[2])*dx3(F[0]))/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + c*(-dx1(F[0])*dx2(F[2]) + dx1(F[2])*dx2(F[0]))/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])), a*(dx2(F[0])*dx3(F[1]) - dx2(F[1])*dx3(F[0]))/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + b*(-dx1(F[0])*dx3(F[1]) + dx1(F[1])*dx3(F[0]))/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + c*(dx1(F[0])*dx2(F[1]) - dx1(F[1])*dx2(F[0]))/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0]))) cov = Covariant(F, abc) assert(simplify(cov) == simplify(expected)) # ... # ... expected = Tuple (a*dx1(F[0])/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + b*dx2(F[0])/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + c*dx3(F[0])/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])), a*dx1(F[1])/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + b*dx2(F[1])/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + c*dx3(F[1])/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])), a*dx1(F[2])/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + b*dx2(F[2])/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0])) + c*dx3(F[2])/(dx1(F[0])*dx2(F[1])*dx3(F[2]) - dx1(F[0])*dx2(F[2])*dx3(F[1]) - dx1(F[1])*dx2(F[0])*dx3(F[2]) + dx1(F[1])*dx2(F[2])*dx3(F[0]) + dx1(F[2])*dx2(F[0])*dx3(F[1]) - dx1(F[2])*dx2(F[1])*dx3(F[0]))) cov = Contravariant(F, abc) assert(simplify(cov) == simplify(expected))
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)
from nodes import StencilMatrixLocalBasis from nodes import StencilVectorLocalBasis from nodes import StencilMatrixGlobalBasis from nodes import StencilVectorGlobalBasis from nodes import ElementOf from nodes import WeightedVolumeQuadrature from nodes import ComputeKernelExpr from nodes import AST from nodes import Block from nodes import Reset from parser import parse # ... abstract model domain = Square() M = Mapping('M', domain.dim) V = ScalarFunctionSpace('V', domain) u,v = elements_of(V, names='u,v') expr = dot(grad(v), grad(u)) # ... # ... grid = Grid() element = Element() g_quad = GlobalTensorQuadrature() l_quad = LocalTensorQuadrature() quad = TensorQuadrature() g_basis = GlobalTensorQuadratureTrialBasis(u) l_basis = LocalTensorQuadratureTrialBasis(u)
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_logical_expr_2d_2(): dim = 2 A = Square('A') B = Square('B') M1 = Mapping('M1', dim=dim) M2 = Mapping('M2', dim=dim) D1 = M1(A) D2 = M2(B) domain = D1.join(D2, name='domain', bnd_minus=D1.get_boundary(axis=0, ext=1), bnd_plus=D2.get_boundary(axis=0, ext=-1)) V1 = ScalarFunctionSpace('V1', domain, kind='h1') V2 = VectorFunctionSpace('V2', domain, kind='h1') u1, v1 = [element_of(V1, name=i) for i in ['u1', 'v1']] u2, v2 = [element_of(V2, name=i) for i in ['u2', 'v2']] int_0 = lambda expr: integral(domain, expr) int_1 = lambda expr: integral(domain.interfaces, expr) expr = LogicalExpr(int_0(u1 * v1), domain) assert str( expr.args[0] ) == 'Integral(A, u1*v1*sqrt(det(Jacobian(M1).T * Jacobian(M1))))' assert str( expr.args[1] ) == 'Integral(B, u1*v1*sqrt(det(Jacobian(M2).T * Jacobian(M2))))' expr = LogicalExpr(int_1(plus(u1) * plus(v1)), domain) assert str( expr ) == 'Integral(A|B, PlusInterfaceOperator(u1)*PlusInterfaceOperator(v1)*sqrt(det(Jacobian(M1|M2).T * Jacobian(M1|M2))))' expr = LogicalExpr(int_1(minus(u1) * minus(v1)), domain) assert str( expr ) == 'Integral(A|B, MinusInterfaceOperator(u1)*MinusInterfaceOperator(v1)*sqrt(det(Jacobian(M1|M2).T * Jacobian(M1|M2))))' expr = LogicalExpr(int_0(dot(u2, v2)), domain) assert str( expr.args[0] ) == 'Integral(A, Dot(u2, v2)*sqrt(det(Jacobian(M1).T * Jacobian(M1))))' assert str( expr.args[1] ) == 'Integral(B, Dot(u2, v2)*sqrt(det(Jacobian(M2).T * Jacobian(M2))))' expr = LogicalExpr(int_1(dot(plus(u2), plus(v2))), domain) assert str( expr ) == 'Integral(A|B, Dot(PlusInterfaceOperator(u2), PlusInterfaceOperator(v2))*sqrt(det(Jacobian(M1|M2).T * Jacobian(M1|M2))))' expr = LogicalExpr(int_1(dot(minus(u2), minus(v2))), domain) assert str( expr ) == 'Integral(A|B, Dot(MinusInterfaceOperator(u2), MinusInterfaceOperator(v2))*sqrt(det(Jacobian(M1|M2).T * Jacobian(M1|M2))))' expr = LogicalExpr(int_1(dot(grad(minus(u2)), grad(minus(v2)))), domain) assert str( expr ) == 'Integral(A|B, Dot((Jacobian(M1)**(-1)).T * Grad(MinusInterfaceOperator(u2)), (Jacobian(M1)**(-1)).T * Grad(MinusInterfaceOperator(v2)))*sqrt(det(Jacobian(M1|M2).T * Jacobian(M1|M2))))' expr = LogicalExpr(int_1(dot(grad(plus(u2)), grad(plus(v2)))), domain) assert str( expr ) == 'Integral(A|B, Dot((Jacobian(M2)**(-1)).T * Grad(PlusInterfaceOperator(u2)), (Jacobian(M2)**(-1)).T * Grad(PlusInterfaceOperator(v2)))*sqrt(det(Jacobian(M1|M2).T * Jacobian(M1|M2))))'
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)