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

template based stochastic problems

parent 16c4a1b7
set(problem set(problem
problem/StochasticProblem.cpp
problem/StochasticEllipticProblem.cpp
) )
\ No newline at end of file
#include "problem/StochasticEllipticProblem.hpp"
template<>
Tensor StochasticLaplace1DT<Vector>::Permeability(const cell &c) const {
mout << *this->fineSample << endl;
mout << this->fineSample->size() << endl;
return this->fineSample->operator()(c(), 0) * One;
}
template<>
Tensor StochasticLaplace2DT<Vector>::Permeability(const cell &c) const {
return this->fineSample->operator()(c(), 0) * One;
}
\ No newline at end of file
...@@ -4,11 +4,11 @@ ...@@ -4,11 +4,11 @@
#include "problem/StochasticProblem.hpp" #include "problem/StochasticProblem.hpp"
class StochasticEllipticProblem : public StochasticProblem { template<typename Sample=Vector>
class IStochasticEllipticProblemT : public IStochasticProblemT<Sample> {
public: public:
StochasticEllipticProblem() = default; IStochasticEllipticProblemT(GeneratorNames generatorNames, Meshes &meshes)
: IStochasticProblemT<Sample>(generatorNames, meshes) {}
~StochasticEllipticProblem() override {};
virtual Scalar Solution(const Point &x) const = 0; virtual Scalar Solution(const Point &x) const = 0;
...@@ -16,21 +16,23 @@ public: ...@@ -16,21 +16,23 @@ public:
virtual Scalar Load(int, const Point &x) const = 0; virtual Scalar Load(int, const Point &x) const = 0;
virtual Tensor Permeability(const cell &c) const { virtual Tensor Permeability(const cell &c) const = 0;
if(fieldSample == nullptr)
Exit("\nNo permeability sample has been set!\n")
return (*fieldSample)(c(), 0) * One;
}
// Todo remove
virtual Tensor InversePermeability(const cell &c) const { virtual Tensor InversePermeability(const cell &c) const {
Tensor permeabilityTensor = Permeability(c); Tensor permeabilityTensor = Permeability(c);
return Invert(permeabilityTensor); return Invert(permeabilityTensor);
} }
}; };
class StochasticLaplace1D : public StochasticEllipticProblem { typedef IStochasticEllipticProblemT<> IStochasticEllipticProblem;
template<typename Sample=Vector>
class StochasticLaplace1DT : public IStochasticEllipticProblemT<Sample> {
public: public:
StochasticLaplace1D() = default; StochasticLaplace1DT(Meshes &meshes) :
IStochasticEllipticProblemT<Sample>(GeneratorNames{"CirculantEmbedding1D"},
meshes) {};
Scalar Solution(const Point &x) const override { return 1 - x[0]; } Scalar Solution(const Point &x) const override { return 1 - x[0]; }
...@@ -40,12 +42,19 @@ public: ...@@ -40,12 +42,19 @@ public:
Scalar Load(int, const Point &x) const override { return 0.0; } Scalar Load(int, const Point &x) const override { return 0.0; }
string Name() const override { return "Stochastic Laplace 1D"; } Tensor Permeability(const cell &c) const override;
string Name() const override { return "StochasticLaplace1D"; }
}; };
class StochasticLaplace2D : public StochasticEllipticProblem { typedef StochasticLaplace1DT<> StochasticLaplace1D;
template<typename Sample=Vector>
class StochasticLaplace2DT : public IStochasticEllipticProblemT<Sample> {
public: public:
StochasticLaplace2D() = default; StochasticLaplace2DT(Meshes &meshes) :
IStochasticEllipticProblemT<Sample>(GeneratorNames{"CirculantEmbedding2D"},
meshes) {}
Scalar Solution(const Point &x) const override { return -x[1]; } Scalar Solution(const Point &x) const override { return -x[1]; }
...@@ -55,7 +64,11 @@ public: ...@@ -55,7 +64,11 @@ public:
Scalar Load(int, const Point &x) const override { return 0.0; } Scalar Load(int, const Point &x) const override { return 0.0; }
string Name() const override { return "Stochastic Laplace 2D"; } Tensor Permeability(const cell &c) const override;
string Name() const override { return "StochasticLaplace2D"; }
}; };
typedef StochasticLaplace2DT<> StochasticLaplace2D;
#endif //STOCHASTICELLIPTICPROBLEM_HPP #endif //STOCHASTICELLIPTICPROBLEM_HPP
#include "StochasticProblem.hpp"
#include "stochastics/CirculantEmbedding.hpp"
#include "stochastics/HybridFluxGenerator.hpp"
template<typename Sample>
SampleGenerator<Sample> *IStochasticProblemT<Sample>
::createGenerator(int l, GeneratorName generatorName) {
if (generatorName == "CirculantEmbedding1D")
return new CirculantEmbedding1D(l, meshes);
if (generatorName == "CirculantEmbedding2D")
return new CirculantEmbedding2D(l, meshes);
// if (generatorName == "HybridFluxGenerator1D")
// return new HybridFluxGenerator1D(l, meshes);
// if (generatorName == "HybridFluxGenerator2D")
// return new HybridFluxGenerator2D(l, meshes);
Exit("Generator not implemented")
}
template<>
SampleGenerator<Vector> *IStochasticProblemT<Vector>
::createGenerator(int l, GeneratorName generatorName) {
if (generatorName == "CirculantEmbedding1D")
return new CirculantEmbedding1D(l, meshes);
if (generatorName == "CirculantEmbedding2D")
return new CirculantEmbedding2D(l, meshes);
// if (generatorName == "HybridFluxGenerator1D")
// return new HybridFluxGenerator1D(l, meshes);
// if (generatorName == "HybridFluxGenerator2D")
// return new HybridFluxGenerator2D(l, meshes);
Exit("Generator not implemented")
}
\ No newline at end of file
...@@ -3,28 +3,54 @@ ...@@ -3,28 +3,54 @@
#include "Algebra.h" #include "Algebra.h"
#include "montecarlo/SampleID.hpp" #include "montecarlo/SampleID.hpp"
#include "stochastics/SampleGenerator.hpp"
class StochasticProblem { typedef std::string GeneratorName;
typedef std::vector<GeneratorName> GeneratorNames;
template<typename Sample=Vector>
class IStochasticProblemT {
private:
SampleGenerator<Sample> *createGenerator(int l, GeneratorName generatorName);
Meshes &meshes; // Todo: should be removed at some point
protected: protected:
Vector *fieldSample = nullptr; typedef std::map<int, std::vector<SampleGenerator<Sample> *>> SampleGenerators;
public:
StochasticProblem() = default; SampleGenerators generators;
virtual ~StochasticProblem() = default;; Sample *fineSample = nullptr;
Sample *coarseSample = nullptr;
void SetSample(Vector *sample) { public:
fieldSample = sample; IStochasticProblemT(GeneratorNames generatorNames, Meshes &meshes) :
meshes(meshes) {
for (int l = meshes.pLevel(); l <= meshes.Level(); l++)
for (auto &name : generatorNames)
generators[l].push_back(createGenerator(l, name));
} }
virtual ~IStochasticProblemT() {
delete fineSample;
delete coarseSample;
for (auto &[level, gens]: generators)
for (auto &gen : gens)
delete gen;
};
void DrawSample(SampleID id) { void DrawSample(SampleID id) {
// Sagt allen Generatoren sie sollen Sample erzeugen for (auto &gens : generators[id.level])
// Falls in SampleID.coarse == true gens->DrawSample(id, fineSample, coarseSample);
// dann vorgehaltenes sample
} }
virtual void PrintInfo() const {}; // Todo make pure virtual at some point
virtual string Name() const = 0; virtual string Name() const = 0;
}; };
typedef IStochasticProblemT<> IStochasticProblem;
#endif #endif
...@@ -5,11 +5,13 @@ ...@@ -5,11 +5,13 @@
#include "StochasticHybridFlux.hpp" #include "StochasticHybridFlux.hpp"
class StochasticReactionProblem : public StochasticProblem { template<typename Sample>
class StochasticReactionProblem : public StochasticProblem<Sample> {
protected: protected:
double convection = 1.0; double convection = 1.0;
double diffusion = 0.001; double diffusion = 0.001;
double reaction = 1.0; double reaction = 1.0;
public: public:
StochasticReactionProblem() { StochasticReactionProblem() {
config.get("Diffusion", diffusion); config.get("Diffusion", diffusion);
...@@ -83,10 +85,11 @@ inline Scalar u0(const Point &x) { ...@@ -83,10 +85,11 @@ inline Scalar u0(const Point &x) {
else return 0.0; else return 0.0;
} }
class ExponentialReaction2D : public StochasticReactionProblem { template<typename Sample>
class ExponentialReaction2D : public StochasticReactionProblem<Sample> {
public: public:
ExponentialReaction2D() : StochasticReactionProblem() {}; ExponentialReaction2D() : StochasticReactionProblem<Sample>() {};
Scalar Concentration(double t, const Point &x) const override { Scalar Concentration(double t, const Point &x) const override {
return u0(x); return u0(x);
...@@ -97,21 +100,22 @@ public: ...@@ -97,21 +100,22 @@ public:
} }
Tensor Diffusion(int sd, const Point &x) const override { Tensor Diffusion(int sd, const Point &x) const override {
return diffusion * One; return this->diffusion * One;
} }
Scalar Reaction(const Point &, double c) const override { Scalar Reaction(const Point &, double c) const override {
return reaction * c; return this->reaction * c;
} }
Scalar DerivativeReaction(const Point &, double c) const override { Scalar DerivativeReaction(const Point &, double c) const override {
return reaction; return this->reaction;
} }
string Name() const override { return "ExponentialReaction2D"; } string Name() const override { return "ExponentialReaction2D"; }
}; };
class LogisticReaction2D : public StochasticReactionProblem { template<typename Sample>
class LogisticReaction2D : public StochasticReactionProblem<Sample> {
private: private:
double r0 = 1.0; double r0 = 1.0;
double r1 = 0.0; double r1 = 0.0;
...@@ -130,7 +134,7 @@ public: ...@@ -130,7 +134,7 @@ public:
} }
Tensor Diffusion(int sd, const Point &x) const override { Tensor Diffusion(int sd, const Point &x) const override {
return diffusion * One; return this->diffusion * One;
} }
Scalar Reaction(const Point &x, double c) const override { Scalar Reaction(const Point &x, double c) const override {
...@@ -144,32 +148,34 @@ public: ...@@ -144,32 +148,34 @@ public:
string Name() const override { return "LogisticReaction2D"; } string Name() const override { return "LogisticReaction2D"; }
}; };
class HybridExponentialReaction2D : public ExponentialReaction2D, template<typename Sample>
class HybridExponentialReaction2D : public ExponentialReaction2D<Sample>,
public StochasticHybridFlux { public StochasticHybridFlux {
VectorField CellConvection(int sd, const cell &c, const Point &x) const override { VectorField CellConvection(int sd, const cell &c, const Point &x) const override {
return convection * hybrid->EvaluateCellFlux(*flux, c); return this->convection * hybrid->EvaluateCellFlux(*flux, c);
} }
double FaceConvection(int sd, const cell &c, int f, double FaceConvection(int sd, const cell &c, int f,
const VectorField &N, const Point &x) const override { const VectorField &N, const Point &x) const override {
return convection * hybrid->EvaluateNormalFlux(*flux, c, f); return this->convection * hybrid->EvaluateNormalFlux(*flux, c, f);
} }
string Name() const override { return "HybridExponentialReaction2D"; } string Name() const override { return "HybridExponentialReaction2D"; }
}; };
class HybridLogisticReaction2D : public LogisticReaction2D, template<typename Sample>
class HybridLogisticReaction2D : public LogisticReaction2D<Sample>,
public StochasticHybridFlux { public StochasticHybridFlux {
VectorField CellConvection(int sd, const cell &c, VectorField CellConvection(int sd, const cell &c,
const Point &x) const override { const Point &x) const override {
return convection * hybrid->EvaluateCellFlux(*flux, c); return this->convection * hybrid->EvaluateCellFlux(*flux, c);
} }
double FaceConvection(int sd, const cell &c, int f, double FaceConvection(int sd, const cell &c, int f,
const VectorField &N, const Point &x) const override { const VectorField &N, const Point &x) const override {
return convection * hybrid->EvaluateNormalFlux(*flux, c, f); return this->convection * hybrid->EvaluateNormalFlux(*flux, c, f);
} }
string Name() const override { return "HybridLogisticReaction2D"; } string Name() const override { return "HybridLogisticReaction2D"; }
......
...@@ -5,18 +5,8 @@ ...@@ -5,18 +5,8 @@
#include "StochasticHybridFlux.hpp" #include "StochasticHybridFlux.hpp"
class StochasticTransportProblem : public StochasticProblem { template<typename Sample=Vector>
/* class IStochasticTransportProblemT : public IStochasticProblemT<Sample> {
* Stochastic Transport Problem can handle:
* - Random right hand sides ->
* - Random initial conditions ->
* - Random inflow conditions ->
* - Random coefficients -> StochasticHybridFluxTransport
* -> warum hat der hier nicht einfach einen generator
*
* Design your problem by deriving from
*
*/
protected: protected:
VectorField fluxField = Point(0.0, -1.0, 0.0); VectorField fluxField = Point(0.0, -1.0, 0.0);
bool exactAverage = false; // Todo maybe bool exactAverage = false; // Todo maybe
...@@ -24,11 +14,11 @@ protected: ...@@ -24,11 +14,11 @@ protected:
public: public:
bool rhs = false; bool rhs = false;
explicit StochasticTransportProblem() { explicit IStochasticTransportProblemT() {
// Deal with b // Deal with b
} }
virtual ~StochasticTransportProblem() = default; virtual ~IStochasticTransportProblemT() = default;
bool RHS() const { return rhs; } bool RHS() const { return rhs; }
...@@ -52,7 +42,10 @@ public: ...@@ -52,7 +42,10 @@ public:
} }
}; };
class StochasticPollution1D : public StochasticTransportProblem, typedef IStochasticTransportProblemT<> IStochasticTransportProblem;
template<typename Sample=Vector>
class StochasticPollution1D : public IStochasticTransportProblemT<Sample>,
public StochasticHybridFlux { public StochasticHybridFlux {
public: public:
StochasticPollution1D() {} StochasticPollution1D() {}
...@@ -76,7 +69,8 @@ public: ...@@ -76,7 +69,8 @@ public:
string Name() const override { return "StochasticPollution1D"; } string Name() const override { return "StochasticPollution1D"; }
}; };
class StochasticPollutionCosHat1D : public StochasticTransportProblem, template<typename Sample=Vector>
class StochasticPollutionCosHat1D : public IStochasticTransportProblemT<Sample>,
public StochasticHybridFlux { public StochasticHybridFlux {
private: private:
double amplitude = 1.00; double amplitude = 1.00;
...@@ -106,7 +100,8 @@ public: ...@@ -106,7 +100,8 @@ public:
string Name() const override { return "StochasticPollutionCosHat1D"; } string Name() const override { return "StochasticPollutionCosHat1D"; }
}; };
class StochasticPollution2D : public StochasticTransportProblem, template<typename Sample=std::pair<Vector, Vector>>
class StochasticPollution2D : public IStochasticTransportProblemT<Sample>,
public StochasticHybridFlux { public StochasticHybridFlux {
public: public:
double ut(double t, const Point &x) const override { double ut(double t, const Point &x) const override {
...@@ -128,7 +123,8 @@ public: ...@@ -128,7 +123,8 @@ public:
string Name() const override { return "Stochastic Pollution 2D"; } string Name() const override { return "Stochastic Pollution 2D"; }
}; };
class StochasticPollutionCosHat2D : public StochasticTransportProblem, template<typename Sample=std::pair<Vector, Vector>>
class StochasticPollutionCosHat2D : public IStochasticTransportProblemT<Sample>,
public StochasticHybridFlux { public StochasticHybridFlux {
private: private:
double amplitude = 1.00; double amplitude = 1.00;
...@@ -157,7 +153,8 @@ public: ...@@ -157,7 +153,8 @@ public:
string Name() const override { return "StochasticPollutionCosHat2D"; } string Name() const override { return "StochasticPollutionCosHat2D"; }
}; };
class StochasticPollutionMollifiedBar2D : public StochasticTransportProblem, template<typename Sample=std::pair<Vector, Vector>>
class StochasticPollutionMollifiedBar2D : public IStochasticTransportProblemT<Sample>,
public StochasticHybridFlux { public StochasticHybridFlux {
private: private:
double ee = 0.12; double ee = 0.12;
...@@ -208,11 +205,12 @@ public: ...@@ -208,11 +205,12 @@ public:
string Name() const override { return "Stochastic Pollution 2D"; } string Name() const override { return "Stochastic Pollution 2D"; }
}; };
class StochasticInitialConditionsTransport1D : public StochasticTransportProblem { template<typename Sample=std::pair<Vector, Vector>>
class StochasticInitialConditionsTransport1D : public IStochasticTransportProblemT<Sample> {
double c = 1.0; double c = 1.0;
public: public:
explicit StochasticInitialConditionsTransport1D() { explicit StochasticInitialConditionsTransport1D() {
rhs = false; this->rhs = false;
} }
double Amplitude(double r) const { double Amplitude(double r) const {
...@@ -231,9 +229,10 @@ public: ...@@ -231,9 +229,10 @@ public:
string Name() const override { return "RiemannTransport1D"; } string Name() const override { return "RiemannTransport1D"; }
}; };
class StochasticInflowTransport1D : public StochasticTransportProblem { template<typename Sample=std::pair<Vector, Vector>>
class StochasticInflowTransport1D : public IStochasticTransportProblemT<Sample> {
public: public:
explicit StochasticInflowTransport1D() : StochasticTransportProblem() {} explicit StochasticInflowTransport1D() {}
double ut(double t, const Point &x) const override { double ut(double t, const Point &x) const override {
if (x[0] == 0.0 && x[1] >= 1.0) return 1.0; if (x[0] == 0.0 && x[1] >= 1.0) return 1.0;
...@@ -243,7 +242,8 @@ public: ...@@ -243,7 +242,8 @@ public:
string Name() const override { return "InflowTest"; } string Name() const override { return "InflowTest"; }
}; };
class StochasticCircleWave2D : public StochasticTransportProblem { template<typename Sample=std::pair<Vector, Vector>>
class StochasticCircleWave2D : public IStochasticTransportProblemT<Sample> {
/* /*
* Todo Idea: create stochastic flux * Todo Idea: create stochastic flux
*/ */
......
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