Debian Codegraph @

python/test/unit/geometry/test_coordinates.py

raw
1 """Unit tests for coordinates interface"""
2
3 # Copyright (C) 2016 Jan Blechta
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 import numpy as np
22
23 from dolfin import UnitIntervalMesh, UnitSquareMesh, UnitCubeMesh, UnitDiscMesh
24 from dolfin import FunctionSpace, VectorFunctionSpace, Function, MPI
25 from dolfin import UserExpression
26 from dolfin import get_coordinates, set_coordinates, Mesh
27 from dolfin import Expression, interpolate
28 from dolfin_utils.test import skip_in_parallel, fixture
29
30
31 @fixture
32 def meshes_p1():
33 return UnitIntervalMesh(10), UnitSquareMesh(3, 3), UnitCubeMesh(2, 2, 2)
34
35
36 @fixture
37 def meshes_p2():
38 return UnitDiscMesh.create(MPI.comm_world, 1, 2, 2), UnitDiscMesh.create(MPI.comm_world, 1, 2, 3)
39
40
41 def _test_get_set_coordinates(mesh):
42 # Get coords
43 V = FunctionSpace(mesh, mesh.ufl_coordinate_element())
44 c = Function(V)
45 get_coordinates(c, mesh.geometry())
46
47 # Check correctness of got coords
48 _check_coords(mesh, c)
49
50 # Backup and zero coords
51 coords = mesh.coordinates()
52 coords_old = coords.copy()
53 coords[:] = 0.0
54 assert np.all(mesh.coordinates() == 0.0)
55
56 # Set again to old value
57 set_coordinates(mesh.geometry(), c)
58
59 # Check
60 assert np.all(mesh.coordinates() == coords_old)
61
62 def _check_coords(mesh, c):
63 # FIXME: This does not work for higher-order geometries although it should
64 if mesh.geometry().degree() > 1:
65 return
66
67 # Compare supplied c with interpolation of x
68 class X(UserExpression):
69 def eval(self, values, x):
70 values[:] = x[:]
71 x = X(domain=mesh, element=mesh.ufl_coordinate_element())
72 x = interpolate(x, c.function_space())
73 x.vector()[:] -= c.vector()
74 assert np.isclose(x.vector().norm("l1"), 0.0)
75
76
77 def test_linear(meshes_p1):
78 for mesh in meshes_p1:
79 _test_get_set_coordinates(mesh)
80
81
82 @skip_in_parallel(reason="FunctionSpace(UnitDiscMesh) not working in parallel")
83 def test_higher_order(meshes_p2):
84 for mesh in meshes_p2:
85 _test_get_set_coordinates(mesh)
86
87
88 def test_raises(meshes_p1):
89 mesh1, mesh2 = meshes_p1[:2]
90
91 # Wrong FE family
92 V = VectorFunctionSpace(mesh2, "Discontinuous Lagrange", 1)
93 c = Function(V)
94 with pytest.raises(RuntimeError):
95 get_coordinates(c, mesh2.geometry())
96 with pytest.raises(RuntimeError):
97 set_coordinates(mesh2.geometry(), c)
98
99 # Wrong value rank
100 V = FunctionSpace(mesh2, "Lagrange", 1)
101 c = Function(V)
102 with pytest.raises(RuntimeError):
103 get_coordinates(c, mesh2.geometry())
104 with pytest.raises(RuntimeError):
105 set_coordinates(mesh2.geometry(), c)
106
107 # Wrong value shape
108 V = VectorFunctionSpace(mesh2, "Lagrange", mesh2.geometry().degree(),
109 dim=mesh2.geometry().dim() - 1)
110 c = Function(V)
111 with pytest.raises(RuntimeError):
112 get_coordinates(c, mesh2.geometry())
113 with pytest.raises(RuntimeError):
114 set_coordinates(mesh2.geometry(), c)
115
116 # Non-matching degree
117 V = VectorFunctionSpace(mesh2, "Lagrange", mesh2.geometry().degree() + 1)
118 c = Function(V)
119 with pytest.raises(RuntimeError):
120 get_coordinates(c, mesh2.geometry())
121 with pytest.raises(RuntimeError):
122 set_coordinates(mesh2.geometry(), c)