def test_target_mapping_2d_1(): dim = 2 x1, x2 = symbols('x1, x2') constants = ['c1', 'c2', 'D', 'k'] c1, c2, D, k = [Constant(i) for i in constants] M = TargetMapping('M', dim=dim) domain = M(Domain('Omega', dim=dim)) assert (not (M[0] == x1)) assert (not (M[1] == x2)) assert (LogicalExpr(M[0], domain) == -D * x1**2 + c1 + x1 * (-k + 1) * cos(x2)) assert (LogicalExpr(M[1], domain) == c2 + x1 * (k + 1) * sin(x2)) assert (LogicalExpr(dx1(M[0]), domain) == -2 * D * x1 + (-k + 1) * cos(x2)) assert (LogicalExpr(dx1(M[1]), domain) == (k + 1) * sin(x2)) assert (LogicalExpr(dx2(M[0]), domain) == -x1 * (-k + 1) * sin(x2)) assert (LogicalExpr(dx2(M[1]), domain) == x1 * (k + 1) * cos(x2)) expected = Matrix( [[-2 * D * x1 + (-k + 1) * cos(x2), -x1 * (-k + 1) * sin(x2)], [(k + 1) * sin(x2), x1 * (k + 1) * cos(x2)]]) assert (Jacobian(M) == expected)
def test_polar_mapping_2d_1(): rdim = 2 x1, x2 = symbols('x1, x2') constants = ['c1', 'c2', 'rmax', 'rmin'] c1, c2, rmax, rmin = [Constant(i) for i in constants] M = PolarMapping('M', rdim) assert (not (M[0] == x1)) assert (not (M[1] == x2)) assert (LogicalExpr(M[0], mapping=M, dim=rdim, subs=True) == c1 + (rmax * x1 + rmin * (-x1 + 1)) * cos(x2)) assert (LogicalExpr(M[1], mapping=M, dim=rdim, subs=True) == c2 + (rmax * x1 + rmin * (-x1 + 1)) * sin(x2)) assert (LogicalExpr(dx1(M[0]), mapping=M, dim=rdim, subs=True) == (rmax - rmin) * cos(x2)) assert (LogicalExpr(dx1(M[1]), mapping=M, dim=rdim, subs=True) == (rmax - rmin) * sin(x2)) expected = -(rmax * x1 + rmin * (-x1 + 1)) * sin(x2) assert (expand(LogicalExpr(dx2(M[0]), mapping=M, dim=rdim, subs=True)) == expand(expected)) assert (LogicalExpr(dx2(M[1]), mapping=M, dim=rdim, subs=True) == (rmax * x1 + rmin * (-x1 + 1)) * cos(x2)) expected = Matrix([[(rmax - rmin) * cos(x2), -(rmax * x1 + rmin * (-x1 + 1)) * sin(x2)], [(rmax - rmin) * sin(x2), (rmax * x1 + rmin * (-x1 + 1)) * cos(x2)]]) assert (expand(LogicalExpr(Jacobian(M), mapping=M, dim=rdim, subs=True)) == expand(expected))
def test_target_mapping_2d_1(): rdim = 2 x1, x2 = symbols('x1, x2') constants = ['c1', 'c2', 'D', 'k'] c1, c2, D, k = [Constant(i) for i in constants] M = TargetMapping('M', rdim) assert (not (M[0] == x1)) assert (not (M[1] == x2)) assert (LogicalExpr(M[0], mapping=M, dim=rdim, subs=True) == -D * x1**2 + c1 + x1 * (-k + 1) * cos(x2)) assert (LogicalExpr(M[1], mapping=M, dim=rdim, subs=True) == c2 + x1 * (k + 1) * sin(x2)) assert (LogicalExpr(dx1(M[0]), mapping=M, dim=rdim, subs=True) == -2 * D * x1 + (-k + 1) * cos(x2)) assert (LogicalExpr(dx1(M[1]), mapping=M, dim=rdim, subs=True) == (k + 1) * sin(x2)) assert (LogicalExpr(dx2(M[0]), mapping=M, dim=rdim, subs=True) == -x1 * (-k + 1) * sin(x2)) assert (LogicalExpr(dx2(M[1]), mapping=M, dim=rdim, subs=True) == x1 * (k + 1) * cos(x2)) expected = Matrix( [[-2 * D * x1 + (-k + 1) * cos(x2), -x1 * (-k + 1) * sin(x2)], [(k + 1) * sin(x2), x1 * (k + 1) * cos(x2)]]) assert (not (Jacobian(M) == expected)) assert (expand(LogicalExpr(Jacobian(M), mapping=M, dim=rdim, subs=True)) == expand(expected))
def test_target_mapping_2d_1(): rdim = 2 x1, x2 = symbols('x1, x2') constants = ['c1', 'c2', 'D', 'k'] c1, c2, D, k = [Constant(i) for i in constants] M = TargetMapping('M', rdim) assert (not (M[0] == x1)) assert (not (M[1] == x2)) assert (LogicalExpr(M, M[0]) == -D * x1**2 + c1 + x1 * (-k + 1) * cos(x2)) assert (LogicalExpr(M, M[1]) == c2 + x1 * (k + 1) * sin(x2)) assert (LogicalExpr(M, dx1(M[0])) == -2 * D * x1 - k * x1 * cos(x2) + (-k + 1) * cos(x2)) assert (LogicalExpr(M, dx1(M[1])) == (k + 1) * sin(x2)) assert (LogicalExpr(M, dx2(M[0])) == x1 * (-k * cos(x2) - (-k + 1) * sin(x2))) assert (LogicalExpr(M, dx2(M[1])) == x1 * (k + 1) * cos(x2)) expected = Matrix([[ -2 * D * x1 - k * x1 * cos(x2) + (-k + 1) * cos(x2), x1 * (-k * cos(x2) - (-k + 1) * sin(x2)) ], [(k + 1) * sin(x2), x1 * (k + 1) * cos(x2)]]) assert (not (M.jacobian == expected)) assert (expand(LogicalExpr(M, M.jacobian)) == expand(expected))
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_twisted_target_mapping_3d_1(): rdim = 3 x1, x2, x3 = symbols('x1, x2, x3') constants = ['c1', 'c2', 'c3', 'D', 'k'] c1, c2, c3, D, k = [Constant(i) for i in constants] M = TwistedTargetMapping('M', rdim) assert (not (M[0] == x1)) assert (not (M[1] == x2)) assert (not (M[2] == x3)) expected = -D * x1**2 + c1 + x1 * (-k + 1) * cos(x2) assert (LogicalExpr(M[0], mapping=M, dim=rdim, subs=True) == expected) expected = c2 + x1 * (k + 1) * sin(x2) assert (LogicalExpr(M[1], mapping=M, dim=rdim, subs=True) == expected) expected = c3 + x1**2 * x3 * sin(2 * x2) assert (LogicalExpr(M[2], mapping=M, dim=rdim, subs=True) == expected) expected = -2 * D * x1 + (-k + 1) * cos(x2) assert (LogicalExpr(dx1(M[0]), mapping=M, dim=rdim, subs=True) == expected) expected = (k + 1) * sin(x2) assert (LogicalExpr(dx1(M[1]), mapping=M, dim=rdim, subs=True) == expected) expected = 2 * x1 * x3 * sin(2 * x2) assert (LogicalExpr(dx1(M[2]), mapping=M, dim=rdim, subs=True) == expected) expected = -x1 * (-k + 1) * sin(x2) assert (LogicalExpr(dx2(M[0]), mapping=M, dim=rdim, subs=True) == expected) expected = x1 * (k + 1) * cos(x2) assert (LogicalExpr(dx2(M[1]), mapping=M, dim=rdim, subs=True) == expected) expected = 2 * x1**2 * x3 * cos(2 * x2) assert (LogicalExpr(dx2(M[2]), mapping=M, dim=rdim, subs=True) == expected) expected = 0 assert (expand(LogicalExpr(dx3(M[0]), mapping=M, dim=rdim, subs=True)) == expand(expected)) expected = 0 assert (LogicalExpr(dx3(M[1]), mapping=M, dim=rdim, subs=True) == expected) expected = x1**2 * sin(2 * x2) assert (LogicalExpr(dx3(M[2]), mapping=M, dim=rdim, subs=True) == expected) expected = Matrix( [[-2 * D * x1 + (-k + 1) * cos(x2), -x1 * (-k + 1) * sin(x2), 0], [(k + 1) * sin(x2), x1 * (k + 1) * cos(x2), 0], [ 2 * x1 * x3 * sin(2 * x2), 2 * x1**2 * x3 * cos(2 * x2), x1**2 * sin(2 * x2) ]]) assert (not (Jacobian(M) == expected)) assert (expand(LogicalExpr(Jacobian(M), mapping=M, dim=rdim, subs=True)) == expand(expected))
def test_torus_mapping_3d_1(): dim = 3 x1, x2, x3 = symbols('x1, x2, x3') R0 = Constant('R0') M = TorusMapping('M', dim=dim) domain = M(Domain('Omega', dim=dim)) assert (not (M[0] == x1)) assert (not (M[1] == x2)) assert (not (M[2] == x3)) expected = (R0 + x1 * cos(x2)) * cos(x3) assert (LogicalExpr(M[0], domain) == expected) expected = (R0 + x1 * cos(x2)) * sin(x3) assert (LogicalExpr(M[1], domain) == expected) expected = x1 * sin(x2) assert (LogicalExpr(M[2], domain) == expected) expected = cos(x2) * cos(x3) assert (LogicalExpr(dx1(M[0]), domain) == expected) expected = sin(x3) * cos(x2) assert (LogicalExpr(dx1(M[1]), domain) == expected) expected = sin(x2) assert (LogicalExpr(dx1(M[2]), domain) == expected) expected = -x1 * sin(x2) * cos(x3) assert (LogicalExpr(dx2(M[0]), domain) == expected) expected = -x1 * sin(x2) * sin(x3) assert (LogicalExpr(dx2(M[1]), domain) == expected) expected = x1 * cos(x2) assert (LogicalExpr(dx2(M[2]), domain) == expected) expected = -(R0 + x1 * cos(x2)) * sin(x3) assert (expand(LogicalExpr(dx3(M[0]), domain)) == expand(expected)) expected = (R0 + x1 * cos(x2)) * cos(x3) assert (LogicalExpr(dx3(M[1]), domain) == expected) expected = 0 assert (LogicalExpr(dx3(M[2]), domain) == expected) expected = Matrix([[ cos(x2) * cos(x3), -x1 * sin(x2) * cos(x3), -(R0 + x1 * cos(x2)) * sin(x3) ], [ sin(x3) * cos(x2), -x1 * sin(x2) * sin(x3), (R0 + x1 * cos(x2)) * cos(x3) ], [sin(x2), x1 * cos(x2), 0]]) assert (all(e.expand().is_zero for e in (Jacobian(M) - expected)))
def test_torus_mapping_3d_1(): rdim = 3 x1, x2, x3 = symbols('x1, x2, x3') R0 = Constant('R0') M = TorusMapping('M', rdim) assert (not (M[0] == x1)) assert (not (M[1] == x2)) assert (not (M[2] == x3)) expected = (R0 + x1 * cos(x2)) * cos(x3) assert (LogicalExpr(M[0], mapping=M, dim=rdim, subs=True) == expected) expected = (R0 + x1 * cos(x2)) * sin(x3) assert (LogicalExpr(M[1], mapping=M, dim=rdim, subs=True) == expected) expected = x1 * sin(x2) assert (LogicalExpr(M[2], mapping=M, dim=rdim, subs=True) == expected) expected = cos(x2) * cos(x3) assert (LogicalExpr(dx1(M[0]), mapping=M, dim=rdim, subs=True) == expected) expected = sin(x3) * cos(x2) assert (LogicalExpr(dx1(M[1]), mapping=M, dim=rdim, subs=True) == expected) expected = sin(x2) assert (LogicalExpr(dx1(M[2]), mapping=M, dim=rdim, subs=True) == expected) expected = -x1 * sin(x2) * cos(x3) assert (LogicalExpr(dx2(M[0]), mapping=M, dim=rdim, subs=True) == expected) expected = -x1 * sin(x2) * sin(x3) assert (LogicalExpr(dx2(M[1]), mapping=M, dim=rdim, subs=True) == expected) expected = x1 * cos(x2) assert (LogicalExpr(dx2(M[2]), mapping=M, dim=rdim, subs=True) == expected) expected = -(R0 + x1 * cos(x2)) * sin(x3) assert (expand(LogicalExpr(dx3(M[0]), mapping=M, dim=rdim, subs=True)) == expand(expected)) expected = (R0 + x1 * cos(x2)) * cos(x3) assert (LogicalExpr(dx3(M[1]), mapping=M, dim=rdim, subs=True) == expected) expected = 0 assert (LogicalExpr(dx3(M[2]), mapping=M, dim=rdim, subs=True) == expected) expected = Matrix([[ cos(x2) * cos(x3), -x1 * sin(x2) * cos(x3), -(R0 + x1 * cos(x2)) * sin(x3) ], [ sin(x3) * cos(x2), -x1 * sin(x2) * sin(x3), (R0 + x1 * cos(x2)) * cos(x3) ], [sin(x2), x1 * cos(x2), 0]]) assert (not (Jacobian(M) == expected)) assert (expand(LogicalExpr(Jacobian(M), mapping=M, dim=rdim, subs=True)) == expand(expected))
def test_collela_mapping_2d_1(): dim = 2 x1, x2 = symbols('x1, x2') constants = ['eps', 'k1', 'k2'] eps, k1, k2 = [Constant(i) for i in constants] M = CollelaMapping2D('M', dim) domain = M(Domain('Omega', dim=dim)) assert (not (M[0] == x1)) assert (not (M[1] == x2)) expected = 2.0 * eps * sin(2.0 * k1 * pi * x1) * sin( 2.0 * k2 * pi * x2) + 2.0 * x1 - 1.0 assert (LogicalExpr(M[0], domain) == expected) expected = 2.0 * eps * sin(2.0 * k1 * pi * x1) * sin( 2.0 * k2 * pi * x2) + 2.0 * x2 - 1.0 assert (LogicalExpr(M[1], domain) == expected) expected = 4.0 * eps * k1 * pi * sin(2.0 * k2 * pi * x2) * cos( 2.0 * k1 * pi * x1) + 2.0 assert (LogicalExpr(dx1(M[0]), domain) == expected) expected = 4.0 * eps * k1 * pi * sin(2.0 * k2 * pi * x2) * cos( 2.0 * k1 * pi * x1) assert (LogicalExpr(dx1(M[1]), domain) == expected) expected = 4.0 * eps * k2 * pi * sin(2.0 * k1 * pi * x1) * cos( 2.0 * k2 * pi * x2) assert (LogicalExpr(dx2(M[0]), domain) == expected) expected = 4.0 * eps * k2 * pi * sin(2.0 * k1 * pi * x1) * cos( 2.0 * k2 * pi * x2) + 2.0 assert (LogicalExpr(dx2(M[1]), domain) == expected) expected = Matrix([[ 4.0 * eps * k1 * pi * sin(2.0 * k2 * pi * x2) * cos(2.0 * k1 * pi * x1) + 2.0, 4.0 * eps * k2 * pi * sin(2.0 * k1 * pi * x1) * cos(2.0 * k2 * pi * x2) ], [ 4.0 * eps * k1 * pi * sin(2.0 * k2 * pi * x2) * cos(2.0 * k1 * pi * x1), 4.0 * eps * k2 * pi * sin(2.0 * k1 * pi * x1) * cos(2.0 * k2 * pi * x2) + 2.0 ]]) assert ((Jacobian(M) == expected))
def test_collela_mapping_2d_1(): rdim = 2 x1, x2 = symbols('x1, x2') constants = ['eps', 'k1', 'k2'] eps, k1, k2 = [Constant(i) for i in constants] M = CollelaMapping('M', rdim) assert (not (M[0] == x1)) assert (not (M[1] == x2)) expected = 2.0 * eps * sin(2.0 * k1 * pi * x1) * sin( 2.0 * k2 * pi * x2) + 2.0 * x1 - 1.0 assert (LogicalExpr(M[0], mapping=M, dim=rdim, subs=True) == expected) expected = 2.0 * eps * sin(2.0 * k1 * pi * x1) * sin( 2.0 * k2 * pi * x2) + 2.0 * x2 - 1.0 assert (LogicalExpr(M[1], mapping=M, dim=rdim, subs=True) == expected) expected = 4.0 * eps * k1 * pi * sin(2.0 * k2 * pi * x2) * cos( 2.0 * k1 * pi * x1) + 2.0 assert (LogicalExpr(dx1(M[0]), mapping=M, dim=rdim, subs=True) == expected) expected = 4.0 * eps * k1 * pi * sin(2.0 * k2 * pi * x2) * cos( 2.0 * k1 * pi * x1) assert (LogicalExpr(dx1(M[1]), mapping=M, dim=rdim, subs=True) == expected) expected = 4.0 * eps * k2 * pi * sin(2.0 * k1 * pi * x1) * cos( 2.0 * k2 * pi * x2) assert (LogicalExpr(dx2(M[0]), mapping=M, dim=rdim, subs=True) == expected) expected = 4.0 * eps * k2 * pi * sin(2.0 * k1 * pi * x1) * cos( 2.0 * k2 * pi * x2) + 2.0 assert (LogicalExpr(dx2(M[1]), mapping=M, dim=rdim, subs=True) == expected) expected = Matrix([[ 4.0 * eps * k1 * pi * sin(2.0 * k2 * pi * x2) * cos(2.0 * k1 * pi * x1) + 2.0, 4.0 * eps * k2 * pi * sin(2.0 * k1 * pi * x1) * cos(2.0 * k2 * pi * x2) ], [ 4.0 * eps * k1 * pi * sin(2.0 * k2 * pi * x2) * cos(2.0 * k1 * pi * x1), 4.0 * eps * k2 * pi * sin(2.0 * k1 * pi * x1) * cos(2.0 * k2 * pi * x2) + 2.0 ]]) assert (not (Jacobian(M) == expected)) assert (expand(LogicalExpr(Jacobian(M), mapping=M, dim=rdim, subs=True)) == expand(expected))
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_eval_field_2d_1(): # ... args = [dx1(u), dx2(u)] # TODO improve stmts = [AugAssign(ProductGenerator(MatrixQuadrature(i), index_quad), '+', Mul(coeff,AtomicNode(i))) for i in args] # ... # ... nderiv = 1 body = construct_logical_expressions(u, nderiv) # ... # ... stmts = body + stmts loop = Loop((l_quad, a_basis), index_quad, stmts) # ... # ... stmts = [loop] loop = Loop((l_basis, l_coeff), index_dof, stmts) # ... # TODO do we need nderiv here? stmt = parse(loop, settings={'dim': domain.dim, 'nderiv': nderiv}) 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_identity_mapping_2d_1(): rdim = 2 x1, x2 = symbols('x1, x2') M = IdentityMapping('M', rdim) assert (not (M[0] == x1)) assert (not (M[1] == x2)) assert (LogicalExpr(M, M[0]) == x1) assert (LogicalExpr(M, M[1]) == x2) assert (LogicalExpr(M, dx1(M[0])) == 1) assert (LogicalExpr(M, dx1(M[1])) == 0) assert (LogicalExpr(M, dx2(M[0])) == 0) assert (LogicalExpr(M, dx2(M[1])) == 1) expected = Matrix([[1, 0], [0, 1]]) assert (not (M.jacobian == expected)) assert (LogicalExpr(M, M.jacobian) == expected)
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_identity_mapping_2d_1(): dim = 2 x1, x2 = symbols('x1, x2') M = IdentityMapping('M', dim=dim) domain = M(Domain('Omega', dim=dim)) assert (not (M[0] == x1)) assert (not (M[1] == x2)) assert (LogicalExpr(M[0], domain) == x1) assert (LogicalExpr(M[1], domain) == x2) assert (LogicalExpr(dx1(M[0]), domain) == 1) assert (LogicalExpr(dx1(M[1]), domain) == 0) assert (LogicalExpr(dx2(M[0]), domain) == 0) assert (LogicalExpr(dx2(M[1]), domain) == 1) expected = Matrix([[1, 0], [0, 1]]) assert (Jacobian(M) == expected)
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_2d_1(): rdim = 2 M = Mapping('M', rdim) domain = Domain('Omega', dim=rdim) alpha = Constant('alpha') V = ScalarFunctionSpace('V', domain) u = element_of(V, name='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_czarny_mapping_2d_1(): rdim = 2 x1, x2 = symbols('x1, x2') constants = ['c2', 'eps', 'b'] c2, eps, b = [Constant(i) for i in constants] M = CzarnyMapping('M', rdim) assert (not (M[0] == x1)) assert (not (M[1] == x2)) expected = (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 1) / eps assert (LogicalExpr(M[0], mapping=M, dim=rdim, subs=True) == expected) expected = b * x1 * sin(x2) / ( sqrt(-eps**2 / 4 + 1) * (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 2)) + c2 assert (LogicalExpr(M[1], mapping=M, dim=rdim, subs=True) == expected) expected = -cos(x2) / sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) assert (LogicalExpr(dx1(M[0]), mapping=M, dim=rdim, subs=True) == expected) expected = b * ( eps * x1 * sin(x2) * cos(x2) / (sqrt(-eps**2 / 4 + 1) * sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) * (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 2)**2) + sin(x2) / (sqrt(-eps**2 / 4 + 1) * (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 2))) assert ((LogicalExpr(dx1(M[1]), mapping=M, dim=rdim, subs=True) - expected).expand() == 0) expected = x1 * sin(x2) / sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) assert (LogicalExpr(dx2(M[0]), mapping=M, dim=rdim, subs=True) == expected) expected = b * x1 * ( -eps * x1 * sin(x2)**2 / (sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) * (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 2)**2) + cos(x2) / (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 2)) / sqrt(-eps**2 / 4 + 1) assert ((LogicalExpr(dx2(M[1]), mapping=M, dim=rdim, subs=True) - expected).expand() == 0) expected = Matrix( [[ -cos(x2) / sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1), x1 * sin(x2) / sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) ], [ b * (eps * x1 * sin(x2) * cos(x2) / (sqrt(-eps**2 / 4 + 1) * sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) * (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 2)**2) + sin(x2) / (sqrt(-eps**2 / 4 + 1) * (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 2))), b * x1 * (-eps * x1 * sin(x2)**2 / (sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) * (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 2)**2) + cos(x2) / (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 2)) / sqrt(-eps**2 / 4 + 1) ]]) assert (not (Jacobian(M) == expected)) assert (expand(LogicalExpr(Jacobian(M), mapping=M, dim=rdim, subs=True)) == expand(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(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_symbolic_expr_3d_1(): rdim = 3 M = Mapping('M', rdim) domain = M(Domain('Omega', dim=rdim)) alpha = Constant('alpha') 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, 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(dx2(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_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_czarny_mapping_2d_1(): dim = 2 x1, x2 = symbols('x1, x2') constants = ['c2', 'eps', 'b'] c2, eps, b = [Constant(i) for i in constants] M = CzarnyMapping('M', dim=dim) domain = M(Domain('Omega', dim=dim)) assert (not (M[0] == x1)) assert (not (M[1] == x2)) expected = (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 1) / eps assert (LogicalExpr(M[0], domain) == expected) expected = b * x1 * sin(x2) / ( sqrt(-eps**2 / 4 + 1) * (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 2)) + c2 assert (LogicalExpr(M[1], domain) == expected) expected = -cos(x2) / sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) assert (LogicalExpr(dx1(M[0]), domain) == expected) expected = b * ( eps * x1 * sin(x2) * cos(x2) / (sqrt(-eps**2 / 4 + 1) * sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) * (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 2)**2) + sin(x2) / (sqrt(-eps**2 / 4 + 1) * (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 2))) assert ((LogicalExpr(dx1(M[1]), domain) - expected).expand() == 0) expected = x1 * sin(x2) / sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) assert (LogicalExpr(dx2(M[0]), domain) == expected) expected = b * x1 * ( -eps * x1 * sin(x2)**2 / (sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) * (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 2)**2) + cos(x2) / (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 2)) / sqrt(-eps**2 / 4 + 1) assert ((LogicalExpr(dx2(M[1]), domain) - expected).expand() == 0) expected = Matrix( [[ -cos(x2) / sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1), x1 * sin(x2) / sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) ], [ b * (eps * x1 * sin(x2) * cos(x2) / (sqrt(-eps**2 / 4 + 1) * sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) * (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 2)**2) + sin(x2) / (sqrt(-eps**2 / 4 + 1) * (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 2))), b * x1 * (-eps * x1 * sin(x2)**2 / (sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) * (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 2)**2) + cos(x2) / (-sqrt(eps * (eps + 2 * x1 * cos(x2)) + 1) + 2)) / sqrt(-eps**2 / 4 + 1) ]]) assert (all(e.expand().is_zero for e in (Jacobian(M) - expected)))