def test_set_Tx(self): """Set a pair of properties of the State and check the properties. Also works as a functional/regression test of CoolProp. """ s = State(substance="water") s.Tx = Q_(400.0, "K"), Q_(0.5, "dimensionless") assert isclose_quant(s.T, Q_(400.0, "K")) assert isclose_quant(s.p, Q_(245769.34557103913, "Pa")) assert isclose_quant(s.Tx[0], Q_(400.0, "K")) assert isclose_quant(s.Tx[1], Q_(0.5, "dimensionless")) assert isclose_quant(s.u, Q_(1534461.5163075812, "J/kg")) assert isclose_quant(s.s, Q_(4329.703956664546, "J/(kg*K)")) assert isclose_quant(s.cp, Q_(4056.471547685226, "J/(kg*K)")) assert isclose_quant(s.cv, Q_(2913.7307270395363, "J/(kg*K)")) assert isclose_quant(s.v, Q_(0.3656547423394701, "m**3/kg")) assert isclose_quant(s.h, Q_(1624328.2430353598, "J/kg")) assert isclose_quant(s.x, Q_(0.5, "dimensionless")) s.Tx = Q_(400.0, "K"), Q_(50, "percent") assert isclose_quant(s.T, Q_(400.0, "K")) assert isclose_quant(s.p, Q_(245769.34557103913, "Pa")) assert isclose_quant(s.Tx[0], Q_(400.0, "K")) assert isclose_quant(s.Tx[1], Q_(0.5, "dimensionless")) assert isclose_quant(s.u, Q_(1534461.5163075812, "J/kg")) assert isclose_quant(s.s, Q_(4329.703956664546, "J/(kg*K)")) assert isclose_quant(s.cp, Q_(4056.471547685226, "J/(kg*K)")) assert isclose_quant(s.cv, Q_(2913.7307270395363, "J/(kg*K)")) assert isclose_quant(s.v, Q_(0.3656547423394701, "m**3/kg")) assert isclose_quant(s.h, Q_(1624328.2430353598, "J/kg")) assert isclose_quant(s.x, Q_(0.5, "dimensionless"))
def test_set_hp(self): """Set a pair of properties of the State and check the properties. Also works as a functional/regression test of CoolProp. """ s = State(substance="water") s.hp = Q_(1061602.391543017, "J/kg"), Q_(101325.0, "Pa") assert isclose_quant(s.T, Q_(373.1242958476843, "K")) assert isclose_quant(s.p, Q_(101325.0, "Pa")) assert isclose_quant(s.hp[0], Q_(1061602.391543017, "J/kg")) assert isclose_quant(s.hp[1], Q_(101325.0, "Pa")) assert isclose_quant(s.u, Q_(1013250, "J/kg")) assert isclose_quant(s.s, Q_(3028.9867985920914, "J/(kg*K)")) assert isclose_quant(s.v, Q_(0.4772010021515822, "m**3/kg")) assert isclose_quant(s.h, Q_(1061602.391543017, "J/kg")) assert isclose_quant(s.x, Q_(0.28475636946248034, "dimensionless")) s.hp = Q_(3336406.139862406, "J/kg"), Q_(101325.0, "Pa") assert isclose_quant(s.T, Q_(700.9882316847855, "K")) assert isclose_quant(s.p, Q_(101325.0, "Pa")) assert isclose_quant(s.hp[0], Q_(3336406.139862406, "J/kg")) assert isclose_quant(s.hp[1], Q_(101325.0, "Pa")) assert isclose_quant(s.u, Q_(3013250, "J/kg")) assert isclose_quant(s.s, Q_(8623.283568815832, "J/(kg*K)")) assert isclose_quant(s.v, Q_(3.189303132125469, "m**3/kg")) assert isclose_quant(s.h, Q_(3336406.139862406, "J/kg")) assert s.x is None
def test_eq(self): """Test equality comparison of states. States are equal when their properties are equal and the substances are the same. """ st_1 = State(substance="water", T=Q_(400.0, "K"), p=Q_(101325.0, "Pa")) st_2 = State(substance="water", T=Q_(400.0, "K"), p=Q_(101325.0, "Pa")) assert st_1 == st_2
def test_comparison(self): """Greater/less than comparisons are not supported.""" st_1 = State(substance="water", T=Q_(400.0, "K"), p=Q_(101325.0, "Pa")) st_2 = State(substance="water", T=Q_(400.0, "K"), p=Q_(101325.0, "Pa")) with pytest.raises(TypeError): st_1 < st_2 with pytest.raises(TypeError): st_1 <= st_2 with pytest.raises(TypeError): st_1 > st_2 with pytest.raises(TypeError): st_1 >= st_2
def test_set_properties_too_many_props_and_values(self): """Passing too many properties to _set_properties raises a StateError.""" s = State(substance="water") with pytest.raises(StateError): s._set_properties( ["T", "v", "h"], [ Q_(373.1242958476844, "K"), Q_(1.801983936953226, "m**3/kg"), Q_(2730301.3859201893, "J/kg"), ], )
def test_lowercase_input(self): """Substances should be able to be specified with lowercase letters.""" State(substance="water") State(substance="r22") State(substance="r134a") State(substance="ammonia") State(substance="propane") State(substance="air") State(substance="isobutane") State(substance="carbondioxide") State(substance="oxygen") State(substance="nitrogen")
def test_set_hv(self): """Set a pair of properties of the State and check the properties. Also works as a functional/regression test of CoolProp. """ s = State(substance="water") s.hv = Q_(1061602.391543017, "J/kg"), Q_(0.4772010021515822, "m**3/kg") assert isclose_quant(s.T, Q_(373.1242958476843, "K")) assert isclose_quant(s.p, Q_(101325.0, "Pa")) assert isclose_quant(s.hv[0], Q_(1061602.391543017, "J/kg")) assert isclose_quant(s.hv[1], Q_(0.4772010021515822, "m**3/kg")) assert isclose_quant(s.u, Q_(1013250, "J/kg")) assert isclose_quant(s.s, Q_(3028.9867985920914, "J/(kg*K)")) assert isclose_quant(s.v, Q_(0.4772010021515822, "m**3/kg")) assert isclose_quant(s.h, Q_(1061602.391543017, "J/kg")) assert isclose_quant(s.x, Q_(0.28475636946248034, "dimensionless"))
def test_set_Tv(self): """Set a pair of properties of the State and check the properties. Also works as a functional/regression test of CoolProp. """ s = State(substance="water") s.Tv = Q_(400.0, "K"), Q_(1.801983936953226, "m**3/kg") assert isclose_quant(s.T, Q_(400.0, "K")) assert isclose_quant(s.p, Q_(101325.0, "Pa")) assert isclose_quant(s.Tv[0], Q_(400.0, "K")) assert isclose_quant(s.Tv[1], Q_(1.801983936953226, "m**3/kg")) assert isclose_quant(s.u, Q_(2547715.3635084038, "J/kg")) assert isclose_quant(s.s, Q_(7496.2021523754065, "J/(kg*K)")) assert isclose_quant(s.cp, Q_(2009.2902478486988, "J/(kg*K)")) assert isclose_quant(s.cv, Q_(1509.1482452129906, "J/(kg*K)")) assert isclose_quant(s.v, Q_(1.801983936953226, "m**3/kg")) assert isclose_quant(s.h, Q_(2730301.3859201893, "J/kg")) assert s.x is None
def test_bad_dimensions(self, prop): """Setting bad dimensions for the input property raises a StateError.""" kwargs = {prop: Q_(1.0, "dimensionless")} if prop == "v": kwargs["T"] = Q_(300.0, "K") else: kwargs["v"] = Q_(1.0, "m**3/kg") with pytest.raises(StateError): State(substance="water", **kwargs)
def test_too_many_props(self): """Specifying too many properties should raise a ValueError.""" with pytest.raises(ValueError): State( substance="water", T=Q_(300, "K"), p=Q_(101325, "Pa"), u=Q_(100, "kJ/kg"), )
def test_bad_x_dimensions(self): """Setting bad dimensions for quality raises a StateError. Must be done in a separate test because the "dimensionless" sentinel value used in the other test is actually the correct dimension for quality. """ with pytest.raises(StateError): State(substance="water", T=Q_(300.0, "K"), x=Q_(1.01325, "K"))
def test_too_few_props(self): """Specifying too few properties should raise a value error.""" with pytest.raises(ValueError): State(substance="water", T=Q_(300, "K"))
def test_quality_gt_one(self): """Vapor qualities greater than 1.0 should raise a StateError.""" with pytest.raises(StateError): State(substance="water", x=Q_(2.0, "dimensionless"), p=Q_(101325, "Pa"))
def test_bad_substance(self): """A substance not in the approved list should raise a ValueError.""" with pytest.raises(ValueError): State(substance="bad substance")
def test_TP_twophase(self): """Setting a two-phase mixture with T and p should raise a StateError.""" with pytest.raises(StateError): State(substance="water", T=Q_(373.1242958476844, "K"), p=Q_(101325.0, "Pa"))
def test_unit_definitions(self): """All of the properties should have units defined.""" st = State("water") props = st._all_props[:] props.extend(st._read_only_props) assert all([a in st._SI_units.keys() for a in props])
def test_negative_pressure(self): """Negative absolute pressures should raise a StateError.""" with pytest.raises(StateError): State(substance="water", T=Q_(300, "K"), p=Q_(-101325, "Pa"))
def test_quality_lt_zero(self): """Vapor qualities less than 0.0 should raise a StateError.""" with pytest.raises(StateError): State(substance="water", x=Q_(-1.0, "dimensionless"), p=Q_(101325, "Pa"))
def test_not_eq_sub(self): """States are not equal when substances are not the same.""" st_1 = State(substance="water", T=Q_(400.0, "K"), p=Q_(101325.0, "Pa")) st_2 = State(substance="ammonia", T=Q_(400.0, "K"), p=Q_(101325.0, "Pa")) assert not st_1 == st_2
def test_not_eq(self): """States are not equal when properties are not equal.""" st_1 = State(substance="water", T=Q_(400.0, "K"), p=Q_(101325.0, "Pa")) st_2 = State(substance="water", T=Q_(300.0, "K"), p=Q_(101325.0, "Pa")) assert not st_1 == st_2
def test_bad_property_setting(self): """Regression test that pressure is lowercase p, not uppercase.""" s = State(substance="water") with pytest.raises(AttributeError): # Should be lowercase p s.TP = Q_(400.0, "K"), Q_(101325.0, "Pa")
def test_bad_get_property(self): """Accessing attributes that aren't one of the properties or pairs raises.""" s = State(substance="water", T=Q_(400.0, "K"), p=Q_(101325.0, "Pa")) with pytest.raises(AttributeError): s.bad_get
def test_negative_volume(self): """Negative absolute specific volumes should raise a StateError.""" with pytest.raises(StateError): State(substance="water", T=Q_(300, "K"), v=Q_(-10.13, "m**3/kg"))
def test_invalid_input_prop(self): """Invalid input properties should raise a ValueError.""" with pytest.raises(ValueError): State( substance="water", x=Q_(0.5, "dimensionless"), bad_prop=Q_(101325, "Pa") )