Skip to content

Commit

Permalink
demonstrate action controllable parameters with a test idaholab#22069
Browse files Browse the repository at this point in the history
  • Loading branch information
YaqiWang committed Sep 12, 2022
1 parent 35e5eb2 commit ae8633b
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 15 deletions.
22 changes: 21 additions & 1 deletion framework/doc/content/syntax/Controls/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
The control system in MOOSE has one primary purpose: +to modify input parameters during runtime
of a MOOSE-based simulation+.

## Creating a Controllable Parameter
## Creating a Controllable Parameter id=sec:control-param

The input parameters of objects you which to be controlled must:

Expand Down Expand Up @@ -91,6 +91,26 @@ In both cases there is an alternative form for defining an object and parameter
`base::object/name`. In this case "base" is the MOOSE base system that the object is derived from.
For example, `Kernel::diff/coef`.

## Controllable Parameters Added by Actions

MOOSE also allows parameters in [Action](Actions/index.md) to be controllable.
The procedure for making a parameter in an [Action](Actions/index.md) controllable is the same as documented in [sec:control-param].
It is important that this controllable parameter must be directly connected with the parameters of MOOSE objects, such as kernels, materials, etc., using this parameter.

!listing test/src/actions/AddLotsOfDiffusion.C
start=GenericConstantArray
end=connectControllableParams
id=connect_controllable
caption=Example of connecting controllable parameters in an action and the objects added by the action.

The action controllable parameter can be referred as usual in an input file. For example,

!listing test/tests/controls/action_control/action_control_test.i
block=Controls
id=controls_example
caption=Example of a "Action" block that contains a parameter that is controlled via a
MOOSE Control object.

## Child Objects

!syntax list /Controls objects=True actions=False subsystems=False
Expand Down
32 changes: 32 additions & 0 deletions test/include/controls/RealVectorFunctionControl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//* 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 "Control.h"

class Function;

class RealVectorFunctionControl : public Control
{
public:
/**
* Class constructor
* @param parameters Input parameters for this Control object
*/
static InputParameters validParams();

RealVectorFunctionControl(const InputParameters & parameters);

virtual void execute() override;

private:
/// The function to execute
const Function & _function;
};
60 changes: 46 additions & 14 deletions test/src/actions/AddLotsOfDiffusion.C
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ AddLotsOfDiffusion::validParams()
"family", family, "Specifies the family of FE shape functions to use for this variable.");
params.addParam<bool>("array", false, "Whether or not to use array variables");
params.addRequiredParam<unsigned int>("number", "The number of variables to add");
params.addParam<unsigned int>("n_components", 2, "The number of components of array variables");
params.addParam<bool>("add_reaction", false, "True to add reaction kernels");

params.addRequiredParam<RealEigenVector>("diffusion_coefficients",
"Diffusion coefficient to be used by all variables");
params.declareControllable("diffusion_coefficients");
return params;
}

Expand All @@ -62,6 +67,7 @@ void
AddLotsOfDiffusion::act()
{
unsigned int number = getParam<unsigned int>("number");
unsigned int ncomp = getParam<unsigned int>("n_components");

const auto array = getParam<bool>("array");

Expand All @@ -74,7 +80,7 @@ AddLotsOfDiffusion::act()
var_params.set<MooseEnum>("family") = getParam<MooseEnum>("family");
var_params.set<MooseEnum>("order") = getParam<MooseEnum>("order");
if (array)
var_params.set<unsigned int>("components") = 2;
var_params.set<unsigned int>("components") = ncomp;
for (unsigned int cur_num = 0; cur_num < number; cur_num++)
{
std::string var_name = name() + Moose::stringify(cur_num);
Expand All @@ -86,13 +92,25 @@ AddLotsOfDiffusion::act()
for (unsigned int cur_num = 0; cur_num < number; cur_num++)
{
std::string var_name = name() + Moose::stringify(cur_num);
const auto kernel_type = array ? "ArrayDiffusion" : "Diffusion";

InputParameters params = _factory.getValidParams(kernel_type);
params.set<NonlinearVariableName>("variable") = var_name;
if (array)
params.set<MaterialPropertyName>("diffusion_coefficient") = "dc";
_problem->addKernel(kernel_type, var_name, params);
{
const auto kernel_type = array ? "ArrayDiffusion" : "Diffusion";

InputParameters params = _factory.getValidParams(kernel_type);
params.set<NonlinearVariableName>("variable") = var_name;
if (array)
params.set<MaterialPropertyName>("diffusion_coefficient") = "dc";
_problem->addKernel(kernel_type, var_name, params);
}

if (getParam<bool>("add_reaction"))
{
const auto kernel_type = array ? "ArrayReaction" : "Reaction";
InputParameters params = _factory.getValidParams(kernel_type);
params.set<NonlinearVariableName>("variable") = var_name;
if (array)
params.set<MaterialPropertyName>("reaction_coefficient") = "rc";
_problem->addKernel(kernel_type, var_name + "_reaction", params);
}
}
}
else if (_current_task == "add_bc")
Expand All @@ -106,15 +124,15 @@ AddLotsOfDiffusion::act()
params.set<NonlinearVariableName>("variable") = var_name;
params.set<std::vector<BoundaryName>>("boundary").push_back("left");
if (array)
params.set<RealEigenVector>("values") = RealEigenVector::Constant(2, 0);
params.set<RealEigenVector>("values") = RealEigenVector::Constant(ncomp, 0);
else
params.set<Real>("value") = 0;

_problem->addBoundaryCondition(bc_type, var_name + "_left", params);

params.set<std::vector<BoundaryName>>("boundary")[0] = "right";
if (array)
params.set<RealEigenVector>("values") = RealEigenVector::Constant(2, 1);
params.set<RealEigenVector>("values") = RealEigenVector::Constant(ncomp, 1);
else
params.set<Real>("value") = 1;

Expand All @@ -123,9 +141,23 @@ AddLotsOfDiffusion::act()
}
else if (_current_task == "add_material" && array)
{
auto params = _factory.getValidParams("GenericConstantArray");
params.set<std::string>("prop_name") = "dc";
params.set<RealEigenVector>("prop_value") = RealEigenVector::Constant(2, 1);
_problem->addMaterial("GenericConstantArray", "dc", params);
{
auto params = _factory.getValidParams("GenericConstantArray");
params.set<std::string>("prop_name") = "dc";
params.set<RealEigenVector>("prop_value") =
getParam<RealEigenVector>("diffusion_coefficients");
_problem->addMaterial("GenericConstantArray", "dc", params);

// pass the control to the material by connecting them
connectControllableParams("diffusion_coefficients", "MaterialBase", "dc", "prop_value");
}

if (getParam<bool>("add_reaction"))
{
auto params = _factory.getValidParams("GenericConstantArray");
params.set<std::string>("prop_name") = "rc";
params.set<RealEigenVector>("prop_value") = RealEigenVector::Constant(ncomp, 1);
_problem->addMaterial("GenericConstantArray", "rc", params);
}
}
}
43 changes: 43 additions & 0 deletions test/src/controls/RealVectorFunctionControl.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//* 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

// MOOSE includes
#include "RealVectorFunctionControl.h"
#include "Function.h"

registerMooseObject("MooseTestApp", RealVectorFunctionControl);

InputParameters
RealVectorFunctionControl::validParams()
{
InputParameters params = Control::validParams();
params.addClassDescription("Sets all component a 'RealEigenVector' input parameters to the value "
"of a provided function.");
params.addRequiredParam<FunctionName>(
"function", "The function to use for controlling the specified parameter.");
params.addRequiredParam<std::string>(
"parameter",
"The input parameter(s) to control. Specify a single parameter name and all "
"parameters in all objects matching the name will be updated");
return params;
}

RealVectorFunctionControl::RealVectorFunctionControl(const InputParameters & parameters)
: Control(parameters), _function(getFunction("function"))
{
}

void
RealVectorFunctionControl::execute()
{
Real value = _function.value(_t);
auto cname = getParam<std::string>("parameter");
unsigned int ncomp = getControllableValueByName<RealEigenVector>(cname).size();
setControllableValueByName<RealEigenVector>(cname, RealEigenVector::Constant(ncomp, value));
}
41 changes: 41 additions & 0 deletions test/tests/controls/action_control/action_control_test.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[Mesh]
[gmg]
type = GeneratedMeshGenerator
dim = 2
nx = 3
ny = 3
[]
[]

[Testing/LotsOfDiffusion/lots]
number = 10
array = true
n_components = 4
diffusion_coefficients = '1 1 1 1'
add_reaction = true
[]

[Functions]
[dc]
type = ParsedFunction
value = t+1
[]
[]

[Controls]
[setdc]
type = RealVectorFunctionControl
function = dc
parameter = Testing/LotsOfDiffusion/lots/diffusion_coefficients
execute_on = timestep_begin
[]
[]

[Executioner]
type = Transient
num_steps = 10
[]

[Outputs]
exodus = true
[]
Binary file not shown.
10 changes: 10 additions & 0 deletions test/tests/controls/action_control/tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Tests]
[test]
type = Exodiff
input = action_control_test.i
exodiff = action_control_test_out.e
issues = '#22068'
design = 'syntax/Controls/index.md'
requirement = "The Control system shall include a means to control input parameters added by MOOSE action systems."
[]
[]
1 change: 1 addition & 0 deletions test/tests/preconditioners/pbp/lots_of_variables.i
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@

[Testing/LotsOfDiffusion/vars]
number = 10
diffusion_coefficients = '1 1'
[]
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
[./LotsOfDiffusion]
[./vars]
number = 1
diffusion_coefficients = 1
[../]
[../]
[]
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
[Testing/LotsOfDiffusion/lots]
number = 1
array = true
diffusion_coefficients = '1 1'
[]

[Executioner]
Expand Down

0 comments on commit ae8633b

Please sign in to comment.