Debian Codegraph @

python/test/unit/adaptivity/test_time_series.py

raw
1 """Unit tests for TimeSeries"""
2
3 # Copyright (C) 2011-2014 Marie E. Rognes
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 import pytest
21 from dolfin import *
22 import os
23 from dolfin_utils.test import skip_in_parallel, skip_if_not_HDF5, fixture, tempdir
24
25 @skip_in_parallel
26 @skip_if_not_HDF5
27 def test_retrieve_all_connectivities(tempdir):
28 _test_retrieve(tempdir, "test_retrieve_all_connectivities")
29
30 def _test_retrieve(tempdir, basename):
31 filename = os.path.join(tempdir, basename)
32
33 times = [t/10.0 for t in range(1, 11)]
34
35 mesh_size = (2, 2, 2)
36 mesh = UnitCubeMesh(*mesh_size)
37 mesh.init()
38 V = FunctionSpace(mesh, "CG", 2)
39
40 u = Function(V)
41 series = TimeSeries(MPI.comm_world, filename)
42 for t in times:
43 u.vector()[:] = t
44 series.store(u.vector(), t)
45 series.store(mesh, t)
46
47 series = TimeSeries(MPI.comm_world, filename)
48 t0 = series.vector_times()[0]
49 T = series.mesh_times()[-1]
50
51 assert round(t0 - times[0], 7) == 0
52 assert round(T - times[-1], 7) == 0
53
54 # Test retreiving of mesh
55 mesh_retreived = Mesh()
56 series.retrieve(mesh_retreived, 0.1)
57
58 mesh_test = mesh
59
60 for entity in range(4):
61 mesh_retreived.init(entity)
62 assert mesh_retreived.topology().size(entity) == \
63 mesh_test.topology().size(entity)
64
65 for i in range(4):
66 for j in range(4):
67 mesh_retreived.init(i, j)
68 assert mesh_retreived.topology()(i, j).size() == \
69 mesh_test.topology()(i, j).size()
70
71
72 @skip_in_parallel
73 @skip_if_not_HDF5
74 def test_subdirectory(tempdir):
75 "Test that retrieve/store works with nonexisting subdirectory"
76 filename = os.path.join(tempdir, "test_subdirectory")
77
78 m0 = UnitSquareMesh(3, 3)
79
80 series0 = TimeSeries(MPI.comm_world, filename)
81 x0 = Vector(MPI.comm_world, 10)
82
83 # Test storage of only one time point for the mesh
84 series0.store(m0, 0.1)
85 series0.store(x0, 0.1)
86 series0.store(x0, 0.2)
87
88 series1 = TimeSeries(MPI.comm_world, filename)
89 m1 = Mesh()
90 x1 = Vector()
91
92 series1.retrieve(m1, 0.1)
93 series1.retrieve(x1, 0.15)