1- /*
2- * Sweeper for scalar test equation
3- */
4-
51#ifndef _EXAMPLES__SCALAR__SCALAR_SWEEPER_HPP_
62#define _EXAMPLES__SCALAR__SCALAR_SWEEPER_HPP_
73
1410using namespace std ;
1511
1612template <typename time = pfasst::time_precision>
13+
14+ /* *
15+ * Sweeper for scalar test equation
16+ *
17+ * \\( u' = \\lambda*u \\quad\\text{ , } u(0) = u_0 \\)
18+ *
19+ * with complex lambda using an IMEX scheme. Derived from the generic imex_sweeper.
20+ *
21+ */
1722class ScalarSweeper
1823 : public pfasst::encap::IMEXSweeper<time>
1924{
25+
2026 private:
2127 typedef pfasst::encap::Encapsulation<time> encap_type;
28+
29+ // ! Define a type for a complex PFASST vector encapsulation.
2230 typedef pfasst::encap::VectorEncapsulation<complex <double >> complex_vector_type;
23-
31+
32+ // ! Parameter lambda and initial value u0
2433 complex <double > lambda, u0;
25- size_t n_f_expl_eval, n_f_impl_eval, n_impl_solve;
26- const complex <double > i_complex = complex <double >(0 , 1 );
34+
35+ // ! The complex unit i = sqrt(-1)
36+ complex <double > i_complex = complex <double >(0 , 1 );
37+
38+ // ! Error at the final time. For the scalar example, an analytical solution is known.
2739 double error;
40+
41+ // ! Counters for how often f_expl_eval, f_impl_eval and impl_solve are called.
42+ size_t n_f_expl_eval, n_f_impl_eval, n_impl_solve;
2843
2944 public:
30- ScalarSweeper (complex <double > lambda, complex <double > u0)
45+
46+ /* *
47+ * Generic constructor; initialize all function call counters with zero.
48+ * @param[in] lambda coefficient in test equation
49+ * @param[in] u0initial value at \\( t=0 \\)
50+ */
51+ ScalarSweeper (const complex <double > lambda, const complex <double > u0)
3152 : lambda(lambda)
3253 , u0(u0)
3354 , n_f_expl_eval(0 )
@@ -36,6 +57,9 @@ class ScalarSweeper
3657 , error(0.0 )
3758 {}
3859
60+ /* *
61+ * Upon destruction, report final error and number of function calls
62+ */
3963 virtual ~ScalarSweeper ()
4064 {
4165 cout << " Final error: " << scientific << this ->error << endl;
@@ -44,6 +68,10 @@ class ScalarSweeper
4468 cout << " Number of implicit solves: " << this ->n_impl_solve << endl;
4569 }
4670
71+ /* *
72+ * Compute error between last state and exact solution at time tand print it to cout
73+ * @param[in] Time t
74+ */
4775 void echo_error (time t)
4876 {
4977 auto & qend = pfasst::encap::as_vector<complex <double >, time>(this ->get_end_state ());
@@ -56,11 +84,17 @@ class ScalarSweeper
5684 this ->error = max_err;
5785 }
5886
87+ /* *
88+ * Returns error, but does not update it!
89+ */
5990 double get_errors ()
6091 {
6192 return this ->error ;
6293 }
6394
95+ /* *
96+ * Prediction step and update of error. Uses predictor as provided by IMEXSweeper.
97+ */
6498 void predict (bool initial) override
6599 {
66100 pfasst::encap::IMEXSweeper<time>::predict (initial);
@@ -69,6 +103,9 @@ class ScalarSweeper
69103 this ->echo_error (t + dt);
70104 }
71105
106+ /* *
107+ * Perform a sweep and update error. Uses sweep as provided by IMEXSweeper.
108+ */
72109 void sweep () override
73110 {
74111 pfasst::encap::IMEXSweeper<time>::sweep ();
@@ -77,6 +114,11 @@ class ScalarSweeper
77114 this ->echo_error (t + dt);
78115 }
79116
117+ /* *
118+ * Computes the exact solution \\( u_0 \\exp \\left( \\lambda*t \\right) \\)
119+ * at a given time t.
120+ * @param[in] Time t
121+ */
80122 void exact (complex_vector_type& q, time t)
81123 {
82124 q[0 ] = this ->u0 * exp (this ->lambda * t);
@@ -88,6 +130,10 @@ class ScalarSweeper
88130 this ->exact (q, t);
89131 }
90132
133+ /* *
134+ * Evaluate the explicit part of the right hand side: Multiply with
135+ * \\( \\text{imag}(\\lambda) \\)
136+ */
91137 void f_expl_eval (shared_ptr<encap_type> f_encap,
92138 shared_ptr<encap_type> q_encap, time t) override
93139 {
@@ -101,6 +147,10 @@ class ScalarSweeper
101147 this ->n_f_expl_eval ++;
102148 }
103149
150+ /* *
151+ * Evaluate the implicit part of the right hand side: Multiply with
152+ * \\( \\text{real}(\\lambda) \\)
153+ */
104154 void f_impl_eval (shared_ptr<encap_type> f_encap,
105155 shared_ptr<encap_type> q_encap, time t) override
106156 {
@@ -114,6 +164,11 @@ class ScalarSweeper
114164 this ->n_f_impl_eval ++;
115165 }
116166
167+ /* *
168+ * For given \\( b \\), solve
169+ * \\( \\left( \\mathbb{I}_d - \\Delta t \\text{real}(\\lambda) \\right) u = b \\)
170+ * for \\( u \\) and set f_encap to \\( \\text{real}(\\lambda) u \\)
171+ */
117172 void impl_solve (shared_ptr<encap_type> f_encap,
118173 shared_ptr<encap_type> q_encap, time t, time dt,
119174 shared_ptr<encap_type> rhs_encap) override
@@ -126,6 +181,8 @@ class ScalarSweeper
126181 // invert f_impl = multiply with inverse of real part of lambda
127182 double inv = 1.0 / (1.0 - double (dt) * real (this ->lambda ));
128183 q[0 ] = inv * rhs[0 ];
184+
185+ // compute f_impl_eval of q[0], i.e. multiply with real(lambda)
129186 f[0 ] = real (this ->lambda ) * q[0 ];
130187
131188 this ->n_impl_solve ++;
0 commit comments