KASKADE 7 development version
Loading...
Searching...
No Matches
hyprecond.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 BOOMERAMG_HH
14#define BOOMERAMG_HH
15
16#include <math.h>
17
18extern "C" {
19#include "protos.h"
20#ifndef MAX_HBNAME
21#include "ios.h"
22#endif
23}
24
25#include "HYPRE_sstruct_ls.h"
26// delete preprocessor macros min and max which were defined in itsol-1/hypre-2.6.0b headers,
27// since these macros may interfere with subsequently included Kaskade7 headers or program code
28#undef min
29#undef max
30#undef HB
31
32#include <vector>
33
34#include "dune/istl/preconditioners.hh"
35
36#include "linalg/triplet.hh"
38
39namespace Kaskade
40{
41 //---------------------------------------------------------------------
42 //---------------------------------------------------------------------
43
49 template <class Op>
50 class BoomerAMG: public Dune::Preconditioner<typename Op::range_type, typename Op::range_type>
51 {
52 typedef typename Op::range_type range_type;
53 typedef range_type domain_type;
54 typedef typename Op::field_type field_type;
55
56 public:
57 BoomerAMG(Op& op, int steps_=1, int coarsentype=21, int interpoltype=0, double tol=1.0e-8,
58 int cycleType=1, int relaxType=2, double strongThreshold=0.3,
59 int variant=0, int overlap=1, int syseqn=1, int verbosity_=2)
60 : op(op), steps(steps_), verbosity(verbosity_), eqnIndex(0)
61 {
62 static char main[] = "main"; // ISO C++ forbids writable string literals - need to have a variable.
63
64 MatrixAsTriplet<field_type> A = op.template get<MatrixAsTriplet<field_type> >();
65 n = A.nrows();
66 int nnz = A.nnz(); /* ierr; */
67 std::vector<int> Ap, Ai;
68 std::vector<double> Az;
69 // FILE *flog = stdout;
70
71 if ( verbosity>=2 )
72 {
73 std::cout << "HYPRE_BoomerAMG: n=" << n << ", nnz=" << nnz <<
74 " steps=" << steps <<
75 ", coarsentype=" << coarsentype << ", interpolationtype=" <<
76 interpoltype << "\n";
77 };
78 Ap.resize(n+1); Ai.resize(nnz); Az.resize(nnz);
80 Ap,Ai,Az);
81 for (int i=0;i<n;i++) Ap[i]=Ap[i+1]-Ap[i];
82 ixx = (int *)Malloc(n*sizeof(int), main);
83 for (int i=0;i<n;i++) ixx[i]=i;
84
85 HYPRE_IJMatrixCreate(0, 0, n-1, 0, n-1, &ij_matrix);
86 HYPRE_IJMatrixSetPrintLevel(ij_matrix,10); /* Debug */
87 HYPRE_IJMatrixSetObjectType(ij_matrix, HYPRE_PARCSR);
88 HYPRE_IJMatrixInitialize(ij_matrix);
89 /* set matrix coefficients */
90 HYPRE_IJMatrixSetValues(ij_matrix,n,&Ap[0],ixx,&Ai[0],&Az[0]);
91 HYPRE_IJMatrixAssemble(ij_matrix);
92 HYPRE_IJMatrixGetObject(ij_matrix, (void **) &parcsr_matrix);
93
94 HYPRE_BoomerAMGCreate(&preconditioner);
95 HYPRE_BoomerAMGSetPrintLevel(preconditioner,1); /* Debug */
96 HYPRE_BoomerAMGSetCoarsenType(preconditioner,coarsentype);
97 HYPRE_BoomerAMGSetInterpType(preconditioner,interpoltype);
98 HYPRE_BoomerAMGSetMaxIter(preconditioner,steps);
99 HYPRE_BoomerAMGSetTol(preconditioner,tol);
100 HYPRE_BoomerAMGSetStrongThreshold(preconditioner, strongThreshold); //RR 0.6 3D
101 HYPRE_BoomerAMGSetCycleType(preconditioner, cycleType); //RR 2 W-Zyklus
102 HYPRE_BoomerAMGSetRelaxType(preconditioner, relaxType); //RR 3 PrecondType::SSOR smoother
103 HYPRE_BoomerAMGSetVariant (preconditioner, variant);
104 HYPRE_BoomerAMGSetOverlap (preconditioner, overlap) ;
105 if (syseqn>1)
106 {
107 HYPRE_BoomerAMGSetNodal(preconditioner, 1);
108 HYPRE_BoomerAMGSetNumFunctions(preconditioner, syseqn);
109 int i, k, lng = n/syseqn;
110 eqnIndex = (int *)Malloc(n*sizeof(int), main);
111 for (i=0; i<syseqn; i++)
112 for (k=0; k<lng; k++)
113 eqnIndex[i*lng+k] = i;
114
115 HYPRE_BoomerAMGSetDofFunc(preconditioner, eqnIndex);
116 }
117
118 HYPRE_BoomerAMGSetup(preconditioner,parcsr_matrix,0,0);
119
120 xxraw = (double *)Malloc(n*sizeof(double), main);
121 yyraw = (double *)Malloc(n*sizeof(double), main);
122
123 HYPRE_IJVectorCreate(0, 0, n-1, &xxij);
124 HYPRE_IJVectorSetPrintLevel(xxij,10); /* Debug */
125 HYPRE_IJVectorSetObjectType(xxij, HYPRE_PARCSR);
126 HYPRE_IJVectorInitialize(xxij);
127 HYPRE_IJVectorAssemble(xxij);
128 HYPRE_IJVectorGetObject(xxij, (void **) &xx);
129
130 HYPRE_IJVectorCreate(0, 0, n-1, &yyij);
131 HYPRE_IJVectorSetPrintLevel(yyij,10); /* Debug */
132 HYPRE_IJVectorSetObjectType(yyij, HYPRE_PARCSR);
133 HYPRE_IJVectorInitialize(yyij);
134 HYPRE_IJVectorAssemble(yyij);
135 HYPRE_IJVectorGetObject(yyij, (void **) &yy);
136 }
138 {
139 HYPRE_IJMatrixDestroy (ij_matrix);
140 HYPRE_BoomerAMGDestroy(preconditioner);
141 HYPRE_IJVectorDestroy (xxij);
142 HYPRE_IJVectorDestroy (yyij);
143 free(xxraw);
144 free(yyraw);
145 free(ixx);
146 if (eqnIndex) free(eqnIndex);
147 }
148
149 virtual void pre (domain_type&, range_type&) {}
150 virtual void post (domain_type&) {}
151
152 virtual void apply (domain_type& x, range_type const& y) {
153 /* set vector values */
154 y.write(yyraw);
155 for (int i=0; i<n; i++)
156 xxraw[i] = 0.0;
157
158 HYPRE_IJVectorSetValues(yyij, n, ixx, yyraw);
159 HYPRE_IJVectorSetValues(xxij, n, ixx, xxraw);
160 HYPRE_BoomerAMGSolve(preconditioner,parcsr_matrix,yy,xx);
161 if (steps>1)
162 {
163 int num_iterations;
164 double rel_resid_norm;
165 HYPRE_BoomerAMGGetNumIterations (preconditioner,&num_iterations);
166 HYPRE_BoomerAMGGetFinalRelativeResidualNorm (preconditioner, &rel_resid_norm);
167 if ( verbosity>=2 )
168 {
169 std::cout << "HYPRE_BoomerAMG1: " << num_iterations << " steps, rel_resid_norm="
170 << rel_resid_norm << '\n';
171 }
172 }
173 HYPRE_IJVectorGetValues(xxij,n,ixx,xxraw);
174 x.read(xxraw);
175 }
176
182 virtual Dune::SolverCategory::Category category() const override
183 {
184 return Dune::SolverCategory::sequential;
185 }
186
187 private:
188 Op& op;
189 HYPRE_Solver preconditioner;
190 HYPRE_IJMatrix ij_matrix;
191 HYPRE_ParCSRMatrix parcsr_matrix;
192 HYPRE_IJVector xxij, yyij;
193 HYPRE_ParVector xx, yy;
194 int n, steps, verbosity;
195 double *xxraw, *yyraw;
196 int *ixx, *eqnIndex;
197 };
198
199 //------------------------------------------------------------------------------------------------
200
206 template <class Op>
207 class Euclid: public Dune::Preconditioner<typename Op::range_type, typename Op::range_type>
208 {
209 typedef typename Op::range_type range_type;
210 typedef range_type domain_type;
211 typedef typename Op::field_type field_type;
212
213 public:
214
215 Euclid(Op& op, int level=1, double droptol=0.0, int printlevel=0, int bj=0,
216 int verbosity=2)
217 : op(op)
218 {
219 static char main[] = "main"; // ISO C++ forbids writable string literals - need to have a variable.
220
221 MatrixAsTriplet<field_type> A = op.template get<MatrixAsTriplet<field_type> >();
222 n = A.nrows();
223 int nnz = A.nnz(); /* ierr; */
224 std::vector<int> Ap, Ai;
225 std::vector<double> Az;
226 // FILE *flog = stdout;
227
228 if ( verbosity>=2 )
229 {
230 std::cout << "PrecondType::EUCLID: n=" << n << ", nnz=" << nnz << ", level=" <<
231 level << ", droptol=" << droptol << ", printlevel=" << printlevel <<
232 ", bj=" << bj << "\n";
233 };
234 Ap.resize(n+1); Ai.resize(nnz); Az.resize(nnz);
236 Ap,Ai,Az);
237 for (int i=0;i<n;i++) Ap[i]=Ap[i+1]-Ap[i];
238 ixx = (int *)Malloc(n*sizeof(int), main);
239 for (int i=0;i<n;i++) ixx[i]=i;
240
241 HYPRE_IJMatrixCreate(0, 0, n-1, 0, n-1, &ij_matrix);
242 HYPRE_IJMatrixSetPrintLevel(ij_matrix,10); /* Debug */
243 HYPRE_IJMatrixSetObjectType(ij_matrix, HYPRE_PARCSR);
244 HYPRE_IJMatrixInitialize(ij_matrix);
245 /* set matrix coefficients */
246 HYPRE_IJMatrixSetValues(ij_matrix,n,&Ap[0],ixx,&Ai[0],&Az[0]);
247 HYPRE_IJMatrixAssemble(ij_matrix);
248 HYPRE_IJMatrixGetObject(ij_matrix, (void **) &parcsr_matrix);
249
250 HYPRE_EuclidCreate(0,&preconditioner);
251 HYPRE_EuclidSetStats(preconditioner,printlevel);
252 HYPRE_EuclidSetMem(preconditioner,printlevel);
253 HYPRE_EuclidSetLevel(preconditioner,level);
254 // HYPRE_EuclidSetSparseA(preconditioner,droptol);
255 HYPRE_EuclidSetRowScale(preconditioner,0);
256 if (bj)
257 HYPRE_EuclidSetBJ(preconditioner,bj);
258 if (droptol!=0.0)
259 HYPRE_EuclidSetILUT(preconditioner,droptol);
260 HYPRE_EuclidSetup(preconditioner,parcsr_matrix,0,0);
261
262 xxraw = (double *)Malloc(n*sizeof(double), main);
263 yyraw = (double *)Malloc(n*sizeof(double), main);
264
265 HYPRE_IJVectorCreate(0, 0, n-1, &xxij);
266 HYPRE_IJVectorSetPrintLevel(xxij,10); /* Debug */
267 HYPRE_IJVectorSetObjectType(xxij, HYPRE_PARCSR);
268 HYPRE_IJVectorInitialize(xxij);
269 HYPRE_IJVectorAssemble(xxij);
270 HYPRE_IJVectorGetObject(xxij, (void **) &xx);
271
272 HYPRE_IJVectorCreate(0, 0, n-1, &yyij);
273 HYPRE_IJVectorSetPrintLevel(yyij,10); /* Debug */
274 HYPRE_IJVectorSetObjectType(yyij, HYPRE_PARCSR);
275 HYPRE_IJVectorInitialize(yyij);
276 }
277
279 { free(xxraw);
280 free(yyraw);
281 free(ixx);
282 HYPRE_EuclidDestroy(preconditioner);
283 HYPRE_IJMatrixDestroy(ij_matrix);
284 HYPRE_IJVectorDestroy(xxij);
285 HYPRE_IJVectorDestroy(yyij);
286 }
287
288 virtual void pre (domain_type&, range_type&) {}
289 virtual void post (domain_type&) {}
290
291 virtual void apply (domain_type& x, range_type const& y) {
292 /* set vector values */
293 y.write(yyraw);
294 HYPRE_IJVectorSetValues(yyij, n, ixx, yyraw);
295 HYPRE_IJVectorAssemble(yyij);
296 HYPRE_IJVectorGetObject(yyij, (void **) &yy);
297
298 HYPRE_EuclidSolve(preconditioner,parcsr_matrix,yy,xx);
299
300 HYPRE_IJVectorGetValues(xxij,n,ixx,xxraw);
301 x.read(xxraw);
302 }
303
309 virtual Dune::SolverCategory::Category category() const override
310 {
311 return Dune::SolverCategory::sequential;
312 }
313
314 private:
315 Op& op;
316 HYPRE_Solver preconditioner;
317 HYPRE_IJMatrix ij_matrix;
318 HYPRE_ParCSRMatrix parcsr_matrix;
319 HYPRE_IJVector xxij, yyij;
320 HYPRE_ParVector xx, yy;
321 int n;
322 double *xxraw, *yyraw;
323 int *ixx;
324 };
325
326} // namespace Kaskade
327#endif
virtual void apply(domain_type &x, range_type const &y)
Definition hyprecond.hh:152
virtual void post(domain_type &)
Definition hyprecond.hh:150
virtual void pre(domain_type &, range_type &)
Definition hyprecond.hh:149
virtual Dune::SolverCategory::Category category() const override
returns the category of the operator
Definition hyprecond.hh:182
BoomerAMG(Op &op, int steps_=1, int coarsentype=21, int interpoltype=0, double tol=1.0e-8, int cycleType=1, int relaxType=2, double strongThreshold=0.3, int variant=0, int overlap=1, int syseqn=1, int verbosity_=2)
Definition hyprecond.hh:57
virtual void pre(domain_type &, range_type &)
Definition hyprecond.hh:288
virtual void post(domain_type &)
Definition hyprecond.hh:289
Euclid(Op &op, int level=1, double droptol=0.0, int printlevel=0, int bj=0, int verbosity=2)
Definition hyprecond.hh:215
virtual void apply(domain_type &x, range_type const &y)
Definition hyprecond.hh:291
virtual Dune::SolverCategory::Category category() const override
returns the category of the operator
Definition hyprecond.hh:309
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 umfpack_triplet_to_col(int rows, int cols, std::vector< long > const &ridx, std::vector< long > const &cidx, std::vector< double > const &values, std::vector< long > &Ap, std::vector< long > &Ai, std::vector< double > &Az)
Triplet to compressed column storage conversion . This is just a frontent to DirectType::UMFPACK util...