4#include <tclap/CmdLine.h>
7#include <boost/property_tree/ptree.hpp>
8#include <boost/property_tree/xml_parser.hpp>
13#include <range/v3/algorithm/copy.hpp>
14#include <range/v3/algorithm/transform.hpp>
15#include <range/v3/numeric/accumulate.hpp>
16#include <range/v3/range/conversion.hpp>
17#include <range/v3/view/filter.hpp>
19#include <unordered_set>
51 std::vector<std::unique_ptr<MeshLib::Mesh>>
const& partitioned_meshes,
54 std::vector<MeshEntityMapInfo>
const& merged_node_map,
55 std::vector<MeshEntityMapInfo>
const& merged_element_map)
74 if (pv_name ==
"OGS_VERSION" || pv_name ==
"IntegrationPointMetaData")
77 merged_mesh, pv_name, item_type, pv_num_components);
83 std::vector<MeshLib::PropertyVector<T>*> partition_property_vectors;
84 partition_property_vectors.reserve(partitioned_meshes.size());
85 for (
auto const& mesh : partitioned_meshes)
87 partition_property_vectors.emplace_back(
88 mesh->getProperties().getPropertyVector<T>(pv_name, item_type,
92 auto createNewCellOrNodePropertyVector =
93 [&](std::vector<MeshEntityMapInfo>
const& mesh_entity_map)
96 merged_mesh, pv_name, item_type, pv_num_components);
97 std::size_t counter = 0;
98 for (
auto const& entity_info : mesh_entity_map)
100 auto const& partition_pv =
101 partition_property_vectors[entity_info.partition_id];
102 for (
int i_com = 0; i_com < pv_num_components; i_com++)
104 (*new_pv)[counter * pv_num_components + i_com] =
105 (*partition_pv)[entity_info.original_id *
115 createNewCellOrNodePropertyVector(merged_node_map);
121 createNewCellOrNodePropertyVector(merged_element_map);
127 std::vector<std::vector<std::size_t>> partition_element_offsets;
128 partition_element_offsets.reserve(partitioned_meshes.size());
129 for (
auto const& mesh : partitioned_meshes)
131 partition_element_offsets.emplace_back(
133 mesh->getElements(), *pv, properties));
137 merged_mesh, pv_name, item_type, pv_num_components);
140 auto const ip_meta_data =
144 auto number_of_integration_points = [&](
auto const* element)
147 ip_meta_data, *element);
150 std::size_t
const counter = ranges::accumulate(
152 ranges::views::transform(number_of_integration_points),
154 new_pv->resize(counter * pv_num_components);
156 auto const global_ip_offsets =
160 std::size_t element_counter = 0;
161 for (
auto const& element_info : merged_element_map)
164 *(partition_property_vectors[element_info.partition_id]);
166 auto const& offsets =
167 partition_element_offsets[element_info.partition_id];
169 int const begin_pos = offsets[element_info.original_id];
170 int const end_pos = offsets[element_info.original_id + 1];
172 std::copy(partition_pv.
begin() + begin_pos,
173 partition_pv.
begin() + end_pos,
174 new_pv->begin() + global_ip_offsets[element_counter]);
185 std::ifstream ins(pvtu_file_name);
189 OGS_FATAL(
"Could not open pvtu file {:s}.", pvtu_file_name);
192 using boost::property_tree::ptree;
196 auto root = pt.get_child(
"VTKFile");
198 std::vector<std::string> vtu_file_names;
202 for (ptree::value_type
const& v : root.get_child(
"PUnstructuredGrid"))
204 if (v.first ==
"Piece")
209 std::filesystem::path(v.second.get(
"<xmlattr>.Source",
""))
215 if (vtu_file_names.empty())
217 OGS_FATAL(
"PVTU file {:s} does not contain any vtu piece",
221 return vtu_file_names;
225std::tuple<std::vector<MeshLib::Node*>, std::vector<std::size_t>>
228 std::vector<std::size_t> number_of_nodes_per_partition;
229 ranges::transform(meshes, std::back_inserter(number_of_nodes_per_partition),
231 {
return mesh->getNumberOfNodes(); });
232 std::vector<std::size_t>
const offsets =
235 std::vector<MeshLib::Node*> all_nodes;
236 all_nodes.reserve(offsets.back());
237 for (
auto const& mesh : meshes)
239 ranges::copy(mesh->getNodes(), std::back_inserter(all_nodes));
241 return {all_nodes, offsets};
244std::tuple<std::vector<MeshLib::Element*>, std::vector<MeshEntityMapInfo>>
247 std::vector<MeshLib::Element*> regular_elements;
249 std::size_t partition_counter = 0;
250 std::vector<MeshEntityMapInfo> merged_element_map;
251 for (
auto const& mesh : meshes)
255 auto const*
const ghost_id_vector =
258 assert(ghost_id_vector);
260 auto const& mesh_elements = mesh->getElements();
262 auto const last_element_id_of_previous_partition =
263 regular_elements.size();
266 {
return (*ghost_id_vector)[element->getID()] == 0; };
268 auto regular_mesh_elements =
269 mesh_elements | ranges::views::filter(is_regular_element);
270 regular_elements.insert(regular_elements.end(),
271 regular_mesh_elements.begin(),
272 regular_mesh_elements.end());
274 for (
auto element_id = last_element_id_of_previous_partition;
275 element_id < regular_elements.size();
278 merged_element_map.push_back(
279 {partition_counter, regular_elements[element_id]->getID()});
285 return {regular_elements, merged_element_map};
292 std::vector<MeshLib::Node*> query_nodes;
294 std::numeric_limits<double>::epsilon() * extent.squaredNorm();
296 auto constexpr dir = std::numeric_limits<double>::max();
297 Eigen::Vector3d
const max = {std::nextafter(node[0] + eps, dir),
298 std::nextafter(node[1] + eps, dir),
299 std::nextafter(node[2] + eps, dir)};
302 if (query_nodes.empty())
305 "query_nodes for node [{}], ({}, {}, {}) of element "
306 "[{}] are empty, eps is {}",
307 node.
getID(), node[0], node[1], node[2], element_id, eps);
310 std::find_if(query_nodes.begin(), query_nodes.end(),
311 [&node, eps](
auto const* p)
313 return (p->asEigenVector3d() - node.asEigenVector3d())
314 .squaredNorm() < eps;
316 if (it == query_nodes.end())
319 "did not find node: [{}] ({}, {}, {}) of element [{}] in "
321 node.
getID(), node[0], node[1], node[2], element_id);
327 std::vector<MeshLib::Element*>
const& regular_elements,
329 Eigen::Vector3d
const& extent)
331 for (
auto& e : regular_elements)
333 for (
unsigned i = 0; i < e->getNumberOfNodes(); i++)
335 auto*
const node = e->getNode(i);
337 if (!oct_tree.
addPoint(node, node_ptr))
340 oct_tree, extent, *node, e->getID());
341 e->setNode(i, node_ptr);
347std::pair<std::vector<MeshLib::Node*>, std::vector<MeshEntityMapInfo>>
349 std::vector<std::size_t>
const& partition_offsets,
352 std::vector<MeshLib::Node*> unique_merged_nodes;
353 unique_merged_nodes.reserve(all_merged_nodes_tmp.size());
355 std::vector<MeshEntityMapInfo> merged_node_map;
356 merged_node_map.reserve(all_merged_nodes_tmp.size());
358 for (std::size_t i = 0; i < partition_offsets.size() - 1; ++i)
360 for (std::size_t pos = partition_offsets[i];
361 pos < partition_offsets[i + 1];
364 auto* node = all_merged_nodes_tmp[pos];
366 if (oct_tree.
addPoint(node, node_ptr))
368 unique_merged_nodes.push_back(node);
369 merged_node_map.push_back({i, pos - partition_offsets[i]});
373 return {unique_merged_nodes, merged_node_map};
377 std::vector<std::unique_ptr<MeshLib::Mesh>>
const& meshes)
380 merged_element_timer.
start();
385 "Collection of {} regular elements and computing element map took {} s",
386 regular_elements.size(), merged_element_timer.
elapsed());
390 collect_nodes_timer.
start();
391 auto [all_merged_nodes_tmp, partition_offsets] =
393 INFO(
"Collection of {} nodes and computing offsets took {} s",
394 all_merged_nodes_tmp.size(), collect_nodes_timer.
elapsed());
397 merged_nodes_timer.
start();
398 GeoLib::AABB aabb(all_merged_nodes_tmp.begin(), all_merged_nodes_tmp.end());
399 auto oct_tree = std::unique_ptr<GeoLib::OctTree<MeshLib::Node, 16>>(
403 auto [unique_merged_nodes, merged_node_map] =
405 INFO(
"Make nodes unique ({} unique nodes) / computing map took {} s",
406 unique_merged_nodes.size(), merged_nodes_timer.
elapsed());
409 reset_nodes_in_elements_timer.
start();
412 INFO(
"Reset nodes in regular elements took {} s",
413 reset_nodes_in_elements_timer.
elapsed());
416 mesh_creation_timer.
start();
419 auto merged_mesh = std::make_unique<MeshLib::Mesh>(
420 "pvtu_merged_mesh", unique_merged_nodes, regular_elements,
422 INFO(
"creation of merged mesh took {} s", mesh_creation_timer.
elapsed());
424 auto const& properties = meshes[0]->getProperties();
427 property_timer.
start();
428 applyToPropertyVectors(
430 [&, &merged_node_map = merged_node_map,
431 &merged_element_map = merged_element_map](
auto type,
432 auto const& property)
435 *merged_mesh, meshes,
438 properties, merged_node_map, merged_element_map);
440 INFO(
"merge properties into merged mesh took {} s",
449 std::string_view
const property_name,
454 INFO(
"Skipping property '{}' since it is not from type '{}'.",
458 auto const* subdomain_pv =
460 auto const number_of_components =
464 original_mesh, std::string(property_name), mesh_item_type,
465 number_of_components);
467 for (std::size_t i = 0; i < global_ids.
size(); ++i)
469 auto const& global_id = global_ids[i];
470 for (std::remove_cvref_t<
decltype(number_of_components)>
471 component_number = 0;
472 component_number < number_of_components;
475 original_pv->getComponent(global_id, component_number) =
476 subdomain_pv->getComponent(i, component_number);
479 INFO(
"Data array {} from data type '{}' transferred.", property_name,
488 auto const& subdomain_properties = subdomain_mesh.
getProperties();
489 if (!subdomain_properties.existsPropertyVector<std::size_t>(
493 "The data array '{}' is required for the transfer of data from "
494 "subdomain mesh to the global mesh. But it doesn't exist in "
495 "subdomain mesh '{}'.",
498 auto const* global_ids =
501 if (global_ids ==
nullptr)
503 OGS_FATAL(
"The data array '{}' is not available but required.",
507 auto const& property_names =
508 subdomain_properties.getPropertyVectorNames(mesh_item_type);
510 for (
auto const& property_name : property_names)
519 "Skipping property '{}' since it is adjusted locally in "
520 "NodeWiseMeshPartitioner::renumberBulkNodeIdsProperty().",
527 "Skipping property '{}' since it is only required for parallel "
533 property_name, mesh_item_type) ||
535 property_name, mesh_item_type) ||
537 property_name, mesh_item_type) ||
539 *global_ids, property_name,
542 *global_ids, property_name, mesh_item_type) ||
543 transfer<int>(subdomain_properties, original_mesh, *global_ids,
544 property_name, mesh_item_type) ||
546 property_name, mesh_item_type))
548 INFO(
"Data array {} transferred from '{}' to'{}'", property_name,
564int main(
int argc,
char* argv[])
567 "This tool merges VTU files of PVTU into one single VTU file. Apart "
568 "from the mesh data, all property data are merged as well"
569 "\n\nOpenGeoSys-6 software, version " +
572 "Copyright (c) 2012-2026, OpenGeoSys Community "
573 "(http://www.opengeosys.org)",
576 TCLAP::ValueArg<std::string> output_arg(
577 "o",
"output",
"Output (.vtu). The output mesh file (format = *.vtu)",
578 true,
"",
"OUTPUT_FILE");
581 TCLAP::ValueArg<std::string> input_arg(
582 "i",
"input",
"Input (.pvtu). The partitioned input mesh file",
true,
586 TCLAP::ValueArg<std::string> original_mesh_input_arg(
587 "m",
"original_mesh",
588 "optional, the original unpartitioned input mesh (*.vtu)",
false,
"",
590 cmd.add(original_mesh_input_arg);
594 cmd.add(log_level_arg);
595 cmd.parse(argc, argv);
602 OGS_FATAL(
"The extension of input file name {:s} is not \"pvtu\"",
603 input_arg.getValue());
607 OGS_FATAL(
"The extension of output file name {:s} is not \"vtu\"",
608 output_arg.getValue());
613 std::vector<std::unique_ptr<MeshLib::Mesh>> meshes;
614 meshes.reserve(vtu_file_names.size());
618 for (
auto const& file_name : vtu_file_names)
620 auto mesh = std::unique_ptr<MeshLib::Mesh>(
628 OGS_FATAL(
"Property vector '{}' does not exist in mesh {:s}.",
632 meshes.emplace_back(std::move(mesh));
634 INFO(
"Reading meshes took {} s", io_timer.
elapsed());
636 std::unique_ptr<MeshLib::Mesh> merged_mesh;
637 if (original_mesh_input_arg.getValue().empty())
647 merged_mesh = std::unique_ptr<MeshLib::Mesh>(
649 original_mesh_input_arg.getValue()));
650 INFO(
"Reading original unpartitioned mesh took {} s",
652 for (
auto const& subdomain_mesh : meshes)
655 *(subdomain_mesh.get()), *merged_mesh);
660 writing_timer.
start();
662 output_arg.getValue()) != 0)
664 ERR(
"Could not write mesh to '{:s}'.", output_arg.getValue());
667 INFO(
"writing mesh took {} s", writing_timer.
elapsed());
674 merged_mesh->shallowClean();
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
bool transfer(MeshLib::Properties const &subdomain_properties, MeshLib::Mesh &original_mesh, MeshLib::PropertyVector< std::size_t > const &global_ids, std::string_view const property_name, MeshLib::MeshItemType const mesh_item_type)
int main(int argc, char *argv[])
std::unique_ptr< MeshLib::Mesh > mergeSubdomainMeshes(std::vector< std::unique_ptr< MeshLib::Mesh > > const &meshes)
std::pair< std::vector< MeshLib::Node * >, std::vector< MeshEntityMapInfo > > makeNodesUnique(std::vector< MeshLib::Node * > const &all_merged_nodes_tmp, std::vector< std::size_t > const &partition_offsets, GeoLib::OctTree< MeshLib::Node, 16 > &oct_tree)
MeshLib::Node * getExistingNodeFromOctTree(GeoLib::OctTree< MeshLib::Node, 16 > &oct_tree, Eigen::Vector3d const &extent, MeshLib::Node const &node, std::size_t const element_id)
void transferPropertiesFromPartitionedMeshToUnpartitionedMesh(MeshLib::Mesh const &subdomain_mesh, MeshLib::Mesh &original_mesh, MeshLib::MeshItemType const mesh_item_type)
void resetNodesInRegularElements(std::vector< MeshLib::Element * > const ®ular_elements, GeoLib::OctTree< MeshLib::Node, 16 > &oct_tree, Eigen::Vector3d const &extent)
std::tuple< std::vector< MeshLib::Element * >, std::vector< MeshEntityMapInfo > > getRegularElements(std::vector< std::unique_ptr< MeshLib::Mesh > > const &meshes)
std::vector< std::string > readVtuFileNames(std::string const &pvtu_file_name)
bool createPropertyVector(MeshLib::Mesh &merged_mesh, std::vector< std::unique_ptr< MeshLib::Mesh > > const &partitioned_meshes, MeshLib::PropertyVector< T > const *const pv, MeshLib::Properties const &properties, std::vector< MeshEntityMapInfo > const &merged_node_map, std::vector< MeshEntityMapInfo > const &merged_element_map)
std::tuple< std::vector< MeshLib::Node * >, std::vector< std::size_t > > getMergedNodesVector(std::vector< std::unique_ptr< MeshLib::Mesh > > const &meshes)
double elapsed() const
Get the elapsed time in seconds.
void start()
Start the timer.
Class AABB is an axis aligned bounding box around a given set of geometric points of (template) type ...
Eigen::Vector3d const & getMaxPoint() const
Eigen::Vector3d const & getMinPoint() const
bool addPoint(POINT *pnt, POINT *&ret_pnt)
void getPointsInRange(T const &min, T const &max, std::vector< POINT * > &pnts) const
static OctTree< POINT, MAX_POINTS > * createOctTree(Eigen::Vector3d ll, Eigen::Vector3d ur, double eps=std::numeric_limits< double >::epsilon())
std::size_t getID() const
Eigen::Vector3d const & asEigenVector3d() const
static MeshLib::Mesh * readVTUFile(std::string const &file_name, bool const compute_element_neighbors=false)
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Properties & getProperties()
const std::string getName() const
Get name of the mesh.
Property manager on mesh items. Class Properties manages scalar, vector or matrix properties....
bool existsPropertyVector(std::string_view name) const
PropertyVector< T > const * getPropertyVector(std::string_view name) const
MeshItemType getMeshItemType() const
int getNumberOfGlobalComponents() const
std::string const & getPropertyName() const
constexpr PROP_VAL_TYPE * begin()
constexpr std::size_t size() const
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
std::string getFileExtension(const std::string &path)
std::string typeToString()
std::string extractPath(std::string const &pathname)
std::string joinPaths(std::string const &pathA, std::string const &pathB)
std::vector< ranges::range_value_t< R > > sizesToOffsets(R const &sizes)
GITINFOLIB_EXPORT const std::string ogs_version
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, std::set< std::string > output_variable_names, bool const use_compression, int const data_mode)
PropertyVector< T > * getOrCreateMeshProperty(Mesh &mesh, std::string const &property_name, MeshItemType const item_type, int const number_of_components)
std::optional< IntegrationPointMetaData > getIntegrationPointMetaData(MeshLib::Properties const &properties)
constexpr std::string_view getBulkIDString(MeshItemType mesh_item_type)
constexpr std::string vtkGhostTypeString
constexpr std::string_view globalIDString(MeshLib::MeshItemType const mesh_item_type)
IntegrationPointMetaDataSingleField getIntegrationPointMetaDataSingleField(std::optional< IntegrationPointMetaData > const &ip_meta_data, std::string const &field_name)