www.mooseframework.org
Adaptivity.h
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 #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 // libMesh
23 #include "libmesh/mesh_refinement.h"
24 
25 class FEProblemBase;
26 class MooseMesh;
27 class DisplacedProblem;
28 template <typename>
32 class MooseEnum;
33 
34 // Forward declare classes in libMesh
35 namespace libMesh
36 {
37 class SystemNorm;
38 class ErrorVector;
39 class ErrorEstimator;
40 }
41 
47 {
48 public:
49  Adaptivity(FEProblemBase & subproblem);
50  virtual ~Adaptivity();
51 
58  void init(unsigned int steps, unsigned int initial_steps);
59 
66  template <typename T>
67  void setParam(const std::string & param_name, const T & param_value);
68 
75  void setErrorEstimator(const MooseEnum & error_estimator_name);
76 
80  void setErrorNorm(SystemNorm & sys_norm);
81 
85  void setPrintMeshChanged(bool state = true) { _print_mesh_changed = state; }
86 
92  unsigned int getInitialSteps() const { return _initial_steps; }
93 
99  unsigned int getSteps() const { return _steps; }
100 
106  unsigned int getCyclesPerStep() const { return _cycles_per_step; }
107 
112  void setCyclesPerStep(const unsigned int & num) { _cycles_per_step = num; }
113 
120 
126 
132  bool adaptMesh(std::string marker_name = std::string());
133 
139  bool initialAdaptMesh();
140 
147  static void uniformRefine(MooseMesh * mesh);
148 
154 
159  void setAdaptivityOn(bool state);
160 
166  bool isOn() { return _mesh_refinement_on; }
167 
174  bool isInitialized() { return _initialized; }
175 
181  void setTimeActive(Real start_time, Real stop_time);
182 
186  void setUseNewSystem();
187 
196  void setMarkerVariableName(std::string marker_field);
197 
206  void setInitialMarkerVariableName(std::string marker_field);
207 
211  void setMaxHLevel(unsigned int level) { _max_h_level = level; }
212 
216  unsigned int getMaxHLevel() { return _max_h_level; }
217 
221  void setInterval(unsigned int interval) { _interval = interval; }
222 
231  ErrorVector & getErrorVector(const std::string & indicator_field);
232 
236  void updateErrorVectors();
237 
241  bool isAdaptivityDue();
242 
243 protected:
246 
252  std::unique_ptr<MeshRefinement> _mesh_refinement;
254  std::unique_ptr<ErrorEstimator> _error_estimator;
256  std::unique_ptr<ErrorVector> _error;
257 
258  std::shared_ptr<DisplacedProblem> _displaced_problem;
259 
261  std::unique_ptr<MeshRefinement> _displaced_mesh_refinement;
262 
264  unsigned int _initial_steps;
266  unsigned int _steps;
267 
270 
272  Real & _t;
274  int & _step;
276  unsigned int _interval;
282  unsigned int _cycles_per_step;
283 
286 
289 
292 
294  unsigned int _max_h_level;
295 
298 
300  std::map<std::string, std::unique_ptr<ErrorVector>> _indicator_field_to_error_vector;
301 
307 };
308 
309 template <typename T>
310 void
311 Adaptivity::setParam(const std::string & param_name, const T & param_value)
312 {
313  if (param_name == "refine fraction")
314  {
315  _mesh_refinement->refine_fraction() = param_value;
317  _displaced_mesh_refinement->refine_fraction() = param_value;
318  }
319  else if (param_name == "coarsen fraction")
320  {
321  _mesh_refinement->coarsen_fraction() = param_value;
323  _displaced_mesh_refinement->coarsen_fraction() = param_value;
324  }
325  else if (param_name == "max h-level")
326  {
327  _mesh_refinement->max_h_level() = param_value;
329  _displaced_mesh_refinement->max_h_level() = param_value;
330  }
331  else if (param_name == "cycles_per_step")
332  _cycles_per_step = param_value;
333  else if (param_name == "recompute_markers_during_cycles")
334  _recompute_markers_during_cycles = param_value;
335  else
336  mooseError("Invalid Param in adaptivity object");
337 }
338 #endif // LIBMESH_ENABLE_AMR
339 
bool adaptMesh(std::string marker_name=std::string())
Adapts the mesh based on the error estimator used.
Definition: Adaptivity.C:125
bool initialAdaptMesh()
Used during initial adaptivity.
Definition: Adaptivity.C:200
void setPrintMeshChanged(bool state=true)
Definition: Adaptivity.h:85
std::shared_ptr< DisplacedProblem > _displaced_problem
Definition: Adaptivity.h:258
Real _stop_time
When adaptivity stops.
Definition: Adaptivity.h:280
Adaptivity(FEProblemBase &subproblem)
Definition: Adaptivity.C:30
std::string _marker_variable_name
Name of the marker variable if using the new adaptivity system.
Definition: Adaptivity.h:288
std::unique_ptr< MeshRefinement > _displaced_mesh_refinement
A mesh refinement object for displaced mesh.
Definition: Adaptivity.h:261
void setInterval(unsigned int interval)
Set the interval (number of timesteps) between refinement steps.
Definition: Adaptivity.h:221
Class for stuff related to variables.
Definition: Adaptivity.h:29
std::unique_ptr< ErrorEstimator > _error_estimator
Error estimator to be used by the apps.
Definition: Adaptivity.h:254
bool _initialized
on/off flag reporting if the adaptivity system has been initialized
Definition: Adaptivity.h:250
PerfID _uniform_refine_with_projection
Definition: Adaptivity.h:305
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
void setRecomputeMarkersFlag(const bool flag)
Set the flag to recompute markers during adaptivity cycles.
Definition: Adaptivity.h:125
virtual ~Adaptivity()
Definition: Adaptivity.C:56
void setCyclesPerStep(const unsigned int &num)
Set the number of cycles_per_step.
Definition: Adaptivity.h:112
unsigned int getCyclesPerStep() const
Pull out the number of cycles_per_step previously set through the AdaptivityAction.
Definition: Adaptivity.h:106
bool _recompute_markers_during_cycles
Whether or not to recompute markers during adaptivity cycles.
Definition: Adaptivity.h:297
void updateErrorVectors()
Update the ErrorVectors that have been requested through calls to getErrorVector().
Definition: Adaptivity.C:288
PerfID _uniform_refine_timer
Definition: Adaptivity.h:304
void init(unsigned int steps, unsigned int initial_steps)
Initialize and turn on adaptivity for the simulation.
Definition: Adaptivity.C:59
void setTimeActive(Real start_time, Real stop_time)
Sets the time when the adaptivity is active.
Definition: Adaptivity.C:254
std::unique_ptr< ErrorVector > _error
Error vector for use with the error estimator.
Definition: Adaptivity.h:256
void setMaxHLevel(unsigned int level)
Set the maximum refinement level (for the new Adaptivity system).
Definition: Adaptivity.h:211
void setUseNewSystem()
Tells this object we&#39;re using the "new" adaptivity system.
Definition: Adaptivity.C:261
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
MooseVariableFE< Real > MooseVariable
Definition: Adaptivity.h:29
PerfID _adapt_mesh_timer
Timers.
Definition: Adaptivity.h:303
bool isOn()
Is adaptivity on?
Definition: Adaptivity.h:166
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:291
static void uniformRefine(MooseMesh *mesh)
Performs uniform refinement of the passed Mesh object.
Definition: Adaptivity.C:206
MooseMesh & _mesh
Definition: Adaptivity.h:245
std::map< std::string, std::unique_ptr< ErrorVector > > _indicator_field_to_error_vector
Stores pointers to ErrorVectors associated with indicator field names.
Definition: Adaptivity.h:300
unsigned int PerfID
Definition: MooseTypes.h:163
Real & _t
Time.
Definition: Adaptivity.h:272
FEProblemBase & _subproblem
Definition: Adaptivity.h:244
bool _print_mesh_changed
True if we want to print out info when mesh has changed.
Definition: Adaptivity.h:269
std::unique_ptr< MeshRefinement > _mesh_refinement
A mesh refinement object to be used either with initial refinement or with Adaptivity.
Definition: Adaptivity.h:252
bool getRecomputeMarkersFlag() const
Pull out the _recompute_markers_during_cycles flag previously set through the AdaptivityAction.
Definition: Adaptivity.h:119
bool _mesh_refinement_on
on/off flag reporting if the adaptivity is being used
Definition: Adaptivity.h:248
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:74
void setAdaptivityOn(bool state)
Allow adaptivity to be toggled programatically.
Definition: Adaptivity.C:244
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
void setErrorEstimator(const MooseEnum &error_estimator_name)
Set the error estimator.
Definition: Adaptivity.C:104
unsigned int _steps
steps of adaptivity to perform
Definition: Adaptivity.h:266
Interface for objects that needs transient capabilities.
void uniformRefineWithProjection()
Performs uniform refinement on the meshes in the current object.
Definition: Adaptivity.C:218
unsigned int getMaxHLevel()
Return the maximum h-level.
Definition: Adaptivity.h:216
unsigned int _initial_steps
the number of adaptivity steps to do at the beginning of simulation
Definition: Adaptivity.h:264
bool _use_new_system
Whether or not to use the "new" adaptivity system.
Definition: Adaptivity.h:285
PerfID _update_error_vectors
Definition: Adaptivity.h:306
unsigned int _interval
intreval between adaptivity runs
Definition: Adaptivity.h:276
Takes care of everything related to mesh adaptivity.
Definition: Adaptivity.h:46
unsigned int getInitialSteps() const
Pull out the number of initial steps previously set by calling init()
Definition: Adaptivity.h:92
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:279
unsigned int getSteps() const
Pull out the number of steps previously set by calling init()
Definition: Adaptivity.h:99
unsigned int _cycles_per_step
The number of adaptivity cycles per step.
Definition: Adaptivity.h:282
Real _start_time
When adaptivity start.
Definition: Adaptivity.h:278
void setErrorNorm(SystemNorm &sys_norm)
Set the error norm (FIXME: improve description)
Definition: Adaptivity.C:118
int & _step
Time Step.
Definition: Adaptivity.h:274
MooseVariableFE< VectorValue< Real > > VectorMooseVariable
Definition: Adaptivity.h:31
bool isAdaptivityDue()
Query if an adaptivity step should be performed at the current time / time step.
Definition: Adaptivity.C:309
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:267
unsigned int _max_h_level
The maximum number of refinement levels.
Definition: Adaptivity.h:294
void setParam(const std::string &param_name, const T &param_value)
Set adaptivity parameter.
Definition: Adaptivity.h:311
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:273
bool isInitialized()
Returns whether or not Adaptivity::init() has ran.
Definition: Adaptivity.h:174