https://mooseframework.inl.gov
SplitMeshAction.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "SplitMeshAction.h"
11 
12 #include "MooseApp.h"
13 #include "MooseUtils.h"
14 #include "MooseMesh.h"
15 
16 #include <filesystem>
17 
18 #include "libmesh/checkpoint_io.h"
19 
20 registerMooseAction("MooseApp", SplitMeshAction, "split_mesh");
21 
24 {
25  return Action::validParams();
26 }
27 
29 
30 void
32 {
33  auto mesh = _app.actionWarehouse().mesh();
34  const std::string split_file_arg = _app.parameters().isParamSetByUser("split_file")
35  ? _app.parameters().get<std::string>("split_file")
36  : "";
37 
38  if (mesh->getFileName() == "" && split_file_arg == "")
39  mooseError("Output mesh file name must be specified (with --split-file) when splitting "
40  "non-file-based meshes");
41 
42  auto splitstr = _app.parameters().get<std::string>("split_mesh");
43  std::vector<unsigned int> splits;
44  bool success = MooseUtils::tokenizeAndConvert(splitstr, splits, ", ");
45  if (!success)
46  mooseError("invalid argument for --split-mesh: '", splitstr, "'");
47 
48  // Decide whether to create ASCII or binary splits based on the split_file_arg. We use the
49  // following rules to decide:
50  // 1.) No file extension -> ASCII + gzip
51  // 2.) .cpr file extension -> binary
52  // 3.) .cpa.gz file extension -> ASCII + gzip
53  // 4.) Any other file extension -> mooseError
54 
55  // Get the file extension without the dot.
56  std::string split_file_arg_ext = MooseUtils::getExtension(split_file_arg);
57 
58  bool checkpoint_binary_flag = false;
59 
60  if (split_file_arg_ext != "")
61  {
62  if (split_file_arg_ext == "cpr")
63  checkpoint_binary_flag = true;
64  else if (split_file_arg_ext == "cpa.gz")
65  checkpoint_binary_flag = false;
66  else
67  mooseError("The argument to --split-file, ",
68  split_file_arg,
69  ", must not end in a file extension other than .cpr or .cpa.gz");
70  }
71 
72  // To name the split files, we start with the given mesh filename
73  // (if set) or the argument to --split-file, strip any existing
74  // extension, and then append either .cpr or .cpa.gz depending on the
75  // checkpoint_binary_flag.
76  auto fname = mesh->getFileName();
77  if (fname == "")
78  fname = split_file_arg;
79  fname = MooseUtils::stripExtension(fname) + (checkpoint_binary_flag ? ".cpr" : ".cpa.gz");
80 
81  for (std::size_t i = 0; i < splits.size(); i++)
82  {
83  processor_id_type n = splits[i];
84  Moose::out << "Splitting " << n << " ways..." << std::endl;
85 
86  auto cp = libMesh::split_mesh(*mesh, n);
87  Moose::out << " - writing " << cp->current_processor_ids().size() << " files per process..."
88  << std::endl;
89  cp->binary() = checkpoint_binary_flag;
90 
91  // different splits will be written into subfolders with n being the folder name
92  cp->write(fname);
93  }
94 
95  // Write mesh metadata
96  if (processor_id() == 0)
97  {
98  const auto filenames = _app.writeRestartableMetaData(MooseApp::MESH_META_DATA, fname);
99  Moose::out << "Mesh meta data written into "
100  << std::filesystem::absolute(filenames[0].parent_path()) << "." << std::endl;
101  }
102 }
SplitMeshAction(const InputParameters &params)
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
bool tokenizeAndConvert(const std::string &str, std::vector< T > &tokenized_vector, const std::string &delimiter=" \\\)
tokenizeAndConvert splits a string using delimiter and then converts to type T.
static const RestartableDataMapName MESH_META_DATA
Definition: MooseApp.h:123
MeshBase & mesh
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::vector< std::filesystem::path > writeRestartableMetaData(const RestartableDataMapName &name, const std::filesystem::path &folder_base)
Writes the restartable meta data for name with a folder base of folder_base.
Definition: MooseApp.C:2557
const InputParameters & parameters()
Get the parameters of the object.
Definition: MooseApp.h:161
Base class for actions.
Definition: Action.h:33
std::string getExtension(const std::string &filename, const bool rfind=false)
Gets the extension of the passed file name.
Definition: MooseUtils.C:407
std::shared_ptr< MooseMesh > & mesh()
uint8_t processor_id_type
static InputParameters validParams()
std::string stripExtension(const std::string &s, const bool rfind=false)
Removes any file extension from the given string s (i.e.
Definition: MooseUtils.C:423
static InputParameters validParams()
Definition: Action.C:24
registerMooseAction("MooseApp", SplitMeshAction, "split_mesh")
ActionWarehouse & actionWarehouse()
Return a writable reference to the ActionWarehouse associated with this app.
Definition: MooseApp.h:237
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:84
bool isParamSetByUser(const std::string &name) const
Method returns true if the parameter was set by the user.
std::unique_ptr< CheckpointIO > split_mesh(MeshBase &mesh, processor_id_type nsplits)
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
processor_id_type processor_id() const
virtual void act() override
Method to add objects to the simulation or perform other setup tasks.