# Path: ht/conv_free_enclosed.py
# def Nu_Nusselt_Rayleigh_Holling_Herwig(Pr, Gr, buoyancy=True):
#     r'''Calculates the Nusselt number for natural convection between two
#     theoretical flat horizontal plates. The height between the plates is infinite, and
#     one of the other dimensions of the plates is much larger than the other.
# 
#     This correlation is for the horizontal plate Rayleigh-Benard classic heat
#     transfer problem, not for real finite geometry plates.
# 
#     This model is a non-linear equation which is solved numerically.
#     The model can calculate `Nu` for `Ra` ranges between 350 and larger
#     numbers; [1]_ recommends :math:`10^{5} < Ra < 10^{15}`.
# 
#     .. math::
#         \text{Nu} = \frac{{Ra}^{1/3}}{[0.05\ln(\frac{0.078}{16}{Ra}^{1.323})
#         + 2D]^{4/3}}
# 
#     .. math::
#         D = -\frac{14.94}{{Ra}^{0.25}} + 3.43
# 
#     Parameters
#     ----------
#     Pr : float
#         Prandtl number with respect to fluid properties [-]
#     Gr : float
#         Grashof number with respect to fluid properties and plate - plate
#         temperature difference [-]
#     buoyancy : bool, optional
#         Whether or not the plate's free convection is buoyancy assisted (hot
#         plate) or not, [-]
# 
#     Returns
#     -------
#     Nu : float
#         Nusselt number with respect to height between the two plates, [-]
# 
#     Notes
#     -----
#     A range of calculated values are provided in [1]_; they all match the
#     results of this function. This model is recommended in [2]_.
# 
#     For :math:`Ra < 1708`, `Nu` = 1; for cases not assited by `buoyancy`,
#     `Nu` is also 1.
# 
#     No success has been found finding an analytical solution in the major CAS
#     packages, but the nonlinear function is in fact a function of one variable;
#     this means a pade or chebyshev expansion could be performed.
# 
# 
#     Examples
#     --------
#     >>> Nu_Nusselt_Rayleigh_Holling_Herwig(5.54, 3.21e8, buoyancy=True)
#     77.54656801896913
# 
#     References
#     ----------
#     .. [1] Hölling, M., and H. Herwig. "Asymptotic Analysis of Heat Transfer in
#        Turbulent Rayleigh–Bénard Convection." International Journal of Heat and
#        Mass Transfer 49, no. 5 (March 1, 2006): 1129-36.
#        https://doi.org/10.1016/j.ijheatmasstransfer.2005.09.002.
#     .. [2] Gesellschaft, V. D. I., ed. VDI Heat Atlas. 2nd ed. 2010 edition.
#        Berlin ; New York: Springer, 2010.
#     '''
#     if not buoyancy:
#         return 1.0
#     Rac = 1708 # Constant
# 
#     Ra = Gr*Pr
#     if Ra < Rac:
#         return 1.0
# 
#     Ra_third = Ra**(1.0/3.0)
#     D2 = 2.0*(-14.94*Ra**-0.25 + 3.43)
#     Nu_guess = Ra_third*(0.1/2.0*log(.078/16.0*Ra**1.323) + D2)**(-4.0/3.0)
#     return secant(Nu_Nusselt_Rayleigh_Holling_Herwig_err, Nu_guess, args=(Ra, Ra_third, D2))


from ht import *
from ht.conv_free_enclosed import Nu_Nusselt_Rayleigh_Holling_Herwig
from fluids.numerics import assert_close, assert_close1d, assert_close2d, numpy as np
    from scipy.interpolate import bisplrep, UnivariateSpline
    from ht.conv_free_enclosed import tck_uninstulated_Catton, ratios_uninsulated_Catton, Racs_uninstulated_Catton
    from ht.conv_free_enclosed import ratios_insulated_Catton, Racs_instulated_Catton, tck_insulated_Catton
    from fluids.optional import pychebfun
    from ht.conv_free_enclosed import insulated_disk_coeffs, uninsulated_disk_coeffs
import pytest
    assert_close(Nu_Nusselt_Rayleigh_Hollands(5.54, 3.21e8, buoyancy=True), 69.02668649510164)
    assert_close(Nu_Nusselt_Rayleigh_Hollands(.7, 3.21e6, buoyancy=True, Rac=Rac_Nusselt_Rayleigh(H=1, L=2, W=.2, insulated=False)), 4.666249131876477)

    assert_close(Nu_Nusselt_Rayleigh_Hollands(.7, 3.21e6, buoyancy=True, Rac=Rac_Nusselt_Rayleigh(H=1, L=1, W=1, insulated=False)), 8.786362614129537)

def test_Rac_Nusselt_Rayleigh_fit_uninsulated():
    all_zs = []
    all_xs = []
    all_ys = []
    for ratio1, Rac_row in zip(ratios_uninsulated_Catton, Racs_uninstulated_Catton):
        for Rac, ratio2 in zip(Rac_row, ratios_uninsulated_Catton):
            all_zs.append(Rac)
            all_xs.append(ratio1)
            all_ys.append(ratio2)

    tck = bisplrep(all_xs, all_ys, np.log(all_zs), kx=3, ky=3, s=0)

    for i in range(len(tck)-2):
        assert_close1d(tck[i], tck_uninstulated_Catton[i], rtol=1e-5)

#    for i, Racs in enumerate(Racs_uninstulated_Catton):
#        plt.semilogy(ratios_uninsulated_Catton, Racs, label=str(ratios_uninsulated_Catton[i]))
#        fit = np.exp(bisplev(ratios_uninsulated_Catton[i], ratios_uninsulated_Catton, tck))
#        plt.semilogy(ratios_uninsulated_Catton, fit, 'o')
#
#    plt.legend()
#    plt.show()

def test_Rac_Nusselt_Rayleigh_fit_insulated():


# Path: ht/conv_free_enclosed.py
# def Nu_Nusselt_Rayleigh_Holling_Herwig(Pr, Gr, buoyancy=True):
#     r'''Calculates the Nusselt number for natural convection between two
#     theoretical flat horizontal plates. The height between the plates is infinite, and
#     one of the other dimensions of the plates is much larger than the other.
# 
#     This correlation is for the horizontal plate Rayleigh-Benard classic heat
#     transfer problem, not for real finite geometry plates.
# 
#     This model is a non-linear equation which is solved numerically.
#     The model can calculate `Nu` for `Ra` ranges between 350 and larger
#     numbers; [1]_ recommends :math:`10^{5} < Ra < 10^{15}`.
# 
#     .. math::
#         \text{Nu} = \frac{{Ra}^{1/3}}{[0.05\ln(\frac{0.078}{16}{Ra}^{1.323})
#         + 2D]^{4/3}}
# 
#     .. math::
#         D = -\frac{14.94}{{Ra}^{0.25}} + 3.43
# 
#     Parameters
#     ----------
#     Pr : float
#         Prandtl number with respect to fluid properties [-]
#     Gr : float
#         Grashof number with respect to fluid properties and plate - plate
#         temperature difference [-]
#     buoyancy : bool, optional
#         Whether or not the plate's free convection is buoyancy assisted (hot
#         plate) or not, [-]
# 
#     Returns
#     -------
#     Nu : float
#         Nusselt number with respect to height between the two plates, [-]
# 
#     Notes
#     -----
#     A range of calculated values are provided in [1]_; they all match the
#     results of this function. This model is recommended in [2]_.
# 
#     For :math:`Ra < 1708`, `Nu` = 1; for cases not assited by `buoyancy`,
#     `Nu` is also 1.
# 
#     No success has been found finding an analytical solution in the major CAS
#     packages, but the nonlinear function is in fact a function of one variable;
#     this means a pade or chebyshev expansion could be performed.
# 
# 
#     Examples
#     --------
#     >>> Nu_Nusselt_Rayleigh_Holling_Herwig(5.54, 3.21e8, buoyancy=True)
#     77.54656801896913
# 
#     References
#     ----------
#     .. [1] Hölling, M., and H. Herwig. "Asymptotic Analysis of Heat Transfer in
#        Turbulent Rayleigh–Bénard Convection." International Journal of Heat and
#        Mass Transfer 49, no. 5 (March 1, 2006): 1129-36.
#        https://doi.org/10.1016/j.ijheatmasstransfer.2005.09.002.
#     .. [2] Gesellschaft, V. D. I., ed. VDI Heat Atlas. 2nd ed. 2010 edition.
#        Berlin ; New York: Springer, 2010.
#     '''
#     if not buoyancy:
#         return 1.0
#     Rac = 1708 # Constant
# 
#     Ra = Gr*Pr
#     if Ra < Rac:
#         return 1.0
# 
#     Ra_third = Ra**(1.0/3.0)
#     D2 = 2.0*(-14.94*Ra**-0.25 + 3.43)
#     Nu_guess = Ra_third*(0.1/2.0*log(.078/16.0*Ra**1.323) + D2)**(-4.0/3.0)
#     return secant(Nu_Nusselt_Rayleigh_Holling_Herwig_err, Nu_guess, args=(Ra, Ra_third, D2))


# Path: tests/test_conv_free_enclosed.py
from ht import *
from ht.conv_free_enclosed import Nu_Nusselt_Rayleigh_Holling_Herwig
from fluids.numerics import assert_close, assert_close1d, assert_close2d, numpy as np
    from scipy.interpolate import bisplrep, UnivariateSpline
    from ht.conv_free_enclosed import tck_uninstulated_Catton, ratios_uninsulated_Catton, Racs_uninstulated_Catton
    from ht.conv_free_enclosed import ratios_insulated_Catton, Racs_instulated_Catton, tck_insulated_Catton
    from fluids.optional import pychebfun
    from ht.conv_free_enclosed import insulated_disk_coeffs, uninsulated_disk_coeffs
import pytest

    assert_close(Nu_Nusselt_Rayleigh_Hollands(5.54, 3.21e8, buoyancy=True), 69.02668649510164)
    assert_close(Nu_Nusselt_Rayleigh_Hollands(.7, 3.21e6, buoyancy=True, Rac=Rac_Nusselt_Rayleigh(H=1, L=2, W=.2, insulated=False)), 4.666249131876477)

    assert_close(Nu_Nusselt_Rayleigh_Hollands(.7, 3.21e6, buoyancy=True, Rac=Rac_Nusselt_Rayleigh(H=1, L=1, W=1, insulated=False)), 8.786362614129537)

def test_Rac_Nusselt_Rayleigh_fit_uninsulated():
    all_zs = []
    all_xs = []
    all_ys = []
    for ratio1, Rac_row in zip(ratios_uninsulated_Catton, Racs_uninstulated_Catton):
        for Rac, ratio2 in zip(Rac_row, ratios_uninsulated_Catton):
            all_zs.append(Rac)
            all_xs.append(ratio1)
            all_ys.append(ratio2)

    tck = bisplrep(all_xs, all_ys, np.log(all_zs), kx=3, ky=3, s=0)

    for i in range(len(tck)-2):
        assert_close1d(tck[i], tck_uninstulated_Catton[i], rtol=1e-5)

#    for i, Racs in enumerate(Racs_uninstulated_Catton):
#        plt.semilogy(ratios_uninsulated_Catton, Racs, label=str(ratios_uninsulated_Catton[i]))
#        fit = np.exp(bisplev(ratios_uninsulated_Catton[i], ratios_uninsulated_Catton, tck))
#        plt.semilogy(ratios_uninsulated_Catton, fit, 'o')
#
#    plt.legend()
#    plt.show()

def test_Rac_Nusselt_Rayleigh_fit_insulated():


    all_zs = []