00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #ifndef XPETRA_MATRIXFACTORY_HPP
00050 #define XPETRA_MATRIXFACTORY_HPP
00051
00052 #include "Xpetra_ConfigDefs.hpp"
00053 #include "Xpetra_Matrix.hpp"
00054 #include "Xpetra_CrsMatrixWrap.hpp"
00055 #include "Xpetra_Map.hpp"
00056 #include "Xpetra_Vector.hpp"
00057 #include "Xpetra_Exceptions.hpp"
00058
00059 namespace Xpetra {
00060 template <class Scalar = Matrix<>::scalar_type,
00061 class LocalOrdinal = typename Matrix<Scalar>::local_ordinal_type,
00062 class GlobalOrdinal =
00063 typename Matrix<Scalar, LocalOrdinal>::global_ordinal_type,
00064 class Node =
00065 typename Matrix<Scalar, LocalOrdinal, GlobalOrdinal>::node_type>
00066 class MatrixFactory {
00067 #undef XPETRA_MATRIXFACTORY_SHORT
00068 #include "Xpetra_UseShortNames.hpp"
00069
00070 private:
00072 MatrixFactory() {}
00073
00074 public:
00075
00077 static RCP<Matrix> Build(const RCP<const Map>& rowMap, size_t maxNumEntriesPerRow, Xpetra::ProfileType pftype = Xpetra::DynamicProfile) {
00078 return rcp(new CrsMatrixWrap(rowMap, maxNumEntriesPerRow, pftype));
00079 }
00080
00082 static RCP<Matrix> Build(const RCP<const Map>& rowMap, const RCP<const Map>& colMap, size_t maxNumEntriesPerRow, Xpetra::ProfileType pftype = Xpetra::DynamicProfile) {
00083 return rcp(new CrsMatrixWrap(rowMap, colMap, maxNumEntriesPerRow, pftype));
00084 }
00085
00087 static RCP<Matrix> Build(const RCP<const Map>& rowMap, const RCP<const Map>& colMap, const ArrayRCP<const size_t> &NumEntriesPerRowToAlloc, Xpetra::ProfileType pftype = Xpetra::DynamicProfile) {
00088 return rcp(new CrsMatrixWrap(rowMap, colMap, NumEntriesPerRowToAlloc, pftype));
00089 }
00090
00092 static RCP<Matrix> Build(const RCP<const Map> &rowMap, const ArrayRCP<const size_t> &NumEntriesPerRowToAlloc, ProfileType pftype = Xpetra::DynamicProfile) {
00093 return rcp( new CrsMatrixWrap(rowMap, NumEntriesPerRowToAlloc, pftype) );
00094 }
00095
00097 static RCP<Matrix> Build(const RCP<const CrsGraph>& graph, const RCP<ParameterList>& paramList = Teuchos::null) {
00098 return rcp(new CrsMatrixWrap(graph, paramList));
00099 }
00100
00102 static RCP<Matrix> Build(const RCP<const Vector>& diagonal) {
00103 Teuchos::ArrayRCP<const Scalar> vals = diagonal->getData(0);
00104 LocalOrdinal NumMyElements = diagonal->getMap()->getNodeNumElements();
00105 Teuchos::ArrayView<const GlobalOrdinal> MyGlobalElements = diagonal->getMap()->getNodeElementList();
00106
00107 Teuchos::RCP<CrsMatrixWrap> mtx = Teuchos::rcp(new CrsMatrixWrap(diagonal->getMap(), 1, Xpetra::StaticProfile));
00108
00109 for (LocalOrdinal i = 0; i < NumMyElements; ++i) {
00110 mtx->insertGlobalValues(MyGlobalElements[i],
00111 Teuchos::tuple<GlobalOrdinal>(MyGlobalElements[i]),
00112 Teuchos::tuple<Scalar>(vals[i]) );
00113 }
00114 mtx->fillComplete();
00115 return mtx;
00116 }
00117
00119 static RCP<Matrix> Build(const RCP<const Matrix>& sourceMatrix, const Import& importer, const RCP<const Map>& domainMap = Teuchos::null, const RCP<const Map>& rangeMap = Teuchos::null, const Teuchos::RCP<Teuchos::ParameterList>& params = Teuchos::null) {
00120 RCP<const CrsMatrixWrap> crsOp = Teuchos::rcp_dynamic_cast<const CrsMatrixWrap>(sourceMatrix);
00121 if (crsOp == Teuchos::null)
00122 throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
00123
00124 RCP<CrsMatrix> originalCrs = crsOp->getCrsMatrix();
00125 RCP<CrsMatrix> newCrs = CrsMatrixFactory::Build(originalCrs, importer, domainMap, rangeMap, params);
00126 if (newCrs->hasMatrix())
00127 return rcp(new CrsMatrixWrap(newCrs));
00128 else
00129 return Teuchos::null;
00130 }
00131
00133 static RCP<Matrix> Build(const RCP<const Matrix> & sourceMatrix, const Export &exporter, const RCP<const Map> & domainMap = Teuchos::null, const RCP<const Map> & rangeMap = Teuchos::null,const Teuchos::RCP<Teuchos::ParameterList>& params = Teuchos::null) {
00134 RCP<const CrsMatrixWrap> crsOp = Teuchos::rcp_dynamic_cast<const CrsMatrixWrap>(sourceMatrix);
00135 if (crsOp == Teuchos::null)
00136 throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
00137
00138 RCP<CrsMatrix> originalCrs = crsOp->getCrsMatrix();
00139 return rcp(new CrsMatrixWrap(CrsMatrixFactory::Build(originalCrs, exporter, domainMap, rangeMap, params)));
00140 }
00141 };
00142 #define XPETRA_MATRIXFACTORY_SHORT
00143
00144
00145 template <class Scalar = Matrix<>::scalar_type,
00146 class LocalOrdinal = typename Matrix<Scalar>::local_ordinal_type,
00147 class GlobalOrdinal =
00148 typename Matrix<Scalar, LocalOrdinal>::global_ordinal_type,
00149 class Node =
00150 typename Matrix<Scalar, LocalOrdinal, GlobalOrdinal>::node_type>
00151 class MatrixFactory2 {
00152 #undef XPETRA_MATRIXFACTORY2_SHORT
00153 #include "Xpetra_UseShortNames.hpp"
00154
00155 public:
00156 static RCP<Matrix> BuildCopy(const RCP<const Matrix> A) {
00157 RCP<const CrsMatrixWrap> oldOp = Teuchos::rcp_dynamic_cast<const CrsMatrixWrap>(A);
00158 if (oldOp == Teuchos::null)
00159 throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
00160
00161 RCP<const CrsMatrix> oldCrsOp = oldOp->getCrsMatrix();
00162
00163 UnderlyingLib lib = A->getRowMap()->lib();
00164
00165 TEUCHOS_TEST_FOR_EXCEPTION(lib != UseEpetra && lib != UseTpetra, Exceptions::RuntimeError,
00166 "Not Epetra or Tpetra matrix");
00167
00168 #ifdef HAVE_XPETRA_EPETRA
00169 if (lib == UseEpetra) {
00170
00171 throw Exceptions::RuntimeError("Xpetra::BuildCopy(): matrix templates are incompatible with Epetra");
00172 }
00173 #endif
00174
00175 #ifdef HAVE_XPETRA_TPETRA
00176 if (lib == UseTpetra) {
00177
00178 RCP<const TpetraCrsMatrix> oldTCrsOp = Teuchos::rcp_dynamic_cast<const TpetraCrsMatrix>(oldCrsOp);
00179
00180 if (oldTCrsOp != Teuchos::null) {
00181 RCP<TpetraCrsMatrix> newTCrsOp(new TpetraCrsMatrix(*oldTCrsOp));
00182 RCP<CrsMatrixWrap> newOp (new CrsMatrixWrap(Teuchos::as<RCP<CrsMatrix> >(newTCrsOp)));
00183
00184 return newOp;
00185 } else {
00186 throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::TpetraCrsMatrix failed");
00187 }
00188 }
00189 #endif
00190
00191 return Teuchos::null;
00192 }
00193 };
00194 #define XPETRA_MATRIXFACTORY2_SHORT
00195
00196 template<>
00197 class MatrixFactory2<double,int,int> {
00198 typedef double Scalar;
00199 typedef int LocalOrdinal;
00200 typedef int GlobalOrdinal;
00201 typedef Matrix<double, int, GlobalOrdinal>::node_type Node;
00202 #undef XPETRA_MATRIXFACTORY2_SHORT
00203 #include "Xpetra_UseShortNames.hpp"
00204
00205 public:
00206 static RCP<Matrix> BuildCopy(const RCP<const Matrix> A);
00207 };
00208
00209 #define XPETRA_MATRIXFACTORY2_SHORT
00210
00211 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00212 template<>
00213 class MatrixFactory2<double,int,long long> {
00214 typedef double Scalar;
00215 typedef int LocalOrdinal;
00216 typedef long long GlobalOrdinal;
00217 typedef Matrix<double, int, GlobalOrdinal>::node_type Node;
00218 #undef XPETRA_MATRIXFACTORY2_SHORT
00219 #include "Xpetra_UseShortNames.hpp"
00220
00221 public:
00222 static RCP<Matrix> BuildCopy(const RCP<const Matrix> A);
00223 };
00224 #endif // HAVE_TEUCHOS_LONG_LONG_INT
00225
00226 #define XPETRA_MATRIXFACTORY2_SHORT
00227
00228 }
00229
00230 #define XPETRA_MATRIXFACTORY_SHORT
00231 #define XPETRA_MATRIXFACTORY2_SHORT
00232 #endif