Debian Codegraph @

tests/test_legacy_readers.py

raw
1 # Copyright (C) 2023 Jørgen Schartum Dokken
2 #
3 # This file is part of adios4dolfinx
4 #
5 # SPDX-License-Identifier: MIT
6
7
8 import inspect
9 import pathlib
10
11 from mpi4py import MPI
12
13 import dolfinx
14 import numpy as np
15 import pytest
16 import ufl
17 from dolfinx.fem.petsc import LinearProblem
18
19 from adios4dolfinx import (
20 read_function,
21 read_function_from_legacy_h5,
22 read_mesh,
23 read_mesh_from_legacy_h5,
24 )
25
26
27 def test_legacy_mesh():
28 comm = MPI.COMM_WORLD
29 path = (pathlib.Path("legacy") / "mesh.h5").absolute()
30 if not path.exists():
31 pytest.skip(f"{path} does not exist")
32 mesh = read_mesh_from_legacy_h5(filename=path, comm=comm, group="/mesh")
33 assert mesh.topology.dim == 3
34 volume = mesh.comm.allreduce(
35 dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * ufl.dx(domain=mesh))),
36 op=MPI.SUM,
37 )
38 surface = mesh.comm.allreduce(
39 dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * ufl.ds(domain=mesh))),
40 op=MPI.SUM,
41 )
42 assert np.isclose(volume, 1)
43 assert np.isclose(surface, 6)
44
45 mesh.topology.create_entities(mesh.topology.dim - 1)
46 num_facets = mesh.topology.index_map(mesh.topology.dim - 1).size_global
47 assert num_facets == 18
48
49
50 def test_read_legacy_mesh_from_checkpoint():
51 comm = MPI.COMM_WORLD
52 filename = (pathlib.Path("legacy") / "mesh_checkpoint.h5").absolute()
53 if not filename.exists():
54 pytest.skip(f"{filename} does not exist")
55 mesh = read_mesh_from_legacy_h5(filename=filename, comm=comm, group="/Mesh/mesh")
56 assert mesh.topology.dim == 3
57 volume = mesh.comm.allreduce(
58 dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * ufl.dx(domain=mesh))),
59 op=MPI.SUM,
60 )
61 surface = mesh.comm.allreduce(
62 dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * ufl.ds(domain=mesh))),
63 op=MPI.SUM,
64 )
65 assert np.isclose(volume, 1)
66 assert np.isclose(surface, 6)
67
68 mesh.topology.create_entities(mesh.topology.dim - 1)
69 num_facets = mesh.topology.index_map(mesh.topology.dim - 1).size_global
70 assert num_facets == 18
71
72
73 def test_legacy_function():
74 comm = MPI.COMM_WORLD
75 path = (pathlib.Path("legacy") / "mesh.h5").absolute()
76 if not path.exists():
77 pytest.skip(f"{path} does not exist")
78 mesh = read_mesh_from_legacy_h5(path, comm, "/mesh")
79 V = dolfinx.fem.functionspace(mesh, ("DG", 2))
80 u = ufl.TrialFunction(V)
81 v = ufl.TestFunction(V)
82 a = ufl.inner(u, v) * ufl.dx
83 x = ufl.SpatialCoordinate(mesh)
84 f = ufl.conditional(ufl.gt(x[0], 0.5), x[1], 2 * x[0])
85 L = ufl.inner(f, v) * ufl.dx
86
87 uh = dolfinx.fem.Function(V)
88 if "petsc_options_prefix" in inspect.signature(LinearProblem.__init__).parameters.keys():
89 extra_options = {"petsc_options_prefix": "legacy_test"}
90 else:
91 extra_options = {}
92 problem = LinearProblem(
93 a, L, bcs=[], u=uh, petsc_options={"ksp_type": "preonly", "pc_type": "lu"}, **extra_options
94 )
95 problem.solve()
96
97 u_in = dolfinx.fem.Function(V)
98 read_function_from_legacy_h5(path, mesh.comm, u_in, group="v")
99 np.testing.assert_allclose(uh.x.array, u_in.x.array, atol=1e-14)
100
101 W = dolfinx.fem.functionspace(mesh, ("DG", 2, (mesh.geometry.dim,)))
102 wh = dolfinx.fem.Function(W)
103 wh.interpolate(lambda x: (x[0], 3 * x[2], 7 * x[1]))
104 w_in = dolfinx.fem.Function(W)
105
106 read_function_from_legacy_h5(path, mesh.comm, w_in, group="w")
107
108 np.testing.assert_allclose(wh.x.array, w_in.x.array, atol=1e-14)
109
110
111 def test_read_legacy_function_from_checkpoint():
112 comm = MPI.COMM_WORLD
113 path = (pathlib.Path("legacy") / "mesh_checkpoint.h5").absolute()
114 if not path.exists():
115 pytest.skip(f"{path} does not exist")
116 mesh = read_mesh_from_legacy_h5(path, comm, "/Mesh/mesh")
117
118 V = dolfinx.fem.functionspace(mesh, ("DG", 2))
119 u = ufl.TrialFunction(V)
120 v = ufl.TestFunction(V)
121 a = ufl.inner(u, v) * ufl.dx
122 x = ufl.SpatialCoordinate(mesh)
123 f = ufl.conditional(ufl.gt(x[0], 0.5), x[1], 2 * x[0])
124 L = ufl.inner(f, v) * ufl.dx
125
126 uh = dolfinx.fem.Function(V)
127 if "petsc_options_prefix" in inspect.signature(LinearProblem.__init__).parameters.keys():
128 extra_options = {"petsc_options_prefix": "legacy_checkpoint_test"}
129 else:
130 extra_options = {}
131 problem = LinearProblem(
132 a, L, bcs=[], u=uh, petsc_options={"ksp_type": "preonly", "pc_type": "lu"}, **extra_options
133 )
134 problem.solve()
135
136 u_in = dolfinx.fem.Function(V)
137 read_function_from_legacy_h5(path, mesh.comm, u_in, group="v", step=0)
138 assert np.allclose(uh.x.array, u_in.x.array)
139
140 # Check second step
141 uh.interpolate(lambda x: x[0])
142 read_function_from_legacy_h5(path, mesh.comm, u_in, group="v", step=1)
143 assert np.allclose(uh.x.array, u_in.x.array)
144
145 W = dolfinx.fem.functionspace(mesh, ("DG", 2, (mesh.geometry.dim,)))
146 wh = dolfinx.fem.Function(W)
147 wh.interpolate(lambda x: (x[0], 3 * x[2], 7 * x[1]))
148 w_in = dolfinx.fem.Function(W)
149
150 read_function_from_legacy_h5(path, mesh.comm, w_in, group="w", step=0)
151 np.testing.assert_allclose(wh.x.array, w_in.x.array, atol=1e-14)
152
153 wh.interpolate(lambda x: np.vstack((x[0], 0 * x[0], x[1])))
154 read_function_from_legacy_h5(path, mesh.comm, w_in, group="w", step=1)
155 np.testing.assert_allclose(wh.x.array, w_in.x.array, atol=1e-14)
156
157
158 def test_adios4dolfinx_legacy():
159 comm = MPI.COMM_WORLD
160 path = (pathlib.Path("legacy_checkpoint") / "adios4dolfinx_checkpoint.bp").absolute()
161 if not path.exists():
162 pytest.skip(f"{path} does not exist")
163
164 el = ("N1curl", 3)
165 mesh = read_mesh(path, comm, "BP4", dolfinx.mesh.GhostMode.shared_facet, legacy=True)
166
167 def f(x):
168 values = np.zeros((2, x.shape[1]), dtype=np.float64)
169 values[0] = x[0]
170 values[1] = -x[1]
171 return values
172
173 V = dolfinx.fem.functionspace(mesh, el)
174 u = dolfinx.fem.Function(V)
175 read_function(path, u, engine="BP4", legacy=True)
176
177 u_ex = dolfinx.fem.Function(V)
178 u_ex.interpolate(f)
179
180 np.testing.assert_allclose(u.x.array, u_ex.x.array, atol=1e-14)