Doxygen 1.15.0
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.2
Loading...
Searching...
No Matches
tsgGridWavelet.hpp
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 __TASMANIAN_SPARSE_GRID_WAVELET_HPP
32#define __TASMANIAN_SPARSE_GRID_WAVELET_HPP
33
34#include "tsgRuleWavelet.hpp"
35
36namespace TasGrid{
37
38#ifndef __TASMANIAN_DOXYGEN_SKIP
39class GridWavelet : public BaseCanonicalGrid{
40public:
41 GridWavelet(AccelerationContext const *acc) : BaseCanonicalGrid(acc), rule1D(1, 10), order(1){}
42 friend struct GridReaderVersion5<GridWavelet>;
43 GridWavelet(AccelerationContext const *acc, const GridWavelet *wav, int ibegin, int iend);
44 GridWavelet(AccelerationContext const *acc, int cnum_dimensions, int cnum_outputs, int depth, int corder, const std::vector<int> &level_limits);
45 GridWavelet(AccelerationContext const *acc, MultiIndexSet &&pset, int cnum_outputs, int corder, Data2D<double> &&vals);
46 ~GridWavelet() = default;
47
48 bool isWavelet() const override{ return true; }
49
50 void write(std::ostream &os, bool iomode) const override{ if (iomode == mode_ascii) write<mode_ascii>(os); else write<mode_binary>(os); }
51
52 template<bool iomode> void write(std::ostream &os) const;
53
54 TypeOneDRule getRule() const override{ return rule_wavelet; }
55 int getOrder() const{ return order; }
56
57 void getLoadedPoints(double *x) const override;
58 void getNeededPoints(double *x) const override;
59 void getPoints(double *x) const override; // returns the loaded points unless no points are loaded, then returns the needed points
60
61 void getQuadratureWeights(double weights[]) const override;
62 void getInterpolationWeights(const double x[], double weights[]) const override;
63 void getDifferentiationWeights(const double x[], double weights[]) const override;
64
65 void loadNeededValues(const double *vals) override;
66
67 void evaluate(const double x[], double y[]) const override;
68 void integrate(double q[], double *conformal_correction) const override;
69 void differentiate(const double x[], double jacobian[]) const override;
70
71 void evaluateBatch(const double x[], int num_x, double y[]) const override;
72
73 void evaluateGpuMixed(const double*, int, double[]) const;
74 void evaluateBatchGPU(const double*, int, double[]) const override;
75 void evaluateBatchGPU(const float*, int, float[]) const override;
76 template<typename T> void evaluateBatchGPUtempl(const T*, int, T[]) const;
77 void evaluateHierarchicalFunctionsGPU(const double gpu_x[], int cpu_num_x, double *gpu_y) const override;
78 void evaluateHierarchicalFunctionsGPU(const float gpu_x[], int cpu_num_x, float *gpu_y) const override;
79
80 void setSurplusRefinement(double tolerance, TypeRefinement criteria, int output, const std::vector<int> &level_limits);
81 void clearRefinement() override;
82 void mergeRefinement() override;
83
84 void beginConstruction() override;
85 void writeConstructionData(std::ostream&, bool) const override;
86 void readConstructionData(std::istream&, bool) override;
87 std::vector<double> getCandidateConstructionPoints(double tolerance, TypeRefinement criteria, int output, std::vector<int> const &level_limits);
88 void loadConstructedPoint(const double[], const std::vector<double> &) override;
89 void loadConstructedPoint(const double[], int, const double[]) override;
90 void finishConstruction() override;
91
92 void evaluateHierarchicalFunctions(const double x[], int num_x, double y[]) const override;
93 std::vector<double> getSupport() const override;
94
95 void setHierarchicalCoefficients(const double c[]) override;
96 void integrateHierarchicalFunctions(double integrals[]) const override;
97
98 const double* getSurpluses() const;
99
100 void updateAccelerationData(AccelerationContext::ChangeType change) const override;
101
102protected:
103 double evalBasis(const int p[], const double x[]) const;
104 void buildInterpolationMatrix() const;
105 void recomputeCoefficients();
106 void solveTransposed(double w[]) const;
107 double evalIntegral(const int p[]) const;
108 void evalDiffBasis(const int p[], const double x[], double jacobian[]) const;
109
110 std::vector<double> getNormalization() const;
111 std::vector<int> getMultiIndex(const double x[]);
112
113 Data2D<int> buildUpdateMap(double tolerance, TypeRefinement criteria, int output) const;
114 MultiIndexSet getRefinementCanidates(double tolerance, TypeRefinement criteria, int output, const std::vector<int> &level_limits) const;
115
116 bool addParent(const int point[], int direction, Data2D<int> &destination) const;
117 void addChild(const int point[], int direction, Data2D<int> &destination) const;
118 void addChildLimited(const int point[], int direction, const std::vector<int> &level_limits, Data2D<int> &destination) const;
119
120 void clearGpuCoefficients() const;
121 void clearGpuBasis() const;
122
123private:
124 RuleWavelet rule1D;
125
126 int order;
127
128 Data2D<double> coefficients; // a.k.a., surpluses
129
130 mutable TasSparse::WaveletBasisMatrix inter_matrix;
131
132 std::unique_ptr<SimpleConstructData> dynamic_values;
133
134 std::unique_ptr<CudaWaveletData<double>>& getGpuCacheOverload(double) const{ return gpu_cache; }
135 std::unique_ptr<CudaWaveletData<float>>& getGpuCacheOverload(float) const{ return gpu_cachef; }
136 template<typename T> std::unique_ptr<CudaWaveletData<T>>& getGpuCache() const{
137 return getGpuCacheOverload(static_cast<T>(0.0));
138 }
139 template<typename T> void loadGpuCoefficients() const;
140 template<typename T> void loadGpuBasis() const;
141 mutable std::unique_ptr<CudaWaveletData<double>> gpu_cache;
142 mutable std::unique_ptr<CudaWaveletData<float>> gpu_cachef;
143};
144
145// Old version reader
146template<> struct GridReaderVersion5<GridWavelet>{
147 template<typename iomode> static std::unique_ptr<GridWavelet> read(AccelerationContext const *acc, std::istream &is){
148 std::unique_ptr<GridWavelet> grid = Utils::make_unique<GridWavelet>(acc);
149
150 grid->num_dimensions = IO::readNumber<iomode, int>(is);
151 grid->num_outputs = IO::readNumber<iomode, int>(is);
152 grid->order = IO::readNumber<iomode, int>(is);
153 grid->rule1D.updateOrder(grid->order);
154
155 if (IO::readFlag<iomode>(is)) grid->points = MultiIndexSet(is, iomode());
156 if (std::is_same<iomode, IO::mode_ascii_type>::value){ // backwards compatible: surpluses and needed, or needed and surpluses
157 if (IO::readFlag<iomode>(is))
158 grid->coefficients = IO::readData2D<iomode, double>(is, grid->num_outputs, grid->points.getNumIndexes());
159 if (IO::readFlag<iomode>(is)) grid->needed = MultiIndexSet(is, iomode());
160 }else{
161 if (IO::readFlag<iomode>(is)) grid->needed = MultiIndexSet(is, iomode());
162 if (IO::readFlag<iomode>(is))
163 grid->coefficients = IO::readData2D<iomode, double>(is, grid->num_outputs, grid->points.getNumIndexes());
164 }
165
166 if (grid->num_outputs > 0) grid->values = StorageSet(is, iomode());
167 grid->buildInterpolationMatrix();
168
169 return grid;
170 }
171};
172#endif // __TASMANIAN_DOXYGEN_SKIP
173
174}
175
176#endif
TypeOneDRule
Used to specify the one dimensional family of rules that induces the sparse grid.
Definition tsgEnumerates.hpp:285
@ rule_wavelet
Wavelet basis with uniformly distributed nodes (primarily for internal use).
Definition tsgEnumerates.hpp:370
void loadNeededValues(std::function< void(double const x[], double y[], size_t thread_id)> model, TasmanianSparseGrid &grid, size_t num_threads)
Loads the current grid with model values, does not perform any refinement.
Definition tsgLoadNeededValues.hpp:104
Encapsulates the Tasmanian Sparse Grid module.
Definition TasmanianSparseGrid.hpp:68