TestNormalDistribution.cpp 2.14 KB
Newer Older
1
#include "generators/NormalDistribution.hpp"
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "basics/Utilities.hpp"
#include "TestEnvironment.hpp"


typedef std::vector<SampleID> TestSampleIDs;

class TestNormalDistribution : public Test {
protected:
    TestSampleIDs sampleIDs;

    Meshes *meshes;

    int testSamples = 1e6;

    void SetUp() override {
        meshes = createTestMeshes("Interval_DirichletBC", 0, 0);
        for (int number = 0; number < testSamples; number++)
            sampleIDs.push_back(SampleID(Level(), number, false));
    }

    void TearDown() override {
        delete meshes;
    }
};

niklas.baumgarten's avatar
niklas.baumgarten committed
27
28
29
30
31
TEST_F(TestNormalDistribution, NormalDistribution) {
    NormalDistribution normalDist(*meshes);
    std::vector<Scalar> results;
    for (auto &id : sampleIDs) {
        normalDist.DrawSample(id);
32
        results.push_back(normalDist.EvalSample());
niklas.baumgarten's avatar
niklas.baumgarten committed
33
34
35
36
37
38
39
40
41
42
    }
    Scalar mean = Mean(results);
    Scalar var = Variance(results);
    ASSERT_NEAR(0.0, mean, sqrt(1.0 / testSamples));
    ASSERT_NEAR(1.0, var, sqrt(10.0 / testSamples));
}

TEST_F(TestNormalDistribution, ComplexNormalDistribution) {
    ComplexNormalDistribution normalDist(*meshes);
    std::vector<Complex> results;
43
    for (auto &id : sampleIDs) {
niklas.baumgarten's avatar
niklas.baumgarten committed
44
        normalDist.DrawSample(id);
45
        results.push_back(normalDist.EvalSample());
46
    }
niklas.baumgarten's avatar
niklas.baumgarten committed
47
48
49
50
51
52
53
54
    Complex mean = Mean(results);
    Complex var = Variance(results);
    ASSERT_NEAR(0.0, mean.real(), sqrt(1.0 / testSamples));
    ASSERT_NEAR(0.0, mean.imag(), sqrt(1.0 / testSamples));
//    ASSERT_NEAR(1.0, var, sqrt(10.0 / testSamples)); // TODO
//    ASSERT_NEAR(1.0, var.imag(), sqrt(10.0 / testSamples));
}

55
56
TEST_F(TestNormalDistribution, NormalDistributionScalarSequence1D) {
    NormalDistributionScalarSequence1D normalDist(*meshes);
niklas.baumgarten's avatar
niklas.baumgarten committed
57
58
    normalDist.Resize(sampleIDs.size());
    normalDist.DrawSample(sampleIDs[0]);
59
    ScalarSequence1D results = normalDist.EvalSample();
niklas.baumgarten's avatar
niklas.baumgarten committed
60
61
    Scalar mean = Mean(results);
    Scalar var = Variance(results);
62
63
64
65
    ASSERT_NEAR(0.0, mean, sqrt(1.0 / testSamples));
    ASSERT_NEAR(1.0, var, sqrt(10.0 / testSamples));
}

niklas.baumgarten's avatar
niklas.baumgarten committed
66

67
68
69
70
71
72
int main(int argc, char **argv) {
    return MppTest(
        MppTestBuilder(argc, argv).
        WithPPM()
    ).RUN_ALL_MPP_TESTS();
}