Skip to content

Commit

Permalink
Merge pull request idaholab#22045 from YaqiWang/setup_22006
Browse files Browse the repository at this point in the history
Add new generic setup function in SetupInterface
  • Loading branch information
lindsayad committed Sep 28, 2022
2 parents ae03f38 + 47bf740 commit 14b5c5a
Show file tree
Hide file tree
Showing 23 changed files with 238 additions and 8 deletions.
26 changes: 24 additions & 2 deletions framework/doc/content/source/interfaces/SetupInterface.md
Expand Up @@ -43,6 +43,20 @@ complete list of execution flags is provided by MOOSE are listed in the "registe

!listing framework/src/base/MooseApp.C start=MooseApp::registerExecFlags() end=void

The default value of "execute_on" is *linear* for most of MOOSE objects with the exception of:

- The auxiliary kernels have the default value of *linear* and *timestep_end*.
- The postprocessors have default value of *timestep_end*.
- The controls have default value of *initial* and *timestep_end*.
- The multi-apps have default value of *timestep_begin*.
- The user objects have default value of *timestep_end*.
- The outputs have default value of *initial* and *timestep_end*.
- The default value for a transfer is set to be the same as the execute_on value of its corresponding sub-application.

Several objects in the framework have custom or selected default "execute_on".
The default value for all objects including objects in MOOSE modules and MOOSE-based
applications can be found in their parameter list.

## Modifying Execute On

When creating objects that inherit from SetupInterface it is possible to set, add, or remove
Expand Down Expand Up @@ -75,8 +89,8 @@ The SetupInterface includes virtual methods that correspond to the primary execu
with MOOSE, these methods are listed in the header as shown here.

!listing framework/include/interfaces/SetupInterface.h
start=~SetupInterface()
end=subdomainSetup
start=static InputParameters validParams()
end=customSetup
include-end=True
include-start=False
strip-leading-whitespace=True
Expand All @@ -89,6 +103,14 @@ A few of the methods were created prior to the execute flags, thus the names do
they remain as is to keep the API consistent: the "jacobianSetup" methods is called prior to the
"NONLINEAR" execute flag and the "residualSetup" is called prior to the "LINEAR" execute flag.

There is also a generic setup function "customSetup" that takes an execute flag as the argument.
This function is called by MOOSE when performing evaluations of objects on the custom execute flags
in [Creating Custom Execute Flags](#creating-custom-execute-flags).

!alert warning title=Note on the "customSetup" function
This function is not called on *initial*, *timestep_begin*, *subdomain*, *nonlinear* and *linear*.
Setup operations for those execute flags should be implemented in *initialSetup*, *timestepSetup*,
*subdomainSetup*, *jacobianSetup* and *residualSetup* functions respectively.

## Creating Custom Execute Flags

Expand Down
15 changes: 15 additions & 0 deletions framework/include/base/MooseFunctor.h
Expand Up @@ -342,6 +342,7 @@ class FunctorBase
virtual void residualSetup();
virtual void jacobianSetup();
virtual void timestepSetup();
virtual void customSetup(const ExecFlagType & exec_type);

/**
* Set how often to clear the functor evaluation cache
Expand Down Expand Up @@ -868,6 +869,14 @@ FunctorBase<T>::jacobianSetup()
clearCacheData();
}

template <typename T>
void
FunctorBase<T>::customSetup(const ExecFlagType & exec_type)
{
if (_clearance_schedule.count(exec_type))
clearCacheData();
}

template <typename T>
typename FunctorBase<T>::GradientType
FunctorBase<T>::gradient(const ElemArg & elem, const unsigned int state) const
Expand Down Expand Up @@ -983,6 +992,7 @@ class FunctorEnvelopeBase
virtual void timestepSetup() = 0;
virtual void residualSetup() = 0;
virtual void jacobianSetup() = 0;
virtual void customSetup(const ExecFlagType & /*exec_type*/) = 0;
virtual bool wrapsNull() const = 0;
virtual std::string returnType() const = 0;
virtual bool isConstant() const = 0;
Expand Down Expand Up @@ -1086,6 +1096,11 @@ class FunctorEnvelope final : public FunctorBase<T>, public FunctorEnvelopeBase
if (_owned)
_owned->timestepSetup();
}
void customSetup(const ExecFlagType & exec_type) override
{
if (_owned)
_owned->customSetup(exec_type);
}
void residualSetup() override
{
if (_owned)
Expand Down
6 changes: 6 additions & 0 deletions framework/include/interfaces/SetupInterface.h
Expand Up @@ -54,6 +54,12 @@ class SetupInterface
*/
virtual void subdomainSetup();

/**
* Gets called in FEProblemBase::execute() for execute flags other than initial, timestep_begin,
* nonlinear, linear and subdomain
*/
virtual void customSetup(const ExecFlagType & /*exec_type*/) {}

/**
* Return the execute on MultiMooseEnum for this object.
*/
Expand Down
1 change: 1 addition & 0 deletions framework/include/linesearches/LineSearch.h
Expand Up @@ -35,6 +35,7 @@ class LineSearch : public MooseObject
virtual void lineSearch() { mooseError("You must implement a line-search method."); }

virtual void timestepSetup() {}
virtual void customSetup(const ExecFlagType & /*exec_type*/) {}
virtual void initialSetup() {}

protected:
Expand Down
6 changes: 6 additions & 0 deletions framework/include/outputs/OutputWarehouse.h
Expand Up @@ -264,6 +264,12 @@ class OutputWarehouse : protected PerfGraphInterface
*/
void timestepSetup();

/**
* Calls the setup function for each of the output objects
* @see FEProblemBase::customSetup(const ExecFlagType & exec_type)
*/
void customSetup(const ExecFlagType & exec_type);

/**
* Calls the jacobianSetup function for each of the output objects
* @see FEProblemBase::computeJacobian
Expand Down
1 change: 1 addition & 0 deletions framework/include/problems/DisplacedProblem.h
Expand Up @@ -339,6 +339,7 @@ class DisplacedProblem : public SubProblem

void initialSetup() override;
void timestepSetup() override;
void customSetup(const ExecFlagType & exec_type) override;

using SubProblem::haveADObjects;
void haveADObjects(bool have_ad_objects) override;
Expand Down
1 change: 1 addition & 0 deletions framework/include/problems/FEProblemBase.h
Expand Up @@ -346,6 +346,7 @@ class FEProblemBase : public SubProblem, public Restartable

void initialSetup() override;
void timestepSetup() override;
void customSetup(const ExecFlagType & exec_type) override;
void residualSetup() override;
void jacobianSetup() override;

Expand Down
1 change: 1 addition & 0 deletions framework/include/problems/SubProblem.h
Expand Up @@ -828,6 +828,7 @@ class SubProblem : public Problem

virtual void initialSetup();
virtual void timestepSetup();
virtual void customSetup(const ExecFlagType & exec_type);
virtual void residualSetup();
virtual void jacobianSetup();

Expand Down
1 change: 1 addition & 0 deletions framework/include/systems/AuxiliarySystem.h
Expand Up @@ -46,6 +46,7 @@ class AuxiliarySystem : public SystemBase, public PerfGraphInterface

virtual void initialSetup() override;
virtual void timestepSetup() override;
virtual void customSetup(const ExecFlagType & exec_type) override;
virtual void subdomainSetup() override;
virtual void residualSetup() override;
virtual void jacobianSetup() override;
Expand Down
1 change: 1 addition & 0 deletions framework/include/systems/NonlinearSystemBase.h
Expand Up @@ -99,6 +99,7 @@ class NonlinearSystemBase : public SystemBase, public PerfGraphInterface
// Setup Functions ////
virtual void initialSetup() override;
virtual void timestepSetup() override;
virtual void customSetup(const ExecFlagType & exec_type) override;
virtual void residualSetup() override;
virtual void jacobianSetup() override;

Expand Down
1 change: 1 addition & 0 deletions framework/include/systems/SystemBase.h
Expand Up @@ -879,6 +879,7 @@ class SystemBase : public libMesh::ParallelObject, public ConsoleStreamInterface
/// Setup Functions
virtual void initialSetup();
virtual void timestepSetup();
virtual void customSetup(const ExecFlagType & exec_type);
virtual void subdomainSetup();
virtual void residualSetup();
virtual void jacobianSetup();
Expand Down
5 changes: 5 additions & 0 deletions framework/include/variables/VariableWarehouse.h
Expand Up @@ -171,6 +171,11 @@ class VariableWarehouse
*/
void timestepSetup();

/**
* Call setup on a particular execute flag for all variables
*/
void customSetup(const ExecFlagType & exec_type);

/**
* Call subdomainSetup for all variables
*/
Expand Down
10 changes: 10 additions & 0 deletions framework/include/warehouses/MooseObjectWarehouse.h
Expand Up @@ -53,6 +53,7 @@ class MooseObjectWarehouse : public MooseObjectWarehouseBase<T>
*/
virtual void initialSetup(THREAD_ID tid = 0) const;
virtual void timestepSetup(THREAD_ID tid = 0) const;
virtual void customSetup(const ExecFlagType & exec_type, THREAD_ID tid = 0) const;
virtual void subdomainSetup(THREAD_ID tid = 0) const;
virtual void subdomainSetup(SubdomainID id, THREAD_ID tid = 0) const;
virtual void jacobianSetup(THREAD_ID tid = 0) const;
Expand Down Expand Up @@ -163,6 +164,15 @@ MooseObjectWarehouse<T>::timestepSetup(THREAD_ID tid /* = 0*/) const
object->timestepSetup();
}

template <typename T>
void
MooseObjectWarehouse<T>::customSetup(const ExecFlagType & exec_type, THREAD_ID tid /* = 0*/) const
{
checkThreadID(tid);
for (const auto & object : _active_objects[tid])
object->customSetup(exec_type);
}

template <typename T>
void
MooseObjectWarehouse<T>::subdomainSetup(SubdomainID id, THREAD_ID tid /* = 0*/) const
Expand Down
7 changes: 7 additions & 0 deletions framework/src/outputs/OutputWarehouse.C
Expand Up @@ -61,6 +61,13 @@ OutputWarehouse::timestepSetup()
obj->timestepSetup();
}

void
OutputWarehouse::customSetup(const ExecFlagType & exec_type)
{
for (const auto & obj : _all_objects)
obj->customSetup(exec_type);
}

void
OutputWarehouse::solveSetup()
{
Expand Down
9 changes: 9 additions & 0 deletions framework/src/problems/DisplacedProblem.C
Expand Up @@ -1152,6 +1152,15 @@ DisplacedProblem::timestepSetup()
_displaced_aux.timestepSetup();
}

void
DisplacedProblem::customSetup(const ExecFlagType & exec_type)
{
SubProblem::customSetup(exec_type);

_displaced_nl.customSetup(exec_type);
_displaced_aux.customSetup(exec_type);
}

void
DisplacedProblem::haveADObjects(const bool have_ad_objects)
{
Expand Down
43 changes: 43 additions & 0 deletions framework/src/problems/FEProblemBase.C
Expand Up @@ -3773,6 +3773,42 @@ FEProblemBase::executeAllObjects(const ExecFlagType & /*exec_type*/)
{
}

void
FEProblemBase::customSetup(const ExecFlagType & exec_type)
{
SubProblem::customSetup(exec_type);

if (_line_search)
_line_search->customSetup(exec_type);

unsigned int n_threads = libMesh::n_threads();
for (THREAD_ID tid = 0; tid < n_threads; tid++)
{
_all_materials.customSetup(exec_type, tid);
_functions.customSetup(exec_type, tid);
}

_aux->customSetup(exec_type);
_nl->customSetup(exec_type);

if (_displaced_problem)
_displaced_problem->customSetup(exec_type);

for (THREAD_ID tid = 0; tid < n_threads; tid++)
{
_internal_side_indicators.customSetup(exec_type, tid);
_indicators.customSetup(exec_type, tid);
_markers.customSetup(exec_type, tid);
}

std::vector<UserObject *> userobjs;
theWarehouse().query().condition<AttribSystem>("UserObject").queryIntoUnsorted(userobjs);
for (auto obj : userobjs)
obj->customSetup(exec_type);

_app.getOutputWarehouse().customSetup(exec_type);
}

void
FEProblemBase::execute(const ExecFlagType & exec_type)
{
Expand All @@ -3782,6 +3818,13 @@ FEProblemBase::execute(const ExecFlagType & exec_type)
if (exec_type != EXEC_INITIAL)
executeControls(exec_type);

// intentially call this after executing controls because the setups may rely on the controls
// FIXME: we skip the following flags because they have dedicated setup functions in
// SetupInterface and it may not be appropriate to call them here.
if (!(exec_type == EXEC_INITIAL || exec_type == EXEC_TIMESTEP_BEGIN ||
exec_type == EXEC_SUBDOMAIN || exec_type == EXEC_NONLINEAR || exec_type == EXEC_LINEAR))
customSetup(exec_type);

// Samplers; EXEC_INITIAL is not called because the Sampler::init() method that is called after
// construction makes the first Sampler::execute() call. This ensures that the random number
// generator object is the correct state prior to any other object (e.g., Transfers) attempts to
Expand Down
8 changes: 8 additions & 0 deletions framework/src/problems/SubProblem.C
Expand Up @@ -1017,6 +1017,14 @@ SubProblem::timestepSetup()
pr.second->timestepSetup();
}

void
SubProblem::customSetup(const ExecFlagType & exec_type)
{
for (auto & map : _functors)
for (auto & pr : map)
pr.second->customSetup(exec_type);
}

void
SubProblem::residualSetup()
{
Expand Down
18 changes: 18 additions & 0 deletions framework/src/systems/AuxiliarySystem.C
Expand Up @@ -138,6 +138,24 @@ AuxiliarySystem::timestepSetup()
}
}

void
AuxiliarySystem::customSetup(const ExecFlagType & exec_type)
{
SystemBase::customSetup(exec_type);

for (unsigned int tid = 0; tid < libMesh::n_threads(); tid++)
{
_aux_scalar_storage.customSetup(exec_type, tid);
_nodal_aux_storage.customSetup(exec_type, tid);
_mortar_nodal_aux_storage.customSetup(exec_type, tid);
_nodal_vec_aux_storage.customSetup(exec_type, tid);
_nodal_array_aux_storage.customSetup(exec_type, tid);
_elemental_aux_storage.customSetup(exec_type, tid);
_elemental_vec_aux_storage.customSetup(exec_type, tid);
_elemental_array_aux_storage.customSetup(exec_type, tid);
}
}

void
AuxiliarySystem::subdomainSetup()
{
Expand Down
54 changes: 54 additions & 0 deletions framework/src/systems/NonlinearSystemBase.C
Expand Up @@ -387,6 +387,60 @@ NonlinearSystemBase::timestepSetup()
_nodal_bcs.timestepSetup();
}

void
NonlinearSystemBase::customSetup(const ExecFlagType & exec_type)
{
SystemBase::customSetup(exec_type);

for (THREAD_ID tid = 0; tid < libMesh::n_threads(); tid++)
{
_kernels.customSetup(exec_type, tid);
_nodal_kernels.customSetup(exec_type, tid);
_dirac_kernels.customSetup(exec_type, tid);
if (_doing_dg)
_dg_kernels.customSetup(exec_type, tid);
_interface_kernels.customSetup(exec_type, tid);
_element_dampers.customSetup(exec_type, tid);
_nodal_dampers.customSetup(exec_type, tid);
_integrated_bcs.customSetup(exec_type, tid);

if (_fe_problem.haveFV())
{
std::vector<FVFluxBC *> bcs;
_fe_problem.theWarehouse()
.query()
.template condition<AttribSystem>("FVFluxBC")
.template condition<AttribThread>(tid)
.queryInto(bcs);

std::vector<FVInterfaceKernel *> iks;
_fe_problem.theWarehouse()
.query()
.template condition<AttribSystem>("FVInterfaceKernel")
.template condition<AttribThread>(tid)
.queryInto(iks);

std::vector<FVFluxKernel *> kernels;
_fe_problem.theWarehouse()
.query()
.template condition<AttribSystem>("FVFluxKernel")
.template condition<AttribThread>(tid)
.queryInto(kernels);

for (auto * bc : bcs)
bc->customSetup(exec_type);
for (auto * ik : iks)
ik->customSetup(exec_type);
for (auto * kernel : kernels)
kernel->customSetup(exec_type);
}
}
_scalar_kernels.customSetup(exec_type);
_constraints.customSetup(exec_type);
_general_dampers.customSetup(exec_type);
_nodal_bcs.customSetup(exec_type);
}

void
NonlinearSystemBase::setDecomposition(const std::vector<std::string> & splits)
{
Expand Down

0 comments on commit 14b5c5a

Please sign in to comment.