Debian Codegraph @

python/test/unit/fem/test_solving.py

raw
1 """Unit tests for the solve function"""
2
3 # Copyright (C) 2011 Anders Logg
4 #
5 # This file is part of DOLFIN.
6 #
7 # DOLFIN is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # DOLFIN is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
19
20 import pytest
21 from dolfin import *
22 from dolfin_utils.test import *
23
24
25 def test_bcs():
26 "Check that the bcs argument is picked up"
27
28 mesh = UnitSquareMesh(4, 4)
29 V = FunctionSpace(mesh, "Lagrange", 1)
30 u = TrialFunction(V)
31 v = TestFunction(V)
32 f = Constant(100.0)
33
34 a = dot(grad(u), grad(v))*dx + u*v*dx
35 L = f*v*dx
36
37 bc = DirichletBC(V, 0.0, DomainBoundary())
38
39 # Single bc argument
40 u1 = Function(V)
41 solve(a == L, u1, bc)
42
43 # List of bcs
44 u2 = Function(V)
45 solve(a == L, u2, [bc])
46
47 # Single bc keyword argument
48 u3 = Function(V)
49 solve(a == L, u3, bcs=bc)
50
51 # List of bcs keyword argument
52 u4 = Function(V)
53 solve(a == L, u4, bcs=[bc])
54
55 # Check all solutions
56 assert round(u1.vector().norm("l2") - 14.9362601686, 10) == 0
57 assert round(u2.vector().norm("l2") - 14.9362601686, 10) == 0
58 assert round(u3.vector().norm("l2") - 14.9362601686, 10) == 0
59 assert round(u4.vector().norm("l2") - 14.9362601686, 10) == 0
60
61
62 def test_bcs_space():
63 "Check that the bc space is checked to be a subspace of trial space"
64 mesh = UnitSquareMesh(4, 4)
65 V = FunctionSpace(mesh, "Lagrange", 1)
66 u = TrialFunction(V)
67 v = TestFunction(V)
68 f = Constant(100.0)
69
70 a = dot(grad(u), grad(v))*dx + u*v*dx
71 L = f*v*dx
72
73 Q = FunctionSpace(mesh, "Lagrange", 2)
74 bc = DirichletBC(Q, 0.0, DomainBoundary())
75
76 u = Function(V)
77
78 with pytest.raises(RuntimeError):
79 solve(a == L, u, bc)
80
81 with pytest.raises(RuntimeError):
82 solve(action(a, u) - L == 0, u, bc)
83
84
85 def test_calling():
86 "Test that unappropriate arguments are not allowed"
87 mesh = UnitSquareMesh(4, 4)
88 V = FunctionSpace(mesh, "Lagrange", 1)
89 u = TrialFunction(V)
90 v = TestFunction(V)
91 f = Constant(100.0)
92
93 a = dot(grad(u), grad(v))*dx + u*v*dx
94 L = f*v*dx
95
96 bc = DirichletBC(V, 0.0, DomainBoundary())
97
98 kwargs = {"solver_parameters":{"linear_solver": "lu"},
99 "form_compiler_parameters":{"optimize": True}}
100
101 A = assemble(a)
102 b = assemble(L)
103 x = Vector()
104
105 with pytest.raises(RuntimeError):
106 solve(A, x, b, **kwargs)
107
108 # FIXME: Include more tests for this versatile function
109
110
111 def test_nonlinear_variational_solver_custom_comm():
112 "Check that nonlinear variational solver works on subset of comm_world"
113 if MPI.rank(MPI.comm_world) == 0:
114 mesh = UnitIntervalMesh(MPI.comm_self, 2)
115 V = FunctionSpace(mesh, "CG", 1)
116 f = Constant(1)
117 u = Function(V)
118 v = TestFunction(V)
119 F = inner(u, v)*dx - inner(f, v)*dx
120
121 # Check that following does not deadlock
122 solve(F == 0, u)
123 solve(F == 0, u, solver_parameters={"nonlinear_solver": "newton"})
124 if has_petsc():
125 solve(F == 0, u, solver_parameters={"nonlinear_solver": "snes"})