within Buildings.DHC.ETS.Cooling.Examples;
model Direct "Example model for direct cooling energy transfer station
  with in-building pumping and controlled district return temperature"
  extends Modelica.Icons.Example;
  package Medium=Buildings.Media.Water
    "Water medium";
  parameter Modelica.Units.SI.HeatFlowRate Q_flow_nominal=-18000
    "Nominal cooling load (negative for cooling)"
  parameter Modelica.Units.SI.MassFlowRate mBui_flow_nominal=-Q_flow_nominal/(cp*(18-7))
    "Nominal mass flow rate of building cooling supply"
  parameter Modelica.Units.SI.SpecificHeatCapacity cp=Medium.specificHeatCapacityCp(
    Medium.setState_pTX(
      Medium.p_default,
      Medium.T_default,
      Medium.X_default))
    "Default specific heat capacity of medium";
  Buildings.DHC.ETS.Cooling.Direct cooETS(
    mBui_flow_nominal=mBui_flow_nominal,
    QChiWat_flow_nominal=Q_flow_nominal,
    dpConVal_nominal=50,
    dpCheVal_nominal=6000,
    nPorts_bChiWat=1,
    nPorts_aChiWat=1) "Energy transfer station"
  Modelica.Blocks.Sources.Constant TSetDisRet_min(k=273.15 + 16)
    "Minimum setpoint temperature for district return"
  Buildings.Fluid.Sources.Boundary_pT sinDis(
    redeclare package Medium = Medium,
    p=340000,
    nPorts=1)
    "District-side sink"
  Buildings.Fluid.Sources.Boundary_pT souDis(
    redeclare package Medium = Medium,
    p=350000,
    use_T_in=true,
    T=280.15,
    nPorts=1)
    "District-side source"
  Modelica.Blocks.Sources.RealExpression TDisSupNoi(
    y=(273.15 + 7) + 2*sin(time*4*3.14/86400))
    "Sinusoidal noise signal for district supply temperature"
  Buildings.Fluid.Movers.FlowControlled_m_flow pum(
    redeclare replaceable package Medium = Medium,
    energyDynamics=Modelica.Fluid.Types.Dynamics.FixedInitial,
    m_flow_nominal=mBui_flow_nominal,
    addPowerToMedium=false,
    nominalValuesDefineDefaultPressureCurve=true,
    constantMassFlowRate=mBui_flow_nominal)
    "Building primary pump"
  Buildings.Fluid.HeatExchangers.HeaterCooler_u loa(
    redeclare package Medium = Medium,
    allowFlowReversal=false,
    m_flow_nominal=mBui_flow_nominal,
    show_T=true,
    from_dp=false,
    dp_nominal=100,
    linearizeFlowResistance=true,
    energyDynamics=Modelica.Fluid.Types.Dynamics.FixedInitial,
    Q_flow_nominal=-1)
    "Aggregate building cooling load"
  Modelica.Blocks.Sources.Ramp ram(
    height=1,
    duration(displayUnit="h") = 3600,
    startTime(displayUnit="h"))
    "Ramp load from zero"
  Modelica.Blocks.Sources.CombiTimeTable QCoo(
    table=[
    0,-100E3;
    6,-80E3;
    6,-50E3;
    12,-20E3;
    18,-150E3;
    24,-100E3],
    extrapolation=Modelica.Blocks.Types.Extrapolation.Periodic,
    timeScale=3600)
    "Cooling demand"
  Modelica.Blocks.Math.Product pro
    "Multiplier to ramp load from zero"
  Modelica.Blocks.Math.Gain gai(k=-1/(cp*(16 - 7)))
    "Multiplier gain for calculating m_flow"
equation
  connect(TSetDisRet_min.y,cooETS.TDisRetSet)
  connect(TDisSupNoi.y, souDis.T_in)
  connect(souDis.ports[1], cooETS.port_aSerCoo)
  connect(cooETS.port_bSerCoo, sinDis.ports[1])
  connect(cooETS.ports_bChiWat[1], pum.port_a)
  connect(pum.port_b, loa.port_a)
  connect(loa.port_b, cooETS.ports_aChiWat[1])
  connect(ram.y, pro.u1)
  connect(QCoo.y[1], pro.u2)
  connect(pro.y, loa.u)
  connect(pro.y, gai.u)
  connect(gai.y, pum.m_flow_in)
end Direct;


within Buildings.DHC.ETS.Cooling.Examples;
model Indirect "Example model for indirect cooling energy transfer station
  with a closed chilled water loop on the building side"
  extends Modelica.Icons.Example;
  package Medium=Buildings.Media.Water
    "Water medium";
  parameter Modelica.Units.SI.HeatFlowRate QCoo_flow_nominal = -19000
    "Cooling design flow rate";
  parameter Modelica.Units.SI.TemperatureDifference dT_nominal = 16-7
    "Design temperature difference";
  parameter Modelica.Units.SI.MassFlowRate mBui_flow_nominal=-QCoo_flow_nominal/dT_nominal/cp
    "Nominal mass flow rate of building cooling supply"
  parameter Modelica.Units.SI.SpecificHeatCapacity cp=Medium.specificHeatCapacityCp(
    Medium.setState_pTX(
      Medium.p_default,
      Medium.T_default,
      Medium.X_default))
    "Default specific heat capacity of medium";
  Buildings.DHC.ETS.Cooling.Indirect cooETS(
    QChiWat_flow_nominal=QCoo_flow_nominal,
    mDis_flow_nominal=mBui_flow_nominal,
    mBui_flow_nominal=mBui_flow_nominal,
    dpConVal_nominal=9000,
    dp1_nominal=9000,
    dp2_nominal=9000,
    Q_flow_nominal=QCoo_flow_nominal,
    T_a1_nominal=278.15,
    T_a2_nominal=289.15,
    k=0.1,
    Ti=40,
    yMax=1,
    yMin=0.01,
    controllerType=Modelica.Blocks.Types.SimpleController.PI,
    nPorts_bChiWat=1,
    nPorts_aChiWat=1) "Direct cooling energy transfer station"
  Buildings.Fluid.Sources.Boundary_pT souDis(
    redeclare package Medium = Medium,
    p(displayUnit="Pa") = 300000 + 2*9000,
    use_T_in=false,
    T=278.15,
    nPorts=1)
    "District (primary) source"
  Buildings.Fluid.Sources.Boundary_pT sinDis(
    redeclare package Medium = Medium,
    T=287.15,
    nPorts=1)
    "District (primary) sink"
  Modelica.Blocks.Sources.Constant TSetCHWS(
    k=273.15+7)
    "Setpoint temperature for building chilled water supply"
  Buildings.Fluid.HeatExchangers.HeaterCooler_u loa(
    redeclare package Medium = Medium,
    allowFlowReversal=false,
    m_flow_nominal=mBui_flow_nominal,
    show_T=true,
    from_dp=false,
    dp_nominal=6000,
    linearizeFlowResistance=true,
    energyDynamics=Modelica.Fluid.Types.Dynamics.SteadyStateInitial,
    Q_flow_nominal=-1)
    "Aggregate building cooling load"
  Buildings.Fluid.Movers.FlowControlled_m_flow pumBui(
    redeclare replaceable package Medium = Medium,
    energyDynamics=Modelica.Fluid.Types.Dynamics.SteadyState,
    allowFlowReversal=false,
    m_flow_nominal=mBui_flow_nominal,
    nominalValuesDefineDefaultPressureCurve=true,
    dp_nominal=6000+9000)
    "Building-side (secondary) pump"
  Modelica.Blocks.Sources.CombiTimeTable QCoo(
    table=[
      0,-10E3;
      6,-8E3;
      6,-5E3;
      12,-2E3;
      18,-15E3;
      24,-10E3],
    extrapolation=Modelica.Blocks.Types.Extrapolation.Periodic,
    timeScale=3600)
    "Cooling demand"
  Modelica.Blocks.Math.Product pro
    "Multiplier to ramp load from zero"
  Modelica.Blocks.Sources.Ramp ram(
    height=1,
    duration(displayUnit="h")=18000,
    startTime(displayUnit="h")=3600) "Ramp load from zero"
  Modelica.Blocks.Math.Gain gai(
    k=-1/(cp*dT_nominal))
    "Multiplier gain for calculating m_flow"
  Buildings.Fluid.Sources.Boundary_pT souBui(
    redeclare package Medium = Medium,
    nPorts=1)
    "Building (secondary) source"
equation
  connect(cooETS.ports_bChiWat[1], pumBui.port_a)
  connect(pumBui.port_b, loa.port_a)
  connect(loa.port_b, cooETS.ports_aChiWat[1])
  connect(QCoo.y[1], gai.u)
  connect(gai.y, pumBui.m_flow_in)
  connect(ram.y, pro.u2)
  connect(QCoo.y[1], pro.u1)
  connect(pro.y, loa.u)
  connect(souDis.ports[1], cooETS.port_aSerCoo)
  connect(cooETS.port_bSerCoo, sinDis.ports[1])
  connect(TSetCHWS.y,cooETS.TBuiSupSet)
  connect(pumBui.port_a, souBui.ports[1])
end Indirect;


<html>
</html>

within Buildings.DHC.ETS.Cooling;
package Examples
  extends Modelica.Icons.ExamplesPackage;
end Examples;


Direct heating ETS model for district energy systems with in-building
  pumping and deltaT control. Direct heating energy transfer station (ETS) model with in-building pumping and
deltaT control. The design is based on a typical district heating ETS described
in ASHRAE's District Heating Guide.
As shown in the figure below, the district and building piping are hydronically
coupled. The control valve ensures that the return temperature to the district
heating network is at or below the maximum specified value. This configuration
naturally results in a fluctuating building supply tempearture.

within Buildings.DHC.ETS.Heating;
model Direct "Direct heating ETS model for district energy systems with in-building
  pumping and deltaT control"
  extends
    Buildings.DHC.ETS.BaseClasses.PartialDirect(
      final typ=Buildings.DHC.Types.DistrictSystemType.HeatingGeneration2to4,
      final have_chiWat=false,
      final have_heaWat=true,
      nPorts_aHeaWat=1,
      nPorts_bHeaWat=1);
equation
  connect(senTDisRet.port_b, port_bSerHea)
  connect(port_aSerHea, senTDisSup.port_a)
  connect(ports_aHeaWat[1], senTBuiRet.port_a)
  connect(senTBuiSup.port_b, ports_bHeaWat[1])
end Direct;


Indirect heating energy transfer station for district energy systems. Indirect heating energy transfer station (ETS) model that controls the
building chilled water supply temperature by modulating a primary control valve
on the district supply side. The design is based on a typical district heating
ETS described in ASHRAE's District Heating Guide.
As shown in the figure below, the building pumping design (constant/variable)
is specified on the building side and not within the ETS.

within Buildings.DHC.ETS.Heating;
model Indirect
  "Indirect heating energy transfer station for district energy systems"
  extends
    Buildings.DHC.ETS.BaseClasses.PartialIndirect(
      QHeaWat_flow_nominal=Q_flow_nominal,
      final typ=Buildings.DHC.Types.DistrictSystemType.HeatingGeneration2to4,
      final have_chiWat=false,
      final have_heaWat=true,
      Q_flow_nominal(min=0),
      nPorts_aHeaWat=1,
      nPorts_bHeaWat=1);
equation
  connect(ports_aHeaWat[1], senTBuiRet.port_a)
  connect(port_aSerHea, senTDisSup.port_a)
  connect(senTBuiSup.port_b, ports_bHeaWat[1])
  connect(senTDisRet.port_b, port_bSerHea)
end Indirect;


within Buildings.DHC.ETS;
package Heating "This package contains models for energy transfer stations used in district heating systems"
  extends Modelica.Icons.VariantsPackage;
end Heating;


within Buildings.DHC.ETS.Heating.Examples;
model Direct "Example model for direct heating energy transfer station
  with in-building pumping and controlled district return temperature"
  extends Modelica.Icons.Example;
  package Medium=Buildings.Media.Water
    "Water medium";
  parameter Modelica.Units.SI.HeatFlowRate Q_flow_nominal=18000
    "Nominal heating load"
  parameter Modelica.Units.SI.MassFlowRate mBui_flow_nominal=Q_flow_nominal/(cp*(50-40))
    "Nominal mass flow rate of building heating supply"
  parameter Modelica.Units.SI.SpecificHeatCapacity cp=Medium.specificHeatCapacityCp(
    Medium.setState_pTX(
      Medium.p_default,
      Medium.T_default,
      Medium.X_default))
    "Default specific heat capacity of medium";
  Buildings.DHC.ETS.Heating.Direct heaETS(
    mBui_flow_nominal=mBui_flow_nominal,
    QHeaWat_flow_nominal=Q_flow_nominal,
    dpConVal_nominal=50,
    dpCheVal_nominal=6000,
    nPorts_bHeaWat=1,
    nPorts_aHeaWat=1) "Energy transfer station"
  Modelica.Blocks.Sources.Constant TSetDisRet_max(k=273.15 + 40)
    "Maximum setpoint temperature for district return"
  Buildings.Fluid.Sources.Boundary_pT sinDis(
    redeclare package Medium = Medium,
    p=340000,
    nPorts=1)
    "District-side sink"
  Buildings.Fluid.Sources.Boundary_pT souDis(
    redeclare package Medium = Medium,
    p=350000,
    use_T_in=true,
    T=280.15,
    nPorts=1)
    "District-side source"
  Modelica.Blocks.Sources.RealExpression TDisSupNoi(y=(273.15 + 50) + 2*sin(
        time*4*3.14/86400))
    "Sinusoidal noise signal for district supply temperature"
  Buildings.Fluid.Movers.FlowControlled_m_flow pum(
    redeclare replaceable package Medium = Medium,
    energyDynamics=Modelica.Fluid.Types.Dynamics.FixedInitial,
    m_flow_nominal=mBui_flow_nominal,
    addPowerToMedium=false,
    nominalValuesDefineDefaultPressureCurve=true,
    constantMassFlowRate=mBui_flow_nominal)
    "Building primary pump"
  Buildings.Fluid.HeatExchangers.HeaterCooler_u loa(
    redeclare package Medium = Medium,
    allowFlowReversal=false,
    m_flow_nominal=mBui_flow_nominal,
    show_T=true,
    from_dp=false,
    dp_nominal=100,
    linearizeFlowResistance=true,
    energyDynamics=Modelica.Fluid.Types.Dynamics.FixedInitial,
    Q_flow_nominal=-1)
    "Aggregate building heating load"
  Modelica.Blocks.Sources.Ramp ram(
    height=1,
    duration(displayUnit="h") = 3600,
    startTime(displayUnit="h"))
    "Ramp load from zero"
  Modelica.Blocks.Sources.CombiTimeTable QHea(
    table=[0,100E3; 6,80E3; 6,50E3; 12,20E3; 18,150E3; 24,100E3],
    extrapolation=Modelica.Blocks.Types.Extrapolation.Periodic,
    timeScale=3600) "Heating demand"
  Modelica.Blocks.Math.Product pro
    "Multiplier to ramp load from zero"
  Modelica.Blocks.Math.Gain gai(k=1/(cp*(50 - 40)))
    "Multiplier gain for calculating m_flow"
equation
  connect(TSetDisRet_max.y,heaETS.TDisRetSet)
  connect(TDisSupNoi.y, souDis.T_in)
  connect(pum.port_b, loa.port_a)
  connect(ram.y, pro.u1)
  connect(QHea.y[1], pro.u2)
  connect(pro.y, loa.u)
  connect(pro.y, gai.u)
  connect(gai.y, pum.m_flow_in)
  connect(heaETS.ports_bHeaWat[1], pum.port_a)
  connect(heaETS.ports_aHeaWat[1], loa.port_b)
  connect(heaETS.port_aSerHea, souDis.ports[1])
  connect(sinDis.ports[1], heaETS.port_bSerHea)
end Direct;


within Buildings.DHC.ETS.Heating.Examples;
model Indirect "Example model for indirect heating energy transfer station
  with a closed chilled water loop on the building side"
  extends Modelica.Icons.Example;
  package Medium=Buildings.Media.Water
    "Water medium";
  parameter Modelica.Units.SI.HeatFlowRate QHea_flow_nominal = 19000
    "Heating design flow rate";
  parameter Modelica.Units.SI.TemperatureDifference dT_nominal = 50-40
    "Design temperature difference";
  parameter Modelica.Units.SI.MassFlowRate mBui_flow_nominal=QHea_flow_nominal/dT_nominal/cp
    "Nominal mass flow rate of building heating supply"
  parameter Modelica.Units.SI.SpecificHeatCapacity cp=Medium.specificHeatCapacityCp(
    Medium.setState_pTX(
      Medium.p_default,
      Medium.T_default,
      Medium.X_default))
    "Default specific heat capacity of medium";
  Buildings.DHC.ETS.Heating.Indirect heaETS(
    QHeaWat_flow_nominal=QHea_flow_nominal,
    mDis_flow_nominal=mBui_flow_nominal,
    mBui_flow_nominal=mBui_flow_nominal,
    dpConVal_nominal=9000,
    dp1_nominal=9000,
    dp2_nominal=9000,
    Q_flow_nominal=QHea_flow_nominal,
    T_a1_nominal=328.15,
    T_a2_nominal=313.15,
    k=0.1,
    Ti=40,
    yMax=1,
    yMin=0.01,
    controllerType=Modelica.Blocks.Types.SimpleController.PI,
    nPorts_aHeaWat=1,
    nPorts_bHeaWat=1)
    "Direct heating energy transfer station"
  Buildings.Fluid.Sources.Boundary_pT souDis(
    redeclare package Medium = Medium,
    p(displayUnit="Pa") = 300000 + 2*9000,
    use_T_in=false,
    T=328.15,
    nPorts=1)
    "District (primary) source"
  Buildings.Fluid.Sources.Boundary_pT sinDis(
    redeclare package Medium = Medium,
    T=314.15,
    nPorts=1)
    "District (primary) sink"
  Modelica.Blocks.Sources.Constant TSetHWS(k=273.15 + 50)
    "Setpoint temperature for building heating water supply"
  Buildings.Fluid.HeatExchangers.HeaterCooler_u loa(
    redeclare package Medium = Medium,
    allowFlowReversal=false,
    m_flow_nominal=mBui_flow_nominal,
    show_T=true,
    from_dp=false,
    dp_nominal=6000,
    linearizeFlowResistance=true,
    energyDynamics=Modelica.Fluid.Types.Dynamics.SteadyStateInitial,
    Q_flow_nominal=-1) "Aggregate building heating load"
  Buildings.Fluid.Movers.FlowControlled_m_flow pumBui(
    redeclare replaceable package Medium = Medium,
    energyDynamics=Modelica.Fluid.Types.Dynamics.SteadyState,
    allowFlowReversal=false,
    m_flow_nominal=mBui_flow_nominal,
    nominalValuesDefineDefaultPressureCurve=true,
    dp_nominal=6000+9000)
    "Building-side (secondary) pump"
  Modelica.Blocks.Sources.CombiTimeTable QHea(
    table=[0,10E3; 6,8E3; 6,5E3; 12,2E3; 18,15E3; 24,10E3],
    extrapolation=Modelica.Blocks.Types.Extrapolation.Periodic,
    timeScale=3600) "Heating demand"
  Modelica.Blocks.Math.Product pro
    "Multiplier to ramp load from zero"
  Modelica.Blocks.Sources.Ramp ram(
    height=1,
    duration(displayUnit="h")=18000,
    startTime(displayUnit="h")=3600) "Ramp load from zero"
  Modelica.Blocks.Math.Gain gai(k=1/(cp*dT_nominal))
    "Multiplier gain for calculating m_flow"
  Buildings.Fluid.Sources.Boundary_pT souBui(
    redeclare package Medium = Medium,
    nPorts=1)
    "Building (secondary) source"
equation
  connect(pumBui.port_b, loa.port_a)
  connect(QHea.y[1], gai.u)
  connect(gai.y, pumBui.m_flow_in)
  connect(ram.y, pro.u2)
  connect(QHea.y[1], pro.u1)
  connect(pro.y, loa.u)
  connect(pumBui.port_a, souBui.ports[1])
  connect(TSetHWS.y,heaETS.TBuiSupSet)
  connect(souDis.ports[1], heaETS.port_aSerHea)
  connect(sinDis.ports[1], heaETS.port_bSerHea)
  connect(loa.port_b, heaETS.ports_aHeaWat[1])
  connect(heaETS.ports_bHeaWat[1], pumBui.port_a)
end Indirect;


<html>
</html>

within Buildings.DHC.ETS.Heating;
package Examples
  extends Modelica.Icons.ExamplesPackage;
end Examples;


Collection of cases study

within Buildings.DHC;
package Examples "Collection of cases study"
  extends Modelica.Icons.ExamplesPackage;
end Examples;


within Buildings.DHC.Examples;
package Combined "Package of example models for DHC systems"
  extends Modelica.Icons.VariantsPackage;
end Combined;


within Buildings.DHC.Examples.Combined;
model SeriesConstantFlow
  "Example of series connection with constant district water mass flow rate"
  extends Buildings.DHC.Examples.Combined.BaseClasses.PartialSeries(redeclare
      Buildings.DHC.Loads.Combined.BuildingTimeSeriesWithETS bui[nBui](final filNam=filNam),
      datDes(
      mPumDis_flow_nominal=95,
      mPipDis_flow_nominal=95,
      dp_length_nominal=69.3,
      epsPla=0.935),
    pumSto(dp_nominal=30000));
  parameter String filNam[nBui]={
    "modelica://Buildings/Resources/Data/DHC/Loads/Examples/SwissOffice_20190916.mos",
    "modelica://Buildings/Resources/Data/DHC/Loads/Examples/SwissResidential_20190916.mos",
    "modelica://Buildings/Resources/Data/DHC/Loads/Examples/SwissHospital_20190916.mos"}
    "Library paths of the files with thermal loads as time series";
  Modelica.Blocks.Sources.Constant masFloMaiPum(
    k=datDes.mPumDis_flow_nominal)
    "Distribution pump mass flow rate"
  Modelica.Blocks.Sources.Constant masFloDisPla(
    k=datDes.mPla_flow_nominal)
    "District water flow rate to plant"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant THotWatSupSet[nBui](
    k=fill(63 + 273.15, nBui))
    "Hot water supply temperature set point"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant TColWat[nBui](
    k=fill(15 + 273.15, nBui))
    "Cold water temperature"
  Networks.Distribution1Pipe_R dis(
    redeclare final package Medium = Medium,
    final nCon=nBui,
    show_TOut=true,
    final mDis_flow_nominal=datDes.mPipDis_flow_nominal,
    final mCon_flow_nominal=datDes.mCon_flow_nominal,
    final dp_length_nominal=datDes.dp_length_nominal,
    final lDis=datDes.lDis,
    final lEnd=datDes.lEnd,
    final allowFlowReversal=allowFlowReversalSer) "Distribution network"
  Modelica.Blocks.Sources.Constant TSewWat(k=273.15 + 17)
    "Sewage water temperature"
equation
  connect(masFloMaiPum.y, pumDis.m_flow_in)
  connect(pumSto.m_flow_in, masFloMaiPum.y)
  connect(masFloDisPla.y, pla.mPum_flow)
  connect(THotWatSupSet.y, bui.THotWatSupSet)
  connect(TColWat.y, bui.TColWat)
  connect(TDisWatSup.port_b, dis.port_aDisSup)
  connect(dis.port_bDisSup, TDisWatRet.port_a)
  connect(TSewWat.y, pla.TSewWat)
  connect(bui.port_bSerAmb, dis.ports_aCon)
  connect(dis.ports_bCon, bui.port_aSerAmb)
end SeriesConstantFlow;


within Buildings.DHC.Examples.Combined;
model SeriesVariableFlow
  "Example of series connection with variable district water mass flow rate"
  extends Buildings.DHC.Examples.Combined.BaseClasses.PartialSeries(redeclare
      Buildings.DHC.Loads.Combined.BuildingTimeSeriesWithETS bui[nBui](final filNam=filNam),
      datDes(
      mPumDis_flow_nominal=97.3,
      mPipDis_flow_nominal=69.5,
      dp_length_nominal=69.3,
      epsPla=0.91),
    pumSto(dp_nominal=30000));
  parameter String filNam[nBui]={
    "modelica://Buildings/Resources/Data/DHC/Loads/Examples/SwissOffice_20190916.mos",
    "modelica://Buildings/Resources/Data/DHC/Loads/Examples/SwissResidential_20190916.mos",
    "modelica://Buildings/Resources/Data/DHC/Loads/Examples/SwissHospital_20190916.mos"}
    "Library paths of the files with thermal loads as time series";
  Modelica.Blocks.Sources.Constant masFloDisPla(
    k=datDes.mPla_flow_nominal)
    "District water flow rate to plant"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant THotWatSupSet[nBui](
    k=fill(63 + 273.15, nBui))
    "Hot water supply temperature set point"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant TColWat[nBui](
    k=fill(15 + 273.15, nBui))
    "Cold water temperature"
  Buildings.DHC.Networks.Controls.MainPump1Pipe conPum(
    nMix=nBui,
    nSou=2,
    nBui=nBui,
    TMin=279.15,
    TMax=290.15,
    use_temperatureShift=false) "Main pump controller"
  Buildings.Controls.OBC.CDL.Reals.MultiplyByParameter gai(k=datDes.mPumDis_flow_nominal)
    "Scale with nominal mass flow rate"
  Networks.Distribution1Pipe_R dis(
    redeclare final package Medium = Medium,
    final nCon=nBui,
    show_TOut=true,
    final mDis_flow_nominal=datDes.mPipDis_flow_nominal,
    final mCon_flow_nominal=datDes.mCon_flow_nominal,
    final dp_length_nominal=datDes.dp_length_nominal,
    final lDis=datDes.lDis,
    final lEnd=datDes.lEnd,
    final allowFlowReversal=allowFlowReversalSer) "Distribution network"
  Modelica.Blocks.Sources.Constant TSewWat(k=273.15 + 17)
    "Sewage water temperature"
equation
  connect(THotWatSupSet.y, bui.THotWatSupSet)
  connect(TColWat.y, bui.TColWat)
  connect(pumDis.m_flow_in, gai.y)
  connect(conPum.y, gai.u)
  connect(dis.TOut, conPum.TMix)
  connect(TDisWatRet.T, conPum.TSouIn[1])
  connect(TDisWatBorLvg.T, conPum.TSouIn[2])
  connect(TDisWatBorLvg.T, conPum.TSouOut[1])
  connect(TDisWatSup.T, conPum.TSouOut[2])
  connect(gai.y, pumSto.m_flow_in)
  connect(masFloDisPla.y, pla.mPum_flow)
  connect(TSewWat.y, pla.TSewWat)
  connect(TDisWatSup.port_b, dis.port_aDisSup)
  connect(TDisWatRet.port_a, dis.port_bDisSup)
  connect(bui.port_aSerAmb, dis.ports_bCon)
  connect(bui.port_bSerAmb, dis.ports_aCon)
  connect(bui.QCoo_flow, conPum.QCoo_flow)
end SeriesVariableFlow;


within Buildings.DHC.Examples.Combined;
model SeriesVariableFlowAgentControl
  "Example of series connection with variable district water mass flow rate with updated agent controller"
  extends Buildings.DHC.Examples.Combined.BaseClasses.PartialSeries(redeclare
      Buildings.DHC.Loads.Combined.BuildingTimeSeriesWithETS bui[nBui](final filNam=filNam),
      datDes(
      mPumDis_flow_nominal=97.3,
      mPipDis_flow_nominal=69.5,
      mSto_flow_nominal=75,
      dpPla_nominal(displayUnit="bar") = 50000,
      TLooMin=279.15,
      dp_length_nominal=250,
      epsPla=0.91),
    pumSto(dp_nominal=40000));
  parameter String filNam[nBui]={
    "modelica://Buildings/Resources/Data/DHC/Loads/Examples/SwissOffice_20190916.mos",
    "modelica://Buildings/Resources/Data/DHC/Loads/Examples/SwissResidential_20190916.mos",
    "modelica://Buildings/Resources/Data/DHC/Loads/Examples/SwissHospital_20190916.mos"}
    "Library paths of the files with thermal loads as time series";
  parameter Modelica.Units.SI.Length diameter=sqrt(4*datDes.mPipDis_flow_nominal/1000/1.5/Modelica.Constants.pi)
    "Pipe diameter (without insulation)";
  parameter Modelica.Units.SI.Height lDisPip=200 "Distribution pipes length";
  parameter Modelica.Units.SI.Radius rPip=diameter/2 "Pipe external radius";
  parameter Modelica.Units.SI.Radius thiGroLay=0.5
    "Dynamic ground layer thickness";
  Modelica.Blocks.Sources.Constant masFloDisPla(
    k=datDes.mPla_flow_nominal)
    "District water flow rate to plant"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant THotWatSupSet[nBui](
    k=fill(63 + 273.15, nBui))
    "Hot water supply temperature set point"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant TColWat[nBui](
    k=fill(15 + 273.15, nBui))
    "Cold water temperature"
  Buildings.DHC.Networks.Controls.MainPump1Pipe conPum(
    nMix=nBui,
    nSou=2,
    nBui=nBui,
    TMin=279.15,
    TMax=290.15,
    dTSlo=1.5) "Main pump controller"
  Buildings.Controls.OBC.CDL.Reals.MultiplyByParameter gai(k=datDes.mPumDis_flow_nominal)
    "Scale with nominal mass flow rate"
  Modelica.Blocks.Sources.CombiTimeTable HXtemperature(table=[0,14 + 273.15; 60
        *86400,12 + 273.15; 210*86400,20 + 273.15; 365*86400,14 + 273.15],
                                     extrapolation=Modelica.Blocks.Types.Extrapolation.Periodic)
  Modelica.Blocks.Math.Product product1
  .Buildings.Controls.OBC.CDL.Reals.MultiplyByParameter gai1(k=datDes.mSto_flow_nominal)
    "Scale with nominal mass flow rate"

  Fluid.Sensors.TemperatureTwoPort TDisWatSup1(redeclare final package Medium =
        Medium, final m_flow_nominal=datDes.mPumDis_flow_nominal)
    "District water supply temperature"
  Fluid.Sensors.TemperatureTwoPort TDisWatRet1(redeclare final package Medium =
        Medium, final m_flow_nominal=datDes.mPumDis_flow_nominal)
    "District water return temperature"
  Fluid.FixedResistances.BuriedPipes.PipeGroundCoupling pipeGroundCouplingMulti[nBui + 3](
    each lPip=lDisPip,
    each rPip=rPip,
    each thiGroLay=thiGroLay,
    each nSeg=1,
    redeclare parameter  Buildings.HeatTransfer.Data.Soil.Generic soiDat(
      each k=2.3,
      each c=1000,
      each d=2600))
  Buildings.DHC.Networks.Controls.AgentPump1Pipe pumPlantControlNsew(
    yPumMin=0,
    dToff=0.5,
    k=0.8,
    Ti=600,
    uLowHea=0.75,
    uHighHea=1.5,
    h=0.5)
  Buildings.DHC.Networks.Controls.AgentPump1Pipe pumPlantControlNsewSto(
    yPumMin=0,
    dToff=0.45,
    k=1.1,
    Ti=600,
    uLowHea=0.25,
    uHighHea=0.5,
    uLowCoo=0.15,
    uHighCoo=0.4)

  Networks.Distribution1PipePlugFlow_v dis(
    nCon=nBui,
    allowFlowReversal=allowFlowReversalSer,
    redeclare package Medium = Medium,
    show_TOut=true,
    mDis_flow_nominal=datDes.mPipDis_flow_nominal,
    mCon_flow_nominal=datDes.mCon_flow_nominal,
    lDis=datDes.lDis,
    lEnd=datDes.lEnd,
    dIns=0.02,
    kIns=0.2)
  Fluid.FixedResistances.PlugFlowPipe supDisPluFlo(
    redeclare package Medium = Medium,
    allowFlowReversal=allowFlowReversalSer,
    m_flow_nominal=datDes.mPipDis_flow_nominal,
    length=lDisPip,
    dIns=0.02,
    kIns=0.2)
  Fluid.FixedResistances.PlugFlowPipe retDisPluFlo(
    redeclare package Medium = Medium,
    allowFlowReversal=allowFlowReversalSer,
    m_flow_nominal=datDes.mPipDis_flow_nominal,
    length=lDisPip,
    dIns=0.02,
    kIns=0.2)
equation
  connect(THotWatSupSet.y, bui.THotWatSupSet)
  connect(TColWat.y, bui.TColWat)
  connect(pumDis.m_flow_in, gai.y)
  connect(conPum.y, gai.u)
  connect(TDisWatRet.T, conPum.TSouIn[1])
  connect(TDisWatBorLvg.T, conPum.TSouIn[2])
  connect(TDisWatBorLvg.T, conPum.TSouOut[1])
  connect(TDisWatSup.T, conPum.TSouOut[2])
  connect(masFloDisPla.y, product1.u1)
  connect(product1.y, pla.mPum_flow)
  connect(gai1.y, pumSto.m_flow_in)
  connect(TDisWatBorLvg.T, pumPlantControlNsew.TSouIn)
  connect(TDisWatSup.T,pumPlantControlNsew.TSouOut)
  connect(TDisWatSup1.T,pumPlantControlNsew.TSupDis)
  connect(TDisWatRet1.T,pumPlantControlNsew.TRetDis)
  connect(borFie.TBorAve, pumPlantControlNsewSto.TSou)
  connect(TDisWatRet.T, pumPlantControlNsewSto.TSouIn)
  connect(TDisWatBorLvg.T,pumPlantControlNsewSto.TSouOut)
  connect(TDisWatSup1.T,pumPlantControlNsewSto.TSupDis)
  connect(TDisWatRet1.T,pumPlantControlNsewSto.TRetDis)
  connect(pumPlantControlNsewSto.y, gai1.u)
  connect(pumPlantControlNsew.y, product1.u2)
  connect(TDisWatSup1.port_b, dis.port_aDisSup)
  connect(dis.port_bDisSup, TDisWatRet1.port_a)
  connect(dis.ports_bCon, bui.port_aSerAmb)
  connect(dis.ports_aCon, bui.port_bSerAmb)
  connect(dis.TOut, conPum.TMix)
  connect(TDisWatSup.port_b, supDisPluFlo.port_a)
  connect(supDisPluFlo.port_b, TDisWatSup1.port_a)
  connect(TDisWatRet1.port_b, retDisPluFlo.port_a)
  connect(retDisPluFlo.port_b, TDisWatRet.port_a)
  connect(pipeGroundCouplingMulti[1:(nBui+1)].heatPorts[1], dis.heatPorts)
  connect(supDisPluFlo.heatPort, pipeGroundCouplingMulti[nBui + 2].heatPorts[1])
  connect(retDisPluFlo.heatPort, pipeGroundCouplingMulti[nBui + 3].heatPorts[1])
  connect(HXtemperature.y[1], pla.TSewWat)
  connect(HXtemperature.y[1], pumPlantControlNsew.TSou)
  connect(bui.QCoo_flow, conPum.QCoo_flow)
end SeriesVariableFlowAgentControl;


Geothermal borefield model. This model represents a borefield composed of 350 boreholes,
with the following main assumptions.

within Buildings.DHC.Examples.Combined.BaseClasses;
model Borefield "Geothermal borefield model"
  extends Buildings.Fluid.Geothermal.Borefields.TwoUTubes(
    final energyDynamics=Modelica.Fluid.Types.Dynamics.FixedInitial,
    final tLoaAgg(displayUnit="h") = 3600,
    final nSeg=5,
    TExt0_start=282.55,
    final z0=10,
    final dT_dz=0.02,
    borFieDat(
      filDat=Buildings.Fluid.Geothermal.Borefields.Data.Filling.Bentonite(
          kFil=2.0,
          cFil=3040,
          dFil=1450),
      soiDat=Buildings.Fluid.Geothermal.Borefields.Data.Soil.SandStone(
          kSoi=2.3,
          cSoi=1000,
          dSoi=2600),
      conDat=Buildings.Fluid.Geothermal.Borefields.Data.Configuration.Example(
        borCon=Buildings.Fluid.Geothermal.Borefields.Types.BoreholeConfiguration.DoubleUTubeParallel,
        dp_nominal=35000,
        hBor=300,
        rBor=0.095,
        nBor=350,
        cooBor=cooBor,
        dBor=1,
        rTub=0.02,
        kTub=0.5,
        eTub=0.0037,
        xC=0.05)),
    show_T=true);
  /*
  Some parameters (such as nBor) cannot be propagated down to
  borFieDat.conDat otherwise Dymola fails to expand.
  We assign them literally within borFieDat.conDat and propagate them up here
  to compute dependent parameters.
  */
  parameter Integer nBor = borFieDat.conDat.nBor
    "Number of boreholes"
  parameter Real dxyBor = 10
    "Distance between boreholes";
  final parameter Modelica.Units.SI.Length cooBor[nBor,2]={dxyBor*{mod(i - 1,
      10),floor((i - 1)/10)} for i in 1:nBor}
    "Cartesian coordinates of the boreholes in meters";
  Buildings.Controls.OBC.CDL.Interfaces.RealOutput Q_flow(final unit="W")
    "Rate at which heat is extracted from soil"
equation
  connect(gaiQ_flow.y, Q_flow)
end Borefield;


within Buildings.DHC.Examples.Combined;
package BaseClasses "Package with base classes that are used by multiple models"
  extends Modelica.Icons.BasesPackage;
end BaseClasses;


Partial model for series network

within Buildings.DHC.Examples.Combined.BaseClasses;
partial model PartialSeries "Partial model for series network"
  extends Modelica.Icons.Example;
  package Medium = Buildings.Media.Water "Medium model";
  constant Real facMul = 10
    "Building loads multiplier factor";
  parameter Real dpDis_length_nominal(final unit="Pa/m") = 250
    "Pressure drop per pipe length at nominal flow rate - Distribution line";
  parameter Real dpCon_length_nominal(final unit="Pa/m") = 250
    "Pressure drop per pipe length at nominal flow rate - Connection line";
  parameter Boolean allowFlowReversalSer = true
    "Set to true to allow flow reversal in the service lines"
  parameter Boolean allowFlowReversalBui = false
    "Set to true to allow flow reversal for in-building systems"
  parameter Modelica.Units.SI.Length dhSto(fixed=false,start=0.05,min=0.01)
    "Hydraulic diameter of the distribution pipe before each connection";
  parameter Modelica.Units.SI.Length dhPla(fixed=false,start=0.05,min=0.01)
    "Hydraulic diameter of the distribution pipe before each connection";
  parameter Integer nBui = datDes.nBui
    "Number of buildings connected to DHC system"
  inner parameter
    Buildings.DHC.Examples.Combined.BaseClasses.DesignDataSeries
    datDes(final mCon_flow_nominal=bui.ets.mSerWat_flow_nominal, lEnd=100)
                                                                 "Design data"
  // COMPONENTS
  Buildings.DHC.Examples.Combined.BaseClasses.Borefield borFie(
      redeclare final package Medium = Medium) "Bore field"
  Buildings.DHC.ETS.BaseClasses.Pump_m_flow pumDis(
    redeclare final package Medium = Medium,
    final m_flow_nominal=datDes.mPumDis_flow_nominal,
    final allowFlowReversal=allowFlowReversalSer,
    dp_nominal=150E3)
    "Distribution pump"
  Buildings.Fluid.Sources.Boundary_pT bou(
    redeclare final package Medium=Medium, nPorts=1)
    "Boundary pressure condition representing the expansion vessel"
  Buildings.DHC.ETS.BaseClasses.Pump_m_flow pumSto(
    redeclare final package Medium = Medium,
    m_flow_nominal=datDes.mSto_flow_nominal)
    "Bore field pump"
  Buildings.DHC.Networks.Connections.Connection1Pipe_R conPla(
    redeclare final package Medium = Medium,
    final mDis_flow_nominal=datDes.mPipDis_flow_nominal,
    final mCon_flow_nominal=datDes.mPla_flow_nominal,
    lDis=50,
    final allowFlowReversal=allowFlowReversalSer,
    dhDis=dhPla)
    "Connection to the plant (pressure drop lumped in plant and network model)"
  Buildings.DHC.Networks.Connections.Connection1Pipe_R conSto(
    redeclare final package Medium = Medium,
    final mDis_flow_nominal=datDes.mPipDis_flow_nominal,
    final mCon_flow_nominal=datDes.mSto_flow_nominal,
    lDis=50,
    final allowFlowReversal=allowFlowReversalSer,
    dhDis=dhSto)
    "Connection to the bore field (pressure drop lumped in plant and network model)"
  Buildings.DHC.Plants.Heating.SewageHeatRecovery
    pla(
    redeclare final package Medium = Medium,
    final mSew_flow_nominal=datDes.mPla_flow_nominal,
    final mDis_flow_nominal=datDes.mPla_flow_nominal,
    final dpSew_nominal=datDes.dpPla_nominal,
    final dpDis_nominal=datDes.dpPla_nominal,
    final epsHex=datDes.epsPla) "Sewage heat recovery plant"
  Fluid.Sensors.TemperatureTwoPort TDisWatSup(
    redeclare final package Medium = Medium,
    final m_flow_nominal=datDes.mPumDis_flow_nominal)
    "District water supply temperature"
  Fluid.Sensors.TemperatureTwoPort TDisWatRet(
    redeclare final package Medium = Medium,
    final m_flow_nominal=datDes.mPumDis_flow_nominal)
    "District water return temperature"
  Fluid.Sensors.TemperatureTwoPort TDisWatBorLvg(
    redeclare final package Medium = Medium,
    final m_flow_nominal=datDes.mPumDis_flow_nominal)
    "District water borefield leaving temperature"
  replaceable
    Buildings.DHC.Loads.Combined.BaseClasses.PartialBuildingWithETS
    bui[nBui] constrainedby
    Buildings.DHC.Loads.Combined.BaseClasses.PartialBuildingWithETS(
    bui(each final facMul=facMul),
    redeclare each final package MediumBui = Medium,
    redeclare each final package MediumSer = Medium,
    each final allowFlowReversalBui=allowFlowReversalBui,
    each final allowFlowReversalSer=allowFlowReversalSer,
    each final TDisWatMin=datDes.TLooMin,
    each final TDisWatMax=datDes.TLooMax) "Building and ETS"
 Buildings.Controls.OBC.CDL.Reals.Sources.Constant THeaWatSupMaxSet[nBui](
    k=bui.THeaWatSup_nominal)
    "Heating water supply temperature set point - Maximum value"
 Buildings.Controls.OBC.CDL.Reals.Sources.Constant TChiWatSupSet[nBui](
    k=bui.TChiWatSup_nominal)
    "Chilled water supply temperature set point"
 Buildings.Controls.OBC.CDL.Reals.Sources.Constant THeaWatSupMinSet[nBui](
    each k=28 + 273.15)
    "Heating water supply temperature set point - Minimum value"
 Buildings.Controls.OBC.CDL.Reals.MultiSum PPumETS(
    final nin=nBui)
    "ETS pump power"
  Modelica.Blocks.Continuous.Integrator EPumETS(
    initType=Modelica.Blocks.Types.Init.InitialState)
    "ETS pump electric energy"
  Modelica.Blocks.Continuous.Integrator EPumDis(
    initType=Modelica.Blocks.Types.Init.InitialState)
    "Distribution pump electric energy"
  Modelica.Blocks.Continuous.Integrator EPumSto(
    initType=Modelica.Blocks.Types.Init.InitialState)
    "Storage pump electric energy"
  Modelica.Blocks.Continuous.Integrator EPumPla(initType=Modelica.Blocks.Types.Init.InitialState)
    "Plant pump electric energy"
 Buildings.Controls.OBC.CDL.Reals.MultiSum EPum(nin=4)
    "Total pump electric energy"
 Buildings.Controls.OBC.CDL.Reals.MultiSum PHeaPump(
    final nin=nBui)
    "Heat pump power"
  Modelica.Blocks.Continuous.Integrator EHeaPum(
    initType=Modelica.Blocks.Types.Init.InitialState)
    "Heat pump electric energy"
 Buildings.Controls.OBC.CDL.Reals.MultiSum ETot(nin=2) "Total electric energy"
  Buildings.DHC.Loads.BaseClasses.ConstraintViolation conVio(
    final uMin(final unit="K", displayUnit="degC")=datDes.TLooMin,
    final uMax(final unit="K", displayUnit="degC")=datDes.TLooMax,
    final nu=3,
    u(each final unit="K", each displayUnit="degC"))
    "Check if loop temperatures are within given range"
equation
  connect(borFie.port_b, conSto.port_aCon)
  connect(pumDis.port_b, conSto.port_aDis)
  connect(borFie.port_a, pumSto.port_b)
  connect(conSto.port_bCon, pumSto.port_a)
  connect(conPla.port_bDis, TDisWatSup.port_a)
  connect(TDisWatRet.port_b, pumDis.port_a)
  connect(conSto.port_bDis, TDisWatBorLvg.port_a)
  connect(TDisWatBorLvg.port_b, conPla.port_aDis)
  connect(THeaWatSupMaxSet.y, bui.THeaWatSupMaxSet)
  connect(TChiWatSupSet.y, bui.TChiWatSupSet)
  connect(pla.port_bSerAmb, conPla.port_aCon)
  connect(conPla.port_bCon, pla.port_aSerAmb)
  connect(THeaWatSupMinSet.y, bui.THeaWatSupMinSet)
  connect(bui.PPumETS, PPumETS.u)
  connect(PPumETS.y, EPumETS.u)
  connect(pumDis.P, EPumDis.u)
  connect(pumSto.P, EPumSto.u)
  connect(pla.PPum, EPumPla.u)
  connect(EPumETS.y, EPum.u[1])
  connect(EPumPla.y, EPum.u[2])
  connect(EPumDis.y, EPum.u[3])
  connect(EPumSto.y, EPum.u[4])
  connect(bui.PHea, PHeaPump.u)
  connect(PHeaPump.y, EHeaPum.u)
  connect(EHeaPum.y, ETot.u[1])
  connect(EPum.y, ETot.u[2])
  connect(TDisWatSup.T, conVio.u[1])
  connect(TDisWatBorLvg.T, conVio.u[2])
  connect(TDisWatRet.T, conVio.u[3])
  connect(bou.ports[1], pumDis.port_b)
end PartialSeries;


within Buildings.DHC.Examples.Cooling;
model ElectricChillersDirectETS "Example model for district cooling system with
  an electric chiller plant and a direct controlled ETS at each building"
  extends Modelica.Icons.Example;
  package Medium=Buildings.Media.Water
    "Medium model for water";
  // Chiller and cooling tower
  replaceable parameter Buildings.Fluid.Chillers.Data.ElectricEIR.ElectricEIRChiller_York_YT_1055kW_5_96COP_Vanes perChi
    "Performance data of chiller"
  parameter Modelica.Units.SI.MassFlowRate mCHW_flow_nominal=18.3
    "Nominal chilled water mass flow rate"
  parameter Modelica.Units.SI.MassFlowRate mCW_flow_nominal=34.7
    "Nominal condenser water mass flow rate"
  parameter Modelica.Units.SI.PressureDifference dpCHW_nominal=44.8*1000
    "Nominal chilled water side pressure"
  parameter Modelica.Units.SI.PressureDifference dpCW_nominal=46.2*1000
    "Nominal condenser water side pressure"
  parameter Modelica.Units.SI.Pressure dpDis=30000
    "Total pressure drop in the district pipes";
  parameter Modelica.Units.SI.Power QChi_nominal=mCHW_flow_nominal*4200*(6.67-18.56)
    "Nominal cooling capaciaty (Negative means cooling)"
  // Pumps
  parameter Buildings.Fluid.Movers.Data.Generic perCHWPum(
    pressure=Buildings.Fluid.Movers.BaseClasses.Characteristics.flowParameters(
      V_flow=mCHW_flow_nominal/1000*{0.2,0.6,0.8,1.0},
      dp=(dpCHW_nominal+dpDis+20000+6000*3)*{1,0.8,0.6,0.2}))
    "Performance data for chilled water pumps"
  parameter Buildings.Fluid.Movers.Data.Generic perCWPum(
    pressure=Buildings.Fluid.Movers.BaseClasses.Characteristics.flowParameters(
      V_flow=mCW_flow_nominal/1000*{0.2,0.6,1.0,1.2},
      dp=(dpCW_nominal+6000*2)*{1,0.8,0.6,0.2}))
    "Performance data for condenser water pumps"
  // Network
  parameter Integer nLoa=3
    "Number of served loads"
  final parameter Modelica.Units.SI.MassFlowRate mCon_flow_nominal[nLoa]=fill(10,nLoa)
    "Nominal mass flow rate in each connection line";
  // Buildings
  parameter String filNam[nLoa]={
   Modelica.Utilities.Files.loadResource("modelica://Buildings/Resources/Data/DHC/Loads/Examples/MediumOffice-90.1-2010-5A.mos"),
   Modelica.Utilities.Files.loadResource("modelica://Buildings/Resources/Data/DHC/Loads/Examples/MediumOffice-90.1-2010-5A.mos"),
   Modelica.Utilities.Files.loadResource("modelica://Buildings/Resources/Data/DHC/Loads/Examples/MediumOffice-90.1-2010-5A.mos")}
    "Library path of the file with thermal loads as time series"
  final parameter Modelica.Units.SI.HeatFlowRate QCoo_flow_nominal[nLoa](
    each max=-Modelica.Constants.eps)={Buildings.DHC.Loads.BaseClasses.getPeakLoad(
    string="#Peak space cooling load",
    filNam=filNam[i]) for i in 1:nLoa}
    "Space cooling design load (<=0)";
  final parameter Modelica.Units.SI.MassFlowRate mBui_flow_nominal[nLoa](
    each final min=0,
    each final start=0.5)={-QCoo_flow_nominal[i]/(cp*buiETS[i].dT_nominal) for i in 1:nLoa}
    "Nominal mass flow rate of building cooling side";
  Buildings.DHC.Plants.Cooling.ElectricChillerParallel pla(
    perChi=perChi,
    dTApp=3,
    perCHWPum=perCHWPum,
    perCWPum=perCWPum,
    mCHW_flow_nominal=mCHW_flow_nominal,
    dpCHW_nominal=dpCHW_nominal,
    QChi_nominal=QChi_nominal,
    mMin_flow=mCHW_flow_nominal*0.25,
    mCW_flow_nominal=mCW_flow_nominal,
    dpCW_nominal=dpCW_nominal,
    TAirInWB_nominal=298.7,
    TCW_nominal=308.15,
    dT_nominal=5.56,
    TMin=288.15,
    PFan_nominal=5000,
    tau=60,
    yCHWP_start=fill(0, 2),
    yCWP_start=fill(0, 2),
    dpCooTowVal_nominal=6000,
    dpCHWPumVal_nominal=6000,
    dpCWPumVal_nominal=6000,
    tWai=30,
    energyDynamics=Modelica.Fluid.Types.Dynamics.FixedInitial)
    "District cooling plant"
  Buildings.BoundaryConditions.WeatherData.ReaderTMY3 weaDat(
    final computeWetBulbTemperature=true,
    filNam=Modelica.Utilities.Files.loadResource("modelica://Buildings/Resources/weatherdata/USA_CA_San.Francisco.Intl.AP.724940_TMY3.mos"))
    "Weather data"
  Modelica.Blocks.Sources.Constant TCHWSupSet(k=273.15+7)
    "Chilled water supply temperature setpoint"
  Buildings.DHC.Networks.Distribution2PipePlugFlow_v dis(
    redeclare final package Medium = Medium,
    nCon=nLoa,
    allowFlowReversal=false,
    mDis_flow_nominal=sum(dis.mCon_flow_nominal),
    mCon_flow_nominal=mBui_flow_nominal,
    mEnd_flow_nominal=mBui_flow_nominal[nLoa],
    lDis=fill(30, nLoa),
    lEnd=30) "Distribution network for district cooling system"
  Buildings.DHC.Loads.Cooling.BuildingTimeSeriesWithETS buiETS[nLoa](
    each yMin=0.05,
    each use_riseTime=true,
    filNam=filNam,
    mBui_flow_nominal=mBui_flow_nominal,
    each bui(w_aLoaCoo_nominal=0.015))
    "Vectorized time series building load model connected with ETS for cooling"
  Modelica.Blocks.Sources.Constant TDisRetSet(k=273.15 + 16)
    "Setpoint for district return temperature"
  Modelica.Blocks.Math.Sum QTotCoo_flow(nin=nLoa)
    "Total cooling flow rate for all buildings "
  Buildings.Controls.OBC.CDL.Reals.LessThreshold offCoo(t=1e-4)
    "Threshold comparison to disable the plant"
  Modelica.Blocks.Math.Gain norQFlo(k=1/sum(QCoo_flow_nominal))
    "Normalized Q_flow"
  HeatTransfer.Sources.FixedTemperature gnd[nLoa](each T=285.15) "Ground"
  Controls.OBC.CDL.Logical.Timer tim(t=3600)
  Controls.OBC.CDL.Logical.Not onPla "On signal for the plant"
  HeatTransfer.Sources.FixedTemperature gnd1[nLoa + 1](each T=285.15) "Ground"
protected
  parameter Modelica.Units.SI.SpecificHeatCapacity cp=Medium.specificHeatCapacityCp(
    Medium.setState_pTX(
      Medium.p_default,
      Medium.T_default,
      Medium.X_default))
    "Default specific heat capacity of medium";
equation
  connect(weaDat.weaBus, pla.weaBus)
  connect(TCHWSupSet.y, pla.TCHWSupSet)
  connect(pla.port_aSerCoo, dis.port_bDisRet)
  connect(dis.port_aDisSup, pla.port_bSerCoo)
  connect(dis.ports_bCon, buiETS.port_aSerCoo)
  connect(buiETS.port_bSerCoo, dis.ports_aCon)
  for i in 1:nLoa loop
    connect(TDisRetSet.y,buiETS[i].TDisRetSet)
  end for;
  connect(buiETS.QCoo_flow, QTotCoo_flow.u)
  connect(QTotCoo_flow.y, norQFlo.u)
  connect(norQFlo.y, offCoo.u)
  connect(offCoo.y, tim.u)
  connect(tim.passed, onPla.u)
  connect(onPla.y, pla.on)
  connect(gnd.port, dis.heatPortsRet)
  connect(gnd1.port, dis.heatPortsDis)
end ElectricChillersDirectETS;


within Buildings.DHC.Examples;
package Cooling "Example model for entire district cooling system"
  extends Modelica.Icons.VariantsPackage;
end Cooling;


within Buildings.DHC.Examples;
package Steam "Package of example models for steam district heating systems"
  extends Modelica.Icons.VariantsPackage;
end Steam;


within Buildings.DHC.Examples.Steam;
model SingleBoiler "Example model for a complete steam district heating system with a
  central plant that contains a single boiler"
  extends Modelica.Icons.Example;

  package MediumSte = Buildings.Media.Steam (p_default=400000,
    T_default=273.15+143.61,
    h_default=2738100)
    "Steam medium";
  package MediumWat =
    Buildings.Media.Specialized.Water.TemperatureDependentDensity (
      p_default=101325,
      T_default=100+273.15,
      h_default=2738100)
    "Water medium";

  parameter Modelica.Units.SI.AbsolutePressure pSat=400000
    "Saturation pressure, high pressure";
  parameter Modelica.Units.SI.AbsolutePressure pLow=200000
    "Reduced pressure, after PRV";
  parameter Modelica.Units.SI.Temperature TSat=
     MediumSte.saturationTemperature(pSat)
     "Saturation temperature, at high pressure";

  parameter Integer N = 3 "Number of buildings";
  parameter Modelica.Units.SI.MassFlowRate mDis_flow_nominal=sum(bld.m_flow_nominal)*1.2
    "Nominal mass flow rate of entire district";
  parameter Modelica.Units.SI.HeatFlowRate QDis_flow_nominal=QBui_flow_nominal*N
    "Nominal heat flow rate of entire district";
  parameter Modelica.Units.SI.HeatFlowRate QBui_flow_nominal=20000
    "Nominal heat flow rate of each building";
  parameter Modelica.Units.SI.PressureDifference dpPip=6000
    "Pressure drop in the condensate return pipe";

  parameter Buildings.Fluid.Movers.Data.Generic perPumFW(
   pressure(V_flow=(mDis_flow_nominal/1000)*{0,1,2},
     dp=(pSat-101325)*{2,1,0}))
    "Performance data for feedwater pump at the plant";

  parameter Modelica.Units.SI.PressureDifference dp_nominal=6000
    "Pressure drop of distribution at nominal mass flow rate";

  Buildings.DHC.Loads.Steam.BuildingTimeSeriesAtETS bld[N](
    redeclare final package MediumSte = MediumSte,
    redeclare final package MediumWat = MediumWat,
    each have_prv=true,
    each dp_nominal=dpPip/2,
    each final pSte_nominal=pSat,
    each final Q_flow_nominal=QBui_flow_nominal,
    each pLow_nominal=pLow,
    each energyDynamics=Modelica.Fluid.Types.Dynamics.FixedInitial,
    each tableOnFile=false,
    each QHeaLoa=
      [0,0.8; 2,1; 10,1; 12,0.5; 20,0.5; 24,0.8]*[1,0;0,QBui_flow_nominal],
    each smoothness=Modelica.Blocks.Types.Smoothness.MonotoneContinuousDerivative1,
    each timeScale(displayUnit="s") = 3600,
    each show_T=true)
    "Buildings"
  Buildings.DHC.Networks.Steam.DistributionCondensatePipe dis(
    redeclare final package MediumSup = MediumSte,
    redeclare final package MediumRet = MediumWat,
    final dp_nominal=dp_nominal,
    final nCon=N,
    final mDis_flow_nominal=mDis_flow_nominal,
    final mCon_flow_nominal=bld.m_flow_nominal)
    "Distribution network"
  Buildings.DHC.Plants.Steam.SingleBoiler pla(
    redeclare final package Medium = MediumWat,
    redeclare final package MediumHea_b = MediumSte,
    final m_flow_nominal=mDis_flow_nominal,
    final pSteSet=pSat,
    final Q_flow_nominal=QDis_flow_nominal,
    final per=perPumFW,
    energyDynamics=Modelica.Fluid.Types.Dynamics.FixedInitial,
    kBoi=600,
    TiBoi(displayUnit="min") = 120,
    kPum=200,
    TiPum=1000)
    "Plant"
equation
  connect(dis.ports_bCon, bld.port_a)
  connect(bld.port_b, dis.ports_aCon)
  connect(pla.port_bSerHea, dis.port_aDisSup)
  connect(dis.port_bDisRet, pla.port_aSerHea)
end SingleBoiler;


within Buildings.DHC;
package Loads "Models for computing thermal loads served by the DES"
  extends Modelica.Icons.VariantsPackage;
end Loads;


within Buildings.DHC.Loads.BaseClasses;
model BuildingRCZ1Valve
  "One-zone RC building model with distribution pumps and mixing valves"
  extends Buildings.DHC.Loads.BaseClasses.PartialBuilding(
    redeclare package Medium=Buildings.Media.Water,
    final have_heaWat=true,
    final have_chiWat=true,
    final have_eleHea=false,
    final have_eleCoo=false,
    final have_weaBus=true);
  package Medium2=Buildings.Media.Air
    "Load side medium";
  parameter Integer nZon=1
    "Number of thermal zones";
  Buildings.BoundaryConditions.SolarIrradiation.DiffusePerez HDifTil[2](
    each outSkyCon=true,
    each outGroCon=true,
    each til=1.5707963267949,
    azi={3.1415926535898,4.7123889803847})
    "Calculates diffuse solar radiation on titled surface for both directions"
  Buildings.BoundaryConditions.SolarIrradiation.DirectTiltedSurface HDirTil[2](each til=
       1.5707963267949, azi={3.1415926535898,4.7123889803847})
    "Calculates direct solar radiation on titled surface for both directions"
  Buildings.ThermalZones.ReducedOrder.SolarGain.CorrectionGDoublePane corGDouPan(
    n=2,
    UWin=2.1)
    "Correction factor for solar transmission"
  Buildings.ThermalZones.ReducedOrder.RC.OneElement thermalZoneOneElement(
    VAir=52.5,
    hRad=4.999999999999999,
    hConWin=2.7000000000000006,
    gWin=1,
    ratioWinConRad=0.09,
    hConExt=2.0490178828959134,
    nExt=1,
    RExt={0.00331421908725},
    CExt={5259932.23},
    RWin=0.01642857143,
    RExtRem=0.1265217391,
    nOrientations=2,
    AWin={7,7},
    ATransparent={7,7},
    AExt={3.5,8},
    redeclare package Medium=Medium2,
    extWallRC(
      thermCapExt(
        each der_T(
          fixed=true))),
    energyDynamics=Modelica.Fluid.Types.Dynamics.FixedInitial,
    T_start=295.15,
    nPorts=2)
    "Thermal zone"
  Buildings.ThermalZones.ReducedOrder.EquivalentAirTemperature.VDI6007WithWindow eqAirTemp(
    n=2,
    wfGro=0,
    wfWall={0.3043478260869566,0.6956521739130435},
    wfWin={0.5,0.5},
    withLongwave=true,
    aExt=0.7,
    hConWallOut=20.0,
    hRad=5.0,
    hConWinOut=20.0,
    TGro=285.15)
    "Computes equivalent air temperature"
  Modelica.Blocks.Math.Add solRad[2]
    "Sums up solar radiation of both directions"
  Buildings.HeatTransfer.Sources.PrescribedTemperature preTem
    "Prescribed temperature for exterior walls outdoor surface temperature"
  Buildings.HeatTransfer.Sources.PrescribedTemperature preTem1
    "Prescribed temperature for windows outdoor surface temperature"
  Modelica.Thermal.HeatTransfer.Components.Convection theConWin
    "Outdoor convective heat transfer of windows"
  Modelica.Thermal.HeatTransfer.Components.Convection theConWall
    "Outdoor convective heat transfer of walls"
  Modelica.Thermal.HeatTransfer.Sources.PrescribedHeatFlow perRad
    "Radiative heat flow of persons"
  Modelica.Thermal.HeatTransfer.Sources.PrescribedHeatFlow perCon
    "Convective heat flow of persons"
  Modelica.Blocks.Sources.CombiTimeTable intGai(
    table=[
      0,0,0,0;
      3600,0,0,0;
      7200,0,0,0;
      10800,0,0,0;
      14400,0,0,0;
      18000,0,0,0;
      21600,0,0,0;
      25200,0,0,0;
      25200,80,80,200;
      28800,80,80,200;
      32400,80,80,200;
      36000,80,80,200;
      39600,80,80,200;
      43200,80,80,200;
      46800,80,80,200;
      50400,80,80,200;
      54000,80,80,200;
      57600,80,80,200;
      61200,80,80,200;
      61200,0,0,0;
      64800,0,0,0;
      72000,0,0,0;
      75600,0,0,0;
      79200,0,0,0;
      82800,0,0,0;
      86400,0,0,0],
    columns={2,3,4},
    extrapolation=Modelica.Blocks.Types.Extrapolation.Periodic)
    "Table with profiles for persons (radiative and convective) and machines
    (convective)"
  Modelica.Blocks.Sources.Constant const[2](
    each k=0)
    "Sets sunblind signal to zero (open)"
  Modelica.Thermal.HeatTransfer.Sources.PrescribedHeatFlow macConv
    "Convective heat flow of machines"
  Modelica.Blocks.Sources.Constant hConWall(
    k=25*11.5)
    "Outdoor coefficient of heat transfer for walls"
  Modelica.Blocks.Sources.Constant hConWin(
    k=20*14)
    "Outdoor coefficient of heat transfer for windows"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant minTSet(
    k=293.15,
    y(final unit="K",
      displayUnit="degC"))
    "Minimum temperature set point"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant maxTSet(
    k=297.15,
    y(final unit="K",
      displayUnit="degC"))
    "Maximum temperature set point"
  Buildings.Controls.OBC.CDL.Reals.MultiSum mulSum(
    nin=2)
  Buildings.DHC.Loads.BaseClasses.FanCoil4Pipe terUni(
    redeclare package Medium1=Medium,
    redeclare package Medium2=Medium2,
    QHea_flow_nominal=1000,
    QCoo_flow_nominal=-5000,
    T_aLoaHea_nominal=293.15,
    T_aLoaCoo_nominal=297.15,
    T_bHeaWat_nominal=308.15,
    T_bChiWat_nominal=285.15,
    T_aHeaWat_nominal=313.15,
    T_aChiWat_nominal=280.15,
    mLoaHea_flow_nominal=1,
    mLoaCoo_flow_nominal=1)
    "Terminal unit"
  Buildings.DHC.Loads.BaseClasses.FlowDistribution disFloHea(
    redeclare package Medium=Medium,
    m_flow_nominal=terUni.mHeaWat_flow_nominal,
    have_pum=true,
    have_val=true,
    dp_nominal=100000,
    nPorts_a1=1,
    nPorts_b1=1)
    "Heating water distribution system"
  Buildings.DHC.Loads.BaseClasses.FlowDistribution disFloCoo(
    redeclare package Medium=Medium,
    m_flow_nominal=terUni.mChiWat_flow_nominal,
    typDis=Buildings.DHC.Loads.BaseClasses.Types.DistributionType.ChilledWater,
    have_pum=true,
    have_val=true,
    dp_nominal=100000,
    nPorts_a1=1,
    nPorts_b1=1)
    "Chilled water distribution system"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant TSetSecHea(
    k=308.15,
    y(final unit="K",
      displayUnit="degC"))
    "Heating water secondary supply temperature set point"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant TSetSecChi(
    k=289.15,
    y(final unit="K",
      displayUnit="degC"))
    "Chilled water secondary supply temperature set point"
equation
  connect(eqAirTemp.TEqAirWin,preTem1.T)
  connect(eqAirTemp.TEqAir,preTem.T)
  connect(intGai.y[1],perRad.Q_flow)
  connect(intGai.y[2],perCon.Q_flow)
  connect(intGai.y[3],macConv.Q_flow)
  connect(const.y,eqAirTemp.sunblind)
  connect(HDifTil.HSkyDifTil,corGDouPan.HSkyDifTil)
  connect(HDirTil.H,corGDouPan.HDirTil)
  connect(HDirTil.H,solRad.u1)
  connect(HDirTil.inc,corGDouPan.inc)
  connect(HDifTil.H,solRad.u2)
  connect(HDifTil.HGroDifTil,corGDouPan.HGroDifTil)
  connect(solRad.y,eqAirTemp.HSol)
  connect(perRad.port,thermalZoneOneElement.intGainsRad)
  connect(theConWin.solid,thermalZoneOneElement.window)
  connect(preTem1.port,theConWin.fluid)
  connect(thermalZoneOneElement.extWall,theConWall.solid)
  connect(theConWall.fluid,preTem.port)
  connect(hConWall.y,theConWall.Gc)
  connect(hConWin.y,theConWin.Gc)
  connect(macConv.port,thermalZoneOneElement.intGainsConv)
  connect(perCon.port,thermalZoneOneElement.intGainsConv)
  connect(corGDouPan.solarRadWinTrans,thermalZoneOneElement.solRad)
  connect(weaBus.TBlaSky,eqAirTemp.TBlaSky)
  connect(weaBus.TDryBul,eqAirTemp.TDryBul)
  connect(weaBus,HDifTil[2].weaBus)
  connect(weaBus,HDirTil[1].weaBus)
  connect(weaBus,HDirTil[2].weaBus)
  connect(weaBus,HDifTil[1].weaBus)
  connect(thermalZoneOneElement.ports[1],terUni.port_aLoa)
  connect(terUni.port_bLoa,thermalZoneOneElement.ports[2])
  connect(terUni.port_bChiWat,disFloCoo.ports_a1[1])
  connect(terUni.port_bHeaWat,disFloHea.ports_a1[1])
  connect(disFloHea.ports_b1[1],terUni.port_aHeaWat)
  connect(disFloCoo.ports_b1[1],terUni.port_aChiWat)
  connect(terUni.mReqHeaWat_flow,disFloHea.mReq_flow[1])
  connect(terUni.mReqChiWat_flow,disFloCoo.mReq_flow[1])
  connect(disFloHea.PPum,mulSum.u[1])
  connect(disFloCoo.PPum,mulSum.u[2])
  connect(thermalZoneOneElement.TAir,terUni.TSen)
  connect(maxTSet.y,terUni.TSetCoo)
  connect(minTSet.y,terUni.TSetHea)
  connect(TSetSecChi.y,disFloCoo.TSupSet)
  connect(TSetSecHea.y,disFloHea.TSupSet)
  connect(mulSum.y, mulPPum.u)
  connect(disFloHea.QActTot_flow, mulQHea_flow.u)
  connect(disFloCoo.QActTot_flow, mulQCoo_flow.u)
  connect(terUni.PFan, mulPFan.u)
  connect(mulHeaWatInl[1].port_b, disFloHea.port_a)
  connect(mulHeaWatOut[1].port_a, disFloHea.port_b)
  connect(mulChiWatInl[1].port_b, disFloCoo.port_a)
  connect(mulChiWatOut[1].port_a, disFloCoo.port_b)
end BuildingRCZ1Valve;


within Buildings.DHC.Loads.BaseClasses;
model BuildingRCZ6
  "Six-zone RC building model based on URBANopt GeoJSON export, with distribution pumps"
  extends Buildings.DHC.Loads.BaseClasses.PartialBuilding(
    redeclare package Medium=Buildings.Media.Water,
    final have_weaBus=true,
    final have_heaWat=true,
    final have_chiWat=true,
    final have_eleHea=false,
    final have_eleCoo=false,
    final have_fan=false,
    final have_pum=true);
  package Medium2=Buildings.Media.Air
    "Load side medium";
  parameter Integer nZon=6
    "Number of thermal zones";
  parameter Real facMulTerUni[nZon]={15 for i in 1:nZon}
    "Multiplier factor for terminal units";
  parameter Modelica.Units.SI.MassFlowRate mLoa_flow_nominal[nZon]=fill(1, nZon)
    "Load side mass flow rate at nominal conditions"
  parameter Modelica.Units.SI.HeatFlowRate QHea_flow_nominal[nZon]=fill(10000,
      nZon) ./ facMulTerUni "Design heating heat flow rate (>=0)"
  parameter Modelica.Units.SI.HeatFlowRate QCoo_flow_nominal[nZon]=cat(
      1,
      fill(-10000, nZon - 1),
      {-50000}) ./ facMulTerUni "Design cooling heat flow rate (<=0)"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant minTSet[nZon](
    k=fill(
      293.15,
      nZon),
    y(each final unit="K",
      each displayUnit="degC"))
    "Minimum temperature set point"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant maxTSet[nZon](
    k=fill(
      297.15,
      nZon),
    y(each final unit="K",
      each displayUnit="degC"))
    "Maximum temperature set point"
  Buildings.DHC.Loads.BaseClasses.Examples.BaseClasses.GeojsonExportRC.OfficeBuilding.Office office
  Buildings.DHC.Loads.BaseClasses.Examples.BaseClasses.GeojsonExportRC.OfficeBuilding.Floor floor
  Buildings.DHC.Loads.BaseClasses.Examples.BaseClasses.GeojsonExportRC.OfficeBuilding.Storage storage
  Buildings.DHC.Loads.BaseClasses.Examples.BaseClasses.GeojsonExportRC.OfficeBuilding.Meeting meeting
  Buildings.DHC.Loads.BaseClasses.Examples.BaseClasses.GeojsonExportRC.OfficeBuilding.Restroom restroom
  Buildings.DHC.Loads.BaseClasses.Examples.BaseClasses.GeojsonExportRC.OfficeBuilding.ICT iCT
  Buildings.Controls.OBC.CDL.Reals.MultiSum mulSum(
    nin=2)
  Buildings.DHC.Loads.BaseClasses.FanCoil4PipeHeatPorts terUni[nZon](
    redeclare each final package Medium1=Medium,
    redeclare each final package Medium2=Medium2,
    final facMul=facMulTerUni,
    final QHea_flow_nominal=QHea_flow_nominal,
    final QCoo_flow_nominal=QCoo_flow_nominal,
    each T_aLoaHea_nominal=293.15,
    each T_aLoaCoo_nominal=297.15,
    each T_bHeaWat_nominal=35+273.15,
    each T_bChiWat_nominal=12+273.15,
    each T_aHeaWat_nominal=40+273.15,
    each T_aChiWat_nominal=7+273.15,
    each mLoaHea_flow_nominal=5,
    each mLoaCoo_flow_nominal=5)
    "Terminal unit"
  Buildings.DHC.Loads.BaseClasses.FlowDistribution disFloHea(
    redeclare package Medium=Medium,
    m_flow_nominal=sum(
      terUni.mHeaWat_flow_nominal .* terUni.facMul),
    have_pum=true,
    dp_nominal=100000,
    nPorts_a1=nZon,
    nPorts_b1=nZon)
    "Heating water distribution system"
  Buildings.DHC.Loads.BaseClasses.FlowDistribution disFloCoo(
    redeclare package Medium=Medium,
    m_flow_nominal=sum(
      terUni.mChiWat_flow_nominal .* terUni.facMul),
    typDis=Buildings.DHC.Loads.BaseClasses.Types.DistributionType.ChilledWater,
    have_pum=true,
    dp_nominal=100000,
    nPorts_a1=nZon,
    nPorts_b1=nZon)
    "Chilled water distribution system"
equation
  connect(terUni.port_bHeaWat,disFloHea.ports_a1)
  connect(terUni.port_bChiWat,disFloCoo.ports_a1)
  connect(disFloHea.ports_b1,terUni.port_aHeaWat)
  connect(disFloCoo.ports_b1,terUni.port_aChiWat)
  connect(weaBus,office.weaBus)
  connect(weaBus,floor.weaBus)
  connect(weaBus,storage.weaBus)
  connect(weaBus,meeting.weaBus)
  connect(weaBus,restroom.weaBus)
  connect(weaBus,iCT.weaBus)
  connect(terUni[1].heaPorCon,office.port_a)
  connect(terUni[2].heaPorCon,floor.port_a)
  connect(terUni[3].heaPorCon,storage.port_a)
  connect(terUni[4].heaPorCon,meeting.port_a)
  connect(terUni[5].heaPorCon,restroom.port_a)
  connect(terUni[6].heaPorCon,iCT.port_a)
  connect(terUni[1].heaPorRad,office.port_a)
  connect(terUni[2].heaPorRad,floor.port_a)
  connect(terUni[3].heaPorRad,storage.port_a)
  connect(terUni[4].heaPorRad,meeting.port_a)
  connect(terUni[5].heaPorRad,restroom.port_a)
  connect(terUni[6].heaPorRad,iCT.port_a)
  connect(terUni.mReqHeaWat_flow,disFloHea.mReq_flow)
  connect(terUni.mReqChiWat_flow,disFloCoo.mReq_flow)
  connect(disFloHea.PPum,mulSum.u[1])
  connect(disFloCoo.PPum,mulSum.u[2])
  connect(maxTSet.y,terUni.TSetCoo)
  connect(minTSet.y,terUni.TSetHea)
  connect(disFloHea.QActTot_flow, mulQHea_flow.u)
  connect(disFloCoo.QActTot_flow, mulQCoo_flow.u)
  connect(mulPPum.u, mulSum.y)
  connect(mulHeaWatInl[1].port_b, disFloHea.port_a)
  connect(mulHeaWatOut[1].port_a, disFloHea.port_b)
  connect(mulChiWatInl[1].port_b, disFloCoo.port_a)
  connect(mulChiWatOut[1].port_a, disFloCoo.port_b)
end BuildingRCZ6;


One-zone EnergyPlus building model

within Buildings.DHC.Loads.BaseClasses;
model BuildingSpawnZ1
  "One-zone EnergyPlus building model"
  extends Buildings.DHC.Loads.BaseClasses.PartialBuilding(
    redeclare package Medium=Buildings.Media.Water,
    final have_heaWat=true,
    final have_chiWat=true,
    final have_pum=false,
    final have_eleHea=false,
    final have_eleCoo=false);
  package Medium2=Buildings.Media.Air
    "Load side medium";
  parameter Integer nZon=1
    "Number of thermal zones";
  parameter String idfName="modelica://Buildings/Resources/Data/ThermalZones/EnergyPlus_9_6_0/Examples/RefBldgSmallOffice/RefBldgSmallOfficeNew2004_Chicago.idf"
    "Name of the IDF file";
  parameter String epwName="modelica://Buildings/Resources/weatherdata/USA_IL_Chicago-OHare.Intl.AP.725300_TMY3.epw"
    "Name of the weather file";
  parameter String weaName="modelica://Buildings/Resources/weatherdata/USA_IL_Chicago-OHare.Intl.AP.725300_TMY3.mos"
    "Name of the weather file";
  Modelica.Blocks.Sources.Constant qConGai_flow(
    k=0)
    "Convective heat gain"
  Modelica.Blocks.Sources.Constant qRadGai_flow(
    k=0)
    "Radiative heat gain"
  Modelica.Blocks.Routing.Multiplex3 multiplex3_1
  Modelica.Blocks.Sources.Constant qLatGai_flow(
    k=0)
    "Latent heat gain"
  Buildings.ThermalZones.EnergyPlus_9_6_0.ThermalZone zon(
    redeclare package Medium=Medium2,
    zoneName="Core_ZN",
    nPorts=2)
    "Thermal zone"
  inner Buildings.ThermalZones.EnergyPlus_9_6_0.Building building(
    idfName=Modelica.Utilities.Files.loadResource(
      idfName),
    epwName=Modelica.Utilities.Files.loadResource(
      epwName),
    weaName=Modelica.Utilities.Files.loadResource(
      weaName))
    "Building model"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant minTSet(
    k=293.15,
    y(final unit="K",
      displayUnit="degC"))
    "Minimum temperature set point"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant maxTSet(
    k=297.15,
    y(final unit="K",
      displayUnit="degC"))
    "Maximum temperature set point"
  Buildings.DHC.Loads.BaseClasses.FanCoil4Pipe terUni(
    redeclare package Medium1=Medium,
    redeclare package Medium2=Medium2,
    QHea_flow_nominal=2000,
    QCoo_flow_nominal=-2000,
    T_aLoaHea_nominal=293.15,
    T_aLoaCoo_nominal=297.15,
    T_bHeaWat_nominal=308.15,
    T_bChiWat_nominal=285.15,
    T_aHeaWat_nominal=313.15,
    T_aChiWat_nominal=280.15,
    mLoaHea_flow_nominal=1,
    mLoaCoo_flow_nominal=1)
    "Terminal unit"
  Buildings.DHC.Loads.BaseClasses.FlowDistribution disFloHea(
    redeclare package Medium=Medium,
    m_flow_nominal=terUni.mHeaWat_flow_nominal,
    dp_nominal=100000,
    nPorts_a1=nZon,
    nPorts_b1=nZon)
    "Heating water distribution system"
  Buildings.DHC.Loads.BaseClasses.FlowDistribution disFloCoo(
    redeclare package Medium=Medium,
    m_flow_nominal=terUni.mChiWat_flow_nominal,
    typDis=Buildings.DHC.Loads.BaseClasses.Types.DistributionType.ChilledWater,
    dp_nominal=100000,
    nPorts_a1=nZon,
    nPorts_b1=nZon)
    "Chilled water distribution system"
equation
  connect(qRadGai_flow.y,multiplex3_1.u1[1])
  connect(qConGai_flow.y,multiplex3_1.u2[1])
  connect(multiplex3_1.u3[1],qLatGai_flow.y)
  connect(multiplex3_1.y,zon.qGai_flow)
  connect(zon.ports[1],terUni.port_aLoa)
  connect(terUni.port_bHeaWat,disFloHea.ports_a1[1])
  connect(terUni.port_bChiWat,disFloCoo.ports_a1[1])
  connect(disFloHea.ports_b1[1],terUni.port_aHeaWat)
  connect(disFloCoo.ports_b1[1],terUni.port_aChiWat)
  connect(terUni.mReqHeaWat_flow,disFloHea.mReq_flow[1])
  connect(terUni.mReqChiWat_flow,disFloCoo.mReq_flow[1])
  connect(terUni.port_bLoa,zon.ports[2])
  connect(zon.TAir,terUni.TSen)
  connect(maxTSet.y,terUni.TSetCoo)
  connect(minTSet.y,terUni.TSetHea)
  connect(terUni.PFan, mulPFan.u)
  connect(disFloHea.QActTot_flow, mulQHea_flow.u)
  connect(disFloCoo.QActTot_flow, mulQCoo_flow.u)
  connect(mulChiWatInl[1].port_b, disFloCoo.port_a)
  connect(mulChiWatOut[1].port_a, disFloCoo.port_b)
  connect(mulHeaWatInl[1].port_b, disFloHea.port_a)
  connect(mulHeaWatOut[1].port_a, disFloHea.port_b)
end BuildingSpawnZ1;


within Buildings.DHC.Loads.BaseClasses;
model BuildingSpawnZ6
  "Six-zone EnergyPlus building model based on URBANopt GeoJSON export, with distribution pumps"
  extends Buildings.DHC.Loads.BaseClasses.PartialBuilding(
    redeclare package Medium=Buildings.Media.Water,
    final have_heaWat=true,
    final have_chiWat=true,
    final have_eleHea=false,
    final have_eleCoo=false,
    final have_pum=true,
    final have_weaBus=false);
  package Medium2=Buildings.Media.Air
    "Medium model";
  parameter Integer nZon=5
    "Number of conditioned thermal zones";
  parameter Real facMulTerUni[nZon]={5 for i in 1:nZon}
    "Multiplier factor for terminal units";
  parameter Modelica.Units.SI.MassFlowRate mLoa_flow_nominal[nZon]=fill(1, nZon)
    "Load side mass flow rate at nominal conditions (single terminal unit)"
  parameter Modelica.Units.SI.HeatFlowRate QHea_flow_nominal[nZon]=fill(2000,
      nZon) ./ facMulTerUni
    "Design heating heat flow rate (single terminal unit)"
  parameter Modelica.Units.SI.HeatFlowRate QCoo_flow_nominal[nZon]=fill(-2000,
      nZon) ./ facMulTerUni
    "Design cooling heat flow rate (single terminal unit)"
  parameter String idfName="modelica://Buildings/Resources/Data/ThermalZones/EnergyPlus_9_6_0/Examples/RefBldgSmallOffice/RefBldgSmallOfficeNew2004_Chicago.idf"
    "Name of the IDF file";
  parameter String epwName="modelica://Buildings/Resources/weatherdata/USA_IL_Chicago-OHare.Intl.AP.725300_TMY3.epw"
    "Name of the weather file";
  parameter String weaName="modelica://Buildings/Resources/weatherdata/USA_IL_Chicago-OHare.Intl.AP.725300_TMY3.mos"
    "Name of the weather file";
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant minTSet[nZon](
    k=fill(
      293.15,
      nZon),
    y(each final unit="K",
      each displayUnit="degC"))
    "Minimum temperature set point"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant maxTSet[nZon](
    k=fill(
      297.15,
      nZon),
    y(each final unit="K",
      each displayUnit="degC"))
    "Maximum temperature set point"
  Modelica.Blocks.Sources.Constant qConGai_flow(
    k=0)
    "Convective heat gain"
  Modelica.Blocks.Sources.Constant qRadGai_flow(
    k=0)
    "Radiative heat gain"
  Modelica.Blocks.Routing.Multiplex3 multiplex3_1
  Modelica.Blocks.Sources.Constant qLatGai_flow(
    k=0)
    "Latent heat gain"
  Buildings.ThermalZones.EnergyPlus_9_6_0.ThermalZone znAttic(
    redeclare package Medium=Medium2,
    zoneName="Attic")
    "Thermal zone"
  Buildings.ThermalZones.EnergyPlus_9_6_0.ThermalZone znCore_ZN(
    redeclare package Medium=Medium2,
    zoneName="Core_ZN",
    nPorts=2)
    "Thermal zone"
  Buildings.ThermalZones.EnergyPlus_9_6_0.ThermalZone znPerimeter_ZN_1(
    redeclare package Medium=Medium2,
    zoneName="Perimeter_ZN_1",
    nPorts=2)
    "Thermal zone"
  Buildings.ThermalZones.EnergyPlus_9_6_0.ThermalZone znPerimeter_ZN_2(
    redeclare package Medium=Medium2,
    zoneName="Perimeter_ZN_2",
    nPorts=2)
    "Thermal zone"
  Buildings.ThermalZones.EnergyPlus_9_6_0.ThermalZone znPerimeter_ZN_3(
    redeclare package Medium=Medium2,
    zoneName="Perimeter_ZN_3",
    nPorts=2)
    "Thermal zone"
  Buildings.ThermalZones.EnergyPlus_9_6_0.ThermalZone znPerimeter_ZN_4(
    redeclare package Medium=Medium2,
    zoneName="Perimeter_ZN_4",
    nPorts=2)
    "Thermal zone"
  inner Buildings.ThermalZones.EnergyPlus_9_6_0.Building building(
    idfName=Modelica.Utilities.Files.loadResource(
      idfName),
    epwName=Modelica.Utilities.Files.loadResource(
      epwName),
    weaName=Modelica.Utilities.Files.loadResource(
      weaName))
    "Building outer component"
  Buildings.Controls.OBC.CDL.Reals.MultiSum mulSum(
    final nin=nZon)
  Buildings.Controls.OBC.CDL.Reals.MultiSum mulSum3(
    nin=2)
  Buildings.DHC.Loads.BaseClasses.FanCoil4Pipe terUni[nZon](
    redeclare each final package Medium1=Medium,
    redeclare each final package Medium2=Medium2,
    final facMul=facMulTerUni,
    final QHea_flow_nominal=QHea_flow_nominal,
    final QCoo_flow_nominal=QCoo_flow_nominal,
    each T_aLoaHea_nominal=293.15,
    each T_aLoaCoo_nominal=297.15,
    each T_bHeaWat_nominal=308.15,
    each T_bChiWat_nominal=285.15,
    each T_aHeaWat_nominal=313.15,
    each T_aChiWat_nominal=280.15,
    final mLoaHea_flow_nominal=mLoa_flow_nominal,
    final mLoaCoo_flow_nominal=mLoa_flow_nominal)
    "Terminal unit"
  Buildings.DHC.Loads.BaseClasses.FlowDistribution disFloHea(
    redeclare package Medium=Medium,
    m_flow_nominal=sum(
      terUni.mHeaWat_flow_nominal .* terUni.facMul),
    have_pum=true,
    dp_nominal=100000,
    nPorts_a1=nZon,
    nPorts_b1=nZon)
    "Heating water distribution system"
  Buildings.DHC.Loads.BaseClasses.FlowDistribution disFloCoo(
    redeclare package Medium=Medium,
    m_flow_nominal=sum(
      terUni.mChiWat_flow_nominal .* terUni.facMul),
    typDis=Buildings.DHC.Loads.BaseClasses.Types.DistributionType.ChilledWater,
    have_pum=true,
    dp_nominal=100000,
    nPorts_a1=nZon,
    nPorts_b1=nZon)
    "Chilled water distribution system"
equation
  connect(qRadGai_flow.y,multiplex3_1.u1[1])
  connect(qConGai_flow.y,multiplex3_1.u2[1])
  connect(multiplex3_1.u3[1],qLatGai_flow.y)
  connect(multiplex3_1.y,znAttic.qGai_flow)
  connect(multiplex3_1.y,znCore_ZN.qGai_flow)
  connect(multiplex3_1.y,znPerimeter_ZN_1.qGai_flow)
  connect(multiplex3_1.y,znPerimeter_ZN_2.qGai_flow)
  connect(multiplex3_1.y,znPerimeter_ZN_3.qGai_flow)
  connect(multiplex3_1.y,znPerimeter_ZN_4.qGai_flow)
  connect(znCore_ZN.ports[1],terUni[1].port_aLoa)
  connect(terUni[1].port_bLoa,znCore_ZN.ports[2])
  connect(znPerimeter_ZN_1.ports[1],terUni[2].port_aLoa)
  connect(terUni[2].port_bLoa,znPerimeter_ZN_1.ports[2])
  connect(znPerimeter_ZN_2.ports[1],terUni[3].port_aLoa)
  connect(terUni[3].port_bLoa,znPerimeter_ZN_2.ports[2])
  connect(znPerimeter_ZN_3.ports[1],terUni[4].port_aLoa)
  connect(terUni[4].port_bLoa,znPerimeter_ZN_3.ports[2])
  connect(znPerimeter_ZN_4.ports[1],terUni[5].port_aLoa)
  connect(terUni[5].port_bLoa,znPerimeter_ZN_4.ports[2])
  connect(terUni.port_bHeaWat,disFloHea.ports_a1)
  connect(disFloHea.ports_b1,terUni.port_aHeaWat)
  connect(disFloCoo.ports_b1,terUni.port_aChiWat)
  connect(terUni.port_bChiWat,disFloCoo.ports_a1)
  connect(terUni.mReqChiWat_flow,disFloCoo.mReq_flow)
  connect(terUni.mReqHeaWat_flow,disFloHea.mReq_flow)
  connect(terUni.PFan,mulSum.u)
  connect(disFloHea.PPum,mulSum3.u[1])
  connect(disFloCoo.PPum,mulSum3.u[2])
  connect(znCore_ZN.TAir,terUni[1].TSen)
  connect(znPerimeter_ZN_1.TAir,terUni[2].TSen)
  connect(znPerimeter_ZN_2.TAir,terUni[3].TSen)
  connect(znPerimeter_ZN_3.TAir,terUni[4].TSen)
  connect(znPerimeter_ZN_4.TAir,terUni[5].TSen)
  connect(maxTSet.y,terUni.TSetCoo)
  connect(minTSet.y,terUni.TSetHea)
  connect(disFloHea.QActTot_flow, mulQHea_flow.u)
  connect(mulSum3.y, mulPPum.u)
  connect(mulSum.y, mulPFan.u)
  connect(disFloCoo.QActTot_flow, mulQCoo_flow.u)
  connect(mulHeaWatInl[1].port_b, disFloHea.port_a)
  connect(mulChiWatInl[1].port_b, disFloCoo.port_a)
  connect(mulChiWatOut[1].port_a, disFloCoo.port_b)
  connect(mulHeaWatOut[1].port_a, disFloHea.port_b)
end BuildingSpawnZ6;


within Buildings.DHC.Loads.BaseClasses;
model BuildingTimeSeries
  "Building model with heating and/or cooling loads provided as time series"
  extends Buildings.DHC.Loads.BaseClasses.PartialBuilding(
    redeclare package Medium=Buildings.Media.Water,
    have_heaWat=true,
    have_chiWat=true,
    final have_fan=false,
    final have_pum=true,
    final have_eleHea=false,
    final have_eleCoo=false,
    final have_weaBus=false);
  replaceable package Medium2=Buildings.Media.Air
    constrainedby Modelica.Media.Interfaces.PartialMedium
    "Load side medium";
  parameter Boolean have_hotWat = false
    "Set to true if SHW load is included in the time series"
  parameter String filNam
    "File name with thermal loads as time series";
  parameter Real facMulHea(min=0)=QHea_flow_nominal /
    (QHea_flow_nominal_ref * abs(T_aLoaHea_nominal - T_aHeaWat_nominal) /
     abs(T_aLoaHea_nominal_ref - T_aHeaWat_nominal_ref) *
     mLoaHea_flow_nominal / mLoaHea_flow_nominal_ref)
    "Heating terminal unit multiplier factor"
  parameter Real facMulCoo(min=0)=QCoo_flow_nominal /
    (QCoo_flow_nominal_ref * abs(h_aLoaCoo_nominal - hSat_nominal) /
     abs(h_aLoaCoo_nominal_ref - hSat_nominal_ref) *
     mLoaCoo_flow_nominal / mLoaCoo_flow_nominal_ref)
    "Cooling terminal unit scaling factor"
  parameter Modelica.Units.SI.Temperature T_aHeaWat_nominal=323.15
    "Heating water inlet temperature at nominal conditions"
  parameter Modelica.Units.SI.Temperature T_bHeaWat_nominal(
    min=273.15,
    displayUnit="degC") = T_aHeaWat_nominal - 10
    "Heating water outlet temperature at nominal conditions"
  parameter Modelica.Units.SI.Temperature T_aChiWat_nominal=280.15
    "Chilled water inlet temperature at nominal conditions "
  parameter Modelica.Units.SI.Temperature T_bChiWat_nominal(
    min=273.15,
    displayUnit="degC") = T_aChiWat_nominal + 5
    "Chilled water outlet temperature at nominal conditions"
  parameter Modelica.Units.SI.Temperature T_aLoaHea_nominal=293.15
    "Load side inlet temperature at nominal conditions in heating mode"
  parameter Modelica.Units.SI.Temperature T_aLoaCoo_nominal=298.15
    "Load side inlet temperature at nominal conditions in cooling mode"
  parameter Modelica.Units.SI.MassFraction w_aLoaCoo_nominal=0.01
    "Load side inlet humidity ratio at nominal conditions in cooling mode"
  parameter Modelica.Units.SI.MassFlowRate mLoaHea_flow_nominal(min=Modelica.Constants.eps)=0.5
    "Load side mass flow rate at nominal conditions in heating mode (single unit)"
  parameter Modelica.Units.SI.MassFlowRate mLoaCoo_flow_nominal(min=Modelica.Constants.eps)=
    mLoaHea_flow_nominal
    "Load side mass flow rate at nominal conditions in cooling mode (single unit)"

  parameter Modelica.Units.SI.Temperature T_aHeaWat_nominal_ref=323.15
    "Heating water inlet temperature at nominal conditions of reference terminal unit"
  parameter Modelica.Units.SI.Temperature T_aLoaHea_nominal_ref=293.15
    "Load side inlet temperature at nominal conditions in heating mode of reference terminal unit"
  parameter Modelica.Units.SI.MassFlowRate mLoaHea_flow_nominal_ref(min=Modelica.Constants.eps) = 0.5
    "Load side mass flow rate at nominal conditions in heating mode of reference terminal unit"
  parameter Modelica.Units.SI.HeatFlowRate QHea_flow_nominal_ref(min=Modelica.Constants.eps) = 4.5E3
    "Heat flow at nominal conditions in heating mode of reference terminal unit"

  parameter Modelica.Units.SI.Temperature T_aChiWat_nominal_ref=279.15
    "Chilled water inlet temperature at nominal conditions of reference terminal unit"
  parameter Modelica.Units.SI.Temperature T_aLoaCoo_nominal_ref=298.15
    "Load side inlet temperature at nominal conditions in cooling mode of reference terminal unit"
  parameter Modelica.Units.SI.MassFraction w_aLoaCoo_nominal_ref=0.01
    "Load side inlet humidity ratio at nominal conditions in cooling mode of reference terminal unit"
  parameter Modelica.Units.SI.MassFlowRate mLoaCoo_flow_nominal_ref(min=Modelica.Constants.eps) = 0.5
    "Load side mass flow rate at nominal conditions in cooling mode of reference terminal unit"
  parameter Modelica.Units.SI.HeatFlowRate QCoo_flow_nominal_ref(max=-Modelica.Constants.eps) = -5.8E3
    "Heat flow at nominal conditions in cooling mode of reference terminal unit"

  parameter Modelica.Units.SI.HeatFlowRate QCoo_flow_nominal(max=0)=
    if have_chiWat then
    Buildings.DHC.Loads.BaseClasses.getPeakLoad(string=
    "#Peak space cooling load",
    filNam=Modelica.Utilities.Files.loadResource(filNam))
    else 0
    "Design cooling heat flow rate (<=0)"
  parameter Modelica.Units.SI.HeatFlowRate QHea_flow_nominal(min=0)=
    if have_heaWat then
    Buildings.DHC.Loads.BaseClasses.getPeakLoad(string=
    "#Peak space heating load",
    filNam=Modelica.Utilities.Files.loadResource(filNam))
    else 0
    "Design heating heat flow rate (>=0)"
  parameter Modelica.Units.SI.MassFlowRate mChiWat_flow_nominal(min=0)=
      QCoo_flow_nominal/cp_default/(T_aChiWat_nominal - T_bChiWat_nominal)
    "Chilled water mass flow rate at nominal conditions (all units)"
  parameter Modelica.Units.SI.MassFlowRate mHeaWat_flow_nominal(min=0)=
      QHea_flow_nominal/cp_default/(T_aHeaWat_nominal - T_bHeaWat_nominal)
    "Heating water mass flow rate at nominal conditions (all units)"
  parameter Real k(
    min=0)=0.1
    "Gain of controller";
  parameter Modelica.Units.SI.Time Ti(min=Modelica.Constants.small) = 10
    "Time constant of integrator block";

  Buildings.Controls.OBC.CDL.Interfaces.RealOutput QReqHotWat_flow(
    final unit="W") if have_hotWat
    "SHW load"
  Buildings.Controls.OBC.CDL.Interfaces.RealOutput QReqHea_flow(
    final quantity="HeatFlowRate",
    final unit="W") if have_heaLoa
    "Heating load"
  Buildings.Controls.OBC.CDL.Interfaces.RealOutput QReqCoo_flow(
    final quantity="HeatFlowRate",
    final unit="W") if have_cooLoa
    "Cooling load"
  Modelica.Blocks.Sources.CombiTimeTable loa(
    tableOnFile=true,
    tableName="tab1",
    fileName=Modelica.Utilities.Files.loadResource(
      filNam),
    extrapolation=Modelica.Blocks.Types.Extrapolation.Periodic,
    y(each unit="W"),
    offset={0,0,0},
    columns={2,3,4},
    smoothness=Modelica.Blocks.Types.Smoothness.MonotoneContinuousDerivative1)
    "Reader for thermal loads (y[1] is cooling load, y[2] is space heating load, y[3] is domestic water heat load)"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant minTSet(
    k=293.15,
    y(final unit="K",
      displayUnit="degC"))
    if have_heaWat
    "Minimum temperature set point"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant maxTSet(
    k=297.15,
    y(final unit="K",
      displayUnit="degC"))
    if have_chiWat
    "Maximum temperature set point"
  replaceable Buildings.DHC.Loads.BaseClasses.Validation.BaseClasses.FanCoil2PipeHeating terUniHea(
    final k=k,
    final Ti=Ti) if have_heaWat
  constrainedby Buildings.DHC.Loads.BaseClasses.PartialTerminalUnit(
    redeclare final package Medium1=Medium,
    redeclare final package Medium2=Medium2,
    final allowFlowReversal=allowFlowReversal,
    final facMul=facMulHea,
    final facMulZon=1,
    final QHea_flow_nominal=QHea_flow_nominal/facMulHea,
    final mLoaHea_flow_nominal=mLoaHea_flow_nominal,
    final T_aHeaWat_nominal=T_aHeaWat_nominal,
    final T_bHeaWat_nominal=T_bHeaWat_nominal,
    final T_aLoaHea_nominal=T_aLoaHea_nominal)
    "Heating terminal unit"
  Buildings.DHC.Loads.BaseClasses.FlowDistribution disFloHea(
    redeclare final package Medium=Medium,
    final allowFlowReversal=allowFlowReversal,
    m_flow_nominal=mHeaWat_flow_nominal,
    have_pum=true,
    typCtr=Buildings.DHC.Loads.BaseClasses.Types.PumpControlType.ConstantHead,
    dp_nominal=100000,
    nPorts_a1=1,
    nPorts_b1=1) if have_heaWat
    "Heating water distribution system"
  Buildings.DHC.Loads.BaseClasses.FlowDistribution disFloCoo(
    redeclare final package Medium=Medium,
    final allowFlowReversal=allowFlowReversal,
    m_flow_nominal=mChiWat_flow_nominal,
    typDis=Buildings.DHC.Loads.BaseClasses.Types.DistributionType.ChilledWater,
    have_pum=true,
    typCtr=Buildings.DHC.Loads.BaseClasses.Types.PumpControlType.ConstantHead,
    dp_nominal=100000,
    nPorts_b1=1,
    nPorts_a1=1) if have_chiWat
    "Chilled water distribution system"
  replaceable
    Buildings.DHC.Loads.BaseClasses.Validation.BaseClasses.FanCoil2PipeCooling
    terUniCoo(
    final k=k,
    final Ti=Ti,
    final QEnv_flow_nominal=if have_heaWat then QHea_flow_nominal/facMulHea else -QCoo_flow_nominal/facMulCoo)
      if have_chiWat
    constrainedby
    Buildings.DHC.Loads.BaseClasses.PartialTerminalUnit(
    redeclare final package Medium1 = Medium,
    redeclare final package Medium2 = Medium2,
    final allowFlowReversal=allowFlowReversal,
    final facMul=facMulCoo,
    final facMulZon=1,
    final QCoo_flow_nominal=QCoo_flow_nominal/facMulCoo,
    final mLoaCoo_flow_nominal=mLoaCoo_flow_nominal,
    final T_aChiWat_nominal=T_aChiWat_nominal,
    final T_bChiWat_nominal=T_bChiWat_nominal,
    final T_aLoaCoo_nominal=T_aLoaCoo_nominal,
    final w_aLoaCoo_nominal=w_aLoaCoo_nominal) "Cooling terminal unit"
  Buildings.Controls.OBC.CDL.Reals.Add addPPum
    "Sum pump power"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant noCoo(
    k=0) if not have_chiWat
    "No cooling system"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant noHea(
    k=0) if not have_heaWat
    "No heating system"
  Buildings.Controls.OBC.CDL.Reals.Add addPFan
    "Sum fan power"
  Buildings.Controls.OBC.CDL.Reals.MultiplyByParameter mulQReqHea_flow(
    u(final unit="W"),
    final k=facMul) if have_heaLoa "Scaling"
  Buildings.Controls.OBC.CDL.Reals.MultiplyByParameter mulQReqCoo_flow(u(
        final unit="W"), final k=facMul) if have_cooLoa "Scaling"
  Buildings.Controls.OBC.CDL.Reals.MultiplyByParameter mulQReqHot_flow(u(final
        unit="W"), final k=facMul) if have_heaLoa "Scaling"
protected
  parameter Modelica.Units.SI.AbsolutePressure pSat_nominal=
    Buildings.Utilities.Psychrometrics.Functions.saturationPressure(T_aChiWat_nominal)
    "Saturation pressure at entering water temperature";
  parameter Modelica.Units.SI.AbsolutePressure pSat_nominal_ref=
    Buildings.Utilities.Psychrometrics.Functions.saturationPressure(T_aChiWat_nominal_ref)
    "Saturation pressure at entering water temperature for reference terminal unit";
  parameter Modelica.Units.SI.MassFraction X1_aLoaCoo_nominal=
     w_aLoaCoo_nominal / (1 + w_aLoaCoo_nominal)
     "Water vapor concentration in [kg/kg total air]";
  parameter Modelica.Units.SI.MassFraction X1Sat_nominal=
    Buildings.Utilities.Psychrometrics.Functions.X_pSatpphi(
      pSat=pSat_nominal, p=Medium2.p_default, phi=1.0)
    "Water vapor concentration at saturation in [kg/kg total air]";
  parameter Modelica.Units.SI.MassFraction X1_aLoaCoo_nominal_ref=
     w_aLoaCoo_nominal_ref / (1 + w_aLoaCoo_nominal_ref)
     "Water vapor concentration in [kg/kg total air]";
  parameter Modelica.Units.SI.MassFraction X1Sat_nominal_ref=
    Buildings.Utilities.Psychrometrics.Functions.X_pSatpphi(
      pSat=pSat_nominal_ref, p=Medium2.p_default, phi=1.0)
    "Water vapor concentration at saturation in [kg/kg total air]";
  parameter Modelica.Units.SI.SpecificEnthalpy h_aLoaCoo_nominal=
    Buildings.Media.Air.specificEnthalpy_pTX(
      p=Medium2.p_default, T=T_aLoaCoo_nominal, X={X1_aLoaCoo_nominal, 1-X1_aLoaCoo_nominal})
    "Specific enthalpy of enytering air at nominal conditions in cooling mode";
  parameter Modelica.Units.SI.SpecificEnthalpy hSat_nominal=
    Buildings.Media.Air.specificEnthalpy_pTX(
      p=Medium2.p_default, T=T_aChiWat_nominal, X={X1Sat_nominal, 1-X1Sat_nominal})
    "Specific enthalpy of saturated air at entering water temperature in cooling mode";
  parameter Modelica.Units.SI.SpecificEnthalpy h_aLoaCoo_nominal_ref=
    Buildings.Media.Air.specificEnthalpy_pTX(
      p=Medium2.p_default, T=T_aLoaCoo_nominal_ref, X={X1_aLoaCoo_nominal_ref, 1-X1_aLoaCoo_nominal_ref})
    "Specific enthalpy of enytering air at nominal conditions for reference terminal unit";
  parameter Modelica.Units.SI.SpecificEnthalpy hSat_nominal_ref=
    Buildings.Media.Air.specificEnthalpy_pTX(
      p=Medium2.p_default, T=T_aChiWat_nominal_ref, X={X1Sat_nominal_ref, 1-X1Sat_nominal_ref})
    "Specific enthalpy of saturated air at entering water temperature for reference terminal unit";
initial equation
  if have_chiWat then
    assert(QCoo_flow_nominal < -Modelica.Constants.eps, "QCoo_flow_nominal must be negative.");
    assert(T_aChiWat_nominal - T_bChiWat_nominal < 0, "Temperature difference (T_aChiWat_nominal - T_bChiWat_nominal) has wrong sign.");
  end if;
  if have_heaWat then
    assert(T_aHeaWat_nominal - T_bHeaWat_nominal > 0, "Temperature difference (T_aHeaWat_nominal - T_bHeaWat_nominal) has wrong sign.");
  end if;

equation
  connect(terUniHea.port_bHeaWat,disFloHea.ports_a1[1])
  connect(disFloHea.ports_b1[1],terUniHea.port_aHeaWat)
  connect(terUniHea.mReqHeaWat_flow,disFloHea.mReq_flow[1])
  connect(loa.y[1],terUniCoo.QReqCoo_flow)
  connect(loa.y[2],terUniHea.QReqHea_flow)
  connect(disFloCoo.ports_b1[1],terUniCoo.port_aChiWat)
  connect(terUniCoo.port_bChiWat,disFloCoo.ports_a1[1])
  connect(terUniCoo.mReqChiWat_flow,disFloCoo.mReq_flow[1])
  connect(minTSet.y,terUniHea.TSetHea)
  connect(maxTSet.y,terUniCoo.TSetCoo)
  connect(disFloHea.PPum,addPPum.u1)
  connect(disFloCoo.PPum,addPPum.u2)
  connect(noHea.y,addPPum.u1)
  connect(noCoo.y,addPPum.u2)
  connect(noHea.y,addPFan.u1)
  connect(noCoo.y,addPFan.u2)
  connect(terUniCoo.PFan,addPFan.u2)
  connect(terUniHea.PFan,addPFan.u1)
  connect(disFloCoo.port_b, mulChiWatOut[1].port_a)
  connect(disFloHea.port_b, mulHeaWatOut[1].port_a)
  connect(mulHeaWatInl[1].port_b, disFloHea.port_a)
  connect(mulChiWatInl[1].port_b, disFloCoo.port_a)
  connect(addPFan.y, mulPFan.u)
  connect(addPPum.y, mulPPum.u)
  connect(mulQReqCoo_flow.y, QReqCoo_flow)
  connect(mulQReqHea_flow.y, QReqHea_flow)
  connect(loa.y[1], mulQReqCoo_flow.u)
  connect(loa.y[2], mulQReqHea_flow.u)
  connect(disFloHea.QActTot_flow, mulQHea_flow.u)
  connect(disFloCoo.QActTot_flow, mulQCoo_flow.u)
  connect(mulQReqHot_flow.y, QReqHotWat_flow)
  connect(mulQReqHot_flow.u, loa.y[3])
end BuildingTimeSeries;


Block that outputs the fraction of time when a constraint is violated. Block that outputs the running fractional time during which any element
ui of the input signal
is not within umin ≤ ui ≤ umax.

within Buildings.DHC.Loads.BaseClasses;
block ConstraintViolation
  "Block that outputs the fraction of time when a constraint is violated"

  parameter Real uMin "Minimum value for input";
  parameter Real uMax "Maximum value for input";
  parameter Integer nu(min=0) = 0
    "Number of input connections"
  Buildings.Controls.OBC.CDL.Interfaces.RealInput u[nu]
    "Variables of interest"
  Buildings.Controls.OBC.CDL.Interfaces.RealOutput y(
    final unit="1",
    start=0,
    fixed=true)
    "Fraction of time when the constraint is violated"
  Modelica.Units.SI.Time t(final start=0, final fixed=true)
    "Integral of violated time";
protected
  parameter Modelica.Units.SI.Time t0(fixed=false) "First sample time instant";
  Boolean vioMin "Flag, true if minimum is violated";
  Boolean vioMax "Flag, true if maximum is violated";
initial equation
  t0 = time - 1E-6;
equation
  vioMin = Modelica.Math.BooleanVectors.anyTrue({u[i] < uMin for i in 1:nu});
  vioMax = Modelica.Math.BooleanVectors.anyTrue({u[i] > uMax for i in 1:nu});
  if vioMin or vioMax then
    der(t) = 1;
  else
    der(t) = 0;
  end if;
  y = t / (time - t0);
end ConstraintViolation;


Model of a sensible only four-pipe fan coil unit computing a required water mass flow rate. This is a simplified model of a sensible only four-pipe fan coil unit for heating and cooling.
It is intended to be coupled to a room model by means of fluid ports.
See

Buildings.DHC.Loads.BaseClasses.PartialFanCoil4Pipe
for a description of the modeling principles.

within Buildings.DHC.Loads.BaseClasses;
model FanCoil4Pipe
  "Model of a sensible only four-pipe fan coil unit computing a required water mass flow rate"
  extends PartialFanCoil4Pipe(
    final have_TSen=true,
    final have_fluPor=true,
    final have_heaPor=false);
equation
  connect(TSen,conCoo.u_m)
  connect(TSen,conHea.u_m)
  connect(hexHea.port_b2,mulLoaMasFloOut.port_a)
  connect(mulLoaMasFloInl.port_b,fan.port_a)
end FanCoil4Pipe;


within Buildings.DHC.Loads.BaseClasses;
model FanCoil4PipeHeatPorts
  "Model of a sensible only four-pipe fan coil unit computing a required water mass flow rate"
  extends PartialFanCoil4Pipe(
    final have_heaPor=true,
    final have_fluPor=false,
    final have_TSen=false);
  Buildings.HeatTransfer.Sources.PrescribedHeatFlow heaFloHeaCon
    "Convective heat flow rate to load"
  Buildings.HeatTransfer.Sources.PrescribedHeatFlow heaFloCooCon
    "Convective heat flow rate to load"
  Fluid.Sources.Boundary_pT retAir(
    redeclare package Medium=Medium2,
    use_T_in=true,
    nPorts=1)
    "Source for return air"
  Fluid.Sources.Boundary_pT sinAir(
    redeclare package Medium=Medium2,
    use_T_in=false,
    nPorts=1)
    "Sink for supply air"
  HeatTransfer.Sources.PrescribedHeatFlow heaFloHeaRad
    "Radiative heat flow rate to load"
  HeatTransfer.Sources.PrescribedHeatFlow heaFloCooRad
    "Radiative heat flow rate to load"
  Buildings.Controls.OBC.CDL.Reals.Sources.Constant zero(
    k=0)
    "Zero"
  Modelica.Thermal.HeatTransfer.Sensors.TemperatureSensor senT
    "Load temperature (measured)"
equation
  connect(hexHea.port_b2,sinAir.ports[1])
  connect(Q_flowCoo.y,heaFloCooCon.Q_flow)
  connect(Q_flowHea.y,heaFloHeaCon.Q_flow)
  connect(zero.y,heaFloHeaRad.Q_flow)
  connect(zero.y,heaFloCooRad.Q_flow)
  connect(retAir.ports[1],fan.port_a)
  connect(heaPorCon,senT.port)
  connect(senT.T,retAir.T_in)
  connect(senT.T,conCoo.u_m)
  connect(senT.T,conHea.u_m)
  connect(heaFloCooCon.port,mulHeaFloCon.port_a)
  connect(heaFloHeaCon.port,mulHeaFloCon.port_a)
  connect(heaFloHeaRad.port,mulHeaFloRad.port_a)
  connect(heaFloCooRad.port,mulHeaFloRad.port_a)
end FanCoil4PipeHeatPorts;