"""Appendix to Chmltech Online Journal Vol. 106, Issue 24:
what the solved power and pumping numbers mean for a reactor.

Given a tank, an impeller and its power number Np and pumping
number Nq (from CFD, a test or a handbook), print the power at a
speed, the speed that draws a target power, the impeller flow, the
turnover time and the 95% blend time from the turbulent correlation
N t95 = 5.2 Np^(-1/3) (T/D)^2 (Grenville and Nienow, 2004;
H = T, baffled, turbulent). The defaults are R-101 of the paper
and its solved values. Python 3, no other packages.
"""
import math

RHO, MU = 1000.0, 0.001       # kg/m3, Pa.s
T, D, V = 2.335, 0.88, 10.0   # tank, impeller (m); batch (m3)
# (Np, Nq) from the paper's medium-mesh k-epsilon runs
SOLVED = {
    "Rushton turbine": (5.07337, 0.78490),
    "Pitched blade turbine": (1.17741, 0.83374),
    "Hydrofoil (generic, 4 blades)": (0.29410, 0.55222),
}


def duty(np_, nq, rpm):
    n = rpm / 60
    p = np_ * RHO * n ** 3 * D ** 5                     # W
    q = nq * n * D ** 3                                 # m3/s
    re = RHO * n * D ** 2 / MU
    t95 = 5.2 / np_ ** (1 / 3) * (T / D) ** 2 / n       # s
    return dict(rpm=rpm, Re=re, P_kW=p / 1000, P_per_m3=p / V,
                Q_m3s=q, turnover_s=V / q, t95_s=t95,
                tip_m_s=math.pi * D * n)


def speed_for_power(np_, p_kW):
    return 60 * (p_kW * 1000 / (np_ * RHO * D ** 5)) ** (1 / 3)


if __name__ == "__main__":
    print(f"Tank T = {T} m, impeller D = {D} m, batch {V} m3")
    for name, (np_, nq) in SOLVED.items():
        a = duty(np_, nq, 84.0)
        n_eq = speed_for_power(np_, 1.84)
        b = duty(np_, nq, n_eq)
        print(f"\n{name}: Np = {np_:.2f}, Nq = {nq:.2f}")
        print(f"  at 84 rpm: P = {a['P_kW']:.2f} kW, "
              f"Q = {a['Q_m3s']:.3f} m3/s,")
        print(f"    turnover {a['turnover_s']:.1f} s, "
              f"blend time {a['t95_s']:.0f} s, Re = {a['Re']:.2e}")
        print(f"  at 1.84 kW: {n_eq:.0f} rpm, "
              f"Q = {b['Q_m3s']:.3f} m3/s,")
        print(f"    turnover {b['turnover_s']:.1f} s, "
              f"blend time {b['t95_s']:.0f} s")
