42 #ifndef TPETRA_REPLACEDIAGONALCRSMATRIX_DEF_HPP
43 #define TPETRA_REPLACEDIAGONALCRSMATRIX_DEF_HPP
49 #include "Tpetra_CrsMatrix.hpp"
50 #include "Tpetra_Vector.hpp"
54 template<
class SC,
class LO,
class GO,
class NT>
57 const Vector<SC, LO, GO, NT>& newDiag) {
59 using map_type = Map<LO, GO, NT>;
60 using crs_matrix_type = CrsMatrix<SC, LO, GO, NT>;
63 const LO oneLO = Teuchos::OrdinalTraits<LO>::one();
66 LO numReplacedDiagEntries = 0;
69 auto rowMapPtr = matrix.getRowMap();
70 if (rowMapPtr.is_null() || rowMapPtr->getComm().is_null()) {
74 return numReplacedDiagEntries;
76 auto colMapPtr = matrix.getColMap();
78 TEUCHOS_TEST_FOR_EXCEPTION
79 (colMapPtr.get () ==
nullptr, std::invalid_argument,
80 "replaceDiagonalCrsMatrix: "
81 "Input matrix A must have a nonnull column Map.");
83 const map_type& rowMap = *rowMapPtr;
84 const map_type& colMap = *colMapPtr;
85 const LO myNumRows =
static_cast<LO
>(matrix.getNodeNumRows());
86 const bool isFillCompleteOnInput = matrix.isFillComplete();
89 TEUCHOS_TEST_FOR_EXCEPTION(!rowMap.isSameAs(*newDiag.getMap()), std::runtime_error,
90 "Row map of matrix and map of input vector do not match.");
93 crs_matrix_type::execution_space::fence();
95 if (isFillCompleteOnInput)
98 Teuchos::ArrayRCP<const SC> newDiagData = newDiag.getVector(0)->getData();
99 LO numReplacedEntriesPerRow = 0;
102 for (LO lclRowInd = 0; lclRowInd < myNumRows; ++lclRowInd) {
105 const GO gblInd = rowMap.getGlobalElement(lclRowInd);
106 const LO lclColInd = colMap.getLocalElement(gblInd);
108 const SC vals[] = {
static_cast<SC
>(newDiagData[lclRowInd])};
109 const LO cols[] = {lclColInd};
112 numReplacedEntriesPerRow = matrix.replaceLocalValues(lclRowInd, oneLO, vals, cols);
115 if (numReplacedEntriesPerRow == oneLO) {
116 ++numReplacedDiagEntries;
118 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
119 "Number of replaced entries in this row is not equal to one. "
120 "It has to be exactly one, since we want to replace the diagonal element.");
124 if (isFillCompleteOnInput)
125 matrix.fillComplete();
127 return numReplacedDiagEntries;
137 #define TPETRA_REPLACEDIAGONALCRSMATRIX_INSTANT(SCALAR,LO,GO,NODE) \
140 LO replaceDiagonalCrsMatrix( \
141 CrsMatrix<SCALAR, LO, GO, NODE>& matrix, \
142 const Vector<SCALAR, LO, GO, NODE>& newDiag); \
145 #endif // #ifndef TPETRA_REPLACEDIAGONALCRSMATRIX_DEF_HPP
static bool debug()
Whether Tpetra is in debug mode.
LO replaceDiagonalCrsMatrix(::Tpetra::CrsMatrix< SC, LO, GO, NT > &matrix, const ::Tpetra::Vector< SC, LO, GO, NT > &newDiag)
Replace diagonal entries of an input Tpetra::CrsMatrix matrix with values given in newDiag...
Declaration of Tpetra::Details::Behavior, a class that describes Tpetra's behavior.