KASKADE 7 development version
Loading...
Searching...
No Matches
limexWithoutJens.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2002-2025 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12#ifndef LIMEX_HH
13#define LIMEX_HH
14
15#include <complex>
16#include <fstream>
17#include <iostream>
18#include <iomanip>
19#include <cstdlib>
20
21#include <boost/timer/timer.hpp>
22
23#include "dune/istl/solvers.hh"
24#include "dune/istl/preconditioners.hh"
25
27#include "fem/iterate_grid.hh"
30
31#include "fem/assemble.hh"
32#include "fem/istlinterface.hh"
33#include "fem/functional_aux.hh"
35#include "fem/lagrangespace.hh"
36
39#include "linalg/mumps_solve.hh"
40// #include "linalg/superlu_solve.hh"
41
43#include "linalg/direct.hh"
44#include "linalg/triplet.hh"
45// #include "linalg/iluprecond.hh"
46// #include "linalg/hyprecond.hh"
48
49#include "utilities/enums.hh"
50
52
53namespace Kaskade
54{
65 template <class Eq>
66 class Limex
67 {
68 public:
70 typedef typename EvolutionEquation::AnsatzVars::VariableSet State;
71
72 private:
75
76 public:
83 typename EvolutionEquation::AnsatzVars const& ansatzVars_,
85 int verbosity_ = 1)
86 : gridManager(gridManager_), ansatzVars(ansatzVars_), eq(&eq_,0)
87 , assembler(ansatzVars.spaces), extrap(0),
89 initSolutionTime(0.0), estimateTime(0), directType(st), precondType(precondType_), verbosity(verbosity_)
90 {}
91
92
109 State const& step(State const& x, double dt, int order,
110 std::vector<std::pair<double,double>> const& tolX)
111 {
112 boost::timer::cpu_timer timer;
113
114 std::vector<double> stepFractions(order+1);
115 for (int i=0; i<=order; ++i)
116 stepFractions[i] = 1.0/(i+1); // harmonic sequence for extrapolation
117 extrap.clear();
118
119 int const nvars = EvolutionEquation::AnsatzVars::noOfVariables;
120 int const neq = EvolutionEquation::TestVars::noOfVariables;
121 size_t nnz = assembler.nnz(0,neq,0,nvars,false);
122
123 MatrixAsTriplet<double> triplet(nnz);
124
125 typedef typename EvolutionEquation::AnsatzVars::template CoefficientVector<0,neq> CoefficientVectors;
126
128 State dx(x), dxsum(x), tmp(x);
129 double const t = eq.parabolicEquation().time();
130
131 eq.parabolicEquation().temporalEvaluationRange(t,t+dt);
132
133 bool const iterative = false; // false: use a direct solver, true: use an iterative solver
134
135
136 // Loop over extrapolation orders. For each order, an Euler solution is computed
137 // on an equidistant time grid with shorter and shorter time sub-steps. At the
138 // joint final time, the value is extrapolated.
139 for (int i=0; i<=order; ++i)
140 {
141 double const tau = stepFractions[i]*dt;
142 eq.setTau(tau);
143 eq.parabolicEquation().setTime(t);
144
145 // mesh adaptation loop. Just once if i>0.
146 gridManager.setVerbosity(verbosity);
147 bool accurate = true;
148 do {
149 int dimension = gridManager.grid().dimension;
150
151 // Evaluate and factorize matrix B(t)-tau*J
152 dx *= 0;
153 timer.start();
154 assembler.assemble(Linearization(eq,x,x,dx));
155 matrixAssemblyTime += (double)(timer.elapsed().user)/1e9;
156 Op A(assembler);
157
158 timer.start();
159 CoefficientVectors rhs(assembler.rhs());
160 CoefficientVectors solution = ansatzVars.zeroCoefficientVector();
161
162 if (iterative)
163 {
164 typedef typename EvolutionEquation::AnsatzVars::template CoefficientVector<0,neq> LinearSpaceX;
165
166 if ( verbosity>0 )
167 {
168 std::cout << " eq 0: dof = " << boost::fusion::at_c<0>(dx.data).space().degreesOfFreedom()
169 << " pts in mesh = " << gridManager.grid().size(dimension) << '\n';
170 if ( verbosity>1 )
171 std::cout << " dimension of grid = " << dimension << '\n';
172 };
173
174 initSolutionTime += (double)(timer.elapsed().user)/1e9;
175 elapsedTimeSinceReset = (double)(timer.elapsed().user)/1e9;
176
177 // in der Folge finden sich vesrschiedene iterative L�ser
178 // und Vorkonditionierer zur Auswahl per Ein-/Auskommentieren.
179 // Der Mechanismus muss noch umgestaltet werden mittels
180 // passender Parameter aus dem Main()-Programm via itegrate.hh !!!!
181
182 int iteSteps = 5000;
183 double iteEps = 1.0e-10;
184 Dune::InverseOperatorResult res;
185
186 switch (precondType)
187 {
189 {
191 precAssemblyTime += (double)(timer.elapsed().user)/1e9-elapsedTimeSinceReset;
192 Dune::BiCGSTABSolver<LinearSpaceX> cg(A,trivial,iteEps,iteSteps,verbosity-1);
193 cg.apply(solution,rhs,res);
194 }
195 break;
196 // case PrecondType::BOOMERAMG:
197 // {
198 // int steps = iteSteps;
199 // int coarsentype = 21;
200 // int interpoltype = 0;
201 // int cycleType = 1;
202 // int relaxType = 3;
203 // int variant = 0;
204 // int overlap = 1;
205 // int syseqn = 1;
206 // double tol = iteEps;
207 // double strongThreshold = (dimension==2)?0.25:0.6;
208 // BoomerAMG<Op> BoomerAMGPrecon(A,steps,coarsentype,interpoltype,tol,cycleType,relaxType,
209 // strongThreshold,variant,overlap,syseqn,verbosity);
210 // precAssemblyTime += (double)(timer.elapsed().user)/1e9-elapsedTimeSinceReset;
211 // Dune::BiCGSTABSolver<LinearSpaceX> cg(A,BoomerAMGPrecon,iteEps,iteSteps,verbosity-1); // 1e-10
212 // cg.apply(solution,rhs,res);
213 // }
214 // break;
215 // case PrecondType::ILUK:
216 // {
217 // int fill_lev = 1;
218 // ILUKPreconditioner<Op> iluk(A,fill_lev,verbosity);
219 // precAssemblyTime += (double)(timer.elapsed().user)/1e9-elapsedTimeSinceReset;
220 // //Dune::CGSolver<LinearSpace> cg(A,iluk,iteEps,iteSteps,verbosity-1);
221 // Dune::BiCGSTABSolver<LinearSpaceX> cg(A,iluk,iteEps,iteSteps,verbosity-1);
222 // cg.apply(solution,rhs,res);
223 // }
224 // break;
226 default:
227 {
228 JacobiPreconditioner<Op> jacobi(A,1.0);
229 precAssemblyTime += (double)(timer.elapsed().user)/1e9-elapsedTimeSinceReset;
230 Dune::BiCGSTABSolver<LinearSpaceX> cg(A,jacobi,iteEps,iteSteps,verbosity-1);
231 // alternative: Dune::LoopSolver<LinearSpaceX> cg(A,jacobi,iteEps,iteSteps,verbosity-1);
232 cg.apply(solution,rhs,res);
233 }
234 break;
235 }
236
237 if ( !(res.converged) || (res.iterations == 2001) ) {
238 std::cout << " no of iterations in cg = " << res.iterations << '\n';
239 std::cout << " convergence status of cg = " << res.converged << '\n';
240 //std::cout << "A: " << std::endl;
241 //for (size_t ii=0; ii<nnz; ii++)
242 // std::cout << A.getmat().ridx[ii] << ", "
243 // << A.getmat().cidx[ii] << ", "
244 // << A.getmat().data[ii] << std::endl;
245 //std::cout << "A, end: " << std::endl;
246 assert(0);
247 }
248 }
249 else
250 {
251 directInverseOperator(A,directType).applyscaleadd(1.0,rhs,solution);
252 factorizationTime += (double)(timer.elapsed().user)/1e9;
253 }
254 dx.data = solution.data;
255 solutionTime += (double)(timer.elapsed().user)/1e9;
256
257
258 timer.start();
259 // Mesh adaptation only if requested and only for the full
260 // implicit Euler step (first stage).
261 if (!tolX.empty() && i==0)
262 {
263 // Compute spatial error estimate
264 tmp = dx;
266 tmp -= dx;
267
268 // perform mesh adaptation
269 accurate = embeddedErrorEstimator(ansatzVars,tmp,x,eq.parabolicEquation().scaling(),tolX,gridManager,verbosity);
270 if (!accurate)
271 nnz = assembler.nnz(0,neq,0,nvars,false);
272 estimateTime += (double)(timer.elapsed().user)/1e9;
273 }
274 } while (!accurate);
275
276 dxsum = dx;
277
278 // propagate by linearly implicit Euler
279 if (order==0)
280 {
281 // insert into extrapolation tableau
282 extrap.push_back(dxsum,stepFractions[i]);
283
284 // restore initial time
285 eq.parabolicEquation().setTime(t);
286 if (verbosity>1)
287 std::cout << " end of limex step" << '\n';
288 return extrap.back();
289 }
290 else
291 for (int j=1; j<=i; ++j)
292 {
293 // Assemble new right hand side tau*f(x_j)
294 eq.parabolicEquation().setTime(eq.parabolicEquation().time()+tau);
295 tmp = x; tmp += dxsum;
296 State zero(x); zero *= 0;
297 timer.start();
298 assembler.assemble(Linearization(eq,tmp,x,zero),Assembler::RHS|Assembler::MATRIX);
299 rhsAssemblyTime += (double)(timer.elapsed().user)/1e9;
300
301 Op A(assembler);
302
303 timer.start();
304 CoefficientVectors rhs(assembler.rhs());
305 CoefficientVectors solution = EvolutionEquation::AnsatzVars::template zeroCoefficientVector(ansatzVars.spaces);
306
307
308 if (iterative)
309 {
310 typedef typename EvolutionEquation::AnsatzVars::template CoefficientVector<> LinearSpaceX;
311
312 initSolutionTime += (double)(timer.elapsed().user)/1e9;
313 elapsedTimeSinceReset = (double)(timer.elapsed().user)/1e9;
314
315
316 int iteSteps = 5000;
317 double iteEps = 1.0e-10;
318 Dune::InverseOperatorResult res;
319
320 switch (precondType)
321 {
323 {
325 precAssemblyTime += (double)(timer.elapsed().user)/1e9-elapsedTimeSinceReset;
326 Dune::BiCGSTABSolver<LinearSpaceX> cg(A,trivial,iteEps,iteSteps,verbosity-1);
327 cg.apply(solution,rhs,res);
328 }
329 break;
330 // case PrecondType::BOOMERAMG:
331 // {
332 // int steps = iteSteps;
333 // int coarsentype = 21;
334 // int interpoltype = 0;
335 // int cycleType = 1;
336 // int relaxType = 3;
337 // int variant = 0;
338 // int overlap = 1;
339 // int syseqn = 1;
340 // double tol = iteEps;
341 // int dimension = gridManager.grid().dimension;
342 // double strongThreshold = (dimension==2)?0.25:0.6;
343 // BoomerAMG<Op> BoomerAMGPrecon(A,steps,coarsentype,interpoltype,tol,cycleType,relaxType,
344 // strongThreshold,variant,overlap,syseqn,verbosity);
345 // precAssemblyTime += (double)(timer.elapsed().user)/1e9-elapsedTimeSinceReset;
346 // Dune::BiCGSTABSolver<LinearSpaceX> cg(A,BoomerAMGPrecon,iteEps,iteSteps,verbosity-1); // 1e-10
347 // cg.apply(solution,rhs,res);
348 // }
349 // break;
350 // case PrecondType::ILUK:
351 // {
352 // int fill_lev = 1;
353 // ILUKPreconditioner<Op> iluk(A,fill_lev,verbosity);
354 // precAssemblyTime += (double)(timer.elapsed().user)/1e9-elapsedTimeSinceReset;
355 // //Dune::CGSolver<LinearSpace> cg(A,iluk,iteEps,iteSteps,verbosity-1);
356 // Dune::BiCGSTABSolver<LinearSpaceX> cg(A,iluk,iteEps,iteSteps,verbosity-1);
357 // cg.apply(solution,rhs,res);
358 // }
359 // break;
361 default:
362 {
363 JacobiPreconditioner<Op> jacobi(A,1.0);
364 precAssemblyTime += (double)(timer.elapsed().user)/1e9-elapsedTimeSinceReset;
365 Dune::BiCGSTABSolver<LinearSpaceX> cg(A,jacobi,iteEps,iteSteps,verbosity-1);
366 cg.apply(solution,rhs,res);
367 }
368 break;
369 }
370
371 if ( !(res.converged) || (res.iterations == 2001) )
372 {
373 typedef typename Op::field_type field_type;
374 MatrixAsTriplet<field_type> triplet = A.template get<MatrixAsTriplet<field_type> >();
375
376 std::cout << " no of iterations in cg = " << res.iterations << '\n';
377 std::cout << " convergence status of cg = " << res.converged << '\n';
378 std::cout << "A: " << '\n';
379 for (size_t ii=0; ii<nnz; ii++)
380 std::cout << triplet.ridx[ii] << ", " << triplet.cidx[ii] << ", " << triplet.data[ii] << '\n';
381 std::cout << "A, end: " << '\n';
382 assert(0);
383 }
384 }
385 else
386 {
387 directInverseOperator(A,directType).applyscaleadd(1.0,rhs,solution);
388 }
389 dx.data = solution.data;
390
391 dxsum += dx;
392 solutionTime += (double)(timer.elapsed().user)/1e9;
393 }
394 // insert into extrapolation tableau
395 extrap.push_back(dxsum,stepFractions[i]);
396
397 // restore initial time
398 eq.parabolicEquation().setTime(t);
399 }
400
401 return extrap.back();
402 }
403
410 std::vector<std::pair<double,double> > estimateError(State const& x,int i, int j) const
411 {
412 assert(extrap.size()>1);
413
414 std::vector<std::pair<double,double> > e(ansatzVars.noOfVariables);
415
416 relativeError(typename EvolutionEquation::AnsatzVars::Variables(),extrap[i].data,
417 extrap[j].data,x.data,
418 ansatzVars.spaces,eq.parabolicEquation().scaling(),e.begin());
419
420 return e;
421 }
422
423 template <class OutStream>
424 void reportTime(OutStream& out) const {
425 out << "Limex time: " << matrixAssemblyTime << "s matrix assembly\n"
426 << " " << rhsAssemblyTime << "s rhs assembly\n"
427 << " " << precAssemblyTime << "s preconditioner assembly\n"
428 << " " << factorizationTime << "s factorization\n"
429 << " " << initSolutionTime << "s init solution\n"
430 << " " << solutionTime << "s solution\n"
431 << " " << estimateTime << "s estimate\n"
432 << std::flush;
433 }
434
435 void advanceTime(double dt)
436 {
437 eq.parabolicEquation().setTime(eq.time()+dt);
438 }
439
440
441 private:
443 typename EvolutionEquation::AnsatzVars const& ansatzVars;
445 Assembler assembler;
446
447 public:
454 };
455} // namespace Kaskade
456#endif
An adaptor presenting a Dune::LinearOperator <domain_type,range_type> interface to a contiguous sub-b...
Polynomial extrapolation following Aitken-Neville.
Class that takes care of the transfer of data (of each FunctionSpaceElement) after modification of th...
EvolutionEquation::AnsatzVars::VariableSet State
Definition limex.hh:36
double rhsAssemblyTime
Definition limex.hh:172
double matrixAssemblyTime
Definition limex.hh:172
void advanceTime(double dt)
State const & step(State const &x, double dt, int order, std::vector< std::pair< double, double > > const &tolX)
Computes a state increment that advances the given state in time. The time in the given evolution equ...
PrecondType precondType
double solutionTime
Definition limex.hh:172
std::vector< std::pair< double, double > > estimateError(State const &x, int i, int j) const
ExtrapolationTableau< State > extrap
Definition limex.hh:171
Limex(GridManager< typename EvolutionEquation::AnsatzVars::Grid > &gridManager_, EvolutionEquation &eq_, typename EvolutionEquation::AnsatzVars const &ansatzVars_, DirectType st=DirectType::MUMPS, PrecondType precondType_=PrecondType::ILUK, int verbosity_=1)
void reportTime(OutStream &out) const
Eq EvolutionEquation
Definition limex.hh:35
double factorizationTime
Definition limex.hh:172
std::vector< SparseIndexInt > ridx
row indices
Definition triplet.hh:669
std::vector< SparseIndexInt > cidx
column indices
Definition triplet.hh:671
std::vector< Scalar > data
data
Definition triplet.hh:673
Linearly implicit Euler method.
Definition semieuler.hh:235
Proxy class for the linearization of semi-linear time stepping schemes.
The trivial preconditioner: this is simply the identity that does exactly nothing.
A class for assembling Galerkin representation matrices and right hand sides for variational function...
PrecondType
Definition enums.hh:18
Utility classes for the definition and use of variational functionals.
void projectHierarchically(VariableSet< VariableSetDescription > &f)
Projects the given FE function onto the the polynomial ansatz subspace of one order lower....
bool embeddedErrorEstimator(VariableSetDescription const &varDesc, typename VariableSetDescription::VariableSet const &err, typename VariableSetDescription::VariableSet const &sol, Scaling const &scaling, std::vector< std::pair< double, double > > const &tol, GridManager< typename VariableSetDescription::Grid > &gridManager, int verbosity=1)
Embedded error estimation and mesh refinement.
DirectType
Available direct solvers for linear equation systems.
Definition enums.hh:35
@ MUMPS
MUMPS sparse solver.
Definition enums.hh:52
@ UMFPACK
UMFPack from SuiteSparse, using 32 bit integer indices.
Definition enums.hh:45
Hierarchic Finite Elements.
Lagrange Finite Elements.
DirectType xyz
InverseLinearOperator< DirectSolver< typename AssembledGalerkinOperator< GOP, firstRow, lastRow, firstCol, lastCol >::Domain, typename AssembledGalerkinOperator< GOP, firstRow, lastRow, firstCol, lastCol >::Range > > directInverseOperator(AssembledGalerkinOperator< GOP, firstRow, lastRow, firstCol, lastCol > const &A, DirectType directType, MatrixProperties properties)
Definition direct.hh:618
void relativeError(Variables const &varDesc, Functions const &f1, Functions const &f2, Functions const &f3, Spaces const &spaces, Scaling const &scaling, OutIter out)
int const LagrangeCubeShapeFunction< ctype, dimension, Scalar, O >::order