Loading [MathJax]/extensions/tex2jax.js
https://mooseframework.inl.gov
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
OversampleOutput.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 // MOOSE includes
11 #include "OversampleOutput.h"
12 #include "FEProblem.h"
13 #include "DisplacedProblem.h"
14 #include "MooseApp.h"
15 
16 #include "libmesh/distributed_mesh.h"
17 #include "libmesh/equation_systems.h"
18 #include "libmesh/mesh_function.h"
19 #include "libmesh/explicit_system.h"
20 
21 using namespace libMesh;
22 
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 }
66 
67 void
69 {
71 
72  // Creates and initializes the oversampled mesh
74 }
75 
76 void
78 {
79  // Output is not allowed
80  if (!_allow_output && type != EXEC_FORCED)
81  return;
82 
83  // If recovering disable output of initial condition, it was already output
84  if (type == EXEC_INITIAL && _app.isRecovering())
85  return;
86 
87  // Return if the current output is not on the desired interval
88  if (type != EXEC_FINAL && !onInterval())
89  return;
90 
91  // store current simulation time
93 
94  // set current type
96 
97  // Call the output method
98  if (shouldOutput())
99  {
100  TIME_SECTION("outputStep", 2, "Outputting Step");
102  output();
103  }
104 
106 }
107 
109 {
110  // TODO: Remove once libmesh Issue #1184 is fixed
111  _oversample_es.reset();
112  _cloned_mesh_ptr.reset();
113 }
114 
115 void
117 {
119 }
120 
121 void
123 {
124  // Perform the mesh cloning, if needed
126  cloneMesh();
127  else
128  return;
129 
130  // Re-position the oversampled mesh
131  if (_change_position)
132  for (auto & node : _mesh_ptr->getMesh().node_ptr_range())
133  *node += _position;
134 
135  // Perform the mesh refinement
136  if (_oversample)
137  {
138  MeshRefinement mesh_refinement(_mesh_ptr->getMesh());
139 
140  // We want original and refined partitioning to match so we can
141  // query from one to the other safely on distributed meshes.
143  mesh_refinement.uniformly_refine(_refinements);
144  }
145 
146  // We can't allow renumbering if we want to output multiple time
147  // steps to the same Exodus file
149 
150  // Create the new EquationSystems
151  _oversample_es = std::make_unique<EquationSystems>(_mesh_ptr->getMesh());
152  _es_ptr = _oversample_es.get();
153 
154  // Reference the system from which we are copying
155  EquationSystems & source_es = _problem_ptr->es();
156 
157  // If we're going to be copying from that system later, we need to keep its
158  // original elements as ghost elements even if it gets grossly
159  // repartitioned, since we can't repartition the oversample mesh to
160  // match.
161  DistributedMesh * dist_mesh = dynamic_cast<DistributedMesh *>(&source_es.get_mesh());
162  if (dist_mesh)
163  {
164  for (auto & elem : dist_mesh->active_local_element_ptr_range())
165  dist_mesh->add_extra_ghost_elem(elem);
166  }
167 
168  // Initialize the _mesh_functions vector
169  unsigned int num_systems = source_es.n_systems();
170  _mesh_functions.resize(num_systems);
171 
172  // Loop over the number of systems
173  for (unsigned int sys_num = 0; sys_num < num_systems; sys_num++)
174  {
175  // Reference to the current system
176  System & source_sys = source_es.get_system(sys_num);
177 
178  // Add the system to the new EquationsSystems
179  ExplicitSystem & dest_sys = _oversample_es->add_system<ExplicitSystem>(source_sys.name());
180 
181  // Loop through the variables in the System
182  unsigned int num_vars = source_sys.n_vars();
183  if (num_vars > 0)
184  {
185  _mesh_functions[sys_num].resize(num_vars);
187  _serialized_solution->init(source_sys.n_dofs(), false, SERIAL);
188 
189  // Need to pull down a full copy of this vector on every processor so we can get values in
190  // parallel
191  source_sys.solution->localize(*_serialized_solution);
192 
193  // Add the variables to the system... simultaneously creating MeshFunctions for them.
194  for (unsigned int var_num = 0; var_num < num_vars; var_num++)
195  {
196  // Add the variable, allow for first and second lagrange
197  const FEType & fe_type = source_sys.variable_type(var_num);
198  FEType second(SECOND, LAGRANGE);
199  if (fe_type == second)
200  dest_sys.add_variable(source_sys.variable_name(var_num), second);
201  else
202  dest_sys.add_variable(source_sys.variable_name(var_num), FEType());
203  }
204  }
205  }
206 
207  // Initialize the newly created EquationSystem
208  _oversample_es->init();
209 }
210 
211 void
213 {
214  // Do nothing if oversampling and changing position are not enabled
215  if (!_oversample && !_change_position)
216  return;
217 
218  // Get a reference to actual equation system
219  EquationSystems & source_es = _problem_ptr->es();
220 
221  // Loop throuch each system
222  for (unsigned int sys_num = 0; sys_num < source_es.n_systems(); ++sys_num)
223  {
224  if (!_mesh_functions[sys_num].empty())
225  {
226  // Get references to the source and destination systems
227  System & source_sys = source_es.get_system(sys_num);
228  System & dest_sys = _oversample_es->get_system(sys_num);
229 
230  // Update the solution for the oversampled mesh
231  _serialized_solution->clear();
232  _serialized_solution->init(source_sys.n_dofs(), false, SERIAL);
233  source_sys.solution->localize(*_serialized_solution);
234 
235  // Update the mesh functions
236  for (unsigned int var_num = 0; var_num < _mesh_functions[sys_num].size(); ++var_num)
237  {
238 
239  // If the mesh has change the MeshFunctions need to be re-built, otherwise simply clear it
240  // for re-initialization
241  if (!_mesh_functions[sys_num][var_num] || _oversample_mesh_changed)
242  _mesh_functions[sys_num][var_num] = std::make_unique<MeshFunction>(
243  source_es, *_serialized_solution, source_sys.get_dof_map(), var_num);
244  else
245  _mesh_functions[sys_num][var_num]->clear();
246 
247  // Initialize the MeshFunctions for application to the oversampled solution
248  _mesh_functions[sys_num][var_num]->init();
249  }
250 
251  // Now loop over the nodes of the oversampled mesh setting values for each variable.
252  for (const auto & node : as_range(_mesh_ptr->localNodesBegin(), _mesh_ptr->localNodesEnd()))
253  for (unsigned int var_num = 0; var_num < _mesh_functions[sys_num].size(); ++var_num)
254  if (node->n_dofs(sys_num, var_num))
255  dest_sys.solution->set(node->dof_number(sys_num, var_num, 0),
256  (*_mesh_functions[sys_num][var_num])(
257  *node - _position)); // 0 value is for component
258 
259  dest_sys.solution->close();
260  }
261  }
262 
263  // Set this to false so that new output files are not created, since the oversampled mesh doesn't
264  // actually change
265  _oversample_mesh_changed = false;
266 }
267 
268 void
270 {
271  // Create the new mesh from a file
272  if (isParamValid("file"))
273  {
274  InputParameters mesh_params = _app.getFactory().getValidParams("FileMesh");
275  mesh_params.applyParameters(parameters(), {}, true);
276  mesh_params.set<bool>("nemesis") = false;
278  _app.getFactory().createUnique<MooseMesh>("FileMesh", "output_problem_mesh", mesh_params);
279  _cloned_mesh_ptr->allowRecovery(false); // We actually want to reread the initial mesh
280  _cloned_mesh_ptr->init();
281  _cloned_mesh_ptr->prepare(/*mesh_to_clone=*/nullptr);
282  _cloned_mesh_ptr->meshChanged();
283  }
284 
285  // Clone the existing mesh
286  else
287  {
288  if (_app.isRecovering())
289  mooseWarning("Recovering or Restarting with Oversampling may not work (especially with "
290  "adapted meshes)!! Refs #2295");
291 
293  }
294 
295  // Make sure that the mesh pointer points to the newly cloned mesh
296  _mesh_ptr = _cloned_mesh_ptr.get();
297 }
298 
299 void
300 OversampleOutput::setFileBaseInternal(const std::string & file_base)
301 {
303  // ** DEPRECATED SUPPORT **
304  if (getParam<bool>("append_oversample"))
305  _file_base += "_oversample";
306 }
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
unsigned int n_systems() const
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:40
void allow_renumbering(bool allow)
OversampleOutput(const InputParameters &parameters)
virtual bool onInterval()
Returns true if the output interval is satisfied.
Definition: Output.C:285
void skip_partitioning(bool skip)
std::vector< std::vector< std::unique_ptr< libMesh::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...
void applyParameters(const InputParameters &common, const std::vector< std::string > &exclude={}, const bool allow_private=false)
Method for applying common parameters.
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
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
virtual void add_extra_ghost_elem(Elem *e)
std::unique_ptr< EquationSystems > _oversample_es
SECOND
const T_sys & get_system(std::string_view name) const
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.
dof_id_type n_dofs() const
Factory & getFactory()
Retrieve a writable reference to the Factory associated with this App.
Definition: MooseApp.h:424
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:283
SERIAL
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:3417
bool _oversample
Flag indicating that oversampling is enabled.
ExecFlagType _current_execute_flag
Current execute on flag.
Definition: Output.h:211
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.
virtual libMesh::EquationSystems & es() override
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition: Output.h:185
std::unique_ptr< NumericVector< Number > > solution
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:88
bool _allow_output
Flag for disabling output.
Definition: Output.h:271
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:51
unsigned int add_variable(std::string_view var, const FEType &type, const std::set< subdomain_id_type > *const active_subdomains=nullptr)
const std::string & variable_name(const unsigned int i) const
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:84
Based class for output objects.
virtual ~OversampleOutput()
virtual void initialSetup()
Call init() method on setup.
const FEType & variable_type(const unsigned int i) const
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...
libMesh::EquationSystems * _es_ptr
Reference the the libMesh::EquationSystems object that contains the data.
Definition: Output.h:194
const MeshBase & get_mesh() const
MooseMesh * _mesh_ptr
A convenience pointer to the current mesh (reference or displaced depending on "use_displaced") ...
Definition: Output.h:197
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 optional parameter and a documentation string to the InputParameters object...
const unsigned int _refinements
The number of oversampling refinements.
const std::string & name() const
MeshBase::node_iterator localNodesEnd()
Definition: MooseMesh.C:3002
static InputParameters validParams()
unsigned int n_vars() const
MeshBase::node_iterator localNodesBegin()
Calls local_nodes_begin/end() on the underlying libMesh mesh object.
Definition: MooseMesh.C:2996
bool isRecovering() const
Whether or not this is a "recover" calculation.
Definition: MooseApp.C:1237
const DofMap & get_dof_map() const
std::unique_ptr< MooseMesh > _cloned_mesh_ptr
bool _change_position
Flag for re-positioning.
static InputParameters validParams()
const ExecFlagType EXEC_FINAL
Definition: Moose.C:39
Real & _time
The current time for output purposes.
Definition: Output.h:214
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