https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
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<std::vector<MaterialPropertyName>>(
27 "diffusivity_matprop",
28 "Material property(ies) defining the diffusion coefficient (on each group of blocks)");
29 params.addParam<std::vector<MooseFunctorName>>(
30 "diffusivity_functor", "Functor(s) specifying the diffusivity (on each group of blocks)");
31 params.addParam<std::vector<std::vector<SubdomainName>>>(
32 "diffusivity_blocks", "Subdomains on which each diffusivity is defined");
33
34 // Source term
35 params.addParam<MooseFunctorName>("source_functor", "Source term in the diffusion problem");
36 params.addParam<Real>("source_coef", 1, "Coefficient multiplying the source");
37
38 // Boundary conditions
39 params.addParam<std::vector<BoundaryName>>(
40 "neumann_boundaries", {}, "Boundaries on which to apply a diffusive flux");
41 params.addParam<std::vector<BoundaryName>>(
42 "dirichlet_boundaries", {}, "Boundaries on which to apply a fixed value");
43 params.addParam<std::vector<MooseFunctorName>>(
44 "boundary_fluxes", {}, "Functors to compute the diffusive flux on each Neumann boundary'");
45 params.addParam<std::vector<MooseFunctorName>>(
46 "boundary_values", {}, "Functors to compute the diffusive flux on each Dirichlet boundary'");
47 params.addParamNamesToGroup("neumann_boundaries dirichlet_boundaries boundary_fluxes "
48 "boundary_values",
49 "Boundary conditions");
50
51 // Postprocessing
52 params.addParam<std::vector<BoundaryName>>(
53 "compute_diffusive_fluxes_on", {}, "Surfaces to compute the diffusive flux on");
54
55 // Preconditioning is implemented so let's use it by default
56 MooseEnum pc_options("default defer", "default");
57 params.addParam<MooseEnum>(
58 "preconditioning", pc_options, "Which preconditioning to use for this Physics");
59
60 return params;
61}
62
64 : PhysicsBase(parameters),
65 PhysicsComponentInterface(parameters),
66 _var_name(getParam<VariableName>("variable_name")),
67 _neumann_boundaries(getParam<std::vector<BoundaryName>>("neumann_boundaries")),
68 _dirichlet_boundaries(getParam<std::vector<BoundaryName>>("dirichlet_boundaries"))
69{
70 // Keep track of variables
72
73 // Parameter checking
74 checkVectorParamsSameLength<BoundaryName, MooseFunctorName>("neumann_boundaries",
75 "boundary_fluxes");
76 checkVectorParamsSameLength<BoundaryName, MooseFunctorName>("dirichlet_boundaries",
77 "boundary_values");
78 checkVectorParamsNoOverlap<BoundaryName>({"neumann_boundaries", "dirichlet_boundaries"});
79 if (isParamSetByUser("source_coef"))
80 checkParamsBothSetOrNotSet("source_functor", "source_coef");
81 if (isParamValid("diffusivity_matprop") &&
82 getParam<std::vector<MaterialPropertyName>>("diffusivity_matprop").size() > 1)
83 checkVectorParamsSameLength<MaterialPropertyName, std::vector<SubdomainName>>(
84 "diffusivity_matprop", "diffusivity_blocks");
85 else if (isParamValid("diffusivity_functor") &&
86 getParam<std::vector<MooseFunctorName>>("diffusivity_functor").size() > 1)
87 checkVectorParamsSameLength<MooseFunctorName, std::vector<SubdomainName>>("diffusivity_functor",
88 "diffusivity_blocks");
89
90 addRequiredPhysicsTask("add_preconditioning");
91 addRequiredPhysicsTask("add_postprocessor");
92 addRequiredPhysicsTask("add_ics_physics");
93}
94
95void
97{
98 // Use a multigrid method, known to work for elliptic problems such as diffusion
99 if (_preconditioning == "default")
100 {
101 // We only pass petsc options as that's all that's needed to set up the preconditioner
102 const auto option_pair1 =
103 std::make_pair<MooseEnumItem, std::string>(MooseEnumItem("-pc_type"), "hypre");
104 const auto option_pair2 =
105 std::make_pair<MooseEnumItem, std::string>(MooseEnumItem("-pc_hypre_type"), "boomeramg");
106 addPetscPairsToPetscOptions({option_pair1, option_pair2});
107 }
108}
109
110void
112{
113 for (const auto & boundary_name :
114 getParam<std::vector<BoundaryName>>("compute_diffusive_fluxes_on"))
115 {
116 // Create the boundary integration of the flux
117 const bool use_ad = isParamValid("use_automatic_differentiation")
118 ? getParam<bool>("use_automatic_differentiation")
119 : false;
120 const std::string pp_type =
121 use_ad ? "ADSideDiffusiveFluxIntegral" : "SideDiffusiveFluxIntegral";
122 auto params = _factory.getValidParams(pp_type);
123 params.set<std::vector<VariableName>>("variable") = {_var_name};
124 if (isParamValid("diffusivity_matprop"))
125 {
126 const auto & diffusivities =
127 getParam<std::vector<MaterialPropertyName>>("diffusivity_matprop");
128 if (diffusivities.size() != 1)
129 paramError("diffusivity_matprop",
130 "Computing diffusive fluxes is not implemented for multiple diffusivities");
131 params.set<MaterialPropertyName>("diffusivity") = diffusivities[0];
132 }
133 else if (isParamValid("diffusivity_functor"))
134 {
135 const auto & diffusivities = getParam<std::vector<MooseFunctorName>>("diffusivity_functor");
136 if (diffusivities.size() != 1)
137 paramError("diffusivity_functor",
138 "Computing diffusive fluxes is not implemented for multiple diffusivities");
139 params.set<MooseFunctorName>("functor_diffusivity") = diffusivities[0];
140 }
141 else
142 params.set<MooseFunctorName>("functor_diffusivity") = "1";
143 params.set<std::vector<BoundaryName>>("boundary") = {boundary_name};
144 // Default to maximum computation
145 params.set<ExecFlagEnum>("execute_on") = {
147 getProblem().addPostprocessor(pp_type, prefix() + "diffusive_flux_" + boundary_name, params);
148 }
149}
150
151void
153{
154 InputParameters params = getFactory().getValidParams("FunctionIC");
155
156 // Get the list of blocks that have ics from components
157 std::vector<SubdomainName> component_ic_blocks;
158 for (const auto & [component_name, component_bc_map] : _components_initial_conditions)
159 {
160 if (!component_bc_map.count(_var_name))
161 continue;
162 const auto & comp_blocks = getActionComponent(component_name).blocks();
163 component_ic_blocks.insert(component_ic_blocks.end(), comp_blocks.begin(), comp_blocks.end());
164 }
165
166 // Keep only blocks that have no component IC
167 std::vector<SubdomainName> remaining_blocks;
168 for (const auto & block : _blocks)
169 if (std::find(component_ic_blocks.begin(), component_ic_blocks.end(), block) ==
170 component_ic_blocks.end())
171 remaining_blocks.push_back(block);
172
173 // No need to add BCs on the Physics block restriction if Components are covering all of it
174 if (remaining_blocks.empty())
175 return;
176 assignBlocks(params, remaining_blocks);
177
178 // first obey any component-specific initial condition
179 // then obey the user specification of initial conditions
180 // NOTE: we may conflict with ICs in the input
181 // there are no default initial conditions
182 mooseAssert(parameters().isParamSetByUser("initial_condition") ||
183 !parameters().hasDefault("initial_condition"),
184 "Should not have a default");
185 if (isParamValid("initial_condition") &&
187 _var_name, remaining_blocks, /*ic is a default*/ false, /*error if defined*/ true))
188 {
189 params.set<VariableName>("variable") = _var_name;
190 params.set<FunctionName>("function") = getParam<FunctionName>("initial_condition");
191
192 getProblem().addInitialCondition("FunctionIC", prefix() + _var_name + "_ic", params);
193 }
194}
195
196void
198{
199 InputParameters params = getFactory().getValidParams("FunctorIC");
200
201 // ICs from components are considered always set by the user, so we do not skip them when
202 // restarting
203 for (const auto & [component_name, component_bc_map] : _components_initial_conditions)
204 {
205 if (!component_bc_map.count(_var_name))
206 continue;
207 assignBlocks(params, getActionComponent(component_name).blocks());
208 params.set<VariableName>("variable") = _var_name;
209 params.set<MooseFunctorName>("functor") = libmesh_map_find(component_bc_map, _var_name);
210
212 "FunctorIC", prefix() + _var_name + "_ic_" + component_name, params);
213 }
214}
const ExecFlagType EXEC_TIMESTEP_END
Definition Moose.C:37
const ExecFlagType EXEC_INITIAL
Definition Moose.C:31
const ExecFlagType EXEC_LINEAR
Definition Moose.C:32
const ExecFlagType EXEC_NONLINEAR
Definition Moose.C:34
const std::vector< SubdomainName > & blocks() const
Returns the subdomains for the component mesh, if any.
virtual void addPostprocessors() override
Add postprocessing of the fluxes.
virtual void addInitialConditions() override
virtual void addInitialConditionsFromComponents() override
const VariableName & _var_name
Name of the diffused variable.
static InputParameters validParams()
DiffusionPhysicsBase(const InputParameters &parameters)
virtual void addPreconditioning() override
A MultiMooseEnum object to hold "execute_on" flags.
virtual void addInitialCondition(const std::string &ic_name, const std::string &name, InputParameters &parameters)
virtual void addPostprocessor(const std::string &pp_name, const std::string &name, InputParameters &parameters)
InputParameters getValidParams(const std::string &name) const
Get valid parameters for the object.
Definition Factory.C:68
void checkVectorParamsSameLength(const std::string &param1, const std::string &param2) const
Check that the two vector parameters are of the same length.
void checkParamsBothSetOrNotSet(const std::string &param1, const std::string &param2) const
Check that two parameters are either both set or both not set.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
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...
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 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.
const InputParameters & parameters() const
Get the parameters of the object.
Definition MooseBase.h:131
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition MooseBase.h:457
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:205
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition MooseBase.h:406
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition MooseBase.h:199
Class for containing MooseEnum item information.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
Factory & _factory
The Factory associated with the MooseApp.
Base class to help creating an entire physics.
Definition PhysicsBase.h:31
void addPetscPairsToPetscOptions(const std::vector< std::pair< MooseEnumItem, std::string > > &petsc_pair_options)
Process the given petsc option pairs into the system solver settings.
static InputParameters validParams()
Definition PhysicsBase.C:24
virtual FEProblemBase & getProblem()
Get the problem for this physics Useful to add objects to the simulation.
Factory & getFactory()
Get the factory for this physics The factory lets you get the parameters for objects.
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.
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.
void saveSolverVariableName(const VariableName &var_name)
Keep track of the name of the solver variable defined in the Physics.
std::string prefix() const
Use prefix() to disambiguate names.
const ActionComponent & getActionComponent(const ComponentName &comp_name) const
Get a component with the requested name.
std::vector< SubdomainName > _blocks
Keep track of the subdomains the Physics is defined on.
const std::vector< SubdomainName > & blocks() const
Return the blocks this physics is defined on.
Definition PhysicsBase.h:60
const MooseEnum & _preconditioning
Whether to add a default preconditioning.
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...
Interface class to help components interact with Physics.
std::map< std::string, std::map< VariableName, MooseFunctorName > > _components_initial_conditions
Map of components to variables and initial conditions.
static InputParameters validParams()