def loss_coefficient_piping(d, D1=None, D2=None):
    r'''Calculates the sum of loss coefficients from possible
    inlet/outlet reducers/expanders around a control valve according to
    IEC 60534 calculations.

    .. math::
        \Sigma \xi = \xi_1 + \xi_2 + \xi_{B1} - \xi_{B2}

    .. math::
        \xi_1 = 0.5\left[1 -\left(\frac{d}{D_1}\right)^2\right]^2

    .. math::
        \xi_2 = 1.0\left[1 -\left(\frac{d}{D_2}\right)^2\right]^2

    .. math::
        \xi_{B1} = 1 - \left(\frac{d}{D_1}\right)^4

    .. math::
        \xi_{B2} = 1 - \left(\frac{d}{D_2}\right)^4

    Parameters
    ----------
    d : float
        Diameter of the valve [m]
    D1 : float
        Diameter of the pipe before the valve [m]
    D2 : float
        Diameter of the pipe after the valve [m]

    Returns
    -------
    loss : float
        Sum of the four loss coefficients [-]

    Examples
    --------
    In example 3, non-choked compressible flow with fittings:

    >>> loss_coefficient_piping(0.05, 0.08, 0.1)
    0.6580810546875

    References
    ----------
    .. [1] IEC 60534-2-1 / ISA-75.01.01-2007
    '''
    loss = 0.
    if D1:
        loss += 1. - (d/D1)**4 # Inlet flow energy
        loss += 0.5*(1. - (d/D1)**2)**2 # Inlet reducer
    if D2:
        loss += 1.0*(1. - (d/D2)**2)**2 # Outlet reducer (expander)
        loss -= 1. - (d/D2)**4 # Outlet flow energy
    return loss
r'''Calculates the sum of loss coefficients from possible
    inlet/outlet reducers/expanders around a control valve according to
    IEC 60534 calculations.

    .. math::
        \Sigma \xi = \xi_1 + \xi_2 + \xi_{B1} - \xi_{B2}

    .. math::
        \xi_1 = 0.5\left[1 -\left(\frac{d}{D_1}\right)^2\right]^2

    .. math::
        \xi_2 = 1.0\left[1 -\left(\frac{d}{D_2}\right)^2\right]^2

    .. math::
        \xi_{B1} = 1 - \left(\frac{d}{D_1}\right)^4

    .. math::
        \xi_{B2} = 1 - \left(\frac{d}{D_2}\right)^4

    Parameters
    ----------
    d : float
        Diameter of the valve [m]
    D1 : float
        Diameter of the pipe before the valve [m]
    D2 : float
        Diameter of the pipe after the valve [m]

    Returns
    -------
    loss : float
        Sum of the four loss coefficients [-]

    Examples
    --------
    In example 3, non-choked compressible flow with fittings:

    >>> loss_coefficient_piping(0.05, 0.08, 0.1)
    0.6580810546875

    References
    ----------
    .. [1] IEC 60534-2-1 / ISA-75.01.01-2007