Doxygen 1.15.0
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.2
Loading...
Searching...
No Matches
tsgDConstructGridGlobal.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_DYNAMIC_CONST_GLOBAL_HPP
32#define __TASMANIAN_SPARSE_GRID_DYNAMIC_CONST_GLOBAL_HPP
33
34#include <forward_list>
35
36#include "tsgIndexManipulator.hpp"
37
52
67
68namespace TasGrid{
69
82struct NodeData{
84 std::vector<int> point;
86 std::vector<double> value;
87};
88
99struct TensorData{
101 double weight;
103 std::vector<int> tensor;
105 MultiIndexSet points;
107 std::vector<bool> loaded;
108};
109
116template <class T>
117std::vector<const T*> makeReverseReferenceVector(const std::forward_list<T> &list){
118 size_t num_entries = (size_t) std::distance(list.begin(), list.end());
119 std::vector<const T*> refs(num_entries);
120 auto p = list.begin();
121 auto r = refs.rbegin();
122 while(p != list.end()) *r++ = &*p++;
123 return refs;
124}
125
132template<bool use_ascii>
133void writeNodeDataList(const std::forward_list<NodeData> &data, std::ostream &os){
134 if (use_ascii == mode_ascii){ os << std::scientific; os.precision(17); }
135
136 auto data_refs = makeReverseReferenceVector(data);
137
138 IO::writeNumbers<use_ascii, IO::pad_line>(os, static_cast<int>(data_refs.size()));
139 for(auto d : data_refs){
140 IO::writeVector<use_ascii, IO::pad_rspace>(d->point, os);
141 IO::writeVector<use_ascii, IO::pad_line>(d->value, os);
142 }
143}
144
151template<typename iomode>
152std::forward_list<NodeData> readNodeDataList(std::istream &is, size_t num_dimensions, size_t num_outputs){
153 std::forward_list<NodeData> data;
154 int num_nodes = IO::readNumber<iomode, int>(is);
155
156 for(int i=0; i<num_nodes; i++){
157 data.emplace_front(NodeData{
158 IO::readVector<iomode, int>(is, num_dimensions), // point
159 IO::readVector<iomode, double>(is, num_outputs) // value
160 });
161 }
162
163 return data;
164}
172template<typename iomode>
173std::forward_list<TensorData> readTensorDataList(std::istream &is, size_t num_dimensions){
174 std::forward_list<TensorData> tensors;
175 int num_entries = IO::readNumber<iomode, int>(is);
176
177 for(int i=0; i<num_entries; i++){
178 tensors.emplace_front(TensorData{
179 IO::readNumber<iomode, double>(is), // weight
180 IO::readVector<iomode, int>(is, num_dimensions), // tensor
181 MultiIndexSet(), // points, will be set later
182 std::vector<bool>() // loaded, will be set later
183 });
184 }
185
186 return tensors;
187}
188
196template<class RuleLike>
197std::vector<double> listToNodes(std::forward_list<NodeData> const &node_list, size_t num_dimensions, RuleLike const &rule){
198 std::vector<double> result(Utils::size_mult(std::distance(node_list.begin(), node_list.end()), num_dimensions));
199 auto ix = result.begin();
200 for(auto const &t : node_list)
201 ix = MultiIndexManipulations::indexesToNodes(t.point, rule, ix);
202 return result;
203}
211template<typename callable_method>
212std::vector<double> listToLocalNodes(std::forward_list<NodeData> const &node_list, size_t num_dimensions, callable_method rule){
213 std::vector<double> result(Utils::size_mult(std::distance(node_list.begin(), node_list.end()), num_dimensions));
214 auto ix = result.begin();
215 for(auto const &t : node_list)
216 ix = std::transform(t.point.begin(), t.point.end(), ix, rule);
217 return result;
218}
219
231class DynamicConstructorDataGlobal{
232public:
234 enum class AddPointResult {
236 tensor_incomplete,
238 tensor_complete,
240 tensor_missing
241 };
242
244 DynamicConstructorDataGlobal(size_t cnum_dimensions, size_t cnum_outputs) : num_dimensions(cnum_dimensions), num_outputs(cnum_outputs){}
246 template<typename iomode>
247 DynamicConstructorDataGlobal(std::istream &is, size_t cnum_dimensions, size_t cnum_outputs, iomode) :
248 num_dimensions(cnum_dimensions),
249 num_outputs(cnum_outputs),
250 tensors(readTensorDataList<iomode>(is, num_dimensions)),
251 data(readNodeDataList<iomode>(is, num_dimensions, num_outputs))
252 {}
254 ~DynamicConstructorDataGlobal() = default;
255
257 template<bool use_ascii> void write(std::ostream &os) const;
258
260 void restrictData(int ibegin, int iend){ for(auto &d : data) d.value = std::vector<double>(d.value.begin() + ibegin, d.value.begin() + iend); }
261
263 int getMaxTensor() const;
265 double getMaxTensorWeight() const{
266 double maxw = 0;
267 for(auto it = tensors.begin(); it != tensors.end(); it++){
268 if (it->weight > maxw) maxw = it->weight;
269 }
270 return maxw;
271 }
272
274 void reloadPoints(std::function<int(int)> getNumPoints);
275
277 void clearTesnors();
278
280 MultiIndexSet getInitialTensors() const;
281
283 void addTensor(const int *tensor, std::function<int(int)> getNumPoints, double weight);
284
286 MultiIndexSet getNodesIndexes();
287
289 AddPointResult addNewNode(const std::vector<int> &point, const std::vector<double> &value); // returns whether a tensor is complete
290
292 void ejectCompleteTensor(MultiIndexSet const &current_tensors, MultiIndexSet &new_tensors, MultiIndexSet &new_points, StorageSet &vals);
293
294private:
295 size_t num_dimensions, num_outputs;
296 std::forward_list<TensorData> tensors;
297 std::forward_list<NodeData> data;
298};
299
312struct SimpleConstructData{
314 SimpleConstructData() = default;
316 template<typename iomode>
317 SimpleConstructData(std::istream &is, int num_dimensions, int num_outputs, iomode) :
318 initial_points(MultiIndexSet(is, iomode())),
319 data(readNodeDataList<iomode>(is, num_dimensions, num_outputs))
320 {}
322 MultiIndexSet initial_points;
324 std::forward_list<NodeData> data;
326 template<bool use_ascii>
327 void write(std::ostream &os) const{
328 initial_points.write<use_ascii>(os);
329 writeNodeDataList<use_ascii>(data, os);
330 }
332 void restrictData(int ibegin, int iend){ for(auto &d : data) d.value = std::vector<double>(d.value.begin() + ibegin, d.value.begin() + iend); }
334 std::vector<double> extractValues(MultiIndexSet const &points){
335 size_t num_outputs = data.front().value.size();
336 int num_points = points.getNumIndexes();
337 Data2D<double> result(num_outputs, num_points);
338 auto p = data.before_begin();
339 auto d = data.begin();
340 while(d != data.end()){
341 int slot = points.getSlot(d->point);
342 if (slot != -1){ // found a point
343 std::copy_n(d->value.begin(), num_outputs, result.getStrip(slot));
344 data.erase_after(p);
345 d = p;
346 d++;
347 }else{
348 p++;
349 d++;
350 }
351 }
352 return result.release();
353 }
354};
355
356}
357
358#endif
constexpr bool mode_ascii
Constant allowing for more expressive selection of ascii and binary mode in IO methods.
Definition tsgIOHelpers.hpp:62
Encapsulates the Tasmanian Sparse Grid module.
Definition TasmanianSparseGrid.hpp:68