Skip to content

Commit a0ee385

Browse files
committed
Add functions to write CG1 and DG0 functions to VTKHDF
1 parent 100c21c commit a0ee385

3 files changed

Lines changed: 103 additions & 1 deletion

File tree

cpp/dolfinx/io/VTKHDF.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
#include <algorithm>
99
#include <concepts>
1010
#include <dolfinx/common/IndexMap.h>
11+
#include <dolfinx/fem/Function.h>
1112
#include <dolfinx/io/cells.h>
13+
#include <dolfinx/io/utils.h>
1214
#include <dolfinx/mesh/Mesh.h>
1315
#include <dolfinx/mesh/Topology.h>
1416
#include <dolfinx/mesh/utils.h>
@@ -298,6 +300,68 @@ void write_data(std::string point_or_cell, std::string filename,
298300
hdf5::close_file(h5file);
299301
}
300302

303+
/// @brief Write a CG1 function to VTKHDF.
304+
///
305+
/// Adds a function to an existing VTKHDF file, which already contains a mesh.
306+
///
307+
/// @tparam U Scalar type.
308+
/// @param[in] filename File for output.
309+
/// @param[in] mesh Mesh, which must be the same as the original mesh
310+
/// used in the file.
311+
/// @param[in] u Function to write to file.
312+
/// @param[in] time Timestamp.
313+
///
314+
/// @note Mesh must be written to file first using `VTKHDF::write_mesh`.
315+
/// @note Only one dataset "u" can be written per file at present, with
316+
/// multiple timesteps.
317+
/// @note Limited support for floating point types at present (no
318+
/// complex number support).
319+
template <std::floating_point U>
320+
void write_CG1_function(std::string filename, const mesh::Mesh<U>& mesh,
321+
const fem::Function<U>& u, double time)
322+
{
323+
io::VTKHDF::write_data<U>(
324+
"Point", filename, mesh,
325+
std::vector<U>(std::begin(u.x()->array()),
326+
std::begin(u.x()->array())
327+
+ mesh.geometry().index_map()->size_local()
328+
* u.function_space()->dofmaps(0)->bs()),
329+
time);
330+
}
331+
332+
/// @brief Write a DG0 function to VTKHDF.
333+
///
334+
/// Adds a function to an existing VTKHDF file, which already contains a mesh.
335+
///
336+
/// @tparam U Scalar type.
337+
/// @param[in] filename File for output.
338+
/// @param[in] mesh Mesh, which must be the same as the original mesh
339+
/// used in the file.
340+
/// @param[in] u Function to write to file.
341+
/// @param[in] time Timestamp.
342+
///
343+
/// @note Mesh must be written to file first using `VTKHDF::write_mesh`.
344+
/// @note Only one dataset "u" can be written per file at present, with
345+
/// multiple timesteps.
346+
/// @note Limited support for floating point types at present (no
347+
/// complex number support).
348+
template <std::floating_point U>
349+
void write_DG0_function(std::string filename, const mesh::Mesh<U>& mesh,
350+
const fem::Function<U>& u, double time)
351+
{
352+
const auto index_maps = mesh.topology()->index_maps(mesh.topology()->dim());
353+
int npoints
354+
= std::accumulate(index_maps.begin(), index_maps.end(), 0,
355+
[](int a, auto im) { return a + im->size_local(); });
356+
357+
io::VTKHDF::write_data<U>(
358+
"Cell", filename, mesh,
359+
std::vector<U>(std::begin(u.x()->array()),
360+
std::begin(u.x()->array())
361+
+ npoints * u.function_space()->dofmaps(0)->bs()),
362+
time);
363+
}
364+
301365
/// @brief Read a mesh from a VTKHDF format file.
302366
///
303367
/// @tparam U Scalar type of mesh

python/dolfinx/io/vtkhdf.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,22 @@
1616
from dolfinx.cpp.io import (
1717
read_vtkhdf_mesh_float32,
1818
read_vtkhdf_mesh_float64,
19+
write_vtkhdf_cg1_function,
1920
write_vtkhdf_data,
21+
write_vtkhdf_dg0_function,
2022
write_vtkhdf_mesh,
2123
)
24+
from dolfinx.fem import Function
2225
from dolfinx.mesh import Mesh
2326

24-
__all__ = ["read_mesh", "write_cell_data", "write_mesh", "write_point_data"]
27+
__all__ = [
28+
"read_mesh",
29+
"write_cell_data",
30+
"write_cg1_function",
31+
"write_dg0_function",
32+
"write_mesh",
33+
"write_point_data",
34+
]
2535

2636

2737
def read_mesh(
@@ -100,3 +110,27 @@ def write_cell_data(filename: str | Path, mesh: Mesh, data: npt.NDArray, time: f
100110
time: Timestamp.
101111
"""
102112
write_vtkhdf_data("Cell", filename, mesh._cpp_object, data, time)
113+
114+
115+
def write_cg1_function(filename: str | Path, mesh: Mesh, u: Function, time: float):
116+
"""Write a CG1 function to file.
117+
118+
Args:
119+
filename: File to write to.
120+
mesh: Mesh.
121+
u: Function to write.
122+
time: Timestamp.
123+
"""
124+
write_vtkhdf_cg1_function(filename, mesh._cpp_object, u._cpp_object, time)
125+
126+
127+
def write_dg0_function(filename: str | Path, mesh: Mesh, u: Function, time: float):
128+
"""Write a DG0 function to file.
129+
130+
Args:
131+
filename: File to write to.
132+
mesh: Mesh.
133+
u: Function to write.
134+
time: Timestamp.
135+
"""
136+
write_vtkhdf_dg0_function(filename, mesh._cpp_object, u._cpp_object, time)

python/dolfinx/wrappers/io.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ void io(nb::module_& m)
225225
.def("write_vtkhdf_mesh", &dolfinx::io::VTKHDF::write_mesh<float>);
226226
m.def("write_vtkhdf_data", &dolfinx::io::VTKHDF::write_data<double>);
227227
m.def("write_vtkhdf_data", &dolfinx::io::VTKHDF::write_data<float>);
228+
m.def("write_vtkhdf_cg1_function", &dolfinx::io::VTKHDF::write_CG1_function<float>);
229+
m.def("write_vtkhdf_cg1_function", &dolfinx::io::VTKHDF::write_CG1_function<double>);
230+
m.def("write_vtkhdf_dg0_function", &dolfinx::io::VTKHDF::write_DG0_function<float>);
231+
m.def("write_vtkhdf_dg0_function", &dolfinx::io::VTKHDF::write_DG0_function<double>);
228232
m.def("read_vtkhdf_mesh_float64",
229233
[](MPICommWrapper comm, const std::string& filename, std::size_t gdim,
230234
std::optional<std::int32_t> max_facet_to_cell_links)

0 commit comments

Comments
 (0)