diff --git a/Applications/Utils/ModelPreparation/PartitionMesh/NodeWiseMeshPartitioner.cpp b/Applications/Utils/ModelPreparation/PartitionMesh/NodeWiseMeshPartitioner.cpp index 94df9d164263254fc4879b6cc84d8a5bdd39710b..f5172a4cd8253a3e978ac03ee6a4c816bfb49dd4 100644 --- a/Applications/Utils/ModelPreparation/PartitionMesh/NodeWiseMeshPartitioner.cpp +++ b/Applications/Utils/ModelPreparation/PartitionMesh/NodeWiseMeshPartitioner.cpp @@ -16,6 +16,7 @@ #include <cstdio> // for binary output #include <iomanip> +#include <iostream> #include <limits> #include <numeric> @@ -29,12 +30,36 @@ namespace ApplicationUtils { struct NodeStruct { + NodeStruct(NodeWiseMeshPartitioner::IntegerType const id_, + double const x_, + double const y_, + double const z_) + : id(id_), x(x_), y(y_), z(z_) + { + } + NodeWiseMeshPartitioner::IntegerType id; double x; double y; double z; }; +std::ostream& Partition::writeNodesBinary( + std::ostream& os, std::vector<std::size_t> const& global_node_ids) const +{ + std::vector<NodeStruct> nodes_buffer; + nodes_buffer.reserve(nodes.size()); + + for (const auto* node : nodes) + { + double const* coords = node->getCoords(); + nodes_buffer.emplace_back(global_node_ids[node->getID()], coords[0], + coords[1], coords[2]); + } + return os.write(reinterpret_cast<const char*>(nodes_buffer.data()), + sizeof(NodeStruct) * nodes_buffer.size()); +} + void NodeWiseMeshPartitioner::findNonGhostNodesInPartition( std::size_t const part_id, const bool is_mixed_high_order_linear_elems, @@ -549,32 +574,27 @@ void NodeWiseMeshPartitioner::writeElementsBinary( fclose(of_bin_ele_g); } -void NodeWiseMeshPartitioner::writeNodesBinary( - const std::string& file_name_base) +/// Write the nodes of all partitions into a binary file. +/// \param file_name_base The prefix of the file name. +/// \param partitions the list of partitions +/// \param global_node_ids global numbering of nodes +void writeNodesBinary(const std::string& file_name_base, + std::vector<Partition> const& partitions, + std::vector<std::size_t> const& global_node_ids) { const std::string fname = file_name_base + "_partitioned_msh_nod" + - std::to_string(_npartitions) + ".bin"; - FILE* of_bin_nod = fopen(fname.c_str(), "wb"); + std::to_string(partitions.size()) + ".bin"; - for (const auto& partition : _partitions) + std::ofstream os(fname, std::ios::binary | std::ios::out); + if (!os) { - std::vector<NodeStruct> nodes_buffer; - nodes_buffer.reserve(partition.nodes.size()); + OGS_FATAL("Could not open file '%s' for output.", fname.c_str()); + } - for (const auto* node : partition.nodes) - { - double const* coords = node->getCoords(); - NodeStruct node_struct{}; - node_struct.id = _nodes_global_ids[node->getID()]; - node_struct.x = coords[0]; - node_struct.y = coords[1]; - node_struct.z = coords[2]; - nodes_buffer.emplace_back(node_struct); - } - fwrite(nodes_buffer.data(), sizeof(NodeStruct), partition.nodes.size(), - of_bin_nod); + for (const auto& partition : partitions) + { + partition.writeNodesBinary(os, global_node_ids); } - fclose(of_bin_nod); } void NodeWiseMeshPartitioner::writeBinary(const std::string& file_name_base) @@ -589,7 +609,7 @@ void NodeWiseMeshPartitioner::writeBinary(const std::string& file_name_base) std::get<1>(elem_integers); writeElementsBinary(file_name_base, num_elem_integers, num_g_elem_integers); - writeNodesBinary(file_name_base); + writeNodesBinary(file_name_base, _partitions, _nodes_global_ids); } void NodeWiseMeshPartitioner::writeConfigDataASCII( diff --git a/Applications/Utils/ModelPreparation/PartitionMesh/NodeWiseMeshPartitioner.h b/Applications/Utils/ModelPreparation/PartitionMesh/NodeWiseMeshPartitioner.h index 9bac0022eb9c75ecb86e542499a14355311aa7f3..36fc2713d95f32bb816f5fa11ee9c5b3e40ab55b 100644 --- a/Applications/Utils/ModelPreparation/PartitionMesh/NodeWiseMeshPartitioner.h +++ b/Applications/Utils/ModelPreparation/PartitionMesh/NodeWiseMeshPartitioner.h @@ -37,6 +37,10 @@ struct Partition /// Non ghost elements std::vector<const MeshLib::Element*> regular_elements; std::vector<const MeshLib::Element*> ghost_elements; + + std::ostream& writeNodesBinary( + std::ostream& os, + std::vector<std::size_t> const& global_node_ids) const; }; /// Mesh partitioner. @@ -294,10 +298,6 @@ private: const std::vector<IntegerType>& num_elem_integers, const std::vector<IntegerType>& num_g_elem_integers); - /// Write the nodes of all partitions into a binary file. - /// \param file_name_base The prefix of the file name. - void writeNodesBinary(const std::string& file_name_base); - /// Write the configuration data of the partition data in ASCII files. /// \param file_name_base The prefix of the file name. void writeConfigDataASCII(const std::string& file_name_base);