Debian Codegraph @

python/test/unit/adaptivity/test_error_control.py

raw
1 """Unit tests for error control"""
2
3 # Copyright (C) 2011 Marie E. Rognes
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 ufl_legacy.algorithms import replace
22
23 from dolfin import *
24 from dolfin.fem.adaptivesolving import *
25 from dolfin_utils.test import skip_in_parallel
26
27 # FIXME: Move this to dolfin for user access?
28 def reconstruct_refined_form(form, functions, mesh):
29 function_mapping = {}
30 for u in functions:
31 w = Function(u.leaf_node().function_space())
32 w.assign(u.leaf_node())
33 function_mapping[u] = w
34 domain = mesh.leaf_node().ufl_domain()
35 newform = replace_integral_domains(replace(form, function_mapping), domain)
36 return newform, function_mapping
37
38
39 # This must be scope function, because the tests will modify some of the objects,
40 # including the mesh which gets its hierarchial adapted submeshes attached.
41 fixt = pytest.fixture(scope="function")
42
43 @fixt
44 def mesh():
45 return UnitSquareMesh(8, 8)
46
47 @fixt
48 def V(mesh):
49 return FunctionSpace(mesh, "Lagrange", 1)
50
51 @fixt
52 def u(V):
53 return Function(V)
54
55 @fixt
56 def a(V):
57 v = TestFunction(V)
58 u = TrialFunction(V)
59 return inner(grad(u), grad(v))*dx()
60
61 @fixt
62 def L(V):
63 v = TestFunction(V)
64 f = Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)", degree=1)
65 g = Expression("sin(5*x[0])", degree=1)
66 return f*v*dx() + g*v*ds()
67
68 @fixt
69 def problem(a, L, u, V):
70 bc = [DirichletBC(V, 0.0, "x[0] < DOLFIN_EPS || x[0] > 1.0 - DOLFIN_EPS")]
71 return LinearVariationalProblem(a, L, u, bc)
72
73 @fixt
74 def goal(u):
75 return u*dx()
76
77 @fixt
78 def ec(problem, goal):
79 return generate_error_control(problem, goal)
80
81
82 @skip_in_parallel
83 def test_check_domains(goal, mesh, a, L):
84 # Asserting that domains are ok before trying error control generation
85 msg = "Expecting only the domain from the mesh to get here through u."
86 assert len(goal.ufl_domains()) == 1, msg
87 assert goal.ufl_domains()[0] == mesh.ufl_domain(), msg
88 assert len(a.ufl_domains()) == 1, msg
89 assert a.ufl_domains()[0] == mesh.ufl_domain(), msg
90 assert len(L.ufl_domains()) == 1, msg
91 assert L.ufl_domains()[0] == mesh.ufl_domain(), msg
92
93
94 @skip_in_parallel
95 def test_error_estimation(problem, u, ec):
96
97 # Solve variational problem once
98 solver = LinearVariationalSolver(problem)
99 solver.solve()
100
101 # Compute error estimate
102 error_estimate = ec.estimate_error(u, problem.bcs())
103
104 # Compare estimate with defined reference
105 reference = 0.0011789985750808342
106 assert round(error_estimate - reference, 7) == 0
107
108
109 @skip_in_parallel
110 def test_error_indicators(problem, u, mesh):
111
112 # Solve variational problem once
113 solver = LinearVariationalSolver(problem)
114 solver.solve()
115
116 # Compute error indicators
117 indicators = Vector(mesh.mpi_comm(), u.function_space().mesh().num_cells())
118 indicators[0] = 1.0
119 #ec.compute_indicators(indicators, u) #
120
121 reference = 1.0 # FIXME
122 assert round(indicators.sum() - reference, 7) == 0
123
124
125 @skip_in_parallel
126 def _test_adaptive_solve(problem, goal, u, mesh):
127
128 # Solve problem adaptively
129 solver = AdaptiveLinearVariationalSolver(problem, goal)
130 tol = 0.00087
131 solver.solve(tol)
132
133 # Note: This old approach is now broken, as it doesn't change the integration domain:
134 #M = replace(goal, {u: w})
135 # This new approach handles the integration domain properly:
136 M, fm = reconstruct_refined_form(goal, [u], mesh)
137
138 # Compare computed goal with reference
139 reference = 0.12583303389560166
140 assert round(assemble(M) - reference, 7) == 0