https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MeshOnlyAction.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 "MeshOnlyAction.h"
11
12#include "MooseApp.h"
13#include "MooseMesh.h"
14#include "Exodus.h"
15#include "AddOutputAction.h"
17
18#include <filesystem>
19
20#include "libmesh/exodusII_io.h"
21#include "libmesh/exodusII_io_helper.h"
22#include "libmesh/checkpoint_io.h"
23
24registerMooseAction("MooseApp", MeshOnlyAction, "mesh_only");
25
31
33
34void
36{
37 // Run error checking on input file first before trying to generate a mesh
38 bool warn = _app.unusedFlagIsWarning();
39 bool err = _app.unusedFlagIsError();
40 _app.builder().errorCheck(comm(), warn, err);
41
42 std::string mesh_file = _app.parameters().get<std::string>("mesh_only");
43 auto & mesh_ptr = _app.actionWarehouse().mesh();
44
45 // Print information about the mesh
46 _console << mesh_ptr->getMesh().get_info(/* verbosity = */ 2) << std::endl;
47
48 if (mesh_file.empty())
49 {
50 mesh_file = _app.builder().getPrimaryFileName();
51 size_t pos = mesh_file.find_last_of('.');
52
53 // Default to writing out an ExodusII mesh base on the input filename.
54 mesh_file = mesh_file.substr(0, pos) + "_in.e";
55 }
56
62 if (mesh_file.find(".e") + 2 == mesh_file.size())
63 {
64 TIME_SECTION("act", 1, "Writing Exodus");
65
66 auto & output_mesh = mesh_ptr->getMesh();
67 ExodusII_IO exio(output_mesh);
68
69 // Default to the maximum name length allowed by libMesh ExodusII
70 exio.set_max_name_length(80);
71
72 // Extract the Output action to look for a non-default name
73 // length, e.g. truncation to 32 to match gold files
74 const auto & output_actions = _app.actionWarehouse().getActionListByName("add_output");
75 for (const auto & act : output_actions)
76 {
77 AddOutputAction * action = dynamic_cast<AddOutputAction *>(act);
78 if (!action)
79 continue;
80
81 InputParameters & params = action->getObjectParams();
82 if (params.isParamSetByUser("max_output_name_length"))
83 {
84 const int max_output_name_length =
85 action->getObjectParams().get<unsigned int>("max_output_name_length");
86
87 exio.set_max_name_length(max_output_name_length);
88
89 break;
90 }
91 }
92
94
95 // Default to non-HDF5 output for wider compatibility
96 exio.set_hdf5_writing(false);
97
98 exio.write(mesh_file);
99
100 // Check if extra element integers should be outputted to Exodus file
101 unsigned int n_eeid = output_mesh.n_elem_integers();
102
103 // Iterate through all actions and see if `Outputs/output_extra_element_ids = true` in input
104 // file
105
106 // Truth of whether to output extra element ids is initially determined by whether
107 // there are extra element ids defined on the mesh
108 bool output_extra_ids = (n_eeid > 0);
109 bool restrict_element_id_names = false;
110 std::vector<std::string> element_id_names;
111 for (const auto & act : output_actions)
112 {
113 // Extract the Output action
114 AddOutputAction * action = dynamic_cast<AddOutputAction *>(act);
115 if (!action)
116 continue;
117
118 InputParameters & params = action->getObjectParams();
119 if (params.isParamSetByUser("output_extra_element_ids"))
120 {
121 // User has set output_extra_element_ids, truth of output_extra_ids determined by value of
122 // parameter
123 output_extra_ids = params.get<bool>("output_extra_element_ids");
124 if (output_extra_ids)
125 {
126 // `Outputs/extra_element_ids_to_output` sets a subset of extra element ids that should
127 // be outputted to Exodus
128 restrict_element_id_names = params.isParamValid("extra_element_ids_to_output");
129 if (restrict_element_id_names)
130 {
131 element_id_names = params.get<std::vector<std::string>>("extra_element_ids_to_output");
132 // Check which element id names actually are defined on the mesh, remove from
133 // element_id_names if they don't belong
134 for (auto it = element_id_names.begin(); it != element_id_names.end();)
135 {
136 // Erase contents of iterator and return iterator if element integer does not exist
137 if (!output_mesh.has_elem_integer(*it))
138 {
139 it = element_id_names.erase(it);
140 mooseWarning("Extra element id ",
141 *it,
142 " defined in Outputs/extra_element_ids_to_output "
143 "is not defined on the mesh and will be ignored.");
144 }
145 // Increment iterator if element integer exists
146 else
147 ++it;
148 }
149 }
150 }
151 break;
152 }
153 }
154
155 if (output_extra_ids)
156 {
157 // Retrieve extra element id names and associated data
158 const auto n_elem = output_mesh.n_elem();
159 std::vector<std::string> eeid_vars;
160 const auto n_eeid_to_output = restrict_element_id_names ? element_id_names.size() : n_eeid;
161 std::vector<Number> eeid_soln(n_elem * n_eeid_to_output);
162 unsigned int soln_index = 0;
163 for (unsigned int i = 0; i < n_eeid; i++)
164 {
165 const auto eeid_name = output_mesh.get_elem_integer_name(i);
166 // If `Outputs/extra_element_ids_to_output` is not set, output all ids to Exodus
167 // Otherwise only output if the extra id name is contained within
168 // `Outputs/extra_element_ids_to_output`
169 if (!restrict_element_id_names ||
170 (std::find(element_id_names.begin(), element_id_names.end(), eeid_name) !=
171 element_id_names.end()))
172 {
173 eeid_vars.push_back(output_mesh.get_elem_integer_name(i));
174 for (const auto & elem : output_mesh.element_ptr_range())
175 {
176 eeid_soln[soln_index] = (int)elem->get_extra_integer(i);
177 ++soln_index;
178 }
179 }
180 }
181
182 // Check size of output variables just in case none of the variables in
183 // `Outpus/extra_element_ids_to_output` are specified on the actual mesh
184 if (eeid_vars.size() > 0)
185 {
186 // Invoke ExodusII_IO_Helper to output extra element ids to Exodus file
187 auto & exio_helper = exio.get_exio_helper();
188
189 // Output empty timestep to Exodus file
190 int empty_timestep = 1;
191 Real default_time = 1.0;
192 exio_helper.write_timestep(empty_timestep, default_time);
193
194 // Write extra element id data to Exodus file
195 std::vector<std::set<subdomain_id_type>> vars_active_subdomains;
196 vars_active_subdomains.resize(n_eeid_to_output);
197 exio_helper.initialize_element_variables(eeid_vars, vars_active_subdomains);
198 exio_helper.write_element_values(
199 output_mesh, eeid_soln, empty_timestep, vars_active_subdomains);
200 }
201 else
203 "Outputs/output_extra_element_ids is set to true but no extra element ids are being "
204 "outputted. Please check extra element ids are properly defined on the mesh and any "
205 "variables specified in Outputs/extra_element_ids_to_output are spelled correctly.");
206 }
207 }
208
209 else if (mesh_file.find(".cpa.gz") + 7 == mesh_file.size())
210 {
211 TIME_SECTION("act", 1, "Writing Checkpoint");
212
213 libMesh::CheckpointIO io(mesh_ptr->getMesh(), false);
214 io.write(mesh_file);
215
216 // Write mesh metadata
217 if (processor_id() == 0)
218 {
219 const auto filenames = _app.writeRestartableMetaData(MooseApp::MESH_META_DATA, mesh_file);
220 Moose::out << "Mesh meta data written into "
221 << std::filesystem::absolute(filenames[0].parent_path()) << "." << std::endl;
222 }
223 }
224 else
225 {
226 // Just write the file using the name requested by the user.
227 mesh_ptr->getMesh().write(mesh_file);
228 }
229}
registerMooseAction("MooseApp", MeshOnlyAction, "mesh_only")
void ErrorVector unsigned int
std::shared_ptr< MooseMesh > & mesh()
const std::list< Action * > & getActionListByName(const std::string &task) const
Retrieve a constant list of Action pointers associated with the passed in task.
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
Action for creating output objects.
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
static void setOutputDimensionInExodusWriter(libMesh::ExodusII_IO &exodus_io, const MooseMesh &mesh, OutputDimension output_dim=OutputDimension::DEFAULT)
Helper method to change the output dimension in the passed in Exodus writer depending on the dimensio...
Definition Exodus.C:294
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.
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another,...
static InputParameters validParams()
MeshOnlyAction(const InputParameters &params)
virtual void act() override
Method to add objects to the simulation or perform other setup tasks.
ActionWarehouse & actionWarehouse()
Return a writable reference to the ActionWarehouse associated with this app.
Definition MooseApp.h:217
bool unusedFlagIsError() const
Returns whether the flag for unused parameters is set to throw an error.
Definition MooseApp.h:1120
Moose::Builder & builder()
Returns a writable reference to the builder.
Definition MooseApp.h:226
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
bool unusedFlagIsWarning() const
Returns whether the flag for unused parameters is set to throw a warning only.
Definition MooseApp.h:1117
static const RestartableDataMapName MESH_META_DATA
Definition MooseApp.h:136
const InputParameters & parameters() const
Get the parameters of the object.
Definition MooseBase.h:131
InputParameters & getObjectParams()
Retrieve the parameters of the object to be created by this action.
std::string getPrimaryFileName(bool stripLeadingPath=true) const
Return the primary (first) filename that was parsed.
Definition Builder.C:179
void errorCheck(const libMesh::Parallel::Communicator &comm, bool warn_unused, bool err_unused)
Definition Builder.C:358
void mooseWarning(Args &&... args) const
virtual void write(const std::string &name) override
processor_id_type processor_id() const
const Parallel::Communicator & comm() const