SwitchingValve22

model SwitchingValve22
    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.PartialValve22;
    extends HydraulicsByFluidon.Components.Valves.Base.PartialValveControlSignal_x2;

    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);

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

    annotation (Documentation(info = "<html>\n            <p>\n                The component SwitchingValve22 is a model of a 2-way valve which will open\n                for input signals equal or above <var>Input threshold to open the valve</var>.\n            </p>\n            <p>\n                <center><img src=\"modelica://HydraulicsByFluidon/Resources/Images/Components/Valves/DirectionalValves/SwitchingValve22.png\"></center>\n            </p>\n            <p>\n                Opening and closing times of the valve are set by <var>Time to close 100 % - 0 %</var>/<var>Time to open 0 % - 100 %</var>, the movement is linear\n                with constant speed until the respective end stop is reached.\n            </p>\n            <p>\n                The valve stroke can be shifted by <var>Relative overlap</var>. \n                In accordance with common valve parameters a negative overlap will open the valve edge.\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 SwitchingValve22;