15#ifndef dealii_precondition_h
16#define dealii_precondition_h
39template <
typename number>
41template <
typename number>
47 template <
typename,
typename>
126 template <
typename PreconditionerType>
247 template <
typename MatrixType>
255 template <
typename VectorType>
257 vmult(VectorType &,
const VectorType &)
const;
263 template <
typename VectorType>
265 Tvmult(VectorType &,
const VectorType &)
const;
270 template <
typename VectorType>
278 template <
typename VectorType>
376 template <
typename MatrixType>
383 template <
typename VectorType>
385 vmult(VectorType &,
const VectorType &)
const;
391 template <
typename VectorType>
393 Tvmult(VectorType &,
const VectorType &)
const;
397 template <
typename VectorType>
405 template <
typename VectorType>
495template <
typename MatrixType = SparseMatrix<
double>,
496 typename VectorType = Vector<
double>>
504 const VectorType &) const;
518 vmult(VectorType &dst,
const VectorType &src)
const;
563template <
typename MatrixType = SparseMatrix<
double>,
564 typename PreconditionerType = IdentityMatrix>
592 EigenvalueAlgorithm::lanczos);
638 template <
typename VectorType>
640 vmult(VectorType &,
const VectorType &)
const;
646 template <
typename VectorType>
648 Tvmult(VectorType &,
const VectorType &)
const;
653 template <
typename VectorType>
655 step(VectorType &x,
const VectorType &rhs)
const;
660 template <
typename VectorType>
662 Tstep(VectorType &x,
const VectorType &rhs)
const;
669 template <
typename VectorType>
714 template <
typename MatrixType,
typename VectorType>
715 using vmult_functions_t =
decltype(std::declval<const MatrixType>().vmult(
716 std::declval<VectorType &>(),
717 std::declval<const VectorType &>(),
719 const std::function<
void(
const unsigned int,
const unsigned int)> &>(),
721 const std::function<
void(
const unsigned int,
const unsigned int)> &>()));
723 template <
typename MatrixType,
725 typename PreconditionerType>
726 constexpr bool has_vmult_with_std_functions =
727 is_supported_operation<vmult_functions_t, MatrixType, VectorType> &&
728 std::is_same_v<PreconditionerType, ::DiagonalMatrix<VectorType>> &&
729 (std::is_same_v<VectorType,
737 template <
typename MatrixType,
typename VectorType>
738 constexpr bool has_vmult_with_std_functions_for_precondition =
739 is_supported_operation<vmult_functions_t, MatrixType, VectorType>;
743 template <
typename T,
typename VectorType>
744 using Tvmult_t =
decltype(std::declval<const T>().Tvmult(
745 std::declval<VectorType &>(),
746 std::declval<const VectorType &>()));
748 template <
typename T,
typename VectorType>
749 constexpr bool has_Tvmult = is_supported_operation<Tvmult_t, T, VectorType>;
751 template <
typename T,
typename VectorType>
752 using step_t =
decltype(std::declval<const T>().step(
753 std::declval<VectorType &>(),
754 std::declval<const VectorType &>()));
756 template <
typename T,
typename VectorType>
757 constexpr bool has_step = is_supported_operation<step_t, T, VectorType>;
759 template <
typename T,
typename VectorType>
761 decltype(std::declval<const T>().step(std::declval<VectorType &>(),
762 std::declval<const VectorType &>(),
763 std::declval<const double>()));
765 template <
typename T,
typename VectorType>
766 constexpr bool has_step_omega =
767 is_supported_operation<step_omega_t, T, VectorType>;
769 template <
typename T,
typename VectorType>
770 using Tstep_t =
decltype(std::declval<const T>().Tstep(
771 std::declval<VectorType &>(),
772 std::declval<const VectorType &>()));
774 template <
typename T,
typename VectorType>
775 constexpr bool has_Tstep = is_supported_operation<Tstep_t, T, VectorType>;
777 template <
typename T,
typename VectorType>
778 using Tstep_omega_t =
779 decltype(std::declval<const T>().Tstep(std::declval<VectorType &>(),
780 std::declval<const VectorType &>(),
781 std::declval<const double>()));
783 template <
typename T,
typename VectorType>
784 constexpr bool has_Tstep_omega =
785 is_supported_operation<Tstep_omega_t, T, VectorType>;
787 template <
typename T,
typename VectorType>
788 using jacobi_step_t =
decltype(std::declval<const T>().Jacobi_step(
789 std::declval<VectorType &>(),
790 std::declval<const VectorType &>(),
791 std::declval<const double>()));
793 template <
typename T,
typename VectorType>
794 constexpr bool has_jacobi_step =
795 is_supported_operation<jacobi_step_t, T, VectorType>;
797 template <
typename T,
typename VectorType>
798 using SOR_step_t =
decltype(std::declval<const T>().SOR_step(
799 std::declval<VectorType &>(),
800 std::declval<const VectorType &>(),
801 std::declval<const double>()));
803 template <
typename T,
typename VectorType>
804 constexpr bool has_SOR_step =
805 is_supported_operation<SOR_step_t, T, VectorType>;
807 template <
typename T,
typename VectorType>
808 using SSOR_step_t =
decltype(std::declval<const T>().SSOR_step(
809 std::declval<VectorType &>(),
810 std::declval<const VectorType &>(),
811 std::declval<const double>()));
813 template <
typename T,
typename VectorType>
814 constexpr bool has_SSOR_step =
815 is_supported_operation<SSOR_step_t, T, VectorType>;
817 template <
typename MatrixType>
818 class PreconditionJacobiImpl
821 PreconditionJacobiImpl(
const MatrixType &A,
const double relaxation)
823 , relaxation(relaxation)
826 template <
typename VectorType>
828 vmult(VectorType &dst,
const VectorType &src)
const
830 this->A->precondition_Jacobi(dst, src, this->relaxation);
833 template <
typename VectorType>
835 Tvmult(VectorType &dst,
const VectorType &src)
const
838 this->vmult(dst, src);
841 template <
typename VectorType,
842 std::enable_if_t<has_jacobi_step<MatrixType, VectorType>,
843 MatrixType> * =
nullptr>
845 step(VectorType &dst,
const VectorType &src)
const
847 this->A->Jacobi_step(dst, src, this->relaxation);
850 template <
typename VectorType,
851 std::enable_if_t<!has_jacobi_step<MatrixType, VectorType>,
852 MatrixType> * =
nullptr>
854 step(VectorType &,
const VectorType &)
const
858 "Matrix A does not provide a Jacobi_step() function!"));
861 template <
typename VectorType>
863 Tstep(VectorType &dst,
const VectorType &src)
const
866 this->step(dst, src);
871 const double relaxation;
874 template <
typename MatrixType>
875 class PreconditionSORImpl
878 PreconditionSORImpl(
const MatrixType &A,
const double relaxation)
880 , relaxation(relaxation)
883 template <
typename VectorType>
885 vmult(VectorType &dst,
const VectorType &src)
const
887 this->A->precondition_SOR(dst, src, this->relaxation);
890 template <
typename VectorType>
892 Tvmult(VectorType &dst,
const VectorType &src)
const
894 this->A->precondition_TSOR(dst, src, this->relaxation);
897 template <
typename VectorType,
898 std::enable_if_t<has_SOR_step<MatrixType, VectorType>,
899 MatrixType> * =
nullptr>
901 step(VectorType &dst,
const VectorType &src)
const
903 this->A->SOR_step(dst, src, this->relaxation);
906 template <
typename VectorType,
907 std::enable_if_t<!has_SOR_step<MatrixType, VectorType>,
908 MatrixType> * =
nullptr>
910 step(VectorType &,
const VectorType &)
const
914 "Matrix A does not provide a SOR_step() function!"));
917 template <
typename VectorType,
918 std::enable_if_t<has_SOR_step<MatrixType, VectorType>,
919 MatrixType> * =
nullptr>
921 Tstep(VectorType &dst,
const VectorType &src)
const
923 this->A->TSOR_step(dst, src, this->relaxation);
926 template <
typename VectorType,
927 std::enable_if_t<!has_SOR_step<MatrixType, VectorType>,
928 MatrixType> * =
nullptr>
930 Tstep(VectorType &,
const VectorType &)
const
934 "Matrix A does not provide a TSOR_step() function!"));
939 const double relaxation;
942 template <
typename MatrixType>
943 class PreconditionSSORImpl
946 using size_type =
typename MatrixType::size_type;
948 PreconditionSSORImpl(
const MatrixType &A,
const double relaxation)
950 , relaxation(relaxation)
961 const size_type n = this->A->n();
962 pos_right_of_diagonal.resize(n,
static_cast<std::size_t
>(-1));
963 for (size_type row = 0; row < n; ++row)
970 typename MatrixType::value_type>::const_iterator it =
972 for (; it < mat->
end(row); ++it)
973 if (it->column() > row)
975 pos_right_of_diagonal[row] = it - mat->
begin();
980 template <
typename VectorType>
982 vmult(VectorType &dst,
const VectorType &src)
const
984 this->A->precondition_SSOR(dst,
987 pos_right_of_diagonal);
990 template <
typename VectorType>
992 Tvmult(VectorType &dst,
const VectorType &src)
const
994 this->A->precondition_SSOR(dst,
997 pos_right_of_diagonal);
1000 template <
typename VectorType,
1001 std::enable_if_t<has_SSOR_step<MatrixType, VectorType>,
1002 MatrixType> * =
nullptr>
1004 step(VectorType &dst,
const VectorType &src)
const
1006 this->A->SSOR_step(dst, src, this->relaxation);
1009 template <
typename VectorType,
1010 std::enable_if_t<!has_SSOR_step<MatrixType, VectorType>,
1011 MatrixType> * =
nullptr>
1013 step(VectorType &,
const VectorType &)
const
1017 "Matrix A does not provide a SSOR_step() function!"));
1020 template <
typename VectorType>
1022 Tstep(VectorType &dst,
const VectorType &src)
const
1025 this->step(dst, src);
1030 const double relaxation;
1036 std::vector<std::size_t> pos_right_of_diagonal;
1039 template <
typename MatrixType>
1040 class PreconditionPSORImpl
1043 using size_type =
typename MatrixType::size_type;
1045 PreconditionPSORImpl(
const MatrixType &A,
1046 const double relaxation,
1047 const std::vector<size_type> &permutation,
1048 const std::vector<size_type> &inverse_permutation)
1050 , relaxation(relaxation)
1051 , permutation(permutation)
1052 , inverse_permutation(inverse_permutation)
1055 template <
typename VectorType>
1057 vmult(VectorType &dst,
const VectorType &src)
const
1060 this->A->PSOR(dst, permutation, inverse_permutation, this->relaxation);
1063 template <
typename VectorType>
1065 Tvmult(VectorType &dst,
const VectorType &src)
const
1068 this->A->TPSOR(dst, permutation, inverse_permutation, this->relaxation);
1073 const double relaxation;
1075 const std::vector<size_type> &permutation;
1076 const std::vector<size_type> &inverse_permutation;
1079 template <
typename MatrixType,
1080 typename PreconditionerType,
1081 typename VectorType,
1082 std::enable_if_t<has_step_omega<PreconditionerType, VectorType>,
1083 PreconditionerType> * =
nullptr>
1085 step(
const MatrixType &,
1086 const PreconditionerType &preconditioner,
1088 const VectorType &src,
1089 const double relaxation,
1093 preconditioner.step(dst, src, relaxation);
1097 typename MatrixType,
1098 typename PreconditionerType,
1099 typename VectorType,
1100 std::enable_if_t<!has_step_omega<PreconditionerType, VectorType> &&
1101 has_step<PreconditionerType, VectorType>,
1102 PreconditionerType> * =
nullptr>
1104 step(
const MatrixType &,
1105 const PreconditionerType &preconditioner,
1107 const VectorType &src,
1108 const double relaxation,
1116 preconditioner.step(dst, src);
1120 typename MatrixType,
1121 typename PreconditionerType,
1122 typename VectorType,
1123 std::enable_if_t<!has_step_omega<PreconditionerType, VectorType> &&
1124 !has_step<PreconditionerType, VectorType>,
1125 PreconditionerType> * =
nullptr>
1127 step(
const MatrixType &A,
1128 const PreconditionerType &preconditioner,
1130 const VectorType &src,
1131 const double relaxation,
1132 VectorType &residual,
1135 residual.reinit(dst,
true);
1136 tmp.reinit(dst,
true);
1138 A.vmult(residual, dst);
1139 residual.sadd(-1.0, 1.0, src);
1141 preconditioner.vmult(tmp, residual);
1142 dst.add(relaxation, tmp);
1145 template <
typename MatrixType,
1146 typename PreconditionerType,
1147 typename VectorType,
1148 std::enable_if_t<has_Tstep_omega<PreconditionerType, VectorType>,
1149 PreconditionerType> * =
nullptr>
1151 Tstep(
const MatrixType &,
1152 const PreconditionerType &preconditioner,
1154 const VectorType &src,
1155 const double relaxation,
1159 preconditioner.Tstep(dst, src, relaxation);
1163 typename MatrixType,
1164 typename PreconditionerType,
1165 typename VectorType,
1166 std::enable_if_t<!has_Tstep_omega<PreconditionerType, VectorType> &&
1167 has_Tstep<PreconditionerType, VectorType>,
1168 PreconditionerType> * =
nullptr>
1170 Tstep(
const MatrixType &,
1171 const PreconditionerType &preconditioner,
1173 const VectorType &src,
1174 const double relaxation,
1182 preconditioner.Tstep(dst, src);
1185 template <
typename MatrixType,
1186 typename VectorType,
1187 std::enable_if_t<has_Tvmult<MatrixType, VectorType>, MatrixType>
1190 Tvmult(
const MatrixType &A, VectorType &dst,
const VectorType &src)
1195 template <
typename MatrixType,
1196 typename VectorType,
1197 std::enable_if_t<!has_Tvmult<MatrixType, VectorType>, MatrixType>
1200 Tvmult(
const MatrixType &, VectorType &,
const VectorType &)
1203 ExcMessage(
"Matrix A does not provide a Tvmult() function!"));
1207 typename MatrixType,
1208 typename PreconditionerType,
1209 typename VectorType,
1210 std::enable_if_t<!has_Tstep_omega<PreconditionerType, VectorType> &&
1211 !has_Tstep<PreconditionerType, VectorType>,
1212 PreconditionerType> * =
nullptr>
1214 Tstep(
const MatrixType &A,
1215 const PreconditionerType &preconditioner,
1217 const VectorType &src,
1218 const double relaxation,
1219 VectorType &residual,
1222 residual.reinit(dst,
true);
1223 tmp.reinit(dst,
true);
1225 Tvmult(A, residual, dst);
1226 residual.sadd(-1.0, 1.0, src);
1228 Tvmult(preconditioner, tmp, residual);
1229 dst.add(relaxation, tmp);
1233 template <
typename MatrixType,
1234 typename PreconditionerType,
1235 typename VectorType,
1236 std::enable_if_t<!has_vmult_with_std_functions_for_precondition<
1241 step_operations(
const MatrixType &A,
1242 const PreconditionerType &preconditioner,
1244 const VectorType &src,
1245 const double relaxation,
1248 const unsigned int i,
1249 const bool transposed)
1254 Tvmult(preconditioner, dst, src);
1256 preconditioner.vmult(dst, src);
1258 if (relaxation != 1.0)
1264 Tstep(A, preconditioner, dst, src, relaxation, tmp1, tmp2);
1266 step(A, preconditioner, dst, src, relaxation, tmp1, tmp2);
1273 typename MatrixType,
1274 typename PreconditionerType,
1275 typename VectorType,
1277 has_vmult_with_std_functions_for_precondition<PreconditionerType,
1279 !has_vmult_with_std_functions_for_precondition<MatrixType,
1283 step_operations(
const MatrixType &A,
1284 const PreconditionerType &preconditioner,
1286 const VectorType &src,
1287 const double relaxation,
1290 const unsigned int i,
1291 const bool transposed)
1294 using Number =
typename VectorType::value_type;
1295 Number *dst_ptr = dst.begin();
1296 const Number *src_ptr = src.begin();
1300 preconditioner.vmult(
1303 [&](
const unsigned int start_range,
const unsigned int end_range) {
1305 if (end_range > start_range)
1306 std::memset(dst_ptr + start_range,
1308 sizeof(Number) * (end_range - start_range));
1310 [&](
const unsigned int start_range,
const unsigned int end_range) {
1311 if (relaxation == 1.0)
1315 for (std::size_t i = start_range; i < end_range; ++i)
1316 dst_ptr[i] *= relaxation;
1321 tmp.reinit(src,
true);
1327 preconditioner.vmult(
1330 [&](
const unsigned int start_range,
const unsigned int end_range) {
1331 const auto tmp_ptr = tmp.begin();
1333 if (relaxation == 1.0)
1336 for (std::size_t i = start_range; i < end_range; ++i)
1337 tmp_ptr[i] = src_ptr[i] - tmp_ptr[i];
1345 for (std::size_t i = start_range; i < end_range; ++i)
1346 tmp_ptr[i] = relaxation * (src_ptr[i] - tmp_ptr[i]);
1349 [&](
const unsigned int,
const unsigned int) {
1359 typename MatrixType,
1360 typename PreconditionerType,
1361 typename VectorType,
1363 has_vmult_with_std_functions_for_precondition<PreconditionerType,
1365 has_vmult_with_std_functions_for_precondition<MatrixType, VectorType>,
1368 step_operations(
const MatrixType &A,
1369 const PreconditionerType &preconditioner,
1371 const VectorType &src,
1372 const double relaxation,
1375 const unsigned int i,
1376 const bool transposed)
1379 using Number =
typename VectorType::value_type;
1381 Number *dst_ptr = dst.begin();
1382 const Number *src_ptr = src.begin();
1386 preconditioner.vmult(
1389 [&](
const unsigned int start_range,
const unsigned int end_range) {
1391 if (end_range > start_range)
1392 std::memset(dst_ptr + start_range,
1394 sizeof(Number) * (end_range - start_range));
1396 [&](
const unsigned int start_range,
const unsigned int end_range) {
1397 if (relaxation == 1.0)
1401 for (std::size_t i = start_range; i < end_range; ++i)
1402 dst_ptr[i] *= relaxation;
1407 tmp.reinit(src,
true);
1408 const auto tmp_ptr = tmp.begin();
1415 [&](
const unsigned int start_range,
const unsigned int end_range) {
1418 if (end_range > start_range)
1419 std::memset(tmp_ptr + start_range,
1421 sizeof(Number) * (end_range - start_range));
1423 [&](
const unsigned int start_range,
const unsigned int end_range) {
1424 if (relaxation == 1.0)
1427 for (std::size_t i = start_range; i < end_range; ++i)
1428 tmp_ptr[i] = src_ptr[i] - tmp_ptr[i];
1436 for (std::size_t i = start_range; i < end_range; ++i)
1437 tmp_ptr[i] = relaxation * (src_ptr[i] - tmp_ptr[i]);
1441 preconditioner.vmult(dst, tmp, [](
const auto,
const auto) {
1449 template <
typename MatrixType,
1450 typename VectorType,
1451 std::enable_if_t<!IsBlockVector<VectorType>::value &&
1452 !has_vmult_with_std_functions<
1456 VectorType> * =
nullptr>
1458 step_operations(
const MatrixType &A,
1459 const ::DiagonalMatrix<VectorType> &preconditioner,
1461 const VectorType &src,
1462 const double relaxation,
1465 const unsigned int i,
1466 const bool transposed)
1468 using Number =
typename VectorType::value_type;
1472 Number *dst_ptr = dst.begin();
1473 const Number *src_ptr = src.begin();
1474 const Number *diag_ptr = preconditioner.get_vector().begin();
1476 if (relaxation == 1.0)
1479 for (
unsigned int i = 0; i < dst.locally_owned_size(); ++i)
1480 dst_ptr[i] = src_ptr[i] * diag_ptr[i];
1485 for (
unsigned int i = 0; i < dst.locally_owned_size(); ++i)
1486 dst_ptr[i] = relaxation * src_ptr[i] * diag_ptr[i];
1491 tmp.reinit(src,
true);
1494 Tvmult(A, tmp, dst);
1498 Number *dst_ptr = dst.begin();
1499 const Number *src_ptr = src.begin();
1500 const Number *tmp_ptr = tmp.begin();
1501 const Number *diag_ptr = preconditioner.get_vector().begin();
1503 if (relaxation == 1.0)
1506 for (
unsigned int i = 0; i < dst.locally_owned_size(); ++i)
1507 dst_ptr[i] += (src_ptr[i] - tmp_ptr[i]) * diag_ptr[i];
1512 for (
unsigned int i = 0; i < dst.locally_owned_size(); ++i)
1514 relaxation * (src_ptr[i] - tmp_ptr[i]) * diag_ptr[i];
1521 template <
typename MatrixType,
1522 typename VectorType,
1523 std::enable_if_t<!IsBlockVector<VectorType>::value &&
1524 has_vmult_with_std_functions<
1528 VectorType> * =
nullptr>
1530 step_operations(
const MatrixType &A,
1531 const ::DiagonalMatrix<VectorType> &preconditioner,
1533 const VectorType &src,
1534 const double relaxation,
1537 const unsigned int i,
1538 const bool transposed)
1541 using Number =
typename VectorType::value_type;
1545 Number *dst_ptr = dst.begin();
1546 const Number *src_ptr = src.begin();
1547 const Number *diag_ptr = preconditioner.get_vector().begin();
1549 if (relaxation == 1.0)
1552 for (
unsigned int i = 0; i < dst.locally_owned_size(); ++i)
1553 dst_ptr[i] = src_ptr[i] * diag_ptr[i];
1558 for (
unsigned int i = 0; i < dst.locally_owned_size(); ++i)
1559 dst_ptr[i] = relaxation * src_ptr[i] * diag_ptr[i];
1564 tmp.reinit(src,
true);
1571 [&](
const unsigned int start_range,
const unsigned int end_range) {
1573 if (end_range > start_range)
1574 std::memset(tmp.begin() + start_range,
1576 sizeof(Number) * (end_range - start_range));
1578 [&](
const unsigned int begin,
const unsigned int end) {
1579 const Number *dst_ptr = dst.begin();
1580 const Number *src_ptr = src.begin();
1581 Number *tmp_ptr = tmp.begin();
1582 const Number *diag_ptr = preconditioner.get_vector().begin();
1586 if (relaxation == 1.0)
1589 for (std::size_t i = begin; i < end; ++i)
1591 dst_ptr[i] + (src_ptr[i] - tmp_ptr[i]) * diag_ptr[i];
1596 for (std::size_t i = begin; i < end; ++i)
1597 tmp_ptr[i] = dst_ptr[i] + relaxation *
1598 (src_ptr[i] - tmp_ptr[i]) *
1640template <
typename MatrixType = SparseMatrix<
double>>
1644 internal::PreconditionRelaxation::PreconditionJacobiImpl<MatrixType>>
1647 internal::PreconditionRelaxation::PreconditionJacobiImpl<MatrixType>;
1710template <
typename MatrixType = SparseMatrix<
double>>
1714 internal::PreconditionRelaxation::PreconditionSORImpl<MatrixType>>
1717 internal::PreconditionRelaxation::PreconditionSORImpl<MatrixType>;
1762template <
typename MatrixType = SparseMatrix<
double>>
1766 internal::PreconditionRelaxation::PreconditionSSORImpl<MatrixType>>
1769 internal::PreconditionRelaxation::PreconditionSSORImpl<MatrixType>;
1818template <
typename MatrixType = SparseMatrix<
double>>
1822 internal::PreconditionRelaxation::PreconditionPSORImpl<MatrixType>>
1825 internal::PreconditionRelaxation::PreconditionPSORImpl<MatrixType>;
1882 const std::vector<size_type> &permutation,
1883 const std::vector<size_type> &inverse_permutation,
2091template <
typename MatrixType = SparseMatrix<
double>,
2092 typename VectorType = Vector<
double>,
2093 typename PreconditionerType = DiagonalMatrix<VectorType>>
2131 const unsigned int degree = 1,
2137 EigenvalueAlgorithm::lanczos,
2186 vmult(VectorType &dst,
const VectorType &src)
const;
2193 Tvmult(VectorType &dst,
const VectorType &src)
const;
2199 step(VectorType &dst,
const VectorType &src)
const;
2205 Tstep(VectorType &dst,
const VectorType &src)
const;
2308 template <
typename VectorType>
2310 set_initial_guess(VectorType &vector)
2312 vector = 1. /
std::sqrt(
static_cast<double>(vector.size()));
2313 if (vector.locally_owned_elements().is_element(0))
2317 template <
typename Number>
2325 for (
unsigned int i = 0; i < vector.
size(); ++i)
2328 const Number mean_value = vector.
mean_value();
2329 vector.
add(-mean_value);
2332 template <
typename Number>
2337 for (
unsigned int block = 0; block < vector.
n_blocks(); ++block)
2338 set_initial_guess(vector.
block(block));
2341 template <
typename Number,
typename MemorySpace>
2358 Kokkos::RangePolicy<
typename MemorySpace::kokkos_space::execution_space,
2359 Kokkos::IndexType<types::global_dof_index>>
2360 policy(0, n_local_elements);
2361 Kokkos::parallel_for(
2362 "::PreconditionChebyshev::set_initial_guess",
2365 values_ptr[i] = (i + first_local_range) % 11;
2367 const Number mean_value = vector.
mean_value();
2368 vector.
add(-mean_value);
2371 struct EigenvalueTracker
2380 std::vector<double>
values;
2387 typename PreconditionerType>
2390 VectorType &eigenvector,
2391 const PreconditionerType &preconditioner,
2392 const unsigned int n_iterations)
2394 typename VectorType::value_type eigenvalue_estimate = 0.;
2395 eigenvector /= eigenvector.l2_norm();
2397 vector1.reinit(eigenvector,
true);
2398 if (!std::is_same_v<PreconditionerType, PreconditionIdentity>)
2399 vector2.reinit(eigenvector,
true);
2400 for (
unsigned int i = 0; i < n_iterations; ++i)
2402 if (!std::is_same_v<PreconditionerType, PreconditionIdentity>)
2404 matrix.vmult(vector2, eigenvector);
2405 preconditioner.vmult(vector1, vector2);
2408 matrix.vmult(vector1, eigenvector);
2410 eigenvalue_estimate = eigenvector * vector1;
2412 vector1 /= vector1.l2_norm();
2413 eigenvector.swap(vector1);
2415 return std::abs(eigenvalue_estimate);
2422 typename PreconditionerType>
2423 EigenvalueInformation
2424 estimate_eigenvalues(
2425 const EigenvalueAlgorithmAdditionalData<PreconditionerType> &
data,
2426 const MatrixType *matrix_ptr,
2427 VectorType &solution_old,
2428 VectorType &temp_vector1,
2429 const unsigned int degree)
2433 EigenvalueInformation info{};
2435 if (
data.eig_cg_n_iterations > 0)
2439 "Need to set at least two iterations to find eigenvalues."));
2441 internal::EigenvalueTracker eigenvalue_tracker;
2446 internal::set_initial_guess(temp_vector1);
2447 data.constraints.set_zero(temp_vector1);
2458 solver.connect_eigenvalues_slot(
2459 [&eigenvalue_tracker](
const std::vector<double> &
eigenvalues) {
2463 solver.solve(*matrix_ptr,
2466 *
data.preconditioner);
2468 info.cg_iterations = control.last_step();
2470 else if (
data.eigenvalue_algorithm ==
2476 ExcMessage(
"Cannot estimate the minimal eigenvalue with the "
2477 "power iteration"));
2479 eigenvalue_tracker.values.push_back(
2482 *
data.preconditioner,
2483 data.eig_cg_n_iterations));
2489 if (eigenvalue_tracker.values.empty())
2490 info.min_eigenvalue_estimate = info.max_eigenvalue_estimate = 1.;
2493 info.min_eigenvalue_estimate = eigenvalue_tracker.values.front();
2497 info.max_eigenvalue_estimate =
2498 1.2 * eigenvalue_tracker.values.back();
2503 info.max_eigenvalue_estimate =
data.max_eigenvalue;
2504 info.min_eigenvalue_estimate =
2505 data.max_eigenvalue /
data.smoothing_range;
2519template <
typename MatrixType>
2529template <
typename VectorType>
2538template <
typename VectorType>
2545template <
typename VectorType>
2554template <
typename VectorType>
2586 const double relaxation)
2587 : relaxation(relaxation)
2596 AdditionalData add_data;
2597 relaxation = add_data.relaxation;
2611template <
typename MatrixType>
2614 const MatrixType &matrix,
2624template <
typename VectorType>
2629 std::is_same_v<size_type, typename VectorType::size_type>,
2630 "PreconditionRichardson and VectorType must have the same size_type.");
2637template <
typename VectorType>
2642 std::is_same_v<size_type, typename VectorType::size_type>,
2643 "PreconditionRichardson and VectorType must have the same size_type.");
2648template <
typename VectorType>
2653 std::is_same_v<size_type, typename VectorType::size_type>,
2654 "PreconditionRichardson and VectorType must have the same size_type.");
2661template <
typename VectorType>
2666 std::is_same_v<size_type, typename VectorType::size_type>,
2667 "PreconditionRichardson and VectorType must have the same size_type.");
2688template <
typename MatrixType,
typename PreconditionerType>
2691 const MatrixType &rA,
2692 const AdditionalData ¶meters)
2695 eigenvalues_are_initialized =
false;
2699 this->data = parameters;
2703template <
typename MatrixType,
typename PreconditionerType>
2707 eigenvalues_are_initialized =
false;
2709 data.relaxation = 1.0;
2710 data.preconditioner =
nullptr;
2713template <
typename MatrixType,
typename PreconditionerType>
2722template <
typename MatrixType,
typename PreconditionerType>
2731template <
typename MatrixType,
typename PreconditionerType>
2732template <
typename VectorType>
2736 const VectorType &src)
const
2741 if (eigenvalues_are_initialized ==
false)
2742 estimate_eigenvalues(src);
2746 for (
unsigned int i = 0; i <
data.n_iterations; ++i)
2747 internal::PreconditionRelaxation::step_operations(*A,
2748 *
data.preconditioner,
2758template <
typename MatrixType,
typename PreconditionerType>
2759template <
typename VectorType>
2763 const VectorType &src)
const
2768 if (eigenvalues_are_initialized ==
false)
2769 estimate_eigenvalues(src);
2773 for (
unsigned int i = 0; i <
data.n_iterations; ++i)
2774 internal::PreconditionRelaxation::step_operations(
2775 *A, *
data.preconditioner, dst, src,
data.relaxation, tmp1, tmp2, i,
true);
2778template <
typename MatrixType,
typename PreconditionerType>
2779template <
typename VectorType>
2783 const VectorType &src)
const
2788 if (eigenvalues_are_initialized ==
false)
2789 estimate_eigenvalues(src);
2793 for (
unsigned int i = 1; i <=
data.n_iterations; ++i)
2794 internal::PreconditionRelaxation::step_operations(*A,
2795 *
data.preconditioner,
2805template <
typename MatrixType,
typename PreconditionerType>
2806template <
typename VectorType>
2810 const VectorType &src)
const
2815 if (eigenvalues_are_initialized ==
false)
2816 estimate_eigenvalues(src);
2820 for (
unsigned int i = 1; i <=
data.n_iterations; ++i)
2821 internal::PreconditionRelaxation::step_operations(
2822 *A, *
data.preconditioner, dst, src,
data.relaxation, tmp1, tmp2, i,
true);
2825template <
typename MatrixType,
typename PreconditionerType>
2826template <
typename VectorType>
2829 const VectorType &src)
const
2833 EigenvalueInformation info;
2835 if (
data.relaxation == 0.0)
2839 solution_old.reinit(src);
2840 temp_vector1.reinit(src,
true);
2842 info = internal::estimate_eigenvalues<MatrixType>(
2843 data, A, solution_old, temp_vector1,
data.n_iterations);
2845 const double alpha =
2846 (
data.smoothing_range > 1. ?
2847 info.max_eigenvalue_estimate /
data.smoothing_range :
2848 std::min(0.9 * info.max_eigenvalue_estimate,
2849 info.min_eigenvalue_estimate));
2852 ->
data.relaxation = 2.0 / (alpha + info.max_eigenvalue_estimate);
2856 ->eigenvalues_are_initialized =
true;
2861template <
typename MatrixType,
typename PreconditionerType>
2865 return data.relaxation;
2871template <
typename MatrixType>
2874 const AdditionalData ¶meters_in)
2878 parameters_in.relaxation != 0.0,
2880 "Relaxation cannot automatically be determined by PreconditionJacobi."));
2882 AdditionalData parameters;
2883 parameters.relaxation = 1.0;
2884 parameters.n_iterations = parameters_in.n_iterations;
2885 parameters.preconditioner =
2886 std::make_shared<PreconditionerType>(A, parameters_in.relaxation);
2888 this->BaseClass::initialize(A, parameters);
2893template <
typename MatrixType>
2896 const AdditionalData ¶meters_in)
2900 parameters_in.relaxation != 0.0,
2902 "Relaxation cannot automatically be determined by PreconditionSOR."));
2904 AdditionalData parameters;
2905 parameters.relaxation = 1.0;
2906 parameters.n_iterations = parameters_in.n_iterations;
2907 parameters.preconditioner =
2908 std::make_shared<PreconditionerType>(A, parameters_in.relaxation);
2910 this->BaseClass::initialize(A, parameters);
2915template <
typename MatrixType>
2918 const AdditionalData ¶meters_in)
2922 parameters_in.relaxation != 0.0,
2924 "Relaxation cannot automatically be determined by PreconditionSSOR."));
2926 AdditionalData parameters;
2927 parameters.relaxation = 1.0;
2928 parameters.n_iterations = parameters_in.n_iterations;
2929 parameters.preconditioner =
2930 std::make_shared<PreconditionerType>(A, parameters_in.relaxation);
2932 this->BaseClass::initialize(A, parameters);
2939template <
typename MatrixType>
2942 const MatrixType &A,
2943 const std::vector<size_type> &p,
2944 const std::vector<size_type> &ip,
2945 const typename BaseClass::AdditionalData ¶meters_in)
2949 parameters_in.relaxation != 0.0,
2951 "Relaxation cannot automatically be determined by PreconditionPSOR."));
2953 typename BaseClass::AdditionalData parameters;
2954 parameters.relaxation = 1.0;
2955 parameters.n_iterations = parameters_in.n_iterations;
2956 parameters.preconditioner =
2957 std::make_shared<PreconditionerType>(A, parameters_in.relaxation, p, ip);
2959 this->BaseClass::initialize(A, parameters);
2963template <
typename MatrixType>
2966 const AdditionalData &additional_data)
2969 additional_data.permutation,
2970 additional_data.inverse_permutation,
2971 additional_data.parameters);
2974template <
typename MatrixType>
2976 const std::vector<size_type> &permutation,
2977 const std::vector<size_type> &inverse_permutation,
2980 : permutation(permutation)
2981 , inverse_permutation(inverse_permutation)
2982 , parameters(parameters)
2989template <
typename MatrixType,
typename VectorType>
2991 const MatrixType &M,
2992 const function_ptr method)
2994 , precondition(method)
2999template <
typename MatrixType,
typename VectorType>
3003 const VectorType &src)
const
3005 (
matrix.*precondition)(dst, src);
3013 template <
typename PreconditionerType>
3016 const double smoothing_range,
3017 const unsigned int eig_cg_n_iterations,
3018 const double eig_cg_residual,
3019 const double max_eigenvalue,
3021 : smoothing_range(smoothing_range)
3022 , eig_cg_n_iterations(eig_cg_n_iterations)
3023 , eig_cg_residual(eig_cg_residual)
3024 , max_eigenvalue(max_eigenvalue)
3025 , eigenvalue_algorithm(eigenvalue_algorithm)
3030 template <
typename PreconditionerType>
3031 inline EigenvalueAlgorithmAdditionalData<PreconditionerType> &
3032 EigenvalueAlgorithmAdditionalData<PreconditionerType>::operator=(
3033 const EigenvalueAlgorithmAdditionalData &other_data)
3035 smoothing_range = other_data.smoothing_range;
3036 eig_cg_n_iterations = other_data.eig_cg_n_iterations;
3037 eig_cg_residual = other_data.eig_cg_residual;
3038 max_eigenvalue = other_data.max_eigenvalue;
3039 preconditioner = other_data.preconditioner;
3040 eigenvalue_algorithm = other_data.eigenvalue_algorithm;
3041 constraints.copy_from(other_data.constraints);
3047template <
typename MatrixType,
typename PreconditionerType>
3050 const unsigned int n_iterations,
3051 const double smoothing_range,
3052 const unsigned int eig_cg_n_iterations,
3053 const double eig_cg_residual,
3054 const double max_eigenvalue,
3055 const EigenvalueAlgorithm eigenvalue_algorithm)
3056 :
internal::EigenvalueAlgorithmAdditionalData<PreconditionerType>(
3058 eig_cg_n_iterations,
3061 eigenvalue_algorithm)
3062 , relaxation(relaxation)
3063 , n_iterations(n_iterations)
3072 namespace PreconditionChebyshevImplementation
3080 template <
typename VectorType,
typename PreconditionerType>
3082 vector_updates(
const VectorType &rhs,
3083 const PreconditionerType &preconditioner,
3084 const unsigned int iteration_index,
3085 const double factor1,
3086 const double factor2,
3087 VectorType &solution_old,
3088 VectorType &temp_vector1,
3089 VectorType &temp_vector2,
3090 VectorType &solution)
3092 if (iteration_index == 0)
3094 solution.equ(factor2, rhs);
3095 preconditioner.vmult(solution_old, solution);
3097 else if (iteration_index == 1)
3100 temp_vector1.sadd(-1.0, 1.0, rhs);
3101 preconditioner.vmult(solution_old, temp_vector1);
3104 solution_old.sadd(factor2, 1 + factor1, solution);
3109 temp_vector1.sadd(-1.0, 1.0, rhs);
3110 preconditioner.vmult(temp_vector2, temp_vector1);
3113 solution_old.sadd(-factor1, factor2, temp_vector2);
3114 solution_old.add(1 + factor1, solution);
3117 solution.swap(solution_old);
3123 typename PreconditionerType,
3125 !has_vmult_with_std_functions_for_precondition<
3132 const PreconditionerType &preconditioner,
3133 const unsigned int iteration_index,
3134 const double factor1_,
3135 const double factor2_,
3144 const Number factor1 = factor1_;
3145 const Number factor1_plus_1 = 1. + factor1_;
3146 const Number factor2 = factor2_;
3148 if (iteration_index == 0)
3151 preconditioner.vmult(solution_old, rhs);
3154 const auto solution_old_ptr = solution_old.
begin();
3157 solution_old_ptr[i] = solution_old_ptr[i] * factor2;
3159 else if (iteration_index == 1)
3162 temp_vector1.
sadd(-1.0, 1.0, rhs);
3164 preconditioner.vmult(solution_old, temp_vector1);
3167 const auto solution_ptr = solution.
begin();
3168 const auto solution_old_ptr = solution_old.
begin();
3171 solution_old_ptr[i] =
3172 factor1_plus_1 * solution_ptr[i] + solution_old_ptr[i] * factor2;
3177 temp_vector1.
sadd(-1.0, 1.0, rhs);
3179 preconditioner.vmult(temp_vector2, temp_vector1);
3182 const auto solution_ptr = solution.
begin();
3183 const auto solution_old_ptr = solution_old.
begin();
3184 const auto temp_vector2_ptr = temp_vector2.
begin();
3187 solution_old_ptr[i] = factor1_plus_1 * solution_ptr[i] -
3188 factor1 * solution_old_ptr[i] +
3189 temp_vector2_ptr[i] * factor2;
3192 solution.
swap(solution_old);
3197 typename PreconditionerType,
3199 has_vmult_with_std_functions_for_precondition<
3206 const PreconditionerType &preconditioner,
3207 const unsigned int iteration_index,
3208 const double factor1_,
3209 const double factor2_,
3218 const Number factor1 = factor1_;
3219 const Number factor1_plus_1 = 1. + factor1_;
3220 const Number factor2 = factor2_;
3222 const auto rhs_ptr = rhs.
begin();
3223 const auto temp_vector1_ptr = temp_vector1.
begin();
3224 const auto temp_vector2_ptr = temp_vector2.
begin();
3225 const auto solution_ptr = solution.
begin();
3226 const auto solution_old_ptr = solution_old.
begin();
3228 if (iteration_index == 0)
3230 preconditioner.vmult(
3233 [&](
const auto start_range,
const auto end_range) {
3234 if (end_range > start_range)
3235 std::memset(solution_ptr + start_range,
3237 sizeof(Number) * (end_range - start_range));
3239 [&](
const auto begin,
const auto end) {
3241 for (std::size_t i = begin; i <
end; ++i)
3242 solution_ptr[i] *= factor2;
3247 preconditioner.vmult(
3250 [&](
const auto begin,
const auto end) {
3252 std::memset(temp_vector2_ptr + begin,
3254 sizeof(Number) * (end - begin));
3257 for (std::size_t i = begin; i <
end; ++i)
3258 temp_vector1_ptr[i] = rhs_ptr[i] - temp_vector1_ptr[i];
3260 [&](
const auto begin,
const auto end) {
3261 if (iteration_index == 1)
3264 for (std::size_t i = begin; i <
end; ++i)
3265 temp_vector2_ptr[i] = factor1_plus_1 * solution_ptr[i] +
3266 factor2 * temp_vector2_ptr[i];
3271 for (std::size_t i = begin; i <
end; ++i)
3272 temp_vector2_ptr[i] = factor1_plus_1 * solution_ptr[i] -
3273 factor1 * solution_old_ptr[i] +
3274 factor2 * temp_vector2_ptr[i];
3279 if (iteration_index > 0)
3281 solution_old.
swap(temp_vector2);
3282 solution_old.
swap(solution);
3289 template <
typename Number>
3290 struct VectorUpdater
3292 VectorUpdater(
const Number *rhs,
3293 const Number *matrix_diagonal_inverse,
3294 const unsigned int iteration_index,
3295 const Number factor1,
3296 const Number factor2,
3297 Number *solution_old,
3301 , matrix_diagonal_inverse(matrix_diagonal_inverse)
3302 , iteration_index(iteration_index)
3305 , solution_old(solution_old)
3306 , tmp_vector(tmp_vector)
3307 , solution(solution)
3311 apply_to_subrange(
const std::size_t begin,
const std::size_t end)
const
3317 const Number factor1 = this->factor1;
3318 const Number factor1_plus_1 = 1. + this->factor1;
3319 const Number factor2 = this->factor2;
3320 if (iteration_index == 0)
3323 for (std::size_t i = begin; i <
end; ++i)
3324 solution[i] = factor2 * matrix_diagonal_inverse[i] * rhs[i];
3326 else if (iteration_index == 1)
3330 for (std::size_t i = begin; i <
end; ++i)
3334 factor1_plus_1 * solution[i] +
3335 factor2 * matrix_diagonal_inverse[i] * (rhs[i] - tmp_vector[i]);
3342 for (std::size_t i = begin; i <
end; ++i)
3349 factor1_plus_1 * solution[i] - factor1 * solution_old[i] +
3350 factor2 * matrix_diagonal_inverse[i] * (rhs[i] - tmp_vector[i]);
3355 const Number *matrix_diagonal_inverse;
3356 const unsigned int iteration_index;
3357 const Number factor1;
3358 const Number factor2;
3359 mutable Number *solution_old;
3360 mutable Number *tmp_vector;
3361 mutable Number *solution;
3364 template <
typename Number>
3367 VectorUpdatesRange(
const VectorUpdater<Number> &updater,
3368 const std::size_t
size)
3372 VectorUpdatesRange::apply_to_subrange(0,
size);
3380 ~VectorUpdatesRange()
override =
default;
3383 apply_to_subrange(
const std::size_t begin,
3384 const std::size_t end)
const override
3386 updater.apply_to_subrange(begin, end);
3389 const VectorUpdater<Number> &updater;
3393 template <
typename Number>
3396 const ::Vector<Number> &rhs,
3398 const unsigned int iteration_index,
3399 const double factor1,
3400 const double factor2,
3406 VectorUpdater<Number> upd(rhs.begin(),
3407 jacobi.get_vector().begin(),
3411 solution_old.
begin(),
3412 temp_vector1.
begin(),
3414 VectorUpdatesRange<Number>(upd, rhs.size());
3417 if (iteration_index == 0)
3424 solution.
swap(temp_vector1);
3425 solution_old.
swap(temp_vector1);
3430 template <
typename Number>
3434 const ::DiagonalMatrix<
3436 const unsigned int iteration_index,
3437 const double factor1,
3438 const double factor2,
3446 VectorUpdater<Number> upd(rhs.
begin(),
3447 jacobi.get_vector().begin(),
3451 solution_old.
begin(),
3452 temp_vector1.
begin(),
3457 if (iteration_index == 0)
3464 solution.
swap(temp_vector1);
3465 solution_old.
swap(temp_vector1);
3476 typename PreconditionerType,
3480 PreconditionerType> &&
3481 !(has_vmult_with_std_functions_for_precondition<PreconditionerType,
3483 has_vmult_with_std_functions_for_precondition<
MatrixType,
3487 vmult_and_update(
const MatrixType &matrix,
3488 const PreconditionerType &preconditioner,
3489 const VectorType &rhs,
3490 const unsigned int iteration_index,
3491 const double factor1,
3492 const double factor2,
3493 VectorType &solution,
3494 VectorType &solution_old,
3495 VectorType &temp_vector1,
3496 VectorType &temp_vector2)
3498 if (iteration_index > 0)
3499 matrix.vmult(temp_vector1, solution);
3516 typename PreconditionerType,
3520 PreconditionerType> &&
3521 (has_vmult_with_std_functions_for_precondition<PreconditionerType,
3523 has_vmult_with_std_functions_for_precondition<
MatrixType,
3527 vmult_and_update(
const MatrixType &matrix,
3528 const PreconditionerType &preconditioner,
3529 const VectorType &rhs,
3530 const unsigned int iteration_index,
3531 const double factor1_,
3532 const double factor2_,
3533 VectorType &solution,
3534 VectorType &solution_old,
3535 VectorType &temp_vector1,
3536 VectorType &temp_vector2)
3538 using Number =
typename VectorType::value_type;
3540 const Number factor1 = factor1_;
3541 const Number factor1_plus_1 = 1. + factor1_;
3542 const Number factor2 = factor2_;
3544 if (iteration_index == 0)
3546 preconditioner.vmult(
3549 [&](
const unsigned int start_range,
const unsigned int end_range) {
3551 if (end_range > start_range)
3552 std::memset(solution.begin() + start_range,
3554 sizeof(Number) * (end_range - start_range));
3556 [&](
const unsigned int start_range,
const unsigned int end_range) {
3557 const auto solution_ptr = solution.begin();
3560 for (std::size_t i = start_range; i < end_range; ++i)
3561 solution_ptr[i] *= factor2;
3566 temp_vector1.reinit(rhs,
true);
3567 temp_vector2.reinit(rhs,
true);
3573 [&](
const unsigned int start_range,
const unsigned int end_range) {
3576 if (end_range > start_range)
3577 std::memset(temp_vector1.begin() + start_range,
3579 sizeof(Number) * (end_range - start_range));
3581 [&](
const unsigned int start_range,
const unsigned int end_range) {
3582 const auto rhs_ptr = rhs.begin();
3583 const auto tmp_ptr = temp_vector1.begin();
3586 for (std::size_t i = start_range; i < end_range; ++i)
3587 tmp_ptr[i] = rhs_ptr[i] - tmp_ptr[i];
3591 preconditioner.vmult(
3594 [&](
const unsigned int start_range,
const unsigned int end_range) {
3597 if (end_range > start_range)
3598 std::memset(temp_vector2.begin() + start_range,
3600 sizeof(Number) * (end_range - start_range));
3602 [&](
const unsigned int start_range,
const unsigned int end_range) {
3603 const auto solution_ptr = solution.begin();
3604 const auto solution_old_ptr = solution_old.begin();
3605 const auto tmp_ptr = temp_vector2.begin();
3607 if (iteration_index == 1)
3610 for (std::size_t i = start_range; i < end_range; ++i)
3612 factor1_plus_1 * solution_ptr[i] + factor2 * tmp_ptr[i];
3617 for (std::size_t i = start_range; i < end_range; ++i)
3618 tmp_ptr[i] = factor1_plus_1 * solution_ptr[i] -
3619 factor1 * solution_old_ptr[i] +
3620 factor2 * tmp_ptr[i];
3624 solution.swap(temp_vector2);
3625 solution_old.swap(temp_vector2);
3633 typename PreconditionerType,
3634 std::enable_if_t<has_vmult_with_std_functions<
MatrixType,
3636 PreconditionerType>,
3639 vmult_and_update(
const MatrixType &matrix,
3640 const PreconditionerType &preconditioner,
3641 const VectorType &rhs,
3642 const unsigned int iteration_index,
3643 const double factor1,
3644 const double factor2,
3645 VectorType &solution,
3646 VectorType &solution_old,
3647 VectorType &temp_vector1,
3650 using Number =
typename VectorType::value_type;
3651 VectorUpdater<Number> updater(rhs.begin(),
3652 preconditioner.get_vector().begin(),
3656 solution_old.begin(),
3657 temp_vector1.begin(),
3659 if (iteration_index > 0)
3663 [&](
const unsigned int start_range,
const unsigned int end_range) {
3666 if (end_range > start_range)
3667 std::memset(temp_vector1.begin() + start_range,
3669 sizeof(Number) * (end_range - start_range));
3671 [&](
const unsigned int start_range,
const unsigned int end_range) {
3672 if (end_range > start_range)
3673 updater.apply_to_subrange(start_range, end_range);
3676 updater.apply_to_subrange(0U, solution.locally_owned_size());
3679 if (iteration_index == 0)
3686 solution.swap(temp_vector1);
3687 solution_old.swap(temp_vector1);
3691 template <
typename MatrixType,
typename PreconditionerType>
3693 initialize_preconditioner(
3694 const MatrixType &matrix,
3695 std::shared_ptr<PreconditionerType> &preconditioner)
3698 (void)preconditioner;
3702 template <
typename MatrixType,
typename VectorType>
3704 initialize_preconditioner(
3705 const MatrixType &matrix,
3708 if (preconditioner.get() ==
nullptr || preconditioner->m() !=
matrix.m())
3710 if (preconditioner.get() ==
nullptr)
3712 std::make_shared<::DiagonalMatrix<VectorType>>();
3715 preconditioner->m() == 0,
3717 "Preconditioner appears to be initialized but not sized correctly"));
3720 if (preconditioner->m() !=
matrix.m())
3722 preconditioner->get_vector().reinit(
matrix.m());
3723 for (
typename VectorType::size_type i = 0; i <
matrix.m(); ++i)
3724 preconditioner->get_vector()(i) = 1. /
matrix.el(i, i);
3733template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
3736 const double smoothing_range,
3737 const unsigned int eig_cg_n_iterations,
3738 const double eig_cg_residual,
3739 const double max_eigenvalue,
3740 const EigenvalueAlgorithm eigenvalue_algorithm,
3741 const PolynomialType polynomial_type)
3742 :
internal::EigenvalueAlgorithmAdditionalData<PreconditionerType>(
3744 eig_cg_n_iterations,
3747 eigenvalue_algorithm)
3749 , polynomial_type(polynomial_type)
3754template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
3759 , eigenvalues_are_initialized(false)
3762 std::is_same_v<size_type, typename VectorType::size_type>,
3763 "PreconditionChebyshev and VectorType must have the same size_type.");
3768template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
3771 const MatrixType &matrix,
3772 const AdditionalData &additional_data)
3775 data = additional_data;
3777 ExcMessage(
"The degree of the Chebyshev method must be positive."));
3778 internal::PreconditionChebyshevImplementation::initialize_preconditioner(
3779 matrix,
data.preconditioner);
3780 eigenvalues_are_initialized =
false;
3785template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
3789 eigenvalues_are_initialized =
false;
3790 theta = delta = 1.0;
3791 matrix_ptr =
nullptr;
3794 solution_old.reinit(empty_vector);
3795 temp_vector1.reinit(empty_vector);
3796 temp_vector2.reinit(empty_vector);
3798 data.preconditioner.reset();
3803template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
3810 solution_old.reinit(src);
3811 temp_vector1.reinit(src,
true);
3813 auto info = internal::estimate_eigenvalues<MatrixType>(
3814 data, matrix_ptr, solution_old, temp_vector1,
data.degree);
3816 const double alpha = (
data.smoothing_range > 1. ?
3817 info.max_eigenvalue_estimate /
data.smoothing_range :
3818 std::min(0.9 * info.max_eigenvalue_estimate,
3819 info.min_eigenvalue_estimate));
3828 const double actual_range = info.max_eigenvalue_estimate / alpha;
3829 const double sigma = (1. -
std::sqrt(1. / actual_range)) /
3831 const double eps =
data.smoothing_range;
3836 1 +
static_cast<unsigned int>(
3841 info.degree =
data.degree;
3846 (
data.polynomial_type == AdditionalData::PolynomialType::fourth_kind) ?
3847 (info.max_eigenvalue_estimate) :
3848 ((info.max_eigenvalue_estimate - alpha) * 0.5);
3851 ->theta = (info.max_eigenvalue_estimate + alpha) * 0.5;
3855 using NumberType =
typename VectorType::value_type;
3863 (std::is_same_v<VectorType,
3866 temp_vector2.
reinit(src, true);
3870 temp_vector2.reinit(empty_vector);
3875 ->eigenvalues_are_initialized =
true;
3882template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
3885 VectorType &solution,
3886 const VectorType &rhs)
const
3888 std::lock_guard<std::mutex> lock(mutex);
3889 if (eigenvalues_are_initialized ==
false)
3890 estimate_eigenvalues(rhs);
3892 internal::PreconditionChebyshevImplementation::vmult_and_update(
3894 *
data.preconditioner,
3898 (
data.polynomial_type == AdditionalData::PolynomialType::fourth_kind) ?
3899 (4. / (3. * delta)) :
3911 double rhok = delta / theta, sigma = theta / delta;
3912 for (
unsigned int k = 0; k <
data.degree - 1; ++k)
3914 double factor1 = 0.0;
3915 double factor2 = 0.0;
3917 if (
data.polynomial_type == AdditionalData::PolynomialType::fourth_kind)
3919 factor1 = (2 * k + 1.) / (2 * k + 5.);
3920 factor2 = (8 * k + 12.) / (delta * (2 * k + 5.));
3924 const double rhokp = 1. / (2. * sigma - rhok);
3925 factor1 = rhokp * rhok;
3926 factor2 = 2. * rhokp / delta;
3930 internal::PreconditionChebyshevImplementation::vmult_and_update(
3932 *
data.preconditioner,
3946template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
3949 VectorType &solution,
3950 const VectorType &rhs)
const
3952 std::lock_guard<std::mutex> lock(mutex);
3953 if (eigenvalues_are_initialized ==
false)
3954 estimate_eigenvalues(rhs);
3956 internal::PreconditionChebyshevImplementation::vector_updates(
3958 *
data.preconditioner,
3961 (
data.polynomial_type == AdditionalData::PolynomialType::fourth_kind) ?
3962 (4. / (3. * delta)) :
3972 double rhok = delta / theta, sigma = theta / delta;
3973 for (
unsigned int k = 0; k <
data.degree - 1; ++k)
3975 double factor1 = 0.0;
3976 double factor2 = 0.0;
3978 if (
data.polynomial_type == AdditionalData::PolynomialType::fourth_kind)
3980 factor1 = (2 * k + 1.) / (2 * k + 5.);
3981 factor2 = (8 * k + 12.) / (delta * (2 * k + 5.));
3985 const double rhokp = 1. / (2. * sigma - rhok);
3986 factor1 = rhokp * rhok;
3987 factor2 = 2. * rhokp / delta;
3991 matrix_ptr->Tvmult(temp_vector1, solution);
3992 internal::PreconditionChebyshevImplementation::vector_updates(
3994 *
data.preconditioner,
4007template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
4010 VectorType &solution,
4011 const VectorType &rhs)
const
4013 std::lock_guard<std::mutex> lock(mutex);
4014 if (eigenvalues_are_initialized ==
false)
4015 estimate_eigenvalues(rhs);
4017 internal::PreconditionChebyshevImplementation::vmult_and_update(
4019 *
data.preconditioner,
4023 (
data.polynomial_type == AdditionalData::PolynomialType::fourth_kind) ?
4024 (4. / (3. * delta)) :
4034 double rhok = delta / theta, sigma = theta / delta;
4035 for (
unsigned int k = 0; k <
data.degree - 1; ++k)
4037 double factor1 = 0.0;
4038 double factor2 = 0.0;
4040 if (
data.polynomial_type == AdditionalData::PolynomialType::fourth_kind)
4042 factor1 = (2 * k + 1.) / (2 * k + 5.);
4043 factor2 = (8 * k + 12.) / (delta * (2 * k + 5.));
4047 const double rhokp = 1. / (2. * sigma - rhok);
4048 factor1 = rhokp * rhok;
4049 factor2 = 2. * rhokp / delta;
4053 internal::PreconditionChebyshevImplementation::vmult_and_update(
4055 *
data.preconditioner,
4069template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
4072 VectorType &solution,
4073 const VectorType &rhs)
const
4075 std::lock_guard<std::mutex> lock(mutex);
4076 if (eigenvalues_are_initialized ==
false)
4077 estimate_eigenvalues(rhs);
4079 matrix_ptr->Tvmult(temp_vector1, solution);
4080 internal::PreconditionChebyshevImplementation::vector_updates(
4082 *
data.preconditioner,
4085 (
data.polynomial_type == AdditionalData::PolynomialType::fourth_kind) ?
4086 (4. / (3. * delta)) :
4096 double rhok = delta / theta, sigma = theta / delta;
4097 for (
unsigned int k = 0; k <
data.degree - 1; ++k)
4099 double factor1 = 0.0;
4100 double factor2 = 0.0;
4102 if (
data.polynomial_type == AdditionalData::PolynomialType::fourth_kind)
4104 factor1 = (2 * k + 1.) / (2 * k + 5.);
4105 factor2 = (8 * k + 12.) / (delta * (2 * k + 5.));
4109 const double rhokp = 1. / (2. * sigma - rhok);
4110 factor1 = rhokp * rhok;
4111 factor2 = 2. * rhokp / delta;
4115 matrix_ptr->Tvmult(temp_vector1, solution);
4116 internal::PreconditionChebyshevImplementation::vector_updates(
4118 *
data.preconditioner,
4131template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
4134 PreconditionerType>::size_type
4138 return matrix_ptr->m();
4143template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
4146 PreconditionerType>::size_type
4150 return matrix_ptr->n();
unsigned int n_blocks() const
BlockType & block(const unsigned int i)
size_type nth_index_in_set(const size_type local_index) const
void sadd(const Number s, const Number a, const Vector< Number, MemorySpace > &V)
Number mean_value() const
Number * get_values() const
size_type locally_owned_size() const
void swap(Vector< Number, MemorySpace > &v) noexcept
::IndexSet locally_owned_elements() const
void Tvmult(VectorType &dst, const VectorType &src) const
void step(VectorType &dst, const VectorType &src) const
EigenvalueInformation estimate_eigenvalues(const VectorType &src) const
void Tstep(VectorType &dst, const VectorType &src) const
void vmult(VectorType &dst, const VectorType &src) const
void initialize(const MatrixType &matrix, const AdditionalData &additional_data=AdditionalData())
bool eigenvalues_are_initialized
ObserverPointer< const MatrixType, PreconditionChebyshev< MatrixType, VectorType, PreconditionerType > > matrix_ptr
void vmult_add(VectorType &, const VectorType &) const
void vmult(VectorType &, const VectorType &) const
void initialize(const MatrixType &matrix, const AdditionalData &additional_data=AdditionalData())
void Tvmult(VectorType &, const VectorType &) const
void Tvmult_add(VectorType &, const VectorType &) const
typename BaseClass::AdditionalData AdditionalData
internal::PreconditionRelaxation::PreconditionJacobiImpl< MatrixType > PreconditionerType
void initialize(const MatrixType &A, const AdditionalData ¶meters=AdditionalData())
AdditionalData(const std::vector< size_type > &permutation, const std::vector< size_type > &inverse_permutation, const typename BaseClass::AdditionalData ¶meters=typename BaseClass::AdditionalData())
BaseClass::AdditionalData parameters
const std::vector< size_type > & inverse_permutation
const std::vector< size_type > & permutation
typename BaseClass::size_type size_type
void initialize(const MatrixType &A, const AdditionalData &additional_data)
internal::PreconditionRelaxation::PreconditionPSORImpl< MatrixType > PreconditionerType
void initialize(const MatrixType &A, const std::vector< size_type > &permutation, const std::vector< size_type > &inverse_permutation, const typename BaseClass::AdditionalData ¶meters=typename BaseClass::AdditionalData())
unsigned int n_iterations
AdditionalData(const double relaxation=1., const unsigned int n_iterations=1, const double smoothing_range=0., const unsigned int eig_cg_n_iterations=8, const double eig_cg_residual=1e-2, const double max_eigenvalue=1, const EigenvalueAlgorithm eigenvalue_algorithm=EigenvalueAlgorithm::lanczos)
double get_relaxation() const
void Tvmult(VectorType &, const VectorType &) const
std::shared_ptr< PreconditionerType > preconditioner
EigenvalueInformation estimate_eigenvalues(const VectorType &src) const
void step(VectorType &x, const VectorType &rhs) const
ObserverPointer< const MatrixType, PreconditionRelaxation< MatrixType > > A
void initialize(const MatrixType &A, const AdditionalData ¶meters=AdditionalData())
bool eigenvalues_are_initialized
void Tstep(VectorType &x, const VectorType &rhs) const
void vmult(VectorType &, const VectorType &) const
types::global_dof_index size_type
AdditionalData(const double relaxation=1.)
types::global_dof_index size_type
void vmult_add(VectorType &, const VectorType &) const
void initialize(const AdditionalData ¶meters)
void initialize(const MatrixType &matrix, const AdditionalData ¶meters)
void vmult(VectorType &, const VectorType &) const
void Tvmult(VectorType &, const VectorType &) const
void Tvmult_add(VectorType &, const VectorType &) const
internal::PreconditionRelaxation::PreconditionSORImpl< MatrixType > PreconditionerType
typename BaseClass::AdditionalData AdditionalData
void initialize(const MatrixType &A, const AdditionalData ¶meters=AdditionalData())
void initialize(const MatrixType &A, const AdditionalData ¶meters=AdditionalData())
typename BaseClass::AdditionalData AdditionalData
internal::PreconditionRelaxation::PreconditionSSORImpl< MatrixType > PreconditionerType
void(MatrixType::*)(VectorType &, const VectorType &) const function_ptr
const function_ptr precondition
const MatrixType & matrix
void vmult(VectorType &dst, const VectorType &src) const
PreconditionUseMatrix(const MatrixType &M, const function_ptr method)
const_iterator end() const
const_iterator begin() const
void add(const std::vector< size_type > &indices, const std::vector< OtherNumber > &values)
Number mean_value() const
virtual size_type size() const override
virtual void swap(Vector< Number > &v) noexcept
#define DEAL_II_OPENMP_SIMD_PRAGMA
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcNotInitialized()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
#define DEAL_II_NOT_IMPLEMENTED()
std::vector< index_type > data
@ matrix
Contents is actually a matrix.
Tpetra::Vector< Number, LO, GO, NodeType< MemorySpace > > VectorType
Tpetra::CrsMatrix< Number, LO, GO, NodeType< MemorySpace > > MatrixType
VectorType::value_type * end(VectorType &V)
VectorType::value_type * begin(VectorType &V)
unsigned int minimum_parallel_grain_size
void reinit(MatrixBlock< MatrixType > &v, const BlockSparsityPattern &p)
static const unsigned int invalid_unsigned_int
::VectorizedArray< Number, width > log(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
unsigned int global_dof_index
PolynomialType polynomial_type
AdditionalData(const unsigned int degree=1, const double smoothing_range=0., const unsigned int eig_cg_n_iterations=8, const double eig_cg_residual=1e-2, const double max_eigenvalue=1, const EigenvalueAlgorithm eigenvalue_algorithm=EigenvalueAlgorithm::lanczos, const PolynomialType polynomial_type=PolynomialType::first_kind)
std::shared_ptr< PreconditionerType > preconditioner
EigenvalueAlgorithmAdditionalData< PreconditionerType > & operator=(const EigenvalueAlgorithmAdditionalData< PreconditionerType > &other_data)
::AffineConstraints< double > constraints
EigenvalueAlgorithmAdditionalData(const double smoothing_range, const unsigned int eig_cg_n_iterations, const double eig_cg_residual, const double max_eigenvalue, const EigenvalueAlgorithm eigenvalue_algorithm)
unsigned int eig_cg_n_iterations
EigenvalueAlgorithm eigenvalue_algorithm
std::array< Number, 1 > eigenvalues(const SymmetricTensor< 2, 1, Number > &T)