import math


def cv_vs_time(Cv_max,t,time_constant=0,characteristic="linear"):
    """
    Control valve flow coefficient vs time / actuator postion
    assuming a linear rate of actuator for the three archetypes of 
    characteristics: linear, equal percentage and fast/quick opening. 
    
    Parameters
    ----------
    Cv_max : float 
        Valve flow coefficient at full open position
    t : float
        Time 
    time_constant : float (optional)
        The time required for the actuator to fully open. 
        Default to instant open
    characteristic : string (optional)
        Valve characteristic
        Default to linear.
    """

    if time_constant == 0:
        return Cv_max
    else:
        if characteristic=="linear":
            return Cv_max * min(t/time_constant,1)
        elif characteristic=="eq":
            # https://www.spiraxsarco.com/learn-about-steam/control-hardware-electric-pneumatic-actuation/control-valve-characteristics
            tau=50
            travel=min(t/time_constant,1)
            return Cv_max * math.exp( travel * math.log(tau)) / tau
        elif characteristic=="fast":
            # square root function used
            return Cv_max * min(t/time_constant,1)**(0.5)
        else:
            return Cv_max