def BVirial_Tsonopoulos_extended(T, Tc, Pc, omega, a=<NUM_LIT:0>, b=<NUM_LIT:0>, species_type='<STR_LIT>', <EOL>dipole=<NUM_LIT:0>, order=<NUM_LIT:0>):
Tr = T/Tc<EOL>if order == <NUM_LIT:0>:<EOL><INDENT>B0 = <NUM_LIT> - <NUM_LIT>/Tr - <NUM_LIT>/Tr**<NUM_LIT:2> - <NUM_LIT>/Tr**<NUM_LIT:3> - <NUM_LIT>/Tr**<NUM_LIT:8><EOL>B1 = <NUM_LIT> + <NUM_LIT>/Tr**<NUM_LIT:2> - <NUM_LIT>/Tr**<NUM_LIT:3> - <NUM_LIT>/Tr**<NUM_LIT:8><EOL>B2 = <NUM_LIT:1.>/Tr**<NUM_LIT:6><EOL>B3 = -<NUM_LIT:1.>/Tr**<NUM_LIT:8><EOL><DEDENT>elif order == <NUM_LIT:1>:<EOL><INDENT>B0 = <NUM_LIT>*Tc/(<NUM_LIT:100>*T**<NUM_LIT:2>) + <NUM_LIT>*Tc**<NUM_LIT:2>/(<NUM_LIT:1000>*T**<NUM_LIT:3>) + <NUM_LIT>*Tc**<NUM_LIT:3>/(<NUM_LIT>*T**<NUM_LIT:4>) + <NUM_LIT>*Tc**<NUM_LIT:8>/(<NUM_LIT>*T**<NUM_LIT:9>)<EOL>B1 = -<NUM_LIT>*Tc**<NUM_LIT:2>/(<NUM_LIT>*T**<NUM_LIT:3>) + <NUM_LIT>*Tc**<NUM_LIT:3>/(<NUM_LIT:1000>*T**<NUM_LIT:4>) + <NUM_LIT:8>*Tc**<NUM_LIT:8>/(<NUM_LIT>*T**<NUM_LIT:9>)<EOL>B2 = -<NUM_LIT>*Tc**<NUM_LIT:6>/T**<NUM_LIT:7><EOL>B3 = <NUM_LIT>*Tc**<NUM_LIT:8>/T**<NUM_LIT:9><EOL><DEDENT>elif order == <NUM_LIT:2>:<EOL><INDENT>B0 = -<NUM_LIT:3>*Tc*(<NUM_LIT> + <NUM_LIT>*Tc/T + <NUM_LIT>*Tc**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LIT:2> - <NUM_LIT>*T**<NUM_LIT:5>*Tc**<NUM_LIT:3> - <NUM_LIT>*Tc**<NUM_LIT:8>)/(<NUM_LIT>*T**<NUM_LIT:7>)<EOL>B2 = -Tc**<NUM_LIT:6>/(<NUM_LIT:5>*T**<NUM_LIT:5>)<EOL>B3 = Tc**<NUM_LIT:8>/(<NUM_LIT:7>*T**<NUM_LIT:7>)<EOL><DEDENT>elif order == -<NUM_LIT:2>:<EOL><INDENT>B0 = <NUM_LIT>*T**<NUM_LIT:2>/<NUM_LIT> - <NUM_LIT>*T*Tc*log(T)/<NUM_LIT> + <NUM_LIT>*T*Tc/<NUM_LIT> + <NUM_LIT>*Tc**<NUM_LIT:2>*log(T)/<NUM_LIT> - (<NUM_LIT>*T**<NUM_LIT:5>*Tc**<NUM_LIT:3> + <NUM_LIT>*Tc**<NUM_LIT:8>)/(<NUM_LIT>*T**<NUM_LIT:6>)<EOL>B1 = <NUM_LIT>*T**<NUM_LIT:2>/<NUM_LIT> - <NUM_LIT>*Tc**<NUM_LIT:2>*log(T)/<NUM_LIT> - (<NUM_LIT>*T**<NUM_LIT:5>*Tc**<NUM_LIT:3> + <NUM_LIT>*Tc**<NUM_LIT:8>)/(<NUM_LIT>*T**<NUM_LIT:6>)<EOL>B2 = Tc**<NUM_LIT:6>/(<NUM_LIT:20>*T**<NUM_LIT:4>)<EOL>B3 = -Tc**<NUM_LIT:8>/(<NUM_LIT>*T**<NUM_LIT:6>)<EOL><DEDENT>else: <EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>if a == <NUM_LIT:0> and b == <NUM_LIT:0> and species_type != '<STR_LIT>':<EOL><INDENT>if species_type == '<STR_LIT>' or species_type == '<STR_LIT>':<EOL><INDENT>a, b = <NUM_LIT:0>, <NUM_LIT:0><EOL><DEDENT>elif species_type == '<STR_LIT>':<EOL><INDENT>a, b = <NUM_LIT>, <NUM_LIT><EOL><DEDENT>elif species_type == '<STR_LIT>':<EOL><INDENT>a, b = -<NUM_LIT>, <NUM_LIT:0><EOL><DEDENT>elif dipole != <NUM_LIT:0> and Tc != <NUM_LIT:0> and Pc != <NUM_LIT:0>:<EOL><INDENT>dipole_r = <NUM_LIT>*dipole**<NUM_LIT:2>*(Pc/<NUM_LIT>)/Tc**<NUM_LIT:2><EOL>if (species_type == '<STR_LIT>' or species_type == '<STR_LIT>'<EOL>or species_type == '<STR_LIT>' or species_type == '<STR_LIT>'<EOL>or species_type == '<STR_LIT>' or species_type == '<STR_LIT>'):<EOL><INDENT>a, b = -<NUM_LIT>*dipole_r-<NUM_LIT>*dipole_r**<NUM_LIT:8>, <NUM_LIT:0><EOL><DEDENT>elif (species_type == '<STR_LIT>' or species_type == '<STR_LIT>'<EOL>or species_type == '<STR_LIT>' or species_type == '<STR_LIT>'):<EOL><INDENT>a, b = -<NUM_LIT>*dipole_r**<NUM_LIT:4>-<NUM_LIT>*dipole_r**<NUM_LIT:8>, <NUM_LIT:0><EOL><DEDENT>elif species_type == '<STR_LIT>':<EOL><INDENT>a, b = <NUM_LIT>, <NUM_LIT>+<NUM_LIT>*dipole_r<EOL><DEDENT><DEDENT><DEDENT>Br = B0 + omega*B1 + a*B2 + b*B3<EOL>return Br*R*Tc/Pc<EOL>
r'''Calculates the second virial coefficient using the
    comprehensive model in [1]_. See the notes for the calculation of `a` and
    `b`.

    .. math::
        \frac{BP_c}{RT_c} = B^{(0)} + \omega B^{(1)} + a B^{(2)} + b B^{(3)}

        B^{(0)}=0.1445-0.33/T_r-0.1385/T_r^2-0.0121/T_r^3

        B^{(1)} = 0.0637+0.331/T_r^2-0.423/T_r^3 -0.423/T_r^3 - 0.008/T_r^8

        B^{(2)} = 1/T_r^6

        B^{(3)} = -1/T_r^8

    Parameters
    ----------
    T : float
        Temperature of fluid [K]
    Tc : float
        Critical temperature of fluid [K]
    Pc : float
        Critical pressure of the fluid [Pa]
    omega : float
        Acentric factor for fluid, [-]
    a : float, optional
        Fit parameter, calculated based on species_type if a is not given and
        species_type matches on of the supported chemical classes.
    b : float, optional
        Fit parameter, calculated based on species_type if a is not given and
        species_type matches on of the supported chemical classes.
    species_type : str, optional
        One of .
    dipole : float
        dipole moment, optional, [Debye]
    order : int, optional
        Order of the calculation. 0 for the calculation of B itself; for 1/2/3, 
        the first/second/third derivative of B with respect to temperature; and  
        for -1/-2, the first/second indefinite integral of B with respect to 
        temperature. No other integrals or derivatives are implemented, and an 
        exception will be raised if any other order is given.

    Returns
    -------
    B : float
        Second virial coefficient in density form or its integral/derivative if
        specified, [m^3/mol or m^3/mol/K^order]

    Notes
    -----
    Analytical models for derivatives and integrals are available for orders
    -2, -1, 1, 2, and 3, all obtained with SymPy.


    To calculate `a` or `b`, the following rules are used:

    For 'simple' or 'normal' fluids:

    .. math::
        a = 0

        b = 0

    For 'ketone', 'aldehyde', 'alkyl nitrile', 'ether', 'carboxylic acid',
    or 'ester' types of chemicals:

    .. math::
        a = -2.14\times 10^{-4} \mu_r - 4.308 \times 10^{-21} (\mu_r)^8

        b = 0

    For 'alkyl halide', 'mercaptan', 'sulfide', or 'disulfide' types of 
    chemicals:

    .. math::
        a = -2.188\times 10^{-4} (\mu_r)^4 - 7.831 \times 10^{-21} (\mu_r)^8

        b = 0

    For 'alkanol' types of chemicals (except methanol):

    .. math::
        a = 0.0878

        b = 0.00908 + 0.0006957 \mu_r

    For methanol:

    .. math::
        a = 0.0878

        b = 0.0525

    For water:

    .. math::
        a = -0.0109

        b = 0

    If required, the form of dipole moment used in the calculation of some
    types of `a` and `b` values is as follows:

    .. math::
        \mu_r = 100000\frac{\mu^2(Pc/101325.0)}{Tc^2}


    For first temperature derivative of B:

    .. math::
        \frac{d B^{(0)}}{dT} = \frac{33 Tc}{100 T^{2}} + \frac{277 Tc^{2}}{1000 T^{3}} + \frac{363 Tc^{3}}{10000 T^{4}} + \frac{607 Tc^{8}}{125000 T^{9}}

        \frac{d B^{(1)}}{dT} = - \frac{331 Tc^{2}}{500 T^{3}} + \frac{1269 Tc^{3}}{1000 T^{4}} + \frac{8 Tc^{8}}{125 T^{9}}

        \frac{d B^{(2)}}{dT} = - \frac{6 Tc^{6}}{T^{7}}

        \frac{d B^{(3)}}{dT} = \frac{8 Tc^{8}}{T^{9}}

    For the second temperature derivative of B:

    .. math::
        \frac{d^2 B^{(0)}}{dT^2} = - \frac{3 Tc}{125000 T^{3}} \left(27500 + \frac{34625 Tc}{T} + \frac{6050 Tc^{2}}{T^{2}} + \frac{1821 Tc^{7}}{T^{7}}\right)

        \frac{d^2 B^{(1)}}{dT^2} = \frac{3 Tc^{2}}{500 T^{4}} \left(331 - \frac{846 Tc}{T} - \frac{96 Tc^{6}}{T^{6}}\right)

        \frac{d^2 B^{(2)}}{dT^2} = \frac{42 Tc^{6}}{T^{8}}

        \frac{d^2 B^{(3)}}{dT^2} = - \frac{72 Tc^{8}}{T^{10}}

    For the third temperature derivative of B:

    .. math::
        \frac{d^3 B^{(0)}}{dT^3} = \frac{3 Tc}{12500 T^{4}} \left(8250 + \frac{13850 Tc}{T} + \frac{3025 Tc^{2}}{T^{2}} + \frac{1821 Tc^{7}}{T^{7}}\right)

        \frac{d^3 B^{(1)}}{dT^3} = \frac{3 Tc^{2}}{250 T^{5}} \left(-662 + \frac{2115 Tc}{T} + \frac{480 Tc^{6}}{T^{6}}\right)

        \frac{d^3 B^{(2)}}{dT^3} = - \frac{336 Tc^{6}}{T^{9}}

        \frac{d^3 B^{(3)}}{dT^3} = \frac{720 Tc^{8}}{T^{11}}

    For the first indefinite integral of B:

    .. math::
        \int{B^{(0)}} dT = \frac{289 T}{2000} - \frac{33 Tc}{100} \log{\left (T \right )} + \frac{1}{7000000 T^{7}} \left(969500 T^{6} Tc^{2} + 42350 T^{5} Tc^{3} + 607 Tc^{8}\right)

        \int{B^{(1)}} dT = \frac{637 T}{10000} - \frac{1}{70000 T^{7}} \left(23170 T^{6} Tc^{2} - 14805 T^{5} Tc^{3} - 80 Tc^{8}\right)

        \int{B^{(2)}} dT = - \frac{Tc^{6}}{5 T^{5}}

        \int{B^{(3)}} dT = \frac{Tc^{8}}{7 T^{7}}

    For the second indefinite integral of B:

    .. math::
        \int\int B^{(0)} dT dT = \frac{289 T^{2}}{4000} - \frac{33 T}{100} Tc \log{\left (T \right )} + \frac{33 T}{100} Tc + \frac{277 Tc^{2}}{2000} \log{\left (T \right )} - \frac{1}{42000000 T^{6}} \left(254100 T^{5} Tc^{3} + 607 Tc^{8}\right)

        \int\int B^{(1)} dT dT = \frac{637 T^{2}}{20000} - \frac{331 Tc^{2}}{1000} \log{\left (T \right )} - \frac{1}{210000 T^{6}} \left(44415 T^{5} Tc^{3} + 40 Tc^{8}\right)

        \int\int B^{(2)} dT dT = \frac{Tc^{6}}{20 T^{4}}

        \int\int B^{(3)} dT dT = - \frac{Tc^{8}}{42 T^{6}}

    Examples
    --------
    Example from Perry's Handbook, 8E, p2-499. Matches to a decimal place.

    >>> BVirial_Tsonopoulos_extended(430., 405.65, 11.28E6, 0.252608, a=0, b=0, species_type='ketone', dipole=1.469)
    -9.679715056695323e-05

    References
    ----------
    .. [1] Tsonopoulos, C., and J. L. Heidman. "From the Virial to the Cubic
       Equation of State." Fluid Phase Equilibria 57, no. 3 (1990): 261-76.
       doi:10.1016/0378-3812(90)85126-U
    .. [2] Tsonopoulos, Constantine, and John H. Dymond. "Second Virial
       Coefficients of Normal Alkanes, Linear 1-Alkanols (and Water), Alkyl
       Ethers, and Their Mixtures." Fluid Phase Equilibria, International
       Workshop on Vapour-Liquid Equilibria and Related Properties in Binary
       and Ternary Mixtures of Ethers, Alkanes and Alkanols, 133, no. 1-2
       (June 1997): 11-34. doi:10.1016/S0378-3812(97)00058-7.