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 
21 namespace ROL {
32  enum EKrylov{
33  KRYLOV_CG = 0,
39  };
40 
41  inline std::string EKrylovToString(EKrylov type) {
42  std::string retString;
43  switch(type) {
44  case KRYLOV_CG: retString = "Conjugate Gradients"; break;
45  case KRYLOV_CR: retString = "Conjugate Residuals"; break;
46  case KRYLOV_GMRES: retString = "GMRES"; break;
47  case KRYLOV_MINRES: retString = "MINRES"; break;
48  case KRYLOV_USERDEFINED: retString = "User Defined"; break;
49  case KRYLOV_LAST: retString = "Last Type (Dummy)"; break;
50  default: retString = "INVALID EKrylov";
51  }
52  return retString;
53  }
54 
60  inline int isValidKrylov(EKrylov type){
61  return( (type == KRYLOV_CG) ||
62  (type == KRYLOV_CR) ||
63  (type == KRYLOV_GMRES) ||
64  (type == KRYLOV_USERDEFINED) );
65  }
66 
67  inline EKrylov & operator++(EKrylov &type) {
68  return type = static_cast<EKrylov>(type+1);
69  }
70 
71  inline EKrylov operator++(EKrylov &type, int) {
72  EKrylov oldval = type;
73  ++type;
74  return oldval;
75  }
76 
77  inline EKrylov & operator--(EKrylov &type) {
78  return type = static_cast<EKrylov>(type-1);
79  }
80 
81  inline EKrylov operator--(EKrylov &type, int) {
82  EKrylov oldval = type;
83  --type;
84  return oldval;
85  }
86 
87  inline EKrylov StringToEKrylov(std::string s) {
88  s = removeStringFormat(s);
89  for ( EKrylov type = KRYLOV_CG; type < KRYLOV_LAST; type++ ) {
90  if ( !s.compare(removeStringFormat(EKrylovToString(type))) ) {
91  return type;
92  }
93  }
94  return KRYLOV_CG;
95  }
96 
97  template<class Real>
98  inline Ptr<Krylov<Real>> KrylovFactory( ParameterList &parlist ) {
99  Real em4(1e-4), em2(1e-2);
100  EKrylov ekv = StringToEKrylov(
101  parlist.sublist("General").sublist("Krylov").get("Type","GMRES"));
102  Real absTol = parlist.sublist("General").sublist("Krylov").get("Absolute Tolerance", em4);
103  Real relTol = parlist.sublist("General").sublist("Krylov").get("Relative Tolerance", em2);
104  int maxit = parlist.sublist("General").sublist("Krylov").get("Iteration Limit", 20);
105  bool inexact = parlist.sublist("General").get("Inexact Hessian-Times-A-Vector",false);
106  switch(ekv) {
107  case KRYLOV_CR:
108  return makePtr<ConjugateResiduals<Real>>(absTol,relTol,maxit,inexact);
109  case KRYLOV_CG:
110  return makePtr<ConjugateGradients<Real>>(absTol,relTol,maxit,inexact);
111  case KRYLOV_MINRES:
112  return makePtr<MINRES<Real>>(absTol,relTol,maxit,inexact);
113  case KRYLOV_GMRES:
114  return makePtr<GMRES<Real>>(parlist);
115  default:
116  return nullPtr;
117  }
118  }
119 
120 }
121 
122 #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.