Doxygen 1.9.1
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.2 (development)
tsgCoreOneDimensional.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, Miroslav Stoyanov
3  *
4  * This file is part of
5  * Toolkit for Adaptive Stochastic Modeling And Non-Intrusive ApproximatioN: TASMANIAN
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
12  * and the following disclaimer in the documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
20  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
21  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * UT-BATTELLE, LLC AND THE UNITED STATES GOVERNMENT MAKE NO REPRESENTATIONS AND DISCLAIM ALL WARRANTIES, BOTH EXPRESSED AND IMPLIED.
25  * THERE ARE NO EXPRESS OR IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR THAT THE USE OF THE SOFTWARE WILL NOT INFRINGE ANY PATENT,
26  * COPYRIGHT, TRADEMARK, OR OTHER PROPRIETARY RIGHTS, OR THAT THE SOFTWARE WILL ACCOMPLISH THE INTENDED RESULTS OR THAT THE SOFTWARE OR ITS USE WILL NOT RESULT IN INJURY OR DAMAGE.
27  * THE USER ASSUMES RESPONSIBILITY FOR ALL LIABILITIES, PENALTIES, FINES, CLAIMS, CAUSES OF ACTION, AND COSTS AND EXPENSES, CAUSED BY, RESULTING FROM OR ARISING OUT OF,
28  * IN WHOLE OR IN PART THE USE, STORAGE OR DISPOSAL OF THE SOFTWARE.
29  */
30 
31 #ifndef __TSG_CORE_ONE_DIMENSIONAL_HPP
32 #define __TSG_CORE_ONE_DIMENSIONAL_HPP
33 
34 #include "tsgIOHelpers.hpp"
35 #include "tsgLinearSolvers.hpp"
36 #include "tsgSequenceOptimizer.hpp"
37 
62 namespace TasGrid{
63 
67 public:
69  CustomTabulated() : num_levels(0){}
71  template<typename iomode> CustomTabulated(std::istream &is, iomode) : CustomTabulated(){
72  if (std::is_same<iomode, IO::mode_ascii_type>::value)
73  read<mode_ascii>(is); else read<mode_binary>(is);
74  }
76  CustomTabulated(std::vector<int> &&cnum_nodes, std::vector<int> &&cprecision,
77  std::vector<std::vector<double>> &&cnodes, std::vector<std::vector<double>> &&cweights,
78  std::string &&cdescription) :
79  num_levels(cnum_nodes.size()), num_nodes(std::move(cnum_nodes)), precision(std::move(cprecision)),
80  nodes(std::move(cnodes)), weights(std::move(cweights)), description(std::move(cdescription))
81  {}
82 
84  template<bool useAscii> void write(std::ostream &os) const;
86  template<bool useAscii> void read(std::istream &is);
87 
89  void read(const char* filename);
90 
92  int getNumLevels() const{ return num_levels; }
94  int getNumPoints(int level) const{ checkLevel(level, "number of points"); return num_nodes[level]; }
96  int getIExact(int level) const{ checkLevel(level, "i-exactness"); return num_nodes[level] -1; }
98  int getQExact(int level) const{ checkLevel(level, "q-exactness"); return precision[level]; }
99 
101  void getWeightsNodes(int level, std::vector<double> &w, std::vector<double> &x) const;
103  void getWeightsNodes(int level, double w[], double x[]) const;
104 
106  const char* getDescription() const;
107 
108 protected:
110  void checkLevel(int level, std::string const &op) const{
111  if (level >= num_levels)
112  throw std::runtime_error(std::string("ERROR: needed custom rule ") + op + " with level " + std::to_string(level) + " but the table ends at " + std::to_string(num_levels - 1));
113  }
114 
115 private:
116  int num_levels;
117  std::vector<int> num_nodes;
118  std::vector<int> precision;
119 
120  std::vector<std::vector<double>> nodes;
121  std::vector<std::vector<double>> weights;
122  std::string description;
123 };
124 
133 CustomTabulated getSubrules(CustomTabulated &ct, int start_index, int stride, std::string description);
134 
138 namespace OneDimensionalMeta{
140  int getNumPoints(int level, TypeOneDRule rule);
142  int getIExact(int level, TypeOneDRule rule);
144  int getQExact(int level, TypeOneDRule rule);
145 
151  bool isGlobal(TypeOneDRule rule);
160 
162  const char* getHumanString(TypeOneDRule rule);
163 
165 
171  if ((type == type_level) || (type == type_iptotal) || (type == type_qptotal)){
172  return type_level;
173  }else if ((type == type_curved) || (type == type_ipcurved) || (type == type_qpcurved)){
174  return type_curved;
175  }else{
176  return type_hyperbolic;
177  }
178  }
179 
181  inline bool isExactLevel(TypeDepth type){
182  return (type == type_level) || (type == type_curved) || (type == type_hyperbolic) || (type == type_tensor);
183  }
184 
186  inline bool isExactInterpolation(TypeDepth type){
187  return (type == type_iptotal) || (type == type_ipcurved) || (type == type_iphyperbolic) || (type == type_iptensor);
188  }
189 
191  inline bool isExactQuadrature(TypeDepth type){
192  return (type == type_qptotal) || (type == type_qpcurved) || (type == type_qphyperbolic) || (type == type_qptensor);
193  }
194 
196 
199  if ((type == type_level) || (type == type_curved) || (type == type_hyperbolic)){
200  return type_level;
201  }else if ((type == type_iptotal) || (type == type_ipcurved) || (type == type_iphyperbolic)){
202  return type_iptotal;
203  }else{
204  return type_qptotal;
205  }
206  }
207 
209  inline bool isTypeCurved(TypeDepth type){ return (getControurType(type) == type_curved); }
210 }
211 
215 namespace OneDimensionalNodes{
216  // non-nested rules
218  void getGaussLegendre(int m, std::vector<double> &w, std::vector<double> &x);
220  void getChebyshev(int m, std::vector<double> &w, std::vector<double> &x);
222  void getGaussChebyshev1(int m, std::vector<double> &w, std::vector<double> &x);
224  void getGaussChebyshev2(int m, std::vector<double> &w, std::vector<double> &x);
226  void getGaussJacobi(int m, std::vector<double> &w, std::vector<double> &x, double alpha, double beta);
228  void getGaussHermite(int m, std::vector<double> &w, std::vector<double> &x, double alpha);
230  void getGaussLaguerre(int m, std::vector<double> &w, std::vector<double> &x, double alpha);
231 
232  // nested rules
234  std::vector<double> getClenshawCurtisNodes(int level);
236  double getClenshawCurtisWeight(int level, int point);
237 
239  std::vector<double> getClenshawCurtisNodesZero(int level); // assuming zero boundary
241  double getClenshawCurtisWeightZero(int level, int point); // assuming zero boundary
242 
244  std::vector<double> getFejer2Nodes(int level);
246  double getFejer2Weight(int level, int point);
247 
249  std::vector<double> getRLeja(int n);
251  std::vector<double> getRLejaCentered(int n);
253  std::vector<double> getRLejaShifted(int n);
254 
256  std::vector<double> getFourierNodes(int level);
257 }
258 
259 }
260 
261 #endif
Class providing manipulation of custom tabulated rules, file I/O and structured access to the points,...
Definition: tsgCoreOneDimensional.hpp:66
void read(const char *filename)
Read from a custom user provided ASCII file, see the file-format section.
int getNumPoints(int level) const
Returns the number of points associated with the selected level.
Definition: tsgCoreOneDimensional.hpp:94
int getQExact(int level) const
Return the exactness of the integration/quadrature rule at level, provided by the user in the custom ...
Definition: tsgCoreOneDimensional.hpp:98
int getNumLevels() const
Returns the number of loaded levels.
Definition: tsgCoreOneDimensional.hpp:92
void write(std::ostream &os) const
Write to an already open ASCII/binary file, used in conjunction with GlobalGrid::write()
void read(std::istream &is)
Read from an already open ASCII/binary file, used in conjunction with GlobalGrid::read()
int getIExact(int level) const
Return the exactness of the interpolation rule at level, usually one less than the number of points.
Definition: tsgCoreOneDimensional.hpp:96
void getWeightsNodes(int level, std::vector< double > &w, std::vector< double > &x) const
Get the points x and quadrature weights w associated with the rule at the level.
CustomTabulated()
Default constructor, create an empty table, need to read from file before any other data can be acces...
Definition: tsgCoreOneDimensional.hpp:69
CustomTabulated(std::vector< int > &&cnum_nodes, std::vector< int > &&cprecision, std::vector< std::vector< double >> &&cnodes, std::vector< std::vector< double >> &&cweights, std::string &&cdescription)
Assume ownership of existing data instead of reading from a file.
Definition: tsgCoreOneDimensional.hpp:76
const char * getDescription() const
Returns the user provided human readable description string.
void getWeightsNodes(int level, double w[], double x[]) const
Overload that writes to an array directly rather than a container. For interface purposes mostly,...
CustomTabulated(std::istream &is, iomode)
Read-constructor.
Definition: tsgCoreOneDimensional.hpp:71
void checkLevel(int level, std::string const &op) const
Throws a std::rumtime_error() if the given level is more than the stored levels, op is used to report...
Definition: tsgCoreOneDimensional.hpp:110
TypeOneDRule
Used to specify the one dimensional family of rules that induces the sparse grid.
Definition: tsgEnumerates.hpp:285
TypeDepth
Used by Global Sequence and Fourier grids, indicates the selection criteria.
Definition: tsgEnumerates.hpp:203
@ type_curved
Ignoring the polynomial space, use rules with index .
Definition: tsgEnumerates.hpp:213
@ type_iptensor
Make a dense tensor grid with interpolation range that includes .
Definition: tsgEnumerates.hpp:245
@ type_qptotal
Total degree polynomial space for quadrature/integration.
Definition: tsgEnumerates.hpp:224
@ type_qpcurved
Curved polynomial space for quadrature/integration.
Definition: tsgEnumerates.hpp:231
@ type_level
Ignoring the polynomial space, use rules with index .
Definition: tsgEnumerates.hpp:209
@ type_qphyperbolic
Hyperbolic cross section polynomial space for quadrature/integration.
Definition: tsgEnumerates.hpp:238
@ type_iptotal
Total degree polynomial space for interpolation, i.e., the span of .
Definition: tsgEnumerates.hpp:221
@ type_qptensor
Make a dense tensor grid with quadrature/integration range that includes .
Definition: tsgEnumerates.hpp:249
@ type_hyperbolic
Ignoring the polynomial space, use rules with index .
Definition: tsgEnumerates.hpp:217
@ type_iphyperbolic
Hyperbolic cross section polynomial space for interpolation, i.e., the span of .
Definition: tsgEnumerates.hpp:235
@ type_tensor
Make a dense tensor grid with rules indexed by .
Definition: tsgEnumerates.hpp:241
@ type_ipcurved
Curved polynomial space for interpolation, i.e., the span of .
Definition: tsgEnumerates.hpp:228
CustomTabulated getSubrules(CustomTabulated &ct, int start_index, int stride, std::string description)
Generates a subset of rules as a CustomTabulated object. The subset has a short description string gi...
bool isGlobal(TypeOneDRule rule)
Return True if the rule has basis with Lagrange polynomials with global support, fit for GridGlobal,...
bool isFourier(TypeOneDRule rule)
Return True if the rule has trigonometric basis.
bool isNonNested(TypeOneDRule rule)
Return True if the rule does not have nested nodes, e.g., gauss-legendre.
bool isExactLevel(TypeDepth type)
Returns true if the type indicates exactness with respect to raw levels.
Definition: tsgCoreOneDimensional.hpp:181
bool isLocalPolynomial(TypeOneDRule rule)
Return True if the rule has polynomial basis with local support, fit for GridLocalPolynomial,...
int getQExact(int level, TypeOneDRule rule)
Return the exactness of the integration/quadrature rule at the level, includes all global rules.
int getIExact(int level, TypeOneDRule rule)
Return the exactness of the interpolation rule at the level, includes all global rules.
bool isTypeCurved(TypeDepth type)
Return True if the multi-index selection type has log-correction term (need to use floating point ind...
Definition: tsgCoreOneDimensional.hpp:209
bool isExactInterpolation(TypeDepth type)
Returns true if the type indicates exactness with respect to interpolation.
Definition: tsgCoreOneDimensional.hpp:186
bool isWavelet(TypeOneDRule rule)
Return True if the rule has wavelet basis with local support.
bool isSequence(TypeOneDRule rule)
Return True if the rule is nested and has single-node growth, fit for GridSequence,...
const char * getHumanString(TypeOneDRule rule)
Map the enumerate to a human readable string, used in printStats().
TypeDepth getControurType(TypeDepth type)
Identifies the general contour, linear type_level, log-corrected type_curved, or hyperbolic type_hype...
Definition: tsgCoreOneDimensional.hpp:170
bool isSingleNodeGrowth(TypeOneDRule rule)
Return True if the rule grows by one point per level.
int getNumPoints(int level, TypeOneDRule rule)
Return the number of points for the rule at the level, includes all global rules.
TypeDepth getSelectionType(TypeDepth type)
Identifies the selection type, level type_level, interpolation type_iptotal, or quadrature type_qptot...
Definition: tsgCoreOneDimensional.hpp:198
bool isExactQuadrature(TypeDepth type)
Returns true if the type indicates exactness with respect to integration.
Definition: tsgCoreOneDimensional.hpp:191
void getGaussHermite(int m, std::vector< double > &w, std::vector< double > &x, double alpha)
Generate Gauss-Hermite weights w and points x for (input) number of points m, using parameters alpha.
void getChebyshev(int m, std::vector< double > &w, std::vector< double > &x)
Generate Chebyshev weights w and points x for (input) number of points m.
std::vector< double > getRLejaCentered(int n)
Generate the first n R-Leja nodes, starting with 0, 1, -1, ...
void getGaussChebyshev1(int m, std::vector< double > &w, std::vector< double > &x)
Generate Gauss-Chebyshev type 1 weights w and points x for (input) number of points m.
double getClenshawCurtisWeightZero(int level, int point)
Return the Clenshaw-Curtis zero-boundary condition weight for the level and node indexed by point.
std::vector< double > getClenshawCurtisNodes(int level)
Generate Clenshaw-Curtis nodes for the level.
std::vector< double > getFourierNodes(int level)
Generate the Fourier nodes for the given level, uniformly distributed points with right-most point om...
double getFejer2Weight(int level, int point)
Return the Fejer type 2 weight for the level and node indexed by point.
std::vector< double > getRLejaShifted(int n)
Generate the first n R-Leja nodes, starting with -0.5, 0.5, ...
void getGaussChebyshev2(int m, std::vector< double > &w, std::vector< double > &x)
Generate Gauss-Chebyshev type 2 weights w and points x for (input) number of points m.
void getGaussLegendre(int m, std::vector< double > &w, std::vector< double > &x)
Generate Gauss-Legendre weights w and points x for (input) number of points m.
std::vector< double > getClenshawCurtisNodesZero(int level)
Generate Clenshaw-Curtis zero-boundary condition nodes for the level.
std::vector< double > getFejer2Nodes(int level)
Generate Fejer type 2 nodes for the level.
std::vector< double > getRLeja(int n)
Generate the first n R-Leja nodes, starting with 1, -1, 0, ...
double getClenshawCurtisWeight(int level, int point)
Return the Clenshaw-Curtis weight for the level and node indexed by point.
void getGaussLaguerre(int m, std::vector< double > &w, std::vector< double > &x, double alpha)
Generate Gauss-Laguerre weights w and points x for (input) number of points m, using parameters alpha...
void getGaussJacobi(int m, std::vector< double > &w, std::vector< double > &x, double alpha, double beta)
Generate Gauss-Jacobi weights w and points x for (input) number of points m, using parameters alpha a...
Encapsulates the Tasmanian Sparse Grid module.
Definition: TasmanianSparseGrid.hpp:68
Templates to simply file I/O.
Linear solvers.
Algorithms for computing greedy sequences.