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 43 : 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 43 : const THREAD_ID tid) 25 43 : : _moose_app(moose_app), 26 43 : _fe_problem(fe_problem), 27 43 : _function_str(function_str), 28 43 : _symbol_names(symbol_names), 29 43 : _symbol_values(symbol_values), 30 43 : _tid(tid), 31 43 : _chain_control_data_system(_moose_app.getChainControlDataSystem()) 32 : { 33 43 : initializeFunctionInputs(); 34 : 35 39 : _function_ptr = std::make_unique<ParsedFunction<Real, RealGradient>>( 36 39 : _function_str, &_symbol_names, &_initial_values); 37 : 38 130 : for (const auto & symbol_name : _symbol_names) 39 91 : _input_values.push_back(&_function_ptr->getVarAddress(symbol_name)); 40 39 : } 41 : 42 : Real 43 1275 : ChainControlParsedFunctionWrapper::evaluate(Real t, const Point & p) 44 : { 45 1275 : updateScalarVariableValues(); 46 1275 : updateFunctionValues(t, p); 47 1275 : updateChainControlDataValues(); 48 1275 : return (*_function_ptr)(p, t); 49 : } 50 : 51 : void 52 43 : ChainControlParsedFunctionWrapper::initializeFunctionInputs() 53 : { 54 134 : for (const auto i : index_range(_symbol_values)) 55 : { 56 95 : if (_chain_control_data_system.hasChainControlDataOfType<Real>(_symbol_values[i])) 57 : { 58 26 : auto & data = _chain_control_data_system.getChainControlData<Real>(_symbol_values[i]); 59 26 : _initial_values.push_back(data.get()); 60 26 : _real_control_data_values.push_back(&data); 61 26 : _real_control_data_indices.push_back(i); 62 : } 63 69 : else if (_chain_control_data_system.hasChainControlDataOfType<bool>(_symbol_values[i])) 64 : { 65 26 : auto & data = _chain_control_data_system.getChainControlData<bool>(_symbol_values[i]); 66 26 : _initial_values.push_back(data.get()); 67 26 : _bool_control_data_values.push_back(&data); 68 26 : _bool_control_data_indices.push_back(i); 69 : } 70 43 : else if (_fe_problem.hasScalarVariable(_symbol_values[i])) 71 : { 72 : const VariableValue & scalar_val = 73 13 : _fe_problem.getScalarVariable(_tid, _symbol_values[i]).sln(); 74 13 : _initial_values.push_back(0); 75 13 : _scalar_values.push_back(&scalar_val); 76 13 : _scalar_indices.push_back(i); 77 : } 78 30 : else if (_fe_problem.hasFunction(_symbol_values[i])) 79 : { 80 13 : const Function & function = _fe_problem.getFunction(_symbol_values[i], _tid); 81 13 : _initial_values.push_back(0); 82 13 : _function_values.push_back(&function); 83 13 : _function_indices.push_back(i); 84 : } 85 17 : else if (MooseUtils::isFloat(_symbol_values[i])) 86 : { 87 13 : _initial_values.push_back(MooseUtils::convert<Real>(_symbol_values[i], true)); 88 : } 89 : else 90 4 : mooseError("Invalid 'symbol_values' entry '", 91 4 : _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 39 : } 99 : 100 : void 101 1275 : ChainControlParsedFunctionWrapper::updateScalarVariableValues() 102 : { 103 1324 : for (const auto i : index_range(_scalar_indices)) 104 49 : (*_input_values[_scalar_indices[i]]) = (*_scalar_values[i])[0]; 105 1275 : } 106 : 107 : void 108 1275 : ChainControlParsedFunctionWrapper::updateFunctionValues(Real t, const Point & pt) 109 : { 110 1324 : for (const auto i : index_range(_function_indices)) 111 49 : (*_input_values[_function_indices[i]]) = _function_values[i]->value(t, pt); 112 1275 : } 113 : 114 : void 115 1275 : ChainControlParsedFunctionWrapper::updateChainControlDataValues() 116 : { 117 1937 : for (const auto i : index_range(_real_control_data_indices)) 118 662 : (*_input_values[_real_control_data_indices[i]]) = _real_control_data_values[i]->get(); 119 : 120 1373 : for (const auto i : index_range(_bool_control_data_indices)) 121 98 : (*_input_values[_bool_control_data_indices[i]]) = 122 98 : static_cast<Real>(_bool_control_data_values[i]->get()); 123 1275 : }