| 1 |
# Copyright (C) 2024 Jørgen Schartum Dokken |
| 2 |
# |
| 3 |
# This file is part of adios4dolfinx |
| 4 |
# |
| 5 |
# SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
|
| 8 |
""" |
| 9 |
Functions to create checkpoints with adios4dolfinx v0.7.x |
| 10 |
""" |
| 11 |
|
| 12 |
import argparse |
| 13 |
import pathlib |
| 14 |
from importlib.metadata import version |
| 15 |
|
| 16 |
from mpi4py import MPI |
| 17 |
|
| 18 |
import dolfinx |
| 19 |
import numpy as np |
| 20 |
|
| 21 |
import adios4dolfinx |
| 22 |
|
| 23 |
a4d_version = version("adios4dolfinx") |
| 24 |
assert a4d_version < "0.7.2", ( |
| 25 |
f"Creating a legacy checkpoint requires adios4dolfinx < 0.7.2, you have {a4d_version}." |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
def f(x): |
| 30 |
values = np.zeros((2, x.shape[1]), dtype=np.float64) |
| 31 |
values[0] = x[0] |
| 32 |
values[1] = -x[1] |
| 33 |
return values |
| 34 |
|
| 35 |
|
| 36 |
def write_checkpoint(filename, mesh, el, f): |
| 37 |
V = dolfinx.fem.FunctionSpace(mesh, el) |
| 38 |
uh = dolfinx.fem.Function(V, dtype=np.float64) |
| 39 |
uh.interpolate(f) |
| 40 |
|
| 41 |
adios4dolfinx.write_mesh(V.mesh, filename) |
| 42 |
adios4dolfinx.write_function(uh, filename) |
| 43 |
|
| 44 |
|
| 45 |
def verify_checkpoint(filename, el, f): |
| 46 |
mesh = adios4dolfinx.read_mesh( |
| 47 |
MPI.COMM_WORLD, filename, "BP4", dolfinx.mesh.GhostMode.shared_facet |
| 48 |
) |
| 49 |
V = dolfinx.fem.FunctionSpace(mesh, el) |
| 50 |
uh = dolfinx.fem.Function(V, dtype=np.float64) |
| 51 |
adios4dolfinx.read_function(uh, filename) |
| 52 |
|
| 53 |
u_ex = dolfinx.fem.Function(V, dtype=np.float64) |
| 54 |
u_ex.interpolate(f) |
| 55 |
|
| 56 |
np.testing.assert_allclose(u_ex.x.array, uh.x.array, atol=1e-15) |
| 57 |
|
| 58 |
|
| 59 |
if __name__ == "__main__": |
| 60 |
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 61 |
parser.add_argument("--output-dir", type=str, default="legacy_checkpoint", dest="dir") |
| 62 |
|
| 63 |
inputs = parser.parse_args() |
| 64 |
path = pathlib.Path(inputs.dir) |
| 65 |
path.mkdir(exist_ok=True, parents=True) |
| 66 |
filename = path / "adios4dolfinx_checkpoint.bp" |
| 67 |
|
| 68 |
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10) |
| 69 |
el = ("N1curl", 3) |
| 70 |
write_checkpoint(filename, mesh, el, f) |
| 71 |
MPI.COMM_WORLD.Barrier() |
| 72 |
verify_checkpoint(filename, el, f) |