SwitchingValve43

model SwitchingValve43
    parameter Real SwitchingThreshold = 0.5 "Input threshold to open the valve"
        annotation (Dialog(group = "Control Parameters"));
    parameter Modelica.SIunits.Time Topen = 0.1 "Time to open 0 % - 100 %"
        annotation (Dialog(group = "Control Parameters"));
    parameter Modelica.SIunits.Time Tclose = 0.1 "Time to close 100 % - 0 %"
        annotation (Dialog(group = "Control Parameters"));

    extends HydraulicsByFluidon.Components.Valves.Base.PartialValve43;
    extends HydraulicsByFluidon.Components.Valves.Base.PartialValveControlSignal_x3;

    Modelica.SIunits.DimensionlessRatio stroke(start = 0, min = -1, max = 1) "rel. Position of valve";
protected
    parameter Real gradientOpen = Topen ^ (-1);
    parameter Real gradientClose = Tclose ^ (-1);
    discrete Real gradient(start = 0);
    Integer ctrlState;
    Integer t1;
    Integer t2;

    function initGradient
        input Real gradientOpen;
        input Real gradientClose;
        input Real inputValue;
        input Real switchingThreshold;
        input Real stroke;
        output Real gradient;
    algorithm
        if switchingThreshold < inputValue and 1 <= stroke then 
            gradient := 0;
        elseif switchingThreshold < inputValue and 0 <= stroke then 
            gradient := gradientOpen;
        elseif switchingThreshold < inputValue and stroke < 0 then 
            gradient := gradientClose;
        elseif inputValue < -switchingThreshold and stroke <= -1 then 
            gradient := 0;
        elseif inputValue < -switchingThreshold and stroke <= 0 then 
            gradient := -gradientOpen;
        elseif inputValue < -switchingThreshold and 0 < stroke then 
            gradient := -gradientClose;
        elseif abs(inputValue) <= switchingThreshold and 0 < stroke then 
            gradient := -gradientClose;
        elseif abs(inputValue) <= switchingThreshold and stroke < 0 then 
            gradient := gradientClose;
        else 
            gradient := 0;
        end if;
    end initGradient;
initial equation
    gradient = initGradient(gradientOpen, gradientClose, Input, SwitchingThreshold, stroke);
equation
    if enableStrokeOut then 
        valveStrokeOut = stroke;
    end if;
    if 1 <= stroke then 
        t1 = 1;
    else 
        t1 = 0;
    end if;
    if stroke <= -1 then 
        t2 = 1;
    else 
        t2 = 0;
    end if;
    if SwitchingThreshold <= Input then 
        ctrlState = 2;
    elseif Input <= -SwitchingThreshold then 
        ctrlState = -2;
    elseif 0 < Input then 
        ctrlState = 1;
    elseif Input < 0 then 
        ctrlState = -1;
    else 
        ctrlState = 0;
    end if;
    when t1 == 1 then 
        reinit(stroke, 1);
    elsewhen t2 == 1 then 
        reinit(stroke, -1);
    elsewhen {stroke <= 0, 0 <= stroke} then 
        reinit(stroke, 0);
    end when;
    when t1 == 1 then 
        gradient = 0;
    elsewhen t2 == 1 then 
        gradient = 0;
    elsewhen ctrlState == 2 and 0 <= stroke then 
        gradient = gradientOpen;
    elsewhen ctrlState == 2 and stroke < 0 then 
        gradient = gradientClose;
    elsewhen ctrlState == -2 and stroke <= 0 then 
        gradient = -gradientOpen;
    elsewhen ctrlState == -2 and 0 < stroke then 
        gradient = -gradientClose;
    elsewhen ctrlState == 1 and 0 < stroke then 
        gradient = -gradientClose;
    elsewhen ctrlState == -1 and stroke < 0 then 
        gradient = gradientClose;
    elsewhen {stroke <= 0, 0 <= stroke} then 
        gradient = 0;
    end when;
    gain1.u = stroke;
    valveEdgeBT.Input = stroke;
    valveEdgePA.Input = stroke;
    der(stroke) = gradient;

    annotation (Documentation(info = "<html>\n            <p>\n                The component SwitchingValve43 is a model of a 3-way valve where the flow (at 100 % \n                valve stroke) is set by <var>Nominal volume flow</var> and <var>Nominal pressure difference</var>.\n            </p>\n            <p>\n                <center><img src=\"modelica://HydraulicsByFluidon/Resources/Images/Components/Valves/DirectionalValves/SwitchingValve43.png\"></center>\n            </p>\n            <p>\n                For Input &#8805; <var>Input threshold to open the valve</var> the valve switches in positive direction up to \n                right position, for Input &#8804; - <var>Input threshold to open the valve</var> the valve switches in \n                negative direction up to left position. For - <var>Input threshold to open the valve</var> &#60; \n                Input &#60; <var>Input threshold to open the valve</var> it switches into resting position (middle position).\n            </p>\n            <p>\n                The opening of the valve lasts always <var>Time to open 0 % - 100 %</var>, the closing always <var>Time to close 100 % - 0 %</var>, i. e. the \n                switching from one end stop position to the other lasts <var>Time to close 100 % - 0 %</var> + <var>Time to open 0 % - 100 %</var>.\n            </p>\n            <p>\n                The valve stroke can be shifted by <var>Valve edge general relative overlap</var>. In accordance with common \n                valve parameters a negative overlap will open the valve edge. <var>Valve edge general relative overlap</var> \n                is applied to all valve edges simultaneously. If at least one of the edge-specific values, e. g. \n                <var>Valve edge PA relative overlap</var> is set to a value different from 0, then  \n                <var>Valve edge general relative overlap</var> is ignored and the individual overlaps are used.\n            </p>\n            <p>\n                The default relationship between input signal and flow rate of the metering edge is linear, but can be changed through the use of a 1D look-up table. \n                The look-up table is provided either manually or by importing a text file. If the parameter <var>Table is provided by file</var> is set to <i>false</i>, the \n                manually entered datapoints from <var>Manually provided look-up table</var> will be used. If it is set to <i>true</i>, the table <var>Table name on file</var> \n                from the file <var>File where look-up table is stored</var> will be utilized. \n            </p> \n            <p>\n                The text file must follow a specific syntax such that it can be read by Modelica. The input values as well as the output values must lie within \n                the range from 0 to 1. An output value of 1 (100 %) corresponds to a fully-opened metering edge. An example for a properly formatted text file with \n                two tables is given in the figure below: \n            </p>\n            <p>\n                <center><img src=\"modelica://HydraulicsByFluidon/Resources/Images/Components/Resistors/ModelicaTableIdealResistorVariable.png\" width=\"300\"></center>\n            </p>\n            <p>\n                A table is declared by its datatype (e. g. double), followed by the table name (e. g. tab1) and its dimensions in brackets (e. g. (5,2)). As can \n                be seen, multiple tables can be defined in the same text file. The table declaration is followed by the actual data. The first column of the table \n                represents the input of the 1D table, whereas the second column lists the corresponding output values. The input values of the table must be in \n                increasing order. The component interpolates linearly between the listed input values. More information regarding the format of tables can be found \n                at <a href=\"modelica://Modelica.Blocks.Tables.CombiTable1D\">CombiTable1D</a>.\n            </p>\n        </html>"));
end SwitchingValve43;