| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// MueLu: A package for multigrid based preconditioning |
| 4 |
// |
| 5 |
// Copyright 2012 NTESS and the MueLu contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#ifndef MUELU_STEEPESTDESCENTSOLVER_DEF_HPP |
| 11 |
#define MUELU_STEEPESTDESCENTSOLVER_DEF_HPP |
| 12 |
|
| 13 |
#include <Xpetra_CrsMatrixFactory.hpp> |
| 14 |
#include <Xpetra_CrsMatrixWrap.hpp> |
| 15 |
#include <Xpetra_MatrixMatrix.hpp> |
| 16 |
|
| 17 |
#include "MueLu_Constraint.hpp" |
| 18 |
#include "MueLu_Monitor.hpp" |
| 19 |
#include "MueLu_Utilities.hpp" |
| 20 |
|
| 21 |
#include "MueLu_SteepestDescentSolver_decl.hpp" |
| 22 |
|
| 23 |
namespace MueLu { |
| 24 |
|
| 25 |
using Teuchos::rcp_const_cast; |
| 26 |
|
| 27 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 28 |
SteepestDescentSolver<Scalar, LocalOrdinal, GlobalOrdinal, Node>::SteepestDescentSolver(size_t Its, SC StepLength) |
| 29 |
: nIts_(Its) |
| 30 |
, stepLength_(StepLength) {} |
| 31 |
|
| 32 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 33 |
void SteepestDescentSolver<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Iterate(const Matrix& Aref, const Constraint& C, const Matrix& P0, RCP<Matrix>& P) const { |
| 34 |
PrintMonitor m(*this, "SD iterations"); |
| 35 |
|
| 36 |
RCP<const Matrix> A = rcpFromRef(Aref); |
| 37 |
RCP<Matrix> AP, G; |
| 38 |
|
| 39 |
Teuchos::FancyOStream& mmfancy = this->GetOStream(Statistics2); |
| 40 |
|
| 41 |
Teuchos::ArrayRCP<const SC> D = Utilities::GetMatrixDiagonal_arcp(*A); |
| 42 |
|
| 43 |
RCP<CrsMatrix> Ptmp_ = CrsMatrixFactory::Build(C.GetPattern()); |
| 44 |
Ptmp_->fillComplete(P0.getDomainMap(), P0.getRangeMap()); |
| 45 |
RCP<Matrix> Ptmp = rcp(new CrsMatrixWrap(Ptmp_)); |
| 46 |
|
| 47 |
// Initial P0 would only be used for multiplication |
| 48 |
P = rcp_const_cast<Matrix>(rcpFromRef(P0)); |
| 49 |
|
| 50 |
for (size_t k = 0; k < nIts_; k++) { |
| 51 |
AP = Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*A, false, *P, false, mmfancy, true, true); |
| 52 |
#if 0 |
| 53 |
// gradient = -2 A^T * A * P |
| 54 |
SC stepLength = 2*stepLength_; |
| 55 |
G = Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*A, true, *AP, false, true, true); |
| 56 |
C.Apply(*G, *Ptmp); |
| 57 |
#else |
| 58 |
// gradient = - A * P |
| 59 |
SC stepLength = stepLength_; |
| 60 |
Utilities::MyOldScaleMatrix(*AP, D, true, true, false); |
| 61 |
C.Apply(*AP, *Ptmp); |
| 62 |
#endif |
| 63 |
|
| 64 |
RCP<Matrix> newP; |
| 65 |
Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::TwoMatrixAdd(*Ptmp, false, -stepLength, *P, false, Teuchos::ScalarTraits<Scalar>::one(), newP, mmfancy); |
| 66 |
newP->fillComplete(P->getDomainMap(), P->getRangeMap()); |
| 67 |
P = newP; |
| 68 |
} |
| 69 |
} |
| 70 |
} // namespace MueLu |
| 71 |
|
| 72 |
#endif // ifndef MUELU_STEEPESTDESCENTSOLVER_DECL_HPP |