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 
54 #if defined(HAVE_MUELU_SERIAL) and defined(HAVE_MUELU_EPETRA)
55 
56 namespace MueLu {
57 
58 int EpetraOperator::ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const {
59  try {
60  // There is no rcpFromRef(const T&), so we need to do const_cast
61  const Xpetra::EpetraMultiVectorT<GO,NO> eX(rcpFromRef(const_cast<Epetra_MultiVector&>(X)));
62  Xpetra::EpetraMultiVectorT<GO,NO> eY(rcpFromRef(Y));
63 
64  // Generally, we assume two different vectors, but AztecOO uses a single vector
65  if (X.Values() == Y.Values()) {
66  // X and Y point to the same memory, use an additional vector
67  RCP<Xpetra::EpetraMultiVectorT<GO,NO> > tmpY = Teuchos::rcp(new Xpetra::EpetraMultiVectorT<GO,NO>(eY.getMap(), eY.getNumVectors()));
68 
69  // InitialGuessIsZero in MueLu::Hierarchy.Iterate() does not zero out components, it
70  // only assumes that user provided an already zeroed out vector
71  bool initialGuessZero = true;
72  tmpY->putScalar(0.0);
73 
74  // apply one V-cycle as preconditioner
75  Hierarchy_->Iterate(eX, *tmpY, 1, initialGuessZero);
76 
77  // deep copy solution from MueLu
78  eY.update(1.0, *tmpY, 0.0);
79 
80  } else {
81  // X and Y point to different memory, pass the vectors through
82 
83  // InitialGuessIsZero in MueLu::Hierarchy.Iterate() does not zero out components, it
84  // only assumes that user provided an already zeroed out vector
85  bool initialGuessZero = true;
86  eY.putScalar(0.0);
87 
88  Hierarchy_->Iterate(eX, eY, 1, initialGuessZero);
89  }
90 
91  } catch (std::exception& e) {
92  //TODO: error msg directly on std::cerr?
93  std::cerr << "Caught an exception in MueLu::EpetraOperator::ApplyInverse():" << std::endl
94  << e.what() << std::endl;
95  return -1;
96  }
97 
98  return 0;
99 }
100 
101 const Epetra_Comm& EpetraOperator::Comm() const {
102  RCP<Matrix> A = Hierarchy_->GetLevel(0)->Get<RCP<Matrix> >("A");
103 
104  //TODO: This code is not pretty
105  RCP<Xpetra::BlockedCrsMatrix<SC, LO, GO, NO> > epbA = Teuchos::rcp_dynamic_cast<Xpetra::BlockedCrsMatrix<SC, LO, GO, NO> >(A);
106  if (epbA != Teuchos::null) {
107  RCP<const Xpetra::EpetraCrsMatrix> tmp_ECrsMtx = rcp_dynamic_cast<Xpetra::EpetraCrsMatrix >(epbA->getMatrix(0,0));
108  if (tmp_ECrsMtx == Teuchos::null)
109  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::EpetraCrsMatrix failed");
110 
111  RCP<Epetra_CrsMatrix> epA = tmp_ECrsMtx->getEpetra_CrsMatrixNonConst();
112  return epA->Comm();
113  }
114 
115  RCP<const Xpetra::CrsMatrixWrap<SC,LO,GO,NO> > crsOp = rcp_dynamic_cast<const Xpetra::CrsMatrixWrap<SC,LO,GO,NO> >(A);
116  if (crsOp == Teuchos::null)
117  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
118  const RCP<const Xpetra::EpetraCrsMatrix> &tmp_ECrsMtx = rcp_dynamic_cast<const Xpetra::EpetraCrsMatrix>(crsOp->getCrsMatrix());
119  if (tmp_ECrsMtx == Teuchos::null)
120  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::EpetraCrsMatrix failed");
121  return tmp_ECrsMtx->getEpetra_CrsMatrixNonConst()->Comm();
122 }
123 
124 const Epetra_Map& EpetraOperator::OperatorDomainMap() const {
125  RCP<Xpetra::Matrix<SC,LO,GO,NO> > A = Hierarchy_->GetLevel(0)->Get<RCP<Matrix> >("A");
126 
127  RCP<Xpetra::BlockedCrsMatrix<SC, LO, GO, NO> > epbA = Teuchos::rcp_dynamic_cast<Xpetra::BlockedCrsMatrix<SC, LO, GO, NO> >(A);
128  if (epbA != Teuchos::null)
129  return Xpetra::toEpetra(epbA->getDomainMap());
130 
131  RCP<const Xpetra::CrsMatrixWrap<SC,LO,GO,NO> > crsOp = rcp_dynamic_cast<const Xpetra::CrsMatrixWrap<SC,LO,GO,NO> >(A);
132  if (crsOp == Teuchos::null)
133  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
134  const RCP<const Xpetra::EpetraCrsMatrix> &tmp_ECrsMtx = rcp_dynamic_cast<const Xpetra::EpetraCrsMatrix>(crsOp->getCrsMatrix());
135  if (tmp_ECrsMtx == Teuchos::null)
136  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::EpetraCrsMatrix failed");
137  return tmp_ECrsMtx->getEpetra_CrsMatrixNonConst()->DomainMap();
138 }
139 
140 const Epetra_Map & EpetraOperator::OperatorRangeMap() const {
141  RCP<Xpetra::Matrix<SC,LO,GO,NO> > A = Hierarchy_->GetLevel(0)->Get<RCP<Matrix> >("A");
142 
143  RCP<Xpetra::BlockedCrsMatrix<SC, LO, GO, NO> > epbA = Teuchos::rcp_dynamic_cast<Xpetra::BlockedCrsMatrix<SC, LO, GO, NO> >(A);
144  if (epbA != Teuchos::null)
145  return Xpetra::toEpetra(epbA->getRangeMap());
146 
147  RCP<const Xpetra::CrsMatrixWrap<SC,LO,GO,NO> > crsOp = rcp_dynamic_cast<const Xpetra::CrsMatrixWrap<SC,LO,GO,NO> >(A);
148  if (crsOp == Teuchos::null)
149  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
150  const RCP<const Xpetra::EpetraCrsMatrix> &tmp_ECrsMtx = rcp_dynamic_cast<const Xpetra::EpetraCrsMatrix>(crsOp->getCrsMatrix());
151  if (tmp_ECrsMtx == Teuchos::null)
152  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::EpetraCrsMatrix failed");
153  return tmp_ECrsMtx->getEpetra_CrsMatrixNonConst()->RangeMap();
154 }
155 
156 } // namespace
157 
158 #endif // #if defined(HAVE_MUELU_SERIAL) and defined(HAVE_MUELU_EPETRA)
Namespace for MueLu classes and methods.