LCOV - code coverage report
Current view: top level - src/likelihoods - Gaussian.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: f45d79 Lines: 30 36 83.3 %
Date: 2025-07-25 05:00:46 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          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         684 : Gaussian::validParams()
      18             : {
      19         684 :   InputParameters params = LikelihoodFunctionBase::validParams();
      20         684 :   params.addClassDescription(
      21             :       "Gaussian likelihood function evaluating the model goodness against experiments.");
      22        1368 :   params.addParam<bool>("log_likelihood", true, "Compute log-likelihood or likelihood.");
      23        1368 :   params.addRequiredParam<ReporterName>(
      24             :       "noise", "Experimental noise plus model deviations against experiments.");
      25        1368 :   params.addParam<FileName>("file_name", "Name of the CSV file with experimental values.");
      26        1368 :   params.addParam<std::string>(
      27             :       "file_column_name", "Name of column in CSV file to use, by default first column is used.");
      28        1368 :   params.addParam<std::vector<Real>>(
      29             :       "exp_values", "User-specified experimental values when CSV file is not provided.");
      30         684 :   return params;
      31           0 : }
      32             : 
      33         344 : Gaussian::Gaussian(const InputParameters & parameters)
      34             :   : LikelihoodFunctionBase(parameters),
      35             :     ReporterInterface(this),
      36         344 :     _log_likelihood(getParam<bool>("log_likelihood")),
      37         688 :     _noise(getReporterValue<Real>("noise"))
      38             : {
      39         688 :   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         688 :   else if (isParamValid("file_name"))
      42             :   {
      43         688 :     MooseUtils::DelimitedFileReader reader(getParam<FileName>("file_name"));
      44         344 :     reader.read();
      45         688 :     if (isParamValid("file_column_name"))
      46           0 :       _exp_values = reader.getData(getParam<std::string>("file_column_name"));
      47             :     else
      48         344 :       _exp_values = reader.getData(0);
      49         344 :   }
      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         344 : }
      56             : 
      57             : Real
      58        6120 : 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       18400 :   for (unsigned i = 0; i < exp.size(); ++i)
      66             :   {
      67       12280 :     val1 = Normal::pdf(exp[i], model[i], noise);
      68       12280 :     val1 = std::log(val1);
      69       12280 :     result += val1;
      70             :   }
      71        6120 :   if (!log_likelihood)
      72          20 :     result = std::exp(result);
      73        6120 :   return result;
      74             : }
      75             : 
      76             : Real
      77        6120 : Gaussian::function(const std::vector<Real> & x) const
      78             : {
      79        6120 :   return function(_exp_values, x, _noise, _log_likelihood);
      80             : }

Generated by: LCOV version 1.14