/usr/include/deal.II/lac/solver_gmres.h is in libdeal.ii-dev 8.4.2-2+b1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 | // ---------------------------------------------------------------------
//
// Copyright (C) 1998 - 2016 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE at
// the top level of the deal.II distribution.
//
// ---------------------------------------------------------------------
#ifndef dealii__solver_gmres_h
#define dealii__solver_gmres_h
#include <deal.II/base/config.h>
#include <deal.II/base/subscriptor.h>
#include <deal.II/base/logstream.h>
#include <deal.II/lac/householder.h>
#include <deal.II/lac/solver.h>
#include <deal.II/lac/solver_control.h>
#include <deal.II/lac/full_matrix.h>
#include <deal.II/lac/lapack_full_matrix.h>
#include <deal.II/lac/vector.h>
#include <vector>
#include <cmath>
#include <algorithm>
DEAL_II_NAMESPACE_OPEN
/*!@addtogroup Solvers */
/*@{*/
namespace internal
{
/**
* A namespace for a helper class to the GMRES solver.
*/
namespace SolverGMRES
{
/**
* Class to hold temporary vectors. This class automatically allocates a
* new vector, once it is needed.
*
* A future version should also be able to shift through vectors
* automatically, avoiding restart.
*/
template <typename VectorType>
class TmpVectors
{
public:
/**
* Constructor. Prepares an array of @p VectorType of length @p
* max_size.
*/
TmpVectors(const unsigned int max_size,
VectorMemory<VectorType> &vmem);
/**
* Delete all allocated vectors.
*/
~TmpVectors();
/**
* Get vector number @p i. If this vector was unused before, an error
* occurs.
*/
VectorType &operator[] (const unsigned int i) const;
/**
* Get vector number @p i. Allocate it if necessary.
*
* If a vector must be allocated, @p temp is used to reinit it to the
* proper dimensions.
*/
VectorType &operator() (const unsigned int i,
const VectorType &temp);
private:
/**
* Pool were vectors are obtained from.
*/
VectorMemory<VectorType> &mem;
/**
* Field for storing the vectors.
*/
std::vector<VectorType *> data;
/**
* Offset of the first vector. This is for later when vector rotation
* will be implemented.
*/
unsigned int offset;
};
}
}
/**
* Implementation of the Restarted Preconditioned Direct Generalized Minimal
* Residual Method. The stopping criterion is the norm of the residual.
*
* The AdditionalData structure contains the number of temporary vectors used.
* The size of the Arnoldi basis is this number minus three. Additionally, it
* allows you to choose between right or left preconditioning. The default is
* left preconditioning. Finally it includes a flag indicating whether or not
* the default residual is used as stopping criterion.
*
*
* <h3>Left versus right preconditioning</h3>
*
* @p AdditionalData allows you to choose between left and right
* preconditioning. As expected, this switches between solving for the systems
* <i>P<sup>-1</sup>A</i> and <i>AP<sup>-1</sup></i>, respectively.
*
* A second consequence is the type of residual which is used to measure
* convergence. With left preconditioning, this is the <b>preconditioned</b>
* residual, while with right preconditioning, it is the residual of the
* unpreconditioned system.
*
* Optionally, this behavior can be overridden by using the flag
* AdditionalData::use_default_residual. A <tt>true</tt> value refers to the
* behavior described in the previous paragraph, while <tt>false</tt> reverts
* it. Be aware though that additional residuals have to be computed in this
* case, impeding the overall performance of the solver.
*
*
* <h3>The size of the Arnoldi basis</h3>
*
* The maximal basis size is controlled by AdditionalData::max_n_tmp_vectors,
* and it is this number minus 2. If the number of iteration steps exceeds
* this number, all basis vectors are discarded and the iteration starts anew
* from the approximation obtained so far.
*
* Note that the minimizing property of GMRes only pertains to the Krylov
* space spanned by the Arnoldi basis. Therefore, restarted GMRes is
* <b>not</b> minimizing anymore. The choice of the basis length is a trade-
* off between memory consumption and convergence speed, since a longer basis
* means minimization over a larger space.
*
* For the requirements on matrices and vectors in order to work with this
* class, see the documentation of the Solver base class.
*
*
* <h3>Observing the progress of linear solver iterations</h3>
*
* The solve() function of this class uses the mechanism described in the
* Solver base class to determine convergence. This mechanism can also be used
* to observe the progress of the iteration.
*
*
* <h3>Eigenvalue and condition number estimates</h3>
*
* This class can estimate eigenvalues and condition number during the
* solution process. This is done by creating the Hessenberg matrix during the
* inner iterations. The eigenvalues are estimated as the eigenvalues of the
* Hessenberg matrix and the condition number is estimated as the ratio of the
* largest and smallest singular value of the Hessenberg matrix. The estimates
* can be obtained by connecting a function as a slot using @p
* connect_condition_number_slot and @p connect_eigenvalues_slot. These slots
* will then be called from the solver with the estimates as argument.
*
*
* @author Wolfgang Bangerth, Guido Kanschat, Ralf Hartmann.
*/
template <class VectorType = Vector<double> >
class SolverGMRES : public Solver<VectorType>
{
public:
/**
* Standardized data struct to pipe additional data to the solver.
*/
struct AdditionalData
{
/**
* Constructor. By default, set the number of temporary vectors to 30,
* i.e. do a restart every 28 iterations. Also set preconditioning from
* left, the residual of the stopping criterion to the default residual,
* and re-orthogonalization only if necessary.
*/
explicit
AdditionalData (const unsigned int max_n_tmp_vectors = 30,
const bool right_preconditioning = false,
const bool use_default_residual = true,
const bool force_re_orthogonalization = false);
/**
* Constructor.
* @deprecated To obtain the estimated eigenvalues instead use:
* connect_eigenvalues_slot
*/
AdditionalData (const unsigned int max_n_tmp_vectors,
const bool right_preconditioning,
const bool use_default_residual,
const bool force_re_orthogonalization,
const bool compute_eigenvalues) DEAL_II_DEPRECATED;
/**
* Maximum number of temporary vectors. This parameter controls the size
* of the Arnoldi basis, which for historical reasons is
* #max_n_tmp_vectors-2.
*/
unsigned int max_n_tmp_vectors;
/**
* Flag for right preconditioning.
*
* @note Change between left and right preconditioning will also change
* the way residuals are evaluated. See the corresponding section in the
* SolverGMRES.
*/
bool right_preconditioning;
/**
* Flag for the default residual that is used to measure convergence.
*/
bool use_default_residual;
/**
* Flag to force re-orthogonalization of orthonormal basis in every step.
* If set to false, the solver automatically checks for loss of
* orthogonality every 5 iterations and enables re-orthogonalization only
* if necessary.
*/
bool force_re_orthogonalization;
/**
* Compute all eigenvalues of the Hessenberg matrix generated while
* solving, i.e., the projected system matrix. This gives an approximation
* of the eigenvalues of the (preconditioned) system matrix. Since the
* Hessenberg matrix is thrown away at restart, the eigenvalues are
* printed for every 30 iterations.
*
* @note Requires LAPACK support.
*/
bool compute_eigenvalues;
};
/**
* Constructor.
*/
SolverGMRES (SolverControl &cn,
VectorMemory<VectorType> &mem,
const AdditionalData &data=AdditionalData());
/**
* Constructor. Use an object of type GrowingVectorMemory as a default to
* allocate memory.
*/
SolverGMRES (SolverControl &cn,
const AdditionalData &data=AdditionalData());
/**
* Solve the linear system $Ax=b$ for x.
*/
template<typename MatrixType, typename PreconditionerType>
void
solve (const MatrixType &A,
VectorType &x,
const VectorType &b,
const PreconditionerType &precondition);
/**
* Connect a slot to retrieve the estimated condition number. Called on each
* outer iteration if every_iteration=true, otherwise called once when
* iterations are ended (i.e., either because convergence has been achieved,
* or because divergence has been detected).
*/
boost::signals2::connection
connect_condition_number_slot(const std_cxx11::function<void (double)> &slot,
const bool every_iteration=false);
/**
* Connect a slot to retrieve the estimated eigenvalues. Called on each
* outer iteration if every_iteration=true, otherwise called once when
* iterations are ended (i.e., either because convergence has been achieved,
* or because divergence has been detected).
*/
boost::signals2::connection
connect_eigenvalues_slot(
const std_cxx11::function<void (const std::vector<std::complex<double> > &)> &slot,
const bool every_iteration=false);
DeclException1 (ExcTooFewTmpVectors,
int,
<< "The number of temporary vectors you gave ("
<< arg1 << ") is too small. It should be at least 10 for "
<< "any results, and much more for reasonable ones.");
protected:
/**
* Includes the maximum number of tmp vectors.
*/
AdditionalData additional_data;
/**
* Signal used to retrieve the estimated condition number. Called once when
* all iterations are ended.
*/
boost::signals2::signal<void (double)> condition_number_signal;
/**
* Signal used to retrieve the estimated condition numbers. Called on each
* outer iteration.
*/
boost::signals2::signal<void (double)> all_condition_numbers_signal;
/**
* Signal used to retrieve the estimated eigenvalues. Called once when all
* iterations are ended.
*/
boost::signals2::signal<void (const std::vector<std::complex<double> > &)> eigenvalues_signal;
/**
* Signal used to retrieve the estimated eigenvalues. Called on each outer
* iteration.
*/
boost::signals2::signal<void (const std::vector<std::complex<double> > &)> all_eigenvalues_signal;
/**
* Implementation of the computation of the norm of the residual.
*/
virtual double criterion();
/**
* Transformation of an upper Hessenberg matrix into tridiagonal structure
* by givens rotation of the last column
*/
void givens_rotation (Vector<double> &h, Vector<double> &b,
Vector<double> &ci, Vector<double> &si,
int col) const;
/**
* Orthogonalize the vector @p vv against the @p dim (orthogonal) vectors
* given by the first argument using the modified Gram-Schmidt algorithm.
* The factors used for orthogonalization are stored in @p h. The boolean @p
* re_orthogonalize specifies whether the modified Gram-Schmidt algorithm
* should be applied twice. The algorithm checks loss of orthogonality in
* the procedure every fifth step and sets the flag to true in that case.
* All subsequent iterations use re-orthogonalization.
*/
static double
modified_gram_schmidt (const internal::SolverGMRES::TmpVectors<VectorType> &orthogonal_vectors,
const unsigned int dim,
const unsigned int accumulated_iterations,
VectorType &vv,
Vector<double> &h,
bool &re_orthogonalize);
/**
* Estimates the eigenvalues from the Hessenberg matrix, H_orig, generated
* during the inner iterations. Uses these estimate to compute the condition
* number. Calls the signals eigenvalues_signal and cond_signal with these
* estimates as arguments. Outputs the eigenvalues to deallog if
* log_eigenvalues is true.
*/
static void
compute_eigs_and_cond(
const FullMatrix<double> &H_orig ,
const unsigned int dim,
const boost::signals2::signal<void (const std::vector<std::complex<double> > &)> &eigenvalues_signal,
const boost::signals2::signal<void(double)> &cond_signal,
const bool log_eigenvalues);
/**
* Projected system matrix
*/
FullMatrix<double> H;
/**
* Auxiliary matrix for inverting @p H
*/
FullMatrix<double> H1;
private:
/**
* No copy constructor.
*/
SolverGMRES (const SolverGMRES<VectorType> &);
};
/**
* Implementation of the Generalized minimal residual method with flexible
* preconditioning method.
*
* This version of the GMRES method allows for the use of a different
* preconditioner in each iteration step. Therefore, it is also more robust
* with respect to inaccurate evaluation of the preconditioner. An important
* application is also the use of a Krylov space method inside the
* preconditioner. As opposed to SolverGMRES which allows one to choose
* between left and right preconditioning, this solver always applies the
* preconditioner from the right.
*
* FGMRES needs two vectors in each iteration steps yielding a total of
* <tt>2*SolverFGMRES::AdditionalData::max_basis_size+1</tt> auxiliary
* vectors.
*
* Caveat: Documentation of this class is not up to date. There are also a few
* parameters of GMRES we would like to introduce here.
*
* @author Guido Kanschat, 2003
*/
template <class VectorType = Vector<double> >
class SolverFGMRES : public Solver<VectorType>
{
public:
/**
* Standardized data struct to pipe additional data to the solver.
*/
struct AdditionalData
{
/**
* Constructor. By default, set the maximum basis size to 30.
*/
explicit
AdditionalData(const unsigned int max_basis_size = 30,
const bool /*use_default_residual*/ = true)
:
max_basis_size(max_basis_size)
{}
/**
* Maximum number of tmp vectors.
*/
unsigned int max_basis_size;
};
/**
* Constructor.
*/
SolverFGMRES (SolverControl &cn,
VectorMemory<VectorType> &mem,
const AdditionalData &data=AdditionalData());
/**
* Constructor. Use an object of type GrowingVectorMemory as a default to
* allocate memory.
*/
SolverFGMRES (SolverControl &cn,
const AdditionalData &data=AdditionalData());
/**
* Solve the linear system $Ax=b$ for x.
*/
template<typename MatrixType, typename PreconditionerType>
void
solve (const MatrixType &A,
VectorType &x,
const VectorType &b,
const PreconditionerType &precondition);
private:
/**
* Additional flags.
*/
AdditionalData additional_data;
/**
* Projected system matrix
*/
FullMatrix<double> H;
/**
* Auxiliary matrix for inverting @p H
*/
FullMatrix<double> H1;
};
/*@}*/
/* --------------------- Inline and template functions ------------------- */
#ifndef DOXYGEN
namespace internal
{
namespace SolverGMRES
{
template <class VectorType>
inline
TmpVectors<VectorType>::
TmpVectors (const unsigned int max_size,
VectorMemory<VectorType> &vmem)
:
mem(vmem),
data (max_size, 0),
offset(0)
{}
template <class VectorType>
inline
TmpVectors<VectorType>::~TmpVectors ()
{
for (typename std::vector<VectorType *>::iterator v = data.begin();
v != data.end(); ++v)
if (*v != 0)
mem.free(*v);
}
template <class VectorType>
inline VectorType &
TmpVectors<VectorType>::operator[] (const unsigned int i) const
{
Assert (i+offset<data.size(),
ExcIndexRange(i, -offset, data.size()-offset));
Assert (data[i-offset] != 0, ExcNotInitialized());
return *data[i-offset];
}
template <class VectorType>
inline VectorType &
TmpVectors<VectorType>::operator() (const unsigned int i,
const VectorType &temp)
{
Assert (i+offset<data.size(),
ExcIndexRange(i,-offset, data.size()-offset));
if (data[i-offset] == 0)
{
data[i-offset] = mem.alloc();
data[i-offset]->reinit(temp);
}
return *data[i-offset];
}
// A comparator for better printing eigenvalues
inline
bool complex_less_pred(const std::complex<double> &x,
const std::complex<double> &y)
{
return x.real() < y.real() || (x.real() == y.real() && x.imag() < y.imag());
}
}
}
template <class VectorType>
inline
SolverGMRES<VectorType>::AdditionalData::
AdditionalData (const unsigned int max_n_tmp_vectors,
const bool right_preconditioning,
const bool use_default_residual,
const bool force_re_orthogonalization)
:
max_n_tmp_vectors(max_n_tmp_vectors),
right_preconditioning(right_preconditioning),
use_default_residual(use_default_residual),
force_re_orthogonalization(force_re_orthogonalization),
compute_eigenvalues(false)
{}
template <class VectorType>
inline
SolverGMRES<VectorType>::AdditionalData::
AdditionalData (const unsigned int max_n_tmp_vectors,
const bool right_preconditioning,
const bool use_default_residual,
const bool force_re_orthogonalization,
const bool compute_eigenvalues)
:
max_n_tmp_vectors(max_n_tmp_vectors),
right_preconditioning(right_preconditioning),
use_default_residual(use_default_residual),
force_re_orthogonalization(force_re_orthogonalization),
compute_eigenvalues(compute_eigenvalues)
{}
template <class VectorType>
SolverGMRES<VectorType>::SolverGMRES (SolverControl &cn,
VectorMemory<VectorType> &mem,
const AdditionalData &data)
:
Solver<VectorType> (cn,mem),
additional_data(data)
{}
template <class VectorType>
SolverGMRES<VectorType>::SolverGMRES (SolverControl &cn,
const AdditionalData &data) :
Solver<VectorType> (cn),
additional_data(data)
{}
template <class VectorType>
inline
void
SolverGMRES<VectorType>::givens_rotation (Vector<double> &h,
Vector<double> &b,
Vector<double> &ci,
Vector<double> &si,
int col) const
{
for (int i=0 ; i<col ; i++)
{
const double s = si(i);
const double c = ci(i);
const double dummy = h(i);
h(i) = c*dummy + s*h(i+1);
h(i+1) = -s*dummy + c*h(i+1);
};
const double r = 1./std::sqrt(h(col)*h(col) + h(col+1)*h(col+1));
si(col) = h(col+1) *r;
ci(col) = h(col) *r;
h(col) = ci(col)*h(col) + si(col)*h(col+1);
b(col+1)= -si(col)*b(col);
b(col) *= ci(col);
}
template <class VectorType>
inline
double
SolverGMRES<VectorType>::modified_gram_schmidt
(const internal::SolverGMRES::TmpVectors<VectorType> &orthogonal_vectors,
const unsigned int dim,
const unsigned int accumulated_iterations,
VectorType &vv,
Vector<double> &h,
bool &re_orthogonalize)
{
Assert(dim > 0, ExcInternalError());
const unsigned int inner_iteration = dim - 1;
// need initial norm for detection of re-orthogonalization, see below
double norm_vv_start = 0;
if (re_orthogonalize == false && inner_iteration % 5 == 4)
norm_vv_start = vv.l2_norm();
// Orthogonalization
h(0) = vv * orthogonal_vectors[0];
for (unsigned int i=1 ; i<dim ; ++i)
h(i) = vv.add_and_dot(-h(i-1), orthogonal_vectors[i-1], orthogonal_vectors[i]);
double norm_vv = std::sqrt(vv.add_and_dot(-h(dim-1), orthogonal_vectors[dim-1], vv));
// Re-orthogonalization if loss of orthogonality detected. For the test, use
// a strategy discussed in C. T. Kelley, Iterative Methods for Linear and
// Nonlinear Equations, SIAM, Philadelphia, 1995: Compare the norm of vv
// after orthogonalization with its norm when starting the
// orthogonalization. If vv became very small (here: less than the square
// root of the machine precision times 10), it is almost in the span of the
// previous vectors, which indicates loss of precision.
if (re_orthogonalize == false && inner_iteration % 5 == 4)
{
if (norm_vv > 10. * norm_vv_start *
std::sqrt(std::numeric_limits<typename VectorType::value_type>::epsilon()))
return norm_vv;
else
{
re_orthogonalize = true;
deallog << "Re-orthogonalization enabled at step "
<< accumulated_iterations << std::endl;
}
}
if (re_orthogonalize == true)
{
double htmp = vv * orthogonal_vectors[0];
h(0) += htmp;
for (unsigned int i=1 ; i<dim ; ++i)
{
htmp = vv.add_and_dot(-htmp, orthogonal_vectors[i-1], orthogonal_vectors[i]);
h(i) += htmp;
}
norm_vv = std::sqrt(vv.add_and_dot(-htmp, orthogonal_vectors[dim-1], vv));
}
return norm_vv;
}
template<class VectorType>
inline void
SolverGMRES<VectorType>::compute_eigs_and_cond
(const FullMatrix<double> &H_orig,
const unsigned int dim,
const boost::signals2::signal<void (const std::vector<std::complex<double> > &)> &eigenvalues_signal,
const boost::signals2::signal<void (double)> &cond_signal,
const bool log_eigenvalues)
{
//Avoid copying the Hessenberg matrix if it isn't needed.
if (!eigenvalues_signal.empty() || !cond_signal.empty() || log_eigenvalues )
{
LAPACKFullMatrix<double> mat(dim,dim);
for (unsigned int i=0; i<dim; ++i)
for (unsigned int j=0; j<dim; ++j)
mat(i,j) = H_orig(i,j);
//Avoid computing eigenvalues if they are not needed.
if (!eigenvalues_signal.empty() || log_eigenvalues )
{
//Copy mat so that we can compute svd below. Necessary since
//compute_eigenvalues will leave mat in state LAPACKSupport::unusable.
LAPACKFullMatrix<double> mat_eig(mat);
mat_eig.compute_eigenvalues();
std::vector<std::complex<double> > eigenvalues(dim);
for (unsigned int i=0; i<mat_eig.n(); ++i)
eigenvalues[i] = mat_eig.eigenvalue(i);
//Sort eigenvalues for nicer output.
std::sort(eigenvalues.begin(), eigenvalues.end(),
internal::SolverGMRES::complex_less_pred);
eigenvalues_signal(eigenvalues);
if (log_eigenvalues)
{
deallog << "Eigenvalue estimate: ";
for (unsigned int i=0; i<mat_eig.n(); ++i)
deallog << ' ' << eigenvalues[i];
deallog << std::endl;
}
}
//Calculate condition number, avoid calculating the svd if a slot
//isn't connected. Need at least a 2-by-2 matrix to do the estimate.
if (!cond_signal.empty() && (mat.n()>1))
{
mat.compute_svd();
double condition_number=mat.singular_value(0)/mat.singular_value(mat.n()-1);
cond_signal(condition_number);
}
}
}
template<class VectorType>
template<typename MatrixType, typename PreconditionerType>
void
SolverGMRES<VectorType>::solve (const MatrixType &A,
VectorType &x,
const VectorType &b,
const PreconditionerType &precondition)
{
// this code was written a very long time ago by people not associated with
// deal.II. we don't make any guarantees to its optimality or that it even
// works as expected...
//TODO:[?] Check, why there are two different start residuals.
//TODO:[GK] Make sure the parameter in the constructor means maximum basis size
deallog.push("GMRES");
const unsigned int n_tmp_vectors = additional_data.max_n_tmp_vectors;
// Generate an object where basis vectors are stored.
internal::SolverGMRES::TmpVectors<VectorType> tmp_vectors (n_tmp_vectors, this->memory);
// number of the present iteration; this
// number is not reset to zero upon a
// restart
unsigned int accumulated_iterations = 0;
const bool do_eigenvalues=
!condition_number_signal.empty()
|!all_condition_numbers_signal.empty()
|!eigenvalues_signal.empty()
|!all_eigenvalues_signal.empty()
|additional_data.compute_eigenvalues;
// for eigenvalue computation, need to collect the Hessenberg matrix (before
// applying Givens rotations)
FullMatrix<double> H_orig;
if (do_eigenvalues)
H_orig.reinit(n_tmp_vectors, n_tmp_vectors-1);
// matrix used for the orthogonalization process later
H.reinit(n_tmp_vectors, n_tmp_vectors-1);
// some additional vectors, also used in the orthogonalization
dealii::Vector<double>
gamma(n_tmp_vectors),
ci (n_tmp_vectors-1),
si (n_tmp_vectors-1),
h (n_tmp_vectors-1);
unsigned int dim = 0;
SolverControl::State iteration_state = SolverControl::iterate;
double last_res = -std::numeric_limits<double>::max();
// switch to determine whether we want a left or a right preconditioner. at
// present, left is default, but both ways are implemented
const bool left_precondition = !additional_data.right_preconditioning;
// Per default the left preconditioned GMRes uses the preconditioned
// residual and the right preconditioned GMRes uses the unpreconditioned
// residual as stopping criterion.
const bool use_default_residual = additional_data.use_default_residual;
// define two aliases
VectorType &v = tmp_vectors(0, x);
VectorType &p = tmp_vectors(n_tmp_vectors-1, x);
// Following vectors are needed
// when not the default residuals
// are used as stopping criterion
VectorType *r=0;
VectorType *x_=0;
dealii::Vector<double> *gamma_=0;
if (!use_default_residual)
{
r=this->memory.alloc();
x_=this->memory.alloc();
r->reinit(x);
x_->reinit(x);
gamma_ = new dealii::Vector<double> (gamma.size());
}
bool re_orthogonalize = additional_data.force_re_orthogonalization;
///////////////////////////////////////////////////////////////////////////
// outer iteration: loop until we either reach convergence or the maximum
// number of iterations is exceeded. each cycle of this loop amounts to one
// restart
do
{
// reset this vector to the right size
h.reinit (n_tmp_vectors-1);
if (left_precondition)
{
A.vmult(p,x);
p.sadd(-1.,1.,b);
precondition.vmult(v,p);
}
else
{
A.vmult(v,x);
v.sadd(-1.,1.,b);
};
double rho = v.l2_norm();
// check the residual here as well since it may be that we got the exact
// (or an almost exact) solution vector at the outset. if we wouldn't
// check here, the next scaling operation would produce garbage
if (use_default_residual)
{
last_res = rho;
iteration_state = this->iteration_status (accumulated_iterations, rho, x);
if (iteration_state != SolverControl::iterate)
break;
}
else
{
deallog << "default_res=" << rho << std::endl;
if (left_precondition)
{
A.vmult(*r,x);
r->sadd(-1.,1.,b);
}
else
precondition.vmult(*r,v);
double res = r->l2_norm();
last_res = res;
iteration_state = this->iteration_status (accumulated_iterations, res, x);
if (iteration_state != SolverControl::iterate)
break;
}
gamma(0) = rho;
v *= 1./rho;
// inner iteration doing at most as many steps as there are temporary
// vectors. the number of steps actually been done is propagated outside
// through the @p dim variable
for (unsigned int inner_iteration=0;
((inner_iteration < n_tmp_vectors-2)
&&
(iteration_state==SolverControl::iterate));
++inner_iteration)
{
++accumulated_iterations;
// yet another alias
VectorType &vv = tmp_vectors(inner_iteration+1, x);
if (left_precondition)
{
A.vmult(p, tmp_vectors[inner_iteration]);
precondition.vmult(vv,p);
}
else
{
precondition.vmult(p, tmp_vectors[inner_iteration]);
A.vmult(vv,p);
}
dim = inner_iteration+1;
const double s = modified_gram_schmidt(tmp_vectors, dim,
accumulated_iterations,
vv, h, re_orthogonalize);
h(inner_iteration+1) = s;
//s=0 is a lucky breakdown, the solver will reach convergence,
//but we must not divide by zero here.
if (s != 0)
vv *= 1./s;
// for eigenvalues, get the resulting coefficients from the
// orthogonalization process
if (do_eigenvalues)
for (unsigned int i=0; i<dim+1; ++i)
H_orig(i,inner_iteration) = h(i);
// Transformation into tridiagonal structure
givens_rotation(h,gamma,ci,si,inner_iteration);
// append vector on matrix
for (unsigned int i=0; i<dim; ++i)
H(i,inner_iteration) = h(i);
// default residual
rho = std::fabs(gamma(dim));
if (use_default_residual)
{
last_res = rho;
iteration_state = this->iteration_status (accumulated_iterations, rho, x);
}
else
{
deallog << "default_res=" << rho << std::endl;
dealii::Vector<double> h_(dim);
*x_=x;
*gamma_=gamma;
H1.reinit(dim+1,dim);
for (unsigned int i=0; i<dim+1; ++i)
for (unsigned int j=0; j<dim; ++j)
H1(i,j) = H(i,j);
H1.backward(h_,*gamma_);
if (left_precondition)
for (unsigned int i=0 ; i<dim; ++i)
x_->add(h_(i), tmp_vectors[i]);
else
{
p = 0.;
for (unsigned int i=0; i<dim; ++i)
p.add(h_(i), tmp_vectors[i]);
precondition.vmult(*r,p);
x_->add(1.,*r);
};
A.vmult(*r,*x_);
r->sadd(-1.,1.,b);
// Now *r contains the unpreconditioned residual!!
if (left_precondition)
{
const double res=r->l2_norm();
last_res = res;
iteration_state = this->iteration_status (accumulated_iterations, res, x);
}
else
{
precondition.vmult(*x_, *r);
const double preconditioned_res=x_->l2_norm();
last_res = preconditioned_res;
iteration_state = this->iteration_status (accumulated_iterations,
preconditioned_res, x);
}
}
};
// end of inner iteration. now calculate the solution from the temporary
// vectors
h.reinit(dim);
H1.reinit(dim+1,dim);
for (unsigned int i=0; i<dim+1; ++i)
for (unsigned int j=0; j<dim; ++j)
H1(i,j) = H(i,j);
compute_eigs_and_cond(H_orig,dim,all_eigenvalues_signal,
all_condition_numbers_signal,
additional_data.compute_eigenvalues);
H1.backward(h,gamma);
if (left_precondition)
for (unsigned int i=0 ; i<dim; ++i)
x.add(h(i), tmp_vectors[i]);
else
{
p = 0.;
for (unsigned int i=0; i<dim; ++i)
p.add(h(i), tmp_vectors[i]);
precondition.vmult(v,p);
x.add(1.,v);
};
// end of outer iteration. restart if no convergence and the number of
// iterations is not exceeded
}
while (iteration_state == SolverControl::iterate);
compute_eigs_and_cond(H_orig,dim,eigenvalues_signal,condition_number_signal,
false);
if (!use_default_residual)
{
this->memory.free(r);
this->memory.free(x_);
delete gamma_;
}
deallog.pop();
// in case of failure: throw exception
AssertThrow(iteration_state == SolverControl::success,
SolverControl::NoConvergence (accumulated_iterations,
last_res));
}
template<class VectorType>
boost::signals2::connection
SolverGMRES<VectorType>::connect_condition_number_slot
(const std_cxx11::function<void(double)> &slot,
const bool every_iteration)
{
if (every_iteration)
{
return all_condition_numbers_signal.connect(slot);
}
else
{
return condition_number_signal.connect(slot);
}
}
template<class VectorType>
boost::signals2::connection
SolverGMRES<VectorType>::connect_eigenvalues_slot
(const std_cxx11::function<void (const std::vector<std::complex<double> > &)> &slot,
const bool every_iteration)
{
if (every_iteration)
{
return all_eigenvalues_signal.connect(slot);
}
else
{
return eigenvalues_signal.connect(slot);
}
}
template<class VectorType>
double
SolverGMRES<VectorType>::criterion ()
{
// dummy implementation. this function is not needed for the present
// implementation of gmres
Assert (false, ExcInternalError());
return 0;
}
//----------------------------------------------------------------------//
template <class VectorType>
SolverFGMRES<VectorType>::SolverFGMRES (SolverControl &cn,
VectorMemory<VectorType> &mem,
const AdditionalData &data)
:
Solver<VectorType> (cn, mem),
additional_data(data)
{}
template <class VectorType>
SolverFGMRES<VectorType>::SolverFGMRES (SolverControl &cn,
const AdditionalData &data)
:
Solver<VectorType> (cn),
additional_data(data)
{}
template<class VectorType>
template<typename MatrixType, typename PreconditionerType>
void
SolverFGMRES<VectorType>::solve (const MatrixType &A,
VectorType &x,
const VectorType &b,
const PreconditionerType &precondition)
{
deallog.push("FGMRES");
SolverControl::State iteration_state = SolverControl::iterate;
const unsigned int basis_size = additional_data.max_basis_size;
// Generate an object where basis vectors are stored.
typename internal::SolverGMRES::TmpVectors<VectorType> v (basis_size, this->memory);
typename internal::SolverGMRES::TmpVectors<VectorType> z (basis_size, this->memory);
// number of the present iteration; this number is not reset to zero upon a
// restart
unsigned int accumulated_iterations = 0;
// matrix used for the orthogonalization process later
H.reinit(basis_size+1, basis_size);
// Vectors for projected system
Vector<double> projected_rhs;
Vector<double> y;
// Iteration starts here
double res = -std::numeric_limits<double>::max();
VectorType *aux = this->memory.alloc();
aux->reinit(x);
do
{
A.vmult(*aux, x);
aux->sadd(-1., 1., b);
double beta = aux->l2_norm();
res = beta;
iteration_state = this->iteration_status(accumulated_iterations, res, x);
if (iteration_state == SolverControl::success)
break;
H.reinit(basis_size+1, basis_size);
double a = beta;
for (unsigned int j=0; j<basis_size; ++j)
{
if (a != 0) // treat lucky breakdown
v(j,x).equ(1./a, *aux);
else
v(j,x) = 0.;
precondition.vmult(z(j,x), v[j]);
A.vmult(*aux, z[j]);
// Gram-Schmidt
H(0,j) = *aux * v[0];
for (unsigned int i=1; i<=j; ++i)
H(i,j) = aux->add_and_dot(-H(i-1,j), v[i-1], v[i]);
H(j+1,j) = a = std::sqrt(aux->add_and_dot(-H(j,j), v[j], *aux));
// Compute projected solution
if (j>0)
{
H1.reinit(j+1,j);
projected_rhs.reinit(j+1);
y.reinit(j);
projected_rhs(0) = beta;
H1.fill(H);
// check convergence. note that the vector 'x' we pass to the
// criterion is not the final solution we compute if we
// decide to jump out of the iteration (we update 'x' again
// right after the current loop)
Householder<double> house(H1);
res = house.least_squares(y, projected_rhs);
iteration_state = this->iteration_status(++accumulated_iterations, res, x);
if (iteration_state != SolverControl::iterate)
break;
}
}
// Update solution vector
for (unsigned int j=0; j<y.size(); ++j)
x.add(y(j), z[j]);
}
while (iteration_state == SolverControl::iterate);
this->memory.free(aux);
deallog.pop();
// in case of failure: throw exception
if (iteration_state != SolverControl::success)
AssertThrow(false, SolverControl::NoConvergence (accumulated_iterations,
res));
}
#endif // DOXYGEN
DEAL_II_NAMESPACE_CLOSE
#endif
|