46 #ifndef MUELU_UTILITIESBASE_DECL_HPP
47 #define MUELU_UTILITIESBASE_DECL_HPP
57 #include <Teuchos_DefaultComm.hpp>
90 #define MueLu_sumAll(rcpComm, in, out) \
91 Teuchos::reduceAll(*rcpComm, Teuchos::REDUCE_SUM, in, Teuchos::outArg(out))
92 #define MueLu_minAll(rcpComm, in, out) \
93 Teuchos::reduceAll(*rcpComm, Teuchos::REDUCE_MIN, in, Teuchos::outArg(out))
94 #define MueLu_maxAll(rcpComm, in, out) \
95 Teuchos::reduceAll(*rcpComm, Teuchos::REDUCE_MAX, in, Teuchos::outArg(out))
104 template <
class Scalar,
105 class LocalOrdinal = int,
106 class GlobalOrdinal = LocalOrdinal,
108 class UtilitiesBase {
110 #undef MUELU_UTILITIESBASE_SHORT
128 return Teuchos::null;
139 size_t numRows = A.getRowMap()->getNodeNumElements();
143 for (
size_t i = 0; i < numRows; ++i) {
146 for (; j < cols.
size(); ++j) {
147 if (Teuchos::as<size_t>(cols[j]) == i) {
152 if (j == cols.
size()) {
190 size_t numRows = A.getRowMap()->getNodeNumElements();
194 for (
size_t i = 0; i < numRows; ++i) {
197 for (LocalOrdinal j = 0; j < cols.
size(); ++j) {
216 if(bA == Teuchos::null) {
225 for (LocalOrdinal j = 0; j < cols.
size(); ++j) {
236 for (
size_t row = 0; row < bA->Rows(); ++row) {
237 for (
size_t col = 0; col < bA->Cols(); ++col) {
238 if (!bA->getMatrix(row,col).
is_null()) {
241 RCP<Vector> ddtemp = bA->getRangeMapExtractor()->ExtractVector(diag,row,bThyraMode);
243 ddtemp->
update(Teuchos::as<Scalar>(1.0),*dd,Teuchos::as<Scalar>(1.0));
244 bA->getRangeMapExtractor()->InsertVector(ddtemp,row,diag,bThyraMode);
268 if(bv.is_null() ==
false) {
272 for(
size_t r = 0; r < bmap->getNumMaps(); ++r) {
284 for (
size_t i = 0; i < v->
getMap()->getNodeNumElements(); ++i) {
288 retVals[i] = tolReplacement;
320 for (LocalOrdinal i = 0; i < localDiagVals.
size(); i++)
321 localDiagVals[i] = diagVals[i];
322 localDiagVals = diagVals = null;
328 if (importer == Teuchos::null) {
365 RES->
update(one, RHS, negone);
376 Resid.
update(one, RHS, negone);
383 int myPID = comm->getRank();
386 for (
int i = 0; i <comm->getSize(); i++) {
388 gethostname(hostname,
sizeof(hostname));
389 std::cout <<
"Host: " << hostname <<
"\tMPI rank: " << myPID <<
",\tPID: " << pid <<
"\n\tattach " << pid << std::endl;
394 std::cout <<
"** Enter a character to continue > " << std::endl;
396 int r = scanf(
"%c", &go);
424 LocalOrdinal niters = 10,
Magnitude tolerance = 1e-2,
bool verbose =
false,
unsigned int seed = 123) {
426 "Utils::PowerMethod: operator must have domain and range maps that are equivalent.");
440 const Scalar zero = STS::zero(), one = STS::one();
442 Scalar lambda = zero;
443 Magnitude residual = STS::magnitude(zero);
456 q->
update(one/norms[0], *z, zero);
459 z->elementWiseMultiply(one, *diagInvVec, *z, zero);
462 if (
iter % 100 == 0 ||
iter + 1 == niters) {
463 r->update(1.0, *z, -lambda, *q, zero);
465 residual = STS::magnitude(norms[0] / lambda);
467 std::cout <<
"Iter = " <<
iter
468 <<
" Lambda = " << lambda
469 <<
" Residual of A*q - lambda*q = " << residual
473 if (residual < tolerance)
491 const size_t numVectors = v.size();
494 for (
size_t j = 0; j < numVectors; j++) {
495 d += (v[j][i0] - v[j][i1])*(v[j][i0] - v[j][i1]);
516 if (count_twos_as_dirichlet) {
517 for (LocalOrdinal row = 0; row < numRows; row++) {
524 for (col = 0; col < nnz; col++)
525 if ( (indices[col] != row) && STS::magnitude(vals[col]) > tol) {
526 if (!boundaryNodes[row])
528 boundaryNodes[row] =
false;
531 boundaryNodes[row] =
true;
535 for (LocalOrdinal row = 0; row < numRows; row++) {
541 for (
size_t col = 0; col < nnz; col++)
542 if ( (indices[col] != row) && STS::magnitude(vals[col]) > tol) {
543 boundaryNodes[row] =
false;
548 return boundaryNodes;
567 bHasZeroDiagonal =
false;
576 for (LocalOrdinal row = 0; row < numRows; row++) {
581 bool bHasDiag =
false;
582 for (decltype(indices.
size()) col = 0; col < indices.
size(); col++) {
583 if ( indices[col] != row) {
584 if (STS::magnitude(vals[col] / STS::magnitude(sqrt(STS::magnitude(diagVecData[row]) * STS::magnitude(diagVecData[col]))) ) > tol) {
587 }
else bHasDiag =
true;
589 if (bHasDiag ==
false) bHasZeroDiagonal =
true;
590 else if(nnz == 0) boundaryNodes[row] =
true;
592 return boundaryNodes;
613 myColsToZero->putScalar(zero);
615 for(
size_t i=0; i<(size_t) dirichletRows.
size(); i++) {
616 if (dirichletRows[i]) {
620 for(
size_t j=0; j<static_cast<size_t>(indices.
size()); j++)
621 myColsToZero->replaceLocalValue(indices[j],0,one);
626 globalColsToZero->putScalar(zero);
629 globalColsToZero->doExport(*myColsToZero,*exporter,
Xpetra::ADD);
631 myColsToZero->doImport(*globalColsToZero,*exporter,
Xpetra::INSERT);
635 for(
size_t i=0; i<colMap->getNodeNumElements(); i++) {
638 return dirichletCols;
654 const Map& AColMap = *A.getColMap();
655 const Map& BColMap = *B.getColMap();
659 size_t nnzA = 0, nnzB = 0;
676 for (
size_t i = 0; i < numRows; i++) {
683 for (
size_t j = 0; j < nnzB; j++)
684 valBAll[indB[j]] = valB[j];
686 for (
size_t j = 0; j < nnzA; j++) {
689 LocalOrdinal ind = BColMap.
getLocalElement(AColMap.getGlobalElement(indA[j]));
691 f += valBAll[ind] * valA[j];
695 for (
size_t j = 0; j < nnzB; j++)
696 valBAll[indB[j]] = zero;
719 int maxint = INT_MAX;
720 int mySeed = Teuchos::as<int>((maxint-1) * (one -(comm.
getRank()+1)/(comm.
getSize()+one)) );
721 if (mySeed < 1 || mySeed == maxint) {
722 std::ostringstream errStr;
723 errStr <<
"Error detected with random seed = " << mySeed <<
". It should be in the interval [1,2^31-2].";
741 std::vector<LocalOrdinal>& dirichletRows,
bool count_twos_as_dirichlet=
false) {
743 dirichletRows.resize(0);
749 for (
size_t j=0; j<(size_t)indices.
size(); j++) {
754 if (nnz == 1 || (count_twos_as_dirichlet && nnz == 2)) {
755 dirichletRows.push_back(i);
763 const std::vector<LocalOrdinal>& dirichletRows) {
769 for(
size_t i=0; i<dirichletRows.size(); i++) {
776 Scalar* valuesNC =
const_cast<Scalar*
>(values.
getRawPtr());
777 for(
size_t j=0; j<(size_t)indices.
size(); j++) {
795 for(
size_t i=0; i<(size_t) dirichletRows.
size(); i++) {
796 if (dirichletRows[i]){
803 Scalar* valuesNC =
const_cast<Scalar*
>(values.
getRawPtr());
804 for(
size_t j=0; j<(size_t)indices.
size(); j++) {
817 const std::vector<LocalOrdinal>& dirichletRows,
819 for(
size_t i=0; i<dirichletRows.size(); i++) {
824 Scalar* valuesNC =
const_cast<Scalar*
>(values.
getRawPtr());
825 for(
size_t j=0; j<(size_t)indices.
size(); j++)
826 valuesNC[j]=replaceWith;
835 for(
size_t i=0; i<(size_t) dirichletRows.
size(); i++) {
836 if (dirichletRows[i]) {
841 Scalar* valuesNC =
const_cast<Scalar*
>(values.
getRawPtr());
842 for(
size_t j=0; j<(size_t)indices.
size(); j++)
843 valuesNC[j]=replaceWith;
853 for(
size_t i=0; i<(size_t) dirichletRows.
size(); i++) {
854 if (dirichletRows[i]) {
855 for(
size_t j=0; j<X->getNumVectors(); j++)
856 X->replaceLocalValue(i,j,replaceWith);
871 Scalar* valuesNC =
const_cast<Scalar*
>(values.
getRawPtr());
872 for(
size_t j=0; j<static_cast<size_t>(indices.
size()); j++)
873 if (dirichletCols[indices[j]])
874 valuesNC[j] = replaceWith;
885 throw std::runtime_error(
"UtilitiesBase::FindDirichletRowsAndPropagateToCols row and domain maps must match.");
888 bool has_import = !importer.
is_null();
891 std::vector<LocalOrdinal> dirichletRows;
895 printf(
"[%d] DirichletRow Ids = ",A->getRowMap()->getComm()->getRank());
896 for(
size_t i=0; i<(size_t) dirichletRows.size(); i++)
897 printf(
"%d ",dirichletRows[i]);
909 for(
size_t i=0; i<(size_t) dirichletRows.size(); i++) {
910 dr[dirichletRows[i]] = 1;
911 if(!has_import) dc[dirichletRows[i]] = 1;
915 isDirichletCol->doImport(*isDirichletRow,*importer,Xpetra::CombineMode::ADD);
933 const size_t numSubMaps = sourceBlockedMap.
getNumMaps();
935 throw std::runtime_error(
"GenerateBlockedTargetMap(): Map compatibility error");
940 for(
size_t i=0; i<numSubMaps; i++) {
945 block_ids->replaceLocalValue(jj,(
int)i);
952 new_block_ids->doImport(*block_ids,Importer,Xpetra::CombineMode::ADD);
964 std::vector<RCP<const Map> > subMaps(numSubMaps);
965 for(
size_t i=0; i<numSubMaps; i++) {
981 #define MUELU_UTILITIESBASE_SHORT
982 #endif // MUELU_UTILITIESBASE_DECL_HPP
virtual Teuchos::ArrayRCP< Scalar > getDataNonConst(size_t j)=0
Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > Matrix
virtual void apply(const MultiVector &X, MultiVector &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, Scalar alpha=Teuchos::ScalarTraits< Scalar >::one(), Scalar beta=Teuchos::ScalarTraits< Scalar >::zero()) const =0
virtual size_t getNodeNumRows() const =0
static RCP< Import< LocalOrdinal, GlobalOrdinal, Node > > Build(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &source, const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &target)
static RCP< Export< LocalOrdinal, GlobalOrdinal, Node > > Build(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &source, const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &target)
virtual void norm2(const Teuchos::ArrayView< typename Teuchos::ScalarTraits< Scalar >::magnitudeType > &norms) const =0
Xpetra::BlockedMultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > BlockedMultiVector
virtual Teuchos::RCP< const Teuchos::Comm< int > > getComm() const =0
virtual int getSize() const =0
static Teuchos::ArrayRCP< const bool > DetectDirichletCols(const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Teuchos::ArrayRCP< const bool > &dirichletRows)
Detect Dirichlet columns based on Dirichlet rows.
virtual int getRank() const =0
static magnitudeType eps()
static Scalar PowerMethod(const Matrix &A, bool scaleByDiag=true, LocalOrdinal niters=10, Magnitude tolerance=1e-2, bool verbose=false, unsigned int seed=123)
Simple transpose for Tpetra::CrsMatrix types.
void setMultiVector(size_t r, Teuchos::RCP< const Vector > v, bool bThyraMode)
static void Residual(const Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const MultiVector &X, const MultiVector &RHS, MultiVector &Resid)
static Teuchos::RCP< Vector > GetInverse(Teuchos::RCP< const Vector > v, Magnitude tol=Teuchos::ScalarTraits< Scalar >::eps()*100, Scalar tolReplacement=Teuchos::ScalarTraits< Scalar >::zero())
Return vector containing inverse of input vector.
Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > MultiVector
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
static RCP< const Xpetra::BlockedMap< LocalOrdinal, GlobalOrdinal, Node > > GeneratedBlockedTargetMap(const Xpetra::BlockedMap< LocalOrdinal, GlobalOrdinal, Node > &sourceBlockedMap, const Xpetra::Import< LocalOrdinal, GlobalOrdinal, Node > &Importer)
static void ApplyOAZToMatrixRows(Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &A, const std::vector< LocalOrdinal > &dirichletRows)
virtual size_t getNodeNumElements() const =0
static void FindDirichletRows(Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &A, std::vector< LocalOrdinal > &dirichletRows, bool count_twos_as_dirichlet=false)
virtual LocalOrdinal getLocalElement(GlobalOrdinal globalIndex) const =0
virtual GlobalOrdinal getIndexBase() const =0
static Teuchos::RCP< const Comm< OrdinalType > > getComm()
Exception throws to report incompatible objects (like maps).
static Teuchos::ArrayRCP< const bool > DetectDirichletRowsExt(const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, bool &bHasZeroDiagonal, const Magnitude &tol=Teuchos::ScalarTraits< Scalar >::zero())
Detect Dirichlet rows (extended version)
virtual void reciprocal(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A)=0
static void PauseForDebugger()
#define MueLu_sumAll(rcpComm, in, out)
virtual void update(const Scalar &alpha, const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Scalar &beta)=0
virtual void doImport(const DistObject< Scalar, LocalOrdinal, GlobalOrdinal, Node > &source, const Import< LocalOrdinal, GlobalOrdinal, Node > &importer, CombineMode CM)=0
static Teuchos::ScalarTraits< Scalar >::magnitudeType Distance2(const Teuchos::Array< Teuchos::ArrayRCP< const Scalar >> &v, LocalOrdinal i0, LocalOrdinal i1)
Squared distance between two rows in a multivector.
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getTargetMap() const =0
virtual void getLocalDiagCopy(Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &diag) const =0
void getLocalDiagOffsets(Teuchos::ArrayRCP< size_t > &offsets) const
virtual void getLocalRowView(LocalOrdinal LocalRow, ArrayView< const LocalOrdinal > &indices, ArrayView< const Scalar > &values) const =0
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getMap() const
static void FindDirichletRowsAndPropagateToCols(Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &A, Teuchos::RCP< Xpetra::Vector< int, LocalOrdinal, GlobalOrdinal, Node > > &isDirichletRow, Teuchos::RCP< Xpetra::Vector< int, LocalOrdinal, GlobalOrdinal, Node > > &isDirichletCol)
void getLocalDiagCopy(Xpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &diag) const
virtual bool isFillComplete() const =0
size_t getNumMaps() const
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getMap() const =0
Xpetra::BlockedVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > BlockedVector
Xpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > CrsMatrix
static Teuchos::ArrayRCP< Scalar > GetLumpedMatrixDiagonal(const Matrix &A)
Extract Matrix Diagonal of lumped matrix.
virtual GlobalOrdinal getGlobalElement(LocalOrdinal localIndex) const =0
static Scalar Frobenius(const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &B)
Frobenius inner product of two matrices.
Teuchos::ScalarTraits< Scalar >::magnitudeType Magnitude
virtual size_t getNumEntriesInLocalRow(LocalOrdinal localRow) const =0
static void seedrandom(unsigned int s)
Xpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > Vector
static RCP< Vector > Build(const Teuchos::RCP< const Map > &map, bool zeroOut=true)
static void ZeroDirichletRows(Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &A, const Teuchos::ArrayRCP< const bool > &dirichletRows, Scalar replaceWith=Teuchos::ScalarTraits< Scalar >::zero())
virtual Scalar dot(const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &a) const =0
static void ApplyOAZToMatrixRows(Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &A, const Teuchos::ArrayRCP< const bool > &dirichletRows)
static magnitudeType magnitude(T a)
const RCP< const Map > getMap(size_t i, bool bThyraMode=false) const
Xpetra::BlockedMap< LocalOrdinal, GlobalOrdinal, Node > BlockedMap
static void ZeroDirichletCols(Teuchos::RCP< Matrix > &A, const Teuchos::ArrayRCP< const bool > &dirichletCols, Scalar replaceWith=Teuchos::ScalarTraits< Scalar >::zero())
static Teuchos::Array< Magnitude > ResidualNorm(const Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const MultiVector &X, const MultiVector &RHS)
void push_back(const value_type &x)
virtual RCP< const CrsGraph > getCrsGraph() const =0
static RCP< MultiVector > Residual(const Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const MultiVector &X, const MultiVector &RHS)
virtual bool isSameAs(const Map< LocalOrdinal, GlobalOrdinal, Node > &map) const =0
static Teuchos::ArrayRCP< const bool > DetectDirichletRows(const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Magnitude &tol=Teuchos::ScalarTraits< Scalar >::zero(), bool count_twos_as_dirichlet=false)
Detect Dirichlet rows.
static RCP< Matrix > Crs2Op(RCP< CrsMatrix > Op)
Xpetra::CrsMatrixWrap< Scalar, LocalOrdinal, GlobalOrdinal, Node > CrsMatrixWrap
static RCP< Teuchos::FancyOStream > MakeFancy(std::ostream &os)
virtual Teuchos::RCP< const Map > getRangeMap() const =0
static Teuchos::RCP< MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Build(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &map, size_t NumVectors, bool zeroOut=true)
Exception throws to report errors in the internal logical of the program.
Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > Map
static Teuchos::RCP< Map< LocalOrdinal, GlobalOrdinal, Node > > Build(UnderlyingLib lib, global_size_t numGlobalElements, GlobalOrdinal indexBase, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, LocalGlobal lg=Xpetra::GloballyDistributed)
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getSourceMap() const =0
virtual UnderlyingLib lib() const
virtual size_t getNumVectors() const =0
static RCP< Vector > GetMatrixOverlappedDiagonal(const Matrix &A)
Extract Overlapped Matrix Diagonal.
static Teuchos::Array< Magnitude > ResidualNorm(const Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const MultiVector &X, const MultiVector &RHS, MultiVector &Resid)
static Teuchos::RCP< Vector > GetLumpedMatrixDiagonal(Teuchos::RCP< const Matrix > rcpA)
Extract Matrix Diagonal of lumped matrix.
static RCP< Vector > GetMatrixDiagonalInverse(const Matrix &A, Magnitude tol=Teuchos::ScalarTraits< Scalar >::eps()*100)
Extract Matrix Diagonal.
virtual Teuchos::ArrayRCP< const Scalar > getData(size_t j) const =0
static void ZeroDirichletRows(Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &A, const std::vector< LocalOrdinal > &dirichletRows, Scalar replaceWith=Teuchos::ScalarTraits< Scalar >::zero())
static void SetRandomSeed(const Teuchos::Comm< int > &comm)
Set seed for random number generator.
virtual Teuchos::RCP< const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getVector(size_t j) const =0
static void ZeroDirichletRows(Teuchos::RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &X, const Teuchos::ArrayRCP< const bool > &dirichletRows, Scalar replaceWith=Teuchos::ScalarTraits< Scalar >::zero())
virtual Teuchos::RCP< const Map > getDomainMap() const =0
static Teuchos::ArrayRCP< Scalar > GetMatrixDiagonal(const Matrix &A)
Extract Matrix Diagonal.