model ValveVaporizing "Valve for possibly vaporizing (almost) incompressible fluids, accounts for choked flow conditions"
    import Modelica.Fluid.Types.CvTypes;
    import Modelica.Constants.pi;

    extends BaseClasses.PartialValve(redeclare replaceable package Medium = Modelica.Media.Water.WaterIF97_ph);

    parameter Real Fl_nominal = 0.9 "Liquid pressure recovery factor";

    replaceable function FlCharacteristic = constrainedby Modelica.Fluid.Valves.BaseClasses.ValveCharacteristics.baseFun "Pressure recovery characteristic";

    Real Ff "Ff coefficient (see IEC/ISA standard)";
    Real Fl "Pressure recovery coefficient Fl (see IEC/ISA standard)";
    SI.Pressure dpEff "Effective pressure drop";
    Medium.Temperature T_in "Inlet temperature";
    Medium.AbsolutePressure p_sat "Saturation pressure";
    Medium.AbsolutePressure p_in "Inlet pressure";
    Medium.AbsolutePressure p_out "Outlet pressure";
    constant SI.ReynoldsNumber Re_turbulent = 4000 "cf. straight pipe for fully open valve -- dp_turbulent increases for closing valve";
    parameter Boolean use_Re = system.use_eps_Re "= true, if turbulent region is defined by Re, otherwise by m_flow_small"
        annotation (
            Dialog(tab = "Advanced"),
            Evaluate = true);
    SI.AbsolutePressure dp_turbulent = if not use_Re then dp_small else max(dp_small, 0.125 * ((Medium.dynamicViscosity(state_a) + Medium.dynamicViscosity(state_b)) ^ 2 * pi) * Re_turbulent ^ 2 / (valveCharacteristic(opening_actual) * Av * (Medium.density(state_a) + Medium.density(state_b))));
initial equation
    assert(not CvData == CvTypes.OpPoint, "OpPoint option not supported for vaporizing valve");
    if checkValve then 
        m_flow = homotopy(valveCharacteristic(opening_actual) * Av * sqrt(Medium.density(state_a)) * Utilities.regRoot2(dpEff, dp_turbulent, 1, 0, use_yd0 = true, yd0 = 0), valveCharacteristic(opening_actual) * m_flow_nominal * dp / dp_nominal);
    elseif not allowFlowReversal then 
        m_flow = homotopy(valveCharacteristic(opening_actual) * Av * sqrt(Medium.density(state_a)) * Utilities.regRoot(dpEff, dp_turbulent), valveCharacteristic(opening_actual) * m_flow_nominal * dp / dp_nominal);
        m_flow = homotopy(valveCharacteristic(opening_actual) * Av * Utilities.regRoot2(dpEff, dp_turbulent, Medium.density(state_a), Medium.density(state_b)), valveCharacteristic(opening_actual) * m_flow_nominal * dp / dp_nominal);
    end if;
    Ff = 0.96 - 0.28 * sqrt(p_sat / Medium.fluidConstants[1].criticalPressure);
    Fl = Fl_nominal * FlCharacteristic(opening_actual);
    T_in = Medium.temperature(state_a);
    p_in = port_a.p;
    p_out = port_b.p;
    p_sat = Medium.saturationPressure(T_in);
    dpEff = if p_out < (1 - Fl ^ 2) * p_in + Ff * Fl ^ 2 * p_sat then Fl ^ 2 * (p_in - Ff * p_sat) else dp "Effective pressure drop, accounting for possible choked conditions";

    annotation (Documentation(
        info = "<html>\n<p>Valve model according to the IEC 534/ISA S.75 standards for valve sizing, incompressible fluid at the inlet, and possibly two-phase fluid at the outlet, including choked flow conditions.</p>\n\n<p>\nThe parameters of this model are explained in detail in\n<a href=\"modelica://Modelica.Fluid.Valves.BaseClasses.PartialValve\">PartialValve</a>\n(the base model for valves).\n</p>\n\n<p>The model operating range includes choked flow operation, which takes place for low outlet pressures due to flashing in the vena contracta; otherwise, non-choking conditions are assumed.</p>\n<p>This model requires a two-phase medium model, to describe the liquid and (possible) two-phase conditions.</p>\n<p>The default liquid pressure recovery coefficient <code>Fl</code> is constant and given by the parameter <code>Fl_nominal</code>. The relative change (per unit) of the recovery coefficient can be specified as a given function of the valve opening by replacing the <code>FlCharacteristic</code> function.</p>\n<p>If <code>checkValve</code> is false, the valve supports reverse flow, with a symmetric flow characteristic curve. Otherwise, reverse flow is stopped (check valve behaviour).</p>\n\n<p>\nThe treatment of parameters <strong>Kv</strong> and <strong>Cv</strong> is\nexplained in detail in the\n<a href=\"modelica://Modelica.Fluid.UsersGuide.ComponentDefinition.ValveCharacteristics\">User's Guide</a>.\n</p>\n\n</html>",
        revisions = "<html>\n<ul>\n<li><em>2 Nov 2005</em>\n    by <a href=\"\">Francesco Casella</a>:<br>\n       Adapted from the ThermoPower library.</li>\n</ul>\n</html>"));
end ValveVaporizing;