PFASST++
test_scalar_conv.cpp
Go to the documentation of this file.
1 /*
2  * Tests for the scalar example solving the test equation
3  */
4 #include <cmath>
5 using namespace std;
6 
7 #include <gtest/gtest.h>
8 #include <gmock/gmock.h>
9 using namespace ::testing;
10 
11 #include <pfasst/quadrature.hpp>
12 
13 #define PFASST_UNIT_TESTING
14 #include "../examples/scalar/scalar_sdc.cpp"
15 #undef PFASST_UNIT_TESTING
16 using namespace pfasst::examples::scalar;
17 
18 /*
19  * parameterized test fixture with number of nodes as parameter
20  */
21 class ConvergenceTest
22  : public TestWithParam<tuple<size_t, pfasst::quadrature::QuadratureType>>
23 {
24  protected:
25  size_t nnodes;
26  size_t niters;
27  double end_time;
28  vector<size_t> nsteps;
29  vector<double> err;
30  vector<double> convrate;
31  complex<double> lambda;
33 
34  public:
35  virtual void SetUp()
36  {
37  this->nnodes = get<0>(GetParam());
38  this->nodetype = get<1>(GetParam());
39 
40  switch (this->nodetype)
41  {
43  this->niters = 2 * this->nnodes - 2;
44  this->end_time = 4.0;
45  this->lambda = complex<double>(-1.0, 1.0);
46  this->nsteps = { 2, 5, 10, 15 };
47  break;
48 
50  this->niters = 2 * this->nnodes;
51  this->end_time = 6.0;
52  this->lambda = complex<double>(-1.0, 2.0);
53  this->nsteps = { 2, 4, 6, 8, 10 };
54  break;
55 
57  this->niters = 2 * this->nnodes;
58  this->end_time = 5.0;
59  this->lambda = complex<double>(-1.0, 2.0);
60  this->nsteps = { 4, 6, 8, 10, 12 };
61  break;
62 
64  this->niters = this->nnodes + 1;
65  this->end_time = 1.0;
66  this->lambda = complex<double>(-1.0, 1.0);
67  this->nsteps = { 7, 9, 11, 13 };
68  break;
69 
71  this->niters = this->nnodes;
72  this->end_time = 5.0;
73  this->lambda = complex<double>(-1.0, 1.0);
74  this->nsteps = { 9, 11, 13, 15 };
75  break;
76 
77  default:
78  break;
79  }
80 
81  // run to compute errors
82  for (size_t i = 0; i < this->nsteps.size(); ++i) {
83  auto dt = this->end_time / double(this->nsteps[i]);
84  this->err.push_back(run_scalar_sdc(this->nsteps[i], dt, this->nnodes,
85  this->niters, this->lambda, this->nodetype));
86  }
87 
88  // compute convergence rates
89  for (size_t i = 0; i < this->nsteps.size() - 1; ++i) {
90  this->convrate.push_back(log10(this->err[i+1] / this->err[i]) /
91  log10(double(this->nsteps[i]) / double(this->nsteps[i + 1])));
92  }
93  }
94 
95 };
96 
97 /*
98  * The test below verifies that the code approximately (up to a safety factor) reproduces
99  * the theoretically expected rate of convergence
100  */
102 {
103  int order;
104  string quad;
105  double fudge = 0.9;
106 
107  switch (this->nodetype)
108  {
110  order = 2 * this->nnodes - 2;
111  quad = "Gauss-Lobatto";
112  break;
113 
115  order = 2 * this->nnodes;
116  quad = "Gauss-Legendre";
117  break;
118 
120  order = 2 * this->nnodes;
121  quad = "Gauss-Radau";
122  break;
123 
125  order = this->nnodes;
126  quad = "Clenshaw-Curtis";
127  break;
128 
130  order = this->nnodes;
131  fudge = 0.8;
132  quad = "Uniform";
133  break;
134 
135  default:
136  EXPECT_TRUE(false);
137  break;
138  }
139 
140  for (size_t i = 0; i < this->nsteps.size() - 1; ++i) {
141  EXPECT_THAT(convrate[i], Ge<double>(fudge * order)) << "Convergence rate for "
142  << this->nnodes << " " << quad << " nodes"
143  << " for nsteps " << this->nsteps[i]
144  << " not within expected range.";
145  }
146 }
147 
149  Combine(Range<size_t>(3, 7),
155 );
156 
157 int main(int argc, char** argv)
158 {
159  testing::InitGoogleTest(&argc, argv);
160  return RUN_ALL_TESTS();
161 }
QuadratureType
Quadrature type descriptors.
Definition: interface.hpp:32
STL namespace.
int main(int argc, char **argv)
virtual void SetUp()
INSTANTIATE_TEST_CASE_P(ScalarSDC, ConvergenceTest, Combine(Range< size_t >(3, 7), Values(pfasst::quadrature::QuadratureType::GaussLobatto, pfasst::quadrature::QuadratureType::GaussLegendre, pfasst::quadrature::QuadratureType::GaussRadau, pfasst::quadrature::QuadratureType::ClenshawCurtis, pfasst::quadrature::QuadratureType::Uniform)))
double run_scalar_sdc(const size_t nsteps, const double dt, const size_t nnodes, const size_t niters, const complex< double > lambda, const quadrature::QuadratureType nodetype)
Scalar test equation example using an encapsulated IMEX sweeper.
Definition: scalar_sdc.cpp:38
complex< double > lambda
TEST_P(ConvergenceTest, AllNodes)
float dt
Definition: plot.py:10