Skip to content

Commit 0feb7c7

Browse files
committed
examples: scalar: some code cleanup
1 parent c9bf1b1 commit 0feb7c7

File tree

1 file changed

+55
-54
lines changed

1 file changed

+55
-54
lines changed

examples/scalar/scalar_sweeper.hpp

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,51 @@
22
* Sweeper for scalar test equation
33
*/
44

5-
#ifndef _SCALAR_SWEEPER_HPP_
6-
#define _SCALAR_SWEEPER_HPP_
5+
#ifndef _EXAMPLES__SCALAR__SCALAR_SWEEPER_HPP_
6+
#define _EXAMPLES__SCALAR__SCALAR_SWEEPER_HPP_
77

88
#include <complex>
99
#include <vector>
10+
1011
#include <pfasst/encap/imex_sweeper.hpp>
1112
#include <pfasst/encap/vector.hpp>
1213

1314
using namespace std;
1415

1516
template<typename time = pfasst::time_precision>
16-
class ScalarSweeper : public pfasst::encap::IMEXSweeper<time>
17+
class ScalarSweeper
18+
: public pfasst::encap::IMEXSweeper<time>
1719
{
20+
private:
21+
typedef pfasst::encap::Encapsulation<time> encap_type;
22+
typedef pfasst::encap::VectorEncapsulation<complex<double>> complex_vector_type;
1823

19-
typedef pfasst::encap::Encapsulation<time> Encapsulation;
20-
typedef pfasst::encap::VectorEncapsulation<complex<double>> CVectorT;
24+
complex<double> _lambda, _u0;
25+
int _n_f_expl_eval, _n_f_impl_eval, _n_impl_solve;
26+
const complex<double> i_complex = complex<double>(0, 1);
2127

2228
public:
2329

24-
ScalarSweeper(complex<double> lambda, complex<double> y0)
25-
{
26-
this->_lambda = lambda;
27-
this->_y0 = y0;
28-
this->_nf1eval = 0;
29-
this->_nf2eval = 0;
30-
this->_nf2comp = 0;
31-
}
30+
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+
{}
3237

3338
virtual ~ScalarSweeper()
3439
{
35-
cout << "Number of calls to f1eval: " << this->_nf1eval << endl;
36-
cout << "Number of calls to f2eval: " << this->_nf2eval << endl;
37-
cout << "Number of calls to f2comp: " << this->_nf2comp << endl;
40+
cout << "Number of explicit evaluations: " << this->_n_f_expl_eval << endl;
41+
cout << "Number of implicit evaluations: " << this->_n_f_impl_eval << endl;
42+
cout << "Number of implicit solves: " << this->_n_impl_solve << endl;
3843
}
3944

4045
void echo_error(time t)
4146
{
42-
auto& qend = pfasst::encap::as_vector<complex<double>,time>(this->get_end_state());
47+
auto& qend = pfasst::encap::as_vector<complex<double>, time>(this->get_end_state());
4348

44-
CVectorT qex(qend.size());
49+
complex_vector_type qex(qend.size());
4550

4651
this->exact(qex, t);
4752
double max_err = abs(qend[0] - qex[0]) / abs(qex[0]);
@@ -64,63 +69,59 @@ class ScalarSweeper : public pfasst::encap::IMEXSweeper<time>
6469
this->echo_error(t + dt);
6570
}
6671

67-
void exact(CVectorT& q, time t)
72+
void exact(complex_vector_type& q, time t)
6873
{
69-
q[0] = _y0 * exp(_lambda * t);
74+
q[0] = this->_u0 * exp(this->_lambda * t);
7075
}
7176

72-
void exact(shared_ptr<Encapsulation> q_encap, time t)
77+
void exact(shared_ptr<encap_type> q_encap, time t)
7378
{
74-
auto& q = pfasst::encap::as_vector<complex<double>,time>(q_encap);
75-
exact(q, t);
79+
auto& q = pfasst::encap::as_vector<complex<double>, time>(q_encap);
80+
this->exact(q, t);
7681
}
7782

78-
void f_expl_eval(shared_ptr<Encapsulation> f_encap,
79-
shared_ptr<Encapsulation> q_encap, time t) override
83+
void f_expl_eval(shared_ptr<encap_type> f_encap,
84+
shared_ptr<encap_type> q_encap, time t) override
8085
{
8186
UNUSED(t);
82-
auto& f = pfasst::encap::as_vector<complex<double>,time>(f_encap);
83-
auto& q = pfasst::encap::as_vector<complex<double>,time>(q_encap);
87+
auto& f = pfasst::encap::as_vector<complex<double>, time>(f_encap);
88+
auto& q = pfasst::encap::as_vector<complex<double>, time>(q_encap);
89+
90+
// f_expl = multiply with imaginary part of lambda
91+
f[0] = this->i_complex * imag(this->_lambda) * q[0];
8492

85-
// f1 = multiply with imaginary part of lambda
86-
f[0] = i_complex * imag(this->_lambda) * q[0];
87-
this->_nf1eval++;
93+
this->_n_f_expl_eval++;
8894
}
8995

90-
void f_impl_eval(shared_ptr<Encapsulation> f_encap,
91-
shared_ptr<Encapsulation> q_encap, time t) override
96+
void f_impl_eval(shared_ptr<encap_type> f_encap,
97+
shared_ptr<encap_type> q_encap, time t) override
9298
{
9399
UNUSED(t);
94-
auto& f = pfasst::encap::as_vector<complex<double>,time>(f_encap);
95-
auto& q = pfasst::encap::as_vector<complex<double>,time>(q_encap);
100+
auto& f = pfasst::encap::as_vector<complex<double>, time>(f_encap);
101+
auto& q = pfasst::encap::as_vector<complex<double>, time>(q_encap);
96102

97-
// f2 = multiply with real part of lambda
103+
// f_impl = multiply with real part of lambda
98104
f[0] = real(this->_lambda) * q[0];
99-
this->_nf2eval++;
105+
106+
this->_n_f_impl_eval++;
100107
}
101108

102-
void impl_solve(shared_ptr<Encapsulation> f_encap,
103-
shared_ptr<Encapsulation> q_encap, time t, time dt,
104-
shared_ptr<Encapsulation> rhs_encap) override
109+
void impl_solve(shared_ptr<encap_type> f_encap,
110+
shared_ptr<encap_type> q_encap, time t, time dt,
111+
shared_ptr<encap_type> rhs_encap) override
105112
{
106113
UNUSED(t);
107-
auto& f = pfasst::encap::as_vector<complex<double>,time>(f_encap);
108-
auto& q = pfasst::encap::as_vector<complex<double>,time>(q_encap);
109-
auto& rhs = pfasst::encap::as_vector<complex<double>,time>(rhs_encap);
114+
auto& f = pfasst::encap::as_vector<complex<double>, time>(f_encap);
115+
auto& q = pfasst::encap::as_vector<complex<double>, time>(q_encap);
116+
auto& rhs = pfasst::encap::as_vector<complex<double>, time>(rhs_encap);
110117

111-
// invert f2=multiply with inverse of real part of lambda
112-
double inv = 1 / (1 - double(dt) * real(this->_lambda));
118+
// invert f_impl = multiply with inverse of real part of lambda
119+
double inv = 1.0 / (1.0 - double(dt) * real(this->_lambda));
113120
q[0] = inv * rhs[0];
114121
f[0] = real(this->_lambda) * q[0];
115-
this->_nf2comp++;
116-
}
117-
118-
119-
private:
120-
121-
complex<double> _lambda, _y0;
122-
int _nf1eval, _nf2eval, _nf2comp;
123-
const complex<double> i_complex = complex<double>(0, 1);
124122

123+
this->_n_impl_solve++;
124+
}
125125
};
126-
#endif
126+
127+
#endif // _EXAMPLES__SCALAR__SCALAR_SWEEPER_HPP_

0 commit comments

Comments
 (0)