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 
24 {
25 
26  // Get the parameters from the parent object
28  params.addParam<unsigned int>("refinements",
29  0,
30  "Number of uniform refinements for oversampling "
31  "(refinement levels beyond any uniform "
32  "refinements)");
33  params.addParam<Point>("position",
34  "Set a positional offset, this vector will get added to the "
35  "nodal coordinates to move the domain.");
36  params.addParam<MeshFileName>("file", "The name of the mesh file to read, for oversampling");
37 
38  // **** DEPRECATED AND REMOVED PARAMETERS ****
39  params.addDeprecatedParam<bool>("oversample",
40  false,
41  "Set to true to enable oversampling",
42  "This parameter is no longer active, simply set 'refinements' to "
43  "a value greater than zero to evoke oversampling");
44  params.addDeprecatedParam<bool>("append_oversample",
45  false,
46  "Append '_oversample' to the output file base",
47  "This parameter is no longer operational, to append "
48  "'_oversample' utilize the output block name or 'file_base'");
49 
50  // 'Oversampling' Group
51  params.addParamNamesToGroup("refinements position file", "Oversampling");
52 
53  return params;
54 }
55 
57  : AdvancedOutput(parameters),
58  _refinements(getParam<unsigned int>("refinements")),
59  _oversample(_refinements > 0 || isParamValid("file")),
60  _change_position(isParamValid("position")),
61  _position(_change_position ? getParam<Point>("position") : Point()),
62  _oversample_mesh_changed(true)
63 {
64 }
65 
66 void
68 {
70 
71  // Creates and initializes the oversampled mesh
73 }
74 
75 void
77 {
78  // Output is not allowed
79  if (!_allow_output && type != EXEC_FORCED)
80  return;
81 
82  // If recovering disable output of initial condition, it was already output
83  if (type == EXEC_INITIAL && _app.isRecovering())
84  return;
85 
86  // Return if the current output is not on the desired interval
87  if (type != EXEC_FINAL && !onInterval())
88  return;
89 
90  // store current simulation time
92 
93  // set current type
95 
96  // Call the output method
97  if (shouldOutput())
98  {
99  TIME_SECTION("outputStep", 2, "Outputting Step");
101  output();
102  }
103 
105 }
106 
108 {
109  // TODO: Remove once libmesh Issue #1184 is fixed
110  _oversample_es.reset();
111  _cloned_mesh_ptr.reset();
112 }
113 
114 void
116 {
118 }
119 
120 void
122 {
123  // Perform the mesh cloning, if needed
125  cloneMesh();
126  else
127  return;
128 
129  // Re-position the oversampled mesh
130  if (_change_position)
131  for (auto & node : _mesh_ptr->getMesh().node_ptr_range())
132  *node += _position;
133 
134  // Perform the mesh refinement
135  if (_oversample)
136  {
137  MeshRefinement mesh_refinement(_mesh_ptr->getMesh());
138 
139  // We want original and refined partitioning to match so we can
140  // query from one to the other safely on distributed meshes.
141  _mesh_ptr->getMesh().skip_partitioning(true);
142  mesh_refinement.uniformly_refine(_refinements);
143  }
144 
145  // We can't allow renumbering if we want to output multiple time
146  // steps to the same Exodus file
147  _mesh_ptr->getMesh().allow_renumbering(false);
148 
149  // Create the new EquationSystems
150  _oversample_es = std::make_unique<EquationSystems>(_mesh_ptr->getMesh());
151  _es_ptr = _oversample_es.get();
152 
153  // Reference the system from which we are copying
154  EquationSystems & source_es = _problem_ptr->es();
155 
156  // If we're going to be copying from that system later, we need to keep its
157  // original elements as ghost elements even if it gets grossly
158  // repartitioned, since we can't repartition the oversample mesh to
159  // match.
160  DistributedMesh * dist_mesh = dynamic_cast<DistributedMesh *>(&source_es.get_mesh());
161  if (dist_mesh)
162  {
163  for (auto & elem : dist_mesh->active_local_element_ptr_range())
164  dist_mesh->add_extra_ghost_elem(elem);
165  }
166 
167  // Initialize the _mesh_functions vector
168  unsigned int num_systems = source_es.n_systems();
169  _mesh_functions.resize(num_systems);
170 
171  // Loop over the number of systems
172  for (unsigned int sys_num = 0; sys_num < num_systems; sys_num++)
173  {
174  // Reference to the current system
175  System & source_sys = source_es.get_system(sys_num);
176 
177  // Add the system to the new EquationsSystems
178  ExplicitSystem & dest_sys = _oversample_es->add_system<ExplicitSystem>(source_sys.name());
179 
180  // Loop through the variables in the System
181  unsigned int num_vars = source_sys.n_vars();
182  if (num_vars > 0)
183  {
184  _mesh_functions[sys_num].resize(num_vars);
186  _serialized_solution->init(source_sys.n_dofs(), false, SERIAL);
187 
188  // Need to pull down a full copy of this vector on every processor so we can get values in
189  // parallel
190  source_sys.solution->localize(*_serialized_solution);
191 
192  // Add the variables to the system... simultaneously creating MeshFunctions for them.
193  for (unsigned int var_num = 0; var_num < num_vars; var_num++)
194  {
195  // Add the variable, allow for first and second lagrange
196  const FEType & fe_type = source_sys.variable_type(var_num);
197  FEType second(SECOND, LAGRANGE);
198  if (fe_type == second)
199  dest_sys.add_variable(source_sys.variable_name(var_num), second);
200  else
201  dest_sys.add_variable(source_sys.variable_name(var_num), FEType());
202  }
203  }
204  }
205 
206  // Initialize the newly created EquationSystem
207  _oversample_es->init();
208 }
209 
210 void
212 {
213  // Do nothing if oversampling and changing position are not enabled
214  if (!_oversample && !_change_position)
215  return;
216 
217  // Get a reference to actual equation system
218  EquationSystems & source_es = _problem_ptr->es();
219 
220  // Loop throuch each system
221  for (unsigned int sys_num = 0; sys_num < source_es.n_systems(); ++sys_num)
222  {
223  if (!_mesh_functions[sys_num].empty())
224  {
225  // Get references to the source and destination systems
226  System & source_sys = source_es.get_system(sys_num);
227  System & dest_sys = _oversample_es->get_system(sys_num);
228 
229  // Update the solution for the oversampled mesh
230  _serialized_solution->clear();
231  _serialized_solution->init(source_sys.n_dofs(), false, SERIAL);
232  source_sys.solution->localize(*_serialized_solution);
233 
234  // Update the mesh functions
235  for (unsigned int var_num = 0; var_num < _mesh_functions[sys_num].size(); ++var_num)
236  {
237 
238  // If the mesh has change the MeshFunctions need to be re-built, otherwise simply clear it
239  // for re-initialization
240  if (!_mesh_functions[sys_num][var_num] || _oversample_mesh_changed)
241  _mesh_functions[sys_num][var_num] = std::make_unique<MeshFunction>(
242  source_es, *_serialized_solution, source_sys.get_dof_map(), var_num);
243  else
244  _mesh_functions[sys_num][var_num]->clear();
245 
246  // Initialize the MeshFunctions for application to the oversampled solution
247  _mesh_functions[sys_num][var_num]->init();
248  }
249 
250  // Now loop over the nodes of the oversampled mesh setting values for each variable.
251  for (const auto & node : as_range(_mesh_ptr->localNodesBegin(), _mesh_ptr->localNodesEnd()))
252  for (unsigned int var_num = 0; var_num < _mesh_functions[sys_num].size(); ++var_num)
253  if (node->n_dofs(sys_num, var_num))
254  dest_sys.solution->set(node->dof_number(sys_num, var_num, 0),
255  (*_mesh_functions[sys_num][var_num])(
256  *node - _position)); // 0 value is for component
257 
258  dest_sys.solution->close();
259  }
260  }
261 
262  // Set this to false so that new output files are not created, since the oversampled mesh doesn't
263  // actually change
264  _oversample_mesh_changed = false;
265 }
266 
267 void
269 {
270  // Create the new mesh from a file
271  if (isParamValid("file"))
272  {
273  InputParameters mesh_params = emptyInputParameters();
274  mesh_params += _mesh_ptr->parameters();
275  mesh_params.set<MeshFileName>("file") = getParam<MeshFileName>("file");
276  mesh_params.set<bool>("nemesis") = false;
277  mesh_params.set<bool>("skip_partitioning") = false;
278  mesh_params.set<std::string>("_object_name") = "output_problem_mesh";
279  _cloned_mesh_ptr = std::make_unique<FileMesh>(mesh_params);
280  _cloned_mesh_ptr->allowRecovery(false); // We actually want to reread the initial mesh
281  _cloned_mesh_ptr->init();
282  _cloned_mesh_ptr->prepare(/*mesh_to_clone=*/nullptr);
283  _cloned_mesh_ptr->meshChanged();
284  }
285 
286  // Clone the existing mesh
287  else
288  {
289  if (_app.isRecovering())
290  mooseWarning("Recovering or Restarting with Oversampling may not work (especially with "
291  "adapted meshes)!! Refs #2295");
292 
294  }
295 
296  // Make sure that the mesh pointer points to the newly cloned mesh
297  _mesh_ptr = _cloned_mesh_ptr.get();
298 }
299 
300 void
301 OversampleOutput::setFileBaseInternal(const std::string & file_base)
302 {
304  // ** DEPRECATED SUPPORT **
305  if (getParam<bool>("append_oversample"))
306  _file_base += "_oversample";
307 }
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.
LAGRANGE
virtual void setFileBaseInternal(const std::string &file_base)
Internal function that sets the file_base.
Definition: FileOutput.C:125
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)
const ExecFlagType EXEC_FORCED
Definition: Moose.C:39
OversampleOutput(const InputParameters &parameters)
virtual bool onInterval()
Returns true if the output interval is satisfied.
Definition: Output.C:286
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...
Point _position
When oversampling, the output is shift by this amount.
const ExecFlagType EXEC_NONE
Definition: Moose.C:27
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:89
virtual bool shouldOutput()
Handles logic for determining if a step should be output.
virtual void output()
A single call to this function should output all the necessary data for a single timestep.
const Parallel::Communicator & _communicator
std::unique_ptr< EquationSystems > _oversample_es
SECOND
void initOversample()
Setups the output object to produce re-positioned and/or oversampled results.
std::unique_ptr< NumericVector< Number > > _serialized_solution
Oversample solution vector.
void mooseWarning(Args &&... args) const
Emits a warning prefixed with object name and type.
InputParameters emptyInputParameters()
virtual EquationSystems & es() override
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
virtual void setFileBaseInternal(const std::string &file_base) override
Appends the base class&#39;s file base string.
Real & _last_output_simulation_time
last simulation time an output has occured
Definition: Output.h:277
SERIAL
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:3198
EquationSystems * _es_ptr
Reference the the libMesh::EquationSystems object that contains the data.
Definition: Output.h:188
bool _oversample
Flag indicating that oversampling is enabled.
ExecFlagType _current_execute_flag
Current execute on flag.
Definition: Output.h:205
virtual void initialSetup() override
Call init() method on setup.
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
void cloneMesh()
Clone mesh in preperation for re-positioning or oversampling.
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition: Output.h:179
bool _allow_output
Flag for disabling output.
Definition: Output.h:265
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:50
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:69
static std::unique_ptr< NumericVector< Number > > build(const Parallel::Communicator &comm, const SolverPackage solver_package=libMesh::default_solver_package())
Based class for output objects.
virtual ~OversampleOutput()
virtual void initialSetup()
Call init() method on setup.
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:18
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...
MooseMesh * _mesh_ptr
A convenience pointer to the current mesh (reference or displaced depending on "use_displaced") ...
Definition: Output.h:191
const InputParameters & parameters() const
Get the parameters of the object.
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.
MeshBase::node_iterator localNodesEnd()
Definition: MooseMesh.C:2783
static InputParameters validParams()
MeshBase::node_iterator localNodesBegin()
Calls local_nodes_begin/end() on the underlying libMesh mesh object.
Definition: MooseMesh.C:2777
bool isRecovering() const
Whether or not this is a "recover" calculation.
Definition: MooseApp.C:1167
std::unique_ptr< MooseMesh > _cloned_mesh_ptr
bool _change_position
Flag for re-positioning.
static InputParameters validParams()
const ExecFlagType EXEC_FINAL
Definition: Moose.C:38
Real & _time
The current time for output purposes.
Definition: Output.h:208
void ErrorVector unsigned int
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
Definition: Moose.C:28