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 : #include "ChainControlParsedFunctionWrapper.h" 11 : #include "MooseApp.h" 12 : #include "FEProblem.h" 13 : #include "MooseVariableScalar.h" 14 : #include "Function.h" 15 : #include "MooseUtils.h" 16 : #include "ChainControlDataSystem.h" 17 : 18 51 : ChainControlParsedFunctionWrapper::ChainControlParsedFunctionWrapper( 19 : MooseApp & moose_app, 20 : FEProblemBase & fe_problem, 21 : const std::string & function_str, 22 : const std::vector<std::string> & symbol_names, 23 : const std::vector<std::string> & symbol_values, 24 51 : const THREAD_ID tid) 25 51 : : _moose_app(moose_app), 26 51 : _fe_problem(fe_problem), 27 51 : _function_str(function_str), 28 51 : _symbol_names(symbol_names), 29 51 : _symbol_values(symbol_values), 30 51 : _tid(tid), 31 51 : _chain_control_data_system(_moose_app.getChainControlDataSystem()) 32 : { 33 51 : initializeFunctionInputs(); 34 : 35 48 : _function_ptr = std::make_unique<ParsedFunction<Real, RealGradient>>( 36 48 : _function_str, &_symbol_names, &_initial_values); 37 : 38 156 : for (const auto & symbol_name : _symbol_names) 39 108 : _input_values.push_back(&_function_ptr->getVarAddress(symbol_name)); 40 48 : } 41 : 42 : Real 43 1214 : ChainControlParsedFunctionWrapper::evaluate(Real t, const Point & p) 44 : { 45 1214 : updateScalarVariableValues(); 46 1214 : updateFunctionValues(t, p); 47 1214 : updateChainControlDataValues(); 48 1214 : return (*_function_ptr)(p, t); 49 : } 50 : 51 : void 52 51 : ChainControlParsedFunctionWrapper::initializeFunctionInputs() 53 : { 54 159 : for (const auto i : index_range(_symbol_values)) 55 : { 56 111 : if (_chain_control_data_system.hasChainControlDataOfType<Real>(_symbol_values[i])) 57 : { 58 24 : auto & data = _chain_control_data_system.getChainControlData<Real>(_symbol_values[i]); 59 24 : _initial_values.push_back(data.get()); 60 24 : _real_control_data_values.push_back(&data); 61 24 : _real_control_data_indices.push_back(i); 62 : } 63 87 : else if (_chain_control_data_system.hasChainControlDataOfType<bool>(_symbol_values[i])) 64 : { 65 48 : auto & data = _chain_control_data_system.getChainControlData<bool>(_symbol_values[i]); 66 48 : _initial_values.push_back(data.get()); 67 48 : _bool_control_data_values.push_back(&data); 68 48 : _bool_control_data_indices.push_back(i); 69 : } 70 39 : else if (_fe_problem.hasScalarVariable(_symbol_values[i])) 71 : { 72 : const VariableValue & scalar_val = 73 12 : _fe_problem.getScalarVariable(_tid, _symbol_values[i]).sln(); 74 12 : _initial_values.push_back(0); 75 12 : _scalar_values.push_back(&scalar_val); 76 12 : _scalar_indices.push_back(i); 77 : } 78 27 : else if (_fe_problem.hasFunction(_symbol_values[i])) 79 : { 80 12 : const Function & function = _fe_problem.getFunction(_symbol_values[i], _tid); 81 12 : _initial_values.push_back(0); 82 12 : _function_values.push_back(&function); 83 12 : _function_indices.push_back(i); 84 : } 85 15 : else if (MooseUtils::isFloat(_symbol_values[i])) 86 : { 87 12 : _initial_values.push_back(MooseUtils::convert<Real>(_symbol_values[i], true)); 88 : } 89 : else 90 3 : mooseError("Invalid 'symbol_values' entry '", 91 3 : _symbol_values[i], 92 : "'. Valid entries:\n" 93 : " - chain control data values (bool or Real)\n" 94 : " - function names\n" 95 : " - scalar variable names\n" 96 : " - constant values"); 97 : } 98 48 : } 99 : 100 : void 101 1214 : ChainControlParsedFunctionWrapper::updateScalarVariableValues() 102 : { 103 1259 : for (const auto i : index_range(_scalar_indices)) 104 45 : (*_input_values[_scalar_indices[i]]) = (*_scalar_values[i])[0]; 105 1214 : } 106 : 107 : void 108 1214 : ChainControlParsedFunctionWrapper::updateFunctionValues(Real t, const Point & pt) 109 : { 110 1259 : for (const auto i : index_range(_function_indices)) 111 45 : (*_input_values[_function_indices[i]]) = _function_values[i]->value(t, pt); 112 1214 : } 113 : 114 : void 115 1214 : ChainControlParsedFunctionWrapper::updateChainControlDataValues() 116 : { 117 1821 : for (const auto i : index_range(_real_control_data_indices)) 118 607 : (*_input_values[_real_control_data_indices[i]]) = _real_control_data_values[i]->get(); 119 : 120 1394 : for (const auto i : index_range(_bool_control_data_indices)) 121 180 : (*_input_values[_bool_control_data_indices[i]]) = 122 180 : static_cast<Real>(_bool_control_data_values[i]->get()); 123 1214 : }