| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2010 - 2025 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_arpack_solver_h |
| 16 |
#define dealii_arpack_solver_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#include <deal.II/base/enable_observer_pointer.h> |
| 21 |
#include <deal.II/base/exceptions.h> |
| 22 |
#include <deal.II/base/observer_pointer.h> |
| 23 |
#include <deal.II/base/types.h> |
| 24 |
|
| 25 |
#include <deal.II/lac/solver_control.h> |
| 26 |
|
| 27 |
#include <cstring> |
| 28 |
|
| 29 |
|
| 30 |
#ifdef DEAL_II_WITH_ARPACK |
| 31 |
|
| 32 |
DEAL_II_NAMESPACE_OPEN |
| 33 |
|
| 34 |
|
| 35 |
extern "C" void |
| 36 |
dnaupd_(int *ido, |
| 37 |
char *bmat, |
| 38 |
unsigned int *n, |
| 39 |
char *which, |
| 40 |
unsigned int *nev, |
| 41 |
const double *tol, |
| 42 |
double *resid, |
| 43 |
int *ncv, |
| 44 |
double *v, |
| 45 |
int *ldv, |
| 46 |
int *iparam, |
| 47 |
int *ipntr, |
| 48 |
double *workd, |
| 49 |
double *workl, |
| 50 |
int *lworkl, |
| 51 |
int *info); |
| 52 |
|
| 53 |
extern "C" void |
| 54 |
dsaupd_(int *ido, |
| 55 |
char *bmat, |
| 56 |
unsigned int *n, |
| 57 |
char *which, |
| 58 |
unsigned int *nev, |
| 59 |
double *tol, |
| 60 |
double *resid, |
| 61 |
int *ncv, |
| 62 |
double *v, |
| 63 |
int *ldv, |
| 64 |
int *iparam, |
| 65 |
int *ipntr, |
| 66 |
double *workd, |
| 67 |
double *workl, |
| 68 |
int *lworkl, |
| 69 |
int *info); |
| 70 |
|
| 71 |
extern "C" void |
| 72 |
dneupd_(int *rvec, |
| 73 |
char *howmany, |
| 74 |
int *select, |
| 75 |
double *d, |
| 76 |
double *di, |
| 77 |
double *z, |
| 78 |
int *ldz, |
| 79 |
double *sigmar, |
| 80 |
double *sigmai, |
| 81 |
double *workev, |
| 82 |
char *bmat, |
| 83 |
unsigned int *n, |
| 84 |
char *which, |
| 85 |
unsigned int *nev, |
| 86 |
double *tol, |
| 87 |
double *resid, |
| 88 |
int *ncv, |
| 89 |
double *v, |
| 90 |
int *ldv, |
| 91 |
int *iparam, |
| 92 |
int *ipntr, |
| 93 |
double *workd, |
| 94 |
double *workl, |
| 95 |
int *lworkl, |
| 96 |
int *info); |
| 97 |
|
| 98 |
extern "C" void |
| 99 |
dseupd_(int *rvec, |
| 100 |
char *howmany, |
| 101 |
int *select, |
| 102 |
double *d, |
| 103 |
double *z, |
| 104 |
int *ldz, |
| 105 |
double *sigmar, |
| 106 |
char *bmat, |
| 107 |
unsigned int *n, |
| 108 |
char *which, |
| 109 |
unsigned int *nev, |
| 110 |
double *tol, |
| 111 |
double *resid, |
| 112 |
int *ncv, |
| 113 |
double *v, |
| 114 |
int *ldv, |
| 115 |
int *iparam, |
| 116 |
int *ipntr, |
| 117 |
double *workd, |
| 118 |
double *workl, |
| 119 |
int *lworkl, |
| 120 |
int *info); |
| 121 |
|
| 122 |
/** |
| 123 |
* Interface for using ARPACK. ARPACK is a collection of Fortran77 subroutines |
| 124 |
* designed to solve large scale eigenvalue problems. Here we interface to |
| 125 |
* the routines <code>dnaupd</code> and <code>dneupd</code> of ARPACK. |
| 126 |
* If the operator is specified to be symmetric we use the symmetric interface |
| 127 |
* <code>dsaupd</code> and <code>dseupd</code> of ARPACK instead. The |
| 128 |
* package is designed to compute a few eigenvalues and corresponding |
| 129 |
* eigenvectors of a general n by n matrix A. It is most appropriate for large |
| 130 |
* sparse matrices A. |
| 131 |
* |
| 132 |
* In this class we make use of the method applied to the generalized |
| 133 |
* eigenspectrum problem $(A-\lambda B)x=0$, for $x\neq0$; where $A$ is a |
| 134 |
* system matrix, $B$ is a @ref GlossMassMatrix "mass matrix", and $\lambda, x$ are a set of |
| 135 |
* eigenvalues and eigenvectors respectively. |
| 136 |
* |
| 137 |
* The ArpackSolver can be used in application codes with serial objects in |
| 138 |
* the following way: |
| 139 |
* @code |
| 140 |
* SolverControl solver_control(1000, 1e-9); |
| 141 |
* ArpackSolver solver(solver_control); |
| 142 |
* solver.solve(A, B, OP, lambda, x, size_of_spectrum); |
| 143 |
* @endcode |
| 144 |
* for the generalized eigenvalue problem $Ax=B\lambda x$, where the variable |
| 145 |
* <code>size_of_spectrum</code> tells ARPACK the number of |
| 146 |
* eigenvector/eigenvalue pairs to solve for. Here, <code>lambda</code> is a |
| 147 |
* vector that will contain the eigenvalues computed, <code>x</code> a vector |
| 148 |
* that will contain the eigenvectors computed, and <code>OP</code> is an |
| 149 |
* inverse operation for the matrix <code>A</code>. Shift and invert |
| 150 |
* transformation around zero is applied. |
| 151 |
* |
| 152 |
* Through the AdditionalData the user can specify some of the parameters to |
| 153 |
* be set. |
| 154 |
* |
| 155 |
* For further information on how the ARPACK routines <code>dsaupd</code>, |
| 156 |
* <code>dseupd</code>, <code>dnaupd</code> and <code>dneupd</code> work |
| 157 |
* and also how to set the parameters appropriately |
| 158 |
* please take a look into the ARPACK manual. |
| 159 |
* |
| 160 |
* @note Whenever you eliminate degrees of freedom using AffineConstraints, |
| 161 |
* you generate spurious eigenvalues and eigenvectors. If you make sure |
| 162 |
* that the diagonals of eliminated matrix rows are all equal to one, you |
| 163 |
* get a single additional eigenvalue. But beware that some functions in |
| 164 |
* deal.II set these diagonals to rather arbitrary (from the point of view |
| 165 |
* of eigenvalue problems) values. See also |
| 166 |
* @ref step_36 "step-36" |
| 167 |
* for an example. |
| 168 |
*/ |
| 169 |
class ArpackSolver : public EnableObserverPointer |
| 170 |
{ |
| 171 |
public: |
| 172 |
/** |
| 173 |
* Declare the type for container size. |
| 174 |
*/ |
| 175 |
using size_type = types::global_dof_index; |
| 176 |
|
| 177 |
|
| 178 |
/** |
| 179 |
* An enum that lists the possible choices for which eigenvalues to compute |
| 180 |
* in the solve() function. |
| 181 |
*/ |
| 182 |
enum WhichEigenvalues |
| 183 |
{ |
| 184 |
/** |
| 185 |
* The algebraically largest eigenvalues. |
| 186 |
*/ |
| 187 |
algebraically_largest, |
| 188 |
/** |
| 189 |
* The algebraically smallest eigenvalues. |
| 190 |
*/ |
| 191 |
algebraically_smallest, |
| 192 |
/** |
| 193 |
* The eigenvalue with the largest magnitudes. |
| 194 |
*/ |
| 195 |
largest_magnitude, |
| 196 |
/** |
| 197 |
* The eigenvalue with the smallest magnitudes. |
| 198 |
*/ |
| 199 |
smallest_magnitude, |
| 200 |
/** |
| 201 |
* The eigenvalues with the largest real parts. |
| 202 |
*/ |
| 203 |
largest_real_part, |
| 204 |
/** |
| 205 |
* The eigenvalues with the smallest real parts. |
| 206 |
*/ |
| 207 |
smallest_real_part, |
| 208 |
/** |
| 209 |
* The eigenvalues with the largest imaginary parts. |
| 210 |
*/ |
| 211 |
largest_imaginary_part, |
| 212 |
/** |
| 213 |
* The eigenvalues with the smallest imaginary parts. |
| 214 |
*/ |
| 215 |
smallest_imaginary_part, |
| 216 |
/** |
| 217 |
* Compute half of the eigenvalues from the high end of the spectrum and |
| 218 |
* the other half from the low end. If the number of requested |
| 219 |
* eigenvectors is odd, then the extra eigenvector comes from the high end |
| 220 |
* of the spectrum. |
| 221 |
*/ |
| 222 |
both_ends |
| 223 |
}; |
| 224 |
|
| 225 |
/** |
| 226 |
* Standardized data struct to pipe additional data to the solver. |
| 227 |
*/ |
| 228 |
struct AdditionalData |
| 229 |
{ |
| 230 |
/** |
| 231 |
* Constructor. By default, set the number of Arnoldi vectors (Lanczos |
| 232 |
* vectors if the problem is symmetric) to 15. Set the solver to find the |
| 233 |
* eigenvalues of largest magnitude for a non-symmetric problem). |
| 234 |
*/ |
| 235 |
explicit AdditionalData( |
| 236 |
const unsigned int number_of_arnoldi_vectors = 15, |
| 237 |
const WhichEigenvalues eigenvalue_of_interest = largest_magnitude, |
| 238 |
const bool symmetric = false); |
| 239 |
|
| 240 |
/** |
| 241 |
* Number of Arnoldi/Lanczos vectors. This number should be less than the |
| 242 |
* size of the problem but greater than 2 times the number of eigenvalues |
| 243 |
* (or n_eigenvalues if it is set) plus one. |
| 244 |
*/ |
| 245 |
const unsigned int number_of_arnoldi_vectors; |
| 246 |
|
| 247 |
/** |
| 248 |
* Specify the eigenvalues of interest. |
| 249 |
*/ |
| 250 |
const WhichEigenvalues eigenvalue_of_interest; |
| 251 |
|
| 252 |
/** |
| 253 |
* Specify if the problem is symmetric or not. |
| 254 |
*/ |
| 255 |
const bool symmetric; |
| 256 |
}; |
| 257 |
|
| 258 |
/** |
| 259 |
* Access to the object that controls convergence. |
| 260 |
*/ |
| 261 |
SolverControl & |
| 262 |
control() const; |
| 263 |
|
| 264 |
/** |
| 265 |
* Constructor. |
| 266 |
*/ |
| 267 |
ArpackSolver(SolverControl &control, |
| 268 |
const AdditionalData &data = AdditionalData()); |
| 269 |
|
| 270 |
/** |
| 271 |
* Set initial vector for building Krylov space. |
| 272 |
*/ |
| 273 |
template <typename VectorType> |
| 274 |
void |
| 275 |
set_initial_vector(const VectorType &vec); |
| 276 |
|
| 277 |
/** |
| 278 |
* Set shift @p sigma for shift-and-invert spectral transformation. |
| 279 |
* |
| 280 |
* If this function is not called, the shift is assumed to be zero. |
| 281 |
*/ |
| 282 |
void |
| 283 |
set_shift(const std::complex<double> sigma); |
| 284 |
|
| 285 |
/** |
| 286 |
* Solve the generalized eigensprectrum problem $A x=\lambda B x$ by calling |
| 287 |
* the <code>dsaupd</code> and <code>dseupd</code> or |
| 288 |
* <code>dnaupd</code> and <code>dneupd</code> functions of ARPACK. |
| 289 |
* |
| 290 |
* The function returns a vector of eigenvalues of length <i>n</i> and a |
| 291 |
* vector of eigenvectors of length <i>n</i> in the symmetric case |
| 292 |
* and of length <i>n+1</i> in the non-symmetric case. In the symmetric case |
| 293 |
* all eigenvectors are real. In the non-symmetric case complex eigenvalues |
| 294 |
* always occur as complex conjugate pairs. Therefore the eigenvector for an |
| 295 |
* eigenvalue with nonzero complex part is stored by putting the real and |
| 296 |
* the imaginary parts in consecutive real-valued vectors. The eigenvector |
| 297 |
* of the complex conjugate eigenvalue does not need to be stored, since it |
| 298 |
* is just the complex conjugate of the stored eigenvector. Thus, if the last |
| 299 |
* n-th eigenvalue has a nonzero imaginary part, Arpack needs in total n+1 |
| 300 |
* real-valued vectors to store real and imaginary parts of the eigenvectors. |
| 301 |
* |
| 302 |
* @param A The operator for which we want to compute eigenvalues. Actually, |
| 303 |
* this parameter is entirely unused. |
| 304 |
* |
| 305 |
* @param B The inner product of the underlying space, typically the mass |
| 306 |
* matrix. For constrained problems, it can be a partial @ref GlossMassMatrix "mass matrix", like |
| 307 |
* for instance the velocity mass matrix of a Stokes problem. Only its |
| 308 |
* function <code>vmult()</code> is used. |
| 309 |
* |
| 310 |
* @param inverse This is the possibly shifted inverse that is actually used |
| 311 |
* instead of <code>A</code>. Only its function <code>vmult()</code> is |
| 312 |
* used. |
| 313 |
* |
| 314 |
* @param eigenvalues is a vector of complex numbers in which the |
| 315 |
* eigenvalues are returned. |
| 316 |
* |
| 317 |
* @param eigenvectors is a <b>real</b> vector of eigenvectors, containing |
| 318 |
* the real parts of all eigenvectors and the imaginary parts of the |
| 319 |
* eigenvectors corresponding to complex conjugate eigenvalue pairs. |
| 320 |
* Therefore, its length should be <i>n</i> in the symmetric case and |
| 321 |
* <i>n+1</i> in the non-symmetric case. In the non-symmetric case the storage |
| 322 |
* scheme leads for example to the following pattern. Suppose that the first |
| 323 |
* two eigenvalues are real and the third and fourth are a complex conjugate |
| 324 |
* pair. Asking for three eigenpairs results in <i>[real(v1),real(v2), |
| 325 |
* real(v3),imag(v3)]</i>. Note that we get the same pattern if we ask for |
| 326 |
* four eigenpairs in this example, since the fourth eigenvector is simply the |
| 327 |
* complex conjugate of the third one. |
| 328 |
* |
| 329 |
* @param n_eigenvalues The purpose of this parameter is not clear, but it |
| 330 |
* is safe to set it to the size of <code>eigenvalues</code> or greater. |
| 331 |
* Leave it at its default zero, which will be reset to the size of |
| 332 |
* <code>eigenvalues</code> internally. |
| 333 |
*/ |
| 334 |
template <typename VectorType, |
| 335 |
typename MatrixType1, |
| 336 |
typename MatrixType2, |
| 337 |
typename INVERSE> |
| 338 |
void |
| 339 |
solve(const MatrixType1 &A, |
| 340 |
const MatrixType2 &B, |
| 341 |
const INVERSE &inverse, |
| 342 |
std::vector<std::complex<double>> &eigenvalues, |
| 343 |
std::vector<VectorType> &eigenvectors, |
| 344 |
const unsigned int n_eigenvalues = 0); |
| 345 |
|
| 346 |
protected: |
| 347 |
/** |
| 348 |
* Reference to the object that controls convergence of the iterative |
| 349 |
* solver. |
| 350 |
*/ |
| 351 |
SolverControl &solver_control; |
| 352 |
|
| 353 |
/** |
| 354 |
* Store a copy of the flags for this particular solver. |
| 355 |
*/ |
| 356 |
const AdditionalData additional_data; |
| 357 |
|
| 358 |
/** |
| 359 |
* Store an initial vector |
| 360 |
*/ |
| 361 |
bool initial_vector_provided; |
| 362 |
std::vector<double> resid; |
| 363 |
|
| 364 |
/** |
| 365 |
* Real part of the shift |
| 366 |
*/ |
| 367 |
double sigmar; |
| 368 |
|
| 369 |
/** |
| 370 |
* Imaginary part of the shift |
| 371 |
*/ |
| 372 |
double sigmai; |
| 373 |
|
| 374 |
|
| 375 |
private: |
| 376 |
/** |
| 377 |
* Exceptions. |
| 378 |
*/ |
| 379 |
DeclException2(ArpackExcInvalidNumberofEigenvalues, |
| 380 |
int, |
| 381 |
int, |
| 382 |
<< "Number of wanted eigenvalues " << arg1 |
| 383 |
<< " is larger that the size of the matrix " << arg2); |
| 384 |
|
| 385 |
DeclException2(ArpackExcInvalidEigenvectorSize, |
| 386 |
int, |
| 387 |
int, |
| 388 |
<< "Number of wanted eigenvalues " << arg1 |
| 389 |
<< " is larger that the size of eigenvectors " << arg2); |
| 390 |
|
| 391 |
DeclException2( |
| 392 |
ArpackExcInvalidEigenvectorSizeNonsymmetric, |
| 393 |
int, |
| 394 |
int, |
| 395 |
<< "To store the real and complex parts of " << arg1 |
| 396 |
<< " eigenvectors in real-valued vectors, their size (currently set to " |
| 397 |
<< arg2 << ") should be greater than or equal to " << arg1 + 1); |
| 398 |
|
| 399 |
DeclException2(ArpackExcInvalidEigenvalueSize, |
| 400 |
int, |
| 401 |
int, |
| 402 |
<< "Number of wanted eigenvalues " << arg1 |
| 403 |
<< " is larger that the size of eigenvalues " << arg2); |
| 404 |
|
| 405 |
DeclException2(ArpackExcInvalidNumberofArnoldiVectors, |
| 406 |
int, |
| 407 |
int, |
| 408 |
<< "Number of Arnoldi vectors " << arg1 |
| 409 |
<< " is larger that the size of the matrix " << arg2); |
| 410 |
|
| 411 |
DeclException2(ArpackExcSmallNumberofArnoldiVectors, |
| 412 |
int, |
| 413 |
int, |
| 414 |
<< "Number of Arnoldi vectors " << arg1 |
| 415 |
<< " is too small to obtain " << arg2 << " eigenvalues"); |
| 416 |
|
| 417 |
DeclException1(ArpackExcArpackIdo, |
| 418 |
int, |
| 419 |
<< "This ido " << arg1 |
| 420 |
<< " is not supported. Check documentation of ARPACK"); |
| 421 |
|
| 422 |
DeclException1(ArpackExcArpackMode, |
| 423 |
int, |
| 424 |
<< "This mode " << arg1 |
| 425 |
<< " is not supported. Check documentation of ARPACK"); |
| 426 |
|
| 427 |
DeclException1(ArpackExcArpackInfodsaupd, |
| 428 |
int, |
| 429 |
<< "Error with dsaupd, info " << arg1 |
| 430 |
<< ". Check documentation of ARPACK"); |
| 431 |
|
| 432 |
DeclException1(ArpackExcArpackInfodnaupd, |
| 433 |
int, |
| 434 |
<< "Error with dnaupd, info " << arg1 |
| 435 |
<< ". Check documentation of ARPACK"); |
| 436 |
|
| 437 |
DeclException1(ArpackExcArpackInfodseupd, |
| 438 |
int, |
| 439 |
<< "Error with dseupd, info " << arg1 |
| 440 |
<< ". Check documentation of ARPACK"); |
| 441 |
|
| 442 |
DeclException1(ArpackExcArpackInfodneupd, |
| 443 |
int, |
| 444 |
<< "Error with dneupd, info " << arg1 |
| 445 |
<< ". Check documentation of ARPACK"); |
| 446 |
|
| 447 |
DeclException1(ArpackExcArpackInfoMaxIt, |
| 448 |
int, |
| 449 |
<< "Maximum number " << arg1 << " of iterations reached."); |
| 450 |
|
| 451 |
DeclExceptionMsg(ArpackExcArpackNoShifts, |
| 452 |
"No shifts could be applied during implicit" |
| 453 |
" Arnoldi update, try increasing the number of" |
| 454 |
" Arnoldi vectors."); |
| 455 |
}; |
| 456 |
|
| 457 |
|
| 458 |
inline ArpackSolver::AdditionalData::AdditionalData( |
| 459 |
const unsigned int number_of_arnoldi_vectors, |
| 460 |
const WhichEigenvalues eigenvalue_of_interest, |
| 461 |
const bool symmetric) |
| 462 |
: number_of_arnoldi_vectors(number_of_arnoldi_vectors) |
| 463 |
, eigenvalue_of_interest(eigenvalue_of_interest) |
| 464 |
, symmetric(symmetric) |
| 465 |
{ |
| 466 |
// Check for possible options for symmetric problems |
| 467 |
if (symmetric) |
| 468 |
{ |
| 469 |
Assert( |
| 470 |
eigenvalue_of_interest != largest_real_part, |
| 471 |
ExcMessage( |
| 472 |
"'largest real part' can only be used for non-symmetric problems!")); |
| 473 |
Assert( |
| 474 |
eigenvalue_of_interest != smallest_real_part, |
| 475 |
ExcMessage( |
| 476 |
"'smallest real part' can only be used for non-symmetric problems!")); |
| 477 |
Assert( |
| 478 |
eigenvalue_of_interest != largest_imaginary_part, |
| 479 |
ExcMessage( |
| 480 |
"'largest imaginary part' can only be used for non-symmetric problems!")); |
| 481 |
Assert( |
| 482 |
eigenvalue_of_interest != smallest_imaginary_part, |
| 483 |
ExcMessage( |
| 484 |
"'smallest imaginary part' can only be used for non-symmetric problems!")); |
| 485 |
} |
| 486 |
// Check for possible options for asymmetric problems |
| 487 |
else |
| 488 |
{ |
| 489 |
Assert( |
| 490 |
eigenvalue_of_interest != algebraically_largest, |
| 491 |
ExcMessage( |
| 492 |
"'largest algebraic part' can only be used for symmetric problems!")); |
| 493 |
Assert( |
| 494 |
eigenvalue_of_interest != algebraically_smallest, |
| 495 |
ExcMessage( |
| 496 |
"'smallest algebraic part' can only be used for symmetric problems!")); |
| 497 |
Assert(eigenvalue_of_interest != both_ends, |
| 498 |
ExcMessage( |
| 499 |
"'both ends' can only be used for symmetric problems!")); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
inline ArpackSolver::ArpackSolver(SolverControl &control, |
| 505 |
const AdditionalData &data) |
| 506 |
: solver_control(control) |
| 507 |
, additional_data(data) |
| 508 |
, initial_vector_provided(false) |
| 509 |
, sigmar(0.0) |
| 510 |
, sigmai(0.0) |
| 511 |
{} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
inline void |
| 516 |
ArpackSolver::set_shift(const std::complex<double> sigma) |
| 517 |
{ |
| 518 |
sigmar = sigma.real(); |
| 519 |
sigmai = sigma.imag(); |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
template <typename VectorType> |
| 525 |
inline void |
| 526 |
ArpackSolver::set_initial_vector(const VectorType &vec) |
| 527 |
{ |
| 528 |
initial_vector_provided = true; |
| 529 |
resid.resize(vec.size()); |
| 530 |
for (size_type i = 0; i < vec.size(); ++i) |
| 531 |
resid[i] = vec[i]; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
template <typename VectorType, |
| 536 |
typename MatrixType1, |
| 537 |
typename MatrixType2, |
| 538 |
typename INVERSE> |
| 539 |
inline void |
| 540 |
ArpackSolver::solve(const MatrixType1 & /*system_matrix*/, |
| 541 |
const MatrixType2 &mass_matrix, |
| 542 |
const INVERSE &inverse, |
| 543 |
std::vector<std::complex<double>> &eigenvalues, |
| 544 |
std::vector<VectorType> &eigenvectors, |
| 545 |
const unsigned int n_eigenvalues) |
| 546 |
{ |
| 547 |
// Problem size |
| 548 |
unsigned int n = eigenvectors[0].size(); |
| 549 |
|
| 550 |
// Number of eigenvalues |
| 551 |
const unsigned int nev_const = |
| 552 |
(n_eigenvalues == 0) ? eigenvalues.size() : n_eigenvalues; |
| 553 |
// nev for arpack, which might change by plus one during dneupd |
| 554 |
unsigned int nev = nev_const; |
| 555 |
|
| 556 |
// check input sizes |
| 557 |
if (additional_data.symmetric) |
| 558 |
{ |
| 559 |
Assert(nev <= eigenvectors.size(), |
| 560 |
ArpackExcInvalidEigenvectorSize(nev, eigenvectors.size())); |
| 561 |
} |
| 562 |
else |
| 563 |
Assert(nev + 1 <= eigenvectors.size(), |
| 564 |
ArpackExcInvalidEigenvectorSizeNonsymmetric(nev, |
| 565 |
eigenvectors.size())); |
| 566 |
|
| 567 |
Assert(nev <= eigenvalues.size(), |
| 568 |
ArpackExcInvalidEigenvalueSize(nev, eigenvalues.size())); |
| 569 |
|
| 570 |
// check large enough problem size |
| 571 |
Assert(nev < n, ArpackExcInvalidNumberofEigenvalues(nev, n)); |
| 572 |
|
| 573 |
Assert(additional_data.number_of_arnoldi_vectors < n, |
| 574 |
ArpackExcInvalidNumberofArnoldiVectors( |
| 575 |
additional_data.number_of_arnoldi_vectors, n)); |
| 576 |
|
| 577 |
// check whether we have enough Arnoldi vectors |
| 578 |
Assert(additional_data.number_of_arnoldi_vectors > 2 * nev + 1, |
| 579 |
ArpackExcSmallNumberofArnoldiVectors( |
| 580 |
additional_data.number_of_arnoldi_vectors, nev)); |
| 581 |
|
| 582 |
// ARPACK mode for dsaupd/dnaupd, here only mode 3, i.e. shift-invert mode |
| 583 |
int mode = 3; |
| 584 |
|
| 585 |
// reverse communication parameter |
| 586 |
int ido = 0; |
| 587 |
|
| 588 |
// 'G' generalized eigenvalue problem 'I' standard eigenvalue problem |
| 589 |
char bmat[2] = "G"; |
| 590 |
|
| 591 |
// Specify the eigenvalues of interest, possible parameters "LA" algebraically |
| 592 |
// largest "SA" algebraically smallest "LM" largest magnitude "SM" smallest |
| 593 |
// magnitude "LR" largest real part "SR" smallest real part "LI" largest |
| 594 |
// imaginary part "SI" smallest imaginary part "BE" both ends of spectrum |
| 595 |
// simultaneous. |
| 596 |
char which[3]; |
| 597 |
switch (additional_data.eigenvalue_of_interest) |
| 598 |
{ |
| 599 |
case algebraically_largest: |
| 600 |
std::strcpy(which, "LA"); |
| 601 |
break; |
| 602 |
case algebraically_smallest: |
| 603 |
std::strcpy(which, "SA"); |
| 604 |
break; |
| 605 |
case largest_magnitude: |
| 606 |
std::strcpy(which, "LM"); |
| 607 |
break; |
| 608 |
case smallest_magnitude: |
| 609 |
std::strcpy(which, "SM"); |
| 610 |
break; |
| 611 |
case largest_real_part: |
| 612 |
std::strcpy(which, "LR"); |
| 613 |
break; |
| 614 |
case smallest_real_part: |
| 615 |
std::strcpy(which, "SR"); |
| 616 |
break; |
| 617 |
case largest_imaginary_part: |
| 618 |
std::strcpy(which, "LI"); |
| 619 |
break; |
| 620 |
case smallest_imaginary_part: |
| 621 |
std::strcpy(which, "SI"); |
| 622 |
break; |
| 623 |
case both_ends: |
| 624 |
std::strcpy(which, "BE"); |
| 625 |
break; |
| 626 |
} |
| 627 |
|
| 628 |
// tolerance for ARPACK |
| 629 |
double tol = control().tolerance(); |
| 630 |
|
| 631 |
// if the starting vector is used it has to be in resid |
| 632 |
if (!initial_vector_provided || resid.size() != n) |
| 633 |
resid.resize(n, 1.); |
| 634 |
|
| 635 |
// number of Arnoldi basis vectors specified |
| 636 |
// in additional_data |
| 637 |
int ncv = additional_data.number_of_arnoldi_vectors; |
| 638 |
|
| 639 |
int ldv = n; |
| 640 |
std::vector<double> v(ldv * ncv, 0.0); |
| 641 |
|
| 642 |
// information to the routines |
| 643 |
std::vector<int> iparam(11, 0); |
| 644 |
|
| 645 |
iparam[0] = 1; // shift strategy |
| 646 |
|
| 647 |
// maximum number of iterations |
| 648 |
iparam[2] = control().max_steps(); |
| 649 |
|
| 650 |
// Set the mode of dsaupd. 1 is exact shifting, 2 is user-supplied shifts, |
| 651 |
// 3 is shift-invert mode, 4 is buckling mode, 5 is Cayley mode. |
| 652 |
|
| 653 |
iparam[6] = mode; |
| 654 |
std::vector<int> ipntr(14, 0); |
| 655 |
|
| 656 |
// work arrays for ARPACK |
| 657 |
std::vector<double> workd(3 * n, 0.); |
| 658 |
int lworkl = |
| 659 |
additional_data.symmetric ? ncv * ncv + 8 * ncv : 3 * ncv * ncv + 6 * ncv; |
| 660 |
std::vector<double> workl(lworkl, 0.); |
| 661 |
|
| 662 |
// information out of the iteration |
| 663 |
int info = 1; |
| 664 |
|
| 665 |
while (ido != 99) |
| 666 |
{ |
| 667 |
// call of ARPACK dsaupd/dnaupd routine |
| 668 |
if (additional_data.symmetric) |
| 669 |
dsaupd_(&ido, |
| 670 |
bmat, |
| 671 |
&n, |
| 672 |
which, |
| 673 |
&nev, |
| 674 |
&tol, |
| 675 |
resid.data(), |
| 676 |
&ncv, |
| 677 |
v.data(), |
| 678 |
&ldv, |
| 679 |
iparam.data(), |
| 680 |
ipntr.data(), |
| 681 |
workd.data(), |
| 682 |
workl.data(), |
| 683 |
&lworkl, |
| 684 |
&info); |
| 685 |
else |
| 686 |
dnaupd_(&ido, |
| 687 |
bmat, |
| 688 |
&n, |
| 689 |
which, |
| 690 |
&nev, |
| 691 |
&tol, |
| 692 |
resid.data(), |
| 693 |
&ncv, |
| 694 |
v.data(), |
| 695 |
&ldv, |
| 696 |
iparam.data(), |
| 697 |
ipntr.data(), |
| 698 |
workd.data(), |
| 699 |
workl.data(), |
| 700 |
&lworkl, |
| 701 |
&info); |
| 702 |
|
| 703 |
if (ido == 99) |
| 704 |
break; |
| 705 |
|
| 706 |
switch (mode) |
| 707 |
{ |
| 708 |
case 3: |
| 709 |
{ |
| 710 |
switch (ido) |
| 711 |
{ |
| 712 |
case -1: |
| 713 |
{ |
| 714 |
VectorType src, dst, tmp; |
| 715 |
src.reinit(eigenvectors[0]); |
| 716 |
dst.reinit(src); |
| 717 |
tmp.reinit(src); |
| 718 |
|
| 719 |
|
| 720 |
for (size_type i = 0; i < src.size(); ++i) |
| 721 |
src(i) = workd[ipntr[0] - 1 + i]; |
| 722 |
|
| 723 |
// multiplication with mass matrix M |
| 724 |
mass_matrix.vmult(tmp, src); |
| 725 |
// solving linear system |
| 726 |
inverse.vmult(dst, tmp); |
| 727 |
|
| 728 |
for (size_type i = 0; i < dst.size(); ++i) |
| 729 |
workd[ipntr[1] - 1 + i] = dst(i); |
| 730 |
} |
| 731 |
break; |
| 732 |
|
| 733 |
case 1: |
| 734 |
{ |
| 735 |
VectorType src, dst, tmp, tmp2; |
| 736 |
src.reinit(eigenvectors[0]); |
| 737 |
dst.reinit(src); |
| 738 |
tmp.reinit(src); |
| 739 |
tmp2.reinit(src); |
| 740 |
|
| 741 |
for (size_type i = 0; i < src.size(); ++i) |
| 742 |
{ |
| 743 |
src(i) = workd[ipntr[2] - 1 + i]; |
| 744 |
tmp(i) = workd[ipntr[0] - 1 + i]; |
| 745 |
} |
| 746 |
// solving linear system |
| 747 |
inverse.vmult(dst, src); |
| 748 |
|
| 749 |
for (size_type i = 0; i < dst.size(); ++i) |
| 750 |
workd[ipntr[1] - 1 + i] = dst(i); |
| 751 |
} |
| 752 |
break; |
| 753 |
|
| 754 |
case 2: |
| 755 |
{ |
| 756 |
VectorType src, dst; |
| 757 |
src.reinit(eigenvectors[0]); |
| 758 |
dst.reinit(src); |
| 759 |
|
| 760 |
for (size_type i = 0; i < src.size(); ++i) |
| 761 |
src(i) = workd[ipntr[0] - 1 + i]; |
| 762 |
|
| 763 |
// Multiplication with mass matrix M |
| 764 |
mass_matrix.vmult(dst, src); |
| 765 |
|
| 766 |
for (size_type i = 0; i < dst.size(); ++i) |
| 767 |
workd[ipntr[1] - 1 + i] = dst(i); |
| 768 |
} |
| 769 |
break; |
| 770 |
|
| 771 |
default: |
| 772 |
Assert(false, ArpackExcArpackIdo(ido)); |
| 773 |
break; |
| 774 |
} |
| 775 |
} |
| 776 |
break; |
| 777 |
default: |
| 778 |
Assert(false, ArpackExcArpackMode(mode)); |
| 779 |
break; |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
// Set number of used iterations in SolverControl |
| 784 |
control().check(iparam[2], 0.); |
| 785 |
|
| 786 |
if (info < 0) |
| 787 |
{ |
| 788 |
if (additional_data.symmetric) |
| 789 |
{ |
| 790 |
Assert(false, ArpackExcArpackInfodsaupd(info)); |
| 791 |
} |
| 792 |
else |
| 793 |
Assert(false, ArpackExcArpackInfodnaupd(info)); |
| 794 |
} |
| 795 |
else |
| 796 |
{ |
| 797 |
// 1 - compute eigenvectors, 0 - only eigenvalues |
| 798 |
int rvec = 1; |
| 799 |
|
| 800 |
// which eigenvectors |
| 801 |
char howmany = 'A'; |
| 802 |
|
| 803 |
std::vector<int> select(ncv, 1); |
| 804 |
|
| 805 |
int ldz = n; |
| 806 |
|
| 807 |
std::vector<double> eigenvalues_real(nev + 1, 0.); |
| 808 |
std::vector<double> eigenvalues_im(nev + 1, 0.); |
| 809 |
|
| 810 |
// call of ARPACK dseupd/dneupd routine |
| 811 |
if (additional_data.symmetric) |
| 812 |
{ |
| 813 |
std::vector<double> z(ldz * nev, 0.); |
| 814 |
dseupd_(&rvec, |
| 815 |
&howmany, |
| 816 |
select.data(), |
| 817 |
eigenvalues_real.data(), |
| 818 |
z.data(), |
| 819 |
&ldz, |
| 820 |
&sigmar, |
| 821 |
bmat, |
| 822 |
&n, |
| 823 |
which, |
| 824 |
&nev, |
| 825 |
&tol, |
| 826 |
resid.data(), |
| 827 |
&ncv, |
| 828 |
v.data(), |
| 829 |
&ldv, |
| 830 |
iparam.data(), |
| 831 |
ipntr.data(), |
| 832 |
workd.data(), |
| 833 |
workl.data(), |
| 834 |
&lworkl, |
| 835 |
&info); |
| 836 |
} |
| 837 |
else |
| 838 |
{ |
| 839 |
std::vector<double> workev(3 * ncv, 0.); |
| 840 |
dneupd_(&rvec, |
| 841 |
&howmany, |
| 842 |
select.data(), |
| 843 |
eigenvalues_real.data(), |
| 844 |
eigenvalues_im.data(), |
| 845 |
v.data(), |
| 846 |
&ldz, |
| 847 |
&sigmar, |
| 848 |
&sigmai, |
| 849 |
workev.data(), |
| 850 |
bmat, |
| 851 |
&n, |
| 852 |
which, |
| 853 |
&nev, |
| 854 |
&tol, |
| 855 |
resid.data(), |
| 856 |
&ncv, |
| 857 |
v.data(), |
| 858 |
&ldv, |
| 859 |
iparam.data(), |
| 860 |
ipntr.data(), |
| 861 |
workd.data(), |
| 862 |
workl.data(), |
| 863 |
&lworkl, |
| 864 |
&info); |
| 865 |
} |
| 866 |
|
| 867 |
if (info == 1) |
| 868 |
{ |
| 869 |
Assert(false, ArpackExcArpackInfoMaxIt(control().max_steps())); |
| 870 |
} |
| 871 |
else if (info == 3) |
| 872 |
{ |
| 873 |
Assert(false, ArpackExcArpackNoShifts()); |
| 874 |
} |
| 875 |
else if (info != 0) |
| 876 |
{ |
| 877 |
if (additional_data.symmetric) |
| 878 |
{ |
| 879 |
Assert(false, ArpackExcArpackInfodseupd(info)); |
| 880 |
} |
| 881 |
else |
| 882 |
Assert(false, ArpackExcArpackInfodneupd(info)); |
| 883 |
} |
| 884 |
|
| 885 |
for (unsigned int i = 0; i < nev; ++i) |
| 886 |
for (unsigned int j = 0; j < n; ++j) |
| 887 |
eigenvectors[i](j) = v[i * n + j]; |
| 888 |
|
| 889 |
for (unsigned int i = 0; i < nev_const; ++i) |
| 890 |
eigenvalues[i] = |
| 891 |
std::complex<double>(eigenvalues_real[i], eigenvalues_im[i]); |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
|
| 896 |
inline SolverControl & |
| 897 |
ArpackSolver::control() const |
| 898 |
{ |
| 899 |
return solver_control; |
| 900 |
} |
| 901 |
|
| 902 |
DEAL_II_NAMESPACE_CLOSE |
| 903 |
|
| 904 |
|
| 905 |
#else |
| 906 |
|
| 907 |
// Make sure the scripts that create the C++20 module input files have |
| 908 |
// something to latch on if the preprocessor #ifdef above would |
| 909 |
// otherwise lead to an empty content of the file. |
| 910 |
DEAL_II_NAMESPACE_OPEN |
| 911 |
DEAL_II_NAMESPACE_CLOSE |
| 912 |
|
| 913 |
#endif |
| 914 |
#endif |