LCOV - code coverage report
Current view: top level - src/distributions - TruncatedNormal.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: #32971 (54bef8) with base c6cf66 Lines: 32 34 94.1 %
Date: 2026-05-29 20:40:35 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          40 : TruncatedNormal::validParams()
      16             : {
      17          40 :   InputParameters params = Normal::validParams();
      18          40 :   params.addClassDescription("Truncated normal distribution");
      19          80 :   params.addParam<Real>(
      20          80 :       "lower_bound", -std::numeric_limits<Real>::max(), "Lower bound of the distribution ");
      21          80 :   params.addParam<Real>(
      22          80 :       "upper_bound", std::numeric_limits<Real>::max(), "Upper bound of the distribution ");
      23          40 :   return params;
      24           0 : }
      25             : 
      26          20 : TruncatedNormal::TruncatedNormal(const InputParameters & parameters)
      27             :   : Normal(parameters),
      28          20 :     _lower_bound(getParam<Real>("lower_bound")),
      29          60 :     _upper_bound(getParam<Real>("upper_bound"))
      30             : {
      31          20 :   if (_lower_bound >= _upper_bound)
      32           0 :     mooseError("lower_bound in truncated normal distribution must be less than upper_bound.");
      33          20 : }
      34             : 
      35             : Real
      36          37 : 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          37 :   if (x <= lower_bound || x >= upper_bound)
      43             :     return 0.0;
      44             :   else
      45          37 :     return (Normal::pdf(x, mean, std_dev)) /
      46          37 :            (Normal::cdf(upper_bound, mean, std_dev) - Normal::cdf(lower_bound, mean, std_dev));
      47             : }
      48             : 
      49             : Real
      50           7 : 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           7 :   if (x <= lower_bound || x >= upper_bound)
      58             :     return 0.0;
      59             :   else
      60           7 :     return (Normal::cdf(x, mean, std_dev) - Normal::cdf(lower_bound, mean, std_dev)) /
      61           7 :            (Normal::cdf(upper_bound, mean, std_dev) - Normal::cdf(lower_bound, mean, std_dev));
      62             : }
      63             : 
      64             : Real
      65         277 : 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         277 :   return Normal::quantile(
      72         277 :       Normal::cdf(lower_bound, mean, std_dev) +
      73         277 :           p * (Normal::cdf(upper_bound, mean, std_dev) - Normal::cdf(lower_bound, mean, std_dev)),
      74             :       mean,
      75         277 :       std_dev);
      76             : }
      77             : 
      78             : Real
      79           7 : TruncatedNormal::pdf(const Real & x) const
      80             : {
      81           7 :   return pdf(x, _mean, _standard_deviation, _lower_bound, _upper_bound);
      82             : }
      83             : 
      84             : Real
      85           7 : TruncatedNormal::cdf(const Real & x) const
      86             : {
      87           7 :   return cdf(x, _mean, _standard_deviation, _lower_bound, _upper_bound);
      88             : }
      89             : 
      90             : Real
      91         277 : TruncatedNormal::quantile(const Real & p) const
      92             : {
      93         277 :   return quantile(p, _mean, _standard_deviation, _lower_bound, _upper_bound);
      94             : }

Generated by: LCOV version 1.14