Skip to content

Commit

Permalink
Prepare for CoolPropDbl->double; see #931
Browse files Browse the repository at this point in the history
  • Loading branch information
ibell committed Jan 19, 2016
1 parent 4f4344e commit 74e36ac
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
6 changes: 4 additions & 2 deletions include/AbstractState.h
Expand Up @@ -337,9 +337,9 @@ class AbstractState {
virtual void calc_ideal_curve(const std::string &type, std::vector<double> &T, std::vector<double> &p){ throw NotImplementedError("calc_ideal_curve is not implemented for this backend"); };

/// Using this backend, get the temperature
virtual long double calc_T(void){ return _T; }
virtual CoolPropDbl calc_T(void){ return _T; }
/// Using this backend, get the molar density in mol/m^3
virtual long double calc_rhomolar(void){ return _rhomolar; }
virtual CoolPropDbl calc_rhomolar(void){ return _rhomolar; }

/// Using this backend, calculate the tangent plane distance for a given trial composition
virtual double calc_tangent_plane_distance(const double T, const double p, const std::vector<double> &w, const double rhomolar_guess){ throw NotImplementedError("calc_tangent_plane_distance is not implemented for this backend"); };
Expand Down Expand Up @@ -418,9 +418,11 @@ class AbstractState {
virtual void set_mass_fractions(const std::vector<CoolPropDbl> &mass_fractions) = 0;
virtual void set_volu_fractions(const std::vector<CoolPropDbl> &mass_fractions){ throw NotImplementedError("Volume composition has not been implemented."); }

#ifndef COOLPROPDBL_MAPS_TO_DOUBLE
void set_mole_fractions(const std::vector<double> &mole_fractions){ set_mole_fractions(std::vector<CoolPropDbl>(mole_fractions.begin(), mole_fractions.end())); };
void set_mass_fractions(const std::vector<double> &mass_fractions){ set_mass_fractions(std::vector<CoolPropDbl>(mass_fractions.begin(), mass_fractions.end())); };
void set_volu_fractions(const std::vector<double> &volu_fractions){ set_volu_fractions(std::vector<CoolPropDbl>(volu_fractions.begin(), volu_fractions.end())); };
#endif

#ifdef EMSCRIPTEN
void set_mole_fractions_double(const std::vector<double> &mole_fractions){ set_mole_fractions(std::vector<CoolPropDbl>(mole_fractions.begin(), mole_fractions.end())); };
Expand Down
2 changes: 2 additions & 0 deletions include/CachedElement.h
Expand Up @@ -62,12 +62,14 @@ class CachedElement {
throw std::exception();
}
}
#ifndef COOLPROPDBL_MAPS_TO_DOUBLE
operator CoolPropDbl() {
if (is_cached) {return value; }
else {
throw std::exception();
}
}
#endif
/// Clear the flag and the value
void clear() {
is_cached = false;
Expand Down
10 changes: 7 additions & 3 deletions include/CoolPropTools.h
Expand Up @@ -66,7 +66,11 @@
#endif
#endif

typedef long double CoolPropDbl;
#ifdef COOLPROPDBL_MAPS_TO_DOUBLE
typedef double CoolPropDbl;
#else
typedef long double CoolPropDbl;
#endif

#if defined(__ISWINDOWS__)
#include <sys/types.h>
Expand Down Expand Up @@ -301,8 +305,8 @@
double powInt(double x, int y);

template<class T> T POW2(T x) { return x*x; }
#define POW3(x) ((x)*(x)*(x))
#define POW4(x) ((x)*(x)*(x)*(x))
template<class T> T POW3(T x) { return POW2(x)*x; }
template<class T> T POW4(T x) { return POW2(x)*POW2(x); }
#define POW5(x) ((x)*(x)*(x)*(x)*(x))
#define POW6(x) ((x)*(x)*(x)*(x)*(x)*(x))
#define POW7(x) ((x)*(x)*(x)*(x)*(x)*(x)*(x))
Expand Down
16 changes: 8 additions & 8 deletions include/rapidjson/rapidjson_include.h
Expand Up @@ -122,9 +122,9 @@ namespace cpjson
};

/// A convenience function to get a long double array compactly
inline std::vector<long double> get_long_double_array(rapidjson::Value &v)
inline std::vector<CoolPropDbl> get_long_double_array(rapidjson::Value &v)
{
std::vector<long double> out;
std::vector<CoolPropDbl> out;
if (!v.IsArray()) { throw CoolProp::ValueError("input is not an array"); }
for (rapidjson::Value::ValueIterator itr = v.Begin(); itr != v.End(); ++itr)
{
Expand Down Expand Up @@ -159,10 +159,10 @@ namespace cpjson
};

/// A convenience function to get a 2D long double array compactly
inline std::vector< std::vector<long double> > get_long_double_array2D(rapidjson::Value &v)
inline std::vector< std::vector<CoolPropDbl> > get_long_double_array2D(rapidjson::Value &v)
{
std::vector< std::vector<long double> > out;
std::vector<long double> tmp;
std::vector< std::vector<CoolPropDbl> > out;
std::vector<CoolPropDbl> tmp;
if (!v.IsArray()) { throw CoolProp::ValueError("input is not an array"); }
for (rapidjson::Value::ValueIterator itr = v.Begin(); itr != v.End(); ++itr)
{
Expand All @@ -179,9 +179,9 @@ namespace cpjson
};

/// A convenience function to get a long double array compactly
inline std::vector<long double> get_long_double_array(rapidjson::Value &v, std::string name)
inline std::vector<CoolPropDbl> get_long_double_array(rapidjson::Value &v, std::string name)
{
std::vector<long double> out;
std::vector<CoolPropDbl> out;
if (!v.HasMember(name.c_str())){ throw CoolProp::ValueError(format("Does not have member [%s]",name.c_str())); }
if (!v[name.c_str()].IsArray()) { throw CoolProp::ValueError("input is not an array"); }
for (rapidjson::Value::ValueIterator itr = v[name.c_str()].Begin(); itr != v[name.c_str()].End(); ++itr)
Expand Down Expand Up @@ -262,7 +262,7 @@ namespace cpjson
};

/// A convenience function to set a double array compactly
inline void set_long_double_array(const char *key, const std::vector<long double> &vec, rapidjson::Value &value, rapidjson::Document &doc)
inline void set_long_double_array(const char *key, const std::vector<CoolPropDbl> &vec, rapidjson::Value &value, rapidjson::Document &doc)
{
rapidjson::Value _v(rapidjson::kArrayType);
for (unsigned int i = 0; i < vec.size(); ++i)
Expand Down
8 changes: 4 additions & 4 deletions src/Backends/Helmholtz/FlashRoutines.cpp
Expand Up @@ -55,7 +55,7 @@ void FlashRoutines::PT_flash_mixtures(HelmholtzEOSMixtureBackend &HEOS)
// Try using Newton's method
CoolPropDbl rhomolar = Newton(resid, rhomolar_guess, 1e-10, 100, errstr);
// Make sure the solution is within the bounds
if (!is_in_closed_range(static_cast<CoolPropDbl>(closest_state.rhomolar), 0.0L, rhomolar)){
if (!is_in_closed_range(static_cast<CoolPropDbl>(closest_state.rhomolar), static_cast<CoolPropDbl>(0.0), rhomolar)){
throw ValueError("out of range");
}
HEOS.update_DmolarT_direct(rhomolar, HEOS._T);
Expand All @@ -64,7 +64,7 @@ void FlashRoutines::PT_flash_mixtures(HelmholtzEOSMixtureBackend &HEOS)
// If that fails, try a bounded solver
CoolPropDbl rhomolar = Brent(resid, closest_state.rhomolar, 1e-10, DBL_EPSILON, 1e-10, 100, errstr);
// Make sure the solution is within the bounds
if (!is_in_closed_range(static_cast<CoolPropDbl>(closest_state.rhomolar), 0.0L, rhomolar)){
if (!is_in_closed_range(static_cast<CoolPropDbl>(closest_state.rhomolar), static_cast<CoolPropDbl>(0.0), rhomolar)){
throw ValueError("out of range");
}
}
Expand All @@ -85,7 +85,7 @@ void FlashRoutines::PT_flash_mixtures(HelmholtzEOSMixtureBackend &HEOS)
}

// Use Rachford-Rice to check whether you are in a homogeneous phase
CoolPropDbl g_RR_0 = g_RachfordRice(HEOS.get_mole_fractions(), lnK, 0.0L);
CoolPropDbl g_RR_0 = g_RachfordRice(HEOS.get_mole_fractions(), lnK, static_cast<CoolPropDbl>(0.0));
if (g_RR_0 < 0){
// Subcooled liquid - done
CoolPropDbl rhomolar_guess = HEOS.solver_rho_Tp_SRK(HEOS._T, HEOS._p, iphase_liquid);
Expand All @@ -95,7 +95,7 @@ void FlashRoutines::PT_flash_mixtures(HelmholtzEOSMixtureBackend &HEOS)
return;
}
else{
CoolPropDbl g_RR_1 = g_RachfordRice(HEOS.get_mole_fractions(), lnK, 1.0L);
CoolPropDbl g_RR_1 = g_RachfordRice(HEOS.get_mole_fractions(), lnK, static_cast<CoolPropDbl>(1.0));
if (g_RR_1 > 0){
// Superheated vapor - done
CoolPropDbl rhomolar_guess = HEOS.solver_rho_Tp_SRK(HEOS._T, HEOS._p, iphase_gas);
Expand Down
2 changes: 1 addition & 1 deletion src/Backends/Tabular/TabularBackends.h
Expand Up @@ -887,7 +887,7 @@ class TabularBackend : public AbstractState
void update(CoolProp::input_pairs input_pair, double Value1, double Value2);
void set_mole_fractions(const std::vector<CoolPropDbl> &mole_fractions){this->AS->set_mole_fractions(mole_fractions);};
void set_mass_fractions(const std::vector<CoolPropDbl> &mass_fractions){ throw NotImplementedError("set_mass_fractions not implemented for Tabular backends"); };
const std::vector<long double> & get_mole_fractions(){return AS->get_mole_fractions();};
const std::vector<CoolPropDbl> & get_mole_fractions(){return AS->get_mole_fractions();};
const std::vector<CoolPropDbl> calc_mass_fractions(void){ return AS->get_mass_fractions(); };

CoolPropDbl calc_molar_mass(void){return AS->molar_mass();};
Expand Down

0 comments on commit 74e36ac

Please sign in to comment.