| 1 |
#!/usr/bin/env py.test |
| 2 |
|
| 3 |
"""Unit tests for MeshView class""" |
| 4 |
|
| 5 |
# Copyright (C) 2011 Garth N. Wells |
| 6 |
# |
| 7 |
# This file is part of DOLFIN. |
| 8 |
# |
| 9 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 10 |
# it under the terms of the GNU Lesser General Public License as published by |
| 11 |
# the Free Software Foundation, either version 3 of the License, or |
| 12 |
# (at your option) any later version. |
| 13 |
# |
| 14 |
# DOLFIN is distributed in the hope that it will be useful, |
| 15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
# GNU Lesser General Public License for more details. |
| 18 |
# |
| 19 |
# You should have received a copy of the GNU Lesser General Public License |
| 20 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
# |
| 22 |
# First added: 2011-02-26 |
| 23 |
# Last changed: 2014-05-30 |
| 24 |
|
| 25 |
import pytest |
| 26 |
from dolfin import * |
| 27 |
from dolfin_utils.test import fixture, skip_in_parallel |
| 28 |
|
| 29 |
|
| 30 |
@fixture |
| 31 |
def cube(): |
| 32 |
return UnitCubeMesh(5, 5, 5) |
| 33 |
|
| 34 |
|
| 35 |
@fixture |
| 36 |
def square(): |
| 37 |
return UnitSquareMesh(5, 5) |
| 38 |
|
| 39 |
|
| 40 |
@pytest.fixture(scope='module', params=range(2)) |
| 41 |
def meshes(cube, square, request): |
| 42 |
mesh = [cube, square] |
| 43 |
return mesh[request.param] |
| 44 |
|
| 45 |
|
| 46 |
@skip_in_parallel |
| 47 |
def test_make_facets_view(square, cube): |
| 48 |
"""Create view of facets""" |
| 49 |
|
| 50 |
def _check_facets_view(mesh): |
| 51 |
marker = MeshFunction("size_t", mesh, mesh.topology().dim()-1, 0) |
| 52 |
D = mesh.topology().dim() |
| 53 |
mesh.init(D-1, D) |
| 54 |
|
| 55 |
c = 0 |
| 56 |
for f in facets(mesh): |
| 57 |
if f.num_global_entities(D) == 1: |
| 58 |
marker[f] = 1 |
| 59 |
c += 1 |
| 60 |
|
| 61 |
m2 = MeshView.create(marker, 1) |
| 62 |
|
| 63 |
assert m2.num_cells() == c |
| 64 |
assert m2.num_cells() == len(m2.topology().mapping()[mesh.id()].cell_map()) |
| 65 |
assert m2.num_vertices() == len(m2.topology().mapping()[mesh.id()].vertex_map()) |
| 66 |
|
| 67 |
_check_facets_view(square) |
| 68 |
_check_facets_view(cube) |
| 69 |
|
| 70 |
|
| 71 |
@skip_in_parallel |
| 72 |
def test_make_cells_view(square, cube): |
| 73 |
"""Create view of cells""" |
| 74 |
|
| 75 |
def _check_cells_view(mesh): |
| 76 |
marker = MeshFunction("size_t", mesh, mesh.topology().dim(), 0) |
| 77 |
|
| 78 |
ct = 0 |
| 79 |
for c in cells(mesh): |
| 80 |
if c.midpoint().x() < 0.5: |
| 81 |
marker[c] = 1 |
| 82 |
ct += 1 |
| 83 |
|
| 84 |
m2 = MeshView.create(marker, 1) |
| 85 |
|
| 86 |
assert m2.num_cells() == ct |
| 87 |
assert m2.num_cells() == len(m2.topology().mapping()[mesh.id()].cell_map()) |
| 88 |
assert m2.num_vertices() == len(m2.topology().mapping()[mesh.id()].vertex_map()) |
| 89 |
|
| 90 |
_check_cells_view(square) |
| 91 |
_check_cells_view(cube) |
| 92 |
|
| 93 |
|
| 94 |
@skip_in_parallel |
| 95 |
def test_make_edges_view(cube): |
| 96 |
"""Create view of edges""" |
| 97 |
|
| 98 |
marker = MeshFunction("size_t", cube, 1, 0) |
| 99 |
cube.init(1, 3) |
| 100 |
|
| 101 |
c = 0 |
| 102 |
for e in edges(cube): |
| 103 |
if (e.midpoint().x() == 0 or e.midpoint().x() == 1): |
| 104 |
marker[e] = 1 |
| 105 |
c += 1 |
| 106 |
elif (e.midpoint().y() == 0 or e.midpoint().y() == 1): |
| 107 |
marker[e] = 1 |
| 108 |
c += 1 |
| 109 |
elif (e.midpoint().z() == 0 or e.midpoint().z() == 1): |
| 110 |
marker[e] = 1 |
| 111 |
c += 1 |
| 112 |
|
| 113 |
m2 = MeshView.create(marker, 1) |
| 114 |
|
| 115 |
assert m2.num_cells() == c |
| 116 |
assert m2.num_cells() == len(m2.topology().mapping()[cube.id()].cell_map()) |
| 117 |
assert m2.num_vertices() == len(m2.topology().mapping()[cube.id()].vertex_map()) |