"""
heat_transfer_correlations.py

Single-phase convective heat transfer for internal tube flow.

Reference
---------
VDI-Wärmeatlas, Kap. G1, 11. Aufl., Springer Vieweg, 2013
"""

import numpy as np
from .base_class_one_phase import OnePhaseTubeBase


class VDIHeatTransfer(OnePhaseTubeBase):
    """
    Nusselt-based heat transfer coefficient for single-phase tube flow
    (VDI Wärmeatlas G1).

    Returns
    -------
    calc() -> dict with keys: alpha [W/(m²K)], meta
    """

    def calc(self):
        fs = self.fluidstate

        if self.Re > 10000:
            Xi = (1.8 * np.log10(self.Re) - 1.5) ** -2                          # G1-(27)
            Nu = ((Xi / 8) * self.Re * self.Pr) / (
                  1 + 12.7 * (Xi / 8) ** 0.5 * (self.Pr ** (2 / 3) - 1)
                 ) * (1 + (self.dh / self.l) ** (2 / 3))                         # G1-(26)

        elif self.Re > 2300:
            gamma   = (self.Re - 2300) / (1e4 - 2300)                            # G1-(30)
            Nu_wmq2 = 1.615 * (2300 * self.Pr * self.dh / self.l) ** (1 / 3)    # G1-(32)
            Nu_wmq3 = (2 / (1 + 22 * self.Pr)) ** (1 / 6) * (2300 * self.dh / self.l) ** 0.5  # G1-(33)
            Nu_wL   = (49.371 + (Nu_wmq2 - 0.7) ** 3 + Nu_wmq3 ** 3) ** (1 / 3) # G1-(31)
            Nu_wT   = ((0.0308 / 8) * 1e4 * self.Pr) / (
                       1 + 12.7 * (0.0308 / 8) ** 0.5 * (self.Pr ** (2 / 3) - 1))  # G1-(37)
            Nu      = (1 - gamma) * Nu_wL + gamma * Nu_wT                        # G1-(29)

        else:
            Nu = 3.66                                                             # G1-(1)

        alpha = float(Nu * fs.thermal_conductivity / self.dh)                    # B1-(9)

        if np.iscomplexobj(alpha):
            raise ValueError("alpha is complex")

        return {
            "alpha": alpha,
            "meta": {
                "Re": float(self.Re),
                "Pr": float(self.Pr),
                "Nu": float(Nu),
                "dh": float(self.dh),
            },
        }