def test_derivatives_2d_with_mapping(): rdim = 2 O = Domain('Omega', dim=rdim) V = ScalarFunctionSpace('V', O) u = element_of(V, 'u') M = Mapping('M', rdim) det_jac = DetJacobian(M) 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(M, dx(u)).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_logical_expr_2d_1(): rdim = 2 M = Mapping('M', rdim) domain = Domain('Omega', dim=rdim) alpha = Constant('alpha') V = ScalarFunctionSpace('V', domain) W = VectorFunctionSpace('V', domain) u, v = [element_of(V, name=i) for i in ['u', 'v']] w = element_of(W, name='w') 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) #print(expr.subs(det_M, det)) #print('') # ... # ... expr = dx(w[0]) expr = LogicalExpr(M, expr)
def test_symbolic_expr_1d_1(): rdim = 1 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(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 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)