Debian Codegraph @

python/test/unit/parallel-assembly-solve/test_solve_result_against_reference.py

raw
1 """This file solves a simple reaction-diffusion problem and compares
2 the norm of the solution vector with a known solution (obtained when
3 running in serial). It is used for validating mesh partitioning and
4 parallel assembly/solve."""
5
6 # Copyright (C) 2009-2014 Anders Logg
7 #
8 # This file is part of DOLFIN.
9 #
10 # DOLFIN is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU Lesser General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # DOLFIN is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU Lesser General Public License for more details.
19 #
20 # You should have received a copy of the GNU Lesser General Public License
21 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
22 #
23 # Modified by Garth N. Wells, 2013
24
25 import pytest
26 import sys
27 from dolfin import *
28 from dolfin_utils.test import *
29
30 # Relative tolerance for regression test
31 tol = 1e-10
32
33 def compute_norm(mesh, degree):
34 "Solve on given mesh file and degree of function space."
35
36 # Create function space
37 V = FunctionSpace(mesh, "Lagrange", degree)
38
39 # Define variational problem
40 v = TestFunction(V)
41 u = TrialFunction(V)
42 f = Expression("sin(x[0])", degree=degree)
43 g = Expression("x[0]*x[1]", degree=degree)
44 a = dot(grad(v), grad(u))*dx + v*u*dx
45 L = v*f*dx - v*g*ds
46
47 # Compute solution
48 w = Function(V)
49 solve(a == L, w)
50
51 # Return norm of solution vector
52 return w.vector().norm("l2")
53
54 def print_reference(results):
55 "Print nicely formatted values for gluing into code as a reference"
56 MPI.barrier(MPI.comm_world)
57 if MPI.rank(MPI.comm_world) == 0:
58 print("reference = {", end=' ')
59 for (i, result) in enumerate(results):
60 if i > 0:
61 print(" ", end=' ')
62 print("(\"%s\", %d): %.16g" % result, end=' ')
63 if i < len(results) - 1:
64 print(",")
65 else:
66 print("}")
67 MPI.barrier(MPI.comm_world)
68
69 def check_results(results, reference, tol):
70 "Compare results with reference"
71 errors = []
72 for (mesh_file, degree, norm) in results:
73 if (mesh_file, degree) not in reference:
74 errors.append((mesh_file, degree, None, None, None))
75 else:
76 ref = reference[(mesh_file, degree)]
77 diff = abs(norm - ref) / abs(ref)
78 if diff >= tol:
79 errors.append((mesh_file, degree, norm, ref, diff))
80 return errors
81
82 def print_errors(errors):
83 MPI.barrier(MPI.comm_world)
84 if MPI.rank(MPI.comm_world) == 0:
85 print("Checking results")
86 print("----------------")
87 for (mesh_file, degree, norm, ref, diff) in errors:
88 print("(%s, %d):\t" % (mesh_file, degree), end=' ')
89 if diff is None:
90 print("missing reference")
91 else:
92 print("*** ERROR", end=' ')
93 print("(norm = %.16g, reference = %.16g, relative diff = %.16g)" % (norm, ref, diff))
94 MPI.barrier(MPI.comm_world)
95
96 def test_computed_norms_against_references():
97 # Reference values for norm of solution vector
98 reference = { ("16x16 unit tri square", 1): 9.547454087328376 ,
99 ("16x16 unit tri square", 2): 18.42366670418269 ,
100 ("16x16 unit tri square", 3): 27.29583104732836 ,
101 ("16x16 unit tri square", 4): 36.16867128121694 ,
102 ("4x4x4 unit tet cube", 1): 12.23389289626038 ,
103 ("4x4x4 unit tet cube", 2): 28.96491629163837 ,
104 ("4x4x4 unit tet cube", 3): 49.97350551329799 ,
105 ("4x4x4 unit tet cube", 4): 74.49938266409099 ,
106 ("16x16 unit quad square", 1): 9.550848071820747 ,
107 ("16x16 unit quad square", 2): 18.423668706176354 ,
108 ("16x16 unit quad square", 3): 27.295831017251672 ,
109 ("16x16 unit quad square", 4): 36.168671281610855 ,
110 ("4x4x4 unit hex cube", 1): 12.151954087339782 ,
111 ("4x4x4 unit hex cube", 2): 28.965646690046885 ,
112 ("4x4x4 unit hex cube", 3): 49.97349423895635 ,
113 ("4x4x4 unit hex cube", 4): 74.49938136593539 }
114
115 # Mesh files and degrees to check
116 meshes = [(UnitSquareMesh(16, 16), "16x16 unit tri square"),
117 (UnitCubeMesh(4, 4, 4), "4x4x4 unit tet cube"),
118 (UnitSquareMesh.create(16, 16, CellType.Type.quadrilateral), "16x16 unit quad square"),
119 (UnitCubeMesh.create(4, 4, 4, CellType.Type.hexahedron), "4x4x4 unit hex cube")]
120 degrees = [1, 2, 3, 4]
121
122 # For MUMPS, increase estimated require memory increase. Typically
123 # required for high order elements on small meshes in 3D
124 if has_petsc():
125 PETScOptions.set("mat_mumps_icntl_14", 40)
126
127 # Iterate over test cases and collect results
128 results = []
129 for mesh in meshes:
130 for degree in degrees:
131 gc_barrier()
132 norm = compute_norm(mesh[0], degree)
133 results.append((mesh[1], degree, norm))
134
135 # Change option back to default
136 if has_petsc():
137 PETScOptions.set("mat_mumps_icntl_14", 20)
138
139 # Check results
140 errors = check_results(results, reference, tol)
141
142 # Print errors for debugging if they fail
143 if errors:
144 print_errors(errors)
145
146 # Print results for use as reference
147 if any(e[-1] is None for e in errors): # e[-1] is diff
148 print_reference(results)
149
150 # A passing test should have no errors
151 assert len(errors) == 0 # See stdout for detailed norms and diffs.