Simplified error handling in pure fluid (tpx) calculations

TPX_Error is now derived from CanteraError, and errors in the tpx calculations
propagate automatically without requiring explicit checks for an error
condition.
This commit is contained in:
Ray Speth 2012-08-08 22:17:48 +00:00
parent 6663ec05c2
commit b7ee30aa04
12 changed files with 108 additions and 240 deletions

View file

@ -553,15 +553,6 @@ protected:
//! Sets the state using a TPX::TV call //! Sets the state using a TPX::TV call
void setTPXState() const; void setTPXState() const;
//! Carry out a internal check on tpx, it may have thrown an error.
/*!
* @param v Defaults to zero
*/
void check(doublereal v = 0.0) const;
//! Report errors in the TPX level
void reportTPXError() const;
private: private:
//! Pointer to the underlying tpx object Substance that does the work //! Pointer to the underlying tpx object Substance that does the work

View file

@ -19,22 +19,18 @@
#ifndef TPX_SUB_H #ifndef TPX_SUB_H
#define TPX_SUB_H #define TPX_SUB_H
#include "cantera/base/ctexceptions.h"
#include <iostream> #include <iostream>
#include <string> #include <string>
namespace tpx namespace tpx
{ {
class TPX_Error class TPX_Error : public Cantera::CanteraError
{ {
public: public:
TPX_Error(std::string p, std::string e) { TPX_Error(std::string p, std::string e) : CanteraError(p, e) { }
ErrorMessage = e;
ErrorProcedure = p;
}
virtual ~TPX_Error() {}
static std::string ErrorMessage;
static std::string ErrorProcedure;
}; };
@ -50,14 +46,6 @@ const int VT = -12, PH = -34, PS = -54, VP = -42, PT = -14, VU = -62,
TS = -51, VS = -52, PU = -64, HV = -23, HT = -13, HS = -53, TS = -51, VS = -52, PU = -64, HV = -23, HT = -13, HS = -53,
XP = -47, XT = -17; XP = -47, XT = -17;
const int NoConverge = -900;
const int GenError = -901;
const int InvalidInput = -902;
const int TempError = -800;
const int PresError = -801;
const int CKError = -802;
const int Pgiven = 0, Tgiven = 1; const int Pgiven = 0, Tgiven = 1;
const int EvalH = 3; const int EvalH = 3;
@ -82,8 +70,6 @@ const int EvalRgas = 19;
const double Undef = 999.1234; const double Undef = 999.1234;
std::string errorMsg(int flag);
class Substance class Substance
{ {
public: public:
@ -205,17 +191,11 @@ public:
void Set(int XY, double x0, double y0); void Set(int XY, double x0, double y0);
void Set_meta(double phase, double pp); void Set_meta(double phase, double pp);
int Error() {
return Err;
}
protected: protected:
double T, Rho; double T, Rho;
double Tslast, Rhf, Rhv; double Tslast, Rhf, Rhv;
double Pst; double Pst;
int Err;
double m_energy_offset; double m_energy_offset;
double m_entropy_offset; double m_entropy_offset;
std::string m_name; std::string m_name;
@ -237,17 +217,6 @@ protected:
int Lever(int itp, double sat, double val, int ifunc); int Lever(int itp, double sat, double val, int ifunc);
void update_sat(); void update_sat();
void set_Err(int ErrFlag) {
if (!Err) {
Err = ErrFlag;
//throw TPX_Error(""errorMsg(Err));
}
}
void clear_Err() {
Err = 0;
}
private: private:
void set_Rho(double r0); void set_Rho(double r0);
void set_T(double t0); void set_T(double t0);
@ -265,9 +234,6 @@ private:
double v_here, P_here; double v_here, P_here;
}; };
void Error(char* message, int flag, double val=Undef);
void Mess(char* message);
} }

View file

@ -134,63 +134,49 @@ doublereal PureFluidPhase::
enthalpy_mole() const enthalpy_mole() const
{ {
setTPXState(); setTPXState();
doublereal h = m_sub->h() * m_mw; return m_sub->h() * m_mw;
check(h);
return h;
} }
doublereal PureFluidPhase:: doublereal PureFluidPhase::
intEnergy_mole() const intEnergy_mole() const
{ {
setTPXState(); setTPXState();
doublereal u = m_sub->u() * m_mw; return m_sub->u() * m_mw;
check(u);
return u;
} }
doublereal PureFluidPhase:: doublereal PureFluidPhase::
entropy_mole() const entropy_mole() const
{ {
setTPXState(); setTPXState();
doublereal s = m_sub->s() * m_mw; return m_sub->s() * m_mw;
check(s);
return s;
} }
doublereal PureFluidPhase:: doublereal PureFluidPhase::
gibbs_mole() const gibbs_mole() const
{ {
setTPXState(); setTPXState();
doublereal g = m_sub->g() * m_mw; return m_sub->g() * m_mw;
check(g);
return g;
} }
doublereal PureFluidPhase:: doublereal PureFluidPhase::
cp_mole() const cp_mole() const
{ {
setTPXState(); setTPXState();
doublereal cp = m_sub->cp() * m_mw; return m_sub->cp() * m_mw;
check(cp);
return cp;
} }
doublereal PureFluidPhase:: doublereal PureFluidPhase::
cv_mole() const cv_mole() const
{ {
setTPXState(); setTPXState();
doublereal cv = m_sub->cv() * m_mw; return m_sub->cv() * m_mw;
check(cv);
return cv;
} }
doublereal PureFluidPhase:: doublereal PureFluidPhase::
pressure() const pressure() const
{ {
setTPXState(); setTPXState();
doublereal p = m_sub->P(); return m_sub->P();
check(p);
return p;
} }
//==================================================================================================================== //====================================================================================================================
void PureFluidPhase:: void PureFluidPhase::
@ -198,16 +184,11 @@ setPressure(doublereal p)
{ {
Set(tpx::TP, temperature(), p); Set(tpx::TP, temperature(), p);
setDensity(1.0/m_sub->v()); setDensity(1.0/m_sub->v());
check();
} }
//==================================================================================================================== //====================================================================================================================
void PureFluidPhase::Set(int n, double x, double y) const void PureFluidPhase::Set(int n, double x, double y) const
{ {
try { m_sub->Set(n, x, y);
m_sub->Set(n, x, y);
} catch (tpx::TPX_Error) {
reportTPXError();
}
} }
//==================================================================================================================== //====================================================================================================================
void PureFluidPhase::setTPXState() const void PureFluidPhase::setTPXState() const
@ -215,21 +196,6 @@ void PureFluidPhase::setTPXState() const
Set(tpx::TV, temperature(), 1.0/density()); Set(tpx::TV, temperature(), 1.0/density());
} }
//==================================================================================================================== //====================================================================================================================
void PureFluidPhase::check(doublereal v) const
{
if (m_sub->Error() || v == tpx::Undef) {
throw CanteraError("PureFluidPhase",string(tpx::errorMsg(
m_sub->Error())));
}
}
//====================================================================================================================
void PureFluidPhase::reportTPXError() const
{
string msg = tpx::TPX_Error::ErrorMessage;
string proc = "tpx::"+tpx::TPX_Error::ErrorProcedure;
throw CanteraError(proc,msg);
}
//====================================================================================================================
doublereal PureFluidPhase::isothermalCompressibility() const doublereal PureFluidPhase::isothermalCompressibility() const
{ {
@ -468,13 +434,8 @@ doublereal PureFluidPhase::critDensity() const
/// saturation temperature /// saturation temperature
doublereal PureFluidPhase::satTemperature(doublereal p) const doublereal PureFluidPhase::satTemperature(doublereal p) const
{ {
try { doublereal ts = m_sub->Tsat(p);
doublereal ts = m_sub->Tsat(p); return ts;
return ts;
} catch (tpx::TPX_Error) {
reportTPXError();
return -1.0;
}
} }
//==================================================================================================================== //====================================================================================================================
void PureFluidPhase::setState_HP(doublereal h, doublereal p, void PureFluidPhase::setState_HP(doublereal h, doublereal p,
@ -482,7 +443,6 @@ void PureFluidPhase::setState_HP(doublereal h, doublereal p,
{ {
Set(tpx::HP, h, p); Set(tpx::HP, h, p);
setState_TR(m_sub->Temp(), 1.0/m_sub->v()); setState_TR(m_sub->Temp(), 1.0/m_sub->v());
check();
} }
//==================================================================================================================== //====================================================================================================================
void PureFluidPhase::setState_UV(doublereal u, doublereal v, void PureFluidPhase::setState_UV(doublereal u, doublereal v,
@ -490,7 +450,6 @@ void PureFluidPhase::setState_UV(doublereal u, doublereal v,
{ {
Set(tpx::UV, u, v); Set(tpx::UV, u, v);
setState_TR(m_sub->Temp(), 1.0/m_sub->v()); setState_TR(m_sub->Temp(), 1.0/m_sub->v());
check();
} }
//==================================================================================================================== //====================================================================================================================
void PureFluidPhase::setState_SV(doublereal s, doublereal v, void PureFluidPhase::setState_SV(doublereal s, doublereal v,
@ -498,7 +457,6 @@ void PureFluidPhase::setState_SV(doublereal s, doublereal v,
{ {
Set(tpx::SV, s, v); Set(tpx::SV, s, v);
setState_TR(m_sub->Temp(), 1.0/m_sub->v()); setState_TR(m_sub->Temp(), 1.0/m_sub->v());
check();
} }
//==================================================================================================================== //====================================================================================================================
void PureFluidPhase::setState_SP(doublereal s, doublereal p, void PureFluidPhase::setState_SP(doublereal s, doublereal p,
@ -506,29 +464,21 @@ void PureFluidPhase::setState_SP(doublereal s, doublereal p,
{ {
Set(tpx::SP, s, p); Set(tpx::SP, s, p);
setState_TR(m_sub->Temp(), 1.0/m_sub->v()); setState_TR(m_sub->Temp(), 1.0/m_sub->v());
check();
} }
//==================================================================================================================== //====================================================================================================================
// saturation pressure // saturation pressure
doublereal PureFluidPhase::satPressure(doublereal t) const doublereal PureFluidPhase::satPressure(doublereal t) const
{ {
doublereal vsv = m_sub->v(); doublereal vsv = m_sub->v();
try { Set(tpx::TV,t,vsv);
Set(tpx::TV,t,vsv); doublereal ps = m_sub->Ps();
doublereal ps = m_sub->Ps(); return ps;
return ps;
} catch (tpx::TPX_Error) {
reportTPXError();
return -1.0;
}
} }
//==================================================================================================================== //====================================================================================================================
doublereal PureFluidPhase::vaporFraction() const doublereal PureFluidPhase::vaporFraction() const
{ {
setTPXState(); setTPXState();
doublereal x = m_sub->x(); return m_sub->x();
check(x);
return x;
} }
//==================================================================================================================== //====================================================================================================================
void PureFluidPhase::setState_Tsat(doublereal t, doublereal x) void PureFluidPhase::setState_Tsat(doublereal t, doublereal x)
@ -537,7 +487,6 @@ void PureFluidPhase::setState_Tsat(doublereal t, doublereal x)
setTPXState(); setTPXState();
Set(tpx::TX, t, x); Set(tpx::TX, t, x);
setDensity(1.0/m_sub->v()); setDensity(1.0/m_sub->v());
check();
} }
//==================================================================================================================== //====================================================================================================================
void PureFluidPhase::setState_Psat(doublereal p, doublereal x) void PureFluidPhase::setState_Psat(doublereal p, doublereal x)
@ -546,7 +495,6 @@ void PureFluidPhase::setState_Psat(doublereal p, doublereal x)
Set(tpx::PX, p, x); Set(tpx::PX, p, x);
setTemperature(m_sub->Temp()); setTemperature(m_sub->Temp());
setDensity(1.0/m_sub->v()); setDensity(1.0/m_sub->v());
check();
} }
//==================================================================================================================== //====================================================================================================================

View file

@ -10,6 +10,9 @@
#include "CarbonDioxide.h" #include "CarbonDioxide.h"
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include "cantera/base/stringUtils.h"
using namespace Cantera;
namespace tpx namespace tpx
{ {
@ -320,8 +323,8 @@ double CarbonDioxide::Psat()
double log, sum=0,P; double log, sum=0,P;
if ((T < Tmn) || (T > Tc)) { if ((T < Tmn) || (T > Tc)) {
std::cout << " error in Psat " << TempError << std::endl; throw TPX_Error("CarbonDixoide::Psat",
set_Err(TempError); // Error("CarbonDioxide::Psat",TempError,T); "Temperature out of range. T = " + fp2str(T));
} }
for (int i=1; i<=8; i++) { for (int i=1; i<=8; i++) {
sum += F[i-1] * pow((T/Tp -1),double(i-1)); sum += F[i-1] * pow((T/Tp -1),double(i-1));
@ -343,8 +346,8 @@ double CarbonDioxide::ldens()
{ {
double xx=1-(T/Tc), sum=0; double xx=1-(T/Tc), sum=0;
if ((T < Tmn) || (T > Tc)) { if ((T < Tmn) || (T > Tc)) {
std::cout << " error in ldens " << TempError << std::endl; throw TPX_Error("CarbonDixoide::ldens",
set_Err(TempError); "Temperature out of range. T = " + fp2str(T));
} }
for (int i=1; i<=6; i++) { for (int i=1; i<=6; i++) {
sum+=D[i-1]*pow(xx,double(i-1)/3.0); sum+=D[i-1]*pow(xx,double(i-1)/3.0);

View file

@ -2,6 +2,9 @@
#include "HFC134a.h" #include "HFC134a.h"
#include <math.h> #include <math.h>
#include "cantera/base/stringUtils.h"
using namespace Cantera;
namespace tpx namespace tpx
{ {
@ -153,7 +156,8 @@ double HFC134a::Pp()
double HFC134a::Psat() double HFC134a::Psat()
{ {
if ((T < Tmn) || (T > Tc)) { if ((T < Tmn) || (T > Tc)) {
set_Err(TempError); throw TPX_Error("HFC134a::Psat",
"Temperature out of range. T = " + fp2str(T));
} }
double x1 = T/Tc; double x1 = T/Tc;
double x2 = 1.0 - x1; double x2 = 1.0 - x1;
@ -178,7 +182,8 @@ double HFC134a::Psat()
double HFC134a::ldens() double HFC134a::ldens()
{ {
if ((T < Tmn) || (T > Tc)) { if ((T < Tmn) || (T > Tc)) {
set_Err(TempError); throw TPX_Error("HFC134a::ldens",
"Temperature out of range. T = " + fp2str(T));
} }
double x1 = T/Tc; double x1 = T/Tc;
double x2 = 1.0 - x1; double x2 = 1.0 - x1;

View file

@ -8,9 +8,12 @@
*/ */
#include "Heptane.h" #include "Heptane.h"
#include "cantera/base/stringUtils.h"
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
using namespace Cantera;
namespace tpx namespace tpx
{ {
@ -277,8 +280,8 @@ double Heptane::Psat()
{ {
double log, sum=0,P; double log, sum=0,P;
if ((T < Tmn) || (T > Tc)) { if ((T < Tmn) || (T > Tc)) {
throw TPX_Error("Heptane::Psat",
set_Err(TempError); // Error("Heptane::Psat",TempError,T); "Temperature out of range. T = " + fp2str(T));
} }
for (int i=1; i<=8; i++) { for (int i=1; i<=8; i++) {
sum += F[i-1] * pow((T/Tp -1),double(i-1)); sum += F[i-1] * pow((T/Tp -1),double(i-1));
@ -299,7 +302,8 @@ double Heptane::ldens()
{ {
double xx=1-(T/Tc), sum=0; double xx=1-(T/Tc), sum=0;
if ((T < Tmn) || (T > Tc)) { if ((T < Tmn) || (T > Tc)) {
set_Err(TempError); throw TPX_Error("Heptane::ldens",
"Temperature out of range. T = " + fp2str(T));
} }
for (int i=1; i<=6; i++) { for (int i=1; i<=6; i++) {
sum+=D[i-1]*pow(xx,double(i-1)/3.0); sum+=D[i-1]*pow(xx,double(i-1)/3.0);

View file

@ -2,6 +2,9 @@
#include "Hydrogen.h" #include "Hydrogen.h"
#include <math.h> #include <math.h>
#include "cantera/base/stringUtils.h"
using namespace Cantera;
namespace tpx namespace tpx
{ {
@ -221,7 +224,8 @@ double hydrogen::Pp()
double hydrogen::ldens() double hydrogen::ldens()
{ {
if ((T < Tmn) || (T > Tc)) { if ((T < Tmn) || (T > Tc)) {
set_Err(TempError); throw TPX_Error("hydrogen::ldens",
"Temperature out of range. T = " + fp2str(T));
} }
double x=1-T/Tc; double x=1-T/Tc;
double sum, term; double sum, term;
@ -240,7 +244,8 @@ double hydrogen::Psat()
double x = (1.0 - Tt/T)/(1.0 - Tt/Tc); double x = (1.0 - Tt/T)/(1.0 - Tt/Tc);
double result; double result;
if ((T < Tmn) || (T > Tc)) { if ((T < Tmn) || (T > Tc)) {
set_Err(TempError); throw TPX_Error("hydrogen::Psat",
"Temperature out of range. T = " + fp2str(T));
} }
result = Fhydro[0]*x + Fhydro[1]*x*x + Fhydro[2]*x*x*x + result = Fhydro[0]*x + Fhydro[1]*x*x + Fhydro[2]*x*x*x +
Fhydro[3]*x*pow(1-x, alpha); Fhydro[3]*x*pow(1-x, alpha);

View file

@ -1,9 +1,12 @@
// Methane // Methane
#include "Methane.h" #include "Methane.h"
#include "cantera/base/stringUtils.h"
#include <math.h> #include <math.h>
#include <iostream> #include <iostream>
using namespace std; using namespace std;
using namespace Cantera;
namespace tpx namespace tpx
{ {
@ -193,7 +196,8 @@ double methane::Psat()
double x = (1.0 - Tt/T)/(1.0 - Tt/Tc); double x = (1.0 - Tt/T)/(1.0 - Tt/Tc);
double result; double result;
if ((T < Tmn) || (T > Tc)) { if ((T < Tmn) || (T > Tc)) {
set_Err(TempError); throw TPX_Error("methane::Psat",
"Temperature out of range. T = " + fp2str(T));
} }
result = Fmeth[0]*x + Fmeth[1]*x*x + Fmeth[2]*x*x*x + result = Fmeth[0]*x + Fmeth[1]*x*x + Fmeth[2]*x*x*x +
Fmeth[3]*x*pow(1-x, alpha); Fmeth[3]*x*pow(1-x, alpha);
@ -208,7 +212,8 @@ double methane::ldens()
double sum; double sum;
double w; double w;
if ((T < Tmn) || (T > Tc)) { if ((T < Tmn) || (T > Tc)) {
set_Err(TempError); throw TPX_Error("methane::ldens",
"Temperature out of range. T = " + fp2str(T));
} }
w = (Tc - T)/(Tc - Tt); w = (Tc - T)/(Tc - Tt);
sum = Dmeth[0]*(1.0 - pow(w, 2.0/3.0)) + Dmeth[1]*(1.0 - pow(w, 4.0/3.0)) sum = Dmeth[0]*(1.0 - pow(w, 2.0/3.0)) + Dmeth[1]*(1.0 - pow(w, 4.0/3.0))

View file

@ -1,9 +1,11 @@
// Nitrogen // Nitrogen
#include "Nitrogen.h" #include "Nitrogen.h"
#include "cantera/base/stringUtils.h"
#include <math.h> #include <math.h>
#include <iostream> #include <iostream>
using namespace std; using namespace std;
using namespace Cantera;
namespace tpx namespace tpx
{ {
@ -209,7 +211,8 @@ double nitrogen::Psat()
double lnp; double lnp;
int i; int i;
if ((T < Tmn) || (T > Tc)) { if ((T < Tmn) || (T > Tc)) {
set_Err(TempError); throw TPX_Error("nitrogen::Psat",
"Temperature out of range. T = " + fp2str(T));
} }
for (i=0, lnp=0; i<=7; i++) { for (i=0, lnp=0; i<=7; i++) {
if (i==3) { if (i==3) {
@ -227,7 +230,8 @@ double nitrogen::ldens()
{ {
double xx=1-T/Tc, sum=0; double xx=1-T/Tc, sum=0;
if ((T < Tmn) || (T > Tc)) { if ((T < Tmn) || (T > Tc)) {
set_Err(TempError); throw TPX_Error("nitrogen::ldens",
"Temperature out of range. T = " + fp2str(T));
} }
for (int i=0; i<=5; i++) { for (int i=0; i<=5; i++) {
sum+=Dnn[i]*pow(xx,double(i)/3.0); sum+=Dnn[i]*pow(xx,double(i)/3.0);

View file

@ -1,8 +1,11 @@
// Oxygen // Oxygen
#include "Oxygen.h" #include "Oxygen.h"
#include "cantera/base/stringUtils.h"
#include <math.h> #include <math.h>
using namespace Cantera;
namespace tpx namespace tpx
{ {
@ -198,7 +201,8 @@ double oxygen::Psat()
double lnp; double lnp;
int i; int i;
if ((T < Tmn) || (T > Tc)) { if ((T < Tmn) || (T > Tc)) {
set_Err(TempError); throw TPX_Error("oxygen::Psat",
"Temperature out of range. T = " + fp2str(T));
} }
for (i=0, lnp=0; i<=7; i++) { for (i=0, lnp=0; i<=7; i++) {
if (i==3) { if (i==3) {
@ -216,7 +220,8 @@ double oxygen::ldens()
{ {
double xx=1-T/Tc, sum=0; double xx=1-T/Tc, sum=0;
if ((T < Tmn) || (T > Tc)) { if ((T < Tmn) || (T > Tc)) {
set_Err(TempError); throw TPX_Error("oxygen::ldens",
"Temperature out of range. T = " + fp2str(T));
} }
for (int i=0; i<=5; i++) { for (int i=0; i<=5; i++) {
sum+=Doxy[i]*pow(xx,double(i)/3.0); sum+=Doxy[i]*pow(xx,double(i)/3.0);

View file

@ -3,51 +3,16 @@
* D. Goodwin, Caltech Nov. 1996 * D. Goodwin, Caltech Nov. 1996
*/ */
#include "cantera/tpx/Sub.h" #include "cantera/tpx/Sub.h"
#include "cantera/base/stringUtils.h"
#include <math.h> #include <math.h>
#include <fstream> #include <fstream>
#include <stdio.h> #include <stdio.h>
using std::string; using std::string;
using namespace Cantera;
namespace tpx namespace tpx
{ {
static string fp2str(double x, string fmt="%g")
{
char buf[30];
sprintf(buf, fmt.c_str(), x);
return string(buf);
}
/*
static string int2str(int n, string fmt="%d") {
char buf[30];
sprintf(buf, fmt.c_str(), n);
return string(buf);
}
*/
string TPX_Error::ErrorMessage = "";
string TPX_Error::ErrorProcedure = "";
string errorMsg(int flag)
{
switch (flag) {
case NoConverge:
return "no convergence";
case GenError:
return "general error";
case InvalidInput:
return "invalid input";
case TempError:
return "temperature error";
case PresError:
return "pressure error";
default:
return "(unknown error)";
}
}
//-------------- Public Member Functions -------------- //-------------- Public Member Functions --------------
Substance::Substance() : Substance::Substance() :
@ -57,7 +22,6 @@ Substance::Substance() :
Rhf(Undef), Rhf(Undef),
Rhv(Undef), Rhv(Undef),
Pst(Undef), Pst(Undef),
Err(0),
m_energy_offset(0.0), m_energy_offset(0.0),
m_entropy_offset(0.0), m_entropy_offset(0.0),
kbr(0) kbr(0)
@ -69,8 +33,7 @@ Substance::Substance() :
/// computed directly from the underlying eos. /// computed directly from the underlying eos.
double Substance::P() double Substance::P()
{ {
double ppp = (TwoPhase() ? Ps() : Pp()); return TwoPhase() ? Ps() : Pp();
return (Err ? Undef : ppp);
} }
const double DeltaT = 0.000001; const double DeltaT = 0.000001;
@ -85,7 +48,7 @@ double Substance::dPsdT()
set_T(T + DeltaT); set_T(T + DeltaT);
dpdt = (Ps() - ps1)/DeltaT; dpdt = (Ps() - ps1)/DeltaT;
set_T(tsave); set_T(tsave);
return (Err ? Undef : dpdt); return dpdt;
} }
/// true if a liquid/vapor mixture, false otherwise /// true if a liquid/vapor mixture, false otherwise
@ -103,21 +66,20 @@ int Substance::TwoPhase()
/// returned if v > Vcrit. /// returned if v > Vcrit.
double Substance::x() double Substance::x()
{ {
double xx, vv, vl; double vv, vl;
if (T >= Tcrit()) { if (T >= Tcrit()) {
return (1.0/Rho < Vcrit() ? 0.0 : 1.0); return (1.0/Rho < Vcrit() ? 0.0 : 1.0);
} else { } else {
update_sat(); update_sat();
if (Rho <= Rhv) { if (Rho <= Rhv) {
xx = 1.0; return 1.0;
} else if (Rho >= Rhf) { } else if (Rho >= Rhf) {
xx = 0.0; return 0.0;
} else { } else {
vv = 1.0/Rhv; vv = 1.0/Rhv;
vl = 1.0/Rhf; vl = 1.0/Rhf;
xx = (1.0/Rho - vl)/(vv - vl); return (1.0/Rho - vl)/(vv - vl);
} }
return (Err ? Undef : xx);
} }
} }
@ -126,10 +88,8 @@ double Substance::Tsat(double p)
{ {
double Tsave, p_here, dp, dt, dpdt, dta, double Tsave, p_here, dp, dt, dpdt, dta,
dtm, tsat; dtm, tsat;
if (Err || (p <= 0.0) || (p > Pcrit())) { if (p <= 0.0 || p > Pcrit()) {
throw TPX_Error("Substance::Tsat","illegal pressure value"); throw TPX_Error("Substance::Tsat", "illegal pressure value");
set_Err(PresError);
return Undef;
} }
int LoopCount = 0; int LoopCount = 0;
double tol = 1.e-6*p; double tol = 1.e-6*p;
@ -141,9 +101,6 @@ double Substance::Tsat(double p)
T = 0.5*(Tcrit() - Tmin()); T = 0.5*(Tcrit() - Tmin());
} }
do { do {
if (Err) {
break;
}
if (T > Tcrit()) { if (T > Tcrit()) {
T = Tcrit() - 0.001; T = Tcrit() - 0.001;
} }
@ -163,13 +120,12 @@ double Substance::Tsat(double p)
LoopCount++; LoopCount++;
if (LoopCount > 100) { if (LoopCount > 100) {
T = Tsave; T = Tsave;
set_Err(NoConverge); throw TPX_Error("Substance::Tsat", "No convergence");
return Undef;
} }
} while (fabs(dp) > tol); } while (fabs(dp) > tol);
tsat = T; tsat = T;
T = Tsave; T = Tsave;
return (Err ? Undef : tsat); return tsat;
} }
@ -187,8 +143,6 @@ void Substance::Set(int XY, double x0, double y0)
{ {
double temp; double temp;
clear_Err(); // clear error flag
/* if inverted (PT) switch order and change sign of XY (TP = -PT) */ /* if inverted (PT) switch order and change sign of XY (TP = -PT) */
if (XY < 0) { if (XY < 0) {
double tmp = x0; double tmp = x0;
@ -274,34 +228,35 @@ void Substance::Set(int XY, double x0, double y0)
case PX: case PX:
temp = Tsat(x0); temp = Tsat(x0);
if ((y0 >= 0.0) && (y0 <= 1.0) && (temp < Tcrit())) { if (y0 > 1.0 || y0 < 0.0) {
throw TPX_Error("Substance::Set",
"Invalid vapor fraction, " + fp2str(y0));
} else if (temp >= Tcrit()) {
throw TPX_Error("Substance::Set",
"Can't set vapor fraction above the critical point");
} else {
set_T(temp); set_T(temp);
update_sat(); update_sat();
Rho = 1.0/((1.0 - y0)/Rhf + y0/Rhv); Rho = 1.0/((1.0 - y0)/Rhf + y0/Rhv);
} else {
set_Err(InvalidInput);
} }
break; break;
case TX: case TX:
if ((y0 >= 0.0) && (y0 <= 1.0) && (x0 < Tcrit())) { if (y0 > 1.0 || y0 < 0.0) {
throw TPX_Error("Substance::Set",
"Invalid vapor fraction, " + fp2str(y0));
} else if (x0 >= Tcrit()) {
throw TPX_Error("Substance::Set",
"Can't set vapor fraction above the critical point");
} else {
set_T(x0); set_T(x0);
update_sat(); update_sat();
Rho = 1.0/((1.0 - y0)/Rhf + y0/Rhv); Rho = 1.0/((1.0 - y0)/Rhf + y0/Rhv);
} else {
set_Err(InvalidInput);
} }
break; break;
default: default:
set_Err(InvalidInput); throw TPX_Error("Substance::Set", "Invalid input.");
}
if (Err) {
T = Undef;
Rho = Undef;
Tslast = Undef;
Rhf = Undef;
Rhv = Undef;
} }
} }
@ -312,7 +267,7 @@ void Substance::set_Rho(double r0)
if (r0 > 0.0) { if (r0 > 0.0) {
Rho = r0; Rho = r0;
} else { } else {
set_Err(InvalidInput); throw TPX_Error("Substance::set_Rho", "Invalid density: " + fp2str(r0));
} }
} }
@ -321,9 +276,7 @@ void Substance::set_T(double t0)
if ((t0 >= Tmin()) && (t0 <= Tmax())) { if ((t0 >= Tmin()) && (t0 <= Tmax())) {
T = t0; T = t0;
} else { } else {
throw TPX_Error("Substance::set_T", throw TPX_Error("Substance::set_T", "illegal temperature: " + fp2str(t0));
"illegal temperature value "+fp2str(t0));
set_Err(TempError);
} }
} }
@ -334,7 +287,6 @@ void Substance::set_v(double v0)
} else { } else {
throw TPX_Error("Substance::set_v", throw TPX_Error("Substance::set_v",
"negative specific volume: "+fp2str(v0)); "negative specific volume: "+fp2str(v0));
set_Err(InvalidInput);
} }
} }
@ -343,8 +295,6 @@ double Substance::Ps()
if (T < Tmin() || T > Tcrit()) { if (T < Tmin() || T > Tcrit()) {
throw TPX_Error("Substance::Ps", throw TPX_Error("Substance::Ps",
"illegal temperature value "+fp2str(T)); "illegal temperature value "+fp2str(T));
set_Err(TempError);
return Undef;
} }
update_sat(); update_sat();
return Pst; return Pst;
@ -427,12 +377,7 @@ void Substance::update_sat()
} }
if (i >= 20) { if (i >= 20) {
Pst = Undef;
Rhv = Undef;
Rhf = Undef;
Tslast = Undef;
throw TPX_Error("substance::update_sat","no convergence"); throw TPX_Error("substance::update_sat","no convergence");
set_Err(NoConverge);
} else { } else {
Pst = pp; Pst = pp;
Tslast = T; Tslast = T;
@ -455,7 +400,7 @@ double Substance::vprop(int ijob)
case EvalP: case EvalP:
return Pp(); return Pp();
default: default:
return Undef; throw TPX_Error("Substance::vprop", "invalid job index");
} }
} }
@ -479,25 +424,22 @@ int Substance::Lever(int itp, double sat, double val, int ifunc)
return 0; return 0;
} }
psat = sat; psat = sat;
T = Tsat(psat); try {
if (T == Undef) { T = Tsat(psat);
Err = 0; } catch (TPX_Error&) {
// Failure to converge here is not an error
T = Tsave; T = Tsave;
Rho = Rhosave; Rho = Rhosave;
return 0; return 0;
} }
} else { } else {
throw TPX_Error("Substance::Lever","general error"); throw TPX_Error("Substance::Lever","general error");
set_Err(GenError);
return GenError;
} }
Set(TX, T, Vapor); Set(TX, T, Vapor);
Valg = vprop(ifunc); Valg = vprop(ifunc);
Set(TX, T, Liquid); Set(TX, T, Liquid);
Valf = vprop(ifunc); Valf = vprop(ifunc);
if (Err) { if (val >= Valf && val <= Valg) {
return Err;
} else if ((val >= Valf) && (val <= Valg)) {
xx = (val - Valf)/(Valg - Valf); xx = (val - Valf)/(Valg - Valf);
vv = (1.0 - xx)/Rhf + xx/Rhv; vv = (1.0 - xx)/Rhf + xx/Rhv;
set_v(vv); set_v(vv);
@ -525,10 +467,6 @@ void Substance::set_xy(int ifx, int ify, double X, double Y,
double v_save = 1.0/Rho; double v_save = 1.0/Rho;
double t_save = T; double t_save = T;
if (Err) {
return;
}
if ((T == Undef) && (Rho == Undef)) { // new object, try to pick if ((T == Undef) && (Rho == Undef)) { // new object, try to pick
Set(TV,Tcrit()*1.1,Vcrit()*1.1); // "reasonable" starting point Set(TV,Tcrit()*1.1,Vcrit()*1.1); // "reasonable" starting point
t_here = T; t_here = T;
@ -545,12 +483,8 @@ void Substance::set_xy(int ifx, int ify, double X, double Y,
Xa = fabs(X); Xa = fabs(X);
Ya = fabs(Y); Ya = fabs(Y);
// loop // loop
do { do {
if (Err) {
break;
}
x_here = prop(ifx); x_here = prop(ifx);
y_here = prop(ify); y_here = prop(ify);
err_x = fabs(X - x_here); err_x = fabs(X - x_here);
@ -616,8 +550,6 @@ void Substance::set_xy(int ifx, int ify, double X, double Y,
LoopCount++; LoopCount++;
if (LoopCount > 200) { if (LoopCount > 200) {
throw TPX_Error("Substance::set_xy","no convergence"); throw TPX_Error("Substance::set_xy","no convergence");
set_Err(NoConverge);
break;
} }
} while (1); } while (1);
} }
@ -713,7 +645,6 @@ void Substance::set_TPp(double Temp, double Pressure)
} }
if (Vmin >= Vmax) { if (Vmin >= Vmax) {
throw TPX_Error("Substance::set_TPp","Vmin >= Vmax"); throw TPX_Error("Substance::set_TPp","Vmin >= Vmax");
set_Err(GenError);
} else if ((Vmin > 0.0) && (Vmax < Big)) { } else if ((Vmin > 0.0) && (Vmax < Big)) {
kbr = 1; kbr = 1;
} }
@ -746,8 +677,6 @@ void Substance::set_TPp(double Temp, double Pressure)
v_here += dv; v_here += dv;
if (dv == 0.0) { if (dv == 0.0) {
throw TPX_Error("Substance::set_TPp","dv = 0 and no convergence"); throw TPX_Error("Substance::set_TPp","dv = 0 and no convergence");
set_Err(NoConverge);
return;
} }
Set(TV, Temp, v_here); Set(TV, Temp, v_here);
LoopCount++; LoopCount++;
@ -756,8 +685,6 @@ void Substance::set_TPp(double Temp, double Pressure)
throw TPX_Error("Substance::set_TPp",string("no convergence for ") throw TPX_Error("Substance::set_TPp",string("no convergence for ")
+"P* = "+fp2str(Pressure/Pcrit())+". V* = " +"P* = "+fp2str(Pressure/Pcrit())+". V* = "
+fp2str(v_save/Vcrit())); +fp2str(v_save/Vcrit()));
set_Err(NoConverge);
return;
} }
} }
Set(TV, Temp,v_here); Set(TV, Temp,v_here);

View file

@ -1,9 +1,12 @@
// water // water
#include "Water.h" #include "Water.h"
#include "cantera/base/stringUtils.h"
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
using namespace Cantera;
namespace tpx namespace tpx
{ {
@ -159,7 +162,8 @@ double water::Psat()
{ {
double log, sum=0,P; double log, sum=0,P;
if ((T < Tmn) || (T > Tc)) { if ((T < Tmn) || (T > Tc)) {
set_Err(TempError); // Error("water::Psat",TempError,T); throw TPX_Error("water::Psat",
"Temperature out of range. T = " + fp2str(T));
} }
for (int i=1; i<=8; i++) { for (int i=1; i<=8; i++) {
sum += F[i-1]*pow(aww*(T-Tp),double(i-1)); // DGG mod sum += F[i-1]*pow(aww*(T-Tp),double(i-1)); // DGG mod
@ -189,7 +193,8 @@ double water::ldens()
double sum=0; double sum=0;
int i; int i;
if ((T < Tmn) || (T >= Tc)) { if ((T < Tmn) || (T >= Tc)) {
set_Err(TempError); // Error("water::ldens",TempError,T); throw TPX_Error("water::ldens",
"Temperature out of range. T = " + fp2str(T));
} }
for (i=0; i<8; i++) { for (i=0; i<8; i++) {
sum+=D[i]*pow(1.0 - T/Tc, double(i+1)/3.0); sum+=D[i]*pow(1.0 - T/Tc, double(i+1)/3.0);