| 1 |
"""Unit tests for the HDF5 io library""" |
| 2 |
|
| 3 |
# Copyright (C) 2012 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 Chris Richardson 2013 |
| 21 |
|
| 22 |
import pytest |
| 23 |
import os |
| 24 |
from dolfin import * |
| 25 |
from dolfin_utils.test import skip_if_not_HDF5, fixture, tempdir, xfail_with_serial_hdf5_in_parallel |
| 26 |
|
| 27 |
@skip_if_not_HDF5 |
| 28 |
@xfail_with_serial_hdf5_in_parallel |
| 29 |
def test_parallel(tempdir): |
| 30 |
filename = os.path.join(tempdir, "y.h5") |
| 31 |
have_parallel = has_hdf5_parallel() |
| 32 |
hdf5 = HDF5File(MPI.comm_world, filename, "w") |
| 33 |
|
| 34 |
@skip_if_not_HDF5 |
| 35 |
@xfail_with_serial_hdf5_in_parallel |
| 36 |
def test_save_vector(tempdir): |
| 37 |
filename = os.path.join(tempdir, "x.h5") |
| 38 |
x = Vector(MPI.comm_world, 305) |
| 39 |
x[:] = 1.0 |
| 40 |
with HDF5File(x.mpi_comm(), filename, "w") as vector_file: |
| 41 |
vector_file.write(x, "/my_vector") |
| 42 |
|
| 43 |
@skip_if_not_HDF5 |
| 44 |
@xfail_with_serial_hdf5_in_parallel |
| 45 |
def test_save_and_read_vector(tempdir): |
| 46 |
filename = os.path.join(tempdir, "vector.h5") |
| 47 |
|
| 48 |
# Write to file |
| 49 |
x = Vector(MPI.comm_world, 305) |
| 50 |
x[:] = 1.2 |
| 51 |
with HDF5File(x.mpi_comm(), filename, "w") as vector_file: |
| 52 |
vector_file.write(x, "/my_vector") |
| 53 |
|
| 54 |
# Read from file |
| 55 |
y = Vector() |
| 56 |
with HDF5File(x.mpi_comm(), filename, "r") as vector_file: |
| 57 |
vector_file.read(y, "/my_vector", False) |
| 58 |
assert y.size() == x.size() |
| 59 |
assert (x - y).norm("l1") == 0.0 |
| 60 |
|
| 61 |
@skip_if_not_HDF5 |
| 62 |
@xfail_with_serial_hdf5_in_parallel |
| 63 |
def test_save_and_read_meshfunction_2D(tempdir): |
| 64 |
filename = os.path.join(tempdir, "meshfn-2d.h5") |
| 65 |
|
| 66 |
# Write to file |
| 67 |
mesh = UnitSquareMesh(20, 20) |
| 68 |
with HDF5File(mesh.mpi_comm(), filename, "w") as mf_file: |
| 69 |
|
| 70 |
# save meshfuns to compare when reading back |
| 71 |
meshfunctions = [] |
| 72 |
for i in range(0,3): |
| 73 |
mf = MeshFunction('double', mesh, i) |
| 74 |
# NB choose a value to set which will be the same |
| 75 |
# on every process for each entity |
| 76 |
for cell in entities(mesh, i): |
| 77 |
mf[cell] = cell.midpoint()[0] |
| 78 |
meshfunctions.append(mf) |
| 79 |
mf_file.write(mf, "/meshfunction/meshfun%d" % i) |
| 80 |
|
| 81 |
# Read back from file |
| 82 |
with HDF5File(mesh.mpi_comm(), filename, "r") as mf_file: |
| 83 |
for i in range(0,3): |
| 84 |
mf2 = MeshFunction('double', mesh, i) |
| 85 |
mf_file.read(mf2, "/meshfunction/meshfun%d" % i) |
| 86 |
for cell in entities(mesh, i): |
| 87 |
assert meshfunctions[i][cell] == mf2[cell] |
| 88 |
|
| 89 |
@skip_if_not_HDF5 |
| 90 |
@xfail_with_serial_hdf5_in_parallel |
| 91 |
def test_save_and_read_meshfunction_3D(tempdir): |
| 92 |
filename = os.path.join(tempdir, "meshfn-3d.h5") |
| 93 |
|
| 94 |
# Write to file |
| 95 |
mesh = UnitCubeMesh(10, 10, 10) |
| 96 |
mf_file = HDF5File(mesh.mpi_comm(), filename, "w") |
| 97 |
|
| 98 |
# save meshfuns to compare when reading back |
| 99 |
meshfunctions = [] |
| 100 |
for i in range(0,4): |
| 101 |
mf = MeshFunction('double', mesh, i) |
| 102 |
# NB choose a value to set which will be the same |
| 103 |
# on every process for each entity |
| 104 |
for cell in entities(mesh, i): |
| 105 |
mf[cell] = cell.midpoint()[0] |
| 106 |
meshfunctions.append(mf) |
| 107 |
mf_file.write(mf, "/meshfunction/group/%d/meshfun"%i) |
| 108 |
mf_file.close() |
| 109 |
|
| 110 |
# Read back from file |
| 111 |
mf_file = HDF5File(mesh.mpi_comm(), filename, "r") |
| 112 |
for i in range(0,4): |
| 113 |
mf2 = MeshFunction('double', mesh, i) |
| 114 |
mf_file.read(mf2, "/meshfunction/group/%d/meshfun"%i) |
| 115 |
for cell in entities(mesh, i): |
| 116 |
assert meshfunctions[i][cell] == mf2[cell] |
| 117 |
mf_file.close() |
| 118 |
|
| 119 |
@skip_if_not_HDF5 |
| 120 |
@xfail_with_serial_hdf5_in_parallel |
| 121 |
def test_save_and_read_mesh_value_collection(tempdir): |
| 122 |
ndiv = 5 |
| 123 |
filename = os.path.join(tempdir, "mesh_value_collection.h5") |
| 124 |
mesh = UnitCubeMesh(ndiv, ndiv, ndiv) |
| 125 |
|
| 126 |
point2list = lambda p : [ p.x(), p.y(), p.z() ] |
| 127 |
|
| 128 |
# write to file |
| 129 |
with HDF5File(mesh.mpi_comm(), filename, 'w') as f : |
| 130 |
for dim in range(mesh.topology().dim()) : |
| 131 |
mvc = MeshValueCollection("size_t", mesh, dim) |
| 132 |
mesh.init(dim) |
| 133 |
for e in entities(mesh, dim) : |
| 134 |
# this can be easily computed to the check the value |
| 135 |
val = int(ndiv*sum(point2list(e.midpoint()))) + 1 |
| 136 |
mvc.set_value(e.index(), val) |
| 137 |
f.write(mvc, "/mesh_value_collection_{}".format(dim)) |
| 138 |
|
| 139 |
# read from file |
| 140 |
with HDF5File(mesh.mpi_comm(), filename, 'r') as f : |
| 141 |
for dim in range(mesh.topology().dim()) : |
| 142 |
mvc = MeshValueCollection("size_t", mesh, dim) |
| 143 |
f.read(mvc, "/mesh_value_collection_{}".format(dim)) |
| 144 |
# check the values |
| 145 |
for (cell, lidx), val in mvc.values().items() : |
| 146 |
eidx = Cell(mesh, cell).entities(dim)[lidx] |
| 147 |
mid = point2list(MeshEntity(mesh, dim, eidx).midpoint()) |
| 148 |
assert val == int(ndiv*sum(mid)) + 1 |
| 149 |
|
| 150 |
@skip_if_not_HDF5 |
| 151 |
@xfail_with_serial_hdf5_in_parallel |
| 152 |
def test_save_and_read_mesh_value_collection_with_only_one_marked_entity(tempdir): |
| 153 |
ndiv = 5 |
| 154 |
filename = os.path.join(tempdir, "mesh_value_collection.h5") |
| 155 |
mesh = UnitCubeMesh(ndiv, ndiv, ndiv) |
| 156 |
mvc = MeshValueCollection("size_t", mesh, 3) |
| 157 |
mesh.init(3) |
| 158 |
if MPI.rank(mesh.mpi_comm()) == 0: |
| 159 |
mvc.set_value(0, 1) |
| 160 |
|
| 161 |
# write to file |
| 162 |
with HDF5File(mesh.mpi_comm(), filename, 'w') as f : |
| 163 |
f.write(mvc, "/mesh_value_collection") |
| 164 |
|
| 165 |
# read from file |
| 166 |
with HDF5File(mesh.mpi_comm(), filename, 'r') as f : |
| 167 |
mvc = MeshValueCollection("size_t", mesh, 3) |
| 168 |
f.read(mvc, "/mesh_value_collection") |
| 169 |
assert MPI.sum(mesh.mpi_comm(), mvc.size()) == 1 |
| 170 |
if MPI.rank(mesh.mpi_comm()) == 0: |
| 171 |
assert mvc.get_value(0, 0) == 1 |
| 172 |
|
| 173 |
@skip_if_not_HDF5 |
| 174 |
@xfail_with_serial_hdf5_in_parallel |
| 175 |
def test_save_and_read_function(tempdir): |
| 176 |
filename = os.path.join(tempdir, "function.h5") |
| 177 |
|
| 178 |
mesh = UnitSquareMesh(10, 10) |
| 179 |
Q = FunctionSpace(mesh, "CG", 3) |
| 180 |
F0 = Function(Q) |
| 181 |
F1 = Function(Q) |
| 182 |
E = Expression("x[0]", degree=1) |
| 183 |
F0.interpolate(E) |
| 184 |
|
| 185 |
# Save to HDF5 File |
| 186 |
|
| 187 |
hdf5_file = HDF5File(mesh.mpi_comm(), filename, "w") |
| 188 |
hdf5_file.write(F0, "/function") |
| 189 |
hdf5_file.close() |
| 190 |
|
| 191 |
#Read back from file |
| 192 |
hdf5_file = HDF5File(mesh.mpi_comm(), filename, "r") |
| 193 |
hdf5_file.read(F1, "/function") |
| 194 |
result = F0.vector() - F1.vector() |
| 195 |
assert len(result.get_local().nonzero()[0]) == 0 |
| 196 |
hdf5_file.close() |
| 197 |
|
| 198 |
@skip_if_not_HDF5 |
| 199 |
@xfail_with_serial_hdf5_in_parallel |
| 200 |
def test_save_and_read_mesh_2D(tempdir): |
| 201 |
filename = os.path.join(tempdir, "mesh2d.h5") |
| 202 |
|
| 203 |
# Write to file |
| 204 |
mesh0 = UnitSquareMesh(20, 20) |
| 205 |
mesh_file = HDF5File(mesh0.mpi_comm(), filename, "w") |
| 206 |
mesh_file.write(mesh0, "/my_mesh") |
| 207 |
mesh_file.close() |
| 208 |
|
| 209 |
# Read from file |
| 210 |
mesh1 = Mesh() |
| 211 |
mesh_file = HDF5File(mesh0.mpi_comm(), filename, "r") |
| 212 |
mesh_file.read(mesh1, "/my_mesh", False) |
| 213 |
mesh_file.close() |
| 214 |
|
| 215 |
assert mesh0.num_entities_global(0) == mesh1.num_entities_global(0) |
| 216 |
dim = mesh0.topology().dim() |
| 217 |
assert mesh0.num_entities_global(dim) == mesh1.num_entities_global(dim) |
| 218 |
|
| 219 |
@skip_if_not_HDF5 |
| 220 |
@xfail_with_serial_hdf5_in_parallel |
| 221 |
def test_save_and_read_mesh_3D(tempdir): |
| 222 |
filename = os.path.join(tempdir, "mesh3d.h5") |
| 223 |
|
| 224 |
# Write to file |
| 225 |
mesh0 = UnitCubeMesh(10, 10, 10) |
| 226 |
mesh_file = HDF5File(mesh0.mpi_comm(), filename, "w") |
| 227 |
mesh_file.write(mesh0, "/my_mesh") |
| 228 |
mesh_file.close() |
| 229 |
|
| 230 |
# Read from file |
| 231 |
mesh1 = Mesh() |
| 232 |
mesh_file = HDF5File(mesh0.mpi_comm(), filename, "r") |
| 233 |
mesh_file.read(mesh1, "/my_mesh", False) |
| 234 |
mesh_file.close() |
| 235 |
|
| 236 |
assert mesh0.num_entities_global(0) == mesh1.num_entities_global(0) |
| 237 |
dim = mesh0.topology().dim() |
| 238 |
assert mesh0.num_entities_global(dim) == mesh1.num_entities_global(dim) |
| 239 |
|
| 240 |
@skip_if_not_HDF5 |
| 241 |
@xfail_with_serial_hdf5_in_parallel |
| 242 |
def test_mpi_atomicity(tempdir): |
| 243 |
comm_world = MPI.comm_world |
| 244 |
if MPI.size(comm_world) > 1: |
| 245 |
filename = os.path.join(tempdir, "mpiatomic.h5") |
| 246 |
with HDF5File(MPI.comm_world, filename, "w") as f: |
| 247 |
assert f.get_mpi_atomicity() is False |
| 248 |
f.set_mpi_atomicity(True) |
| 249 |
assert f.get_mpi_atomicity() is True |