Commit 1be73b07 authored by niklas.baumgarten's avatar niklas.baumgarten
Browse files

stochastic fields on new data structure + refactoring

parent e6596357
...@@ -74,11 +74,11 @@ target_link_libraries(MLMC-M++ MLMC sprng SRC LIB_PS ${SUPERLU} blas lapack fftw ...@@ -74,11 +74,11 @@ target_link_libraries(MLMC-M++ MLMC sprng SRC LIB_PS ${SUPERLU} blas lapack fftw
#---------------------------------------------------------------------------------------# #---------------------------------------------------------------------------------------#
# Test Executables # Test Executables
add_executable(TestCirculantEmbedding mlmc/tests/TestCirculantEmbedding.C) #add_executable(TestCirculantEmbedding mlmc/tests/TestCirculantEmbedding.C)
add_executable(TestRNManager mlmc/tests/TestRNManager.C) #add_executable(TestRNManager mlmc/tests/TestRNManager.C)
# Linking # Linking
target_link_libraries(TestCirculantEmbedding MultilevelMonteCarlo sprng SRC SRC_Solver #target_link_libraries(TestCirculantEmbedding MLMC sprng SRC LIB_PS ${SUPERLU}
blas lapack ${SUPERLU} fftw3 m ${GTEST_LIB}) # blas lapack fftw3 m ${GTEST_LIB})
target_link_libraries(TestRNManager MultilevelMonteCarlo sprng fftw3 m ${GTEST_LIB}) #target_link_libraries(TestRNManager MLMC sprng fftw3 m ${GTEST_LIB})
#---------------------------------------------------------------------------------------# #---------------------------------------------------------------------------------------#
loadconf = elliptic.conf loadconf = elliptic.conf
#loadconf = transport.conf #loadconf = transport.conf
loadconf = mlmc.conf loadconf = mlmc.conf
loadconf = stochfield.conf \ No newline at end of file
\ No newline at end of file
Model = LinearFEM Model = LagrangeFEM
#Model = QuadraticFEM
#Model = MixedFEM #Model = MixedFEM
#Model = HybridFEM #Model = HybridFEM
#Model = FVFEM # Don't forget to swith on Overlap, sign and penalty #Model = DGFEM
#Model = LinearDGFEM # Don't forget to swith on Overlap, sign and penalty
#Model = QuadraticDGFEM # Don't forget to swith on Overlap, sign and penalty
#Overlap = dG1 #Overlap = dG1
#sign = 1 #sign = 1
...@@ -18,24 +15,24 @@ Problem = StochasticLaplace2D ...@@ -18,24 +15,24 @@ Problem = StochasticLaplace2D
StochasticField = LogNormal StochasticField = LogNormal
#StochasticField = Gaussian #StochasticField = Gaussian
Experiment = ConvergenceTest #Experiment = ConvergenceTest
#Experiment = MLMCExperiment #Experiment = MLMCExperiment
#Experiment = MLMCOverEpsilon Experiment = MLMCOverEpsilon
size_init = 4 initLevels = 3, 4, 5
l_init = 3 4 5 6 initSampleAmount = 12, 6, 3
M_init = 3 3 3 3
size_init = 3 #l_init = 3, 4, 5
l_init = 3 4 5 #M_init = 45, 15, 5
M_init = 45 15 5
epsilon = 0.01 epsilon = 0.01
size_lst = 2 mcOnly = false
epsilon_lst = 0.05 0.01 epsilon_lst = 0.05, 0.01, 0.005
maxLevel = 8
pLevel = 2
degree = 1
Lmax = 9
plevel = 2
functional = L2 functional = L2
#functional = Energy #functional = Energy
#funcitonal = H1 #funcitonal = H1
...@@ -53,7 +50,7 @@ enablePython = 1 ...@@ -53,7 +50,7 @@ enablePython = 1
mean = 1.0 mean = 1.0
sigma = 1.0 sigma = 1.0
norm_p = 2 norm_p = 2
lambda1 = 0.15 lambda1 = 0.1
lambda2 = 0.15 lambda2 = 0.1
alpha = 1.8 alpha = 1.8
evtol = 1e-10 evtol = 1e-10
\ No newline at end of file
...@@ -9,9 +9,8 @@ set(MLMC_SRC ...@@ -9,9 +9,8 @@ set(MLMC_SRC
assemble/MixedEllipticAssemble.C assemble/MixedEllipticAssemble.C
assemble/HybridEllipticAssemble.C assemble/HybridEllipticAssemble.C
assemble/DGEllipticAssemble.C assemble/DGEllipticAssemble.C
stochfields/RNManager.C stochastics/RNManager.C
stochfields/StochasticFields.C stochastics/CirculantEmbedding.C
stochfields/CirculantEmbedding.C problem/StochasticEllipticProblem.h stochastics/SampleGenerator.h stochastics/HybridFluxCalculator.C stochastics/HybridFluxCalculator.h)
)
add_library(MLMC STATIC ${MLMC_SRC}) add_library(MLMC STATIC ${MLMC_SRC})
#include "MLMCMain.h" #include "MLMCMain.h"
using namespace std; using namespace std;
void MLMCMain::initialize() { void MLMCMain::initialize() {
meshes = getMeshes(); meshes = getMeshes();
disc = getDiscretization(); disc = getDiscretization();
stochFields = getStochasticFields();
// fieldSample = getFieldSampleVector();
// stochasticField = getStochasticField();
stochasticField = new StochasticField(meshes);
problem = getStochasticProblem(); problem = getStochasticProblem();
assemble = getAssemble(); assemble = getAssemble();
graphs = getMatrixGraphs(); graphs = getMatrixGraphs();
mlmc = new MultilevelMonteCarlo(l_init, M_init, meshes, mlmc = new MultilevelMonteCarlo(initLevels, initSampleAmount, meshes,
stochFields, assemble, graphs); stochasticField, assemble, graphs);
} }
Meshes *MLMCMain::getMeshes() { Meshes *MLMCMain::getMeshes() {
if (problemName.find("1D") != string::npos) if (problemName.find("1D") != string::npos)
return new Meshes("Line", plevel, Lmax); return new Meshes("Line", pLevel, maxLevel);
if (problemName.find("2D") != string::npos) if (problemName.find("2D") != string::npos)
return new Meshes("UnitSquare", plevel, Lmax); return new Meshes("UnitSquare", pLevel, maxLevel);
Exit("\nMesh name not found\n") Exit("\nMesh name not found\n")
} }
Discretization *MLMCMain::getDiscretization(string _modelName_) { Discretization *MLMCMain::getDiscretization() {
if (_modelName_.empty()) _modelName_ = modelName; if (modelName == "LagrangeFEM") {
if (_modelName_ == "LinearFEM") if (degree == 1)
return new Discretization("linear", 1, meshes->dim()); return new Discretization("linear", 1, meshes->dim());
if (_modelName_ == "QuadraticFEM") if (degree == 2)
return new Discretization("quadratic", 1, meshes->dim()); return new Discretization("serendipity", 1, meshes->dim());
if (_modelName_ == "MixedFEM" || _modelName_ == "HybridFEM") }
if (modelName == "MixedFEM" || modelName == "HybridFEM")
return new Discretization("RT0_P0", 1, meshes->dim()); return new Discretization("RT0_P0", 1, meshes->dim());
if (_modelName_ == "FVFEM") if (modelName == "DGFEM")
return new DGDiscretization(new DGDoF(0)); return new DGDiscretization(new DGDoF(degree));
if (_modelName_ == "LinearDGFEM") if (modelName == "EGFEM")
return new DGDiscretization(new DGDoF(1)); return new EGDiscretization(new EGDoF(degree));
if (_modelName_ == "QuadraticDGFEM") if (modelName == "LinearWC")
return new DGDiscretization(new DGDoF(2)); return nullptr;
if (modelName == "QuadraticWC")
return nullptr;
if (modelName == "LinearDPG")
return nullptr;
if (modelName == "QuadraticDPG")
return nullptr;
if (modelName == "DGTransport")
return new DGDiscretization(new DGDoF(degree));
Exit("\nDiscretization not implemented yet\n") Exit("\nDiscretization not implemented yet\n")
} }
StochasticFields *MLMCMain::getStochasticFields() { Vector *MLMCMain::getFieldSampleVector() {
return new StochasticFields(fieldName, *meshes); if (problemName == "StochasticLaplace1D" || "StochasticLaplace2D") {
} MatrixGraphs cellMatrixGraphs(*meshes, dof("cell", 1));
return new Vector(cellMatrixGraphs.fine());
StochasticProblem *MLMCMain::getStochasticProblem(string _problemName_) {
if (_problemName_.empty()) _problemName_ = problemName;
if (_problemName_ == "StochasticLaplace1D")
return new StochasticLaplace1D(*stochFields);
if (_problemName_ == "StochasticLaplace2D")
return new StochasticLaplace2D(*stochFields);
if (_problemName_ == "StochasticTransport1D")
return nullptr;
if (_problemName_ == "StochasticTransport2D") {
// Discretization *_disc_ = getDiscretization("HybridFEM");
// StochasticProblem *_problem_ = getStochasticProblem(
// "StochasticLaplace2D");
//
// Assemble *hybridAssemble = getAssemble("HybridFEM",
// "StochasticLaplace2D",
// _disc_, _problem_);
// Vector normalFlux(graphs->fine());
// return new StochasticPollution2D(hybridAssemble, normalFlux, stochFields);
} }
if (problemName == "StochasticPollution1D" || "StochasticPollution2D") {
MatrixGraphs cellMatrixGraphs(*meshes, dof("cell", 3));
return new Vector(cellMatrixGraphs.fine());
}
}
StochasticField *MLMCMain::getStochasticField() {
if (problemName == "StochasticLaplace1D")
return new StochasticField(meshes);
if (problemName == "StochasticLaplace1D")
return new StochasticField(meshes);
if (problemName == "StochasticPollution1D")
return new StochasticVectorField(meshes);
if (problemName == "StochasticPollution2D")
return new StochasticField(meshes);
}
StochasticProblem *MLMCMain::getStochasticProblem() {
if (problemName == "StochasticLaplace1D")
return new StochasticLaplace1D();
if (problemName == "StochasticLaplace2D")
return new StochasticLaplace2D();
if (problemName == "StochasticPollution1D")
return new StochasticPollution1D();
if (problemName == "StochasticPollution2D")
return new StochasticPollution2D();
Exit("\nStochastic problem not implemented yet\n") Exit("\nStochastic problem not implemented yet\n")
return nullptr;
} }
Assemble *MLMCMain::getAssemble(string _modelName_, string _problemName_, Assemble *MLMCMain::getAssemble() {
Discretization *_disc_, if (problemName.find("Laplace") != string::npos) {
StochasticProblem *_problem_) { auto ellipticProblem = dynamic_cast<StochasticEllipticProblem *>(problem);
if (_problemName_.empty()) _problemName_ = problemName; if (modelName == "LagrangeFEM")
if (_modelName_.empty()) _modelName_ = modelName; return new EllipticAssemble(disc, ellipticProblem);
if (_disc_ == nullptr) _disc_ = disc; if (modelName == "MixedFEM")
if (_problem_ == nullptr) _problem_ = problem; return new MixedEllipticAssemble(disc, ellipticProblem);
if (_problemName_.find("Laplace") != string::npos) { if (modelName == "HybridFEM")
if (_modelName_ == "LinearFEM" || _modelName_ == "QuadraticFEM") return new HybridEllipticAssemble(disc, ellipticProblem);
return new EllipticAssemble(_disc_, _problem_); if (modelName == "DGFEM")
if (_modelName_ == "MixedFEM") return new DGEllipticAssemble(disc, ellipticProblem);
return new MixedEllipticAssemble(_disc_, _problem_); } else if (problemName.find("Pollution") != string::npos) {
if (_modelName_ == "HybridFEM") auto transportProblem = dynamic_cast<StochasticTransportProblem *>(problem);
return new HybridEllipticAssemble(_disc_, _problem_); Plot plot(meshes->fine(), meshes->dim(), 2);
if (_modelName_ == "FVFEM" || _modelName_ == "LinearDGFEM" || return new DGTransportAssemble(disc, transportProblem, &plot);
_modelName_ == "QuadraticDGFEM")
return new DGEllipticAssemble(_disc_, _problem_);
} else if (_problemName_.find("Transport") != string::npos) {
if (_modelName_ == "FVFEM")
return nullptr;
if (_modelName_ == "LinearDGFEM")
return nullptr;
if (_modelName_ == "QuadraticDGFEM")
return nullptr;
} }
Exit("\nAssembling for this problem not implemented yet\n") Exit("\nAssembling for this problem not implemented yet\n")
return nullptr;
} }
MatrixGraphs *MLMCMain::getMatrixGraphs(string _modelName_, MatrixGraphs *MLMCMain::getMatrixGraphs() {
Meshes *_meshes_, if (modelName == "LagrangeFEM" || modelName == "MixedFEM")
Discretization *_disc_) { return new MatrixGraphs(*meshes, *disc);
if (_modelName_.empty()) _modelName_ = modelName; if (modelName == "HybridFEM")
if (_meshes_ == nullptr) _meshes_ = meshes; return new MatrixGraphs(*meshes, dof("face", 1));
if (_disc_ == nullptr) _disc_ = disc; if (modelName == "DGFEM")
if (_modelName_ == "LinearFEM" || _modelName_ == "QuadraticFEM" || return new CellMatrixGraphs(*meshes, *disc);
_modelName_ == "MixedFEM") Exit("\nMatrix graph not implemented yet\n")
return new MatrixGraphs(*_meshes_, *_disc_);
if (_modelName_ == "HybridFEM")
return new MatrixGraphs(*_meshes_, dof("face", 1));
if (_modelName_ == "FVFEM" || _modelName_ == "LinearDGFEM" ||
_modelName_ == "QuadraticDGFEM")
return new CellMatrixGraphs(*_meshes_, *_disc_);
Exit("\nMatrix graph Not implemented yet\n")
return nullptr;
} }
void MLMCMain::run() { void MLMCMain::run() {
printParameters(); printParameters();
//Todo ConvergenceTest bool mcOnly
if (experimentName == "ConvergenceTest") { if (experimentName == "ConvergenceTest") {
headConvergenceTest(); headConvergenceTest();
mlmc->method(); mlmc->Method();
mlmc->showMCTable(); mlmc->ShowMCTable();
mlmc->showKurtosisWarning(); mlmc->ShowKurtosisWarning();
mlmc->showExponentResults(); mlmc->ShowExponentResults();
if (enablePython == 1) { if (enablePython == 1) {
system("python3 ../tools/plot_statistics.py ConvergenceTest"); system("python3 ../tools/plot_statistics.py ConvergenceTest");
} }
} else if (experimentName == "MLMCExperiment") { } else if (experimentName == "MLMCExperiment") {
headMLMCExperiment(); headMLMCExperiment();
mlmc->method(epsilon); mlmc->Method(epsilon);
mlmc->showMCTable(); mlmc->ShowMCTable();
mlmc->showResultsMLMCRun(epsilon); mlmc->ShowResultsMLMCRun(epsilon);
mlmc->showKurtosisWarning(); mlmc->ShowKurtosisWarning();
mlmc->showExponentResults(); mlmc->ShowExponentResults();
if (enablePython == 1 && PPM->master()) { if (enablePython == 1 && PPM->master()) {
system("python3 ../tools/plot_statistics.py MLMCExperiment"); system("python3 ../tools/plot_statistics.py MLMCExperiment");
system("python3 ../tools/plot_mlmc.py MLMCExperiment"); system("python3 ../tools/plot_mlmc.py MLMCExperiment");
} }
} else if (experimentName == "MLMCOverEpsilon") { } else if (experimentName == "MLMCOverEpsilon") {
headMLMCOverEpsilon(); headMLMCOverEpsilon();
mlmc->method(epsilon_lst); mlmc->Method(epsilonList);
mlmc->showMCTable(); mlmc->ShowMCTable();
mlmc->showResultsOverEpsilon(epsilon_lst); mlmc->ShowResultsOverEpsilon(epsilonList);
if (enablePython == 1 && PPM->master()) { if (enablePython == 1 && PPM->master()) {
system("python3 ../tools/plot_statistics.py MLMCOverEpsilon"); system("python3 ../tools/plot_statistics.py MLMCOverEpsilon");
system("python3 ../tools/plot_mlmc.py MLMCOverEpsilon"); system("python3 ../tools/plot_mlmc.py MLMCOverEpsilon");
...@@ -156,13 +159,13 @@ void MLMCMain::printParameters() { ...@@ -156,13 +159,13 @@ void MLMCMain::printParameters() {
mout << "Model \t=\t" << modelName << endl; mout << "Model \t=\t" << modelName << endl;
mout << "Experiment\t=\t" << experimentName << endl; mout << "Experiment\t=\t" << experimentName << endl;
mout << "StochField\t=\t" << fieldName << endl; mout << "StochField\t=\t" << fieldName << endl;
mout << "l_init \t=\t" << vec2str(l_init) << endl; mout << "initLevels\t=\t" << vec2str(initLevels) << endl;
mout << "M_init \t=\t" << vec2str(M_init) << endl; mout << "initSample\t=\t" << vec2str(initSampleAmount) << endl;
if (experimentName == "MLMCExperiment") if (experimentName == "MLMCExperiment")
mout << "eps \t=\t" << epsilon << endl; mout << "eps \t=\t" << epsilon << endl;
if (experimentName == "MLMCOverEpsilon") if (experimentName == "MLMCOverEpsilon")
mout << "eps_lst \t=\t" << vec2str(epsilon_lst) << endl; mout << "eps_lst \t=\t" << vec2str(epsilonList) << endl;
mout << "Lmax \t=\t" << Lmax << endl; mout << "maxLevel \t=\t" << maxLevel << endl;
mout << "plevel \t=\t" << plevel << endl; mout << "pLevel \t=\t" << pLevel << endl;
mout << "enablePy \t=\t" << enablePython << endl; mout << "enablePy \t=\t" << enablePython << endl;
} }
...@@ -5,47 +5,55 @@ ...@@ -5,47 +5,55 @@
#include "problem/StochasticEllipticProblem.h" #include "problem/StochasticEllipticProblem.h"
#include "problem/StochasticTransportProblem.h" #include "problem/StochasticTransportProblem.h"
#include "MultilevelMonteCarlo.h" #include "MultilevelMonteCarlo.h"
#include "discgalerkin/DGDiscretization.h" #include "discretization/DGDiscretization.h"
#include "stochastics/StochasticField.h"
class MLMCMain { class MLMCMain {
public: public:
int enablePython = 1;
double epsilon = 0.1;
int size_init = 3, plevel = 2, Lmax = 8, size_lst = 3;
vector<int> l_init = {3, 4, 5}, M_init = {12, 6, 3};
vector<double> epsilon_lst = {0.1, 0.05, 0.01};
string problemName = "", modelName = ""; string problemName = "", modelName = "";
string fieldName = "", experimentName = ""; string fieldName = "", experimentName = "";
int enablePython = 1;
double epsilon = 0.1;
int pLevel = 0, maxLevel = 8, degree = 0;
vector<int> initLevels = {3, 4, 5}, initSampleAmount = {12, 6, 3};
vector<double> epsilonList = {0.1, 0.05, 0.01};
Meshes *meshes = nullptr; Meshes *meshes = nullptr;
Discretization *disc = nullptr; Discretization *disc = nullptr;
StochasticFields *stochFields = nullptr;
StochasticField *stochasticField = nullptr;
StochasticProblem *problem = nullptr; StochasticProblem *problem = nullptr;
Assemble *assemble = nullptr; Assemble *assemble = nullptr;
MatrixGraphs *graphs = nullptr; MatrixGraphs *graphs = nullptr;
Plot *plot = nullptr;
MultilevelMonteCarlo *mlmc = nullptr; MultilevelMonteCarlo *mlmc = nullptr;
MLMCMain() { MLMCMain() {
ReadConfig(Settings, "enablePython", enablePython); config.get("enablePython", enablePython);
ReadConfig(Settings, "epsilon", epsilon); config.get("epsilon", epsilon);
ReadConfig(Settings, "plevel", plevel); config.get("pLevel", pLevel);
ReadConfig(Settings, "Lmax", Lmax); config.get("maxLevel", maxLevel);
ReadConfig(Settings, "size_init", size_init); config.get("degree", degree);
ReadConfig(Settings, "l_init", l_init, size_init); config.get("initLevels", initLevels);
ReadConfig(Settings, "M_init", M_init, size_init); config.get("initSampleAmount", initSampleAmount);
ReadConfig(Settings, "size_lst", size_lst); config.get("epsilonList", epsilonList);
ReadConfig(Settings, "epsilon_lst", epsilon_lst, size_lst); config.get("Problem", problemName);
ReadConfig(Settings, "Problem", problemName); config.get("Model", modelName);
ReadConfig(Settings, "Model", modelName); config.get("StochasticField", fieldName);
ReadConfig(Settings, "StochasticField", fieldName); config.get("Experiment", experimentName);
ReadConfig(Settings, "Experiment", experimentName);
if (initSampleAmount.size() != initLevels.size()) Exit(
"\ninitLevels and initSampleAmount have to be of the same size.\n")
if (maxLevel < initLevels.back()) Exit(
"\nLast element of initLevels has to be smaller or equal to maxLevel.\n")
initialize(); initialize();
} }
~MLMCMain() { ~MLMCMain() {
delete meshes, delete disc, delete stochFields; delete meshes, delete disc, delete stochasticField;
delete problem, delete assemble, delete graphs; delete problem, delete assemble, delete graphs;
delete mlmc; delete mlmc;
} }
...@@ -54,19 +62,17 @@ public: ...@@ -54,19 +62,17 @@ public:
Meshes *getMeshes(); Meshes *getMeshes();
Discretization *getDiscretization(string _modelName_ = ""); Discretization *getDiscretization();
Vector *getFieldSampleVector();
StochasticFields *getStochasticFields(); StochasticField *getStochasticField();
StochasticProblem *getStochasticProblem(string _problemName_ = ""); StochasticProblem *getStochasticProblem();
Assemble *getAssemble(string _modelName_ = "", string _problemName_ = "", Assemble *getAssemble();
Discretization *_disc_ = nullptr,
StochasticProblem *_problem_ = nullptr);
MatrixGraphs *getMatrixGraphs(string _modelName_ = "", MatrixGraphs *getMatrixGraphs();
Meshes *_meshes_ = nullptr,
Discretization *_disc_ = nullptr);
void run(); void run();
......
#include "MultilevelMonteCarlo.h" #include "MultilevelMonteCarlo.h"
using namespace std; using namespace std;
MonteCarlo *MultilevelMonteCarlo::getMonteCarlo(int l, int dM, bool baseLevel) { MonteCarlo *MultilevelMonteCarlo::getMonteCarlo(int l, int dM, bool onlyFineLevel) {
auto ellipticAssemble = dynamic_cast<EllipticAssemble *>(assemble); auto ellipticAssemble = dynamic_cast<EllipticAssemble *>(assemble);
// auto transportAssemble = dynamic_cast<DGTransportAssemble *>(assemble); auto transportAssemble = dynamic_cast<DGTransportAssemble *>(assemble);
if (ellipticAssemble != nullptr) if (ellipticAssemble != nullptr)
return new MonteCarloElliptic(l, dM, baseLevel, meshes, stochFields, return new MonteCarloElliptic(l, dM, onlyFineLevel, meshes,
graphs, ellipticAssemble); stochasticField, ellipticAssemble);
// if (transportAssemble != nullptr) if (transportAssemble != nullptr)
// return new MonteCarloTransport(l, dM, baseLevel, meshes, stochFields, return new MonteCarloTransport(l, dM, onlyFineLevel, meshes,
// graphs, transportAssemble); stochasticField, transportAssemble);
return nullptr; return nullptr;
} }
void MultilevelMonteCarlo::initializeMapMC() { void MultilevelMonteCarlo::initializeMapMonteCarlo() {
clearMapMC(); clearMapMonteCarlo();
for (unsigned long i = 0; i < l_init.size(); i++) { for (unsigned long i = 0; i < initLevels.size(); i++) {
bool baseLevel = i == 0; bool onlyFineLevel = (i == 0) || (mcOnly);
int l = l_init[i], M = M_init[i]; int l = initLevels[i], M = initSampleAmount[i];
map_mc[l] = getMonteCarlo(l, M, baseLevel); mapMonteCarlo[l] = getMonteCarlo(l, M, onlyFineLevel);
} }
} }
void MultilevelMonteCarlo::initializeValuesMLMC() { void MultilevelMonteCarlo::initializeValues() {
levels = {}, numsamples = {}; levels = {}, numsamples = {};
value = 0.0, cost = 0.0;