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

refactored logger and MonteCarloTransport

parent 4ce7755d
......@@ -26,11 +26,13 @@ StochasticField = LogNormal
Experiment = MLMCExperiment
#Experiment = MLMCOverEpsilon
#initLevels = 3, 4, 5
#initSampleAmount = 12, 6, 3
initLevels = 4, 5, 6
initSampleAmount = 0, 0, 1
epsilon = 0.01
mcOnly = true
mcOnly = false
epsilon_lst = 0.05, 0.01, 0.005
maxLevel = 6
......@@ -44,10 +46,11 @@ functional = Energy
#functional = Goal
#functional = Mass
GeneratorVerbose = 1
MCVerbose = 2
MLMCVerbose = 2
PDEVerbose = 1
GeneratorVerbose = 1
GeneratorPlotting = 4
MCPlotting = 3
......
......@@ -3,6 +3,7 @@ set(MLMC_SRC
Utils.C
MLMCMain.C
MultilevelMonteCarlo.C
MonteCarloLogger.C
mc/MonteCarloElliptic.C
mc/MonteCarloTransport.C
assemble/EllipticAssemble.C
......
#include "MonteCarloLogger.h"
IndentedLogger *IndentedLogger::instance = nullptr;
......@@ -4,160 +4,59 @@
#include "m++.h"
class MultilevelMonteCarloLogger {
private:
Date timestamp;
int verbose = 0;
class IndentedLogger {
static IndentedLogger *instance;
public:
string defaultIndent = " ";
string indent = "";
string innerIndent = defaultIndent;
MultilevelMonteCarloLogger() {
config.get("MLMCVerbose", verbose);
}
void IncreaseIndent() {
indent += defaultIndent;
innerIndent += defaultIndent;
}
string indent;
string innerIndent;
void DecreaseIndent() {
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;
}
const string defaultIndent = " ";
virtual void LogMsgv1(const string &msg) {
vout(1) << innerIndent << msg << endl;
}
vector<Date> timestamps;
virtual void LogMsgv2(const string &msg) {
vout(2) << innerIndent << msg << endl;
IndentedLogger() {
indent = "";
}
};
class MonteCarloLogger : public MultilevelMonteCarloLogger {
private:
Date timestamp;
int verbose = 0;
public:
MonteCarloLogger() : MultilevelMonteCarloLogger() {
ReadConfig(Settings, "MCVerbose", verbose);
indent += defaultIndent;
innerIndent += defaultIndent;
}
void StartMethodMsg() override {
Date start;
timestamp = start;
vout (1) << indent << "<Start MC method>" << endl;
static IndentedLogger *GetInstance() {
if (!instance)
instance = new IndentedLogger;
return instance;
}
void EndMethodMsg() override {
vout (1) << indent << "<End after " << Date() - timestamp << ">" << endl;
int DecreaseIndent() {
indent = indent.erase(0, defaultIndent.length());
}
void LogMsgv1(const string &msg) override {
vout(1) << innerIndent << msg << endl;
}
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;
void IncreaseIndent() {
indent = indent.append(defaultIndent);
}
};
class GeneratorLogger : public MonteCarloLogger {
private:
Date timestamp;
int verbose = 0;
public:
GeneratorLogger() : MonteCarloLogger() {
ReadConfig(Settings, "GeneratorVerbose", verbose);
indent += defaultIndent;
innerIndent += defaultIndent;
string GetIndent() {
return indent;
}
void StartMethodMsg() override {
void StartMethod(const string& msg, int verbose) {
Date start;
timestamp = start;
vout (1) << indent << "<Generating input sample>" << endl;
timestamps.push_back(start);
vout (1) << indent << "<" << msg << ">" << endl;
IncreaseIndent();
}
void EndMethodMsg() override {
vout (1) << indent << "<End after " << Date() - timestamp << ">" << endl;
void EndMethod(int verbose) {
DecreaseIndent();
vout (1) << indent << "<End after " << Date() - timestamps.back() << ">" << endl;
timestamps.erase(timestamps.end());
}
void LogMsgv1(const string &msg) override {
vout(1) << innerIndent << msg << endl;
void InnerMsg(const string& msg, int verbose) {
vout (1) << indent << 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;
void LogMsgFlush(const string &msg, int verbose) {
vout(1) << "\r" << indent << msg << flush;
}
};
#endif //MLMC__MULTILEVELMONTECARLO__HPP
......@@ -38,15 +38,15 @@ void MultilevelMonteCarlo::clearMapMonteCarlo() {
}
void MultilevelMonteCarlo::Method() {
logger->StartMethodMsg();
logger->StartMethod("Start MLMC Method", verbose);
for (auto &mc : mapMonteCarlo)
mc.second->Method();
logger->EndMethodMsg();
logger->EndMethod(verbose);
}
void MultilevelMonteCarlo::Method(const double eps) {
logger->StartMethodMsg();
logger->LogMsgv1("eps: " + to_string(eps));
logger->StartMethod("Start MLMC Method", verbose);
logger->InnerMsg("eps: " + to_string(eps), verbose);
bool converged = false;
while (!converged) {
for (auto &mc : mapMonteCarlo)
......@@ -62,11 +62,11 @@ void MultilevelMonteCarlo::Method(const double eps) {
}
}
wrapUpResults(value, cost, levels, numsamples);
logger->EndMethodMsg();
logger->EndMethod(verbose);
}
void MultilevelMonteCarlo::Method(const vector<double> &epsLst) {
logger->StartMethodMsg();
logger->StartMethod("Start MLMC Method", verbose);
// logger->IncreaseIndent();
for (auto &eps : epsLst) {
initializeMapMonteCarlo();
......@@ -78,7 +78,7 @@ void MultilevelMonteCarlo::Method(const vector<double> &epsLst) {
costOverEpsilon.push_back(cost);
}
// logger->DecreaseIndent();
logger->EndMethodMsg();
logger->EndMethod(verbose);
}
void MultilevelMonteCarlo::estimateExponents(bool excludeBaseLevel) {
......@@ -101,9 +101,9 @@ void MultilevelMonteCarlo::estimateExponents(bool excludeBaseLevel) {
v = linear_fit(level_lst, costs);
gamma = v.front();
logger->LogMsgv2("alpha: " + to_string(alpha) +
logger->InnerMsg("alpha: " + to_string(alpha) +
" beta: " + to_string(beta) +
" gamma: " + to_string(gamma));
" gamma: " + to_string(gamma), verbose);
}
void MultilevelMonteCarlo::updateSampleAmount(const double &eps) {
......@@ -120,7 +120,7 @@ void MultilevelMonteCarlo::updateSampleAmount(const double &eps) {
string msg = "dM:";
for (auto &mc:mapMonteCarlo) msg += " " + to_string(mc.second->dM);
logger->LogMsgv2(msg);
logger->InnerMsg(msg, verbose);
}
bool MultilevelMonteCarlo::checkForNewLevel() {
......@@ -145,7 +145,7 @@ double MultilevelMonteCarlo::estimateNumericalError() {
exponent--;
}
logger->LogMsgv2("err: " + to_string(maxErr));
logger->InnerMsg("err: " + to_string(maxErr), verbose);
return maxErr;
}
......@@ -154,7 +154,7 @@ void MultilevelMonteCarlo::appendLevel(double eps) {
if (L > maxLevel) {
Exit ("Maximum level has been reached.")
} else {
logger->LogMsgv2("new level: " + to_string(L));
logger->InnerMsg("new level: " + to_string(L), verbose);
mapMonteCarlo[L] = getMonteCarlo(L, 0, false);
mapMonteCarlo[L]->varY = mapMonteCarlo[L - 1]->varY / pow(2.0, beta);
mapMonteCarlo[L]->avgCost = mapMonteCarlo[L - 1]->avgCost * pow(2.0, gamma);
......
......@@ -35,7 +35,8 @@ private:
public:
int plotting = 0;
MultilevelMonteCarloLogger *logger;
int verbose = 0;
IndentedLogger *logger;
int maxLevel, pLevel;
vector<int> &initLevels, &initSampleAmount;
......@@ -68,7 +69,8 @@ public:
config.get("MLMCPlotting", plotting);
config.get("mcOnly", mcOnly);
logger = new MultilevelMonteCarloLogger();
config.get("MLMCVerbose", verbose);
logger = IndentedLogger::GetInstance();
maxLevel = meshes->Level();
pLevel = meshes->pLevel();
......
......@@ -8,7 +8,7 @@
class DGTransportAssemble : public DGTAssemble {
public:
StochasticTransportProblem *problem;
PDELogger *logger;
IndentedLogger *logger;
string sampleDir = "";
......@@ -19,7 +19,7 @@ public:
Plot *plot) : DGTAssemble(dynamic_cast<DGDiscretization *>(disc),
plot), problem(problem) {
ReadConfig(Settings, "flux_alpha", flux_alpha);
logger = new PDELogger();
logger = IndentedLogger::GetInstance();
}
const char *Name() const override { return "DGTransportAssemble"; }
......@@ -241,7 +241,7 @@ public:
if (abs(rate.second) > 1e-7)
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 {
......
......@@ -14,7 +14,9 @@ public:
int plotting = 0;
Plot *coarsePlot = nullptr;
Plot *finePlot = nullptr;
MonteCarloLogger *logger = nullptr;
int verbose = 0;
IndentedLogger *logger = nullptr;
int l = 0;
int pLevel = 0;
......@@ -35,6 +37,9 @@ public:
double sumQ2 = 0.0, avgQ2 = 0.0, varQ = 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,
Meshes *meshes, StochasticField *stochFields) :
l(l), dM(dM), onlyFineLevel(onlyFineLevel),
......@@ -46,7 +51,9 @@ public:
coarsePlot = new Plot((*meshes)[l - 1], meshes->dim());
finePlot = new Plot((*meshes)[l], meshes->dim());
logger = new MonteCarloLogger();
config.get("MCVerbose", verbose);
logger = IndentedLogger::GetInstance();
}
~MonteCarlo() {
......@@ -56,22 +63,20 @@ public:
}
void Method() {
logger->StartMethodMsg();
logger->StartMethod("Start MC Method", verbose);
if (!onlyFineLevel)
logger->LogMsgv1("l: " + to_string(l)
+ " dM: " + to_string(dM));
logger->InnerMsg("l: " + to_string(l) + " dM: " + to_string(dM), verbose);
else
logger->LogMsgv1("l: " + to_string(l)
+ " dM: " + to_string(dM)
+ " onlyFineLevel: True");
logger->InnerMsg("l: " + to_string(l) + " dM: " + to_string(dM)
+ " onlyFineLevel: True", verbose);
method();
updateAvg();
updateStatistic();
logger->LogMsgv1("|E[Y]|: " + to_string(avgY)
+ " V[Y]: " + to_string(varY));
logger->EndMethodMsg();
logger->InnerMsg("|E[Y]|: " + to_string(avgY) + " V[Y]: " + to_string(varY),
verbose);
logger->EndMethod(verbose);
}
protected:
......
#include "MonteCarloTransport.h"
using namespace std;
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;
fineSolution = 0.0, coarseSolution = 0.0;
double fineQ, coarseQ, fineCost, coarseCost;
fineQ = 0.0, coarseQ = 0.0, fineCost = 0.0, coarseCost = 0.0;
}
void MonteCarloTransport::method() {
for (int m = M; m < M + dM; m++) {
string sampleDirName = mkSampleDirName(m, true);
mkSampleDir(m, sampleDirName);
......@@ -32,6 +25,7 @@ void MonteCarloTransport::method() {
solvePDE(l, true, fineQ, fineCost, fineSolution);
if (!onlyFineLevel) {
sampleDirName = mkSampleDirName(m, false);
mkSampleDir(m, sampleDirName);
......@@ -44,8 +38,8 @@ void MonteCarloTransport::method() {
assemble->problem->LoadNewSample(&coarseNormalFlux);
solvePDE(l, false, coarseQ, coarseCost, coarseSolution);
} else {
coarseQ = 0.0, coarseCost = 0.0;
}
updateSums(fineCost, fineQ - coarseQ, fineQ);
......@@ -57,7 +51,7 @@ void MonteCarloTransport::solvePDE(int l,
double &valueQ,
double &cost,
Vector &solution) {
assemble->logger->StartMethodMsg();
assemble->logger->StartMethod("Start Solving PDE", verbose);
Preconditioner *pc = GetPC("PointBlockGaussSeidel"); //TODO RMV after usage
Solver solver(pc, "GMRES");
......@@ -75,7 +69,7 @@ void MonteCarloTransport::solvePDE(int l,
if (functional == "Outflow")
valueQ = assemble->InFlowOutFlowRate(solution).second;
mout << endl;
assemble->logger->EndMethodMsg();
assemble->logger->EndMethod(verbose);
}
TimeSeries MonteCarloTransport::getTimeSeries(bool fineLevel) {
......
......@@ -30,14 +30,27 @@ public:
MatrixGraphs faceMatrixGraphs;
CellMatrixGraphs solMatrixGraphs;
Vector fineNormalFlux;
Vector coarseNormalFlux;
Vector fineSolution;
Vector coarseSolution;
MonteCarloTransport(int l, int dM, bool baseLevel, Meshes *meshes,
StochasticField *stochFields, DGTransportAssemble *assemble) :
MonteCarlo(l, dM, baseLevel, meshes, stochFields),
assemble(assemble),
cellMatrixGraphs(MatrixGraphs(*meshes, dof("cell", 3))),
faceMatrixGraphs(MatrixGraphs(*meshes, dof("face", 3))),
solMatrixGraphs(*meshes, *assemble->disc) {
solMatrixGraphs(*meshes, *assemble->disc),
fineNormalFlux(Vector(faceMatrixGraphs[l - pLevel])),
coarseNormalFlux(Vector(faceMatrixGraphs[l - pLevel - 1])),
fineSolution(Vector(solMatrixGraphs[l - pLevel])),
coarseSolution(Vector(solMatrixGraphs[l - pLevel - 1])) {
fineNormalFlux = 0.0, coarseNormalFlux = 0.0;
fineSolution = 0.0, coarseSolution = 0.0;
fineQ = 0.0, coarseQ = 0.0, fineCost = 0.0, coarseCost = 0.0;
config.get("startTime", startTime);
config.get("endTime", endTime);
}
......
......@@ -92,17 +92,12 @@ public:
}
VectorField cellB(const cell &c, const Point &x) const override {
// VectorField localFlux;
// for (int d = 0; d < 3; d++)
// localFlux[d] = (*fieldSample)(c(), d);
// return localFlux;
return assemble->EvaluateCellFlux (*fieldSample, c);
}
double faceB(const cell &c, int f,
const VectorField &N, const Point &x) const override {
// return N * VectorField(0.0, -1.0);
return assemble->EvaluateNormalFlux (*fieldSample, c, f);
}
......
......@@ -11,7 +11,9 @@ class SampleGenerator {
protected:
int plotting = 0;
Plot *plot = nullptr;
GeneratorLogger *logger = nullptr;
int verbose = 0;
IndentedLogger *logger = nullptr;
string sampleDir = "";
......@@ -27,7 +29,9 @@ public:
explicit SampleGenerator(int l, Meshes &meshes) : l(l), meshes(meshes) {
config.get("GeneratorPlotting", plotting);
logger = new GeneratorLogger();
config.get("GeneratorVerbose", verbose);
logger = IndentedLogger::GetInstance();
}
~SampleGenerator() {
......@@ -35,18 +39,18 @@ public:
}
void GetFineSample(Vector &fineField) {
logger->StartMethodMsg();
logger->StartMethod("Start Generator Fine Sample", verbose);
generateFineSample(fineField);
logger->EndMethodMsg();
logger->EndMethod(verbose);
}
void GetCoarseSample(const Vector &fineField, Vector &coarseField) {
logger->StartMethodMsg();
logger->StartMethod("Start Generator Coarse Sample", verbose);
generateCoarseSample(fineField, coarseField);
logger->EndMethodMsg();
logger->EndMethod(verbose);
}
void SetSampleDir(const string& sampleDirName) {
void SetSampleDir(const string &sampleDirName) {
sampleDir = sampleDirName;
}
......
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