| 1 |
"""Unit tests for the fem interface""" |
| 2 |
|
| 3 |
# Copyright (C) 2009-2014 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 |
import pytest |
| 21 |
import numpy as np |
| 22 |
|
| 23 |
import sys |
| 24 |
|
| 25 |
from dolfin import * |
| 26 |
from dolfin_utils.test import * |
| 27 |
|
| 28 |
|
| 29 |
xfail = pytest.mark.xfail(strict=True) |
| 30 |
|
| 31 |
@fixture |
| 32 |
def mesh(): |
| 33 |
return UnitSquareMesh(4, 4) |
| 34 |
|
| 35 |
|
| 36 |
reorder_dofs = set_parameters_fixture("reorder_dofs_serial", [True, False]) |
| 37 |
|
| 38 |
|
| 39 |
@pytest.mark.parametrize('mesh_factory', [(UnitIntervalMesh, (8,)), |
| 40 |
(UnitSquareMesh, (4, 4)), |
| 41 |
(UnitCubeMesh, (2, 2, 2)), |
| 42 |
# cell.contains(Point) does not work correctly |
| 43 |
# for quad/hex cells once it is fixed, this test will pass |
| 44 |
pytest.param(((UnitSquareMesh.create, (4, 4, CellType.Type.quadrilateral))), marks=xfail), |
| 45 |
pytest.param(((UnitCubeMesh.create, (2, 2, 2, CellType.Type.hexahedron))), marks=xfail)]) |
| 46 |
def test_tabulate_all_coordinates(mesh_factory): |
| 47 |
func, args = mesh_factory |
| 48 |
mesh = func(*args) |
| 49 |
V = FunctionSpace(mesh, "Lagrange", 1) |
| 50 |
W0 = FiniteElement("Lagrange", mesh.ufl_cell(), 1) |
| 51 |
W1 = VectorElement("Lagrange", mesh.ufl_cell(), 1) |
| 52 |
W = FunctionSpace(mesh, W0*W1) |
| 53 |
|
| 54 |
D = mesh.geometry().dim() |
| 55 |
V_dofmap = V.dofmap() |
| 56 |
W_dofmap = W.dofmap() |
| 57 |
|
| 58 |
all_coords_V = V.tabulate_dof_coordinates() |
| 59 |
all_coords_W = W.tabulate_dof_coordinates() |
| 60 |
local_size_V = V_dofmap.ownership_range()[1]-V_dofmap.ownership_range()[0] |
| 61 |
local_size_W = W_dofmap.ownership_range()[1]-W_dofmap.ownership_range()[0] |
| 62 |
|
| 63 |
all_coords_V = all_coords_V.reshape(local_size_V, D) |
| 64 |
all_coords_W = all_coords_W.reshape(local_size_W, D) |
| 65 |
|
| 66 |
checked_V = [False]*local_size_V |
| 67 |
checked_W = [False]*local_size_W |
| 68 |
|
| 69 |
# Check that all coordinates are within the cell it should be |
| 70 |
for cell in cells(mesh): |
| 71 |
dofs_V = V_dofmap.cell_dofs(cell.index()) |
| 72 |
for di in dofs_V: |
| 73 |
if di >= local_size_V: |
| 74 |
continue |
| 75 |
assert cell.contains(Point(all_coords_V[di])) |
| 76 |
checked_V[di] = True |
| 77 |
|
| 78 |
dofs_W = W_dofmap.cell_dofs(cell.index()) |
| 79 |
for di in dofs_W: |
| 80 |
if di >= local_size_W: |
| 81 |
continue |
| 82 |
assert cell.contains(Point(all_coords_W[di])) |
| 83 |
checked_W[di] = True |
| 84 |
|
| 85 |
# Assert that all dofs have been checked by the above |
| 86 |
assert all(checked_V) |
| 87 |
assert all(checked_W) |
| 88 |
|
| 89 |
|
| 90 |
@pytest.mark.parametrize('mesh_factory', [(UnitSquareMesh, (4, 4)), (UnitSquareMesh.create, (4, 4, CellType.Type.quadrilateral))]) |
| 91 |
def test_tabulate_dofs(mesh_factory): |
| 92 |
func, args = mesh_factory |
| 93 |
mesh = func(*args) |
| 94 |
W0 = FiniteElement("Lagrange", mesh.ufl_cell(), 1) |
| 95 |
W1 = VectorElement("Lagrange", mesh.ufl_cell(), 1) |
| 96 |
W = FunctionSpace(mesh, W0*W1) |
| 97 |
|
| 98 |
L0 = W.sub(0) |
| 99 |
L1 = W.sub(1) |
| 100 |
L01 = L1.sub(0) |
| 101 |
L11 = L1.sub(1) |
| 102 |
|
| 103 |
for i, cell in enumerate(cells(mesh)): |
| 104 |
dofs0 = L0.dofmap().cell_dofs(cell.index()) |
| 105 |
dofs1 = L01.dofmap().cell_dofs(cell.index()) |
| 106 |
dofs2 = L11.dofmap().cell_dofs(cell.index()) |
| 107 |
dofs3 = L1.dofmap().cell_dofs(cell.index()) |
| 108 |
|
| 109 |
assert np.array_equal(dofs0, L0.dofmap().cell_dofs(i)) |
| 110 |
assert np.array_equal(dofs1, L01.dofmap().cell_dofs(i)) |
| 111 |
assert np.array_equal(dofs2, L11.dofmap().cell_dofs(i)) |
| 112 |
assert np.array_equal(dofs3, L1.dofmap().cell_dofs(i)) |
| 113 |
|
| 114 |
assert len(np.intersect1d(dofs0, dofs1)) == 0 |
| 115 |
assert len(np.intersect1d(dofs0, dofs2)) == 0 |
| 116 |
assert len(np.intersect1d(dofs1, dofs2)) == 0 |
| 117 |
assert np.array_equal(np.append(dofs1, dofs2), dofs3) |
| 118 |
|
| 119 |
|
| 120 |
@pytest.mark.parametrize('mesh_factory', [(UnitSquareMesh, (4, 4)), (UnitSquareMesh.create, (4, 4, CellType.Type.quadrilateral))]) |
| 121 |
def test_tabulate_coord_periodic(mesh_factory): |
| 122 |
|
| 123 |
class PeriodicBoundary2(SubDomain): |
| 124 |
def inside(self, x, on_boundary): |
| 125 |
return x[0] < DOLFIN_EPS |
| 126 |
|
| 127 |
def map(self, x, y): |
| 128 |
y[0] = x[0] - 1.0 |
| 129 |
y[1] = x[1] |
| 130 |
|
| 131 |
# Create periodic boundary condition |
| 132 |
periodic_boundary = PeriodicBoundary2() |
| 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, constrained_domain=periodic_boundary) |
| 142 |
W = FunctionSpace(mesh, W, constrained_domain=periodic_boundary) |
| 143 |
|
| 144 |
L0 = W.sub(0) |
| 145 |
L1 = W.sub(1) |
| 146 |
L01 = L1.sub(0) |
| 147 |
L11 = L1.sub(1) |
| 148 |
|
| 149 |
sdim = V.element().space_dimension() |
| 150 |
coord0 = np.zeros((sdim, 2), dtype="d") |
| 151 |
coord1 = np.zeros((sdim, 2), dtype="d") |
| 152 |
coord2 = np.zeros((sdim, 2), dtype="d") |
| 153 |
coord3 = np.zeros((sdim, 2), dtype="d") |
| 154 |
|
| 155 |
for cell in cells(mesh): |
| 156 |
coord0 = V.element().tabulate_dof_coordinates(cell) |
| 157 |
coord1 = L0.element().tabulate_dof_coordinates(cell) |
| 158 |
coord2 = L01.element().tabulate_dof_coordinates(cell) |
| 159 |
coord3 = L11.element().tabulate_dof_coordinates(cell) |
| 160 |
coord4 = L1.element().tabulate_dof_coordinates(cell) |
| 161 |
|
| 162 |
assert (coord0 == coord1).all() |
| 163 |
assert (coord0 == coord2).all() |
| 164 |
assert (coord0 == coord3).all() |
| 165 |
assert (coord4[:sdim] == coord0).all() |
| 166 |
assert (coord4[sdim:] == coord0).all() |
| 167 |
|
| 168 |
|
| 169 |
@pytest.mark.parametrize('mesh_factory', [(UnitSquareMesh, (5, 5)), (UnitSquareMesh.create, (5, 5, CellType.Type.quadrilateral))]) |
| 170 |
def test_tabulate_dofs_periodic(mesh_factory): |
| 171 |
|
| 172 |
class PeriodicBoundary2(SubDomain): |
| 173 |
def inside(self, x, on_boundary): |
| 174 |
return x[0] < DOLFIN_EPS |
| 175 |
|
| 176 |
def map(self, x, y): |
| 177 |
y[0] = x[0] - 1.0 |
| 178 |
y[1] = x[1] |
| 179 |
|
| 180 |
func, args = mesh_factory |
| 181 |
mesh = func(*args) |
| 182 |
|
| 183 |
# Create periodic boundary |
| 184 |
periodic_boundary = PeriodicBoundary2() |
| 185 |
|
| 186 |
V = FiniteElement("Lagrange", mesh.ufl_cell(), 2) |
| 187 |
Q = VectorElement("Lagrange", mesh.ufl_cell(), 2) |
| 188 |
W = V*Q |
| 189 |
|
| 190 |
V = FunctionSpace(mesh, V, constrained_domain=periodic_boundary) |
| 191 |
Q = FunctionSpace(mesh, Q, constrained_domain=periodic_boundary) |
| 192 |
W = FunctionSpace(mesh, W, constrained_domain=periodic_boundary) |
| 193 |
|
| 194 |
L0 = W.sub(0) |
| 195 |
L1 = W.sub(1) |
| 196 |
L01 = L1.sub(0) |
| 197 |
L11 = L1.sub(1) |
| 198 |
|
| 199 |
# Check dimensions |
| 200 |
assert V.dim() == 110 |
| 201 |
assert Q.dim() == 220 |
| 202 |
assert L0.dim() == V.dim() |
| 203 |
assert L1.dim() == Q.dim() |
| 204 |
assert L01.dim() == V.dim() |
| 205 |
assert L11.dim() == V.dim() |
| 206 |
|
| 207 |
for i, cell in enumerate(cells(mesh)): |
| 208 |
dofs0 = L0.dofmap().cell_dofs(cell.index()) |
| 209 |
dofs1 = L01.dofmap().cell_dofs(cell.index()) |
| 210 |
dofs2 = L11.dofmap().cell_dofs(cell.index()) |
| 211 |
dofs3 = L1.dofmap().cell_dofs(cell.index()) |
| 212 |
|
| 213 |
assert np.array_equal(dofs0, L0.dofmap().cell_dofs(i)) |
| 214 |
assert np.array_equal(dofs1, L01.dofmap().cell_dofs(i)) |
| 215 |
assert np.array_equal(dofs2, L11.dofmap().cell_dofs(i)) |
| 216 |
assert np.array_equal(dofs3, L1.dofmap().cell_dofs(i)) |
| 217 |
|
| 218 |
assert len(np.intersect1d(dofs0, dofs1)) == 0 |
| 219 |
assert len(np.intersect1d(dofs0, dofs2)) == 0 |
| 220 |
assert len(np.intersect1d(dofs1, dofs2)) == 0 |
| 221 |
assert np.array_equal(np.append(dofs1, dofs2), dofs3) |
| 222 |
|
| 223 |
|
| 224 |
@pytest.mark.parametrize('mesh_factory', [(UnitSquareMesh, (3, 3)), (UnitSquareMesh.create, (3, 3, CellType.Type.quadrilateral))]) |
| 225 |
def test_global_dof_builder(mesh_factory): |
| 226 |
func, args = mesh_factory |
| 227 |
mesh = func(*args) |
| 228 |
|
| 229 |
V = VectorElement("CG", mesh.ufl_cell(), 1) |
| 230 |
Q = FiniteElement("CG", mesh.ufl_cell(), 1) |
| 231 |
R = FiniteElement("R", mesh.ufl_cell(), 0) |
| 232 |
|
| 233 |
W = FunctionSpace(mesh, MixedElement([Q, Q, Q, R])) |
| 234 |
W = FunctionSpace(mesh, MixedElement([Q, Q, R, Q])) |
| 235 |
W = FunctionSpace(mesh, V*R) |
| 236 |
W = FunctionSpace(mesh, R*V) |
| 237 |
|
| 238 |
|
| 239 |
@pytest.mark.parametrize('mesh_factory', [(UnitSquareMesh, (3, 3)), (UnitSquareMesh.create, (3, 3, CellType.Type.quadrilateral))]) |
| 240 |
def test_dof_to_vertex_map(mesh_factory, reorder_dofs): |
| 241 |
func, args = mesh_factory |
| 242 |
mesh = func(*args) |
| 243 |
|
| 244 |
def _test_maps_consistency(space): |
| 245 |
v2d = vertex_to_dof_map(space) |
| 246 |
d2v = dof_to_vertex_map(space) |
| 247 |
assert len(v2d) == len(d2v) |
| 248 |
assert np.all(v2d[d2v] == np.arange(len(v2d))) |
| 249 |
assert np.all(d2v[v2d] == np.arange(len(d2v))) |
| 250 |
|
| 251 |
# Check for both reordered and UFC ordered dofs |
| 252 |
v = FiniteElement("Lagrange", mesh.ufl_cell(), 1) |
| 253 |
q = VectorElement("Lagrange", mesh.ufl_cell(), 1) |
| 254 |
w = v*q |
| 255 |
|
| 256 |
V = FunctionSpace(mesh, v) |
| 257 |
Q = FunctionSpace(mesh, q) |
| 258 |
W = FunctionSpace(mesh, w) |
| 259 |
|
| 260 |
_test_maps_consistency(V) |
| 261 |
_test_maps_consistency(Q) |
| 262 |
_test_maps_consistency(W) |
| 263 |
|
| 264 |
u = Function(V) |
| 265 |
e = Expression("x[0] + x[1]", degree=1) |
| 266 |
u.interpolate(e) |
| 267 |
|
| 268 |
vert_values = mesh.coordinates().sum(1) |
| 269 |
func_values = u.vector().get_local(vertex_to_dof_map(V)) |
| 270 |
assert round(max(abs(func_values - vert_values)), 7) == 0 |
| 271 |
|
| 272 |
c0 = Constant((1, 2)) |
| 273 |
u0 = Function(Q) |
| 274 |
u0.interpolate(c0) |
| 275 |
|
| 276 |
vert_values = np.zeros(mesh.num_vertices()*2) |
| 277 |
u1 = Function(Q) |
| 278 |
vert_values[::2] = 1 |
| 279 |
vert_values[1::2] = 2 |
| 280 |
|
| 281 |
dim = Q.dofmap().index_map().size(IndexMap.MapSize.OWNED) |
| 282 |
u1.vector().set_local(vert_values[dof_to_vertex_map(Q)[:dim]].copy()) |
| •283 |
assert round((u0.vector()[:]-u1.vector()[:]).sum() - 0.0, 7) == 0 |
| 284 |
|
| 285 |
W = FunctionSpace(mesh, "DG", 0) |
| 286 |
with pytest.raises(RuntimeError): |
| 287 |
dof_to_vertex_map(W) |
| 288 |
|
| 289 |
W = FunctionSpace(mesh, q*FiniteElement("R", mesh.ufl_cell(), 0)) |
| 290 |
with pytest.raises(RuntimeError): |
| 291 |
dof_to_vertex_map(W) |
| 292 |
|
| 293 |
W = FunctionSpace(mesh, "CG", 2) |
| 294 |
with pytest.raises(RuntimeError): |
| 295 |
dof_to_vertex_map(W) |
| 296 |
|
| 297 |
W = VectorFunctionSpace(mesh, "CG", 1) |
| 298 |
with pytest.raises(RuntimeError): |
| 299 |
dof_to_vertex_map(W.sub(0)) |
| 300 |
|
| 301 |
|
| 302 |
def test_entity_dofs(mesh): |
| 303 |
|
| 304 |
# Test that num entity dofs is correctly wrapped to |
| 305 |
# dolfin::DofMap |
| 306 |
V = FunctionSpace(mesh, "CG", 1) |
| 307 |
assert V.dofmap().num_entity_dofs(0) == 1 |
| 308 |
assert V.dofmap().num_entity_dofs(1) == 0 |
| 309 |
assert V.dofmap().num_entity_dofs(2) == 0 |
| 310 |
|
| 311 |
V = VectorFunctionSpace(mesh, "CG", 1) |
| 312 |
assert V.dofmap().num_entity_dofs(0) == 2 |
| 313 |
assert V.dofmap().num_entity_dofs(1) == 0 |
| 314 |
assert V.dofmap().num_entity_dofs(2) == 0 |
| 315 |
|
| 316 |
V = FunctionSpace(mesh, "CG", 2) |
| 317 |
assert V.dofmap().num_entity_dofs(0) == 1 |
| 318 |
assert V.dofmap().num_entity_dofs(1) == 1 |
| 319 |
assert V.dofmap().num_entity_dofs(2) == 0 |
| 320 |
|
| 321 |
V = FunctionSpace(mesh, "CG", 3) |
| 322 |
assert V.dofmap().num_entity_dofs(0) == 1 |
| 323 |
assert V.dofmap().num_entity_dofs(1) == 2 |
| 324 |
assert V.dofmap().num_entity_dofs(2) == 1 |
| 325 |
|
| 326 |
V = FunctionSpace(mesh, "DG", 0) |
| 327 |
assert V.dofmap().num_entity_dofs(0) == 0 |
| 328 |
assert V.dofmap().num_entity_dofs(1) == 0 |
| 329 |
assert V.dofmap().num_entity_dofs(2) == 1 |
| 330 |
|
| 331 |
V = FunctionSpace(mesh, "DG", 1) |
| 332 |
assert V.dofmap().num_entity_dofs(0) == 0 |
| 333 |
assert V.dofmap().num_entity_dofs(1) == 0 |
| 334 |
assert V.dofmap().num_entity_dofs(2) == 3 |
| 335 |
|
| 336 |
V = VectorFunctionSpace(mesh, "CG", 1) |
| 337 |
|
| 338 |
# Note this numbering is dependent on FFC and can change This test |
| 339 |
# is here just to check that we get correct numbers mapped from |
| 340 |
# ufc generated code to dolfin |
| 341 |
for i, cdofs in enumerate([[0, 3], [1, 4], [2, 5]]): |
| 342 |
dofs = V.dofmap().tabulate_entity_dofs(0, i) |
| 343 |
assert all(d == cd for d, cd in zip(dofs, cdofs)) |
| 344 |
|
| 345 |
|
| 346 |
@skip_in_parallel |
| 347 |
@pytest.mark.parametrize('mesh_factory', [(UnitSquareMesh, (2, 2)), (UnitSquareMesh.create, (2, 2, CellType.Type.quadrilateral))]) |
| 348 |
def test_entity_closure_dofs(mesh_factory): |
| 349 |
func, args = mesh_factory |
| 350 |
mesh = func(*args) |
| 351 |
tdim = mesh.topology().dim() |
| 352 |
|
| 353 |
for degree in (1, 2, 3): |
| 354 |
V = FunctionSpace(mesh, "CG", degree) |
| 355 |
for d in range(tdim + 1): |
| 356 |
covered = set() |
| 357 |
covered2 = set() |
| 358 |
all_entities = np.array([entity for entity in range(mesh.num_entities(d))], dtype=np.uintp) |
| 359 |
for entity in all_entities: |
| 360 |
entities = np.array([entity], dtype=np.uintp) |
| 361 |
dofs_on_this_entity = V.dofmap().entity_dofs(mesh, d, entities) |
| 362 |
closure_dofs = V.dofmap().entity_closure_dofs(mesh, d, entities) |
| 363 |
assert len(dofs_on_this_entity) == V.dofmap().num_entity_dofs(d) |
| 364 |
assert len(dofs_on_this_entity) <= len(closure_dofs) |
| 365 |
covered.update(dofs_on_this_entity) |
| 366 |
covered2.update(closure_dofs) |
| 367 |
dofs_on_all_entities = V.dofmap().entity_dofs(mesh, d, all_entities) |
| 368 |
closure_dofs_on_all_entities = V.dofmap().entity_closure_dofs(mesh, d, all_entities) |
| 369 |
assert len(dofs_on_all_entities) == V.dofmap().num_entity_dofs(d) * mesh.num_entities(d) |
| 370 |
assert covered == set(dofs_on_all_entities) |
| 371 |
assert covered2 == set(closure_dofs_on_all_entities) |
| 372 |
d = tdim |
| 373 |
all_cells = np.array([entity for entity in range(mesh.num_entities(d))], dtype=np.uintp) |
| 374 |
assert set(V.dofmap().entity_closure_dofs(mesh, d, all_cells)) == set(range(V.dim())) |
| 375 |
|
| 376 |
|
| 377 |
def test_clear_sub_map_data_scalar(mesh): |
| 378 |
V = FunctionSpace(mesh, "CG", 2) |
| 379 |
with pytest.raises(ValueError): |
| 380 |
V.sub(1) |
| 381 |
|
| 382 |
V = VectorFunctionSpace(mesh, "CG", 2) |
| 383 |
V1 = V.sub(1) |
| 384 |
|
| 385 |
# Clean sub-map data |
| 386 |
V.dofmap().clear_sub_map_data() |
| 387 |
|
| 388 |
# Can still get previously computed map |
| 389 |
V1 = V.sub(1) |
| 390 |
|
| 391 |
# New sub-map should throw an error |
| 392 |
with pytest.raises(RuntimeError): |
| 393 |
V.sub(0) |
| 394 |
|
| 395 |
|
| 396 |
def test_clear_sub_map_data_vector(mesh): |
| 397 |
mesh = UnitSquareMesh(8, 8) |
| 398 |
P1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1) |
| 399 |
W = FunctionSpace(mesh, P1*P1) |
| 400 |
|
| 401 |
# Check block size |
| 402 |
assert W.dofmap().block_size() == 2 |
| 403 |
|
| 404 |
W.dofmap().clear_sub_map_data() |
| 405 |
with pytest.raises(RuntimeError): |
| 406 |
W0 = W.sub(0) |
| 407 |
with pytest.raises(RuntimeError): |
| 408 |
W1 = W.sub(1) |
| 409 |
|
| 410 |
|
| 411 |
def test_block_size(mesh): |
| 412 |
meshes = [UnitSquareMesh(8, 8), UnitCubeMesh(4, 4, 4), |
| 413 |
UnitSquareMesh.create(8, 8, CellType.Type.quadrilateral), |
| 414 |
UnitCubeMesh.create(4, 4, 4, CellType.Type.hexahedron)] |
| 415 |
for mesh in meshes: |
| 416 |
P2 = FiniteElement("Lagrange", mesh.ufl_cell(), 2) |
| 417 |
|
| 418 |
V = FunctionSpace(mesh, P2) |
| 419 |
assert V.dofmap().block_size() == 1 |
| 420 |
|
| 421 |
V = FunctionSpace(mesh, P2*P2) |
| 422 |
assert V.dofmap().block_size() == 2 |
| 423 |
|
| 424 |
for i in range(1, 6): |
| 425 |
W = FunctionSpace(mesh, MixedElement(i*[P2])) |
| 426 |
assert W.dofmap().block_size() == i |
| 427 |
|
| 428 |
V = VectorFunctionSpace(mesh, "Lagrange", 2) |
| 429 |
assert V.dofmap().block_size() == mesh.geometry().dim() |
| 430 |
|
| 431 |
|
| 432 |
def test_block_size_real(mesh): |
| 433 |
mesh = UnitIntervalMesh(12) |
| 434 |
V = FiniteElement('DG', mesh.ufl_cell(), 0) |
| 435 |
R = FiniteElement('R', mesh.ufl_cell(), 0) |
| 436 |
X = FunctionSpace(mesh, V*R) |
| 437 |
assert X.dofmap().block_size() == 1 |
| 438 |
|
| 439 |
|
| 440 |
@skip_in_serial |
| 441 |
@pytest.mark.parametrize('mesh_factory', [(UnitIntervalMesh, (8,)), |
| 442 |
(UnitSquareMesh, (4, 4)), |
| 443 |
(UnitCubeMesh, (2, 2, 2)), |
| 444 |
(UnitSquareMesh.create, (4, 4, CellType.Type.quadrilateral)), |
| 445 |
(UnitCubeMesh.create, (2, 2, 2, CellType.Type.hexahedron))]) |
| 446 |
def test_mpi_dofmap_stats(mesh_factory): |
| 447 |
func, args = mesh_factory |
| 448 |
mesh = func(*args) |
| 449 |
|
| 450 |
V = FunctionSpace(mesh, "CG", 1) |
| 451 |
assert len(V.dofmap().shared_nodes()) > 0 |
| 452 |
neighbours = V.dofmap().neighbours() |
| 453 |
for processes in V.dofmap().shared_nodes().values(): |
| 454 |
for process in processes: |
| 455 |
assert process in neighbours |
| 456 |
|
| 457 |
for owner in V.dofmap().off_process_owner(): |
| 458 |
assert owner in neighbours |
| 459 |
|
| 460 |
@pytest.mark.parametrize('mesh_factory', [(UnitSquareMesh, (4, 4)), (UnitSquareMesh.create, (4, 4, CellType.Type.quadrilateral))]) |
| 461 |
def test_local_dimension(mesh_factory): |
| 462 |
func, args = mesh_factory |
| 463 |
mesh = func(*args) |
| 464 |
|
| 465 |
v = FiniteElement("Lagrange", mesh.ufl_cell(), 1) |
| 466 |
q = VectorElement("Lagrange", mesh.ufl_cell(), 1) |
| 467 |
w = v*q |
| 468 |
|
| 469 |
V = FunctionSpace(mesh, v) |
| 470 |
Q = FunctionSpace(mesh, q) |
| 471 |
W = FunctionSpace(mesh, w) |
| 472 |
|
| 473 |
for space in [V, Q, W]: |
| 474 |
dofmap = space.dofmap() |
| 475 |
local_to_global_map = dofmap.tabulate_local_to_global_dofs() |
| 476 |
ownership_range = dofmap.ownership_range() |
| 477 |
dim1 = dofmap.index_map().size(IndexMap.MapSize.OWNED) |
| 478 |
dim2 = dofmap.index_map().size(IndexMap.MapSize.UNOWNED) |
| 479 |
dim3 = dofmap.index_map().size(IndexMap.MapSize.ALL) |
| 480 |
assert dim1 == ownership_range[1] - ownership_range[0] |
| 481 |
assert dim3 == local_to_global_map.size |
| 482 |
assert dim1 + dim2 == dim3 |
| 483 |
# with pytest.raises(RuntimeError): |
| 484 |
# dofmap.index_map().size('foo') |
| 485 |
|
| 486 |
|
| 487 |
# Failures in FFC on quads/hexes |
| 488 |
xfail_ffc = pytest.mark.xfail(raises=Exception, strict=True) |
| 489 |
|
| 490 |
@skip_in_parallel |
| 491 |
@pytest.mark.parametrize('space', [ |
| 492 |
"FunctionSpace(UnitIntervalMesh.create(10), 'P', 1)", |
| 493 |
"FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.triangle), 'P', 1)", |
| 494 |
"FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.tetrahedron), 'P', 1)", |
| 495 |
"FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.quadrilateral), 'Q', 1)", |
| 496 |
"FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.hexahedron), 'Q', 1)", |
| 497 |
"FunctionSpace(UnitIntervalMesh.create(10), 'P', 2)", |
| 498 |
"FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.triangle), 'P', 2)", |
| 499 |
"FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.tetrahedron), 'P', 2)", |
| 500 |
"FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.quadrilateral), 'Q', 2)", |
| 501 |
"FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.hexahedron), 'Q', 2)", |
| 502 |
"FunctionSpace(UnitIntervalMesh.create(10), 'P', 3)", |
| 503 |
"FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.triangle), 'P', 3)", |
| 504 |
"FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.tetrahedron), 'P', 3)", |
| 505 |
"FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.quadrilateral), 'Q', 3)", |
| 506 |
"FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.hexahedron), 'Q', 3)", |
| 507 |
"FunctionSpace(UnitIntervalMesh.create(10), 'DP', 1)", |
| 508 |
"FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.triangle), 'DP', 1)", |
| 509 |
"FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.tetrahedron), 'DP', 1)", |
| 510 |
"FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.quadrilateral), 'DQ', 1)", |
| 511 |
"FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.hexahedron), 'DQ', 1)", |
| 512 |
"FunctionSpace(UnitIntervalMesh.create(10), 'DP', 2)", |
| 513 |
"FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.triangle), 'DP', 2)", |
| 514 |
"FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.tetrahedron), 'DP', 2)", |
| 515 |
"FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.quadrilateral), 'DQ', 2)", |
| 516 |
"FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.hexahedron), 'DQ', 2)", |
| 517 |
"FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.triangle), 'N1curl', 1)", |
| 518 |
"FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.tetrahedron), 'N1curl', 1)", |
| 519 |
pytest.param(("FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.quadrilateral), 'N1curl', 1)"), marks=xfail_ffc), |
| 520 |
pytest.param(("FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.hexahedron), 'N1curl', 1)"), marks=xfail_ffc), |
| 521 |
"FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.triangle), 'N1curl', 2)", |
| 522 |
"FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.tetrahedron), 'N1curl', 2)", |
| 523 |
pytest.param(("FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.quadrilateral), 'N1curl', 2)"), marks=xfail_ffc), |
| 524 |
pytest.param(("FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.hexahedron), 'N1curl', 2)"), marks=xfail_ffc), |
| 525 |
"FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.triangle), 'RT', 1)", |
| 526 |
"FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.tetrahedron), 'RT', 1)", |
| 527 |
pytest.param(("FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.quadrilateral), 'RT', 1)"), marks=xfail_ffc), |
| 528 |
pytest.param(("FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.hexahedron), 'RT', 1)"), marks=xfail_ffc), |
| 529 |
]) |
| 530 |
def test_dofs_dim(space): |
| 531 |
"""Test function GenericDofMap::dofs(mesh, dim)""" |
| 532 |
V = eval(space) |
| 533 |
dofmap = V.dofmap() |
| 534 |
mesh = V.mesh() |
| 535 |
for dim in range(0, mesh.topology().dim()): |
| 536 |
edofs = dofmap.dofs(mesh, dim) |
| 537 |
num_mesh_entities = mesh.num_entities(dim) |
| 538 |
dofs_per_entity = dofmap.num_entity_dofs(dim) |
| 539 |
assert len(edofs) == dofs_per_entity*num_mesh_entities |
| 540 |
|
| 541 |
|
| 542 |
def test_readonly_view_local_to_global_unwoned(mesh): |
| 543 |
"""Test that local_to_global_unwoned() returns readonly |
| 544 |
view into the data; in particular test lifetime of data |
| 545 |
owner""" |
| 546 |
V = FunctionSpace(mesh, "P", 1) |
| 547 |
dofmap = V.dofmap() |
| 548 |
index_map = dofmap.index_map() |
| 549 |
|
| 550 |
rc = sys.getrefcount(dofmap) |
| 551 |
l2gu = dofmap.local_to_global_unowned() |
| 552 |
assert sys.getrefcount(dofmap) == rc + 1 if l2gu.size else rc |
| 553 |
assert not l2gu.flags.writeable |
| 554 |
assert all(l2gu < V.dofmap().global_dimension()) |
| 555 |
del l2gu |
| 556 |
assert sys.getrefcount(dofmap) == rc |
| 557 |
|
| 558 |
rc = sys.getrefcount(index_map) |
| 559 |
l2gu = index_map.local_to_global_unowned() |
| 560 |
assert sys.getrefcount(index_map) == rc + 1 if l2gu.size else rc |
| 561 |
assert not l2gu.flags.writeable |
| 562 |
assert all(l2gu < V.dofmap().global_dimension()) |
| 563 |
del l2gu |
| 564 |
assert sys.getrefcount(index_map) == rc |