00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
00090
00091
try{
00092
00093
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
00128
00129 setVaryProp(vp);
00130
00131
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
00173
throw new Exception(
"Not implemented");
00174 }
00175
00176
00177 };
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
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