https://mooseframework.inl.gov
DiffusionPhysicsBase.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 "DiffusionPhysicsBase.h"
11 #include "PetscSupport.h"
12 #include "MooseEnumItem.h"
13 
16 {
19  params.addClassDescription("Base class for creating a diffusion equation");
20 
21  // Variable parameters
22  params.addParam<VariableName>("variable_name", "u", "Variable name for the equation");
23  params.addParam<FunctionName>("initial_condition", "Initial condition for the diffused variable");
24 
25  // Diffusivity
26  params.addParam<MaterialPropertyName>("diffusivity_matprop",
27  "Material property defining the diffusion coefficient");
28  params.addParam<MooseFunctorName>("diffusivity_functor", "Functor specifying the diffusivity");
29 
30  // Source term
31  params.addParam<MooseFunctorName>("source_functor", "Source term in the diffusion problem");
32  params.addParam<Real>("source_coef", 1, "Coefficient multiplying the source");
33 
34  // Boundary conditions
35  params.addParam<std::vector<BoundaryName>>(
36  "neumann_boundaries", {}, "Boundaries on which to apply a diffusive flux");
37  params.addParam<std::vector<BoundaryName>>(
38  "dirichlet_boundaries", {}, "Boundaries on which to apply a fixed value");
39  params.addParam<std::vector<MooseFunctorName>>(
40  "boundary_fluxes", {}, "Functors to compute the diffusive flux on each Neumann boundary'");
41  params.addParam<std::vector<MooseFunctorName>>(
42  "boundary_values", {}, "Functors to compute the diffusive flux on each Dirichlet boundary'");
43  params.addParamNamesToGroup("neumann_boundaries dirichlet_boundaries boundary_fluxes "
44  "boundary_values",
45  "Boundary conditions");
46 
47  // Postprocessing
48  params.addParam<std::vector<BoundaryName>>(
49  "compute_diffusive_fluxes_on", {}, "Surfaces to compute the diffusive flux on");
50 
51  // Preconditioning is implemented so let's use it by default
52  MooseEnum pc_options("default defer", "default");
53  params.addParam<MooseEnum>(
54  "preconditioning", pc_options, "Which preconditioning to use for this Physics");
55 
56  return params;
57 }
58 
60  : PhysicsBase(parameters),
61  PhysicsComponentInterface(parameters),
62  _var_name(getParam<VariableName>("variable_name")),
63  _neumann_boundaries(getParam<std::vector<BoundaryName>>("neumann_boundaries")),
64  _dirichlet_boundaries(getParam<std::vector<BoundaryName>>("dirichlet_boundaries"))
65 {
66  // Keep track of variables
68 
69  // Parameter checking
70  checkVectorParamsSameLength<BoundaryName, MooseFunctorName>("neumann_boundaries",
71  "boundary_fluxes");
72  checkVectorParamsSameLength<BoundaryName, MooseFunctorName>("dirichlet_boundaries",
73  "boundary_values");
74  checkVectorParamsNoOverlap<BoundaryName>({"neumann_boundaries", "dirichlet_boundaries"});
75  if (isParamSetByUser("source_coef"))
76  checkParamsBothSetOrNotSet("source_functor", "source_coef");
77 
78  addRequiredPhysicsTask("add_preconditioning");
79  addRequiredPhysicsTask("add_postprocessor");
80 }
81 
82 void
84 {
85  // Use a multigrid method, known to work for elliptic problems such as diffusion
86  if (_preconditioning == "default")
87  {
88  // We only pass petsc options as that's all that's needed to set up the preconditioner
89  const auto option_pair1 =
90  std::make_pair<MooseEnumItem, std::string>(MooseEnumItem("-pc_type"), "hypre");
91  const auto option_pair2 =
92  std::make_pair<MooseEnumItem, std::string>(MooseEnumItem("-pc_hypre_type"), "boomeramg");
93  addPetscPairsToPetscOptions({option_pair1, option_pair2});
94  }
95 }
96 
97 void
99 {
100  for (const auto & boundary_name :
101  getParam<std::vector<BoundaryName>>("compute_diffusive_fluxes_on"))
102  {
103  // Create the boundary integration of the flux
104  const bool use_ad = isParamValid("use_automatic_differentiation")
105  ? getParam<bool>("use_automatic_differentiation")
106  : false;
107  const std::string pp_type =
108  use_ad ? "ADSideDiffusiveFluxIntegral" : "SideDiffusiveFluxIntegral";
109  auto params = _factory.getValidParams(pp_type);
110  params.set<std::vector<VariableName>>("variable") = {_var_name};
111  if (isParamValid("diffusivity_matprop"))
112  params.set<MaterialPropertyName>("diffusivity") =
113  getParam<MaterialPropertyName>("diffusivity_matprop");
114  else if (isParamValid("diffusivity_functor"))
115  params.set<MooseFunctorName>("functor_diffusivity") =
116  getParam<MooseFunctorName>("diffusivity_functor");
117  else
118  params.set<MooseFunctorName>("functor_diffusivity") = "1";
119  params.set<std::vector<BoundaryName>>("boundary") = {boundary_name};
120  // Default to maximum computation
121  params.set<ExecFlagEnum>("execute_on") = {
123  getProblem().addPostprocessor(pp_type, prefix() + "diffusive_flux_" + boundary_name, params);
124  }
125 }
126 
127 void
129 {
130  InputParameters params = getFactory().getValidParams("FunctionIC");
131 
132  // Get the list of blocks that have ics from components
133  std::vector<SubdomainName> component_ic_blocks;
134  for (const auto & [component_name, component_bc_map] : _components_initial_conditions)
135  {
136  if (!component_bc_map.count(_var_name))
137  continue;
138  const auto & comp_blocks = getActionComponent(component_name).blocks();
139  component_ic_blocks.insert(component_ic_blocks.end(), comp_blocks.begin(), comp_blocks.end());
140  }
141 
142  // Keep only blocks that have no component IC
143  std::vector<SubdomainName> remaining_blocks;
144  for (const auto & block : _blocks)
145  if (std::find(component_ic_blocks.begin(), component_ic_blocks.end(), block) ==
146  component_ic_blocks.end())
147  remaining_blocks.push_back(block);
148 
149  // No need to add BCs on the Physics block restriction if Components are covering all of it
150  if (remaining_blocks.empty())
151  return;
152  assignBlocks(params, remaining_blocks);
153 
154  // first obey any component-specific initial condition
155  // then obey the user specification of initial conditions
156  // NOTE: we may conflict with ICs in the input
157  // there are no default initial conditions
158  mooseAssert(parameters().isParamSetByUser("initial_condition") ||
159  !parameters().hasDefault("initial_condition"),
160  "Should not have a default");
161  if (isParamValid("initial_condition") &&
163  _var_name, remaining_blocks, /*ic is a default*/ false, /*error if defined*/ true))
164  {
165  params.set<VariableName>("variable") = _var_name;
166  params.set<FunctionName>("function") = getParam<FunctionName>("initial_condition");
167 
168  getProblem().addInitialCondition("FunctionIC", prefix() + _var_name + "_ic", params);
169  }
170 }
171 
172 void
174 {
175  InputParameters params = getFactory().getValidParams("FunctorIC");
176 
177  // ICs from components are considered always set by the user, so we do not skip them when
178  // restarting
179  for (const auto & [component_name, component_bc_map] : _components_initial_conditions)
180  {
181  if (!component_bc_map.count(_var_name))
182  continue;
183  assignBlocks(params, getActionComponent(component_name).blocks());
184  params.set<VariableName>("variable") = _var_name;
185  params.set<MooseFunctorName>("functor") = libmesh_map_find(component_bc_map, _var_name);
186 
188  "FunctorIC", prefix() + _var_name + "_ic_" + component_name, params);
189  }
190 }
std::string prefix() const
Use prefix() to disambiguate names.
Definition: PhysicsBase.h:135
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:21
void assignBlocks(InputParameters &params, const std::vector< SubdomainName > &blocks) const
Set the blocks parameter to the input parameters of an object this Physics will create.
Definition: PhysicsBase.C:484
static InputParameters validParams()
Factory & getFactory()
Get the factory for this physics The factory lets you get the parameters for objects.
Definition: PhysicsBase.h:108
void addRequiredPhysicsTask(const std::string &task)
Add a new required task for all physics deriving from this class NOTE: This does not register the tas...
Definition: PhysicsBase.h:158
void addPetscPairsToPetscOptions(const std::vector< std::pair< MooseEnumItem, std::string >> &petsc_pair_options)
Process the given petsc option pairs into the system solver settings.
Definition: PhysicsBase.C:575
static InputParameters validParams()
virtual void addPostprocessors() override
Add postprocessing of the fluxes.
Base class to help creating an entire physics.
Definition: PhysicsBase.h:30
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
DiffusionPhysicsBase(const InputParameters &parameters)
InputParameters getValidParams(const std::string &name) const
Get valid parameters for the object.
Definition: Factory.C:68
const std::vector< SubdomainName > & blocks() const
Return the blocks this physics is defined on.
Definition: PhysicsBase.h:60
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const MooseEnum & _preconditioning
Whether to add a default preconditioning.
Definition: PhysicsBase.h:258
const ExecFlagType EXEC_TIMESTEP_END
Definition: Moose.C:34
bool shouldCreateIC(const VariableName &var_name, const std::vector< SubdomainName > &blocks, const bool ic_is_default_ic, const bool error_if_already_defined) const
Returns whether this Physics should create the variable.
Definition: PhysicsBase.C:626
std::map< std::string, std::map< VariableName, MooseFunctorName > > _components_initial_conditions
Map of components to variables and initial conditions.
const VariableName & _var_name
Name of the diffused variable.
std::vector< SubdomainName > _blocks
Keep track of the subdomains the Physics is defined on.
Definition: PhysicsBase.h:261
void checkParamsBothSetOrNotSet(const std::string &param1, const std::string &param2) const
Check that two parameters are either both set or both not set.
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Factory & _factory
The Factory associated with the MooseApp.
virtual void addInitialCondition(const std::string &ic_name, const std::string &name, InputParameters &parameters)
virtual FEProblemBase & getProblem()
Get the problem for this physics Useful to add objects to the simulation.
Definition: PhysicsBase.h:112
virtual void addPostprocessor(const std::string &pp_name, const std::string &name, InputParameters &parameters)
virtual void addInitialConditionsFromComponents() override
Interface class to help components interact with Physics.
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
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 ExecFlagType EXEC_LINEAR
Definition: Moose.C:29
static InputParameters validParams()
Definition: PhysicsBase.C:24
const ExecFlagType EXEC_NONLINEAR
Definition: Moose.C:31
virtual void addInitialConditions() override
bool isParamSetByUser(const std::string &nm) const
Test if the supplied parameter is set by a user, as opposed to not set or set to default.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:18
const std::vector< SubdomainName > & blocks() const
Returns the subdomains for the component mesh, if any.
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...
const InputParameters & parameters() const
Get the parameters of the object.
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...
const ActionComponent & getActionComponent(const ComponentName &comp_name) const
Get a component with the requested name.
Definition: PhysicsBase.C:318
virtual void addPreconditioning() override
void saveSolverVariableName(const VariableName &var_name)
Keep track of the name of the solver variable defined in the Physics.
Definition: PhysicsBase.h:138
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...
const ExecFlagType EXEC_INITIAL
Definition: Moose.C:28