https://mooseframework.inl.gov
Adaptivity.h
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 #pragma once
11 
12 #include "libmesh/libmesh_config.h"
13 
14 #ifdef LIBMESH_ENABLE_AMR
15 
16 #include "Moose.h"
17 #include "MooseError.h"
18 #include "ConsoleStreamInterface.h"
19 #include "MooseTypes.h"
20 #include "PerfGraphInterface.h"
21 
22 #include "libmesh/parallel_object.h"
23 
24 // libMesh
25 #include "libmesh/mesh_refinement.h"
26 #include "libmesh/hp_coarsentest.h"
27 #include "libmesh/sibling_coupling.h"
28 
29 class FEProblemBase;
30 class MooseMesh;
31 class DisplacedProblem;
32 template <typename>
36 class MooseEnum;
37 class MultiMooseEnum;
38 
39 // Forward declare classes in libMesh
40 namespace libMesh
41 {
42 class SystemNorm;
43 class ErrorVector;
44 class ErrorEstimator;
45 }
46 
50 enum class AdaptivityType
51 {
52  H = 0,
53  P = 1,
54  HP = 2
55 };
56 
62  public PerfGraphInterface,
64 {
65 public:
66  Adaptivity(FEProblemBase & fe_problem);
67  virtual ~Adaptivity();
68 
76  void init(const unsigned int steps,
77  const unsigned int initial_steps,
78  const AdaptivityType adaptivity_type);
79 
86  template <typename T>
87  void setParam(const std::string & param_name, const T & param_value);
88 
95  void setErrorEstimator(const MooseEnum & error_estimator_name);
96 
100  void setErrorNorm(libMesh::SystemNorm & sys_norm);
101 
105  void setPrintMeshChanged(bool state = true) { _print_mesh_changed = state; }
106 
112  unsigned int getInitialSteps() const { return _initial_steps; }
113 
119  unsigned int getSteps() const { return _steps; }
120 
126  unsigned int getCyclesPerStep() const { return _cycles_per_step; }
127 
132  void setCyclesPerStep(const unsigned int & num) { _cycles_per_step = num; }
133 
140 
146 
159  bool adaptMesh(std::string marker_name = std::string());
160 
166  bool initialAdaptMesh();
167 
174  static void uniformRefine(MooseMesh * mesh, unsigned int level = libMesh::invalid_uint);
175 
181 
186  void setAdaptivityOn(bool state);
187 
193  bool isOn() { return _mesh_refinement_on; }
194 
201  bool isInitialized() { return _initialized; }
202 
208  void setTimeActive(Real start_time, Real stop_time);
209 
213  void setUseNewSystem();
214 
223  void setMarkerVariableName(std::string marker_field);
224 
233  void setInitialMarkerVariableName(std::string marker_field);
234 
238  void setMaxHLevel(unsigned int level) { _max_h_level = level; }
239 
243  unsigned int getMaxHLevel() { return _max_h_level; }
244 
248  void setInterval(unsigned int interval) { _interval = interval; }
249 
258  libMesh::ErrorVector & getErrorVector(const std::string & indicator_field);
259 
263  void updateErrorVectors();
264 
268  bool isAdaptivityDue();
269 
270 protected:
273 
279  std::unique_ptr<libMesh::MeshRefinement> _mesh_refinement;
281  std::unique_ptr<libMesh::ErrorEstimator> _error_estimator;
283  std::unique_ptr<libMesh::ErrorVector> _error;
284 
285  std::shared_ptr<DisplacedProblem> _displaced_problem;
286 
288  std::unique_ptr<libMesh::MeshRefinement> _displaced_mesh_refinement;
289 
291  unsigned int _initial_steps;
293  unsigned int _steps;
294 
297 
299  Real & _t;
301  int & _step;
303  unsigned int _interval;
309  unsigned int _cycles_per_step;
310 
313 
316 
319 
322 
324  unsigned int _max_h_level;
325 
328 
331  std::unique_ptr<libMesh::SiblingCoupling> _sibling_coupling;
332 
334  std::unique_ptr<libMesh::HPCoarsenTest> _hp_coarsen_test;
335 
337  std::map<std::string, std::unique_ptr<libMesh::ErrorVector>> _indicator_field_to_error_vector;
338 };
339 
340 template <typename T>
341 void
342 Adaptivity::setParam(const std::string & param_name, const T & param_value)
343 {
344  if (param_name == "refine fraction")
345  {
346  _mesh_refinement->refine_fraction() = param_value;
348  _displaced_mesh_refinement->refine_fraction() = param_value;
349  }
350  else if (param_name == "coarsen fraction")
351  {
352  _mesh_refinement->coarsen_fraction() = param_value;
354  _displaced_mesh_refinement->coarsen_fraction() = param_value;
355  }
356  else if (param_name == "max h-level")
357  {
358  _mesh_refinement->max_h_level() = param_value;
360  _displaced_mesh_refinement->max_h_level() = param_value;
361  }
362  else if (param_name == "cycles_per_step")
363  _cycles_per_step = param_value;
364  else if (param_name == "recompute_markers_during_cycles")
365  _recompute_markers_during_cycles = param_value;
366  else
367  mooseError("Invalid Param in adaptivity object");
368 }
369 #endif // LIBMESH_ENABLE_AMR
bool adaptMesh(std::string marker_name=std::string())
Adapts the mesh based on the error estimator used.
Definition: Adaptivity.C:145
bool initialAdaptMesh()
Used during initial adaptivity.
Definition: Adaptivity.C:295
std::unique_ptr< libMesh::ErrorVector > _error
Error vector for use with the error estimator.
Definition: Adaptivity.h:283
void setPrintMeshChanged(bool state=true)
Definition: Adaptivity.h:105
std::shared_ptr< DisplacedProblem > _displaced_problem
Definition: Adaptivity.h:285
Real _stop_time
When adaptivity stops.
Definition: Adaptivity.h:307
std::string _marker_variable_name
Name of the marker variable if using the new adaptivity system.
Definition: Adaptivity.h:315
void setInterval(unsigned int interval)
Set the interval (number of timesteps) between refinement steps.
Definition: Adaptivity.h:248
const unsigned int invalid_uint
Class for stuff related to variables.
Definition: Adaptivity.h:33
std::unique_ptr< libMesh::SiblingCoupling > _sibling_coupling
Sibling coupling object for HP adaptivity for evaluating data on elements&#39; siblings in HPCoarsenTest...
Definition: Adaptivity.h:331
MooseVariableFE< libMesh::VectorValue< Real > > VectorMooseVariable
Definition: Adaptivity.h:35
bool _initialized
on/off flag reporting if the adaptivity system has been initialized
Definition: Adaptivity.h:277
static void uniformRefine(MooseMesh *mesh, unsigned int level=libMesh::invalid_uint)
Performs uniform refinement of the passed Mesh object.
Definition: Adaptivity.C:301
AdaptivityType
Defines types of mesh adaptivity options available.
Definition: Adaptivity.h:50
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
void setRecomputeMarkersFlag(const bool flag)
Set the flag to recompute markers during adaptivity cycles.
Definition: Adaptivity.h:145
virtual ~Adaptivity()
Definition: Adaptivity.C:57
void setCyclesPerStep(const unsigned int &num)
Set the number of cycles_per_step.
Definition: Adaptivity.h:132
std::unique_ptr< libMesh::ErrorEstimator > _error_estimator
Error estimator to be used by the apps.
Definition: Adaptivity.h:281
unsigned int getCyclesPerStep() const
Pull out the number of cycles_per_step previously set through the AdaptivityAction.
Definition: Adaptivity.h:126
bool _recompute_markers_during_cycles
Whether or not to recompute markers during adaptivity cycles.
Definition: Adaptivity.h:327
void updateErrorVectors()
Update the ErrorVectors that have been requested through calls to getErrorVector().
Definition: Adaptivity.C:399
std::unique_ptr< libMesh::MeshRefinement > _mesh_refinement
A mesh refinement object to be used either with initial refinement or with Adaptivity.
Definition: Adaptivity.h:279
void setTimeActive(Real start_time, Real stop_time)
Sets the time when the adaptivity is active.
Definition: Adaptivity.C:365
void setMaxHLevel(unsigned int level)
Set the maximum refinement level (for the new Adaptivity system).
Definition: Adaptivity.h:238
void setUseNewSystem()
Tells this object we&#39;re using the "new" adaptivity system.
Definition: Adaptivity.C:372
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
MooseVariableFE< Real > MooseVariable
Definition: Adaptivity.h:33
bool isOn()
Is adaptivity on?
Definition: Adaptivity.h:193
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
std::string _initial_marker_variable_name
Name of the initial marker variable if using the new adaptivity system.
Definition: Adaptivity.h:321
MooseMesh & _mesh
Definition: Adaptivity.h:272
Real & _t
Time.
Definition: Adaptivity.h:299
bool _print_mesh_changed
True if we want to print out info when mesh has changed.
Definition: Adaptivity.h:296
bool getRecomputeMarkersFlag() const
Pull out the _recompute_markers_during_cycles flag previously set through the AdaptivityAction.
Definition: Adaptivity.h:139
bool _mesh_refinement_on
on/off flag reporting if the adaptivity is being used
Definition: Adaptivity.h:275
Adaptivity(FEProblemBase &fe_problem)
Definition: Adaptivity.C:33
An inteface for the _console for outputting to the Console object.
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:93
void setAdaptivityOn(bool state)
Allow adaptivity to be toggled programatically.
Definition: Adaptivity.C:355
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:54
std::unique_ptr< libMesh::MeshRefinement > _displaced_mesh_refinement
A mesh refinement object for displaced mesh.
Definition: Adaptivity.h:288
void setErrorEstimator(const MooseEnum &error_estimator_name)
Set the error estimator.
Definition: Adaptivity.C:124
unsigned int _steps
steps of adaptivity to perform
Definition: Adaptivity.h:293
Interface for objects interacting with the PerfGraph.
void uniformRefineWithProjection()
Performs uniform refinement on the meshes in the current object.
Definition: Adaptivity.C:328
unsigned int getMaxHLevel()
Return the maximum h-level.
Definition: Adaptivity.h:243
void init(const unsigned int steps, const unsigned int initial_steps, const AdaptivityType adaptivity_type)
Initialize and turn on adaptivity for the simulation.
Definition: Adaptivity.C:60
unsigned int _initial_steps
the number of adaptivity steps to do at the beginning of simulation
Definition: Adaptivity.h:291
bool _use_new_system
Whether or not to use the "new" adaptivity system.
Definition: Adaptivity.h:312
std::map< std::string, std::unique_ptr< libMesh::ErrorVector > > _indicator_field_to_error_vector
Stores pointers to ErrorVectors associated with indicator field names.
Definition: Adaptivity.h:337
unsigned int _interval
intreval between adaptivity runs
Definition: Adaptivity.h:303
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Takes care of everything related to mesh adaptivity.
Definition: Adaptivity.h:61
AdaptivityType _adaptivity_type
Type of mesh adaptivity.
Definition: Adaptivity.h:318
unsigned int getInitialSteps() const
Pull out the number of initial steps previously set by calling init()
Definition: Adaptivity.h:112
libMesh::ErrorVector & getErrorVector(const std::string &indicator_field)
Get an ErrorVector that will be filled up with values corresponding to the indicator field name passe...
Definition: Adaptivity.C:390
unsigned int getSteps() const
Pull out the number of steps previously set by calling init()
Definition: Adaptivity.h:119
unsigned int _cycles_per_step
The number of adaptivity cycles per step.
Definition: Adaptivity.h:309
Real _start_time
When adaptivity start.
Definition: Adaptivity.h:305
int & _step
Time Step.
Definition: Adaptivity.h:301
bool isAdaptivityDue()
Query if an adaptivity step should be performed at the current time / time step.
Definition: Adaptivity.C:420
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type...
void setMarkerVariableName(std::string marker_field)
Sets the name of the field variable to actually use to flag elements for refinement / coarsening...
Definition: Adaptivity.C:378
unsigned int _max_h_level
The maximum number of refinement levels.
Definition: Adaptivity.h:324
void setParam(const std::string &param_name, const T &param_value)
Set adaptivity parameter.
Definition: Adaptivity.h:342
void setErrorNorm(libMesh::SystemNorm &sys_norm)
Set the error norm (FIXME: improve description)
Definition: Adaptivity.C:138
void setInitialMarkerVariableName(std::string marker_field)
Sets the name of the field variable to actually use to flag elements for initial refinement / coarsen...
Definition: Adaptivity.C:384
FEProblemBase & _fe_problem
Definition: Adaptivity.h:271
bool isInitialized()
Returns whether or not Adaptivity::init() has ran.
Definition: Adaptivity.h:201
std::unique_ptr< libMesh::HPCoarsenTest > _hp_coarsen_test
Object for HP adaptivity.
Definition: Adaptivity.h:334