| 1 |
"""Unit tests for the HDF5 io library - timeseries io""" |
| 2 |
|
| 3 |
# Copyright (C) 2014 Chris Richardson |
| 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 |
|
| 21 |
import pytest |
| 22 |
import os |
| 23 |
from dolfin import * |
| 24 |
from dolfin_utils.test import skip_if_not_HDF5, fixture, tempdir, \ |
| 25 |
xfail_with_serial_hdf5_in_parallel |
| 26 |
|
| 27 |
@skip_if_not_HDF5 |
| 28 |
@xfail_with_serial_hdf5_in_parallel |
| 29 |
def test_save_and_read_function_timeseries(tempdir): |
| 30 |
filename = os.path.join(tempdir, "function.h5") |
| 31 |
|
| 32 |
mesh = UnitSquareMesh(10, 10) |
| 33 |
Q = FunctionSpace(mesh, "CG", 3) |
| 34 |
F0 = Function(Q) |
| 35 |
F1 = Function(Q) |
| 36 |
E = Expression("t*x[0]", t = 0.0, degree=1) |
| 37 |
F0.interpolate(E) |
| 38 |
|
| 39 |
# Save to HDF5 File |
| 40 |
hdf5_file = HDF5File(mesh.mpi_comm(), filename, "w") |
| 41 |
for t in range(10): |
| 42 |
E.t = t |
| 43 |
F0.interpolate(E) |
| 44 |
hdf5_file.write(F0, "/function", t) |
| 45 |
hdf5_file.close() |
| 46 |
|
| 47 |
#Read back from file |
| 48 |
hdf5_file = HDF5File(mesh.mpi_comm(), filename, "r") |
| 49 |
for t in range(10): |
| 50 |
E.t = t |
| 51 |
F1.interpolate(E) |
| 52 |
vec_name = "/function/vector_%d"%t |
| 53 |
hdf5_file.read(F0, vec_name) |
| 54 |
timestamp = hdf5_file.attributes(vec_name)["timestamp"] |
| 55 |
assert timestamp == t |
| 56 |
result = F0.vector() - F1.vector() |
| 57 |
assert len(result.get_local().nonzero()[0]) == 0 |
| 58 |
hdf5_file.close() |