libMesh
splitter.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 // Read in a Mesh file and write out partitionings of it that are suitable
20 // for reading into a DistributedMesh
21 #include "libmesh/libmesh.h"
22 #include "libmesh/replicated_mesh.h"
23 #include "libmesh/default_coupling.h"
24 #include "libmesh/checkpoint_io.h"
25 #include "libmesh/metis_partitioner.h"
26 #include "libmesh/getpot.h"
27 
28 using namespace libMesh;
29 
30 // From: http://stackoverflow.com/a/6417908/2042320
31 std::string remove_extension (const std::string & filename)
32 {
33  size_t lastdot = filename.find_last_of(".");
34 
35  if (lastdot == std::string::npos)
36  return filename;
37 
38  return filename.substr(0, lastdot);
39 }
40 
41 int main (int argc, char ** argv)
42 {
43  LibMeshInit init (argc, argv);
44 
45  if (libMesh::on_command_line("--help") || argc < 3)
46  {
47  libMesh::out << "Example: " << argv[0] << " --mesh=filename.e --n-procs='4 8 16' "
48  "[--num-ghost-layers <n>] [--dry-run] [--ascii]\n\n"
49  << "--mesh Full name of the mesh file to read in. \n"
50  << "--n-procs Vector of number of processors.\n"
51  << "--num-ghost-layers Number of layers to ghost when partitioning (Default: 1).\n"
52  << "--dry-run Only test the partitioning, don't write any files.\n"
53  << "--ascii Write ASCII cpa files rather than binary cpr files.\n"
54  << std::endl;
55 
56  return 0;
57  }
58 
59  std::string filename = libMesh::command_line_value("--mesh", std::string());
60 
61  std::vector<processor_id_type> all_n_procs;
62  libMesh::command_line_vector("--n-procs", all_n_procs);
63 
64  unsigned int num_ghost_layers = libMesh::command_line_value("--num-ghost-layers", 1);
65 
66  ReplicatedMesh mesh(init.comm());
67 
68  // If the user has requested additional ghosted layers, we need to add a ghosting functor.
69  DefaultCoupling default_coupling;
70  if (num_ghost_layers > 1)
71  {
72  default_coupling.set_n_levels(num_ghost_layers);
73  mesh.add_ghosting_functor(default_coupling);
74  }
75 
76  libMesh::out << "Reading " << filename << std::endl;
77 
78  mesh.read(filename);
79 
80  for (const auto & n_procs : all_n_procs)
81  {
82  libMesh::out << "splitting " << n_procs << " ways..." << std::endl;
83 
84  auto cpr = split_mesh(mesh, n_procs);
85 
86  if (!libMesh::on_command_line("--dry-run"))
87  {
88  libMesh::out << " * writing " << cpr->current_processor_ids().size() << " files per process..." << std::endl;
89 
90  const bool binary = !libMesh::on_command_line("--ascii");
91 
92  cpr->binary() = binary;
93  std::ostringstream outputname;
94  outputname << remove_extension(filename) << (binary ? ".cpr" : ".cpa");
95  cpr->write(outputname.str());
96  }
97  }
98 
99  return 0;
100 }
libMesh::MeshBase::read
virtual void read(const std::string &name, void *mesh_data=nullptr, bool skip_renumber_nodes_and_elements=false, bool skip_find_neighbors=false)=0
Interfaces for reading/writing a mesh to/from a file.
main
int main(int argc, char **argv)
Definition: splitter.C:41
remove_extension
std::string remove_extension(const std::string &filename)
Definition: splitter.C:31
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
mesh
MeshBase & mesh
Definition: mesh_communication.C:1257
libMesh::ReplicatedMesh
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
Definition: replicated_mesh.h:47
libMesh::TriangleWrapper::init
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
libMesh::command_line_value
T command_line_value(const std::string &, T)
Definition: libmesh.C:931
libMesh::split_mesh
std::unique_ptr< CheckpointIO > split_mesh(MeshBase &mesh, processor_id_type nsplits)
split_mesh takes the given initialized/opened mesh and partitions it into nsplits pieces or chunks.
Definition: checkpoint_io.C:134
libMesh::LibMeshInit
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:83
libMesh::DefaultCoupling
This class implements the default algebraic coupling in libMesh: elements couple to themselves,...
Definition: default_coupling.h:41
libMesh::MeshBase::add_ghosting_functor
void add_ghosting_functor(GhostingFunctor &ghosting_functor)
Adds a functor which can specify ghosting requirements for use on distributed meshes.
Definition: mesh_base.h:1089
libMesh::command_line_vector
void command_line_vector(const std::string &, std::vector< T > &)
Definition: libmesh.C:976
libMesh::on_command_line
bool on_command_line(std::string arg)
Definition: libmesh.C:898
libMesh::out
OStreamProxy out
libMesh::DefaultCoupling::set_n_levels
void set_n_levels(unsigned int n_levels)
Definition: default_coupling.h:65