def gassmann(K0, Kin, Kfin, Kfout, phi):
    """
    Use Gassmann's equation to perform fluid substitution. Use the bulk modulus
    of a rock saturated with one fluid (or dry frame, Kfin=0) to preduct the
    bulk modulus of a rock second with a second fluid.

    :param K0: Frame mineral modulus (Gpa)
    :param Kin: Input rock modulus (can be fluid saturated or dry)
    :param Kfin: Bulk modulus of the pore-filling fluid of the inital rock
                 (0 if input is the dry-rock modulus)
    :param Kfout: Bulk modulus of the pore-filling fluid of the output
                  (0 if output is dry-rock modulus)
    :param phi: Porosity of the rock
    """
    A = Kfout / (phi*(K0 - Kfout))
    B = Kin / (K0 - Kin)
    C = Kfin / (phi*(K0 - Kfin))
    D = A + B - C

    Kout = K0*D / (1 + D)

    return(Kout)