// @HEADER // ***************************************************************************** // MueLu: A package for multigrid based preconditioning // // Copyright 2012 NTESS and the MueLu contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef MUELU_STEEPESTDESCENTSOLVER_DEF_HPP #define MUELU_STEEPESTDESCENTSOLVER_DEF_HPP #include #include #include #include "MueLu_Constraint.hpp" #include "MueLu_Monitor.hpp" #include "MueLu_Utilities.hpp" #include "MueLu_SteepestDescentSolver_decl.hpp" namespace MueLu { using Teuchos::rcp_const_cast; template SteepestDescentSolver::SteepestDescentSolver(size_t Its, SC StepLength) : nIts_(Its) , stepLength_(StepLength) {} template void SteepestDescentSolver::Iterate(const Matrix& Aref, const Constraint& C, const Matrix& P0, RCP& P) const { PrintMonitor m(*this, "SD iterations"); RCP A = rcpFromRef(Aref); RCP AP, G; Teuchos::FancyOStream& mmfancy = this->GetOStream(Statistics2); Teuchos::ArrayRCP D = Utilities::GetMatrixDiagonal_arcp(*A); RCP Ptmp_ = CrsMatrixFactory::Build(C.GetPattern()); Ptmp_->fillComplete(P0.getDomainMap(), P0.getRangeMap()); RCP Ptmp = rcp(new CrsMatrixWrap(Ptmp_)); // Initial P0 would only be used for multiplication P = rcp_const_cast(rcpFromRef(P0)); for (size_t k = 0; k < nIts_; k++) { AP = Xpetra::MatrixMatrix::Multiply(*A, false, *P, false, mmfancy, true, true); #if 0 // gradient = -2 A^T * A * P SC stepLength = 2*stepLength_; G = Xpetra::MatrixMatrix::Multiply(*A, true, *AP, false, true, true); C.Apply(*G, *Ptmp); #else // gradient = - A * P SC stepLength = stepLength_; Utilities::MyOldScaleMatrix(*AP, D, true, true, false); C.Apply(*AP, *Ptmp); #endif RCP newP; Xpetra::MatrixMatrix::TwoMatrixAdd(*Ptmp, false, -stepLength, *P, false, Teuchos::ScalarTraits::one(), newP, mmfancy); newP->fillComplete(P->getDomainMap(), P->getRangeMap()); P = newP; } } } // namespace MueLu #endif // ifndef MUELU_STEEPESTDESCENTSOLVER_DECL_HPP