Commit 580bbbf5 authored by niklas.baumgarten's avatar niklas.baumgarten
Browse files

refactored logger and MonteCarloTransport

parent 4ce7755d
...@@ -26,11 +26,13 @@ StochasticField = LogNormal ...@@ -26,11 +26,13 @@ StochasticField = LogNormal
Experiment = MLMCExperiment Experiment = MLMCExperiment
#Experiment = MLMCOverEpsilon #Experiment = MLMCOverEpsilon
#initLevels = 3, 4, 5
#initSampleAmount = 12, 6, 3
initLevels = 4, 5, 6 initLevels = 4, 5, 6
initSampleAmount = 0, 0, 1 initSampleAmount = 0, 0, 1
epsilon = 0.01 epsilon = 0.01
mcOnly = true mcOnly = false
epsilon_lst = 0.05, 0.01, 0.005 epsilon_lst = 0.05, 0.01, 0.005
maxLevel = 6 maxLevel = 6
...@@ -44,10 +46,11 @@ functional = Energy ...@@ -44,10 +46,11 @@ functional = Energy
#functional = Goal #functional = Goal
#functional = Mass #functional = Mass
GeneratorVerbose = 1
MCVerbose = 2 MCVerbose = 2
MLMCVerbose = 2 MLMCVerbose = 2
PDEVerbose = 1 PDEVerbose = 1
GeneratorVerbose = 1
GeneratorPlotting = 4 GeneratorPlotting = 4
MCPlotting = 3 MCPlotting = 3
......
...@@ -3,6 +3,7 @@ set(MLMC_SRC ...@@ -3,6 +3,7 @@ set(MLMC_SRC
Utils.C Utils.C
MLMCMain.C MLMCMain.C
MultilevelMonteCarlo.C MultilevelMonteCarlo.C
MonteCarloLogger.C
mc/MonteCarloElliptic.C mc/MonteCarloElliptic.C
mc/MonteCarloTransport.C mc/MonteCarloTransport.C
assemble/EllipticAssemble.C assemble/EllipticAssemble.C
......
#include "MonteCarloLogger.h" #include "MonteCarloLogger.h"
IndentedLogger *IndentedLogger::instance = nullptr;
...@@ -4,160 +4,59 @@ ...@@ -4,160 +4,59 @@
#include "m++.h" #include "m++.h"
class MultilevelMonteCarloLogger { class IndentedLogger {
private: static IndentedLogger *instance;
Date timestamp;
int verbose = 0;
public: string indent;
string defaultIndent = " "; string innerIndent;
string indent = "";
string innerIndent = defaultIndent;
MultilevelMonteCarloLogger() {
config.get("MLMCVerbose", verbose);
}
void IncreaseIndent() {
indent += defaultIndent;
innerIndent += defaultIndent;
}
void DecreaseIndent() { const string defaultIndent = " ";
int pos = indent.rfind(defaultIndent);
if (pos != -1) {
indent.erase(pos, -3);
innerIndent.erase(pos, -3);
}
}
virtual void StartMethodMsg() {
Date start;
timestamp = start;
vout (1) << indent << "<Start MLMC method>" << endl;
}
virtual void EndMethodMsg() {
vout (1) << indent << "<End after " << Date() - timestamp << ">" << endl;
}
virtual void LogMsgv1(const string &msg) { vector<Date> timestamps;
vout(1) << innerIndent << msg << endl;
}
virtual void LogMsgv2(const string &msg) { IndentedLogger() {
vout(2) << innerIndent << msg << endl; indent = "";
} }
};
class MonteCarloLogger : public MultilevelMonteCarloLogger {
private:
Date timestamp;
int verbose = 0;
public: public:
MonteCarloLogger() : MultilevelMonteCarloLogger() { static IndentedLogger *GetInstance() {
ReadConfig(Settings, "MCVerbose", verbose); if (!instance)
indent += defaultIndent; instance = new IndentedLogger;
innerIndent += defaultIndent; return instance;
}
void StartMethodMsg() override {
Date start;
timestamp = start;
vout (1) << indent << "<Start MC method>" << endl;
} }
void EndMethodMsg() override { int DecreaseIndent() {
vout (1) << indent << "<End after " << Date() - timestamp << ">" << endl; indent = indent.erase(0, defaultIndent.length());
} }
void LogMsgv1(const string &msg) override { void IncreaseIndent() {
vout(1) << innerIndent << msg << endl; indent = indent.append(defaultIndent);
}
void LogMsgv2(const string &msg) override {
vout(2) << innerIndent << msg << endl;
}
};
class PDELogger : public MonteCarloLogger {
private:
Date timestamp;
int verbose = 0;
public:
PDELogger() : MonteCarloLogger() {
ReadConfig(Settings, "PDEVerbose", verbose);
indent += defaultIndent;
innerIndent += defaultIndent;
}
void StartMethodMsg() override {
Date start;
timestamp = start;
vout (1) << indent << "<Start solving PDE>" << endl;
}
void EndMethodMsg() override {
vout (1) << indent << "<End after " << Date() - timestamp << ">" << endl;
}
void LogMsgv1(const string &msg) override {
vout(1) << innerIndent << msg << endl;
}
void LogMsgv2(const string &msg) override {
vout(2) << innerIndent << msg << endl;
}
void LogMsgv1Flush(const string &msg) {
vout(1) << "\r" << innerIndent << msg << flush;
}
void LogMsgv2Flush(const string &msg) {
vout(2) << "\r" << innerIndent << msg << flush;
} }
};
class GeneratorLogger : public MonteCarloLogger {
private:
Date timestamp;
int verbose = 0;
public: string GetIndent() {
GeneratorLogger() : MonteCarloLogger() { return indent;
ReadConfig(Settings, "GeneratorVerbose", verbose);
indent += defaultIndent;
innerIndent += defaultIndent;
} }
void StartMethodMsg() override { void StartMethod(const string& msg, int verbose) {
Date start; Date start;
timestamp = start; timestamps.push_back(start);
vout (1) << indent << "<Generating input sample>" << endl; vout (1) << indent << "<" << msg << ">" << endl;
IncreaseIndent();
} }
void EndMethodMsg() override { void EndMethod(int verbose) {
vout (1) << indent << "<End after " << Date() - timestamp << ">" << endl; DecreaseIndent();
vout (1) << indent << "<End after " << Date() - timestamps.back() << ">" << endl;
timestamps.erase(timestamps.end());
} }
void LogMsgv1(const string &msg) override { void InnerMsg(const string& msg, int verbose) {
vout(1) << innerIndent << msg << endl; vout (1) << indent << msg << endl;
} }
void LogMsgv2(const string &msg) override { void LogMsgFlush(const string &msg, int verbose) {
vout(2) << innerIndent << msg << endl; vout(1) << "\r" << indent << msg << flush;
}
void LogMsgv1Flush(const string &msg) {
vout(1) << "\r" << innerIndent << msg << flush;
}
void LogMsgv2Flush(const string &msg) {
vout(2) << "\r" << innerIndent << msg << flush;
} }
}; };
#endif //MLMC__MULTILEVELMONTECARLO__HPP #endif //MLMC__MULTILEVELMONTECARLO__HPP
...@@ -38,15 +38,15 @@ void MultilevelMonteCarlo::clearMapMonteCarlo() { ...@@ -38,15 +38,15 @@ void MultilevelMonteCarlo::clearMapMonteCarlo() {
} }
void MultilevelMonteCarlo::Method() { void MultilevelMonteCarlo::Method() {
logger->StartMethodMsg(); logger->StartMethod("Start MLMC Method", verbose);
for (auto &mc : mapMonteCarlo) for (auto &mc : mapMonteCarlo)
mc.second->Method(); mc.second->Method();
logger->EndMethodMsg(); logger->EndMethod(verbose);
} }
void MultilevelMonteCarlo::Method(const double eps) { void MultilevelMonteCarlo::Method(const double eps) {
logger->StartMethodMsg(); logger->StartMethod("Start MLMC Method", verbose);
logger->LogMsgv1("eps: " + to_string(eps)); logger->InnerMsg("eps: " + to_string(eps), verbose);
bool converged = false; bool converged = false;
while (!converged) { while (!converged) {
for (auto &mc : mapMonteCarlo) for (auto &mc : mapMonteCarlo)
...@@ -62,11 +62,11 @@ void MultilevelMonteCarlo::Method(const double eps) { ...@@ -62,11 +62,11 @@ void MultilevelMonteCarlo::Method(const double eps) {
} }
} }
wrapUpResults(value, cost, levels, numsamples); wrapUpResults(value, cost, levels, numsamples);
logger->EndMethodMsg(); logger->EndMethod(verbose);
} }
void MultilevelMonteCarlo::Method(const vector<double> &epsLst) { void MultilevelMonteCarlo::Method(const vector<double> &epsLst) {
logger->StartMethodMsg(); logger->StartMethod("Start MLMC Method", verbose);
// logger->IncreaseIndent(); // logger->IncreaseIndent();
for (auto &eps : epsLst) { for (auto &eps : epsLst) {
initializeMapMonteCarlo(); initializeMapMonteCarlo();
...@@ -78,7 +78,7 @@ void MultilevelMonteCarlo::Method(const vector<double> &epsLst) { ...@@ -78,7 +78,7 @@ void MultilevelMonteCarlo::Method(const vector<double> &epsLst) {
costOverEpsilon.push_back(cost); costOverEpsilon.push_back(cost);
} }
// logger->DecreaseIndent(); // logger->DecreaseIndent();
logger->EndMethodMsg(); logger->EndMethod(verbose);
} }
void MultilevelMonteCarlo::estimateExponents(bool excludeBaseLevel) { void MultilevelMonteCarlo::estimateExponents(bool excludeBaseLevel) {
...@@ -101,9 +101,9 @@ void MultilevelMonteCarlo::estimateExponents(bool excludeBaseLevel) { ...@@ -101,9 +101,9 @@ void MultilevelMonteCarlo::estimateExponents(bool excludeBaseLevel) {
v = linear_fit(level_lst, costs); v = linear_fit(level_lst, costs);
gamma = v.front(); gamma = v.front();
logger->LogMsgv2("alpha: " + to_string(alpha) + logger->InnerMsg("alpha: " + to_string(alpha) +
" beta: " + to_string(beta) + " beta: " + to_string(beta) +
" gamma: " + to_string(gamma)); " gamma: " + to_string(gamma), verbose);
} }
void MultilevelMonteCarlo::updateSampleAmount(const double &eps) { void MultilevelMonteCarlo::updateSampleAmount(const double &eps) {
...@@ -120,7 +120,7 @@ void MultilevelMonteCarlo::updateSampleAmount(const double &eps) { ...@@ -120,7 +120,7 @@ void MultilevelMonteCarlo::updateSampleAmount(const double &eps) {
string msg = "dM:"; string msg = "dM:";
for (auto &mc:mapMonteCarlo) msg += " " + to_string(mc.second->dM); for (auto &mc:mapMonteCarlo) msg += " " + to_string(mc.second->dM);
logger->LogMsgv2(msg); logger->InnerMsg(msg, verbose);
} }
bool MultilevelMonteCarlo::checkForNewLevel() { bool MultilevelMonteCarlo::checkForNewLevel() {
...@@ -145,7 +145,7 @@ double MultilevelMonteCarlo::estimateNumericalError() { ...@@ -145,7 +145,7 @@ double MultilevelMonteCarlo::estimateNumericalError() {
exponent--; exponent--;
} }
logger->LogMsgv2("err: " + to_string(maxErr)); logger->InnerMsg("err: " + to_string(maxErr), verbose);
return maxErr; return maxErr;
} }
...@@ -154,7 +154,7 @@ void MultilevelMonteCarlo::appendLevel(double eps) { ...@@ -154,7 +154,7 @@ void MultilevelMonteCarlo::appendLevel(double eps) {
if (L > maxLevel) { if (L > maxLevel) {
Exit ("Maximum level has been reached.") Exit ("Maximum level has been reached.")
} else { } else {
logger->LogMsgv2("new level: " + to_string(L)); logger->InnerMsg("new level: " + to_string(L), verbose);
mapMonteCarlo[L] = getMonteCarlo(L, 0, false); mapMonteCarlo[L] = getMonteCarlo(L, 0, false);
mapMonteCarlo[L]->varY = mapMonteCarlo[L - 1]->varY / pow(2.0, beta); mapMonteCarlo[L]->varY = mapMonteCarlo[L - 1]->varY / pow(2.0, beta);
mapMonteCarlo[L]->avgCost = mapMonteCarlo[L - 1]->avgCost * pow(2.0, gamma); mapMonteCarlo[L]->avgCost = mapMonteCarlo[L - 1]->avgCost * pow(2.0, gamma);
......
...@@ -35,7 +35,8 @@ private: ...@@ -35,7 +35,8 @@ private:
public: public:
int plotting = 0; int plotting = 0;
MultilevelMonteCarloLogger *logger; int verbose = 0;
IndentedLogger *logger;
int maxLevel, pLevel; int maxLevel, pLevel;
vector<int> &initLevels, &initSampleAmount; vector<int> &initLevels, &initSampleAmount;
...@@ -68,7 +69,8 @@ public: ...@@ -68,7 +69,8 @@ public:
config.get("MLMCPlotting", plotting); config.get("MLMCPlotting", plotting);
config.get("mcOnly", mcOnly); config.get("mcOnly", mcOnly);
logger = new MultilevelMonteCarloLogger(); config.get("MLMCVerbose", verbose);
logger = IndentedLogger::GetInstance();
maxLevel = meshes->Level(); maxLevel = meshes->Level();
pLevel = meshes->pLevel(); pLevel = meshes->pLevel();
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
class DGTransportAssemble : public DGTAssemble { class DGTransportAssemble : public DGTAssemble {
public: public:
StochasticTransportProblem *problem; StochasticTransportProblem *problem;
PDELogger *logger; IndentedLogger *logger;
string sampleDir = ""; string sampleDir = "";
...@@ -19,7 +19,7 @@ public: ...@@ -19,7 +19,7 @@ public:
Plot *plot) : DGTAssemble(dynamic_cast<DGDiscretization *>(disc), Plot *plot) : DGTAssemble(dynamic_cast<DGDiscretization *>(disc),
plot), problem(problem) { plot), problem(problem) {
ReadConfig(Settings, "flux_alpha", flux_alpha); ReadConfig(Settings, "flux_alpha", flux_alpha);
logger = new PDELogger(); logger = IndentedLogger::GetInstance();
} }
const char *Name() const override { return "DGTransportAssemble"; } const char *Name() const override { return "DGTransportAssemble"; }
...@@ -241,7 +241,7 @@ public: ...@@ -241,7 +241,7 @@ public:
if (abs(rate.second) > 1e-7) if (abs(rate.second) > 1e-7)
msg = msg.append(" OutFlowRate(u) = ").append(to_string(rate.second)); msg = msg.append(" OutFlowRate(u) = ").append(to_string(rate.second));
logger->LogMsgv1Flush(msg); logger->LogMsgFlush(msg, verbose);
} }
void VtkPlotting(double t, const Vector &u) const override { void VtkPlotting(double t, const Vector &u) const override {
......
...@@ -14,7 +14,9 @@ public: ...@@ -14,7 +14,9 @@ public:
int plotting = 0; int plotting = 0;
Plot *coarsePlot = nullptr; Plot *coarsePlot = nullptr;
Plot *finePlot = nullptr; Plot *finePlot = nullptr;
MonteCarloLogger *logger = nullptr;
int verbose = 0;
IndentedLogger *logger = nullptr;
int l = 0; int l = 0;
int pLevel = 0; int pLevel = 0;
...@@ -35,6 +37,9 @@ public: ...@@ -35,6 +37,9 @@ public:
double sumQ2 = 0.0, avgQ2 = 0.0, varQ = 0.0; double sumQ2 = 0.0, avgQ2 = 0.0, varQ = 0.0;
double kurtosis = 0.0; double kurtosis = 0.0;
double fineQ = 0.0, coarseQ = 0.0;
double fineCost = 0.0, coarseCost = 0.0;
MonteCarlo(int l, int dM, bool onlyFineLevel, MonteCarlo(int l, int dM, bool onlyFineLevel,
Meshes *meshes, StochasticField *stochFields) : Meshes *meshes, StochasticField *stochFields) :
l(l), dM(dM), onlyFineLevel(onlyFineLevel), l(l), dM(dM), onlyFineLevel(onlyFineLevel),
...@@ -46,7 +51,9 @@ public: ...@@ -46,7 +51,9 @@ public:
coarsePlot = new Plot((*meshes)[l - 1], meshes->dim()); coarsePlot = new Plot((*meshes)[l - 1], meshes->dim());
finePlot = new Plot((*meshes)[l], meshes->dim()); finePlot = new Plot((*meshes)[l], meshes->dim());
logger = new MonteCarloLogger();
config.get("MCVerbose", verbose);
logger = IndentedLogger::GetInstance();
} }
~MonteCarlo() { ~MonteCarlo() {
...@@ -56,22 +63,20 @@ public: ...@@ -56,22 +63,20 @@ public:
} }
void Method() { void Method() {
logger->StartMethodMsg(); logger->StartMethod("Start MC Method", verbose);
if (!onlyFineLevel) if (!onlyFineLevel)
logger->LogMsgv1("l: " + to_string(l) logger->InnerMsg("l: " + to_string(l) + " dM: " + to_string(dM), verbose);
+ " dM: " + to_string(dM));
else else
logger->LogMsgv1("l: " + to_string(l) logger->InnerMsg("l: " + to_string(l) + " dM: " + to_string(dM)
+ " dM: " + to_string(dM) + " onlyFineLevel: True", verbose);
+ " onlyFineLevel: True");
method(); method();
updateAvg(); updateAvg();
updateStatistic(); updateStatistic();
logger->LogMsgv1("|E[Y]|: " + to_string(avgY) logger->InnerMsg("|E[Y]|: " + to_string(avgY) + " V[Y]: " + to_string(varY),
+ " V[Y]: " + to_string(varY)); verbose);
logger->EndMethodMsg(); logger->EndMethod(verbose);
} }
protected: protected:
......
#include "MonteCarloTransport.h" #include "MonteCarloTransport.h"
using namespace std; using namespace std;
void MonteCarloTransport::initialize() { void MonteCarloTransport::initialize() {
}
void MonteCarloTransport::method() {
Vector fineNormalFlux(faceMatrixGraphs[l - pLevel]);
Vector coarseNormalFlux(faceMatrixGraphs[l - pLevel - 1]);
Vector fineSolution(solMatrixGraphs[l - pLevel]);
Vector coarseSolution(solMatrixGraphs[l - pLevel - 1]);
fineNormalFlux = 0.0, coarseNormalFlux = 0.0; fineNormalFlux = 0.0, coarseNormalFlux = 0.0;
fineSolution = 0.0, coarseSolution = 0.0; fineSolution = 0.0, coarseSolution = 0.0;
double fineQ, coarseQ, fineCost, coarseCost;
fineQ = 0.0, coarseQ = 0.0, fineCost = 0.0, coarseCost = 0.0; fineQ = 0.0, coarseQ = 0.0, fineCost = 0.0, coarseCost = 0.0;
}
void MonteCarloTransport::method() {
for (int m = M; m < M + dM; m++) { for (int m = M; m < M + dM; m++) {
string sampleDirName = mkSampleDirName(m, true); string sampleDirName = mkSampleDirName(m, true);
mkSampleDir(m, sampleDirName); mkSampleDir(m, sampleDirName);
...@@ -32,6 +25,7 @@ void MonteCarloTransport::method() { ...@@ -32,6 +25,7 @@ void MonteCarloTransport::method() {
solvePDE(l, true, fineQ, fineCost, fineSolution); solvePDE(l, true, fineQ, fineCost, fineSolution);
if (!onlyFineLevel) { if (!onlyFineLevel) {
sampleDirName = mkSampleDirName(m, false); sampleDirName = mkSampleDirName(m, false);
mkSampleDir(m, sampleDirName); mkSampleDir(m, sampleDirName);
...@@ -44,8 +38,8 @@ void MonteCarloTransport::method() { ...@@ -44,8 +38,8 @@ void MonteCarloTransport::method() {
assemble->problem->LoadNewSample(&coarseNormalFlux); assemble->problem->LoadNewSample(&coarseNormalFlux);
solvePDE(l, false, coarseQ, coarseCost, coarseSolution); solvePDE(l, false, coarseQ, coarseCost, coarseSolution);
} else { } else {
coarseQ = 0.0, coarseCost = 0.0; coarseQ = 0.0, coarseCost = 0.0;
} }