3#include "Matrix_decl.hpp"
16template <
class SC,
class LO,
class GO,
class NO>
17Matrix<SC,LO,GO,NO>::Matrix():
23template <
class SC,
class LO,
class GO,
class NO>
24Matrix<SC,LO,GO,NO>::Matrix( TpetraMatrixPtr_Type& tpetraMatPtrIn ):
25matrix_( tpetraMatPtrIn )
30template <
class SC,
class LO,
class GO,
class NO>
31Matrix<SC,LO,GO,NO>::Matrix( MapConstPtr_Type map , LO numEntries):
34 matrix_ = Teuchos::RCP(
new TpetraMatrix_Type(map->getTpetraMap(), numEntries));
39template <
class SC,
class LO,
class GO,
class NO>
40Matrix<SC,LO,GO,NO>::Matrix( MatrixPtr_Type matrixIn ):
43 matrix_ =Teuchos::RCP(
new TpetraMatrix_Type( matrixIn->getMap()->getTpetraMap(), matrixIn->getGlobalMaxNumRowEntries() ));
44 if(matrixIn->isLocallyIndexed())
46 Teuchos::ArrayView<const SC> values;
47 Teuchos::ArrayView<const LO> indices;
49 MapConstPtr_Type colMap = matrixIn->getMap(
"col");
50 MapConstPtr_Type rowMap = matrixIn->getMap(
"row");
52 for (UN i=0; i<matrixIn->getNodeNumRows(); i++)
54 matrixIn->getLocalRowView( i, indices, values );
55 Teuchos::Array<GO> indicesGlobal( indices.size() );
56 for (UN j=0; j<indices.size(); j++)
58 indicesGlobal[j] = colMap->getGlobalElement( indices[j] );
61 matrix_->insertGlobalValues( rowMap->getGlobalElement( i ), indicesGlobal(), values );
65 matrix_->fillComplete( matrixIn->getMap(
"domain")->getTpetraMap(), matrixIn->getMap(
"range")->getTpetraMap() );
68template <
class SC,
class LO,
class GO,
class NO>
69Matrix<SC,LO,GO,NO>::~Matrix(){
73template <
class SC,
class LO,
class GO,
class NO>
76 TEUCHOS_TEST_FOR_EXCEPTION(matrix_.is_null(),std::runtime_error,
"");
77 matrix_->insertGlobalValues( globalRow, cols, vals );
80template <
class SC,
class LO,
class GO,
class NO>
82 return matrix_->getLocalNumRows();
86template <
class SC,
class LO,
class GO,
class NO>
89 TEUCHOS_TEST_FOR_EXCEPTION(matrix_.is_null(),std::runtime_error,
"RCP<Matrix> is null.");
90 TpetraMapConstPtr_Type tpetraMap;
91 if (!map_string.compare(
"row")) {
92 tpetraMap = matrix_->getRowMap();
94 else if (!map_string.compare(
"col")) {
95 tpetraMap = matrix_->getColMap();
97 else if (!map_string.compare(
"domain")) {
98 tpetraMap = matrix_->getDomainMap();
100 else if (!map_string.compare(
"range")) {
101 tpetraMap = matrix_->getRangeMap();
104 tpetraMap = matrix_->getMap();
106 return Teuchos::rcp(
new Map_Type(tpetraMap) );
109template <
class SC,
class LO,
class GO,
class NO>
112 TEUCHOS_TEST_FOR_EXCEPTION(matrix_.is_null(),std::runtime_error,
"RCP<Matrix> is null.");
113 TpetraMapConstPtr_Type tpetraMap;
114 if (!map_string.compare(
"row")) {
115 tpetraMap = matrix_->getRowMap();
117 else if (!map_string.compare(
"col")) {
118 tpetraMap = matrix_->getColMap();
120 else if (!map_string.compare(
"domain")) {
121 tpetraMap = matrix_->getDomainMap();
123 else if (!map_string.compare(
"range")) {
124 tpetraMap = matrix_->getRangeMap();
127 tpetraMap = matrix_->getMap();
129 return Teuchos::rcp(
new Map_Type(tpetraMap) );
133template <
class SC,
class LO,
class GO,
class NO>
136 TEUCHOS_TEST_FOR_EXCEPTION(matrix_.is_null(),std::runtime_error,
"RCP<Matrix> is null.");
138 if (!map_string.compare(
"row")) {
139 return matrix_->getRowMap();
141 else if (!map_string.compare(
"col")) {
142 return matrix_->getColMap();
144 else if (!map_string.compare(
"domain")) {
145 return matrix_->getDomainMap();
147 else if (!map_string.compare(
"range")) {
148 return matrix_->getRangeMap();
150 return matrix_->getMap();
153template <
class SC,
class LO,
class GO,
class NO>
158 Teuchos::RCP<Thyra::LinearOpBase<SC> > thyraOp = Teuchos::null;
160 Teuchos::RCP<Tpetra::CrsMatrix<SC,LO,GO,NO> > tpCrsMat = matrix_;
161 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(tpCrsMat));
162 Teuchos::RCP<Tpetra::RowMatrix<SC,LO,GO,NO> > tpRowMat = Teuchos::rcp_dynamic_cast<Tpetra::RowMatrix<SC,LO,GO,NO> >(tpCrsMat,
true);
163 Teuchos::RCP<Tpetra::Operator <SC,LO,GO,NO> > tpOperator = Teuchos::rcp_dynamic_cast<Tpetra::Operator<SC,LO,GO,NO> >(tpRowMat,
true);
165 thyraOp = Thyra::createLinearOp(tpOperator);
171template <
class SC,
class LO,
class GO,
class NO>
175 Teuchos::RCP<Thyra::LinearOpBase<SC> > thyraOp = Teuchos::null;
177 Teuchos::RCP<Tpetra::CrsMatrix<SC,LO,GO,NO> > tpCrsMat = matrix_;
178 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(tpCrsMat));
179 Teuchos::RCP<Tpetra::RowMatrix<SC,LO,GO,NO> > tpRowMat = Teuchos::rcp_dynamic_cast<Tpetra::RowMatrix<SC,LO,GO,NO> >(tpCrsMat,
true);
180 Teuchos::RCP<Tpetra::Operator <SC,LO,GO,NO> > tpOperator = Teuchos::rcp_dynamic_cast<Tpetra::Operator<SC,LO,GO,NO> >(tpRowMat,
true);
182 thyraOp = Thyra::createLinearOp(tpOperator);
184 return Teuchos::rcp_const_cast<Thyra::LinearOpBase<SC> > (thyraOp);
187template <
class SC,
class LO,
class GO,
class NO>
190 Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
191 matrix_->describe(*out,verbLevel);
194template <
class SC,
class LO,
class GO,
class NO>
196 matrix_->resumeFill();
199template <
class SC,
class LO,
class GO,
class NO>
201 matrix_->fillComplete();
204template <
class SC,
class LO,
class GO,
class NO>
206 matrix_->fillComplete( domainMap->getTpetraMap(), rangeMap->getTpetraMap() );
209template <
class SC,
class LO,
class GO,
class NO>
211 return matrix_->isFillComplete();
214template <
class SC,
class LO,
class GO,
class NO>
215bool Matrix<SC,LO,GO,NO>::isLocallyIndexed(){
216 return matrix_->isLocallyIndexed();
224template <
class SC,
class LO,
class GO,
class NO>
226 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.");
228 typename Tpetra::CrsMatrix<SC,LO,GO,NO>::global_inds_host_view_type Indices;
229 typename Tpetra::CrsMatrix<SC,LO,GO,NO>::values_host_view_type Values;
233 matrix_->getGlobalRowView(globalRow, Indices, Values);
234 indices = Teuchos::ArrayView<const GO> (Indices.data(), Indices.extent(0));
235 values = Teuchos::ArrayView<const SC> (
reinterpret_cast<const SC*
>(Values.data()), Values.extent(0));
239template <
class SC,
class LO,
class GO,
class NO>
241 TEUCHOS_TEST_FOR_EXCEPTION(matrix_->isGloballyIndexed(),std::logic_error,
"Underlying matrix is globally indexed and we can not use a local row view.");
242 typename Tpetra::CrsMatrix<SC,LO,GO,NO>::local_inds_host_view_type Indices;
243 typename Tpetra::CrsMatrix<SC,LO,GO,NO>::values_host_view_type Values;
247 matrix_->getLocalRowView(localRow, Indices, Values);
248 indices = Teuchos::ArrayView<const LO> (Indices.data(), Indices.extent(0));
249 values = Teuchos::ArrayView<const SC> (
reinterpret_cast<const SC*
>(Values.data()), Values.extent(0));
254template <
class SC,
class LO,
class GO,
class NO>
256 matrix_->replaceGlobalValues(globalRow, indices, values);
259template <
class SC,
class LO,
class GO,
class NO>
261 matrix_->replaceLocalValues(localRow, indices, values);
264template <
class SC,
class LO,
class GO,
class NO>
269template <
class SC,
class LO,
class GO,
class NO>
272 Teuchos::ETransp mode,
276 TpetraMVPtr_Type yTpetra = Y.getTpetraMultiVectorNonConst();
278 matrix_->apply( *X.getTpetraMultiVector(), *yTpetra, mode, alpha, beta );
281template <
class SC,
class LO,
class GO,
class NO>
284 matrix_->scale( alpha );
288template <
class SC,
class LO,
class GO,
class NO>
290 TEUCHOS_TEST_FOR_EXCEPTION( matrix_.is_null(), std::runtime_error,
"Matrix in writeMM is null.");
298 TpetraMatrixPtr_Type tpetraMat = matrix_;
300 Tpetra::MatrixMarket::Writer< TpetraMatrix_Type > tpetraWriter;
302 tpetraWriter.writeSparseFile(fileName, tpetraMat,
"matrix",
"");
305template <
class SC,
class LO,
class GO,
class NO>
308 if (B->isFillComplete())
310 TEUCHOS_TEST_FOR_EXCEPTION( B->isLocallyIndexed(), std::runtime_error,
"Matrix in is locally index but Trilinos Epetra/Tpetra can not add to a matrix at this stage.");
315 Tpetra::MatrixMatrix::Add(*matrix_,
false, alpha, *B->matrix_, beta);
320template <
class SC,
class LO,
class GO,
class NO>
323 MapConstPtr_Type map = this->
getMap(
"row");
324 MapConstPtr_Type mapCol = this->
getMap(
"col");
326 int numMV = mapCol->getMaxAllGlobalIndex() + 1;
328 mv = Teuchos::rcp(
new MultiVector_Type ( map, numMV ) );
329 Teuchos::ArrayView< const LO > indices;
330 Teuchos::ArrayView< const SC > values;
331 Teuchos::ArrayRCP< SC > valuesMV;
335 for (
int j=0; j<indices.size(); j++) {
336 globalCol = mapCol->getGlobalElement( indices[j] );
337 valuesMV = mv->getDataNonConst( globalCol );
338 valuesMV[ i ] = values[ j ];
342template <
class SC,
class LO,
class GO,
class NO>
344 return matrix_->getGlobalMaxNumRowEntries();
347template <
class SC,
class LO,
class GO,
class NO>
348void Matrix<SC,LO,GO,NO>::insertLocalValues(LO localRow,
const Teuchos::ArrayView< const LO > &cols,
const Teuchos::ArrayView< const SC > &vals){
350 TEUCHOS_TEST_FOR_EXCEPTION(matrix_.is_null(),std::runtime_error,
"");
351 matrix_->insertLocalValues( localRow, cols, vals );
355template <
class SC,
class LO,
class GO,
class NO>
356void Matrix<SC,LO,GO,NO>::importFromVector( MatrixPtr_Type mvIn,
bool reuseImport, std::string combineMode, std::string type) {
360 if ( importer_.is_null() || !reuseImport) {
362 importer_ = Teuchos::RCP(
new Tpetra::Import<LO, GO, NO>( mvIn->getMapTpetra(), this->getMapTpetra() ));
363 else if(type==
"Reverse")
364 importer_ = Teuchos::RCP(
new Tpetra::Import<LO, GO, NO>( this->getMapTpetra(), mvIn->getMapTpetra() ));
366 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Unknown type for import. Choose Forward or Reverse");
369 TEUCHOS_TEST_FOR_EXCEPTION( !importer_->getSourceMap()->isSameAs( *mvIn->getMap()->getTpetraMap() ), std::logic_error,
"Source maps of Importer and Matrix are not the same.");
370 TEUCHOS_TEST_FOR_EXCEPTION( !importer_->getTargetMap()->isSameAs( *this->getMap()->getTpetraMap() ), std::logic_error,
"Target maps of Importer and Matrix are not the same.");
374 if (type==
"Forward") {
375 if ( !combineMode.compare(
"Insert") )
376 matrix_->doImport ( *mvIn->getTpetraMatrix(), *importer_, Tpetra::INSERT);
377 else if ( !combineMode.compare(
"Add") )
378 matrix_->doImport ( *mvIn->getTpetraMatrix(), *importer_, Tpetra::ADD);
380 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Unknown combine mode.");
382 else if(type==
"Reverse"){
383 if ( !combineMode.compare(
"Insert") )
384 matrix_->doExport ( *mvIn->getTpetraMatrix(), *importer_, Tpetra::INSERT);
385 else if ( !combineMode.compare(
"Add") )
386 matrix_->doExport ( *mvIn->getTpetraMatrix(), *importer_, Tpetra::ADD);
388 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Unknown combine mode.");
391 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Unknown type for import. Choose Forward or Reverse");
394template <
class SC,
class LO,
class GO,
class NO>
395void Matrix<SC,LO,GO,NO>::exportFromVector( MatrixPtr_Type mvIn,
bool reuseExport, std::string combineMode, std::string type) {
399 if ( exporter_.is_null() || !reuseExport) {
401 exporter_ = Teuchos::RCP(
new Tpetra::Export<LO, GO, NO>( mvIn->getMapTpetra(), this->getMapTpetra() ));
402 else if(type==
"Reverse")
403 exporter_ = Teuchos::RCP(
new Tpetra::Export<LO, GO, NO>( this->getMapTpetra(), mvIn->getMapTpetra() ));
405 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Unknown type for import. Choose Forward or Reverse");
408 TEUCHOS_TEST_FOR_EXCEPTION( !exporter_->getSourceMap()->isSameAs( *this->getMap()->getTpetraMap() ), std::logic_error,
"Source maps of Exporter and Multivector are not the same.");
409 TEUCHOS_TEST_FOR_EXCEPTION( !exporter_->getTargetMap()->isSameAs( *mvIn->getMap()->getTpetraMap() ), std::logic_error,
"Target maps of Exporter and Multivector are not the same.");
413 if (type==
"Forward") {
414 if ( !combineMode.compare(
"Insert") )
415 matrix_->doExport ( *mvIn->getTpetraMatrix(), *exporter_, Tpetra::INSERT);
416 else if ( !combineMode.compare(
"Add") )
417 matrix_->doExport ( *mvIn->getTpetraMatrix(), *exporter_, Tpetra::ADD);
419 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Unknown combine mode.");
421 else if(type==
"Reverse"){
422 if ( !combineMode.compare(
"Insert") )
423 matrix_->doImport ( *mvIn->getTpetraMatrix(), *exporter_, Tpetra::INSERT);
424 else if ( !combineMode.compare(
"Add") )
425 matrix_->doImport ( *mvIn->getTpetraMatrix(), *exporter_, Tpetra::ADD);
427 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Unknown combine mode.");
430 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:240
Teuchos::RCP< const Thyra::LinearOpBase< SC > > getThyraLinOp() const
i.e. for NOX
Definition Matrix_def.hpp:154
LO getNodeNumRows() const
Returns the local number of rows.
Definition Matrix_def.hpp:81
void print(Teuchos::EVerbosityLevel verbLevel=Teuchos::VERB_EXTREME)
printing matrix
Definition Matrix_def.hpp:188
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:260
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:255
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:225
void toMV(MultiVectorPtr_Type &mv)
Turning Matrix into MultiVector Format.
Definition Matrix_def.hpp:321
void scale(const SC &alpha)
Scaling this with constant alpha.
Definition Matrix_def.hpp:282
TpetraMapConstPtr_Type getMapTpetra(std::string map_string="")
Return map in Xpetra Format of type " ".
Definition Matrix_def.hpp:134
MapConstPtr_Type getMap(std::string map_string="")
Returns map of type " ". i.e. row or column map.
Definition Matrix_def.hpp:87
Teuchos::RCP< Thyra::LinearOpBase< SC > > getThyraLinOpNonConst()
i.e. for NOX
Definition Matrix_def.hpp:172
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:74
bool isFillComplete()
Check if matrix is already filled complete.
Definition Matrix_def.hpp:210
void writeMM(std::string fileName="matrix.mm") const
Writing Matrix in file.
Definition Matrix_def.hpp:289
void resumeFill()
Resuming filling process. But only limited options i.e. scaling remain.
Definition Matrix_def.hpp:195
void addMatrix(SC alpha, const MatrixPtr_Type &B, SC beta)
B = alpha*this + beta*B.
Definition Matrix_def.hpp:306
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:270
void fillComplete()
after inserting global values into matrix. After this step the column map is fixed....
Definition Matrix_def.hpp:200
LO getGlobalMaxNumRowEntries() const
Maximum number of entries in any row of the matrix, over all processes.
Definition Matrix_def.hpp:343
TpetraMatrixConstPtr_Type getTpetraMatrix() const
Return matrix in Xpetra Format of type " ".
Definition Matrix_def.hpp:265
Adaptive Mesh Refinement.
Definition AdaptiveMeshRefinement.cpp:5