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  addRequiredPhysicsTask("add_ics_physics");
81 }
82 
83 void
85 {
86  // Use a multigrid method, known to work for elliptic problems such as diffusion
87  if (_preconditioning == "default")
88  {
89  // We only pass petsc options as that's all that's needed to set up the preconditioner
90  const auto option_pair1 =
91  std::make_pair<MooseEnumItem, std::string>(MooseEnumItem("-pc_type"), "hypre");
92  const auto option_pair2 =
93  std::make_pair<MooseEnumItem, std::string>(MooseEnumItem("-pc_hypre_type"), "boomeramg");
94  addPetscPairsToPetscOptions({option_pair1, option_pair2});
95  }
96 }
97 
98 void
100 {
101  for (const auto & boundary_name :
102  getParam<std::vector<BoundaryName>>("compute_diffusive_fluxes_on"))
103  {
104  // Create the boundary integration of the flux
105  const bool use_ad = isParamValid("use_automatic_differentiation")
106  ? getParam<bool>("use_automatic_differentiation")
107  : false;
108  const std::string pp_type =
109  use_ad ? "ADSideDiffusiveFluxIntegral" : "SideDiffusiveFluxIntegral";
110  auto params = _factory.getValidParams(pp_type);
111  params.set<std::vector<VariableName>>("variable") = {_var_name};
112  if (isParamValid("diffusivity_matprop"))
113  params.set<MaterialPropertyName>("diffusivity") =
114  getParam<MaterialPropertyName>("diffusivity_matprop");
115  else if (isParamValid("diffusivity_functor"))
116  params.set<MooseFunctorName>("functor_diffusivity") =
117  getParam<MooseFunctorName>("diffusivity_functor");
118  else
119  params.set<MooseFunctorName>("functor_diffusivity") = "1";
120  params.set<std::vector<BoundaryName>>("boundary") = {boundary_name};
121  // Default to maximum computation
122  params.set<ExecFlagEnum>("execute_on") = {
124  getProblem().addPostprocessor(pp_type, prefix() + "diffusive_flux_" + boundary_name, params);
125  }
126 }
127 
128 void
130 {
131  InputParameters params = getFactory().getValidParams("FunctionIC");
132 
133  // Get the list of blocks that have ics from components
134  std::vector<SubdomainName> component_ic_blocks;
135  for (const auto & [component_name, component_bc_map] : _components_initial_conditions)
136  {
137  if (!component_bc_map.count(_var_name))
138  continue;
139  const auto & comp_blocks = getActionComponent(component_name).blocks();
140  component_ic_blocks.insert(component_ic_blocks.end(), comp_blocks.begin(), comp_blocks.end());
141  }
142 
143  // Keep only blocks that have no component IC
144  std::vector<SubdomainName> remaining_blocks;
145  for (const auto & block : _blocks)
146  if (std::find(component_ic_blocks.begin(), component_ic_blocks.end(), block) ==
147  component_ic_blocks.end())
148  remaining_blocks.push_back(block);
149 
150  // No need to add BCs on the Physics block restriction if Components are covering all of it
151  if (remaining_blocks.empty())
152  return;
153  assignBlocks(params, remaining_blocks);
154 
155  // first obey any component-specific initial condition
156  // then obey the user specification of initial conditions
157  // NOTE: we may conflict with ICs in the input
158  // there are no default initial conditions
159  mooseAssert(parameters().isParamSetByUser("initial_condition") ||
160  !parameters().hasDefault("initial_condition"),
161  "Should not have a default");
162  if (isParamValid("initial_condition") &&
164  _var_name, remaining_blocks, /*ic is a default*/ false, /*error if defined*/ true))
165  {
166  params.set<VariableName>("variable") = _var_name;
167  params.set<FunctionName>("function") = getParam<FunctionName>("initial_condition");
168 
169  getProblem().addInitialCondition("FunctionIC", prefix() + _var_name + "_ic", params);
170  }
171 }
172 
173 void
175 {
176  InputParameters params = getFactory().getValidParams("FunctorIC");
177 
178  // ICs from components are considered always set by the user, so we do not skip them when
179  // restarting
180  for (const auto & [component_name, component_bc_map] : _components_initial_conditions)
181  {
182  if (!component_bc_map.count(_var_name))
183  continue;
184  assignBlocks(params, getActionComponent(component_name).blocks());
185  params.set<VariableName>("variable") = _var_name;
186  params.set<MooseFunctorName>("functor") = libmesh_map_find(component_bc_map, _var_name);
187 
189  "FunctorIC", prefix() + _var_name + "_ic_" + component_name, params);
190  }
191 }
std::string prefix() const
Use prefix() to disambiguate names.
Definition: PhysicsBase.h:141
KOKKOS_INLINE_FUNCTION const T * find(const T &target, const T *const begin, const T *const end)
Find a value in an array.
Definition: KokkosUtils.h:40
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:491
static InputParameters validParams()
Factory & getFactory()
Get the factory for this physics The factory lets you get the parameters for objects.
Definition: PhysicsBase.h:114
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseBase.h:416
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:164
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:592
static InputParameters validParams()
virtual void addPostprocessors() override
Add postprocessing of the fluxes.
Factory & _factory
The Factory associated with the MooseApp.
Base class to help creating an entire physics.
Definition: PhysicsBase.h:30
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
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:270
const ExecFlagType EXEC_TIMESTEP_END
Definition: Moose.C:36
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:643
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:273
void checkParamsBothSetOrNotSet(const std::string &param1, const std::string &param2) const
Check that two parameters are either both set or both not set.
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:118
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.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:54
const ExecFlagType EXEC_LINEAR
Definition: Moose.C:31
static InputParameters validParams()
Definition: PhysicsBase.C:24
const ExecFlagType EXEC_NONLINEAR
Definition: Moose.C:33
virtual void addInitialConditions() override
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...
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...
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseBase.h:209
const ActionComponent & getActionComponent(const ComponentName &comp_name) const
Get a component with the requested name.
Definition: PhysicsBase.C:325
virtual void addPreconditioning() override
bool isParamSetByUser(const std::string &name) const
Test if the supplied parameter is set by a user, as opposed to not set or set to default.
Definition: MooseBase.h:215
void saveSolverVariableName(const VariableName &var_name)
Keep track of the name of the solver variable defined in the Physics.
Definition: PhysicsBase.h:144
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:30