Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 3 : //* 4 : //* All rights reserved, see COPYRIGHT for full restrictions 5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT 6 : //* 7 : //* Licensed under LGPL 2.1, please see LICENSE for details 8 : //* https://www.gnu.org/licenses/lgpl-2.1.html 9 : 10 : #pragma once 11 : 12 : #include <gtest/gtest.h> 13 : #include "MooseObjectName.h" 14 : #include "MooseTypes.h" 15 : #include "ControllableItem.h" 16 : #include "ControllableParameter.h" 17 : #include "ControlOutput.h" 18 : 19 : // Forward declarations 20 : class InputParameters; 21 : class Factory; 22 : class ActionFactory; 23 : 24 : /** 25 : * Storage container for all InputParamter objects. 26 : * 27 : * This object is responsible for InputParameter objects, all MooseObjects should 28 : * contain a reference to the parameters object stored here. 29 : * 30 : * To avoid abuse, this warehouse is also designed to restrict the ability to change the parameter 31 : * to Control objects only. 32 : */ 33 : class InputParameterWarehouse 34 : { 35 : public: 36 : /** 37 : * Class constructor 38 : */ 39 : InputParameterWarehouse(); 40 : 41 : /** 42 : * Destruction 43 : */ 44 114518 : virtual ~InputParameterWarehouse() = default; 45 : 46 : /** 47 : * Class that is used as a parameter to [add/remove]InputParameters() 48 : * to restrict access. 49 : */ 50 : class AddRemoveParamsKey 51 : { 52 : friend class Factory; 53 : friend class ActionFactory; 54 : FRIEND_TEST(InputParameterWarehouseTest, getControllableItems); 55 : FRIEND_TEST(InputParameterWarehouseTest, getControllableParameter); 56 : FRIEND_TEST(InputParameterWarehouseTest, getControllableParameterValues); 57 : FRIEND_TEST(InputParameterWarehouseTest, emptyControllableParameterValues); 58 : FRIEND_TEST(InputParameterWarehouseTest, addControllableParameterConnection); 59 : FRIEND_TEST(InputParameterWarehouseTest, addControllableParameterAlias); 60 5402930 : AddRemoveParamsKey() {} 61 : AddRemoveParamsKey(const AddRemoveParamsKey &) {} 62 : }; 63 : 64 : ///@{ 65 : /** 66 : * Return a const reference to the InputParameters for the named object 67 : * @param tag The tag of the object (e.g., 'Kernel') 68 : * @param name The name of the parameters object, including the tag (name only input) or 69 : * MooseObjectName object 70 : * @param tid The thread id 71 : * @return A const reference to the warehouse copy of the InputParameters 72 : */ 73 : const InputParameters & getInputParametersObject(const std::string & name, 74 : THREAD_ID tid = 0) const; 75 : const InputParameters & getInputParametersObject(const std::string & tag, 76 : const std::string & name, 77 : THREAD_ID tid = 0) const; 78 : const InputParameters & getInputParametersObject(const MooseObjectName & object_name, 79 : THREAD_ID tid = 0) const; 80 : ///@} 81 : /** 82 : * Return const reference to the map containing the InputParameter objects 83 : */ 84 : const std::multimap<MooseObjectName, std::shared_ptr<InputParameters>> & 85 : getInputParameters(THREAD_ID tid = 0) const; 86 : 87 : /** 88 : * Method for linking control parameters of different names 89 : */ 90 : void addControllableParameterConnection(const MooseObjectParameterName & primary, 91 : const MooseObjectParameterName & secondary, 92 : bool error_on_empty = true); 93 : 94 : /** 95 : * Method for creating alias to an existing controllable parameters. 96 : * 97 : * @param alias The new name to serve as an alias. 98 : * @param secondary The name of the secondary parameter to be aliased. 99 : */ 100 : void addControllableParameterAlias(const MooseObjectParameterName & alias, 101 : const MooseObjectParameterName & secondary); 102 : 103 : /** 104 : * Method for creating alias for all shared controllable parameters between the two objects. 105 : */ 106 : void addControllableObjectAlias(const MooseObjectName & alias, const MooseObjectName & secondary); 107 : 108 : /*** 109 : * Helper method for printing controllable items. 110 : */ 111 : std::string dumpChangedControls(bool reset_changed) const; 112 : 113 : /** 114 : * Returns a copy of the current values for a controllable parameter. This method 115 : * is designed to provide access to objects for monitoring the state of a controllable parameter. 116 : */ 117 : template <typename T> 118 : std::vector<T> getControllableParameterValues(const MooseObjectParameterName & input) const; 119 : 120 : /** 121 : * Return a vector of parameters names matching the supplied name. 122 : */ 123 : std::vector<MooseObjectParameterName> 124 : getControllableParameterNames(const MooseObjectParameterName & input) const; 125 : 126 : /** 127 : * Method for adding a new InputParameters object 128 : * @param parameters The InputParameters object to copy and store in the warehouse 129 : * @return A reference to the warehouse copy of the InputParameters, this 130 : * is what should be passed into the MooseObjects constructors. 131 : * 132 : * A new object is created from the old object because InputParameters objects 133 : * are generic until Factory::create() is called and the actual MooseObject 134 : * is created. 135 : * 136 : * This method is protected by the AddRemoveParamsKey, because only the factories 137 : * that are creating objects should be able to call this method. 138 : */ 139 : InputParameters & addInputParameters(const std::string & name, 140 : const InputParameters & parameters, 141 : THREAD_ID tid, 142 : const AddRemoveParamsKey); 143 : 144 : /** 145 : * Allows for the deletion and cleanup of an object while the simulation is running. 146 : */ 147 : void 148 : removeInputParameters(const MooseObject & moose_object, THREAD_ID tid, const AddRemoveParamsKey); 149 : 150 : private: 151 : /// Storage for the InputParameters objects 152 : /// TODO: Remove multimap 153 : std::vector<std::multimap<MooseObjectName, std::shared_ptr<InputParameters>>> _input_parameters; 154 : 155 : /// Storage for controllable parameters via ControllableItem objects, a unique_ptr is 156 : /// used to avoid creating multiple copies. All access to the objects are done via 157 : /// pointers. The ControllableItem objects are not designed and will not be used directly in 158 : /// user code. All user level access goes through the ControllableParameter object. 159 : std::vector<std::vector<std::shared_ptr<ControllableItem>>> _controllable_items; 160 : 161 : /** 162 : * Returns a ControllableParameter object that contains all matches to ControllableItem objects 163 : * for the provided name. 164 : * 165 : * This is private because it should only be accessed via a Control object. 166 : */ 167 : ControllableParameter getControllableParameter(const MooseObjectParameterName & input) const; 168 : 169 : /** 170 : * Returns a ControllableItem iterator, if the name is located. 171 : * @see Control 172 : */ 173 : std::vector<ControllableItem *> getControllableItems(const MooseObjectParameterName & desired, 174 : THREAD_ID tid = 0) const; 175 : 176 : ///@{ 177 : /** 178 : * Return a reference to the InputParameters for the named object 179 : * @param tag The tag of the object (e.g., 'Kernel') 180 : * @param name The name of the parameters object, including the tag (name only input) or 181 : * MooseObjectName object 182 : * @param tid The thread id 183 : * @return A reference to the warehouse copy of the InputParameters 184 : * 185 : * If you are using this method to access a writable reference to input parameters, this 186 : * will break the ability to control the parameters with the MOOSE control logic system. 187 : * Only change parameters if you know what you are doing. Hence, this is private for a reason. 188 : */ 189 : InputParameters & getInputParameters(const std::string & name, THREAD_ID tid = 0) const; 190 : InputParameters & 191 : getInputParameters(const std::string & tag, const std::string & name, THREAD_ID tid = 0) const; 192 : InputParameters & getInputParameters(const MooseObjectName & object_name, 193 : THREAD_ID tid = 0) const; 194 : ///@} 195 : 196 : /// Only controls are allowed to call getControllableParameter. The 197 : /// Control::getControllableParameter is the only method that calls getControllableParameter. 198 : /// However, this method cannot be made a friend explicitly because the method would need to be 199 : /// public. If the method is public then it is possible to call the method by getting access to 200 : /// the Control object. 201 : friend class Control; 202 : 203 : // Allow unit test to call methods 204 : FRIEND_TEST(InputParameterWarehouseTest, getControllableItems); 205 : FRIEND_TEST(InputParameterWarehouseTest, getControllableParameter); 206 : FRIEND_TEST(InputParameterWarehouseTest, getControllableParameterValues); 207 : FRIEND_TEST(InputParameterWarehouseTest, emptyControllableParameterValues); 208 : FRIEND_TEST(InputParameterWarehouseTest, addControllableParameterConnection); 209 : FRIEND_TEST(InputParameterWarehouseTest, addControllableParameterAlias); 210 : }; 211 : 212 : template <typename T> 213 : std::vector<T> 214 2 : InputParameterWarehouse::getControllableParameterValues( 215 : const MooseObjectParameterName & input) const 216 : { 217 2 : ControllableParameter param = getControllableParameter(input); 218 4 : return param.get<T>(); 219 2 : }