www.mooseframework.org
OversampleOutput.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 // MOOSE includes
11 #include "OversampleOutput.h"
12 #include "FEProblem.h"
13 #include "DisplacedProblem.h"
14 #include "FileMesh.h"
15 #include "MooseApp.h"
16 
17 #include "libmesh/distributed_mesh.h"
18 #include "libmesh/equation_systems.h"
19 #include "libmesh/mesh_function.h"
20 #include "libmesh/explicit_system.h"
21 
22 template <>
25 {
26 
27  // Get the parameters from the parent object
29  params.addParam<unsigned int>("refinements",
30  0,
31  "Number of uniform refinements for oversampling "
32  "(refinement levels beyond any uniform "
33  "refinements)");
34  params.addParam<Point>("position",
35  "Set a positional offset, this vector will get added to the "
36  "nodal coordinates to move the domain.");
37  params.addParam<MeshFileName>("file", "The name of the mesh file to read, for oversampling");
38 
39  // **** DEPRECATED AND REMOVED PARAMETERS ****
40  params.addDeprecatedParam<bool>("oversample",
41  false,
42  "Set to true to enable oversampling",
43  "This parameter is no longer active, simply set 'refinements' to "
44  "a value greater than zero to evoke oversampling");
45  params.addDeprecatedParam<bool>("append_oversample",
46  false,
47  "Append '_oversample' to the output file base",
48  "This parameter is no longer operational, to append "
49  "'_oversample' utilize the output block name or 'file_base'");
50 
51  // 'Oversampling' Group
52  params.addParamNamesToGroup("refinements position file", "Oversampling");
53 
54  return params;
55 }
56 
58  : AdvancedOutput(parameters),
59  _refinements(getParam<unsigned int>("refinements")),
60  _oversample(_refinements > 0 || isParamValid("file")),
61  _change_position(isParamValid("position")),
62  _position(_change_position ? getParam<Point>("position") : Point()),
63  _oversample_mesh_changed(true)
64 {
65  // ** DEPRECATED SUPPORT **
66  if (getParam<bool>("append_oversample"))
67  _file_base += "_oversample";
68 }
69 
70 void
72 {
74 
75  // Creates and initializes the oversampled mesh
77 }
78 
79 void
81 {
82  // Output is not allowed
83  if (!_allow_output && type != EXEC_FORCED)
84  return;
85 
86  // If recovering disable output of initial condition, it was already output
87  if (type == EXEC_INITIAL && _app.isRecovering())
88  return;
89 
90  // Return if the current output is not on the desired interval
91  if (type != EXEC_FINAL && !onInterval())
92  return;
93 
94  // Call the output method (this has the file checking built in b/c OversampleOutput is a
95  // FileOutput)
96  if (shouldOutput(type))
97  {
98  TIME_SECTION(_output_step_timer);
100  output(type);
101  }
102 }
103 
105 {
106  // TODO: Remove once libmesh Issue #1184 is fixed
107  _oversample_es.reset();
108  _cloned_mesh_ptr.reset();
109 }
110 
111 void
113 {
115 }
116 
117 void
119 {
120  // Perform the mesh cloning, if needed
122  cloneMesh();
123  else
124  return;
125 
126  // Re-position the oversampled mesh
127  if (_change_position)
128  for (auto & node : _mesh_ptr->getMesh().node_ptr_range())
129  *node += _position;
130 
131  // Perform the mesh refinement
132  if (_oversample)
133  {
134  MeshRefinement mesh_refinement(_mesh_ptr->getMesh());
135 
136  // We want original and refined partitioning to match so we can
137  // query from one to the other safely on distributed meshes.
138  _mesh_ptr->getMesh().skip_partitioning(true);
139  mesh_refinement.uniformly_refine(_refinements);
140  }
141 
142  // We can't allow renumbering if we want to output multiple time
143  // steps to the same Exodus file
144  _mesh_ptr->getMesh().allow_renumbering(false);
145 
146  // Create the new EquationSystems
147  _oversample_es = libmesh_make_unique<EquationSystems>(_mesh_ptr->getMesh());
148  _es_ptr = _oversample_es.get();
149 
150  // Reference the system from which we are copying
151  EquationSystems & source_es = _problem_ptr->es();
152 
153  // If we're going to be copying from that system later, we need to keep its
154  // original elements as ghost elements even if it gets grossly
155  // repartitioned, since we can't repartition the oversample mesh to
156  // match.
157  DistributedMesh * dist_mesh = dynamic_cast<DistributedMesh *>(&source_es.get_mesh());
158  if (dist_mesh)
159  {
160  for (auto & elem : dist_mesh->active_local_element_ptr_range())
161  dist_mesh->add_extra_ghost_elem(elem);
162  }
163 
164  // Initialize the _mesh_functions vector
165  unsigned int num_systems = source_es.n_systems();
166  _mesh_functions.resize(num_systems);
167 
168  // Loop over the number of systems
169  for (unsigned int sys_num = 0; sys_num < num_systems; sys_num++)
170  {
171  // Reference to the current system
172  System & source_sys = source_es.get_system(sys_num);
173 
174  // Add the system to the new EquationsSystems
175  ExplicitSystem & dest_sys = _oversample_es->add_system<ExplicitSystem>(source_sys.name());
176 
177  // Loop through the variables in the System
178  unsigned int num_vars = source_sys.n_vars();
179  if (num_vars > 0)
180  {
181  _mesh_functions[sys_num].resize(num_vars);
182  _serialized_solution = NumericVector<Number>::build(_communicator);
183  _serialized_solution->init(source_sys.n_dofs(), false, SERIAL);
184 
185  // Need to pull down a full copy of this vector on every processor so we can get values in
186  // parallel
187  source_sys.solution->localize(*_serialized_solution);
188 
189  // Add the variables to the system... simultaneously creating MeshFunctions for them.
190  for (unsigned int var_num = 0; var_num < num_vars; var_num++)
191  {
192  // Add the variable, allow for first and second lagrange
193  const FEType & fe_type = source_sys.variable_type(var_num);
194  FEType second(SECOND, LAGRANGE);
195  if (fe_type == second)
196  dest_sys.add_variable(source_sys.variable_name(var_num), second);
197  else
198  dest_sys.add_variable(source_sys.variable_name(var_num), FEType());
199  }
200  }
201  }
202 
203  // Initialize the newly created EquationSystem
204  _oversample_es->init();
205 }
206 
207 void
209 {
210  // Do nothing if oversampling and changing position are not enabled
211  if (!_oversample && !_change_position)
212  return;
213 
214  // Get a reference to actual equation system
215  EquationSystems & source_es = _problem_ptr->es();
216 
217  // Loop throuch each system
218  for (unsigned int sys_num = 0; sys_num < source_es.n_systems(); ++sys_num)
219  {
220  if (!_mesh_functions[sys_num].empty())
221  {
222  // Get references to the source and destination systems
223  System & source_sys = source_es.get_system(sys_num);
224  System & dest_sys = _oversample_es->get_system(sys_num);
225 
226  // Update the solution for the oversampled mesh
227  _serialized_solution->clear();
228  _serialized_solution->init(source_sys.n_dofs(), false, SERIAL);
229  source_sys.solution->localize(*_serialized_solution);
230 
231  // Update the mesh functions
232  for (unsigned int var_num = 0; var_num < _mesh_functions[sys_num].size(); ++var_num)
233  {
234 
235  // If the mesh has change the MeshFunctions need to be re-built, otherwise simply clear it
236  // for re-initialization
237  if (!_mesh_functions[sys_num][var_num] || _oversample_mesh_changed)
238  _mesh_functions[sys_num][var_num] = libmesh_make_unique<MeshFunction>(
239  source_es, *_serialized_solution, source_sys.get_dof_map(), var_num);
240  else
241  _mesh_functions[sys_num][var_num]->clear();
242 
243  // Initialize the MeshFunctions for application to the oversampled solution
244  _mesh_functions[sys_num][var_num]->init();
245  }
246 
247  // Now loop over the nodes of the oversampled mesh setting values for each variable.
248  for (const auto & node : as_range(_mesh_ptr->localNodesBegin(), _mesh_ptr->localNodesEnd()))
249  for (unsigned int var_num = 0; var_num < _mesh_functions[sys_num].size(); ++var_num)
250  if (node->n_dofs(sys_num, var_num))
251  dest_sys.solution->set(node->dof_number(sys_num, var_num, 0),
252  (*_mesh_functions[sys_num][var_num])(
253  *node - _position)); // 0 value is for component
254 
255  dest_sys.solution->close();
256  }
257  }
258 
259  // Set this to false so that new output files are not created, since the oversampled mesh doesn't
260  // actually change
261  _oversample_mesh_changed = false;
262 }
263 
264 void
266 {
267  // Create the new mesh from a file
268  if (isParamValid("file"))
269  {
270  InputParameters mesh_params = emptyInputParameters();
271  mesh_params += _mesh_ptr->parameters();
272  mesh_params.set<MeshFileName>("file") = getParam<MeshFileName>("file");
273  mesh_params.set<bool>("nemesis") = false;
274  mesh_params.set<bool>("skip_partitioning") = false;
275  mesh_params.set<std::string>("_object_name") = "output_problem_mesh";
276  _cloned_mesh_ptr = libmesh_make_unique<FileMesh>(mesh_params);
277  _cloned_mesh_ptr->allowRecovery(false); // We actually want to reread the initial mesh
278  _cloned_mesh_ptr->init();
279  _cloned_mesh_ptr->prepare();
280  _cloned_mesh_ptr->meshChanged();
281  }
282 
283  // Clone the existing mesh
284  else
285  {
286  if (_app.isRecovering())
287  mooseWarning("Recovering or Restarting with Oversampling may not work (especially with "
288  "adapted meshes)!! Refs #2295");
289 
291  }
292 
293  // Make sure that the mesh pointer points to the newly cloned mesh
294  _mesh_ptr = _cloned_mesh_ptr.get();
295 }
bool _oversample_mesh_changed
A flag indicating that the mesh has changed and the oversampled mesh needs to be re-initialized.
virtual void meshChanged() override
Called on this object when the mesh changes.
virtual void updateOversample()
Performs the update of the solution vector for the oversample/re-positioned mesh. ...
void addDeprecatedParam(const std::string &name, const T &value, const std::string &doc_string, const std::string &deprecation_message)
InputParameters validParams< OversampleOutput >()
const ExecFlagType EXEC_FORCED
void mooseWarning(Args &&... args) const
Definition: MooseObject.h:155
OversampleOutput(const InputParameters &parameters)
virtual bool onInterval()
Returns true if the output interval is satisfied.
Definition: Output.C:199
std::vector< std::vector< std::unique_ptr< MeshFunction > > > _mesh_functions
A vector of pointers to the mesh functions This is only populated when the oversample() function is c...
InputParameters validParams< AdvancedOutput >()
Point _position
When oversampling, the output is shift by this amount.
MeshBase::const_node_iterator localNodesBegin()
Calls local_nodes_begin/end() on the underlying libMesh mesh object.
Definition: MooseMesh.C:2205
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::string _file_base
The base filename from the input paramaters.
Definition: FileOutput.h:81
std::unique_ptr< EquationSystems > _oversample_es
void initOversample()
Setups the output object to produce re-positioned and/or oversampled results.
std::unique_ptr< NumericVector< Number > > _serialized_solution
Oversample solution vector.
InputParameters emptyInputParameters()
MeshBase::const_node_iterator localNodesEnd()
Definition: MooseMesh.C:2211
virtual EquationSystems & es() override
const std::string & type() const
Get the type of this object.
Definition: MooseObject.h:53
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2567
EquationSystems * _es_ptr
Reference the the libMesh::EquationSystems object that contains the data.
Definition: Output.h:176
bool _oversample
Flag indicating that oversampling is enabled.
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseObject.h:65
virtual void output(const ExecFlagType &type)
A single call to this function should output all the necessary data for a single timestep.
virtual void initialSetup() override
Initialization method.
void cloneMesh()
Clone mesh in preperation for re-positioning or oversampling.
virtual bool shouldOutput(const ExecFlagType &type)
Handles logic for determining if a step should be output.
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition: Output.h:167
bool _allow_output
Flag for disabling output.
Definition: Output.h:233
PerfID _output_step_timer
Timers.
Definition: Output.h:245
Based class for output objects.
virtual ~OversampleOutput()
MatType type
virtual void initialSetup()
Initialization method.
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:21
virtual std::unique_ptr< MooseMesh > safeClone() const =0
A safer version of the clone() method that hands back an allocated object wrapped in a smart pointer...
MooseApp & _app
The MooseApp this object is associated with.
Definition: MooseObject.h:177
MooseMesh * _mesh_ptr
A convenience pointer to the current mesh (reference or displaced depending on "use_displaced") ...
Definition: Output.h:179
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
const unsigned int _refinements
The number of oversampling refinements.
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:89
bool isRecovering() const
Whether or not this is a "recover" calculation.
Definition: MooseApp.C:859
std::unique_ptr< MooseMesh > _cloned_mesh_ptr
bool _change_position
Flag for re-positioning.
const ExecFlagType EXEC_FINAL
virtual void outputStep(const ExecFlagType &type) override
A single call to this function should output all the necessary data for a single timestep.
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...
const ExecFlagType EXEC_INITIAL