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 "Gaussian.h" 11 : #include "Normal.h" 12 : #include "DelimitedFileReader.h" 13 : 14 : registerMooseObject("StochasticToolsApp", Gaussian); 15 : 16 : InputParameters 17 350 : Gaussian::validParams() 18 : { 19 350 : InputParameters params = LikelihoodFunctionBase::validParams(); 20 350 : params.addClassDescription( 21 : "Gaussian likelihood function evaluating the model goodness against experiments."); 22 700 : params.addParam<bool>("log_likelihood", true, "Compute log-likelihood or likelihood."); 23 700 : params.addRequiredParam<ReporterName>( 24 : "noise", "Experimental noise plus model deviations against experiments."); 25 700 : params.addParam<FileName>("file_name", "Name of the CSV file with experimental values."); 26 700 : params.addParam<std::string>( 27 : "file_column_name", "Name of column in CSV file to use, by default first column is used."); 28 700 : params.addParam<std::vector<Real>>( 29 : "exp_values", "User-specified experimental values when CSV file is not provided."); 30 350 : return params; 31 0 : } 32 : 33 176 : Gaussian::Gaussian(const InputParameters & parameters) 34 : : LikelihoodFunctionBase(parameters), 35 : ReporterInterface(this), 36 176 : _log_likelihood(getParam<bool>("log_likelihood")), 37 352 : _noise(getReporterValue<Real>("noise")) 38 : { 39 352 : if (isParamValid("exp_values") && isParamValid("file_name")) 40 0 : paramError("exp_values", "exp_values and file_name both cannot be set at the same time."); 41 352 : else if (isParamValid("file_name")) 42 : { 43 352 : MooseUtils::DelimitedFileReader reader(getParam<FileName>("file_name")); 44 176 : reader.read(); 45 352 : if (isParamValid("file_column_name")) 46 0 : _exp_values = reader.getData(getParam<std::string>("file_column_name")); 47 : else 48 176 : _exp_values = reader.getData(0); 49 176 : } 50 0 : else if (isParamValid("exp_values")) 51 0 : _exp_values = getParam<std::vector<Real>>("exp_values"); 52 : else 53 0 : mooseError("Either 'exp_values' or 'file_name' parameters must be specified to represent " 54 : "experimental data."); 55 176 : } 56 : 57 : Real 58 3140 : Gaussian::function(const std::vector<Real> & exp, 59 : const std::vector<Real> & model, 60 : const Real & noise, 61 : const bool & log_likelihood) 62 : { 63 : Real result = 0.0; 64 : Real val1; 65 9440 : for (unsigned i = 0; i < exp.size(); ++i) 66 : { 67 6300 : val1 = Normal::pdf(exp[i], model[i], noise); 68 6300 : val1 = std::log(val1); 69 6300 : result += val1; 70 : } 71 3140 : if (!log_likelihood) 72 10 : result = std::exp(result); 73 3140 : return result; 74 : } 75 : 76 : Real 77 3140 : Gaussian::function(const std::vector<Real> & x) const 78 : { 79 3140 : return function(_exp_values, x, _noise, _log_likelihood); 80 : }