Commit 787ffe24 authored by niklas.baumgarten's avatar niklas.baumgarten
Browse files

added sparse grid generator and tests

parent 47f39f6f
Pipeline #157099 failed with stages
in 11 minutes and 29 seconds
add_library(GENERATORS STATIC
NormalDistribution.cpp
UniformDistribution.cpp
SparseGridGenerator.cpp
SampleGeneratorContainer.cpp
algorithms/CirculantEmbedding.cpp
algorithms/SymmetricCovariance.cpp
algorithms/HybridFluxGenerator.cpp
)
target_link_libraries(GENERATORS PDESOLVER sprng MPP_LIBRARIES)
target_link_libraries(GENERATORS PDESOLVER sprng MPP_LIBRARIES Tasmanian_libsparsegrid)
#include "SparseGridGenerator.hpp"
#ifndef SPARSEGRIDGENERATOR_HPP
#define SPARSEGRIDGENERATOR_HPP
#include "TasmanianSparseGrid.hpp"
// Todo Use RVector and such
class SparseGridGenerator {
protected:
TasGrid::TasmanianSparseGrid grid;
TasGrid::TypeOneDRule rule;
TasGrid::TypeDepth depth;
int dimension;
int outputs;
public:
SparseGridGenerator(int dimension, int outputs,
TasGrid::TypeDepth depth = TasGrid::type_level,
TasGrid::TypeOneDRule rule = TasGrid::rule_clenshawcurtis) :
dimension(dimension), outputs(outputs), depth(depth), rule(rule) {
}
void CreateGlobalGrid(int level) {
grid.makeGlobalGrid(dimension, outputs, level, depth, rule);
}
std::vector<double> GetPoints() {
return grid.getPoints();
}
std::vector<double> GetWeights() {
return grid.getQuadratureWeights();
}
int GetNumPoints() {
return grid.getNumPoints();
}
double Quadrature(double func (double, double)) {
double I = 0.0;
std::vector<double> points = GetPoints();
std::vector<double> weights = GetWeights();
int num_points = GetNumPoints();
for (int i = 0; i < num_points; i++) {
double x = points[i * dimension];
double y = points[i * dimension + 1];
I += weights[i] * func(x, y);
}
return I;
}
};
#endif //SPARSEGRIDGENERATOR_HPP
......@@ -14,13 +14,14 @@ add_mpp_test(generators/TestNormalDistribution GENERATORS)
add_mpp_test(generators/TestUniformDistribution GENERATORS)
add_mpp_test(generators/TestCirculantEmbedding GENERATORS)
add_mpp_test(generators/TestSymmetricCovariance GENERATORS)
add_mpp_test(generators/TestSparseGridGenerator GENERATORS)
#add_mpp_test(generators/TestHybridFluxGenerator GENERATORS)
add_mpp_test(estimators/datastructure/TestLevelMaps MONTECARLO)
add_mpp_test(estimators/datastructure/TestExponents MONTECARLO)
add_mpp_test(estimators/datastructure/TestMultilevelErrors MONTECARLO)
add_mpp_test(estimators/TestTasmanian Tasmanian_libsparsegrid)
add_mpp_test(pdesolver/TestPDESolver PDESOLVER)
......
#include "TestEnvironment.hpp"
#include "TasmanianSparseGrid.hpp"
//using namespace TasGrid;
class TestTasmanian : public Test {
protected:
TasGrid::TasmanianSparseGrid grid;
TasGrid::TypeOneDRule rule;
TasGrid::TypeDepth type;
TestTasmanian(TasGrid::TypeDepth type, TasGrid::TypeOneDRule rule) :
type(type), rule(rule) {
// Adapted for each test case
}
void TearDown() override {
// grid clear
}
};
double TestFunction(std::vector<double> x) {
return 1.0;
}
class TestClenshawCurtis : public TestTasmanian {
public:
TestClenshawCurtis() : TestTasmanian(TasGrid::type_level,
TasGrid::rule_clenshawcurtis) {}
};
TEST_F(TestClenshawCurtis, TestNodes) {
std::cout
<< "\n---------------------------------------------------------------------------------------------------\n";
std::cout << std::scientific;
std::cout.precision(17);
std::cout << "Example 1: integrate f(x,y) = exp(-x^2) * cos(y),\n"
<< " using clenshaw-curtis nodes and grid of type level\n";
int dimension = 2;
int level = 6;
TasGrid::TasmanianSparseGrid grid;
grid.makeGlobalGrid(dimension,
0,
level,
TasGrid::type_level,
TasGrid::rule_clenshawcurtis);
std::vector<double> points = grid.getPoints();
std::vector<double> weights = grid.getQuadratureWeights();
int num_points = grid.getNumPoints(); // also equal to weights.size()
double I = 0.0;
for (int i = 0; i < num_points; i++) {
double x = points[i * dimension];
double y = points[i * dimension + 1];
I += weights[i] * std::exp(-x * x) * std::cos(y);
}
double exact = 2.513723354063905e+00;
double E = std::abs(exact - I);
std::cout << " at level: " << level
<< "\n the grid has: " << num_points
<< "\n integral: " << I
<< "\n error: " << E << "\n\n";
level = 7;
grid.makeGlobalGrid(dimension,
0,
level,
TasGrid::type_level,
TasGrid::rule_clenshawcurtis);
points = grid.getPoints();
weights = grid.getQuadratureWeights();
num_points = grid.getNumPoints();
I = 0.0;
for (int i = 0; i < num_points; i++) {
double x = points[i * dimension];
double y = points[i * dimension + 1];
I += weights[i] * std::exp(-x * x) * std::cos(y);
}
E = std::abs(exact - I);
std::cout << " at level: " << level
<< "\n the grid has: " << num_points
<< "\n integral: " << I
<< "\n error: " << E << endl;
}
int main(int argc, char **argv) {
return MppTest(
MppTestBuilder(argc, argv).
WithScreenLogging().
WithPPM()
).RUN_ALL_MPP_TESTS();
}
#ifndef TESTTASMANIANINTERFACE_HPP
#define TESTTASMANIANINTERFACE_HPP
class TestTasmanian {
};
#endif //TESTTASMANIANINTERFACE_HPP
#include "TestEnvironment.hpp"
#include "SparseGridGenerator.hpp"
const double TEST_TOLERANCE = 1e-10;
class TestSparseGridGenerator : public Test {
protected:
SparseGridGenerator generator;
TestSparseGridGenerator(int dimension, int outputs) :
generator(dimension, outputs) {
int level = 6;
double exact = 2.513723354063905e+00;
generator.CreateGlobalGrid(level);
}
void TearDown() override {}
};
double TestFunction(std::vector<double> x) {
return 1.0;
}
class Test1DSparseGridGenerator : public TestSparseGridGenerator {
public:
Test1DSparseGridGenerator() : TestSparseGridGenerator(1, 0) {}
};
class Test2DSparseGridGenerator : public TestSparseGridGenerator {
public:
Test2DSparseGridGenerator() : TestSparseGridGenerator(2, 0) {}
};
TEST_F(Test1DSparseGridGenerator, TestNumOfGridPoints) {
EXPECT_EQ(generator.GetNumPoints(), generator.GetWeights().size());
EXPECT_EQ(generator.GetNumPoints(), generator.GetPoints().size());
}
TEST_F(Test2DSparseGridGenerator, TestNumOfGridPoints) {
EXPECT_EQ(generator.GetNumPoints(), generator.GetWeights().size());
// EXPECT_EQ(generator.GetNumPoints(), generator.GetPoints().size());
}
TEST_F(Test1DSparseGridGenerator, TestSumOfWeights) {
double sum = 0.0;
for (auto &weight : generator.GetWeights())
sum += weight;
EXPECT_NEAR(sum, 2.0, TEST_TOLERANCE);
}
TEST_F(Test2DSparseGridGenerator, TestSumOfWeights) {
double sum = 0.0;
for (auto &weight : generator.GetWeights())
sum += weight;
EXPECT_NEAR(sum, 4.0, TEST_TOLERANCE);
}
TEST_F(Test1DSparseGridGenerator, TestQuadrature) {
std::cout << std::scientific;
std::cout.precision(17);
std::cout << "Example 1: integrate f(x,y) = exp(-x^2) * cos(y),\n"
<< " using clenshaw-curtis nodes and grid of type level\n";
std::cout << "Example is 2D" << endl;
}
TEST_F(Test2DSparseGridGenerator, TestQuadrature) {
std::cout << std::scientific;
std::cout.precision(17);
std::cout << "Example 1: integrate f(x,y) = exp(-x^2) * cos(y),\n"
<< " using clenshaw-curtis nodes and grid of type level\n";
std::cout << "Example is 2D" << endl;
double I = generator.Quadrature([](double x, double y) {return std::exp(-x * x) * std::cos(y); });
std::cout << I << endl;
}
int main(int argc, char **argv) {
return MppTest(
MppTestBuilder(argc, argv).
WithScreenLogging().
WithPPM()
).RUN_ALL_MPP_TESTS();
}
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