Debian Codegraph @

packages/shylu/shylu_dd/core/src/epetra/IQRSolver.cpp

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 /** \file IQRSolver.h
11
12 \brief Encapsulates the IQR inexact solver functionality
13
14 \author Radu Popescu
15
16 */
17
18 #include <IQRSolver.h>
19
20 #include <cmath>
21
22 #include <Ifpack_config.h>
23
24 #ifdef HAVE_IFPACK_DYNAMIC_FACTORY
25 #include "Ifpack_DynamicFactory.h"
26 #else
27 #include "Ifpack.h"
28 #endif
29
30 namespace IQR {
31
32 IQRSolver::IQRSolver(const Teuchos::ParameterList& pList)
33 : gmresStateManager_(),
34 prec_(0),
35 isComputed_(false),
36 krylovDim_(0.0),
37 numIter_(0),
38 doScaling_(false),
39 useFullIQR_(true),
40 precType_("Amesos"),
41 precAmesosType_("Amesos_Klu")
42 {
43 // We shouldn't modify the external parameter list
44 Teuchos::ParameterList params = pList;
45
46 // Default mode, G or IQR?
47 useFullIQR_ = params.get<bool>("Use full IQR", true);
48
49 // Default parameters for IQR
50 krylovDim_ = params.get<double>("IQR Krylov Dim", 0.5);
51 numIter_ = params.get<int>("IQR Number Iterations", 0);
52 doScaling_ = params.get<bool>("IQR Scaling", false);
53 precType_ = params.get<std::string>("IQR Initial Prec Type", "Amesos");
54 precAmesosType_ = params.get<std::string>("IQR Initial Prec Amesos Type", "Amesos_Klu");
55 }
56
57 IQRSolver::~IQRSolver()
58 {
59 }
60
61 int IQRSolver::Compute(const ShyLU_Probing_Operator& S,
62 const Epetra_MultiVector& B,
63 Epetra_MultiVector & X)
64 {
65 // The preconditioner is used regardless of the value ofuseFullIQR_
66 Epetra_CrsMatrix* G = S.G_;
67 #ifdef HAVE_IFPACK_DYNAMIC_FACTORY
68 Ifpack_DynamicFactory Factory;
69 #else
70 Ifpack Factory;
71 #endif
72 prec_ = Teuchos::rcp(Factory.Create(precType_, G, 1));
73 Teuchos::ParameterList pList;
74 pList.set("amesos: solver type", precAmesosType_, "");
75 pList.set("Reindex", true);
76 prec_->SetParameters(pList);
77
78 IFPACK_CHK_ERR(prec_->Initialize());
79 IFPACK_CHK_ERR(prec_->Compute());
80
81 // Only for IQR
82 if (useFullIQR_) {
83 // Computation phase - only during the first outer GMRES iteration
84 int sSize = S.OperatorDomainMap().NumGlobalElements();
85 int kSize = std::floor(krylovDim_ * sSize);
86
87 gmresStateManager_ = Teuchos::rcp(
88 new GMRESStateManager(S.OperatorDomainMap(),
89 kSize, false, doScaling_));
90 gmresStateManager_->isFirst = true;
91
92 double tol = 1e-10;
93 IQR::IdPreconditioner L;
94 IQR::GMRES<Epetra_Operator,
95 Epetra_MultiVector,
96 IQR::IdPreconditioner,
97 Ifpack_Preconditioner,
98 GMRESStateManager,
99 std::vector<double>, double>
100 (S, X, B, &L, &(*prec_), *(gmresStateManager_), kSize, tol);
101 gmresStateManager_->P2 = &(*prec_);
102
103 // if (! Xs.Comm().MyPID()) {
104 // std::cout << "KSIZE: " << kSize
105 // << ", SSIZE: " << sSize
106 // << ", TOL: " << tol << std::endl;
107 // }
108 }
109 isComputed_ = true;
110
111 return 0;
112 }
113
114 int IQRSolver::Solve(const ShyLU_Probing_Operator& S,
115 const Epetra_MultiVector& B,
116 Epetra_MultiVector & X)
117 {
118 if (! isComputed_) {
119 IFPACK_CHK_ERR(Compute(S, B, X));
120 }
121 // Solve phase
122 if (! useFullIQR_) {
123 IFPACK_CHK_ERR(prec_->ApplyInverse(B, X));
124 } else {
125 if (numIter_ > 0) {
126 GMRESStateManager newGmresManager(S.OperatorDomainMap(),
127 numIter_+1, false);
128 double tol=1e-10;
129 IQR::IdPreconditioner L;
130 IQR::GMRES<Epetra_Operator, Epetra_MultiVector,
131 IQR::IdPreconditioner, GMRESStateManager, GMRESStateManager,
132 std::vector<double>, double>
133 (S, X, B, &L, &*(gmresStateManager_), newGmresManager, numIter_, tol);
134 } else {
135 IFPACK_CHK_ERR(gmresStateManager_->ApplyInverse(B, X));
136 }
137 }
138
139 return 0;
140 }
141
142 } /* namespace IQR */