|
| 1 | +/* |
| 2 | + * Sweeper for scalar test equation |
| 3 | + */ |
| 4 | + |
| 5 | +#ifndef _SCALAR_SWEEPER_HPP_ |
| 6 | +#define _SCALAR_SWEEPER_HPP_ |
| 7 | + |
| 8 | +#include <complex> |
| 9 | +#include <vector> |
| 10 | +#include <pfasst/encap/imex_sweeper.hpp> |
| 11 | +#include <pfasst/encap/vector.hpp> |
| 12 | + |
| 13 | +using namespace std; |
| 14 | + |
| 15 | +template<typename time = pfasst::time_precision> |
| 16 | +class ScalarSweeper : public pfasst::encap::IMEXSweeper<time> |
| 17 | +{ |
| 18 | + |
| 19 | + typedef pfasst::encap::Encapsulation<time> Encapsulation; |
| 20 | + typedef pfasst::encap::VectorEncapsulation<complex<double>> CVectorT; |
| 21 | + |
| 22 | + public: |
| 23 | + |
| 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 | + } |
| 32 | + |
| 33 | + virtual ~ScalarSweeper() |
| 34 | + { |
| 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; |
| 38 | + } |
| 39 | + |
| 40 | + void echo_error(time t) |
| 41 | + { |
| 42 | + auto& qend = pfasst::encap::as_vector<complex<double>,time>(this->get_end_state()); |
| 43 | + |
| 44 | + CVectorT qex(qend.size()); |
| 45 | + |
| 46 | + this->exact(qex, t); |
| 47 | + double max_err = abs(qend[0] - qex[0]) / abs(qex[0]); |
| 48 | + cout << "err: " << scientific << max_err << endl; |
| 49 | + } |
| 50 | + |
| 51 | + void predict(bool initial) override |
| 52 | + { |
| 53 | + pfasst::encap::IMEXSweeper<time>::predict(initial); |
| 54 | + time t = this->get_controller()->get_time(); |
| 55 | + time dt = this->get_controller()->get_time_step(); |
| 56 | + this->echo_error(t + dt); |
| 57 | + } |
| 58 | + |
| 59 | + void sweep() override |
| 60 | + { |
| 61 | + pfasst::encap::IMEXSweeper<time>::sweep(); |
| 62 | + time t = this->get_controller()->get_time(); |
| 63 | + time dt = this->get_controller()->get_time_step(); |
| 64 | + this->echo_error(t + dt); |
| 65 | + } |
| 66 | + |
| 67 | + void exact(CVectorT& q, time t) |
| 68 | + { |
| 69 | + q[0] = _y0 * exp(_lambda * t); |
| 70 | + } |
| 71 | + |
| 72 | + void exact(shared_ptr<Encapsulation> q_encap, time t) |
| 73 | + { |
| 74 | + auto& q = pfasst::encap::as_vector<complex<double>,time>(q_encap); |
| 75 | + exact(q, t); |
| 76 | + } |
| 77 | + |
| 78 | + void f_expl_eval(shared_ptr<Encapsulation> f_encap, |
| 79 | + shared_ptr<Encapsulation> q_encap, time t) override |
| 80 | + { |
| 81 | + 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); |
| 84 | + |
| 85 | + // f1 = multiply with imaginary part of lambda |
| 86 | + f[0] = i_complex * imag(this->_lambda) * q[0]; |
| 87 | + this->_nf1eval++; |
| 88 | + } |
| 89 | + |
| 90 | + void f_impl_eval(shared_ptr<Encapsulation> f_encap, |
| 91 | + shared_ptr<Encapsulation> q_encap, time t) override |
| 92 | + { |
| 93 | + 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); |
| 96 | + |
| 97 | + // f2 = multiply with real part of lambda |
| 98 | + f[0] = real(this->_lambda) * q[0]; |
| 99 | + this->_nf2eval++; |
| 100 | + } |
| 101 | + |
| 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 |
| 105 | + { |
| 106 | + 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); |
| 110 | + |
| 111 | + // invert f2=multiply with inverse of real part of lambda |
| 112 | + double inv = 1 / (1 - double(dt) * real(this->_lambda)); |
| 113 | + q[0] = inv * rhs[0]; |
| 114 | + 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); |
| 124 | + |
| 125 | +}; |
| 126 | +#endif |
0 commit comments