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::tuple<std::vector<std::string>, std::vector<std::string>>
172extract_function_names(
const typename adios2_writer::U<T>& u)
174 std::vector<std::string> names, dg0_names;
178 [&names, &dg0_names](
auto&& u)
180 using U = std::decay_t<
decltype(u)>;
181 using X =
typename U::element_type;
182 std::vector<std::string>* fnames = &names;
183 if (impl::is_cellwise(*(u->function_space()->element())))
187 if constexpr (std::is_floating_point_v<typename X::value_type>)
188 fnames->push_back(u->name);
191 fnames->push_back(u->name + impl_adios2::field_ext[0]);
192 fnames->push_back(u->name + impl_adios2::field_ext[1]);
198 return {names, dg0_names};
210template <
typename T, std::
floating_po
int X>
211void vtx_write_data(adios2::IO& io, adios2::Engine& engine,
212 const fem::Function<T, X>& u)
216 std::span<const T> u_vector = u.x()->array();
220 std::span<const std::size_t> value_shape
221 = u.function_space()->element()->value_shape();
222 std::size_t
rank = value_shape.size();
223 std::size_t num_comp = std::reduce(value_shape.begin(), value_shape.end(), 1,
225 if (num_comp < std::pow(3, rank))
226 num_comp = std::pow(3, rank);
228 std::shared_ptr<const fem::DofMap> dofmap = u.function_space()->dofmap();
230 std::shared_ptr<const common::IndexMap> index_map = dofmap->index_map;
232 int index_map_bs = dofmap->index_map_bs();
233 int dofmap_bs = dofmap->bs();
234 std::uint32_t num_dofs = index_map_bs
235 * (index_map->size_local() + index_map->num_ghosts())
237 if constexpr (std::is_scalar_v<T>)
240 std::vector<T> data(num_dofs * num_comp, 0);
241 for (std::size_t i = 0; i < num_dofs; ++i)
242 for (
int j = 0; j < index_map_bs; ++j)
243 data[i * num_comp + j] = u_vector[i * index_map_bs + j];
245 adios2::Variable output = impl_adios2::define_variable<T>(
246 io, u.name, {}, {}, {num_dofs, num_comp});
247 spdlog::debug(
"Output data size={}", data.size());
248 engine.Put(output, data.data(), adios2::Mode::Sync);
253 using U =
typename T::value_type;
255 std::vector<U> data(num_dofs * num_comp, 0);
256 for (std::size_t i = 0; i < num_dofs; ++i)
257 for (
int j = 0; j < index_map_bs; ++j)
258 data[i * num_comp + j] = std::real(u_vector[i * index_map_bs + j]);
260 adios2::Variable output_real = impl_adios2::define_variable<U>(
261 io, u.name + impl_adios2::field_ext[0], {}, {}, {num_dofs, num_comp});
262 engine.Put(output_real, data.data(), adios2::Mode::Sync);
264 std::ranges::fill(data, 0);
265 for (std::size_t i = 0; i < num_dofs; ++i)
266 for (
int j = 0; j < index_map_bs; ++j)
267 data[i * num_comp + j] = std::imag(u_vector[i * index_map_bs + j]);
268 adios2::Variable output_imag = impl_adios2::define_variable<U>(
269 io, u.name + impl_adios2::field_ext[1], {}, {}, {num_dofs, num_comp});
270 engine.Put(output_imag, data.data(), adios2::Mode::Sync);
278template <std::
floating_po
int T>
279void vtx_write_mesh(adios2::IO& io, adios2::Engine& engine,
280 const mesh::Mesh<T>& mesh)
282 const mesh::Geometry<T>& geometry = mesh.geometry();
283 auto topology = mesh.topology();
287 std::shared_ptr<const common::IndexMap> x_map = geometry.index_map();
288 std::uint32_t num_vertices = x_map->size_local() + x_map->num_ghosts();
289 adios2::Variable local_geometry = impl_adios2::define_variable<T>(
290 io,
"geometry", {}, {}, {num_vertices, 3});
291 spdlog::debug(
"Put local_geometry: {}x3", num_vertices);
292 engine.Put(local_geometry, geometry.x().data());
296 adios2::Variable vertices = impl_adios2::define_variable<std::uint32_t>(
297 io,
"NumberOfNodes", {adios2::LocalValueDim});
298 engine.Put<std::uint32_t>(vertices, num_vertices);
300 auto [vtkcells, shape]
301 = io::extract_vtk_connectivity(geometry.dofmap(), topology->cell_type());
304 int tdim = topology->dim();
305 adios2::Variable cell_var = impl_adios2::define_variable<std::uint32_t>(
306 io,
"NumberOfCells", {adios2::LocalValueDim});
307 engine.Put<std::uint32_t>(cell_var, shape[0]);
308 spdlog::debug(
"Put local_cells: {}", shape[0]);
310 adios2::Variable celltype_var
311 = impl_adios2::define_variable<std::uint32_t>(io,
"types");
312 engine.Put<std::uint32_t>(
313 celltype_var, cells::get_vtk_cell_type(topology->cell_type(), tdim));
318 std::vector<std::int64_t>
cells(shape[0] * (shape[1] + 1), shape[1]);
319 for (std::size_t c = 0; c < shape[0]; ++c)
321 std::span vtkcell(vtkcells.data() + c * shape[1], shape[1]);
322 std::span cell(
cells.data() + c * (shape[1] + 1), shape[1] + 1);
323 std::ranges::copy(vtkcell, std::next(cell.begin()));
327 adios2::Variable local_topology = impl_adios2::define_variable<std::int64_t>(
328 io,
"connectivity", {}, {}, {shape[0], shape[1] + 1});
329 spdlog::debug(
"Put local_topology: {}x{}", shape[0], shape[1] + 1);
331 engine.Put(local_topology,
cells.data());
334 adios2::Variable orig_id = impl_adios2::define_variable<std::int64_t>(
335 io,
"vtkOriginalPointIds", {}, {}, {num_vertices});
336 engine.Put(orig_id, geometry.input_global_indices().data());
338 std::vector<std::uint8_t> x_ghost(num_vertices, 0);
339 std::fill(std::next(x_ghost.begin(), x_map->size_local()), x_ghost.end(), 1);
340 adios2::Variable ghost = impl_adios2::define_variable<std::uint8_t>(
341 io,
"vtkGhostType", {}, {}, {x_ghost.size()});
342 engine.Put(ghost, x_ghost.data());
343 engine.PerformPuts();
353template <std::
floating_po
int T>
354std::pair<std::vector<std::int64_t>, std::vector<std::uint8_t>>
355vtx_write_mesh_from_space(adios2::IO& io, adios2::Engine& engine,
356 const fem::FunctionSpace<T>& V)
358 auto mesh = V.mesh();
360 auto topology = mesh->topology();
362 int tdim = topology->dim();
365 auto [x, xshape, x_id, x_ghost, vtk, vtkshape] = io::vtk_mesh_from_space(V);
367 spdlog::debug(
"x={}, xshape={}x{}, x_id={}, x_ghost={}", x.size(), xshape[0],
368 xshape[1], x_id.size(), x_ghost.size());
370 std::uint32_t num_dofs = xshape[0];
376 spdlog::debug(
"Create cells: [{}x{}]", vtkshape[0], vtkshape[1]);
379 std::vector<std::int64_t>
cells(vtkshape[0] * (vtkshape[1] + 1), vtkshape[1]);
382 for (std::size_t c = 0; c < vtkshape[0]; ++c)
384 std::span vtkcell(vtk.data() + c * vtkshape[1], vtkshape[1]);
385 std::span cell(
cells.data() + c * (vtkshape[1] + 1), vtkshape[1] + 1);
386 std::ranges::copy(vtkcell, std::next(cell.begin()));
391 adios2::Variable local_geometry
392 = impl_adios2::define_variable<T>(io,
"geometry", {}, {}, {num_dofs, 3});
393 adios2::Variable local_topology = impl_adios2::define_variable<std::int64_t>(
394 io,
"connectivity", {}, {}, {vtkshape[0], vtkshape[1] + 1});
395 adios2::Variable cell_type
396 = impl_adios2::define_variable<std::uint32_t>(io,
"types");
397 adios2::Variable vertices = impl_adios2::define_variable<std::uint32_t>(
398 io,
"NumberOfNodes", {adios2::LocalValueDim});
399 adios2::Variable elements = impl_adios2::define_variable<std::uint32_t>(
400 io,
"NumberOfEntities", {adios2::LocalValueDim});
403 spdlog::debug(
"vertices={}, elements={}, local_geom={}, local_cells={}",
404 num_dofs, vtkshape[0], x.size(),
cells.size());
405 engine.Put<std::uint32_t>(vertices, num_dofs);
406 engine.Put<std::uint32_t>(elements, vtkshape[0]);
407 engine.Put<std::uint32_t>(
408 cell_type, cells::get_vtk_cell_type(topology->cell_type(), tdim));
409 engine.Put(local_geometry, x.data());
410 engine.Put(local_topology,
cells.data());
413 adios2::Variable orig_id = impl_adios2::define_variable<std::int64_t>(
414 io,
"vtkOriginalPointIds", {}, {}, {x_id.size(), 1});
415 engine.Put(orig_id, x_id.data());
416 adios2::Variable ghost = impl_adios2::define_variable<std::uint8_t>(
417 io,
"vtkGhostType", {}, {}, {x_ghost.size(), 1});
418 engine.Put(ghost, x_ghost.data());
420 engine.PerformPuts();
421 return {std::move(x_id), std::move(x_ghost)};
426enum class VTXMeshPolicy : std::int8_t
438template <std::
floating_po
int T>
439class VTXWriter :
public ADIOS2Writer
453 VTXWriter(MPI_Comm comm,
const std::filesystem::path& filename,
454 std::shared_ptr<
const mesh::Mesh<T>> mesh,
455 std::string engine =
"BPFile")
456 : ADIOS2Writer(comm, filename,
"VTX mesh writer", engine), _mesh(mesh),
457 _mesh_reuse_policy(VTXMeshPolicy::update),
458 _has_piecewise_constant(false)
461 std::string vtk_scheme = impl_vtx::create_vtk_schema({}, {}).str();
462 impl_adios2::define_attribute<std::string>(*_io,
"vtk.xml", vtk_scheme);
480 VTXWriter(MPI_Comm comm,
const std::filesystem::path& filename,
481 const typename adios2_writer::U<T>& u, std::string engine,
482 VTXMeshPolicy mesh_policy = VTXMeshPolicy::update)
483 : ADIOS2Writer(comm, filename,
"VTX function writer", engine),
484 _mesh(impl_adios2::extract_common_mesh<T>(u)), _u(u),
485 _mesh_reuse_policy(mesh_policy), _has_piecewise_constant(false)
488 throw std::runtime_error(
"VTXWriter fem::Function list is empty.");
491 auto V0 = std::visit([](
auto& u) { return u->function_space().get(); },
496 bool has_V0_changed =
false;
500 [&V0, &has_V0_changed](
auto& u)
502 auto V = u->function_space().get();
504 if (!impl::is_cellwise(*V->element()))
507 has_V0_changed =
true;
514 auto element0 = V0->element().get();
518 if (element0->is_mixed())
520 throw std::runtime_error(
521 "Mixed functions are not supported by VTXWriter.");
527 if (!element0->interpolation_ident())
529 throw std::runtime_error(
530 "Only (discontinuous) Lagrange functions are "
531 "supported. Interpolate Functions before output.");
540 auto element = u->function_space()->element();
542 bool is_piecewise_constant = impl::is_cellwise(*element);
543 _has_piecewise_constant
544 = _has_piecewise_constant || is_piecewise_constant;
545 if (*element != *V0->element().get() and !is_piecewise_constant)
547 throw std::runtime_error(
"All functions in VTXWriter must have "
548 "the same element type.");
551 auto dmap0 = V0->dofmap()->map();
552 auto dmap = u->function_space()->dofmap()->map();
553 if ((dmap0.size() != dmap.size()
554 or !std::equal(dmap0.data_handle(),
555 dmap0.data_handle() + dmap0.size(),
557 and !is_piecewise_constant)
559 throw std::runtime_error(
560 "All functions must have the same dofmap for VTXWriter.");
568 auto [names, dg0_names] = impl_vtx::extract_function_names<T>(u);
569 std::string vtk_scheme;
570 vtk_scheme = impl_vtx::create_vtk_schema(names, dg0_names).str();
572 impl_adios2::define_attribute<std::string>(*_io,
"vtk.xml", vtk_scheme);
589 VTXWriter(MPI_Comm comm,
const std::filesystem::path& filename,
590 const typename adios2_writer::U<T>& u,
591 VTXMeshPolicy mesh_policy = VTXMeshPolicy::update)
592 : VTXWriter(comm, filename, u,
"BPFile", mesh_policy)
597 VTXWriter(
const VTXWriter&) =
delete;
600 VTXWriter(VTXWriter&& file) =
default;
603 ~VTXWriter() =
default;
606 VTXWriter& operator=(VTXWriter&&) =
default;
609 VTXWriter& operator=(
const VTXWriter&) =
delete;
616 adios2::Variable var_step
617 = impl_adios2::define_variable<double>(*_io,
"step");
619 spdlog::debug(
"ADIOS2: step");
621 _engine->BeginStep();
622 _engine->template Put<double>(var_step, t);
625 auto [names, dg0_names] = impl_vtx::extract_function_names<T>(_u);
626 if ((names.size() == 0) or _u.empty())
628 spdlog::debug(
"ADIOS2: write_mesh");
629 impl_vtx::vtx_write_mesh(*_io, *_engine, *_mesh);
633 if (_mesh_reuse_policy == VTXMeshPolicy::update
634 or !(_io->template InquireVariable<std::int64_t>(
"connectivity")))
638 std::tie(_x_id, _x_ghost) = std::visit(
641 spdlog::debug(
"ADIOS2: write_mesh_from_space");
642 return impl_vtx::vtx_write_mesh_from_space(*_io, *_engine,
643 *u->function_space());
650 adios2::Variable orig_id = impl_adios2::define_variable<std::int64_t>(
651 *_io,
"vtkOriginalPointIds", {}, {}, {_x_id.size()});
652 _engine->Put(orig_id, _x_id.data());
653 adios2::Variable ghost = impl_adios2::define_variable<std::uint8_t>(
654 *_io,
"vtkGhostType", {}, {}, {_x_ghost.size()});
655 _engine->Put(ghost, _x_ghost.data());
656 _engine->PerformPuts();
660 spdlog::debug(
"Write function data");
664 std::visit([&](
auto& u) { impl_vtx::vtx_write_data(*_io, *_engine, *u); },
672 std::shared_ptr<const mesh::Mesh<T>> _mesh;
673 adios2_writer::U<T> _u;
677 VTXMeshPolicy _mesh_reuse_policy;
678 std::vector<std::int64_t> _x_id;
679 std::vector<std::uint8_t> _x_ghost;
682 bool _has_piecewise_constant;
686template <
typename U,
typename T>
687VTXWriter(MPI_Comm comm, U filename, T mesh)
688 -> VTXWriter<
typename std::remove_cvref<
689 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_expression_impl.h:23
Support for file IO.
Definition cells.h:119