Skip to content

Commit 38f6a8e

Browse files
committed
scalar: some further cleanup
1 parent 96ce86c commit 38f6a8e

File tree

3 files changed

+45
-55
lines changed

3 files changed

+45
-55
lines changed

examples/scalar/scalar_sdc.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,27 @@
1818
*/
1919

2020
double run_scalar_sdc(const size_t nsteps, const double dt, const size_t nnodes,
21-
const size_t niters, const complex<double> lambda)
21+
const size_t niters, const complex<double> lambda)
2222
{
23-
2423
pfasst::SDC<> sdc;
2524

26-
const complex<double> y0 = complex<double>(1.0, 0.0);
25+
const complex<double> y0 = complex<double>(1.0, 0.0);
2726
auto nodes = pfasst::compute_nodes(nnodes, "gauss-lobatto");
2827
auto factory = make_shared<pfasst::encap::VectorFactory<complex<double>>>(1);
29-
auto sweeper = make_shared<ScalarSweeper<>>(lambda,y0);
30-
28+
auto sweeper = make_shared<ScalarSweeper<>>(lambda, y0);
29+
3130
sweeper->set_nodes(nodes);
3231
sweeper->set_factory(factory);
33-
32+
3433
sdc.add_level(sweeper);
3534
sdc.set_duration(0.0, dt*nsteps, dt, niters);
3635
sdc.setup();
37-
36+
3837
auto q0 = sweeper->get_state(0);
3938
sweeper->exact(q0, 0.0);
40-
39+
4140
sdc.run();
42-
41+
4342
return sweeper->get_errors();
4443
}
4544

@@ -51,7 +50,7 @@ int main(int /*argc*/, char** /*argv*/)
5150
const size_t nnodes = 4;
5251
const size_t niters = 6;
5352
const complex<double> lambda = complex<double>(-1.0, 1.0);
54-
53+
5554
run_scalar_sdc(nsteps, dt, nnodes, niters, lambda);
5655
}
5756
#endif

examples/scalar/scalar_sweeper.hpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,27 @@ class ScalarSweeper
2121
typedef pfasst::encap::Encapsulation<time> encap_type;
2222
typedef pfasst::encap::VectorEncapsulation<complex<double>> complex_vector_type;
2323

24-
complex<double> _lambda, _u0;
25-
int _n_f_expl_eval, _n_f_impl_eval, _n_impl_solve;
24+
complex<double> lambda, u0;
25+
size_t n_f_expl_eval, n_f_impl_eval, n_impl_solve;
2626
const complex<double> i_complex = complex<double>(0, 1);
27-
double _error;
27+
double error;
2828

2929
public:
3030
ScalarSweeper(complex<double> lambda, complex<double> u0)
31-
: _lambda(lambda)
32-
, _u0(u0)
33-
, _n_f_expl_eval(0)
34-
, _n_f_impl_eval(0)
35-
, _n_impl_solve(0)
36-
, _error(0.0)
31+
: lambda(lambda)
32+
, u0(u0)
33+
, n_f_expl_eval(0)
34+
, n_f_impl_eval(0)
35+
, n_impl_solve(0)
36+
, error(0.0)
3737
{}
3838

3939
virtual ~ScalarSweeper()
4040
{
41-
cout << "Final error: " << scientific << this->_error << endl;
42-
cout << "Number of explicit evaluations: " << this->_n_f_expl_eval << endl;
43-
cout << "Number of implicit evaluations: " << this->_n_f_impl_eval << endl;
44-
cout << "Number of implicit solves: " << this->_n_impl_solve << endl;
41+
cout << "Final error: " << scientific << this->error << endl;
42+
cout << "Number of explicit evaluations: " << this->n_f_expl_eval << endl;
43+
cout << "Number of implicit evaluations: " << this->n_f_impl_eval << endl;
44+
cout << "Number of implicit solves: " << this->n_impl_solve << endl;
4545
}
4646

4747
void echo_error(time t)
@@ -53,12 +53,12 @@ class ScalarSweeper
5353
this->exact(qex, t);
5454
double max_err = abs(qend[0] - qex[0]) / abs(qex[0]);
5555
cout << "err: " << scientific << max_err << endl;
56-
this->_error = max_err;
56+
this->error = max_err;
5757
}
58-
58+
5959
double get_errors()
6060
{
61-
return this->_error;
61+
return this->error;
6262
}
6363

6464
void predict(bool initial) override
@@ -79,7 +79,7 @@ class ScalarSweeper
7979

8080
void exact(complex_vector_type& q, time t)
8181
{
82-
q[0] = this->_u0 * exp(this->_lambda * t);
82+
q[0] = this->u0 * exp(this->lambda * t);
8383
}
8484

8585
void exact(shared_ptr<encap_type> q_encap, time t)
@@ -96,9 +96,9 @@ class ScalarSweeper
9696
auto& q = pfasst::encap::as_vector<complex<double>, time>(q_encap);
9797

9898
// f_expl = multiply with imaginary part of lambda
99-
f[0] = this->i_complex * imag(this->_lambda) * q[0];
99+
f[0] = this->i_complex * imag(this->lambda) * q[0];
100100

101-
this->_n_f_expl_eval++;
101+
this->n_f_expl_eval++;
102102
}
103103

104104
void f_impl_eval(shared_ptr<encap_type> f_encap,
@@ -109,9 +109,9 @@ class ScalarSweeper
109109
auto& q = pfasst::encap::as_vector<complex<double>, time>(q_encap);
110110

111111
// f_impl = multiply with real part of lambda
112-
f[0] = real(this->_lambda) * q[0];
112+
f[0] = real(this->lambda) * q[0];
113113

114-
this->_n_f_impl_eval++;
114+
this->n_f_impl_eval++;
115115
}
116116

117117
void impl_solve(shared_ptr<encap_type> f_encap,
@@ -124,11 +124,11 @@ class ScalarSweeper
124124
auto& rhs = pfasst::encap::as_vector<complex<double>, time>(rhs_encap);
125125

126126
// invert f_impl = multiply with inverse of real part of lambda
127-
double inv = 1.0 / (1.0 - double(dt) * real(this->_lambda));
127+
double inv = 1.0 / (1.0 - double(dt) * real(this->lambda));
128128
q[0] = inv * rhs[0];
129-
f[0] = real(this->_lambda) * q[0];
129+
f[0] = real(this->lambda) * q[0];
130130

131-
this->_n_impl_solve++;
131+
this->n_impl_solve++;
132132
}
133133
};
134134

tests/examples/scalar/test_scalar.cpp

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
#include "../examples/scalar/scalar_sdc.cpp"
1111
#undef PFASST_UNIT_TESTING
1212

13-
MATCHER (DoubleMore, "")
14-
{
15-
return get<0>(arg) > get<1>(arg);
16-
}
17-
1813

1914
/*
2015
* For Lobatto nodes, the resulting method should of order 2*M-2 with M=number of nodes
@@ -23,49 +18,45 @@ MATCHER (DoubleMore, "")
2318
*/
2419
TEST(ConvergenceTest, ScalarSDC)
2520
{
26-
2721
const complex<double> lambda = complex<double>(-1.0, 1.0);
2822
const double Tend = 4.0;
2923
const vector<size_t> nsteps = { 2, 5, 10, 15, 20 };
3024
size_t nsteps_l = nsteps.size();
3125

3226
vector<double> err(nsteps.size());
3327
vector<double> convrate(nsteps.size()-1);
34-
28+
3529
double dt;
36-
37-
// Test converge up to 6 nodes: For more nodes, errors are of the order of machine
38-
// precision and convergence can no longer be monitored
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
3933
for ( size_t nnodes = 2; nnodes<=6; ++nnodes)
4034
{
4135
// Expect convergence rate of 2*nodes-2 from collocation formula,
4236
// doing an identical number of iteration should suffice to reach this as each
4337
// iteration should increase order by one
4438
size_t niters = 2*nnodes-2;
45-
39+
4640
for ( size_t i = 0; i<=nsteps_l-1; ++i)
4741
{
4842
dt = Tend/double(nsteps[i]);
4943
err[i] = run_scalar_sdc(nsteps[i], dt, nnodes, niters, lambda);
5044
}
51-
45+
5246
for ( size_t i = 0; i<=nsteps_l-2; ++i)
5347
{
5448
convrate[i] = log10(err[i+1]/err[i])/log10(double(nsteps[i])/double(nsteps[i+1]));
55-
56-
EXPECT_THAT(convrate[i], testing::DoubleNear(double(2*nnodes-2), 0.99)) << "Convergence rate at node " << i << " not within expected range";
49+
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";
5754
}
58-
59-
// NOTE: There is probably a much more elegant way to test this, because
60-
// expected_cr contains the same value in all entries. But I could not so far figure
61-
// out how to build a more clever MATCHER here so far....
62-
// EXPECT_THAT(convrate, testing::Pointwise(DoubleMore(), expected_cr ));
63-
6455
}
6556
}
6657

6758
int main(int argc, char** argv)
6859
{
6960
testing::InitGoogleTest(&argc, argv);
7061
return RUN_ALL_TESTS();
71-
}
62+
}

0 commit comments

Comments
 (0)