Debian Codegraph @

python/test/unit/fem/test_finite_element.py

raw
1 """Unit tests for the fem interface"""
2
3 # Copyright (C) 2009 Garth N. Wells
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 # First added: 2009-07-28
21 # Last changed: 2009-07-28
22
23 import pytest
24 import numpy
25 from dolfin import *
26 from dolfin_utils.test import fixture
27
28
29 xfail = pytest.mark.xfail(strict=True)
30
31
32 @pytest.mark.parametrize('mesh_factory', [(UnitSquareMesh, (4, 4)),
33 (UnitCubeMesh, (2, 2, 2)),
34 (UnitSquareMesh.create, (4, 4, CellType.Type.quadrilateral)),
35 # cell_normal has not been implemented for hex cell
36 # cell.orientation() does not work
37 pytest.param(((UnitCubeMesh.create, (2, 2, 2, CellType.Type.hexahedron))), marks=xfail)])
38 def test_evaluate_dofs(mesh_factory):
39
40 func, args = mesh_factory
41 mesh = func(*args)
42
43 v = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
44 q = VectorElement("Lagrange", mesh.ufl_cell(), 1)
45 w = v*q
46
47 V = FunctionSpace(mesh, v)
48 W = FunctionSpace(mesh, w)
49
50 sdim = V.element().space_dimension()
51 gdim = V.element().geometric_dimension()
52
53 e = Expression("x[0] + x[1]", degree=1)
54 e2 = Expression(["x[0] + x[1]"]*gdim, degree=1)
55
56 coords = numpy.zeros((sdim, gdim), dtype="d")
57 coord = numpy.zeros(gdim, dtype="d")
58 values0 = numpy.zeros(sdim, dtype="d")
59 values1 = numpy.zeros(sdim, dtype="d")
60 values2 = numpy.zeros(sdim, dtype="d")
61 values3 = numpy.zeros(sdim, dtype="d")
62 values4 = numpy.zeros(gdim*sdim, dtype="d")
63
64 L0 = W.sub(0)
65 L1 = W.sub(1)
66 L01 = L1.sub(0)
67 L11 = L1.sub(1)
68
69 for cell in cells(mesh):
70 vx = cell.get_vertex_coordinates()
71 orientation = cell.orientation()
72 coords = V.element().tabulate_dof_coordinates(cell)
73 for i in range(coords.shape[0]):
74 coord[:] = coords[i, :]
75 values0[i] = e(*coord)
76 values1 = L0.element().evaluate_dofs(e, vx, orientation, cell)
77 values2 = L01.element().evaluate_dofs(e, vx, orientation, cell)
78 values3 = L11.element().evaluate_dofs(e, vx, orientation, cell)
79 values4 = L1.element().evaluate_dofs(e2, vx, orientation, cell)
80
81 for i in range(sdim):
82 assert round(values0[i] - values1[i], 7) == 0
83 assert round(values0[i] - values2[i], 7) == 0
84 assert round(values0[i] - values3[i], 7) == 0
85 assert round(values4[:sdim][i] - values0[i], 7) == 0
86 if gdim == 3:
87 assert round(values4[sdim:sdim*2][i] - values0[i], 7) == 0
88 assert round(values4[sdim*2:][i] - values0[i], 7) == 0
89 else:
90 assert round(values4[sdim:][i] - values0[i], 7) == 0
91
92
93 def test_evaluate_dofs_manifolds_affine():
94 "Testing evaluate_dofs vs tabulated coordinates."
95
96 n = 4
97 mesh = BoundaryMesh(UnitSquareMesh(n, n), "exterior")
98 mesh2 = BoundaryMesh(UnitCubeMesh(n, n, n), "exterior")
99 DG0 = FunctionSpace(mesh, "DG", 0)
100 DG1 = FunctionSpace(mesh, "DG", 1)
101 CG1 = FunctionSpace(mesh, "CG", 1)
102 CG2 = FunctionSpace(mesh, "CG", 2)
103 DG20 = FunctionSpace(mesh2, "DG", 0)
104 DG21 = FunctionSpace(mesh2, "DG", 1)
105 CG21 = FunctionSpace(mesh2, "CG", 1)
106 CG22 = FunctionSpace(mesh2, "CG", 2)
107 elements = [DG0, DG1, CG1, CG2, DG20, DG21, CG21, CG22]
108
109 f = Expression("x[0] + x[1]", degree=1)
110 for V in elements:
111 sdim = V.element().space_dimension()
112 gdim = V.mesh().geometry().dim()
113 coord = numpy.zeros(gdim, dtype="d")
114 values0 = numpy.zeros(sdim, dtype="d")
115 values1 = numpy.zeros(sdim, dtype="d")
116 for cell in cells(V.mesh()):
117 vx = cell.get_vertex_coordinates()
118 orientation = cell.orientation()
119 coords = V.element().tabulate_dof_coordinates(cell)
120 for i in range(coords.shape[0]):
121 coord[:] = coords[i, :]
122 values0[i] = f(*coord)
123 values1 = V.element().evaluate_dofs(f, vx, orientation, cell)
124 for i in range(sdim):
125 assert round(values0[i] - values1[i], 7) == 0
126
127
128 @pytest.mark.parametrize('mesh_factory', [(UnitSquareMesh, (4, 4)),
129 (UnitCubeMesh, (2, 2, 2)),
130 (UnitSquareMesh.create, (4, 4, CellType.Type.quadrilateral)),
131 (UnitCubeMesh.create, (2, 2, 2, CellType.Type.hexahedron))])
132 def test_tabulate_coord(mesh_factory):
133
134 func, args = mesh_factory
135 mesh = func(*args)
136
137 v = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
138 q = VectorElement("Lagrange", mesh.ufl_cell(), 1)
139 w = v*q
140
141 V = FunctionSpace(mesh, v)
142 W = FunctionSpace(mesh, w)
143
144 sdim = V.element().space_dimension()
145 gdim = V.element().geometric_dimension()
146 coord0 = numpy.zeros((sdim, gdim), dtype="d")
147 coord1 = numpy.zeros((sdim, gdim), dtype="d")
148 coord2 = numpy.zeros((sdim, gdim), dtype="d")
149 coord3 = numpy.zeros((sdim, gdim), dtype="d")
150 coord4 = numpy.zeros((gdim*sdim, gdim), dtype="d")
151
152 L0 = W.sub(0)
153 L1 = W.sub(1)
154 L01 = L1.sub(0)
155 L11 = L1.sub(1)
156
157 for cell in cells(mesh):
158 coord0 = V.element().tabulate_dof_coordinates(cell)
159 coord1 = L0.element().tabulate_dof_coordinates(cell)
160 coord2 = L01.element().tabulate_dof_coordinates(cell)
161 coord3 = L11.element().tabulate_dof_coordinates(cell)
162 coord4 = L1.element().tabulate_dof_coordinates(cell)
163
164 assert (coord0 == coord1).all()
165 assert (coord0 == coord2).all()
166 assert (coord0 == coord3).all()
167 if gdim == 3:
168 assert (coord4[sdim:sdim*2] == coord0).all()
169 assert (coord4[sdim*2:] == coord0).all()
170 else:
171 assert (coord4[sdim:] == coord0).all()