OGS
Metis.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include <fstream>
5
6#include "BaseLib/Error.h"
8
9namespace ApplicationUtils
10{
11void writeMETIS(std::vector<MeshLib::Element*> const& elements,
12 const std::string& file_name)
13{
14 std::ofstream os(file_name, std::ios::trunc);
15 if (!os.is_open())
16 {
17 OGS_FATAL("Error: cannot open file {:s}.", file_name.data());
18 }
19
20 if (!os.good())
21 {
22 OGS_FATAL("Error: Cannot write in file {:s}.", file_name.data());
23 }
24
25 os << elements.size() << " \n";
26 for (const auto* elem : elements)
27 {
28 os << getNodeIndex(*elem, 0) + 1;
29 for (unsigned j = 1; j < elem->getNumberOfNodes(); j++)
30 {
31 os << " " << getNodeIndex(*elem, j) + 1;
32 }
33 os << "\n";
34 }
35}
36
37std::vector<std::size_t> readMetisData(const std::string& file_name_base,
38 long const number_of_partitions,
39 std::size_t const number_of_nodes)
40{
41 const std::string npartitions_str = std::to_string(number_of_partitions);
42
43 // Read partitioned mesh data from METIS
44 const std::string fname_parts =
45 file_name_base + ".mesh.npart." + npartitions_str;
46
47 std::ifstream npart_in(fname_parts);
48 if (!npart_in.is_open())
49 {
51 "Error: cannot open file {:s}. It may not exist!\n"
52 "Run mpmetis beforehand or use option -m",
53 fname_parts.data());
54 }
55
56 std::vector<std::size_t> partition_ids(number_of_nodes);
57
58 std::size_t counter = 0;
59 while (!npart_in.eof())
60 {
61 npart_in >> partition_ids[counter++] >> std::ws;
62 if (counter == number_of_nodes)
63 {
64 break;
65 }
66 }
67
68 if (npart_in.bad())
69 {
70 OGS_FATAL("Error while reading file {:s}.", fname_parts.data());
71 }
72
73 if (counter != number_of_nodes)
74 {
75 OGS_FATAL("Error: data in {:s} are less than expected.",
76 fname_parts.data());
77 }
78
79 return partition_ids;
80}
81
82void removeMetisPartitioningFiles(std::string const& file_name_base,
83 long const number_of_partitions)
84{
85 const std::string npartitions_str = std::to_string(number_of_partitions);
86
87 std::remove((file_name_base + ".mesh.npart." + npartitions_str).c_str());
88 std::remove((file_name_base + ".mesh.epart." + npartitions_str).c_str());
89}
90} // namespace ApplicationUtils
#define OGS_FATAL(...)
Definition Error.h:19
void writeMETIS(std::vector< MeshLib::Element * > const &elements, const std::string &file_name)
Definition Metis.cpp:11
void removeMetisPartitioningFiles(std::string const &file_name_base, long const number_of_partitions)
Definition Metis.cpp:82
std::vector< std::size_t > readMetisData(const std::string &file_name_base, long const number_of_partitions, std::size_t const number_of_nodes)
Definition Metis.cpp:37