| 1 |
""" Tests for the meshconvert module.""" |
| 2 |
|
| 3 |
# Copyright (C) 2012 |
| 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 |
# Modified by Jan Blechta 2012 |
| 21 |
# |
| 22 |
# First added: |
| 23 |
# Last changed: 2014-05-30 |
| 24 |
|
| 25 |
import pytest |
| 26 |
import os |
| 27 |
import glob |
| 28 |
import tempfile |
| 29 |
|
| 30 |
from dolfin_utils.meshconvert import meshconvert |
| 31 |
from dolfin_utils.meshconvert.meshconvert import DataHandler |
| 32 |
from dolfin import MPI |
| 33 |
from functools import reduce |
| 34 |
from dolfin_utils.test import skip_in_parallel |
| 35 |
|
| 36 |
|
| 37 |
class Tester: |
| 38 |
def assertTrue(self, a): |
| 39 |
assert a |
| 40 |
|
| 41 |
assert_ = assertTrue |
| 42 |
|
| 43 |
def assertFalse(self, a): |
| 44 |
assert not a |
| 45 |
|
| 46 |
def assertEqual(self, a, b): |
| 47 |
assert a == b |
| 48 |
|
| 49 |
def assertAlmostEqual(self, a, b): |
| 50 |
assert abs(a-b) < 1e-7 |
| 51 |
|
| 52 |
def assertNotEqual(self, a, b): |
| 53 |
assert a != b |
| 54 |
|
| 55 |
def assertIsInstance(self, obj, cls): |
| 56 |
assert isinstance(obj, cls) |
| 57 |
|
| 58 |
def assertNotIsInstance(self, obj, cls): |
| 59 |
assert not isinstance(obj, cls) |
| 60 |
|
| 61 |
def assertRaises(self, e, f, *args): |
| 62 |
if args==[]: |
| 63 |
with pytest.raises(e): |
| 64 |
f() |
| 65 |
elif len(args)==1: |
| 66 |
with pytest.raises(e): |
| 67 |
f(args[0]) |
| 68 |
elif len(args)==2: |
| 69 |
with pytest.raises(e): |
| 70 |
f(args[0], args[1]) |
| 71 |
|
| 72 |
def assertEqualValues(self, A, B): |
| 73 |
B = as_ufl(B) |
| 74 |
self.assertEqual(A.ufl_shape, B.ufl_shape) |
| 75 |
self.assertEqual(inner(A-B, A-B)(None), 0) |
| 76 |
|
| 77 |
|
| 78 |
class TestCase(Tester): |
| 79 |
def _get_tempfname(self, suffix=None): |
| 80 |
fd, fname = tempfile.mkstemp(suffix=suffix) |
| 81 |
os.close(fd) |
| 82 |
return fname |
| 83 |
|
| 84 |
class _TestHandler(DataHandler): |
| 85 |
def __init__(self, cell_type, mesh_dim, test): |
| 86 |
DataHandler.__init__(self) |
| 87 |
self.vertices, self.cells, self.functions = [], [], {} |
| 88 |
self.vertices_ended = self.cells_ended = self.closed = False |
| 89 |
self.__type, self.__dim = cell_type, mesh_dim |
| 90 |
self.__test = test |
| 91 |
test.assertEqual(self._state, self.State_Invalid) |
| 92 |
self.test_warnings = [] |
| 93 |
|
| 94 |
def set_mesh_type(self, *args): |
| 95 |
DataHandler.set_mesh_type(self, *args) |
| 96 |
test = self.__test |
| 97 |
test.assertEqual(self._state, self.State_Init) |
| 98 |
test.assertEqual(self._cell_type, self.__type) |
| 99 |
test.assertEqual(self._dim, self.__dim) |
| 100 |
|
| 101 |
def start_vertices(self, num_vertices): |
| 102 |
DataHandler.start_vertices(self, num_vertices) |
| 103 |
self.vertices = [] |
| 104 |
for i in range(num_vertices): |
| 105 |
self.vertices.append(None) |
| 106 |
|
| 107 |
def add_vertex(self, vertex, coords): |
| 108 |
DataHandler.add_vertex(self, vertex, coords) |
| 109 |
self.vertices[vertex] = tuple(coords) |
| 110 |
|
| 111 |
def end_vertices(self): |
| 112 |
DataHandler.end_vertices(self) |
| 113 |
self.vertices_ended = True |
| 114 |
|
| 115 |
def start_cells(self, num_cells): |
| 116 |
DataHandler.start_cells(self, num_cells) |
| 117 |
for i in range(num_cells): |
| 118 |
self.cells.append(None) |
| 119 |
|
| 120 |
def add_cell(self, cell, nodes): |
| 121 |
DataHandler.add_cell(self, cell, nodes) |
| 122 |
self.cells[cell] = tuple(nodes) |
| 123 |
|
| 124 |
def end_cells(self): |
| 125 |
DataHandler.end_cells(self) |
| 126 |
self.cells_ended = True |
| 127 |
|
| 128 |
def start_meshfunction(self, name, dim, sz): |
| 129 |
DataHandler.start_meshfunction(self, name, dim, sz) |
| 130 |
entries = [] |
| 131 |
for i in range(sz): |
| 132 |
entries.append(None) |
| 133 |
self.__curfunc = self.functions[name] = [dim, sz, entries, False] |
| 134 |
|
| 135 |
def add_entity_meshfunction(self, index, value): |
| 136 |
DataHandler.add_entity_meshfunction(self, index, value) |
| 137 |
self.__curfunc[2][index] = value |
| 138 |
|
| 139 |
def end_meshfunction(self): |
| 140 |
DataHandler.end_meshfunction(self) |
| 141 |
self.__curfunc[-1] = True |
| 142 |
|
| 143 |
def warn(self, msg): |
| 144 |
self.test_warnings.append(msg) |
| 145 |
|
| 146 |
def close(self): |
| 147 |
DataHandler.close(self) |
| 148 |
self.__test.assertEqual(self._state, self.State_Invalid) |
| 149 |
self.closed = True |
| 150 |
|
| 151 |
class _ConverterTest(TestCase): |
| 152 |
""" Base converter test class. |
| 153 |
""" |
| 154 |
|
| 155 |
class AbaqusTest(_ConverterTest): |
| 156 |
""" Test AbaqusConverter.""" |
| 157 |
|
| 158 |
def test_success(self): |
| 159 |
""" Test success case. |
| 160 |
""" |
| 161 |
handler = self.__convert("abaqus.inp") |
| 162 |
# Verify vertices |
| 163 |
self.assertEqual(handler.vertices, [ |
| 164 |
(0.0, 0.0, 0.0), |
| 165 |
(10.0, 10.0, 10.0), |
| 166 |
(10.0, 10.0, 11.0), |
| 167 |
(0.0, 1.0, 0.0), |
| 168 |
(1.0, 1.0, 0.0), |
| 169 |
(0.0, 0.0, 0.0), |
| 170 |
(10.0, 10.0, 11.0), |
| 171 |
(10.0, 10.0, 10.0), |
| 172 |
(1.0, 1.0, 0.0), |
| 173 |
(1.0, 0.0, 0.0), |
| 174 |
(1.0, 0.0, 0.0), |
| 175 |
(0.0, 1.0, 0.0) |
| 176 |
]) |
| 177 |
self.assert_(handler.vertices_ended) |
| 178 |
|
| 179 |
# Verify cells |
| 180 |
self.assertEqual(handler.cells, [ |
| 181 |
(0, 9, 3, 8), |
| 182 |
(0, 9, 1, 2), |
| 183 |
(3, 8, 1, 2) |
| 184 |
]) |
| 185 |
self.assert_(handler.cells_ended) |
| 186 |
|
| 187 |
# Verify materials |
| 188 |
print(list(handler.functions.keys())) |
| 189 |
#self.assertEqual(handler.functions.keys(), ["material"]) |
| 190 |
#dim, sz, entries, ended = handler.functions["material"] |
| 191 |
#self.assertEqual(dim, 3) |
| 192 |
#self.assertEqual(sz, 2) |
| 193 |
## Cell 0 should have material 0, cell 1 material 1 |
| 194 |
#self.assertEqual(entries, [0, 1]) |
| 195 |
#self.assert_(ended) |
| 196 |
|
| 197 |
self.assert_(handler.closed) |
| 198 |
|
| 199 |
def xtest_error(self): |
| 200 |
""" Test various cases of erroneus input. |
| 201 |
""" |
| 202 |
def convert(fname, text, error=False): |
| 203 |
f = file(fname, "w") |
| 204 |
f.write(text) |
| 205 |
f.close() |
| 206 |
if not error: |
| 207 |
handler = self.__convert(fname) |
| 208 |
self.assert_(handler.test_warnings) |
| 209 |
else: |
| 210 |
self.assertRaises(meshconvert.ParseError, self.__convert, fname) |
| 211 |
os.remove(fname) |
| 212 |
|
| 213 |
fname = self._get_tempfname(suffix=".inp") |
| 214 |
|
| 215 |
# Too few coordinates |
| 216 |
# convert(fname, """*NODE |
| 217 |
#1, 0, 0""") |
| 218 |
# Non-numeric index |
| 219 |
# convert(fname, """*NODE |
| 220 |
#a, 0, 0, 0""") |
| 221 |
# Non-numeric coordinate |
| 222 |
# convert(fname, """*NODE |
| 223 |
#1, 0, 0, a""") |
| 224 |
# Unsupported element type, also the body should be ignored |
| 225 |
# convert(fname, """*ELEMENT, TYPE=sometype |
| 226 |
#0 |
| 227 |
#""") |
| 228 |
# # Bad parameter syntax |
| 229 |
# convert(fname, "*ELEMENT, TYPE=sometype, BAD") |
| 230 |
# # Missing type specifier |
| 231 |
# convert(fname, "*ELEMENT", error=True) |
| 232 |
# # Non-existent node |
| 233 |
# convert(fname, """*NODE |
| 234 |
#1, 0, 0, 0 |
| 235 |
#2, 0, 0, 0 |
| 236 |
#3, 0, 0, 0 |
| 237 |
#*ELEMENT, TYPE=C3D4 |
| 238 |
#1, 1, 2, 3, 4 |
| 239 |
#""", error=True) |
| 240 |
# # Too few nodes |
| 241 |
# convert(fname, """*NODE |
| 242 |
#1, 0, 0, 0 |
| 243 |
##2, 0, 0, 0 |
| 244 |
#3, 0, 0, 0 |
| 245 |
#*ELEMENT, TYPE=C3D4 |
| 246 |
#1, 1, 2, 3 |
| 247 |
#""", error=True) |
| 248 |
# # Non-existent element set |
| 249 |
# convert(fname, """*MATERIAL, NAME=MAT |
| 250 |
#*SOLID SECTION, ELSET=NONE, MATERIAL=MAT""", error=True) |
| 251 |
|
| 252 |
def test_facet_success(self): |
| 253 |
""" Test facet export. |
| 254 |
""" |
| 255 |
dim = 3 |
| 256 |
nb_facets = 1170 # The total number of facets in the mesh |
| 257 |
marker_counter = {0: 990, |
| 258 |
1: 42, |
| 259 |
2: 42, |
| 260 |
3: 96, |
| 261 |
4: 0} |
| 262 |
handler = self.__convert("abaqus_facet.inp") |
| 263 |
self.assert_(handler.vertices_ended) |
| 264 |
self.assert_(handler.cells_ended) |
| 265 |
|
| 266 |
self.assert_("facet_region" in list(handler.functions.keys())) |
| 267 |
cell_type = DataHandler.CellType_Triangle |
| 268 |
function_dim, sz, entries, ended = handler.functions["facet_region"] |
| 269 |
|
| 270 |
# the dimension of the meshfunction should be dim-1 |
| 271 |
self.assertEqual(function_dim, dim - 1) |
| 272 |
# There should be size facets in the mesh function |
| 273 |
self.assertEqual(len(entries), nb_facets) |
| 274 |
self.assertEqual(sz, nb_facets) |
| 275 |
|
| 276 |
|
| 277 |
# Check that the right number of facets are marked |
| 278 |
for marker, count in marker_counter.items(): |
| 279 |
self.assert_(len([i for i in entries if i == marker]) == count) |
| 280 |
|
| 281 |
self.assert_(ended) |
| 282 |
self.assert_(handler.closed) |
| 283 |
|
| 284 |
def __convert(self, fname): |
| 285 |
handler = _TestHandler(DataHandler.CellType_Tetrahedron, 3, self) |
| 286 |
if not os.path.isabs(fname): |
| 287 |
fname = os.path.join("data", fname) |
| 288 |
meshconvert.convert(fname, handler) |
| 289 |
return handler |
| 290 |
|
| 291 |
class TestGmsh(_ConverterTest): |
| 292 |
""" Test Gmsh convertor. |
| 293 |
""" |
| 294 |
def test_success(self): |
| 295 |
""" Test success case. |
| 296 |
""" |
| 297 |
handler = self.__convert("gmsh.msh") |
| 298 |
# Verify vertices |
| 299 |
self.assertEqual(handler.vertices, [ |
| 300 |
(0, 0, 0), |
| 301 |
(1, 0, 1), |
| 302 |
(-0, 0.8, 0.6), |
| 303 |
(0.3, 0.8, -0.1), |
| 304 |
(0.6, 0.3, -0.4), |
| 305 |
(0.5, 0, 0.5), |
| 306 |
(0.5, 0.4, 0.8), |
| 307 |
(0.76, 0.26, 0.63), |
| 308 |
(0.53, 0.53, 0.26), |
| 309 |
(0.8, 0.15, 0.3) |
| 310 |
]) |
| 311 |
self.assert_(handler.vertices_ended) |
| 312 |
|
| 313 |
# Verify cells |
| 314 |
self.assertEqual(handler.cells, [ |
| 315 |
(9, 5, 1, 7), |
| 316 |
(4, 8, 0, 9), |
| 317 |
(8, 5, 0, 9), |
| 318 |
(8, 5, 9, 7), |
| 319 |
(8, 3, 4, 0), |
| 320 |
(8, 2, 3, 0), |
| 321 |
(7, 5, 6, 8), |
| 322 |
(5, 2, 6, 8), |
| 323 |
(1, 7, 5, 6), |
| 324 |
(5, 2, 8, 0) |
| 325 |
]) |
| 326 |
self.assert_(handler.cells_ended) |
| 327 |
|
| 328 |
# Verify physical regions |
| 329 |
self.assertEqual(list(handler.functions.keys()), ["physical_region"]) |
| 330 |
dim, sz, entries, ended = handler.functions["physical_region"] |
| 331 |
self.assertEqual(dim, 3) |
| 332 |
self.assertEqual(sz, 10) # There are 10 cells |
| 333 |
# Cells 0 thru 4 should be in region 1000, cells 5 thru 9 in |
| 334 |
# region 2000 |
| 335 |
self.assertEqual(entries, [1000]*5 + [2000]*5) |
| 336 |
self.assert_(ended) |
| 337 |
self.assert_(handler.closed) |
| 338 |
|
| 339 |
# FIXME: test disabled, see https://bitbucket.org/fenics-project/dolfin/issues/682 |
| 340 |
def xtest_1D_facet_markings_2 (self): |
| 341 |
""" |
| 342 |
Test to see if the 1D facet markings behave as expected. |
| 343 |
2 vertices marked |
| 344 |
""" |
| 345 |
marked_facets = [0,2] |
| 346 |
self._facet_marker_driver(1, 2, marked_facets, 11) |
| 347 |
|
| 348 |
# FIXME: test disabled, see https://bitbucket.org/fenics-project/dolfin/issues/682 |
| 349 |
def xtest_2D_facet_markings_1 (self): |
| 350 |
""" |
| 351 |
Test to see if the 2D facet markings behave as expected. |
| 352 |
1 edge marked |
| 353 |
""" |
| 354 |
marked_facets = [7] |
| 355 |
self._facet_marker_driver(2, 1, marked_facets, 8) |
| 356 |
|
| 357 |
# FIXME: test disabled, see https://bitbucket.org/fenics-project/dolfin/issues/682 |
| 358 |
def xtest_2D_facet_markings_2 (self): |
| 359 |
""" |
| 360 |
Test to see if the 2D facet markings behave as expected. |
| 361 |
2 edges marked |
| 362 |
""" |
| 363 |
marked_facets = [2,5] |
| 364 |
self._facet_marker_driver(2, 2, marked_facets, 8) |
| 365 |
|
| 366 |
# FIXME: test disabled, see https://bitbucket.org/fenics-project/dolfin/issues/682 |
| 367 |
def xtest_2D_facet_markings_3 (self): |
| 368 |
""" |
| 369 |
Test to see if the 2D facet markings behave as expected. |
| 370 |
3 edges marked |
| 371 |
""" |
| 372 |
marked_facets = [5,6,7] |
| 373 |
self._facet_marker_driver(2, 3, marked_facets, 8) |
| 374 |
|
| 375 |
# FIXME: test disabled, see https://bitbucket.org/fenics-project/dolfin/issues/682 |
| 376 |
def xtest_2D_facet_markings_4 (self): |
| 377 |
""" |
| 378 |
Test to see if the 2D facet markings behave as expected. |
| 379 |
4 edges marked |
| 380 |
""" |
| 381 |
marked_facets = [2,5,6,7] |
| 382 |
self._facet_marker_driver(2, 4, marked_facets, 8) |
| 383 |
|
| 384 |
# FIXME: test disabled, see https://bitbucket.org/fenics-project/dolfin/issues/682 |
| 385 |
def xtest_3D_facet_markings_1 (self): |
| 386 |
""" |
| 387 |
Test the marking of 3D facets |
| 388 |
Unit cube, 1 Face marked |
| 389 |
""" |
| 390 |
# [0, 0, 0, 999, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 999, 0, 0, 0, 0, 0, 0, 0, 0, 999, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 999, |
| 391 |
# 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
| 392 |
marked_facets = [3, 15, 24, 39,] |
| 393 |
self._facet_marker_driver(3, 1, marked_facets, 60) |
| 394 |
|
| 395 |
def _facet_marker_driver (self, dim, id, marked_facets, size ): |
| 396 |
if dim == 1: |
| 397 |
cell_type = DataHandler.CellType_Interval |
| 398 |
elif dim == 2: |
| 399 |
cell_type = DataHandler.CellType_Triangle |
| 400 |
elif dim == 3: |
| 401 |
cell_type = DataHandler.CellType_Tetrahedron |
| 402 |
|
| 403 |
handler = self.__convert("gmsh_test_facet_regions_%dD_%d.msh" % (dim, id), cell_type, dim) |
| 404 |
|
| 405 |
free_facets = list(range(size)) |
| 406 |
|
| 407 |
for i in marked_facets: |
| 408 |
free_facets.remove(i) |
| 409 |
|
| 410 |
function_dim, sz, entries, ended = handler.functions["facet_region"] |
| 411 |
|
| 412 |
# the dimension of the meshfunction should be dim-1 |
| 413 |
self.assertEqual(function_dim, dim-1) |
| 414 |
# There should be size facets in the mesh function |
| 415 |
self.assertEqual(len(entries), size) |
| 416 |
self.assertEqual(sz, size) |
| 417 |
# marked |
| 418 |
self.assert_( all ( entries[i] == 999 for i in marked_facets ) ) |
| 419 |
# all other edges should be zero |
| 420 |
self.assert_( all ( entries[i] == 0 for i in free_facets ) ) |
| 421 |
|
| 422 |
self.assert_(ended) |
| 423 |
self.assert_(handler.closed) |
| 424 |
|
| 425 |
def __convert(self, fname, cell_type=DataHandler.CellType_Tetrahedron, dim=3): |
| 426 |
handler = _TestHandler(cell_type, dim, self) |
| 427 |
if not os.path.isabs(fname): |
| 428 |
fname = os.path.join(os.path.dirname(__file__), "data", fname) |
| 429 |
meshconvert.convert(fname, handler) |
| 430 |
return handler |
| 431 |
|
| 432 |
@skip_in_parallel |
| 433 |
class TestTriangle(Tester): |
| 434 |
|
| 435 |
def test_convert_triangle(self): # Disabled because it fails, see FIXME below |
| 436 |
|
| 437 |
# test no. 1 |
| 438 |
from dolfin import Mesh, MPI |
| 439 |
|
| 440 |
fname = os.path.join(os.path.dirname(__file__), "data", "triangle") |
| 441 |
dfname = fname+".xml" |
| 442 |
|
| 443 |
# Read triangle file and convert to a dolfin xml mesh file |
| 444 |
meshconvert.triangle2xml(fname, dfname) |
| 445 |
|
| 446 |
# Read in dolfin mesh and check number of cells and vertices |
| 447 |
mesh = Mesh(dfname) |
| 448 |
self.assertEqual(mesh.num_vertices(), 96) |
| 449 |
self.assertEqual(mesh.num_cells(), 159) |
| 450 |
|
| 451 |
# Clean up |
| 452 |
os.unlink(dfname) |
| 453 |
|
| 454 |
# test no. 2 |
| 455 |
from dolfin import MPI, Mesh, MeshFunction, \ |
| 456 |
edges, Edge, faces, Face, \ |
| 457 |
SubsetIterator, facets |
| 458 |
|
| 459 |
fname = os.path.join(os.path.dirname(__file__), "data", "test_Triangle_3") |
| 460 |
dfname = fname+".xml" |
| 461 |
dfname0 = fname+".attr0.xml" |
| 462 |
|
| 463 |
# Read triangle file and convert to a dolfin xml mesh file |
| 464 |
meshconvert.triangle2xml(fname, dfname) |
| 465 |
|
| 466 |
# Read in dolfin mesh and check number of cells and vertices |
| 467 |
mesh = Mesh(dfname) |
| 468 |
mesh.init() |
| 469 |
mfun = MeshFunction('double', mesh, dfname0) |
| 470 |
self.assertEqual(mesh.num_vertices(), 58) |
| 471 |
self.assertEqual(mesh.num_cells(), 58) |
| 472 |
|
| 473 |
# Create a size_t MeshFunction and assign the values based on the |
| 474 |
# converted Meshfunction |
| 475 |
cf = MeshFunction("size_t", mesh, mesh.topology().dim()) |
| 476 |
cf.array()[mfun.array()==10.0] = 0 |
| 477 |
cf.array()[mfun.array()==-10.0] = 1 |
| 478 |
|
| 479 |
# Meassure total area of cells with 1 and 2 marker |
| 480 |
add = lambda x, y : x+y |
| 481 |
area0 = reduce(add, (Face(mesh, cell.index()).area() \ |
| 482 |
for cell in SubsetIterator(cf, 0)), 0.0) |
| 483 |
area1 = reduce(add, (Face(mesh, cell.index()).area() \ |
| 484 |
for cell in SubsetIterator(cf, 1)), 0.0) |
| 485 |
total_area = reduce(add, (face.area() for face in faces(mesh)), 0.0) |
| 486 |
|
| 487 |
# Check that all cells in the two domains are either above or below y=0 |
| 488 |
self.assertTrue(all(cell.midpoint().y()<0 for cell in SubsetIterator(cf, 0))) |
| 489 |
self.assertTrue(all(cell.midpoint().y()>0 for cell in SubsetIterator(cf, 1))) |
| 490 |
|
| 491 |
# Check that the areas add up |
| 492 |
self.assertAlmostEqual(area0+area1, total_area) |
| 493 |
|
| 494 |
# Measure the edge length of the two edge domains |
| 495 |
#edge_markers = mesh.domains().facet_domains() |
| 496 |
edge_markers = mesh.domains().markers(mesh.topology().dim()-1) |
| 497 |
self.assertTrue(edge_markers is not None) |
| 498 |
#length0 = reduce(add, (Edge(mesh, e.index()).length() \ |
| 499 |
# for e in SubsetIterator(edge_markers, 0)), 0.0) |
| 500 |
length0, length1 = 0.0, 0.0 |
| 501 |
for item in list(edge_markers.items()): |
| 502 |
if item[1] == 0: |
| 503 |
e = Edge(mesh, int(item[0])) |
| 504 |
length0 += Edge(mesh, int(item[0])).length() |
| 505 |
elif item [1] == 1: |
| 506 |
length1 += Edge(mesh, int(item[0])).length() |
| 507 |
|
| 508 |
# Total length of all edges and total length of boundary edges |
| 509 |
total_length = reduce(add, (e.length() for e in edges(mesh)), 0.0) |
| 510 |
boundary_length = reduce(add, (Edge(mesh, f.index()).length() \ |
| 511 |
for f in facets(mesh) if f.exterior()), 0.0) |
| 512 |
|
| 513 |
# Check that the edges add up |
| 514 |
self.assertAlmostEqual(length0 + length1, total_length) |
| 515 |
self.assertAlmostEqual(length1, boundary_length) |
| 516 |
|
| 517 |
# Clean up |
| 518 |
os.unlink(dfname) |
| 519 |
os.unlink(dfname0) |
| 520 |
|
| 521 |
@skip_in_parallel |
| 522 |
class TestDiffPack(Tester): |
| 523 |
def test_convert_diffpack(self): |
| 524 |
|
| 525 |
from dolfin import Mesh, MPI, MeshFunction |
| 526 |
|
| 527 |
fname = os.path.join(os.path.dirname(__file__), "data", "diffpack_tet") |
| 528 |
dfname = fname+".xml" |
| 529 |
|
| 530 |
# Read triangle file and convert to a dolfin xml mesh file |
| 531 |
meshconvert.diffpack2xml(fname+".grid", dfname) |
| 532 |
|
| 533 |
# Read in dolfin mesh and check number of cells and vertices |
| 534 |
mesh = Mesh(dfname) |
| 535 |
self.assertEqual(mesh.num_vertices(), 27) |
| 536 |
self.assertEqual(mesh.num_cells(), 48) |
| 537 |
self.assertEqual(len(mesh.domains().markers(3)), 48) |
| 538 |
self.assertEqual(len(mesh.domains().markers(2)), 16) |
| 539 |
|
| 540 |
mf_basename = dfname.replace(".xml", "_marker_%d.xml") |
| 541 |
for marker, num in [(3, 9), (6, 9), (7, 3), (8, 1)]: |
| 542 |
|
| 543 |
mf_name = mf_basename % marker |
| 544 |
mf = MeshFunction("size_t", mesh, mf_name) |
| 545 |
self.assertEqual(sum(mf.array()==marker), num) |
| 546 |
os.unlink(mf_name) |
| 547 |
|
| 548 |
# Clean up |
| 549 |
os.unlink(dfname) |
| 550 |
|
| 551 |
def test_convert_diffpack_2d(self): |
| 552 |
|
| 553 |
from dolfin import Mesh, MPI, MeshFunction |
| 554 |
|
| 555 |
fname = os.path.join(os.path.dirname(__file__), "data", "diffpack_tri") |
| 556 |
dfname = fname+".xml" |
| 557 |
|
| 558 |
# Read triangle file and convert to a dolfin xml mesh file |
| 559 |
meshconvert.diffpack2xml(fname+".grid", dfname) |
| 560 |
|
| 561 |
# Read in dolfin mesh and check number of cells and vertices |
| 562 |
mesh = Mesh(dfname) |
| 563 |
|
| 564 |
self.assertEqual(mesh.num_vertices(), 41) |
| 565 |
self.assertEqual(mesh.num_cells(), 64) |
| 566 |
self.assertEqual(len(mesh.domains().markers(2)), 64) |
| 567 |
|
| 568 |
mf_basename = dfname.replace(".xml", "_marker_%d.xml") |
| 569 |
for marker, num in [(1,10), (2,5), (3,5)]: |
| 570 |
|
| 571 |
mf_name = mf_basename % marker |
| 572 |
mf = MeshFunction("size_t", mesh, mf_name) |
| 573 |
self.assertEqual(sum(mf.array()==marker), num) |
| 574 |
os.unlink(mf_name) |
| 575 |
|
| 576 |
# Clean up |
| 577 |
os.unlink(dfname) |