https://mooseframework.inl.gov
PhysicsBase.h
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 #pragma once
11 
12 #include "Action.h"
13 #include "ActionWarehouse.h"
15 #include "ActionComponent.h"
16 
17 // We include these headers for all the derived classes that will be building objects
18 #include "FEProblemBase.h"
19 #include "Factory.h"
20 #include "MultiMooseEnum.h"
21 
22 #define registerPhysicsBaseTasks(app_name, derived_name) \
23  registerMooseAction(app_name, derived_name, "init_physics"); \
24  registerMooseAction(app_name, derived_name, "copy_vars_physics"); \
25  registerMooseAction(app_name, derived_name, "check_integrity_early_physics")
26 
30 class PhysicsBase : public Action, public InputParametersChecksUtils<PhysicsBase>
31 {
32 public:
34 
36 
39 
40  // Responding to tasks //
46  virtual void act() override final;
47 
49  virtual void actOnAdditionalTasks() {}
50 
51  // Block restriction //
56  void addBlocks(const std::vector<SubdomainName> & blocks);
57  void addBlocksById(const std::vector<SubdomainID> & block_ids);
58 
60  const std::vector<SubdomainName> & blocks() const { return _blocks; }
61 
68  bool checkBlockRestrictionIdentical(const std::string & object_name,
69  const std::vector<SubdomainName> & blocks,
70  const bool error_if_not_identical = true) const;
71 
76  bool hasBlocks(const std::vector<SubdomainName> & blocks) const;
77 
78  // Coupling with Physics //
84  template <typename T>
85  const T * getCoupledPhysics(const PhysicsName & phys_name, const bool allow_fail = false) const;
87  template <typename T>
88  const std::vector<T *> getCoupledPhysics(const bool allow_fail = false) const;
89 
91  unsigned int dimension() const;
92 
93  // Coupling with Components //
95  const ActionComponent & getActionComponent(const ComponentName & comp_name) const;
97  template <typename T>
98  void checkComponentType(const ActionComponent & component) const;
101  virtual void addComponent(const ActionComponent & component);
102 
104  const std::vector<VariableName> & solverVariableNames() const { return _solver_var_names; };
106  const std::vector<VariableName> & auxVariableNames() const { return _aux_var_names; };
107 
108 protected:
110  bool isTransient() const;
111 
114  Factory & getFactory() { return _factory; }
115  Factory & getFactory() const { return _factory; }
119  {
120  mooseAssert(_problem, "Requesting the problem too early");
121  return *_problem;
122  }
123  virtual const FEProblemBase & getProblem() const
124  {
125  mooseAssert(_problem, "Requesting the problem too early");
126  return *_problem;
127  }
128 
130  void prepareCopyVariablesFromMesh() const;
137  void copyVariablesFromMesh(const std::vector<VariableName> & variables_to_copy,
138  bool are_nonlinear = true);
139 
141  std::string prefix() const { return name() + "_"; }
142 
144  void saveSolverVariableName(const VariableName & var_name)
145  {
146  _solver_var_names.push_back(var_name);
147  }
149  void saveAuxVariableName(const VariableName & var_name) { _aux_var_names.push_back(var_name); }
150 
152  bool variableExists(const VariableName & var_name, bool error_if_aux) const;
154  bool solverVariableExists(const VariableName & var_name) const;
155 
158  const SolverSystemName & getSolverSystem(unsigned int variable_index) const;
160  const SolverSystemName & getSolverSystem(const VariableName & variable_name) const;
161 
164  void addRequiredPhysicsTask(const std::string & task) { _required_tasks.insert(task); }
165 
171  void assignBlocks(InputParameters & params, const std::vector<SubdomainName> & blocks) const;
176  bool allMeshBlocks(const std::vector<SubdomainName> & blocks) const;
177  bool allMeshBlocks(const std::set<SubdomainName> & blocks) const;
178  // These APIs can deal with ANY_BLOCK_ID or ids with no names. They will be slower than the
179  // MooseMeshUtils' APIs, but are more convenient for setup purposes
181  std::set<SubdomainID> getSubdomainIDs(const std::set<SubdomainName> & blocks) const;
183  std::vector<std::string> getSubdomainNamesAndIDs(const std::set<SubdomainID> & blocks) const;
184 
189  const std::vector<std::pair<MooseEnumItem, std::string>> & petsc_pair_options);
190 
191  // Helpers to check on variable types
193  bool isVariableFV(const VariableName & var_name) const;
195  bool isVariableScalar(const VariableName & var_name) const;
196 
197  // Routines to help with deciding when to create objects
205  bool shouldCreateVariable(const VariableName & var_name,
206  const std::vector<SubdomainName> & blocks,
207  const bool error_if_aux);
208 
222  bool shouldCreateIC(const VariableName & var_name,
223  const std::vector<SubdomainName> & blocks,
224  const bool ic_is_default_ic,
225  const bool error_if_already_defined) const;
226 
237  bool shouldCreateTimeDerivative(const VariableName & var_name,
238  const std::vector<SubdomainName> & blocks,
239  const bool error_if_already_defined) const;
240 
241  // Other conceivable "shouldCreate" routines for things that are unique to a variable
242  // - shouldCreateTimeIntegrator
243  // - shouldCreatePredictor/Corrector
244 
252  void reportPotentiallyMissedParameters(const std::vector<std::string> & param_names,
253  const std::string & object_type,
254  const std::string & object_name = "") const;
255 
257  virtual void checkIntegrity() const {}
258 
260  std::vector<SolverSystemName> _system_names;
261 
263  std::vector<unsigned int> _system_numbers;
264 
266  const bool _verbose;
267 
271 
273  std::vector<SubdomainName> _blocks;
274 
275 private:
279  virtual void addRelationshipManagers(Moose::RelationshipManagerType input_rm_type) override;
280 
282  void initializePhysics();
284  virtual void initializePhysicsAdditional() {}
286  virtual void checkIntegrityEarly() const;
287 
292  virtual void addSolverVariables() {}
293  virtual void addAuxiliaryVariables() {}
294  virtual void addInitialConditions() {}
295  virtual void addFEKernels() {}
296  virtual void addFVKernels() {}
297  virtual void addNodalKernels() {}
298  virtual void addDiracKernels() {}
299  virtual void addDGKernels() {}
300  virtual void addScalarKernels() {}
301  virtual void addInterfaceKernels() {}
302  virtual void addFVInterfaceKernels() {}
303  virtual void addFEBCs() {}
304  virtual void addFVBCs() {}
305  virtual void addNodalBCs() {}
306  virtual void addPeriodicBCs() {}
307  virtual void addFunctions() {}
308  virtual void addAuxiliaryKernels() {}
309  virtual void addMaterials() {}
310  virtual void addFunctorMaterials() {}
311  virtual void addUserObjects() {}
312  virtual void addCorrectors() {}
313  virtual void addMultiApps() {}
314  virtual void addTransfers() {}
315  virtual void addPostprocessors() {}
316  virtual void addVectorPostprocessors() {}
317  virtual void addReporters() {}
318  virtual void addOutputs() {}
319  virtual void addPreconditioning() {}
320  virtual void addExecutioner() {}
321  virtual void addExecutors() {}
322 
324  void checkRequiredTasks() const;
325 
329 
331  std::vector<VariableName> _solver_var_names;
333  std::vector<VariableName> _aux_var_names;
334 
337  unsigned int _dim = libMesh::invalid_uint;
338 
340  std::set<std::string> _required_tasks;
341 };
342 
343 template <typename T>
344 const T *
345 PhysicsBase::getCoupledPhysics(const PhysicsName & phys_name, const bool allow_fail) const
346 {
347  constexpr bool is_physics = std::is_base_of<PhysicsBase, T>::value;
348  libmesh_ignore(is_physics);
349  mooseAssert(is_physics, "Must be a PhysicsBase to be retrieved by getCoupledPhysics");
350  const auto all_T_physics = _awh.getActions<T>();
351  for (const auto * const physics : all_T_physics)
352  {
353  if (physics->name() == phys_name)
354  return physics;
355  }
356  if (!allow_fail)
357  mooseError("Requested Physics '",
358  phys_name,
359  "' does not exist or is not of type '",
360  MooseUtils::prettyCppType<T>(),
361  "'");
362  else
363  return nullptr;
364 }
365 
366 template <typename T>
367 const std::vector<T *>
368 PhysicsBase::getCoupledPhysics(const bool allow_fail) const
369 {
370  constexpr bool is_physics = std::is_base_of<PhysicsBase, T>::value;
371  libmesh_ignore(is_physics);
372  mooseAssert(is_physics, "Must be a PhysicsBase to be retrieved by getCoupledPhysics");
373  const auto all_T_physics = _awh.getActions<T>();
374  if (!allow_fail && all_T_physics.empty())
375  mooseError("No Physics of requested type '", MooseUtils::prettyCppType<T>(), "'");
376  else
377  return all_T_physics;
378 }
379 
380 template <typename T>
381 void
383 {
384  if (!dynamic_cast<const T *>(&component))
385  mooseError("Component '" + component.name() + "' must be of type '" +
386  MooseUtils::prettyCppType<T>() + "'.\nIt is currently of type '" + component.type() +
387  "'");
388 }
std::string prefix() const
Use prefix() to disambiguate names.
Definition: PhysicsBase.h:141
const std::vector< VariableName > & auxVariableNames() const
Return the list of aux variables in this physics.
Definition: PhysicsBase.h:106
virtual void act() override final
Forwards from the action tasks to the implemented addXYZ() in the derived classes If you need more th...
Definition: PhysicsBase.C:124
virtual InputParameters getAdditionalRMParams() const
Provide additional parameters for the relationship managers.
Definition: PhysicsBase.h:38
virtual void addInitialConditions()
Definition: PhysicsBase.h:294
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
bool shouldCreateVariable(const VariableName &var_name, const std::vector< SubdomainName > &blocks, const bool error_if_aux)
Returns whether this Physics should create the variable.
Definition: PhysicsBase.C:619
std::vector< VariableName > _aux_var_names
Vector of the aux variables in the Physics.
Definition: PhysicsBase.h:333
Factory & getFactory()
Get the factory for this physics The factory lets you get the parameters for objects.
Definition: PhysicsBase.h:114
virtual void addFVInterfaceKernels()
Definition: PhysicsBase.h:302
virtual void addPreconditioning()
Definition: PhysicsBase.h:319
MooseEnum _is_transient
Whether the physics is to be solved as a transient.
Definition: PhysicsBase.h:328
const unsigned int invalid_uint
RelationshipManagerType
Main types of Relationship Managers.
Definition: MooseTypes.h:1012
ActionWarehouse & _awh
Reference to ActionWarehouse where we store object build by actions.
Definition: Action.h:169
void initializePhysics()
Process some parameters that require the problem to be created. Executed on init_physics.
Definition: PhysicsBase.C:331
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
Generic factory class for build all sorts of objects.
Definition: Factory.h:28
Factory & _factory
The Factory associated with the MooseApp.
virtual void checkIntegrity() const
Additional checks performed near the end of the setup phase.
Definition: PhysicsBase.h:257
virtual const FEProblemBase & getProblem() const
Definition: PhysicsBase.h:123
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
virtual void checkIntegrityEarly() const
Additional checks performed once the executioner / executor has been created.
Definition: PhysicsBase.C:378
virtual void addMultiApps()
Definition: PhysicsBase.h:313
virtual void addAuxiliaryKernels()
Definition: PhysicsBase.h:308
const T * getCoupledPhysics(const PhysicsName &phys_name, const bool allow_fail=false) const
Get a Physics from the ActionWarehouse with the requested type and name.
Definition: PhysicsBase.h:345
virtual void addPeriodicBCs()
Definition: PhysicsBase.h:306
const std::vector< SubdomainName > & blocks() const
Return the blocks this physics is defined on.
Definition: PhysicsBase.h:60
void reportPotentiallyMissedParameters(const std::vector< std::string > &param_names, const std::string &object_type, const std::string &object_name="") const
When this is called, we are knowingly not using the value of these parameters.
Definition: PhysicsBase.C:806
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
bool solverVariableExists(const VariableName &var_name) const
Check whether a variable already exists and is a solver variable.
Definition: PhysicsBase.C:441
const bool _verbose
Whether to output additional information.
Definition: PhysicsBase.h:266
Base class for components that are defined using an action.
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
bool shouldCreateTimeDerivative(const VariableName &var_name, const std::vector< SubdomainName > &blocks, const bool error_if_already_defined) const
Returns whether this Physics should create the variable.
Definition: PhysicsBase.C:705
bool allMeshBlocks(const std::vector< SubdomainName > &blocks) const
Check if a vector contains all the mesh blocks.
Definition: PhysicsBase.C:561
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
std::vector< SubdomainName > _blocks
Keep track of the subdomains the Physics is defined on.
Definition: PhysicsBase.h:273
Base class for actions.
Definition: Action.h:34
unsigned int dimension() const
Return the maximum dimension of the blocks the Physics is active on.
Definition: PhysicsBase.C:252
InputParameters emptyInputParameters()
void checkComponentType(const ActionComponent &component) const
Check that the component is of the desired type.
Definition: PhysicsBase.h:382
virtual void addFVBCs()
Definition: PhysicsBase.h:304
void addBlocks(const std::vector< SubdomainName > &blocks)
Add new blocks to the Physics.
Definition: PhysicsBase.C:290
Utility class to help check parameters.
void saveAuxVariableName(const VariableName &var_name)
Keep track of the name of an aux variable defined in the Physics.
Definition: PhysicsBase.h:149
virtual FEProblemBase & getProblem()
Get the problem for this physics Useful to add objects to the simulation.
Definition: PhysicsBase.h:118
void libmesh_ignore(const Args &...)
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:103
virtual void addNodalBCs()
Definition: PhysicsBase.h:305
const SolverSystemName & getSolverSystem(unsigned int variable_index) const
Get the solver system for this variable index.
Definition: PhysicsBase.C:447
virtual void addNodalKernels()
Definition: PhysicsBase.h:297
virtual void addDGKernels()
Definition: PhysicsBase.h:299
virtual void addMaterials()
Definition: PhysicsBase.h:309
std::set< std::string > _required_tasks
Manually keeps track of the tasks required by each physics as tasks cannot be inherited.
Definition: PhysicsBase.h:340
bool isVariableScalar(const VariableName &var_name) const
Whether the variable is a scalar variable (global single scalar, not a field)
Definition: PhysicsBase.C:613
PhysicsBase(const InputParameters &parameters)
Definition: PhysicsBase.C:106
void copyVariablesFromMesh(const std::vector< VariableName > &variables_to_copy, bool are_nonlinear=true)
Copy nonlinear or aux variables from the mesh file.
Definition: PhysicsBase.C:406
unsigned int _dim
Dimension of the physics, which we expect for now to be the dimension of the mesh NOTE: this is not k...
Definition: PhysicsBase.h:337
virtual void addReporters()
Definition: PhysicsBase.h:317
bool isVariableFV(const VariableName &var_name) const
Whether the variable is a finite volume variable.
Definition: PhysicsBase.C:606
std::vector< std::string > getSubdomainNamesAndIDs(const std::set< SubdomainID > &blocks) const
Get the vector of subdomain names and ids for the incoming set of subdomain IDs.
Definition: PhysicsBase.C:275
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:93
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:54
virtual void addPostprocessors()
Definition: PhysicsBase.h:315
Factory & getFactory() const
Definition: PhysicsBase.h:115
virtual void addFunctions()
Definition: PhysicsBase.h:307
virtual void addFEBCs()
Definition: PhysicsBase.h:303
virtual void addSolverVariables()
The default implementation of these routines will do nothing as we do not expect all Physics to be de...
Definition: PhysicsBase.h:292
virtual void addExecutioner()
Definition: PhysicsBase.h:320
bool hasBlocks(const std::vector< SubdomainName > &blocks) const
Whether the Physics is defined on those blocks.
Definition: PhysicsBase.C:551
const std::vector< VariableName > & solverVariableNames() const
Return the list of solver (nonlinear + linear) variables in this physics.
Definition: PhysicsBase.h:104
bool variableExists(const VariableName &var_name, bool error_if_aux) const
Check whether a variable already exists.
Definition: PhysicsBase.C:428
bool checkBlockRestrictionIdentical(const std::string &object_name, const std::vector< SubdomainName > &blocks, const bool error_if_not_identical=true) const
Check if an external object has the same block restriction.
Definition: PhysicsBase.C:504
virtual void addFVKernels()
Definition: PhysicsBase.h:296
static InputParameters validParams()
Definition: PhysicsBase.C:24
std::vector< VariableName > _solver_var_names
Vector of the solver variables (nonlinear and linear) in the Physics.
Definition: PhysicsBase.h:331
std::set< SubdomainID > getSubdomainIDs(const std::set< SubdomainName > &blocks) const
Get the set of subdomain ids for the incoming vector of subdomain names.
Definition: PhysicsBase.C:260
virtual void addComponent(const ActionComponent &component)
Most basic way of adding a component: simply adding the blocks to the block restriction of the Physic...
Definition: PhysicsBase.C:311
virtual void addFEKernels()
Definition: PhysicsBase.h:295
virtual void addUserObjects()
Definition: PhysicsBase.h:311
virtual void addRelationshipManagers(Moose::RelationshipManagerType input_rm_type) override
Method to add a relationship manager for the objects being added to the system.
Definition: PhysicsBase.C:318
virtual void actOnAdditionalTasks()
Routine to add additional setup work on additional registered tasks to a Physics. ...
Definition: PhysicsBase.h:49
void addBlocksById(const std::vector< SubdomainID > &block_ids)
Definition: PhysicsBase.C:300
void prepareCopyVariablesFromMesh() const
Tell the app if we want to use Exodus restart.
Definition: PhysicsBase.C:230
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:281
virtual void addOutputs()
Definition: PhysicsBase.h:318
bool addRelationshipManagers(Moose::RelationshipManagerType when_type, const InputParameters &moose_object_pars)
Method to add a relationship manager for the objects being added to the system.
Definition: Action.C:131
std::shared_ptr< FEProblemBase > & _problem
Convenience reference to a problem this action works on.
Definition: Action.h:178
virtual void addDiracKernels()
Definition: PhysicsBase.h:298
virtual void addVectorPostprocessors()
Definition: PhysicsBase.h:316
virtual void addExecutors()
Definition: PhysicsBase.h:321
virtual void addInterfaceKernels()
Definition: PhysicsBase.h:301
virtual void addFunctorMaterials()
Definition: PhysicsBase.h:310
const ActionComponent & getActionComponent(const ComponentName &comp_name) const
Get a component with the requested name.
Definition: PhysicsBase.C:325
std::vector< const T * > getActions()
Retrieve all actions in a specific type ordered by their names.
std::vector< unsigned int > _system_numbers
System numbers for the system(s) owning the solver variables.
Definition: PhysicsBase.h:263
virtual void addTransfers()
Definition: PhysicsBase.h:314
virtual void addCorrectors()
Definition: PhysicsBase.h:312
void saveSolverVariableName(const VariableName &var_name)
Keep track of the name of the solver variable defined in the Physics.
Definition: PhysicsBase.h:144
std::vector< SolverSystemName > _system_names
System names for the system(s) owning the solver variables.
Definition: PhysicsBase.h:260
bool isTransient() const
Return whether the Physics is solved using a transient.
Definition: PhysicsBase.C:240
virtual void addAuxiliaryVariables()
Definition: PhysicsBase.h:293
virtual void initializePhysicsAdditional()
Additional initialization work that should happen very early, as soon as the problem is created...
Definition: PhysicsBase.h:284
virtual void addScalarKernels()
Definition: PhysicsBase.h:300
void checkRequiredTasks() const
Check the list of required tasks for missing tasks.
Definition: PhysicsBase.C:475