FixedBoundary

model FixedBoundary "Boundary source component"
    import Modelica.Media.Interfaces.Choices.IndependentVariables;

    extends Sources.BaseClasses.PartialSource;

    parameter Boolean use_p = true "select p or d"
        annotation (
            Evaluate = true,
            Dialog(group = "Boundary pressure or Boundary density"));
    parameter Medium.AbsolutePressure p = Medium.p_default "Boundary pressure"
        annotation (Dialog(
            group = "Boundary pressure or Boundary density",
            enable = use_p));
    parameter Medium.Density d = if use_T then Medium.density_pTX(Medium.p_default, Medium.T_default, Medium.X_default) else Medium.density_phX(Medium.p_default, Medium.h_default, Medium.X_default) "Boundary density"
        annotation (Dialog(
            group = "Boundary pressure or Boundary density",
            enable = not use_p));
    parameter Boolean use_T = true "select T or h"
        annotation (
            Evaluate = true,
            Dialog(group = "Boundary temperature or Boundary specific enthalpy"));
    parameter Medium.Temperature T = Medium.T_default "Boundary temperature"
        annotation (Dialog(
            group = "Boundary temperature or Boundary specific enthalpy",
            enable = use_T));
    parameter Medium.SpecificEnthalpy h = Medium.h_default "Boundary specific enthalpy"
        annotation (Dialog(
            group = "Boundary temperature or Boundary specific enthalpy",
            enable = not use_T));
    parameter Medium.MassFraction X[Medium.nX](quantity = Medium.substanceNames) = Medium.X_default "Boundary mass fractions m_i/m"
        annotation (Dialog(
            group = "Only for multi-substance flow",
            enable = 0 < Medium.nXi));
    parameter Medium.ExtraProperty C[Medium.nC](quantity = Medium.extraPropertiesNames) = Medium.C_default "Boundary trace substances"
        annotation (Dialog(
            group = "Only for trace-substance flow",
            enable = 0 < Medium.nC));
protected
    Medium.ThermodynamicState state;
equation
    if use_p or Medium.singleState then 
        if use_T then 
            state = Medium.setState_pTX(p, T, X);
        else 
            state = Medium.setState_phX(p, h, X);
        end if;
        if Medium.ThermoStates == IndependentVariables.dTX then 
            medium.d = Medium.density(state);
        else 
            medium.p = Medium.pressure(state);
        end if;
        if Medium.ThermoStates == IndependentVariables.ph or Medium.ThermoStates == IndependentVariables.phX then 
            medium.h = Medium.specificEnthalpy(state);
        else 
            medium.T = Medium.temperature(state);
        end if;
    else 
        if use_T then 
            state = Medium.setState_dTX(d, T, X);
            if Medium.ThermoStates == IndependentVariables.dTX then 
                medium.d = Medium.density(state);
            else 
                medium.p = Medium.pressure(state);
            end if;
            if Medium.ThermoStates == IndependentVariables.ph or Medium.ThermoStates == IndependentVariables.phX then 
                medium.h = Medium.specificEnthalpy(state);
            else 
                medium.T = Medium.temperature(state);
            end if;
        else 
            medium.d = d;
            medium.h = h;
            state = Medium.setState_dTX(d, T, X);
        end if;
    end if;
    Modelica.Fluid.Utilities.checkBoundary(Medium.mediumName, Medium.substanceNames, Medium.singleState, use_p, X, "FixedBoundary");
    medium.Xi = X[1:Medium.nXi];
    ports.C_outflow = fill(C, nPorts);

    annotation (
        defaultComponentName = "boundary",
        Icon(
            coordinateSystem(
                preserveAspectRatio = false,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Ellipse(
                    extent = {
                        {-100, 100}, 
                        {100, -100}},
                    fillPattern = FillPattern.Sphere,
                    fillColor = {0, 127, 255}), 
                Text(
                    extent = {
                        {-150, 110}, 
                        {150, 150}},
                    textString = "%name",
                    lineColor = {0, 0, 255})}),
        Documentation(info = "<html>\n<p>\nModel <strong>FixedBoundary</strong> defines constant values for boundary conditions:\n</p>\n<ul>\n<li> Boundary pressure or boundary density.</li>\n<li> Boundary temperature or boundary specific enthalpy.</li>\n<li> Boundary composition (only for multi-substance or trace-substance flow).</li>\n</ul>\n<p>\nNote, that boundary temperature, density, specific enthalpy,\nmass fractions and trace substances have only an effect if the mass flow\nis from the Boundary into the port. If mass is flowing from\nthe port into the boundary, the boundary definitions,\nwith exception of boundary pressure, do not have an effect.\n</p>\n</html>"));
end FixedBoundary;