MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_Utilities_def.hpp
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 #ifndef MUELU_UTILITIES_DEF_HPP
47 #define MUELU_UTILITIES_DEF_HPP
48 
49 #include <Teuchos_DefaultComm.hpp>
51 
52 #include "MueLu_ConfigDefs.hpp"
53 
54 #ifdef HAVE_MUELU_EPETRA
55 # ifdef HAVE_MPI
56 # include "Epetra_MpiComm.h"
57 # endif
58 #endif
59 
60 #if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_EPETRAEXT)
61 #include <EpetraExt_MatrixMatrix.h>
62 #include <EpetraExt_RowMatrixOut.h>
64 #include <EpetraExt_CrsMatrixIn.h>
66 #include <EpetraExt_BlockMapIn.h>
67 #include <Xpetra_EpetraUtils.hpp>
68 #include <Xpetra_EpetraMultiVector.hpp>
69 #include <EpetraExt_BlockMapOut.h>
70 #endif
71 
72 #ifdef HAVE_MUELU_TPETRA
73 #include <MatrixMarket_Tpetra.hpp>
74 #include <Tpetra_RowMatrixTransposer.hpp>
75 #include <TpetraExt_MatrixMatrix.hpp>
76 #include <Xpetra_TpetraMultiVector.hpp>
77 #include <Xpetra_TpetraCrsMatrix.hpp>
78 #include <Xpetra_TpetraBlockCrsMatrix.hpp>
79 #endif
80 
81 #ifdef HAVE_MUELU_EPETRA
82 #include <Xpetra_EpetraMap.hpp>
83 #endif
84 
85 #include <Xpetra_BlockedCrsMatrix.hpp>
86 //#include <Xpetra_DefaultPlatform.hpp>
87 #include <Xpetra_IO.hpp>
88 #include <Xpetra_Import.hpp>
89 #include <Xpetra_ImportFactory.hpp>
90 #include <Xpetra_Map.hpp>
91 #include <Xpetra_MapFactory.hpp>
92 #include <Xpetra_Matrix.hpp>
93 #include <Xpetra_MatrixFactory.hpp>
94 #include <Xpetra_MultiVector.hpp>
95 #include <Xpetra_MultiVectorFactory.hpp>
96 #include <Xpetra_Operator.hpp>
97 #include <Xpetra_Vector.hpp>
98 #include <Xpetra_VectorFactory.hpp>
99 
100 #include <Xpetra_MatrixMatrix.hpp>
101 
102 #include <MueLu_Utilities_decl.hpp>
103 #if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_ML)
104 #include <ml_operator.h>
105 #include <ml_epetra_utils.h>
106 #endif
107 
108 namespace MueLu {
109 
110 #ifdef HAVE_MUELU_EPETRA
111  //using Xpetra::EpetraCrsMatrix; // TODO: mv in Xpetra_UseShortNamesScalar
112  //using Xpetra::EpetraMultiVector;
113 #endif
114 
115 #ifdef HAVE_MUELU_EPETRA
116  template<typename SC,typename LO,typename GO,typename NO>
118  return Xpetra::Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<SC,LO,GO,NO>(epAB);
119  }
120 #endif
121 
122 #ifdef HAVE_MUELU_EPETRA
123  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
124  RCP<const Epetra_MultiVector> Utilities<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MV2EpetraMV(const RCP<Xpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > vec) {
125  RCP<const Xpetra::EpetraMultiVectorT<GlobalOrdinal,Node> > tmpVec = rcp_dynamic_cast<Xpetra::EpetraMultiVectorT<GlobalOrdinal,Node> >(vec);
126  if (tmpVec == Teuchos::null)
127  throw Exceptions::BadCast("Cast from Xpetra::MultiVector to Xpetra::EpetraMultiVector failed");
128  return tmpVec->getEpetra_MultiVector();
129  }
130 
131  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
132  RCP<Epetra_MultiVector> Utilities<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MV2NonConstEpetraMV(RCP<Xpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > vec) {
133  RCP<const Xpetra::EpetraMultiVectorT<GlobalOrdinal,Node> > tmpVec = rcp_dynamic_cast<Xpetra::EpetraMultiVectorT<GlobalOrdinal,Node> >(vec);
134  if (tmpVec == Teuchos::null)
135  throw Exceptions::BadCast("Cast from Xpetra::MultiVector to Xpetra::EpetraMultiVector failed");
136  return tmpVec->getEpetra_MultiVector();
137  }
138 
139  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
140  Epetra_MultiVector& Utilities<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MV2NonConstEpetraMV(Xpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &vec) {
141  const Xpetra::EpetraMultiVectorT<GlobalOrdinal,Node> & tmpVec = dynamic_cast<const Xpetra::EpetraMultiVectorT<GlobalOrdinal,Node> &>(vec);
142  return *(tmpVec.getEpetra_MultiVector());
143  }
144 
145  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
146  const Epetra_MultiVector& Utilities<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MV2EpetraMV(const Xpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& vec) {
147  const Xpetra::EpetraMultiVectorT<GlobalOrdinal,Node> & tmpVec = dynamic_cast<const Xpetra::EpetraMultiVectorT<GlobalOrdinal,Node> &>(vec);
148  return *(tmpVec.getEpetra_MultiVector());
149  }
150 
151  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
152  RCP<const Epetra_CrsMatrix> Utilities<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Op2EpetraCrs(RCP<const Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > Op) {
153  RCP<const Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node> > crsOp = rcp_dynamic_cast<const Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node> >(Op);
154  if (crsOp == Teuchos::null)
155  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
156  const RCP<const Xpetra::EpetraCrsMatrixT<GlobalOrdinal,Node>>& tmp_ECrsMtx = rcp_dynamic_cast<const Xpetra::EpetraCrsMatrixT<GlobalOrdinal,Node> >(crsOp->getCrsMatrix());
157  if (tmp_ECrsMtx == Teuchos::null)
158  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::EpetraCrsMatrix failed");
159  return tmp_ECrsMtx->getEpetra_CrsMatrix();
160  }
161 
162  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
163  RCP<Epetra_CrsMatrix> Utilities<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Op2NonConstEpetraCrs(RCP<Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > Op) {
164  RCP<const Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node> > crsOp = rcp_dynamic_cast<const Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node> >(Op);
165  if (crsOp == Teuchos::null)
166  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
167  const RCP<const Xpetra::EpetraCrsMatrixT<GlobalOrdinal,Node> > &tmp_ECrsMtx = rcp_dynamic_cast<const Xpetra::EpetraCrsMatrixT<GlobalOrdinal,Node> >(crsOp->getCrsMatrix());
168  if (tmp_ECrsMtx == Teuchos::null)
169  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::EpetraCrsMatrix failed");
170  return tmp_ECrsMtx->getEpetra_CrsMatrixNonConst();
171  }
172 
173  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
174  const Epetra_CrsMatrix& Utilities<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Op2EpetraCrs(const Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Op) {
175  try {
176  const Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node>& crsOp = dynamic_cast<const Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node>&>(Op);
177  try {
178  const Xpetra::EpetraCrsMatrixT<GlobalOrdinal,Node>& tmp_ECrsMtx = dynamic_cast<const Xpetra::EpetraCrsMatrixT<GlobalOrdinal,Node>&>(*crsOp.getCrsMatrix());
179  return *tmp_ECrsMtx.getEpetra_CrsMatrix();
180  } catch (std::bad_cast&) {
181  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::EpetraCrsMatrix failed");
182  }
183  } catch (std::bad_cast&) {
184  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
185  }
186  }
187 
188  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
189  Epetra_CrsMatrix& Utilities<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Op2NonConstEpetraCrs(Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Op) {
190  try {
191  Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node>& crsOp = dynamic_cast<Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node>&>(Op);
192  try {
193  Xpetra::EpetraCrsMatrixT<GlobalOrdinal,Node>& tmp_ECrsMtx = dynamic_cast<Xpetra::EpetraCrsMatrixT<GlobalOrdinal,Node>&>(*crsOp.getCrsMatrix());
194  return *tmp_ECrsMtx.getEpetra_CrsMatrixNonConst();
195  } catch (std::bad_cast&) {
196  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::EpetraCrsMatrix failed");
197  }
198  } catch (std::bad_cast&) {
199  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
200  }
201  }
202 
203  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
204  const Epetra_Map& Utilities<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Map2EpetraMap(const Xpetra::Map<LocalOrdinal,GlobalOrdinal,Node>& map) {
205  RCP<const Xpetra::EpetraMapT<GlobalOrdinal,Node> > xeMap = rcp_dynamic_cast<const Xpetra::EpetraMapT<GlobalOrdinal,Node> >(rcpFromRef(map));
206  if (xeMap == Teuchos::null)
207  throw Exceptions::BadCast("Utilities::Map2EpetraMap : Cast from Xpetra::Map to Xpetra::EpetraMap failed");
208  return xeMap->getEpetra_Map();
209  }
210 #endif
211 
212 #ifdef HAVE_MUELU_TPETRA
213  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
215  Utilities<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MV2TpetraMV(RCP<Xpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > const vec) {
216  RCP<const Xpetra::TpetraMultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > tmpVec = rcp_dynamic_cast<Xpetra::TpetraMultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> >(vec);
217  if (tmpVec == Teuchos::null)
218  throw Exceptions::BadCast("Cast from Xpetra::MultiVector to Xpetra::TpetraMultiVector failed");
219  return tmpVec->getTpetra_MultiVector();
220  }
221 
222  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
224  RCP<const Xpetra::TpetraMultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > tmpVec = rcp_dynamic_cast<Xpetra::TpetraMultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> >(vec);
225  if (tmpVec == Teuchos::null)
226  throw Exceptions::BadCast("Cast from Xpetra::MultiVector to Xpetra::TpetraMultiVector failed");
227  return tmpVec->getTpetra_MultiVector();
228  }
229 
230  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
231  Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> & Utilities<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MV2NonConstTpetraMV(Xpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& vec) {
232  const Xpetra::TpetraMultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& tmpVec = dynamic_cast<const Xpetra::TpetraMultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>&>(vec);
233  return *(tmpVec.getTpetra_MultiVector());
234  }
235 
236  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
238  const Xpetra::TpetraMultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& tmpVec = dynamic_cast<const Xpetra::TpetraMultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>&>(vec);
239  return tmpVec.getTpetra_MultiVector();
240  }
241 
242  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
243  const Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>&
244  Utilities<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MV2TpetraMV(const Xpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& vec) {
245  const Xpetra::TpetraMultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& tmpVec = dynamic_cast<const Xpetra::TpetraMultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>&>(vec);
246  return *(tmpVec.getTpetra_MultiVector());
247  }
248 
249  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
251  // Get the underlying Tpetra Mtx
252  RCP<const Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node> > crsOp = rcp_dynamic_cast<const Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node> >(Op);
253  if (crsOp == Teuchos::null)
254  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
255  const RCP<const Xpetra::TpetraCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > &tmp_ECrsMtx = rcp_dynamic_cast<const Xpetra::TpetraCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> >(crsOp->getCrsMatrix());
256  if (tmp_ECrsMtx == Teuchos::null)
257  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::TpetraCrsMatrix failed");
258  return tmp_ECrsMtx->getTpetra_CrsMatrix();
259  }
260 
261  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
263  RCP<const Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node> > crsOp = rcp_dynamic_cast<const Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node> >(Op);
264  if (crsOp == Teuchos::null)
265  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
266  const RCP<const Xpetra::TpetraCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > &tmp_ECrsMtx = rcp_dynamic_cast<const Xpetra::TpetraCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> >(crsOp->getCrsMatrix());
267  if (tmp_ECrsMtx == Teuchos::null)
268  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::TpetraCrsMatrix failed");
269  return tmp_ECrsMtx->getTpetra_CrsMatrixNonConst();
270  }
271 
272  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
273  const Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Utilities<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Op2TpetraCrs(const Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Op) {
274  try {
275  const Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node>& crsOp = dynamic_cast<const Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node>&>(Op);
276  try {
277  const Xpetra::TpetraCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>& tmp_ECrsMtx = dynamic_cast<const Xpetra::TpetraCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>&>(*crsOp.getCrsMatrix());
278  return *tmp_ECrsMtx.getTpetra_CrsMatrix();
279  } catch (std::bad_cast&) {
280  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::TpetraCrsMatrix failed");
281  }
282  } catch (std::bad_cast&) {
283  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
284  }
285  }
286 
287  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
288  Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Utilities<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Op2NonConstTpetraCrs(Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Op) {
289  try {
290  Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node>& crsOp = dynamic_cast<Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node>&>(Op);
291  try {
292  Xpetra::TpetraCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>& tmp_ECrsMtx = dynamic_cast<Xpetra::TpetraCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>&>(*crsOp.getCrsMatrix());
293  return *tmp_ECrsMtx.getTpetra_CrsMatrixNonConst();
294  } catch (std::bad_cast&) {
295  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::TpetraCrsMatrix failed");
296  }
297  } catch (std::bad_cast&) {
298  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
299  }
300  }
301 
302  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
304  RCP<const Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node> > crsOp = rcp_dynamic_cast<const Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node> >(Op);
305  if (crsOp == Teuchos::null)
306  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
307 
309  const RCP<const Xpetra::TpetraCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > tmp_Crs = rcp_dynamic_cast<const Xpetra::TpetraCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> >(crsMat);
311  if(!tmp_Crs.is_null()) {
312  return tmp_Crs->getTpetra_CrsMatrixNonConst();
313  }
314  else {
315  tmp_BlockCrs= rcp_dynamic_cast<const Xpetra::TpetraBlockCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> >(crsMat);
316  if (tmp_BlockCrs.is_null())
317  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::TpetraCrsMatrix and Xpetra::TpetraBlockCrsMatrix failed");
318  return tmp_BlockCrs->getTpetra_BlockCrsMatrixNonConst();
319  }
320  }
321 
322  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
324  RCP<const Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node> > crsOp = rcp_dynamic_cast<const Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node> >(Op);
325  if (crsOp == Teuchos::null)
326  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
327 
329  const RCP<const Xpetra::TpetraCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > tmp_Crs = rcp_dynamic_cast<const Xpetra::TpetraCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> >(crsMat);
331  if(!tmp_Crs.is_null()) {
332  return tmp_Crs->getTpetra_CrsMatrixNonConst();
333  }
334  else {
335  tmp_BlockCrs= rcp_dynamic_cast<const Xpetra::TpetraBlockCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> >(crsMat);
336  if (tmp_BlockCrs.is_null())
337  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::TpetraCrsMatrix and Xpetra::TpetraBlockCrsMatrix failed");
338  return tmp_BlockCrs->getTpetra_BlockCrsMatrixNonConst();
339  }
340  }
341 
342 
343  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
345  const RCP<const Xpetra::TpetraMap<LocalOrdinal,GlobalOrdinal,Node>>& tmp_TMap = rcp_dynamic_cast<const Xpetra::TpetraMap<LocalOrdinal,GlobalOrdinal,Node> >(rcpFromRef(map));
346  if (tmp_TMap == Teuchos::null)
347  throw Exceptions::BadCast("Utilities::Map2TpetraMap : Cast from Xpetra::Map to Xpetra::TpetraMap failed");
348  return tmp_TMap->getTpetra_Map();
349  }
350 #endif
351 
352  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
353  void Utilities<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MyOldScaleMatrix(Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Op, const Teuchos::ArrayRCP<const Scalar>& scalingVector, bool doInverse,
354  bool doFillComplete,
355  bool doOptimizeStorage)
356  {
358  Teuchos::ArrayRCP<Scalar> sv(scalingVector.size());
359  if (doInverse) {
360  for (int i = 0; i < scalingVector.size(); ++i)
361  sv[i] = one / scalingVector[i];
362  } else {
363  for (int i = 0; i < scalingVector.size(); ++i)
364  sv[i] = scalingVector[i];
365  }
366 
367  switch (Op.getRowMap()->lib()) {
368  case Xpetra::UseTpetra:
369  MyOldScaleMatrix_Tpetra(Op, sv, doFillComplete, doOptimizeStorage);
370  break;
371 
372  case Xpetra::UseEpetra:
373  MyOldScaleMatrix_Epetra(Op, sv, doFillComplete, doOptimizeStorage);
374  break;
375 
376  default:
377  throw Exceptions::RuntimeError("Only Epetra and Tpetra matrices can be scaled.");
378  }
379  }
380 
381  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
382  void Utilities<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MyOldScaleMatrix_Epetra(Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>& /* Op */, const Teuchos::ArrayRCP<Scalar>& /* scalingVector */, bool /* doFillComplete */, bool /* doOptimizeStorage */) {
383  throw Exceptions::RuntimeError("MyOldScaleMatrix_Epetra: Epetra needs SC=double and LO=GO=int.");
384  }
385 
386  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
387  void Utilities<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MyOldScaleMatrix_Tpetra(Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Op, const Teuchos::ArrayRCP<Scalar>& scalingVector,
388  bool doFillComplete,
389  bool doOptimizeStorage)
390  {
391 #ifdef HAVE_MUELU_TPETRA
392  try {
393  Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& tpOp = Op2NonConstTpetraCrs(Op);
394 
395  const RCP<const Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > rowMap = tpOp.getRowMap();
396  const RCP<const Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > domainMap = tpOp.getDomainMap();
397  const RCP<const Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > rangeMap = tpOp.getRangeMap();
398 
399  size_t maxRowSize = tpOp.getNodeMaxNumRowEntries();
400  if (maxRowSize == Teuchos::as<size_t>(-1)) // hasn't been determined yet
401  maxRowSize = 20;
402 
403  std::vector<Scalar> scaledVals(maxRowSize);
404  if (tpOp.isFillComplete())
405  tpOp.resumeFill();
406 
407  if (Op.isLocallyIndexed() == true) {
410 
411  for (size_t i = 0; i < rowMap->getNodeNumElements(); ++i) {
412  tpOp.getLocalRowView(i, cols, vals);
413  size_t nnz = tpOp.getNumEntriesInLocalRow(i);
414  if (nnz > maxRowSize) {
415  maxRowSize = nnz;
416  scaledVals.resize(maxRowSize);
417  }
418  for (size_t j = 0; j < nnz; ++j)
419  scaledVals[j] = vals[j]*scalingVector[i];
420 
421  if (nnz > 0) {
422  Teuchos::ArrayView<const Scalar> valview(&scaledVals[0], nnz);
423  tpOp.replaceLocalValues(i, cols, valview);
424  }
425  } //for (size_t i=0; ...
426 
427  } else {
430 
431  for (size_t i = 0; i < rowMap->getNodeNumElements(); ++i) {
432  GlobalOrdinal gid = rowMap->getGlobalElement(i);
433  tpOp.getGlobalRowView(gid, cols, vals);
434  size_t nnz = tpOp.getNumEntriesInGlobalRow(gid);
435  if (nnz > maxRowSize) {
436  maxRowSize = nnz;
437  scaledVals.resize(maxRowSize);
438  }
439  // FIXME FIXME FIXME FIXME FIXME FIXME
440  for (size_t j = 0; j < nnz; ++j)
441  scaledVals[j] = vals[j]*scalingVector[i]; //FIXME i or gid?
442 
443  if (nnz > 0) {
444  Teuchos::ArrayView<const Scalar> valview(&scaledVals[0], nnz);
445  tpOp.replaceGlobalValues(gid, cols, valview);
446  }
447  } //for (size_t i=0; ...
448  }
449 
450  if (doFillComplete) {
451  if (domainMap == Teuchos::null || rangeMap == Teuchos::null)
452  throw Exceptions::RuntimeError("In Utilities::Scaling: cannot fillComplete because the domain and/or range map hasn't been defined");
453 
455  params->set("Optimize Storage", doOptimizeStorage);
456  params->set("No Nonlocal Changes", true);
457  Op.fillComplete(Op.getDomainMap(), Op.getRangeMap(), params);
458  }
459  } catch(...) {
460  throw Exceptions::RuntimeError("Only Tpetra::CrsMatrix types can be scaled (Err.1)");
461  }
462 #else
463  throw Exceptions::RuntimeError("Matrix scaling is not possible because Tpetra has not been enabled.");
464 #endif
465  } //MyOldScaleMatrix_Tpetra()
466 
467  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
470  Transpose (Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Op, bool /* optimizeTranspose */,const std::string & label,const Teuchos::RCP<Teuchos::ParameterList> &params) {
471 #if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_EPETRAEXT)
472  std::string TorE = "epetra";
473 #else
474  std::string TorE = "tpetra";
475 #endif
476 
477 #if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_EPETRAEXT)
478  try {
480  (void) epetraOp; // silence "unused variable" compiler warning
481  } catch (...) {
482  TorE = "tpetra";
483  }
484 #endif
485 
486 #ifdef HAVE_MUELU_TPETRA
487  if (TorE == "tpetra") {
488  try {
489  const Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& tpetraOp = Utilities<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Op2TpetraCrs(Op);
490 
492  Tpetra::RowMatrixTransposer<Scalar, LocalOrdinal, GlobalOrdinal, Node> transposer(rcpFromRef(tpetraOp),label); //more than meets the eye
493 
494  {
496  using Teuchos::rcp;
497  RCP<ParameterList> transposeParams = params.is_null () ?
498  rcp (new ParameterList) :
499  rcp (new ParameterList (*params));
500  transposeParams->set ("sort", false);
501  A = transposer.createTranspose (transposeParams);
502  }
503 
504  RCP<Xpetra::TpetraCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > AA = rcp(new Xpetra::TpetraCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>(A) );
505  RCP<Xpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > AAA = rcp_implicit_cast<Xpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >(AA);
506  RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > AAAA = rcp( new Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(AAA) );
507  if (!AAAA->isFillComplete())
508  AAAA->fillComplete(Op.getRangeMap(), Op.getDomainMap());
509 
510  if (Op.IsView("stridedMaps"))
511  AAAA->CreateView("stridedMaps", Teuchos::rcpFromRef(Op), true/*doTranspose*/);
512 
513  return AAAA;
514 
515  } catch (std::exception& e) {
516  std::cout << "threw exception '" << e.what() << "'" << std::endl;
517  throw Exceptions::RuntimeError("Utilities::Transpose failed, perhaps because matrix is not a Crs matrix");
518  }
519  } //if
520 #endif
521 
522  // Epetra case
523  std::cout << "Utilities::Transpose() not implemented for Epetra" << std::endl;
524  return Teuchos::null;
525 
526  } // Transpose
527 
528 
529  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
534 #if defined(HAVE_XPETRA_TPETRA) && (defined(HAVE_TPETRA_INST_COMPLEX_DOUBLE) || defined(HAVE_TPETRA_INST_COMPLEX_FLOAT))
535  typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType real_type;
536  // Need to cast the real-valued multivector to Scalar=complex
537  if ((typeid(Scalar).name() == typeid(std::complex<double>).name()) ||
538  (typeid(Scalar).name() == typeid(std::complex<float>).name())) {
539  Xscalar = Xpetra::MultiVectorFactory<Scalar,LocalOrdinal,GlobalOrdinal,Node>::Build(X->getMap(),X->getNumVectors());
540  size_t numVecs = X->getNumVectors();
541  for (size_t j=0;j<numVecs;j++) {
542  Teuchos::ArrayRCP<const real_type> XVec = X->getData(j);
543  Teuchos::ArrayRCP<Scalar> XVecScalar = Xscalar->getDataNonConst(j);
544  for(size_t i = 0; i < static_cast<size_t>(XVec.size()); ++i)
545  XVecScalar[i]=XVec[i];
546  }
547  } else
548 #endif
549  Xscalar = rcp_dynamic_cast<Xpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> >(X);
550  return Xscalar;
551  }
552 
553 
554  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
558 
560 
561  // check whether coordinates are contained in parameter list
562  if(paramList.isParameter ("Coordinates") == false)
563  return coordinates;
564 
565 #if defined(HAVE_MUELU_TPETRA)
566  // only Tpetra code
567 
568  // define Tpetra::MultiVector type with Scalar=float only if
569  // * ETI is turned off, since then the compiler will instantiate it automatically OR
570  // * Tpetra is instantiated on Scalar=float
571 #if !defined(HAVE_TPETRA_EXPLICIT_INSTANTIATION) || defined(HAVE_TPETRA_INST_FLOAT)
572  typedef Tpetra::MultiVector<float, LocalOrdinal, GlobalOrdinal, Node> tfMV;
573  RCP<tfMV> floatCoords = Teuchos::null;
574 #endif
575 
576  // define Tpetra::MultiVector type with Scalar=double only if
577  // * ETI is turned off, since then the compiler will instantiate it automatically OR
578  // * Tpetra is instantiated on Scalar=double
579 #if !defined(HAVE_TPETRA_EXPLICIT_INSTANTIATION) || defined(HAVE_TPETRA_INST_DOUBLE)
580  typedef Tpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LocalOrdinal, GlobalOrdinal, Node> tdMV;
581  RCP<tdMV> doubleCoords = Teuchos::null;
582  if (paramList.isType<RCP<tdMV> >("Coordinates")) {
583  // Coordinates are stored as a double vector
584  doubleCoords = paramList.get<RCP<tdMV> >("Coordinates");
585  paramList.remove("Coordinates");
586  }
587 #if !defined(HAVE_TPETRA_EXPLICIT_INSTANTIATION) || defined(HAVE_TPETRA_INST_FLOAT)
588  else if (paramList.isType<RCP<tfMV> >("Coordinates")) {
589  // check if coordinates are stored as a float vector
590  floatCoords = paramList.get<RCP<tfMV> >("Coordinates");
591  paramList.remove("Coordinates");
592  doubleCoords = rcp(new tdMV(floatCoords->getMap(), floatCoords->getNumVectors()));
593  deep_copy(*doubleCoords, *floatCoords);
594  }
595 #endif
596  // We have the coordinates in a Tpetra double vector
597  if(doubleCoords != Teuchos::null) {
598  //rcp(new Xpetra::TpetraMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>(Vtpetra));
599  coordinates = Teuchos::rcp_dynamic_cast<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType,LocalOrdinal,GlobalOrdinal,Node> >(MueLu::TpetraMultiVector_To_XpetraMultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType,LocalOrdinal,GlobalOrdinal,Node>(doubleCoords));
601  TEUCHOS_TEST_FOR_EXCEPT(doubleCoords->getNumVectors() != coordinates->getNumVectors());
602  }
603 #else
604  // coordinates usually are stored as double vector
605  // Tpetra is not instantiated on scalar=double
606  throw Exceptions::RuntimeError("ExtractCoordinatesFromParameterList: The coordinates vector in parameter list is expected to be a Tpetra multivector with SC=double or float.");
607 #endif
608 #endif // endif HAVE_TPETRA
609 
610  // check for Xpetra coordinates vector
611  if(paramList.isType<decltype(coordinates)>("Coordinates")) {
612  coordinates = paramList.get<decltype(coordinates)>("Coordinates");
613  }
614 
615  return coordinates;
616  } // ExtractCoordinatesFromParameterList
617 
618 } //namespace MueLu
619 
620 #define MUELU_UTILITIES_SHORT
621 #endif // MUELU_UTILITIES_DEF_HPP
622 
623 // LocalWords: LocalOrdinal
Exception indicating invalid cast attempted.
static const RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > Map2TpetraMap(const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &map)
MueLu::DefaultLocalOrdinal LocalOrdinal
static RCP< const Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > MV2TpetraMV(RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > const vec)
Helper utility to pull out the underlying Tpetra objects from an Xpetra object.
static void MyOldScaleMatrix(Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const Teuchos::ArrayRCP< const Scalar > &scalingVector, bool doInverse=true, bool doFillComplete=true, bool doOptimizeStorage=true)
static RCP< const Tpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op2TpetraCrs(RCP< const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op)
static const Epetra_Map & Map2EpetraMap(const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &map)
static RCP< Epetra_CrsMatrix > Op2NonConstEpetraCrs(RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op)
T & get(const std::string &name, T def_value)
static RCP< Xpetra::MultiVector< typename Teuchos::ScalarTraits< Scalar >::magnitudeType, LocalOrdinal, GlobalOrdinal, Node > > ExtractCoordinatesFromParameterList(ParameterList &paramList)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
bool is_null(const std::shared_ptr< T > &p)
static RCP< const Epetra_MultiVector > MV2EpetraMV(RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > const vec)
Helper utility to pull out the underlying Epetra objects from an Xpetra object.
size_type size() const
MueLu::DefaultNode Node
static RCP< Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > MV2NonConstTpetraMV(RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > vec)
bool isParameter(const std::string &name) const
bool remove(std::string const &name, bool throwIfNotExists=true)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MueLu::DefaultScalar Scalar
MueLu::DefaultGlobalOrdinal GlobalOrdinal
static RCP< Epetra_MultiVector > MV2NonConstEpetraMV(RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > vec)
static RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > RealValuedToScalarMultiVector(RCP< Xpetra::MultiVector< typename Teuchos::ScalarTraits< Scalar >::magnitudeType, LocalOrdinal, GlobalOrdinal, Node > > X)
static RCP< Tpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op2NonConstTpetraCrs(RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op)
static RCP< Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > MV2NonConstTpetraMV2(Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &vec)
static RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Transpose(Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, bool optimizeTranspose=false, const std::string &label=std::string(), const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
static RCP< const Tpetra::RowMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op2TpetraRow(RCP< const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op)
RCP< Xpetra::CrsMatrixWrap< SC, LO, GO, NO > > Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap(RCP< Epetra_CrsMatrix > &epAB)
static void MyOldScaleMatrix_Epetra(Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const Teuchos::ArrayRCP< Scalar > &scalingVector, bool doFillComplete, bool doOptimizeStorage)
static RCP< const Epetra_CrsMatrix > Op2EpetraCrs(RCP< const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op)
bool isType(const std::string &name) const
Exception throws to report errors in the internal logical of the program.
static RCP< Tpetra::RowMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op2NonConstTpetraRow(RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op)
static void MyOldScaleMatrix_Tpetra(Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const Teuchos::ArrayRCP< Scalar > &scalingVector, bool doFillComplete, bool doOptimizeStorage)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
void deep_copy(const DynRankView< DT, DP...> &dst, typename ViewTraits< DT, DP...>::const_value_type &value, typename std::enable_if< std::is_same< typename ViewTraits< DT, DP...>::specialize, void >::value >::type *=nullptr)
bool is_null() const