https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
20registerMooseAction("MooseApp", SplitMeshAction, "split_mesh");
21
27
29
30void
32{
33 auto mesh = _app.actionWarehouse().mesh();
34
35 // Keep the mesh unpartitioned until right before the split
36 mesh->getMesh().skip_partitioning(false);
37
38 const std::string split_file_arg = _app.parameters().isParamSetByUser("split_file")
39 ? _app.parameters().get<std::string>("split_file")
40 : "";
41
42 if (mesh->getFileName() == "" && split_file_arg == "")
43 mooseError("Output mesh file name must be specified (with --split-file) when splitting "
44 "non-file-based meshes");
45
46 auto splitstr = _app.parameters().get<std::string>("split_mesh");
47 std::vector<unsigned int> splits;
48 bool success = MooseUtils::tokenizeAndConvert(splitstr, splits, ", ");
49 if (!success)
50 mooseError("invalid argument for --split-mesh: '", splitstr, "'");
51
52 // Decide whether to create ASCII or binary splits based on the split_file_arg. We use the
53 // following rules to decide:
54 // 1.) No file extension -> ASCII + gzip
55 // 2.) .cpr file extension -> binary
56 // 3.) .cpa.gz file extension -> ASCII + gzip
57 // 4.) Any other file extension -> mooseError
58
59 // Get the file extension without the dot.
60 std::string split_file_arg_ext = MooseUtils::getExtension(split_file_arg);
61
62 bool checkpoint_binary_flag = false;
63
64 if (split_file_arg_ext != "")
65 {
66 if (split_file_arg_ext == "cpr")
67 checkpoint_binary_flag = true;
68 else if (split_file_arg_ext == "cpa.gz")
69 checkpoint_binary_flag = false;
70 else
71 mooseError("The argument to --split-file, ",
72 split_file_arg,
73 ", must not end in a file extension other than .cpr or .cpa.gz");
74 }
75
76 // To name the split files, we start with the given mesh filename
77 // (if set) or the argument to --split-file, strip any existing
78 // extension, and then append either .cpr or .cpa.gz depending on the
79 // checkpoint_binary_flag.
80 auto fname = mesh->getFileName();
81 if (fname == "")
82 fname = split_file_arg;
83 fname = MooseUtils::stripExtension(fname) + (checkpoint_binary_flag ? ".cpr" : ".cpa.gz");
84
85 for (std::size_t i = 0; i < splits.size(); i++)
86 {
87 processor_id_type n = splits[i];
88 Moose::out << "Splitting " << n << " ways..." << std::endl;
89
90 auto cp = libMesh::split_mesh(*mesh, n);
91 Moose::out << " - writing " << cp->current_processor_ids().size() << " files per process..."
92 << std::endl;
93 cp->binary() = checkpoint_binary_flag;
94
95 // different splits will be written into subfolders with n being the folder name
96 cp->write(fname);
97 }
98
99 // Write mesh metadata
100 if (processor_id() == 0)
101 {
102 const auto filenames = _app.writeRestartableMetaData(MooseApp::MESH_META_DATA, fname);
103 Moose::out << "Mesh meta data written into "
104 << std::filesystem::absolute(filenames[0].parent_path()) << "." << std::endl;
105 }
106}
registerMooseAction("MooseApp", SplitMeshAction, "split_mesh")
std::shared_ptr< MooseMesh > & mesh()
Base class for actions.
Definition Action.h:38
static InputParameters validParams()
Definition Action.C:26
MooseApp & _app
The MOOSE application this is associated with.
Definition MooseBase.h:375
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
bool isParamSetByUser(const std::string &name) const
Method returns true if the parameter was set by the user.
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.
ActionWarehouse & actionWarehouse()
Return a writable reference to the ActionWarehouse associated with this app.
Definition MooseApp.h:217
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:2552
static const RestartableDataMapName MESH_META_DATA
Definition MooseApp.h:136
const InputParameters & parameters() const
Get the parameters of the object.
Definition MooseBase.h:131
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition MooseBase.h:271
static InputParameters validParams()
virtual void act() override
Method to add objects to the simulation or perform other setup tasks.
SplitMeshAction(const InputParameters &params)
processor_id_type processor_id() const
MeshBase & mesh
bool tokenizeAndConvert(const std::string &str, std::vector< T > &tokenized_vector, const std::string &delimiter=" \t\n\v\f\r")
tokenizeAndConvert splits a string using delimiter and then converts to type T.
std::string getExtension(const std::string &filename, const bool rfind)
Definition MooseUtils.C:422
std::string stripExtension(const std::string &s, const bool rfind)
Definition MooseUtils.C:438
std::unique_ptr< CheckpointIO > split_mesh(MeshBase &mesh, processor_id_type nsplits)