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 
118  virtual std::vector<UserObjectName> getSuppliedUserObjects() const { return {}; }
119 
120 protected:
122  bool isTransient() const;
123 
126  Factory & getFactory() { return _factory; }
127  Factory & getFactory() const { return _factory; }
131  {
132  mooseAssert(_problem, "Requesting the problem too early");
133  return *_problem;
134  }
135  virtual const FEProblemBase & getProblem() const
136  {
137  mooseAssert(_problem, "Requesting the problem too early");
138  return *_problem;
139  }
140 
142  void prepareCopyVariablesFromMesh() const;
149  void copyVariablesFromMesh(const std::vector<VariableName> & variables_to_copy,
150  bool are_nonlinear = true);
151 
153  std::string prefix() const { return name() + "_"; }
154 
162  void
163  addUserObject(const std::string & uo_type, const std::string & uo_name, InputParameters & params);
164 
166  void saveSolverVariableName(const VariableName & var_name)
167  {
168  _solver_var_names.push_back(var_name);
169  }
171  void saveAuxVariableName(const VariableName & var_name) { _aux_var_names.push_back(var_name); }
172 
174  bool variableExists(const VariableName & var_name, bool error_if_aux) const;
176  bool solverVariableExists(const VariableName & var_name) const;
177 
180  const SolverSystemName & getSolverSystem(unsigned int variable_index) const;
182  const SolverSystemName & getSolverSystem(const VariableName & variable_name) const;
183 
186  void addRequiredPhysicsTask(const std::string & task) { _required_tasks.insert(task); }
187 
193  void assignBlocks(InputParameters & params, const std::vector<SubdomainName> & blocks) const;
198  bool allMeshBlocks(const std::vector<SubdomainName> & blocks) const;
199  bool allMeshBlocks(const std::set<SubdomainName> & blocks) const;
200  // These APIs can deal with ANY_BLOCK_ID or ids with no names. They will be slower than the
201  // MooseMeshUtils' APIs, but are more convenient for setup purposes
203  std::set<SubdomainID> getSubdomainIDs(const std::set<SubdomainName> & blocks) const;
205  std::vector<std::string> getSubdomainNamesAndIDs(const std::set<SubdomainID> & blocks) const;
206 
211  const std::vector<std::pair<MooseEnumItem, std::string>> & petsc_pair_options);
212 
213  // Helpers to check on variable types
215  bool isVariableFV(const VariableName & var_name) const;
217  bool isVariableScalar(const VariableName & var_name) const;
218 
219  // Routines to help with deciding when to create objects
227  bool shouldCreateVariable(const VariableName & var_name,
228  const std::vector<SubdomainName> & blocks,
229  const bool error_if_aux);
230 
244  bool shouldCreateIC(const VariableName & var_name,
245  const std::vector<SubdomainName> & blocks,
246  const bool ic_is_default_ic,
247  const bool error_if_already_defined) const;
248 
259  bool shouldCreateTimeDerivative(const VariableName & var_name,
260  const std::vector<SubdomainName> & blocks,
261  const bool error_if_already_defined) const;
262 
263  // Other conceivable "shouldCreate" routines for things that are unique to a variable
264  // - shouldCreateTimeIntegrator
265  // - shouldCreatePredictor/Corrector
266 
274  void reportPotentiallyMissedParameters(const std::vector<std::string> & param_names,
275  const std::string & object_type,
276  const std::string & object_name = "") const;
277 
279  virtual void checkIntegrity() const {}
280 
282  std::vector<SolverSystemName> _system_names;
283 
285  std::vector<unsigned int> _system_numbers;
286 
288  const bool _verbose;
289 
293 
295  std::vector<SubdomainName> _blocks;
296 
297 private:
301  virtual void addRelationshipManagers(Moose::RelationshipManagerType input_rm_type) override;
302 
304  void initializePhysics();
306  virtual void initializePhysicsAdditional() {}
308  virtual void checkIntegrityEarly() const;
309 
314  virtual void addSolverVariables() {}
315  virtual void addAuxiliaryVariables() {}
316  virtual void addInitialConditions() {}
317  virtual void addFEKernels() {}
318  virtual void addFVKernels() {}
319  virtual void addFVInterpolationMethods() {}
320  virtual void addNodalKernels() {}
321  virtual void addDiracKernels() {}
322  virtual void addDGKernels() {}
323  virtual void addScalarKernels() {}
324  virtual void addInterfaceKernels() {}
325  virtual void addFVInterfaceKernels() {}
326  virtual void addFEBCs() {}
327  virtual void addFVBCs() {}
328  virtual void addNodalBCs() {}
329  virtual void addPeriodicBCs() {}
330  virtual void addFunctions() {}
331  virtual void addAuxiliaryKernels() {}
332  virtual void addMaterials() {}
333  virtual void addFunctorMaterials() {}
334  virtual void addUserObjects() {}
335  virtual void addCorrectors() {}
336  virtual void addMultiApps() {}
337  virtual void addTransfers() {}
338  virtual void addPostprocessors() {}
339  virtual void addVectorPostprocessors() {}
340  virtual void addReporters() {}
341  virtual void addOutputs() {}
342  virtual void addPreconditioning() {}
343  virtual void addExecutioner() {}
344  virtual void addExecutors() {}
345 
347  void checkRequiredTasks() const;
348 
352 
354  std::vector<VariableName> _solver_var_names;
356  std::vector<VariableName> _aux_var_names;
357 
360  unsigned int _dim = libMesh::invalid_uint;
361 
363  std::set<std::string> _required_tasks;
364 };
365 
366 template <typename T>
367 const T *
368 PhysicsBase::getCoupledPhysics(const PhysicsName & phys_name, 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  for (const auto * const physics : all_T_physics)
375  {
376  if (physics->name() == phys_name)
377  return physics;
378  }
379  if (!allow_fail)
380  mooseError("Requested Physics '",
381  phys_name,
382  "' does not exist or is not of type '",
383  MooseUtils::prettyCppType<T>(),
384  "'");
385  else
386  return nullptr;
387 }
388 
389 template <typename T>
390 const std::vector<T *>
391 PhysicsBase::getCoupledPhysics(const bool allow_fail) const
392 {
393  constexpr bool is_physics = std::is_base_of<PhysicsBase, T>::value;
394  libmesh_ignore(is_physics);
395  mooseAssert(is_physics, "Must be a PhysicsBase to be retrieved by getCoupledPhysics");
396  const auto all_T_physics = _awh.getActions<T>();
397  if (!allow_fail && all_T_physics.empty())
398  mooseError("No Physics of requested type '", MooseUtils::prettyCppType<T>(), "'");
399  else
400  return all_T_physics;
401 }
402 
403 template <typename T>
404 void
406 {
407  if (!dynamic_cast<const T *>(&component))
408  mooseError("Component '" + component.name() + "' must be of type '" +
409  MooseUtils::prettyCppType<T>() + "'.\nIt is currently of type '" + component.type() +
410  "'");
411 }
std::string prefix() const
Use prefix() to disambiguate names.
Definition: PhysicsBase.h:153
void addUserObject(const std::string &uo_type, const std::string &uo_name, InputParameters &params)
Add a UserObject supplied by this Physics to the problem.
Definition: PhysicsBase.C:233
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:316
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:511
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:639
std::vector< VariableName > _aux_var_names
Vector of the aux variables in the Physics.
Definition: PhysicsBase.h:356
Factory & getFactory()
Get the factory for this physics The factory lets you get the parameters for objects.
Definition: PhysicsBase.h:126
virtual void addFVInterfaceKernels()
Definition: PhysicsBase.h:325
virtual void addPreconditioning()
Definition: PhysicsBase.h:342
MooseEnum _is_transient
Whether the physics is to be solved as a transient.
Definition: PhysicsBase.h:351
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:351
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:186
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:612
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:279
virtual const FEProblemBase & getProblem() const
Definition: PhysicsBase.h:135
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:398
virtual void addMultiApps()
Definition: PhysicsBase.h:336
virtual void addAuxiliaryKernels()
Definition: PhysicsBase.h:331
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:368
virtual void addPeriodicBCs()
Definition: PhysicsBase.h:329
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:826
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:292
bool solverVariableExists(const VariableName &var_name) const
Check whether a variable already exists and is a solver variable.
Definition: PhysicsBase.C:461
const bool _verbose
Whether to output additional information.
Definition: PhysicsBase.h:288
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:663
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:725
bool allMeshBlocks(const std::vector< SubdomainName > &blocks) const
Check if a vector contains all the mesh blocks.
Definition: PhysicsBase.C:581
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:295
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:272
InputParameters emptyInputParameters()
void checkComponentType(const ActionComponent &component) const
Check that the component is of the desired type.
Definition: PhysicsBase.h:405
virtual void addFVBCs()
Definition: PhysicsBase.h:327
void addBlocks(const std::vector< SubdomainName > &blocks)
Add new blocks to the Physics.
Definition: PhysicsBase.C:310
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:171
virtual FEProblemBase & getProblem()
Get the problem for this physics Useful to add objects to the simulation.
Definition: PhysicsBase.h:130
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:328
const SolverSystemName & getSolverSystem(unsigned int variable_index) const
Get the solver system for this variable index.
Definition: PhysicsBase.C:467
virtual void addNodalKernels()
Definition: PhysicsBase.h:320
virtual void addDGKernels()
Definition: PhysicsBase.h:322
virtual void addMaterials()
Definition: PhysicsBase.h:332
std::set< std::string > _required_tasks
Manually keeps track of the tasks required by each physics as tasks cannot be inherited.
Definition: PhysicsBase.h:363
bool isVariableScalar(const VariableName &var_name) const
Whether the variable is a scalar variable (global single scalar, not a field)
Definition: PhysicsBase.C:633
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:426
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:360
virtual void addReporters()
Definition: PhysicsBase.h:340
bool isVariableFV(const VariableName &var_name) const
Whether the variable is a finite volume variable.
Definition: PhysicsBase.C:626
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:295
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:338
Factory & getFactory() const
Definition: PhysicsBase.h:127
virtual void addFunctions()
Definition: PhysicsBase.h:330
virtual void addFEBCs()
Definition: PhysicsBase.h:326
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:314
virtual void addExecutioner()
Definition: PhysicsBase.h:343
bool hasBlocks(const std::vector< SubdomainName > &blocks) const
Whether the Physics is defined on those blocks.
Definition: PhysicsBase.C:571
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:448
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:524
virtual void addFVKernels()
Definition: PhysicsBase.h:318
static InputParameters validParams()
Definition: PhysicsBase.C:24
virtual std::vector< UserObjectName > getSuppliedUserObjects() const
Return the names of the UserObjects this Physics adds to the problem.
Definition: PhysicsBase.h:118
virtual void addFVInterpolationMethods()
Definition: PhysicsBase.h:319
std::vector< VariableName > _solver_var_names
Vector of the solver variables (nonlinear and linear) in the Physics.
Definition: PhysicsBase.h:354
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:280
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:331
virtual void addFEKernels()
Definition: PhysicsBase.h:317
virtual void addUserObjects()
Definition: PhysicsBase.h:334
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:338
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:320
void prepareCopyVariablesFromMesh() const
Tell the app if we want to use Exodus restart.
Definition: PhysicsBase.C:250
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
virtual void addOutputs()
Definition: PhysicsBase.h:341
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:321
virtual void addVectorPostprocessors()
Definition: PhysicsBase.h:339
virtual void addExecutors()
Definition: PhysicsBase.h:344
virtual void addInterfaceKernels()
Definition: PhysicsBase.h:324
virtual void addFunctorMaterials()
Definition: PhysicsBase.h:333
const ActionComponent & getActionComponent(const ComponentName &comp_name) const
Get a component with the requested name.
Definition: PhysicsBase.C:345
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:285
virtual void addTransfers()
Definition: PhysicsBase.h:337
virtual void addCorrectors()
Definition: PhysicsBase.h:335
void saveSolverVariableName(const VariableName &var_name)
Keep track of the name of the solver variable defined in the Physics.
Definition: PhysicsBase.h:166
std::vector< SolverSystemName > _system_names
System names for the system(s) owning the solver variables.
Definition: PhysicsBase.h:282
bool isTransient() const
Return whether the Physics is solved using a transient.
Definition: PhysicsBase.C:260
virtual void addAuxiliaryVariables()
Definition: PhysicsBase.h:315
virtual void initializePhysicsAdditional()
Additional initialization work that should happen very early, as soon as the problem is created...
Definition: PhysicsBase.h:306
virtual void addScalarKernels()
Definition: PhysicsBase.h:323
void checkRequiredTasks() const
Check the list of required tasks for missing tasks.
Definition: PhysicsBase.C:495