def test_VaporPressure_fitting11_bad_method(): Ts = [ 203.65, 209.55, 212.45, 234.05, 237.04, 243.25, 249.35, 253.34, 257.25, 262.12, 264.5, 267.05, 268.95, 269.74, 272.95, 273.46, 275.97, 276.61, 277.23, 282.03, 283.06, 288.94, 291.49, 293.15, 293.15, 293.85, 294.25, 294.45, 294.6, 294.63, 294.85, 297.05, 297.45, 298.15, 298.15, 298.15, 298.15, 298.15, 299.86, 300.75, 301.35, 303.15, 303.15, 304.35, 304.85, 305.45, 306.25, 308.15, 308.15, 308.15, 308.22, 308.35, 308.45, 308.85, 309.05, 311.65, 311.85, 311.85, 311.95, 312.25, 314.68, 314.85, 317.75, 317.85, 318.05, 318.15, 318.66, 320.35, 320.35, 320.45, 320.65, 322.55, 322.65, 322.85, 322.95, 322.95, 323.35, 323.55, 324.65, 324.75, 324.85, 324.85, 325.15, 327.05, 327.15, 327.2, 327.25, 327.35, 328.22, 328.75, 328.85, 333.73, 338.95 ] Psats = [ 58.93, 94.4, 118.52, 797.1, 996.5, 1581.2, 2365, 3480, 3893, 5182, 6041, 6853, 7442, 7935, 9290, 9639, 10983, 11283, 13014, 14775, 15559, 20364, 22883, 24478, 24598, 25131, 25665, 25931, 25998, 26079, 26264, 29064, 29598, 30397, 30544, 30611, 30784, 30851, 32636, 33931, 34864, 37637, 37824, 39330, 40130, 41063, 42396, 45996, 46090, 46356, 45462, 46263, 46396, 47129, 47396, 52996, 52929, 53262, 53062, 53796, 58169, 59328, 66395, 66461, 67461, 67661, 67424, 72927, 73127, 73061, 73927, 79127, 79527, 80393, 79927, 80127, 81993, 80175, 85393, 85660, 85993, 86260, 86660, 92726, 92992, 92992, 93126, 93326, 94366, 98325, 98592, 113737, 136626 ] with pytest.raises(ValueError): TDependentProperty.fit_data_to_model(Ts=Ts, data=Psats, model='NOTAMETHOD')
def test_VaporPressure_fitting0(): obj = VaporPressure(CASRN='13838-16-9') Tmin, Tmax = obj.WAGNER_POLING_Tmin, obj.WAGNER_POLING_Tmax Ts = linspace(Tmin, Tmax, 10) Ps = [obj(T) for T in Ts] Tc, Pc = obj.WAGNER_POLING_Tc, obj.WAGNER_POLING_Pc fitted = obj.fit_data_to_model(Ts=Ts, data=Ps, model='Wagner', use_numba=False, model_kwargs={ 'Tc': obj.WAGNER_POLING_Tc, 'Pc': obj.WAGNER_POLING_Pc }) res = fitted assert 'Tc' in res assert 'Pc' in res assert_close(res['a'], obj.WAGNER_POLING_coefs[0]) assert_close(res['b'], obj.WAGNER_POLING_coefs[1]) assert_close(res['c'], obj.WAGNER_POLING_coefs[2]) assert_close(res['d'], obj.WAGNER_POLING_coefs[3]) # Heavy compound fit Ts = linspace(179.15, 237.15, 5) props_calc = [Antoine(T, A=138., B=520200.0, C=3670.0) for T in Ts] res, stats = TDependentProperty.fit_data_to_model( Ts=Ts, data=props_calc, model='Antoine', do_statistics=True, use_numba=False, model_kwargs={'base': 10.0}, fit_method='lm') assert stats['MAE'] < 1e-5 # Fit with very low range and no C Ts = linspace(374, 377.0, 5) props_calc = [Antoine(T, A=12.852103, B=2942.98, C=0.0) for T in Ts] res, stats = TDependentProperty.fit_data_to_model( Ts=Ts, data=props_calc, model='Antoine', do_statistics=True, use_numba=False, model_kwargs={'base': 10.0}, fit_method='lm') assert stats['MAE'] < 1e-5
def test_many_local_methods(): obj = TDependentProperty(extrapolation='linear') M1_value = 2.0 M2_value = 3.0 obj.add_method(M1_value, name='M1') obj.add_method(M2_value, name='M2') obj.method = 'M1' assert_close(M1_value, obj.T_dependent_property(300.)) obj.method = 'M2' assert_close(M2_value, obj.T_dependent_property(300.))
def test_derivative(): obj = TDependentProperty(extrapolation='linear') Tmin = 300 Tmax = 400 f = lambda T: T*T*T f_der = lambda T: 3*T*T f_der2 = lambda T: 6*T f_der3 = lambda T: 6 obj.add_method(f=f, f_der=f_der, f_der2=f_der2, f_der3=f_der3, Tmin=Tmin, Tmax=Tmax) T_in_range = (Tmin + Tmax) * 0.5 for order, fun in zip([1,2,3], [f_der, f_der2, f_der3]): # Within valid T range assert_close(obj.T_dependent_property_derivative(T_in_range, order=order), fun(T_in_range)) # Extrapolate left T = Tmin - 50 obj.RAISE_PROPERTY_CALCULATION_ERROR = True assert_close(obj.T_dependent_property_derivative(T, order=1), f_der(Tmin)) # Extrapolate right T = Tmax + 50 assert_close(obj.T_dependent_property_derivative(T, order=1), f_der(Tmax))
def test_integrals(): obj = TDependentProperty(extrapolation='constant') Tmin = 300 Tmax = 400 f = lambda T: T + 10. f_int = lambda T1, T2: (T2*T2 - T1*T1) * 0.5 + 10 * (T2 - T1) f_int_over_T = lambda T1, T2: T2 - T1 + 10 * log(T2/T1) obj.add_method(f=f, f_int=f_int, f_int_over_T=f_int_over_T, Tmin=Tmin, Tmax=Tmax) # Within valid T range T1 = 300. T2 = 310. dT = T2 - T1 assert_close(obj.T_dependent_property_integral(T1, T2), f_int(T1, T2)) assert_close(obj.T_dependent_property_integral_over_T(T1, T2), f_int_over_T(T1, T2)) # Extrapolate left T1 = Tmin - 50 T2 = Tmin - 25 dT = T2 - T1 constant_low = f(Tmin) assert_close(obj.T_dependent_property_integral(T1, T2), constant_low * dT) assert_close(obj.T_dependent_property_integral_over_T(T1, T2), constant_low * log(T2/T1)) # Extrapolate right T1 = Tmax + 25 T2 = Tmax + 50 dT = T2 - T1 constant_high = f(Tmax) assert_close(obj.T_dependent_property_integral(T1, T2), constant_high * dT) assert_close(obj.T_dependent_property_integral_over_T(T1, T2), constant_high * log(T2/T1)) # Extrapolate left to center (piece-wise) T1 = Tmin - 50 T2 = Tmin + 25 constant_low = f(Tmin) assert_close(obj.T_dependent_property_integral(T1, T2), f_int(Tmin, T2) + constant_low * (Tmin - T1)) assert_close(obj.T_dependent_property_integral_over_T(T1, T2), f_int_over_T(Tmin, T2) + constant_low * log(Tmin/T1)) # Extrapolate right to center (piece-wise) T1 = Tmax - 25 T2 = Tmax + 50 constant_high = f(Tmax) assert_close(obj.T_dependent_property_integral(T1, T2), f_int(T1, Tmax) + constant_high * (T2 - Tmax)) assert_close(obj.T_dependent_property_integral_over_T(T1, T2), f_int_over_T(T1, Tmax) + constant_high * log(T2/Tmax)) # Extrapolate across both sides (piece-wise) T1 = Tmin - 50 T2 = Tmax + 50 constant_low = f(Tmin) constant_high = f(Tmax) assert_close(obj.T_dependent_property_integral(T1, T2), constant_low * (Tmin - T1) + f_int(Tmin, Tmax) + constant_high * (T2 - Tmax)) assert_close(obj.T_dependent_property_integral_over_T(T1, T2), constant_low * log(Tmin/T1) + f_int_over_T(Tmin, Tmax) + constant_high * log(T2/Tmax)) ### Test linear extrapolation obj.extrapolation = 'linear' # Extrapolate left T1 = Tmin - 50 T2 = Tmin - 25 dT = T2 - T1 constant_low = f(Tmin) assert_close(obj.T_dependent_property_integral(T1, T2), f_int(T1, T2)) assert_close(obj.T_dependent_property_integral_over_T(T1, T2), f_int_over_T(T1, T2)) # Extrapolate right T1 = Tmax + 25 T2 = Tmax + 50 dT = T2 - T1 constant_high = f(Tmax) assert_close(obj.T_dependent_property_integral(T1, T2), f_int(T1, T2)) assert_close(obj.T_dependent_property_integral_over_T(T1, T2), f_int_over_T(T1, T2)) # Extrapolate left to center (piece-wise) T1 = Tmin - 50 T2 = Tmin + 25 constant_low = f(Tmin) assert_close(obj.T_dependent_property_integral(T1, T2), f_int(T1, T2)) assert_close(obj.T_dependent_property_integral_over_T(T1, T2), f_int_over_T(T1, T2)) # Extrapolate right to center (piece-wise) T1 = Tmax - 25 T2 = Tmax + 50 constant_high = f(Tmax) assert_close(obj.T_dependent_property_integral(T1, T2), f_int(T1, T2)) assert_close(obj.T_dependent_property_integral_over_T(T1, T2), f_int_over_T(T1, T2)) # Extrapolate across both sides (piece-wise) T1 = Tmin - 50 T2 = Tmax + 50 constant_low = f(Tmin) constant_high = f(Tmax) assert_close(obj.T_dependent_property_integral(T1, T2), f_int(T1, T2)) assert_close(obj.T_dependent_property_integral_over_T(T1, T2), f_int_over_T(T1, T2))
def test_extrapolation(): obj = TDependentProperty(extrapolation='constant') Tmin = 300 Tmax = 400 f = lambda T: T f_int = lambda T1, T2: (T2*T2 - T1*T1) / 2. f_int_over_T = lambda T1, T2: T2 - T1 obj.add_method(f=f, Tmin=Tmin, Tmax=Tmax) with pytest.raises(ValueError): obj.extrapolate(350, obj.method, in_range='error') T_low = Tmin - 100 T_inrange = (Tmin + Tmax) * 0.5 T_high = Tmax + 100 assert_close(Tmin, obj.extrapolate(T_low, obj.method, in_range='error')) assert_close(Tmax, obj.extrapolate(T_high, obj.method, in_range='error')) assert_close(Tmin, obj.extrapolate(T_low, obj.method, in_range='low')) assert_close(Tmin, obj.extrapolate(T_inrange, obj.method, in_range='low')) assert_close(Tmax, obj.extrapolate(T_high, obj.method, in_range='low')) assert_close(Tmin, obj.extrapolate(T_low, obj.method, in_range='high')) assert_close(Tmax, obj.extrapolate(T_inrange, obj.method, in_range='high')) assert_close(Tmax, obj.extrapolate(T_high, obj.method, in_range='high'))
def test_local_constant_method(): # Test user defined method # Within valid T range obj = TDependentProperty(extrapolation='linear') constant = 100. T1 = T = 300. T2 = 310. dT = T2 - T1 obj.add_method(constant) assert_close(obj.T_dependent_property(T), constant) for order in (1, 2, 3): assert_close(obj.T_dependent_property_derivative(T, order), 0.) assert_close(obj.T_dependent_property_integral(T1, T2), constant * dT) assert_close(obj.T_dependent_property_integral_over_T(T1, T2), constant * log(T2 / T1)) # Extrapolate Tmin = 350. Tmax = 400. obj.add_method(constant, Tmin, Tmax) obj.extrapolation = 'constant' assert_close(obj.T_dependent_property(T), constant) for order in (1, 2, 3): assert_close(obj.T_dependent_property_derivative(T, order), 0., atol=1e-6) assert_close(obj.T_dependent_property_integral(T1, T2), constant * dT) assert_close(obj.T_dependent_property_integral_over_T(T1, T2), constant * log(T2 / T1)) # Test extrapolation on both sides assert_close(obj.T_dependent_property_integral(Tmin - 50, Tmax + 50), constant * (100 + Tmax - Tmin)) assert_close(obj.T_dependent_property_integral_over_T(Tmin - 50, Tmax + 50), constant * log((Tmax + 50) / (Tmin - 50))) # Do not allow extrapolation obj.extrapolation = None obj.add_method(constant, Tmin, Tmax) assert obj.T_dependent_property(T) is None
def test_local_method(): # Test user defined method # Within valid T range obj = TDependentProperty(extrapolation='linear') T = 300. Tmin = 200. Tmax = 400. T1 = 300. T2 = 310. dT = T2 - T1 f = lambda T: T*T / 1e6 f_der = lambda T: T / 1e6 f_der2 = lambda T: 1. / 1e6 f_der3 = lambda T: 0. / 1e6 f_int = lambda T1, T2: (T2*T2*T2 - T1*T1*T1) / 3. / 1e6 f_int_over_T = lambda T1, T2: (T2*T2 - T1*T1) / 2. / 1e6 obj.add_method(f, Tmin, Tmax, f_der, f_der2, f_der3, f_int, f_int_over_T) assert_close(obj.T_dependent_property(T), f(T)) assert_close(obj.T_dependent_property_derivative(T, 1), f_der(T)) assert_close(obj.T_dependent_property_derivative(T, 2), f_der2(T)) assert_close(obj.T_dependent_property_derivative(T, 3), f_der3(T)) assert_close(obj.T_dependent_property_integral(T1, T2), f_int(T1, T2)) assert_close(obj.T_dependent_property_integral_over_T(T1, T2), f_int_over_T(T1, T2)) # Extrapolate Tmin = 300. + 1e-3 obj.add_method(f, Tmin, Tmax) # obj.RAISE_PROPERTY_CALCULATION_ERROR = True # obj.CASRN = 'CASRN' assert_close(obj.T_dependent_property(T), f(T)) for order in (1, 2, 3): assert obj.T_dependent_property_derivative(T, order) is not None assert_close(obj.T_dependent_property_integral(T1, T2), f_int(T1, T2)) assert_close(obj.T_dependent_property_integral_over_T(T1, T2), f_int_over_T(T1, T2)) # Do not allow extrapolation obj.extrapolation = None obj.add_method(f, Tmin, Tmax) assert obj.T_dependent_property(T) is None assert obj.T_dependent_property_integral(T1, T2) is None assert obj.T_dependent_property_integral_over_T(T1, T2) is None