www.mooseframework.org
AdaptivityAction.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 #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"
21 
22 // libMesh includes
23 #include "libmesh/transient_system.h"
24 #include "libmesh/system_norm.h"
25 #include "libmesh/enum_norm_type.h"
26 
27 registerMooseAction("MooseApp", AdaptivityAction, "setup_adaptivity");
28 registerMooseAction("MooseApp", AdaptivityAction, "add_geometric_rm");
29 registerMooseAction("MooseApp", AdaptivityAction, "add_algebraic_rm");
30 
31 template <>
34 {
36  MooseEnum estimators("KellyErrorEstimator LaplacianErrorEstimator PatchRecoveryErrorEstimator",
37  "KellyErrorEstimator");
38 
39  params.addParam<unsigned int>(
40  "steps", 0, "The number of adaptivity steps to perform at any one time for steady state");
41  params.addRangeCheckedParam<unsigned int>(
42  "interval", 1, "interval>0", "The number of time steps betweeen each adaptivity phase");
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<unsigned int>(
54  "max_h_level",
55  0,
56  "Maximum number of times a single element can be refined. If 0 then infinite.");
57  params.addParam<MooseEnum>(
58  "error_estimator", estimators, "The class name of the error estimator you want to use.");
59  params.addDeprecatedParam<bool>(
60  "print_changed_info",
61  false,
62  "Determines whether information about the mesh is printed when adaptivity occurs",
63  "Use the Console output parameter 'print_mesh_changed_info'");
64  params.addParam<Real>("start_time",
65  -std::numeric_limits<Real>::max(),
66  "The time that adaptivity will be active after.");
67  params.addParam<Real>("stop_time",
68  std::numeric_limits<Real>::max(),
69  "The time after which adaptivity will no longer be active.");
70  params.addParam<std::vector<std::string>>(
71  "weight_names", "List of names of variables that will be associated with weight_values");
72  params.addParam<std::vector<Real>>(
73  "weight_values",
74  "List of values between 0 and 1 to weight the associated weight_names error by");
75  params.addParam<unsigned int>("cycles_per_step", 1, "The number of adaptivity cycles per step");
76 
77  params.addParam<bool>(
78  "show_initial_progress", true, "Show the progress of the initial adaptivity");
79  params.addParam<bool>(
80  "recompute_markers_during_cycles", false, "Recompute markers during adaptivity cycles");
81  return params;
82 }
83 
85 
86 void
88 {
89  // Here we are going to mostly mimic the default ghosting in libmesh
90  // By default libmesh adds:
91  // 1) GhostPointNeighbors on the mesh
92  // 2) DefaultCoupling with 1 layer as an algebraic ghosting functor on the dof_map, which also
93  // gets added to the mesh at the time a new System is added
94  // 3) DefaultCoupling with 0 layers as a coupling functor on the dof_map, which also gets added to
95  // the mesh at the time a new System is added
96  //
97  // What we will do differently is:
98  // - The 3rd ghosting functor adds nothing so we will not add it at all
99 
100  if (_current_task == "add_algebraic_rm")
101  {
102  auto rm_params = _factory.getValidParams("ElementSideNeighborLayers");
103 
104  rm_params.set<std::string>("for_whom") = "Adaptivity";
105  rm_params.set<MooseMesh *>("mesh") = _mesh.get();
106  rm_params.set<Moose::RelationshipManagerType>("rm_type") =
108 
109  if (rm_params.areAllRequiredParamsValid())
110  {
111  auto rm_obj = _factory.create<RelationshipManager>(
112  "ElementSideNeighborLayers", "adaptivity_algebraic_ghosting", rm_params);
113 
114  // Delete the resources created on behalf of the RM if it ends up not being added to the
115  // App.
116  if (!_app.addRelationshipManager(rm_obj))
118  }
119  else
120  mooseError("Invalid initialization of ElementSideNeighborLayers");
121  }
122 
123  else if (_current_task == "add_geometric_rm")
124  {
125  auto rm_params = _factory.getValidParams("MooseGhostPointNeighbors");
126 
127  rm_params.set<std::string>("for_whom") = "Adaptivity";
128  rm_params.set<MooseMesh *>("mesh") = _mesh.get();
129  rm_params.set<Moose::RelationshipManagerType>("rm_type") =
131 
132  if (rm_params.areAllRequiredParamsValid())
133  {
134  auto rm_obj = _factory.create<RelationshipManager>(
135  "MooseGhostPointNeighbors", "adaptivity_geometric_ghosting", rm_params);
136 
137  // Delete the resources created on behalf of the RM if it ends up not being added to the
138  // App.
139  if (!_app.addRelationshipManager(rm_obj))
141  }
142  else
143  mooseError("Invalid initialization of MooseGhostPointNeighbors");
144  }
145 
146  else if (_current_task == "setup_adaptivity")
147  {
148  NonlinearSystemBase & system = _problem->getNonlinearSystemBase();
149 
150  Adaptivity & adapt = _problem->adaptivity();
151 
152  // we don't need to run mesh modifiers *again* after they ran already during the mesh
153  // splitting process. Adaptivity::init must be called for any adaptivity to work, however, so we
154  // can't just skip it for the useSplit case.
155  if (_app.isUseSplit())
156  adapt.init(0, 0);
157  else
158  adapt.init(getParam<unsigned int>("steps"), getParam<unsigned int>("initial_adaptivity"));
159 
160  adapt.setErrorEstimator(getParam<MooseEnum>("error_estimator"));
161 
162  adapt.setParam("cycles_per_step", getParam<unsigned int>("cycles_per_step"));
163  adapt.setParam("refine fraction", getParam<Real>("refine_fraction"));
164  adapt.setParam("coarsen fraction", getParam<Real>("coarsen_fraction"));
165  adapt.setParam("max h-level", getParam<unsigned int>("max_h_level"));
166  adapt.setParam("recompute_markers_during_cycles",
167  getParam<bool>("recompute_markers_during_cycles"));
168 
169  adapt.setPrintMeshChanged(getParam<bool>("print_changed_info"));
170 
171  const std::vector<std::string> & weight_names =
172  getParam<std::vector<std::string>>("weight_names");
173  const std::vector<Real> & weight_values = getParam<std::vector<Real>>("weight_values");
174 
175  auto num_weight_names = weight_names.size();
176  auto num_weight_values = weight_values.size();
177 
178  if (num_weight_names)
179  {
180  if (num_weight_names != num_weight_values)
181  mooseError("Number of weight_names must be equal to number of weight_values in "
182  "Execution/Adaptivity");
183 
184  // If weights have been specified then set the default weight to zero
185  std::vector<Real> weights(system.nVariables(), 0);
186 
187  for (MooseIndex(num_weight_names) i = 0; i < num_weight_names; i++)
188  {
189  std::string name = weight_names[i];
190  auto value = weight_values[i];
191 
192  weights[system.getVariable(0, name).number()] = value;
193  }
194 
195  std::vector<FEMNormType> norms(system.nVariables(), H1_SEMINORM);
196 
197  SystemNorm sys_norm(norms, weights);
198 
199  adapt.setErrorNorm(sys_norm);
200  }
201 
202  adapt.setTimeActive(getParam<Real>("start_time"), getParam<Real>("stop_time"));
203  adapt.setInterval(getParam<unsigned int>("interval"));
204  }
205 }
206 
207 #endif // LIBMESH_ENABLE_AMR
void setPrintMeshChanged(bool state=true)
Definition: Adaptivity.h:85
std::shared_ptr< MooseObject > create(const std::string &obj_name, const std::string &name, InputParameters parameters, THREAD_ID tid=0, bool print_deprecated=true)
Build an object (must be registered) - THIS METHOD IS DEPRECATED (Use create<T>()) ...
Definition: Factory.C:87
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:221
RelationshipManagerType
Main types of Relationship Managers.
Definition: MooseTypes.h:716
const std::string & name() const
The name of the action.
Definition: Action.h:76
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: Factory.C:67
void init(unsigned int steps, unsigned int initial_steps)
Initialize and turn on adaptivity for the simulation.
Definition: Adaptivity.C:59
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
void setTimeActive(Real start_time, Real stop_time)
Sets the time when the adaptivity is active.
Definition: Adaptivity.C:254
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
Factory & _factory
The Factory associated with the MooseApp.
Definition: Action.h:186
Base class for actions.
Definition: Action.h:35
Nonlinear system to be solved.
bool addRelationshipManager(std::shared_ptr< RelationshipManager > relationship_manager)
Transfers ownership of a RelationshipManager to the application for lifetime management.
Definition: MooseApp.C:1692
nl system()
bool isUseSplit() const
Whether or not we are running with pre-split (distributed mesh)
Definition: MooseApp.C:877
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:74
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
const std::string & _current_task
The current action (even though we have seperate instances for each action)
Definition: Action.h:210
void setErrorEstimator(const MooseEnum &error_estimator_name)
Set the error estimator.
Definition: Adaptivity.C:104
AdaptivityAction(InputParameters params)
InputParameters validParams< AdaptivityAction >()
void releaseSharedObjects(const MooseObject &moose_object, THREAD_ID tid=0)
Releases any shared resources created as a side effect of creating an object through the Factory::cre...
Definition: Factory.C:166
std::shared_ptr< MooseMesh > & _mesh
Definition: Action.h:212
Takes care of everything related to mesh adaptivity.
Definition: Adaptivity.h:46
RelationshipManagers are used for describing what kinds of non-local resources are needed for an obje...
registerMooseAction("MooseApp", AdaptivityAction, "setup_adaptivity")
std::shared_ptr< FEProblemBase > & _problem
Convenience reference to a problem this action works on.
Definition: Action.h:216
void setErrorNorm(SystemNorm &sys_norm)
Set the error norm (FIXME: improve description)
Definition: Adaptivity.C:118
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...
void addRangeCheckedParam(const std::string &name, const T &value, const std::string &parsed_function, const std::string &doc_string)
MooseApp & _app
The MOOSE application this is associated with.
Definition: Action.h:183
void setParam(const std::string &param_name, const T &param_value)
Set adaptivity parameter.
Definition: Adaptivity.h:311
virtual void act() override
Method to add objects to the simulation or perform other setup tasks.
InputParameters validParams< Action >()
Definition: Action.C:22