Skip to content

Commit

Permalink
Merge pull request #529 from mikekaganski/master
Browse files Browse the repository at this point in the history
First bunch of Coverity Scan static analysis warning fixes
  • Loading branch information
ibell committed Mar 9, 2015
2 parents 7a37595 + 3fcca01 commit ea21408
Show file tree
Hide file tree
Showing 25 changed files with 178 additions and 298 deletions.
2 changes: 1 addition & 1 deletion include/AbstractState.h
Expand Up @@ -316,7 +316,7 @@ class AbstractState {

public:

AbstractState(){clear();};
AbstractState():_fluid_type(FLUID_TYPE_UNDEFINED),_phase(iphase_unknown),_rhospline(-_HUGE),_dsplinedp(-_HUGE),_dsplinedh(-_HUGE){clear();}
virtual ~AbstractState(){};

/// A factory function to return a pointer to a new-allocated instance of one of the backends.
Expand Down
16 changes: 11 additions & 5 deletions include/Ancillaries.h
Expand Up @@ -30,7 +30,7 @@ class SurfaceTensionCorrelation
std::size_t N;

std::string BibTeX;
SurfaceTensionCorrelation(){};
SurfaceTensionCorrelation():Tc(_HUGE),N(0){}
SurfaceTensionCorrelation(rapidjson::Value &json_code)
{
a = cpjson::get_long_double_array(json_code["a"]);
Expand Down Expand Up @@ -82,15 +82,21 @@ class SaturationAncillaryFunction
private:
Eigen::MatrixXd num_coeffs, ///< Coefficients for numerator in rational polynomial
den_coeffs; ///< Coefficients for denominator in rational polynomial
std::vector<double> n, t, s;
bool using_tau_r;
CoolPropDbl Tmax, Tmin, reducing_value, T_r, max_abs_error;
std::vector<double> n, t, s; // For TYPE_NOT_EXPONENTIAL & TYPE_EXPONENTIAL
union{
CoolPropDbl max_abs_error; // For TYPE_RATIONAL_POLYNOMIAL
struct{ // For TYPE_NOT_EXPONENTIAL & TYPE_EXPONENTIAL
bool using_tau_r;
CoolPropDbl reducing_value, T_r;
std::size_t N;
};
};
CoolPropDbl Tmax, Tmin;
enum ancillaryfunctiontypes{TYPE_NOT_SET = 0,
TYPE_NOT_EXPONENTIAL,
TYPE_EXPONENTIAL,
TYPE_RATIONAL_POLYNOMIAL};
ancillaryfunctiontypes type;
std::size_t N;
public:

SaturationAncillaryFunction(){type = TYPE_NOT_SET;};
Expand Down
11 changes: 11 additions & 0 deletions include/CPmsgpack.h
@@ -0,0 +1,11 @@
// Workaround MSVC warnings
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4267)
#endif

#include "msgpack.hpp"

#ifdef _MSC_VER
#pragma warning(pop)
#endif
25 changes: 15 additions & 10 deletions include/Configuration.h
Expand Up @@ -45,13 +45,6 @@ std::string config_key_to_string(configuration_keys keys);
class ConfigurationItem
{
public:
enum ConfigurationDataTypes {CONFIGURATION_NOT_DEFINED_TYPE = 0,
CONFIGURATION_BOOL_TYPE,
CONFIGURATION_DOUBLE_TYPE,
CONFIGURATION_INTEGER_TYPE,
CONFIGURATION_STRING_TYPE,
CONFIGURATION_ENDOFLIST_TYPE};
ConfigurationDataTypes type;

/// Cast to boolean
operator bool() const { check_data_type(CONFIGURATION_BOOL_TYPE); return v_bool; };
Expand Down Expand Up @@ -149,14 +142,26 @@ class ConfigurationItem
#endif // !defined(SWIG)

protected:
enum ConfigurationDataTypes {
CONFIGURATION_NOT_DEFINED_TYPE = 0,
CONFIGURATION_BOOL_TYPE,
CONFIGURATION_DOUBLE_TYPE,
CONFIGURATION_INTEGER_TYPE,
CONFIGURATION_STRING_TYPE,
CONFIGURATION_ENDOFLIST_TYPE
};
void check_data_type(ConfigurationDataTypes type) const {
if (type != this->type){
throw ValueError(format("type does not match"));
}
};
double v_double;
bool v_bool;
int v_integer;
ConfigurationDataTypes type;
union {
double v_double;
bool v_bool;
int v_integer;

};
std::string v_string;
configuration_keys key;
};
Expand Down
19 changes: 10 additions & 9 deletions include/CoolPropFluid.h
Expand Up @@ -300,13 +300,13 @@ class TransportPropertyData
epsilon_over_k; ///< The Lennard-Jones 12-6 \f$ \varepsilon/k \f$ parameter
ViscosityHardcodedEnum hardcoded_viscosity; ///< Hardcoded flags for the viscosity
ConductivityHardcodedEnum hardcoded_conductivity; ///< Hardcoded flags for the conductivity
TransportPropertyData(){hardcoded_viscosity = VISCOSITY_NOT_HARDCODED;
hardcoded_conductivity = CONDUCTIVITY_NOT_HARDCODED;
viscosity_using_ECS = false;
conductivity_using_ECS = false;
viscosity_model_provided = false;
conductivity_model_provided = false;
};
TransportPropertyData():hardcoded_viscosity(VISCOSITY_NOT_HARDCODED),
hardcoded_conductivity(CONDUCTIVITY_NOT_HARDCODED),
viscosity_using_ECS(false),
conductivity_using_ECS(false),
viscosity_model_provided(false),
conductivity_model_provided(false),
sigma_eta(_HUGE),epsilon_over_k(_HUGE){}
};

struct Ancillaries
Expand Down Expand Up @@ -452,9 +452,10 @@ class CoolPropFluid {
std::string ECSReferenceFluid; ///< A string that gives the name of the fluids that should be used for the ECS method for transport properties
double ECS_qd; ///< The critical qd parameter for the Olchowy-Sengers cross-over term
public:
CoolPropFluid(){};
CoolPropFluid():ECS_qd(-_HUGE){};
~CoolPropFluid(){};
EquationOfState &EOS() {return EOSVector[0];}; ///< Get a reference to the equation of state
const EquationOfState &EOS() const {return EOSVector[0];} ///< Get a reference to the equation of state
EquationOfState &EOS() {return EOSVector[0];} ///< Get a reference to the equation of state
std::vector<EquationOfState> EOSVector; ///< The equations of state that could be used for this fluid

std::string name; ///< The name of the fluid
Expand Down
2 changes: 1 addition & 1 deletion include/CoolPropTools.h
Expand Up @@ -579,7 +579,7 @@ template<class T> void normalize_vector(std::vector<T> &x)
std::vector<double> B;
public:
double a,b,c,d;
SplineClass();
SplineClass():Nconstraints(0),A(4, std::vector<double>(4, 0)),B(4,0),a(_HUGE),b(_HUGE),c(_HUGE),d(_HUGE){}
bool build(void);
bool add_value_constraint(double x, double y);
void add_4value_constraints(double x1, double x2, double x3, double x4, double y1, double y2, double y3, double y4);
Expand Down
2 changes: 1 addition & 1 deletion include/PolyMath.h
Expand Up @@ -164,7 +164,7 @@ class Polynomial2D {
};


class Poly2DResidual : public FuncWrapper1D {
class Poly2DResidual : public FuncWrapper1DWithDeriv {
protected:
enum dims {iX, iY};
Eigen::MatrixXd coefficients;
Expand Down
27 changes: 19 additions & 8 deletions include/Solvers.h
Expand Up @@ -11,10 +11,14 @@ namespace CoolProp
class FuncWrapper1D
{
public:
FuncWrapper1D(){};
virtual ~FuncWrapper1D(){};
virtual double call(double) = 0;
virtual double deriv(double){throw NotImplementedError("deriv function not implemented");};
};

class FuncWrapper1DWithDeriv : public FuncWrapper1D
{
public:
virtual double deriv(double) = 0;
};

class FuncWrapperND
Expand All @@ -30,14 +34,21 @@ class FuncWrapperND
double Brent(FuncWrapper1D* f, double a, double b, double macheps, double t, int maxiter, std::string &errstr);
double Secant(FuncWrapper1D* f, double x0, double dx, double ftol, int maxiter, std::string &errstring);
double BoundedSecant(FuncWrapper1D* f, double x0, double xmin, double xmax, double dx, double ftol, int maxiter, std::string &errstring);
double Newton(FuncWrapper1D* f, double x0, double ftol, int maxiter, std::string &errstring);
double Newton(FuncWrapper1DWithDeriv* f, double x0, double ftol, int maxiter, std::string &errstring);

// Single-Dimensional solvers
double Brent(FuncWrapper1D &f, double a, double b, double macheps, double t, int maxiter, std::string &errstr);
double Secant(FuncWrapper1D &f, double x0, double dx, double ftol, int maxiter, std::string &errstring);
double BoundedSecant(FuncWrapper1D &f, double x0, double xmin, double xmax, double dx, double ftol, int maxiter, std::string &errstring);
double Newton(FuncWrapper1D &f, double x0, double ftol, int maxiter, std::string &errstring);

inline double Brent(FuncWrapper1D &f, double a, double b, double macheps, double t, int maxiter, std::string &errstr){
return Brent(&f, a, b, macheps, t, maxiter, errstr);
}
inline double Secant(FuncWrapper1D &f, double x0, double dx, double ftol, int maxiter, std::string &errstring){
return Secant(&f, x0, dx, ftol, maxiter, errstring);
}
inline double BoundedSecant(FuncWrapper1D &f, double x0, double xmin, double xmax, double dx, double ftol, int maxiter, std::string &errstring){
return BoundedSecant(&f, x0, xmin, xmax, dx, ftol, maxiter, errstring);
}
inline double Newton(FuncWrapper1DWithDeriv &f, double x0, double ftol, int maxiter, std::string &errstring){
return Newton(&f, x0, ftol, maxiter, errstring);
}

// Multi-Dimensional solvers
std::vector<double> NDNewtonRaphson_Jacobian(FuncWrapperND *f, const std::vector<double> &x0, double tol, int maxiter, std::string *errstring);
Expand Down
64 changes: 33 additions & 31 deletions src/Backends/Helmholtz/FlashRoutines.cpp
Expand Up @@ -251,8 +251,7 @@ void FlashRoutines::QT_flash(HelmholtzEOSMixtureBackend &HEOS)
else if (!(HEOS.components[0].EOS().pseudo_pure))
{
// Set some imput options
SaturationSolvers::saturation_T_pure_Akasaka_options options;
options.use_guesses = false;
SaturationSolvers::saturation_T_pure_Akasaka_options options(false);

// Actually call the solver
SaturationSolvers::saturation_T_pure_Maxwell(HEOS, HEOS._T, options);
Expand Down Expand Up @@ -506,7 +505,11 @@ void FlashRoutines::PT_Q_flash_mixtures(HelmholtzEOSMixtureBackend &HEOS, parame

// Shift the solution if needed to ensure that imax+2 and imax-1 are both in range
if (imax+2 >= env.T.size()){ imax--; }
else if (imax-1 < 0){ imax++; }
else if (imax == 0){ imax++; }
// Here imax+2 or imax-1 is still possibly out of range:
// 1. If imax initially is 1, and env.T.size() <= 3, then imax will become 0.
// 2. If imax initially is 0, and env.T.size() <= 2, then imax will become MAX_UINT.
// 3. If imax+2 initially is more than env.T.size(), then single decrement will not bring it to range

SaturationSolvers::newton_raphson_saturation NR;
SaturationSolvers::newton_raphson_saturation_options IO;
Expand All @@ -525,6 +528,9 @@ void FlashRoutines::PT_Q_flash_mixtures(HelmholtzEOSMixtureBackend &HEOS, parame
IO.rhomolar_vap = CubicInterp(env.T, env.rhomolar_vap, imax-1, imax, imax+1, imax+2, static_cast<CoolPropDbl>(IO.T));
IO.p = CubicInterp(env.rhomolar_vap, env.p, imax-1, imax, imax+1, imax+2, IO.rhomolar_vap);
}
else{
throw ValueError();
}
IO.rhomolar_liq = CubicInterp(env.rhomolar_vap, env.rhomolar_liq, imax-1, imax, imax+1, imax+2, IO.rhomolar_vap);

if (quality == SATURATED_VAPOR){
Expand Down Expand Up @@ -657,7 +663,7 @@ void FlashRoutines::HSU_D_flash_twophase(HelmholtzEOSMixtureBackend &HEOS, CoolP

public:
HelmholtzEOSMixtureBackend &HEOS;
CoolPropDbl rhomolar_spec, Qd, Qo;
CoolPropDbl rhomolar_spec;
parameters other;
CoolPropDbl value;
Residual(HelmholtzEOSMixtureBackend &HEOS, CoolPropDbl rhomolar_spec, parameters other, CoolPropDbl value) : HEOS(HEOS), rhomolar_spec(rhomolar_spec), other(other), value(value){};
Expand All @@ -666,9 +672,9 @@ void FlashRoutines::HSU_D_flash_twophase(HelmholtzEOSMixtureBackend &HEOS, CoolP
HelmholtzEOSMixtureBackend &SatL = HEOS.get_SatL(),
&SatV = HEOS.get_SatV();
// Quality from density
Qd = (1/HEOS.rhomolar()-1/SatL.rhomolar())/(1/SatV.rhomolar()-1/SatL.rhomolar());
CoolPropDbl Qd = (1 / HEOS.rhomolar() - 1 / SatL.rhomolar()) / (1 / SatV.rhomolar() - 1 / SatL.rhomolar());
// Quality from other parameter (H,S,U)
Qo = (value-SatL.keyed_output(other))/(SatV.keyed_output(other)-SatL.keyed_output(other));
CoolPropDbl Qo = (value - SatL.keyed_output(other)) / (SatV.keyed_output(other) - SatL.keyed_output(other));
// Residual is the difference between the two
return Qo-Qd;
}
Expand All @@ -694,12 +700,12 @@ void FlashRoutines::PHSU_D_flash(HelmholtzEOSMixtureBackend &HEOS, parameters ot
public:

HelmholtzEOSMixtureBackend *HEOS;
CoolPropDbl r, eos, rhomolar, value, T;
CoolPropDbl rhomolar, value;
int other;

solver_resid(HelmholtzEOSMixtureBackend *HEOS, CoolPropDbl rhomolar, CoolPropDbl value, int other) : HEOS(HEOS), rhomolar(rhomolar), value(value), other(other){};
double call(double T){
this->T = T;
CoolPropDbl eos;
switch(other)
{
case iP:
Expand All @@ -714,8 +720,7 @@ void FlashRoutines::PHSU_D_flash(HelmholtzEOSMixtureBackend &HEOS, parameters ot
throw ValueError(format("Input not supported"));
}

r = eos - value;
return r;
return eos - value;
};
};

Expand Down Expand Up @@ -992,45 +997,42 @@ void FlashRoutines::HSU_P_flash_singlephase_Brent(HelmholtzEOSMixtureBackend &HE
public:

HelmholtzEOSMixtureBackend *HEOS;
CoolPropDbl r, eos, p, value, T, rhomolar;
CoolPropDbl p, value;
int other;
int iter;
CoolPropDbl r0, r1, T1, T0, eos0, eos1, pp;
CoolPropDbl eos0, eos1;
solver_resid(HelmholtzEOSMixtureBackend *HEOS, CoolPropDbl p, CoolPropDbl value, int other) :
HEOS(HEOS), p(p), value(value), other(other)
HEOS(HEOS), p(p), value(value), other(other), iter(0), eos0(-_HUGE), eos1(-_HUGE)
{
iter = 0;
// Specify the state to avoid saturation calls, but only if phase is subcritical
if (HEOS->phase() == iphase_liquid || HEOS->phase() == iphase_gas ){
HEOS->specify_phase(HEOS->phase());
switch (CoolProp::phases phase = HEOS->phase()) {
case iphase_liquid: case iphase_gas:
HEOS->specify_phase(phase);
}
};
}
double call(double T){

this->T = T;

// Run the solver with T,P as inputs;
HEOS->update(PT_INPUTS, p, T);

rhomolar = HEOS->rhomolar();
CoolPropDbl rhomolar = HEOS->rhomolar();
HEOS->update(DmolarT_INPUTS, rhomolar, T);
// Get the value of the desired variable
eos = HEOS->keyed_output(other);
pp = HEOS->p();
CoolPropDbl eos = HEOS->keyed_output(other);

// Difference between the two is to be driven to zero
r = eos - value;
CoolPropDbl r = eos - value;

// Store values for later use if there are errors
if (iter == 0){
r0 = r; T0 = T; eos0 = eos;
eos0 = eos;
}
else if (iter == 1){
r1 = r; T1 = T; eos1 = eos;
eos1 = eos;
}
else{
r0 = r1; T0 = T1; eos0 = eos1;
r1 = r; T1 = T; eos1 = eos;
eos0 = eos1;
eos1 = eos;
}

iter++;
Expand Down Expand Up @@ -1237,16 +1239,16 @@ void FlashRoutines::HS_flash_twophase(HelmholtzEOSMixtureBackend &HEOS, CoolProp

public:
HelmholtzEOSMixtureBackend &HEOS;
CoolPropDbl hmolar, smolar, Qs, Qh;
CoolPropDbl hmolar, smolar;
Residual(HelmholtzEOSMixtureBackend &HEOS, CoolPropDbl hmolar_spec, CoolPropDbl smolar_spec) : HEOS(HEOS), hmolar(hmolar_spec), smolar(smolar_spec){};
double call(double T){
HEOS.update(QT_INPUTS, 0, T);
HelmholtzEOSMixtureBackend &SatL = HEOS.get_SatL(),
&SatV = HEOS.get_SatV();
// Quality from entropy
Qs = (smolar-SatL.smolar())/(SatV.smolar()-SatL.smolar());
CoolPropDbl Qs = (smolar-SatL.smolar())/(SatV.smolar()-SatL.smolar());
// Quality from enthalpy
Qh = (hmolar-SatL.hmolar())/(SatV.hmolar()-SatL.hmolar());
CoolPropDbl Qh = (hmolar-SatL.hmolar())/(SatV.hmolar()-SatL.hmolar());
// Residual is the difference between the two
return Qh-Qs;
}
Expand Down

0 comments on commit ea21408

Please sign in to comment.