|
8 | 8 | #include <algorithm> |
9 | 9 | #include <concepts> |
10 | 10 | #include <dolfinx/common/IndexMap.h> |
| 11 | +#include <dolfinx/fem/Function.h> |
11 | 12 | #include <dolfinx/io/cells.h> |
| 13 | +#include <dolfinx/io/utils.h> |
12 | 14 | #include <dolfinx/mesh/Mesh.h> |
13 | 15 | #include <dolfinx/mesh/Topology.h> |
14 | 16 | #include <dolfinx/mesh/utils.h> |
@@ -298,6 +300,68 @@ void write_data(std::string point_or_cell, std::string filename, |
298 | 300 | hdf5::close_file(h5file); |
299 | 301 | } |
300 | 302 |
|
| 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 | + |
301 | 365 | /// @brief Read a mesh from a VTKHDF format file. |
302 | 366 | /// |
303 | 367 | /// @tparam U Scalar type of mesh |
|
0 commit comments