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