Commit 72171eb0 authored by Tobias Meisel's avatar Tobias Meisel
Browse files

Review follow up

parent 4434c70d
......@@ -86,16 +86,12 @@ XdmfHdfWriter::XdmfHdfWriter(MeshLib::Mesh const& mesh,
filepath.parent_path() / (filepath.stem().string() + ".xdmf");
std::vector<XdmfData> xdmf_attributes;
std::transform(attributes.begin(), attributes.end(),
std::back_inserter(xdmf_attributes),
[&attributes](AttributeMeta& att) -> XdmfData
{
size_t const i = &att - &attributes.front();
// index 1 geo, index 2 topology, attributes start at
// index 3
att.xdmf.index = i + 4;
return att.xdmf;
});
for (size_t i = 0; i < attributes.size(); ++i)
{
// index 1 geo, index 2 topology, attributes start at index 3
attributes[i].xdmf.index = i + 4;
}
std::function<bool(XdmfData)> is_variable_xdmf_attribute =
[&variable_output_names](XdmfData data) -> bool {
......@@ -125,8 +121,8 @@ XdmfHdfWriter::XdmfHdfWriter(MeshLib::Mesh const& mesh,
}
}
void XdmfHdfWriter::writeStep([[maybe_unused]] int const& time_step,
double const& time)
void XdmfHdfWriter::writeStep([[maybe_unused]] int const time_step,
double const time)
{
// ToDo (tm) time_step will be used for simulation continuation (restart)
_hdf_writer->writeStep();
......
......@@ -48,7 +48,7 @@ public:
* @param time_step number of the step (temporal collection)
* @param time time value of the current time_step
*/
void writeStep(int const& time_step, double const& time);
void writeStep(int time_step, double time);
private:
// hdf_writer must be destructed before xdmf_writer
......
......@@ -8,10 +8,10 @@
namespace MeshLib::IO
{
XdmfWriter::XdmfWriter(std::string const& xdmf_filename,
XdmfWriter::XdmfWriter(std::string xdmf_filename,
std::function<std::string(std::vector<double>)>
xdmf_writer_fn)
: filename(xdmf_filename), xdmf_writer(xdmf_writer_fn)
: filename(std::move(xdmf_filename)), xdmf_writer(xdmf_writer_fn)
{
}
......
......@@ -25,7 +25,7 @@ public:
* @param xdmf_filename absolute or relative filepath to the xdmf file
* @param xdmf_writer_fn function that generates xdmf string
*/
XdmfWriter(std::string const& xdmf_filename,
XdmfWriter(std::string xdmf_filename,
std::function<std::string(std::vector<double>)>
xdmf_writer_fn);
XdmfWriter(XdmfWriter&&) = default;
......
......@@ -62,9 +62,10 @@ constexpr auto elemOGSTypeToXDMFType()
return elem_type;
}
auto cellTypeOGS2XDMF(MeshLib::CellType const& cell_type)
constexpr auto elem_type_ogs2xdmf = elemOGSTypeToXDMFType();
constexpr auto cellTypeOGS2XDMF(MeshLib::CellType const& cell_type)
{
constexpr auto elem_type_ogs2xdmf = elemOGSTypeToXDMFType();
return elem_type_ogs2xdmf[to_underlying(cell_type)];
}
......
......@@ -6,6 +6,7 @@
#include <spdlog/fmt/bundled/format.h>
#include <algorithm>
#include <array>
#include <fstream>
#include <iostream>
#include <iterator>
......@@ -15,6 +16,7 @@
#include <string_view>
#include <vector>
#include "BaseLib/Error.h"
#include "BaseLib/cpp23.h"
#include "MeshLib/Location.h"
......@@ -34,7 +36,7 @@ constexpr char const* mesh_item_type_strings[] = {"Node", "Edge", "Face",
"Cell", "IntegrationPoint"};
// Transforms MeshItemType into string
static std::string mesh_item_type_string(
static std::string meshItemTypeString(
std::optional<MeshItemType> const& item_type)
{
/// Char array names for all of MeshItemType values.
......@@ -42,10 +44,7 @@ static std::string mesh_item_type_string(
{
return mesh_item_type_strings[static_cast<int>(item_type.value())];
}
else
{
return "";
}
OGS_FATAL("Mesh item type is unknown");
}
// Transforms MeshPropertyDataType into string
......@@ -89,11 +88,12 @@ static constexpr auto meshPropertyDatatypeSize()
return property_sizes;
}
inline auto ogs_to_xdmf_type = meshPropertyDatatypeSize();
// Transform MeshPropertyDataType into type_sizes (in bytes)
static std::string getPropertyDataTypeSize(
MeshPropertyDataType const& ogs_data_type)
{
constexpr auto ogs_to_xdmf_type = meshPropertyDatatypeSize();
return fmt::format("{}", ogs_to_xdmf_type[to_underlying(ogs_data_type)]);
}
......@@ -110,10 +110,8 @@ std::function<std::string(std::vector<double>)> write_xdmf(
// return a unary function
// _a suffix is a user defined literal for fmt named arguments
auto const time_dataitem_genfn = [](int const time_step, int const max_step,
std::string const h5filename)
{
return [time_step, max_step, h5filename](auto const& xdmfdata)
{
std::string const h5filename) {
return [time_step, max_step, h5filename](auto const& xdmfdata) {
return fmt::format(
"\n\t\t<DataItem DataType=\"{datatype}\" "
"Dimensions=\"{local_dimensions}\" "
......@@ -139,16 +137,15 @@ std::function<std::string(std::vector<double>)> write_xdmf(
};
// string_join could be moved to ogs lib if used more
auto const string_join_fn = [](auto const& collection)
{ return fmt::to_string(fmt::join(collection, "")); };
auto const string_join_fn = [](auto const& collection) {
return fmt::to_string(fmt::join(collection, ""));
};
// m_bind could be moved to ogs lib if used more
auto const m_bind_fn = [](auto const& transform, auto const& join)
{
return [join, transform](auto const& collection)
{
auto const m_bind_fn = [](auto const& transform, auto const& join) {
return [join, transform](auto const& collection) {
std::vector<std::string> temp;
temp.resize(collection.size());
temp.reserve(collection.size());
std::transform(collection.begin(), collection.end(),
std::back_inserter(temp), transform);
return join(temp);
......@@ -158,8 +155,7 @@ std::function<std::string(std::vector<double>)> write_xdmf(
// XDMF if part of the data that is already written in any previous step
// subsequent steps can refer to this data collection of elements navigates
// the xml tree (take first child, then in child take 2nd child ...)
auto const pointer_transfrom = [](auto const& elements)
{
auto const pointer_transfrom = [](auto const& elements) {
return fmt::format(
"\n\t<xi:include xpointer=\"element(/{elements})\"/>",
"elements"_a = fmt::join(elements, "/"));
......@@ -167,32 +163,29 @@ std::function<std::string(std::vector<double>)> write_xdmf(
// Defines content of <Attribute> in XDMF, child of Attribute is a single
// <DataItem>
auto const attribute_transform =
[](XdmfData const& attribute, auto const& dataitem_transform)
{
auto const attribute_transform = [](XdmfData const& attribute,
auto const& dataitem_transform) {
return fmt::format(
"\n\t<Attribute Center=\"{center}\" ElementCell=\"\" "
"ElementDegree=\"0\" "
"ElementFamily=\"\" ItemType=\"\" Name=\"{name}\" "
"Type=\"None\">{dataitem}\n\t</Attribute>",
"center"_a = mesh_item_type_string(attribute.attribute_center),
"center"_a = meshItemTypeString(attribute.attribute_center),
"name"_a = attribute.name,
"dataitem"_a = dataitem_transform(attribute));
};
// Define content of <Geometry> in XDMF, same as attribute_transform
auto const geometry_transform =
[](XdmfData const& geometry, auto const& dataitem_transform)
{
auto const geometry_transform = [](XdmfData const& geometry,
auto const& dataitem_transform) {
return fmt::format(
"\n\t<Geometry Origin=\"\" Type=\"XYZ\">{dataitem}\n\t</Geometry>",
"dataitem"_a = dataitem_transform(geometry));
};
// Define content of <Topology> in XDMF, same as attribute_transform
auto const topology_transform =
[](XdmfData const& topology, auto const& dataitem_transform)
{
auto const topology_transform = [](XdmfData const& topology,
auto const& dataitem_transform) {
return fmt::format(
"\n\t<Topology Dimensions=\"{dimensions}\" "
"Type=\"Mixed\">{dataitem}\n\t</Topology>",
......@@ -202,10 +195,10 @@ std::function<std::string(std::vector<double>)> write_xdmf(
// Defines content of <Grid> of a single time step, takes specific transform
// functions for all of its child elements
auto const grid_transform =
[](double const& time_value, auto const& geometry, auto const& topology,
auto const& constant_attributes, auto const& variable_attributes)
{
auto const grid_transform = [](double const& time_value,
auto const& geometry, auto const& topology,
auto const& constant_attributes,
auto const& variable_attributes) {
return fmt::format(
"\n<Grid Name=\"Grid\">\n\t<Time "
"Value=\"{time_value}\"/"
......@@ -229,13 +222,12 @@ std::function<std::string(std::vector<double>)> write_xdmf(
h5filename](auto const& component_transform,
int const time_step,
int const max_step,
time_attribute const variable)
{
time_attribute const variable) {
return [component_transform, time_dataitem_genfn, pointer_transfrom,
variable, time_step, max_step, h5filename](XdmfData const& attr)
{
variable, time_step, max_step,
h5filename](XdmfData const& attr) {
// new data arrived
bool changed =
bool const changed =
((time_step > 0 && (variable == time_attribute::variable)) ||
time_step == 0);
if (changed)
......@@ -246,8 +238,8 @@ std::function<std::string(std::vector<double>)> write_xdmf(
}
else
{
std::vector<unsigned int> d = {1, 1, 2, 1, attr.index};
return pointer_transfrom(d);
std::array<unsigned int, 5> position = {1, 1, 2, 1, attr.index};
return pointer_transfrom(position);
};
};
};
......@@ -261,30 +253,29 @@ std::function<std::string(std::vector<double>)> write_xdmf(
geometry_transform, topology_transform, attribute_transform](
double const time, int const step, int const max_step,
auto const& geometry, auto const& topology,
auto const& constant_attributes, auto const& variable_attributes)
{
auto const time_step_geometry_transform = time_step_fn(
geometry_transform, step, max_step, time_attribute::constant);
auto const time_step_topology_transform = time_step_fn(
topology_transform, step, max_step, time_attribute::constant);
auto const time_step_const_attribute_fn = time_step_fn(
attribute_transform, step, max_step, time_attribute::constant);
auto const time_step_variable_attribute_fn = time_step_fn(
attribute_transform, step, max_step, time_attribute::variable);
auto const& constant_attributes, auto const& variable_attributes) {
auto const time_step_geometry_transform = time_step_fn(
geometry_transform, step, max_step, time_attribute::constant);
auto const time_step_topology_transform = time_step_fn(
topology_transform, step, max_step, time_attribute::constant);
auto const time_step_const_attribute_fn = time_step_fn(
attribute_transform, step, max_step, time_attribute::constant);
auto const time_step_variable_attribute_fn = time_step_fn(
attribute_transform, step, max_step, time_attribute::variable);
// lift both functions from handling single attributes to work with
// collection of attributes
auto const variable_attributes_transform =
m_bind_fn(time_step_variable_attribute_fn, string_join_fn);
auto const constant_attributes_transform =
m_bind_fn(time_step_const_attribute_fn, string_join_fn);
// lift both functions from handling single attributes to work with
// collection of attributes
auto const variable_attributes_transform =
m_bind_fn(time_step_variable_attribute_fn, string_join_fn);
auto const constant_attributes_transform =
m_bind_fn(time_step_const_attribute_fn, string_join_fn);
return grid_transform(
time, time_step_geometry_transform(geometry),
time_step_topology_transform(topology),
constant_attributes_transform(constant_attributes),
variable_attributes_transform(variable_attributes));
};
return grid_transform(
time, time_step_geometry_transform(geometry),
time_step_topology_transform(topology),
constant_attributes_transform(constant_attributes),
variable_attributes_transform(variable_attributes));
};
// Function that combines all the data from spatial (geometry, topology,
// attribute) and temporal domain (time) with the time domain
......@@ -292,31 +283,29 @@ std::function<std::string(std::vector<double>)> write_xdmf(
auto const temporal_grid_collection_transform =
[time_grid_transform](
auto const& times, auto const& geometry, auto const& topology,
auto const& constant_attributes, auto const& variable_attributes)
{
// c++20 ranges zip missing
auto const max_step = times.size();
std::vector<std::string> grids;
grids.resize(max_step);
for (size_t time_step = 0; time_step < max_step; ++time_step)
{
grids.push_back(time_grid_transform(
times[time_step], time_step, max_step, geometry, topology,
constant_attributes, variable_attributes));
}
return fmt::format(
"\n<Grid CollectionType=\"Temporal\" GridType=\"Collection\" "
"Name=\"Collection\">{grids}\n</Grid>\n",
"grids"_a = fmt::join(grids, ""));
};
auto const& constant_attributes, auto const& variable_attributes) {
// c++20 ranges zip missing
auto const max_step = times.size();
std::vector<std::string> grids;
grids.reserve(max_step);
for (size_t time_step = 0; time_step < max_step; ++time_step)
{
grids.push_back(time_grid_transform(
times[time_step], time_step, max_step, geometry, topology,
constant_attributes, variable_attributes));
}
return fmt::format(
"\n<Grid CollectionType=\"Temporal\" GridType=\"Collection\" "
"Name=\"Collection\">{grids}\n</Grid>\n",
"grids"_a = fmt::join(grids, ""));
};
// Generator function that return a function that represents complete xdmf
// structure
auto const xdmf_writer_fn =
[temporal_grid_collection_transform](
auto const& ogs_version, auto const& geometry, auto const& topology,
auto const& constant_attributes, auto const& variable_attributes)
{
auto const xdmf_writer_fn = [temporal_grid_collection_transform](
auto const& ogs_version, auto geometry,
auto topology, auto constant_attributes,
auto variable_attributes) {
// This function will be the return of the surrounding named function
// capture by copy - it is the function that will survive this scope!!
// Use generalized lambda capture to move for optimization
......@@ -324,8 +313,7 @@ std::function<std::string(std::vector<double>)> write_xdmf(
geometry = std::move(geometry), topology = std::move(topology),
constant_attributes = std::move(constant_attributes),
variable_attributes = std::move(variable_attributes)](
std::vector<double> const& times)
{
std::vector<double> const& times) {
return fmt::format(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Xdmf "
"xmlns:xi=\"http://www.w3.org/2001/XInclude\" "
......
......@@ -50,7 +50,7 @@ int writeMeshToFile(const MeshLib::Mesh& mesh,
return 0;
}
ERR("writeMeshToFile(): Unknown file extension '{:s}'. Can not write file "
"'{'s}'.",
"'{:s}'.",
file_path.extension().string(), file_path.string());
return 0;
}
......
......@@ -179,7 +179,8 @@ struct Output::OutputFile
std::string const& prefix, std::string const& suffix,
std::string const& mesh_name, int const timestep, double const t,
int const iteration, int const data_mode_,
bool const compression_, std::set<std::string> const& outputnames)
bool const compression_,
std::set<std::string> const& outputnames)
: name(constructFilename(type, prefix, suffix, mesh_name, timestep, t,
iteration)),
path(BaseLib::joinPaths(directory, name)),
......@@ -322,7 +323,6 @@ void Output::doOutputAlways(Process const& process,
}
else if (_output_file_type == ProcessLib::OutputType::xdmf)
{
OutputFile const file(
_output_directory, _output_file_type, _output_file_prefix, "",
mesh.getName(), timestep, t, iteration, _output_file_data_mode,
......@@ -330,9 +330,6 @@ void Output::doOutputAlways(Process const& process,
_output_data_specification.output_variables);
outputMeshXdmf(file, mesh, timestep, t);
}
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment