Commit 53b5e489 authored by niklas.baumgarten's avatar niklas.baumgarten
Browse files

excluding Level class

parent cad33330
Pipeline #145640 failed with stages
in 10 minutes and 16 seconds
...@@ -68,13 +68,13 @@ struct Level { ...@@ -68,13 +68,13 @@ struct Level {
template<typename T> template<typename T>
struct LevelMap { struct LevelMap {
protected: protected:
std::map<Level, T> _levelMap; std::map<int, T> _levelMap;
public: public:
LevelMap() {}; LevelMap() {};
LevelMap(std::initializer_list<std::pair<Level, T>> levelMap) { LevelMap(std::initializer_list<std::pair<int, T>> levelMap) {
for (std::pair<Level, T> pair : levelMap) { for (std::pair<int, T> pair : levelMap) {
_levelMap.emplace(pair.first, T(pair.second)); _levelMap.emplace(pair.first, T(pair.second));
} }
} }
...@@ -82,7 +82,7 @@ public: ...@@ -82,7 +82,7 @@ public:
std::vector<int> GetLevelVector() const { std::vector<int> GetLevelVector() const {
std::vector<int> levelVector; std::vector<int> levelVector;
for (auto &&[level, entry]: _levelMap) for (auto &&[level, entry]: _levelMap)
levelVector.push_back(level.fine); levelVector.push_back(level);
return levelVector; return levelVector;
} }
...@@ -125,7 +125,7 @@ public: ...@@ -125,7 +125,7 @@ public:
auto clear() { _levelMap.clear(); } auto clear() { _levelMap.clear(); }
auto insert(std::pair<Level, T> pair) { _levelMap.insert(pair); } auto insert(std::pair<int, T> pair) { _levelMap.insert(pair); }
auto begin() { return _levelMap.begin(); } auto begin() { return _levelMap.begin(); }
...@@ -143,11 +143,11 @@ public: ...@@ -143,11 +143,11 @@ public:
auto rend() const { return _levelMap.end(); } auto rend() const { return _levelMap.end(); }
auto operator[](Level level) { return _levelMap[level]; } auto operator[](int level) { return _levelMap[level]; }
auto at(Level level) { return _levelMap.at(level); } auto at(int level) { return _levelMap.at(level); }
auto find(Level level) { return _levelMap.find(level); } auto find(int level) { return _levelMap.find(level); }
friend Logging &operator<<(Logging &s, const LevelMap<T> &levelMap) { friend Logging &operator<<(Logging &s, const LevelMap<T> &levelMap) {
s << "["; s << "[";
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
void MonteCarlo::Method() { void MonteCarlo::Method() {
mout.StartBlock("MonteCarlo l=" + to_string(level.fine)); mout.StartBlock("MonteCarlo l=" + to_string(level));
vout(1) << "Start with: " << aggregate; vout(1) << "Start with: " << aggregate;
method(); method();
aggregate.UpdateParallel(); aggregate.UpdateParallel();
......
...@@ -9,90 +9,92 @@ ...@@ -9,90 +9,92 @@
class MonteCarlo { class MonteCarlo {
protected: protected:
int verbose = 1; int verbose = 1;
int plotting = 0; int plotting = 0;
bool onlyFine = false; bool onlyFine = false;
bool parallel = true; bool parallel = true;
Level level; int level;
Meshes *meshes; Meshes *meshes;
PDESolver *pdeSolver; PDESolver *pdeSolver;
public: public:
WelfordAggregate aggregate; WelfordAggregate aggregate;
SampleCounter ctr; // todo remove, is still here for test SampleCounter ctr; // todo remove, is still here for test
Sums sums; Sums sums;
Averages avgs; Averages avgs;
Variances vars; Variances vars;
Kurtosis kurtosis; Kurtosis kurtosis;
PDESolverCreator pdeSolverCreator; PDESolverCreator pdeSolverCreator;
MeshesCreator meshesCreator; MeshesCreator meshesCreator;
SampleID coarseId; SampleID coarseId;
SampleID fineId; SampleID fineId;
MonteCarlo(Level level, int dM, bool onlyFine) : MonteCarlo(int level, int dM, bool onlyFine) :
level(level), level(level),
onlyFine(onlyFine), onlyFine(onlyFine),
pdeSolverCreator(PDESolverCreator()), pdeSolverCreator(PDESolverCreator()),
meshesCreator(MeshesCreator(pdeSolverCreator.GetMeshName()). meshesCreator(MeshesCreator(pdeSolverCreator.GetMeshName()).
WithCommSplit(aggregate.commSplit). // Todo: Is this correct? WithCommSplit(aggregate.commSplit). // Todo: Is this correct?
WithPLevel(level.coarse). WithPLevel(level - 1).
WithLevel(level.fine)) { WithLevel(level)) {
Init(dM); Init(dM);
} }
MonteCarlo(Level level, int dM, bool onlyFine, MonteCarlo(int level, int dM, bool onlyFine,
MeshesCreator meshesCreator, PDESolverCreator pdeCreator) : MeshesCreator meshesCreator, PDESolverCreator pdeCreator) :
level(level), level(level),
onlyFine(onlyFine), onlyFine(onlyFine),
pdeSolverCreator(pdeCreator), pdeSolverCreator(pdeCreator),
meshesCreator(meshesCreator) { meshesCreator(meshesCreator) {
Init(dM); Init(dM);
} }
void Init(int dM) { void Init(int dM) {
config.get("MCPlotting", plotting); config.get("MCPlotting", plotting);
config.get("MCVerbose", verbose); config.get("MCVerbose", verbose);
config.get("MCParallel", parallel); config.get("MCParallel", parallel);
meshes = meshesCreator.Create(); meshes = meshesCreator.Create();
pdeSolver = pdeSolverCreator.Create(*meshes); pdeSolver = pdeSolverCreator.Create(*meshes);
fineId.fLevel = level.fine; fineId.fLevel = level;
fineId.coarse = false; fineId.cLevel = level - 1;
coarseId.cLevel = level.coarse; fineId.coarse = false;
coarseId.coarse = true; coarseId.fLevel = level;
ctr.parallel = parallel; coarseId.cLevel = level - 1;
ctr.UpdateSampleCounter(dM); coarseId.coarse = true;
aggregate.parallel = parallel; ctr.parallel = parallel;
aggregate.UpdateSampleCounter(dM); ctr.UpdateSampleCounter(dM);
} aggregate.parallel = parallel;
aggregate.UpdateSampleCounter(dM);
}
~MonteCarlo() { ~MonteCarlo() {
if (!pdeSolver) delete pdeSolver; if (!pdeSolver) delete pdeSolver;
if (!meshes) delete meshes; if (!meshes) delete meshes;
} }
void Method(); void Method();
private: private:
void method(); void method();
void computeSampleSolution(int m, SampleID &id, SampleSolution &solution); void computeSampleSolution(int m, SampleID &id, SampleSolution &solution);
}; };
#endif //MLMC_MC_HPP #endif //MLMC_MC_HPP
\ No newline at end of file
...@@ -6,8 +6,8 @@ void MultilevelMonteCarlo::initializeMonteCarloMap() { ...@@ -6,8 +6,8 @@ void MultilevelMonteCarlo::initializeMonteCarloMap() {
for (unsigned long i = 0; i < initLevels.size(); i++) { for (unsigned long i = 0; i < initLevels.size(); i++) {
bool onlyFine = (i == 0) || (mcOnly); bool onlyFine = (i == 0) || (mcOnly);
int M = initSampleAmount[i]; int M = initSampleAmount[i];
Level level(initLevels[i]); auto mc = LevelMonteCarloPair(initLevels[i], MonteCarlo(initLevels[i], M, onlyFine));
mcMap.insert(LevelMonteCarloPair(level, MonteCarlo(level, M, onlyFine))); mcMap.insert(mc);
} }
} }
...@@ -52,8 +52,7 @@ void MultilevelMonteCarlo::adaptiveMethod() { ...@@ -52,8 +52,7 @@ void MultilevelMonteCarlo::adaptiveMethod() {
vout(1) << "numErr=" << errors.numeric << endl; vout(1) << "numErr=" << errors.numeric << endl;
if (errors.numeric > epsilon / 2) { if (errors.numeric > epsilon / 2) {
Level newLevel(mcMap.rbegin()->first.fine + 1); mcMap.AppendLevel(epsilon, exponents, mcMap.rbegin()->first + 1);
mcMap.AppendLevel(epsilon, exponents, newLevel);
} else { } else {
errors.Estimate(data); errors.Estimate(data);
vout(1) << "statErr=" << errors.stochastic << endl; vout(1) << "statErr=" << errors.stochastic << endl;
......
...@@ -18,7 +18,7 @@ void MonteCarloMap::UpdateSampleCounter(double epsilon) { ...@@ -18,7 +18,7 @@ void MonteCarloMap::UpdateSampleCounter(double epsilon) {
} }
} }
void MonteCarloMap::AppendLevel(double epsilon, Exponents exponents, Level newLevel) { void MonteCarloMap::AppendLevel(double epsilon, Exponents exponents, int newLevel) {
if (newLevel <= maxLevel) { if (newLevel <= maxLevel) {
auto oldLevel = this->rbegin(); auto oldLevel = this->rbegin();
double varsY = oldLevel->second.vars.Y / pow(2.0, exponents.beta); double varsY = oldLevel->second.vars.Y / pow(2.0, exponents.beta);
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
struct Exponents; struct Exponents;
typedef std::pair<Level, MonteCarlo> LevelMonteCarloPair; typedef std::pair<int, MonteCarlo> LevelMonteCarloPair;
struct MonteCarloMap : public LevelMap<MonteCarlo> { struct MonteCarloMap : public LevelMap<MonteCarlo> {
...@@ -26,14 +26,14 @@ struct MonteCarloMap : public LevelMap<MonteCarlo> { ...@@ -26,14 +26,14 @@ struct MonteCarloMap : public LevelMap<MonteCarlo> {
void UpdateSampleCounter(double epsilon); void UpdateSampleCounter(double epsilon);
void AppendLevel(double epsilon, Exponents exponents, Level newLevel); void AppendLevel(double epsilon, Exponents exponents, int newLevel);
}; };
struct SampleCounterMap : public LevelMap<SampleCounter> { struct SampleCounterMap : public LevelMap<SampleCounter> {
SampleCounterMap() {}; SampleCounterMap() {};
SampleCounterMap(std::initializer_list<std::pair<Level, SampleCounter>> ctrs) : SampleCounterMap(std::initializer_list<std::pair<int, SampleCounter>> ctrs) :
LevelMap<SampleCounter>(ctrs) {}; LevelMap<SampleCounter>(ctrs) {};
void Update(const MonteCarloMap &mcMap); void Update(const MonteCarloMap &mcMap);
...@@ -47,7 +47,7 @@ struct AveragesMap : public LevelMap<Averages> { ...@@ -47,7 +47,7 @@ struct AveragesMap : public LevelMap<Averages> {
AveragesMap() {}; AveragesMap() {};
AveragesMap(std::initializer_list<std::pair<Level, Averages>> avgs) : AveragesMap(std::initializer_list<std::pair<int, Averages>> avgs) :
LevelMap<Averages>(avgs) {}; LevelMap<Averages>(avgs) {};
AveragesMap(const MonteCarloMap &mcMap) { AveragesMap(const MonteCarloMap &mcMap) {
...@@ -61,7 +61,7 @@ struct VariancesMap : public LevelMap<Variances> { ...@@ -61,7 +61,7 @@ struct VariancesMap : public LevelMap<Variances> {
VariancesMap() {}; VariancesMap() {};
VariancesMap(std::initializer_list<std::pair<Level, Variances>> vars) : VariancesMap(std::initializer_list<std::pair<int, Variances>> vars) :
LevelMap<Variances>(vars) {}; LevelMap<Variances>(vars) {};
VariancesMap(const MonteCarloMap &mcMap) { VariancesMap(const MonteCarloMap &mcMap) {
...@@ -75,7 +75,7 @@ struct KurtosisMap : LevelMap<Kurtosis> { ...@@ -75,7 +75,7 @@ struct KurtosisMap : LevelMap<Kurtosis> {
KurtosisMap() {}; KurtosisMap() {};
KurtosisMap(std::initializer_list<std::pair<Level, Kurtosis>> kurtosis) : KurtosisMap(std::initializer_list<std::pair<int, Kurtosis>> kurtosis) :
LevelMap<Kurtosis>(kurtosis) {}; LevelMap<Kurtosis>(kurtosis) {};
KurtosisMap(const MonteCarloMap &mcMap) { KurtosisMap(const MonteCarloMap &mcMap) {
......
...@@ -26,7 +26,7 @@ protected: ...@@ -26,7 +26,7 @@ protected:
WithProblem(GetParam()). WithProblem(GetParam()).
WithQuantity("GeneratorValue"). WithQuantity("GeneratorValue").
WithModel("DummyPDESolver")), WithModel("DummyPDESolver")),
mc(MonteCarlo(Level(level), dM, true, meshesCreator, pdeSolverCreator)) { mc(MonteCarlo(level, dM, true, meshesCreator, pdeSolverCreator)) {
mc.Method(); mc.Method();
} }
......
...@@ -6,60 +6,60 @@ ...@@ -6,60 +6,60 @@
struct TestData { struct TestData {
MonteCarloMap _mcMap = { MonteCarloMap _mcMap = {
{Level(3), MonteCarlo(Level(3), 0, true)}, {3, MonteCarlo(3, 0, true)},
{Level(4), MonteCarlo(Level(4), 0, true)}, {4, MonteCarlo(4, 0, true)},
{Level(5), MonteCarlo(Level(5), 0, true)}, {5, MonteCarlo(5, 0, true)},
{Level(6), MonteCarlo(Level(6), 0, true)} {6, MonteCarlo(6, 0, true)}
}; };
SampleCounterMap _ctrs = { SampleCounterMap _ctrs = {
{Level(3), SampleCounter(1600, 0)}, {3, SampleCounter(1600, 0)},
{Level(4), SampleCounter(400, 0)}, {4, SampleCounter(400, 0)},
{Level(5), SampleCounter(100, 0)}, {5, SampleCounter(100, 0)},
{Level(6), SampleCounter(25, 0)} {6, SampleCounter(25, 0)}
}; };
AveragesMap _avgs = { AveragesMap _avgs = {
{Level(3), Averages(2.0e-02, 4.0e-04, 8.0e-06, 16.0e-08, {3, Averages(2.0e-02, 4.0e-04, 8.0e-06, 16.0e-08,
2.0e-02, 4.0e-04, 8.0e-06, 16.0e-08, 2.0e-02, 4.0e-04, 8.0e-06, 16.0e-08,
400)}, 400)},
{Level(4), Averages(1.0e-02, 2.0e-04, 4.0e-06, 8.0e-08, {4, Averages(1.0e-02, 2.0e-04, 4.0e-06, 8.0e-08,
1.0e-02, 2.0e-04, 4.0e-06, 8.0e-08, 1.0e-02, 2.0e-04, 4.0e-06, 8.0e-08,
800)}, 800)},
{Level(5), Averages(0.5e-02, 1.0e-04, 2.0e-06, 4.0e-08, {5, Averages(0.5e-02, 1.0e-04, 2.0e-06, 4.0e-08,
0.5e-02, 1.0e-04, 2.0e-06, 4.0e-08, 0.5e-02, 1.0e-04, 2.0e-06, 4.0e-08,
1600)}, 1600)},
{Level(6), Averages(0.25e-02, 0.5e-04, 1.0e-06, 2.0e-08, {6, Averages(0.25e-02, 0.5e-04, 1.0e-06, 2.0e-08,
0.25e-02, 0.5e-04, 1.0e-06, 2.0e-08, 0.25e-02, 0.5e-04, 1.0e-06, 2.0e-08,
3200)} 3200)}
}; };
VariancesMap _vars = { VariancesMap _vars = {
{Level(3), Variances(64.0e-02, 64.0e-02)}, {3, Variances(64.0e-02, 64.0e-02)},
{Level(4), Variances(16.0e-02, 16.0e-02)}, {4, Variances(16.0e-02, 16.0e-02)},
{Level(5), Variances(4.0e-02, 4.0e-02)}, {5, Variances(4.0e-02, 4.0e-02)},
{Level(6), Variances(1.0e-02, 1.0e-02)} {6, Variances(1.0e-02, 1.0e-02)}
}; };
KurtosisMap _kurtosis = { KurtosisMap _kurtosis = {
{Level(3), Kurtosis()}, {3, Kurtosis()},
{Level(4), Kurtosis()}, {4, Kurtosis()},
{Level(5), Kurtosis()}, {5, Kurtosis()},
{Level(6), Kurtosis()} {6, Kurtosis()}
}; };
const MultiLevelMonteCarloData _data{_ctrs, _avgs, _vars, _kurtosis}; const MultiLevelMonteCarloData _data{_ctrs, _avgs, _vars, _kurtosis};
MonteCarloMap GenerateMonteCarloMapWithData() { MonteCarloMap GenerateMonteCarloMapWithData() {
for (auto &[level, mc] : _mcMap) { for (auto &[level, mc] : _mcMap) {
mc.ctr = _ctrs[level]; mc.ctr = _ctrs[level];
mc.avgs = _avgs[level]; mc.avgs = _avgs[level];
mc.vars = _vars[level]; mc.vars = _vars[level];
mc.kurtosis = _kurtosis[level]; mc.kurtosis = _kurtosis[level];
}
return _mcMap;
} }
return _mcMap;
}
}; };
#endif //TESTDATA_HPP #endif //TESTDATA_HPP
...@@ -25,8 +25,8 @@ TEST_F(TestEmpiricMeasureLevelMaps, TestMonteCarloMapUpdateSampleCounter) { ...@@ -25,8 +25,8 @@ TEST_F(TestEmpiricMeasureLevelMaps, TestMonteCarloMapUpdateSampleCounter) {
} }
TEST_F(TestEmpiricMeasureLevelMaps, TestMonteCarloMapAppendLevel) { TEST_F(TestEmpiricMeasureLevelMaps, TestMonteCarloMapAppendLevel) {
auto newLevel = Level(7); int newLevel = 7;
mcMap.AppendLevel(0.1, Exponents(data), newLevel); mcMap.AppendLevel(0.1, Exponents(data), 7);
EXPECT_FLOAT_EQ(mcMap.find(newLevel)->second.vars.Y, 0.0025); EXPECT_FLOAT_EQ(mcMap.find(newLevel)->second.vars.Y, 0.0025);
EXPECT_FLOAT_EQ(mcMap.find(newLevel)->second.avgs.Cost, 6400.0); EXPECT_FLOAT_EQ(mcMap.find(newLevel)->second.avgs.Cost, 6400.0);
EXPECT_TRUE(data.ctrs[newLevel].dM == 0); EXPECT_TRUE(data.ctrs[newLevel].dM == 0);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment