model ResistorTableAx
    import HydraulicsByFluidon.Media.Base.FluidInterface;

    parameter Boolean tableFromFile = false "Table is provided by file"
        annotation (Dialog(group = "Look-up table definition A(x)"));
    parameter String fileName = "NoFile" "File where look-up table is stored"
        annotation (Dialog(
            group = "Look-up table definition A(x)",
                filter = "Text files (*.txt);;MATLAB MAT-files (*.mat)",
                caption = "Open file in which table is present")));
    parameter String tableName = "NoName" "Table name on file"
        annotation (Dialog(group = "Look-up table definition A(x)"));
    parameter Real manualTable[:,:] = [0,0; 1,2.444e-5] "Manually provided look-up table"
        annotation (Dialog(
            group = "Look-up table definition A(x)",
            enable = not tableFromFile));
    parameter Real alphaD = 0.6 "Flow coefficient alphaD"
        annotation (Dialog(group = "Flow parameters"));

    extends HydraulicsByFluidon.Components.Base.PartialResistorBase;

    Modelica.Blocks.Interfaces.RealInput u annotation (Placement(
        visible = true,
            origin = {-100, 0},
            extent = {
                {-20, -20}, 
                {20, 20}},
            rotation = 0),
            origin = {-80, 0},
            extent = {
                {-20, -20}, 
                {20, 20}},
            rotation = 0)));
    Modelica.Blocks.Tables.CombiTable1Ds inputTable(table = manualTable, tableOnFile = tableFromFile, tableName = tableName, fileName = fileName, smoothness = Modelica.Blocks.Types.Smoothness.LinearSegments, extrapolation = Modelica.Blocks.Types.Extrapolation.HoldLastPoint) annotation (Placement(
        visible = true,
            origin = {-50, 0},
            extent = {
                {-10, -10}, 
                {10, 10}},
            rotation = 0)));
    Modelica.SIunits.Area InputArea;
    InputArea = inputTable.y[1];
    fluidPortB.mFlow = alphaD * InputArea * sign(dp) * sqrt(2 * abs(dp) * FluidInterface.calcRho(fluidId, fluidPortB.p, fluidPortB.fluidTemperature));
    inputTable.u = u;

    annotation (
            coordinateSystem(initialScale = 0.1),
            graphics = {
                Line(points = {
                    {-26, -13}, 
                    {26, 13}}), 
                    origin = {40, 20},
                    rotation = 297,
                    fillPattern = FillPattern.Solid,
                    points = {
                        {0, 0}, 
                        {-5, -20}, 
                        {5, -20}, 
                        {0, 0}})}),
        Documentation(info = "<html>\n            <p>\n                The component ResistorTableAx is a model of a flow resistance that behaves like an orifice.\n            </p>\n            <p>\n                Based on the parameter <var>Flow coefficient alphaD</var> and a look-up table  for the <i>Cross-sectional area</i>\n                as a function of the input signal, the flow rate is calculated according to the formula given below:\n            </p>\n            <p>\n                <center><img align=\"middle\" src=\"modelica://HydraulicsByFluidon/Resources/Images/Components/Resistors/TurbulentFlow.png\"></center>\n            </p>\n            <p>\n                <center><img align=\"middle\" src=\"modelica://HydraulicsByFluidon/Resources/Images/Components/Resistors/TurbulentIGraph.png\"></center>\n            </p>\n            <p>\n                The relationship between input signal and <i>Cross-sectional area</i> of the resistor is described by a 1D look-up table.\n                The look-up table is provided by importing a text file or can be given manually in the dialog. If the parameter\n                <var>Table is provided by file</var> is set to <var>false</var>, the datapoints from <var>Manually provided look-up table</var>\n                will be used. If it is set to <var>true</var>, the table from the text file will be utilized. The text file must follow a specific\n                syntax such that it can be read by Modelica. An example for a properly formatted text file with input signal and output area is given\n                in the figure below: \n            </p>\n            <p>\n                <center><img src=\"modelica://HydraulicsByFluidon/Resources/Images/Components/Resistors/ResistorTableAx.png\"></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)).\n                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\n                be arranged in increasing order. The output of the table must be the cross-sectional area (in m&sup2;). The component interpolates \n                linearly between the listed input values. More information regarding the format of tables can be found in the \n                component <a href=\"modelica://Modelica.Blocks.Tables.CombiTable1Ds\">CombiTable1Ds</a>.\n            </p>\n        </html>"));
end ResistorTableAx;