14#include <basix/mdspan.hpp>
18#include <dolfinx/common/IndexMap.h>
20#include <dolfinx/fem/FiniteElement.h>
21#include <dolfinx/fem/Function.h>
22#include <dolfinx/mesh/Geometry.h>
23#include <dolfinx/mesh/Mesh.h>
37template <dolfinx::scalar T, std::
floating_po
int U>
43namespace adios2_writer
46template <std::
floating_po
int T>
47using U = std::vector<std::variant<
48 std::shared_ptr<const fem::Function<float, T>>,
49 std::shared_ptr<const fem::Function<double, T>>,
50 std::shared_ptr<const fem::Function<std::complex<float>, T>>,
51 std::shared_ptr<const fem::Function<std::complex<double>, T>>>>;
64 ADIOS2Writer(MPI_Comm comm,
const std::filesystem::path& filename,
65 std::string tag, std::string engine);
68 ADIOS2Writer(ADIOS2Writer&& writer) =
default;
71 ADIOS2Writer(
const ADIOS2Writer&) =
delete;
77 ADIOS2Writer& operator=(ADIOS2Writer&& writer) =
default;
80 ADIOS2Writer& operator=(
const ADIOS2Writer&) =
delete;
87 std::unique_ptr<adios2::ADIOS> _adios;
88 std::unique_ptr<adios2::IO> _io;
89 std::unique_ptr<adios2::Engine> _engine;
97constexpr std::array field_ext = {
"_real",
"_imag"};
102adios2::Attribute<T> define_attribute(adios2::IO& io, std::string name,
103 const T& value, std::string var_name =
"",
104 std::string separator =
"/")
106 if (adios2::Attribute<T> attr = io.InquireAttribute<T>(name); attr)
109 return io.DefineAttribute<T>(name, value, var_name, separator);
115adios2::Variable<T> define_variable(adios2::IO& io, std::string name,
116 const adios2::Dims& shape = adios2::Dims(),
117 const adios2::Dims& start = adios2::Dims(),
118 const adios2::Dims& count = adios2::Dims())
120 if (adios2::Variable v = io.InquireVariable<T>(name); v)
122 if (v.Count() != count and v.ShapeID() == adios2::ShapeID::LocalArray)
123 v.SetSelection({start, count});
127 return io.DefineVariable<T>(name, shape, start, count);
131template <std::
floating_po
int T>
132std::shared_ptr<const mesh::Mesh<T>>
133extract_common_mesh(
const typename adios2_writer::U<T>& u)
137 auto mesh = std::visit([](
auto&& u) {
return u->function_space()->mesh(); },
147 if (mesh != u->function_space()->mesh())
149 throw std::runtime_error(
150 "ADIOS2Writer only supports functions sharing the same mesh");
166std::stringstream create_vtk_schema(
const std::vector<std::string>& point_data,
167 const std::vector<std::string>& cell_data);
170template <std::
floating_po
int T>
171std::vector<std::string>
172extract_function_names(
const typename adios2_writer::U<T>& u)
174 std::vector<std::string> names;
180 using U = std::decay_t<
decltype(u)>;
181 using X =
typename U::element_type;
182 if constexpr (std::is_floating_point_v<typename X::value_type>)
183 names.push_back(u->name);
186 names.push_back(u->name + impl_adios2::field_ext[0]);
187 names.push_back(u->name + impl_adios2::field_ext[1]);
205template <
typename T, std::
floating_po
int X>
206void vtx_write_data(adios2::IO& io, adios2::Engine& engine,
207 const fem::Function<T, X>& u)
211 std::span<const T> u_vector = u.x()->array();
215 std::span<const std::size_t> value_shape
216 = u.function_space()->element()->value_shape();
217 int rank = value_shape.size();
218 int num_comp = std::reduce(value_shape.begin(), value_shape.end(), 1,
220 if (num_comp < std::pow(3, rank))
221 num_comp = std::pow(3, rank);
223 std::shared_ptr<const fem::DofMap> dofmap = u.function_space()->dofmap();
225 std::shared_ptr<const common::IndexMap> index_map = dofmap->index_map;
227 int index_map_bs = dofmap->index_map_bs();
228 int dofmap_bs = dofmap->bs();
229 std::uint32_t num_dofs = index_map_bs
230 * (index_map->size_local() + index_map->num_ghosts())
232 if constexpr (std::is_scalar_v<T>)
235 std::vector<T> data(num_dofs * num_comp, 0);
236 for (std::size_t i = 0; i < num_dofs; ++i)
237 for (
int j = 0; j < index_map_bs; ++j)
238 data[i * num_comp + j] = u_vector[i * index_map_bs + j];
240 adios2::Variable output = impl_adios2::define_variable<T>(
241 io, u.name, {}, {}, {num_dofs, num_comp});
242 engine.Put(output, data.data(), adios2::Mode::Sync);
247 using U =
typename T::value_type;
249 std::vector<U> data(num_dofs * num_comp, 0);
250 for (std::size_t i = 0; i < num_dofs; ++i)
251 for (
int j = 0; j < index_map_bs; ++j)
252 data[i * num_comp + j] = std::real(u_vector[i * index_map_bs + j]);
254 adios2::Variable output_real = impl_adios2::define_variable<U>(
255 io, u.name + impl_adios2::field_ext[0], {}, {}, {num_dofs, num_comp});
256 engine.Put(output_real, data.data(), adios2::Mode::Sync);
258 std::ranges::fill(data, 0);
259 for (std::size_t i = 0; i < num_dofs; ++i)
260 for (
int j = 0; j < index_map_bs; ++j)
261 data[i * num_comp + j] = std::imag(u_vector[i * index_map_bs + j]);
262 adios2::Variable output_imag = impl_adios2::define_variable<U>(
263 io, u.name + impl_adios2::field_ext[1], {}, {}, {num_dofs, num_comp});
264 engine.Put(output_imag, data.data(), adios2::Mode::Sync);
272template <std::
floating_po
int T>
273void vtx_write_mesh(adios2::IO& io, adios2::Engine& engine,
274 const mesh::Mesh<T>& mesh)
276 const mesh::Geometry<T>& geometry = mesh.geometry();
277 auto topology = mesh.topology();
281 std::shared_ptr<const common::IndexMap> x_map = geometry.index_map();
282 std::uint32_t num_vertices = x_map->size_local() + x_map->num_ghosts();
283 adios2::Variable local_geometry = impl_adios2::define_variable<T>(
284 io,
"geometry", {}, {}, {num_vertices, 3});
285 engine.Put(local_geometry, geometry.x().data());
289 adios2::Variable vertices = impl_adios2::define_variable<std::uint32_t>(
290 io,
"NumberOfNodes", {adios2::LocalValueDim});
291 engine.Put<std::uint32_t>(vertices, num_vertices);
293 auto [vtkcells, shape]
294 = io::extract_vtk_connectivity(geometry.dofmap(), topology->cell_type());
297 int tdim = topology->dim();
298 adios2::Variable cell_var = impl_adios2::define_variable<std::uint32_t>(
299 io,
"NumberOfCells", {adios2::LocalValueDim});
300 engine.Put<std::uint32_t>(cell_var, shape[0]);
301 adios2::Variable celltype_var
302 = impl_adios2::define_variable<std::uint32_t>(io,
"types");
303 engine.Put<std::uint32_t>(
304 celltype_var, cells::get_vtk_cell_type(topology->cell_type(), tdim));
309 std::vector<std::int64_t>
cells(shape[0] * (shape[1] + 1), shape[1]);
310 for (std::size_t c = 0; c < shape[0]; ++c)
312 std::span vtkcell(vtkcells.data() + c * shape[1], shape[1]);
313 std::span cell(
cells.data() + c * (shape[1] + 1), shape[1] + 1);
314 std::ranges::copy(vtkcell, std::next(cell.begin()));
318 adios2::Variable local_topology = impl_adios2::define_variable<std::int64_t>(
319 io,
"connectivity", {}, {}, {shape[0], shape[1] + 1});
320 engine.Put(local_topology,
cells.data());
323 adios2::Variable orig_id = impl_adios2::define_variable<std::int64_t>(
324 io,
"vtkOriginalPointIds", {}, {}, {num_vertices});
325 engine.Put(orig_id, geometry.input_global_indices().data());
327 std::vector<std::uint8_t> x_ghost(num_vertices, 0);
328 std::fill(std::next(x_ghost.begin(), x_map->size_local()), x_ghost.end(), 1);
329 adios2::Variable ghost = impl_adios2::define_variable<std::uint8_t>(
330 io,
"vtkGhostType", {}, {}, {x_ghost.size()});
331 engine.Put(ghost, x_ghost.data());
332 engine.PerformPuts();
342template <std::
floating_po
int T>
343std::pair<std::vector<std::int64_t>, std::vector<std::uint8_t>>
344vtx_write_mesh_from_space(adios2::IO& io, adios2::Engine& engine,
345 const fem::FunctionSpace<T>& V)
347 auto mesh = V.mesh();
349 auto topology = mesh->topology();
351 int tdim = topology->dim();
354 auto [x, xshape, x_id, x_ghost, vtk, vtkshape] = io::vtk_mesh_from_space(V);
356 std::uint32_t num_dofs = xshape[0];
363 std::vector<std::int64_t>
cells(vtkshape[0] * (vtkshape[1] + 1), vtkshape[1]);
366 for (std::size_t c = 0; c < vtkshape[0]; ++c)
368 std::span vtkcell(vtk.data() + c * vtkshape[1], vtkshape[1]);
369 std::span cell(
cells.data() + c * (vtkshape[1] + 1), vtkshape[1] + 1);
370 std::ranges::copy(vtkcell, std::next(cell.begin()));
375 adios2::Variable local_geometry
376 = impl_adios2::define_variable<T>(io,
"geometry", {}, {}, {num_dofs, 3});
377 adios2::Variable local_topology = impl_adios2::define_variable<std::int64_t>(
378 io,
"connectivity", {}, {}, {vtkshape[0], vtkshape[1] + 1});
379 adios2::Variable cell_type
380 = impl_adios2::define_variable<std::uint32_t>(io,
"types");
381 adios2::Variable vertices = impl_adios2::define_variable<std::uint32_t>(
382 io,
"NumberOfNodes", {adios2::LocalValueDim});
383 adios2::Variable elements = impl_adios2::define_variable<std::uint32_t>(
384 io,
"NumberOfEntities", {adios2::LocalValueDim});
387 engine.Put<std::uint32_t>(vertices, num_dofs);
388 engine.Put<std::uint32_t>(elements, vtkshape[0]);
389 engine.Put<std::uint32_t>(
390 cell_type, cells::get_vtk_cell_type(topology->cell_type(), tdim));
391 engine.Put(local_geometry, x.data());
392 engine.Put(local_topology,
cells.data());
395 adios2::Variable orig_id = impl_adios2::define_variable<std::int64_t>(
396 io,
"vtkOriginalPointIds", {}, {}, {x_id.size()});
397 engine.Put(orig_id, x_id.data());
398 adios2::Variable ghost = impl_adios2::define_variable<std::uint8_t>(
399 io,
"vtkGhostType", {}, {}, {x_ghost.size()});
400 engine.Put(ghost, x_ghost.data());
402 engine.PerformPuts();
403 return {std::move(x_id), std::move(x_ghost)};
408enum class VTXMeshPolicy
420template <std::
floating_po
int T>
421class VTXWriter :
public ADIOS2Writer
435 VTXWriter(MPI_Comm comm,
const std::filesystem::path& filename,
436 std::shared_ptr<
const mesh::Mesh<T>> mesh,
437 std::string engine =
"BPFile")
438 : ADIOS2Writer(comm, filename,
"VTX mesh writer", engine), _mesh(mesh),
439 _mesh_reuse_policy(VTXMeshPolicy::update), _is_piecewise_constant(false)
442 std::string vtk_scheme = impl_vtx::create_vtk_schema({}, {}).str();
443 impl_adios2::define_attribute<std::string>(*_io,
"vtk.xml", vtk_scheme);
461 VTXWriter(MPI_Comm comm,
const std::filesystem::path& filename,
462 const typename adios2_writer::U<T>& u, std::string engine,
463 VTXMeshPolicy mesh_policy = VTXMeshPolicy::update)
464 : ADIOS2Writer(comm, filename,
"VTX function writer", engine),
465 _mesh(impl_adios2::extract_common_mesh<T>(u)),
466 _mesh_reuse_policy(mesh_policy), _u(u), _is_piecewise_constant(false)
469 throw std::runtime_error(
"VTXWriter fem::Function list is empty.");
472 auto V0 = std::visit([](
auto& u) { return u->function_space().get(); },
475 auto element0 = V0->element().get();
479 if (element0->is_mixed())
481 throw std::runtime_error(
482 "Mixed functions are not supported by VTXWriter.");
488 if (!element0->interpolation_ident())
490 throw std::runtime_error(
491 "Only (discontinuous) Lagrange functions are "
492 "supported. Interpolate Functions before output.");
496 if (element0->space_dimension() / element0->block_size() == 1)
497 _is_piecewise_constant =
true;
505 auto element = u->function_space()->element();
507 if (*element != *V0->element().get())
509 throw std::runtime_error(
"All functions in VTXWriter must have "
510 "the same element type.");
513 auto dmap0 = V0->dofmap()->map();
514 auto dmap = u->function_space()->dofmap()->map();
515 if (dmap0.size() != dmap.size()
516 or !std::equal(dmap0.data_handle(),
517 dmap0.data_handle() + dmap0.size(),
520 throw std::runtime_error(
521 "All functions must have the same dofmap for VTXWriter.");
529 std::vector<std::string> names = impl_vtx::extract_function_names<T>(u);
530 std::string vtk_scheme;
531 if (_is_piecewise_constant)
532 vtk_scheme = impl_vtx::create_vtk_schema({}, names).str();
534 vtk_scheme = impl_vtx::create_vtk_schema(names, {}).str();
536 impl_adios2::define_attribute<std::string>(*_io,
"vtk.xml", vtk_scheme);
553 VTXWriter(MPI_Comm comm,
const std::filesystem::path& filename,
554 const typename adios2_writer::U<T>& u,
555 VTXMeshPolicy mesh_policy = VTXMeshPolicy::update)
556 : VTXWriter(comm, filename, u,
"BPFile", mesh_policy)
561 VTXWriter(
const VTXWriter&) =
delete;
564 VTXWriter(VTXWriter&& file) =
default;
567 ~VTXWriter() =
default;
570 VTXWriter& operator=(VTXWriter&&) =
default;
573 VTXWriter& operator=(
const VTXWriter&) =
delete;
580 adios2::Variable var_step
581 = impl_adios2::define_variable<double>(*_io,
"step");
584 _engine->BeginStep();
585 _engine->template Put<double>(var_step, t);
588 if (_is_piecewise_constant or _u.empty())
590 impl_vtx::vtx_write_mesh(*_io, *_engine, *_mesh);
591 if (_is_piecewise_constant)
595 std::visit([&](
auto& u)
596 { impl_vtx::vtx_write_data(*_io, *_engine, *u); }, v);
602 if (_mesh_reuse_policy == VTXMeshPolicy::update
603 or !(_io->template InquireVariable<std::int64_t>(
"connectivity")))
607 std::tie(_x_id, _x_ghost) = std::visit(
610 return impl_vtx::vtx_write_mesh_from_space(*_io, *_engine,
611 *u->function_space());
618 adios2::Variable orig_id = impl_adios2::define_variable<std::int64_t>(
619 *_io,
"vtkOriginalPointIds", {}, {}, {_x_id.size()});
620 _engine->Put(orig_id, _x_id.data());
621 adios2::Variable ghost = impl_adios2::define_variable<std::uint8_t>(
622 *_io,
"vtkGhostType", {}, {}, {_x_ghost.size()});
623 _engine->Put(ghost, _x_ghost.data());
624 _engine->PerformPuts();
630 std::visit([&](
auto& u)
631 { impl_vtx::vtx_write_data(*_io, *_engine, *u); }, v);
639 std::shared_ptr<const mesh::Mesh<T>> _mesh;
640 adios2_writer::U<T> _u;
644 VTXMeshPolicy _mesh_reuse_policy;
645 std::vector<std::int64_t> _x_id;
646 std::vector<std::uint8_t> _x_ghost;
649 bool _is_piecewise_constant;
653template <
typename U,
typename T>
654VTXWriter(MPI_Comm comm, U filename, T mesh)
655 -> VTXWriter<
typename std::remove_cvref<
656 typename T::element_type>::type::geometry_type::value_type>;
Degree-of-freedom map representations and tools.
int rank(MPI_Comm comm)
Return process rank for the communicator.
Definition MPI.cpp:64
void cells(la::SparsityPattern &pattern, std::array< std::span< const std::int32_t >, 2 > cells, std::array< std::reference_wrapper< const DofMap >, 2 > dofmaps)
Iterate over cells and insert entries into sparsity pattern.
Definition sparsitybuild.cpp:16
Finite element method functionality.
Definition assemble_matrix_impl.h:26
Support for file IO.
Definition cells.h:119