Skip to content

Commit

Permalink
Merge pull request #28083 from ddm42/next_PR2
Browse files Browse the repository at this point in the history
Explicit Dynamics: Initial Condition for Solution Time Derivatives
  • Loading branch information
GiudGiud authored Jan 28, 2025
2 parents e948b5b + 30c2e94 commit f29335c
Show file tree
Hide file tree
Showing 27 changed files with 721 additions and 18 deletions.
7 changes: 7 additions & 0 deletions framework/doc/content/syntax/ICs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ object's variable *must* be defined on the block - otherwise the IC for the
next lowest block ID for the node is used - and so forth until one has the
variable defined.

## Old and Older ICs

The ICs system supports the ability to set ICs on old and older states. This can be useful for initializing old and older states of variables needed for various time integration schemes. It can be set with the [!param](/ICs/ConstantIC/state) parameter and specifying `OLD` or `OLDER`.




!syntax list /ICs objects=True actions=False subsystems=False

!syntax list /ICs objects=False actions=False subsystems=True
Expand Down
3 changes: 3 additions & 0 deletions framework/include/auxkernels/CopyValueAux.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class CopyValueAux : public AuxKernel
protected:
virtual Real computeValue() override;

// variable used to specify state being copied
unsigned short _state;

/// The variable to project from
const VariableValue & _v;

Expand Down
2 changes: 2 additions & 0 deletions framework/include/fvics/FVInitialConditionBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#pragma once

#include "InitialConditionInterface.h"
#include "MooseObject.h"
#include "FunctionInterface.h"
#include "UserObjectInterface.h"
Expand All @@ -30,6 +31,7 @@ class Point;
* description
*/
class FVInitialConditionBase : public MooseObject,
public InitialConditionInterface,
public BlockRestrictable,
public FunctionInterface,
public Restartable,
Expand Down
2 changes: 2 additions & 0 deletions framework/include/ics/InitialConditionBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#pragma once

#include "InitialConditionInterface.h"
#include "MooseObject.h"
#include "Coupleable.h"
#include "FunctionInterface.h"
Expand Down Expand Up @@ -36,6 +37,7 @@ class Point;
* hierarchy: the `compute` method
*/
class InitialConditionBase : public MooseObject,
public InitialConditionInterface,
public BlockRestrictable,
public Coupleable,
public MaterialPropertyInterface,
Expand Down
41 changes: 41 additions & 0 deletions framework/include/ics/InitialConditionInterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#pragma once

#include "InputParameters.h"

/**
* InitialConditionInterface serves as the abstract class for InitialConditions,
* FVInitialConditions, and ScalarInitialConditions
*/
class InitialConditionInterface
{
public:
/**
* Constructor
*
* @param parameters The parameters object holding data for the class to use.
*/
InitialConditionInterface(const InputParameters & parameters);

virtual ~InitialConditionInterface();

static InputParameters validParams();

/**
* Retrieves the state of this initial condition.
* @return The state of this initial condition.
*/
unsigned short getState() const;

protected:
// variable used when applying initial conditions to previous states
unsigned short _my_state;
};
11 changes: 8 additions & 3 deletions framework/include/ics/InitialConditionWarehouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ class InitialConditionWarehouse : public MooseObjectWarehouseBase<InitialConditi

protected:
///@{
/// Variable name to block/boundary IDs for error checking
std::vector<std::map<std::string, std::set<BoundaryID>>> _boundary_ics;
std::vector<std::map<std::string, std::set<SubdomainID>>> _block_ics;
/// Maps used to check if multiple ICs define the same variable on the same block/boundary for the same state (e.g CURRENT, OLD, OLDER).
/// They are vectors of maps because each map is repeated for each thread.
/// Each map relates string-int tuples to a set of block/boundary IDs.
/// The string-int tuple is a unique identifier for a specific variable and state. The string-int tuple is renamed to ic_key_type for clarity.
/// The algorithm then makes sure that a new IC object does not overlap with a previous IC object (i.e. same block/boundary).
using ic_key_type = std::tuple<VariableName, unsigned short>;
std::vector<std::map<ic_key_type, std::set<BoundaryID>>> _boundary_ics;
std::vector<std::map<ic_key_type, std::set<SubdomainID>>> _block_ics;
///@}
};
2 changes: 2 additions & 0 deletions framework/include/ics/ScalarInitialCondition.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#pragma once

#include "InitialConditionInterface.h"
#include "MooseObject.h"
#include "ScalarCoupleable.h"
#include "FunctionInterface.h"
Expand All @@ -30,6 +31,7 @@ class DenseVector;
* InitialConditions are objects that set the initial value of variables.
*/
class ScalarInitialCondition : public MooseObject,
public InitialConditionInterface,
public ScalarCoupleable,
public FunctionInterface,
public UserObjectInterface,
Expand Down
9 changes: 9 additions & 0 deletions framework/include/problems/FEProblemBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,12 @@ class FEProblemBase : public SubProblem, public Restartable

void projectSolution();

/**
* Retrieves the current initial condition state.
* @return current initial condition state
*/
unsigned short getCurrentICState();

/**
* Project initial conditions for custom \p elem_range and \p bnd_node_range
* This is needed when elements/boundary nodes are added to a specific subdomain
Expand Down Expand Up @@ -2790,6 +2796,9 @@ class FEProblemBase : public SubProblem, public Restartable
/// requested an AD material property or whether any suppier has declared an AD material property
bool _using_ad_mat_props;

// loop state during projection of initial conditions
unsigned short _current_ic_state;

private:
/**
* Handle exceptions. Note that the result of this call will be a thrown MooseException. The
Expand Down
13 changes: 12 additions & 1 deletion framework/src/auxkernels/CopyValueAux.C
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,22 @@ CopyValueAux::validParams()
params.addClassDescription("Returns the specified variable as an auxiliary variable with a "
"simple copy of the variable values.");
params.addRequiredCoupledVar("source", "Variable to take the value of.");
MooseEnum stateEnum("CURRENT=0 OLD=1 OLDER=2", "CURRENT");
params.addParam<MooseEnum>(
"state",
stateEnum,
"This parameter specifies the state being copied. CURRENT=0 OLD=1 OLDER=2. Copying an older "
"state allows access to previous solution information if necessary.");
return params;
}

CopyValueAux::CopyValueAux(const InputParameters & parameters)
: AuxKernel(parameters), _v(coupledValue("source")), _source_variable(*getVar("source", 0))
: AuxKernel(parameters),
_state(getParam<MooseEnum>("state")),
_v(_state == 0 ? coupledValue("source")
: _state == 1 ? coupledValueOld("source")
: coupledValueOlder("source")),
_source_variable(*getVar("source", 0))
{
if (_var.feType().family != _source_variable.feType().family)
paramError("variable",
Expand Down
2 changes: 2 additions & 0 deletions framework/src/fvics/FVInitialConditionBase.C
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ InputParameters
FVInitialConditionBase::validParams()
{
InputParameters params = MooseObject::validParams();
params += InitialConditionInterface::validParams();
params += BlockRestrictable::validParams();

params.addRequiredParam<VariableName>("variable",
Expand All @@ -28,6 +29,7 @@ FVInitialConditionBase::validParams()

FVInitialConditionBase::FVInitialConditionBase(const InputParameters & parameters)
: MooseObject(parameters),
InitialConditionInterface(parameters),
BlockRestrictable(this),
FunctionInterface(this),
Restartable(this, "FVInitialConditionBases"),
Expand Down
2 changes: 2 additions & 0 deletions framework/src/ics/InitialConditionBase.C
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ InputParameters
InitialConditionBase::validParams()
{
InputParameters params = MooseObject::validParams();
params += InitialConditionInterface::validParams();
params += BlockRestrictable::validParams();
params += BoundaryRestrictable::validParams();
params += MaterialPropertyInterface::validParams();
Expand All @@ -38,6 +39,7 @@ InitialConditionBase::validParams()

InitialConditionBase::InitialConditionBase(const InputParameters & parameters)
: MooseObject(parameters),
InitialConditionInterface(parameters),
BlockRestrictable(this),
Coupleable(this,
getParam<SystemBase *>("_sys")
Expand Down
45 changes: 45 additions & 0 deletions framework/src/ics/InitialConditionInterface.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#include "InitialConditionInterface.h"
#include "InputParameters.h"
#include "MooseEnum.h"

InputParameters
InitialConditionInterface::validParams()
{
InputParameters params = emptyInputParameters();

MooseEnum stateEnum("CURRENT=0 OLD=1 OLDER=2", "CURRENT");
params.addParam<MooseEnum>(
"state",
stateEnum,
"This parameter is used to set old state solutions at the start of simulation. If specifying "
"multiple states at the start of simulation, use one IC object for each state being "
"specified. The states are CURRENT=0 OLD=1 OLDER=2. States older than 2 are not currently "
"supported. When the user only specifies current state, the solution is copied to the old "
"and older states, as expected. This functionality is mainly used for dynamic simulations "
"with explicit time integration schemes, where old solution states are used in the velocity "
"and acceleration approximations.");

return params;
}

InitialConditionInterface::InitialConditionInterface(const InputParameters & parameters)
: _my_state(parameters.get<MooseEnum>("state"))
{
}

InitialConditionInterface::~InitialConditionInterface() {}

unsigned short
InitialConditionInterface::getState() const
{
return _my_state;
}
26 changes: 13 additions & 13 deletions framework/src/ics/InitialConditionWarehouse.C
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ InitialConditionWarehouse::addObject(std::shared_ptr<InitialConditionBase> objec
{
// Check that when object is boundary restricted that the variable is nodal
const MooseVariableFEBase & var = object->variable();
const auto ic_key = std::tuple(var.name(), object->getState());

// Boundary Restricted
if (object->boundaryRestricted())
Expand All @@ -43,43 +44,42 @@ InitialConditionWarehouse::addObject(std::shared_ptr<InitialConditionBase> objec
mooseError("You are trying to set a boundary restricted variable on non-nodal variable. That "
"is not allowed.");

std::map<std::string, std::set<BoundaryID>>::const_iterator iter =
_boundary_ics[tid].find(var.name());
const auto iter = _boundary_ics[tid].find(ic_key);
if (iter != _boundary_ics[tid].end() && object->hasBoundary(iter->second))
mooseError("The initial condition '",
object->name(),
"' is being defined on a boundary that already has an initial condition defined.");
"' is being defined on a boundary that already has an initial condition defined "
"with the same variable and state.");
else
_boundary_ics[tid][var.name()].insert(object->boundaryIDs().begin(),
object->boundaryIDs().end());
_boundary_ics[tid][ic_key].insert(object->boundaryIDs().begin(), object->boundaryIDs().end());
}

// Block Restricted
else if (object->blockRestricted())
{
std::map<std::string, std::set<SubdomainID>>::const_iterator iter =
_block_ics[tid].find(var.name());
auto iter = _block_ics[tid].find(ic_key);
if (iter != _block_ics[tid].end() &&
(object->hasBlocks(iter->second) ||
(iter->second.find(Moose::ANY_BLOCK_ID) != iter->second.end())))
mooseError("The initial condition '",
object->name(),
"' is being defined on a block that already has an initial condition defined.");
"' is being defined on a block that already has an initial condition defined "
"with the same variable and state.");
else
_block_ics[tid][var.name()].insert(object->blockIDs().begin(), object->blockIDs().end());
_block_ics[tid][ic_key].insert(object->blockIDs().begin(), object->blockIDs().end());
}

// Non-restricted
else
{
std::map<std::string, std::set<SubdomainID>>::const_iterator iter =
_block_ics[tid].find(var.name());
auto iter = _block_ics[tid].find(ic_key);
if (iter != _block_ics[tid].end())
mooseError("The initial condition '",
object->name(),
"' is being defined on a block that already has an initial condition defined.");
"' is being defined on a block that already has an initial condition defined "
"with the same variable and state.");
else
_block_ics[tid][var.name()].insert(Moose::ANY_BLOCK_ID);
_block_ics[tid][ic_key].insert(Moose::ANY_BLOCK_ID);
}

// Add the IC to the storage
Expand Down
2 changes: 2 additions & 0 deletions framework/src/ics/ScalarInitialCondition.C
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ InputParameters
ScalarInitialCondition::validParams()
{
InputParameters params = MooseObject::validParams();
params += InitialConditionInterface::validParams();
params.addParam<VariableName>(
"variable", "The variable this initial condition is supposed to provide values for.");

Expand All @@ -26,6 +27,7 @@ ScalarInitialCondition::validParams()

ScalarInitialCondition::ScalarInitialCondition(const InputParameters & parameters)
: MooseObject(parameters),
InitialConditionInterface(parameters),
ScalarCoupleable(this),
FunctionInterface(this),
UserObjectInterface(this),
Expand Down
4 changes: 3 additions & 1 deletion framework/src/loops/ComputeInitialConditionThread.C
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ComputeInitialConditionThread::operator()(const ConstElemRange & range)
{
ParallelUniqueId puid;
_tid = puid.id;
const auto current_ic_state = _fe_problem.getCurrentICState();

const InitialConditionWarehouse & warehouse = _fe_problem.getInitialConditionWarehouse();
printGeneralExecutionInformation();
Expand Down Expand Up @@ -65,7 +66,8 @@ ComputeInitialConditionThread::operator()(const ConstElemRange & range)
if (warehouse.hasActiveBlockObjects(id, _tid))
for (auto ic : warehouse.getActiveBlockObjects(id, _tid))
{
if ((id != elem->subdomain_id()) && !ic->variable().isNodal())
if ((id != elem->subdomain_id() && !ic->variable().isNodal()) ||
ic->getState() != current_ic_state)
continue;
order.push_back(&(ic->variable()));
groups[&(ic->variable())].push_back(ic);
Expand Down
Loading

0 comments on commit f29335c

Please sign in to comment.