16 #ifndef dealii_solver_gmres_h 17 #define dealii_solver_gmres_h 21 #include <deal.II/base/config.h> 23 #include <deal.II/base/logstream.h> 24 #include <deal.II/base/std_cxx14/memory.h> 25 #include <deal.II/base/subscriptor.h> 27 #include <deal.II/lac/full_matrix.h> 28 #include <deal.II/lac/householder.h> 29 #include <deal.II/lac/lapack_full_matrix.h> 30 #include <deal.II/lac/solver.h> 31 #include <deal.II/lac/solver_control.h> 32 #include <deal.II/lac/vector.h> 38 DEAL_II_NAMESPACE_OPEN
48 namespace SolverGMRESImplementation
58 template <
typename VectorType>
77 VectorType &
operator[](
const unsigned int i)
const;
86 operator()(
const unsigned int i,
const VectorType &temp);
105 std::vector<typename VectorMemory<VectorType>::Pointer>
data;
177 template <
class VectorType = Vector<
double>>
249 template <
typename MatrixType,
typename PreconditionerType>
251 solve(
const MatrixType & A,
253 const VectorType & b,
254 const PreconditionerType &preconditioner);
262 boost::signals2::connection
264 const bool every_iteration =
false);
272 boost::signals2::connection
274 const std::function<
void(
const std::vector<std::complex<double>> &)> &slot,
275 const bool every_iteration =
false);
284 boost::signals2::connection
287 const bool every_iteration =
true);
295 boost::signals2::connection
306 boost::signals2::connection
312 <<
"The number of temporary vectors you gave (" << arg1
313 <<
") is too small. It should be at least 10 for " 314 <<
"any results, and much more for reasonable ones.");
338 boost::signals2::signal<void(const std::vector<std::complex<double>> &)>
345 boost::signals2::signal<void(const std::vector<std::complex<double>> &)>
358 boost::signals2::signal<void(const FullMatrix<double> &)>
365 boost::signals2::signal<void(
405 & orthogonal_vectors,
406 const unsigned int dim,
407 const unsigned int accumulated_iterations,
410 bool & re_orthogonalize,
412 boost::signals2::signal<
void(
int)>());
423 const unsigned int dim,
424 const boost::signals2::signal<
428 const boost::signals2::signal<
void(
double)> &cond_signal);
459 template <
class VectorType = Vector<
double>>
481 const bool use_default_residual)
484 (void)use_default_residual;
510 template <
typename MatrixType,
typename PreconditionerType>
512 solve(
const MatrixType & A,
514 const VectorType & b,
515 const PreconditionerType &preconditioner);
541 namespace SolverGMRESImplementation
543 template <
class VectorType>
552 template <
class VectorType>
564 template <
class VectorType>
567 const VectorType & temp)
570 if (data[i] ==
nullptr)
573 data[i]->reinit(temp);
580 template <
class VectorType>
584 return (data.size() > 0 ? data.size() - 1 : 0);
591 complex_less_pred(
const std::complex<double> &x,
592 const std::complex<double> &y)
594 return x.real() < y.real() ||
595 (x.real() == y.real() && x.imag() < y.imag());
602 template <
class VectorType>
604 const unsigned int max_n_tmp_vectors,
605 const bool right_preconditioning,
606 const bool use_default_residual,
607 const bool force_re_orthogonalization)
608 : max_n_tmp_vectors(max_n_tmp_vectors)
609 , right_preconditioning(right_preconditioning)
610 , use_default_residual(use_default_residual)
611 , force_re_orthogonalization(force_re_orthogonalization)
613 Assert(3 <= max_n_tmp_vectors,
614 ExcMessage(
"SolverGMRES needs at least three " 615 "temporary vectors."));
620 template <
class VectorType>
623 const AdditionalData & data)
625 , additional_data(data)
630 template <
class VectorType>
632 const AdditionalData &data)
634 , additional_data(data)
639 template <
class VectorType>
647 for (
int i = 0; i < col; i++)
649 const double s = si(i);
650 const double c = ci(i);
651 const double dummy = h(i);
652 h(i) = c * dummy + s * h(i + 1);
653 h(i + 1) = -s * dummy + c * h(i + 1);
656 const double r = 1. / std::sqrt(h(col) * h(col) + h(col + 1) * h(col + 1));
657 si(col) = h(col + 1) * r;
658 ci(col) = h(col) * r;
659 h(col) = ci(col) * h(col) + si(col) * h(col + 1);
660 b(col + 1) = -si(col) *
b(col);
666 template <
class VectorType>
670 & orthogonal_vectors,
671 const unsigned int dim,
672 const unsigned int accumulated_iterations,
675 bool & reorthogonalize,
676 const boost::signals2::signal<
void(
int)> &reorthogonalize_signal)
679 const unsigned int inner_iteration = dim - 1;
682 double norm_vv_start = 0;
683 const bool consider_reorthogonalize =
684 (reorthogonalize ==
false) && (inner_iteration % 5 == 4);
685 if (consider_reorthogonalize)
686 norm_vv_start = vv.l2_norm();
689 h(0) = vv * orthogonal_vectors[0];
690 for (
unsigned int i = 1; i < dim; ++i)
692 orthogonal_vectors[i - 1],
693 orthogonal_vectors[i]);
695 std::sqrt(vv.add_and_dot(-h(dim - 1), orthogonal_vectors[dim - 1], vv));
704 if (consider_reorthogonalize)
707 10. * norm_vv_start *
709 std::numeric_limits<typename VectorType::value_type>::epsilon()))
714 reorthogonalize =
true;
715 if (!reorthogonalize_signal.empty())
716 reorthogonalize_signal(accumulated_iterations);
720 if (reorthogonalize ==
true)
722 double htmp = vv * orthogonal_vectors[0];
724 for (
unsigned int i = 1; i < dim; ++i)
727 orthogonal_vectors[i - 1],
728 orthogonal_vectors[i]);
732 std::sqrt(vv.add_and_dot(-htmp, orthogonal_vectors[dim - 1], vv));
740 template <
class VectorType>
744 const unsigned int dim,
745 const boost::signals2::signal<
void(
const std::vector<std::complex<double>> &)>
749 const boost::signals2::signal<
void(
double)> &cond_signal)
752 if ((!eigenvalues_signal.empty() || !hessenberg_signal.empty() ||
753 !cond_signal.empty()) &&
757 for (
unsigned int i = 0; i < dim; ++i)
758 for (
unsigned int j = 0; j < dim; ++j)
759 mat(i, j) = H_orig(i, j);
760 hessenberg_signal(H_orig);
762 if (!eigenvalues_signal.empty())
768 mat_eig.compute_eigenvalues();
769 std::vector<std::complex<double>>
eigenvalues(dim);
770 for (
unsigned int i = 0; i < mat_eig.n(); ++i)
775 internal::SolverGMRESImplementation::complex_less_pred);
780 if (!cond_signal.empty() && (mat.n() > 1))
783 double condition_number =
784 mat.singular_value(0) / mat.singular_value(mat.n() - 1);
785 cond_signal(condition_number);
792 template <
class VectorType>
793 template <
typename MatrixType,
typename PreconditionerType>
797 const VectorType & b,
798 const PreconditionerType &preconditioner)
808 const unsigned int n_tmp_vectors =
813 n_tmp_vectors, this->memory);
818 unsigned int accumulated_iterations = 0;
820 const bool do_eigenvalues =
821 !condition_number_signal.empty() || !all_condition_numbers_signal.empty() ||
822 !eigenvalues_signal.empty() || !all_eigenvalues_signal.empty() ||
823 !hessenberg_signal.empty() || !all_hessenberg_signal.empty();
828 H_orig.
reinit(n_tmp_vectors, n_tmp_vectors - 1);
831 H.reinit(n_tmp_vectors, n_tmp_vectors - 1);
835 si(n_tmp_vectors - 1), h(n_tmp_vectors - 1);
838 unsigned int dim = 0;
841 double last_res = -std::numeric_limits<double>::max();
853 VectorType &v = tmp_vectors(0, x);
854 VectorType &p = tmp_vectors(n_tmp_vectors - 1, x);
860 std::unique_ptr<::Vector<double>> gamma_;
861 if (!use_default_residual)
868 gamma_ = std_cxx14::make_unique<::Vector<double>>(gamma.size());
880 h.
reinit(n_tmp_vectors - 1);
882 if (left_precondition)
886 preconditioner.vmult(v, p);
894 double rho = v.l2_norm();
899 if (use_default_residual)
903 this->iteration_status(accumulated_iterations, rho, x);
910 deallog <<
"default_res=" << rho << std::endl;
912 if (left_precondition)
918 preconditioner.vmult(*r, v);
920 double res = r->l2_norm();
923 this->iteration_status(accumulated_iterations, res, x);
936 for (
unsigned int inner_iteration = 0;
937 ((inner_iteration < n_tmp_vectors - 2) &&
941 ++accumulated_iterations;
943 VectorType &vv = tmp_vectors(inner_iteration + 1, x);
945 if (left_precondition)
947 A.vmult(p, tmp_vectors[inner_iteration]);
948 preconditioner.vmult(vv, p);
952 preconditioner.vmult(p, tmp_vectors[inner_iteration]);
956 dim = inner_iteration + 1;
958 const double s = modified_gram_schmidt(tmp_vectors,
960 accumulated_iterations,
964 re_orthogonalize_signal);
965 h(inner_iteration + 1) = s;
975 for (
unsigned int i = 0; i < dim + 1; ++i)
976 H_orig(i, inner_iteration) = h(i);
982 for (
unsigned int i = 0; i < dim; ++i)
983 H(i, inner_iteration) = h(i);
986 rho = std::fabs(gamma(dim));
988 if (use_default_residual)
992 this->iteration_status(accumulated_iterations, rho, x);
996 deallog <<
"default_res=" << rho << std::endl;
1001 H1.reinit(dim + 1, dim);
1003 for (
unsigned int i = 0; i < dim + 1; ++i)
1004 for (
unsigned int j = 0; j < dim; ++j)
1007 H1.backward(h_, *gamma_);
1009 if (left_precondition)
1010 for (
unsigned int i = 0; i < dim; ++i)
1011 x_->add(h_(i), tmp_vectors[i]);
1015 for (
unsigned int i = 0; i < dim; ++i)
1016 p.add(h_(i), tmp_vectors[i]);
1017 preconditioner.vmult(*r, p);
1021 r->sadd(-1., 1., b);
1023 if (left_precondition)
1025 const double res = r->l2_norm();
1029 this->iteration_status(accumulated_iterations, res, x);
1033 preconditioner.vmult(*x_, *r);
1034 const double preconditioned_res = x_->l2_norm();
1035 last_res = preconditioned_res;
1038 this->iteration_status(accumulated_iterations,
1047 H1.reinit(dim + 1, dim);
1049 for (
unsigned int i = 0; i < dim + 1; ++i)
1050 for (
unsigned int j = 0; j < dim; ++j)
1053 compute_eigs_and_cond(H_orig,
1055 all_eigenvalues_signal,
1056 all_hessenberg_signal,
1057 condition_number_signal);
1059 H1.backward(h, gamma);
1061 if (left_precondition)
1062 for (
unsigned int i = 0; i < dim; ++i)
1063 x.add(h(i), tmp_vectors[i]);
1067 for (
unsigned int i = 0; i < dim; ++i)
1068 p.add(h(i), tmp_vectors[i]);
1069 preconditioner.vmult(v, p);
1077 compute_eigs_and_cond(H_orig,
1081 condition_number_signal);
1083 if (!krylov_space_signal.empty())
1084 krylov_space_signal(tmp_vectors);
1093 template <
class VectorType>
1094 boost::signals2::connection
1096 const std::function<
void(
double)> &slot,
1097 const bool every_iteration)
1099 if (every_iteration)
1101 return all_condition_numbers_signal.connect(slot);
1105 return condition_number_signal.connect(slot);
1111 template <
class VectorType>
1112 boost::signals2::connection
1114 const std::function<
void(
const std::vector<std::complex<double>> &)> &slot,
1115 const bool every_iteration)
1117 if (every_iteration)
1119 return all_eigenvalues_signal.connect(slot);
1123 return eigenvalues_signal.connect(slot);
1129 template <
class VectorType>
1130 boost::signals2::connection
1133 const bool every_iteration)
1135 if (every_iteration)
1137 return all_hessenberg_signal.connect(slot);
1141 return hessenberg_signal.connect(slot);
1147 template <
class VectorType>
1148 boost::signals2::connection
1150 const std::function<
void(
1153 return krylov_space_signal.connect(slot);
1158 template <
class VectorType>
1159 boost::signals2::connection
1161 const std::function<
void(
int)> &slot)
1163 return re_orthogonalize_signal.connect(slot);
1168 template <
class VectorType>
1181 template <
class VectorType>
1184 const AdditionalData & data)
1186 , additional_data(data)
1191 template <
class VectorType>
1193 const AdditionalData &data)
1195 , additional_data(data)
1200 template <
class VectorType>
1201 template <
typename MatrixType,
typename PreconditionerType>
1205 const VectorType & b,
1206 const PreconditionerType &preconditioner)
1212 const unsigned int basis_size = additional_data.max_basis_size;
1216 basis_size, this->memory);
1218 basis_size, this->memory);
1222 unsigned int accumulated_iterations = 0;
1225 H.reinit(basis_size + 1, basis_size);
1232 double res = -std::numeric_limits<double>::max();
1239 aux->sadd(-1., 1., b);
1241 double beta = aux->l2_norm();
1243 iteration_state = this->iteration_status(accumulated_iterations, res, x);
1247 H.reinit(basis_size + 1, basis_size);
1250 for (
unsigned int j = 0; j < basis_size; ++j)
1253 v(j, x).equ(1. / a, *aux);
1258 preconditioner.vmult(z(j, x), v[j]);
1259 A.vmult(*aux, z[j]);
1262 H(0, j) = *aux * v[0];
1263 for (
unsigned int i = 1; i <= j; ++i)
1264 H(i, j) = aux->add_and_dot(-H(i - 1, j), v[i - 1], v[i]);
1265 H(j + 1, j) = a = std::sqrt(aux->add_and_dot(-H(j, j), v[j], *aux));
1271 H1.reinit(j + 1, j);
1272 projected_rhs.
reinit(j + 1);
1274 projected_rhs(0) = beta;
1282 res = house.least_squares(y, projected_rhs);
1284 this->iteration_status(++accumulated_iterations, res, x);
1291 for (
unsigned int j = 0; j < y.
size(); ++j)
1304 DEAL_II_NAMESPACE_CLOSE
SolverFGMRES(SolverControl &cn, VectorMemory< VectorType > &mem, const AdditionalData &data=AdditionalData())
boost::signals2::signal< void(const std::vector< std::complex< double >> &)> eigenvalues_signal
Number add_and_dot(const Number a, const Vector< Number > &V, const Vector< Number > &W)
TmpVectors(const unsigned int max_size, VectorMemory< VectorType > &vmem)
boost::signals2::connection connect_condition_number_slot(const std::function< void(double)> &slot, const bool every_iteration=false)
boost::signals2::signal< void(const internal::SolverGMRESImplementation::TmpVectors< VectorType > &)> krylov_space_signal
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
boost::signals2::connection connect_re_orthogonalization_slot(const std::function< void(int)> &slot)
boost::signals2::signal< void(double)> condition_number_signal
AdditionalData additional_data
static ::ExceptionBase & ExcNotInitialized()
std::vector< typename VectorMemory< VectorType >::Pointer > data
#define AssertThrow(cond, exc)
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
AdditionalData additional_data
VectorType & operator()(const unsigned int i, const VectorType &temp)
AdditionalData(const unsigned int max_basis_size=30)
boost::signals2::signal< void(int)> re_orthogonalize_signal
static ::ExceptionBase & ExcTooFewTmpVectors(int arg1)
bool right_preconditioning
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
static ::ExceptionBase & ExcMessage(std::string arg1)
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)
#define DeclException1(Exception1, type1, outsequence)
Stop iteration, goal reached.
void reinit(const TableIndices< N > &new_size, const bool omit_default_initialization=false)
#define Assert(cond, exc)
boost::signals2::connection connect_krylov_space_slot(const std::function< void(const internal::SolverGMRESImplementation::TmpVectors< VectorType > &)> &slot)
unsigned int max_basis_size
static double modified_gram_schmidt(const internal::SolverGMRESImplementation::TmpVectors< VectorType > &orthogonal_vectors, const unsigned int dim, const unsigned int accumulated_iterations, VectorType &vv, Vector< double > &h, bool &re_orthogonalize, const boost::signals2::signal< void(int)> &re_orthogonalize_signal=boost::signals2::signal< void(int)>())
boost::signals2::signal< void(const FullMatrix< double > &)> hessenberg_signal
bool force_re_orthogonalization
AdditionalData(const unsigned int max_basis_size, const bool use_default_residual)
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
VectorMemory< VectorType > & mem
boost::signals2::signal< void(const std::vector< std::complex< double >> &)> all_eigenvalues_signal
virtual double criterion()
unsigned int size() const
std::array< NumberType, 3 > givens_rotation(const NumberType &x, const NumberType &y)
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
boost::signals2::signal< void(const FullMatrix< double > &)> all_hessenberg_signal
std::array< Number, 1 > eigenvalues(const SymmetricTensor< 2, 1, Number > &T)
bool use_default_residual
VectorType & operator[](const unsigned int i) const
boost::signals2::connection connect_hessenberg_slot(const std::function< void(const FullMatrix< double > &)> &slot, const bool every_iteration=true)
SolverGMRES(SolverControl &cn, VectorMemory< VectorType > &mem, const AdditionalData &data=AdditionalData())
void givens_rotation(Vector< double > &h, Vector< double > &b, Vector< double > &ci, Vector< double > &si, int col) const
boost::signals2::connection connect_eigenvalues_slot(const std::function< void(const std::vector< std::complex< double >> &)> &slot, const bool every_iteration=false)
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(const FullMatrix< double > &)> &hessenberg_signal, const boost::signals2::signal< void(double)> &cond_signal)
boost::signals2::signal< void(double)> all_condition_numbers_signal
unsigned int max_n_tmp_vectors
static ::ExceptionBase & ExcInternalError()