MonteCarlo.hpp 2.69 KB
Newer Older
1
2
3
#ifndef MLMC_MC_HPP
#define MLMC_MC_HPP

niklas.baumgarten's avatar
niklas.baumgarten committed
4
#include "montecarlo/datastructure/EmpiricMeasures.hpp"
5
6
7
#include "pdesolver/PDESolverCreator.hpp"

#include "mesh/MeshesCreator.hpp"
8
9


10
class MonteCarlo {
niklas.baumgarten's avatar
niklas.baumgarten committed
11
protected:
niklas.baumgarten's avatar
niklas.baumgarten committed
12
    int verbose = 1;
13

niklas.baumgarten's avatar
niklas.baumgarten committed
14
15
    int plotting = 0;

16
    std::string problemName;
17

18
    std::string modelName;
19
20
21

    int degree = 1;

22
23
    bool onlyFine = false;

24
    Level level;
niklas.baumgarten's avatar
niklas.baumgarten committed
25

26
27
28
29
30
31
32
    Meshes *meshes;

    PDESolver *pdeSolver;

    MatrixGraphs *solMGraphs;

public:
niklas.baumgarten's avatar
niklas.baumgarten committed
33
34
35
36
37
38
    SampleCounter ctr;
    Sums sums;
    Averages avgs;
    Variances vars;
    Kurtosis kurtosis;

niklas.baumgarten's avatar
niklas.baumgarten committed
39
40
41
    SampleID coarseId;
    SampleID fineId;

42
    MonteCarlo(Level level, int dM, bool onlyFine) :
43
44
45
        level(level), ctr(SampleCounter(dM)), onlyFine(onlyFine) {

        readConfigEntries();
niklas.baumgarten's avatar
niklas.baumgarten committed
46

47
        // Todo make function problemName -> meshes
48
49
        if (problemName.find("1D") != string::npos)
            meshes = MeshesCreator("Interval").
50
                WithCommSplit(ctr.commSplit).
51
52
53
                WithPLevel(level.coarse).
                WithLevel(level.fine).
                Create();
niklas.baumgarten's avatar
niklas.baumgarten committed
54

55
56
        else if (problemName.find("2D") != string::npos)
            meshes = MeshesCreator("Square").
57
                WithCommSplit(ctr.commSplit).
58
59
60
                WithPLevel(level.coarse).
                WithLevel(level.fine).
                Create();
61

62
63
        else
            meshes = MeshesCreator("Square").
64
                WithCommSplit(ctr.commSplit).
65
66
67
68
                WithPLevel(level.coarse).
                WithLevel(level.fine).
                Create();

69
        pdeSolver = PDESolverCreator().
70
71
72
            WithProblem(problemName).
            WithModel(modelName).
            WithDegree(degree).
73
            Create(*meshes);
74
75

        solMGraphs = pdeSolver->CreateSolutionMatrixGraphs(*meshes);
niklas.baumgarten's avatar
niklas.baumgarten committed
76
77
78
79
80

        fineId.level = level;
        fineId.coarse = false;
        coarseId.level = level;
        coarseId.coarse = true;
81
    }
niklas.baumgarten's avatar
niklas.baumgarten committed
82

83
84
85
    MonteCarlo(Level level, int dM, bool onlyFine,
               MeshesCreator meshesCreator, PDESolverCreator pdeCreator) :
        level(level), ctr(SampleCounter(dM)), onlyFine(onlyFine) {
niklas.baumgarten's avatar
niklas.baumgarten committed
86
87
88

        readConfigEntries();

niklas.baumgarten's avatar
bug fix    
niklas.baumgarten committed
89
90
        meshes = meshesCreator.Create();
        pdeSolver = pdeCreator.Create(*meshes);
niklas.baumgarten's avatar
niklas.baumgarten committed
91
92
93
94
95
96
97
98
        solMGraphs = pdeSolver->CreateSolutionMatrixGraphs(*meshes);

        fineId.level = level;
        fineId.coarse = false;
        coarseId.level = level;
        coarseId.coarse = true;
    }

99
    ~MonteCarlo() {
niklas.baumgarten's avatar
bug fix    
niklas.baumgarten committed
100
101
102
        if (!solMGraphs) delete solMGraphs;
        if (!pdeSolver) delete pdeSolver;
        if (!meshes) delete meshes;
niklas.baumgarten's avatar
niklas.baumgarten committed
103
    }
104

105
    void Method();
niklas.baumgarten's avatar
bug fix    
niklas.baumgarten committed
106
107
108
109
110
111
112

private:
    void method();

    void computeSampleSolution(int m, SampleID &id, SampleSolution &solution);

    void readConfigEntries();
113
114
115
};

#endif //MLMC_MC_HPP