SparseGridGenerator.hpp 2.6 KB
Newer Older
1
2
3
4
#ifndef SPARSEGRIDGENERATOR_HPP
#define SPARSEGRIDGENERATOR_HPP

#include "TasmanianSparseGrid.hpp"
niklas.baumgarten's avatar
niklas.baumgarten committed
5
#include "SampleGenerator.hpp"
6
7


niklas.baumgarten's avatar
niklas.baumgarten committed
8
class SparseGridGenerator : public SampleGenerator<RVector> {
9
10
11
12
13
14
15
16
17
18
19
protected:
  TasGrid::TasmanianSparseGrid grid;

  TasGrid::TypeOneDRule rule;

  TasGrid::TypeDepth depth;

  int dimension;

  int outputs;

chinsp's avatar
chinsp committed
20
21
  //double alpha;

22
23
  double weight = 0.0;

niklas.baumgarten's avatar
niklas.baumgarten committed
24
  RVector sample{};
25
26

  std::vector<double> weights{};
niklas.baumgarten's avatar
niklas.baumgarten committed
27

28
  std::vector<RVector> samples{};
29

30
31
32
33
34
35
36
37
38
  void fillSampleVector() {
    std::vector<double> points = grid.getPoints();
    for (auto it_start = points.begin(); it_start != points.end(); it_start += dimension)
      samples.emplace_back(std::vector<double>(it_start, it_start + dimension));
  }

  void drawSample(const SampleID &id) override {
    sample = samples[id.number];
    weight = weights[id.number];
niklas.baumgarten's avatar
niklas.baumgarten committed
39
40
  };

41
public:
niklas.baumgarten's avatar
niklas.baumgarten committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

  // Todo:
  //    * Use other grid types. (e.g. for normally distributed rv)
  //    * If so, write tests and split this class
  //    * Maybe overload other generators than SampleGenerator<RVector>
  //
  // Todo:
  //    * Find out which level corresponds to which total degree and exact degree
  //    * Which depth_rule will be the most useful for our purpose
  //
  // Todo:
  //    * Move functions to cpp and only include tasmanian there!
  //
  // Todo:
  //    * Distribute samples ob different processes


59
  SparseGridGenerator(const Meshes &meshes, int dimension, int outputs, int level,
60
61
                      TasGrid::TypeDepth depth = TasGrid::type_level,
                      TasGrid::TypeOneDRule rule = TasGrid::rule_clenshawcurtis) :
niklas.baumgarten's avatar
niklas.baumgarten committed
62
    SampleGenerator(meshes), dimension(dimension), outputs(outputs),
63
    depth(depth), rule(rule) {
64
65
    grid.makeGlobalGrid(dimension, outputs, level, depth, rule);
    weights = grid.getQuadratureWeights();
66
    fillSampleVector();
67
68
69
70
71
72
  }

  void CreateGlobalGrid(int level) {
    grid.makeGlobalGrid(dimension, outputs, level, depth, rule);
  }

73
74
  std::vector<RVector> GetSamples() {
    return samples;
75
76
77
  }

  std::vector<double> GetWeights() {
78
    return weights;
79
80
  }

niklas.baumgarten's avatar
niklas.baumgarten committed
81
82
83
  // Todo implement linear trafo for areas other than [-1, 1]^N
  // Todo Use TASMANIAN interface for that

84
85
86
87
  double SampleWeight(const SampleID &id) {
    return weights[id.number];
  }

88
89
90
91
  int GetNumPoints() {
    return grid.getNumPoints();
  }

niklas.baumgarten's avatar
niklas.baumgarten committed
92
  double SumOfWeights() const {
niklas.baumgarten's avatar
niklas.baumgarten committed
93
    // Todo has to be adapted to area
niklas.baumgarten's avatar
niklas.baumgarten committed
94
95
96
    return 2.0 * dimension;
  }

niklas.baumgarten's avatar
niklas.baumgarten committed
97
98
99
100
  int GetStochDimension() {
    return grid.getNumDimensions();
  }

101
102
103
104
  RVector EvalSample() const override {
    return sample;
  }

niklas.baumgarten's avatar
niklas.baumgarten committed
105
  string Name() const override { return "SparseGridGenerator"; };
106
107
108
};

#endif //SPARSEGRIDGENERATOR_HPP