def _visit_PhysicalValueNode(self, expr, **kwargs): mapping = self.mapping expr = LogicalExpr(mapping, expr.expr) expr = SymbolicExpr(expr) inv_jac = SymbolicInverseDeterminant(mapping) jac = SymbolicExpr(mapping.det_jacobian) expr = expr.subs(1 / jac, inv_jac) return expr
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_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_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_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_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_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_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(): 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_logical_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, 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, 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 = 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)
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_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_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_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_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_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_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_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_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_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_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_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)
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 eval(cls, *_args, **kwargs): """.""" if not _args: return if not len(_args) == 1: raise ValueError('Expecting one argument') expr = _args[0] d_atoms = kwargs.pop('d_atoms', {}) mapping = kwargs.pop('mapping', None) if isinstance(expr, Add): args = [ cls.eval(a, d_atoms=d_atoms, mapping=mapping) for a in expr.args ] return Add(*args) elif isinstance(expr, Mul): coeffs = [a for a in expr.args if isinstance(a, _coeffs_registery)] stests = [ a for a in expr.args if not (a in coeffs) and a.atoms(ScalarTestFunction) ] vtests = [ a for a in expr.args if not (a in coeffs) and a.atoms(VectorTestFunction) ] vectors = [ a for a in expr.args if (not (a in coeffs) and not (a in stests) and not (a in vtests)) ] a = S.One if coeffs: a = Mul(*coeffs) b = S.One if vectors: args = [cls(i, evaluate=False) for i in vectors] b = Mul(*args) sb = S.One if stests: args = [ cls.eval(i, d_atoms=d_atoms, mapping=mapping) for i in stests ] sb = Mul(*args) vb = S.One if vtests: args = [ cls.eval(i, d_atoms=d_atoms, mapping=mapping) for i in vtests ] vb = Mul(*args) return Mul(a, b, sb, vb) elif isinstance(expr, _coeffs_registery): return expr elif isinstance(expr, Pow): b = expr.base e = expr.exp return Pow(cls.eval(b), e) 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], d_atoms=d_atoms, mapping=mapping)) lines.append(line) return Matrix(lines) elif isinstance(expr, DomainExpression): # TODO to be removed target = expr.target expr = expr.expr return cls.eval(expr, d_atoms=d_atoms, mapping=mapping) elif isinstance(expr, BilinearForm): trials = list(expr.variables[0]) tests = list(expr.variables[1]) # ... # TODO improve terminal_expr = TerminalExpr(expr)[0] # ... # ... variables = list(terminal_expr.atoms(ScalarTestFunction)) variables += list(terminal_expr.atoms(IndexedTestTrial)) d_atoms = {} for a in variables: new = _split_test_function(a) d_atoms[a] = new # ... # ... logical = False if not (mapping is None): logical = True terminal_expr = LogicalExpr(mapping, terminal_expr.expr) det_M = DetJacobian(mapping) det = SymbolicDeterminant(mapping) terminal_expr = terminal_expr.subs(det_M, det) terminal_expr = expand(terminal_expr) # ... # ... expr = cls.eval(terminal_expr, d_atoms=d_atoms, mapping=mapping) # ... # ... trials = [ a for a in variables if ((isinstance(a, ScalarTestFunction) and a in trials) or ( isinstance(a, IndexedTestTrial) and a.base in trials)) ] tests = [ a for a in variables if ((isinstance(a, ScalarTestFunction) and a in tests) or ( isinstance(a, IndexedTestTrial) and a.base in tests)) ] # ... expr = _replace_atomic_expr(expr, trials, tests, d_atoms, logical=logical) return expr if expr.atoms(ScalarTestFunction) or expr.atoms(IndexedTestTrial): return _tensorize_atomic_expr(expr, d_atoms) return cls(expr, evaluate=False)
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)))