| 1 |
"Unit tests for the mesh library" |
| 2 |
|
| 3 |
# Copyright (C) 2006 Anders Logg |
| 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 |
| 22 |
|
| 23 |
import sys |
| 24 |
import os |
| 25 |
|
| 26 |
import FIAT |
| 27 |
from dolfin import * |
| 28 |
from dolfin_utils.test import fixture, set_parameters_fixture |
| 29 |
from dolfin_utils.test import skip_in_parallel, xfail_in_parallel |
| 30 |
from dolfin_utils.test import cd_tempdir |
| 31 |
|
| 32 |
|
| 33 |
@fixture |
| 34 |
def mesh1d(): |
| 35 |
# Create 1D mesh with degenerate cell |
| 36 |
mesh1d = UnitIntervalMesh(4) |
| 37 |
mesh1d.coordinates()[4] = mesh1d.coordinates()[3] |
| 38 |
return mesh1d |
| 39 |
|
| 40 |
|
| 41 |
@fixture |
| 42 |
def mesh2d(): |
| 43 |
# Create 2D mesh with one equilateral triangle |
| 44 |
mesh2d = UnitSquareMesh(1, 1, 'left') |
| 45 |
mesh2d.coordinates()[3] += 0.5*(sqrt(3.0)-1.0) |
| 46 |
return mesh2d |
| 47 |
|
| 48 |
|
| 49 |
@fixture |
| 50 |
def mesh3d(): |
| 51 |
# Create 3D mesh with regular tetrahedron and degenerate cells |
| 52 |
mesh3d = UnitCubeMesh(1, 1, 1) |
| 53 |
mesh3d.coordinates()[2][0] = 1.0 |
| 54 |
mesh3d.coordinates()[7][1] = 0.0 |
| 55 |
return mesh3d |
| 56 |
|
| 57 |
|
| 58 |
@fixture |
| 59 |
def c0(mesh3d): |
| 60 |
# Original tetrahedron from UnitCubeMesh(1, 1, 1) |
| 61 |
return Cell(mesh3d, 0) |
| 62 |
|
| 63 |
|
| 64 |
@fixture |
| 65 |
def c1(mesh3d): |
| 66 |
# Degenerate cell |
| 67 |
return Cell(mesh3d, 1) |
| 68 |
|
| 69 |
|
| 70 |
@fixture |
| 71 |
def c5(mesh3d): |
| 72 |
# Regular tetrahedron with edge sqrt(2) |
| 73 |
return Cell(mesh3d, 5) |
| 74 |
|
| 75 |
|
| 76 |
@fixture |
| 77 |
def interval(): |
| 78 |
return UnitIntervalMesh(10) |
| 79 |
|
| 80 |
|
| 81 |
@fixture |
| 82 |
def square(): |
| 83 |
return UnitSquareMesh(5, 5) |
| 84 |
|
| 85 |
|
| 86 |
@fixture |
| 87 |
def rectangle(): |
| 88 |
return RectangleMesh(Point(0, 0), Point(2, 2), 5, 5) |
| 89 |
|
| 90 |
|
| 91 |
@fixture |
| 92 |
def cube(): |
| 93 |
return UnitCubeMesh(3, 3, 3) |
| 94 |
|
| 95 |
|
| 96 |
@fixture |
| 97 |
def box(): |
| 98 |
return BoxMesh(Point(0, 0, 0), Point(2, 2, 2), 2, 2, 5) |
| 99 |
|
| 100 |
|
| 101 |
@fixture |
| 102 |
def mesh(): |
| 103 |
return UnitSquareMesh(3, 3) |
| 104 |
|
| 105 |
|
| 106 |
@fixture |
| 107 |
def f(mesh): |
| 108 |
return MeshFunction('int', mesh, 0) |
| 109 |
|
| 110 |
|
| 111 |
def test_UFLCell(interval, square, rectangle, cube, box): |
| 112 |
import ufl_legacy as ufl |
| 113 |
assert ufl.interval == interval.ufl_cell() |
| 114 |
assert ufl.triangle == square.ufl_cell() |
| 115 |
assert ufl.triangle == rectangle.ufl_cell() |
| 116 |
assert ufl.tetrahedron == cube.ufl_cell() |
| 117 |
assert ufl.tetrahedron == box.ufl_cell() |
| 118 |
|
| 119 |
|
| 120 |
def test_UFLDomain(interval, square, rectangle, cube, box): |
| 121 |
import ufl_legacy as ufl |
| 122 |
|
| 123 |
def _check_ufl_domain(mesh): |
| 124 |
domain = mesh.ufl_domain() |
| 125 |
assert mesh.geometry().dim() == domain.geometric_dimension() |
| 126 |
assert mesh.topology().dim() == domain.topological_dimension() |
| 127 |
assert mesh.ufl_cell() == domain.ufl_cell() |
| 128 |
assert mesh.id() == domain.ufl_id() |
| 129 |
|
| 130 |
_check_ufl_domain(interval) |
| 131 |
_check_ufl_domain(square) |
| 132 |
_check_ufl_domain(rectangle) |
| 133 |
_check_ufl_domain(cube) |
| 134 |
_check_ufl_domain(box) |
| 135 |
|
| 136 |
|
| 137 |
def test_UnitSquareMesh(): |
| 138 |
"""Create mesh of unit square.""" |
| 139 |
mesh = UnitSquareMesh(5, 7) |
| 140 |
assert mesh.num_entities_global(0) == 48 |
| 141 |
assert mesh.num_entities_global(2) == 70 |
| 142 |
|
| 143 |
|
| 144 |
def test_UnitSquareMeshDistributed(): |
| 145 |
"""Create mesh of unit square.""" |
| 146 |
mesh = UnitSquareMesh(MPI.comm_world, 5, 7) |
| 147 |
assert mesh.num_entities_global(0) == 48 |
| 148 |
assert mesh.num_entities_global(2) == 70 |
| 149 |
|
| 150 |
|
| 151 |
def test_UnitSquareMeshLocal(): |
| 152 |
"""Create mesh of unit square.""" |
| 153 |
mesh = UnitSquareMesh(MPI.comm_self, 5, 7) |
| 154 |
assert mesh.num_vertices() == 48 |
| 155 |
assert mesh.num_cells() == 70 |
| 156 |
|
| 157 |
|
| 158 |
def test_UnitCubeMesh(): |
| 159 |
"""Create mesh of unit cube.""" |
| 160 |
mesh = UnitCubeMesh(5, 7, 9) |
| 161 |
assert mesh.num_entities_global(0) == 480 |
| 162 |
assert mesh.num_entities_global(3) == 1890 |
| 163 |
|
| 164 |
|
| 165 |
def test_UnitCubeMeshDistributed(): |
| 166 |
"""Create mesh of unit cube.""" |
| 167 |
mesh = UnitCubeMesh(MPI.comm_world, 5, 7, 9) |
| 168 |
assert mesh.num_entities_global(0) == 480 |
| 169 |
assert mesh.num_entities_global(3) == 1890 |
| 170 |
|
| 171 |
|
| 172 |
def test_UnitCubeMeshDistributedLocal(): |
| 173 |
"""Create mesh of unit cube.""" |
| 174 |
mesh = UnitCubeMesh(MPI.comm_self, 5, 7, 9) |
| 175 |
assert mesh.num_vertices() == 480 |
| 176 |
assert mesh.num_cells() == 1890 |
| 177 |
|
| 178 |
|
| 179 |
def test_UnitQuadMesh(): |
| 180 |
mesh = UnitSquareMesh.create(5, 7, CellType.Type.quadrilateral) |
| 181 |
assert mesh.num_entities_global(0) == 48 |
| 182 |
assert mesh.num_entities_global(2) == 35 |
| 183 |
|
| 184 |
|
| 185 |
def test_RectangleMeshQuadCreateDistributed(): |
| 186 |
mesh = RectangleMesh.create(MPI.comm_world, [Point(0, 0), Point(1, 1)], [5, 7], CellType.Type.quadrilateral) |
| 187 |
assert mesh.num_entities_global(0) == 48 |
| 188 |
assert mesh.num_entities_global(2) == 35 |
| 189 |
|
| 190 |
|
| 191 |
def test_RectangleMeshQuadCreateDistributedLocal(): |
| 192 |
mesh = RectangleMesh.create(MPI.comm_self, [Point(0, 0), Point(1, 1)], [5, 7], CellType.Type.quadrilateral) |
| 193 |
assert mesh.num_entities(0) == 48 |
| 194 |
assert mesh.num_entities(2) == 35 |
| 195 |
|
| 196 |
|
| 197 |
def test_UnitHexMesh(): |
| 198 |
mesh = UnitCubeMesh.create(5, 7, 9, CellType.Type.hexahedron) |
| 199 |
assert mesh.num_entities_global(0) == 480 |
| 200 |
assert mesh.num_entities_global(3) == 315 |
| 201 |
|
| 202 |
|
| 203 |
def test_BoxMeshHexCreateDistributed(): |
| 204 |
mesh = BoxMesh.create(MPI.comm_world, [Point(0, 0, 0), Point(1, 1, 1)], [5, 7, 9], CellType.Type.hexahedron) |
| 205 |
assert mesh.num_entities_global(0) == 480 |
| 206 |
assert mesh.num_entities_global(3) == 315 |
| 207 |
|
| 208 |
|
| 209 |
def test_BoxMeshHexCreateDistributedLocal(): |
| 210 |
mesh = BoxMesh.create(MPI.comm_self, [Point(0, 0, 0), Point(1, 1, 1)], [5, 7, 9], CellType.Type.hexahedron) |
| 211 |
assert mesh.num_entities(0) == 480 |
| 212 |
assert mesh.num_entities(3) == 315 |
| 213 |
|
| 214 |
|
| 215 |
def test_RefineUnitIntervalMesh(): |
| 216 |
"""Refine mesh of unit interval.""" |
| 217 |
mesh = UnitIntervalMesh(20) |
| 218 |
cell_markers = MeshFunction("bool", mesh, mesh.topology().dim(), False) |
| 219 |
cell_markers[0] = (MPI.rank(mesh.mpi_comm()) == 0) |
| 220 |
mesh2 = refine(mesh, cell_markers) |
| 221 |
assert mesh2.num_entities_global(0) == 22 |
| 222 |
assert mesh2.num_entities_global(1) == 21 |
| 223 |
|
| 224 |
|
| 225 |
def test_RefineUnitSquareMesh(): |
| 226 |
"""Refine mesh of unit square.""" |
| 227 |
mesh = UnitSquareMesh(5, 7) |
| 228 |
mesh = refine(mesh) |
| 229 |
assert mesh.num_entities_global(0) == 165 |
| 230 |
assert mesh.num_entities_global(2) == 280 |
| 231 |
|
| 232 |
|
| 233 |
def test_RefineUnitCubeMesh(): |
| 234 |
"""Refine mesh of unit cube.""" |
| 235 |
mesh = UnitCubeMesh(5, 7, 9) |
| 236 |
mesh = refine(mesh) |
| 237 |
assert mesh.num_entities_global(0) == 3135 |
| 238 |
assert mesh.num_entities_global(3) == 15120 |
| 239 |
|
| 240 |
|
| 241 |
def test_P_RefineUnitSquareMesh(): |
| 242 |
mesh = UnitSquareMesh(5, 7) |
| 243 |
mesh = p_refine(mesh) |
| 244 |
assert mesh.geometry().degree() == 2 |
| 245 |
|
| 246 |
coords_per_dim = (1, 1, 0) |
| 247 |
for d in range(3): |
| 248 |
assert mesh.geometry().num_entity_coordinates(d) == coords_per_dim[d] |
| 249 |
|
| 250 |
e_idx = mesh.geometry().get_entity_index(1, 0, 0) |
| 251 |
coord = numpy.array((0.1, 0.2), dtype=numpy.double) |
| 252 |
mesh.coordinates()[e_idx,:] = coord |
| 253 |
assert all(mesh.coordinates()[e_idx,:] == coord) |
| 254 |
|
| 255 |
|
| 256 |
def test_P_RefineUnitCubeMesh(): |
| 257 |
mesh = UnitCubeMesh(5, 7, 9) |
| 258 |
mesh = p_refine(mesh) |
| 259 |
assert mesh.geometry().degree() == 2 |
| 260 |
|
| 261 |
coords_per_dim = (1, 1, 0, 0) |
| 262 |
for d in range(4): |
| 263 |
assert mesh.geometry().num_entity_coordinates(d) == coords_per_dim[d] |
| 264 |
|
| 265 |
e_idx = mesh.geometry().get_entity_index(1, 0, 0) |
| 266 |
coord = numpy.array((0.1, 0.2, 0.3), dtype=numpy.double) |
| 267 |
mesh.coordinates()[e_idx,:] = coord |
| 268 |
assert all(mesh.coordinates()[e_idx,:] == coord) |
| 269 |
|
| 270 |
|
| 271 |
def test_BoundaryComputation(): |
| 272 |
"""Compute boundary of mesh.""" |
| 273 |
mesh = UnitCubeMesh(2, 2, 2) |
| 274 |
boundary = BoundaryMesh(mesh, "exterior") |
| 275 |
assert boundary.num_entities_global(0) == 26 |
| 276 |
assert boundary.num_entities_global(2) == 48 |
| 277 |
|
| 278 |
|
| 279 |
@xfail_in_parallel |
| 280 |
def test_BoundaryBoundary(): |
| 281 |
"""Compute boundary of boundary.""" |
| 282 |
mesh = UnitCubeMesh(2, 2, 2) |
| 283 |
b0 = BoundaryMesh(mesh, "exterior") |
| 284 |
b1 = BoundaryMesh(b0, "exterior") |
| 285 |
assert b1.num_vertices() == 0 |
| 286 |
assert b1.num_cells() == 0 |
| 287 |
|
| 288 |
|
| 289 |
@skip_in_parallel |
| 290 |
def test_Assign(mesh, f): |
| 291 |
"""Assign value of mesh function.""" |
| 292 |
f = f |
| 293 |
f[3] = 10 |
| 294 |
v = Vertex(mesh, 3) |
| 295 |
assert f[v] == 10 |
| 296 |
|
| 297 |
|
| 298 |
@skip_in_parallel |
| 299 |
def test_Write(cd_tempdir, f): |
| 300 |
"""Construct and save a simple meshfunction.""" |
| 301 |
f = f |
| 302 |
f[0] = 1 |
| 303 |
f[1] = 2 |
| 304 |
file = File("saved_mesh_function.xml") |
| 305 |
file << f |
| 306 |
|
| 307 |
|
| 308 |
@skip_in_parallel |
| 309 |
def test_Read(cd_tempdir): |
| 310 |
"""Construct and save a simple meshfunction. Then read it back from |
| 311 |
file.""" |
| 312 |
# mf = mesh.data().create_mesh_function("mesh_data_function", 2) |
| 313 |
# print "***************", mf |
| 314 |
# mf[0] = 3 |
| 315 |
# mf[1] = 4 |
| 316 |
|
| 317 |
# f[0] = 1 |
| 318 |
# f[1] = 2 |
| 319 |
# file = File("saved_mesh_function.xml") |
| 320 |
# file << f |
| 321 |
# f = MeshFunction('int', mesh, "saved_mesh_function.xml") |
| 322 |
# assert all(f.array() == f.array()) |
| 323 |
|
| 324 |
|
| 325 |
def test_hash(): |
| 326 |
h1 = UnitSquareMesh(4, 4).hash() |
| 327 |
h2 = UnitSquareMesh(4, 5).hash() |
| 328 |
h3 = UnitSquareMesh(4, 4).hash() |
| 329 |
assert h1 == h3 |
| 330 |
assert h1 != h2 |
| 331 |
|
| 332 |
|
| 333 |
@skip_in_parallel |
| 334 |
def test_SubsetIterators(mesh): |
| 335 |
def inside1(x): |
| 336 |
return x[0] <= 0.5 |
| 337 |
|
| 338 |
def inside2(x): |
| 339 |
return x[0] >= 0.5 |
| 340 |
sd1 = AutoSubDomain(inside1) |
| 341 |
sd2 = AutoSubDomain(inside2) |
| 342 |
cf = MeshFunction('size_t', mesh, mesh.topology().dim()) |
| 343 |
cf.set_all(0) |
| 344 |
sd1.mark(cf, 1) |
| 345 |
sd2.mark(cf, 2) |
| 346 |
|
| 347 |
for i in range(3): |
| 348 |
num = 0 |
| 349 |
for e in SubsetIterator(cf, i): |
| 350 |
num += 1 |
| 351 |
assert num == 6 |
| 352 |
|
| 353 |
|
| 354 |
# FIXME: Mesh IO tests should be in io test directory |
| 355 |
@skip_in_parallel |
| 356 |
def test_MeshXML2D(cd_tempdir): |
| 357 |
"""Write and read 2D mesh to/from file""" |
| 358 |
mesh_out = UnitSquareMesh(3, 3) |
| 359 |
mesh_in = Mesh() |
| 360 |
file = File("unitsquare.xml") |
| 361 |
file << mesh_out |
| 362 |
file >> mesh_in |
| 363 |
assert mesh_in.num_vertices() == 16 |
| 364 |
|
| 365 |
|
| 366 |
@skip_in_parallel |
| 367 |
def test_MeshXML3D(cd_tempdir): |
| 368 |
"""Write and read 3D mesh to/from file""" |
| 369 |
mesh_out = UnitCubeMesh(3, 3, 3) |
| 370 |
mesh_in = Mesh() |
| 371 |
file = File("unitcube.xml") |
| 372 |
file << mesh_out |
| 373 |
file >> mesh_in |
| 374 |
assert mesh_in.num_vertices() == 64 |
| 375 |
|
| 376 |
|
| 377 |
@skip_in_parallel |
| 378 |
def xtest_MeshFunction(cd_tempdir): |
| 379 |
"""Write and read mesh function to/from file""" |
| 380 |
mesh = UnitSquareMesh(1, 1) |
| 381 |
f = MeshFunction('int', mesh, 0) |
| 382 |
f[0] = 2 |
| 383 |
f[1] = 4 |
| 384 |
f[2] = 6 |
| 385 |
f[3] = 8 |
| 386 |
file = File("meshfunction.xml") |
| 387 |
file << f |
| 388 |
g = MeshFunction('int', mesh, 0) |
| 389 |
file >> g |
| 390 |
for v in vertices(mesh): |
| 391 |
assert f[v] == g[v] |
| 392 |
|
| 393 |
|
| 394 |
def test_GetGeometricalDimension(): |
| 395 |
"""Get geometrical dimension of mesh""" |
| 396 |
mesh = UnitSquareMesh(5, 5) |
| 397 |
assert mesh.geometry().dim() == 2 |
| 398 |
|
| 399 |
|
| 400 |
@skip_in_parallel |
| 401 |
def test_GetCoordinates(): |
| 402 |
"""Get coordinates of vertices""" |
| 403 |
mesh = UnitSquareMesh(5, 5) |
| 404 |
assert len(mesh.coordinates()) == 36 |
| 405 |
|
| 406 |
|
| 407 |
def test_GetCells(): |
| 408 |
"""Get cells of mesh""" |
| 409 |
mesh = UnitSquareMesh(5, 5) |
| 410 |
assert MPI.sum(mesh.mpi_comm(), len(mesh.cells())) == 50 |
| 411 |
|
| 412 |
|
| 413 |
@skip_in_parallel |
| 414 |
def test_cell_inradius(c0, c1, c5): |
| 415 |
assert round(c0.inradius() - (3.0-sqrt(3.0))/6.0, 7) == 0 |
| 416 |
assert round(c1.inradius() - 0.0, 7) == 0 |
| 417 |
assert round(c5.inradius() - sqrt(3.0)/6.0, 7) == 0 |
| 418 |
|
| 419 |
|
| 420 |
@skip_in_parallel |
| 421 |
def test_cell_circumradius(c0, c1, c5): |
| 422 |
from math import isnan |
| 423 |
assert round(c0.circumradius() - sqrt(3.0)/2.0, 7) == 0 |
| 424 |
# Implementation of diameter() does not work accurately |
| 425 |
# for degenerate cells - sometimes yields NaN |
| 426 |
assert isnan(c1.circumradius()) |
| 427 |
assert round(c5.circumradius() - sqrt(3.0)/2.0, 7) == 0 |
| 428 |
|
| 429 |
|
| 430 |
@skip_in_parallel |
| 431 |
def test_cell_h(c0, c1, c5): |
| 432 |
from math import isnan |
| 433 |
assert round(c0.h() - sqrt(2.0), 7) == 0 |
| 434 |
assert round(c1.h() - sqrt(2.0), 7) == 0 |
| 435 |
assert round(c5.h() - sqrt(2.0), 7) == 0 |
| 436 |
|
| 437 |
|
| 438 |
@skip_in_parallel |
| 439 |
def test_cell_radius_ratio(c0, c1, c5): |
| 440 |
assert round(c0.radius_ratio() - sqrt(3.0) + 1.0, 7) == 0 |
| 441 |
assert round(c1.radius_ratio() - 0.0, 7) == 0 |
| 442 |
assert round(c5.radius_ratio() - 1.0, 7) == 0 |
| 443 |
|
| 444 |
|
| 445 |
@skip_in_parallel |
| 446 |
def xtest_hmin_hmax(mesh1d, mesh2d, mesh3d): |
| 447 |
assert round(mesh1d.hmin() - 0.0, 7) == 0 |
| 448 |
assert round(mesh1d.hmax() - 0.25, 7) == 0 |
| 449 |
assert round(mesh2d.hmin() - sqrt(2.0), 7) == 0 |
| 450 |
assert round(mesh2d.hmax() - 2.0*sqrt(6.0)/3.0, 7) == 0 |
| 451 |
# nans are not taken into account in hmax and hmin |
| 452 |
assert round(mesh3d.hmin() - sqrt(3.0), 7) == 0 |
| 453 |
assert round(mesh3d.hmax() - sqrt(3.0), 7) == 0 |
| 454 |
|
| 455 |
|
| 456 |
@skip_in_parallel |
| 457 |
def test_rmin_rmax(mesh1d, mesh2d, mesh3d): |
| 458 |
assert round(mesh1d.rmin() - 0.0, 7) == 0 |
| 459 |
assert round(mesh1d.rmax() - 0.125, 7) == 0 |
| 460 |
assert round(mesh2d.rmin() - 1.0/(2.0+sqrt(2.0)), 7) == 0 |
| 461 |
assert round(mesh2d.rmax() - sqrt(6.0)/6.0, 7) == 0 |
| 462 |
assert round(mesh3d.rmin() - 0.0, 7) == 0 |
| 463 |
assert round(mesh3d.rmax() - sqrt(3.0)/6.0, 7) == 0 |
| 464 |
|
| 465 |
|
| 466 |
def test_basic_cell_orientations(): |
| 467 |
"Test that default cell orientations initialize and update as expected." |
| 468 |
mesh = UnitIntervalMesh(12) |
| 469 |
orientations = mesh.cell_orientations() |
| 470 |
print(len(orientations)) |
| 471 |
assert len(orientations) == 0 |
| 472 |
|
| 473 |
mesh.init_cell_orientations(Expression(("0.0", "1.0", "0.0"), degree=0)) |
| 474 |
orientations = mesh.cell_orientations() |
| 475 |
assert len(orientations) == mesh.num_cells() |
| 476 |
for i in range(mesh.num_cells()): |
| 477 |
assert mesh.cell_orientations()[i] == 0 |
| 478 |
|
| 479 |
|
| 480 |
@skip_in_parallel |
| 481 |
def test_cell_orientations(): |
| 482 |
"Test that cell orientations update as expected." |
| 483 |
mesh = UnitIntervalMesh(12) |
| 484 |
mesh.init_cell_orientations(Expression(("0.0", "1.0", "0.0"), degree=0)) |
| 485 |
for i in range(mesh.num_cells()): |
| 486 |
assert mesh.cell_orientations()[i] == 0 |
| 487 |
|
| 488 |
mesh = UnitSquareMesh(2, 2) |
| 489 |
mesh.init_cell_orientations(Expression(("0.0", "0.0", "1.0"), degree=0)) |
| 490 |
reference = numpy.array((0, 1, 0, 1, 0, 1, 0, 1)) |
| 491 |
# Only compare against reference in serial (don't know how to |
| 492 |
# compare in parallel) |
| 493 |
for i in range(mesh.num_cells()): |
| 494 |
assert mesh.cell_orientations()[i] == reference[i] |
| 495 |
|
| 496 |
mesh = BoundaryMesh(UnitSquareMesh(2, 2), "exterior") |
| 497 |
mesh.init_cell_orientations(Expression(("x[0]", "x[1]", "x[2]"), degree=1)) |
| 498 |
print(mesh.cell_orientations()) |
| 499 |
|
| 500 |
|
| 501 |
# - Facilities to run tests on combination of meshes |
| 502 |
|
| 503 |
ghost_mode = set_parameters_fixture("ghost_mode", [ |
| 504 |
"none", |
| 505 |
"shared_facet", |
| 506 |
"shared_vertex", |
| 507 |
]) |
| 508 |
|
| 509 |
def _create_two_quads(good): |
| 510 |
"""Create mesh with two quads either with good or |
| 511 |
bad ordering according to bool parameter |
| 512 |
""" |
| 513 |
mesh = Mesh() |
| 514 |
e = MeshEditor() |
| 515 |
e.open(mesh, "quadrilateral", 2, 2, degree=1) |
| 516 |
e.init_vertices(6) |
| 517 |
e.init_cells(2) |
| 518 |
e.add_vertex(0, Point(0, 0)) |
| 519 |
e.add_vertex(1, Point(1, 0)) |
| 520 |
e.add_vertex(2, Point(2, 0)) |
| 521 |
e.add_vertex(3, Point(0, 1)) |
| 522 |
e.add_vertex(4, Point(1, 1)) |
| 523 |
e.add_vertex(5, Point(2, 1)) |
| 524 |
e.add_cell(0, [0, 1, 3, 4]) |
| 525 |
if good: |
| 526 |
e.add_cell(1, [1, 2, 4, 5]) |
| 527 |
else: |
| 528 |
e.add_cell(1, [4, 5, 1, 2]) |
| 529 |
e.close() |
| 530 |
return mesh |
| 531 |
|
| 532 |
def create_two_good_quads(): |
| 533 |
"""Create good mesh with two quads""" |
| 534 |
return _create_two_quads(True) |
| 535 |
|
| 536 |
def create_two_bad_quads(): |
| 537 |
"""Create mesh with two badly ordered quads""" |
| 538 |
return _create_two_quads(False) |
| 539 |
|
| 540 |
mesh_factories = [ |
| 541 |
(UnitIntervalMesh, (8,)), |
| 542 |
(UnitSquareMesh, (4, 4)), |
| 543 |
(UnitDiscMesh.create, (MPI.comm_world, 10, 1, 2)), |
| 544 |
(UnitDiscMesh.create, (MPI.comm_world, 10, 2, 2)), |
| 545 |
(UnitDiscMesh.create, (MPI.comm_world, 10, 1, 3)), |
| 546 |
(UnitDiscMesh.create, (MPI.comm_world, 10, 2, 3)), |
| 547 |
(SphericalShellMesh.create, (MPI.comm_world, 1,)), |
| 548 |
(SphericalShellMesh.create, (MPI.comm_world, 2,)), |
| 549 |
(UnitCubeMesh, (2, 2, 2)), |
| 550 |
(UnitSquareMesh.create, (4, 4, CellType.Type.quadrilateral)), |
| 551 |
(UnitCubeMesh.create, (2, 2, 2, CellType.Type.hexahedron)), |
| 552 |
(create_two_good_quads, ()), |
| 553 |
# FIXME: Add mechanism for testing meshes coming from IO |
| 554 |
] |
| 555 |
|
| 556 |
mesh_factories_broken_shared_entities = [ |
| 557 |
(UnitIntervalMesh, (8,)), |
| 558 |
(UnitSquareMesh, (4, 4)), |
| 559 |
# FIXME: Problem in test_shared_entities |
| 560 |
pytest.param(((UnitDiscMesh.create, (MPI.comm_world, 10, 1, 2))), marks=xfail_in_parallel), |
| 561 |
pytest.param(((UnitDiscMesh.create, (MPI.comm_world, 10, 2, 2))), marks=xfail_in_parallel), |
| 562 |
pytest.param(((UnitDiscMesh.create, (MPI.comm_world, 10, 1, 3))), marks=xfail_in_parallel), |
| 563 |
pytest.param(((UnitDiscMesh.create, (MPI.comm_world, 10, 2, 3))), marks=xfail_in_parallel), |
| 564 |
pytest.param(((SphericalShellMesh.create, (MPI.comm_world, 1,))), marks=xfail_in_parallel), |
| 565 |
pytest.param(((SphericalShellMesh.create, (MPI.comm_world, 2,))), marks=xfail_in_parallel), |
| 566 |
(UnitCubeMesh, (2, 2, 2)), |
| 567 |
(UnitSquareMesh.create, (4, 4, CellType.Type.quadrilateral)), |
| 568 |
(UnitCubeMesh.create, (2, 2, 2, CellType.Type.hexahedron)), |
| 569 |
pytest.param(((create_two_good_quads, ())), marks=xfail_in_parallel), |
| 570 |
] |
| 571 |
|
| 572 |
# FIXME: Fix this xfail |
| 573 |
def xfail_ghosted_quads_hexes(mesh_factory, ghost_mode): |
| 574 |
"""Xfail when mesh_factory on quads/hexes uses |
| 575 |
shared_vertex mode. Needs implementing. |
| 576 |
""" |
| 577 |
if mesh_factory in [UnitSquareMesh.create, UnitCubeMesh.create]: |
| 578 |
if ghost_mode == 'shared_vertex': |
| 579 |
pytest.xfail(reason="Missing functionality in '{}' with '' " |
| 580 |
"mode".format(mesh_factory, ghost_mode)) |
| 581 |
|
| 582 |
|
| 583 |
def test_unorderable_cells(): |
| 584 |
"""Test that mesh with unordered quads raises: |
| 585 |
https://bitbucket.org/fenics-project/dolfin/issues/997/ |
| 586 |
""" |
| 587 |
with pytest.raises(RuntimeError): |
| 588 |
create_two_bad_quads() |
| 589 |
|
| 590 |
|
| 591 |
@pytest.mark.parametrize('mesh_factory', mesh_factories_broken_shared_entities) |
| 592 |
def test_shared_entities(mesh_factory, ghost_mode): |
| 593 |
func, args = mesh_factory |
| 594 |
xfail_ghosted_quads_hexes(func, ghost_mode) |
| 595 |
mesh = func(*args) |
| 596 |
dim = mesh.topology().dim() |
| 597 |
|
| 598 |
# FIXME: Implement a proper test |
| 599 |
for shared_dim in range(dim + 1): |
| 600 |
# Initialise global indices (if not already) |
| 601 |
mesh.init_global(shared_dim) |
| 602 |
|
| 603 |
assert isinstance(mesh.topology().shared_entities(shared_dim), dict) |
| 604 |
assert isinstance(mesh.topology().global_indices(shared_dim), |
| 605 |
numpy.ndarray) |
| 606 |
|
| 607 |
if mesh.topology().have_shared_entities(shared_dim): |
| 608 |
for e in entities(mesh, shared_dim): |
| 609 |
sharing = e.sharing_processes() |
| 610 |
assert isinstance(sharing, set) |
| 611 |
assert (len(sharing) > 0) == e.is_shared() |
| 612 |
|
| 613 |
n_entities = mesh.num_entities(shared_dim) |
| 614 |
n_global_entities = mesh.num_entities_global(shared_dim) |
| 615 |
shared_entities = mesh.topology().shared_entities(shared_dim) |
| 616 |
|
| 617 |
# Check that sum(local-shared) = global count |
| 618 |
rank = MPI.rank(mesh.mpi_comm()) |
| 619 |
ct = sum(1 for val in shared_entities.values() if list(val)[0] < rank) |
| 620 |
num_entities_global = MPI.sum(mesh.mpi_comm(), mesh.num_entities(shared_dim) - ct) |
| 621 |
|
| 622 |
assert num_entities_global == mesh.num_entities_global(shared_dim) |
| 623 |
|
| 624 |
|
| 625 |
@pytest.mark.parametrize('mesh_factory', mesh_factories) |
| 626 |
def test_mesh_topology_against_fiat(mesh_factory, ghost_mode): |
| 627 |
"""Test that mesh cells have topology matching to FIAT reference |
| 628 |
cell they were created from. |
| 629 |
""" |
| 630 |
func, args = mesh_factory |
| 631 |
xfail_ghosted_quads_hexes(func, ghost_mode) |
| 632 |
mesh = func(*args) |
| 633 |
assert mesh.ordered() |
| 634 |
tdim = mesh.topology().dim() |
| 635 |
|
| 636 |
# Create FIAT cell |
| 637 |
cell_name = CellType.type2string(mesh.type().cell_type()) |
| 638 |
fiat_cell = FIAT.ufc_cell(cell_name) |
| 639 |
|
| 640 |
# Initialize all mesh entities and connectivities |
| 641 |
mesh.init() |
| 642 |
|
| 643 |
for cell in cells(mesh): |
| 644 |
# Get mesh-global (MPI-local) indices of cell vertices |
| 645 |
vertex_global_indices = cell.entities(0) |
| 646 |
|
| 647 |
# Loop over all dimensions of reference cell topology |
| 648 |
for d, d_topology in fiat_cell.get_topology().items(): |
| 649 |
|
| 650 |
# Get entities of dimension d on the cell |
| 651 |
entities = cell.entities(d) |
| 652 |
if len(entities) == 0: # Fixup for highest dimension |
| 653 |
entities = (cell.index(),) |
| 654 |
|
| 655 |
# Loop over all entities of fixed dimension d |
| 656 |
for entity_index, entity_topology in d_topology.items(): |
| 657 |
|
| 658 |
# Check that entity vertices map to cell vertices in right order |
| 659 |
entity = MeshEntity(mesh, d, entities[entity_index]) |
| 660 |
entity_vertices = entity.entities(0) |
| 661 |
assert all(vertex_global_indices[numpy.array(entity_topology)] |
| 662 |
== entity_vertices) |
| 663 |
|
| 664 |
|
| 665 |
@pytest.mark.parametrize('mesh_factory', mesh_factories) |
| 666 |
def test_mesh_ufc_ordering(mesh_factory, ghost_mode): |
| 667 |
"""Test that DOLFIN follows that UFC standard in numbering |
| 668 |
mesh entities. See chapter 5 of UFC manual |
| 669 |
https://fenicsproject.org/pub/documents/ufc/ufc-user-manual/ufc-user-manual.pdf |
| 670 |
|
| 671 |
In fact, numbering of other mesh entities than vertices is |
| 672 |
not followed. |
| 673 |
""" |
| 674 |
func, args = mesh_factory |
| 675 |
xfail_ghosted_quads_hexes(func, ghost_mode) |
| 676 |
mesh = func(*args) |
| 677 |
assert mesh.ordered() |
| 678 |
tdim = mesh.topology().dim() |
| 679 |
|
| 680 |
# Loop over pair of dimensions d, d1 with d>d1 |
| 681 |
for d in range(tdim+1): |
| 682 |
for d1 in range(d): |
| 683 |
|
| 684 |
# NOTE: DOLFIN UFC noncompliance! |
| 685 |
# DOLFIN has increasing indices only for d-0 incidence |
| 686 |
# with any d; UFC convention for d-d1 with d>d1>0 is not |
| 687 |
# respected in DOLFIN |
| 688 |
if d1 != 0: |
| 689 |
continue |
| 690 |
|
| 691 |
# Initialize d-d1 connectivity and d1 global indices |
| 692 |
mesh.init(d, d1) |
| 693 |
mesh.init_global(d1) |
| 694 |
assert mesh.topology().have_global_indices(d1) |
| 695 |
|
| 696 |
# Loop over entities of dimension d |
| 697 |
for e in entities(mesh, d): |
| 698 |
|
| 699 |
# Get global indices |
| 700 |
subentities_indices = [e1.global_index() for e1 in entities(e, d1)] |
| 701 |
assert subentities_indices.count(-1) == 0 |
| 702 |
|
| 703 |
# Check that d1-subentities of d-entity have increasing indices |
| 704 |
assert sorted(subentities_indices) == subentities_indices |
| 705 |
|
| 706 |
|
| 707 |
def test_mesh_topology_reference(): |
| 708 |
"""Check that Mesh.topology() returns a reference rather |
| 709 |
than copy""" |
| 710 |
mesh = UnitSquareMesh(4, 4) |
| 711 |
assert mesh.topology().id() == mesh.topology().id() |
| 712 |
|
| 713 |
|
| 714 |
def test_mesh_topology_lifetime(): |
| 715 |
"""Check that lifetime of Mesh.topology() is bound to |
| 716 |
underlying mesh object""" |
| 717 |
mesh = UnitSquareMesh(4, 4) |
| 718 |
|
| 719 |
rc = sys.getrefcount(mesh) |
| 720 |
topology = mesh.topology() |
| 721 |
assert sys.getrefcount(mesh) == rc + 1 |
| 722 |
del topology |
| 723 |
assert sys.getrefcount(mesh) == rc |
| 724 |
|
| 725 |
|
| 726 |
def test_mesh_coordinates_lifetime(): |
| 727 |
"""Check that lifetime of Mesh.coordinates() is bound to |
| 728 |
underlying mesh object""" |
| 729 |
mesh = UnitSquareMesh(4, 4) |
| 730 |
|
| 731 |
rc = sys.getrefcount(mesh) |
| 732 |
coordinates = mesh.coordinates() |
| 733 |
assert sys.getrefcount(mesh) == rc + 1 |
| 734 |
del coordinates |
| 735 |
assert sys.getrefcount(mesh) == rc |
| 736 |
|
| 737 |
|
| 738 |
def test_mesh_connectivity_lifetime(): |
| 739 |
"""Check that lifetime of MeshConnectivity is bound to |
| 740 |
underlying mesh topology object""" |
| 741 |
mesh = UnitSquareMesh(4, 4) |
| 742 |
mesh.init(1, 2) |
| 743 |
topology = mesh.topology() |
| 744 |
|
| 745 |
# Refcount checks on the MeshConnectivity object |
| 746 |
rc = sys.getrefcount(topology) |
| 747 |
connectivity = topology(1, 2) |
| 748 |
assert sys.getrefcount(topology) == rc + 1 |
| 749 |
del connectivity |
| 750 |
assert sys.getrefcount(topology) == rc |
| 751 |
|
| 752 |
# Refcount checks on the returned connectivities array |
| 753 |
conn = topology(1, 2) |
| 754 |
rc = sys.getrefcount(conn) |
| 755 |
cells = conn(0) |
| 756 |
assert sys.getrefcount(conn) == rc + 1 |
| 757 |
del cells |
| 758 |
assert sys.getrefcount(conn) == rc |