Tested this out on a bunch more data. I have a bunch of data from an operating engine and I'm calculating the compressor efficiency. The function works fine but it's relatively slow. Any ideas to improve speed would be appreciated. To give some idea, the iterative loop maybe goes thru 5 iterations so it's relatively efficient. I've updated my latest version to optionally calculate the isentropic enthalpy rise as well.
Function isencomp(t_in As Double, p_in As Double, p_out As Double, fluid As String, Optional calcdhi As Boolean = False) As Variant
'this function calculates the isentropic outlet temperature, t_outi, and optionally the corresponding enthalpy, dh_outi = h(T_outi,p_out) - h(t_in,p_in)
'of an isentropic compression from (t_in, p_in) to p_out of fluid described in CoolProp fluid string 'fluid'
'temps in K, press in Pa
'this function is needed because Coolprop can't calculate h(s,p) for fluid mixtures
'if calcdhi is FALSE (the default) only t_outi is calculated if it's true the function returns a length 2 vector where the first element is t_outi and the second is dh_outi
Dim r_gas As Double 'gas constant for gas, J/kg/K
Dim t_outi As Double, t_outi1 As Double 'outlet temp for isentropic compression and iteration version, K
Dim count As Long
Dim s_in As Double 'inlet entropy, J/kg/K
Dim resid As Variant 'residual
r_gas = CoolProp.Props1SI(fluid, "GAS_CONSTANT") / CoolProp.Props1SI(fluid, "MOLAR_MASS")
s_in = CoolProp.PropsSI("SMASS", "T|gas", t_in, "P", p_in, fluid)
t_outi1 = 0
t_outi = t_in * (p_out / p_in) ^ (0.4 / 1.4) 'calorically perfect guess
count = 0
While Abs(t_outi - t_outi1) > 0.0001 And count < 200
t_outi1 = t_outi
resid = CoolProp.PropsSI("SMASS", "T|gas", t_outi, "P", p_out, fluid) - s_in
t_outi = t_outi - resid * t_outi / CoolProp.PropsSI("CPMASS", "T|gas", t_outi, "P", p_out, fluid) 'calorically perfect update
count = count + 1
Wend
If Not calcdhi Then
isencomp = t_outi
Else
Dim outi(1 To 2) As Double
outi(1) = t_outi
outi(2) = CoolProp.PropsSI("HMASS", "T|gas", t_outi, "P", p_out, fluid) - CoolProp.PropsSI("HMASS", "T|gas", t_in, "P", p_in, fluid)
isencomp = outi
End If
End Function