KASKADE 7 development version
sdcEuler.hh
Go to the documentation of this file.
1/*
2 * sdcEuler.hh
3 *
4 * Created on: May 17, 2015
5 * Author: sunayana_ghosh
6 */
7/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
8/* */
9/* This file is part of the library KASKADE 7 */
10/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
11/* */
12/* Copyright (C) 2015-2015 Zuse Institute Berlin */
13/* */
14/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
15/* see $KASKADE/academic.txt */
16/* */
17/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
18#ifndef KASKADE_TIMESTEPPING_EULERSDC_SDCEULER_HH_
19#define KASKADE_TIMESTEPPING_EULERSDC_SDCEULER_HH_
20
21//includes from current project
22#include "norm.hh"
23#include "util.hh"
24#include "workModel.hh"
25
26
27//includes from c, c++
28#include <algorithm>
29#include <cmath>
30#include <fstream>
31#include <functional>
32#include <iostream>
33#include <vector>
34
35//includes from DUNE
36#include "dune/common/dynmatrix.hh"
37#include "dune/common/dynvector.hh"
38
39//includes from Kaskade
40#include "timestepping/sdc.hh"
41
42namespace Kaskade {
43//=======================================================================================================
44// Implementation for enum class AlgorithmType
45//=======================================================================================================
51enum class AlgorithmType {
52 ADAPTIVE,
53 NAIVE
54};
55
56//=======================================================================================================
57// Implementation for template class EulerSDC
58//=======================================================================================================
59
66template<class Vector, class Norm, class Utils, class TimeGrid=LobattoTimeGrid>
68{
69public:
70 using field_type = typename Vector::value_type;
71
72 using RealVector = Dune::DynamicVector<double>;
73 using RealMatrix = Dune::DynamicMatrix<double>;
74
77
78 //constructor for class EulerSDC
79 //The constructor consists of member initializer lists which are relevant for all work models
80 EulerSDC(field_type t0, field_type t1, size_t nIntervals_,
81 field_type tol_, field_type maxSDCIterations_,
82 Norm& norm_, Utils& util_,
83 Kaskade::NormType norm_type, Kaskade::WorkModelType work_type,
84 bool verbose_ = false) :
85 tbegin(t0), tend(t1), nIntervals(nIntervals_), grid(nIntervals, tbegin, tend),
86 tol(tol_), maxSDCIterations(maxSDCIterations_),
87 norm(norm_), util(util_), norm_t(norm_type), work_t(work_type), verbose(verbose_) {}
88
89
90 //private members necessary for different work models are set using the set methods.
91 //set tolerance for Newton method used for the Iteration work model
93 {
94 tolNewton = newtTol;
95 }
96
97 //set max iterations for Newton method, used for the Iteration work model
98 void setNewtonIterations(int newtIter)
99 {
100 maxIterNewton = newtIter;
101 }
102
103 //set rhoit: contraction factor for the iteration work model
105 {
106 rhoit = rit;
107 }
108
109 //set mmin: minimum number of iterations necessary for the iteration work model.
110 void setMmin(int minnum)
111 {
112 mmin = minnum;
113 }
114
115 //set dimensions, used in case of the FiniteElementDiscretization work model.
116 void setDimFED(int d)
117 {
118 dim = d;
119 //std::cout << "dim = " << dim << std::endl;
120 }
121
122 //set localTolerance for NAIVE ALGORITHM
124 {
125 localTol = locTol;
126 }
127
128
129
138 Vector integrate(Vector const& initialValue,
139 std::function<Vector (typename Vector::value_type, Vector const&)> rightHandSide,
140 Kaskade::AlgorithmType integrate_t)
141 {
142 //initialize the solution vector yi and the error vector dyi
143 std::vector<Vector> yi(grid.points().size(), initialValue), dyi(grid.points().size(), Vector(0.0));
144 std::vector<Vector> y0 = yi;
145 std::vector<Vector> yPrev = yi;
146
147 //TODO: Change to dump output to a log file
148 //print the entries of the vector yi
149 if (verbose)
150 {
151 std::cout << "print entries to the vector yi" << std::endl;
152 for (auto i = 0u; i < yi.size(); ++i)
153 std::cout << "[ " << yi[i] << " ]" << std::endl;
154 }
155
156 //extract time points and integration matrix from timegrid
157 auto const& tPts = grid.points();
158 auto const& integMat = grid.integrationMatrix();
159
160 //initialize the tolerance vector to zero entries
161 //usually the tolerance vector is the last column of the tolerance matrix.
162 //We can compute an actual tolerance vector only after the first two computes of the inexact SDC iteration step.
163 RealVector tolVector(nIntervals+1, 0.0);
164 field_type rho = 0.5; //is there a need to set the value of rho?
165
166 //first inexactSDC step, where the tolVector is set to a zero vector and rho = 0.5.
167 Kaskade::inexactSDCExplicitEulerIterationStep(grid, rightHandSide, tolVector, rho, norm_t, yi, dyi, verbose);
168 //create vectors y1, yCurrent
169 std::vector<Vector> y1 = yi;
170 std::vector<Vector> yCurrent = yi;
171 std::vector<Vector> yNext = yi;
172 int totalJ = 2;
173
174
175 while (maxSDCIterations > 0)
176 {
177 //print dyi
178 if (verbose)
179 {
180 std::cout << "dyi = ";
181 for (int i = 0; i < dyi.size(); ++i)
182 {
183 std::cout << "dyi[" << i << "] = " << dyi[i] << std::endl;
184 }
185 }
186
187 //carry out an inexact SDC step
188 Kaskade::inexactSDCExplicitEulerIterationStep(grid, rightHandSide, tolVector, rho, norm_t, yi, dyi, verbose);
189 yNext = yi;
190 //compute the sdc contraction factor
191 rho = util.sdcContractionFactor(yPrev, yCurrent, yNext, norm);
192 //TODO: change to setw() instead of tab
193 if(totalJ == 2)
194 std::cout << "\t \t \t \t \t \t \t \t \t \t \t " << rho << std::endl;
195 else
196 std::cout << "\t \t \t " << rho << std::endl;
197
198 //compute alphaVec or gammaVec depending on the norm
199 auto alphaVec = util.computeAlphaVec(tPts, integMat, yPrev, yCurrent, yNext);
200
201 //Create the work model object depending on the work model type
202 switch(work_t)
203 {
204 //=====================================================================================
205 // Iteration Work Model
206 //=====================================================================================
208 {
209 //initialize the iteration work model object
210 Iter workModel(tol, y0, y1, norm, util, tPts, integMat, tolNewton, maxIterNewton, rhoit, mmin);
211 //compute maxSDCIterations
212 maxSDCIterations = workModel.getIterJ(yPrev, yCurrent, yNext, rho);
213 std::cout << "\t \t" << maxSDCIterations;
214 if(maxSDCIterations > 0)
215 {
216 switch(integrate_t)
217 {
219 {
220 tolMat = workModel.computeLocalTolerances(yPrev, yCurrent, yNext);
221 break;
222 }
224 {
225 //create the matrix of local tolerances
226 tolMat = RealMatrix(nIntervals, maxSDCIterations, localTol);
227 break;
228 }
229 }
230 //extract the tolVector
231 tolVector = extractLastColumn(tolMat);
232 totalCost = workModel.computeCost(yPrev, yCurrent, yNext, tolMat);
233 val = std::log(totalCost);
234 std::cout << "\t\t" << val;
235 }
236 break;
237 }
238 //=====================================================================================
239 // FiniteElementDiscretization Work Model
240 //=====================================================================================
242 {
243 //initialize the finite element discretization work model object
244 Fed workModel(tol, y0, y1, norm, util, tPts, integMat, dim);
245 //compute maxSDCIterations
246 maxSDCIterations = workModel.getIterJ(yPrev, yCurrent, rho);
247 std::cout << "\t \t" << maxSDCIterations;
248 if (maxSDCIterations > 0)
249 {
250 switch(integrate_t)
251 {
253 {
254 tolMat = workModel.computeLocalTolerances(yPrev, yCurrent, yNext);
255 break;
256 }
258 {
259 //create the matrix of local tolerances
260 tolMat = RealMatrix(nIntervals, maxSDCIterations, localTol);
261 break;
262 }
263 } //end switch for type of integration algorithm
264 //extract the tolVector
265 tolVector = extractLastColumn(tolMat);
266 totalCost = workModel.computeCost(tolMat);
267 val = std::log(totalCost);
268 std::cout << "\t \t" << val;
269 }
270 break;
271 }
272 }
273
274 ++totalJ;
275 if (maxSDCIterations > 0)
276 std::cout << "\t \t \t \t " << totalJ;
277 else
278 std::cout << "\t \t \t \t \t \t " << totalJ;
279 yPrev = yCurrent;
280 yCurrent = yNext;
281
282 }
283 //reset maxSDCIterations so as to be able to use another algorithm (NAIVE or ADAPTIVE)
284 maxSDCIterations = 1;
285 return yi.back();
286 } //end integrate function
287
288private:
289 field_type tbegin;
290 field_type tend;
291 size_t nIntervals;
292 TimeGrid grid;
293 field_type tol;
294 field_type maxSDCIterations;
295 Norm norm;
296 Utils util;
297 Kaskade::NormType norm_t;
299 bool verbose;
300 field_type tolNewton = 1e-2;
301 int maxIterNewton = 1000;
302 int dim = 2;
303 field_type rhoit = 0.5;
304 int mmin = 3;
305 RealMatrix tolMat;
306 RealVector tolVec;
307 field_type totalCost;
308 field_type localTol = 1e-3;
309 field_type val;
310
311
312
313 //private function to extract the last column of a given matrix
314 RealVector const& extractLastColumn(RealMatrix const& toleranceMatrix)
315 {
316 auto rows = toleranceMatrix.rows();
317 auto cols = toleranceMatrix.cols();
318 tolVec = RealVector(rows, 0.0);
319 for (auto i = 0; i < rows; ++i)
320 {
321 tolVec[i] = toleranceMatrix[i][cols-1];
322 }
323 return tolVec;
324 }
325};
326
327} //end namespace Kaskade
328
329
330#endif /* KASKADE_TIMESTEPPING_EULERSDC_SDCEULER_HH_ */
Base class to perform SDC iterations based on the forward Euler Method. Total iterations performed de...
Definition: sdcEuler.hh:68
void setDimFED(int d)
Definition: sdcEuler.hh:116
void setNaiveLocalTol(field_type locTol)
Definition: sdcEuler.hh:123
void setMmin(int minnum)
Definition: sdcEuler.hh:110
Vector integrate(Vector const &initialValue, std::function< Vector(typename Vector::value_type, Vector const &)> rightHandSide, Kaskade::AlgorithmType integrate_t)
Definition: sdcEuler.hh:138
void setRhoIT(field_type rit)
Definition: sdcEuler.hh:104
EulerSDC(field_type t0, field_type t1, size_t nIntervals_, field_type tol_, field_type maxSDCIterations_, Norm &norm_, Utils &util_, Kaskade::NormType norm_type, Kaskade::WorkModelType work_type, bool verbose_=false)
Definition: sdcEuler.hh:80
typename Vector::value_type field_type
Definition: sdcEuler.hh:70
void setNewtonTolerance(field_type newtTol)
Definition: sdcEuler.hh:92
Dune::DynamicVector< double > RealVector
Definition: sdcEuler.hh:72
void setNewtonIterations(int newtIter)
Definition: sdcEuler.hh:98
Dune::DynamicMatrix< double > RealMatrix
Definition: sdcEuler.hh:73
field_type computeCost(RealMatrix const &tolMat)
Definition: workModel.hh:620
virtual RealMatrix const & computeLocalTolerances(std::vector< Vector > const &yPrev, std::vector< Vector > const &yCurrent, std::vector< Vector > const &yNext)
Pure virtual function implemented in derived classes for different work models. Computes the local to...
Definition: workModel.hh:309
field_type getIterJ(std::vector< Vector > const &yPrev, std::vector< Vector > const &yCurrent, field_type rho)
Definition: workModel.hh:640
field_type computeCost(std::vector< Vector > const &yPrev, std::vector< Vector > const &yCurrent, std::vector< Vector > const &yNext, RealMatrix const &tolMat)
Definition: workModel.hh:408
field_type getIterJ(std::vector< Vector > const &yPrev, std::vector< Vector > const &yCurrent, std::vector< Vector > const &yNext, field_type rho)
Definition: workModel.hh:438
virtual RealMatrix const & computeLocalTolerances(std::vector< Vector > const &yPrev, std::vector< Vector > const &yCurrent, std::vector< Vector > const &yNext)
Pure virtual function implemented in derived classes for different work models. Computes the local to...
Definition: workModel.hh:167
Abstract base class of various norms.
Definition: norm.hh:59
Dune::FieldVector< Scalar, dim > Vector
Dune::DynamicVector< double > RealVector
Definition: util.hh:498
AlgorithmType
enum class to steer the main integrate algorithm in EulerSDC class in a certain way.
Definition: sdcEuler.hh:51
WorkModelType
Enum class to define the norm type is class Norm.
Definition: workModel.hh:49
NormType
Enum class to define the norm type in class Norm.
Definition: norm.hh:39
Dune::DynamicMatrix< double > RealMatrix
Definition: util.hh:499
void inexactSDCExplicitEulerIterationStep(Kaskade::SDCTimeGrid const &grid, std::function< Vector(typename Vector::value_type, Vector const &)> rhsFunc, RealVector const &toleranceVector, typename Vector::value_type rho, Kaskade::NormType norm_t, std::vector< Vector > &yi, std::vector< Vector > &dyi, bool verbose=false)
Definition: util.hh:167