46 #ifndef MUELU_CGSOLVER_DEF_HPP 47 #define MUELU_CGSOLVER_DEF_HPP 49 #include <Xpetra_MatrixFactory.hpp> 50 #include <Xpetra_MatrixMatrix.hpp> 54 #include "MueLu_Constraint.hpp" 56 #include "MueLu_Utilities.hpp" 60 using Teuchos::rcp_const_cast;
62 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
67 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
72 finalP = MatrixFactory2::BuildCopy(rcpFromRef(P0));
78 RCP<const Matrix> A = rcpFromRef(Aref);
80 RCP<Matrix> X, P, R, Z, AP;
81 RCP<Matrix> newX, tmpAP;
82 #ifndef TWO_ARG_MATRIX_ADD 83 RCP<Matrix> newR, newP;
86 SC oldRZ, newRZ, alpha, beta, app;
88 bool useTpetra = (A->getRowMap()->lib() == Xpetra::UseTpetra);
93 RCP<CrsMatrix> T_ = CrsMatrixFactory::Build(C.
GetPattern());
94 T_->fillComplete(P0.getDomainMap(), P0.getRangeMap());
95 RCP<Matrix> T = rcp(
new CrsMatrixWrap(T_));
97 SC one = Teuchos::ScalarTraits<SC>::one();
102 X = rcp_const_cast<Matrix>(rcpFromRef(P0));
104 bool doFillComplete =
true;
106 bool optimizeStorage =
true;
108 tmpAP = Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*A,
false, *X,
false, mmfancy, doFillComplete, optimizeStorage);
112 R = MatrixFactory2::BuildCopy(T);
113 #ifdef HAVE_MUELU_TPETRA 119 R->fillComplete(R->getDomainMap(), R->getRangeMap());
122 Z = MatrixFactory2::BuildCopy(R);
126 P = MatrixFactory2::BuildCopy(Z);
130 for (
size_t k = 0; k <
nIts_; k++) {
132 if (k == 0 || useTpetra) {
137 tmpAP = Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*A,
false, *P,
false, mmfancy, doFillComplete, optimizeStorage);
140 tmpAP = Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*A,
false, *P,
false, tmpAP, mmfancy, doFillComplete, optimizeStorage);
146 if (Teuchos::ScalarTraits<SC>::magnitude(app) < Teuchos::ScalarTraits<SC>::sfmin()) {
151 X = MatrixFactory2::BuildCopy(rcpFromRef(P0));
160 #ifndef TWO_ARG_MATRIX_ADD 161 newX = Teuchos::null;
162 Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::TwoMatrixAdd(*P,
false, alpha, *X,
false, Teuchos::ScalarTraits<Scalar>::one(), newX, mmfancy);
163 newX->fillComplete(P0.getDomainMap(), P0.getRangeMap());
166 Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::TwoMatrixAdd(*P,
false, alpha, *X, one);
173 #ifndef TWO_ARG_MATRIX_ADD 174 newR = Teuchos::null;
175 Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::TwoMatrixAdd(*AP,
false, -alpha, *R,
false, Teuchos::ScalarTraits<Scalar>::one(), newR, mmfancy);
176 newR->fillComplete(P0.getDomainMap(), P0.getRangeMap());
179 Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::TwoMatrixAdd(*AP,
false, -alpha, *R, one);
183 Z = MatrixFactory2::BuildCopy(R);
188 beta = newRZ / oldRZ;
191 #ifndef TWO_ARG_MATRIX_ADD 192 newP = Teuchos::null;
193 Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::TwoMatrixAdd(*P,
false, beta, *Z,
false, Teuchos::ScalarTraits<Scalar>::one(), newP, mmfancy);
194 newP->fillComplete(P0.getDomainMap(), P0.getRangeMap());
197 Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::TwoMatrixAdd(*Z,
false, one, *P, beta);
206 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
212 TEUCHOS_TEST_FOR_EXCEPTION(!A.getRowMap()->isSameAs(*B.getRowMap()),
Exceptions::Incompatible,
"MueLu::CGSolver::Frobenius: row maps are incompatible");
213 TEUCHOS_TEST_FOR_EXCEPTION(!A.isFillComplete() || !B.isFillComplete(),
Exceptions::RuntimeError,
"Matrices must be fill completed");
215 const Map& AColMap = *A.getColMap();
216 const Map& BColMap = *B.getColMap();
218 Teuchos::ArrayView<const LO> indA, indB;
219 Teuchos::ArrayView<const SC> valA, valB;
220 size_t nnzA = 0, nnzB = 0;
232 Teuchos::Array<SC> valBAll(BColMap.getNodeNumElements());
234 LO invalid = Teuchos::OrdinalTraits<LO>::invalid();
235 SC zero = Teuchos::ScalarTraits<SC> ::zero(), f = zero, gf;
236 size_t numRows = A.getNodeNumRows();
237 for (
size_t i = 0; i < numRows; i++) {
238 A.getLocalRowView(i, indA, valA);
239 B.getLocalRowView(i, indB, valB);
244 for (
size_t j = 0; j < nnzB; j++)
245 valBAll[indB[j]] = valB[j];
247 for (
size_t j = 0; j < nnzA; j++) {
250 LO ind = BColMap.getLocalElement(AColMap.getGlobalElement(indA[j]));
252 f += valBAll[ind] * valA[j];
256 for (
size_t j = 0; j < nnzB; j++)
257 valBAll[indB[j]] = zero;
267 #endif //ifndef MUELU_CGSOLVER_DECL_HPP Constraint space information for the potential prolongator.
Namespace for MueLu classes and methods.
Exception throws to report incompatible objects (like maps).
RCP< const CrsGraph > GetPattern() const
Print even more statistics.
void Iterate(const Matrix &A, const Constraint &C, const Matrix &P0, RCP< Matrix > &P) const
Iterate.
#define MueLu_sumAll(rcpComm, in, out)
static void MyOldScaleMatrix(Matrix &Op, const Teuchos::ArrayRCP< const SC > &scalingVector, bool doInverse=true, bool doFillComplete=true, bool doOptimizeStorage=true)
static RCP< Tpetra::CrsMatrix< SC, LO, GO, NO > > Op2NonConstTpetraCrs(RCP< Matrix > Op)
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
SC Frobenius(const Matrix &A, const Matrix &B) const
void Apply(const Matrix &P, Matrix &Projected) const
Apply constraint.
size_t nIts_
Number of performed iterations.
static Teuchos::ArrayRCP< SC > GetMatrixDiagonal(const Matrix &A)
Extract Matrix Diagonal.
Exception throws to report errors in the internal logical of the program.
Description of what is happening (more verbose)