Debian Codegraph @

packages/shylu/shylu_dd/frosch/src/SolverInterfaces/FROSch_BelosSolverTpetra_def.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // ShyLU: Scalable Hybrid LU Preconditioner and Solver
4 //
5 // Copyright 2011 NTESS and the ShyLU contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #ifndef _FROSCH_BELOSSOLVERTPETRA_DEF_HPP
11 #define _FROSCH_BELOSSOLVERTPETRA_DEF_HPP
12
13 #include <FROSch_BelosSolverTpetra_decl.hpp>
14
15
16 namespace FROSch {
17
18 using namespace std;
19 using namespace Teuchos;
20 using namespace Xpetra;
21
22 template<class SC,class LO,class GO,class NO>
23 int BelosSolverTpetra<SC,LO,GO,NO>::initialize()
24 {
25 FROSCH_TIMER_START_SOLVER(initializeTime,"BelosSolverTpetra::initialize");
26 this->IsInitialized_ = true;
27 this->IsComputed_ = false;
28 return 0;
29 }
30
31 template<class SC,class LO,class GO,class NO>
32 int BelosSolverTpetra<SC,LO,GO,NO>::compute()
33 {
34 FROSCH_TIMER_START_SOLVER(computeTime,"BelosSolverTpetra::compute");
35 FROSCH_ASSERT(this->IsInitialized_,"FROSch::BelosSolverTpetra: !this->IsInitialized_");
36 this->IsComputed_ = true;
37 return 0;
38 }
39
40 template<class SC,class LO,class GO,class NO>
41 void BelosSolverTpetra<SC,LO,GO,NO>::apply(const XMultiVector &x,
42 XMultiVector &y,
43 ETransp mode,
44 SC alpha,
45 SC beta) const
46 {
47 FROSCH_TIMER_START_SOLVER(applyTime,"BelosSolverTpetra::apply");
48 FROSCH_ASSERT(this->IsComputed_,"FROSch::BelosSolverTpetra: !this->IsComputed_.");
49
50 const TpetraMultiVector<SC,LO,GO,NO> * xTpetraMultiVectorX = dynamic_cast<const TpetraMultiVector<SC,LO,GO,NO> *>(&x);
51 FROSCH_ASSERT(xTpetraMultiVectorX,"FROSch::BelosSolverTpetra: dynamic_cast failed.");
52 TMultiVectorPtr tpetraMultiVectorX = xTpetraMultiVectorX->getTpetra_MultiVector();
53
54 if (Y_.is_null()) Y_ = XMultiVectorFactory::Build(y.getMap(),y.getNumVectors());
55 const TpetraMultiVector<SC,LO,GO,NO> * xTpetraMultiVectorY = dynamic_cast<const TpetraMultiVector<SC,LO,GO,NO> *>(Y_.get());
56 FROSCH_ASSERT(xTpetraMultiVectorY,"FROSch::BelosSolverTpetra: dynamic_cast failed.");
57 TMultiVectorPtr tpetraMultiVectorY = xTpetraMultiVectorY->getTpetra_MultiVector();
58
59 BelosLinearProblem_->setProblem(tpetraMultiVectorY,tpetraMultiVectorX);
60 BelosSolver_->solve();
61
62 y.update(alpha,*Y_,beta);
63 }
64
65 template<class SC,class LO,class GO,class NO>
66 int BelosSolverTpetra<SC,LO,GO,NO>::updateMatrix(ConstXMatrixPtr k,
67 bool reuseInitialize)
68 {
69 FROSCH_TIMER_START_SOLVER(updateMatrixTime,"BelosSolverTpetra::updateMatrix");
70 this->K_ = k;
71 FROSCH_ASSERT(!this->K_.is_null(),"FROSch::BelosSolverTpetra: K_ is null.");
72
73 const CrsMatrixWrap<SC,LO,GO,NO>& crsOp = dynamic_cast<const CrsMatrixWrap<SC,LO,GO,NO>&>(*this->K_);
74 const TpetraCrsMatrix<SC,LO,GO,NO>& xTpetraMat = dynamic_cast<const TpetraCrsMatrix<SC,LO,GO,NO>&>(*crsOp.getCrsMatrix());
75 ConstTCrsMatrixPtr tpetraMat = xTpetraMat.getTpetra_CrsMatrix();
76 TEUCHOS_TEST_FOR_EXCEPT(tpetraMat.is_null());
77
78 BelosLinearProblem_->setOperator(tpetraMat);
79
80 return 0;
81 }
82
83 template<class SC,class LO,class GO,class NO>
84 BelosSolverTpetra<SC,LO,GO,NO>::BelosSolverTpetra(ConstXMatrixPtr k,
85 ParameterListPtr parameterList,
86 string description) :
87 Solver<SC,LO,GO,NO> (k,parameterList,description)
88 {
89 FROSCH_TIMER_START_SOLVER(BelosSolverTpetraTime,"BelosSolverTpetra::BelosSolverTpetra");
90 FROSCH_ASSERT(!this->K_.is_null(),"FROSch::BelosSolverTpetra: K_ is null.");
91 FROSCH_ASSERT(this->K_->getRowMap()->lib()==UseTpetra,"FROSch::BelosSolverTpetra: Not compatible with Epetra.")
92
93 const CrsMatrixWrap<SC,LO,GO,NO>& crsOp = dynamic_cast<const CrsMatrixWrap<SC,LO,GO,NO>&>(*this->K_);
94 const TpetraCrsMatrix<SC,LO,GO,NO>& xTpetraMat = dynamic_cast<const TpetraCrsMatrix<SC,LO,GO,NO>&>(*crsOp.getCrsMatrix());
95 ConstTCrsMatrixPtr tpetraMat = xTpetraMat.getTpetra_CrsMatrix();
96 TEUCHOS_TEST_FOR_EXCEPT(tpetraMat.is_null());
97
98 TMultiVectorPtr xTmp;
99 TMultiVectorPtr bTmp;
100
101 BelosLinearProblem_.reset(new BelosLinearProblem(tpetraMat,xTmp,bTmp));
102
103 ParameterListPtr belosParameterList = sublist(this->ParameterList_,"Belos");
104 if (belosParameterList->isSublist(this->ParameterList_->get("Solver","GMRES"))) belosParameterList = sublist(belosParameterList,this->ParameterList_->get("Solver","GMRES"));
105
106 BelosSolverFactory belosFactory;
107 BelosSolver_ = belosFactory.create(this->ParameterList_->get("Solver","GMRES"),belosParameterList);
108 BelosSolver_->setProblem(BelosLinearProblem_);
109 }
110
111 }
112
113 #endif