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 "Moose.h" 13 : 14 : // MOOSE includes 15 : #include "MooseVariableBase.h" 16 : 17 : // C++ includes 18 : #include <unordered_map> 19 : #include <string> 20 : #include <vector> 21 : 22 : // Forward declarations 23 : class FEProblemBase; 24 : class InputParameters; 25 : class MooseObject; 26 : class MooseVariableScalar; 27 : 28 : /** 29 : * Interface for objects that needs scalar coupling capabilities 30 : * 31 : */ 32 : class ScalarCoupleable 33 : { 34 : public: 35 : /** 36 : * Constructing the object 37 : * @param parameters Parameters that come from constructing the object 38 : */ 39 : ScalarCoupleable(const MooseObject * moose_object); 40 : 41 : /** 42 : * Get the list of coupled scalar variables 43 : * @return The list of coupled variables 44 : */ 45 8551864 : const std::vector<MooseVariableScalar *> & getCoupledMooseScalarVars() 46 : { 47 8551864 : return _coupled_moose_scalar_vars; 48 : } 49 : 50 17885 : const std::set<TagID> & getScalarVariableCoupleableVectorTags() const 51 : { 52 17885 : return _sc_coupleable_vector_tags; 53 : } 54 : 55 17885 : const std::set<TagID> & getScalarVariableCoupleableMatrixTags() const 56 : { 57 17885 : return _sc_coupleable_matrix_tags; 58 : } 59 : 60 : protected: 61 : /** 62 : * Returns true if a variables has been coupled_as name. 63 : * @param var_name The of the coupled variable 64 : * @param i By default 0, in general the index to test in a vector of MooseVariable pointers. 65 : */ 66 : bool isCoupledScalar(const std::string & var_name, unsigned int i = 0) const; 67 : 68 : /** 69 : * Return the number of components to the coupled scalar variable 70 : * @param var_name The of the coupled variable 71 : */ 72 : unsigned int coupledScalarComponents(const std::string & var_name) const; 73 : 74 : /** 75 : * Returns the index for a scalar coupled variable by name 76 : * @param var_name Name of coupled variable 77 : * @param comp Component number for vector of coupled variables 78 : * @return Index of coupled variable 79 : */ 80 : unsigned int coupledScalar(const std::string & var_name, unsigned int comp = 0) const; 81 : 82 : /** 83 : * Returns the order for a scalar coupled variable by name 84 : * @param var_name Name of coupled variable 85 : * @param comp Component number for vector of coupled variables 86 : * @return Order of coupled variable 87 : */ 88 : libMesh::Order coupledScalarOrder(const std::string & var_name, unsigned int comp = 0) const; 89 : 90 : /** 91 : * Returns value of a scalar coupled variable 92 : * @param var_name Name of coupled variable 93 : * @param comp Component number for vector of coupled variables 94 : * @return Reference to a VariableValue for the coupled variable 95 : */ 96 : const VariableValue & coupledScalarValue(const std::string & var_name, 97 : unsigned int comp = 0) const; 98 : 99 : /** 100 : * Returns AD value of a scalar coupled variable 101 : * @param var_name Name of coupled variable 102 : * @param comp Component number for vector of coupled variables 103 : * @return Reference to a ADVariableValue for the coupled variable 104 : */ 105 : const ADVariableValue & adCoupledScalarValue(const std::string & var_name, 106 : unsigned int comp = 0) const; 107 : 108 : /** 109 : * Returns value of a coupled scalar variable for use in templated automatic differentiation 110 : * classes 111 : * @param var_name Name of coupled scalar variable 112 : * @param comp Component number for vector of coupled scalar variables 113 : * @return Reference to a GenericVariableValue for the coupled scalar variable 114 : */ 115 : template <bool is_ad> 116 : const GenericVariableValue<is_ad> & coupledGenericScalarValue(const std::string & var_name, 117 : unsigned int comp = 0) const; 118 : 119 : /** 120 : * Returns value of a scalar coupled variable 121 : * @param var_name Name of coupled variable 122 : * @param tag Tag ID of coupled vector ; 123 : * @param comp Component number for vector of coupled variables 124 : * @return Reference to a VariableValue for the coupled variable 125 : */ 126 : const VariableValue & 127 : coupledVectorTagScalarValue(const std::string & var_name, TagID tag, unsigned int comp = 0) const; 128 : 129 : /** 130 : * Returns value of a scalar coupled variable 131 : * @param var_name Name of coupled variable 132 : * @param tag Tag ID of coupled matrix; 133 : * @param comp Component number for vector of coupled variables 134 : * @return Reference to a VariableValue for the coupled variable 135 : */ 136 : const VariableValue & 137 : coupledMatrixTagScalarValue(const std::string & var_name, TagID tag, unsigned int comp = 0) const; 138 : 139 : /** 140 : * Returns the old (previous time step) value of a scalar coupled variable 141 : * @param var_name Name of coupled variable 142 : * @param comp Component number for vector of coupled variables 143 : * @return Reference to a old VariableValue for the coupled variable 144 : */ 145 : const VariableValue & coupledScalarValueOld(const std::string & var_name, 146 : unsigned int comp = 0) const; 147 : 148 : /** 149 : * Returns the older (two time steps previous) value of a scalar coupled variable 150 : * @param var_name Name of coupled variable 151 : * @param comp Component number for vector of coupled variables 152 : * @return Reference to a older VariableValue for the coupled variable 153 : */ 154 : const VariableValue & coupledScalarValueOlder(const std::string & var_name, 155 : unsigned int comp = 0) const; 156 : /** 157 : * Returns the time derivative of a scalar coupled variable 158 : * @param var_name Name of coupled variable 159 : * @param comp Component number for vector of coupled variables 160 : * @return Reference to a time derivative VariableValue for the coupled variable 161 : */ 162 : const VariableValue & coupledScalarDot(const std::string & var_name, unsigned int comp = 0) const; 163 : 164 : /** 165 : * Returns the time derivative of a scalar coupled variable, including its dependence on the 166 : * nonlinear degrees of freedom through automatic differentiation 167 : * @param var_name Name of coupled variable 168 : * @param comp Component number for vector of coupled variables 169 : * @return Reference to the time derivative at quadrature points for the coupled variable, 170 : * including automatic differentiation information 171 : */ 172 : const ADVariableValue & adCoupledScalarDot(const std::string & var_name, 173 : unsigned int comp = 0) const; 174 : 175 : /** 176 : * Returns the second time derivative of a scalar coupled variable 177 : * @param var_name Name of coupled variable 178 : * @param comp Component number for vector of coupled variables 179 : * @return Reference to a time derivative VariableValue for the coupled variable 180 : */ 181 : const VariableValue & coupledScalarDotDot(const std::string & var_name, 182 : unsigned int comp = 0) const; 183 : 184 : /** 185 : * Returns the old time derivative of a scalar coupled variable 186 : * @param var_name Name of coupled variable 187 : * @param comp Component number for vector of coupled variables 188 : * @return Reference to a time derivative VariableValue for the coupled variable 189 : */ 190 : const VariableValue & coupledScalarDotOld(const std::string & var_name, 191 : unsigned int comp = 0) const; 192 : 193 : /** 194 : * Returns the old second time derivative of a scalar coupled variable 195 : * @param var_name Name of coupled variable 196 : * @param comp Component number for vector of coupled variables 197 : * @return Reference to a time derivative VariableValue for the coupled variable 198 : */ 199 : const VariableValue & coupledScalarDotDotOld(const std::string & var_name, 200 : unsigned int comp = 0) const; 201 : 202 : /** 203 : * Time derivative of a scalar coupled variable with respect to the coefficients 204 : * @param var_name Name of coupled variable 205 : * @param comp Component number for vector of coupled variables 206 : * @return Reference to a VariableValue containing the time derivative of the coupled variable 207 : * with respect to the coefficients 208 : */ 209 : const VariableValue & coupledScalarDotDu(const std::string & var_name, 210 : unsigned int comp = 0) const; 211 : 212 : /** 213 : * Second time derivative of a scalar coupled variable with respect to the coefficients 214 : * @param var_name Name of coupled variable 215 : * @param comp Component number for vector of coupled variables 216 : * @return Reference to a VariableValue containing the time derivative of the coupled variable 217 : * with respect to the coefficients 218 : */ 219 : const VariableValue & coupledScalarDotDotDu(const std::string & var_name, 220 : unsigned int comp = 0) const; 221 : 222 : /** 223 : * Extract pointer to a scalar coupled variable 224 : * @param var_name Name of parameter desired 225 : * @param comp Component number of multiple coupled variables 226 : * @return Pointer to the desired variable 227 : */ 228 : const MooseVariableScalar * getScalarVar(const std::string & var_name, unsigned int comp) const; 229 : 230 : protected: 231 : // Reference to FEProblemBase 232 : FEProblemBase & _sc_fe_problem; 233 : 234 : /// Thread ID of the thread using this object 235 : const THREAD_ID _sc_tid; 236 : 237 : /// Scalar zero 238 : const Real & _real_zero; 239 : 240 : /// Zero value of a scalar variable 241 : const VariableValue & _scalar_zero; 242 : 243 : /// Zero point 244 : const Point & _point_zero; 245 : 246 : private: 247 : /** 248 : * Helper method to return (and insert if necessary) the default value 249 : * for an uncoupled variable. 250 : * @param var_name the name of the variable for which to retrieve a default value 251 : * @return VariableValue * a pointer to the associated VariableValue. 252 : */ 253 : const VariableValue * getDefaultValue(const std::string & var_name) const; 254 : 255 : /** 256 : * Helper method to return (and insert if necessary) the AD default value 257 : * for an uncoupled variable. 258 : * @param var_name the name of the variable for which to retrieve a default value 259 : * @return ADVariableValue * a pointer to the associated ADVariableValue. 260 : */ 261 : const ADVariableValue * getADDefaultValue(const std::string & var_name) const; 262 : 263 : /** 264 : * Check that the right kind of variable is being coupled in 265 : * 266 : * @param var_name The name of the coupled variable 267 : */ 268 : void checkVar(const std::string & var_name) const; 269 : 270 : /** 271 : * Checks to make sure that the current Executioner has set "_is_transient" when old/older values 272 : * are coupled in. 273 : * @param name the name of the variable 274 : * @param fn_name The name of the function that called this method - used in the error message 275 : */ 276 : void validateExecutionerType(const std::string & name, const std::string & fn_name) const; 277 : 278 : // Reference to the interface's input parameters 279 : const InputParameters & _sc_parameters; 280 : 281 : /// The name of the object this interface is part of 282 : const std::string & _sc_name; 283 : 284 : /// True if implicit value is required 285 : const bool _sc_is_implicit; 286 : 287 : /// Coupled vars whose values we provide 288 : std::unordered_map<std::string, std::vector<MooseVariableScalar *>> _coupled_scalar_vars; 289 : 290 : /// Will hold the default value for optional coupled scalar variables. 291 : mutable std::unordered_map<std::string, std::unique_ptr<VariableValue>> _default_value; 292 : 293 : /// Will hold the default AD value for optional coupled scalar variables. 294 : mutable std::unordered_map<std::string, std::unique_ptr<ADVariableValue>> _dual_default_value; 295 : 296 : /// Vector of coupled variables 297 : std::vector<MooseVariableScalar *> _coupled_moose_scalar_vars; 298 : 299 : /// Field variables coupled into this object (for error checking) 300 : std::unordered_map<std::string, std::vector<MooseVariableFieldBase *>> _sc_coupled_vars; 301 : 302 : /// The scalar coupleable vector tags 303 : mutable std::set<TagID> _sc_coupleable_vector_tags; 304 : /// The scalar coupleable matrix tags 305 : mutable std::set<TagID> _sc_coupleable_matrix_tags; 306 : };