KASKADE 7 development version
Loading...
Searching...
No Matches
iluprecond.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-2020 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
13#ifndef ILUPRECOND_HH
14#define ILUPRECOND_HH
15
16extern "C"
17{
18#include "protos.h"
19#include "ios.h"
20}
21#undef max
22#undef HB
23
24extern "C" void set_arms_pars(io_t* io, int Dscale, int *ipar, double *tolcoef, int *lfil);
25
26#include <boost/timer/timer.hpp>
27#include "dune/istl/preconditioners.hh"
28#include "linalg/triplet.hh"
29
30//--------------------------------------------------------------------------------------------------
31//--------------------------------------------------------------------------------------------------
32
33namespace Kaskade
34{
40 template <class Op>
41 class ILUTPreconditioner: public Dune::Preconditioner<typename Op::Range, typename Op::Range>
42 {
43 typedef typename Op::Range Range;
44 typedef Range Domain;
45 typedef typename Op::field_type field_type;
46
47 public:
48 ILUTPreconditioner(Op& op, int lfil=240, double tol=1.0e-2, int verbosity=2)
49 {
50 static char main[] = "main";
51
52 boost::timer::cpu_timer iluTimer;
53 std::cout << "ilut constructor: ";
54 MatrixAsTriplet<field_type> A = op.template get<MatrixAsTriplet<field_type> >();
55 int n = A.nrows(), nnz = A.nnz(), ierr;
56 FILE *flog = stdout;
57
58 csmat = (csptr)Malloc( sizeof(SparMat), main );
59 ierr = COOcs(n, nnz, &A.data[0], &A.cidx[0], &A.ridx[0], csmat);
60 if( ierr != 0 )
61 {
62 printf(" *** ILU error - COOcs code %d csmat=%p\n", ierr, csmat);
63 throw ierr;
64 }
65
66 lu = (iluptr)Malloc(sizeof(ILUSpar), main);
67 ierr = ilut(csmat, lu, lfil, tol, flog);
68 if( ierr != 0 )
69 {
70 printf(" *** PrecondType::ILUT error - ilut code %d lu=%p\n", ierr, lu);
71 throw ierr;
72 }
73
74 xx = (double *)Malloc(n*sizeof(double), main);
75 yy = (double *)Malloc(n*sizeof(double), main);
76
77 if ( verbosity>=2 )
78 std::cout << "PrecondType::ILUT: n=" << n << ", nnz=" << nnz << " dropTol=" << tol << ", fillfac="
79 << 1.0*nnz_ilu(lu)/(nnz+1.0) << ", lfil=" << lfil << ", time="
80 << (double)(iluTimer.elapsed().user)/1e9 << "s\n";
81 }
82
84 {
85 cleanILU(lu);
86 cleanCS(csmat);
87 free(xx);
88 free(yy);
89 }
90
91 virtual void pre (Domain&, Range&) {}
92 virtual void post (Domain&) {}
93
94 virtual void apply (Domain& x, Range const& y)
95 {
96 y.write(yy);
97 lusolC(yy, xx, lu);
98 x.read(xx);
99 }
100
106 virtual Dune::SolverCategory::Category category() const override
107 {
108 return Dune::SolverCategory::sequential;
109 }
110
111
112 private:
113 csptr csmat;
114 iluptr lu;
115 double *xx, *yy;
116 };
117
118 //------------------------------------------------------------------------------------------------
119
125 template <class Op>
126 class ILUKPreconditioner: public Dune::Preconditioner<typename Op::Range, typename Op::Range>
127 {
128 typedef typename Op::Range Range;
129 typedef Range Domain;
130 typedef typename Op::field_type field_type;
131
132 public:
133
134 ILUKPreconditioner(Op& op, int fill_lev=3, int verbosity=2)
135 {
136 static char main[] = "main";
137
138 boost::timer::cpu_timer iluTimer;
139 MatrixAsTriplet<field_type> A = op.template get<MatrixAsTriplet<field_type> >();
140 int n = A.nrows(), nnz = A.nnz(), ierr;
141 FILE *flog = stdout;
142
143 csmat = (csptr)Malloc( sizeof(SparMat), main );
144 ierr = COOcs(n, nnz, &A.data[0], &A.cidx[0], &A.ridx[0], csmat);
145 if( ierr != 0 )
146 {
147 printf(" *** ILU error - COOcs code %d csmat=%p\n", ierr, csmat);
148 throw ierr;
149 }
150
151 lu = (iluptr)Malloc(sizeof(ILUSpar), main);
152 ierr = ilukC(fill_lev, csmat, lu, flog);
153 if( ierr != 0 )
154 {
155 printf(" *** PrecondType::ILUK error - ilut code %d lu=%p\n", ierr, lu);
156 throw ierr;
157 }
158
159 xx = (double *)Malloc(n*sizeof(double), main);
160 yy = (double *)Malloc(n*sizeof(double), main);
161
162 if ( verbosity>=2 )
163 {
164 std::cout << "PrecondType::ILUK: n=" << n << ", nnz=" << nnz << ", fillfac="
165 << 1.0*nnz_ilu(lu)/(nnz+1.0) << ", fill_lev="
166 << fill_lev << ", time=" << (double)(iluTimer.elapsed().user)/1e9 << "s\n";
167 }
168 }
169
171 {
172 cleanILU(lu);
173 cleanCS(csmat);
174 free(xx);
175 free(yy);
176 }
177
178 virtual void pre (Domain&, Range&) {}
179 virtual void post (Domain&) {}
180
181 virtual void apply (Domain& x, Range const& y)
182 {
183 y.write(yy);
184 lusolC(yy, xx, lu);
185 x.read(xx);
186 }
187
193 virtual Dune::SolverCategory::Category category() const override
194 {
195 return Dune::SolverCategory::sequential;
196 }
197
198 private:
199 csptr csmat;
200 iluptr lu;
201 double *xx, *yy;
202 };
203 //---------------------------------------------------------------------
204
210 template <class Op>
211 class ARMSPreconditioner: public Dune::Preconditioner<typename Op::Range, typename Op::Range>
212 {
213 typedef typename Op::Range Range;
214 typedef Range Domain;
215 typedef typename Op::field_type field_type;
216
217 public:
218 ARMSPreconditioner(Op& op, int lfil=4, double tol=1.0e-2, int lev_reord=1, double tolind = 0.2, int verbosity=2)
219 {
220 static char main[] = "main";
221 static char main2[] = "main:ArmsSt";
222
223 boost::timer::cpu_timer iluTimer;
224 MatrixAsTriplet<field_type> A = op.template get<MatrixAsTriplet<field_type> >();
225 int n = A.nrows(), nnz = A.nnz(), ierr, diagscal = 1;
226
227 int lfil_arr[7];
228 double droptol[7], dropcoef[7];
229 int ipar[18];
230 io_t io;
231 FILE *flog = stdout;
232
233 csmat = (csptr)Malloc( sizeof(SparMat), main );
234 ierr = COOcs(n, nnz, &A.data[0], &A.cidx[0], &A.ridx[0], csmat);
235 if( ierr != 0 )
236 {
237 printf(" *** PrecondType::ARMS error - COOcs code %d csmat=%p\n", ierr, csmat);
238 throw ierr;
239 }
240
241 memset(&io,0,sizeof(io));
242 io.perm_type = 0;
243 io.Bsize = 400;
244 set_arms_pars(&io, diagscal, ipar, dropcoef, lfil_arr);
245 for (int j=0; j<7; j++)
246 {
247 lfil_arr[j] = lfil*((int) nnz/n);
248 droptol[j] = tol*dropcoef[j];
249 }
250 ipar[1] = lev_reord;
251 ArmsSt = (arms) Malloc(sizeof(armsMat),main2);
252 setup_arms(ArmsSt);
253
254 ierr = arms2(csmat, ipar, droptol, lfil_arr, tolind, ArmsSt, flog);
255 if (ierr != 0)
256 {
257 printf(" *** PrecondType::ARMS error - arms2 code %d ArmsSt=%p\n", ierr, ArmsSt);
258 throw ierr;
259 }
260
261 yy = (double *)Malloc(n*sizeof(double), main);
262
263 if (verbosity>=2)
264 std::cout << "PrecondType::ARMS: n=" << n << ", nnz=" << nnz << ", dropTol=" << tol
265 << ", lev_reord=" << lev_reord
266 << ", lfil=" << lfil << ", time=" << (double)(iluTimer.elapsed().user)/1e9 << "s\n";
267 }
268
270 {
271 cleanARMS(ArmsSt);
272 cleanCS(csmat);
273 free(yy);
274 }
275
276 virtual void pre (Domain&, Range&) {}
277 virtual void post (Domain&) {}
278
279 virtual void apply (Domain& x, Range const& y)
280 {
281 y.write(yy);
282 armsol2(yy, ArmsSt);
283 x.read(yy);
284 }
285
291 virtual Dune::SolverCategory::Category category() const override
292 {
293 return Dune::SolverCategory::sequential;
294 }
295
296 private:
297 csptr csmat;
298 arms ArmsSt;
299 double *yy;
300 };
301
302} // namespace Kaskade
303#endif
virtual void apply(Domain &x, Range const &y)
virtual Dune::SolverCategory::Category category() const override
returns the category of the operator
virtual void post(Domain &)
ARMSPreconditioner(Op &op, int lfil=4, double tol=1.0e-2, int lev_reord=1, double tolind=0.2, int verbosity=2)
virtual void pre(Domain &, Range &)
virtual void post(Domain &)
ILUKPreconditioner(Op &op, int fill_lev=3, int verbosity=2)
virtual void pre(Domain &, Range &)
virtual Dune::SolverCategory::Category category() const override
returns the category of the operator
virtual void apply(Domain &x, Range const &y)
virtual void apply(Domain &x, Range const &y)
Definition iluprecond.hh:94
virtual void pre(Domain &, Range &)
Definition iluprecond.hh:91
virtual void post(Domain &)
Definition iluprecond.hh:92
virtual Dune::SolverCategory::Category category() const override
returns the category of the operator
ILUTPreconditioner(Op &op, int lfil=240, double tol=1.0e-2, int verbosity=2)
Definition iluprecond.hh:48
size_t nnz() const
Definition triplet.hh:254
SparseIndexInt nrows() const
Returns number of rows (computes them, if not known)
Definition triplet.hh:202
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
void set_arms_pars(io_t *io, int Dscale, int *ipar, double *tolcoef, int *lfil)