MueLu  Version of the Day
MueLu_EpetraOperator.cpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #include <Xpetra_Matrix.hpp>
47 #include <Xpetra_CrsMatrixWrap.hpp>
48 #include <Xpetra_BlockedCrsMatrix.hpp>
49 #include <Xpetra_EpetraMultiVector.hpp>
50 
51 #include "MueLu_EpetraOperator.hpp"
52 #include "MueLu_Level.hpp"
53 #include "MueLu_Utilities.hpp"
54 
55 namespace MueLu {
56 
57 int EpetraOperator::ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const {
58  try {
59  // There is no rcpFromRef(const T&), so we need to do const_cast
60  const Xpetra::EpetraMultiVector eX(rcpFromRef(const_cast<Epetra_MultiVector&>(X)));
61  Xpetra::EpetraMultiVector eY(rcpFromRef(Y));
62 
63  // Generally, we assume two different vectors, but AztecOO uses a single vector
64  if (X.Values() == Y.Values()) {
65  // X and Y point to the same memory, use an additional vector
66  RCP<Xpetra::EpetraMultiVector> tmpY = Teuchos::rcp(new Xpetra::EpetraMultiVector(eY.getMap(), eY.getNumVectors()));
67 
68  // InitialGuessIsZero in MueLu::Hierarchy.Iterate() does not zero out components, it
69  // only assumes that user provided an already zeroed out vector
70  bool initialGuessZero = true;
71  tmpY->putScalar(0.0);
72 
73  // apply one V-cycle as preconditioner
74  Hierarchy_->Iterate(eX, *tmpY, 1, initialGuessZero);
75 
76  // deep copy solution from MueLu
77  eY.update(1.0, *tmpY, 0.0);
78 
79  } else {
80  // X and Y point to different memory, pass the vectors through
81 
82  // InitialGuessIsZero in MueLu::Hierarchy.Iterate() does not zero out components, it
83  // only assumes that user provided an already zeroed out vector
84  bool initialGuessZero = true;
85  eY.putScalar(0.0);
86 
87  Hierarchy_->Iterate(eX, eY, 1, initialGuessZero);
88  }
89 
90  } catch (std::exception& e) {
91  //TODO: error msg directly on std::cerr?
92  std::cerr << "Caught an exception in MueLu::EpetraOperator::ApplyInverse():" << std::endl
93  << e.what() << std::endl;
94  return -1;
95  }
96 
97  return 0;
98 }
99 
100 const Epetra_Comm& EpetraOperator::Comm() const {
101  RCP<Matrix> A = Hierarchy_->GetLevel(0)->Get<RCP<Matrix> >("A");
102 
103  //TODO: This code is not pretty
104  RCP<Xpetra::BlockedCrsMatrix<double, int, int> > epbA = Teuchos::rcp_dynamic_cast<Xpetra::BlockedCrsMatrix<double, int, int> >(A);
105  if (epbA != Teuchos::null) {
106  RCP<const Xpetra::EpetraCrsMatrix> tmp_ECrsMtx = rcp_dynamic_cast<Xpetra::EpetraCrsMatrix >(epbA->getMatrix(0,0));
107  if (tmp_ECrsMtx == Teuchos::null)
108  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::EpetraCrsMatrix failed");
109 
110  RCP<Epetra_CrsMatrix> epA = tmp_ECrsMtx->getEpetra_CrsMatrixNonConst();
111  return epA->Comm();
112  }
113 
114  RCP<Epetra_CrsMatrix> epA = Utils::Op2NonConstEpetraCrs(A);
115  return epA->Comm();
116 }
117 
118 const Epetra_Map& EpetraOperator::OperatorDomainMap() const {
119  RCP<Matrix> A = Hierarchy_->GetLevel(0)->Get<RCP<Matrix> >("A");
120 
121  RCP<Xpetra::BlockedCrsMatrix<double, int, int> > epbA = Teuchos::rcp_dynamic_cast<Xpetra::BlockedCrsMatrix<double, int, int> >(A);
122  if (epbA != Teuchos::null)
123  return Xpetra::toEpetra(epbA->getDomainMap());
124 
125  RCP<Epetra_CrsMatrix> epA = Utils::Op2NonConstEpetraCrs(A);
126  return epA->DomainMap();
127 }
128 
129 const Epetra_Map & EpetraOperator::OperatorRangeMap() const {
130  RCP<Matrix> A = Hierarchy_->GetLevel(0)->Get<RCP<Matrix> >("A");
131 
132  RCP<Xpetra::BlockedCrsMatrix<double, int, int> > epbA = Teuchos::rcp_dynamic_cast<Xpetra::BlockedCrsMatrix<double, int, int> >(A);
133  if (epbA != Teuchos::null)
134  return Xpetra::toEpetra(epbA->getRangeMap());
135 
136  RCP<Epetra_CrsMatrix> epA = Utils::Op2NonConstEpetraCrs(A);
137  return epA->RangeMap();
138 }
139 
140 } // namespace
Exception indicating invalid cast attempted.
Namespace for MueLu classes and methods.
const Epetra_Map & OperatorRangeMap() const
Returns the Epetra_Map object associated with the range of this operator.
int ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Returns the result of a Epetra_Operator inverse applied to an Epetra_MultiVector X in Y...
const Epetra_Comm & Comm() const
Returns a pointer to the Epetra_Comm communicator associated with this operator.
const Epetra_Map & OperatorDomainMap() const
Returns the Epetra_Map object associated with the domain of this operator.
static RCP< Epetra_CrsMatrix > Op2NonConstEpetraCrs(RCP< Matrix > Op)