| 1 |
"Unit tests for XML input/output of MeshFunction (class XMLMeshFunction)" |
| 2 |
|
| 3 |
# Copyright (C) 2011-2014 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 |
import os |
| 24 |
from dolfin import * |
| 25 |
from dolfin_utils.test import skip_in_parallel, fixture, cd_tempdir |
| 26 |
|
| 27 |
|
| 28 |
@skip_in_parallel |
| 29 |
def test_io_size_t(cd_tempdir): |
| 30 |
"Test input/output for size_t" |
| 31 |
|
| 32 |
# Write some data |
| 33 |
mesh = UnitSquareMesh(5, 5) |
| 34 |
f = MeshFunction("size_t", mesh, 1) |
| 35 |
f.set_all(0) |
| 36 |
f[2] = 3 |
| 37 |
f[5] = 7 |
| 38 |
|
| 39 |
filename = "XMLMeshFunction_test_io_size_t.xml" |
| 40 |
|
| 41 |
# Write |
| 42 |
output_file = File(filename) |
| 43 |
output_file << f |
| 44 |
|
| 45 |
# Read from file |
| 46 |
g = MeshFunction("size_t", mesh, 1) |
| 47 |
input_file = File(filename) |
| 48 |
input_file >> g |
| 49 |
|
| 50 |
# Check values |
| 51 |
for i in range(f.size()): |
| 52 |
assert f[i] == g[i] |
| 53 |
|
| 54 |
|
| 55 |
@skip_in_parallel |
| 56 |
def test_io_int(cd_tempdir): |
| 57 |
"Test input/output for int" |
| 58 |
|
| 59 |
# Write some data |
| 60 |
mesh = UnitSquareMesh(5, 5) |
| 61 |
f = MeshFunction("int", mesh, 1) |
| 62 |
f.set_all(0) |
| 63 |
f[2] = -3 |
| 64 |
f[5] = 7 |
| 65 |
|
| 66 |
filename = "XMLMeshFunction_test_io_int.xml" |
| 67 |
|
| 68 |
# Write |
| 69 |
output_file = File(filename) |
| 70 |
output_file << f |
| 71 |
|
| 72 |
# Read from file |
| 73 |
g = MeshFunction("int", mesh, 1) |
| 74 |
input_file = File(filename) |
| 75 |
input_file >> g |
| 76 |
|
| 77 |
# Check values |
| 78 |
for i in range(f.size()): |
| 79 |
assert f[i] == g[i] |
| 80 |
|
| 81 |
|
| 82 |
@skip_in_parallel |
| 83 |
def test_io_double(cd_tempdir): |
| 84 |
"Test input/output for double" |
| 85 |
|
| 86 |
# Write some data |
| 87 |
mesh = UnitSquareMesh(5, 5) |
| 88 |
f = MeshFunction("double", mesh, 1) |
| 89 |
f.set_all(0.0) |
| 90 |
f[2] = 3.14 |
| 91 |
f[5] = 10000000.0 |
| 92 |
|
| 93 |
filename = "XMLMeshFunction_test_io_double.xml" |
| 94 |
|
| 95 |
# Write |
| 96 |
output_file = File(filename) |
| 97 |
output_file << f |
| 98 |
|
| 99 |
# Read from file |
| 100 |
g = MeshFunction("double", mesh, 1) |
| 101 |
input_file = File(filename) |
| 102 |
input_file >> g |
| 103 |
|
| 104 |
# Check values |
| 105 |
for i in range(f.size()): |
| 106 |
assert f[i] == g[i] |
| 107 |
|
| 108 |
|
| 109 |
@skip_in_parallel |
| 110 |
def test_io_bool(cd_tempdir): |
| 111 |
"Test input/output for bool" |
| 112 |
|
| 113 |
# Write some data |
| 114 |
mesh = UnitSquareMesh(5, 5) |
| 115 |
f = MeshFunction("bool", mesh, 1) |
| 116 |
f.set_all(False) |
| 117 |
f[2] = True |
| 118 |
f[5] = False |
| 119 |
|
| 120 |
filename = "XMLMeshFunction_test_io_bool.xml" |
| 121 |
|
| 122 |
# Write |
| 123 |
output_file = File(filename) |
| 124 |
output_file << f |
| 125 |
|
| 126 |
# Read from file |
| 127 |
g = MeshFunction("bool", mesh, 1) |
| 128 |
input_file = File(filename) |
| 129 |
input_file >> g |
| 130 |
|
| 131 |
# Check values |
| 132 |
for i in range(f.size()): |
| 133 |
assert f[i] == g[i] |