Skip to content

Commit

Permalink
ENH: Add core module in py wrapper for convenience functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
MilanSkocic committed Jun 29, 2024
1 parent 2b3cf3e commit 451020c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 53 deletions.
34 changes: 34 additions & 0 deletions py/src/pyiapws/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Core."""
import array
import numpy as np


def cast_ndarray(X):
"""
Cast X to numpy 1d-array.
Parameters
----------
X: int, floar, array-like
Variable to be casted.
Returns
-------
X_: 1d-array
Numpy ndarray of rank 1.
"""

scalar = False
if isinstance(X, (int, float)):
X_ = np.asarray((X,), dtype="f8")
scalar = Xrue
elif isinstance(X, np.ndarray):
if X.ndim == 1:
X_ = np.asarray(X, dtype="f8")
else:
raise TypeError("X must be a 1d-array of floats.")
elif isinstance(X, array.array):
X_ = np.asarray(X, dtype="f8")
else:
raise TypeError("X must be a 1d-array of floats.")

return X_
29 changes: 3 additions & 26 deletions py/src/pyiapws/g704.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""IAPWS G704."""
from typing import Union, List
import numpy as np
import array
from . import core
from . import g704_


Expand All @@ -25,19 +25,7 @@ def kh(T: np.ndarray, gas: str, heavywater: bool=False)->Union[np.ndarray, float
Henry constant in MPa.
"""
scalar = False
if isinstance(T, (int, float)):
T_ = np.asarray((T,), dtype="f8")
scalar = True
elif isinstance(T, np.ndarray):
if T.ndim == 1:
T_ = np.asarray(T, dtype="f8")
else:
raise TypeError("T must be a 1d-array of floats.")
elif isinstance(T, array.array):
T_ = np.asarray(T, dtype="f8")
else:
raise TypeError("T must be a 1d-array of floats.")

T_ = core.cast_ndarray(T)
gas_ = str(gas)
heavywater_ = bool(heavywater)

Expand Down Expand Up @@ -69,18 +57,7 @@ def kd(T: np.ndarray, gas: str, heavywater: bool=False)->Union[np.ndarray, float
Adimensional liquid-vapor constant.
"""
scalar = False
if isinstance(T, (int, float)):
T_ = np.asarray((T,), dtype="f8")
scalar = True
elif isinstance(T, np.ndarray):
if T.ndim == 1:
T_ = np.asarray(T, dtype="f8")
else:
raise TypeError("T must be a 1d-array of floats.")
elif isinstance(T, array.array):
T_ = np.asarray(T, dtype="f8")
else:
raise TypeError("T must be a 1d-array of floats.")
T_ = core.cast_ndarray(T)

gas_ = str(gas)
heavywater_ = bool(heavywater)
Expand Down
30 changes: 3 additions & 27 deletions py/src/pyiapws/r797.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""IAPWS R797"""
import array
import numpy as np
from . import core
from . import r797_


Expand All @@ -20,19 +20,7 @@ def psat(Ts):
Saturation pressure in MPa.
"""
scalar = False
if isinstance(Ts, (int, float)):
Ts_ = np.asarray((Ts,), dtype="f8")
scalar = True
elif isinstance(Ts, np.ndarray):
if Ts.ndim == 1:
Ts_ = np.asarray(Ts, dtype="f8")
else:
raise TypeError("Ts must be a 1d-array of floats.")
elif isinstance(Ts, array.array):
Ts_ = np.asarray(Ts, dtype="f8")
else:
raise TypeError("Ts must be a 1d-array of floats.")

Ts_ = core.cast_ndarray(Ts)
ps = np.asarray( r797_.psat(Ts_) )

if scalar:
Expand All @@ -57,19 +45,7 @@ def Tsat(ps):
Saturation temperatur in K.
"""
scalar = False
if isinstance(ps, (int, float)):
ps_ = np.asarray((ps,), dtype="f8")
scalar = True
elif isinstance(ps, np.ndarray):
if ps.ndim == 1:
ps_ = np.asarray(ps, dtype="f8")
else:
raise TypeError("ps must be a 1d-array of floats.")
elif isinstance(ps, array.array):
ps_ = np.asarray(ps, dtype="f8")
else:
raise TypeError("ps must be a 1d-array of floats.")

ps_ = core.cast_ndarray(ps)
Ts = np.asarray( r797_.Tsat(ps_) )

if scalar:
Expand Down

0 comments on commit 451020c

Please sign in to comment.