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 "THMParsedFunctionWrapper.h" 11 : #include "Simulation.h" 12 : #include "FEProblem.h" 13 : #include "MooseVariableScalar.h" 14 : #include "Function.h" 15 : #include "MooseUtils.h" 16 : 17 122 : THMParsedFunctionWrapper::THMParsedFunctionWrapper(Simulation & sim, 18 : FEProblemBase & feproblem, 19 : const std::string & function_str, 20 : const std::vector<std::string> & vars, 21 : const std::vector<std::string> & vals, 22 122 : const THREAD_ID tid) 23 122 : : _sim(sim), 24 122 : _feproblem(feproblem), 25 122 : _function_str(function_str), 26 122 : _vars(vars), 27 122 : _vals_input(vals), 28 122 : _tid(tid) 29 : { 30 122 : initialize(); 31 : 32 122 : _function_ptr = std::make_unique<libMesh::ParsedFunction<Real, RealGradient>>( 33 122 : _function_str, &_vars, &_initial_vals); 34 : 35 316 : for (auto & v : _vars) 36 194 : _addr.push_back(&_function_ptr->getVarAddress(v)); 37 122 : } 38 : 39 : Real 40 636 : THMParsedFunctionWrapper::evaluate(Real t, const Point & p) 41 : { 42 636 : update(); 43 636 : updateFunctionValues(t, p); 44 636 : updateControlDataValues(); 45 636 : return (*_function_ptr)(p, t); 46 : } 47 : 48 : void 49 122 : THMParsedFunctionWrapper::initialize() 50 : { 51 316 : for (unsigned int i = 0; i < _vals_input.size(); ++i) 52 : { 53 194 : if (_sim.hasControlData<Real>(_vals_input[i])) 54 : { 55 120 : ControlData<Real> * cd_val = _sim.getControlData<Real>(_vals_input[i]); 56 120 : _initial_vals.push_back(cd_val->get()); 57 120 : _cd_real_vals.push_back(cd_val); 58 120 : _cd_real_index.push_back(i); 59 : } 60 74 : else if (_sim.hasControlData<bool>(_vals_input[i])) 61 : { 62 18 : ControlData<bool> * cd_val = _sim.getControlData<bool>(_vals_input[i]); 63 18 : _initial_vals.push_back(cd_val->get()); 64 18 : _cd_bool_vals.push_back(cd_val); 65 18 : _cd_bool_index.push_back(i); 66 : } 67 56 : else if (_feproblem.hasScalarVariable(_vals_input[i])) 68 : { 69 18 : const VariableValue & scalar_val = _feproblem.getScalarVariable(_tid, _vals_input[i]).sln(); 70 18 : _initial_vals.push_back(0); 71 18 : _scalar_vals.push_back(&scalar_val); 72 18 : _scalar_index.push_back(i); 73 : } 74 38 : else if (_feproblem.hasFunction(_vals_input[i])) 75 : { 76 38 : const Function & fn = _feproblem.getFunction(_vals_input[i], _tid); 77 38 : _initial_vals.push_back(0); 78 38 : _functions.push_back(&fn); 79 38 : _function_index.push_back(i); 80 : } 81 : else 82 : { 83 0 : Real val = MooseUtils::convert<Real>(_vals_input[i], true); 84 0 : _initial_vals.push_back(val); 85 : } 86 : } 87 122 : } 88 : 89 : void 90 636 : THMParsedFunctionWrapper::update() 91 : { 92 680 : for (unsigned int i = 0; i < _scalar_index.size(); ++i) 93 44 : (*_addr[_scalar_index[i]]) = (*_scalar_vals[i])[0]; 94 636 : } 95 : 96 : void 97 636 : THMParsedFunctionWrapper::updateFunctionValues(Real t, const Point & pt) 98 : { 99 768 : for (unsigned int i = 0; i < _function_index.size(); ++i) 100 132 : (*_addr[_function_index[i]]) = _functions[i]->value(t, pt); 101 636 : } 102 : 103 : void 104 636 : THMParsedFunctionWrapper::updateControlDataValues() 105 : { 106 1184 : for (unsigned int i = 0; i < _cd_real_index.size(); ++i) 107 548 : (*_addr[_cd_real_index[i]]) = _cd_real_vals[i]->get(); 108 : 109 680 : for (unsigned int i = 0; i < _cd_bool_index.size(); ++i) 110 44 : (*_addr[_cd_bool_index[i]]) = static_cast<Real>(_cd_bool_vals[i]->get()); 111 636 : }