dune-pdelab  2.6.0-rc1
arpackpp_geneo.hh
Go to the documentation of this file.
1 /*
2  * Modified version of the ISTL Arpack++ wrapper for supporting
3  * generalized eigenproblems as required by the GenEO coarse space.
4  */
5 
6 #ifndef DUNE_PDELAB_BACKEND_ISTL_GENEO_ARPACK_GENEO_HH
7 #define DUNE_PDELAB_BACKEND_ISTL_GENEO_ARPACK_GENEO_HH
8 
9 #if HAVE_ARPACKPP
10 
11 #include <cmath> // provides std::abs, std::pow, std::sqrt
12 
13 #include <iostream> // provides std::cout, std::endl
14 #include <string> // provides std::string
15 
16 #include <algorithm>
17 #include <numeric>
18 #include <vector>
19 
20 #include <dune/common/fvector.hh> // provides Dune::FieldVector
21 #include <dune/common/exceptions.hh> // provides DUNE_THROW(...)
22 
23 #include <dune/istl/bvector.hh> // provides Dune::BlockVector
24 #include <dune/istl/istlexception.hh> // provides Dune::ISTLError
25 #include <dune/istl/io.hh> // provides Dune::printvector(...)
26 
28 
29 #ifdef Status
30 #undef Status // prevent preprocessor from damaging the ARPACK++
31  // code when "X11/Xlib.h" is included (the latter
32  // defines Status as "#define Status int" and
33  // ARPACK++ provides a class with a method called
34  // Status)
35 #endif
36 #include "arssym.h" // provides ARSymStdEig
37 #include "argsym.h" // provides ARSymGenEig
38 #include "argnsym.h" // provides ARSymGenEig
39 
40 
41 namespace ArpackGeneo
42 {
43 
56  template <class BCRSMatrix>
57  class ArPackPlusPlus_BCRSMatrixWrapperGen
58  {
59  public:
61  typedef typename BCRSMatrix::field_type Real;
62 
66  public:
68  ArPackPlusPlus_BCRSMatrixWrapperGen (const BCRSMatrix& A, const BCRSMatrix& B)
69  : A_(A), B_(B),
70  A_solver(A_),
71  a_(A_.M() * mBlock), n_(A_.N() * nBlock)
72  {
73  // assert that BCRSMatrix type has blocklevel 2
74  static_assert
75  (BCRSMatrix::blocklevel == 2,
76  "Only BCRSMatrices with blocklevel 2 are supported.");
77 
78  // allocate memory for auxiliary block vector objects
79  // which are compatible to matrix rows / columns
80  domainBlockVector.resize(A_.N(),false);
81  rangeBlockVector.resize(A_.M(),false);
82  }
83 
85  inline void multMv (Real* v, Real* w)
86  {
87  // get vector v as an object of appropriate type
88  arrayToDomainBlockVector(v,domainBlockVector);
89 
90  // perform matrix-vector product
91  B_.mv(domainBlockVector,rangeBlockVector);
92 
93  Dune::InverseOperatorResult result;
94  auto rangeBlockVector2 = rangeBlockVector;
95  A_solver.apply(rangeBlockVector2, rangeBlockVector, result);
96 
97  // get vector w from object of appropriate type
98  rangeBlockVectorToArray(rangeBlockVector2,w);
99  };
100 
101  inline void multMvB (Real* v, Real* w)
102  {
103  // get vector v as an object of appropriate type
104  arrayToDomainBlockVector(v,domainBlockVector);
105 
106  // perform matrix-vector product
107  B_.mv(domainBlockVector,rangeBlockVector);
108 
109  // get vector w from object of appropriate type
110  rangeBlockVectorToArray(rangeBlockVector,w);
111  };
112 
113 
115  inline int nrows () const { return a_; }
116 
118  inline int ncols () const { return n_; }
119 
120  protected:
121  // Number of rows and columns in each block of the matrix
122  constexpr static int mBlock = BCRSMatrix::block_type::rows;
123  constexpr static int nBlock = BCRSMatrix::block_type::cols;
124 
125  // Type of vectors in the domain of the linear map associated with
126  // the matrix, i.e. block vectors compatible to matrix rows
127  constexpr static int dbvBlockSize = nBlock;
128  typedef Dune::FieldVector<Real,dbvBlockSize> DomainBlockVectorBlock;
129  typedef Dune::BlockVector<DomainBlockVectorBlock> DomainBlockVector;
130 
131  // Type of vectors in the range of the linear map associated with
132  // the matrix, i.e. block vectors compatible to matrix columns
133  constexpr static int rbvBlockSize = mBlock;
134  typedef Dune::FieldVector<Real,rbvBlockSize> RangeBlockVectorBlock;
135  typedef Dune::BlockVector<RangeBlockVectorBlock> RangeBlockVector;
136 
137  // Types for vector index access
138  typedef typename DomainBlockVector::size_type dbv_size_type;
139  typedef typename RangeBlockVector::size_type rbv_size_type;
140  typedef typename DomainBlockVectorBlock::size_type dbvb_size_type;
141  typedef typename RangeBlockVectorBlock::size_type rbvb_size_type;
142 
143  // Get vector v from a block vector object which is compatible to
144  // matrix rows
145  static inline void
146  domainBlockVectorToArray (const DomainBlockVector& dbv, Real* v)
147  {
148  for (dbv_size_type block = 0; block < dbv.N(); ++block)
149  for (dbvb_size_type iBlock = 0; iBlock < dbvBlockSize; ++iBlock)
150  v[block*dbvBlockSize + iBlock] = dbv[block][iBlock];
151  }
152 
153  // Get vector v from a block vector object which is compatible to
154  // matrix columns
155  static inline void
156  rangeBlockVectorToArray (const RangeBlockVector& rbv, Real* v)
157  {
158  for (rbv_size_type block = 0; block < rbv.N(); ++block)
159  for (rbvb_size_type iBlock = 0; iBlock < rbvBlockSize; ++iBlock)
160  v[block*rbvBlockSize + iBlock] = rbv[block][iBlock];
161  }
162 
163  public:
166  static inline void arrayToDomainBlockVector (const Real* v,
167  DomainBlockVector& dbv)
168  {
169  for (dbv_size_type block = 0; block < dbv.N(); ++block)
170  for (dbvb_size_type iBlock = 0; iBlock < dbvBlockSize; ++iBlock)
171  dbv[block][iBlock] = v[block*dbvBlockSize + iBlock];
172  }
173 
174  template<typename Vector>
175  static inline void arrayToVector(const Real* data, Vector& v)
176  {
177  std::copy(data,data + v.flatsize(),v.begin());
178  }
179 
182  static inline void arrayToRangeBlockVector (const Real* v,
183  RangeBlockVector& rbv)
184  {
185  for (rbv_size_type block = 0; block < rbv.N(); ++block)
186  for (rbvb_size_type iBlock = 0; iBlock < rbvBlockSize; ++iBlock)
187  rbv[block][iBlock] = v[block*rbvBlockSize + iBlock];
188  }
189 
190  protected:
191  // The DUNE-ISTL BCRSMatrix
192  const BCRSMatrix& A_;
193  const BCRSMatrix& B_;
194 
195  Dune::UMFPack<BCRSMatrix> A_solver;
196 
197  // Number of rows and columns in the matrix
198  const int a_, n_;
199 
200  // Auxiliary block vector objects which are
201  // compatible to matrix rows / columns
202  mutable DomainBlockVector domainBlockVector;
203  mutable RangeBlockVector rangeBlockVector;
204  };
205 
206 
225  template <typename BCRSMatrix, typename BlockVectorWrapper>
226  class ArPackPlusPlus_Algorithms
227  {
228  public:
229 
231  typedef typename BlockVector::field_type Real;
232 
233  public:
252  ArPackPlusPlus_Algorithms (const BCRSMatrix& m,
253  const unsigned int nIterationsMax = 100000,
254  const unsigned int verbosity_level = 0)
255  : a_(m), nIterationsMax_(nIterationsMax),
256  verbosity_level_(verbosity_level),
257  nIterations_(0),
258  title_(" ArPackPlusPlus_Algorithms: "),
259  blank_(title_.length(),' ')
260  {}
261 
262 
263  inline void computeGenNonSymMinMagnitude (const BCRSMatrix& b_, const Real& epsilon,
264  std::vector<BlockVectorWrapper>& x, std::vector<Real>& lambda, Real sigma) const
265  {
266  // print verbosity information
267  if (verbosity_level_ > 0)
268  std::cout << title_ << "Computing an approximation of the "
269  << "least dominant eigenvalue of a matrix which "
270  << "is assumed to be symmetric." << std::endl;
271 
272 
273 
274  // allocate memory for variables, set parameters
275  const int nev = x.size(); // Number of eigenvalues to compute
276  const int ncv = 0; // Number of Arnoldi vectors generated at each iteration (0 == auto)
277  const Real tol = epsilon; // Stopping tolerance (relative accuracy of Ritz values) (0 == machine precision)
278  const int maxit = nIterationsMax_*nev; // Maximum number of Arnoldi update iterations allowed (0 == 100*nev)
279  auto ev = std::vector<Real>(nev); // Computed generalized eigenvalues
280  auto ev_imag = std::vector<Real>(nev); // Computed generalized eigenvalues
281  const bool ivec = true; // Flag deciding if eigenvectors shall be determined
282 
283  BCRSMatrix ashiftb(a_);
284  ashiftb.axpy(-sigma,b_);
285 
286  // use type ArPackPlusPlus_BCRSMatrixWrapperGen to store matrix information
287  // and to perform the product (A-sigma B)^-1 v (LU decomposition is not used)
288  typedef ArPackPlusPlus_BCRSMatrixWrapperGen<BCRSMatrix> WrappedMatrix;
289  WrappedMatrix A(ashiftb,b_);
290 
291  // get number of rows and columns in A
292  const int nrows = A.nrows();
293  const int ncols = A.ncols();
294 
295  // assert that A is square
296  if (nrows != ncols)
297  DUNE_THROW(Dune::ISTLError,"Matrix is not square ("
298  << nrows << "x" << ncols << ").");
299 
300  // define what we need: eigenvalues with smallest magnitude
301  char which[] = "LM";
302  //ARNonSymGenEig<Real,WrappedMatrix,WrappedMatrix>
303  // dprob(nrows, nev, &A, &WrappedMatrix::multMv, &A, &WrappedMatrix::multMvB, sigma, which, ncv, tol, maxit);
304 
305  //Non generalised problem
306  ARNonSymStdEig<Real,WrappedMatrix>
307  dprob(nrows, nev, &A, &WrappedMatrix::multMv, which, ncv, tol, maxit);
308 
309 
310  // set ARPACK verbosity mode if requested
311  if (verbosity_level_ > 3) dprob.Trace();
312 
313  // find eigenvalues and eigenvectors of A
314  // The broken interface of ARPACK++ actually wants a reference to a pointer...
315  auto ev_data = ev.data();
316  auto ev_imag_data = ev_imag.data();
317  dprob.Eigenvalues(ev_data,ev_imag_data,ivec);
318 
319  // Get sorting permutation for un-shifted eigenvalues
320  std::vector<int> index(nev, 0);
321  std::iota(index.begin(),index.end(),0);
322 
323  std::sort(index.begin(), index.end(),
324  [&](const int& a, const int& b) {
325  return (sigma+1./ev[a] < sigma+1./ev[b]);
326  }
327  );
328 
329  // Unshift eigenpairs
330  for (int i = 0; i < nev; i++) {
331  lambda[i] = sigma+1./ev[index[i]];
332  Real* x_raw = dprob.RawEigenvector(index[i]);
333  WrappedMatrix::arrayToVector(x_raw,x[i]);
334  }
335 
336  // obtain number of Arnoldi update iterations actually taken
337  nIterations_ = dprob.GetIter();
338 
339  }
340 
345  inline unsigned int getIterationCount () const
346  {
347  if (nIterations_ == 0)
348  DUNE_THROW(Dune::ISTLError,"No algorithm applied, yet.");
349 
350  return nIterations_;
351  }
352 
353  protected:
354  // parameters related to iterative eigenvalue algorithms
355  const BCRSMatrix& a_;
356  const unsigned int nIterationsMax_;
357 
358  // verbosity setting
359  const unsigned int verbosity_level_;
360 
361  // memory for storing temporary variables (mutable as they shall
362  // just be effectless auxiliary variables of the const apply*(...)
363  // methods)
364  mutable unsigned int nIterations_;
365 
366  // constants for printing verbosity information
367  const std::string title_;
368  const std::string blank_;
369  };
370 
371 } // namespace Dune
372 
373 
374 #endif // HAVE_ARPACKPP
375 
376 #endif // DUNE_PDELAB_BACKEND_ISTL_GENEO_ARPACK_GENEO_HH
VTKWriter & w
Definition: function.hh:829
typename native_type< T >::type Native
Alias of the native container type associated with T or T itself if it is not a backend wrapper.
Definition: backend/interface.hh:176
typename impl::BackendVectorSelector< GridFunctionSpace, FieldType >::Type Vector
alias of the return type of BackendVectorSelector
Definition: backend/interface.hh:106
std::size_t index
Definition: interpolate.hh:118