Commit 6ad73e86 authored by niklas.baumgarten's avatar niklas.baumgarten
Browse files

Merge branch '30-replace-generator-container' into 'feature'

Resolve "Replace Generator Container"

Closes #30

See merge request !42
parents 80d6fe9a a12ac537
Pipeline #157226 passed with stages
in 11 minutes and 22 seconds
......@@ -2,9 +2,8 @@ add_library(GENERATORS STATIC
NormalDistribution.cpp
UniformDistribution.cpp
SparseGridGenerator.cpp
SampleGeneratorContainer.cpp
algorithms/CirculantEmbedding.cpp
algorithms/SymmetricCovariance.cpp
algorithms/HybridFluxGenerator.cpp
CirculantEmbedding.cpp
SymmetricCovariance.cpp
HybridFluxGenerator.cpp
)
target_link_libraries(GENERATORS PDESOLVER sprng MPP_LIBRARIES Tasmanian_libsparsegrid)
target_link_libraries(GENERATORS MPP_LIBRARIES PDESOLVER sprng Tasmanian_libsparsegrid)
......@@ -85,7 +85,7 @@ RVector CirculantEmbedding1D::GenerateGaussianField(const SampleID &id) {
return gaussianField;
}
Tensor CirculantEmbedding1D::EvalSample(const cell &c) {
Tensor CirculantEmbedding1D::EvalSample(const cell &c) const {
return sampleVec->operator()(c(), 0) * One;
}
......@@ -174,7 +174,7 @@ RMatrix CirculantEmbedding2D::GenerateGaussianField(const SampleID &id) {
return gaussianField;
}
Tensor CirculantEmbedding2D::EvalSample(const cell &c) {
Tensor CirculantEmbedding2D::EvalSample(const cell &c) const {
return sampleVec->operator()(c(), 0) * One;
}
......
......@@ -63,7 +63,7 @@ public:
RVector GenerateGaussianField(const SampleID &id);
Tensor EvalSample(const cell &c) override;
Tensor EvalSample(const cell &c) const override;
string Name() const override { return "CirculantEmbedding1D"; }
};
......@@ -120,7 +120,7 @@ public:
RMatrix GenerateGaussianField(const SampleID &id);
Tensor EvalSample(const cell &c) override;
Tensor EvalSample(const cell &c) const override;
string Name() const override { return "CirculantEmbedding2D"; }
};
......
......@@ -28,7 +28,7 @@ void HybridFaceNormalFluxGenerator::drawSample(const SampleID &id) {
assemble->SetNormalFlux(solutionFaceValues->U, solutionFaceFlux->U);
}
Scalar HybridFaceNormalFluxGenerator::EvalSample(int face, const cell &c) {
Scalar HybridFaceNormalFluxGenerator::EvalSample(int face, const cell &c) const {
RTLagrangeElement elem(solutionFaceFlux->U, c);
RTFaceElement faceElem(solutionFaceFlux->U, c, face);
return (solutionFaceFlux->U)(elem[face], 0) * elem.Sign(face) / faceElem.Area();
......@@ -40,7 +40,7 @@ HybridFaceNormalFluxGenerator::~HybridFaceNormalFluxGenerator() {
if (!solutionFaceFlux) delete solutionFaceFlux;
}
VectorField HybridCellFluxGenerator::EvalSample(const cell &c) {
VectorField HybridCellFluxGenerator::EvalSample(const cell &c) const {
RTLagrangeElement elem(generator.solutionFaceFlux->U, c);
VectorField F = zero;
double area = 0;
......
......@@ -14,24 +14,24 @@ private:
void drawSample(const SampleID &id) override;
public:
PDESolver *pdeSolver;
PDESolver *pdeSolver{};
SampleSolution *solutionFaceValues;
SampleSolution *solutionFaceValues{};
SampleSolution *solutionFaceFlux;
SampleSolution *solutionFaceFlux{};
HybridFaceNormalFluxGenerator(const Meshes &meshes) :
explicit HybridFaceNormalFluxGenerator(const Meshes &meshes) :
SampleGenerator(meshes) {
createPDESolver();
}
~HybridFaceNormalFluxGenerator();
~HybridFaceNormalFluxGenerator() override;
string Name() const override {
return "HybridFaceNormalFluxGenerator";
}
Scalar EvalSample(int face, const cell &c) override;
Scalar EvalSample(int face, const cell &c) const override;
};
class HybridCellFluxGenerator : public SampleGenerator<VectorField> {
......@@ -44,7 +44,7 @@ public:
SampleGenerator(meshes),
generator(generator) {}
VectorField EvalSample(const cell &c) override;
VectorField EvalSample(const cell &c) const override;
string Name() const override {
return "HybridCellFluxGenerator";
......
......@@ -22,7 +22,7 @@ void NormalDistributionReal::updateCounter() {
internalCounter = internalCounter % 2;
}
Scalar NormalDistributionReal::EvalSample() {
Scalar NormalDistributionReal::EvalSample() const {
if (internalCounter)
return firstSample;
else
......
#ifndef NORMALDISTRIBUTION_HPP
#define NORMALDISTRIBUTION_HPP
#include "SampleGenerator.hpp"
#include "UniformDistribution.hpp"
......@@ -9,9 +8,9 @@ class NormalDistributionReal : public SampleGenerator<Scalar> {
private:
UniformDistributionReal uniformDist;
Scalar firstSample;
Scalar firstSample{};
Scalar secondSample;
Scalar secondSample{};
int internalCounter = 0;
......@@ -20,10 +19,10 @@ private:
void updateCounter();
public:
NormalDistributionReal(const Meshes &meshes) :
explicit NormalDistributionReal(const Meshes &meshes) :
SampleGenerator(meshes), uniformDist(UniformDistributionReal(meshes, 0.0, 1.0)) {}
Scalar EvalSample() override;
Scalar EvalSample() const override;
string Name() const override {
return "NormalDistributionReal";
......@@ -43,11 +42,11 @@ private:
}
public:
NormalDistributionComplex(const Meshes &meshes) :
explicit NormalDistributionComplex(const Meshes &meshes) :
SampleGenerator(meshes),
normalDist(meshes) {}
Complex EvalSample() override {
Complex EvalSample() const override {
return sample;
}
......@@ -68,7 +67,7 @@ private:
}
public:
NormalDistributionRVector(const Meshes &meshes, int size = -1) :
explicit NormalDistributionRVector(const Meshes &meshes, int size = -1) :
SampleGenerator(meshes),
normalDist(meshes) {
if (size != -1)
......@@ -81,7 +80,7 @@ public:
sample.resize(sizeN);
}
RVector EvalSample() override {
RVector EvalSample() const override {
return sample;
}
......@@ -102,7 +101,7 @@ private:
}
public:
NormalDistributionCVector(const Meshes &meshes, int size = -1) :
explicit NormalDistributionCVector(const Meshes &meshes, int size = -1) :
SampleGenerator(meshes),
normalDist(meshes) {
if (size != -1)
......@@ -115,11 +114,7 @@ public:
sample.resize(sizeN);
}
size_t SampleSize() const override {
return sample.size();
}
CVector EvalSample() override {
CVector EvalSample() const override {
return sample;
}
......@@ -140,7 +135,7 @@ private:
}
public:
NormalDistributionRMatrix(const Meshes &meshes, int rows = -1, int cols = -1) :
explicit NormalDistributionRMatrix(const Meshes &meshes, int rows = -1, int cols = -1) :
SampleGenerator(meshes),
normalDist(meshes, cols) {
if (rows != -1)
......@@ -153,7 +148,7 @@ public:
sample.resize(rows, cols);
}
RMatrix EvalSample() override {
RMatrix EvalSample() const override {
return sample;
}
......@@ -188,7 +183,7 @@ public:
sample.resize(rows, cols);
}
CMatrix EvalSample() override {
CMatrix EvalSample() const override {
return sample;
}
......
......@@ -35,7 +35,7 @@ public:
config.get("GeneratorVerbose", verbose);
}
virtual ~SampleGenerator() {}
virtual ~SampleGenerator() = default;
void DrawSample(const SampleID &id) {
mout.StartBlock(Name());
......@@ -46,33 +46,31 @@ public:
virtual string Name() const = 0;
virtual size_t SampleSize() const { return 0; };
virtual T EvalSample() {
virtual T EvalSample() const {
Exit("Not implemented")
}
virtual T EvalSample(const Point &x) {
virtual T EvalSample(const Point &x) const {
Exit("Not implemented")
}
virtual T EvalSample(int n, const Point &x) {
virtual T EvalSample(int n, const Point &x) const {
Exit("Not implemented")
}
virtual T EvalSample(double t, const Point &x) {
virtual T EvalSample(double t, const Point &x) const {
Exit("Not implemented")
}
virtual T EvalSample(const cell &c) {
virtual T EvalSample(const cell &c) const {
Exit("Not implemented")
}
virtual T EvalSample(int n, const cell &c) {
virtual T EvalSample(int n, const cell &c) const {
Exit("Not implemented")
}
virtual T EvalSample(double t, const cell &c) {
virtual T EvalSample(double t, const cell &c) const {
Exit("Not implemented")
}
......@@ -114,90 +112,98 @@ public:
class ScalarDummy : public SampleGenerator<Scalar> {
public:
ScalarDummy(const Meshes &meshes) : SampleGenerator(meshes) { verbose = 0; }
explicit ScalarDummy(const Meshes &meshes) :
SampleGenerator(meshes) { verbose = 0; }
void drawSample(const SampleID &id) override {
vout(1) << "Dummy Generator" << endl;
};
virtual string Name() const override { return "DummyGenerator"; };
string Name() const override { return "DummyGenerator"; };
};
class ComplexDummy : public SampleGenerator<Complex> {
public:
ComplexDummy(const Meshes &meshes) : SampleGenerator(meshes) { verbose = 0; }
explicit ComplexDummy(const Meshes &meshes) :
SampleGenerator(meshes) { verbose = 0; }
void drawSample(const SampleID &id) override {
vout(1) << "Dummy Generator" << endl;
};
virtual string Name() const override { return "DummyGenerator"; };
string Name() const override { return "DummyGenerator"; };
};
class VectorFieldDummy : public SampleGenerator<VectorField> {
public:
VectorFieldDummy(const Meshes &meshes) : SampleGenerator(meshes) { verbose = 0; }
explicit VectorFieldDummy(const Meshes &meshes) :
SampleGenerator(meshes) { verbose = 0; }
void drawSample(const SampleID &id) override {
vout(1) << "Dummy Generator" << endl;
};
virtual string Name() const override { return "DummyGenerator"; };
string Name() const override { return "DummyGenerator"; };
};
class TensorDummy : public SampleGenerator<Tensor> {
public:
TensorDummy(const Meshes &meshes) : SampleGenerator(meshes) { verbose = 0; }
explicit TensorDummy(const Meshes &meshes) :
SampleGenerator(meshes) { verbose = 0; }
void drawSample(const SampleID &id) override {
vout(1) << "Dummy Generator" << endl;
};
virtual string Name() const override { return "DummyGenerator"; };
string Name() const override { return "DummyGenerator"; };
};
class RVectorDummy : public SampleGenerator<RVector> {
public:
RVectorDummy(const Meshes &meshes) : SampleGenerator(meshes) { verbose = 0; }
explicit RVectorDummy(const Meshes &meshes) :
SampleGenerator(meshes) { verbose = 0; }
void drawSample(const SampleID &id) override {
vout(1) << "Dummy Generator" << endl;
};
virtual string Name() const override { return "DummyGenerator"; };
string Name() const override { return "DummyGenerator"; };
};
class CVectorDummy : public SampleGenerator<CVector> {
public:
CVectorDummy(const Meshes &meshes) : SampleGenerator(meshes) { verbose = 0; }
explicit CVectorDummy(const Meshes &meshes) :
SampleGenerator(meshes) { verbose = 0; }
void drawSample(const SampleID &id) override {
vout(1) << "Dummy Generator" << endl;
};
virtual string Name() const override { return "DummyGenerator"; };
string Name() const override { return "DummyGenerator"; };
};
class RMatrixDummy : public SampleGenerator<RMatrix> {
public:
RMatrixDummy(const Meshes &meshes) : SampleGenerator(meshes) { verbose = 0; }
explicit RMatrixDummy(const Meshes &meshes) :
SampleGenerator(meshes) { verbose = 0; }
void drawSample(const SampleID &id) override {
vout(1) << "Dummy Generator" << endl;
};
virtual string Name() const override { return "DummyGenerator"; };
string Name() const override { return "DummyGenerator"; };
};
class CMatrixDummy : public SampleGenerator<CMatrix> {
public:
CMatrixDummy(const Meshes &meshes) : SampleGenerator(meshes) { verbose = 0; }
explicit CMatrixDummy(const Meshes &meshes) :
SampleGenerator(meshes) { verbose = 0; }
void drawSample(const SampleID &id) override {
vout(1) << "Dummy Generator" << endl;
};
virtual string Name() const override { return "DummyGenerator"; };
string Name() const override { return "DummyGenerator"; };
};
#endif //SAMPLEGENERATOR_HPP
#include "SampleGeneratorContainer.hpp"
void SampleGeneratorContainer::init(const Meshes &meshes, GeneratorNames names) {
for (auto const &genName: names) {
if (genName == "NormalDistributionReal")
scalarGenerator = new NormalDistributionReal(meshes);
if (genName == "NormalDistributionComplex")
complexGenerator = new NormalDistributionComplex(meshes);
if (genName == "NormalDistributionRVector")
rVectorGenerator = new NormalDistributionRVector(meshes);
if (genName == "NormalDistributionCVector")
cVectorGenerator = new NormalDistributionCVector(meshes);
if (genName == "NormalDistributionRMatrix")
rMatrixGenerator = new NormalDistributionRMatrix(meshes);
if (genName == "NormalDistributionCMatrix")
cMatrixGenerator = new NormalDistributionCMatrix(meshes);
if (genName == "UniformDistributionReal")
scalarGenerator = new UniformDistributionReal(meshes, 0.0, 1.0);
if (genName == "UniformDistributionRVector")
rVectorGenerator = new UniformDistributionRVector(meshes, 2, -1.0, 1.0);
if (genName == "CirculantEmbedding") {
if (meshes.dim() == 1)
tensorGenerator = new CirculantEmbedding1D(meshes);
else
tensorGenerator = new CirculantEmbedding2D(meshes);
}
if (genName == "HybridFluxGenerator")
new HybridFaceNormalFluxGenerator(meshes);
}
}
void SampleGeneratorContainer::DrawSample(const SampleID &id) {
if (scalarGenerator != nullptr)
scalarGenerator->DrawSample(id);
if (complexGenerator != nullptr)
complexGenerator->DrawSample(id);
if (vectorFieldGenerator != nullptr)
vectorFieldGenerator->DrawSample(id);
if (tensorGenerator != nullptr)
tensorGenerator->DrawSample(id);
if (rVectorGenerator != nullptr)
rVectorGenerator->DrawSample(id);
if (cVectorGenerator != nullptr)
cVectorGenerator->DrawSample(id);
if (rMatrixGenerator != nullptr)
rMatrixGenerator->DrawSample(id);
if (cMatrixGenerator != nullptr)
cMatrixGenerator->DrawSample(id);
}
#ifndef SAMPLEGENERATORCONTAINER_HPP
#define SAMPLEGENERATORCONTAINER_HPP
#include "SampleGenerator.hpp"
#include "algorithms/CirculantEmbedding.hpp"
#include "HybridFluxGenerator.hpp"
#include "NormalDistribution.hpp"
#include "UniformDistribution.hpp"
typedef std::vector<std::string> GeneratorNames;
class SampleGeneratorContainer {
void init(const Meshes &meshes, GeneratorNames names);
public:
SampleGenerator<Scalar> *scalarGenerator = nullptr;
SampleGenerator<Complex> *complexGenerator = nullptr;
SampleGenerator<VectorField> *vectorFieldGenerator = nullptr;
SampleGenerator<Tensor> *tensorGenerator = nullptr;
SampleGenerator<RVector> *rVectorGenerator = nullptr;
SampleGenerator<CVector> *cVectorGenerator = nullptr;
SampleGenerator<RMatrix> *rMatrixGenerator = nullptr;
SampleGenerator<CMatrix> *cMatrixGenerator = nullptr;
SampleGeneratorContainer(const GeneratorNames &names, const Meshes &meshes) {
init(meshes, names);
}
~SampleGeneratorContainer() {
delete scalarGenerator;
delete complexGenerator;
delete vectorFieldGenerator;
delete tensorGenerator;
delete rVectorGenerator;
delete cVectorGenerator;
delete rMatrixGenerator;
delete cMatrixGenerator;
}
void DrawSample(const SampleID &id);
};
#endif //SAMPLEGENERATORCONTAINER_HPP
......@@ -53,7 +53,7 @@ public:
~UniformDistributionReal() { randNumberGen->free_sprng(); };
Scalar EvalSample() override {
Scalar EvalSample() const override {
if (internalCounter)
return firstSample;
else
......@@ -93,7 +93,7 @@ public:
sample.resize(sizeN);
}
RVector EvalSample() override {
RVector EvalSample() const override {
return sample;
}
......@@ -116,7 +116,8 @@ private:
}
public:
UniformDistributionRMatrix(const Meshes &meshes, int rows, int cols, double a, double b) :
UniformDistributionRMatrix(const Meshes &meshes, int rows, int cols, double a, double b)
:
SampleGenerator(meshes),
uniformDist(meshes, cols, a, b) {
if (rows != -1)
......@@ -129,7 +130,7 @@ public:
sample.resize(rows, cols);
}
RMatrix EvalSample() override {
RMatrix EvalSample() const override {
return sample;
}
......
#ifndef ISTOCHASTICPROBLEM_HPP
#define ISTOCHASTICPROBLEM_HPP
#include "Sample.hpp"
#include "SampleGeneratorContainer.hpp"
#include "NormalDistribution.hpp"
#include "UniformDistribution.hpp"
#include "SparseGridGenerator.hpp"
#include "HybridFluxGenerator.hpp"
#include "CirculantEmbedding.hpp"
class IStochasticProblem {
......@@ -11,62 +14,68 @@ protected:
const Meshes &meshes;
SampleGeneratorContainer genContainer;
public:
IStochasticProblem(const Meshes &meshes, const GeneratorNames &genNames) :
meshes(meshes), genContainer(genNames, meshes) {
explicit IStochasticProblem(const Meshes &meshes) : meshes(meshes) {
config.get("ProblemVerbose", verbose);
}
void DrawSample(const SampleID &id) {
genContainer.DrawSample(id);
}
virtual void DrawSample(const SampleID &id) = 0;
virtual string Name() const = 0;
};
class StochasticDummyProblem : public IStochasticProblem {
public:
StochasticDummyProblem(
const Meshes &meshes, GeneratorNames genNames = {}) :
IStochasticProblem(meshes, genNames) {}
explicit StochasticDummyProblem(const Meshes &meshes) :
IStochasticProblem(meshes) {}
virtual double GeneratorValue() {
return 0.0;
};
virtual string Name() const {
void DrawSample(const SampleID &id) override {}
string Name() const override {
return "StochasticDummyProblem";
}
};
class StochasticDummyScalarGeneratorProblem : public StochasticDummyProblem {
NormalDistributionReal scalarGenerator;
public: