Skip to content

Commit

Permalink
update formating
Browse files Browse the repository at this point in the history
- reformat files with black
- fix some pylint warnings
- use better logging static/classmethods
- move demo scripts
  • Loading branch information
drunsinn committed Dec 16, 2023
1 parent 7db0df1 commit a513e4f
Show file tree
Hide file tree
Showing 19 changed files with 491 additions and 803 deletions.
20 changes: 15 additions & 5 deletions pyXSteam/Constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
Sources:
* IAPWS Industrial formulation 1997 for the Thermodynamic Properties of Water and Steam, September 1997
* IAWPS Release on Vaues of Temperature, Pressure and Density of Ordinary and Heavy Water Substances at their Respective Critical Points Released September 1992, Revision of the Release of 1992
* IAPWS Industrial formulation 1997 for the Thermodynamic Properties of Water
and Steam, September 1997
* IAWPS Release on Vaues of Temperature, Pressure and Density of Ordinary and
Heavy Water Substances at their Respective Critical Points Released
September 1992, Revision of the Release of 1992
"""
from enum import IntEnum

Expand Down Expand Up @@ -48,16 +50,21 @@


class UnitSystem(IntEnum):
"""enum for supported unit systems"""

BARE = 1 # m/kg/sec/K/MPa/W
MKS = 1 # m/kg/sec/°C/bar/W
FLS = 2 # ft/lb/sec/°F/psi/btu

@classmethod
def has_value(cls, value):
def has_value(cls, value: int):
"""check if value is member of enum"""
return value in cls._value2member_map_


class IceType(IntEnum):
"""enum for the types of ice"""

Ih = 1
III = 3
V = 5
Expand All @@ -66,11 +73,14 @@ class IceType(IntEnum):
NONE = -1

@classmethod
def has_value(cls, value):
def has_value(cls, value: int):
"""check if value is member of enum"""
return value in cls._value2member_map_


class DiagramRegion(IntEnum):
"""enum for the regions"""

NILL = 0 # Error, Outside valid area
R1 = 1
R2 = 2
Expand Down
22 changes: 6 additions & 16 deletions pyXSteam/IAPWS_R12.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def _my_dash_0(T_dash: float) -> float:
numerator = 100 * math.sqrt(T_dash)
denominator = 0
for i in range(0, 4):
denominator += H[i] / T_dash ** i
denominator += H[i] / T_dash**i
return numerator / denominator


Expand Down Expand Up @@ -79,15 +79,13 @@ def _y(xi: float) -> float:
coefficient_1 = 1 - summand_0 + summand_1 - summand_2
return coefficient_0 * coefficient_1

psi_D = math.acos((1 + (q_D ** 2) * (xi ** 2)) ** (-1 / 2)) # ep 17
psi_D = math.acos((1 + (q_D**2) * (xi**2)) ** (-1 / 2)) # ep 17

summand_0 = 1 / 12 * math.sin(3 * psi_D) # ep16, part 1

summand_1 = (1 / (4 * q_C * xi)) * math.sin(2 * psi_D) # ep16, part 2

summand_2 = (
(1 / (q_C * xi) ** 2) * (1 - (5 / 4) * (q_C * xi) ** 2) * math.sin(psi_D)
) # ep16, part 3
summand_2 = (1 / (q_C * xi) ** 2) * (1 - (5 / 4) * (q_C * xi) ** 2) * math.sin(psi_D) # ep16, part 3

w = abs((q_C * xi - 1) / (q_C * xi + 1)) ** (1 / 2) * math.tan(psi_D / 2) # ep 19
sum3_sub0 = (1 - (3 / 2 * (q_C * xi) ** 2)) * psi_D
Expand All @@ -105,9 +103,7 @@ def _sigma(rho_dash: float, T_dash: float) -> float:
# eq 21a
# TODO
logger.debug("rho_dash %f T_dash %f", rho_dash, T_dash)
raise NotImplementedError(
"use derivative of density on pressure at constant temperature"
)
raise NotImplementedError("use derivative of density on pressure at constant temperature")
return -1


Expand All @@ -121,7 +117,6 @@ def _delta_chi_dash(rho_dash: float, T_dash: float) -> float:


def _my_dash_2(T: float, rho: float, T_dash: float, rho_dash: float) -> float:

if 645.91 < T < 650.77 and 245.8 < rho < 405.3: # eq 13, page 6
logger.debug("values for T and rho are within critical region")
# critical region
Expand All @@ -143,9 +138,7 @@ def _my_dash_2(T: float, rho: float, T_dash: float, rho_dash: float) -> float:
logger.debug("value for my_dash_2:%f", my_dash_2)
return my_dash_2

logger.debug(
"values for T and rho are outside of critical region, use my_dash_2=1.0"
)
logger.debug("values for T and rho are outside of critical region, use my_dash_2=1.0")
return 1.0 # section 2.8, page 8


Expand Down Expand Up @@ -189,10 +182,7 @@ def eq10(T: float, rho: float, industrial: bool = True) -> float:

my_dash = my_dash_0 * my_dash_1 * my_dash_2 # eq 10

logger.debug(
"calculated values µ_0=%f µ_1=%f µ_2=%f µ_dash=%f"
% (my_dash_0, my_dash_1, my_dash_2, my_dash)
)
logger.debug("calculated values µ_0=%f µ_1=%f µ_2=%f µ_dash=%f" % (my_dash_0, my_dash_1, my_dash_2, my_dash))

my = my_dash * my_star # eq8, value is in µPa*s
# my = my * 10E6 # in Pa*s
Expand Down
14 changes: 7 additions & 7 deletions pyXSteam/IAPWS_R14.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def pmelt_T_iceIII(T: float) -> float:
T_star = 251.165
p_star = 208.566
theta = T / T_star
pi_melt = 1 - 0.299948 * (1.0 - theta ** 60)
pi_melt = 1 - 0.299948 * (1.0 - theta**60)
p_melt = pi_melt * p_star
logger.debug("result for 'melting preasure of ice type III': %f", p_melt)
return p_melt
Expand All @@ -67,7 +67,7 @@ def pmelt_T_iceV(T: float) -> float:
T_star = 256.164
p_star = 350.1
theta = T / T_star
pi_melt = 1 - 1.18721 * (1.0 - theta ** 8)
pi_melt = 1 - 1.18721 * (1.0 - theta**8)
p_melt = pi_melt * p_star
logger.debug("result for 'melting preasure of ice type V': %f", p_melt)
return p_melt
Expand All @@ -86,7 +86,7 @@ def pmelt_T_iceVI(T: float) -> float:
T_star = 273.31
p_star = 632.4
theta = T / T_star
pi_melt = 1 - 1.07476 * (1.0 - theta ** 4.6)
pi_melt = 1 - 1.07476 * (1.0 - theta**4.6)
p_melt = pi_melt * p_star
logger.debug("result for 'melting preasure of ice type VI': %f", p_melt)
return p_melt
Expand All @@ -105,9 +105,9 @@ def pmelt_T_iceVII(T: float) -> float:
T_star = 355.0
p_star = 2216.0
theta = T / T_star
p1 = 0.173683e1 * (1 - (theta ** -1))
p2 = 0.544606e-1 * (1 - (theta ** 5))
p3 = 0.806106e-7 * (1 - (theta ** 22))
p1 = 0.173683e1 * (1 - (theta**-1))
p2 = 0.544606e-1 * (1 - (theta**5))
p3 = 0.806106e-7 * (1 - (theta**22))
pi_melt = math.exp(p1 - p2 + p3)
p_melt = pi_melt * p_star
logger.debug("result for 'melting preasure of ice type VII': %f", p_melt)
Expand All @@ -132,7 +132,7 @@ def psubl_T(T: float) -> float:
temp_sum = 0
for i in range(0, 3):
temp_sum += a[i] * theta ** b[i]
pi_subl = math.exp((theta ** -1) * temp_sum)
pi_subl = math.exp((theta**-1) * temp_sum)
p_subl = pi_subl * p_star
logger.debug("result for 'sublimation preasure of ice': %f", p_subl)
return p_subl
34 changes: 15 additions & 19 deletions pyXSteam/IAPWS_R4.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ def myHW_rhoT_R4(rho: float, T: float) -> float:
A = [1.0, 0.940695, 0.578377, -0.202044]
sum_A_T = 0
for i in range(0, 4):
sum_A_T += A[i] / (T_dash ** i)
sum_A_T += A[i] / (T_dash**i)
my_0_dash = math.sqrt(T_dash) / sum_A_T

B = list()
B.append(
[0.4864192, -0.2448372, -0.8702035, 0.8716056, -1.051126, 0.3458395]
) # j= 0
B = []
B.append([0.4864192, -0.2448372, -0.8702035, 0.8716056, -1.051126, 0.3458395]) # j= 0
B.append([0.3509007, 1.315436, 1.297752, 1.353448, 0.0, 0.0]) # j= 1
B.append([-0.2847572, -1.037026, -1.287846, 0.0, 0.0, -0.02148229]) # j= 2
B.append([0.07013759, 0.4660127, 0.2292075, -0.4857462, 0.0, 0.0]) # j= 3
Expand Down Expand Up @@ -67,20 +65,20 @@ def myHW_rhoT_R4(rho: float, T: float) -> float:

def tcHW_rhoT_R4(rho: float, T: float) -> float:
"""
Thermal conductivity as a function of density and temperature for heavy water substance.
Thermal conductivity as a function of density and temperature for heavy
water substance.
Returns NaN if arguments are out of range
Note: function name is using tc instead of lambda to minimize the confusion with the python
Note: function name is using tc instead of lambda to minimize the confusion
with the python
function of the same name.
:param rho: density value for heavy water in [kg / m³]
:param T: temperature value for heavy water in [K]
:return: thermal conductivity λ or NaN, in [W / (m * K)]
"""
logger.debug(
"calculating 'thermal conductivity of heavy water' for ρ=%f and T=%f", rho, T
)
logger.debug("calculating 'thermal conductivity of heavy water' for ρ=%f and T=%f", rho, T)

T_star = 643.847 # K
rho_star = 358 # kg / m³
Expand All @@ -102,32 +100,30 @@ def tcHW_rhoT_R4(rho: float, T: float) -> float:
rho_r1 = 0.125698
D_1 = -741.112
tau = T_dash / (math.fabs(T_dash - 1.1) + 1.1) # B15
f_1 = math.exp(C_T1 * T_dash + C_T2 * (T_dash ** 2)) # B12
f_2 = math.exp(C_R1 * (rho_dash - 1.0) ** 2) + C_R2 * math.exp(
C_R3 * (rho_dash - rho_r1) ** 2
) # B13
f_1 = math.exp(C_T1 * T_dash + C_T2 * (T_dash**2)) # B12
f_2 = math.exp(C_R1 * (rho_dash - 1.0) ** 2) + C_R2 * math.exp(C_R3 * (rho_dash - rho_r1) ** 2) # B13
f_3 = 1 + math.exp(60.0 * (tau - 1.0) + 20.0) # B14
f_4 = 1 + math.exp(100.0 * (tau - 1.0) + 15.0) # B14
part_C2 = (C_2 * f_1 ** 4) / f_3
part_C2 = (C_2 * f_1**4) / f_3
part_f2 = (3.5 * f_2) / f_4

# equation B8
temp_sum = 0
for i in range(0, 6):
temp_sum += A[i] * (T_dash ** i)
temp_sum += A[i] * (T_dash**i)
tc_o = temp_sum

# equation B9
temp_sum = 0
for i in range(1, 5):
temp_sum += B[i] * (rho_dash ** i)
temp_sum += B[i] * (rho_dash**i)
delta_tc = B[0] * (1.0 - math.exp(B_e * rho_dash)) + temp_sum

# equation B10
delta_tc_c = C_1 * f_1 * f_2 * (1.0 + f_2 ** 2 * (part_C2 + part_f2))
delta_tc_c = C_1 * f_1 * f_2 * (1.0 + f_2**2 * (part_C2 + part_f2))

# equation B11
delta_tc_L = D_1 * f_1 ** 1.2 * (1.0 - math.exp(-1.0 * (rho_dash / 2.5) ** 10))
delta_tc_L = D_1 * f_1**1.2 * (1.0 - math.exp(-1.0 * (rho_dash / 2.5) ** 10))

# equation B7
tc_dash = tc_o + delta_tc + delta_tc_c + delta_tc_L
Expand Down
8 changes: 3 additions & 5 deletions pyXSteam/IAPWS_R5.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
Revised Release on Surface Tension of Heavy Water Substance
http://www.iapws.org/relguide/surfd2o.pdf
"""
import math
import logging

from .Constants import TRIPLE_POINT_TEMPERATURE, CRITICAL_TEMPERATURE_D20_1992
Expand All @@ -27,7 +26,6 @@ def surface_tension_T(T: float) -> float:
my = 1.25
if TRIPLE_POINT_TEMPERATURE < T < CRITICAL_TEMPERATURE_D20_1992:
tau = 1 - T / CRITICAL_TEMPERATURE_D20_1992
return B * tau ** my * (1 + bb * tau)
else:
logger.warning("Temperature out of range of validity")
return float("NaN")
return B * tau**my * (1 + bb * tau)
logger.warning("Temperature out of range of validity")
return float("NaN")
Loading

0 comments on commit a513e4f

Please sign in to comment.