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 64 : 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 64 : const THREAD_ID tid) 23 64 : : _sim(sim), 24 64 : _feproblem(feproblem), 25 64 : _function_str(function_str), 26 64 : _vars(vars), 27 64 : _vals_input(vals), 28 64 : _tid(tid) 29 : { 30 64 : initialize(); 31 : 32 64 : _function_ptr = std::make_unique<libMesh::ParsedFunction<Real, RealGradient>>( 33 64 : _function_str, &_vars, &_initial_vals); 34 : 35 164 : for (auto & v : _vars) 36 100 : _addr.push_back(&_function_ptr->getVarAddress(v)); 37 64 : } 38 : 39 : Real 40 378 : THMParsedFunctionWrapper::evaluate(Real t, const Point & p) 41 : { 42 378 : update(); 43 378 : updateFunctionValues(t, p); 44 378 : updateControlDataValues(); 45 378 : return (*_function_ptr)(p, t); 46 : } 47 : 48 : void 49 64 : THMParsedFunctionWrapper::initialize() 50 : { 51 164 : for (unsigned int i = 0; i < _vals_input.size(); ++i) 52 : { 53 100 : if (_sim.hasControlData<Real>(_vals_input[i])) 54 : { 55 62 : ControlData<Real> * cd_val = _sim.getControlData<Real>(_vals_input[i]); 56 62 : _initial_vals.push_back(cd_val->get()); 57 62 : _cd_real_vals.push_back(cd_val); 58 62 : _cd_real_index.push_back(i); 59 : } 60 38 : else if (_sim.hasControlData<bool>(_vals_input[i])) 61 : { 62 8 : ControlData<bool> * cd_val = _sim.getControlData<bool>(_vals_input[i]); 63 8 : _initial_vals.push_back(cd_val->get()); 64 8 : _cd_bool_vals.push_back(cd_val); 65 8 : _cd_bool_index.push_back(i); 66 : } 67 30 : else if (_feproblem.hasScalarVariable(_vals_input[i])) 68 : { 69 8 : const VariableValue & scalar_val = _feproblem.getScalarVariable(_tid, _vals_input[i]).sln(); 70 8 : _initial_vals.push_back(0); 71 8 : _scalar_vals.push_back(&scalar_val); 72 8 : _scalar_index.push_back(i); 73 : } 74 22 : else if (_feproblem.hasFunction(_vals_input[i])) 75 : { 76 22 : const Function & fn = _feproblem.getFunction(_vals_input[i], _tid); 77 22 : _initial_vals.push_back(0); 78 22 : _functions.push_back(&fn); 79 22 : _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 64 : } 88 : 89 : void 90 378 : THMParsedFunctionWrapper::update() 91 : { 92 400 : for (unsigned int i = 0; i < _scalar_index.size(); ++i) 93 22 : (*_addr[_scalar_index[i]]) = (*_scalar_vals[i])[0]; 94 378 : } 95 : 96 : void 97 378 : THMParsedFunctionWrapper::updateFunctionValues(Real t, const Point & pt) 98 : { 99 466 : for (unsigned int i = 0; i < _function_index.size(); ++i) 100 88 : (*_addr[_function_index[i]]) = _functions[i]->value(t, pt); 101 378 : } 102 : 103 : void 104 378 : THMParsedFunctionWrapper::updateControlDataValues() 105 : { 106 690 : for (unsigned int i = 0; i < _cd_real_index.size(); ++i) 107 312 : (*_addr[_cd_real_index[i]]) = _cd_real_vals[i]->get(); 108 : 109 400 : for (unsigned int i = 0; i < _cd_bool_index.size(); ++i) 110 22 : (*_addr[_cd_bool_index[i]]) = static_cast<Real>(_cd_bool_vals[i]->get()); 111 378 : }