0
def htc_gnielinski_calc(
    self, MFR_ref_in, rho_ref_in, mu_ref_in, cp_ref_in, k_ref_in
):
    """
    Gnielinski Refrigerant Heat transfer model: Calculate HTC
    """
    V_ref_in = MFR_ref_in / (rho_ref_in * pi * self.ID ** 2 / 4)
    Re = (rho_ref_in * V_ref_in * self.ID) / mu_ref_in
    if Re < 3000:
        HTC_Gnielinski = (k_ref_in / self.ID) * 3.66
        return HTC_Gnielinski
    f = (1.58 * log(Re) - 3.28) ** (-2)
    Pr_ref = mu_ref_in * cp_ref_in / k_ref_in
    HTC_Gnielinski = (
        (k_ref_in / self.ID)
        * ((f / 2) * (Re - 1000) * Pr_ref)
        / (1 + 12.7 * (f / 2) ** 0.5 * (Pr_ref ** (2 / 3) - 1))
    )
    return HTC_Gnielinski

This is a equation solving heat transfer coefficient of refrigerant.

Importing cython to make my project run faster, actually it takes more time.

Here's my cython code

cpdef htc_gnielinski_calc(double MFR_ref_in, double rho_ref_in, double mu_ref_in, double cp_ref_in, double k_ref_in, double ID):
    """
    Gnielinski Refrigerant Heat transfer model: Calculate HTC
    """
    cdef double V_ref_in = MFR_ref_in / (rho_ref_in * pi * ID ** 2 / 4)
    cdef double Re = (rho_ref_in * V_ref_in * ID) / mu_ref_in
    cdef double HTC_Gnielinski, f, Pr_ref

    if Re < 3000:
        HTC_Gnielinski = (k_ref_in / ID) * 3.66
        return HTC_Gnielinski

    f = (1.58 * log(Re) - 3.28) ** (-2)
    Pr_ref = mu_ref_in * cp_ref_in / k_ref_in
    HTC_Gnielinski = (
        (k_ref_in / ID)
        * ((f / 2) * (Re - 1000) * Pr_ref)
        / (1 + 12.7 * (f / 2) ** 0.5 * (Pr_ref ** (2 / 3) - 1))
    )
    return HTC_Gnielinski

However, code below this make my project run faster. (Different equation)

cpdef htc_shah_cond_calc(
    double MFR_ref_in,
    double P_ref_in,
    double x_ref_in,
    double mu_ref_l_in,
    double mu_ref_g_in,
    double cp_ref_l_in,
    double k_ref_l_in,
    double rho_ref_l_in,
    double rho_ref_g_in,
    double ID,
    double P_critical_ref
):
    "Two-phase HTC"
    cdef int theta = 0
    cdef double G = MFR_ref_in / ((pi * ID ** 2) / 4)
    cdef double P_r = P_ref_in / P_critical_ref
    cdef double Z = (1 / x_ref_in - 1) ** 0.8 * P_r ** 0.4
    cdef double Re_LS = (G * (1 - x_ref_in) * ID) / mu_ref_l_in  # Reynolds number assuming liquid phase flowing alone
    cdef double Pr_l = mu_ref_l_in * cp_ref_l_in / k_ref_l_in
    cdef double h_LS = 0.023 * Re_LS ** 0.8 * Pr_l ** 0.4 * (k_ref_l_in / ID)
    cdef double h_I = ( h_LS * (1 + 3.8 / (Z ** 0.95)) * (mu_ref_l_in / (14 * mu_ref_g_in)) ** (0.0058 + 0.557 * P_r) )
    cdef double h_Nu = (1.32* Re_LS ** (-1 / 3)* (rho_ref_l_in* (rho_ref_l_in - rho_ref_g_in)* 9.80665* k_ref_l_in ** 3/ (mu_ref_l_in ** 2))** (1 / 3))
    cdef double J_g = (x_ref_in * G) / ( 9.80665 * ID * rho_ref_g_in * (rho_ref_l_in - rho_ref_g_in) ) ** 0.5
    cdef double HTC_Shah_Cond

    if theta == 0:
        if J_g >= (0.98 * (Z + 0.263) ** (-0.62)):
            HTC_Shah_Cond = h_I  # Regime 1 in horizontal tube
        elif J_g <= (0.95 * (1.254 + 2.27 * Z ** (1.249)) ** (-1)):
            HTC_Shah_Cond = h_Nu  # Regime 3 in horizontal tube
        elif J_g > (0.95 * (1.254 + 2.27 * Z ** (1.249)) ** (-1)) and J_g < (
            0.98 * (Z + 0.263) ** (-0.62)
        ):
            HTC_Shah_Cond = h_I + h_Nu  # Regime 2 in horizontal tube
    elif theta == 90:
        if J_g >= (1 / (2.4 * Z + 0.73)):
            HTC_Shah_Cond = h_I  # Regime 1 in vertical tube
        elif J_g <= (0.89 - 0.93 * exp(-0.087 * Z ** (-1.17))):
            HTC_Shah_Cond = h_Nu  # Regime 3 in vertical tube
        elif J_g > (0.89 - 0.93 * exp(-0.087 * Z ** (-1.17))) and J_g < (
            1 / (2.4 * Z + 0.73)
        ):
            HTC_Shah_Cond = h_I + h_Nu  # Regime 2 in vertical tube
    return HTC_Shah_Cond

What would be the differences?

Are there any criteria for taking cython during optimization?

  • Two thoughts: 1) have you looked at the annotated html (`cython -a filename.pyx`); 2) Could this be due to int vs float division (it depends a bit on the Cython version you're using, but most versions use the old Python2 behaviour) - i.e. have you checked that the result is the same – DavidW Mar 29 '21 at 13:06
  • 3) Where does `log` come from - if it comes from `import math` then it won't be efficient in Cython and you should try using libc math instead – DavidW Mar 29 '21 at 13:07
  • Thank you so much. I'll check everything you said Thanks. – onlyhappiness Mar 29 '21 at 23:52
  • Dear David, I found that the result has changed. And the annotate says "float division". (added `@cython.cdivision(True)`) – onlyhappiness Mar 30 '21 at 00:58
  • What should I do for "int vs float division"? – onlyhappiness Mar 30 '21 at 00:59
  • You should [specify the `language_level`](https://stackoverflow.com/a/53992016/4657412) to match the Python behaviour you expect – DavidW Mar 30 '21 at 07:15

0 Answers0