An open source method of characteristics neutron transport code.
Quadrature.h
Go to the documentation of this file.
1 
14 #ifndef QUADRATURE_H_
15 #define QUADRATURE_H_
16 
17 #ifdef __cplusplus
18 #ifdef SWIG
19 #include "Python.h"
20 #endif
21 #include "constants.h"
22 #include "log.h"
23 #include <sstream>
24 #endif
25 #include <vector>
26 #include <iostream>
27 #include <cmath>
28 #include <algorithm>
29 
30 
36  TABUCHI_YAMAMOTO,
37  LEONARD,
38  GAUSS_LEGENDRE,
39  EQUAL_WEIGHT,
40  EQUAL_ANGLE
41 };
42 
44 typedef std::vector<double> DoubleVec;
45 typedef DoubleVec::const_iterator DVCI;
46 
48 template <typename T>
49  std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
50  if (vec.size() == 0) {
51  os << "EMPTY";
52  }
53 
54  else {
55 
56  using CI = typename std::vector<T>::const_iterator;
57 
58  os << "[";
59  for (CI cit = vec.begin(); cit != vec.end(); ++cit) {
60  if (cit != vec.begin()) {
61  os << ", ";
62  }
63  os << *cit;
64  }
65  os << "]";
66  }
67  return os;
68 }
69 
70 
71 
76 class Quadrature {
77 
78 protected:
79 
82 
84  size_t _num_azim;
85 
87  size_t _num_polar;
88 
90  std::vector<DoubleVec> _sin_thetas;
91 
93  std::vector<DoubleVec> _thetas;
94 
97 
100 
103 
105  std::vector<DoubleVec> _polar_spacings;
106 
108  std::vector<DoubleVec> _polar_weights;
109 
111  std::vector<DoubleVec> _total_weights;
112 
113  /* Templates for setting the same values to complimentary and supplementary
114  * angles */
115  template <typename T>
116  void setPolarValues(std::vector< std::vector<T> >& vec, size_t azim_index,
117  size_t polar_index, T value) {
118  size_t c_azim_index = _num_azim/2 - azim_index - 1;
119  size_t s_polar_index = _num_polar - polar_index - 1;
120  vec.at(azim_index).at(polar_index) = value;
121  vec.at(c_azim_index).at(polar_index) = value;
122  vec.at(azim_index).at(s_polar_index) = value;
123  vec.at(c_azim_index).at(s_polar_index) = value;
124  }
125 
126  template <typename T>
127  void setAzimuthalValues(std::vector<T>& vec, size_t azim_index, T value) {
128  vec.at(azim_index) = value;
129  vec.at(_num_azim/2 - azim_index - 1) = value;
130  }
131 
132  template <typename T>
133  static void resize2D(std::vector< std::vector<T> >& vec, size_t dim1,
134  size_t dim2) {
135  vec.resize(dim1);
136  for (size_t i = 0; i < dim1; ++i)
137  {
138  vec.at(i).resize(dim2);
139  }
140  }
141 
142 public:
143 
144  Quadrature();
145 
146  size_t getNumPolarAngles() const;
147  size_t getNumAzimAngles() const;
148  double getSinTheta(size_t azim, size_t polar) const;
149  double getSinThetaInline(size_t azim, size_t polar) const;
150  double getTheta(size_t azim, size_t polar) const;
151  double getPhi(size_t azim) const;
152  double getAzimWeight(size_t azim) const;
153  double getPolarWeight(size_t azim, size_t polar) const;
154  double getWeight(size_t azim, size_t polar) const;
155  double getWeightInline(size_t azim, size_t polar) const;
156  const std::vector<DoubleVec>& getSinThetas() const;
157  const std::vector<DoubleVec>& getThetas() const;
158  const DoubleVec& getPhis() const;
159  const DoubleVec& getAzimWeights() const;
160  const std::vector<DoubleVec>& getPolarWeights() const;
162  const DoubleVec& getAzimSpacings() const;
163  double getAzimSpacing(size_t azim) const;
164  const std::vector<DoubleVec>& getPolarSpacings() const;
165  double getPolarSpacing(size_t azim, size_t polar) const;
166 
167  virtual void setNumAzimAngles(size_t num_azim);
168  virtual void setNumPolarAngles(size_t num_polar);
169  void setThetas(const DoubleVec& thetas);
170  void setPolarWeights(const DoubleVec& weights);
171  void setTheta(double theta, size_t azim, size_t polar);
172  void setPhi(double phi, size_t azim);
173  void setAzimSpacing(double spacing, size_t azim);
174  void setPolarSpacing(double spacing, size_t azim, size_t polar);
175  void setAzimWeight(double weight, size_t azim);
176  void setPolarWeight(double weight, size_t azim, size_t polar);
177 
178  virtual void initialize();
179  virtual void precomputeWeights(bool solve_3D);
180 
181  std::string toString() const;
182 
183  friend std::ostream& operator<<(std::ostream& os, const Quadrature& quad);
184 };
185 
186 
187 
192 class TYPolarQuad: public Quadrature {
193 
194 private:
195 
196 public:
197  TYPolarQuad();
198  void setNumPolarAngles(size_t num_polar);
199  void initialize();
200  void precomputeWeights(bool solve_3D);
201 };
202 
203 
209 
210 private:
211 
212 public:
214  void setNumPolarAngles(size_t num_polar);
215  void initialize();
216  void precomputeWeights(bool solve_3D);
217 };
218 
219 
220 
225 class GLPolarQuad: public Quadrature {
226 
227 private:
228 
230  std::vector <double> _roots;
231 
233  std::vector <double> _adjusted_roots;
234 
236  bool _correct_weights;
237 
238 public:
239  GLPolarQuad();
240  void setNumPolarAngles(size_t num_polar);
241  void useCorrectedWeights(bool use_corrected_weights);
242  void initialize();
243  void precomputeWeights(bool solve_3D);
244  DoubleVec getCorrectedWeights(size_t azim) const;
245 
246  static double legendrePolynomial(size_t n, double x);
247  static double logDerivLegendre(size_t n, double x);
248  static double secondLogDerivLegendre(size_t n, double x);
249  static double getSingleWeight(double root, size_t n);
250  static DoubleVec getLegendreRoots(size_t n);
251  static DoubleVec getGLWeights(const DoubleVec& roots, size_t n);
252 };
253 
254 
255 
261 
262 private:
263 
264 public:
266  void setNumPolarAngles(size_t num_polar);
267  void initialize();
268  void precomputeWeights(bool solve_3D);
269 };
270 
271 
272 
278 
279 private:
280 
281 public:
283  void setNumPolarAngles(size_t num_polar);
284  void initialize();
285  void precomputeWeights(bool solve_3D);
286 };
287 
288 
296 inline double Quadrature::getSinThetaInline(size_t azim, size_t polar) const {
297 
298  if (azim >= _num_azim/2)
299  azim = _num_azim - azim - 1;
300 
301  return _sin_thetas[azim][polar];
302 }
303 
304 
313 inline double Quadrature::getWeightInline(size_t azim, size_t polar) const {
314  return _total_weights[azim][polar];
315 }
316 
317 #endif /* QUADRATURE_H_ */
void setTheta(double theta, size_t azim, size_t polar)
Sets the polar angle for the given indexes.
Definition: Quadrature.cpp:492
double getSinTheta(size_t azim, size_t polar) const
Returns the value for a particular polar angle.
Definition: Quadrature.cpp:41
double getPolarSpacing(size_t azim, size_t polar) const
Returns the adjusted polar spacing at the requested azimuthal angle index and polar angle index...
Definition: Quadrature.cpp:357
void precomputeWeights(bool solve_3D)
Calculates total weights for every azimuthal/polar combination based on the Leonard polar quadrature...
Definition: Quadrature.cpp:954
void setPolarWeight(double weight, size_t azim, size_t polar)
Sets the polar weight for the given indexes.
Definition: Quadrature.cpp:615
const DoubleVec & getAzimSpacings() const
Returns an vector of adjusted azimuthal spacings.
Definition: Quadrature.cpp:316
std::vector< DoubleVec > _sin_thetas
Definition: Quadrature.h:90
DoubleVec _azim_spacings
Definition: Quadrature.h:99
double getSinThetaInline(size_t azim, size_t polar) const
Returns the value for a particular polar angle.
Definition: Quadrature.h:296
std::vector< double > DoubleVec
Definition: Quadrature.h:44
Leonard&#39;s polar quadrature.
Definition: Quadrature.h:208
virtual void precomputeWeights(bool solve_3D)
This private routine computes the product of the sine thetas and weights for each angle in the polar ...
Definition: Quadrature.cpp:674
DoubleVec getCorrectedWeights(size_t azim) const
Calculates the weights to be used in numerical integration.
Definition: Quadrature.cpp:1273
std::vector< DoubleVec > _polar_weights
Definition: Quadrature.h:108
double getWeightInline(size_t azim, size_t polar) const
Returns the total weight for Tracks with the given azimuthal and polar indexes without error checking...
Definition: Quadrature.h:313
void initialize()
Routine to initialize the polar quadrature.
Definition: Quadrature.cpp:919
QuadratureType
The types of quadrature sets supported by OpenMOC.
Definition: Quadrature.h:35
Math constants and comparision tolerances.
void precomputeWeights(bool solve_3D)
Calculates total weights for every azimuthal/polar combination based on the equal angle polar quadrat...
Definition: Quadrature.cpp:1514
void setAzimWeight(double weight, size_t azim)
Sets the azimuthal weight for the given index.
Definition: Quadrature.cpp:593
friend std::ostream & operator<<(std::ostream &os, const Quadrature &quad)
Prints to the provided output stream.
Definition: Quadrature.cpp:772
void setNumPolarAngles(size_t num_polar)
Set the number of polar angles to initialize.
Definition: Quadrature.cpp:1468
const DoubleVec & getAzimWeights() const
Returns a pointer to the Quadrature&#39;s vector of azimuthal weights.
Definition: Quadrature.cpp:255
std::vector< DoubleVec > _thetas
Definition: Quadrature.h:93
Quadrature()
Dummy constructor sets the default number of angles to zero.
Definition: Quadrature.cpp:11
const std::vector< DoubleVec > & getThetas() const
Returns a reference to the Quadrature&#39;s vector of polar angles .
Definition: Quadrature.cpp:226
virtual void setNumPolarAngles(size_t num_polar)
Set the number of polar angles to initialize.
Definition: Quadrature.cpp:367
EqualWeightPolarQuad()
Dummy constructor calls the parent constructor.
Definition: Quadrature.cpp:1367
void setThetas(const DoubleVec &thetas)
Sets the Quadrature&#39;s vector of polar angles.
Definition: Quadrature.cpp:412
std::vector< DoubleVec > _total_weights
Definition: Quadrature.h:111
Tabuchi-Yamamoto&#39;s polar quadrature.
Definition: Quadrature.h:192
static double getSingleWeight(double root, size_t n)
Calculates the weights to be used in Gauss-Legendre Quadrature.
Definition: Quadrature.cpp:1260
void setNumPolarAngles(size_t num_polar)
Set the number of polar angles to initialize.
Definition: Quadrature.cpp:997
void useCorrectedWeights(bool use_corrected_weights)
Indicates whether to correct weights based on altered polar angles.
Definition: Quadrature.cpp:1035
std::vector< DoubleVec > _polar_spacings
Definition: Quadrature.h:105
static DoubleVec getLegendreRoots(size_t n)
Finds the roots of Legendre polynomial of order n.
Definition: Quadrature.cpp:1128
static double legendrePolynomial(size_t n, double x)
the Legendre polynomial of degree n evaluated at x
Definition: Quadrature.cpp:1076
The arbitrary quadrature parent class.
Definition: Quadrature.h:76
Equal angle polar quadrature.
Definition: Quadrature.h:277
static DoubleVec getGLWeights(const DoubleVec &roots, size_t n)
Calculates the weights to be used in Gauss-Legendre Quadrature.
Definition: Quadrature.cpp:1241
void setPolarSpacing(double spacing, size_t azim, size_t polar)
Sets the polar spacing for the given indexes.
Definition: Quadrature.cpp:567
void initialize()
Routine to initialize the polar quadrature.
Definition: Quadrature.cpp:1008
TYPolarQuad()
Dummy constructor calls the parent constructor.
Definition: Quadrature.cpp:790
void setNumPolarAngles(size_t num_polar)
Set the number of polar angles to initialize.
Definition: Quadrature.cpp:800
double getAzimWeight(size_t azim) const
Returns the azimuthal angle weight value for a particular azimuthal angle.
Definition: Quadrature.cpp:126
void setNumPolarAngles(size_t num_polar)
Set the number of polar angles to initialize.
Definition: Quadrature.cpp:904
double getAzimSpacing(size_t azim) const
Returns the adjusted azimuthal spacing at the requested azimuthal angle index.
Definition: Quadrature.cpp:330
GLPolarQuad()
Dummy constructor calls the parent constructor.
Definition: Quadrature.cpp:986
QuadratureType _quad_type
Definition: Quadrature.h:81
EqualAnglePolarQuad()
Dummy constructor calls the parent constructor.
Definition: Quadrature.cpp:1458
void setPolarWeights(const DoubleVec &weights)
Set the Quadrature&#39;s vector of polar weights.
Definition: Quadrature.cpp:460
size_t getNumPolarAngles() const
Returns the number of polar angles.
Definition: Quadrature.cpp:21
const std::vector< DoubleVec > & getPolarWeights() const
Returns a pointer to the Quadrature&#39;s vector of polar weights.
Definition: Quadrature.cpp:269
std::string toString() const
Converts this Quadrature to a character vector of its attributes.
Definition: Quadrature.cpp:745
QuadratureType getQuadratureType() const
Returns the type of Quadrature created.
Definition: Quadrature.cpp:782
void initialize()
Routine to initialize the polar quadrature.
Definition: Quadrature.cpp:815
static double secondLogDerivLegendre(size_t n, double x)
The second logarithmic derivative of a Legendre polynomial.
Definition: Quadrature.cpp:1111
const DoubleVec & getPhis() const
Returns a pointer to the Quadrature&#39;s vector of azimuthal angles .
Definition: Quadrature.cpp:241
size_t _num_azim
Definition: Quadrature.h:84
const std::vector< DoubleVec > & getPolarSpacings() const
Returns a 2D vector of adjusted polar spacings.
Definition: Quadrature.cpp:342
virtual void initialize()
Initialize the polar quadrature azimuthal angles.
Definition: Quadrature.cpp:646
void precomputeWeights(bool solve_3D)
Calculates total weights for every azimuthal/polar combination based on the equal weight polar quadra...
Definition: Quadrature.cpp:1419
Gauss-Legendre&#39;s polar quadrature.
Definition: Quadrature.h:225
const std::vector< DoubleVec > & getSinThetas() const
Returns a pointer to the Quadrature&#39;s vector of polar angle sines .
Definition: Quadrature.cpp:211
double getPhi(size_t azim) const
Returns the azimuthal angle value in radians.
Definition: Quadrature.cpp:101
void setPhi(double phi, size_t azim)
Sets the azimuthal angle for the given index.
Definition: Quadrature.cpp:522
virtual void setNumAzimAngles(size_t num_azim)
Set the number of azimuthal angles to initialize.
Definition: Quadrature.cpp:283
LeonardPolarQuad()
Dummy constructor calls the parent constructor.
Definition: Quadrature.cpp:894
void initialize()
Routine to initialize the polar quadrature.
Definition: Quadrature.cpp:1477
void precomputeWeights(bool solve_3D)
Calculates total weights for every azimuthal/polar combination based on the TY quadrature.
Definition: Quadrature.cpp:856
void initialize()
Routine to initialize the polar quadrature.
Definition: Quadrature.cpp:1386
#define weights(i, p)
Definition: GPUSolver.h:51
double getTheta(size_t azim, size_t polar) const
Returns the polar angle in radians for a given azimuthal and polar angle index.
Definition: Quadrature.cpp:72
double getWeight(size_t azim, size_t polar) const
Returns the total weight for Tracks with the given azimuthal and polar indexes.
Definition: Quadrature.cpp:182
size_t getNumAzimAngles() const
Returns the number of azimuthal angles.
Definition: Quadrature.cpp:30
double getPolarWeight(size_t azim, size_t polar) const
Returns the polar weight for a particular azimuthal and polar angle.
Definition: Quadrature.cpp:150
DoubleVec _azim_weights
Definition: Quadrature.h:102
Utility functions for writing log messages to the screen.
static double logDerivLegendre(size_t n, double x)
The first logarithmic derivative of a Legendre polynomial.
Definition: Quadrature.cpp:1098
DoubleVec _phis
Definition: Quadrature.h:96
void setAzimSpacing(double spacing, size_t azim)
Sets the azimuthal spacing for the given index.
Definition: Quadrature.cpp:544
void precomputeWeights(bool solve_3D)
Calculates total weights for every azimuthal/polar combination based on the Gauss-Legendre polar quad...
Definition: Quadrature.cpp:1045
Equal weight polar quadrature.
Definition: Quadrature.h:260
size_t _num_polar
Definition: Quadrature.h:87
void setNumPolarAngles(size_t num_polar)
Set the number of polar angles to initialize.
Definition: Quadrature.cpp:1377