|
1 | 1 | /* |
2 | 2 | * Tests for the scalar example solving the test equation |
3 | | - */ |
4 | | - |
| 3 | + */ |
5 | 4 | #include <cmath> |
| 5 | + |
6 | 6 | #include <gtest/gtest.h> |
7 | 7 | #include <gmock/gmock.h> |
8 | 8 |
|
| 9 | +using namespace ::testing; |
| 10 | + |
9 | 11 | #define PFASST_UNIT_TESTING |
10 | 12 | #include "../examples/scalar/scalar_sdc.cpp" |
11 | 13 | #undef PFASST_UNIT_TESTING |
12 | 14 |
|
| 15 | +/* |
| 16 | + * parameterized test fixture with number of nodes as parameter |
| 17 | + */ |
| 18 | +class ConvergenceTest |
| 19 | + : public ::testing::TestWithParam<size_t> |
| 20 | +{ |
| 21 | + protected: |
| 22 | + size_t nnodes; // parameter |
| 23 | + |
| 24 | + const complex<double> lambda = complex<double>(-1.0, 1.0); |
| 25 | + const double Tend = 4.0; |
| 26 | + const vector<size_t> nsteps = { 2, 5, 10, 15, 20 }; |
| 27 | + size_t nsteps_l; |
| 28 | + vector<double> err; |
| 29 | + vector<double> convrate; |
| 30 | + double dt; |
| 31 | + size_t niters; |
| 32 | + |
| 33 | + public: |
| 34 | + virtual void SetUp() |
| 35 | + { |
| 36 | + this->nnodes = this->GetParam(); |
| 37 | + this->nsteps_l = this->nsteps.size(); |
| 38 | + this->err.resize(this->nsteps.size()); |
| 39 | + this->convrate.resize(this->nsteps.size()); |
| 40 | + |
| 41 | + // Expect convergence rate of 2*nodes-2 from collocation formula, |
| 42 | + // doing an identical number of iteration should suffice to reach this as each |
| 43 | + // iteration should increase order by one |
| 44 | + this->niters = 2 * nnodes - 2; |
| 45 | + |
| 46 | + for (size_t i = 0; i <= nsteps_l - 1; ++i) { |
| 47 | + dt = Tend / double(nsteps[i]); |
| 48 | + err[i] = run_scalar_sdc(nsteps[i], dt, nnodes, niters, lambda); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + virtual void TearDown() |
| 53 | + {} |
| 54 | +}; |
13 | 55 |
|
14 | 56 | /* |
15 | 57 | * For Lobatto nodes, the resulting method should of order 2*M-2 with M=number of nodes |
16 | 58 | * The test below verifies that the code approximately (up to a safety factor) reproduces |
17 | 59 | * the theoretically expected rate of convergence |
18 | 60 | */ |
19 | | -TEST(ConvergenceTest, ScalarSDC) |
| 61 | +TEST_P(ConvergenceTest, GaussLobattoNodes) |
20 | 62 | { |
21 | | - const complex<double> lambda = complex<double>(-1.0, 1.0); |
22 | | - const double Tend = 4.0; |
23 | | - const vector<size_t> nsteps = { 2, 5, 10, 15, 20 }; |
24 | | - size_t nsteps_l = nsteps.size(); |
25 | | - |
26 | | - vector<double> err(nsteps.size()); |
27 | | - vector<double> convrate(nsteps.size()-1); |
28 | | - |
29 | | - double dt; |
30 | | - |
31 | | - // Test converge up to 6 nodes: For more nodes, errors are of the order of machine |
32 | | - // precision and convergence can no longer be monitored |
33 | | - for ( size_t nnodes = 2; nnodes<=6; ++nnodes) |
34 | | - { |
35 | | - // Expect convergence rate of 2*nodes-2 from collocation formula, |
36 | | - // doing an identical number of iteration should suffice to reach this as each |
37 | | - // iteration should increase order by one |
38 | | - size_t niters = 2*nnodes-2; |
39 | | - |
40 | | - for ( size_t i = 0; i<=nsteps_l-1; ++i) |
41 | | - { |
42 | | - dt = Tend/double(nsteps[i]); |
43 | | - err[i] = run_scalar_sdc(nsteps[i], dt, nnodes, niters, lambda); |
44 | | - } |
45 | | - |
46 | | - for ( size_t i = 0; i<=nsteps_l-2; ++i) |
47 | | - { |
48 | | - convrate[i] = log10(err[i+1]/err[i])/log10(double(nsteps[i])/double(nsteps[i+1])); |
| 63 | + for (size_t i = 0; i <= nsteps_l - 2; ++i) { |
| 64 | + convrate[i] = log10(err[i+1] / err[i]) / log10(double(nsteps[i]) / double(nsteps[i + 1])); |
49 | 65 |
|
50 | | - EXPECT_THAT(convrate[i], |
51 | | - testing::DoubleNear(double(2*nnodes-2), 0.99)) << "Convergence rate at node " |
52 | | - << i |
53 | | - << " not within expected range"; |
54 | | - } |
| 66 | + EXPECT_THAT(convrate[i], |
| 67 | + testing::DoubleNear(double(2 * nnodes - 2), 0.99)) << "Convergence rate at node " |
| 68 | + << i |
| 69 | + << " not within expected range"; |
55 | 70 | } |
56 | 71 | } |
57 | 72 |
|
| 73 | +INSTANTIATE_TEST_CASE_P(ScalarSDC, ConvergenceTest, Range<size_t>(2, 7)); |
| 74 | + |
58 | 75 | int main(int argc, char** argv) |
59 | 76 | { |
60 | 77 | testing::InitGoogleTest(&argc, argv); |
|
0 commit comments