| 1 |
"Unit tests for XML input/output of Mesh (class XMLMesh)" |
| 2 |
|
| 3 |
# Copyright (C) 2011 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 |
# Modified by Anders Logg 2011 |
| 21 |
|
| 22 |
import pytest |
| 23 |
from dolfin import * |
| 24 |
import os |
| 25 |
from dolfin_utils.test import skip_in_parallel, fixture, cd_tempdir |
| 26 |
|
| 27 |
|
| 28 |
@skip_in_parallel |
| 29 |
def test_save_plain_mesh2D(cd_tempdir): |
| 30 |
mesh = UnitSquareMesh(8, 8) |
| 31 |
f = File("unit_square.xml") |
| 32 |
f << mesh |
| 33 |
|
| 34 |
@skip_in_parallel |
| 35 |
def test_save_plain_mesh3D(cd_tempdir): |
| 36 |
mesh = UnitCubeMesh(8, 8, 8) |
| 37 |
f = File("unit_cube.xml") |
| 38 |
f << mesh |
| 39 |
|
| 40 |
@skip_in_parallel |
| 41 |
def test_mesh_domains_io(cd_tempdir): |
| 42 |
"Test input/output for mesh domains" |
| 43 |
|
| 44 |
# Define subdomains for the 6 faces of the unit cube |
| 45 |
class F0(SubDomain): |
| 46 |
def inside(self, x, inside): |
| 47 |
return near(x[0], 0.0) |
| 48 |
class F1(SubDomain): |
| 49 |
def inside(self, x, inside): |
| 50 |
return near(x[0], 1.0) |
| 51 |
class F2(SubDomain): |
| 52 |
def inside(self, x, inside): |
| 53 |
return near(x[1], 0.0) |
| 54 |
class F3(SubDomain): |
| 55 |
def inside(self, x, inside): |
| 56 |
return near(x[1], 1.0) |
| 57 |
class F4(SubDomain): |
| 58 |
def inside(self, x, inside): |
| 59 |
return near(x[2], 0.0) |
| 60 |
class F5(SubDomain): |
| 61 |
def inside(self, x, inside): |
| 62 |
return near(x[2], 1.0) |
| 63 |
|
| 64 |
# Define subdomains for left and right of x = 0.5 |
| 65 |
class S0(SubDomain): |
| 66 |
def inside(self, x, inside): |
| 67 |
return x[0] < 0.5 + DOLFIN_EPS |
| 68 |
|
| 69 |
class S1(SubDomain): |
| 70 |
def inside(self, x, inside): |
| 71 |
return x[0] > 0.5 - DOLFIN_EPS |
| 72 |
|
| 73 |
# Create a mesh |
| 74 |
output_mesh = UnitCubeMesh(3, 3, 3) |
| 75 |
|
| 76 |
# Apply facet markers |
| 77 |
f0 = F0() |
| 78 |
f0.mark_facets(output_mesh, 0) |
| 79 |
f1 = F1() |
| 80 |
f1.mark_facets(output_mesh, 1) |
| 81 |
f2 = F2() |
| 82 |
f2.mark_facets(output_mesh, 2) |
| 83 |
f3 = F3() |
| 84 |
f3.mark_facets(output_mesh, 3) |
| 85 |
f4 = F4() |
| 86 |
f4.mark_facets(output_mesh, 4) |
| 87 |
f5 = F5() |
| 88 |
f5.mark_facets(output_mesh, 5) |
| 89 |
|
| 90 |
# Apply cell markers |
| 91 |
s0 = S0() |
| 92 |
s0.mark_cells(output_mesh, 0) |
| 93 |
s1 = S1() |
| 94 |
s1.mark_cells(output_mesh, 1) |
| 95 |
|
| 96 |
# Write to file |
| 97 |
output_file = File("XMLMesh_test_mesh_domains_io.xml") |
| 98 |
output_file << output_mesh |
| 99 |
|
| 100 |
# Read from file |
| 101 |
input_file = File("XMLMesh_test_mesh_domains_io.xml") |
| 102 |
input_mesh = Mesh() |
| 103 |
input_file >> input_mesh |
| 104 |
|
| 105 |
# Get some data and check that it matches |
| 106 |
assert len(input_mesh.domains().markers(0)) == \ |
| 107 |
len(output_mesh.domains().markers(0)) |
| 108 |
assert len(input_mesh.domains().markers(1)) == \ |
| 109 |
len(output_mesh.domains().markers(1)) |
| 110 |
assert len(input_mesh.domains().markers(2)) == \ |
| 111 |
len(output_mesh.domains().markers(2)) |
| 112 |
assert len(input_mesh.domains().markers(3)) == \ |
| 113 |
len(output_mesh.domains().markers(3)) |