def test_equation_2d_5(): domain = Square() x, y = domain.coordinates f0 = Matrix([ 2 * pi**2 * sin(pi * x) * sin(pi * y), 2 * pi**2 * sin(pi * x) * sin(pi * y) ]) f1 = cos(pi * x) * cos(pi * y) W = VectorFunctionSpace('W', domain) V = ScalarFunctionSpace('V', domain) X = ProductSpace(W, V) F = element_of(W, name='F') G = element_of(V, name='G') u, v = [element_of(W, name=i) for i in ['u', 'v']] p, q = [element_of(V, name=i) for i in ['p', 'q']] int_0 = lambda expr: integral(domain, expr) a0 = BilinearForm((v, u), int_0(inner(grad(v), grad(u)))) print(' a0 done.') a1 = BilinearForm((q, p), int_0(p * q)) print(' a1 done.') a = BilinearForm(((v, q), (u, p)), a0(v, u) + a1(q, p)) print(' a done.') l0 = LinearForm(v, int_0(dot(f0, v))) l1 = LinearForm(q, int_0(f1 * q)) l = LinearForm((v, q), l0(v) + l1(q)) print('****************************') bc = EssentialBC(u, 0, domain.boundary) equation = Equation(a, l, tests=[v, q], trials=[u, p], bc=bc) # ... print('=======') print(equation.lhs.expr) print('') # ... # ... print('=======') print(equation.rhs.expr) print('')
def test_evaluation_2d_2(): domain = Square() x, y = domain.coordinates f0 = Tuple(2 * pi**2 * sin(pi * x) * sin(pi * y), 2 * pi**2 * sin(pi * x) * sin(pi * y)) f1 = cos(pi * x) * cos(pi * y) W = VectorFunctionSpace('W', domain) V = FunctionSpace('V', domain) X = ProductSpace(W, V) # TODO improve: naming are not given the same way F = VectorField(W, name='F') G = Field('G', V) u, v = [VectorTestFunction(W, name=i) for i in ['u', 'v']] p, q = [TestFunction(V, name=i) for i in ['p', 'q']] a0 = BilinearForm((v, u), inner(grad(v), grad(u))) a1 = BilinearForm((q, p), p * q) a = BilinearForm(((v, q), (u, p)), a0(v, u) + a1(q, p)) l0 = LinearForm(v, dot(f0, v)) l1 = LinearForm(q, f1 * q) l = LinearForm((v, q), l0(v) + l1(q)) # ... print(a) print(evaluate(a)) print('') # ... # ... print(l) print(evaluate(l)) print('')
def test_calculus_3d_3(): domain = Domain('Omega', dim=3) H1 = ScalarFunctionSpace('V0', domain, kind='H1') Hcurl = VectorFunctionSpace('V1', domain, kind='Hcurl') Hdiv = VectorFunctionSpace('V2', domain, kind='Hdiv') L2 = ScalarFunctionSpace('V3', domain, kind='L2') V = ScalarFunctionSpace('V', domain, kind=None) W = VectorFunctionSpace('W', domain, kind=None) X = ProductSpace(H1, Hcurl, Hdiv, L2) v0, v1, v2, v3 = element_of(X, ['v0', 'v1', 'v2', 'v3']) v = element_of(V, 'v') w = element_of(W, 'w') # ... consistency of grad operator # we can apply grad on an undefined space type or H1 expr = grad(v0) expr = grad(v) expr = grad(w) # wa cannot apply grad to a function in Hcurl with pytest.raises(ArgumentTypeError): expr = grad(v1) # wa cannot apply grad to a function in Hdiv with pytest.raises(ArgumentTypeError): expr = grad(v2) # wa cannot apply grad to a function in L2 with pytest.raises(ArgumentTypeError): expr = grad(v3) # ... # ... consistency of curl operator # we can apply curl on an undefined space type, H1 or Hcurl expr = curl(v0) expr = curl(v1) expr = curl(v) expr = curl(w) # wa cannot apply curl to a function in Hdiv with pytest.raises(ArgumentTypeError): expr = curl(v2) # wa cannot apply curl to a function in L2 with pytest.raises(ArgumentTypeError): expr = curl(v3) # ... # ... consistency of div operator # we can apply div on an undefined space type, H1 or Hdiv expr = div(v0) expr = div(v2) expr = div(v) expr = div(w) # wa cannot apply div to a function in Hcurl with pytest.raises(ArgumentTypeError): expr = div(v1) # wa cannot apply div to a function in L2 with pytest.raises(ArgumentTypeError): expr = div(v3)
def test_linearity_2d_2(): domain = Domain('Omega', dim=DIM) V1 = FunctionSpace('V1', domain) V2 = FunctionSpace('V2', domain) U1 = FunctionSpace('U1', domain) U2 = FunctionSpace('U2', domain) W1 = VectorFunctionSpace('W1', domain) W2 = VectorFunctionSpace('W2', domain) T1 = VectorFunctionSpace('T1', domain) T2 = VectorFunctionSpace('T2', domain) v1 = TestFunction(V1, name='v1') v2 = TestFunction(V2, name='v2') u1 = TestFunction(U1, name='u1') u2 = TestFunction(U2, name='u2') w1 = VectorTestFunction(W1, name='w1') w2 = VectorTestFunction(W2, name='w2') t1 = VectorTestFunction(T1, name='t1') t2 = VectorTestFunction(T2, name='t2') V = ProductSpace(V1, V2) U = ProductSpace(U1, U2) x, y = V1.coordinates alpha = Constant('alpha') F = Field('F', space=V1) # ... l1 = LinearForm(v1, x * y * v1, check=True) l = LinearForm(v2, l1(v2), check=True) # ... # ... l1 = LinearForm(v1, x * y * v1, check=True) l2 = LinearForm(v2, cos(x + y) * v2, check=True) l = LinearForm((u1, u2), l1(u1) + l2(u2), check=True) # ... # ... l1 = LinearForm(v1, x * y * v1, check=True) l2 = LinearForm(v2, cos(x + y) * v2, check=True) l = LinearForm((u1, u2), l1(u1) + alpha * l2(u2), check=True) # ... # ... l1 = LinearForm(v1, x * y * v1, check=True) l2 = LinearForm(w1, div(w1), check=True) l = LinearForm((v2, w2), l1(v2) + l2(w2), check=True) # ... ################################ # non bilinear forms ################################ # ... with pytest.raises(UnconsistentLinearExpressionError): l = LinearForm(v1, x * y * v1**2, check=True) # ... # ... with pytest.raises(UnconsistentLinearExpressionError): l = LinearForm(v1, x * y, check=True)
def test_calls_2d(): domain = Domain('Omega', dim=DIM) V1 = FunctionSpace('V1', domain) V2 = FunctionSpace('V2', domain) U1 = FunctionSpace('U1', domain) U2 = FunctionSpace('U2', domain) W1 = VectorFunctionSpace('W1', domain) W2 = VectorFunctionSpace('W2', domain) T1 = VectorFunctionSpace('T1', domain) T2 = VectorFunctionSpace('T2', domain) v1 = TestFunction(V1, name='v1') v2 = TestFunction(V2, name='v2') u1 = TestFunction(U1, name='u1') u2 = TestFunction(U2, name='u2') w1 = VectorTestFunction(W1, name='w1') w2 = VectorTestFunction(W2, name='w2') t1 = VectorTestFunction(T1, name='t1') t2 = VectorTestFunction(T2, name='t2') V = ProductSpace(V1, V2) U = ProductSpace(U1, U2) x, y = V1.coordinates alpha = Constant('alpha') F = Field('F', space=V1) # ... a1 = BilinearForm((v1, u1), u1 * v1, name='a1') print(a1) print(atomize(a1)) print(evaluate(a1)) print('') expr = a1(v2, u2) a = BilinearForm((v2, u2), expr, name='a') print(a) print(atomize(a)) print(evaluate(a)) print('') # ... # ... a = BilinearForm((v1, u1), dot(grad(v1), grad(u1)), name='a') print(a) print(atomize(a)) print(evaluate(a)) print('') # ... # ... a1 = BilinearForm((v1, u1), dot(grad(v1), grad(u1)), name='a1') expr = a1(v2, u2) a = BilinearForm((v2, u2), expr, name='a') print(a) print(atomize(a)) print(evaluate(a)) print('') # ... # ... a1 = BilinearForm((v1, u1), u1 * v1, name='a1') a2 = BilinearForm((v1, u1), dx(u1) * dx(v1), name='a2') expr = a1(v2, u2) + a2(v2, u2) a = BilinearForm((v2, u2), expr, name='a') print(a) print(atomize(a)) print(evaluate(a)) print('') # ... # ... a1 = BilinearForm((v1, u1), u1 * v1, name='a1') a2 = BilinearForm((v1, u1), dx(u1) * dx(v1), name='a2') expr = a1(v1, u2) print('> before = ', expr) expr = expr.subs(u2, u1) print('> after = ', expr) print('') expr = a1(v1, u2) + a1(v2, u2) print('> before = ', expr) expr = expr.subs(u2, u1) print('> after = ', expr) print('') # ... # ... a1 = BilinearForm((v1, u1), u1 * v1, name='a1') a2 = BilinearForm((v1, u1), dx(u1) * dx(v1), name='a2') expr = a1(v1, u2) + a2(v2, u1) a = BilinearForm(((v1, v2), (u1, u2)), expr, name='a') print(a) print(atomize(a)) print(evaluate(a)) print('') # ... # ... a = BilinearForm((w1, t1), rot(w1) * rot(t1) + div(w1) * div(t1), name='a') print(a) print(atomize(a)) print(evaluate(a)) # ... # ... a1 = BilinearForm((v1, u1), u1 * v1, name='a1') a2 = BilinearForm((v1, u1), dx(u1) * dx(v1), name='a2') a3 = BilinearForm((w1, t1), rot(w1) * rot(t1) + div(w1) * div(t1), name='a3') a4 = BilinearForm((w1, u1), div(w1) * u1, name='a4') expr = a3(w2, t2) + a2(v2, u2) + a4(w2, u2) a = BilinearForm(((w2, v2), (t2, u2)), expr, name='a') print(a) print(atomize(a)) print(evaluate(a)) # ... # ... a1 = BilinearForm((v1, u1), laplace(u1) * laplace(v1), name='a1') print(a1) print(atomize(a1)) print(evaluate(a1)) print('') # ... # ... a1 = BilinearForm((v1, u1), inner(hessian(u1), hessian(v1)), name='a1') print('================================') print(a1) print(atomize(a1)) print(evaluate(a1)) print('') # ... # ... l1 = LinearForm(v1, x * y * v1, name='11') expr = l1(v2) l = LinearForm(v2, expr, name='1') print(l) print(atomize(l)) print(evaluate(l)) # ... # ... l1 = LinearForm(v1, x * y * v1, name='l1') l2 = LinearForm(v2, cos(x + y) * v2, name='l2') expr = l1(u1) + l2(u2) l = LinearForm((u1, u2), expr, name='1') print(l) print(atomize(l)) print(evaluate(l)) # ... # ... l1 = LinearForm(v1, x * y * v1, name='l1') l2 = LinearForm(v2, cos(x + y) * v2, name='l2') expr = l1(u1) + alpha * l2(u2) l = LinearForm((u1, u2), expr, name='1') print(l) print(atomize(l)) print(evaluate(l)) # ... # ... l1 = LinearForm(v1, x * y * v1, name='l1') l2 = LinearForm(w1, div(w1), name='l2') expr = l1(v2) + l2(w2) l = LinearForm((v2, w2), expr, name='1') print(l) print(atomize(l)) print(evaluate(l)) # ... # ... I1 = Integral(x * y, domain, name='I1') print(I1) print(atomize(I1)) print(evaluate(I1)) # ... # ... expr = F - cos(2 * pi * x) * cos(3 * pi * y) expr = dot(grad(expr), grad(expr)) I2 = Integral(expr, domain, name='I2') print(I2) print(atomize(I2)) print(evaluate(I2)) # ... # ... expr = F - cos(2 * pi * x) * cos(3 * pi * y) expr = dot(grad(expr), grad(expr)) I2 = Integral(expr, domain, name='I2') print(I2) print(atomize(I2)) print(evaluate(I2)) # ... # ... stokes V = VectorFunctionSpace('V', domain) W = FunctionSpace('W', domain) v = VectorTestFunction(V, name='v') u = VectorTestFunction(V, name='u') p = TestFunction(W, name='p') q = TestFunction(W, name='q') a = BilinearForm((v, u), inner(grad(v), grad(u)), name='a') b = BilinearForm((v, p), div(v) * p, name='b') A = BilinearForm(((v, q), (u, p)), a(v, u) - b(v, p) + b(u, q), name='A') print(A) print(atomize(A)) print(evaluate(A))
def test_bilinearity_2d_2(): domain = Domain('Omega', dim=DIM) V1 = FunctionSpace('V1', domain) V2 = FunctionSpace('V2', domain) U1 = FunctionSpace('U1', domain) U2 = FunctionSpace('U2', domain) W1 = VectorFunctionSpace('W1', domain) W2 = VectorFunctionSpace('W2', domain) T1 = VectorFunctionSpace('T1', domain) T2 = VectorFunctionSpace('T2', domain) v1 = TestFunction(V1, name='v1') v2 = TestFunction(V2, name='v2') u1 = TestFunction(U1, name='u1') u2 = TestFunction(U2, name='u2') w1 = VectorTestFunction(W1, name='w1') w2 = VectorTestFunction(W2, name='w2') t1 = VectorTestFunction(T1, name='t1') t2 = VectorTestFunction(T2, name='t2') V = ProductSpace(V1, V2) U = ProductSpace(U1, U2) x, y = V1.coordinates alpha = Constant('alpha') F = Field('F', space=V1) # ... a1 = BilinearForm((v1, u1), u1 * v1, check=True) a = BilinearForm((v2, u2), a1(v2, u2), check=True) # ... # ... a = BilinearForm((v1, u1), dot(grad(v1), grad(u1)), check=True) # ... # ... a1 = BilinearForm((v1, u1), dot(grad(v1), grad(u1)), check=True) a = BilinearForm((v2, u2), a1(v2, u2), check=True) # ... # ... a1 = BilinearForm((v1, u1), u1 * v1) a2 = BilinearForm((v1, u1), dx(u1) * dx(v1), check=True) a = BilinearForm((v2, u2), a1(v2, u2) + a2(v2, u2), check=True) # ... # ... a1 = BilinearForm((v1, u1), u1 * v1, check=True) a2 = BilinearForm((v1, u1), dx(u1) * dx(v1), check=True) # ... # ... a1 = BilinearForm((v1, u1), u1 * v1, check=True) a2 = BilinearForm((v1, u1), dx(u1) * dx(v1), check=True) a = BilinearForm(((v1, v2), (u1, u2)), a1(v1, u2) + a2(v2, u1), check=True) # ... # ... a = BilinearForm((w1, t1), rot(w1) * rot(t1) + div(w1) * div(t1), check=True) # ... # ... a1 = BilinearForm((v1, u1), u1 * v1, check=True) a2 = BilinearForm((v1, u1), dx(u1) * dx(v1), check=True) a3 = BilinearForm((w1, t1), rot(w1) * rot(t1) + div(w1) * div(t1), check=True) a4 = BilinearForm((w1, u1), div(w1) * u1, check=True) a = BilinearForm(((w2, v2), (t2, u2)), a3(w2, t2) + a2(v2, u2) + a4(w2, u2), check=True) # ... # ... a1 = BilinearForm((v1, u1), laplace(u1) * laplace(v1), check=True) # ... # ... a1 = BilinearForm((v1, u1), inner(hessian(u1), hessian(v1)), check=True) # ... # ... stokes V = VectorFunctionSpace('V', domain) W = FunctionSpace('W', domain) v = VectorTestFunction(V, name='v') u = VectorTestFunction(V, name='u') p = TestFunction(W, name='p') q = TestFunction(W, name='q') a = BilinearForm((v, u), inner(grad(v), grad(u)), check=True) b = BilinearForm((v, p), div(v) * p, check=True) A = BilinearForm(((v, q), (u, p)), a(v, u) - b(v, p) + b(u, q), check=True) # ... ################################ # non bilinear forms ################################ # ... with pytest.raises(UnconsistentLinearExpressionError): a = BilinearForm((v1, u1), dot(grad(v1), grad(u1)) + v1, check=True) # ... # ... with pytest.raises(UnconsistentLinearExpressionError): a = BilinearForm((v1, u1), v1**2 * u1, check=True) # ... # ... with pytest.raises(UnconsistentLinearExpressionError): a = BilinearForm((v1, u1), dot(grad(v1), grad(v1)), check=True)