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.");
96 typename crs_matrix_type::execution_space().fence();
98 if (isFillCompleteOnInput)
101 Teuchos::ArrayRCP<const SC> newDiagData = newDiag.getVector(0)->getData();
102 LO numReplacedEntriesPerRow = 0;
105 for (LO lclRowInd = 0; lclRowInd < myNumRows; ++lclRowInd) {
108 const GO gblInd = rowMap.getGlobalElement(lclRowInd);
109 const LO lclColInd = colMap.getLocalElement(gblInd);
111 const SC vals[] = {
static_cast<SC
>(newDiagData[lclRowInd])};
112 const LO cols[] = {lclColInd};
115 numReplacedEntriesPerRow = matrix.replaceLocalValues(lclRowInd, oneLO, vals, cols);
118 if (numReplacedEntriesPerRow == oneLO) {
119 ++numReplacedDiagEntries;
121 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
122 "Number of replaced entries in this row is not equal to one. "
123 "It has to be exactly one, since we want to replace the diagonal element.");
127 if (isFillCompleteOnInput)
128 matrix.fillComplete();
130 return numReplacedDiagEntries;
140 #define TPETRA_REPLACEDIAGONALCRSMATRIX_INSTANT(SCALAR,LO,GO,NODE) \
143 LO replaceDiagonalCrsMatrix( \
144 CrsMatrix<SCALAR, LO, GO, NODE>& matrix, \
145 const Vector<SCALAR, LO, GO, NODE>& newDiag); \
148 #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.