Debian Codegraph @

python/test/unit/fem/test_local_assembler.py

raw
1 """Unit tests for local assembly"""
2
3 # Copyright (C) 2015 Tormod Landet
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 numpy
21 from dolfin import *
22 from dolfin_utils.test import set_parameters_fixture
23
24
25 ghost_mode = set_parameters_fixture("ghost_mode", ["shared_facet"])
26
27
28 def test_local_assembler_1D():
29 mesh = UnitIntervalMesh(20)
30 V = FunctionSpace(mesh, 'CG', 1)
31 u = TrialFunction(V)
32 v = TestFunction(V)
33 c = Cell(mesh, 0)
34
35 a_scalar = Constant(1)*dx(domain=mesh)
36 a_vector = v*dx
37 a_matrix = u*v*dx
38
39 A_scalar = assemble_local(a_scalar, c)
40 A_vector = assemble_local(a_vector, c)
41 A_matrix = assemble_local(a_matrix, c)
42
43 assert isinstance(A_scalar, float)
44 assert near(A_scalar, 0.05)
45
46 assert isinstance(A_vector, numpy.ndarray)
47 assert A_vector.shape == (2,)
48 assert near(A_vector[0], 0.025)
49 assert near(A_vector[1], 0.025)
50
51 assert isinstance(A_matrix, numpy.ndarray)
52 assert A_matrix.shape == (2, 2)
53 assert near(A_matrix[0, 0], 1/60)
54 assert near(A_matrix[0, 1], 1/120)
55 assert near(A_matrix[1, 0], 1/120)
56 assert near(A_matrix[1, 1], 1/60)
57
58
59 def test_local_assembler_on_facet_integrals(ghost_mode):
60 mesh = UnitSquareMesh(4, 4, 'right')
61 Vcg = FunctionSpace(mesh, 'CG', 1)
62 Vdg = FunctionSpace(mesh, 'DG', 0)
63 Vdgt = FunctionSpace(mesh, 'DGT', 1)
64
65 v = TestFunction(Vdgt)
66 n = FacetNormal(mesh)
67
68 # Initialize DG function "w" in discontinuous pattern
69 w = Expression('(1.0 + pow(x[0], 2.2) + 1/(0.1 + pow(x[1], 3)))*300.0',
70 element=Vdg.ufl_element())
71
72 # Define form that tests that the correct + and - values are used
73 L = w('-')*v('+')*dS
74
75 # Compile form. This is collective
76 L = Form(L)
77
78 # Get global cell 10. This will return a cell only on one of the
79 # processes
80 c = get_cell_at(mesh, 5/12, 1/3, 0)
81
82 if c:
83 # Assemble locally on the selected cell
84 b_e = assemble_local(L, c)
85
86 # Compare to values from phonyx (fully independent
87 # implementation)
88 b_phonyx = numpy.array([266.55210302, 266.55210302, 365.49000122,
89 365.49000122, 0.0, 0.0])
90 error = sum((b_e - b_phonyx)**2)**0.5
91 error = float(error) # MPI.max does strange things to numpy.float64
92
93 else:
94 error = 0.0
95
96 error = MPI.max(MPI.comm_world, float(error))
97 assert error < 1e-8
98
99
100 def test_local_assembler_on_facet_integrals2(ghost_mode):
101 mesh = UnitSquareMesh(4, 4)
102 Vu = VectorFunctionSpace(mesh, 'DG', 1)
103 Vv = FunctionSpace(mesh, 'DGT', 1)
104 u = TrialFunction(Vu)
105 v = TestFunction(Vv)
106 n = FacetNormal(mesh)
107
108 # Define form
109 a = dot(u, n)*v*ds
110 for R in '+-':
111 a += dot(u(R), n(R))*v(R)*dS
112
113 # Compile form. This is collective
114 a = Form(a)
115
116 # Get global cell 0. This will return a cell only on one of the
117 # processes
118 c = get_cell_at(mesh, 1/6, 1/12, 0)
119
120 if c:
121 A_e = assemble_local(a, c)
122 A_correct = numpy.array([[0, 1/12, 1/24, 0, 0, 0],
123 [0, 1/24, 1/12, 0, 0, 0],
124 [-1/12, 0, -1/24, 1/12, 0, 1/24],
125 [-1/24, 0, -1/12, 1/24, 0, 1/12],
126 [0, 0, 0, -1/12, -1/24, 0],
127 [0, 0, 0, -1/24, -1/12, 0]])
128 error = ((A_e - A_correct)**2).sum()**0.5
129 error = float(error) # MPI.max does strange things to numpy.float64
130
131 else:
132 error = 0.0
133
134 error = MPI.max(MPI.comm_world, float(error))
135 assert error < 1e-16
136
137
138 def get_cell_at(mesh, x, y, z, eps=1e-3):
139 """Return the cell with the given midpoint or None if not found. The
140 function also checks that the cell is found on one of the
141 processes when running in parallel to avoid that the above tests
142 always suceed if the cell is not found on any of the processes.
143
144 """
145 found = None
146 for cell in cells(mesh):
147 mp = cell.midpoint()
148 if abs(mp.x() - x) + abs(mp.y() - y) + abs(mp.y() - y) < eps:
149 found = cell
150 break
151
152 # Make sure this cell is on at least one of the parallel processes
153 marker = 1 if found is not None else 0
154 assert MPI.max(MPI.comm_world, marker) == 1
155
156 return found