6 #ifndef DUNE_PDELAB_BACKEND_ISTL_GENEO_ARPACK_GENEO_HH 7 #define DUNE_PDELAB_BACKEND_ISTL_GENEO_ARPACK_GENEO_HH 20 #include <dune/common/fvector.hh> 21 #include <dune/common/exceptions.hh> 23 #include <dune/istl/bvector.hh> 24 #include <dune/istl/istlexception.hh> 25 #include <dune/istl/io.hh> 30 #undef Status // prevent preprocessor from damaging the ARPACK++ 56 template <
class BCRSMatrix>
57 class ArPackPlusPlus_BCRSMatrixWrapperGen
61 typedef typename BCRSMatrix::field_type Real;
68 ArPackPlusPlus_BCRSMatrixWrapperGen (
const BCRSMatrix& A,
const BCRSMatrix& B)
71 a_(A_.M() * mBlock), n_(A_.N() * nBlock)
75 (BCRSMatrix::blocklevel == 2,
76 "Only BCRSMatrices with blocklevel 2 are supported.");
80 domainBlockVector.resize(A_.N(),
false);
81 rangeBlockVector.resize(A_.M(),
false);
85 inline void multMv (Real* v, Real*
w)
88 arrayToDomainBlockVector(v,domainBlockVector);
91 B_.mv(domainBlockVector,rangeBlockVector);
93 Dune::InverseOperatorResult result;
94 auto rangeBlockVector2 = rangeBlockVector;
95 A_solver.apply(rangeBlockVector2, rangeBlockVector, result);
98 rangeBlockVectorToArray(rangeBlockVector2,
w);
101 inline void multMvB (Real* v, Real*
w)
104 arrayToDomainBlockVector(v,domainBlockVector);
107 B_.mv(domainBlockVector,rangeBlockVector);
110 rangeBlockVectorToArray(rangeBlockVector,
w);
115 inline int nrows ()
const {
return a_; }
118 inline int ncols ()
const {
return n_; }
122 constexpr
static int mBlock = BCRSMatrix::block_type::rows;
123 constexpr
static int nBlock = BCRSMatrix::block_type::cols;
127 constexpr
static int dbvBlockSize = nBlock;
128 typedef Dune::FieldVector<Real,dbvBlockSize> DomainBlockVectorBlock;
129 typedef Dune::BlockVector<DomainBlockVectorBlock> DomainBlockVector;
133 constexpr
static int rbvBlockSize = mBlock;
134 typedef Dune::FieldVector<Real,rbvBlockSize> RangeBlockVectorBlock;
135 typedef Dune::BlockVector<RangeBlockVectorBlock> RangeBlockVector;
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;
146 domainBlockVectorToArray (
const DomainBlockVector& dbv, Real* v)
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];
156 rangeBlockVectorToArray (
const RangeBlockVector& rbv, Real* v)
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];
166 static inline void arrayToDomainBlockVector (
const Real* v,
167 DomainBlockVector& dbv)
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];
174 template<
typename Vector>
175 static inline void arrayToVector(
const Real* data,
Vector& v)
177 std::copy(data,data + v.flatsize(),v.begin());
182 static inline void arrayToRangeBlockVector (
const Real* v,
183 RangeBlockVector& rbv)
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];
192 const BCRSMatrix& A_;
193 const BCRSMatrix& B_;
195 Dune::UMFPack<BCRSMatrix> A_solver;
202 mutable DomainBlockVector domainBlockVector;
203 mutable RangeBlockVector rangeBlockVector;
225 template <
typename BCRSMatrix,
typename BlockVectorWrapper>
226 class ArPackPlusPlus_Algorithms
231 typedef typename BlockVector::field_type Real;
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),
258 title_(
" ArPackPlusPlus_Algorithms: "),
259 blank_(title_.length(),
' ')
263 inline void computeGenNonSymMinMagnitude (
const BCRSMatrix& b_,
const Real& epsilon,
264 std::vector<BlockVectorWrapper>& x, std::vector<Real>& lambda, Real sigma)
const 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;
275 const int nev = x.size();
277 const Real tol = epsilon;
278 const int maxit = nIterationsMax_*nev;
279 auto ev = std::vector<Real>(nev);
280 auto ev_imag = std::vector<Real>(nev);
281 const bool ivec =
true;
283 BCRSMatrix ashiftb(a_);
284 ashiftb.axpy(-sigma,b_);
288 typedef ArPackPlusPlus_BCRSMatrixWrapperGen<BCRSMatrix> WrappedMatrix;
289 WrappedMatrix A(ashiftb,b_);
292 const int nrows = A.nrows();
293 const int ncols = A.ncols();
297 DUNE_THROW(Dune::ISTLError,
"Matrix is not square (" 298 << nrows <<
"x" << ncols <<
").");
306 ARNonSymStdEig<Real,WrappedMatrix>
307 dprob(nrows, nev, &A, &WrappedMatrix::multMv, which, ncv, tol, maxit);
311 if (verbosity_level_ > 3) dprob.Trace();
315 auto ev_data = ev.data();
316 auto ev_imag_data = ev_imag.data();
317 dprob.Eigenvalues(ev_data,ev_imag_data,ivec);
320 std::vector<int>
index(nev, 0);
324 [&](
const int& a,
const int& b) {
325 return (sigma+1./ev[a] < sigma+1./ev[b]);
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]);
337 nIterations_ = dprob.GetIter();
345 inline unsigned int getIterationCount ()
const 347 if (nIterations_ == 0)
348 DUNE_THROW(Dune::ISTLError,
"No algorithm applied, yet.");
355 const BCRSMatrix& a_;
356 const unsigned int nIterationsMax_;
359 const unsigned int verbosity_level_;
364 mutable unsigned int nIterations_;
367 const std::string title_;
368 const std::string blank_;
374 #endif // HAVE_ARPACKPP 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