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_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(): 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(): 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_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_mapping_1d(): print('============ test_mapping_1d ==============') rdim = 1 F = Mapping('F', rdim) 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_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_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_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_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(Jacobian(F) == expected) # ... # ... expected = dx1(F[0])*dx2(F[1]) - dx1(F[1])*dx2(F[0]) assert(Jacobian(F).det() == 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_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_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_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_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[0], mapping=M, dim=rdim, subs=True) == x1) assert (LogicalExpr(M[1], mapping=M, dim=rdim, subs=True) == x2) assert (LogicalExpr(dx1(M[0]), mapping=M, dim=rdim, subs=True) == 1) assert (LogicalExpr(dx1(M[1]), mapping=M, dim=rdim, subs=True) == 0) assert (LogicalExpr(dx2(M[0]), mapping=M, dim=rdim, subs=True) == 0) assert (LogicalExpr(dx2(M[1]), mapping=M, dim=rdim, subs=True) == 1) expected = Matrix([[1, 0], [0, 1]]) assert (not (M.jacobian == expected)) assert (LogicalExpr(Jacobian(M), mapping=M, dim=rdim, subs=True) == expected)
def eval(cls, *_args, **kwargs): """.""" if not _args: return if not len(_args) == 1: raise ValueError('Expecting one argument') expr = _args[0] n_rows = kwargs.pop('n_rows', None) n_cols = kwargs.pop('n_cols', None) dim = kwargs.pop('dim', None) logical = kwargs.pop('logical', None) if isinstance(expr, Add): args = [cls.eval(a, dim=dim, logical=logical) for a in expr.args] o = args[0] for arg in args[1:]: o = o + arg return o elif isinstance(expr, Mul): args = [cls.eval(a, dim=dim, logical=logical) for a in expr.args] o = args[0] for arg in args[1:]: o = o * arg return o elif isinstance(expr, Abs): return Abs(cls.eval(expr.args[0], dim=dim, logical=logical)) elif isinstance(expr, Pow): base = cls.eval(expr.base, dim=dim, logical=logical) exp = cls.eval(expr.exp, dim=dim, logical=logical) return base**exp elif isinstance(expr, JacobianSymbol): axis = expr.axis J = Jacobian(expr.mapping) if axis is None: return J else: return J.col_del(axis) elif isinstance(expr, SymbolicDeterminant): return cls.eval(expr.arg, dim=dim, logical=logical).det().factor() elif isinstance(expr, SymbolicTrace): return cls.eval(expr.arg, dim=dim, logical=logical).trace() elif isinstance(expr, Transpose): return cls.eval(expr.arg, dim=dim, logical=logical).T elif isinstance(expr, Inverse): return cls.eval(expr.arg, dim=dim, logical=logical).inv() elif isinstance(expr, (ScalarTestFunction, VectorTestFunction)): return expr elif isinstance(expr, PullBack): return cls.eval(expr.expr, dim=dim, logical=True) elif isinstance(expr, MatrixElement): base = cls.eval(expr.base, dim=dim, logical=logical) return base[expr.indices] elif isinstance(expr, BasicForm): # ... dim = expr.ldim domain = expr.domain if not isinstance(domain, Union): logical = domain.mapping is None domain = (domain, ) # ... d_expr = OrderedDict() for d in domain: d_expr[d] = S.Zero # ... if isinstance(expr.expr, Add): for a in expr.expr.args: newexpr = cls.eval(a, dim=dim, logical=True) # ... try: domain = _get_domain(a) if not isinstance(domain, Union): domain = (domain, ) except: pass # ... for d in domain: d_expr[d] += newexpr # ... else: newexpr = cls.eval(expr.expr, dim=dim, logical=logical) # ... if isinstance(expr, Functional): domain = expr.domain else: domain = _get_domain(expr.expr) if isinstance(domain, Union): domain = list(domain._args) elif not is_sequence(domain): domain = [domain] # ... # ... for d in domain: d_expr[d] = newexpr # ... trials, tests = _get_trials_tests_flattened(expr) d_new = OrderedDict() for domain, newexpr in d_expr.items(): if newexpr != 0: # TODO ARA make sure thre is no problem with psydac # we should always take the interior of a domain if not isinstance(domain, (Boundary, Interface, InteriorDomain)): domain = domain.interior d_new[domain] = _to_matrix_form(newexpr, trials=trials, tests=tests) # ... ls = [] d_all = OrderedDict() # ... treating interfaces keys = [k for k in d_new.keys() if isinstance(k, Interface)] for interface in keys: # ... trials = None tests = None if expr.is_bilinear: trials = list(expr.variables[0]) tests = list(expr.variables[1]) elif expr.is_linear: tests = list(expr.variables) # ... # ... newexpr = d_new[interface] ls_int, d_bnd = _split_expr_over_interface(newexpr, interface, tests=tests, trials=trials) # ... ls += ls_int # ... for k, v in d_bnd.items(): if k in d_all.keys(): d_all[k] += v else: d_all[k] = v # ... # ... treating subdomains keys = [k for k in d_new.keys() if isinstance(k, Union)] for domain in keys: newexpr = d_new[domain] d = OrderedDict( (interior, newexpr) for interior in domain.as_tuple()) # ... for k, v in d.items(): if k in d_all.keys(): d_all[k] += v else: d_all[k] = v d = OrderedDict() for k, v in d_new.items(): if not isinstance(k, (Interface, Union)): d[k] = d_new[k] for k, v in d_all.items(): if k in d.keys(): d[k] += v else: d[k] = v d_new = d # ... for domain, newexpr in d_new.items(): if isinstance(domain, Boundary): ls += [BoundaryExpression(domain, newexpr)] elif isinstance(domain, BasicDomain): ls += [DomainExpression(domain, newexpr)] else: raise TypeError('not implemented for {}'.format( type(domain))) # ... return tuple(ls) elif isinstance(expr, Integral): dim = expr.domain.dim if dim is None else dim logical = expr.domain.mapping is None return cls.eval(expr._args[0], dim=dim, logical=logical) elif isinstance(expr, NormalVector): lines = [[expr[i] for i in range(dim)]] return ImmutableDenseMatrix(lines) elif isinstance(expr, TangentVector): lines = [[expr[i] for i in range(dim)]] return ImmutableDenseMatrix(lines) elif isinstance(expr, BasicExpr): return cls.eval(expr.expr, dim=dim, logical=logical) elif isinstance(expr, _diff_ops): op = type(expr) if logical: new = eval('Logical{0}_{1}d'.format(op, dim)) else: new = eval('{0}_{1}d'.format(op, dim)) args = [cls.eval(i, dim=dim, logical=logical) for i in expr.args] return new(*args) elif isinstance(expr, _generic_ops): # if i = Dot(...) then type(i) is Grad op = type(expr) new = eval('{0}_{1}d'.format(op, dim)) args = [cls.eval(i, dim=dim, logical=logical) for i in expr.args] return new(*args) elif isinstance(expr, Trace): # TODO treate different spaces if expr.order == 0: return cls.eval(expr.expr, dim=dim, logical=logical) elif expr.order == 1: # TODO give a name to normal vector normal_vector_name = 'n' n = NormalVector(normal_vector_name) M = cls.eval(expr.expr, dim=dim, logical=logical) if dim == 1: return M else: if isinstance(M, (Add, Mul)): ls = M.atoms(Tuple) for i in ls: M = M.subs(i, Matrix(i)) M = simplify(M) e = 0 for i in range(0, dim): e += M[i] * n[i] return e else: raise ValueError( '> Only traces of order 0 and 1 are available') elif isinstance(expr, (Matrix, ImmutableDenseMatrix)): n, m = expr.shape lines = [] for i in range(0, n): line = [] for j in range(0, m): line.append(cls.eval(expr[i, j], dim=dim, logical=logical)) lines.append(line) return ImmutableDenseMatrix(lines) elif isinstance(expr, LogicalExpr): M = expr.mapping dim = expr.dim expr = cls(expr.expr, dim=dim) dim = M.rdim return LogicalExpr(expr, mapping=M, dim=dim) return expr
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_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(): 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_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)))