Commit 1061a80c authored by niklas.baumgarten's avatar niklas.baumgarten
Browse files

worked on TestSparseGridGenerator, hpp might be deleted

parent 4902bc6c
#include "TestEnvironment.hpp"
#include "MeshesCreator.hpp"
#include "TestSparseGridGenerator.hpp"
#include "SparseGridGenerator.hpp"
#include "MeshesCreator.hpp"
#include "TestEnvironment.hpp"
const double TEST_TOLERANCE = 1e-10;
constexpr double TEST_TOLERANCE = 1e-10;
class TestSparseGridGenerator : public Test {
struct TestParams {
int dimension;
int level;
double exact;
};
Logging &operator<<(Logging &s, const TestParams &testParams) {
return s << "Dimension: " << testParams.dimension << endl
<< "Level: " << testParams.level << endl
<< "Exact solution: " << testParams.exact << endl;
}
class TestSparseGridGenerator : public TestWithParam<TestParams> {
protected:
int level;
int outputs;
int dimension;
double exact;
std::unique_ptr<Meshes> meshes;
SparseGridGenerator generator;
TestSparseGridGenerator(int dimension, int outputs) :
TestSparseGridGenerator(TasGrid::TypeDepth depth, TasGrid::TypeOneDRule rule) :
level(GetParam().level),
dimension(GetParam().dimension),
exact(GetParam().exact), outputs(0),
meshes(MeshesCreator("Interval").CreateUnique()),
generator(SparseGridGenerator(*meshes, dimension, outputs)) {
int level = 6;
double exact = 2.513723354063905e+00;
generator.CreateGlobalGrid(level);
generator(SparseGridGenerator(*meshes, dimension, outputs, level, depth, rule)) {
}
void TearDown() override {}
};
class Test1DSparseGridGenerator : public TestSparseGridGenerator {
public:
Test1DSparseGridGenerator() : TestSparseGridGenerator(1, 0) {}
};
class Test2DSparseGridGenerator : public TestSparseGridGenerator {
class TestClenshawCurtis : public TestSparseGridGenerator {
public:
Test2DSparseGridGenerator() : TestSparseGridGenerator(2, 0) {}
TestClenshawCurtis() :
TestSparseGridGenerator(TasGrid::type_level, TasGrid::rule_clenshawcurtis) {}
};
TEST_F(Test1DSparseGridGenerator, TestNumOfGridPoints) {
EXPECT_EQ(generator.GetNumPoints(), generator.GetWeights().size());
EXPECT_EQ(generator.GetNumPoints(), generator.GetPoints().size());
}
INSTANTIATE_TEST_SUITE_P(TestSparseGridGenerator, TestClenshawCurtis, Values(
TestParams{1, 6, 1.0},
TestParams{2, 6, 1.0}
));
TEST_F(Test2DSparseGridGenerator, TestNumOfGridPoints) {
TEST_P(TestClenshawCurtis, TestNumOfGridPoints) {
EXPECT_EQ(generator.GetNumPoints(), generator.GetWeights().size());
// EXPECT_EQ(generator.GetNumPoints(), generator.GetPoints().size());
EXPECT_EQ(generator.GetNumPoints() * dimension, generator.GetPoints().size());
}
TEST_F(Test1DSparseGridGenerator, TestSumOfWeights) {
TEST_P(TestClenshawCurtis, TestSumOfWeights) {
double sum = 0.0;
for (auto &weight : generator.GetWeights())
sum += weight;
EXPECT_NEAR(sum, 2.0, TEST_TOLERANCE);
EXPECT_NEAR(sum, 2.0 * dimension, 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;
TEST_P(TestClenshawCurtis, TestQuadrature) {
// double I = 0.0; // should leave and be implemented in wrapper around Tasmanian
// for (int i = 0; i < num_points; i++) {
// double x = points[i * dimension];
// I += weights[i] * TestFunction(x);
// }
//
// double exact = 2.0;
// double error = std::abs(exact - I);
// EXPECT_NEAR(error, 0.0, TEST_TOLERANCE);
// std::cout << " at level: " << level
// << "\n the grid has: " << num_points
// << "\n integral: " << I
// << "\n error: " << error << "\n\n";
}
int main(int argc, char **argv) {
return MppTest(
MppTestBuilder(argc, argv).
......
#ifndef TESTSPARSEGRIDGENERATOR_HPP
#define TESTSPARSEGRIDGENERATOR_HPP
#endif //TESTSPARSEGRIDGENERATOR_HPP
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