Skip to content

Commit

Permalink
Added a method to Simulation for adding generic variables
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuahansel committed Apr 25, 2023
1 parent e80d8f4 commit cf41d7d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
13 changes: 13 additions & 0 deletions modules/thermal_hydraulics/include/base/Simulation.h
Expand Up @@ -156,6 +156,19 @@ class Simulation : public libMesh::ParallelObject, public LoggingInterface, publ
const std::vector<SubdomainName> & subdomain_names,
Real scaling_factor = 1.0);

/**
* Queues a generic variable to be added to the nonlinear or aux system.
*
* @param[in] nl True if this is a nonlinear (solution) variable
* @param[in] var_type Type (class) of the variable
* @param[in] name Name of the variable
* @param[in] params Input parameters for the variable
*/
void addSimVariable(bool nl,
const std::string & var_type,
const VariableName & name,
const InputParameters & params);

/**
* Reports an error if the variable name is not too long
*/
Expand Down
74 changes: 74 additions & 0 deletions modules/thermal_hydraulics/src/base/Simulation.C
Expand Up @@ -395,6 +395,80 @@ Simulation::addSimVariable(bool nl,
}
}

void
Simulation::addSimVariable(bool nl,
const std::string & var_type,
const VariableName & name,
const InputParameters & params)
{
checkVariableNameLength(name);

if (_vars.find(name) == _vars.end()) // variable is new
{
VariableInfo vi;
vi._nl = nl;
vi._var_type = var_type;
vi._params = params;

_vars[name] = vi;
}
else // variable was previously added
{
VariableInfo & vi = _vars[name];
InputParameters & vi_params = vi._params;

if (vi._nl != nl)
mooseError("The variable '",
name,
"' has already been added in a different system (nonlinear or aux).");

if (vi._var_type != var_type)
mooseError("The variable '",
name,
"' has already been added with a different type than '",
var_type,
"'.");

// Check that all valid parameters (other than 'block') are consistent
for (auto it = params.begin(); it != params.end(); it++)
{
const std::string param_name = it->first;
if (param_name == "block")
{
if (vi_params.isParamValid("block"))
{
auto blocks = vi_params.get<std::vector<SubdomainName>>("block");
const auto new_blocks = params.get<std::vector<SubdomainName>>("block");
for (const auto & subdomain_name : new_blocks)
if (std::find(blocks.begin(), blocks.end(), subdomain_name) == blocks.end())
blocks.push_back(subdomain_name);
vi_params.set<std::vector<SubdomainName>>("block") = blocks;
}
else
mooseError("The variable '", name, "' was added previously without block restriction.");
}
else if (params.isParamValid(param_name))
{
if (vi_params.isParamValid(param_name))
{
if (params.rawParamVal(param_name) != vi_params.rawParamVal(param_name))
mooseError("The variable '",
name,
"' was added previously with a different value for the parameter '",
param_name,
"'.");
}
else
mooseError("The variable '",
name,
"' was added previously without the parameter '",
param_name,
"'.");
}
}
}
}

void
Simulation::checkVariableNameLength(const std::string & name) const
{
Expand Down

0 comments on commit cf41d7d

Please sign in to comment.