KASKADE 7 development version
sdc_euler.hh
Go to the documentation of this file.
1#ifndef SDC_EULER_HH
2#define SDC_EULER_HH
3
4#include <algorithm>
5#include <functional>
6#include <vector>
7#include "timestepping/sdc.hh"
8#include "fem/fixdune.hh"
9
10namespace Kaskade
11{
12 //FIXME:Adapt and move the norms to Kaskade::LinAlg::Norm
25 template<class Vector>
26 typename Vector::field_type oneNorm(std::vector<Vector> const& values)
27 {
28 return std::accumulate(values.begin(),values.end(),0.0,[](double init, Vector const& v)
29 {
30 return init + v.one_norm();
31 });
32// auto norm = 0.0;
33
34// for (auto i = 0u; i < values.size(); ++i)
35// //one_norm: public member function of Dune::DenseVector
36// norm += values[i].one_norm();
37
38// return norm;
39 }
40
53 template<class Vector>
54 typename Vector::field_type maxNorm(std::vector<Vector> const& values)
55 {
56 typename Vector::field_type max = values[0].infinity_norm();
57
58 for (auto i = 1u; i < values.size(); ++i)
59 //infinity_norm: public member function of Dune::DenseVector
60 max = std::max(max, values[i].infinity_norm());
61
62 return max;
63 }
64
65
66 //FIXME: make the sdcExEulerIterationStep generic, instead of std::vector<Vector> replace with generic class Vectors.
115 //single SDC iteration step, which returns
116 template <class Vector>
117 typename Vector::field_type sdcExEulerIterationStep(Kaskade::SDCTimeGrid const& grid, std::vector<Vector> & yi, std::vector<Vector> & dyi,
118 std::function<Vector(typename Vector::field_type, Vector const&)> rhsFunc,
119 std::function<typename Vector::field_type(std::vector<Vector> const&)> normFunc,
120 bool verbose = false)
121 {
122 //extracting time points from the time grid
123 auto const& pts = grid.points();
124 //extracting values of the integration matrix corresponding to the time grid
125 auto const& integ = grid.integrationMatrix();
126 //number of subintervals
127 int const n = pts.size() - 1;
128
129 //including starting point for yi and dyi
130 assert(yi.size() == n+1);
131 assert(dyi.size() == n+1);
132
133 //initialize the correction at starting point to zero, where dyi[0] is a vector in general
134 //and every component of dyi is set to 0.0 by assignment below.
135 dyi[0] = 0.0;
136
137 //declare Vector total to be used inside the for loop
138 Vector total(0.0);
139 typename Vector::field_type norm = 0.0;
140
141 //The Sdc step for explicit Euler
142 //perform n Euler steps
143 boost::timer::cpu_timer timer;
144 for (int i = 1; i <= n; ++i)
145 {
146 //Initialize all entries of Vector total to 0.
147 for (size_t t = 0; t < total.size(); ++t)
148 total[t] = 0.0;
149 //Computation of the Lagrange interpolation
150 for (int k = 0; k < n; ++k)
151 total += integ[i-1][k] * rhsFunc(pts[k], yi[k]);
152 //approximate correction computation.
153 dyi[i] = dyi[i-1] + (pts[i] - pts[i-1]) *(rhsFunc(pts[i-1],yi[i-1]+dyi[i-1]) - rhsFunc(pts[i-1],yi[i-1])) + total - yi[i] + yi[i-1];
154
155 }
156
157 //compute the next iteration of yi.
158 for (int i = 0; i <= n; ++i)
159 {
160 yi[i] += dyi[i];
161 if(verbose) std::cout << "yi[" << i << "] = " << yi[i] << "\n";
162 }
163
164 norm = normFunc(dyi);
165 return norm;
166 }
167
176 template <class Vector, class TimeGrid=LobattoTimeGrid>
178 {
179 public:
180 EulerSDC(double t0, double t1, size_t nIntervals, bool verbose_=false)
181 : tbegin(t0), tend(t1), verbose(verbose_), grid(nIntervals,tbegin,tend)
182 {}
183
184 EulerSDC(double t0, double t1, size_t nIntervals, size_t maxSDCIter, bool verbose_=false)
185 : tbegin(t0), tend(t1), maxSDCiterations(maxSDCIter), verbose(verbose_), grid(nIntervals,tbegin,tend)
186 {}
187
188 EulerSDC(double t0, double t1, size_t nIntervals, size_t maxSDCIter, double tol, bool verbose_=false)
189 : tbegin(t0), tend(t1), maxSDCiterations(maxSDCIter), tolerance(tol), verbose(verbose_), grid(nIntervals, tbegin, tend)
190 {}
191
192
193 void setMaxSDCIterations(size_t maxIter)
194 {
195 maxSDCiterations = maxIter;
196 }
197
198 void setTolerance(double tau)
199 {
200 tolerance = tau;
201 }
202
203
204 //integrate function to carry out SDC iterations based on explicit Euler method based on the number of iterations(maxSDCiterations) given by user.
205 Vector integrate( Vector const& initialValue, std::function<Vector (double,Vector const&)> rightHandSide,
206 std::function<typename Vector::field_type(std::vector<Vector const&>)> normFunc)
207 {
208 //initialize the vector yi
209 std::vector<Vector> yi(grid.points().size(),initialValue), dyi(grid.points().size(),Vector(0.0));
210
211 //print the entries of the vector yi
212 if(verbose)
213 {
214 std::cout << "print entries to the vector yi" << std::endl;
215 for (int i = 0; i < yi.size(); ++i)
216 std::cout << "[ " << yi[i] << " ]" << std::endl;
217 }
218
219 for (int i = 0; i < maxSDCiterations; ++i)
220 {
221 if(verbose) std::cout << "Iteration : " << i << "\n";
222 auto stepNorm = sdcExEulerIterationStep(grid, yi, dyi, rightHandSide, normFunc, verbose);
223 if(verbose) std::cout << "norm = " << stepNorm << std::endl;
224 }
225
226 return yi.back();
227 }
228
229 //integrate function to carry out SDC iterations based on explicit Euler method based on the tolerance given by the user.
230 //In case the tolerance is not reached within the default value of maxSDCiterations, the last value of yi at maxSDCiterations is returned.
231 Vector integrateTOL( Vector const& initialValue, std::function<Vector(double,Vector const&)> rightHandSide,
232 std::function<typename Vector::field_type(std::vector<Vector> const&)> normFunc)
233 {
234 //initialize the vector yi with initial value and the correction vector dyi to 0 at all grid points.
235 std::vector<Vector> yi(grid.points().size(),initialValue), dyi(grid.points().size(),Vector(0.0));
236 //initialize loop variable to keep track of when loop exceeds maxSDCiterations
237 int loop = 1;
238
239 //print the entries of the vector yi
240 if(verbose)
241 {
242 std::cout << "print entries to the vector yi" << std::endl;
243 for (int i = 0; i < yi.size(); ++i)
244 std::cout << "[ " << yi[i] << " ]" << std::endl;
245 }
246
247 //while loop to carry out sdc iterations till the given tolerance is reached failing which it loops till maxSDCiterations is reached.
248 auto stepNorm = sdcExEulerIterationStep(grid, yi, dyi, rightHandSide, normFunc, verbose);
249 while (stepNorm > tolerance && loop < maxSDCiterations)
250 {
251 //std::cout << "step norm = " << stepNorm << "\n";
252 if(verbose) std::cout << "Iteration: " << loop << "\n";
253 stepNorm = sdcExEulerIterationStep(grid, yi, dyi, rightHandSide, normFunc, verbose);
254 if(verbose) std::cout << "norm = " << stepNorm << std::endl;
255 ++loop;
256 }
257 return yi.back();
258 }
259
260 private:
261 double tbegin = 0, tend = 1;
262 size_t maxSDCiterations = 100;
263 double tolerance = 0.1;
264 bool verbose = false;
265 TimeGrid grid;
266 };
267
268}
269
270#endif // SDC_EULER_HH
Base class to perform SDC iterations based on forward Euler method. Total iterations performed depend...
Definition: sdc_euler.hh:178
EulerSDC(double t0, double t1, size_t nIntervals, size_t maxSDCIter, double tol, bool verbose_=false)
Definition: sdc_euler.hh:188
Vector integrate(Vector const &initialValue, std::function< Vector(double, Vector const &)> rightHandSide, std::function< typename Vector::field_type(std::vector< Vector const & >)> normFunc)
Definition: sdc_euler.hh:205
EulerSDC(double t0, double t1, size_t nIntervals, bool verbose_=false)
Definition: sdc_euler.hh:180
EulerSDC(double t0, double t1, size_t nIntervals, size_t maxSDCIter, bool verbose_=false)
Definition: sdc_euler.hh:184
Vector integrateTOL(Vector const &initialValue, std::function< Vector(double, Vector const &)> rightHandSide, std::function< typename Vector::field_type(std::vector< Vector > const &)> normFunc)
Definition: sdc_euler.hh:231
void setMaxSDCIterations(size_t maxIter)
Definition: sdc_euler.hh:193
void setTolerance(double tau)
Definition: sdc_euler.hh:198
Abstract base class of time grids for (block) spectral defect correction methods.
Definition: sdc.hh:68
virtual RealVector const & points() const =0
Time points in the time step.
virtual RealMatrix const & integrationMatrix() const =0
Integration matrix .
This file contains various utility functions that augment the basic functionality of Dune.
Dune::FieldVector< T, n > max(Dune::FieldVector< T, n > x, Dune::FieldVector< T, n > const &y)
Componentwise maximum.
Definition: fixdune.hh:110
Dune::FieldVector< Scalar, dim > Vector
Vector::field_type maxNorm(std::vector< Vector > const &values)
A function to compute the partial norm or norm of a vector of vectors. If denotes a vector of vector...
Definition: sdc_euler.hh:54
Vector::field_type sdcExEulerIterationStep(Kaskade::SDCTimeGrid const &grid, std::vector< Vector > &yi, std::vector< Vector > &dyi, std::function< Vector(typename Vector::field_type, Vector const &)> rhsFunc, std::function< typename Vector::field_type(std::vector< Vector > const &)> normFunc, bool verbose=false)
A single spectral defect correction iteration sweep based on explicit Euler method.
Definition: sdc_euler.hh:117