13#ifndef dealii_precondition_h
14#define dealii_precondition_h
31#include <Kokkos_Core.hpp>
39template <
typename number>
41template <
typename number>
47 template <
typename,
typename>
49 template <
typename,
typename>
126 template <
typename PreconditionerType>
256 template <
typename MatrixType>
264 template <
typename VectorType>
266 vmult(VectorType &,
const VectorType &)
const;
272 template <
typename VectorType>
274 Tvmult(VectorType &,
const VectorType &)
const;
279 template <
typename VectorType>
287 template <
typename VectorType>
385 template <
typename MatrixType>
392 template <
typename VectorType>
394 vmult(VectorType &,
const VectorType &)
const;
400 template <
typename VectorType>
402 Tvmult(VectorType &,
const VectorType &)
const;
406 template <
typename VectorType>
414 template <
typename VectorType>
504template <
typename MatrixType = SparseMatrix<
double>,
505 typename VectorType = Vector<
double>>
513 const VectorType &)
const;
527 vmult(VectorType &dst,
const VectorType &src)
const;
572template <
typename MatrixType = SparseMatrix<
double>,
573 typename PreconditionerType = IdentityMatrix>
601 EigenvalueAlgorithm::lanczos,
623 const AdditionalData ¶meters = AdditionalData());
648 template <
typename VectorType>
650 vmult(VectorType &,
const VectorType &)
const;
656 template <
typename VectorType>
658 Tvmult(VectorType &,
const VectorType &)
const;
663 template <
typename VectorType>
665 step(VectorType &x,
const VectorType &rhs)
const;
670 template <
typename VectorType>
672 Tstep(VectorType &x,
const VectorType &rhs)
const;
679 template <
typename VectorType>
724 template <
typename MatrixType,
typename VectorType>
725 using vmult_functions_t =
decltype(std::declval<const MatrixType>().vmult(
726 std::declval<VectorType &>(),
727 std::declval<const VectorType &>(),
729 const std::function<
void(
const unsigned int,
const unsigned int)> &>(),
731 const std::function<
void(
const unsigned int,
const unsigned int)> &>()));
733 template <
typename MatrixType,
735 typename PreconditionerType>
736 constexpr bool has_vmult_with_std_functions =
738 std::is_same_v<PreconditionerType, ::DiagonalMatrix<VectorType>> &&
739 (std::is_same_v<VectorType,
747 template <
typename MatrixType,
typename VectorType>
748 constexpr bool has_vmult_with_std_functions_for_precondition =
753 template <
typename T,
typename VectorType>
754 using Tvmult_t =
decltype(std::declval<const T>().Tvmult(
755 std::declval<VectorType &>(),
756 std::declval<const VectorType &>()));
758 template <
typename T,
typename VectorType>
759 constexpr bool has_Tvmult = is_supported_operation<Tvmult_t, T, VectorType>;
761 template <
typename T,
typename VectorType>
762 using step_t =
decltype(std::declval<const T>().step(
763 std::declval<VectorType &>(),
764 std::declval<const VectorType &>()));
766 template <
typename T,
typename VectorType>
767 constexpr bool has_step = is_supported_operation<step_t, T, VectorType>;
769 template <
typename T,
typename VectorType>
771 decltype(std::declval<const T>().step(std::declval<VectorType &>(),
772 std::declval<const VectorType &>(),
773 std::declval<const double>()));
775 template <
typename T,
typename VectorType>
776 constexpr bool has_step_omega =
777 is_supported_operation<step_omega_t, T, VectorType>;
779 template <
typename T,
typename VectorType>
780 using Tstep_t =
decltype(std::declval<const T>().Tstep(
781 std::declval<VectorType &>(),
782 std::declval<const VectorType &>()));
784 template <
typename T,
typename VectorType>
785 constexpr bool has_Tstep = is_supported_operation<Tstep_t, T, VectorType>;
787 template <
typename T,
typename VectorType>
788 using Tstep_omega_t =
789 decltype(std::declval<const T>().Tstep(std::declval<VectorType &>(),
790 std::declval<const VectorType &>(),
791 std::declval<const double>()));
793 template <
typename T,
typename VectorType>
794 constexpr bool has_Tstep_omega =
795 is_supported_operation<Tstep_omega_t, T, VectorType>;
797 template <
typename T,
typename VectorType>
798 using jacobi_step_t =
decltype(std::declval<const T>().Jacobi_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_jacobi_step =
805 is_supported_operation<jacobi_step_t, T, VectorType>;
807 template <
typename T,
typename VectorType>
808 using SOR_step_t =
decltype(std::declval<const T>().SOR_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_SOR_step =
815 is_supported_operation<SOR_step_t, T, VectorType>;
817 template <
typename T,
typename VectorType>
818 using SSOR_step_t =
decltype(std::declval<const T>().SSOR_step(
819 std::declval<VectorType &>(),
820 std::declval<const VectorType &>(),
821 std::declval<const double>()));
823 template <
typename T,
typename VectorType>
824 constexpr bool has_SSOR_step =
825 is_supported_operation<SSOR_step_t, T, VectorType>;
827 template <
typename MatrixType>
828 class PreconditionJacobiImpl
831 PreconditionJacobiImpl(
const MatrixType &A,
const double relaxation)
833 , relaxation(relaxation)
836 template <
typename VectorType>
838 vmult(VectorType &dst,
const VectorType &src)
const
840 this->A->precondition_Jacobi(dst, src, this->relaxation);
843 template <
typename VectorType>
845 Tvmult(VectorType &dst,
const VectorType &src)
const
848 this->vmult(dst, src);
851 template <
typename VectorType,
852 std::enable_if_t<has_jacobi_step<MatrixType, VectorType>,
853 MatrixType> * =
nullptr>
855 step(VectorType &dst,
const VectorType &src)
const
857 this->A->Jacobi_step(dst, src, this->relaxation);
860 template <
typename VectorType,
861 std::enable_if_t<!has_jacobi_step<MatrixType, VectorType>,
862 MatrixType> * =
nullptr>
864 step(VectorType &,
const VectorType &)
const
868 "Matrix A does not provide a Jacobi_step() function!"));
871 template <
typename VectorType>
873 Tstep(VectorType &dst,
const VectorType &src)
const
876 this->step(dst, src);
881 const double relaxation;
884 template <
typename MatrixType>
885 class PreconditionSORImpl
888 PreconditionSORImpl(
const MatrixType &A,
const double relaxation)
890 , relaxation(relaxation)
893 template <
typename VectorType>
895 vmult(VectorType &dst,
const VectorType &src)
const
897 this->A->precondition_SOR(dst, src, this->relaxation);
900 template <
typename VectorType>
902 Tvmult(VectorType &dst,
const VectorType &src)
const
904 this->A->precondition_TSOR(dst, src, this->relaxation);
907 template <
typename VectorType,
908 std::enable_if_t<has_SOR_step<MatrixType, VectorType>,
909 MatrixType> * =
nullptr>
911 step(VectorType &dst,
const VectorType &src)
const
913 this->A->SOR_step(dst, src, this->relaxation);
916 template <
typename VectorType,
917 std::enable_if_t<!has_SOR_step<MatrixType, VectorType>,
918 MatrixType> * =
nullptr>
920 step(VectorType &,
const VectorType &)
const
924 "Matrix A does not provide a SOR_step() function!"));
927 template <
typename VectorType,
928 std::enable_if_t<has_SOR_step<MatrixType, VectorType>,
929 MatrixType> * =
nullptr>
931 Tstep(VectorType &dst,
const VectorType &src)
const
933 this->A->TSOR_step(dst, src, this->relaxation);
936 template <
typename VectorType,
937 std::enable_if_t<!has_SOR_step<MatrixType, VectorType>,
938 MatrixType> * =
nullptr>
940 Tstep(VectorType &,
const VectorType &)
const
944 "Matrix A does not provide a TSOR_step() function!"));
949 const double relaxation;
952 template <
typename MatrixType>
953 class PreconditionSSORImpl
956 using size_type =
typename MatrixType::size_type;
958 PreconditionSSORImpl(
const MatrixType &A,
const double relaxation)
960 , relaxation(relaxation)
971 const size_type n = this->A->n();
972 pos_right_of_diagonal.resize(n,
static_cast<std::size_t
>(-1));
973 for (size_type row = 0; row < n; ++row)
982 for (; it < mat->
end(row); ++it)
983 if (it->column() > row)
985 pos_right_of_diagonal[row] = it - mat->
begin();
990 template <
typename VectorType>
992 vmult(VectorType &dst,
const VectorType &src)
const
994 this->A->precondition_SSOR(dst,
997 pos_right_of_diagonal);
1000 template <
typename VectorType>
1002 Tvmult(VectorType &dst,
const VectorType &src)
const
1004 this->A->precondition_SSOR(dst,
1007 pos_right_of_diagonal);
1010 template <
typename VectorType,
1011 std::enable_if_t<has_SSOR_step<MatrixType, VectorType>,
1012 MatrixType> * =
nullptr>
1014 step(VectorType &dst,
const VectorType &src)
const
1016 this->A->SSOR_step(dst, src, this->relaxation);
1019 template <
typename VectorType,
1020 std::enable_if_t<!has_SSOR_step<MatrixType, VectorType>,
1021 MatrixType> * =
nullptr>
1023 step(VectorType &,
const VectorType &)
const
1027 "Matrix A does not provide a SSOR_step() function!"));
1030 template <
typename VectorType>
1032 Tstep(VectorType &dst,
const VectorType &src)
const
1035 this->step(dst, src);
1040 const double relaxation;
1046 std::vector<std::size_t> pos_right_of_diagonal;
1049 template <
typename MatrixType>
1050 class PreconditionPSORImpl
1053 using size_type =
typename MatrixType::size_type;
1055 PreconditionPSORImpl(
const MatrixType &A,
1056 const double relaxation,
1057 const std::vector<size_type> &permutation,
1058 const std::vector<size_type> &inverse_permutation)
1060 , relaxation(relaxation)
1061 , permutation(permutation)
1062 , inverse_permutation(inverse_permutation)
1065 template <
typename VectorType>
1067 vmult(VectorType &dst,
const VectorType &src)
const
1070 this->A->PSOR(dst, permutation, inverse_permutation, this->relaxation);
1073 template <
typename VectorType>
1075 Tvmult(VectorType &dst,
const VectorType &src)
const
1078 this->A->TPSOR(dst, permutation, inverse_permutation, this->relaxation);
1083 const double relaxation;
1085 const std::vector<size_type> &permutation;
1086 const std::vector<size_type> &inverse_permutation;
1089 template <
typename MatrixType,
1090 typename PreconditionerType,
1091 typename VectorType,
1092 std::enable_if_t<has_step_omega<PreconditionerType, VectorType>,
1093 PreconditionerType> * =
nullptr>
1095 step(
const MatrixType &,
1096 const PreconditionerType &preconditioner,
1098 const VectorType &src,
1099 const double relaxation,
1103 preconditioner.step(dst, src, relaxation);
1107 typename MatrixType,
1108 typename PreconditionerType,
1109 typename VectorType,
1110 std::enable_if_t<!has_step_omega<PreconditionerType, VectorType> &&
1111 has_step<PreconditionerType, VectorType>,
1112 PreconditionerType> * =
nullptr>
1114 step(
const MatrixType &,
1115 const PreconditionerType &preconditioner,
1117 const VectorType &src,
1118 const double relaxation,
1126 preconditioner.step(dst, src);
1130 typename MatrixType,
1131 typename PreconditionerType,
1132 typename VectorType,
1133 std::enable_if_t<!has_step_omega<PreconditionerType, VectorType> &&
1134 !has_step<PreconditionerType, VectorType>,
1135 PreconditionerType> * =
nullptr>
1137 step(
const MatrixType &A,
1138 const PreconditionerType &preconditioner,
1140 const VectorType &src,
1141 const double relaxation,
1146 tmp.reinit(dst,
true);
1151 preconditioner.vmult(tmp,
residual);
1152 dst.add(relaxation, tmp);
1155 template <
typename MatrixType,
1156 typename PreconditionerType,
1157 typename VectorType,
1158 std::enable_if_t<has_Tstep_omega<PreconditionerType, VectorType>,
1159 PreconditionerType> * =
nullptr>
1161 Tstep(
const MatrixType &,
1162 const PreconditionerType &preconditioner,
1164 const VectorType &src,
1165 const double relaxation,
1169 preconditioner.Tstep(dst, src, relaxation);
1173 typename MatrixType,
1174 typename PreconditionerType,
1175 typename VectorType,
1176 std::enable_if_t<!has_Tstep_omega<PreconditionerType, VectorType> &&
1177 has_Tstep<PreconditionerType, VectorType>,
1178 PreconditionerType> * =
nullptr>
1180 Tstep(
const MatrixType &,
1181 const PreconditionerType &preconditioner,
1183 const VectorType &src,
1184 const double relaxation,
1192 preconditioner.Tstep(dst, src);
1195 template <
typename MatrixType,
1196 typename VectorType,
1197 std::enable_if_t<has_Tvmult<MatrixType, VectorType>, MatrixType>
1200 Tvmult(
const MatrixType &A, VectorType &dst,
const VectorType &src)
1205 template <
typename MatrixType,
1206 typename VectorType,
1207 std::enable_if_t<!has_Tvmult<MatrixType, VectorType>, MatrixType>
1210 Tvmult(
const MatrixType &, VectorType &,
const VectorType &)
1213 ExcMessage(
"Matrix A does not provide a Tvmult() function!"));
1217 typename MatrixType,
1218 typename PreconditionerType,
1219 typename VectorType,
1220 std::enable_if_t<!has_Tstep_omega<PreconditionerType, VectorType> &&
1221 !has_Tstep<PreconditionerType, VectorType>,
1222 PreconditionerType> * =
nullptr>
1224 Tstep(
const MatrixType &A,
1225 const PreconditionerType &preconditioner,
1227 const VectorType &src,
1228 const double relaxation,
1233 tmp.reinit(dst,
true);
1238 Tvmult(preconditioner, tmp,
residual);
1239 dst.add(relaxation, tmp);
1243 template <
typename MatrixType,
1244 typename PreconditionerType,
1245 typename VectorType,
1246 std::enable_if_t<!has_vmult_with_std_functions_for_precondition<
1251 step_operations(
const MatrixType &A,
1252 const PreconditionerType &preconditioner,
1254 const VectorType &src,
1255 const double relaxation,
1258 const unsigned int i,
1259 const bool transposed)
1264 Tvmult(preconditioner, dst, src);
1266 preconditioner.vmult(dst, src);
1268 if (relaxation != 1.0)
1274 Tstep(A, preconditioner, dst, src, relaxation, tmp1, tmp2);
1276 step(A, preconditioner, dst, src, relaxation, tmp1, tmp2);
1283 typename MatrixType,
1284 typename PreconditionerType,
1285 typename VectorType,
1287 has_vmult_with_std_functions_for_precondition<PreconditionerType,
1289 !has_vmult_with_std_functions_for_precondition<MatrixType,
1293 step_operations(
const MatrixType &A,
1294 const PreconditionerType &preconditioner,
1296 const VectorType &src,
1297 const double relaxation,
1300 const unsigned int i,
1301 const bool transposed)
1304 using Number =
typename VectorType::value_type;
1305 Number *dst_ptr = dst.begin();
1306 const Number *src_ptr = src.begin();
1310 preconditioner.vmult(
1313 [&](
const unsigned int start_range,
const unsigned int end_range) {
1315 if (end_range > start_range)
1316 std::memset(dst_ptr + start_range,
1318 sizeof(Number) * (end_range - start_range));
1320 [&](
const unsigned int start_range,
const unsigned int end_range) {
1321 if (relaxation == 1.0)
1325 for (std::size_t i = start_range; i < end_range; ++i)
1326 dst_ptr[i] *= relaxation;
1331 tmp.reinit(src,
true);
1337 preconditioner.vmult(
1340 [&](
const unsigned int start_range,
const unsigned int end_range) {
1341 const auto tmp_ptr = tmp.begin();
1343 if (relaxation == 1.0)
1346 for (std::size_t i = start_range; i < end_range; ++i)
1347 tmp_ptr[i] = src_ptr[i] - tmp_ptr[i];
1355 for (std::size_t i = start_range; i < end_range; ++i)
1356 tmp_ptr[i] = relaxation * (src_ptr[i] - tmp_ptr[i]);
1359 [&](
const unsigned int,
const unsigned int) {
1369 typename MatrixType,
1370 typename PreconditionerType,
1371 typename VectorType,
1373 has_vmult_with_std_functions_for_precondition<PreconditionerType,
1375 has_vmult_with_std_functions_for_precondition<MatrixType, VectorType>,
1378 step_operations(
const MatrixType &A,
1379 const PreconditionerType &preconditioner,
1381 const VectorType &src,
1382 const double relaxation,
1385 const unsigned int i,
1386 const bool transposed)
1389 using Number =
typename VectorType::value_type;
1391 Number *dst_ptr = dst.begin();
1392 const Number *src_ptr = src.begin();
1396 preconditioner.vmult(
1399 [&](
const unsigned int start_range,
const unsigned int end_range) {
1401 if (end_range > start_range)
1402 std::memset(dst_ptr + start_range,
1404 sizeof(Number) * (end_range - start_range));
1406 [&](
const unsigned int start_range,
const unsigned int end_range) {
1407 if (relaxation == 1.0)
1411 for (std::size_t i = start_range; i < end_range; ++i)
1412 dst_ptr[i] *= relaxation;
1417 tmp.reinit(src,
true);
1418 const auto tmp_ptr = tmp.begin();
1425 [&](
const unsigned int start_range,
const unsigned int end_range) {
1428 if (end_range > start_range)
1429 std::memset(tmp_ptr + start_range,
1431 sizeof(Number) * (end_range - start_range));
1433 [&](
const unsigned int start_range,
const unsigned int end_range) {
1434 if (relaxation == 1.0)
1437 for (std::size_t i = start_range; i < end_range; ++i)
1438 tmp_ptr[i] = src_ptr[i] - tmp_ptr[i];
1446 for (std::size_t i = start_range; i < end_range; ++i)
1447 tmp_ptr[i] = relaxation * (src_ptr[i] - tmp_ptr[i]);
1451 preconditioner.vmult(dst, tmp, [](
const auto,
const auto) {
1460 typename MatrixType,
1461 typename VectorType,
1468 !has_vmult_with_std_functions<MatrixType,
1471 VectorType> * =
nullptr>
1473 step_operations(
const MatrixType &A,
1474 const ::DiagonalMatrix<VectorType> &preconditioner,
1476 const VectorType &src,
1477 const double relaxation,
1480 const unsigned int i,
1481 const bool transposed)
1483 using Number =
typename VectorType::value_type;
1487 Number *dst_ptr = dst.begin();
1488 const Number *src_ptr = src.begin();
1489 const Number *diag_ptr = preconditioner.get_vector().begin();
1491 if (relaxation == 1.0)
1494 for (
unsigned int i = 0; i < dst.locally_owned_size(); ++i)
1495 dst_ptr[i] = src_ptr[i] * diag_ptr[i];
1500 for (
unsigned int i = 0; i < dst.locally_owned_size(); ++i)
1501 dst_ptr[i] = relaxation * src_ptr[i] * diag_ptr[i];
1506 tmp.reinit(src,
true);
1509 Tvmult(A, tmp, dst);
1513 Number *dst_ptr = dst.begin();
1514 const Number *src_ptr = src.begin();
1515 const Number *tmp_ptr = tmp.begin();
1516 const Number *diag_ptr = preconditioner.get_vector().begin();
1518 if (relaxation == 1.0)
1521 for (
unsigned int i = 0; i < dst.locally_owned_size(); ++i)
1522 dst_ptr[i] += (src_ptr[i] - tmp_ptr[i]) * diag_ptr[i];
1527 for (
unsigned int i = 0; i < dst.locally_owned_size(); ++i)
1529 relaxation * (src_ptr[i] - tmp_ptr[i]) * diag_ptr[i];
1536 template <
typename MatrixType,
1537 typename VectorType,
1538 std::enable_if_t<!IsBlockVector<VectorType>::value &&
1539 has_vmult_with_std_functions<
1543 VectorType> * =
nullptr>
1545 step_operations(
const MatrixType &A,
1546 const ::DiagonalMatrix<VectorType> &preconditioner,
1548 const VectorType &src,
1549 const double relaxation,
1552 const unsigned int i,
1553 const bool transposed)
1556 using Number =
typename VectorType::value_type;
1560 Number *dst_ptr = dst.begin();
1561 const Number *src_ptr = src.begin();
1562 const Number *diag_ptr = preconditioner.get_vector().begin();
1564 if (relaxation == 1.0)
1567 for (
unsigned int i = 0; i < dst.locally_owned_size(); ++i)
1568 dst_ptr[i] = src_ptr[i] * diag_ptr[i];
1573 for (
unsigned int i = 0; i < dst.locally_owned_size(); ++i)
1574 dst_ptr[i] = relaxation * src_ptr[i] * diag_ptr[i];
1579 tmp.reinit(src,
true);
1586 [&](
const unsigned int start_range,
const unsigned int end_range) {
1588 if (end_range > start_range)
1589 std::memset(tmp.begin() + start_range,
1591 sizeof(Number) * (end_range - start_range));
1593 [&](
const unsigned int begin,
const unsigned int end) {
1594 const Number *dst_ptr = dst.begin();
1595 const Number *src_ptr = src.begin();
1596 Number *tmp_ptr = tmp.begin();
1597 const Number *diag_ptr = preconditioner.get_vector().begin();
1601 if (relaxation == 1.0)
1604 for (std::size_t i =
begin; i <
end; ++i)
1606 dst_ptr[i] + (src_ptr[i] - tmp_ptr[i]) * diag_ptr[i];
1611 for (std::size_t i =
begin; i <
end; ++i)
1612 tmp_ptr[i] = dst_ptr[i] + relaxation *
1613 (src_ptr[i] - tmp_ptr[i]) *
1655template <
typename MatrixType = SparseMatrix<
double>>
1659 internal::PreconditionRelaxation::PreconditionJacobiImpl<MatrixType>>
1662 internal::PreconditionRelaxation::PreconditionJacobiImpl<MatrixType>;
1725template <
typename MatrixType = SparseMatrix<
double>>
1729 internal::PreconditionRelaxation::PreconditionSORImpl<MatrixType>>
1732 internal::PreconditionRelaxation::PreconditionSORImpl<MatrixType>;
1777template <
typename MatrixType = SparseMatrix<
double>>
1781 internal::PreconditionRelaxation::PreconditionSSORImpl<MatrixType>>
1784 internal::PreconditionRelaxation::PreconditionSSORImpl<MatrixType>;
1833template <
typename MatrixType = SparseMatrix<
double>>
1837 internal::PreconditionRelaxation::PreconditionPSORImpl<MatrixType>>
1840 internal::PreconditionRelaxation::PreconditionPSORImpl<MatrixType>;
1867 const typename BaseClass::AdditionalData &
parameters =
1868 typename BaseClass::AdditionalData());
1897 const std::vector<size_type> &permutation,
1898 const std::vector<size_type> &inverse_permutation,
1899 const typename BaseClass::AdditionalData ¶meters =
1900 typename BaseClass::AdditionalData());
2106template <
typename MatrixType = SparseMatrix<
double>,
2107 typename VectorType = Vector<
double>,
2108 typename PreconditionerType = DiagonalMatrix<VectorType>>
2146 const unsigned int degree = 1,
2152 EigenvalueAlgorithm::lanczos,
2202 vmult(VectorType &dst,
const VectorType &src)
const;
2209 Tvmult(VectorType &dst,
const VectorType &src)
const;
2215 step(VectorType &dst,
const VectorType &src)
const;
2221 Tstep(VectorType &dst,
const VectorType &src)
const;
2265 template <
bool use_transpose>
2268 const VectorType &src,
2269 VectorType &dst)
const;
2334 template <
typename VectorType>
2336 set_initial_guess(VectorType &vector)
2338 vector = 1. /
std::sqrt(
static_cast<double>(vector.size()));
2339 if (vector.locally_owned_elements().is_element(0))
2345 template <
typename Number>
2347 set_initial_guess(::Vector<Number> &vector)
2353 for (
unsigned int i = 0; i < vector.
size(); ++i)
2358 vector.
add(-mean_value);
2363 template <
typename Number,
typename MemorySpace>
2366 ::LinearAlgebra::distributed::BlockVector<Number, MemorySpace>
2369 for (
unsigned int block = 0; block < vector.
n_blocks(); ++block)
2370 set_initial_guess(vector.
block(block));
2375 template <
typename Number,
typename MemorySpace>
2378 ::LinearAlgebra::distributed::Vector<Number, MemorySpace> &vector)
2392 typename MemorySpace::kokkos_space::execution_space exec;
2393 Kokkos::RangePolicy<
typename MemorySpace::kokkos_space::execution_space,
2394 Kokkos::IndexType<types::global_dof_index>>
2395 policy(exec, 0, n_local_elements);
2397 Kokkos::parallel_for(
2398 "::PreconditionChebyshev::set_initial_guess",
2401 values_ptr[i] = (i + first_local_range) % 11;
2407 vector.
add(-mean_value);
2412 struct EigenvalueTracker
2421 std::vector<double>
values;
2428 typename PreconditionerType>
2431 VectorType &eigenvector,
2432 const PreconditionerType &preconditioner,
2433 const unsigned int n_iterations)
2435 typename VectorType::value_type eigenvalue_estimate = 0.;
2436 eigenvector /= eigenvector.l2_norm();
2438 vector1.reinit(eigenvector,
true);
2439 if (!std::is_same_v<PreconditionerType, PreconditionIdentity>)
2440 vector2.reinit(eigenvector,
true);
2441 for (
unsigned int i = 0; i < n_iterations; ++i)
2443 if (!std::is_same_v<PreconditionerType, PreconditionIdentity>)
2445 matrix.vmult(vector2, eigenvector);
2446 preconditioner.vmult(vector1, vector2);
2449 matrix.vmult(vector1, eigenvector);
2451 eigenvalue_estimate = eigenvector * vector1;
2453 vector1 /= vector1.l2_norm();
2454 eigenvector.swap(vector1);
2456 return std::abs(eigenvalue_estimate);
2463 typename PreconditionerType>
2465 estimate_eigenvalues(
2467 const MatrixType *matrix_ptr,
2468 VectorType &solution_old,
2469 VectorType &temp_vector1,
2470 const unsigned int degree,
2471 const double safety_factor)
2476 safety_factor >= 1.,
2478 "The safety factor must be at least 1.0 to ensure the maximum eigenvalue is "
2479 "not underestimated. Please set safety_factor >= 1.0 in your AdditionalData."));
2483 if (data.eig_cg_n_iterations > 0)
2485 Assert(data.eig_cg_n_iterations > 2,
2487 "Need to set at least two iterations to find eigenvalues."));
2489 internal::EigenvalueTracker eigenvalue_tracker;
2494 internal::set_initial_guess(temp_vector1);
2495 data.constraints.set_zero(temp_vector1);
2506 if (temp_vector1.all_zero() ==
false)
2508 if (data.eigenvalue_algorithm ==
2512 IterationNumberControl control(data.eig_cg_n_iterations,
2517 ::SolverCG<VectorType> solver(control);
2518 solver.connect_eigenvalues_slot(
2519 [&eigenvalue_tracker](
2524 solver.solve(*matrix_ptr,
2527 *data.preconditioner);
2529 info.cg_iterations = control.last_step();
2531 else if (data.eigenvalue_algorithm ==
2538 "Cannot estimate the minimal eigenvalue with the "
2539 "power iteration"));
2541 eigenvalue_tracker.values.push_back(
2544 *data.preconditioner,
2545 data.eig_cg_n_iterations));
2552 if (eigenvalue_tracker.values.empty())
2553 info.min_eigenvalue_estimate = info.max_eigenvalue_estimate = 1.;
2556 info.min_eigenvalue_estimate = eigenvalue_tracker.values.front();
2560 info.max_eigenvalue_estimate =
2561 safety_factor * eigenvalue_tracker.values.back();
2566 info.max_eigenvalue_estimate = data.max_eigenvalue;
2567 info.min_eigenvalue_estimate =
2568 data.max_eigenvalue / data.smoothing_range;
2582template <
typename MatrixType>
2592template <
typename VectorType>
2601template <
typename VectorType>
2608template <
typename VectorType>
2617template <
typename VectorType>
2649 const double relaxation)
2650 : relaxation(relaxation)
2660 relaxation = add_data.relaxation;
2674template <
typename MatrixType>
2677 const MatrixType &matrix,
2687template <
typename VectorType>
2692 std::is_same_v<size_type, typename VectorType::size_type>,
2693 "PreconditionRichardson and VectorType must have the same size_type.");
2700template <
typename VectorType>
2705 std::is_same_v<size_type, typename VectorType::size_type>,
2706 "PreconditionRichardson and VectorType must have the same size_type.");
2711template <
typename VectorType>
2716 std::is_same_v<size_type, typename VectorType::size_type>,
2717 "PreconditionRichardson and VectorType must have the same size_type.");
2724template <
typename VectorType>
2729 std::is_same_v<size_type, typename VectorType::size_type>,
2730 "PreconditionRichardson and VectorType must have the same size_type.");
2751template <
typename MatrixType,
typename PreconditionerType>
2754 const MatrixType &rA,
2755 const AdditionalData ¶meters)
2758 eigenvalues_are_initialized =
false;
2762 this->data = parameters;
2766template <
typename MatrixType,
typename PreconditionerType>
2770 eigenvalues_are_initialized =
false;
2772 data.relaxation = 1.0;
2773 data.preconditioner =
nullptr;
2776template <
typename MatrixType,
typename PreconditionerType>
2785template <
typename MatrixType,
typename PreconditionerType>
2794template <
typename MatrixType,
typename PreconditionerType>
2795template <
typename VectorType>
2799 const VectorType &src)
const
2804 if (eigenvalues_are_initialized ==
false)
2805 estimate_eigenvalues(src);
2807 VectorType tmp1, tmp2;
2809 for (
unsigned int i = 0; i < data.n_iterations; ++i)
2810 internal::PreconditionRelaxation::step_operations(*A,
2811 *data.preconditioner,
2821template <
typename MatrixType,
typename PreconditionerType>
2822template <
typename VectorType>
2826 const VectorType &src)
const
2831 if (eigenvalues_are_initialized ==
false)
2832 estimate_eigenvalues(src);
2834 VectorType tmp1, tmp2;
2836 for (
unsigned int i = 0; i < data.n_iterations; ++i)
2837 internal::PreconditionRelaxation::step_operations(
2838 *A, *data.preconditioner, dst, src, data.relaxation, tmp1, tmp2, i,
true);
2841template <
typename MatrixType,
typename PreconditionerType>
2842template <
typename VectorType>
2846 const VectorType &src)
const
2851 if (eigenvalues_are_initialized ==
false)
2852 estimate_eigenvalues(src);
2854 VectorType tmp1, tmp2;
2856 for (
unsigned int i = 1; i <= data.n_iterations; ++i)
2857 internal::PreconditionRelaxation::step_operations(*A,
2858 *data.preconditioner,
2868template <
typename MatrixType,
typename PreconditionerType>
2869template <
typename VectorType>
2873 const VectorType &src)
const
2878 if (eigenvalues_are_initialized ==
false)
2879 estimate_eigenvalues(src);
2881 VectorType tmp1, tmp2;
2883 for (
unsigned int i = 1; i <= data.n_iterations; ++i)
2884 internal::PreconditionRelaxation::step_operations(
2885 *A, *data.preconditioner, dst, src, data.relaxation, tmp1, tmp2, i,
true);
2888template <
typename MatrixType,
typename PreconditionerType>
2889template <
typename VectorType>
2892 const VectorType &src)
const
2896 EigenvalueInformation info;
2898 if (data.relaxation == 0.0)
2900 VectorType solution_old, temp_vector1;
2902 solution_old.reinit(src);
2903 temp_vector1.reinit(src,
true);
2905 info = internal::estimate_eigenvalues<MatrixType>(data,
2910 data.safety_factor);
2912 const double alpha =
2913 (data.smoothing_range > 1. ?
2914 info.max_eigenvalue_estimate / data.smoothing_range :
2915 std::min(0.9 * info.max_eigenvalue_estimate,
2916 info.min_eigenvalue_estimate));
2919 ->data.relaxation = 2.0 / (alpha + info.max_eigenvalue_estimate);
2923 ->eigenvalues_are_initialized =
true;
2928template <
typename MatrixType,
typename PreconditionerType>
2932 return data.relaxation;
2938template <
typename MatrixType>
2941 const AdditionalData ¶meters_in)
2945 parameters_in.relaxation != 0.0,
2947 "Relaxation cannot automatically be determined by PreconditionJacobi."));
2949 AdditionalData parameters;
2950 parameters.relaxation = 1.0;
2951 parameters.n_iterations = parameters_in.n_iterations;
2952 parameters.preconditioner =
2953 std::make_shared<PreconditionerType>(A, parameters_in.relaxation);
2955 this->BaseClass::initialize(A, parameters);
2960template <
typename MatrixType>
2963 const AdditionalData ¶meters_in)
2967 parameters_in.relaxation != 0.0,
2969 "Relaxation cannot automatically be determined by PreconditionSOR."));
2971 AdditionalData parameters;
2972 parameters.relaxation = 1.0;
2973 parameters.n_iterations = parameters_in.n_iterations;
2974 parameters.preconditioner =
2975 std::make_shared<PreconditionerType>(A, parameters_in.relaxation);
2977 this->BaseClass::initialize(A, parameters);
2982template <
typename MatrixType>
2985 const AdditionalData ¶meters_in)
2989 parameters_in.relaxation != 0.0,
2991 "Relaxation cannot automatically be determined by PreconditionSSOR."));
2993 AdditionalData parameters;
2994 parameters.relaxation = 1.0;
2995 parameters.n_iterations = parameters_in.n_iterations;
2996 parameters.preconditioner =
2997 std::make_shared<PreconditionerType>(A, parameters_in.relaxation);
2999 this->BaseClass::initialize(A, parameters);
3006template <
typename MatrixType>
3009 const MatrixType &A,
3010 const std::vector<size_type> &p,
3011 const std::vector<size_type> &ip,
3012 const typename BaseClass::AdditionalData ¶meters_in)
3016 parameters_in.relaxation != 0.0,
3018 "Relaxation cannot automatically be determined by PreconditionPSOR."));
3020 typename BaseClass::AdditionalData parameters;
3021 parameters.relaxation = 1.0;
3022 parameters.n_iterations = parameters_in.n_iterations;
3023 parameters.preconditioner =
3024 std::make_shared<PreconditionerType>(A, parameters_in.relaxation, p, ip);
3026 this->BaseClass::initialize(A, parameters);
3030template <
typename MatrixType>
3033 const AdditionalData &additional_data)
3036 additional_data.permutation,
3037 additional_data.inverse_permutation,
3038 additional_data.parameters);
3041template <
typename MatrixType>
3043 const std::vector<size_type> &permutation,
3044 const std::vector<size_type> &inverse_permutation,
3045 const typename PreconditionPSOR<MatrixType>::BaseClass::AdditionalData
3047 : permutation(permutation)
3048 , inverse_permutation(inverse_permutation)
3049 , parameters(parameters)
3056template <
typename MatrixType,
typename VectorType>
3058 const MatrixType &M,
3059 const function_ptr method)
3061 , precondition(method)
3066template <
typename MatrixType,
typename VectorType>
3070 const VectorType &src)
const
3072 (
matrix.*precondition)(dst, src);
3080 template <
typename PreconditionerType>
3083 const double smoothing_range,
3084 const unsigned int eig_cg_n_iterations,
3085 const double eig_cg_residual,
3086 const double max_eigenvalue,
3088 const double safety_factor)
3089 : smoothing_range(smoothing_range)
3090 , eig_cg_n_iterations(eig_cg_n_iterations)
3091 , eig_cg_residual(eig_cg_residual)
3092 , max_eigenvalue(max_eigenvalue)
3093 , eigenvalue_algorithm(eigenvalue_algorithm)
3094 , safety_factor(safety_factor)
3099 template <
typename PreconditionerType>
3100 inline EigenvalueAlgorithmAdditionalData<PreconditionerType> &
3101 EigenvalueAlgorithmAdditionalData<PreconditionerType>::operator=(
3102 const EigenvalueAlgorithmAdditionalData &other_data)
3104 smoothing_range = other_data.smoothing_range;
3105 eig_cg_n_iterations = other_data.eig_cg_n_iterations;
3106 eig_cg_residual = other_data.eig_cg_residual;
3107 max_eigenvalue = other_data.max_eigenvalue;
3108 preconditioner = other_data.preconditioner;
3109 eigenvalue_algorithm = other_data.eigenvalue_algorithm;
3110 safety_factor = other_data.safety_factor;
3111 constraints.copy_from(other_data.constraints);
3117template <
typename MatrixType,
typename PreconditionerType>
3120 const unsigned int n_iterations,
3121 const double smoothing_range,
3122 const unsigned int eig_cg_n_iterations,
3123 const double eig_cg_residual,
3124 const double max_eigenvalue,
3125 const EigenvalueAlgorithm eigenvalue_algorithm,
3126 const double safety_factor)
3127 :
internal::EigenvalueAlgorithmAdditionalData<PreconditionerType>(
3129 eig_cg_n_iterations,
3132 eigenvalue_algorithm,
3134 , relaxation(relaxation)
3135 , n_iterations(n_iterations)
3144 namespace PreconditionChebyshevImplementation
3152 template <
typename VectorType,
typename PreconditionerType>
3154 vector_updates(
const VectorType &rhs,
3155 const PreconditionerType &preconditioner,
3156 const unsigned int iteration_index,
3157 const double factor1,
3158 const double factor2,
3159 VectorType &solution_old,
3160 VectorType &temp_vector1,
3161 VectorType &temp_vector2,
3162 VectorType &solution)
3164 if (iteration_index == 0)
3166 solution.equ(factor2, rhs);
3167 preconditioner.vmult(solution_old, solution);
3169 else if (iteration_index == 1)
3172 temp_vector1.sadd(-1.0, 1.0, rhs);
3173 preconditioner.vmult(solution_old, temp_vector1);
3176 solution_old.sadd(factor2, 1 + factor1, solution);
3181 temp_vector1.sadd(-1.0, 1.0, rhs);
3182 preconditioner.vmult(temp_vector2, temp_vector1);
3185 solution_old.sadd(-factor1, factor2, temp_vector2);
3186 solution_old.add(1 + factor1, solution);
3189 solution.swap(solution_old);
3195 typename PreconditionerType,
3197 !has_vmult_with_std_functions_for_precondition<
3199 LinearAlgebra::distributed::Vector<Number, MemorySpace::Host>>,
3203 const LinearAlgebra::distributed::Vector<Number, MemorySpace::Host> &rhs,
3204 const PreconditionerType &preconditioner,
3205 const unsigned int iteration_index,
3206 const double factor1_,
3207 const double factor2_,
3208 LinearAlgebra::distributed::Vector<Number, MemorySpace::Host>
3210 LinearAlgebra::distributed::Vector<Number, MemorySpace::Host>
3212 LinearAlgebra::distributed::Vector<Number, MemorySpace::Host>
3214 LinearAlgebra::distributed::Vector<Number, MemorySpace::Host> &solution)
3216 const Number factor1 = factor1_;
3217 const Number factor1_plus_1 = 1. + factor1_;
3218 const Number factor2 = factor2_;
3220 if (iteration_index == 0)
3223 preconditioner.vmult(solution_old, rhs);
3226 const auto solution_old_ptr = solution_old.
begin();
3229 solution_old_ptr[i] = solution_old_ptr[i] * factor2;
3231 else if (iteration_index == 1)
3234 temp_vector1.
sadd(-1.0, 1.0, rhs);
3236 preconditioner.vmult(solution_old, temp_vector1);
3239 const auto solution_ptr = solution.
begin();
3240 const auto solution_old_ptr = solution_old.
begin();
3243 solution_old_ptr[i] =
3244 factor1_plus_1 * solution_ptr[i] + solution_old_ptr[i] * factor2;
3249 temp_vector1.
sadd(-1.0, 1.0, rhs);
3251 preconditioner.vmult(temp_vector2, temp_vector1);
3254 const auto solution_ptr = solution.
begin();
3255 const auto solution_old_ptr = solution_old.
begin();
3256 const auto temp_vector2_ptr = temp_vector2.
begin();
3259 solution_old_ptr[i] = factor1_plus_1 * solution_ptr[i] -
3260 factor1 * solution_old_ptr[i] +
3261 temp_vector2_ptr[i] * factor2;
3264 solution.
swap(solution_old);
3269 typename PreconditionerType,
3271 has_vmult_with_std_functions_for_precondition<
3273 LinearAlgebra::distributed::Vector<Number, MemorySpace::Host>>,
3277 const LinearAlgebra::distributed::Vector<Number, MemorySpace::Host> &rhs,
3278 const PreconditionerType &preconditioner,
3279 const unsigned int iteration_index,
3280 const double factor1_,
3281 const double factor2_,
3282 LinearAlgebra::distributed::Vector<Number, MemorySpace::Host>
3284 LinearAlgebra::distributed::Vector<Number, MemorySpace::Host>
3286 LinearAlgebra::distributed::Vector<Number, MemorySpace::Host>
3288 LinearAlgebra::distributed::Vector<Number, MemorySpace::Host> &solution)
3290 const Number factor1 = factor1_;
3291 const Number factor1_plus_1 = 1. + factor1_;
3292 const Number factor2 = factor2_;
3294 const auto rhs_ptr = rhs.
begin();
3295 const auto temp_vector1_ptr = temp_vector1.
begin();
3296 const auto temp_vector2_ptr = temp_vector2.
begin();
3297 const auto solution_ptr = solution.
begin();
3298 const auto solution_old_ptr = solution_old.
begin();
3300 if (iteration_index == 0)
3302 preconditioner.vmult(
3305 [&](
const auto start_range,
const auto end_range) {
3306 if (end_range > start_range)
3307 std::memset(temp_vector2_ptr + start_range,
3309 sizeof(Number) * (end_range - start_range));
3311 [&](
const auto begin,
const auto end) {
3313 for (std::size_t i =
begin; i <
end; ++i)
3314 temp_vector2_ptr[i] *= factor2;
3319 preconditioner.vmult(
3322 [&](
const auto begin,
const auto end) {
3324 std::memset(temp_vector2_ptr +
begin,
3329 for (std::size_t i =
begin; i <
end; ++i)
3330 temp_vector1_ptr[i] = rhs_ptr[i] - temp_vector1_ptr[i];
3332 [&](
const auto begin,
const auto end) {
3333 if (iteration_index == 1)
3336 for (std::size_t i =
begin; i <
end; ++i)
3337 temp_vector2_ptr[i] = factor1_plus_1 * solution_ptr[i] +
3338 factor2 * temp_vector2_ptr[i];
3343 for (std::size_t i =
begin; i <
end; ++i)
3344 temp_vector2_ptr[i] = factor1_plus_1 * solution_ptr[i] -
3345 factor1 * solution_old_ptr[i] +
3346 factor2 * temp_vector2_ptr[i];
3351 solution_old.
swap(temp_vector2);
3352 solution_old.
swap(solution);
3356 template <
typename Number>
3359 const LinearAlgebra::distributed::Vector<Number, MemorySpace::Default>
3361 const ::DiagonalMatrix<
3362 LinearAlgebra::distributed::Vector<Number, MemorySpace::Default>>
3364 const unsigned int iteration_index,
3365 const double factor1_,
3366 const double factor2_,
3367 LinearAlgebra::distributed::Vector<Number, MemorySpace::Default>
3369 LinearAlgebra::distributed::Vector<Number, MemorySpace::Default>
3371 LinearAlgebra::distributed::Vector<Number, MemorySpace::Default> &,
3372 LinearAlgebra::distributed::Vector<Number, MemorySpace::Default>
3375 const Number factor1 = factor1_;
3376 const Number factor1_plus_1 = 1. + factor1_;
3377 const Number factor2 = factor2_;
3379 auto exec = typename ::MemorySpace::Default::kokkos_space::
3383 const Number *prec_data = preconditioner.get_vector().begin();
3386 using size_type =
typename LinearAlgebra::distributed::
3387 Vector<Number, MemorySpace::Default>::size_type;
3389 if (iteration_index == 0)
3391 Kokkos::parallel_for(
3393 Kokkos::RangePolicy<
3394 ::MemorySpace::Default::kokkos_space::execution_space>(
3396 KOKKOS_LAMBDA(size_type i) {
3397 sol_old_data[i] = prec_data[i] * factor2 * rhs_data[i];
3400 else if (iteration_index == 1)
3402 Kokkos::parallel_for(
3404 Kokkos::RangePolicy<
3405 ::MemorySpace::Default::kokkos_space::execution_space>(
3407 KOKKOS_LAMBDA(size_type i) {
3409 sol_old_data[i] = factor2 * prec_data[i] *
residual +
3410 factor1_plus_1 * sol_data[i];
3415 Kokkos::parallel_for(
3417 Kokkos::RangePolicy<
3418 ::MemorySpace::Default::kokkos_space::execution_space>(
3420 KOKKOS_LAMBDA(size_type i) {
3422 sol_old_data[i] = factor2 * prec_data[i] *
residual -
3423 factor1 * sol_old_data[i] +
3424 factor1_plus_1 * sol_data[i];
3429 solution.
swap(solution_old);
3435 template <
typename Number>
3436 struct VectorUpdater
3438 VectorUpdater(
const Number *rhs,
3439 const Number *matrix_diagonal_inverse,
3440 const unsigned int iteration_index,
3441 const Number factor1,
3442 const Number factor2,
3443 Number *solution_old,
3447 , matrix_diagonal_inverse(matrix_diagonal_inverse)
3448 , iteration_index(iteration_index)
3451 , solution_old(solution_old)
3452 , tmp_vector(tmp_vector)
3453 , solution(solution)
3457 apply_to_subrange(
const std::size_t
begin,
const std::size_t
end)
const
3463 const Number factor1 = this->factor1;
3464 const Number factor1_plus_1 = 1. + this->factor1;
3465 const Number factor2 = this->factor2;
3466 if (iteration_index == 0)
3469 for (std::size_t i =
begin; i <
end; ++i)
3470 tmp_vector[i] = factor2 * matrix_diagonal_inverse[i] * rhs[i];
3472 else if (iteration_index == 1)
3476 for (std::size_t i =
begin; i <
end; ++i)
3480 factor1_plus_1 * solution[i] +
3481 factor2 * matrix_diagonal_inverse[i] * (rhs[i] - tmp_vector[i]);
3488 for (std::size_t i =
begin; i <
end; ++i)
3495 factor1_plus_1 * solution[i] - factor1 * solution_old[i] +
3496 factor2 * matrix_diagonal_inverse[i] * (rhs[i] - tmp_vector[i]);
3501 const Number *matrix_diagonal_inverse;
3502 const unsigned int iteration_index;
3505 mutable Number *solution_old;
3506 mutable Number *tmp_vector;
3507 mutable Number *solution;
3510 template <
typename Number>
3511 struct VectorUpdatesRange :
public ::parallel::ParallelForInteger
3513 VectorUpdatesRange(
const VectorUpdater<Number> &updater,
3514 const std::size_t size)
3518 VectorUpdatesRange::apply_to_subrange(0, size);
3526 ~VectorUpdatesRange()
override =
default;
3529 apply_to_subrange(
const std::size_t
begin,
3530 const std::size_t
end)
const override
3532 updater.apply_to_subrange(
begin,
end);
3535 const VectorUpdater<Number> &updater;
3539 template <
typename Number>
3542 const ::Vector<Number> &rhs,
3543 const ::DiagonalMatrix<::Vector<Number>> &
jacobi,
3544 const unsigned int iteration_index,
3545 const double factor1,
3546 const double factor2,
3547 ::Vector<Number> &solution_old,
3548 ::Vector<Number> &temp_vector1,
3550 ::Vector<Number> &solution)
3552 VectorUpdater<Number> upd(rhs.begin(),
3553 jacobi.get_vector().begin(),
3557 solution_old.
begin(),
3558 temp_vector1.
begin(),
3560 VectorUpdatesRange<Number>(upd, rhs.size());
3563 solution.
swap(temp_vector1);
3564 solution_old.
swap(temp_vector1);
3568 template <
typename Number>
3571 const LinearAlgebra::distributed::Vector<Number, MemorySpace::Host> &rhs,
3572 const ::DiagonalMatrix<
3573 LinearAlgebra::distributed::Vector<Number, MemorySpace::Host>> &
jacobi,
3574 const unsigned int iteration_index,
3575 const double factor1,
3576 const double factor2,
3577 LinearAlgebra::distributed::Vector<Number, MemorySpace::Host>
3579 LinearAlgebra::distributed::Vector<Number, MemorySpace::Host>
3581 LinearAlgebra::distributed::Vector<Number, MemorySpace::Host> &,
3582 LinearAlgebra::distributed::Vector<Number, MemorySpace::Host> &solution)
3584 VectorUpdater<Number> upd(rhs.
begin(),
3585 jacobi.get_vector().begin(),
3589 solution_old.
begin(),
3590 temp_vector1.
begin(),
3595 solution.
swap(temp_vector1);
3596 solution_old.
swap(temp_vector1);
3606 typename PreconditionerType,
3610 PreconditionerType> &&
3611 !(has_vmult_with_std_functions_for_precondition<PreconditionerType,
3613 has_vmult_with_std_functions_for_precondition<
MatrixType,
3617 vmult_and_update(
const MatrixType &matrix,
3618 const PreconditionerType &preconditioner,
3619 const VectorType &rhs,
3620 const unsigned int iteration_index,
3621 const double factor1,
3622 const double factor2,
3623 VectorType &solution,
3624 VectorType &solution_old,
3625 VectorType &temp_vector1,
3626 VectorType &temp_vector2)
3628 if (iteration_index > 0)
3629 matrix.vmult(temp_vector1, solution);
3646 typename PreconditionerType,
3650 PreconditionerType> &&
3651 (has_vmult_with_std_functions_for_precondition<PreconditionerType,
3653 has_vmult_with_std_functions_for_precondition<
MatrixType,
3657 vmult_and_update(
const MatrixType &matrix,
3658 const PreconditionerType &preconditioner,
3659 const VectorType &rhs,
3660 const unsigned int iteration_index,
3661 const double factor1_,
3662 const double factor2_,
3663 VectorType &solution,
3664 VectorType &solution_old,
3665 VectorType &temp_vector1,
3666 VectorType &temp_vector2)
3668 using Number =
typename VectorType::value_type;
3670 const Number factor1 = factor1_;
3671 const Number factor1_plus_1 = 1. + factor1_;
3672 const Number factor2 = factor2_;
3674 if (iteration_index == 0)
3676 preconditioner.vmult(
3679 [&](
const unsigned int start_range,
const unsigned int end_range) {
3681 if (end_range > start_range)
3682 std::memset(temp_vector2.begin() + start_range,
3684 sizeof(Number) * (end_range - start_range));
3686 [&](
const unsigned int start_range,
const unsigned int end_range) {
3687 const auto tmp_ptr = temp_vector2.begin();
3690 for (std::size_t i = start_range; i < end_range; ++i)
3691 tmp_ptr[i] *= factor2;
3696 temp_vector1.reinit(rhs,
true);
3697 temp_vector2.reinit(rhs,
true);
3703 [&](
const unsigned int start_range,
const unsigned int end_range) {
3706 if (end_range > start_range)
3707 std::memset(temp_vector1.begin() + start_range,
3709 sizeof(Number) * (end_range - start_range));
3711 [&](
const unsigned int start_range,
const unsigned int end_range) {
3712 const auto rhs_ptr = rhs.begin();
3713 const auto tmp_ptr = temp_vector1.begin();
3716 for (std::size_t i = start_range; i < end_range; ++i)
3717 tmp_ptr[i] = rhs_ptr[i] - tmp_ptr[i];
3721 preconditioner.vmult(
3724 [&](
const unsigned int start_range,
const unsigned int end_range) {
3727 if (end_range > start_range)
3728 std::memset(temp_vector2.begin() + start_range,
3730 sizeof(Number) * (end_range - start_range));
3732 [&](
const unsigned int start_range,
const unsigned int end_range) {
3733 const auto solution_ptr = solution.begin();
3734 const auto solution_old_ptr = solution_old.begin();
3735 const auto tmp_ptr = temp_vector2.begin();
3737 if (iteration_index == 1)
3740 for (std::size_t i = start_range; i < end_range; ++i)
3742 factor1_plus_1 * solution_ptr[i] + factor2 * tmp_ptr[i];
3747 for (std::size_t i = start_range; i < end_range; ++i)
3748 tmp_ptr[i] = factor1_plus_1 * solution_ptr[i] -
3749 factor1 * solution_old_ptr[i] +
3750 factor2 * tmp_ptr[i];
3754 solution.swap(temp_vector2);
3755 solution_old.swap(temp_vector2);
3762 typename PreconditionerType,
3763 std::enable_if_t<has_vmult_with_std_functions<
MatrixType,
3765 PreconditionerType>,
3768 vmult_and_update(
const MatrixType &matrix,
3769 const PreconditionerType &preconditioner,
3770 const VectorType &rhs,
3771 const unsigned int iteration_index,
3772 const double factor1,
3773 const double factor2,
3774 VectorType &solution,
3775 VectorType &solution_old,
3776 VectorType &temp_vector1,
3779 using Number =
typename VectorType::value_type;
3780 VectorUpdater<Number> updater(rhs.begin(),
3781 preconditioner.get_vector().begin(),
3785 solution_old.begin(),
3786 temp_vector1.begin(),
3788 if (iteration_index > 0)
3792 [&](
const unsigned int start_range,
const unsigned int end_range) {
3795 if (end_range > start_range)
3796 std::memset(temp_vector1.begin() + start_range,
3798 sizeof(Number) * (end_range - start_range));
3800 [&](
const unsigned int start_range,
const unsigned int end_range) {
3801 if (end_range > start_range)
3802 updater.apply_to_subrange(start_range, end_range);
3805 updater.apply_to_subrange(0U, solution.locally_owned_size());
3808 solution.swap(temp_vector1);
3809 solution_old.swap(temp_vector1);
3812 template <
typename MatrixType,
typename PreconditionerType>
3814 initialize_preconditioner(
3815 const MatrixType & ,
3816 std::shared_ptr<PreconditionerType> &preconditioner)
3818 (void)preconditioner;
3822 template <
typename MatrixType,
typename VectorType>
3824 initialize_preconditioner(
3825 const MatrixType &matrix,
3826 std::shared_ptr<::DiagonalMatrix<VectorType>> &preconditioner)
3828 if (preconditioner.get() ==
nullptr || preconditioner->m() !=
matrix.m())
3830 if (preconditioner.get() ==
nullptr)
3832 std::make_shared<::DiagonalMatrix<VectorType>>();
3835 preconditioner->m() == 0,
3837 "Preconditioner appears to be initialized but not sized correctly"));
3840 if (preconditioner->m() !=
matrix.m())
3842 preconditioner->get_vector().reinit(
matrix.m());
3843 for (
typename VectorType::size_type i = 0; i <
matrix.m(); ++i)
3844 preconditioner->get_vector()(i) = 1. /
matrix.el(i, i);
3853template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
3856 const double smoothing_range,
3857 const unsigned int eig_cg_n_iterations,
3858 const double eig_cg_residual,
3859 const double max_eigenvalue,
3860 const EigenvalueAlgorithm eigenvalue_algorithm,
3861 const PolynomialType polynomial_type,
3862 const double safety_factor)
3863 :
internal::EigenvalueAlgorithmAdditionalData<PreconditionerType>(
3865 eig_cg_n_iterations,
3868 eigenvalue_algorithm,
3871 , polynomial_type(polynomial_type)
3876template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
3881 , eigenvalues_are_initialized(false)
3884 std::is_same_v<size_type, typename VectorType::size_type>,
3885 "PreconditionChebyshev and VectorType must have the same size_type.");
3890template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
3893 const MatrixType &matrix,
3894 const AdditionalData &additional_data)
3897 data = additional_data;
3899 ExcMessage(
"The degree of the Chebyshev method must be positive."));
3900 internal::PreconditionChebyshevImplementation::initialize_preconditioner(
3901 matrix, data.preconditioner);
3902 eigenvalues_are_initialized =
false;
3907template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
3911 eigenvalues_are_initialized =
false;
3912 theta = delta = 1.0;
3913 matrix_ptr =
nullptr;
3915 VectorType empty_vector;
3916 solution_old.reinit(empty_vector);
3917 temp_vector1.reinit(empty_vector);
3918 temp_vector2.reinit(empty_vector);
3920 data.preconditioner.reset();
3925template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
3932 solution_old.reinit(src);
3933 temp_vector1.reinit(src,
true);
3935 auto info = internal::estimate_eigenvalues<MatrixType>(data,
3940 data.safety_factor);
3942 const double alpha = (data.smoothing_range > 1. ?
3943 info.max_eigenvalue_estimate / data.smoothing_range :
3944 std::min(0.9 * info.max_eigenvalue_estimate,
3945 info.min_eigenvalue_estimate));
3961 Assert(data.smoothing_range < 1.,
3963 "When using PreconditionChebyshev as a solver (i.e., when "
3964 "AdditionalData::degree == numbers::invalid_unsigned_int), "
3965 "AdditionalData::smoothing_range must be strictly less than "
3966 "one because it is interpreted as the relative target "
3967 "tolerance of the Chebyshev iteration. A value >= 1 is only "
3968 "meaningful when PreconditionChebyshev is used as a smoother "
3969 "with a fixed polynomial degree."));
3971 const double actual_range = info.max_eigenvalue_estimate / alpha;
3972 const double sigma = (1. -
std::sqrt(1. / actual_range)) /
3974 const double eps = data.smoothing_range;
3979 1 +
static_cast<unsigned int>(
3984 info.degree = data.degree;
3989 (data.polynomial_type == AdditionalData::PolynomialType::fourth_kind) ?
3990 (info.max_eigenvalue_estimate) :
3991 ((info.max_eigenvalue_estimate - alpha) * 0.5);
3994 ->
theta = (info.max_eigenvalue_estimate + alpha) * 0.5;
3998 using NumberType =
typename VectorType::value_type;
4006 (std::is_same_v<VectorType,
4009 temp_vector2.reinit(src,
true);
4012 VectorType empty_vector;
4013 temp_vector2.reinit(empty_vector);
4025template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
4026template <
bool do_transpose>
4030 const VectorType &rhs,
4031 VectorType &solution)
const
4033 std::scoped_lock lock(mutex);
4034 if (eigenvalues_are_initialized ==
false)
4035 estimate_eigenvalues(rhs);
4037 if constexpr (do_transpose)
4039 matrix_ptr->Tvmult(temp_vector1, solution);
4040 internal::PreconditionChebyshevImplementation::vector_updates(
4042 *data.preconditioner,
4043 zero_out_dst ? 0 : 1,
4045 (data.polynomial_type == AdditionalData::PolynomialType::fourth_kind) ?
4046 (4. / (3. * delta)) :
4054 internal::PreconditionChebyshevImplementation::vmult_and_update(
4056 *data.preconditioner,
4058 zero_out_dst ? 0 : 1,
4060 (data.polynomial_type == AdditionalData::PolynomialType::fourth_kind) ?
4061 (4. / (3. * delta)) :
4070 if (data.degree < 2 ||
std::abs(delta) < 1e-40)
4073 double rhok = delta /
theta, sigma =
theta / delta;
4074 for (
unsigned int k = 0; k < data.degree - 1; ++k)
4076 double factor1 = 0.0;
4077 double factor2 = 0.0;
4079 if (data.polynomial_type == AdditionalData::PolynomialType::fourth_kind)
4081 factor1 = (2 * k + 1.) / (2 * k + 5.);
4082 factor2 = (8 * k + 12.) / (delta * (2 * k + 5.));
4086 const double rhokp = 1. / (2. * sigma - rhok);
4087 factor1 = rhokp * rhok;
4088 factor2 = 2. * rhokp / delta;
4092 if constexpr (do_transpose)
4094 matrix_ptr->Tvmult(temp_vector1, solution);
4095 internal::PreconditionChebyshevImplementation::vector_updates(
4097 *data.preconditioner,
4098 k + (zero_out_dst ? 1 : 2),
4108 internal::PreconditionChebyshevImplementation::vmult_and_update(
4110 *data.preconditioner,
4112 k + (zero_out_dst ? 1 : 2),
4125template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
4128 VectorType &solution,
4129 const VectorType &rhs)
const
4131 apply_internal<false>(
true, rhs, solution);
4136template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
4139 VectorType &solution,
4140 const VectorType &rhs)
const
4142 apply_internal<true>(
true, rhs, solution);
4147template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
4150 VectorType &solution,
4151 const VectorType &rhs)
const
4153 apply_internal<false>(
false, rhs, solution);
4158template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
4161 VectorType &solution,
4162 const VectorType &rhs)
const
4164 apply_internal<true>(
false, rhs, solution);
4169template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
4172 PreconditionerType>::size_type
4176 return matrix_ptr->m();
4181template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
4184 PreconditionerType>::size_type
4188 return matrix_ptr->n();
* * const_iterator()=default
unsigned int n_blocks() const
BlockType & block(const unsigned int i)
friend class ObserverPointer
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
internal::EigenvalueInformation EigenvalueInformation
types::global_dof_index size_type
void apply_internal(const bool zero_out_dst, const VectorType &src, VectorType &dst) 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
types::global_dof_index size_type
void Tvmult_add(VectorType &, const VectorType &) const
typename BaseClass::AdditionalData AdditionalData
PreconditionRelaxation< MatrixType, PreconditionerType > BaseClass
void initialize(const MatrixType &A, const AdditionalData ¶meters=AdditionalData())
internal::PreconditionRelaxation::PreconditionJacobiImpl< MatrixType > PreconditionerType
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
PreconditionRelaxation< MatrixType, PreconditionerType > BaseClass
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
internal::EigenvalueAlgorithm EigenvalueAlgorithm
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, const double safety_factor=1.2)
double get_relaxation() const
void Tvmult(VectorType &, const VectorType &) const
std::shared_ptr< PreconditionerType > preconditioner
EigenvalueInformation estimate_eigenvalues(const VectorType &src) const
internal::EigenvalueInformation EigenvalueInformation
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
PreconditionRelaxation< MatrixType, PreconditionerType > BaseClass
typename BaseClass::AdditionalData AdditionalData
void initialize(const MatrixType &A, const AdditionalData ¶meters=AdditionalData())
internal::PreconditionRelaxation::PreconditionSORImpl< MatrixType > PreconditionerType
internal::PreconditionRelaxation::PreconditionSSORImpl< SparseMatrix > PreconditionerType
PreconditionRelaxation< SparseMatrix, PreconditionerType > BaseClass
void initialize(const MatrixType &A, const AdditionalData ¶meters=AdditionalData())
typename BaseClass::AdditionalData AdditionalData
const function_ptr precondition
const MatrixType & matrix
void vmult(VectorType &dst, const VectorType &src) const
PreconditionUseMatrix(const MatrixType &M, const function_ptr method)
void(MatrixType::*)(VectorType &, const VectorType &) const function_ptr
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
#define DEAL_II_NOT_IMPLEMENTED()
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcNotInitialized()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
types::global_dof_index size_type
@ matrix
Contents is actually a matrix.
Tpetra::Vector< Number, LO, GO, NodeType< MemorySpace > > VectorType
Tpetra::CrsMatrix< Number, LO, GO, NodeType< MemorySpace > > MatrixType
* * * RotationFunction< dim, Number >::RotationFunction Number(dim)
unsigned int minimum_parallel_grain_size
constexpr bool is_supported_operation
constexpr 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
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, const double safety_factor=1.2)
PolynomialType polynomial_type
internal::EigenvalueAlgorithm EigenvalueAlgorithm
std::shared_ptr< PreconditionerType > preconditioner
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, const double safety_factor=1.2)
EigenvalueAlgorithmAdditionalData< PreconditionerType > & operator=(const EigenvalueAlgorithmAdditionalData< PreconditionerType > &other_data)
::AffineConstraints< double > constraints
unsigned int eig_cg_n_iterations
EigenvalueAlgorithm eigenvalue_algorithm
std::array< Number, 1 > eigenvalues(const SymmetricTensor< 2, 1, Number > &T)