https://mooseframework.inl.gov
AdaptivityAction.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 #include "AdaptivityAction.h"
11 
12 #ifdef LIBMESH_ENABLE_AMR
13 
14 #include "FEProblem.h"
15 #include "NonlinearSystemBase.h"
16 #include "Adaptivity.h"
17 #include "Executioner.h"
18 #include "MooseEnum.h"
19 #include "MooseVariableFE.h"
20 #include "RelationshipManager.h"
22 
23 // libMesh includes
24 #include "libmesh/transient_system.h"
25 #include "libmesh/system_norm.h"
26 #include "libmesh/enum_norm_type.h"
27 
28 using namespace libMesh;
29 
30 registerMooseAction("MooseApp", AdaptivityAction, "setup_adaptivity");
31 registerMooseAction("MooseApp", AdaptivityAction, "add_geometric_rm");
32 registerMooseAction("MooseApp", AdaptivityAction, "add_algebraic_rm");
33 
36 {
38  params.addClassDescription(
39  "Add libMesh based adaptation schemes via the Executioner/Adaptivity input syntax.");
40  MooseEnum estimators("KellyErrorEstimator LaplacianErrorEstimator PatchRecoveryErrorEstimator",
41  "KellyErrorEstimator");
42 
43  params.addParam<unsigned int>(
44  "initial_adaptivity",
45  0,
46  "The number of adaptivity steps to perform using the initial conditions");
47  params.addParam<Real>("refine_fraction",
48  0.0,
49  "The fraction of elements or error to refine. Should be between 0 and 1.");
50  params.addParam<Real>("coarsen_fraction",
51  0.0,
52  "The fraction of elements or error to coarsen. Should be between 0 and 1.");
53  params.addParam<MooseEnum>(
54  "error_estimator", estimators, "The class name of the error estimator you want to use.");
55  params.addDeprecatedParam<bool>(
56  "print_changed_info",
57  false,
58  "Determines whether information about the mesh is printed when adaptivity occurs",
59  "Use the Console output parameter 'print_mesh_changed_info'");
60  params.addParam<std::vector<std::string>>(
61  "weight_names", {}, "List of names of variables that will be associated with weight_values");
62  params.addParam<std::vector<Real>>(
63  "weight_values",
64  {},
65  "List of values between 0 and 1 to weight the associated weight_names error by");
66  params.addParam<bool>(
67  "show_initial_progress", true, "Show the progress of the initial adaptivity");
68  return params;
69 }
70 
72 
73 void
75 {
76  // Here we are going to mostly mimic the default ghosting in libmesh
77  // By default libmesh adds:
78  // 1) GhostPointNeighbors on the mesh
79  // 2) DefaultCoupling with 1 layer as an algebraic ghosting functor on the dof_map, which also
80  // gets added to the mesh at the time a new System is added
81  // 3) DefaultCoupling with 0 layers as a coupling functor on the dof_map, which also gets added to
82  // the mesh at the time a new System is added
83  //
84  // What we will do differently is:
85  // - The 3rd ghosting functor adds nothing so we will not add it at all
86 
87  if (_current_task == "add_algebraic_rm")
88  {
89  auto rm_params = _factory.getValidParams("ElementSideNeighborLayers");
90 
91  rm_params.set<std::string>("for_whom") = "Adaptivity";
92  rm_params.set<MooseMesh *>("mesh") = _mesh.get();
93  rm_params.set<Moose::RelationshipManagerType>("rm_type") =
95 
96  if (rm_params.areAllRequiredParamsValid())
97  {
98  auto rm_obj = _factory.create<RelationshipManager>(
99  "ElementSideNeighborLayers", "adaptivity_algebraic_ghosting", rm_params);
100 
101  // Delete the resources created on behalf of the RM if it ends up not being added to the
102  // App.
103  if (!_app.addRelationshipManager(rm_obj))
104  _factory.releaseSharedObjects(*rm_obj);
105  }
106  else
107  mooseError("Invalid initialization of ElementSideNeighborLayers");
108  }
109 
110  else if (_current_task == "add_geometric_rm")
111  {
112  auto rm_params = _factory.getValidParams("ElementPointNeighborLayers");
113 
114  rm_params.set<std::string>("for_whom") = "Adaptivity";
115  rm_params.set<MooseMesh *>("mesh") = _mesh.get();
116  rm_params.set<Moose::RelationshipManagerType>("rm_type") =
118 
119  if (rm_params.areAllRequiredParamsValid())
120  {
121  auto rm_obj = _factory.create<RelationshipManager>(
122  "ElementPointNeighborLayers", "adaptivity_geometric_ghosting", rm_params);
123 
124  // Delete the resources created on behalf of the RM if it ends up not being added to the
125  // App.
126  if (!_app.addRelationshipManager(rm_obj))
127  _factory.releaseSharedObjects(*rm_obj);
128  }
129  else
130  mooseError("Invalid initialization of ElementPointNeighborLayers");
131  }
132 
133  else if (_current_task == "setup_adaptivity")
134  {
135  NonlinearSystemBase & system = _problem->getNonlinearSystemBase(/*nl_sys_num=*/0);
136 
137  Adaptivity & adapt = _problem->adaptivity();
138 
139  // we don't need to run mesh modifiers *again* after they ran already during the mesh
140  // splitting process. Adaptivity::init must be called for any adaptivity to work, however, so we
141  // can't just skip it for the useSplit case.
142  if (_mesh->isSplit())
143  adapt.init(0, 0, getParam<bool>("switch_h_to_p_refinement"));
144  else
145  adapt.init(getParam<unsigned int>("steps"),
146  getParam<unsigned int>("initial_adaptivity"),
147  getParam<bool>("switch_h_to_p_refinement"));
148 
149  adapt.setErrorEstimator(getParam<MooseEnum>("error_estimator"));
150 
151  adapt.setParam("cycles_per_step", getParam<unsigned int>("cycles_per_step"));
152  adapt.setParam("refine fraction", getParam<Real>("refine_fraction"));
153  adapt.setParam("coarsen fraction", getParam<Real>("coarsen_fraction"));
154  adapt.setParam("max h-level", getParam<unsigned int>("max_h_level"));
155  adapt.setParam("recompute_markers_during_cycles",
156  getParam<bool>("recompute_markers_during_cycles"));
157 
158  adapt.setPrintMeshChanged(getParam<bool>("print_changed_info"));
159 
160  const std::vector<std::string> & weight_names =
161  getParam<std::vector<std::string>>("weight_names");
162  const std::vector<Real> & weight_values = getParam<std::vector<Real>>("weight_values");
163 
164  auto num_weight_names = weight_names.size();
165  auto num_weight_values = weight_values.size();
166 
167  if (num_weight_names)
168  {
169  if (num_weight_names != num_weight_values)
170  mooseError("Number of weight_names must be equal to number of weight_values in "
171  "Execution/Adaptivity");
172 
173  // If weights have been specified then set the default weight to zero
174  std::vector<Real> weights(system.nVariables(), 0);
175 
176  for (MooseIndex(num_weight_names) i = 0; i < num_weight_names; i++)
177  {
178  std::string name = weight_names[i];
179  auto value = weight_values[i];
180 
181  weights[system.getVariable(0, name).number()] = value;
182  }
183 
184  std::vector<FEMNormType> norms(system.nVariables(), H1_SEMINORM);
185 
186  SystemNorm sys_norm(norms, weights);
187 
188  adapt.setErrorNorm(sys_norm);
189  }
190 
191  adapt.setTimeActive(getParam<Real>("start_time"), getParam<Real>("stop_time"));
192  adapt.setInterval(getParam<unsigned int>("interval"));
193  }
194 }
195 
196 #endif // LIBMESH_ENABLE_AMR
void setPrintMeshChanged(bool state=true)
Definition: Adaptivity.h:91
void addDeprecatedParam(const std::string &name, const T &value, const std::string &doc_string, const std::string &deprecation_message)
void setInterval(unsigned int interval)
Set the interval (number of timesteps) between refinement steps.
Definition: Adaptivity.h:234
RelationshipManagerType
Main types of Relationship Managers.
Definition: MooseTypes.h:964
unsigned int number() const
Get variable number coming from libMesh.
Factory & _factory
The Factory associated with the MooseApp.
void setTimeActive(Real start_time, Real stop_time)
Sets the time when the adaptivity is active.
Definition: Adaptivity.C:338
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void init(const unsigned int steps, const unsigned int initial_steps, const bool p_refinement)
Initialize and turn on adaptivity for the simulation.
Definition: Adaptivity.C:59
AdaptivityAction(const InputParameters &params)
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
Base class for actions.
Definition: Action.h:33
virtual unsigned int nVariables() const
Get the number of variables in this system.
Definition: SystemBase.C:883
Nonlinear system to be solved.
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:99
bool addRelationshipManager(std::shared_ptr< RelationshipManager > relationship_manager)
Transfers ownership of a RelationshipManager to the application for lifetime management.
Definition: MooseApp.C:3024
virtual std::unique_ptr< Base > create()=0
H1_SEMINORM
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
static InputParameters validParams()
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:88
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:33
const std::string & _current_task
The current action (even though we have separate instances for each action)
Definition: Action.h:165
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:353
void setErrorEstimator(const MooseEnum &error_estimator_name)
Set the error estimator.
Definition: Adaptivity.C:110
std::shared_ptr< MooseMesh > & _mesh
Definition: Action.h:167
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Takes care of everything related to mesh adaptivity.
Definition: Adaptivity.h:49
RelationshipManagers are used for describing what kinds of non-local resources are needed for an obje...
registerMooseAction("MooseApp", AdaptivityAction, "setup_adaptivity")
InputParameters commonAdaptivityParams()
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition: MooseBase.h:267
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
std::shared_ptr< FEProblemBase > & _problem
Convenience reference to a problem this action works on.
Definition: Action.h:171
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...
MooseVariableFieldBase & getVariable(THREAD_ID tid, const std::string &var_name) const
Gets a reference to a variable of with specified name.
Definition: SystemBase.C:90
void setParam(const std::string &param_name, const T &param_value)
Set adaptivity parameter.
Definition: Adaptivity.h:320
virtual void act() override
Method to add objects to the simulation or perform other setup tasks.
void setErrorNorm(libMesh::SystemNorm &sys_norm)
Set the error norm (FIXME: improve description)
Definition: Adaptivity.C:124