www.mooseframework.org
SplitMeshAction.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 #include "libmesh/checkpoint_io.h"
16 
17 registerMooseAction("MooseApp", SplitMeshAction, "split_mesh");
18 
19 template <>
22 {
23  return validParams<Action>();
24 }
25 
27 
28 void
30 {
31  auto mesh = _app.actionWarehouse().mesh();
32  auto split_file_arg = _app.parameters().get<std::string>("split_file");
33 
34  if (mesh->getFileName() == "" && split_file_arg == "")
35  mooseError("Output mesh file name must be specified (with --split-file) when splitting "
36  "non-file-based meshes");
37 
38  auto splitstr = _app.parameters().get<std::string>("split_mesh");
39  std::vector<unsigned int> splits;
40  bool success = MooseUtils::tokenizeAndConvert(splitstr, splits, ", ");
41  if (!success)
42  mooseError("invalid argument for --split-mesh: '", splitstr, "'");
43 
44  // Decide whether to create ASCII or binary splits based on the split_file_arg. We use the
45  // following rules to decide:
46  // 1.) No file extension -> binary
47  // 2.) .cpr file extension -> binary
48  // 3.) .cpa file extension -> ASCII
49  // 4.) Any other file extension -> mooseError
50 
51  // Get the file extension without the dot.
52  // TODO: Maybe this should be in MooseUtils?
53  std::string split_file_arg_ext;
54  auto pos = split_file_arg.rfind(".");
55  if (pos != std::string::npos)
56  split_file_arg_ext = split_file_arg.substr(pos + 1, std::string::npos);
57 
58  // If stripExtension() returns the original string, then there is no
59  // file extension or the original string was empty.
60  bool checkpoint_binary_flag = true;
61 
62  if (split_file_arg_ext != "")
63  {
64  if (split_file_arg_ext == "cpr")
65  checkpoint_binary_flag = true;
66  else if (split_file_arg_ext == "cpa")
67  checkpoint_binary_flag = false;
68  else
69  mooseError("The argument to --split-file, ",
70  split_file_arg,
71  ", must not end in a file extension other than .cpr or .cpa");
72  }
73 
74  for (std::size_t i = 0; i < splits.size(); i++)
75  {
76  processor_id_type n = splits[i];
77  Moose::out << "Splitting " << n << " ways..." << std::endl;
78 
79  auto cp = libMesh::split_mesh(*mesh, n);
80  Moose::out << " - writing " << cp->current_processor_ids().size() << " files per process..."
81  << std::endl;
82  cp->binary() = checkpoint_binary_flag;
83 
84  // To name the split files, we start with the given mesh filename
85  // (if set) or the argument to --split-file, strip any existing
86  // extension, and then append either .cpr or .cpa depending on the
87  // checkpoint_binary_flag.
88  auto fname = mesh->getFileName();
89  if (fname == "")
90  fname = split_file_arg;
91  fname = MooseUtils::stripExtension(fname) + (checkpoint_binary_flag ? ".cpr" : ".cpa");
92  cp->write(fname);
93  }
94 }
SplitMeshAction(InputParameters params)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
InputParameters & parameters()
Get the parameters of the object.
Definition: MooseApp.h:86
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.
Definition: MooseUtils.h:455
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::string stripExtension(const std::string &s)
Removes any file extension from the fiven string s (i.e.
Definition: MooseUtils.C:280
Base class for actions.
Definition: Action.h:35
std::shared_ptr< MooseMesh > & mesh()
registerMooseAction("MooseApp", SplitMeshAction, "split_mesh")
ActionWarehouse & actionWarehouse()
Definition: MooseApp.h:146
PetscInt n
InputParameters validParams< SplitMeshAction >()
MooseApp & _app
The MOOSE application this is associated with.
Definition: Action.h:183
virtual void act() override
Method to add objects to the simulation or perform other setup tasks.
InputParameters validParams< Action >()
Definition: Action.C:22