Doxygen 1.9.8
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.2
 
Loading...
Searching...
No Matches
tsgDConstructGridGlobal.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 __TASMANIAN_SPARSE_GRID_DYNAMIC_CONST_GLOBAL_HPP
32#define __TASMANIAN_SPARSE_GRID_DYNAMIC_CONST_GLOBAL_HPP
33
34#include <forward_list>
35
37
68namespace TasGrid{
69
82struct NodeData{
84 std::vector<int> point;
86 std::vector<double> value;
87};
88
101 double weight;
103 std::vector<int> tensor;
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){
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
232public:
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 {}
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
278
281
283 void addTensor(const int *tensor, std::function<int(int)> getNumPoints, double weight);
284
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
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 {}
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
Generic 2D data structure divided into contiguous strips of fixed length (similar to a matrix).
Definition tsgIndexSets.hpp:104
T * getStrip(int i)
Returns a reference to the i-th strip.
Definition tsgIndexSets.hpp:141
std::vector< T > release()
Moves the data vector out of the class, this method invalidates the object.
Definition tsgIndexSets.hpp:164
Helper class that stores data from dynamic construction of a Global grid.
Definition tsgDConstructGridGlobal.hpp:231
void clearTesnors()
Delete the tensors with non-negative weights, i.e., clear all but the tensors selected by the initial...
void write(std::ostream &os) const
Write the data to a stream using ascii or binary format.
void reloadPoints(std::function< int(int)> getNumPoints)
Called after read, reinitializes the points and loaded structures for the tensors.
~DynamicConstructorDataGlobal()=default
Default destructor, release all used memory and erase all stored data.
AddPointResult addNewNode(const std::vector< int > &point, const std::vector< double > &value)
Add a new data point with the index and the value, returns information about the tensor.
void ejectCompleteTensor(MultiIndexSet const &current_tensors, MultiIndexSet &new_tensors, MultiIndexSet &new_points, StorageSet &vals)
Returns a new set of tensors, points and values that can be added to the current tensors.
int getMaxTensor() const
Returns the maximum index of any of the stored tensors.
DynamicConstructorDataGlobal(std::istream &is, size_t cnum_dimensions, size_t cnum_outputs, iomode)
Read constructor.
Definition tsgDConstructGridGlobal.hpp:247
AddPointResult
Defines the result of adding a point to the list of know points with values.
Definition tsgDConstructGridGlobal.hpp:234
@ tensor_missing
Indicates the point is associated with an unknown tensor.
@ tensor_incomplete
Indicates the point was added to an existing tensor but more points are needed before processing the ...
@ tensor_complete
Indicates the point was added to an existing tensor and the tensor is ready for processing.
DynamicConstructorDataGlobal(size_t cnum_dimensions, size_t cnum_outputs)
Constructor, requires that the dimension and the number of model outputs is specified.
Definition tsgDConstructGridGlobal.hpp:244
void addTensor(const int *tensor, std::function< int(int)> getNumPoints, double weight)
Add a new tensor to the candidates, with the given weight and using getNumPoints() to define the asso...
MultiIndexSet getInitialTensors() const
Get a set of all tensors with negative weight, i.e., the tensors selected for the initial run.
MultiIndexSet getNodesIndexes()
Get the node indexes of the points associated with the candidate tensors, the order is the same as th...
double getMaxTensorWeight() const
Returns the maximum tensor weight.
Definition tsgDConstructGridGlobal.hpp:265
void restrictData(int ibegin, int iend)
Restrict data between ibegin and iend entries.
Definition tsgDConstructGridGlobal.hpp:260
Class that stores multi-indexes in sorted (lexicographical) order.
Definition tsgIndexSets.hpp:234
int getSlot(const int *p) const
Returns the slot containing index p, returns -1 if not found.
int getNumIndexes() const
Returns the number of indexes.
Definition tsgIndexSets.hpp:265
void write(std::ostream &os) const
Write the set to ASCII or binary stream, use with std::ofstream and std::ifstream.
Class that stores values, i.e., model outputs, the order of the values is in sync with the order of s...
Definition tsgIndexSets.hpp:334
void writeNumbers(std::ostream &os, Vals... vals)
Write a bunch of numbers with the same type.
Definition tsgIOHelpers.hpp:383
OutputIteratorLike indexesToNodes(IndexList const &list, RuleLike const &rule, OutputIteratorLike nodes)
Converts int-indexes to double-valued abscissas using the provided rule.
Definition tsgIndexManipulator.hpp:569
std::vector< const T * > makeReverseReferenceVector(const std::forward_list< T > &list)
Takes a list and creates a vector of references in reversed order, needed for I/O so that read can be...
Definition tsgDConstructGridGlobal.hpp:117
std::forward_list< NodeData > readNodeDataList(std::istream &is, size_t num_dimensions, size_t num_outputs)
Reads a NodeData std::forward_list from a file using either binary or ascii format....
Definition tsgDConstructGridGlobal.hpp:152
std::vector< double > listToNodes(std::forward_list< NodeData > const &node_list, size_t num_dimensions, RuleLike const &rule)
Using MultiIndexManipulations::indexesToNodes() convert the node_list to actual points according to t...
Definition tsgDConstructGridGlobal.hpp:197
void writeNodeDataList(const std::forward_list< NodeData > &data, std::ostream &os)
Writes a NodeData std::forward_list to a file using either binary or ascii format....
Definition tsgDConstructGridGlobal.hpp:133
std::vector< double > listToLocalNodes(std::forward_list< NodeData > const &node_list, size_t num_dimensions, callable_method rule)
Using MultiIndexManipulations::indexesToNodes() convert the node_list to actual points according to t...
Definition tsgDConstructGridGlobal.hpp:212
std::forward_list< TensorData > readTensorDataList(std::istream &is, size_t num_dimensions)
Reads a TensorData std::forward_list from a file using either binary or ascii format.
Definition tsgDConstructGridGlobal.hpp:173
constexpr bool mode_ascii
Constant allowing for more expressive selection of ascii and binary mode in IO methods.
Definition tsgIOHelpers.hpp:62
size_t size_mult(IntA a, IntB b)
Converts two integer-like variables to size_t and returns the product..
Definition tsgUtils.hpp:82
Encapsulates the Tasmanian Sparse Grid module.
Definition TasmanianSparseGrid.hpp:68
Holds the pair of point index and model value, the struct is used in a std::forward_list.
Definition tsgDConstructGridGlobal.hpp:82
std::vector< double > value
The values of the model outputs at the point.
Definition tsgDConstructGridGlobal.hpp:86
std::vector< int > point
The multi-index of the point.
Definition tsgDConstructGridGlobal.hpp:84
Holds a std::forward_list of pairs of points indexes and values, and a MultiIndexSet of initial nodes...
Definition tsgDConstructGridGlobal.hpp:312
std::forward_list< NodeData > data
A list of pair indicating point and model output values.
Definition tsgDConstructGridGlobal.hpp:324
void restrictData(int ibegin, int iend)
Restrict data between ibegin and iend entries.
Definition tsgDConstructGridGlobal.hpp:332
void write(std::ostream &os) const
Save to a file in either ascii or binary format.
Definition tsgDConstructGridGlobal.hpp:327
std::vector< double > extractValues(MultiIndexSet const &points)
Remove points from the data and return a vector of the values in the points order.
Definition tsgDConstructGridGlobal.hpp:334
MultiIndexSet initial_points
Keeps track of the initial point set, so those can be computed first.
Definition tsgDConstructGridGlobal.hpp:322
SimpleConstructData(std::istream &is, int num_dimensions, int num_outputs, iomode)
Read constructor, requires the number of dimensions and outputs.
Definition tsgDConstructGridGlobal.hpp:317
SimpleConstructData()=default
Default empty constructor.
Holds the description of a single tensor candidate for inclusion into the grid.
Definition tsgDConstructGridGlobal.hpp:99
MultiIndexSet points
The points associated with the surplus operator, the tensor can be included only when all points are ...
Definition tsgDConstructGridGlobal.hpp:105
std::vector< bool > loaded
For each point false if the model data is not yet available for the point, true otherwise.
Definition tsgDConstructGridGlobal.hpp:107
double weight
The weight indicates the relative importance of the tensor.
Definition tsgDConstructGridGlobal.hpp:101
std::vector< int > tensor
Multi-index of the tensor.
Definition tsgDConstructGridGlobal.hpp:103
Algorithms for manipulating sets of multi-indexes.