A monte carlo pin cell spectral code for nuclear engineering applications.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
integrate.h
Go to the documentation of this file.
1 
14 #ifndef INTEGRATE_H_
15 #define INTEGRATE_H_
16 
17 #ifdef __cplusplus
18 #include <stdio.h>
19 #include <string>
20 #include <stdlib.h>
21 #include <math.h>
22 #endif
23 
24 
34 typedef enum integrationSchemes {
50 
51 
61 template <typename T, typename U>
62 void cumulativeIntegral(T* x, T* y, U* cdf, int length,
63  integrationScheme scheme) {
64 
65  /* Calculate cumulative integral */
66  for (int i=1; i < length+1; i++)
67  cdf[i-1] = (U)integrate(x, y, i, scheme);
68 
69  return;
70 }
71 
72 
81 template <typename T>
82 double computeRiemannLeft(T* x, T* y, int length) {
83 
84  double integral = 0;
85  double delta_x = 0;
86 
87  for (int i = 0; i < length; i++) {
88  if (i < length - 1)
89  delta_x = x[i+1] - x[i];
90  else
91  delta_x = x[i] - x[i-1];
92 
93  integral += delta_x * y[i];
94  }
95 
96  return integral;
97 }
98 
99 
108 template <typename T>
109 double computeRiemannRight(T* x, T* y, int length) {
110 
111  double integral = 0;
112  double delta_x = 0;
113 
114  for (int i = 1; i < length-1; i++) {
115  delta_x = x[i] - x[i-1];
116  integral += delta_x * y[i];
117  }
118 
119  return integral;
120 }
121 
122 
131 template <typename T>
132 double computeRiemannCenter(T* x, T* y, int length) {
133 
134  double integral = 0;
135  double delta_x = 0;
136 
137  for (int i = 0; i < length; i++) {
138  if (i == 0)
139  delta_x = x[i+1] - x[i];
140  else if (i == length - 1)
141  delta_x = x[i] - x[i-1];
142  else
143  delta_x = (x[i] - x[i-1]) / 2.0 + (x[i+1] - x[i]) / 2.0;
144 
145  integral += delta_x * y[i];
146  }
147 
148  return integral;
149 }
150 
151 
160 template <typename T>
161 double computeTrapezoidal(T* x, T* y, int length) {
162 
163  double integral = 0;
164  double delta_x = 0;
165 
166  for (int i = 1; i < length; i++) {
167  delta_x = x[i] - x[i-1];
168  integral += delta_x * (y[i] + y[i-1]) / 2.0;
169  }
170 
171  return integral;
172 }
173 
174 
183 template <typename T>
184 double computeSimpsons(T* x, T* y, int length) {
185 
186  double integral = 0;
187  double delta_x = 0;
188 
189  for (int i = 0; i < length - 2; i++) {
190  delta_x = x[i+1] - x[i];
191  integral += (delta_x / 6.0) * (y[i] + 4*y[i+1] + y[i+2]);
192  }
193 
194  return integral;
195 }
196 
197 
206 template <typename T>
207 double computeSimpsons38(T* x, T* y, int length) {
208 
209  double integral = 0;
210  double delta_x = 0;
211 
212  for (int i = 0; i < length - 3; i++) {
213  delta_x = x[i+1] - x[i];
214  integral += (delta_x / 8.0) * (y[i] + 3*y[i+1] + 3*y[i+2] + y[i+3]);
215  }
216 
217  return integral;
218 }
219 
220 
229 template<typename T>
230 double computeBooles(T* x, T* y, int length) {
231 
232  double integral = 0;
233  double delta_x = 0;
234 
235  for (int i = 0; i < length - 4; i++) {
236  delta_x = x[i+1] - x[i];
237  integral += (delta_x / 90.0) * (7*y[i] + 32*y[i+1] + 12*y[i+2] +
238  32*y[i+3] + 7*y[i+4]);
239  }
240 
241  return integral;
242 }
243 
244 
254 template <typename T>
255 double integrate(T* x, T* y, int length, integrationScheme scheme) {
256 
257  double integral = 0;
258 
259  switch(scheme) {
260  case RIEMANN_RIGHT:
261  integral = computeRiemannRight(x, y, length);
262  break;
263  case RIEMANN_LEFT:
264  integral = computeRiemannLeft(x, y, length);
265  break;
266  case RIEMANN_CENTER:
267  integral = computeRiemannCenter(x, y, length);
268  break;
269  case TRAPEZOIDAL:
270  integral = computeTrapezoidal(x, y, length);
271  break;
272  case SIMPSONS:
273  integral = computeSimpsons(x, y, length);
274  break;
275  case SIMPSONS38:
276  integral = computeSimpsons38(x, y, length);
277  break;
278  case BOOLES:
279  integral = computeBooles(x, y, length);
280  break;
281  }
282 
283  return integral;
284 }
285 
286 
287 #endif
Definition: integrate.h:42
double computeTrapezoidal(T *x, T *y, int length)
Performs a numerical integral using the trapezoidal numerical integration method. ...
Definition: integrate.h:161
double computeSimpsons38(T *x, T *y, int length)
Performs a numerical integral using the Simpson's 3/8ths numerical integration method.
Definition: integrate.h:207
Definition: integrate.h:36
Definition: integrate.h:48
double computeSimpsons(T *x, T *y, int length)
Performs a numerical integral using the Simpson's numerical integration method.
Definition: integrate.h:184
void cumulativeIntegral(T *x, T *y, U *cdf, int length, integrationScheme scheme)
Performs a cumulative numerical integral over arrays of x and y values using the specificed integrati...
Definition: integrate.h:62
double computeRiemannCenter(T *x, T *y, int length)
Performs a numerical integral using the midpoint-centered Riemann numerical integration method...
Definition: integrate.h:132
Definition: integrate.h:40
enum integrationSchemes integrationScheme
Numerical integration methods.
double computeRiemannLeft(T *x, T *y, int length)
Performs a numerical integral using the left-centered Riemann numerical integration method...
Definition: integrate.h:82
double computeBooles(T *x, T *y, int length)
Performs a numerical integral using the Boole's numerical integration method.
Definition: integrate.h:230
integrationSchemes
Numerical integration methods.
Definition: integrate.h:34
double integrate(T *x, T *y, int length, integrationScheme scheme)
Performs a 1D numerical integral over arrays of x and y values using the specified integration scheme...
Definition: integrate.h:255
Definition: integrate.h:44
Definition: integrate.h:38
Definition: integrate.h:46
double computeRiemannRight(T *x, T *y, int length)
Performs a numerical integral using the right-centered Riemann numerical integration method...
Definition: integrate.h:109