15template <
class SC,
class LO,
class GO,
class NO>
16Matrix<SC,LO,GO,NO>::Matrix():
22template <
class SC,
class LO,
class GO,
class NO>
23Matrix<SC,LO,GO,NO>::Matrix( TpetraMatrixPtr_Type& tpetraMatPtrIn ):
24matrix_( tpetraMatPtrIn )
29template <
class SC,
class LO,
class GO,
class NO>
30Matrix<SC,LO,GO,NO>::Matrix( MapConstPtr_Type map , LO numEntries):
33 matrix_ = Teuchos::RCP(
new TpetraMatrix_Type(map->getTpetraMap(), numEntries));
38template <
class SC,
class LO,
class GO,
class NO>
39Matrix<SC,LO,GO,NO>::Matrix( MatrixPtr_Type matrixIn ):
42 matrix_ =Teuchos::RCP(
new TpetraMatrix_Type( matrixIn->getMap()->getTpetraMap(), matrixIn->getGlobalMaxNumRowEntries() ));
43 if(matrixIn->isLocallyIndexed())
45 Teuchos::ArrayView<const SC> values;
46 Teuchos::ArrayView<const LO> indices;
48 MapConstPtr_Type colMap = matrixIn->getMap(
"col");
49 MapConstPtr_Type rowMap = matrixIn->getMap(
"row");
51 for (UN i=0; i<matrixIn->getNodeNumRows(); i++)
53 matrixIn->getLocalRowView( i, indices, values );
54 Teuchos::Array<GO> indicesGlobal( indices.size() );
55 for (UN j=0; j<indices.size(); j++)
57 indicesGlobal[j] = colMap->getGlobalElement( indices[j] );
60 matrix_->insertGlobalValues( rowMap->getGlobalElement( i ), indicesGlobal(), values );
64 matrix_->fillComplete( matrixIn->getMap(
"domain")->getTpetraMap(), matrixIn->getMap(
"range")->getTpetraMap() );
67template <
class SC,
class LO,
class GO,
class NO>
68Matrix<SC,LO,GO,NO>::~Matrix(){
72template <
class SC,
class LO,
class GO,
class NO>
75 TEUCHOS_TEST_FOR_EXCEPTION(matrix_.is_null(),std::runtime_error,
"");
76 matrix_->insertGlobalValues( globalRow, cols, vals );
79template <
class SC,
class LO,
class GO,
class NO>
81 return matrix_->getLocalNumRows();
85template <
class SC,
class LO,
class GO,
class NO>
88 TEUCHOS_TEST_FOR_EXCEPTION(matrix_.is_null(),std::runtime_error,
"RCP<Matrix> is null.");
89 TpetraMapConstPtr_Type tpetraMap;
90 if (!map_string.compare(
"row")) {
91 tpetraMap = matrix_->getRowMap();
93 else if (!map_string.compare(
"col")) {
94 tpetraMap = matrix_->getColMap();
96 else if (!map_string.compare(
"domain")) {
97 tpetraMap = matrix_->getDomainMap();
99 else if (!map_string.compare(
"range")) {
100 tpetraMap = matrix_->getRangeMap();
103 tpetraMap = matrix_->getMap();
105 return Teuchos::rcp(
new Map_Type(tpetraMap) );
108template <
class SC,
class LO,
class GO,
class NO>
111 TEUCHOS_TEST_FOR_EXCEPTION(matrix_.is_null(),std::runtime_error,
"RCP<Matrix> is null.");
112 TpetraMapConstPtr_Type tpetraMap;
113 if (!map_string.compare(
"row")) {
114 tpetraMap = matrix_->getRowMap();
116 else if (!map_string.compare(
"col")) {
117 tpetraMap = matrix_->getColMap();
119 else if (!map_string.compare(
"domain")) {
120 tpetraMap = matrix_->getDomainMap();
122 else if (!map_string.compare(
"range")) {
123 tpetraMap = matrix_->getRangeMap();
126 tpetraMap = matrix_->getMap();
128 return Teuchos::rcp(
new Map_Type(tpetraMap) );
132template <
class SC,
class LO,
class GO,
class NO>
135 TEUCHOS_TEST_FOR_EXCEPTION(matrix_.is_null(),std::runtime_error,
"RCP<Matrix> is null.");
137 if (!map_string.compare(
"row")) {
138 return matrix_->getRowMap();
140 else if (!map_string.compare(
"col")) {
141 return matrix_->getColMap();
143 else if (!map_string.compare(
"domain")) {
144 return matrix_->getDomainMap();
146 else if (!map_string.compare(
"range")) {
147 return matrix_->getRangeMap();
149 return matrix_->getMap();
152template <
class SC,
class LO,
class GO,
class NO>
154 Teuchos::RCP<Thyra::LinearOpBase<SC> > thyraOp = Teuchos::null;
156 Teuchos::RCP<Tpetra::CrsMatrix<SC,LO,GO,NO> > tpCrsMat = matrix_;
157 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(tpCrsMat));
158 Teuchos::RCP<Tpetra::RowMatrix<SC,LO,GO,NO> > tpRowMat = Teuchos::rcp_dynamic_cast<Tpetra::RowMatrix<SC,LO,GO,NO> >(tpCrsMat,
true);
159 Teuchos::RCP<Tpetra::Operator <SC,LO,GO,NO> > tpOperator = Teuchos::rcp_dynamic_cast<Tpetra::Operator<SC,LO,GO,NO> >(tpRowMat,
true);
161 thyraOp = Thyra::createLinearOp(tpOperator);
167template <
class SC,
class LO,
class GO,
class NO>
170 Teuchos::RCP<Thyra::LinearOpBase<SC> > thyraOp = Teuchos::null;
172 Teuchos::RCP<Tpetra::CrsMatrix<SC,LO,GO,NO> > tpCrsMat = matrix_;
173 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(tpCrsMat));
174 Teuchos::RCP<Tpetra::RowMatrix<SC,LO,GO,NO> > tpRowMat = Teuchos::rcp_dynamic_cast<Tpetra::RowMatrix<SC,LO,GO,NO> >(tpCrsMat,
true);
175 Teuchos::RCP<Tpetra::Operator <SC,LO,GO,NO> > tpOperator = Teuchos::rcp_dynamic_cast<Tpetra::Operator<SC,LO,GO,NO> >(tpRowMat,
true);
177 thyraOp = Thyra::createLinearOp(tpOperator);
179 return Teuchos::rcp_const_cast<Thyra::LinearOpBase<SC> > (thyraOp);
182template <
class SC,
class LO,
class GO,
class NO>
185 Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
186 matrix_->describe(*out,verbLevel);
189template <
class SC,
class LO,
class GO,
class NO>
191 matrix_->resumeFill();
194template <
class SC,
class LO,
class GO,
class NO>
196 matrix_->fillComplete();
199template <
class SC,
class LO,
class GO,
class NO>
201 matrix_->fillComplete( domainMap->getTpetraMap(), rangeMap->getTpetraMap() );
204template <
class SC,
class LO,
class GO,
class NO>
206 return matrix_->isFillComplete();
209template <
class SC,
class LO,
class GO,
class NO>
210bool Matrix<SC,LO,GO,NO>::isLocallyIndexed(){
211 return matrix_->isLocallyIndexed();
214template <
class SC,
class LO,
class GO,
class NO>
223 Tpetra::MatrixMatrix::Multiply( *tpA->matrix_, transposeA , *tpB->matrix_, transposeB, *matrix_,
fillComplete, std::string(), Teuchos::null);
227template <
class SC,
class LO,
class GO,
class NO>
229 MatrixPtr_Type matrix(
new Matrix_Type( matrix_ ));
230 MatrixPtr_Type diagInverse(
new Matrix_Type( matrix->getMap(
"row"), 1) );
231 MapConstPtr_Type colMap = matrix->getMap(
"col");
232 MapConstPtr_Type rowMap = matrix->getMap(
"row");
235 if(diagonalType ==
"Diagonal")
237 for(
int i =0; i< rowMap->getNodeNumElements(); i ++){
238 Teuchos::ArrayView<const SC> valuesOld;
239 Teuchos::ArrayView<const LO> indices;
240 matrix->getLocalRowView(i, indices, valuesOld);
242 GO globalDof = rowMap->getGlobalElement( i );
244 Teuchos::Array<SC> values( 1, 0);
245 Teuchos::Array<GO> indicesGO( 1 , 0 );
248 for (UN j=0; j<indices.size() && !setOne; j++) {
249 if ( colMap->getGlobalElement( indices[j] ) == globalDof ){
250 values[0] = 1./valuesOld[j];
251 indicesGO[0] = colMap->getGlobalElement(indices[j]);
255 GO row = GO ( rowMap->getGlobalElement( i) );
256 diagInverse->insertGlobalValues( row, indicesGO(), values() );
259 else if(diagonalType ==
"AbsRowSum")
261 for(
int i =0; i< rowMap->getNodeNumElements(); i ++){
262 Teuchos::ArrayView<const SC> valuesOld;
263 Teuchos::ArrayView<const LO> indices;
264 matrix->getLocalRowView(i, indices, valuesOld);
266 GO globalDof = rowMap->getGlobalElement( i );
269 for (UN j=0; j<indices.size(); j++) {
270 rowSum += abs(valuesOld[j]);
273 Teuchos::Array<SC> values( 1, 0);
274 Teuchos::Array<GO> indicesGO( 1 , 0 );
277 for (UN j=0; j<indices.size() && !setOne; j++) {
278 if ( colMap->getGlobalElement( indices[j] ) == globalDof ){
279 values[0] = 1./rowSum;
280 indicesGO[0] = colMap->getGlobalElement(indices[j]);
285 GO row = GO ( rowMap->getGlobalElement( i) );
286 diagInverse->insertGlobalValues( row, indicesGO(), values() );
290 diagInverse->fillComplete();
300template <
class SC,
class LO,
class GO,
class NO>
302 TEUCHOS_TEST_FOR_EXCEPTION(matrix_->isLocallyIndexed(),std::logic_error,
"Underlying matrix is locally indexed and we can not use a global row view. Global row copy is valid here and can be implemented.");
304 typename Tpetra::CrsMatrix<SC,LO,GO,NO>::global_inds_host_view_type Indices;
305 typename Tpetra::CrsMatrix<SC,LO,GO,NO>::values_host_view_type Values;
309 matrix_->getGlobalRowView(globalRow, Indices, Values);
310 indices = Teuchos::ArrayView<const GO> (Indices.data(), Indices.extent(0));
311 values = Teuchos::ArrayView<const SC> (
reinterpret_cast<const SC*
>(Values.data()), Values.extent(0));
315template <
class SC,
class LO,
class GO,
class NO>
317 TEUCHOS_TEST_FOR_EXCEPTION(matrix_->isGloballyIndexed(),std::logic_error,
"Underlying matrix is globally indexed and we can not use a local row view.");
318 typename Tpetra::CrsMatrix<SC,LO,GO,NO>::local_inds_host_view_type Indices;
319 typename Tpetra::CrsMatrix<SC,LO,GO,NO>::values_host_view_type Values;
323 matrix_->getLocalRowView(localRow, Indices, Values);
324 indices = Teuchos::ArrayView<const LO> (Indices.data(), Indices.extent(0));
325 values = Teuchos::ArrayView<const SC> (
reinterpret_cast<const SC*
>(Values.data()), Values.extent(0));
330template <
class SC,
class LO,
class GO,
class NO>
332 matrix_->replaceGlobalValues(globalRow, indices, values);
335template <
class SC,
class LO,
class GO,
class NO>
337 matrix_->replaceLocalValues(localRow, indices, values);
340template <
class SC,
class LO,
class GO,
class NO>
345template <
class SC,
class LO,
class GO,
class NO>
348 Teuchos::ETransp mode,
352 TpetraMVPtr_Type yTpetra = Y.getTpetraMultiVectorNonConst();
354 matrix_->apply( *X.getTpetraMultiVector(), *yTpetra, mode, alpha, beta );
357template <
class SC,
class LO,
class GO,
class NO>
360 matrix_->scale( alpha );
364template <
class SC,
class LO,
class GO,
class NO>
366 TEUCHOS_TEST_FOR_EXCEPTION( matrix_.is_null(), std::runtime_error,
"Matrix in writeMM is null.");
368 TpetraMatrixPtr_Type tpetraMat = matrix_;
370 Tpetra::MatrixMarket::Writer< TpetraMatrix_Type > tpetraWriter;
372 tpetraWriter.writeSparseFile(fileName, tpetraMat,
"matrix",
"");
375template <
class SC,
class LO,
class GO,
class NO>
378 if (B->isFillComplete())
380 TEUCHOS_TEST_FOR_EXCEPTION( B->isLocallyIndexed(), std::runtime_error,
"Matrix in is locally index but Trilinos Tpetra cannot add to a matrix at this stage.");
382 Tpetra::MatrixMatrix::Add(*matrix_,
false, alpha, *B->matrix_, beta);
386template <
class SC,
class LO,
class GO,
class NO>
389 MapConstPtr_Type map = this->
getMap(
"row");
390 MapConstPtr_Type mapCol = this->
getMap(
"col");
392 int numMV = mapCol->getMaxAllGlobalIndex() + 1;
394 mv = Teuchos::rcp(
new MultiVector_Type ( map, numMV ) );
395 Teuchos::ArrayView< const LO > indices;
396 Teuchos::ArrayView< const SC > values;
397 Teuchos::ArrayRCP< SC > valuesMV;
401 for (
int j=0; j<indices.size(); j++) {
402 globalCol = mapCol->getGlobalElement( indices[j] );
403 valuesMV = mv->getDataNonConst( globalCol );
404 valuesMV[ i ] = values[ j ];
408template <
class SC,
class LO,
class GO,
class NO>
410 return matrix_->getGlobalMaxNumRowEntries();
413template <
class SC,
class LO,
class GO,
class NO>
414void Matrix<SC,LO,GO,NO>::insertLocalValues(LO localRow,
const Teuchos::ArrayView< const LO > &cols,
const Teuchos::ArrayView< const SC > &vals){
416 TEUCHOS_TEST_FOR_EXCEPTION(matrix_.is_null(),std::runtime_error,
"");
417 matrix_->insertLocalValues( localRow, cols, vals );
421template <
class SC,
class LO,
class GO,
class NO>
426 if ( importer_.is_null() || !reuseImport) {
428 importer_ = Teuchos::RCP(
new Tpetra::Import<LO, GO, NO>( mvIn->getMapTpetra(), this->getMapTpetra() ));
429 else if(type==
"Reverse")
430 importer_ = Teuchos::RCP(
new Tpetra::Import<LO, GO, NO>( this->
getMapTpetra(), mvIn->getMapTpetra() ));
432 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Unknown type for import. Choose Forward or Reverse");
435 TEUCHOS_TEST_FOR_EXCEPTION( !importer_->getSourceMap()->isSameAs( *mvIn->getMap()->getTpetraMap() ), std::logic_error,
"Source maps of Importer and Matrix are not the same.");
436 TEUCHOS_TEST_FOR_EXCEPTION( !importer_->getTargetMap()->isSameAs( *this->getMap()->getTpetraMap() ), std::logic_error,
"Target maps of Importer and Matrix are not the same.");
440 if (type==
"Forward") {
441 if ( !combineMode.compare(
"Insert") )
442 matrix_->doImport ( *mvIn->getTpetraMatrix(), *importer_, Tpetra::INSERT);
443 else if ( !combineMode.compare(
"Add") )
444 matrix_->doImport ( *mvIn->getTpetraMatrix(), *importer_, Tpetra::ADD);
446 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Unknown combine mode.");
448 else if(type==
"Reverse"){
449 if ( !combineMode.compare(
"Insert") )
450 matrix_->doExport ( *mvIn->getTpetraMatrix(), *importer_, Tpetra::INSERT);
451 else if ( !combineMode.compare(
"Add") )
452 matrix_->doExport ( *mvIn->getTpetraMatrix(), *importer_, Tpetra::ADD);
454 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Unknown combine mode.");
457 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Unknown type for import. Choose Forward or Reverse");
460template <
class SC,
class LO,
class GO,
class NO>
465 if ( exporter_.is_null() || !reuseExport) {
467 exporter_ = Teuchos::RCP(
new Tpetra::Export<LO, GO, NO>( mvIn->getMapTpetra(), this->getMapTpetra() ));
468 else if(type==
"Reverse")
469 exporter_ = Teuchos::RCP(
new Tpetra::Export<LO, GO, NO>( this->
getMapTpetra(), mvIn->getMapTpetra() ));
471 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Unknown type for import. Choose Forward or Reverse");
474 TEUCHOS_TEST_FOR_EXCEPTION( !exporter_->getSourceMap()->isSameAs( *this->getMap()->getTpetraMap() ), std::logic_error,
"Source maps of Exporter and Multivector are not the same.");
475 TEUCHOS_TEST_FOR_EXCEPTION( !exporter_->getTargetMap()->isSameAs( *mvIn->getMap()->getTpetraMap() ), std::logic_error,
"Target maps of Exporter and Multivector are not the same.");
479 if (type==
"Forward") {
480 if ( !combineMode.compare(
"Insert") )
481 matrix_->doExport ( *mvIn->getTpetraMatrix(), *exporter_, Tpetra::INSERT);
482 else if ( !combineMode.compare(
"Add") )
483 matrix_->doExport ( *mvIn->getTpetraMatrix(), *exporter_, Tpetra::ADD);
485 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Unknown combine mode.");
487 else if(type==
"Reverse"){
488 if ( !combineMode.compare(
"Insert") )
489 matrix_->doImport ( *mvIn->getTpetraMatrix(), *exporter_, Tpetra::INSERT);
490 else if ( !combineMode.compare(
"Add") )
491 matrix_->doImport ( *mvIn->getTpetraMatrix(), *exporter_, Tpetra::ADD);
493 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Unknown combine mode.");
496 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Unknown type for import. Choose Forward or Reverse");
void getLocalRowView(LO localRow, Teuchos::ArrayView< const LO > &indices, Teuchos::ArrayView< const SC > &values) const
Extracting single rows of Matrix with local row ID. Indices returns local indices of entries stored i...
Definition Matrix_def.hpp:316
MatrixPtr_Type buildDiagonalInverse(std::string diagonalType)
Definition Matrix_def.hpp:228
Teuchos::RCP< const Thyra::LinearOpBase< SC > > getThyraLinOp() const
i.e. for NOX
Definition Matrix_def.hpp:153
LO getNodeNumRows() const
Returns the local number of rows.
Definition Matrix_def.hpp:80
void print(Teuchos::EVerbosityLevel verbLevel=Teuchos::VERB_EXTREME)
printing matrix
Definition Matrix_def.hpp:183
void replaceLocalValues(LO localRow, const Teuchos::ArrayView< const LO > &indices, const Teuchos::ArrayView< const SC > &values)
Replacing single rows of Matrix with local row ID. Indices returns local indices of entries stored in...
Definition Matrix_def.hpp:336
void replaceGlobalValues(GO globalRow, const Teuchos::ArrayView< const GO > &indices, const Teuchos::ArrayView< const SC > &values)
Replacing single rows of Matrix with global row ID. Indices returns global indices of entries stored ...
Definition Matrix_def.hpp:331
void exportFromVector(MatrixPtr_Type mvIn, bool reuseExport=false, std::string combineMode="Insert", std::string type="Forward")
Definition Matrix_def.hpp:461
void getGlobalRowView(GO globalRow, Teuchos::ArrayView< const GO > &indices, Teuchos::ArrayView< const SC > &values) const
Extracting single rows of Matrix with global row ID. Indices returns global indices of entries stored...
Definition Matrix_def.hpp:301
void toMV(MultiVectorPtr_Type &mv)
Turning Matrix into MultiVector Format.
Definition Matrix_def.hpp:387
void scale(const SC &alpha)
Scaling this with constant alpha.
Definition Matrix_def.hpp:358
TpetraMapConstPtr_Type getMapTpetra(std::string map_string="")
Return map in Tpetra Format of type " ".
Definition Matrix_def.hpp:133
MapConstPtr_Type getMap(std::string map_string="")
Returns map of type " ". i.e. row or column map.
Definition Matrix_def.hpp:86
Teuchos::RCP< Thyra::LinearOpBase< SC > > getThyraLinOpNonConst()
i.e. for NOX
Definition Matrix_def.hpp:168
void insertGlobalValues(GO globalRow, const Teuchos::ArrayView< const GO > &cols, const Teuchos::ArrayView< const SC > &vals)
Intertion of values in global row 'globalRow'. Matrix is distributed nodewise. If values are added to...
Definition Matrix_def.hpp:73
bool isFillComplete()
Check if matrix is already filled complete.
Definition Matrix_def.hpp:205
void writeMM(std::string fileName="matrix.mm") const
Writing Matrix in file.
Definition Matrix_def.hpp:365
void importFromVector(MatrixPtr_Type mvIn, bool reuseImport=false, std::string combineMode="Insert", std::string type="Forward")
Definition Matrix_def.hpp:422
void resumeFill()
Resuming filling process. But only limited options i.e. scaling remain.
Definition Matrix_def.hpp:190
void addMatrix(SC alpha, const MatrixPtr_Type &B, SC beta)
B = alpha*this + beta*B.
Definition Matrix_def.hpp:376
void apply(const MultiVector_Type &X, MultiVector_Type &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, SC alpha=Teuchos::ScalarTraits< SC >::one(), SC beta=Teuchos::ScalarTraits< SC >::zero()) const
Matrix Vector Operation. Applying MultiVector X to this. Y = alpha * (this)^mode * X + beta * Y....
Definition Matrix_def.hpp:346
void fillComplete()
after inserting global values into matrix. After this step the column map is fixed....
Definition Matrix_def.hpp:195
void Multiply(const MatrixPtr_Type &tpA, bool transposeA, const MatrixPtr_Type &tpB, bool transposeB, bool fillComplete=true)
Definition Matrix_def.hpp:215
LO getGlobalMaxNumRowEntries() const
Maximum number of entries in any row of the matrix, over all processes.
Definition Matrix_def.hpp:409
TpetraMatrixConstPtr_Type getTpetraMatrix() const
Return matrix in Tpetra Format.
Definition Matrix_def.hpp:341
Adaptive Mesh Refinement.
Definition AdaptiveMeshRefinement_decl.hpp:36