import math


def viscosity_water_vapor(T, density):
    """
    Calculate the viscosity of water vapor based on a function of absolute
    temperature and density.  Taken from Sengers, J. V. and B. Kamgar-Parsi.
    "Representative Equations for the Viscosity of Water Substance."  Journal
    of Physical and Chemical Reference Data.  13, no. 1 (1984): 185-205.

    Enter: T: temperature in K
           density: density in kg/(m^3)
    Exit:  viscosity_vapor: viscosity in kg/m/s (Pa*s)
    """
    scaledT = T/647.27
    mu0 = 1e-6*scaledT**0.5/(0.0181583+0.0177624/scaledT +
                             0.0105287/(scaledT**2)-0.0036744/(scaledT**3))
    scaledRho = density/317.763
    bij = [[0.501938, 0.162888, -0.130356, 0.907919, -0.551119, 0.146543],
           [0.235622, 0.789383, 0.673665, 1.207552, 0.0670665, -0.0843370],
           [-0.274637, -0.743539, -0.959456, -0.687343, -0.497089, 0.195286],
           [0.145831, 0.263129, 0.347247, 0.213486, 0.100754, -0.032932],
           [-0.0270448, -0.0253093, -0.0267758, -0.0822904, 0.0602253,
            -0.0202595]]
    invScaledT_1 = 1 / scaledT - 1
    scaledRho_1 = scaledRho - 1
    factor = 0
    for j in range(len(bij)):
        for i in range(len(bij[0])):
            factor += bij[j][i] * invScaledT_1**i * scaledRho_1**j
    mu = mu0*math.exp(scaledRho*factor)
    return mu