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