Skip to content

Commit

Permalink
code changes to have the new action constructor interface and let the…
Browse files Browse the repository at this point in the history
… input parameter warehouse to keep the action parameters idaholab#22068
  • Loading branch information
YaqiWang committed Sep 13, 2022
1 parent d2aa34f commit 01b28fc
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 23 deletions.
34 changes: 33 additions & 1 deletion framework/include/actions/Action.h
Expand Up @@ -15,6 +15,7 @@
#include "Registry.h"
#include "PerfGraphInterface.h"
#include "DataFileInterface.h"
#include "MooseObjectParameterName.h"

#include "libmesh/parallel_object.h"

Expand Down Expand Up @@ -111,9 +112,17 @@ class Action : public ConsoleStreamInterface,
std::string getShortName() const;
///@}

/**
* The unique name for accessing input parameters of this action in the InputParameterWarehouse
*/
MooseObjectName uniqueActionName() const
{
return MooseObjectName(
_pars.get<std::string>("_moose_base"), _pars.get<std::string>("_unique_action_name"), "::");
}

const std::string & type() const { return _action_type; }

InputParameters & parameters() { return _pars; }
const InputParameters & parameters() const { return _pars; }

const std::string & specificTaskName() const { return _specific_task_name; }
Expand Down Expand Up @@ -207,6 +216,29 @@ class Action : public ConsoleStreamInterface,
*/
virtual void act() = 0;

/**
* Connect controllable parameter of this action with the controllable parameters of the
* objects added by this action.
* @param parameter Name of the controllable parameter of this action
* @param object_type Type of the object added by this action. Acceptable types include
* LineSearch, MooseMesh, AuxScalarKernel, AuxKernel, VectorAuxKernel,
* ArrayAuxKernel, ScalarKernel, Kernel, VectorKernel, ArrayKernel,
* EigenKernel, Executioner, FVKernel, MaterialBase, BoundaryCondition,
* MeshGenerator, Constraint, Marker, VectorPostprocessor, MoosePartitioner,
* Indicator, Postprocessor, Predictor, Problem, Transfer, DiracKernel,
* Damper, DGKernel, MultiApp, InterfaceKernel, VectorInterfaceKernel,
* UserObject, Executor, MoosePreconditioner, MooseVariableBase, Sampler,
* TimeStepper, Function, FVBoundaryCondition, Reporter, Output,
* FVInterfaceKernel, InitialCondition, ScalarInitialCondition, Distribution,
* TimeIntegrator, NodalKernel, Split, RelationshipManager.
* @param object_name Name of the object added by this action.
* @param object_parameter Name of the parameter of the object.
*/
virtual void connectControllableParams(const std::string & parameter,
const std::string & object_type,
const std::string & object_name,
const std::string & object_parameter) const;

/// Input parameters for the action
const InputParameters & _pars;

Expand Down
9 changes: 9 additions & 0 deletions framework/include/base/MooseObject.h
Expand Up @@ -15,6 +15,7 @@
#include "Registry.h"
#include "MooseUtils.h"
#include "DataFileInterface.h"
#include "MooseObjectParameterName.h"

#include "libmesh/parallel_object.h"

Expand Down Expand Up @@ -65,6 +66,14 @@ class MooseObject : public ConsoleStreamInterface,
*/
virtual const std::string & name() const { return _name; }

/**
* The unique parameter name of a valid parameter of this object for accessing parameter controls
*/
MooseObjectParameterName uniqueParameterName(const std::string & parameter_name) const
{
return MooseObjectParameterName(_pars.get<std::string>("_moose_base"), _name, parameter_name);
}

/**
* Get the object's combined type and name; useful in error handling.
* @return The type and name of this object in the form '<type()> "<name()>"'.
Expand Down
6 changes: 3 additions & 3 deletions framework/include/outputs/OutputWarehouse.h
Expand Up @@ -101,13 +101,13 @@ class OutputWarehouse : protected PerfGraphInterface
*
* @see CommonOutputAction
*/
void setCommonParameters(InputParameters * params_ptr);
void setCommonParameters(const InputParameters * params_ptr);

/**
* Get a reference to the common output parameters
* @return Pointer to the common InputParameters object
*/
InputParameters * getCommonParameters();
const InputParameters * getCommonParameters() const;

/**
* Return the sync times for all objects
Expand Down Expand Up @@ -326,7 +326,7 @@ class OutputWarehouse : protected PerfGraphInterface
std::set<OutFileBase> _file_base_set;

/// Pointer to the common InputParameters (@see CommonOutputAction)
InputParameters * _common_params_ptr;
const InputParameters * _common_params_ptr;

/// Sync times for all objects
std::set<Real> _sync_times;
Expand Down
3 changes: 3 additions & 0 deletions framework/include/utils/InputParameterWarehouse.h
Expand Up @@ -15,6 +15,7 @@
#include "ControllableItem.h"
#include "ControllableParameter.h"
#include "Factory.h"
#include "ActionFactory.h"
#include "ControlOutput.h"

// Forward declarations
Expand Down Expand Up @@ -176,6 +177,8 @@ class InputParameterWarehouse
/// The factory is allowed to call addInputParameters and removeInputParameters.
friend MooseObjectPtr Factory::create(
const std::string &, const std::string &, const InputParameters &, THREAD_ID, bool);
friend std::shared_ptr<Action>
ActionFactory::create(const std::string &, const std::string &, InputParameters &);
friend void Factory::releaseSharedObjects(const MooseObject &, THREAD_ID);
///@}

Expand Down
28 changes: 28 additions & 0 deletions framework/src/actions/Action.C
Expand Up @@ -16,6 +16,7 @@
#include "FEProblemBase.h"
#include "DisplacedProblem.h"
#include "RelationshipManager.h"
#include "InputParameterWarehouse.h"

InputParameters
Action::validParams()
Expand Down Expand Up @@ -43,6 +44,10 @@ Action::validParams()
params.addPrivateParam<std::string>("action_type");
params.addPrivateParam<ActionWarehouse *>("awh", nullptr);

params.addParam<std::vector<std::string>>(
"control_tags",
"Adds user-defined labels for accessing object parameters via control logic.");
params.registerBase("Action");
return params;
}

Expand Down Expand Up @@ -173,3 +178,26 @@ Action::getBaseName() const
mooseDeprecated("getBaseName() is deprecated.");
return MooseUtils::baseName(_name);
}

void
Action::connectControllableParams(const std::string & parameter,
const std::string & object_type,
const std::string & object_name,
const std::string & object_parameter) const
{
MooseObjectParameterName primary_name(uniqueActionName(), parameter);
MooseObjectParameterName secondary_name(object_type, object_name, object_parameter);
_app.getInputParameterWarehouse().addControllableParameterConnection(primary_name,
secondary_name);

const std::vector<std::string> & tags = _pars.get<std::vector<std::string>>("control_tags");
for (const auto & tag : tags)
{
if (!tag.empty())
{
MooseObjectParameterName tagged_name(tag, _name, parameter);
_app.getInputParameterWarehouse().addControllableParameterConnection(tagged_name,
secondary_name);
}
}
}
22 changes: 18 additions & 4 deletions framework/src/actions/ActionFactory.C
Expand Up @@ -10,6 +10,8 @@
// MOOSE includes
#include "ActionFactory.h"
#include "MooseApp.h"
#include "InputParameterWarehouse.h"
#include "MooseObjectAction.h"

ActionFactory::ActionFactory(MooseApp & app) : _app(app) {}

Expand Down Expand Up @@ -40,15 +42,26 @@ ActionFactory::reg(const std::string & name,

std::shared_ptr<Action>
ActionFactory::create(const std::string & action,
const std::string & action_name,
const std::string & full_action_name,
InputParameters & parameters)
{
std::string action_name = MooseUtils::shortName(full_action_name);
parameters.addPrivateParam("_moose_app", &_app);
parameters.addPrivateParam("action_type", action);
std::pair<ActionFactory::iterator, ActionFactory::iterator> iters;

if (!(parameters.have_parameter<bool>("isObjectAction") &&
parameters.get<bool>("isObjectAction")))
parameters.set<std::vector<std::string>>("control_tags")
.push_back(MooseUtils::baseName(full_action_name));

std::string unique_action_name = action + parameters.get<std::string>("task") + full_action_name;
// Create the actual parameters object that the object will reference
InputParameters & params =
_app.getInputParameterWarehouse().addInputParameters(unique_action_name, parameters);

// Check to make sure that all required parameters are supplied
parameters.checkParams(action_name);
params.checkParams(action_name);

iters = _name_to_build_info.equal_range(action);
BuildInfo * build_info = &(iters.first->second);
Expand All @@ -58,8 +71,9 @@ ActionFactory::create(const std::string & action,
action_name);

// Add the name to the parameters and create the object
parameters.set<std::string>("_action_name") = action_name;
std::shared_ptr<Action> action_obj = (*build_info->_build_pointer)(parameters);
params.set<std::string>("_action_name") = action_name;
params.set<std::string>("_unique_action_name") = unique_action_name;
std::shared_ptr<Action> action_obj = (*build_info->_build_pointer)(params);

if (parameters.get<std::string>("task") == "")
action_obj->appendTask(build_info->_task);
Expand Down
2 changes: 0 additions & 2 deletions framework/src/actions/CommonOutputAction.C
Expand Up @@ -176,8 +176,6 @@ CommonOutputAction::act()
// Only create a Console if screen output was not created
if (getParam<bool>("console") && !hasConsole())
create("Console");
else
_pars.set<bool>("console") = false;

if (getParam<bool>("csv"))
create("CSV");
Expand Down
7 changes: 7 additions & 0 deletions framework/src/actions/GlobalParamsAction.C
Expand Up @@ -40,3 +40,10 @@ GlobalParamsAction::remove(const std::string & name)
{
parameters().remove(name);
}

InputParameters &
GlobalParamsAction::parameters()
{
const auto & params = _app.getInputParameterWarehouse().getInputParameters();
return *(params.find(uniqueActionName())->second.get());
}
3 changes: 2 additions & 1 deletion framework/src/outputs/ControlOutput.C
Expand Up @@ -73,7 +73,8 @@ ControlOutput::outputActiveObjects()
for (const auto & iter : objects)
{
std::shared_ptr<InputParameters> ptr = iter.first;
if (ptr->get<bool>("enable"))
// actions do not have 'enable' parameter
if (!ptr->have_parameter<bool>("enable") || ptr->get<bool>("enable"))
{
// We print slightly differently in the first iteration of the loop.
bool first_iteration = true;
Expand Down
6 changes: 3 additions & 3 deletions framework/src/outputs/OutputWarehouse.C
Expand Up @@ -277,13 +277,13 @@ OutputWarehouse::getFileNumbers()
}

void
OutputWarehouse::setCommonParameters(InputParameters * params_ptr)
OutputWarehouse::setCommonParameters(const InputParameters * params_ptr)
{
_common_params_ptr = params_ptr;
}

InputParameters *
OutputWarehouse::getCommonParameters()
const InputParameters *
OutputWarehouse::getCommonParameters() const
{
return _common_params_ptr;
}
Expand Down
2 changes: 1 addition & 1 deletion framework/src/parser/Parser.C
Expand Up @@ -486,7 +486,7 @@ Parser::walkRaw(std::string /*fullpath*/, std::string /*nodepath*/, hit::Node *

// Create the Action
std::shared_ptr<Action> action_obj =
_action_factory.create(it->second._action, MooseUtils::shortName(curr_identifier), params);
_action_factory.create(it->second._action, curr_identifier, params);

{
// extract the MooseObject params if necessary
Expand Down
2 changes: 1 addition & 1 deletion framework/src/problems/FEProblemBase.C
Expand Up @@ -7293,7 +7293,7 @@ FEProblemBase::addOutput(const std::string & object_type,
}

// Apply the common parameters loaded with Outputs input syntax
InputParameters * common = output_warehouse.getCommonParameters();
const InputParameters * common = output_warehouse.getCommonParameters();
if (common)
parameters.applyParameters(*common, exclude);

Expand Down
20 changes: 13 additions & 7 deletions framework/src/utils/InputParameterWarehouse.C
Expand Up @@ -63,25 +63,26 @@ InputParameterWarehouse::addInputParameters(const std::string & name,
{
if (!tag.empty())
{
auto short_name = MooseUtils::shortName(name);
_input_parameters[tid].insert(std::pair<MooseObjectName, std::shared_ptr<InputParameters>>(
MooseObjectName(tag, name), ptr));
object_names.emplace_back(tag, name);
MooseObjectName(tag, short_name), ptr));
object_names.emplace_back(tag, short_name);
}
}
}

// Store controllable parameters using all possible names
for (libMesh::Parameters::iterator map_iter = ptr->begin(); map_iter != ptr->end(); ++map_iter)
{
const std::string & name = map_iter->first;
const std::string & pname = map_iter->first;
libMesh::Parameters::Value * value = MooseUtils::get(map_iter->second);

if (ptr->isControllable(name))
if (ptr->isControllable(pname))
for (const auto & object_name : object_names)
{
MooseObjectParameterName param_name(object_name, name);
MooseObjectParameterName param_name(object_name, pname);
_controllable_items[tid].emplace_back(std::make_shared<ControllableItem>(
param_name, value, ptr->getControllableExecuteOnTypes(name)));
param_name, value, ptr->getControllableExecuteOnTypes(pname)));
}
}

Expand All @@ -92,7 +93,12 @@ InputParameterWarehouse::addInputParameters(const std::string & name,
ptr->addPrivateParam<std::string>("_unique_name", oss.str());
ptr->addPrivateParam<std::string>("_object_name", name);
ptr->addPrivateParam<THREAD_ID>("_tid", tid);
ptr->allowCopy(false); // no more copies allowed

// no more copies allowed
// Note: we have to skip action parameters for now due to parameter copy operations in
// the input parameter printing capability in ActionWarehouse::printInputFile.
if (base != "Action")
ptr->allowCopy(false);

// Return a reference to the InputParameters object
return *ptr;
Expand Down

0 comments on commit 01b28fc

Please sign in to comment.