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