Commit bb9eca46 authored by niklas.baumgarten's avatar niklas.baumgarten
Browse files

refactored MonteCarlo

parent 7adc11c0
......@@ -10,21 +10,40 @@ void MonteCarlo::Method() {
mout.EndBlock(verbose == 0);
}
void MonteCarlo::updateSampleIds(SampleSolution &fSol, SampleSolution &cSol) {
fineId.number = aggregate.index();
coarseId.number = aggregate.index();
fSol.id = fineId;
cSol.id = coarseId;
}
void MonteCarlo::method() {
SampleSolution cSolution(pdeSolver->GetDisc(), cId);
SampleSolution fSolution(pdeSolver->GetDisc(), fId);
std::unique_ptr<Meshes> meshes = meshesCreator.
WithMeshName(pdeSolverCreator.GetMeshName()).
WithCommSplit(aggregate.commSplit).
WithPLevel(level - 1).
WithLevel(level).
CreateUnique();
std::unique_ptr<PDESolver> pdeSolver = pdeSolverCreator.
CreateUnique(*meshes);
SampleSolution fineSolution(pdeSolver->GetDisc(), fineId);
SampleSolution coarseSolution(pdeSolver->GetDisc(), coarseId);
while (aggregate.ctr.dMcomm != 0) {
computeSampleSolution(aggregate.index(), fId, fSolution);
if (onlyFine) cSolution.Init();
else computeSampleSolution(aggregate.index(), cId, cSolution);
aggregate.Update(fSolution, cSolution);
}
}
updateSampleIds(fineSolution, coarseSolution);
pdeSolver->DrawSample(fineId);
pdeSolver->Run(fineSolution);
void MonteCarlo::computeSampleSolution(int m, SampleID &id, SampleSolution &solution) {
id.number = m;
solution.id = id;
pdeSolver->DrawSample(id);
pdeSolver->Run(solution);
if (onlyFine) {
coarseSolution.Init();
} else {
pdeSolver->DrawSample(coarseId);
pdeSolver->Run(coarseSolution);
}
aggregate.Update(fineSolution, coarseSolution);
}
}
......@@ -18,92 +18,36 @@ protected:
bool parallel;
// Todo make unique pointer, there are issues with cleaning up these objects
Meshes *meshes;
SampleID fineId;
PDESolver *pdeSolver;
public:
PDESolverCreator pdeSolverCreator;
SampleID coarseId;
MeshesCreator meshesCreator;
SampleID cId;
SampleID fId;
MonteCarlo(int level, int dM, bool onlyFine, bool parallel) :
level(level), onlyFine(onlyFine), parallel(parallel),
Estimator(dM), pdeSolverCreator(PDESolverCreator()),
meshesCreator(MeshesCreator(pdeSolverCreator.GetMeshName())) {
Init(dM);
}
MonteCarlo(int level, int dM, bool onlyFine, bool parallel,
MeshesCreator meshesCreator, PDESolverCreator pdeCreator) :
level(level), onlyFine(onlyFine), parallel(parallel), Estimator(dM),
pdeSolverCreator(pdeCreator), meshesCreator(meshesCreator) {
Init(dM);
}
PDESolverCreator pdeSolverCreator;
MonteCarlo() :
Estimator(), pdeSolverCreator(PDESolverCreator()),
meshesCreator(MeshesCreator(pdeSolverCreator.GetMeshName())) {
int dM = 0;
config.get("Level", level);
config.get("Samples", dM);
config.get("OnlyFine", onlyFine);
config.get("MCParallel", parallel);
Init(dM);
}
void updateSampleIds(SampleSolution &fSol, SampleSolution &cSol);
MonteCarlo(MeshesCreator meshesCreator, PDESolverCreator pdeCreator) :
Estimator(), pdeSolverCreator(pdeCreator), meshesCreator(meshesCreator) {
int dM = 0;
config.get("Level", level);
config.get("Samples", dM);
config.get("OnlyFine", onlyFine);
config.get("MCParallel", parallel);
Init(dM);
}
void method();
std::string Name() const override {
return "MonteCarlo";
}
public:
MonteCarlo(int level, int initSamples, bool onlyFine, bool parallel,
MeshesCreator meshesCreator = MeshesCreator(),
PDESolverCreator pdeCreator = PDESolverCreator()) :
Estimator(initSamples), level(level), onlyFine(onlyFine), parallel(parallel),
fineId(SampleID(level, 0, false)), coarseId(SampleID(level, 0, true)),
meshesCreator(meshesCreator), pdeSolverCreator(pdeCreator) {
void Init(int dM) {
config.get("MCVerbose", verbose);
config.get("MCParallel", parallel);
fId.fLevel = level;
fId.cLevel = level - 1;
fId.coarse = false;
cId.fLevel = level;
cId.cLevel = level - 1;
cId.coarse = true;
aggregate.parallel = parallel;
aggregate.UpdateSampleCounter(dM);
meshes = meshesCreator.
WithCommSplit(aggregate.commSplit).
WithPLevel(level - 1).
WithLevel(level).
Create();
pdeSolver = pdeSolverCreator.Create(*meshes);
aggregate.UpdateSampleCounter(initSamples);
}
~MonteCarlo() {
if (!meshes) delete meshes;
if (!pdeSolver) delete pdeSolver;
}
std::string Name() const override { return "MonteCarlo"; }
void Method() override;
private:
void method();
void computeSampleSolution(int m, SampleID &id, SampleSolution &solution);
};
#endif //MLMC_MC_HPP
\ No newline at end of file
Supports Markdown
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