Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Mpp
MLUQ
Commits
1be73b07
Commit
1be73b07
authored
Dec 16, 2019
by
niklas.baumgarten
Browse files
stochastic fields on new data structure + refactoring
parent
e6596357
Changes
49
Hide whitespace changes
Inline
Side-by-side
CMakeLists.txt
View file @
1be73b07
...
...
@@ -74,11 +74,11 @@ target_link_libraries(MLMC-M++ MLMC sprng SRC LIB_PS ${SUPERLU} blas lapack fftw
#---------------------------------------------------------------------------------------#
# Test Executables
add_executable
(
TestCirculantEmbedding mlmc/tests/TestCirculantEmbedding.C
)
add_executable
(
TestRNManager mlmc/tests/TestRNManager.C
)
#
add_executable(TestCirculantEmbedding mlmc/tests/TestCirculantEmbedding.C)
#
add_executable(TestRNManager mlmc/tests/TestRNManager.C)
# Linking
target_link_libraries
(
TestCirculantEmbedding M
ultilevelMonteCarlo sprng SRC SRC_Solver
blas lapack
${
SUPERLU
}
fftw3 m
${
GTEST_LIB
}
)
target_link_libraries
(
TestRNManager M
ultilevelMonteCarlo
sprng fftw3 m
${
GTEST_LIB
}
)
#
target_link_libraries(TestCirculantEmbedding M
LMC sprng SRC LIB_PS ${SUPERLU}
#
blas lapack fftw3 m ${GTEST_LIB})
#
target_link_libraries(TestRNManager M
LMC
sprng fftw3 m ${GTEST_LIB})
#---------------------------------------------------------------------------------------#
mlmc/conf/m++conf
→
mlmc/conf/m++
.
conf
View file @
1be73b07
loadconf
=
elliptic
.
conf
#loadconf = transport.conf
loadconf = mlmc.conf
loadconf = stochfield.conf
\ No newline at end of file
loadconf
=
mlmc
.
conf
\ No newline at end of file
mlmc/conf/mlmc.conf
View file @
1be73b07
Model
=
LinearFEM
#Model = QuadraticFEM
Model
=
LagrangeFEM
#Model = MixedFEM
#Model = HybridFEM
#Model = FVFEM # Don't forget to swith on Overlap, sign and penalty
#Model = LinearDGFEM # Don't forget to swith on Overlap, sign and penalty
#Model = QuadraticDGFEM # Don't forget to swith on Overlap, sign and penalty
#Model = DGFEM
#Overlap = dG1
#sign = 1
...
...
@@ -18,24 +15,24 @@ Problem = StochasticLaplace2D
StochasticField
=
LogNormal
#StochasticField = Gaussian
Experiment
=
ConvergenceTest
#
Experiment = ConvergenceTest
#Experiment = MLMCExperiment
#
Experiment = MLMCOverEpsilon
Experiment
=
MLMCOverEpsilon
size_init
=
4
l_init
=
3
4
5
6
M_init
=
3
3
3
3
initLevels
=
3
,
4
,
5
initSampleAmount
=
12
,
6
,
3
size_init
=
3
l_init
=
3
4
5
M_init
=
45
15
5
#l_init = 3, 4, 5
#M_init = 45, 15, 5
epsilon
=
0
.
01
size_lst
=
2
epsilon_lst
=
0
.
05
0
.
01
mcOnly
=
false
epsilon_lst
=
0
.
05
,
0
.
01
,
0
.
005
maxLevel
=
8
pLevel
=
2
degree
=
1
Lmax
=
9
plevel
=
2
functional
=
L2
#functional = Energy
#funcitonal = H1
...
...
@@ -53,7 +50,7 @@ enablePython = 1
mean
=
1
.
0
sigma
=
1
.
0
norm_p
=
2
lambda1
=
0
.
1
5
lambda2
=
0
.
1
5
lambda1
=
0
.
1
lambda2
=
0
.
1
alpha
=
1
.
8
evtol
=
1
e
-
10
\ No newline at end of file
mlmc/src/CMakeLists.txt
View file @
1be73b07
...
...
@@ -9,9 +9,8 @@ set(MLMC_SRC
assemble/MixedEllipticAssemble.C
assemble/HybridEllipticAssemble.C
assemble/DGEllipticAssemble.C
stochfields/RNManager.C
stochfields/StochasticFields.C
stochfields/CirculantEmbedding.C
)
stochastics/RNManager.C
stochastics/CirculantEmbedding.C
problem/StochasticEllipticProblem.h stochastics/SampleGenerator.h stochastics/HybridFluxCalculator.C stochastics/HybridFluxCalculator.h
)
add_library
(
MLMC STATIC
${
MLMC_SRC
}
)
mlmc/src/MLMCMain.C
View file @
1be73b07
#include
"MLMCMain.h"
using
namespace
std
;
void
MLMCMain
::
initialize
()
{
meshes
=
getMeshes
();
disc
=
getDiscretization
();
stochFields
=
getStochasticFields
();
// fieldSample = getFieldSampleVector();
// stochasticField = getStochasticField();
stochasticField
=
new
StochasticField
(
meshes
);
problem
=
getStochasticProblem
();
assemble
=
getAssemble
();
graphs
=
getMatrixGraphs
();
mlmc
=
new
MultilevelMonteCarlo
(
l_
init
,
M_ini
t
,
meshes
,
stochField
s
,
assemble
,
graphs
);
mlmc
=
new
MultilevelMonteCarlo
(
init
Levels
,
initSampleAmoun
t
,
meshes
,
stoch
astic
Field
,
assemble
,
graphs
);
}
Meshes
*
MLMCMain
::
getMeshes
()
{
if
(
problemName
.
find
(
"1D"
)
!=
string
::
npos
)
return
new
Meshes
(
"Line"
,
p
l
evel
,
L
max
);
return
new
Meshes
(
"Line"
,
p
L
evel
,
max
Level
);
if
(
problemName
.
find
(
"2D"
)
!=
string
::
npos
)
return
new
Meshes
(
"UnitSquare"
,
p
l
evel
,
L
max
);
return
new
Meshes
(
"UnitSquare"
,
p
L
evel
,
max
Level
);
Exit
(
"
\n
Mesh name not found
\n
"
)
}
Discretization
*
MLMCMain
::
getDiscretization
(
string
_modelName_
)
{
if
(
_modelName_
.
empty
())
_modelName_
=
modelName
;
if
(
_modelName_
==
"LinearFEM"
)
return
new
Discretization
(
"linear"
,
1
,
meshes
->
dim
());
if
(
_modelName_
==
"QuadraticFEM"
)
return
new
Discretization
(
"quadratic"
,
1
,
meshes
->
dim
());
if
(
_modelName_
==
"MixedFEM"
||
_modelName_
==
"HybridFEM"
)
Discretization
*
MLMCMain
::
getDiscretization
()
{
if
(
modelName
==
"LagrangeFEM"
)
{
if
(
degree
==
1
)
return
new
Discretization
(
"linear"
,
1
,
meshes
->
dim
());
if
(
degree
==
2
)
return
new
Discretization
(
"serendipity"
,
1
,
meshes
->
dim
());
}
if
(
modelName
==
"MixedFEM"
||
modelName
==
"HybridFEM"
)
return
new
Discretization
(
"RT0_P0"
,
1
,
meshes
->
dim
());
if
(
_modelName_
==
"FVFEM"
)
return
new
DGDiscretization
(
new
DGDoF
(
0
));
if
(
_modelName_
==
"LinearDGFEM"
)
return
new
DGDiscretization
(
new
DGDoF
(
1
));
if
(
_modelName_
==
"QuadraticDGFEM"
)
return
new
DGDiscretization
(
new
DGDoF
(
2
));
if
(
modelName
==
"DGFEM"
)
return
new
DGDiscretization
(
new
DGDoF
(
degree
));
if
(
modelName
==
"EGFEM"
)
return
new
EGDiscretization
(
new
EGDoF
(
degree
));
if
(
modelName
==
"LinearWC"
)
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
(
"
\n
Discretization not implemented yet
\n
"
)
}
StochasticFields
*
MLMCMain
::
getStochasticFields
()
{
return
new
StochasticFields
(
fieldName
,
*
meshes
);
}
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);
Vector
*
MLMCMain
::
getFieldSampleVector
()
{
if
(
problemName
==
"StochasticLaplace1D"
||
"StochasticLaplace2D"
)
{
MatrixGraphs
cellMatrixGraphs
(
*
meshes
,
dof
(
"cell"
,
1
));
return
new
Vector
(
cellMatrixGraphs
.
fine
());
}
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
(
"
\n
Stochastic problem not implemented yet
\n
"
)
return
nullptr
;
}
Assemble
*
MLMCMain
::
getAssemble
(
string
_modelName_
,
string
_problemName_
,
Discretization
*
_disc_
,
StochasticProblem
*
_problem_
)
{
if
(
_problemName_
.
empty
())
_problemName_
=
problemName
;
if
(
_modelName_
.
empty
())
_modelName_
=
modelName
;
if
(
_disc_
==
nullptr
)
_disc_
=
disc
;
if
(
_problem_
==
nullptr
)
_problem_
=
problem
;
if
(
_problemName_
.
find
(
"Laplace"
)
!=
string
::
npos
)
{
if
(
_modelName_
==
"LinearFEM"
||
_modelName_
==
"QuadraticFEM"
)
return
new
EllipticAssemble
(
_disc_
,
_problem_
);
if
(
_modelName_
==
"MixedFEM"
)
return
new
MixedEllipticAssemble
(
_disc_
,
_problem_
);
if
(
_modelName_
==
"HybridFEM"
)
return
new
HybridEllipticAssemble
(
_disc_
,
_problem_
);
if
(
_modelName_
==
"FVFEM"
||
_modelName_
==
"LinearDGFEM"
||
_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
;
Assemble
*
MLMCMain
::
getAssemble
()
{
if
(
problemName
.
find
(
"Laplace"
)
!=
string
::
npos
)
{
auto
ellipticProblem
=
dynamic_cast
<
StochasticEllipticProblem
*>
(
problem
);
if
(
modelName
==
"LagrangeFEM"
)
return
new
EllipticAssemble
(
disc
,
ellipticProblem
);
if
(
modelName
==
"MixedFEM"
)
return
new
MixedEllipticAssemble
(
disc
,
ellipticProblem
);
if
(
modelName
==
"HybridFEM"
)
return
new
HybridEllipticAssemble
(
disc
,
ellipticProblem
);
if
(
modelName
==
"DGFEM"
)
return
new
DGEllipticAssemble
(
disc
,
ellipticProblem
);
}
else
if
(
problemName
.
find
(
"Pollution"
)
!=
string
::
npos
)
{
auto
transportProblem
=
dynamic_cast
<
StochasticTransportProblem
*>
(
problem
);
Plot
plot
(
meshes
->
fine
(),
meshes
->
dim
(),
2
);
return
new
DGTransportAssemble
(
disc
,
transportProblem
,
&
plot
);
}
Exit
(
"
\n
Assembling for this problem not implemented yet
\n
"
)
return
nullptr
;
}
MatrixGraphs
*
MLMCMain
::
getMatrixGraphs
(
string
_modelName_
,
Meshes
*
_meshes_
,
Discretization
*
_disc_
)
{
if
(
_modelName_
.
empty
())
_modelName_
=
modelName
;
if
(
_meshes_
==
nullptr
)
_meshes_
=
meshes
;
if
(
_disc_
==
nullptr
)
_disc_
=
disc
;
if
(
_modelName_
==
"LinearFEM"
||
_modelName_
==
"QuadraticFEM"
||
_modelName_
==
"MixedFEM"
)
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
(
"
\n
Matrix graph Not implemented yet
\n
"
)
return
nullptr
;
MatrixGraphs
*
MLMCMain
::
getMatrixGraphs
()
{
if
(
modelName
==
"LagrangeFEM"
||
modelName
==
"MixedFEM"
)
return
new
MatrixGraphs
(
*
meshes
,
*
disc
);
if
(
modelName
==
"HybridFEM"
)
return
new
MatrixGraphs
(
*
meshes
,
dof
(
"face"
,
1
));
if
(
modelName
==
"DGFEM"
)
return
new
CellMatrixGraphs
(
*
meshes
,
*
disc
);
Exit
(
"
\n
Matrix graph not implemented yet
\n
"
)
}
void
MLMCMain
::
run
()
{
printParameters
();
//Todo ConvergenceTest bool mcOnly
if
(
experimentName
==
"ConvergenceTest"
)
{
headConvergenceTest
();
mlmc
->
m
ethod
();
mlmc
->
s
howMCTable
();
mlmc
->
s
howKurtosisWarning
();
mlmc
->
s
howExponentResults
();
mlmc
->
M
ethod
();
mlmc
->
S
howMCTable
();
mlmc
->
S
howKurtosisWarning
();
mlmc
->
S
howExponentResults
();
if
(
enablePython
==
1
)
{
system
(
"python3 ../tools/plot_statistics.py ConvergenceTest"
);
}
}
else
if
(
experimentName
==
"MLMCExperiment"
)
{
headMLMCExperiment
();
mlmc
->
m
ethod
(
epsilon
);
mlmc
->
s
howMCTable
();
mlmc
->
s
howResultsMLMCRun
(
epsilon
);
mlmc
->
s
howKurtosisWarning
();
mlmc
->
s
howExponentResults
();
mlmc
->
M
ethod
(
epsilon
);
mlmc
->
S
howMCTable
();
mlmc
->
S
howResultsMLMCRun
(
epsilon
);
mlmc
->
S
howKurtosisWarning
();
mlmc
->
S
howExponentResults
();
if
(
enablePython
==
1
&&
PPM
->
master
())
{
system
(
"python3 ../tools/plot_statistics.py MLMCExperiment"
);
system
(
"python3 ../tools/plot_mlmc.py MLMCExperiment"
);
}
}
else
if
(
experimentName
==
"MLMCOverEpsilon"
)
{
headMLMCOverEpsilon
();
mlmc
->
m
ethod
(
epsilon
_l
st
);
mlmc
->
s
howMCTable
();
mlmc
->
s
howResultsOverEpsilon
(
epsilon
_l
st
);
mlmc
->
M
ethod
(
epsilon
Li
st
);
mlmc
->
S
howMCTable
();
mlmc
->
S
howResultsOverEpsilon
(
epsilon
Li
st
);
if
(
enablePython
==
1
&&
PPM
->
master
())
{
system
(
"python3 ../tools/plot_statistics.py MLMCOverEpsilon"
);
system
(
"python3 ../tools/plot_mlmc.py MLMCOverEpsilon"
);
...
...
@@ -156,13 +159,13 @@ void MLMCMain::printParameters() {
mout
<<
"Model
\t
=
\t
"
<<
modelName
<<
endl
;
mout
<<
"Experiment
\t
=
\t
"
<<
experimentName
<<
endl
;
mout
<<
"StochField
\t
=
\t
"
<<
fieldName
<<
endl
;
mout
<<
"
l_
init
\t
=
\t
"
<<
vec2str
(
l_
init
)
<<
endl
;
mout
<<
"
M_
init
\t
=
\t
"
<<
vec2str
(
M_
init
)
<<
endl
;
mout
<<
"init
Levels
\t
=
\t
"
<<
vec2str
(
init
Levels
)
<<
endl
;
mout
<<
"init
Sample
\t
=
\t
"
<<
vec2str
(
init
SampleAmount
)
<<
endl
;
if
(
experimentName
==
"MLMCExperiment"
)
mout
<<
"eps
\t
=
\t
"
<<
epsilon
<<
endl
;
if
(
experimentName
==
"MLMCOverEpsilon"
)
mout
<<
"eps_lst
\t
=
\t
"
<<
vec2str
(
epsilon
_l
st
)
<<
endl
;
mout
<<
"
L
max
\t
=
\t
"
<<
L
max
<<
endl
;
mout
<<
"p
l
evel
\t
=
\t
"
<<
p
l
evel
<<
endl
;
mout
<<
"eps_lst
\t
=
\t
"
<<
vec2str
(
epsilon
Li
st
)
<<
endl
;
mout
<<
"max
Level
\t
=
\t
"
<<
max
Level
<<
endl
;
mout
<<
"p
L
evel
\t
=
\t
"
<<
p
L
evel
<<
endl
;
mout
<<
"enablePy
\t
=
\t
"
<<
enablePython
<<
endl
;
}
mlmc/src/MLMCMain.h
View file @
1be73b07
...
...
@@ -5,47 +5,55 @@
#include
"problem/StochasticEllipticProblem.h"
#include
"problem/StochasticTransportProblem.h"
#include
"MultilevelMonteCarlo.h"
#include
"discgalerkin/DGDiscretization.h"
#include
"discretization/DGDiscretization.h"
#include
"stochastics/StochasticField.h"
class
MLMCMain
{
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
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
;
Discretization
*
disc
=
nullptr
;
StochasticFields
*
stochFields
=
nullptr
;
StochasticField
*
stochasticField
=
nullptr
;
StochasticProblem
*
problem
=
nullptr
;
Assemble
*
assemble
=
nullptr
;
MatrixGraphs
*
graphs
=
nullptr
;
Plot
*
plot
=
nullptr
;
MultilevelMonteCarlo
*
mlmc
=
nullptr
;
MLMCMain
()
{
ReadConfig
(
Settings
,
"enablePython"
,
enablePython
);
ReadConfig
(
Settings
,
"epsilon"
,
epsilon
);
ReadConfig
(
Settings
,
"plevel"
,
plevel
);
ReadConfig
(
Settings
,
"Lmax"
,
Lmax
);
ReadConfig
(
Settings
,
"size_init"
,
size_init
);
ReadConfig
(
Settings
,
"l_init"
,
l_init
,
size_init
);
ReadConfig
(
Settings
,
"M_init"
,
M_init
,
size_init
);
ReadConfig
(
Settings
,
"size_lst"
,
size_lst
);
ReadConfig
(
Settings
,
"epsilon_lst"
,
epsilon_lst
,
size_lst
);
ReadConfig
(
Settings
,
"Problem"
,
problemName
);
ReadConfig
(
Settings
,
"Model"
,
modelName
);
ReadConfig
(
Settings
,
"StochasticField"
,
fieldName
);
ReadConfig
(
Settings
,
"Experiment"
,
experimentName
);
config
.
get
(
"enablePython"
,
enablePython
);
config
.
get
(
"epsilon"
,
epsilon
);
config
.
get
(
"pLevel"
,
pLevel
);
config
.
get
(
"maxLevel"
,
maxLevel
);
config
.
get
(
"degree"
,
degree
);
config
.
get
(
"initLevels"
,
initLevels
);
config
.
get
(
"initSampleAmount"
,
initSampleAmount
);
config
.
get
(
"epsilonList"
,
epsilonList
);
config
.
get
(
"Problem"
,
problemName
);
config
.
get
(
"Model"
,
modelName
);
config
.
get
(
"StochasticField"
,
fieldName
);
config
.
get
(
"Experiment"
,
experimentName
);
if
(
initSampleAmount
.
size
()
!=
initLevels
.
size
())
Exit
(
"
\n
initLevels and initSampleAmount have to be of the same size.
\n
"
)
if
(
maxLevel
<
initLevels
.
back
())
Exit
(
"
\n
Last element of initLevels has to be smaller or equal to maxLevel.
\n
"
)
initialize
();
}
~
MLMCMain
()
{
delete
meshes
,
delete
disc
,
delete
stochField
s
;
delete
meshes
,
delete
disc
,
delete
stoch
astic
Field
;
delete
problem
,
delete
assemble
,
delete
graphs
;
delete
mlmc
;
}
...
...
@@ -54,19 +62,17 @@ public:
Meshes
*
getMeshes
();
Discretization
*
getDiscretization
(
string
_modelName_
=
""
);
Discretization
*
getDiscretization
();
Vector
*
getFieldSampleVector
();
StochasticField
s
*
getStochasticField
s
();
StochasticField
*
getStochasticField
();
StochasticProblem
*
getStochasticProblem
(
string
_problemName_
=
""
);
StochasticProblem
*
getStochasticProblem
();
Assemble
*
getAssemble
(
string
_modelName_
=
""
,
string
_problemName_
=
""
,
Discretization
*
_disc_
=
nullptr
,
StochasticProblem
*
_problem_
=
nullptr
);
Assemble
*
getAssemble
();
MatrixGraphs
*
getMatrixGraphs
(
string
_modelName_
=
""
,
Meshes
*
_meshes_
=
nullptr
,
Discretization
*
_disc_
=
nullptr
);
MatrixGraphs
*
getMatrixGraphs
();
void
run
();
...
...
mlmc/src/MultilevelMonteCarlo.C
View file @
1be73b07
#include
"MultilevelMonteCarlo.h"
using
namespace
std
;
MonteCarlo
*
MultilevelMonteCarlo
::
getMonteCarlo
(
int
l
,
int
dM
,
bool
bas
eLevel
)
{
MonteCarlo
*
MultilevelMonteCarlo
::
getMonteCarlo
(
int
l
,
int
dM
,
bool
onlyFin
eLevel
)
{
auto
ellipticAssemble
=
dynamic_cast
<
EllipticAssemble
*>
(
assemble
);
//
auto transportAssemble = dynamic_cast<DGTransportAssemble *>(assemble);
auto
transportAssemble
=
dynamic_cast
<
DGTransportAssemble
*>
(
assemble
);
if
(
ellipticAssemble
!=
nullptr
)
return
new
MonteCarloElliptic
(
l
,
dM
,
bas
eLevel
,
meshes
,
stochFields
,
graphs
,
ellipticAssemble
);
//
if (transportAssemble != nullptr)
//
return new MonteCarloTransport(l, dM,
bas
eLevel, meshes,
stochFields,
//
graphs
, transportAssemble);
return
new
MonteCarloElliptic
(
l
,
dM
,
onlyFin
eLevel
,
meshes
,
stochasticField
,
ellipticAssemble
);
if
(
transportAssemble
!=
nullptr
)
return
new
MonteCarloTransport
(
l
,
dM
,
onlyFin
eLevel
,
meshes
,
stochasticField
,
transportAssemble
);
return
nullptr
;
}
void
MultilevelMonteCarlo
::
initializeMapM
C
()
{
clearMapM
C
();
for
(
unsigned
long
i
=
0
;
i
<
l_
init
.
size
();
i
++
)
{
bool
bas
eLevel
=
i
==
0
;
int
l
=
l_
init
[
i
],
M
=
M_
init
[
i
];
map
_mc
[
l
]
=
getMonteCarlo
(
l
,
M
,
bas
eLevel
);
void
MultilevelMonteCarlo
::
initializeMapM
onteCarlo
()
{
clearMapM
onteCarlo
();
for
(
unsigned
long
i
=
0
;
i
<
init
Levels
.
size
();
i
++
)
{
bool
onlyFin
eLevel
=
(
i
==
0
)
||
(
mcOnly
)
;
int
l
=
init
Levels
[
i
],
M
=
init
SampleAmount
[
i
];
map
MonteCarlo
[
l
]
=
getMonteCarlo
(
l
,
M
,
onlyFin
eLevel
);
}
}
void
MultilevelMonteCarlo
::
initializeValues
MLMC
()
{
void
MultilevelMonteCarlo
::
initializeValues
()
{
levels
=
{},
numsamples
=
{};
value
=
0
.
0
,
cost
=
0
.
0
;
alpha
=
0
.
0
,
beta
=
0
.
0
,
gamma
=
0
.
0
;
}
void
MultilevelMonteCarlo
::
clearMapMC
()
{
for
(
auto
&
iter
:
map_mc
)
{
void
MultilevelMonteCarlo
::
clearMapMonteCarlo
()
{
for
(
auto
&
iter
:
mapMonteCarlo
)
{
delete
iter
.
second
;
}
map
_mc
.
clear
();
map
MonteCarlo
.
clear
();
}
void
MultilevelMonteCarlo
::
m
ethod
()
{
void
MultilevelMonteCarlo
::
M
ethod
()
{
logger
->
startMethodMSG
();
for
(
auto
&
mc
:
map
_mc
)
mc
.
second
->
m
ethod
();
for
(
auto
&
mc
:
map
MonteCarlo
)
mc
.
second
->
M
ethod
();
logger
->
endMethodMSG
();
}
void
MultilevelMonteCarlo
::
m
ethod
(
const
double
eps
)
{
void
MultilevelMonteCarlo
::
M
ethod
(
const
double
eps
)
{
logger
->
startMethodMSG
();
logger
->
logMSGV1
(
"eps: "
+
to_string
(
eps
));
bool
converged
=
false
;
while
(
!
converged
)
{
for
(
auto
&
mc
:
map
_mc
)
for
(
auto
&
mc
:
map
MonteCarlo
)
if
(
mc
.
second
->
dM
>
0
)
mc
.
second
->
m
ethod
();
mc
.
second
->
M
ethod
();
estimateExponents
();
update
Num
Sample
s
(
eps
);
updateSample
Amount
(
eps
);
if
(
checkForNewLevel
())
{
if
(
estimateNumericalError
()
>
eps
/
sqrt
(
2
.
0
))
appendLevel
(
eps
);
...
...
@@ -61,23 +61,23 @@ void MultilevelMonteCarlo::method(const double eps) {
converged
=
true
;
}
}
Vector
u
((
*
graphs
)[
map
_mc
.
rbegin
()
->
first
-
pLevel
]);
Vector
u
((
*
graphs
)[
map
MonteCarlo
.
rbegin
()
->
first
-
pLevel
]);
u
=
0
.
0
;
wrapUpResults
(
u
,
value
,
cost
,
levels
,
numsamples
);
logger
->
endMethodMSG
();
}
void
MultilevelMonteCarlo
::
m
ethod
(
const
vector
<
double
>
&
eps_lst
)
{
void
MultilevelMonteCarlo
::
M
ethod
(
const
vector
<
double
>
&
eps_lst
)
{
logger
->
startMethodMSG
();
// logger->increase_indent();
for
(
auto
&
eps
:
eps_lst
)
{
initializeMapM
C
();
initializeValues
MLMC
();
m
ethod
(
eps
);
levels
_vs_eps
.
push_back
(
levels
);
num
sample
s_vs_eps
.
push_back
(
numsamples
);
value
_vs_eps
.
push_back
(
value
);
cost
_vs_eps
.
push_back
(
cost
);
initializeMapM
onteCarlo
();
initializeValues
();
M
ethod
(
eps
);
levels
OverEpsilon
.
push_back
(
levels
);
sample
AmountOverEpsilon
.
push_back
(
numsamples
);
value
OverEpsilon
.
push_back
(
value
);
cost
OverEpsilon
.
push_back
(
cost
);
}
// logger->decrease_indent();
logger
->
endMethodMSG
();
...
...
@@ -85,15 +85,15 @@ void MultilevelMonteCarlo::method(const vector<double> &eps_lst) {
void
MultilevelMonteCarlo
::
estimateExponents
(
bool
excludeBaseLevel
)
{
vector
<
double
>
level_lst
,
avgs_Y
,
vars_Y
,
costs
;
auto
iter
=
map
_mc
.
begin
();
auto
iter
=
map
MonteCarlo
.
begin
();
if
(
excludeBaseLevel
)
iter
++
;
for
(;
iter
!=
map
_mc
.
end
();
iter
++
)
{
for
(;
iter
!=
map
MonteCarlo
.
end
();
iter
++
)
{
level_lst
.
push_back
((
double
)
iter
->
first
);
avgs_Y
.
push_back
(
-
log2
(
iter
->
second
->
avg
_
Y
));
vars_Y
.
push_back
(
-
log2
(
iter
->
second
->
var
_
Y
));
costs
.
push_back
(
log2
(
iter
->
second
->
avg
_c
ost
));
avgs_Y
.
push_back
(
-
log2
(
iter
->
second
->
avgY
));
vars_Y
.
push_back
(
-
log2
(
iter
->
second
->
varY
));