LCOV - code coverage report
Current view: top level - src/distributions - TruncatedNormal.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: f45d79 Lines: 32 34 94.1 %
Date: 2025-07-25 05:00:46 Functions: 8 8 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 "TruncatedNormal.h"
      11             : 
      12             : registerMooseObject("StochasticToolsApp", TruncatedNormal);
      13             : 
      14             : InputParameters
      15          96 : TruncatedNormal::validParams()
      16             : {
      17          96 :   InputParameters params = Normal::validParams();
      18          96 :   params.addClassDescription("Truncated normal distribution");
      19         192 :   params.addParam<Real>(
      20         192 :       "lower_bound", -std::numeric_limits<Real>::max(), "Lower bound of the distribution ");
      21         192 :   params.addParam<Real>(
      22         192 :       "upper_bound", std::numeric_limits<Real>::max(), "Upper bound of the distribution ");
      23          96 :   return params;
      24           0 : }
      25             : 
      26          48 : TruncatedNormal::TruncatedNormal(const InputParameters & parameters)
      27             :   : Normal(parameters),
      28          48 :     _lower_bound(getParam<Real>("lower_bound")),
      29         144 :     _upper_bound(getParam<Real>("upper_bound"))
      30             : {
      31          48 :   if (_lower_bound >= _upper_bound)
      32           0 :     mooseError("lower_bound in truncated normal distribution must be less than upper_bound.");
      33          48 : }
      34             : 
      35             : Real
      36          76 : TruncatedNormal::pdf(const Real & x,
      37             :                      const Real & mean,
      38             :                      const Real & std_dev,
      39             :                      const Real & lower_bound,
      40             :                      const Real & upper_bound)
      41             : {
      42          76 :   if (x <= lower_bound || x >= upper_bound)
      43             :     return 0.0;
      44             :   else
      45          76 :     return (Normal::pdf(x, mean, std_dev)) /
      46          76 :            (Normal::cdf(upper_bound, mean, std_dev) - Normal::cdf(lower_bound, mean, std_dev));
      47             : }
      48             : 
      49             : Real
      50          16 : TruncatedNormal::cdf(const Real & x,
      51             :                      const Real & mean,
      52             :                      const Real & std_dev,
      53             :                      const Real & lower_bound,
      54             :                      const Real & upper_bound)
      55             : {
      56             : 
      57          16 :   if (x <= lower_bound || x >= upper_bound)
      58             :     return 0.0;
      59             :   else
      60          16 :     return (Normal::cdf(x, mean, std_dev) - Normal::cdf(lower_bound, mean, std_dev)) /
      61          16 :            (Normal::cdf(upper_bound, mean, std_dev) - Normal::cdf(lower_bound, mean, std_dev));
      62             : }
      63             : 
      64             : Real
      65         616 : TruncatedNormal::quantile(const Real & p,
      66             :                           const Real & mean,
      67             :                           const Real & std_dev,
      68             :                           const Real & lower_bound,
      69             :                           const Real & upper_bound)
      70             : {
      71         616 :   return Normal::quantile(
      72         616 :       Normal::cdf(lower_bound, mean, std_dev) +
      73         616 :           p * (Normal::cdf(upper_bound, mean, std_dev) - Normal::cdf(lower_bound, mean, std_dev)),
      74             :       mean,
      75         616 :       std_dev);
      76             : }
      77             : 
      78             : Real
      79          16 : TruncatedNormal::pdf(const Real & x) const
      80             : {
      81          16 :   return pdf(x, _mean, _standard_deviation, _lower_bound, _upper_bound);
      82             : }
      83             : 
      84             : Real
      85          16 : TruncatedNormal::cdf(const Real & x) const
      86             : {
      87          16 :   return cdf(x, _mean, _standard_deviation, _lower_bound, _upper_bound);
      88             : }
      89             : 
      90             : Real
      91         616 : TruncatedNormal::quantile(const Real & p) const
      92             : {
      93         616 :   return quantile(p, _mean, _standard_deviation, _lower_bound, _upper_bound);
      94             : }

Generated by: LCOV version 1.14