ROL
ROL_KrylovFactory.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_KRYLOVFACTORY_H
11 #define ROL_KRYLOVFACTORY_H
12 
13 #include "ROL_Ptr.hpp"
14 #include "ROL_Types.hpp"
15 
18 #include "ROL_GMRES.hpp"
19 #include "ROL_MINRES.hpp"
20 #include "ROL_BiCGSTAB.hpp"
21 
22 namespace ROL {
34  enum EKrylov{
35  KRYLOV_CG = 0,
42  };
43 
44  inline std::string EKrylovToString(EKrylov type) {
45  std::string retString;
46  switch(type) {
47  case KRYLOV_CG: retString = "Conjugate Gradients"; break;
48  case KRYLOV_CR: retString = "Conjugate Residuals"; break;
49  case KRYLOV_GMRES: retString = "GMRES"; break;
50  case KRYLOV_MINRES: retString = "MINRES"; break;
51  case KRYLOV_BICGSTAB: retString = "BiCGSTAB"; break;
52  case KRYLOV_USERDEFINED: retString = "User Defined"; break;
53  case KRYLOV_LAST: retString = "Last Type (Dummy)"; break;
54  default: retString = "INVALID EKrylov";
55  }
56  return retString;
57  }
58 
64  inline int isValidKrylov(EKrylov type){
65  return( (type == KRYLOV_CG) ||
66  (type == KRYLOV_CR) ||
67  (type == KRYLOV_GMRES) ||
68  (type == KRYLOV_MINRES) ||
69  (type == KRYLOV_BICGSTAB) ||
70  (type == KRYLOV_USERDEFINED) );
71  }
72 
73  inline EKrylov & operator++(EKrylov &type) {
74  return type = static_cast<EKrylov>(type+1);
75  }
76 
77  inline EKrylov operator++(EKrylov &type, int) {
78  EKrylov oldval = type;
79  ++type;
80  return oldval;
81  }
82 
83  inline EKrylov & operator--(EKrylov &type) {
84  return type = static_cast<EKrylov>(type-1);
85  }
86 
87  inline EKrylov operator--(EKrylov &type, int) {
88  EKrylov oldval = type;
89  --type;
90  return oldval;
91  }
92 
93  inline EKrylov StringToEKrylov(std::string s) {
94  s = removeStringFormat(s);
95  for ( EKrylov type = KRYLOV_CG; type < KRYLOV_LAST; type++ ) {
96  if ( !s.compare(removeStringFormat(EKrylovToString(type))) ) {
97  return type;
98  }
99  }
100  return KRYLOV_CG;
101  }
102 
103  template<class Real>
104  inline Ptr<Krylov<Real>> KrylovFactory( ParameterList &parlist ) {
105  Real em4(1e-4), em2(1e-2);
106  EKrylov ekv = StringToEKrylov(
107  parlist.sublist("General").sublist("Krylov").get("Type","GMRES"));
108  Real absTol = parlist.sublist("General").sublist("Krylov").get("Absolute Tolerance", em4);
109  Real relTol = parlist.sublist("General").sublist("Krylov").get("Relative Tolerance", em2);
110  int maxit = parlist.sublist("General").sublist("Krylov").get("Iteration Limit", 20);
111  bool inexact = parlist.sublist("General").get("Inexact Hessian-Times-A-Vector",false);
112  switch(ekv) {
113  case KRYLOV_CR:
114  return makePtr<ConjugateResiduals<Real>>(absTol,relTol,maxit,inexact);
115  case KRYLOV_CG:
116  return makePtr<ConjugateGradients<Real>>(absTol,relTol,maxit,inexact);
117  case KRYLOV_GMRES:
118  return makePtr<GMRES<Real>>(parlist);
119  case KRYLOV_MINRES:
120  return makePtr<MINRES<Real>>(absTol,relTol,maxit,inexact);
121  case KRYLOV_BICGSTAB:
122  return makePtr<BiCGSTAB<Real>>(absTol,relTol,maxit,inexact);
123  default:
124  return nullPtr;
125  }
126  }
127 
128 }
129 
130 #endif
EPolyProjAlgo & operator++(EPolyProjAlgo &type)
Contains definitions of custom data types in ROL.
std::string removeStringFormat(std::string s)
Definition: ROL_Types.hpp:215
EKrylov
Enumeration of Krylov methods.
EKrylov StringToEKrylov(std::string s)
EPolyProjAlgo & operator--(EPolyProjAlgo &type)
Ptr< Krylov< Real > > KrylovFactory(ParameterList &parlist)
std::string EKrylovToString(EKrylov type)
int isValidKrylov(EKrylov type)
Verifies validity of a Krylov enum.