Debian Codegraph @

include/deal.II/lac/parpack_solver.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2015 - 2024 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_parpack_solver_h
16 #define dealii_parpack_solver_h
17
18 #include <deal.II/base/config.h>
19
20 #include <deal.II/base/index_set.h>
21 #include <deal.II/base/memory_consumption.h>
22 #include <deal.II/base/observer_pointer.h>
23
24 #include <deal.II/lac/solver_control.h>
25 #include <deal.II/lac/vector_operation.h>
26
27 #include <cstring>
28
29
30 #ifdef DEAL_II_ARPACK_WITH_PARPACK
31
32 DEAL_II_NAMESPACE_OPEN
33
34 extern "C"
35 {
36 // http://www.mathkeisan.com/usersguide/man/pdnaupd.html
37 void
38 pdnaupd_(MPI_Fint *comm,
39 int *ido,
40 char *bmat,
41 int *n,
42 char *which,
43 int *nev,
44 double *tol,
45 double *resid,
46 int *ncv,
47 double *v,
48 int *nloc,
49 int *iparam,
50 int *ipntr,
51 double *workd,
52 double *workl,
53 int *lworkl,
54 int *info);
55
56 // http://www.mathkeisan.com/usersguide/man/pdsaupd.html
57 void
58 pdsaupd_(MPI_Fint *comm,
59 int *ido,
60 char *bmat,
61 int *n,
62 char *which,
63 int *nev,
64 double *tol,
65 double *resid,
66 int *ncv,
67 double *v,
68 int *nloc,
69 int *iparam,
70 int *ipntr,
71 double *workd,
72 double *workl,
73 int *lworkl,
74 int *info);
75
76 // http://www.mathkeisan.com/usersguide/man/pdneupd.html
77 void
78 pdneupd_(MPI_Fint *comm,
79 int *rvec,
80 char *howmany,
81 int *select,
82 double *d,
83 double *di,
84 double *z,
85 int *ldz,
86 double *sigmar,
87 double *sigmai,
88 double *workev,
89 char *bmat,
90 int *n,
91 char *which,
92 int *nev,
93 double *tol,
94 double *resid,
95 int *ncv,
96 double *v,
97 int *nloc,
98 int *iparam,
99 int *ipntr,
100 double *workd,
101 double *workl,
102 int *lworkl,
103 int *info);
104
105 // http://www.mathkeisan.com/usersguide/man/pdseupd.html
106 void
107 pdseupd_(MPI_Fint *comm,
108 int *rvec,
109 char *howmany,
110 int *select,
111 double *d,
112 double *z,
113 int *ldz,
114 double *sigmar,
115 char *bmat,
116 int *n,
117 char *which,
118 int *nev,
119 double *tol,
120 double *resid,
121 int *ncv,
122 double *v,
123 int *nloc,
124 int *iparam,
125 int *ipntr,
126 double *workd,
127 double *workl,
128 int *lworkl,
129 int *info);
130
131 // other resources:
132 // http://acts.nersc.gov/superlu/example5/pnslac.c.html
133 // https://github.com/phpisciuneri/tijo/blob/master/dvr_parpack.cpp
134 }
135
136 /**
137 * Interface for using PARPACK. PARPACK is a collection of Fortran77
138 * subroutines designed to solve large scale eigenvalue problems. Here we
139 * interface to the routines <code>pdneupd</code>, <code>pdseupd</code>,
140 * <code>pdnaupd</code>, <code>pdsaupd</code> of PARPACK. The package is
141 * designed to compute a few eigenvalues and corresponding eigenvectors of a
142 * general n by n matrix A. It is most appropriate for large sparse matrices
143 * A.
144 *
145 * In this class we make use of the method applied to the generalized
146 * eigenspectrum problem $(A-\lambda B)x=0$, for $x\neq0$; where $A$ is a
147 * system matrix, $B$ is a @ref GlossMassMatrix "mass matrix", and $\lambda, x$ are a set of
148 * eigenvalues and eigenvectors respectively.
149 *
150 * The ArpackSolver can be used in application codes in the following way:
151 * @code
152 * SolverControl solver_control (1000, 1e-9);
153 * const unsigned int num_arnoldi_vectors = 2*size_of_spectrum + 2;
154 * PArpackSolver<V>::AdditionalData
155 * additional_data(num_arnoldi_vectors,
156 * dealii::PArpackSolver<V>::largest_magnitude,
157 * true);
158 *
159 * PArpackSolver<V> eigensolver (solver_control,
160 * mpi_communicator,
161 * additional_data);
162 * eigensolver.set_shift(sigma);
163 * eigensolver.reinit(locally_owned_dofs);
164 * eigensolver.solve (A,
165 * B,
166 * OP,
167 * lambda,
168 * x,
169 * size_of_spectrum);
170 * @endcode
171 * for the generalized eigenvalue problem $Ax=B\lambda x$, where the variable
172 * <code>size_of_spectrum</code> tells PARPACK the number of
173 * eigenvector/eigenvalue pairs to solve for. Here, <code>lambda</code> is a
174 * vector that will contain the eigenvalues computed, <code>x</code> a vector
175 * of objects of type <code>V</code> that will contain the eigenvectors
176 * computed.
177 *
178 * Currently, only three modes of (P)Arpack are implemented. In mode 3
179 * (default), <code>OP</code> is an inverse operation for the matrix <code>A -
180 * sigma * B</code>, where <code> sigma </code> is a shift value, set to zero
181 * by default. Whereas in mode 2, <code>OP</code> is an inverse of
182 * <code>M</code>. Finally, mode 1 corresponds to standard eigenvalue problem
183 * without spectral transformation $Ax=\lambda x$. The mode can be specified via
184 * AdditionalData object. Note that for shift-and-invert (mode=3), the sought
185 * eigenpairs are those after the spectral transformation is applied.
186 *
187 * The <code>OP</code> can be specified by using a LinearOperator:
188 * @code
189 * const double shift = 5.0;
190 * const auto op_A = linear_operator<vector_t>(A);
191 * const auto op_B = linear_operator<vector_t>(B);
192 * const auto op_shift = op_A - shift * op_B;
193 * SolverControl solver_control_lin (1000, 1e-10,false,false);
194 *
195 * SolverCG<vector_t> cg(solver_control_lin);
196 * const auto op_shift_invert =
197 * inverse_operator(op_shift, cg, PreconditionIdentity ());
198 * @endcode
199 *
200 * The class is intended to be used with MPI and can work on arbitrary vector
201 * and matrix distributed classes. Both symmetric and non-symmetric
202 * <code>A</code> are supported.
203 *
204 * For further information on how the PARPACK routines <code>pdneupd</code>,
205 * <code>pdseupd</code>, <code>pdnaupd</code>, <code>pdsaupd</code> work and
206 * also how to set the parameters appropriately please take a look into the
207 * PARPACK manual.
208 */
209 template <typename VectorType>
210 class PArpackSolver : public EnableObserverPointer
211 {
212 public:
213 /**
214 * Declare the type for container size.
215 */
216 using size_type = types::global_dof_index;
217
218 /**
219 * An enum that lists the possible choices for which eigenvalues to compute
220 * in the solve() function. Note, that this corresponds to the problem after
221 * shift-and-invert (the only currently supported spectral transformation)
222 * is applied.
223 *
224 * A particular choice is limited based on symmetric or non-symmetric matrix
225 * <code>A</code> considered.
226 */
227 enum WhichEigenvalues
228 {
229 /**
230 * The algebraically largest eigenvalues.
231 */
232 algebraically_largest,
233 /**
234 * The algebraically smallest eigenvalues.
235 */
236 algebraically_smallest,
237 /**
238 * The eigenvalue with the largest magnitudes.
239 */
240 largest_magnitude,
241 /**
242 * The eigenvalue with the smallest magnitudes.
243 */
244 smallest_magnitude,
245 /**
246 * The eigenvalues with the largest real parts.
247 */
248 largest_real_part,
249 /**
250 * The eigenvalues with the smallest real parts.
251 */
252 smallest_real_part,
253 /**
254 * The eigenvalues with the largest imaginary parts.
255 */
256 largest_imaginary_part,
257 /**
258 * The eigenvalues with the smallest imaginary parts.
259 */
260 smallest_imaginary_part,
261 /**
262 * Compute half of the eigenvalues from the high end of the spectrum and
263 * the other half from the low end. If the number of requested
264 * eigenvectors is odd, then the extra eigenvector comes from the high end
265 * of the spectrum.
266 */
267 both_ends
268 };
269
270 /**
271 * Standardized data struct to pipe additional data to the solver, should it
272 * be needed.
273 */
274 struct AdditionalData
275 {
276 const unsigned int number_of_arnoldi_vectors;
277 const WhichEigenvalues eigenvalue_of_interest;
278 const bool symmetric;
279 const int mode;
280 AdditionalData(
281 const unsigned int number_of_arnoldi_vectors = 15,
282 const WhichEigenvalues eigenvalue_of_interest = largest_magnitude,
283 const bool symmetric = false,
284 const int mode = 3);
285 };
286
287 /**
288 * Access to the object that controls convergence.
289 */
290 SolverControl &
291 control() const;
292
293 /**
294 * Constructor.
295 */
296 PArpackSolver(SolverControl &control,
297 const MPI_Comm mpi_communicator,
298 const AdditionalData &data = AdditionalData());
299
300 /**
301 * Initialize internal variables.
302 */
303 void
304 reinit(const IndexSet &locally_owned_dofs);
305
306 /**
307 * Initialize internal variables when working with BlockVectors.
308 * @p locally_owned_dofs is used to set the dimension of the problem,
309 * whereas @p partitioning is used for calling the reinit of the deal.II
310 * blockvectors used.
311 */
312 void
313 reinit(const IndexSet &locally_owned_dofs,
314 const std::vector<IndexSet> &partitioning);
315
316 /**
317 * Initialize internal variables from the input @p distributed_vector.
318 */
319 void
320 reinit(const VectorType &distributed_vector);
321
322 /**
323 * Set initial vector for building Krylov space.
324 */
325 void
326 set_initial_vector(const VectorType &vec);
327
328 /**
329 * Set shift @p sigma for shift-and-invert spectral transformation.
330 *
331 * If this function is not called, the shift is assumed to be zero.
332 *
333 * @note only relevant for <code>mode=3</code> (see the general documentation of this
334 * class for a definition of what the different modes are).
335 */
336 void
337 set_shift(const std::complex<double> sigma);
338
339 /**
340 * Solve the generalized eigensprectrum problem $A x=\lambda B x$ by calling
341 * the <code>pd(n/s)eupd</code> and <code>pd(n/s)aupd</code> functions of
342 * PARPACK.
343 *
344 * In <code>mode=3</code>, @p inverse should correspond to $[A-\sigma B]^{-1}$,
345 * whereas in <code>mode=2</code> it should represent $B^{-1}$. For
346 * <code>mode=1</code> both @p B and @p inverse are ignored.
347 */
348 template <typename MatrixType1, typename MatrixType2, typename INVERSE>
349 void
350 solve(const MatrixType1 &A,
351 const MatrixType2 &B,
352 const INVERSE &inverse,
353 std::vector<std::complex<double>> &eigenvalues,
354 std::vector<VectorType> &eigenvectors,
355 const unsigned int n_eigenvalues);
356
357 /**
358 * Same as above but takes eigenvectors as pointers.
359 */
360 template <typename MatrixType1, typename MatrixType2, typename INVERSE>
361 void
362 solve(const MatrixType1 &A,
363 const MatrixType2 &B,
364 const INVERSE &inverse,
365 std::vector<std::complex<double>> &eigenvalues,
366 std::vector<VectorType *> &eigenvectors,
367 const unsigned int n_eigenvalues);
368
369 /**
370 * Return the memory consumption of this class in bytes.
371 */
372 std::size_t
373 memory_consumption() const;
374
375 protected:
376 /**
377 * Reference to the object that controls convergence of the iterative
378 * solver.
379 */
380 SolverControl &solver_control;
381
382 /**
383 * Store a copy of the flags for this particular solver.
384 */
385 const AdditionalData additional_data;
386
387 // keep MPI communicator non-const as Arpack functions are not const either:
388
389 /**
390 * C++ MPI communicator.
391 */
392 MPI_Comm mpi_communicator;
393
394 /**
395 * Fortran MPI communicator.
396 */
397 MPI_Fint mpi_communicator_fortran;
398
399 // C++98 guarantees that the elements of a vector are stored contiguously
400
401 /**
402 * Length of the work array workl.
403 */
404 int lworkl;
405
406 /**
407 * Double precision work array of length lworkl
408 */
409 std::vector<double> workl;
410
411 /**
412 * Double precision work array of length 3*N
413 */
414 std::vector<double> workd;
415
416 /**
417 * Number of local degrees of freedom.
418 */
419 int nloc;
420
421 /**
422 * Number of Arnoldi basis vectors specified in additional_data
423 */
424 int ncv;
425
426
427 /**
428 * The leading dimension of the array v
429 */
430 int ldv;
431
432 /**
433 * Double precision vector of size ldv by NCV. Will contains the final set
434 * of Arnoldi basis vectors.
435 */
436 std::vector<double> v;
437
438 /**
439 * An auxiliary flag which is set to true when initial vector is provided.
440 */
441 bool initial_vector_provided;
442
443 /**
444 * The initial residual vector, possibly from a previous run. On output, it
445 * contains the final residual vector.
446 */
447 std::vector<double> resid;
448
449 /**
450 * The leading dimension of the array Z equal to nloc.
451 */
452 int ldz;
453
454 /**
455 * A vector of minimum size of nloc by NEV+1. Z contains the B-orthonormal
456 * Ritz vectors of the eigensystem A*z = lambda*B*z corresponding to the
457 * Ritz value approximations.
458 */
459 std::vector<double> z;
460
461 /**
462 * The size of the workev array.
463 */
464 int lworkev;
465
466 /**
467 * Double precision work array of dimension 3* NCV.
468 */
469 std::vector<double> workev;
470
471 /**
472 * A vector of dimension NCV.
473 */
474 std::vector<int> select;
475
476 /**
477 * Temporary vectors used between Arpack and deal.II
478 */
479 VectorType src, dst, tmp;
480
481 /**
482 * Indices of local degrees of freedom.
483 */
484 std::vector<types::global_dof_index> local_indices;
485
486 /**
487 * Real part of the shift
488 */
489 double sigmar;
490
491 /**
492 * Imaginary part of the shift
493 */
494 double sigmai;
495
496 private:
497 /**
498 * Initialize internal variables which depend on
499 * @p locally_owned_dofs.
500 *
501 * This function is called inside the reinit() functions
502 */
503 void
504 internal_reinit(const IndexSet &locally_owned_dofs);
505
506 /**
507 * PArpackExcInfoPdnaupds.
508 */
509 DeclException2(PArpackExcConvergedEigenvectors,
510 int,
511 int,
512 << arg1 << " eigenpairs were requested, but only " << arg2
513 << " converged");
514
515 DeclException2(PArpackExcInvalidNumberofEigenvalues,
516 int,
517 int,
518 << "Number of wanted eigenvalues " << arg1
519 << " is larger that the size of the matrix " << arg2);
520
521 DeclException2(PArpackExcInvalidEigenvectorSize,
522 int,
523 int,
524 << "Number of wanted eigenvalues " << arg1
525 << " is larger that the size of eigenvectors " << arg2);
526
527 DeclException2(
528 PArpackExcInvalidEigenvectorSizeNonsymmetric,
529 int,
530 int,
531 << "To store the real and complex parts of " << arg1
532 << " eigenvectors in real-valued vectors, their size (currently set to "
533 << arg2 << ") should be greater than or equal to " << arg1 + 1);
534
535 DeclException2(PArpackExcInvalidEigenvalueSize,
536 int,
537 int,
538 << "Number of wanted eigenvalues " << arg1
539 << " is larger that the size of eigenvalues " << arg2);
540
541 DeclException2(PArpackExcInvalidNumberofArnoldiVectors,
542 int,
543 int,
544 << "Number of Arnoldi vectors " << arg1
545 << " is larger that the size of the matrix " << arg2);
546
547 DeclException2(PArpackExcSmallNumberofArnoldiVectors,
548 int,
549 int,
550 << "Number of Arnoldi vectors " << arg1
551 << " is too small to obtain " << arg2 << " eigenvalues");
552
553 DeclException1(PArpackExcIdo,
554 int,
555 << "This ido " << arg1
556 << " is not supported. Check documentation of ARPACK");
557
558 DeclException1(PArpackExcMode,
559 int,
560 << "This mode " << arg1
561 << " is not supported. Check documentation of ARPACK");
562
563 DeclException1(PArpackExcInfoPdnaupd,
564 int,
565 << "Error with Pdnaupd, info " << arg1
566 << ". Check documentation of ARPACK");
567
568 DeclException1(PArpackExcInfoPdneupd,
569 int,
570 << "Error with Pdneupd, info " << arg1
571 << ". Check documentation of ARPACK");
572
573 DeclException1(PArpackExcInfoMaxIt,
574 int,
575 << "Maximum number " << arg1 << " of iterations reached.");
576
577 DeclException1(PArpackExcNoShifts,
578 int,
579 << "No shifts could be applied during implicit"
580 << " Arnoldi update, try increasing the number of"
581 << " Arnoldi vectors.");
582 };
583
584
585
586 template <typename VectorType>
587 std::size_t
588 PArpackSolver<VectorType>::memory_consumption() const
589 {
590 return MemoryConsumption::memory_consumption(double()) *
591 (workl.size() + workd.size() + v.size() + resid.size() + z.size() +
592 workev.size()) +
593 src.memory_consumption() + dst.memory_consumption() +
594 tmp.memory_consumption() +
595 MemoryConsumption::memory_consumption(types::global_dof_index()) *
596 local_indices.size();
597 }
598
599
600
601 template <typename VectorType>
602 PArpackSolver<VectorType>::AdditionalData::AdditionalData(
603 const unsigned int number_of_arnoldi_vectors,
604 const WhichEigenvalues eigenvalue_of_interest,
605 const bool symmetric,
606 const int mode)
607 : number_of_arnoldi_vectors(number_of_arnoldi_vectors)
608 , eigenvalue_of_interest(eigenvalue_of_interest)
609 , symmetric(symmetric)
610 , mode(mode)
611 {
612 // Check for possible options for symmetric problems
613 if (symmetric)
614 {
615 Assert(
616 eigenvalue_of_interest != largest_real_part,
617 ExcMessage(
618 "'largest real part' can only be used for non-symmetric problems!"));
619 Assert(
620 eigenvalue_of_interest != smallest_real_part,
621 ExcMessage(
622 "'smallest real part' can only be used for non-symmetric problems!"));
623 Assert(
624 eigenvalue_of_interest != largest_imaginary_part,
625 ExcMessage(
626 "'largest imaginary part' can only be used for non-symmetric problems!"));
627 Assert(
628 eigenvalue_of_interest != smallest_imaginary_part,
629 ExcMessage(
630 "'smallest imaginary part' can only be used for non-symmetric problems!"));
631 }
632 Assert(mode >= 1 && mode <= 3,
633 ExcMessage("Currently, only modes 1, 2 and 3 are supported."));
634 }
635
636
637
638 template <typename VectorType>
639 PArpackSolver<VectorType>::PArpackSolver(SolverControl &control,
640 const MPI_Comm mpi_communicator,
641 const AdditionalData &data)
642 : solver_control(control)
643 , additional_data(data)
644 , mpi_communicator(mpi_communicator)
645 , mpi_communicator_fortran(MPI_Comm_c2f(mpi_communicator))
646 , lworkl(0)
647 , nloc(0)
648 , ncv(0)
649 , ldv(0)
650 , initial_vector_provided(false)
651 , ldz(0)
652 , lworkev(0)
653 , sigmar(0.0)
654 , sigmai(0.0)
655 {}
656
657
658
659 template <typename VectorType>
660 void
661 PArpackSolver<VectorType>::set_shift(const std::complex<double> sigma)
662 {
663 sigmar = sigma.real();
664 sigmai = sigma.imag();
665 }
666
667
668
669 template <typename VectorType>
670 void
671 PArpackSolver<VectorType>::set_initial_vector(const VectorType &vec)
672 {
673 initial_vector_provided = true;
674 Assert(resid.size() == local_indices.size(),
675 ExcDimensionMismatch(resid.size(), local_indices.size()));
676 vec.extract_subvector_to(local_indices.begin(),
677 local_indices.end(),
678 resid.data());
679 }
680
681
682
683 template <typename VectorType>
684 void
685 PArpackSolver<VectorType>::internal_reinit(const IndexSet &locally_owned_dofs)
686 {
687 // store local indices to write to vectors
688 local_indices = locally_owned_dofs.get_index_vector();
689
690 // scalars
691 nloc = locally_owned_dofs.n_elements();
692 ncv = additional_data.number_of_arnoldi_vectors;
693
694 AssertDimension(local_indices.size(), nloc);
695
696 // vectors
697 ldv = nloc;
698 v.resize(ldv * ncv, 0.0);
699
700 resid.resize(nloc, 1.0);
701
702 // work arrays for ARPACK
703 workd.resize(3 * nloc, 0.0);
704
705 lworkl =
706 additional_data.symmetric ? ncv * ncv + 8 * ncv : 3 * ncv * ncv + 6 * ncv;
707 workl.resize(lworkl, 0.);
708
709 ldz = nloc;
710 z.resize(ldz * ncv, 0.); // TODO we actually need only ldz*nev
711
712 // WORKEV Double precision work array of dimension 3*NCV.
713 lworkev = additional_data.symmetric ? 0 /*not used in symmetric case*/
714 :
715 3 * ncv;
716 workev.resize(lworkev, 0.);
717
718 select.resize(ncv, 0);
719 }
720
721
722
723 template <typename VectorType>
724 void
725 PArpackSolver<VectorType>::reinit(const IndexSet &locally_owned_dofs)
726 {
727 internal_reinit(locally_owned_dofs);
728
729 // deal.II vectors:
730 src.reinit(locally_owned_dofs, mpi_communicator);
731 dst.reinit(locally_owned_dofs, mpi_communicator);
732 tmp.reinit(locally_owned_dofs, mpi_communicator);
733 }
734
735
736
737 template <typename VectorType>
738 void
739 PArpackSolver<VectorType>::reinit(const VectorType &distributed_vector)
740 {
741 internal_reinit(distributed_vector.locally_owned_elements());
742
743 // deal.II vectors:
744 src.reinit(distributed_vector);
745 dst.reinit(distributed_vector);
746 tmp.reinit(distributed_vector);
747 }
748
749
750
751 template <typename VectorType>
752 void
753 PArpackSolver<VectorType>::reinit(const IndexSet &locally_owned_dofs,
754 const std::vector<IndexSet> &partitioning)
755 {
756 internal_reinit(locally_owned_dofs);
757
758 // deal.II vectors:
759 src.reinit(partitioning, mpi_communicator);
760 dst.reinit(partitioning, mpi_communicator);
761 tmp.reinit(partitioning, mpi_communicator);
762 }
763
764
765
766 template <typename VectorType>
767 template <typename MatrixType1, typename MatrixType2, typename INVERSE>
768 void
769 PArpackSolver<VectorType>::solve(const MatrixType1 &A,
770 const MatrixType2 &B,
771 const INVERSE &inverse,
772 std::vector<std::complex<double>> &eigenvalues,
773 std::vector<VectorType> &eigenvectors,
774 const unsigned int n_eigenvalues)
775 {
776 std::vector<VectorType *> eigenvectors_ptr(eigenvectors.size());
777 for (unsigned int i = 0; i < eigenvectors.size(); ++i)
778 eigenvectors_ptr[i] = &eigenvectors[i];
779 solve(A, B, inverse, eigenvalues, eigenvectors_ptr, n_eigenvalues);
780 }
781
782
783
784 template <typename VectorType>
785 template <typename MatrixType1, typename MatrixType2, typename INVERSE>
786 void
787 PArpackSolver<VectorType>::solve(const MatrixType1 &system_matrix,
788 const MatrixType2 &mass_matrix,
789 const INVERSE &inverse,
790 std::vector<std::complex<double>> &eigenvalues,
791 std::vector<VectorType *> &eigenvectors,
792 const unsigned int n_eigenvalues)
793 {
794 if (additional_data.symmetric)
795 {
796 Assert(n_eigenvalues <= eigenvectors.size(),
797 PArpackExcInvalidEigenvectorSize(n_eigenvalues,
798 eigenvectors.size()));
799 }
800 else
801 Assert(n_eigenvalues + 1 <= eigenvectors.size(),
802 PArpackExcInvalidEigenvectorSizeNonsymmetric(n_eigenvalues,
803 eigenvectors.size()));
804
805 Assert(n_eigenvalues <= eigenvalues.size(),
806 PArpackExcInvalidEigenvalueSize(n_eigenvalues, eigenvalues.size()));
807
808
809 // use eigenvectors to get the problem size so that it is possible to
810 // employ LinearOperator for mass_matrix.
811 Assert(n_eigenvalues < eigenvectors[0]->size(),
812 PArpackExcInvalidNumberofEigenvalues(n_eigenvalues,
813 eigenvectors[0]->size()));
814
815 Assert(additional_data.number_of_arnoldi_vectors < eigenvectors[0]->size(),
816 PArpackExcInvalidNumberofArnoldiVectors(
817 additional_data.number_of_arnoldi_vectors, eigenvectors[0]->size()));
818
819 Assert(additional_data.number_of_arnoldi_vectors > 2 * n_eigenvalues + 1,
820 PArpackExcSmallNumberofArnoldiVectors(
821 additional_data.number_of_arnoldi_vectors, n_eigenvalues));
822
823 int mode = additional_data.mode;
824
825 // reverse communication parameter
826 // must be zero on the first call to pdnaupd
827 int ido = 0;
828
829 // 'G' generalized eigenvalue problem
830 // 'I' standard eigenvalue problem
831 char bmat[2];
832 bmat[0] = (mode == 1) ? 'I' : 'G';
833 bmat[1] = '\0';
834
835 // Specify the eigenvalues of interest, possible parameters:
836 // "LA" algebraically largest
837 // "SA" algebraically smallest
838 // "LM" largest magnitude
839 // "SM" smallest magnitude
840 // "LR" largest real part
841 // "SR" smallest real part
842 // "LI" largest imaginary part
843 // "SI" smallest imaginary part
844 // "BE" both ends of spectrum simultaneous
845 char which[3];
846 switch (additional_data.eigenvalue_of_interest)
847 {
848 case algebraically_largest:
849 std::strcpy(which, "LA");
850 break;
851 case algebraically_smallest:
852 std::strcpy(which, "SA");
853 break;
854 case largest_magnitude:
855 std::strcpy(which, "LM");
856 break;
857 case smallest_magnitude:
858 std::strcpy(which, "SM");
859 break;
860 case largest_real_part:
861 std::strcpy(which, "LR");
862 break;
863 case smallest_real_part:
864 std::strcpy(which, "SR");
865 break;
866 case largest_imaginary_part:
867 std::strcpy(which, "LI");
868 break;
869 case smallest_imaginary_part:
870 std::strcpy(which, "SI");
871 break;
872 case both_ends:
873 std::strcpy(which, "BE");
874 break;
875 }
876
877 // tolerance for ARPACK
878 double tol = control().tolerance();
879
880 // information to the routines
881 std::vector<int> iparam(11, 0);
882
883 iparam[0] = 1;
884 // shift strategy: exact shifts with respect to the current Hessenberg matrix
885 // H.
886
887 // maximum number of iterations
888 iparam[2] = control().max_steps();
889
890 // Parpack currently works only for NB = 1
891 iparam[3] = 1;
892
893 // Sets the mode of dsaupd:
894 // 1 is A*x=lambda*x, OP = A, B = I
895 // 2 is A*x = lambda*M*x, OP = inv[M]*A, B = M
896 // 3 is shift-invert mode, OP = inv[A-sigma*M]*M, B = M
897 // 4 is buckling mode,
898 // 5 is Cayley mode.
899
900 iparam[6] = mode;
901 std::vector<int> ipntr(14, 0);
902
903 // information out of the iteration
904 // If INFO .EQ. 0, a random initial residual vector is used.
905 // If INFO .NE. 0, RESID contains the initial residual vector,
906 // possibly from a previous run.
907 // Typical choices in this situation might be to use the final value
908 // of the starting vector from the previous eigenvalue calculation
909 int info = initial_vector_provided ? 1 : 0;
910
911 // Number of eigenvalues of OP to be computed. 0 < NEV < N.
912 int nev = n_eigenvalues;
913 int n_inside_arpack = nloc;
914
915 // IDO = 99: done
916 while (ido != 99)
917 {
918 // call of ARPACK pdnaupd routine
919 if (additional_data.symmetric)
920 pdsaupd_(&mpi_communicator_fortran,
921 &ido,
922 bmat,
923 &n_inside_arpack,
924 which,
925 &nev,
926 &tol,
927 resid.data(),
928 &ncv,
929 v.data(),
930 &ldv,
931 iparam.data(),
932 ipntr.data(),
933 workd.data(),
934 workl.data(),
935 &lworkl,
936 &info);
937 else
938 pdnaupd_(&mpi_communicator_fortran,
939 &ido,
940 bmat,
941 &n_inside_arpack,
942 which,
943 &nev,
944 &tol,
945 resid.data(),
946 &ncv,
947 v.data(),
948 &ldv,
949 iparam.data(),
950 ipntr.data(),
951 workd.data(),
952 workl.data(),
953 &lworkl,
954 &info);
955
956 AssertThrow(info == 0, PArpackExcInfoPdnaupd(info));
957
958 // if we converge, we shall not modify anything in work arrays!
959 if (ido == 99)
960 break;
961
962 // IPNTR(1) is the pointer into WORKD for X,
963 // IPNTR(2) is the pointer into WORKD for Y.
964 const int shift_x = ipntr[0] - 1;
965 const int shift_y = ipntr[1] - 1;
966 Assert(shift_x >= 0, dealii::ExcInternalError());
967 Assert(shift_x + nloc <= static_cast<int>(workd.size()),
968 dealii::ExcInternalError());
969 Assert(shift_y >= 0, dealii::ExcInternalError());
970 Assert(shift_y + nloc <= static_cast<int>(workd.size()),
971 dealii::ExcInternalError());
972
973 src = 0.;
974
975 // switch based on both ido and mode
976 if ((ido == -1) || (ido == 1 && mode < 3))
977 // compute Y = OP * X
978 {
979 src.add(nloc, local_indices.data(), workd.data() + shift_x);
980 src.compress(VectorOperation::add);
981
982 if (mode == 3)
983 // OP = inv[K - sigma*M]*M
984 {
985 mass_matrix.vmult(tmp, src);
986 inverse.vmult(dst, tmp);
987 }
988 else if (mode == 2)
989 // OP = inv[M]*K
990 {
991 system_matrix.vmult(tmp, src);
992 // store M*X in X
993 tmp.extract_subvector_to(local_indices.begin(),
994 local_indices.end(),
995 workd.data() + shift_x);
996 inverse.vmult(dst, tmp);
997 }
998 else if (mode == 1)
999 {
1000 system_matrix.vmult(dst, src);
1001 }
1002 else
1003 AssertThrow(false, PArpackExcMode(mode));
1004 }
1005 else if (ido == 1 && mode >= 3)
1006 // compute Y = OP * X for mode 3, 4 and 5, where
1007 // the vector B * X is already available in WORKD(ipntr(3)).
1008 {
1009 const int shift_b_x = ipntr[2] - 1;
1010 Assert(shift_b_x >= 0, dealii::ExcInternalError());
1011 Assert(shift_b_x + nloc <= static_cast<int>(workd.size()),
1012 dealii::ExcInternalError());
1013
1014 // B*X
1015 src.add(nloc, local_indices.data(), workd.data() + shift_b_x);
1016 src.compress(VectorOperation::add);
1017
1018 // solving linear system
1019 Assert(mode == 3, ExcNotImplemented());
1020 inverse.vmult(dst, src);
1021 }
1022 else if (ido == 2)
1023 // compute Y = B * X
1024 {
1025 src.add(nloc, local_indices.data(), workd.data() + shift_x);
1026 src.compress(VectorOperation::add);
1027
1028 // Multiplication with mass matrix M
1029 if (mode == 1)
1030 {
1031 dst = src;
1032 }
1033 else
1034 // mode 2,3 and 5 have B=M
1035 {
1036 mass_matrix.vmult(dst, src);
1037 }
1038 }
1039 else
1040 AssertThrow(false, PArpackExcIdo(ido));
1041 // Note: IDO = 3 does not appear to be required for currently
1042 // implemented modes
1043
1044 // store the result
1045 dst.extract_subvector_to(local_indices.begin(),
1046 local_indices.end(),
1047 workd.data() + shift_y);
1048 } // end of pd*aupd_ loop
1049
1050 // 1 - compute eigenvectors,
1051 // 0 - only eigenvalues
1052 int rvec = 1;
1053
1054 // which eigenvectors
1055 char howmany[4] = "All";
1056
1057 std::vector<double> eigenvalues_real(n_eigenvalues + 1, 0.);
1058 std::vector<double> eigenvalues_im(n_eigenvalues + 1, 0.);
1059
1060 // call of ARPACK pdneupd routine
1061 if (additional_data.symmetric)
1062 pdseupd_(&mpi_communicator_fortran,
1063 &rvec,
1064 howmany,
1065 select.data(),
1066 eigenvalues_real.data(),
1067 z.data(),
1068 &ldz,
1069 &sigmar,
1070 bmat,
1071 &n_inside_arpack,
1072 which,
1073 &nev,
1074 &tol,
1075 resid.data(),
1076 &ncv,
1077 v.data(),
1078 &ldv,
1079 iparam.data(),
1080 ipntr.data(),
1081 workd.data(),
1082 workl.data(),
1083 &lworkl,
1084 &info);
1085 else
1086 pdneupd_(&mpi_communicator_fortran,
1087 &rvec,
1088 howmany,
1089 select.data(),
1090 eigenvalues_real.data(),
1091 eigenvalues_im.data(),
1092 v.data(),
1093 &ldz,
1094 &sigmar,
1095 &sigmai,
1096 workev.data(),
1097 bmat,
1098 &n_inside_arpack,
1099 which,
1100 &nev,
1101 &tol,
1102 resid.data(),
1103 &ncv,
1104 v.data(),
1105 &ldv,
1106 iparam.data(),
1107 ipntr.data(),
1108 workd.data(),
1109 workl.data(),
1110 &lworkl,
1111 &info);
1112
1113 if (info == 1)
1114 {
1115 AssertThrow(false, PArpackExcInfoMaxIt(control().max_steps()));
1116 }
1117 else if (info == 3)
1118 {
1119 AssertThrow(false, PArpackExcNoShifts(1));
1120 }
1121 else if (info != 0)
1122 {
1123 AssertThrow(false, PArpackExcInfoPdneupd(info));
1124 }
1125
1126 for (int i = 0; i < nev; ++i)
1127 {
1128 (*eigenvectors[i]) = 0.0;
1129 AssertIndexRange(i * nloc + nloc, v.size() + 1);
1130
1131 eigenvectors[i]->add(nloc, local_indices.data(), &v[i * nloc]);
1132 eigenvectors[i]->compress(VectorOperation::add);
1133 }
1134
1135 for (size_type i = 0; i < n_eigenvalues; ++i)
1136 eigenvalues[i] =
1137 std::complex<double>(eigenvalues_real[i], eigenvalues_im[i]);
1138
1139 // Throw an error if the solver did not converge.
1140 AssertThrow(iparam[4] >= static_cast<int>(n_eigenvalues),
1141 PArpackExcConvergedEigenvectors(n_eigenvalues, iparam[4]));
1142
1143 // both PDNAUPD and PDSAUPD compute eigenpairs of inv[A - sigma*M]*M
1144 // with respect to a semi-inner product defined by M.
1145
1146 // resid likely contains residual with respect to M-norm.
1147 {
1148 tmp = 0.0;
1149 tmp.add(nloc, local_indices.data(), resid.data());
1150 tmp.compress(VectorOperation::add);
1151 solver_control.check(iparam[2], tmp.l2_norm());
1152 }
1153 }
1154
1155
1156
1157 template <typename VectorType>
1158 SolverControl &
1159 PArpackSolver<VectorType>::control() const
1160 {
1161 return solver_control;
1162 }
1163
1164 DEAL_II_NAMESPACE_CLOSE
1165
1166
1167 #else
1168
1169 // Make sure the scripts that create the C++20 module input files have
1170 // something to latch on if the preprocessor #ifdef above would
1171 // otherwise lead to an empty content of the file.
1172 DEAL_II_NAMESPACE_OPEN
1173 DEAL_II_NAMESPACE_CLOSE
1174
1175 #endif
1176 #endif