Commit 2027608e authored by niklas.baumgarten's avatar niklas.baumgarten
Browse files

refactored plotter

parent d6d9f6dd
......@@ -14,10 +14,10 @@ degree = 1
plevel = 2
Functional = L2
#functional = Energy
#funcitonal = H1
#functional = Outflow
#functional = Goal
#Functional = Energy
#Funcitonal = H1
#Functional = Outflow
#Functional = Goal
# ----- Multilevel Monte Carlo -----
maxLevel = 9
......
......@@ -10,6 +10,7 @@ void MainProgram::Initialize() {
problem = createStochasticProblem();
assemble = createAssemble();
plotter = make_shared<MultilevelPlotter>(*meshes);
mlmc = make_unique<MultilevelMonteCarlo>(initLevels, initSampleAmount,
meshes.get(), stochField.get(),
assemble.get());
......
......@@ -58,7 +58,7 @@ void MonteCarlo::mkSampleDir(int m, const string &sampleDirName) {
string pathToData = "data/vtk/";
char *sampleDirNameCharArr =
const_cast<char *> ((pathToData.append(sampleDirName)).c_str());
int status = mkdir(sampleDirNameCharArr, 0777);
mkdir(sampleDirNameCharArr, 0777);
}
string MonteCarlo::buildName(int m, const string &name, const string &suffix) {
......
......@@ -4,7 +4,6 @@
#include "m++.h"
#include "stochastics/StochasticField.h"
#include "utils/IndentedLogger.hpp"
#include <sys/stat.h>
#include <utility>
#include <utils/MultilevelPlotter.hpp>
......
......@@ -4,69 +4,57 @@
using namespace std;
void MonteCarloElliptic::Initialize() {
finePermeability = 0.0, coarsePermeability = 0.0;
fineInput = 0.0, coarseInput = 0.0;
fineSolution = 0.0, coarseSolution = 0.0;
fineQ = 0.0, coarseQ = 0.0, fineCost = 0.0, coarseCost = 0.0;
}
void MonteCarloElliptic::method() {
Initialize();
for (int m = M; m < M + dM; m++) {
// Refactor dir stuff
string sampleDirName = mkSampleDirName(m, true);
mkSampleDir(m, sampleDirName);
stochasticField->SetSampleDir(l, sampleDirName);
stochasticField->GetFineSample(l, finePermeability);
// assemble->sampleDir = sampleDirName;
// assemble->plot = finePlot;
assemble->problem->LoadNewSample(&finePermeability);
plotter->SetDirectory(sampleDirName);
stochasticField->GetFineSample(l, fineInput);
assemble->problem->SetSample(&fineInput);
solvePDE(l, m, true, fineQ, fineCost, fineSolution);
// if (plotting) assemble->PlotResults(fineSolution);
if (!onlyFineLevel) {
sampleDirName = mkSampleDirName(m, false);
mkSampleDir(m, sampleDirName);
stochasticField->SetSampleDir(l, sampleDirName);
// stochasticField->SetPlot(l, coarsePlot);
stochasticField->GetCoarseSample(l, finePermeability, coarsePermeability);
if (plotting)
plotter->PlotVector("u", fineSolution,
1, l, "VertexData");
// assemble->sampleDir = sampleDirName;
// assemble->plot = coarsePlot;
assemble->problem->LoadNewSample(&coarsePermeability);
if (onlyFineLevel)
coarseQ = 0.0, coarseCost = 0.0;
solvePDE(l, 0, false, coarseQ, coarseCost, coarseSolution);
else {
sampleDirName = mkSampleDirName(m, false);
plotter->SetDirectory(sampleDirName);
// if (plotting) assemble->PlotResults(coarseSolution);
stochasticField->GetCoarseSample(l, fineInput, coarseInput);
assemble->problem->SetSample(&coarseInput);
solvePDE(l, 0, false,
coarseQ, coarseCost, coarseSolution);
} else {
coarseQ = 0.0, coarseCost = 0.0;
if (plotting)
plotter->PlotVector("u", coarseSolution,
1, l - 1, "VertexData");
}
updateSums(fineCost, fineQ - coarseQ, fineQ);
}
}
void MonteCarloElliptic::solvePDE(int l,
int m,
bool fineLevel,
double &valueQ,
double &cost,
Vector &solution) {
void MonteCarloElliptic::solvePDE(int l, int m, bool fineLevel,
double &valueQ, double &cost, Vector &solution) {
// Todo refactor
Preconditioner *pc = GetPC("SuperLU");
Solver solver(pc, "GMRES");
Newton newton(solver);
newton(*assemble, solution);
cost = solution.pSize();
if (functional == "L2") valueQ = assemble->L2(solution);
if (functional == "Energy") valueQ = assemble->Energy(solution);
if (functional == "Outflow")
valueQ = assemble->getInflowOutflow(solution).second;
if (functional == "Outflow") valueQ = assemble->getInflowOutflow(solution).second;
if (functional == "Goal") valueQ = assemble->getGoal(solution);
}
\ No newline at end of file
......@@ -24,8 +24,8 @@ public:
MatrixGraphs cellMatrixGraphs;
MatrixGraphs solMatrixGraphs;
Vector finePermeability;
Vector coarsePermeability;
Vector fineInput;
Vector coarseInput;
Vector fineSolution;
Vector coarseSolution;
......@@ -39,8 +39,8 @@ public:
assemble(assemble),
cellMatrixGraphs(MatrixGraphs(*meshes, dof("cell", 3))),
solMatrixGraphs(MatrixGraphs(*meshes, *assemble->disc)),
finePermeability(Vector(cellMatrixGraphs[l - pLevel])),
coarsePermeability(Vector(cellMatrixGraphs[l - pLevel - 1])),
fineInput(Vector(cellMatrixGraphs[l - pLevel])),
coarseInput(Vector(cellMatrixGraphs[l - pLevel - 1])),
fineSolution(Vector(solMatrixGraphs[l - pLevel])),
coarseSolution(Vector(solMatrixGraphs[l - pLevel - 1])) {
......
......@@ -4,7 +4,7 @@
using namespace std;
void MonteCarloTransport::Initialize() {
fineNormalFlux = 0.0, coarseNormalFlux = 0.0;
fineInput = 0.0, coarseInput = 0.0;
fineSolution = 0.0, coarseSolution = 0.0;
fineQ = 0.0, coarseQ = 0.0, fineCost = 0.0, coarseCost = 0.0;
}
......@@ -13,40 +13,38 @@ void MonteCarloTransport::method() {
Initialize();
for (int m = M; m < M + dM; m++) {
string sampleDirName = mkSampleDirName(m, true);
mkSampleDir(m, sampleDirName);
stochasticField->SetSampleDir(l, sampleDirName);
stochasticField->GetFineSample(l, fineNormalFlux);
assemble->problem->LoadNewSample(&fineNormalFlux);
plotter->SetDirectory(sampleDirName);
stochasticField->GetFineSample(l, fineInput);
assemble->problem->SetSample(&fineInput);
solvePDE(l, m, true, fineQ, fineCost, fineSolution);
if (!onlyFineLevel) {
sampleDirName = mkSampleDirName(m, false);
mkSampleDir(m, sampleDirName);
// if (plotting)
// plotter->PlotVector("u", fineSolution,
// 1, l, "VertexData");
stochasticField->SetSampleDir(l, sampleDirName);
stochasticField->GetCoarseSample(l, fineNormalFlux, coarseNormalFlux);
if (onlyFineLevel)
coarseQ = 0.0, coarseCost = 0.0;
assemble->problem->LoadNewSample(&coarseNormalFlux);
else {
sampleDirName = mkSampleDirName(m, false);
plotter->SetDirectory(sampleDirName);
solvePDE(l - 1, m, false, coarseQ, coarseCost, coarseSolution);
} else {
stochasticField->GetCoarseSample(l, fineInput, coarseInput);
assemble->problem->SetSample(&coarseInput);
solvePDE(l - 1, m, false,
coarseQ, coarseCost, coarseSolution);
coarseQ = 0.0, coarseCost = 0.0;
// if (plotting)
// plotter->PlotVector("u", coarseSolution,
// 1, l - 1, "VertexData");
}
updateSums(fineCost, fineQ - coarseQ, fineQ);
}
}
void MonteCarloTransport::solvePDE(int l,
int m,
bool fineLevel,
double &valueQ,
double &cost,
Vector &solution) {
void MonteCarloTransport::solvePDE(int l, int m, bool fineLevel,
double &valueQ, double &cost, Vector &solution) {
logger->StartMethod("Start Solving PDE", verbose);
logger->InnerMsg("l: " + to_string(l) +
" m: " + to_string(m), verbose);
......
......@@ -33,8 +33,8 @@ public:
MatrixGraphs faceMatrixGraphs;
CellMatrixGraphs solMatrixGraphs;
Vector fineNormalFlux;
Vector coarseNormalFlux;
Vector fineInput;
Vector coarseInput;
Vector fineSolution;
Vector coarseSolution;
......@@ -49,8 +49,8 @@ public:
cellMatrixGraphs(MatrixGraphs(*meshes, dof("cell", 3))),
faceMatrixGraphs(MatrixGraphs(*meshes, dof("face", 3))),
solMatrixGraphs(*meshes, *assemble->disc),
fineNormalFlux(Vector(faceMatrixGraphs[l - pLevel])),
coarseNormalFlux(Vector(faceMatrixGraphs[l - pLevel - 1])),
fineInput(Vector(faceMatrixGraphs[l - pLevel])),
coarseInput(Vector(faceMatrixGraphs[l - pLevel - 1])),
fineSolution(Vector(solMatrixGraphs[l - pLevel])),
coarseSolution(Vector(solMatrixGraphs[l - pLevel - 1])) {
......
......@@ -12,7 +12,7 @@ public:
virtual ~StochasticProblem() = default;;
void LoadNewSample(Vector *sample) {
void SetSample(Vector *sample) {
fieldSample = sample;
}
......
......@@ -22,7 +22,9 @@ void CirculantEmbedding1D::generateFineSample(Vector &fineField) {
internalCounter += 1;
internalCounter %= 2;
if (plotting) generatePlot(fineField);
if (plotting)
plotter->PlotVector("permeability", fineField,
1, l, "CellData");
}
void CirculantEmbedding1D::generateCoarseSample(const Vector &fineField,
......@@ -39,7 +41,9 @@ void CirculantEmbedding1D::generateCoarseSample(const Vector &fineField,
coarseField(coarseRow, i) /= c.Children();
}
if (plotting) generatePlot(coarseField);
if (plotting)
plotter->PlotVector("permeability", coarseField, 1,
l - 1, "CellData");
}
vector<complex<double>> CirculantEmbedding1D::generateField() {
......@@ -167,7 +171,9 @@ void CirculantEmbedding2D::generateFineSample(Vector &fineField) {
internalCounter += 1;
internalCounter %= 2;
if (plotting) generatePlot(fineField);
if (plotting)
plotter->PlotVector("permeability", fineField,
1, l, "CellData");
}
void CirculantEmbedding2D::generateCoarseSample(const Vector &fineField,
......@@ -184,7 +190,9 @@ void CirculantEmbedding2D::generateCoarseSample(const Vector &fineField,
coarseField(coarseRow, i) /= c.Children();
}
if (plotting) generatePlot(coarseField);
if (plotting)
plotter->PlotVector("permeability", coarseField,
1, l - 1, "CellData");
}
vector<vector<complex<double>>> CirculantEmbedding2D::generateField() {
......
......@@ -12,15 +12,6 @@ protected:
int internalCounter = 0;
RNManager *rnmg;
void generatePlot(const Vector &sample) override {
string namePerm = sampleDir + "/permeability";
// mout << OUT(sample) << endl;
plotter->PlotVector(namePerm, sample, 1, l, "CellData");
}
public:
double evtol = 1e-10;
string fieldType = "LogNormal";
......
......@@ -5,7 +5,7 @@ void HybridFluxGenerator::generateFineSample(Vector &fineField) {
initialize(true);
permeabilityGenerator->SetSampleDir(sampleDir);
permeabilityGenerator->GetFineSample(*finePerm);
assemble->problem->LoadNewSample(finePerm);
assemble->problem->SetSample(finePerm);
Preconditioner *pc = GetPC("SuperLU");
Solver solver(pc, "GMRES");
......@@ -15,14 +15,16 @@ void HybridFluxGenerator::generateFineSample(Vector &fineField) {
assemble->setFlux(*u, *flux);
assemble->SetNormalFlux(*u, fineField);
if (plotting) generatePlot(*flux);
if (plotting)
plotter->PlotVector("flux", *flux,
3, l, "CellData");
}
void HybridFluxGenerator::generateCoarseSample(const Vector &fineField,
Vector &coarseField) {
initialize(false);
permeabilityGenerator->GetCoarseSample(*finePerm, *coarsePerm);
assemble->problem->LoadNewSample(coarsePerm);
assemble->problem->SetSample(coarsePerm);
Preconditioner *pc = GetPC("SuperLU");
Solver solver(pc, "GMRES");
......@@ -32,7 +34,9 @@ void HybridFluxGenerator::generateCoarseSample(const Vector &fineField,
assemble->setFlux(*u, *flux);
assemble->SetNormalFlux(*u, coarseField);
if (plotting) generatePlot(*flux);
if (plotting)
plotter->PlotVector("flux", *flux,
3, l - 1, "CellData");
}
void HybridFluxGenerator::initialize(bool fineField) {
......
......@@ -29,11 +29,6 @@ protected:
void generateCoarseSample(const Vector &fineField, Vector &coarseField) override;
void generatePlot(const Vector &sample) override {
string nameFlux = sampleDir + "/flux";
plotter->PlotVector(nameFlux, sample, 3, l, "CellData3");
}
public:
explicit HybridFluxGenerator(int l, Meshes &meshes)
: SampleGenerator(l, meshes),
......
......@@ -11,7 +11,6 @@
class SampleGenerator {
protected:
int plotting = 0;
MultilevelPlotter *plotter = nullptr;
int verbose = 0;
IndentedLogger *logger = nullptr;
......@@ -22,18 +21,14 @@ protected:
virtual void generateCoarseSample(const Vector &fineField, Vector &coarseField) = 0;
virtual void generatePlot(const Vector &sample) = 0;
public:
int l;
Meshes &meshes;
explicit SampleGenerator(int l, Meshes &meshes) : l(l), meshes(meshes) {
config.get("GeneratorPlotting", plotting);
config.get("GeneratorVerbose", verbose);
logger = IndentedLogger::GetInstance();
// plotter = MultilevelPlotter::GetInstance(meshes);
}
void GetFineSample(Vector &fineField) {
......
......@@ -27,10 +27,6 @@ public:
generators[l]->GetCoarseSample(fineField, coarseField);
}
void SetSampleDir(int l, const string &sampleDirName) {
generators[l]->SetSampleDir(sampleDirName);
}
private:
SampleGenerator *getGenerator(int l) {
if (generatorName == "CirculantEmbedding") {
......
......@@ -3,11 +3,16 @@
#include "Plot.h"
#include <memory>
#include <sys/stat.h>
//#include "plot/VtuPlot.h"
class MultilevelPlotter {
private:
const string defaultDir = "data/vtk/";
string currentDir = defaultDir;
string currentDirShort = "";
void initializeMap(const vector<int> &levels, const Meshes &meshes) {
for (auto &l: levels) {
PlotMap[l] = new Plot(meshes[l], meshes.dim());
......@@ -23,34 +28,35 @@ public:
initializeMap(levels, meshes);
}
void SetDirectory(const string &dir) {
currentDirShort = dir;
currentDir = defaultDir + dir;
char *currentDirAsCharArr = const_cast<char *> (currentDir.c_str());
mkdir(currentDirAsCharArr, 0777);
}
// map<int, unique_ptr<SerialVtuPlot>> PlotMap;
map<int, Plot *> PlotMap;
// static MultilevelPlotter *GetInstance(const Meshes &meshes) {
// if (!instance)
// instance = new MultilevelPlotter(meshes);
// return instance;
// }
void PlotVector(const string &name, const Vector &data,
int dim, int l, const string &dataType) {
int dataDim, int l, const string &dataType) {
if (dataType == "VertexData") {
PlotMap[l]->vertexdata(data, dim);
if (dim > 1)
PlotMap[l]->vtk_vertexvector(name);
PlotMap[l]->vertexdata(data, dataDim);
if (dataDim > 1)
PlotMap[l]->vtk_vertexvector(currentDirShort + "/" + name);
else
PlotMap[l]->vtk_vertexdata(name);
PlotMap[l]->vtk_vertexdata(currentDirShort + "/" + name);
return;
// PlotMap[l]->AddPointData(name, data, dim);
// PlotMap[l]->PlotFile(name);
}
if (dataType == "CellData") {
PlotMap[l]->celldata(data, dim);
if (dim > 1)
PlotMap[l]->vtk_cellvector(name);
PlotMap[l]->celldata(data, dataDim);
if (dataDim > 1)
PlotMap[l]->vtk_cellvector(currentDirShort + "/" + name);
else
PlotMap[l]->vtk_celldata(name);
PlotMap[l]->vtk_celldata(currentDirShort + "/" + name);
return;
// PlotMap[l]->AddCellData(name, data, dim);
// PlotMap[l]->PlotFile(name);
......
......@@ -26,32 +26,61 @@ protected:
mainProgram->Initialize();
}
void checkPlotting() {
if (!PPM->master())
return;
int level = mainProgram->initLevels[0];
for (auto &sampleAmount : mainProgram->initSampleAmount) {
for (int sample = 0; sample < sampleAmount; sample++) {
string permeability = "sample_" + to_string(level) +
"_" + to_string(sample) + "/permeability";
string u = "sample_" + to_string(level) +
"_" + to_string(sample) + "/u";
ASSERT_TRUE(FileExists("data/vtk/" + permeability + ".vtk"));
ASSERT_TRUE(FileExists("data/vtk/" + u + ".vtk"));
if (level != mainProgram->initLevels[0]) {
string name_coarse = "sample_coarse_" + to_string(level) +
"_" + to_string(sample) + "/permeability";
string u_coarse = "sample_coarse_" + to_string(level) +
"_" + to_string(sample) + "/u";
ASSERT_TRUE(FileExists("data/vtk/" + name_coarse + ".vtk"));
ASSERT_TRUE(FileExists("data/vtk/" + u_coarse + ".vtk"));
}
}
level++;
}
}
void TearDown() {
if (PPM->master()) {
delete logging;
system("rm -rf data/vtk/*");
}
}
};
INSTANTIATE_TEST_CASE_P(TestMainProgram, TestMainProgram, Values(
// Tests with default elliptic config
ConfigMapsForTest{defaultEllipticConfigMap,
std::map<std::string, std::string>{
{"GeneratorPlotting", "0"}, // Todo
{"MCPlotting", "0"}}},
{"GeneratorPlotting", "1"},
{"MCPlotting", "1"}}},
ConfigMapsForTest{defaultEllipticConfigMap,
std::map<std::string, std::string>{
{"GeneratorPlotting", "0"},
{"MCPlotting", "0"},
{"Problem", "StochasticLaplace2D"}}}
// // Tests with default transport config
// ConfigMapsForTest{defaultTransportConfigMap,
// std::map<std::string, std::string>{}},
//
// ConfigMapsForTest{defaultTransportConfigMap,
// std::map<std::string, std::string>{
// {"Problem", "StochasticPollution2D"}}}
{"GeneratorPlotting", "1"},
{"MCPlotting", "1"},
{"Problem", "StochasticLaplace2D"}}},
// Tests with default transport config
ConfigMapsForTest{defaultTransportConfigMap,
std::map<std::string, std::string>{}},
ConfigMapsForTest{defaultTransportConfigMap,
std::map<std::string, std::string>{
{"Problem", "StochasticPollution2D"}}}
));
TEST_P(TestMainProgram, TestInitialize) {
......@@ -66,6 +95,7 @@ TEST_P(TestMainProgram, TestInitialize) {
TEST_P(TestMainProgram, TestRunMLMCExperiment) {
ASSERT_EQ(mainProgram->Run(), 0);
ASSERT_TRUE(mainProgram->mlmc->totalErr < mainProgram->epsilon);
checkPlotting();
}
int main(int argc, char **argv) {
......
......@@ -11,10 +11,6 @@ protected:
meshes = make_shared<Meshes>("UnitSquare", 1, 4);
plotter = make_shared<MultilevelPlotter>(*meshes);
pLevel = meshes->pLevel();
// // Refactor
// logging = new Logging;
// logging->initialize();
}
void TearDown() override {
......
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