| 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 os |
| 21 |
import gc |
| 22 |
import pytest |
| 23 |
from dolfin import * |
| 24 |
from dolfin_utils.test import skip_in_parallel, datadir, fixture |
| 25 |
|
| 26 |
|
| 27 |
@pytest.mark.parametrize("MeshFunc", [ |
| 28 |
(UnitIntervalMesh, (10,)), |
| 29 |
(UnitSquareMesh, (10, 10)), |
| 30 |
(UnitCubeMesh, (10, 10, 10)), |
| 31 |
]) |
| 32 |
@skip_in_parallel |
| 33 |
def test_creation(MeshFunc): |
| 34 |
"""Create SubMesh.""" |
| 35 |
args = MeshFunc[1] |
| 36 |
MeshFunc = MeshFunc[0] |
| 37 |
mesh = MeshFunc(*args) |
| 38 |
dim_t = mesh.topology().dim() |
| 39 |
mesh.domains().init(dim_t) |
| 40 |
domains = MeshFunction("size_t", mesh, mesh.topology().dim(), 0) |
| 41 |
for cell in cells(mesh): |
| 42 |
# Mark half the cells |
| 43 |
if cell.index() > mesh.num_cells()/2: |
| 44 |
break |
| 45 |
domains[cell] = 1 |
| 46 |
mesh.domains().set_marker((cell.index(), 1), dim_t) |
| 47 |
|
| 48 |
# Create mesh from stored MeshValueCollection and |
| 49 |
# external MeshFunction |
| 50 |
smesh0 = SubMesh(mesh, 1) |
| 51 |
smesh1 = SubMesh(mesh, domains, 1) |
| 52 |
assert smesh0.num_cells() == smesh1.num_cells() |
| 53 |
assert smesh0.num_vertices() == smesh1.num_vertices() |
| 54 |
# Check that we create the same sub mesh with the same |
| 55 |
# MeshValueCollection |
| 56 |
for cell0, cell1 in zip(cells(smesh0), cells(smesh1)): |
| 57 |
assert cell0.index() == cell1.index() |
| 58 |
assert smesh0.domains().get_marker(cell0.index(), dim_t) == \ |
| 59 |
smesh1.domains().get_marker(cell1.index(), dim_t) |
| 60 |
|
| 61 |
# This test passed for unittest because it called SubMesh((mesh, 2)) |
| 62 |
# and not SubMesh(mesh, 2) |
| 63 |
# with pytest.raises(RuntimeError): |
| 64 |
# SubMesh(mesh, 2) |
| 65 |
|
| 66 |
mesh = MeshFunc(*args) |
| 67 |
with pytest.raises(RuntimeError): |
| 68 |
SubMesh(mesh, 1) |
| 69 |
|
| 70 |
|
| 71 |
@skip_in_parallel |
| 72 |
def test_facet_domain_propagation(datadir): |
| 73 |
|
| 74 |
# Boxes contains two subdomains with marked faces between |
| 75 |
# them. These faces are marked with 5, 10, 15. |
| 76 |
mesh = Mesh(os.path.join(datadir, "boxes.xml")) |
| 77 |
|
| 78 |
inner = SubMesh(mesh, 1) |
| 79 |
outer = SubMesh(mesh, 2) |
| 80 |
|
| 81 |
# Test dict interface |
| 82 |
D = mesh.topology().dim() - 1 |
| 83 |
parent_facets = mesh.domains().markers(D) |
| 84 |
inner_facets = inner.domains().markers(D) |
| 85 |
outer_facets = outer.domains().markers(D) |
| 86 |
|
| 87 |
for value in [5, 10, 15]: |
| 88 |
sum_parent = 0 |
| 89 |
sum_inner = 0 |
| 90 |
sum_outer = 0 |
| 91 |
for key, val in parent_facets.items(): |
| 92 |
if val == value: |
| 93 |
sum_parent += val |
| 94 |
for key, val in inner_facets.items(): |
| 95 |
if val == value: |
| 96 |
sum_inner += val |
| 97 |
for key, val in outer_facets.items(): |
| 98 |
if val == value: |
| 99 |
sum_outer += val |
| 100 |
|
| 101 |
assert sum_outer == sum_inner |
| 102 |
assert sum_outer == sum_parent |
| 103 |
|
| 104 |
# Test Meshfunction interface |
| 105 |
parent_facets = MeshFunction("size_t", mesh, D, mesh.domains()) |
| 106 |
inner_facets = MeshFunction("size_t", inner, D, inner.domains()) |
| 107 |
outer_facets = MeshFunction("size_t", outer, D, outer.domains()) |
| 108 |
|
| 109 |
# Check we have the same number of value-marked facets |
| 110 |
for value in [5, 10, 15]: |
| 111 |
assert ((inner_facets.array() == value).sum() == |
| 112 |
(outer_facets.array() == value).sum()) |
| 113 |
assert ((parent_facets.array() == value).sum() == |
| 114 |
(outer_facets.array() == value).sum()) |