SimpleElbow

model SimpleElbow "Elbow"
    import HydraulicsByFluidon.Media.Base.FluidInterface;

    outer HydraulicsByFluidon.Media.Environment environment;
    parameter Modelica.SIunits.Length Diameter = 0.032 "Diameter";
    parameter Modelica.SIunits.Angle Angle(min = 0.005555555555556 * (30 * Modelica.Constants.pi), max = 0.005555555555556 * (120 * Modelica.Constants.pi)) = 0.5 * Modelica.Constants.pi "Elbow angle";
    parameter Boolean forwardFluidProperties = true "Forward fluid properties between ports"
        annotation (
            Dialog(tab = "Fluid Properties"),
            choices(checkBox = true));
    HydraulicsByFluidon.Interfaces.FluidPort fluidPortA(p(start = environment.pAmbient, nominal = 100000)) "Hydraulic port A"
        annotation (Placement(transformation(extent = {
            {-110, -10}, 
            {-90, 10}})));
    HydraulicsByFluidon.Interfaces.FluidPort fluidPortB(p(start = environment.pAmbient, nominal = 100000)) "Hydraulic port B"
        annotation (Placement(transformation(extent = {
            {-10, 110}, 
            {10, 90}})));
protected
    Real A = 0.25 * (Diameter * Diameter * Modelica.Constants.pi);
    Real zetaAngle = 6.666666667e-5 * (Angle * 180 / Modelica.Constants.pi) ^ (2.17);
    Real zeta;
    Real rho;
    Real re;
    Real dp;
    Real mFlow;
    Integer fluidId = fluidPortA.fluidId;
equation
    if forwardFluidProperties then 
        fluidPortA.fluidTemperature = fluidPortB.fluidTemperature;
        fluidPortA.fluidId = fluidPortB.fluidId;
        fluidPortA.proportionUndissolvedAir = fluidPortB.proportionUndissolvedAir;
        fluidPortA.polytropicExponent = fluidPortB.polytropicExponent;
    end if;
    if noEvent(fluidPortB.p < fluidPortA.p) then 
        rho = FluidInterface.calcRho(fluidPortA.fluidId, fluidPortA.p, fluidPortA.fluidTemperature);
        mFlow = fluidPortA.mFlow;
    else 
        rho = FluidInterface.calcRho(fluidPortB.fluidId, fluidPortB.p, fluidPortB.fluidTemperature);
        mFlow = fluidPortB.mFlow;
    end if;
    fluidPortA.mFlow + fluidPortB.mFlow = 0;
    dp = fluidPortB.p - fluidPortA.p;
    re = abs(mFlow / rho * Diameter / (FluidInterface.calcNu(fluidPortB.fluidId, HydraulicsByFluidon.Tools.max(fluidPortA.p, fluidPortB.p), fluidPortB.fluidTemperature) * A));
    zeta = (789 + zetaAngle * re) / max(re, 10);
    fluidPortB.mFlow = sqrt(2 / zeta) * A * sqrt(abs(dp) * rho) * noEvent(sign(dp));

    annotation (
        Icon(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -40}, 
                    {40, 100}}),
            graphics = {
                Polygon(
                    fillColor = {255, 255, 255},
                    fillPattern = FillPattern.Solid,
                    points = {
                        {-90, -30}, 
                        {30, -30}, 
                        {30, 90}, 
                        {-30, 90}, 
                        {-30, 30}, 
                        {-90, 30}, 
                        {-90, -30}}), 
                Line(points = {
                    {30, -30}, 
                    {-30, 30}})}),
        Documentation(info = "<html>\n            <p>\n                The component Elbow is a valid model for <var>Elbow angle</var> between 30° and 120°.\n            </p>\n            <p>\n                <center><img align=\"middle\" src=\"modelica://HydraulicsByFluidon/Resources/Images/Components/Lines/ElbowSketch.png\"></center>\n            </p>\n            <p>\n                <b>Please consider:</b> The component was modeled in order to consider the pressure losses of sharp edged elbows. However, the use of \n                the component can lead to extremely long simulation times, since a small stepsize must be selected due to  \n                the low resistance of an elbow.\n            </p>\n            <p>\n                Formulas are taken from W. Wagner, Strömung und Druckverlust, 7. Auflage 2012, and Will, D., Hydraulik, Grundlagen, Komponenten, Schaltungen, 5. Auflage 2011.\n            </p></html>"));
end SimpleElbow;