Main Page | Class Hierarchy | Class List | File List | Class Members | Related Pages

solver.h

00001 /* 00002 00003 freesteam - IAPWS-IF97 steam tables library 00004 Copyright (C) 2004-2005 John Pye 00005 00006 This program is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU General Public License 00008 as published by the Free Software Foundation; either version 2 00009 of the License, or (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00019 00020 */ 00021 00022 #ifndef SOLVER_H 00023 #define SOLVER_H 00024 00025 #include "steamcalculator.h" 00026 #include "steamproperty.h" 00027 #include "zeroin.h" 00028 #include "exception.h" 00029 #include <string> 00030 using namespace std; 00031 00033 00040 template<class MainProp, class OtherProp, class VaryProp, int MainPropAlt,int OtherPropAlt,int VaryPropAlt> 00041 class SolverBase : public DesignByContract 00042 { 00043 00044 friend class ZeroIn<SolverBase,VaryProp,OtherProp>; 00045 00046 protected: 00048 SolverBase(const MainProp &mp,const OtherProp &op){ 00049 //cerr << "Running SolverBase creator" << endl; 00050 this->mp=mp; 00051 this->op=op; 00052 } 00053 00054 virtual ~SolverBase(){} 00055 00056 public: 00057 00058 virtual void setLowerBound(const VaryProp &lowerbound){ 00059 this->lowerbound = lowerbound; 00060 } 00061 virtual void setUpperBound(const VaryProp &upperbound){ 00062 this->upperbound = upperbound; 00063 } 00064 00065 00067 void setTarget(const OtherProp &op){ 00068 this->op=op; 00069 } 00070 00072 void setMainProp(const MainProp &mp){ 00073 this->mp=mp; 00074 } 00075 00077 00082 SteamCalculator 00083 solve( 00084 OtherProp maxerror 00085 ,VaryProp tol 00086 ){ 00087 ZeroIn<SolverBase,OtherProp,VaryProp> z; 00088 00089 //cerr << endl << "Solving for "<< SteamProperty<OtherProp,OtherPropAlt>::name() << " = " << op << " by varying " << SteamProperty<VaryProp,VaryPropAlt>::name() << ", and with " << SteamProperty<MainProp,MainPropAlt>::name() << " fixed at " << mp << endl; 00090 00091 try{ 00092 00093 //S.set_pT(P_CRIT,T); 00094 00095 z.setLowerBound(lowerbound); 00096 z.setUpperBound(upperbound); 00097 z.setTolerance(tol); 00098 00099 z.setMethod(&SolverBase::getError_vp); 00100 00101 z.visit(this); 00102 00103 if(!z.isSolved(maxerror)){ 00104 stringstream s; 00105 s.flags(ios_base::showbase); 00106 s << "Failed solution: target "<< SteamProperty<OtherProp,OtherPropAlt>::name() << " = " << op << ", with " << SteamProperty<MainProp,MainPropAlt>::name() << " fixed at " << mp << endl; 00107 00108 s << " (error was " << z.getError() << ", max allowed is " << maxerror << ")"; 00109 throw new Exception(s.str()); 00110 } 00111 00112 setVaryProp(z.getSolution()); 00113 00114 return S; 00115 }catch(Exception *e){ 00116 stringstream s; 00117 s << "Solver<" << SteamProperty<MainProp,MainPropAlt>::name() << "," << SteamProperty<OtherProp,OtherPropAlt>::name() << "," << SteamProperty<VaryProp,VaryPropAlt>::name() << ">::solve: " << e->what(); 00118 delete e; 00119 throw new Exception(s.str()); 00120 } 00121 } 00122 protected: 00123 00124 virtual void setVaryProp(const VaryProp &vp) = 0; 00125 00126 OtherProp getError_vp(const VaryProp &vp){ 00127 //cerr << endl << "Solver::getError_vp: " << SteamProperty<VaryProp,VaryPropAlt>::name() << " = " << vp; 00128 00129 setVaryProp(vp); 00130 00131 //cerr << " -> " << SteamProperty<OtherProp,OtherPropAlt>::name() << " = " << SteamProperty<OtherProp,OtherPropAlt>::get(S) ; 00132 00133 return SteamProperty<OtherProp,OtherPropAlt>::get(S) - op; 00134 } 00135 00136 protected: 00137 00138 OtherProp op; 00139 MainProp mp; 00140 SteamCalculator S; 00141 VaryProp lowerbound; 00142 VaryProp upperbound; 00143 }; 00144 00146 00164 template<class MainProp, class OtherProp, class VaryProp, int MainPropAlt=0,int OtherPropAlt=0,int VaryPropAlt=0> 00165 class Solver 00166 : public SolverBase<MainProp,OtherProp,VaryProp,MainPropAlt,OtherPropAlt,VaryPropAlt> 00167 { 00168 public: 00169 00170 Solver(const MainProp &mp,const OtherProp &op) 00171 : SolverBase<MainProp,OtherProp,VaryProp,MainPropAlt,OtherPropAlt,VaryPropAlt>(mp,op){ 00172 // not implemented 00173 throw new Exception("Not implemented"); 00174 } 00175 00176 00177 }; 00178 00179 /* 00180 00181 // Solving for specific volume given temperature 00182 class Solver<Temperature,SpecificVolume,Pressure,0,0,0> 00183 : public SolverBase<Temperature,SpecificVolume,Pressure,0,0,0> 00184 { 00185 public: 00186 00187 Solver(Temperature T,SpecificVolume v, bool isSuperheated=false) 00188 : SolverBase<Temperature,SpecificVolume,Pressure,0,0,0>(T,v){ 00189 00190 00191 if(T > T_CRIT){ 00192 lowerbound = P_MIN; 00193 upperbound = P_MAX; 00194 }else{ 00195 if(isSuperheated){ 00196 lowerbound = Boundaries::getSatPres_T(T); 00197 upperbound = P_MAX; 00198 }else{ 00199 lowerbound = P_MIN; 00200 upperbound = Boundaries::getSatPres_T(T); 00201 } 00202 } 00203 } 00204 00205 protected: 00206 00207 virtual void setVaryProp(const Pressure &p){ 00208 S.set_pT(p,mp); 00209 } 00210 00211 }; 00212 00213 */ 00214 00216 00226 template<class OtherProp, int OtherPropAlt> 00227 class Solver<Temperature,OtherProp,Pressure,0,OtherPropAlt,0> 00228 : public SolverBase<Temperature,OtherProp,Pressure,0,OtherPropAlt,0> 00229 { 00230 public: 00231 00232 Solver(Temperature T,OtherProp op) 00233 : SolverBase<Temperature,OtherProp,Pressure,0,OtherPropAlt,0>(T,op){ 00234 SolverBase<Temperature,OtherProp,Pressure,0,OtherPropAlt,0>::lowerbound = P_TRIPLE; 00235 SolverBase<Temperature,OtherProp,Pressure,0,OtherPropAlt,0>::upperbound = P_MAX; 00236 } 00237 00238 protected: 00239 00240 virtual void setVaryProp(const Pressure &p){ 00241 SolverBase<Temperature,OtherProp,Pressure,0,OtherPropAlt,0>::S.set_pT(p,SolverBase<Temperature,OtherProp,Pressure,0,OtherPropAlt,0>::mp); 00242 } 00243 }; 00244 00246 00249 template<class OtherProp, int OtherPropAlt> 00250 class Solver<Pressure,OtherProp,Temperature,0,OtherPropAlt,0> 00251 : public SolverBase<Pressure,OtherProp,Temperature,0,OtherPropAlt,0> 00252 { 00253 public: 00254 00255 Solver(Pressure p, OtherProp op) 00256 : SolverBase<Pressure,OtherProp,Temperature,0,OtherPropAlt,0>(p,op){ 00257 SolverBase<Pressure,OtherProp,Temperature,0,OtherPropAlt,0>::lowerbound = T_MIN; 00258 SolverBase<Pressure,OtherProp,Temperature,0,OtherPropAlt,0>::upperbound = T_MAX; 00259 } 00260 00261 protected: 00262 00263 virtual void setVaryProp(const Temperature &T){ 00264 SolverBase<Pressure,OtherProp,Temperature,0,OtherPropAlt,0>::S.set_pT(SolverBase<Pressure,OtherProp,Temperature,0,OtherPropAlt,0>::mp,T); 00265 } 00266 }; 00267 00268 #endif

Generated on Tue Mar 22 19:07:05 2005 for freesteam by doxygen 1.3.8