Xpetra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Xpetra_MatrixFactory2_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Xpetra: A linear algebra interface package
4 //
5 // Copyright 2012 NTESS and the Xpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 // WARNING: This code is experimental. Backwards compatibility should not be expected.
11 
12 #ifndef XPETRA_MATRIXFACTORY2_DEF_HPP
13 #define XPETRA_MATRIXFACTORY2_DEF_HPP
14 
16 #include "Xpetra_BlockedCrsMatrix.hpp"
17 
18 namespace Xpetra {
19 
20 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
21 RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> MatrixFactory2<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildCopy(const RCP<const Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> A, bool setFixedBlockSize) {
22  RCP<const CrsMatrixWrap> oldOp = Teuchos::rcp_dynamic_cast<const CrsMatrixWrap>(A);
23  if (oldOp == Teuchos::null)
24  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
25 
26  RCP<const CrsMatrix> oldCrsOp = oldOp->getCrsMatrix();
27 
28  UnderlyingLib lib = A->getRowMap()->lib();
29 
30  TEUCHOS_TEST_FOR_EXCEPTION(lib != UseEpetra && lib != UseTpetra, Exceptions::RuntimeError,
31  "Not Epetra or Tpetra matrix");
32 
33 #ifdef HAVE_XPETRA_EPETRA
34  if (lib == UseEpetra) {
35  // NOTE: The proper Epetra conversion in Xpetra_MatrixFactory.cpp
36  throw Exceptions::RuntimeError("Xpetra::BuildCopy(): matrix templates are incompatible with Epetra");
37  }
38 #endif
39 
40 #ifdef HAVE_XPETRA_TPETRA
41  if (lib == UseTpetra) {
42  // Underlying matrix is Tpetra
43  RCP<const TpetraCrsMatrix> oldTCrsOp = Teuchos::rcp_dynamic_cast<const TpetraCrsMatrix>(oldCrsOp);
44 
45  if (oldTCrsOp != Teuchos::null) {
46  RCP<TpetraCrsMatrix> newTCrsOp(new TpetraCrsMatrix(*oldTCrsOp));
47  RCP<CrsMatrixWrap> newOp(new CrsMatrixWrap(Teuchos::as<RCP<CrsMatrix>>(newTCrsOp)));
48  if (setFixedBlockSize)
49  newOp->SetFixedBlockSize(A->GetFixedBlockSize());
50 
51  return newOp;
52  } else {
53  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::TpetraCrsMatrix failed");
54  }
55  }
56 #endif
57 
58  return Teuchos::null;
59 }
60 
61 } // namespace Xpetra
62 
63 #endif
Exception throws to report errors in the internal logical of the program.
Exception indicating invalid cast attempted.
Concrete implementation of Xpetra::Matrix.
static RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > BuildCopy(const RCP< const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > A, bool setFixedBlockSize=true)
Xpetra-specific matrix class.