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 {
template<typename T>
struct LevelMap {
protected:
std::map<Level, T> _levelMap;
std::map<int, T> _levelMap;
public:
LevelMap() {};
LevelMap(std::initializer_list<std::pair<Level, T>> levelMap) {
for (std::pair<Level, T> pair : levelMap) {
LevelMap(std::initializer_list<std::pair<int, T>> levelMap) {
for (std::pair<int, T> pair : levelMap) {
_levelMap.emplace(pair.first, T(pair.second));
}
}
......@@ -82,7 +82,7 @@ public:
std::vector<int> GetLevelVector() const {
std::vector<int> levelVector;
for (auto &&[level, entry]: _levelMap)
levelVector.push_back(level.fine);
levelVector.push_back(level);
return levelVector;
}
......@@ -125,7 +125,7 @@ public:
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(); }
......@@ -143,11 +143,11 @@ public:
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) {
s << "[";
......
......@@ -2,7 +2,7 @@
void MonteCarlo::Method() {
mout.StartBlock("MonteCarlo l=" + to_string(level.fine));
mout.StartBlock("MonteCarlo l=" + to_string(level));
vout(1) << "Start with: " << aggregate;
method();
aggregate.UpdateParallel();
......
......@@ -9,90 +9,92 @@
class MonteCarlo {
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:
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) :
level(level),
onlyFine(onlyFine),
pdeSolverCreator(PDESolverCreator()),
meshesCreator(MeshesCreator(pdeSolverCreator.GetMeshName()).
WithCommSplit(aggregate.commSplit). // Todo: Is this correct?
WithPLevel(level.coarse).
WithLevel(level.fine)) {
Init(dM);
}
MonteCarlo(int level, int dM, bool onlyFine) :
level(level),
onlyFine(onlyFine),
pdeSolverCreator(PDESolverCreator()),
meshesCreator(MeshesCreator(pdeSolverCreator.GetMeshName()).
WithCommSplit(aggregate.commSplit). // Todo: Is this correct?
WithPLevel(level - 1).
WithLevel(level)) {
Init(dM);
}
MonteCarlo(Level level, int dM, bool onlyFine,
MeshesCreator meshesCreator, PDESolverCreator pdeCreator) :
level(level),
onlyFine(onlyFine),
pdeSolverCreator(pdeCreator),
meshesCreator(meshesCreator) {
Init(dM);
}
MonteCarlo(int level, int dM, bool onlyFine,
MeshesCreator meshesCreator, PDESolverCreator pdeCreator) :
level(level),
onlyFine(onlyFine),
pdeSolverCreator(pdeCreator),
meshesCreator(meshesCreator) {
Init(dM);
}
void Init(int dM) {
config.get("MCPlotting", plotting);
config.get("MCVerbose", verbose);
config.get("MCParallel", parallel);
void Init(int dM) {
config.get("MCPlotting", plotting);
config.get("MCVerbose", verbose);
config.get("MCParallel", parallel);
meshes = meshesCreator.Create();
pdeSolver = pdeSolverCreator.Create(*meshes);
meshes = meshesCreator.Create();
pdeSolver = pdeSolverCreator.Create(*meshes);
fineId.fLevel = level.fine;
fineId.coarse = false;
coarseId.cLevel = level.coarse;
coarseId.coarse = true;
ctr.parallel = parallel;
ctr.UpdateSampleCounter(dM);
aggregate.parallel = parallel;
aggregate.UpdateSampleCounter(dM);
}
fineId.fLevel = level;
fineId.cLevel = level - 1;
fineId.coarse = false;
coarseId.fLevel = level;
coarseId.cLevel = level - 1;
coarseId.coarse = true;
ctr.parallel = parallel;
ctr.UpdateSampleCounter(dM);
aggregate.parallel = parallel;
aggregate.UpdateSampleCounter(dM);
}
~MonteCarlo() {
if (!pdeSolver) delete pdeSolver;
if (!meshes) delete meshes;
}
~MonteCarlo() {
if (!pdeSolver) delete pdeSolver;
if (!meshes) delete meshes;
}
void Method();
void Method();
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
\ No newline at end of file
......@@ -6,8 +6,8 @@ void MultilevelMonteCarlo::initializeMonteCarloMap() {
for (unsigned long i = 0; i < initLevels.size(); i++) {
bool onlyFine = (i == 0) || (mcOnly);
int M = initSampleAmount[i];
Level level(initLevels[i]);
mcMap.insert(LevelMonteCarloPair(level, MonteCarlo(level, M, onlyFine)));
auto mc = LevelMonteCarloPair(initLevels[i], MonteCarlo(initLevels[i], M, onlyFine));
mcMap.insert(mc);
}
}
......@@ -52,8 +52,7 @@ void MultilevelMonteCarlo::adaptiveMethod() {
vout(1) << "numErr=" << errors.numeric << endl;
if (errors.numeric > epsilon / 2) {
Level newLevel(mcMap.rbegin()->first.fine + 1);
mcMap.AppendLevel(epsilon, exponents, newLevel);
mcMap.AppendLevel(epsilon, exponents, mcMap.rbegin()->first + 1);
} else {
errors.Estimate(data);
vout(1) << "statErr=" << errors.stochastic << endl;
......
......@@ -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) {
auto oldLevel = this->rbegin();
double varsY = oldLevel->second.vars.Y / pow(2.0, exponents.beta);
......
......@@ -9,7 +9,7 @@
struct Exponents;
typedef std::pair<Level, MonteCarlo> LevelMonteCarloPair;
typedef std::pair<int, MonteCarlo> LevelMonteCarloPair;
struct MonteCarloMap : public LevelMap<MonteCarlo> {
......@@ -26,14 +26,14 @@ struct MonteCarloMap : public LevelMap<MonteCarlo> {
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> {
SampleCounterMap() {};
SampleCounterMap(std::initializer_list<std::pair<Level, SampleCounter>> ctrs) :
SampleCounterMap(std::initializer_list<std::pair<int, SampleCounter>> ctrs) :
LevelMap<SampleCounter>(ctrs) {};
void Update(const MonteCarloMap &mcMap);
......@@ -47,7 +47,7 @@ struct AveragesMap : public LevelMap<Averages> {
AveragesMap() {};
AveragesMap(std::initializer_list<std::pair<Level, Averages>> avgs) :
AveragesMap(std::initializer_list<std::pair<int, Averages>> avgs) :
LevelMap<Averages>(avgs) {};
AveragesMap(const MonteCarloMap &mcMap) {
......@@ -61,7 +61,7 @@ struct VariancesMap : public LevelMap<Variances> {
VariancesMap() {};
VariancesMap(std::initializer_list<std::pair<Level, Variances>> vars) :
VariancesMap(std::initializer_list<std::pair<int, Variances>> vars) :
LevelMap<Variances>(vars) {};
VariancesMap(const MonteCarloMap &mcMap) {
......@@ -75,7 +75,7 @@ struct KurtosisMap : LevelMap<Kurtosis> {
KurtosisMap() {};
KurtosisMap(std::initializer_list<std::pair<Level, Kurtosis>> kurtosis) :
KurtosisMap(std::initializer_list<std::pair<int, Kurtosis>> kurtosis) :
LevelMap<Kurtosis>(kurtosis) {};
KurtosisMap(const MonteCarloMap &mcMap) {
......
......@@ -26,7 +26,7 @@ protected:
WithProblem(GetParam()).
WithQuantity("GeneratorValue").
WithModel("DummyPDESolver")),
mc(MonteCarlo(Level(level), dM, true, meshesCreator, pdeSolverCreator)) {
mc(MonteCarlo(level, dM, true, meshesCreator, pdeSolverCreator)) {
mc.Method();
}
......
......@@ -6,60 +6,60 @@
struct TestData {
MonteCarloMap _mcMap = {
{Level(3), MonteCarlo(Level(3), 0, true)},
{Level(4), MonteCarlo(Level(4), 0, true)},
{Level(5), MonteCarlo(Level(5), 0, true)},
{Level(6), MonteCarlo(Level(6), 0, true)}
};
MonteCarloMap _mcMap = {
{3, MonteCarlo(3, 0, true)},
{4, MonteCarlo(4, 0, true)},
{5, MonteCarlo(5, 0, true)},
{6, MonteCarlo(6, 0, true)}
};
SampleCounterMap _ctrs = {
{Level(3), SampleCounter(1600, 0)},
{Level(4), SampleCounter(400, 0)},
{Level(5), SampleCounter(100, 0)},
{Level(6), SampleCounter(25, 0)}
};
SampleCounterMap _ctrs = {
{3, SampleCounter(1600, 0)},
{4, SampleCounter(400, 0)},
{5, SampleCounter(100, 0)},
{6, SampleCounter(25, 0)}
};
AveragesMap _avgs = {
{Level(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,
400)},
{Level(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,
800)},
{Level(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,
1600)},
{Level(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,
3200)}
};
AveragesMap _avgs = {
{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,
400)},
{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,
800)},
{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,
1600)},
{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,
3200)}
};
VariancesMap _vars = {
{Level(3), Variances(64.0e-02, 64.0e-02)},
{Level(4), Variances(16.0e-02, 16.0e-02)},
{Level(5), Variances(4.0e-02, 4.0e-02)},
{Level(6), Variances(1.0e-02, 1.0e-02)}
};
VariancesMap _vars = {
{3, Variances(64.0e-02, 64.0e-02)},
{4, Variances(16.0e-02, 16.0e-02)},
{5, Variances(4.0e-02, 4.0e-02)},
{6, Variances(1.0e-02, 1.0e-02)}
};
KurtosisMap _kurtosis = {
{Level(3), Kurtosis()},
{Level(4), Kurtosis()},
{Level(5), Kurtosis()},
{Level(6), Kurtosis()}
};
KurtosisMap _kurtosis = {
{3, Kurtosis()},
{4, Kurtosis()},
{5, Kurtosis()},
{6, Kurtosis()}
};
const MultiLevelMonteCarloData _data{_ctrs, _avgs, _vars, _kurtosis};
const MultiLevelMonteCarloData _data{_ctrs, _avgs, _vars, _kurtosis};
MonteCarloMap GenerateMonteCarloMapWithData() {
for (auto &[level, mc] : _mcMap) {
mc.ctr = _ctrs[level];
mc.avgs = _avgs[level];
mc.vars = _vars[level];
mc.kurtosis = _kurtosis[level];
}
return _mcMap;
MonteCarloMap GenerateMonteCarloMapWithData() {
for (auto &[level, mc] : _mcMap) {
mc.ctr = _ctrs[level];
mc.avgs = _avgs[level];
mc.vars = _vars[level];
mc.kurtosis = _kurtosis[level];
}
return _mcMap;
}
};
#endif //TESTDATA_HPP
......@@ -25,8 +25,8 @@ TEST_F(TestEmpiricMeasureLevelMaps, TestMonteCarloMapUpdateSampleCounter) {
}
TEST_F(TestEmpiricMeasureLevelMaps, TestMonteCarloMapAppendLevel) {
auto newLevel = Level(7);
mcMap.AppendLevel(0.1, Exponents(data), newLevel);
int newLevel = 7;
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.avgs.Cost, 6400.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