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 40 : 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 40 : const THREAD_ID tid) 25 40 : : _moose_app(moose_app), 26 40 : _fe_problem(fe_problem), 27 40 : _function_str(function_str), 28 40 : _symbol_names(symbol_names), 29 40 : _symbol_values(symbol_values), 30 40 : _tid(tid), 31 40 : _chain_control_data_system(_moose_app.getChainControlDataSystem()) 32 : { 33 40 : initializeFunctionInputs(); 34 : 35 36 : _function_ptr = std::make_unique<ParsedFunction<Real, RealGradient>>( 36 36 : _function_str, &_symbol_names, &_initial_values); 37 : 38 120 : for (const auto & symbol_name : _symbol_names) 39 84 : _input_values.push_back(&_function_ptr->getVarAddress(symbol_name)); 40 36 : } 41 : 42 : Real 43 1169 : ChainControlParsedFunctionWrapper::evaluate(Real t, const Point & p) 44 : { 45 1169 : updateScalarVariableValues(); 46 1169 : updateFunctionValues(t, p); 47 1169 : updateChainControlDataValues(); 48 1169 : return (*_function_ptr)(p, t); 49 : } 50 : 51 : void 52 40 : ChainControlParsedFunctionWrapper::initializeFunctionInputs() 53 : { 54 124 : for (const auto i : index_range(_symbol_values)) 55 : { 56 88 : 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 64 : else if (_chain_control_data_system.hasChainControlDataOfType<bool>(_symbol_values[i])) 64 : { 65 24 : auto & data = _chain_control_data_system.getChainControlData<bool>(_symbol_values[i]); 66 24 : _initial_values.push_back(data.get()); 67 24 : _bool_control_data_values.push_back(&data); 68 24 : _bool_control_data_indices.push_back(i); 69 : } 70 40 : 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 28 : 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 16 : 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 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 36 : } 99 : 100 : void 101 1169 : ChainControlParsedFunctionWrapper::updateScalarVariableValues() 102 : { 103 1214 : for (const auto i : index_range(_scalar_indices)) 104 45 : (*_input_values[_scalar_indices[i]]) = (*_scalar_values[i])[0]; 105 1169 : } 106 : 107 : void 108 1169 : ChainControlParsedFunctionWrapper::updateFunctionValues(Real t, const Point & pt) 109 : { 110 1214 : for (const auto i : index_range(_function_indices)) 111 45 : (*_input_values[_function_indices[i]]) = _function_values[i]->value(t, pt); 112 1169 : } 113 : 114 : void 115 1169 : ChainControlParsedFunctionWrapper::updateChainControlDataValues() 116 : { 117 1776 : 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 1259 : for (const auto i : index_range(_bool_control_data_indices)) 121 90 : (*_input_values[_bool_control_data_indices[i]]) = 122 90 : static_cast<Real>(_bool_control_data_values[i]->get()); 123 1169 : }