26#include "HYPRE_sstruct_ls.h"
34#include "dune/istl/preconditioners.hh"
50 class BoomerAMG:
public Dune::Preconditioner<typename Op::range_type, typename Op::range_type>
52 typedef typename Op::range_type range_type;
53 typedef range_type domain_type;
54 typedef typename Op::field_type field_type;
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)
62 static char main[] =
"main";
67 std::vector<int> Ap, Ai;
68 std::vector<double> Az;
73 std::cout <<
"HYPRE_BoomerAMG: n=" << n <<
", nnz=" << nnz <<
75 ", coarsentype=" << coarsentype <<
", interpolationtype=" <<
78 Ap.resize(n+1); Ai.resize(nnz); Az.resize(nnz);
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;
85 HYPRE_IJMatrixCreate(0, 0, n-1, 0, n-1, &ij_matrix);
86 HYPRE_IJMatrixSetPrintLevel(ij_matrix,10);
87 HYPRE_IJMatrixSetObjectType(ij_matrix, HYPRE_PARCSR);
88 HYPRE_IJMatrixInitialize(ij_matrix);
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);
94 HYPRE_BoomerAMGCreate(&preconditioner);
95 HYPRE_BoomerAMGSetPrintLevel(preconditioner,1);
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);
101 HYPRE_BoomerAMGSetCycleType(preconditioner, cycleType);
102 HYPRE_BoomerAMGSetRelaxType(preconditioner, relaxType);
103 HYPRE_BoomerAMGSetVariant (preconditioner, variant);
104 HYPRE_BoomerAMGSetOverlap (preconditioner, overlap) ;
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;
115 HYPRE_BoomerAMGSetDofFunc(preconditioner, eqnIndex);
121 HYPRE_BoomerAMGSetup(preconditioner,parcsr_matrix,0,0);
123 xxraw = (
double *)Malloc(n*
sizeof(
double), main);
124 yyraw = (
double *)Malloc(n*
sizeof(
double), main);
126 HYPRE_IJVectorCreate(0, 0, n-1, &xxij);
127 HYPRE_IJVectorSetPrintLevel(xxij,10);
128 HYPRE_IJVectorSetObjectType(xxij, HYPRE_PARCSR);
129 HYPRE_IJVectorInitialize(xxij);
130 HYPRE_IJVectorAssemble(xxij);
131 HYPRE_IJVectorGetObject(xxij, (
void **) &xx);
133 HYPRE_IJVectorCreate(0, 0, n-1, &yyij);
134 HYPRE_IJVectorSetPrintLevel(yyij,10);
135 HYPRE_IJVectorSetObjectType(yyij, HYPRE_PARCSR);
136 HYPRE_IJVectorInitialize(yyij);
137 HYPRE_IJVectorAssemble(yyij);
138 HYPRE_IJVectorGetObject(yyij, (
void **) &yy);
142 HYPRE_IJMatrixDestroy (ij_matrix);
143 HYPRE_BoomerAMGDestroy(preconditioner);
144 HYPRE_IJVectorDestroy (xxij);
145 HYPRE_IJVectorDestroy (yyij);
149 if (eqnIndex) free(eqnIndex);
152 virtual void pre (domain_type&, range_type&) {}
153 virtual void post (domain_type&) {}
155 virtual void apply (domain_type& x, range_type
const& y) {
158 for (
int i=0; i<n; i++)
161 HYPRE_IJVectorSetValues(yyij, n, ixx, yyraw);
162 HYPRE_IJVectorSetValues(xxij, n, ixx, xxraw);
163 HYPRE_BoomerAMGSolve(preconditioner,parcsr_matrix,yy,xx);
167 double rel_resid_norm;
168 HYPRE_BoomerAMGGetNumIterations (preconditioner,&num_iterations);
169 HYPRE_BoomerAMGGetFinalRelativeResidualNorm (preconditioner, &rel_resid_norm);
172 std::cout <<
"HYPRE_BoomerAMG1: " << num_iterations <<
" steps, rel_resid_norm="
173 << rel_resid_norm << std::endl;
176 HYPRE_IJVectorGetValues(xxij,n,ixx,xxraw);
185 virtual Dune::SolverCategory::Category
category()
const override
187 return Dune::SolverCategory::sequential;
191 HYPRE_Solver preconditioner;
192 HYPRE_IJMatrix ij_matrix;
193 HYPRE_ParCSRMatrix parcsr_matrix;
194 HYPRE_IJVector xxij, yyij;
195 HYPRE_ParVector xx, yy;
196 int n, steps, verbosity;
197 double *xxraw, *yyraw;
209 class Euclid:
public Dune::Preconditioner<typename Op::range_type, typename Op::range_type>
211 typedef typename Op::range_type range_type;
212 typedef range_type domain_type;
213 typedef typename Op::field_type field_type;
217 Euclid(Op& op,
int level=1,
double droptol=0.0,
int printlevel=0,
int bj=0,
221 static char main[] =
"main";
226 std::vector<int> Ap, Ai;
227 std::vector<double> Az;
232 std::cout <<
"PrecondType::EUCLID: n=" << n <<
", nnz=" << nnz <<
", level=" <<
233 level <<
", droptol=" << droptol <<
", printlevel=" << printlevel <<
234 ", bj=" << bj <<
"\n";
236 Ap.resize(n+1); Ai.resize(nnz); Az.resize(nnz);
239 for (
int i=0;i<n;i++) Ap[i]=Ap[i+1]-Ap[i];
240 ixx = (
int *)Malloc(n*
sizeof(
int), main);
241 for (
int i=0;i<n;i++) ixx[i]=i;
243 HYPRE_IJMatrixCreate(0, 0, n-1, 0, n-1, &ij_matrix);
244 HYPRE_IJMatrixSetPrintLevel(ij_matrix,10);
245 HYPRE_IJMatrixSetObjectType(ij_matrix, HYPRE_PARCSR);
246 HYPRE_IJMatrixInitialize(ij_matrix);
248 HYPRE_IJMatrixSetValues(ij_matrix,n,&Ap[0],ixx,&Ai[0],&Az[0]);
249 HYPRE_IJMatrixAssemble(ij_matrix);
250 HYPRE_IJMatrixGetObject(ij_matrix, (
void **) &parcsr_matrix);
252 HYPRE_EuclidCreate(0,&preconditioner);
253 HYPRE_EuclidSetStats(preconditioner,printlevel);
254 HYPRE_EuclidSetMem(preconditioner,printlevel);
255 HYPRE_EuclidSetLevel(preconditioner,level);
257 HYPRE_EuclidSetRowScale(preconditioner,0);
259 HYPRE_EuclidSetBJ(preconditioner,bj);
261 HYPRE_EuclidSetILUT(preconditioner,droptol);
262 HYPRE_EuclidSetup(preconditioner,parcsr_matrix,0,0);
264 xxraw = (
double *)Malloc(n*
sizeof(
double), main);
265 yyraw = (
double *)Malloc(n*
sizeof(
double), main);
267 HYPRE_IJVectorCreate(0, 0, n-1, &xxij);
268 HYPRE_IJVectorSetPrintLevel(xxij,10);
269 HYPRE_IJVectorSetObjectType(xxij, HYPRE_PARCSR);
270 HYPRE_IJVectorInitialize(xxij);
271 HYPRE_IJVectorAssemble(xxij);
272 HYPRE_IJVectorGetObject(xxij, (
void **) &xx);
274 HYPRE_IJVectorCreate(0, 0, n-1, &yyij);
275 HYPRE_IJVectorSetPrintLevel(yyij,10);
276 HYPRE_IJVectorSetObjectType(yyij, HYPRE_PARCSR);
277 HYPRE_IJVectorInitialize(yyij);
284 HYPRE_EuclidDestroy(preconditioner);
285 HYPRE_IJMatrixDestroy(ij_matrix);
286 HYPRE_IJVectorDestroy(xxij);
287 HYPRE_IJVectorDestroy(yyij);
290 virtual void pre (domain_type&, range_type&) {}
291 virtual void post (domain_type&) {}
293 virtual void apply (domain_type& x, range_type
const& y) {
296 HYPRE_IJVectorSetValues(yyij, n, ixx, yyraw);
297 HYPRE_IJVectorAssemble(yyij);
298 HYPRE_IJVectorGetObject(yyij, (
void **) &yy);
300 HYPRE_EuclidSolve(preconditioner,parcsr_matrix,yy,xx);
302 HYPRE_IJVectorGetValues(xxij,n,ixx,xxraw);
311 virtual Dune::SolverCategory::Category
category()
const override
313 return Dune::SolverCategory::sequential;
318 HYPRE_Solver preconditioner;
319 HYPRE_IJMatrix ij_matrix;
320 HYPRE_ParCSRMatrix parcsr_matrix;
321 HYPRE_IJVector xxij, yyij;
322 HYPRE_ParVector xx, yy;
324 double *xxraw, *yyraw;
virtual void apply(domain_type &x, range_type const &y)
virtual void post(domain_type &)
virtual void pre(domain_type &, range_type &)
virtual Dune::SolverCategory::Category category() const override
returns the category of the operator
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)
virtual void pre(domain_type &, range_type &)
virtual void post(domain_type &)
Euclid(Op &op, int level=1, double droptol=0.0, int printlevel=0, int bj=0, int verbosity=2)
virtual void apply(domain_type &x, range_type const &y)
virtual Dune::SolverCategory::Category category() const override
returns the category of the operator
SparseIndexInt nrows() const
Returns number of rows (computes them, if not known)
std::vector< SparseIndexInt > ridx
row indices
std::vector< SparseIndexInt > cidx
column indices
std::vector< Scalar > data
data
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...