ROL
ROL_KrylovFactory.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #ifndef ROL_KRYLOVFACTORY_H
45 #define ROL_KRYLOVFACTORY_H
46 
47 #include "ROL_Ptr.hpp"
48 #include "ROL_Types.hpp"
49 
52 #include "ROL_GMRES.hpp"
53 #include "ROL_MINRES.hpp"
54 
55 namespace ROL {
66  enum EKrylov{
67  KRYLOV_CG = 0,
73  };
74 
75  inline std::string EKrylovToString(EKrylov type) {
76  std::string retString;
77  switch(type) {
78  case KRYLOV_CG: retString = "Conjugate Gradients"; break;
79  case KRYLOV_CR: retString = "Conjugate Residuals"; break;
80  case KRYLOV_GMRES: retString = "GMRES"; break;
81  case KRYLOV_MINRES: retString = "MINRES"; break;
82  case KRYLOV_USERDEFINED: retString = "User Defined"; break;
83  case KRYLOV_LAST: retString = "Last Type (Dummy)"; break;
84  default: retString = "INVALID EKrylov";
85  }
86  return retString;
87  }
88 
94  inline int isValidKrylov(EKrylov type){
95  return( (type == KRYLOV_CG) ||
96  (type == KRYLOV_CR) ||
97  (type == KRYLOV_GMRES) ||
98  (type == KRYLOV_USERDEFINED) );
99  }
100 
101  inline EKrylov & operator++(EKrylov &type) {
102  return type = static_cast<EKrylov>(type+1);
103  }
104 
105  inline EKrylov operator++(EKrylov &type, int) {
106  EKrylov oldval = type;
107  ++type;
108  return oldval;
109  }
110 
111  inline EKrylov & operator--(EKrylov &type) {
112  return type = static_cast<EKrylov>(type-1);
113  }
114 
115  inline EKrylov operator--(EKrylov &type, int) {
116  EKrylov oldval = type;
117  --type;
118  return oldval;
119  }
120 
121  inline EKrylov StringToEKrylov(std::string s) {
122  s = removeStringFormat(s);
123  for ( EKrylov type = KRYLOV_CG; type < KRYLOV_LAST; type++ ) {
124  if ( !s.compare(removeStringFormat(EKrylovToString(type))) ) {
125  return type;
126  }
127  }
128  return KRYLOV_CG;
129  }
130 
131  template<class Real>
132  inline Ptr<Krylov<Real>> KrylovFactory( ParameterList &parlist ) {
133  Real em4(1e-4), em2(1e-2);
134  EKrylov ekv = StringToEKrylov(
135  parlist.sublist("General").sublist("Krylov").get("Type","GMRES"));
136  Real absTol = parlist.sublist("General").sublist("Krylov").get("Absolute Tolerance", em4);
137  Real relTol = parlist.sublist("General").sublist("Krylov").get("Relative Tolerance", em2);
138  int maxit = parlist.sublist("General").sublist("Krylov").get("Iteration Limit", 20);
139  bool inexact = parlist.sublist("General").get("Inexact Hessian-Times-A-Vector",false);
140  switch(ekv) {
141  case KRYLOV_CR:
142  return makePtr<ConjugateResiduals<Real>>(absTol,relTol,maxit,inexact);
143  case KRYLOV_CG:
144  return makePtr<ConjugateGradients<Real>>(absTol,relTol,maxit,inexact);
145  case KRYLOV_MINRES:
146  return makePtr<MINRES<Real>>(absTol,relTol,maxit,inexact);
147  case KRYLOV_GMRES:
148  return makePtr<GMRES<Real>>(parlist);
149  default:
150  return nullPtr;
151  }
152  }
153 
154 }
155 
156 #endif
EKrylov & operator++(EKrylov &type)
Contains definitions of custom data types in ROL.
std::string removeStringFormat(std::string s)
Definition: ROL_Types.hpp:247
EKrylov
Enumeration of Krylov methods.
EKrylov StringToEKrylov(std::string s)
Ptr< Krylov< Real > > KrylovFactory(ParameterList &parlist)
std::string EKrylovToString(EKrylov type)
int isValidKrylov(EKrylov type)
Verifies validity of a Krylov enum.
EKrylov & operator--(EKrylov &type)