Debian Codegraph @

packages/shylu/shylu_dd/frosch/src/SolverInterfaces/FROSch_BelosSolverEpetra_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_BELOSSOLVEREPETRA_DEF_HPP
11 #define _FROSCH_BELOSSOLVEREPETRA_DEF_HPP
12
13 #include <FROSch_BelosSolverEpetra_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 BelosSolverEpetra<SC,LO,GO,NO>::initialize()
24 {
25 FROSCH_TIMER_START_SOLVER(initializeTime,"BelosSolverEpetra::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 BelosSolverEpetra<SC,LO,GO,NO>::compute()
33 {
34 FROSCH_TIMER_START_SOLVER(computeTime,"BelosSolverEpetra::compute");
35 FROSCH_ASSERT(this->IsInitialized_,"FROSch::BelosSolverEpetra: !this->IsInitialized_");
36 this->IsComputed_ = true;
37 return 0;
38 }
39
40 template<class SC,class LO,class GO,class NO>
41 void BelosSolverEpetra<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,"BelosSolverEpetra::apply");
48 FROSCH_ASSERT(this->IsComputed_,"FROSch::BelosSolverEpetra: !this->IsComputed_.");
49
50 const EpetraMultiVectorT<GO,NO> * xEpetraMultiVectorX = dynamic_cast<const EpetraMultiVectorT<GO,NO> *>(&x);
51 FROSCH_ASSERT(xEpetraMultiVectorX,"FROSch::BelosSolverEpetra: dynamic_cast failed.");
52 RCP<EMultiVector> epetraMultiVectorX = xEpetraMultiVectorX->getEpetra_MultiVector();
53
54 if (Y_.is_null()) Y_ = XMultiVectorFactory::Build(y.getMap(),y.getNumVectors());
55 EpetraMultiVectorT<GO,NO> * xEpetraMultiVectorY = dynamic_cast<EpetraMultiVectorT<GO,NO> *>(Y_.get());
56 FROSCH_ASSERT(xEpetraMultiVectorY,"FROSch::BelosSolverEpetra: dynamic_cast failed.");
57 RCP<EMultiVector> epetraMultiVectorY = xEpetraMultiVectorY->getEpetra_MultiVector();
58
59 BelosLinearProblem_->setProblem(epetraMultiVectorY,epetraMultiVectorX);
60 BelosSolver_->solve();
61
62 y.update(alpha,*Y_,beta);
63 }
64
65 template<class SC,class LO,class GO,class NO>
66 int BelosSolverEpetra<SC,LO,GO,NO>::updateMatrix(ConstXMatrixPtr k,
67 bool reuseInitialize)
68 {
69 FROSCH_TIMER_START_SOLVER(updateMatrixTime,"BelosSolverEpetra::updateMatrix");
70 this->K_ = k;
71 FROSCH_ASSERT(!this->K_.is_null(),"FROSch::BelosSolverEpetra: K_ is null.");
72
73 const CrsMatrixWrap<SC,LO,GO,NO>& crsOp = dynamic_cast<const CrsMatrixWrap<SC,LO,GO,NO>&>(*this->K_);
74 const EpetraCrsMatrixT<GO,NO>& xEpetraMat = dynamic_cast<const EpetraCrsMatrixT<GO,NO>&>(*crsOp.getCrsMatrix());
75 ECrsMatrixPtr epetraMat = xEpetraMat.getEpetra_CrsMatrixNonConst();
76 TEUCHOS_TEST_FOR_EXCEPT(epetraMat.is_null());
77
78 BelosLinearProblem_->setOperator(epetraMat);
79
80 return 0;
81 }
82
83 template<class SC,class LO,class GO,class NO>
84 BelosSolverEpetra<SC,LO,GO,NO>::BelosSolverEpetra(ConstXMatrixPtr k,
85 ParameterListPtr parameterList,
86 string description) :
87 Solver<SC,LO,GO,NO> (k,parameterList,description)
88 {
89 FROSCH_TIMER_START_SOLVER(BelosSolverEpetraTime,"BelosSolverEpetra::BelosSolverEpetra");
90 FROSCH_ASSERT(!this->K_.is_null(),"FROSch::BelosSolverEpetra: K_ is null.");
91 FROSCH_ASSERT(this->K_->getRowMap()->lib()==UseEpetra,"FROSch::BelosSolverEpetra: Not compatible with Tpetra.")
92
93 const CrsMatrixWrap<SC,LO,GO,NO>& crsOp = dynamic_cast<const CrsMatrixWrap<SC,LO,GO,NO>&>(*this->K_);
94 const EpetraCrsMatrixT<GO,NO>& xEpetraMat = dynamic_cast<const EpetraCrsMatrixT<GO,NO>&>(*crsOp.getCrsMatrix());
95 ECrsMatrixPtr epetraMat = xEpetraMat.getEpetra_CrsMatrixNonConst();
96 TEUCHOS_TEST_FOR_EXCEPT(epetraMat.is_null());
97
98 EMultiVectorPtr xTmp;
99 EMultiVectorPtr bTmp;
100
101 BelosLinearProblem_.reset(new BelosLinearProblem(epetraMat,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