OGS
MeshLib::IO::XdmfHdfWriter Class Referencefinal

Detailed Description

Definition at line 26 of file XdmfHdfWriter.h.

#include <XdmfHdfWriter.h>

Public Member Functions

 XdmfHdfWriter (std::vector< std::reference_wrapper< const MeshLib::Mesh >> meshes, std::filesystem::path const &filepath, unsigned long long time_step, double initial_time, std::set< std::string > const &variable_output_names, bool use_compression, unsigned int n_files)
 Write xdmf and h5 file with geometry and topology data. More...
 
void writeStep (double time)
 Adds data for either lazy (xdmf) or eager (hdf) writing algorithm. More...
 

Private Attributes

std::unique_ptr< HdfWriter_hdf_writer
 
std::vector< std::unique_ptr< XdmfWriter > > _xdmf_writer
 

Constructor & Destructor Documentation

◆ XdmfHdfWriter()

MeshLib::IO::XdmfHdfWriter::XdmfHdfWriter ( std::vector< std::reference_wrapper< const MeshLib::Mesh >>  meshes,
std::filesystem::path const &  filepath,
unsigned long long  time_step,
double  initial_time,
std::set< std::string > const &  variable_output_names,
bool  use_compression,
unsigned int  n_files 
)

Write xdmf and h5 file with geometry and topology data.

Parameters
meshesMeshes or NodePartitionedMeshes to be written to file(s)
filepathabsolute or relative filepath to the hdf5 file
time_stepnumber of the step (temporal collection)
initial_timetime in seconds of the first time step
variable_output_namesnames of all process variables (attributes) that change over time
use_compressionif true, zlib compression in HDFWriter component is used
n_filesnumber of hdf5 output files

Definition at line 37 of file XdmfHdfWriter.cpp.

43 {
44  // ogs meshes to vector of Xdmf/HDF meshes (we keep Xdmf and HDF together
45  // because XDMF depends on HDF) to meta
46 
47  // if no output name is specified, all data will be assumened to be
48  // variable over the timesteps. The xdmfhdfwriter is an alternative to
49  // other writers, that do not consider the constantness of data Callers
50  // of xdmfwriter (e.g. ogs tools) do not provide these information yet
51  // and indicate with empty list
52  std::function<bool(HdfData)> const is_variable_hdf_attribute =
53  [&variable_output_names](
54  bool outputnames) -> std::function<bool(HdfData)>
55  {
56  if (outputnames)
57  {
58  return [&variable_output_names](HdfData const& data) -> bool
59  {
60  return std::find(variable_output_names.begin(),
61  variable_output_names.end(),
62  data.name) != variable_output_names.end();
63  };
64  }
65  else
66  {
67  return [](HdfData const&) -> bool { return true; };
68  }
69  }(!variable_output_names.empty());
70 
71  auto is_variable_xdmf_attribute =
72  [&variable_output_names](XdmfData const& data) -> bool
73  {
74  return std::find(variable_output_names.begin(),
75  variable_output_names.end(),
76  data.name) != variable_output_names.end();
77  };
78 
79  // Transform the data to be written into a format conforming with the rules
80  // of xdmf topology and geometry
81  auto const transform_ogs_mesh_data_to_xdmf_conforming_data =
82  [&n_files](auto const& mesh)
83  {
84  auto flattened_geometry_values = transformToXDMFGeometry(mesh);
85  // actually this line is only needed to calculate the offset
86  XdmfHdfData const& geometry =
87  transformGeometry(mesh, flattened_geometry_values.data(), n_files);
88  auto const flattened_topology_values =
89  transformToXDMFTopology(mesh, geometry.hdf.offsets[0]);
90  return std::make_unique<TransformedMeshData>(
91  TransformedMeshData{std::move(flattened_geometry_values),
92  std::move(flattened_topology_values)});
93  };
94 
95  // create metadata for transformed data and original ogs mesh data
96  auto const transform_to_meta_data =
97  [&transform_ogs_mesh_data_to_xdmf_conforming_data,
98  &n_files](auto const& mesh)
99  {
100  // important: transformed data must survive and be unique, raw pointer
101  // to its memory!
102  std::unique_ptr<TransformedMeshData> xdmf_conforming_data =
103  transform_ogs_mesh_data_to_xdmf_conforming_data(mesh);
104  auto const geometry = transformGeometry(
105  mesh, xdmf_conforming_data->flattened_geometry_values.data(),
106  n_files);
107  auto const topology = transformTopology(
108  xdmf_conforming_data->flattened_topology_values, n_files);
109  auto const attributes = transformAttributes(mesh, n_files);
110  return XdmfHdfMesh{std::move(geometry), std::move(topology),
111  std::move(attributes), mesh.get().getName(),
112  std::move(xdmf_conforming_data)};
113  };
114 
115  // extract meta data relevant for HDFWriter
116  auto const transform_metamesh_to_hdf =
117  [&is_variable_hdf_attribute](auto const& metamesh)
118  {
119  // topology and geometry can be treated as any other attribute
120  std::vector<HdfData> hdf_data_attributes = {metamesh.geometry.hdf,
121  metamesh.topology.hdf};
122 
123  hdf_data_attributes.reserve(hdf_data_attributes.size() +
124  metamesh.attributes.size());
125  std::transform(metamesh.attributes.begin(), metamesh.attributes.end(),
126  std::back_inserter(hdf_data_attributes),
127  [](XdmfHdfData att) -> HdfData { return att.hdf; });
128 
129  HDFAttributes constant_attributes;
130  std::copy_if(hdf_data_attributes.begin(), hdf_data_attributes.end(),
131  back_inserter(constant_attributes),
132  std::not_fn(is_variable_hdf_attribute));
133  HDFAttributes variable_attributes;
134  std::copy_if(hdf_data_attributes.begin(), hdf_data_attributes.end(),
135  back_inserter(variable_attributes),
136  is_variable_hdf_attribute);
137 
138  return MeshHdfData{
139  .constant_attributes = std::move(constant_attributes),
140  .variable_attributes = std::move(variable_attributes),
141  .name = std::move(metamesh.name)};
142  };
143 
144  // --------------- XDMF + HDF ---------------------
145  std::vector<XdmfHdfMesh> xdmf_hdf_meshes;
146  xdmf_hdf_meshes.reserve(meshes.size());
147  std::transform(meshes.begin(), meshes.end(),
148  std::back_inserter(xdmf_hdf_meshes), transform_to_meta_data);
149 
150  std::vector<MeshHdfData> hdf_meshes;
151  hdf_meshes.reserve(xdmf_hdf_meshes.size());
152  std::transform(xdmf_hdf_meshes.begin(), xdmf_hdf_meshes.end(),
153  std::back_inserter(hdf_meshes), transform_metamesh_to_hdf);
154 
155  // --------------- HDF ---------------------
156  std::filesystem::path const hdf_filepath =
157  filepath.parent_path() / (filepath.stem().string() + ".h5");
158 
159  auto const is_file_manager = isFileManager();
160  _hdf_writer = std::make_unique<HdfWriter>(std::move(hdf_meshes), time_step,
161  hdf_filepath, use_compression,
162  is_file_manager, n_files);
163 
164  // --------------- XDMF ---------------------
165  // The light data is only written by just one process
166  if (!is_file_manager)
167  {
168  return;
169  }
170 
171  // xdmf section
172  // extract meta data relevant for XDMFWriter
173  auto const transform_metamesh_to_xdmf =
174  [&is_variable_xdmf_attribute, &filepath, &hdf_filepath,
175  &initial_time](XdmfHdfMesh& metamesh)
176  {
177  std::string const xdmf_name = metamesh.name;
178  std::filesystem::path const xdmf_filepath =
179  filepath.parent_path() /
180  (filepath.stem().string() + "_" + xdmf_name + ".xdmf");
181 
182  std::vector<XdmfData> xdmf_attributes;
183  std::transform(metamesh.attributes.begin(), metamesh.attributes.end(),
184  std::back_inserter(xdmf_attributes),
185  [](XdmfHdfData const& att) -> XdmfData
186  { return att.xdmf; });
187 
188  for (std::size_t i = 0; i < metamesh.attributes.size(); ++i)
189  {
190  // index 1 time, index 2 geo, index 3 topology, attributes start at
191  // index 4
192  xdmf_attributes[i].index = i + 4;
193  }
194 
195  std::vector<XdmfData> xdmf_variable_attributes;
196  std::copy_if(xdmf_attributes.begin(), xdmf_attributes.end(),
197  back_inserter(xdmf_variable_attributes),
198  is_variable_xdmf_attribute);
199  std::vector<XdmfData> xdmf_constant_attributes;
200  std::copy_if(xdmf_attributes.begin(), xdmf_attributes.end(),
201  back_inserter(xdmf_constant_attributes),
202  std::not_fn(is_variable_xdmf_attribute));
203 
204  auto const xdmf_writer_fn =
205  write_xdmf(metamesh.geometry.xdmf, metamesh.topology.xdmf,
206  xdmf_constant_attributes, xdmf_variable_attributes,
207  hdf_filepath.filename().string(),
209  auto xdmf_writer = std::make_unique<XdmfWriter>(xdmf_filepath.string(),
210  xdmf_writer_fn);
211  xdmf_writer->addTimeStep(initial_time);
212  return xdmf_writer;
213  };
214 
215  std::transform(xdmf_hdf_meshes.begin(), xdmf_hdf_meshes.end(),
216  std::back_inserter(_xdmf_writer),
217  transform_metamesh_to_xdmf);
218 }
std::unique_ptr< HdfWriter > _hdf_writer
Definition: XdmfHdfWriter.h:55
std::vector< std::unique_ptr< XdmfWriter > > _xdmf_writer
Definition: XdmfHdfWriter.h:56
GITINFOLIB_EXPORT const std::string ogs_version
std::vector< XdmfHdfData > transformAttributes(MeshLib::Mesh const &mesh, unsigned int const n_files)
Create meta data for attributes used for hdf5 and xdmf.
XdmfHdfData transformGeometry(MeshLib::Mesh const &mesh, double const *data_ptr, unsigned int const n_files)
Create meta data for geometry used for hdf5 and xdmf.
std::vector< int > transformToXDMFTopology(MeshLib::Mesh const &mesh, std::size_t const offset)
Copies all cells into a new vector. Contiguous data used for writing. The topology is specific to xdm...
std::vector< double > transformToXDMFGeometry(MeshLib::Mesh const &mesh)
Copies all node points into a new vector. Contiguous data used for writing. Conform with XDMF standar...
std::function< std::string(std::vector< double >)> write_xdmf(XdmfData const &geometry, XdmfData const &topology, std::vector< XdmfData > const &constant_attributes, std::vector< XdmfData > const &variable_attributes, std::string const &h5filename, std::string const &ogs_version, std::string const &mesh_name)
Generator function that creates a function capturing the spatial data of a mesh Temporal data can lat...
Definition: writeXdmf.cpp:100
std::vector< HdfData > HDFAttributes
Definition: HdfWriter.h:25
XdmfHdfData transformTopology(std::vector< int > const &values, unsigned int const n_files)
Create meta data for topology used for HDF5 and XDMF.
bool isFileManager()
Definition: partition.cpp:26

References _hdf_writer, _xdmf_writer, MeshLib::IO::MeshHdfData::constant_attributes, MeshLib::IO::XdmfHdfMesh::geometry, MeshLib::IO::XdmfHdfData::hdf, MeshLib::IO::isFileManager(), MeshLib::IO::HdfData::offsets, GitInfoLib::GitInfo::ogs_version, MeshLib::IO::transformAttributes(), MeshLib::IO::transformGeometry(), MeshLib::IO::transformTopology(), MeshLib::IO::transformToXDMFGeometry(), MeshLib::IO::transformToXDMFTopology(), and MeshLib::IO::write_xdmf().

Member Function Documentation

◆ writeStep()

void MeshLib::IO::XdmfHdfWriter::writeStep ( double  time)

Adds data for either lazy (xdmf) or eager (hdf) writing algorithm.

Parameters
timetime value of the current time_step

Definition at line 220 of file XdmfHdfWriter.cpp.

221 {
222  // ToDo (tm) time_step will be used for simulation continuation (restart)
223  _hdf_writer->writeStep(time);
224  // The light data is only written by just one process
225  if (isFileManager())
226  {
227  for (auto const& xdmf_writer : _xdmf_writer)
228  {
229  xdmf_writer->addTimeStep(time);
230  }
231  }
232 }

References _hdf_writer, _xdmf_writer, and MeshLib::IO::isFileManager().

Member Data Documentation

◆ _hdf_writer

std::unique_ptr<HdfWriter> MeshLib::IO::XdmfHdfWriter::_hdf_writer
private

Definition at line 55 of file XdmfHdfWriter.h.

Referenced by XdmfHdfWriter(), and writeStep().

◆ _xdmf_writer

std::vector<std::unique_ptr<XdmfWriter> > MeshLib::IO::XdmfHdfWriter::_xdmf_writer
private

Definition at line 56 of file XdmfHdfWriter.h.

Referenced by XdmfHdfWriter(), and writeStep().


The documentation for this class was generated from the following files: