DFNgen  2.0
DFN Model Generator
expDist.cpp
Go to the documentation of this file.
1 #include "expDist.h"
2 #include <cmath>
3 #include <iostream>
4 
5 /*
6  Exponential Distribution Class
7 
8  This class was created to allow the user to specify
9  the min and max values received from the exponential
10  distrubution function.
11 */
12 
13 /***************************************************************************/
14 /**************************** Constructor **********************************/
21 ExpDist::ExpDist(double maxDecimal, std::mt19937_64 &_generator) : generator(_generator) {
22  maxInput = maxDecimal;
23  generator = _generator;
24 }
25 
26 
27 /***************************************************************************/
28 /************** Random Uniform Random Number Generator *********************/
33 double ExpDist::unifRandom(double min, double max) {
34  return ((max-min) * double(generator())/generator.max() + min);
35 }
36 
37 
38 /***************************************************************************/
39 /***************************************************************************/
40 // Overloaded Function
46 double ExpDist::getValue(double lambda, double rv) {
47 
48  if (rv > 1) {
49  std::cout << "ERROR: Attempted to input random value of greater than 1 to the exponential "
50  << "distribution class's getValue() function. Input must be on [0,1] interval.\n";
51  exit(1);
52  }
53 
54  // Using inverse CDF
55  if (rv != 1) {
56  return -std::log(1-rv)/lambda;
57  }
58  else {
59  return -std::log(1-maxInput)/lambda;
60  }
61 }
62 
63 
64 /***************************************************************************/
65 /***************************************************************************/
66 // Overloaded Function
81 double ExpDist::getValue(double lambda, double minVal, double maxVal) {
82  // Uniform distrubution on [minVal, maxVal)
83  if ( maxVal > 1 || minVal > 1) {
84  // Passing 1 into exp. distribution will reuturn inf
85  std::cout << "ERROR: Passed min, or max, input value of greater than 1 to getValue()"
86  << " in expDist.cpp. Input must be in [0,1] interval.\n";
87  exit(1);
88  }
89 
90  double randVar = unifRandom(minVal, maxVal);
91 
92  // Using inverse CDF
93  // If randVar is 1, this function returns inf
94  if (randVar != 1) {
95  return -std::log(1-randVar)/lambda;
96  }
97  else {
98  return -std::log(1-maxInput)/lambda;
99  }
100 }
101 
102 
103 /***************************************************************************/
104 /******************* Get Max Value Possible ******************************/
115 double ExpDist::getMaxValue(double lambda) {
116  return -std::log(1-maxInput)/lambda;
117 }
118 
119 
120 /***************************************************************************/
121 /********* Compute Input to Distribution for a Certain Output ************/
129 double ExpDist::computeInput(double output, double lambda) {
130  return 1.0 - std::exp(-lambda*output);
131 }
132 
133 
double getValue(double lambda, double rv)
Definition: expDist.cpp:46
double unifRandom(double, double)
Definition: expDist.cpp:33
double getMaxValue(double lambda)
Definition: expDist.cpp:115
double maxInput
Definition: expDist.h:19
double computeInput(double output, double lambda)
Definition: expDist.cpp:129
std::mt19937_64 & generator
Definition: expDist.h:25
ExpDist(double maxDecimal, std::mt19937_64 &_generator)
Definition: expDist.cpp:21