Test model for UACp

within Buildings.Fluid.DXSystems.Cooling.BaseClasses.Examples;
model UACp "Test model for UACp"
  extends Modelica.Icons.Example;
  package Medium =Buildings.Media.Air;
  Buildings.Fluid.DXSystems.Cooling.BaseClasses.UACp uacp(
    per=nomVal,
    redeclare package Medium = Medium,
    homotopyInitialization=true) "Calculates UA/Cp value for the coil"
  parameter Cooling.AirSource.Data.Generic.BaseClasses.NominalValues nomVal(
    Q_flow_nominal=-21000,
    COP_nominal=3,
    SHR_nominal=0.8,
    m_flow_nominal=1.5) "Nominal values"
end UACp;


Test model for WetCoil

within Buildings.Fluid.DXSystems.Cooling.BaseClasses.Examples;
model WetCoil "Test model for WetCoil"
 extends Modelica.Icons.Example;
 package Medium =  Buildings.Media.Air;
  Modelica.Blocks.Sources.Constant p(
    k=101325) "Pressure"
  Buildings.Fluid.DXSystems.Cooling.BaseClasses.WetCoil wetCoi(
    redeclare package Medium = Medium,
    datCoi = datCoi,
    variableSpeedCoil=true,
    use_mCon_flow=false,
    redeclare
      Buildings.Fluid.DXSystems.BaseClasses.CapacityAirSource
      coiCap) "Performs calculation for wet coil condition"
  Modelica.Blocks.Sources.Constant TConIn(
    k=273.15 + 35) "Condenser inlet air temperature"
  Modelica.Blocks.Sources.IntegerStep onOff(
    startTime=600) "Compressor on-off signal"
  Modelica.Blocks.Sources.Ramp m_flow(
    duration=600,
    startTime=1200,
    height=1.05) "Mass flow rate of air"
  Modelica.Blocks.Sources.Ramp XEvaIn(
    duration=600,
    startTime=2400,
    height=-0.002,
    offset=0.012) "Inlet mass-fraction"
  Modelica.Blocks.Sources.Ramp hEvaIn(
    duration=600,
    startTime=2400,
    offset=60000,
    height=-10000) "Specific enthalpy of air entering the coil"
  Modelica.Blocks.Sources.Ramp TEvaIn(
    duration=600,
    startTime=2400,
    height=-5,
    offset=273.15 + 29) "Inlet air temperature"
  Modelica.Blocks.Sources.TimeTable speRat(table=[0.0,0.0; 900,0.25; 1800,0.50;
        2700,0.75]) "Speed ratio "
  parameter Buildings.Fluid.DXSystems.Cooling.AirSource.Data.Generic.DXCoil datCoi(nSta=4, sta={
        Buildings.Fluid.DXSystems.Cooling.AirSource.Data.Generic.BaseClasses.Stage(
        spe=900/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.AirSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-12000,
          COP_nominal=3,
          SHR_nominal=0.8,
          m_flow_nominal=0.9),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.BaseClasses.Examples.PerformanceCurves.Curve_I_AirCooled()),
        Buildings.Fluid.DXSystems.Cooling.AirSource.Data.Generic.BaseClasses.Stage(
        spe=1200/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.AirSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-18000,
          COP_nominal=3,
          SHR_nominal=0.8,
          m_flow_nominal=1.2),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.BaseClasses.Examples.PerformanceCurves.Curve_I_AirCooled()),
        Buildings.Fluid.DXSystems.Cooling.AirSource.Data.Generic.BaseClasses.Stage(
        spe=1800/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.AirSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-21000,
          COP_nominal=3,
          SHR_nominal=0.8,
          m_flow_nominal=1.5),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.BaseClasses.Examples.PerformanceCurves.Curve_II_AirCooled()),
        Buildings.Fluid.DXSystems.Cooling.AirSource.Data.Generic.BaseClasses.Stage(
        spe=2400/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.AirSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-30000,
          COP_nominal=3,
          SHR_nominal=0.8,
          m_flow_nominal=1.8),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.BaseClasses.Examples.PerformanceCurves.Curve_III_AirCooled())})
    "Coil data"
equation
  connect(p.y, wetCoi.p)
  connect(TConIn.y, wetCoi.TConIn)
  connect(m_flow.y, wetCoi.m_flow)
  connect(XEvaIn.y, wetCoi.XEvaIn)
  connect(hEvaIn.y, wetCoi.hEvaIn)
  connect(TEvaIn.y, wetCoi.TEvaIn)
  connect(speRat.y, wetCoi.speRat)
  connect(onOff.y, wetCoi.stage)
end WetCoil;


Package with sevral performance curves

within Buildings.Fluid.DXSystems.Cooling.BaseClasses.Examples;
package PerformanceCurves "Package with sevral performance curves"
end PerformanceCurves;


Package with functions

within Buildings.Fluid.DXSystems.Cooling.BaseClasses;
package Functions "Package with functions"
  extends Modelica.Icons.BasesPackage;
end Functions;


Interpolates value between two speeds. This function interpolates data for intermediate compressor speeds
using cubic hermite splines with linear extrapolation.
To avoid linear extrapolation below minimum standard speed,
the origin is added to the modified arrays.

within Buildings.Fluid.DXSystems.Cooling.BaseClasses.Functions;
function speedShift "Interpolates value between two speeds"
  //x in cubic Hermite function
  input Real spe "Speed of compressor";
  //x1, x2  in cubic Hermite function
  input Real speSet[:] "Array of standard compressor speeds";
  //y1, y2 in cubic Hermite function
  input Real u[:] "Array to be interpolated";
  output Real y "Interpolated value";
protected
  Integer n= size(u,1)+1 "Number of elements in modified array";
  Real derv[n] "deriative";
  Integer i=1 "Integer to select data interval";
  Real modSpeSet[n] "Speed set with origin";
  Real modU[n];
algorithm
    //y1d, y2d in cubic Hermite function
    modSpeSet:= cat(1, {0.00}, speSet);
    modU     := cat(1, {0.00}, u);
    //Derivatives at standard speeds
    derv     := Buildings.Utilities.Math.Functions.splineDerivatives(
                   x=modSpeSet,
                   y=modU,
                   ensureMonotonicity=Buildings.Utilities.Math.Functions.isMonotonic(x=modU,
                                                                                strict=false));
    //locate the range between which compressor operates
    for j in 1:size(modSpeSet, 1) - 1 loop
      if spe > modSpeSet[j] then
        i :=j;
      end if;
    end for;
    y :=Buildings.Utilities.Math.Functions.cubicHermiteLinearExtrapolation(
      x=spe,
      x1=modSpeSet[i],
      x2=modSpeSet[i + 1],
      y1=modU[i],
      y2=modU[i + 1],
      y1d=derv[i],
      y2d=derv[i + 1]);
end speedShift;


Multi speed water source DX coils. This model can be used to simulate a water source DX cooling coil with multiple
operating stages. Depending on the used performance curves, each
stage could be a different compressor speed, or a different mode
of operation, such as with or without hot gas reheat.

within Buildings.Fluid.DXSystems.Cooling.WaterSource;
model MultiStage "Multi speed water source DX coils"
  extends
    Buildings.Fluid.DXSystems.Cooling.BaseClasses.PartialWaterCooledDXCoil(
      redeclare final Buildings.Fluid.DXSystems.Cooling.AirSource.MultiStage eva);

  Modelica.Blocks.Interfaces.IntegerInput stage
    "Stage of cooling coil (0: off, 1: first stage, 2: second stage...)"

equation
  connect(eva.stage, stage)
end MultiStage;


Water source DX coils

within Buildings.Fluid.DXSystems.Cooling;
package WaterSource "Water source DX coils"
  extends Modelica.Icons.VariantsPackage;
end WaterSource;


Single speed water source DX coils. This model can be used to simulate a water source DX cooling coil with single speed compressor.

within Buildings.Fluid.DXSystems.Cooling.WaterSource;
model SingleSpeed "Single speed water source DX coils"
  extends Buildings.Fluid.DXSystems.Cooling.BaseClasses.PartialWaterCooledDXCoil(
    redeclare Buildings.Fluid.DXSystems.Cooling.AirSource.SingleSpeed eva);

  Modelica.Blocks.Interfaces.BooleanInput on
    "Set to true to enable compressor, or false to disable compressor"
equation
  connect(eva.on, on)
end SingleSpeed;


Variable speed water source DX coils. This model can be used to simulate a water source DX cooling coil with variable speed compressor.

within Buildings.Fluid.DXSystems.Cooling.WaterSource;
model VariableSpeed "Variable speed water source DX coils"
  extends
    Buildings.Fluid.DXSystems.Cooling.BaseClasses.PartialWaterCooledDXCoil(
      redeclare final
      Buildings.Fluid.DXSystems.Cooling.AirSource.VariableSpeed
      eva(
        final minSpeRat=minSpeRat,
        final speRatDeaBan=speRatDeaBan));

  parameter Real minSpeRat(min=0,max=1) "Minimum speed ratio";
  parameter Real speRatDeaBan= 0.05 "Deadband for minimum speed ratio";

  Modelica.Blocks.Interfaces.RealInput speRat(
   min=0,
   max=1,
   final unit="1")
   "Speed ratio"

equation

  connect(speRat, eva.speRat)
end VariableSpeed;


within Buildings.Fluid.DXSystems.Cooling.WaterSource;
package Data "Package with performance data for water source DX coils"
  extends Modelica.Icons.MaterialPropertiesPackage;
end Data;


Performance record for a DX Cooling DXCoil with one or multiple stages. This record declares the performance data for the water source DX cooling coil model.
The performance data are structured as follows:

within Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic;
record DXCoil
  "Performance record for a DX Cooling DXCoil with one or multiple stages"
  extends
    Buildings.Fluid.DXSystems.Cooling.AirSource.Data.Generic.DXCoil(
  redeclare parameter Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage sta[nSta]);
end DXCoil;


Package with generic performance curves

within Buildings.Fluid.DXSystems.Cooling.WaterSource.Data;
package Generic "Package with generic performance curves"
  extends Modelica.Icons.MaterialPropertiesPackage;
end Generic;


within Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic;
package BaseClasses "Package with base classes for performance data"
  extends Modelica.Icons.BasesPackage;
end BaseClasses;


within Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples;
model MultiStage "Test model for multi speed water source DX coil"
  package MediumAir = Buildings.Media.Air;
  package MediumWater = Buildings.Media.Water;
  extends Modelica.Icons.Example;
  parameter Modelica.Units.SI.MassFlowRate m_flow_nominal=datCoi.sta[datCoi.nSta].nomVal.m_flow_nominal
    "Nominal mass flow rate";
  parameter Modelica.Units.SI.PressureDifference dpEva_nominal=1000
    "Pressure drop at m_flow_nominal";
  parameter Modelica.Units.SI.PressureDifference dpCon_nominal=40000
    "Pressure drop at mCon_flow_nominal";
  Buildings.Fluid.Sources.Boundary_pT sinAir(
    redeclare package Medium = MediumAir,
    nPorts=1,
    p(displayUnit="Pa")) "Sink on air side"
  Buildings.Fluid.Sources.MassFlowSource_T souAir(
    redeclare package Medium = MediumAir,
    use_T_in=true,
    nPorts=1,
    m_flow=1.5,
    T=299.85) "Source on air side"
  Modelica.Blocks.Sources.Ramp TEvaIn(
    duration=600,
    startTime=2400,
    height=-5,
    offset=273.15 + 23) "Temperature"
  Buildings.Fluid.DXSystems.Cooling.WaterSource.MultiStage  mulSpeDX(
    redeclare package MediumEva = MediumAir,
    redeclare package MediumCon = MediumWater,
    datCoi=datCoi,
    dpEva_nominal=dpEva_nominal,
    dpCon_nominal=dpCon_nominal,
    show_T=true,
    energyDynamics=Modelica.Fluid.Types.Dynamics.SteadyState)
                 "Multi-speed DX coil"
  parameter
    Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.DXCoil
    datCoi(nSta=4, sta={
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=900/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-12000,
          COP_nominal=3,
          SHR_nominal=0.8,
          m_flow_nominal=0.9,
          mCon_flow_nominal=0.57143,
          TEvaIn_nominal=273.15 + 26.67,
          TConIn_nominal=273.15 + 29.4),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=1200/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-18000,
          COP_nominal=3,
          SHR_nominal=0.8,
          m_flow_nominal=1.2,
          mCon_flow_nominal=0.85714,
          TEvaIn_nominal=273.15 + 26.67,
          TConIn_nominal=273.15 + 29.4),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=1800/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-21000,
          COP_nominal=3,
          SHR_nominal=0.8,
          m_flow_nominal=1.5,
          mCon_flow_nominal=1,
          TEvaIn_nominal=273.15 + 26.67,
          TConIn_nominal=273.15 + 29.4),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=2400/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-30000,
          COP_nominal=3,
          SHR_nominal=0.8,
          m_flow_nominal=1.8,
          mCon_flow_nominal=1.42857,
          TEvaIn_nominal=273.15 + 26.67,
          TConIn_nominal=273.15 + 29.4),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I())})
    "Coil data"
  Modelica.Blocks.Sources.Ramp mCon_flow(
    duration=600,
    startTime=6000,
    height=0,
    offset=1) "Condenser inlet mass flow"
  Buildings.Fluid.Sources.MassFlowSource_T souWat(
    redeclare package Medium = MediumWater,
    nPorts=1,
    use_T_in=false,
    use_m_flow_in=true,
    T=298.15) "Source on water side"
  Buildings.Fluid.Sources.Boundary_pT sinWat(
    redeclare package Medium = MediumWater,
    nPorts=1,
    p(displayUnit="Pa")) "Sink on water side"
  Modelica.Blocks.Sources.IntegerTable speRat(table=[
    0.0,0.0;
    900,1;
    1800,4;
    2700,3;
    3600,2]) "Speed ratio "
equation
  connect(TEvaIn.y, souAir.T_in)
  connect(mulSpeDX.portCon_a, souWat.ports[1])
  connect(mulSpeDX.portCon_b, sinWat.ports[1])
  connect(souAir.ports[1], mulSpeDX.port_a)
  connect(sinAir.ports[1], mulSpeDX.port_b)
  connect(mCon_flow.y, souWat.m_flow_in)
  connect(speRat.y,mulSpeDX. stage)
end MultiStage;


within Buildings.Fluid.DXSystems.Cooling.WaterSource;
package Examples "Package with example of water source DX cooling coil models"
  extends Modelica.Icons.ExamplesPackage;
end Examples;


Test model for single speed DX coil

within Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples;
model SingleSpeed "Test model for single speed DX coil"

  package MediumAir = Buildings.Media.Air;
  package MediumWater = Buildings.Media.Water;
  extends Modelica.Icons.Example;
  parameter Modelica.Units.SI.MassFlowRate m_flow_nominal=datCoi.sta[datCoi.nSta].nomVal.m_flow_nominal
    "Nominal mass flow rate";
  parameter Modelica.Units.SI.PressureDifference dpEva_nominal=1000
    "Pressure drop at m_flow_nominal";
  parameter Modelica.Units.SI.PressureDifference dpCon_nominal=40000
    "Pressure drop at mCon_flow_nominal";
  Buildings.Fluid.Sources.Boundary_pT sinAir(
    redeclare package Medium = MediumAir,
    nPorts=1,
    p(displayUnit="Pa")) "Sink on air side"
  Buildings.Fluid.Sources.MassFlowSource_T souAir(
    redeclare package Medium = MediumAir,
    use_T_in=true,
    nPorts=1,
    m_flow=1.5,
    T=299.85) "Source on air side"
  Modelica.Blocks.Sources.BooleanStep onOff(startTime=600)
    "Compressor on-off signal"
  Modelica.Blocks.Sources.Ramp TEvaIn(
    duration=600,
    startTime=2400,
    height=-5,
    offset=273.15 + 23) "Temperature"
  Buildings.Fluid.DXSystems.Cooling.WaterSource.SingleSpeed sinSpeDX(
    redeclare package MediumEva = MediumAir,
    redeclare package MediumCon = MediumWater,
    datCoi=datCoi,
    dpEva_nominal=dpEva_nominal,
    dpCon_nominal=dpCon_nominal,
    show_T=true,
    energyDynamics=Modelica.Fluid.Types.Dynamics.SteadyState)
    "Single speed DX coil"
  parameter
    Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.DXCoil
    datCoi(nSta=1, sta={
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=1800/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-21000,
          COP_nominal=3,
          SHR_nominal=0.8,
          m_flow_nominal=1.5,
          mCon_flow_nominal=1,
          TEvaIn_nominal=273.15 + 26.67,
          TConIn_nominal=273.15 + 29.4),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I())})
    "Coil data"
  Modelica.Blocks.Sources.Ramp mCon_flow(
    duration=600,
    startTime=6000,
    height=0,
    offset=1) "Condenser inlet mass flow"
  Buildings.Fluid.Sources.MassFlowSource_T souWat(
    redeclare package Medium = MediumWater,
    nPorts=1,
    use_T_in=false,
    use_m_flow_in=true,
    T=298.15) "Source on water side"
  Buildings.Fluid.Sources.Boundary_pT sinWat(
    redeclare package Medium = MediumWater,
    nPorts=1,
    p(displayUnit="Pa")) "Sink on water side"
equation
  connect(TEvaIn.y, souAir.T_in)
  connect(onOff.y, sinSpeDX.on)
  connect(sinSpeDX.portCon_a, souWat.ports[1])
  connect(sinSpeDX.portCon_b, sinWat.ports[1])
  connect(souAir.ports[1], sinSpeDX.port_a)
  connect(sinAir.ports[1], sinSpeDX.port_b)
  connect(mCon_flow.y, souWat.m_flow_in)
end SingleSpeed;


Test model for variable speed DX coil

within Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples;
model VariableSpeed "Test model for variable speed DX coil"

  package MediumAir = Buildings.Media.Air;
  package MediumWater = Buildings.Media.Water;
  extends Modelica.Icons.Example;
  parameter Modelica.Units.SI.MassFlowRate m_flow_nominal=datCoi.sta[datCoi.nSta].nomVal.m_flow_nominal
    "Nominal mass flow rate";
  parameter Modelica.Units.SI.PressureDifference dpEva_nominal=1000
    "Pressure drop at m_flow_nominal";
  parameter Modelica.Units.SI.PressureDifference dpCon_nominal=40000
    "Pressure drop at mCon_flow_nominal";
  Buildings.Fluid.Sources.Boundary_pT sinAir(
    redeclare package Medium = MediumAir,
    nPorts=1,
    p(displayUnit="Pa")) "Sink on air side"
  Buildings.Fluid.Sources.MassFlowSource_T souAir(
    redeclare package Medium = MediumAir,
    use_T_in=true,
    nPorts=1,
    m_flow=1.5,
    T=299.85) "Source on air side"
  Modelica.Blocks.Sources.Ramp TEvaIn(
    duration=600,
    startTime=2400,
    height=-5,
    offset=273.15 + 23) "Temperature"
  Buildings.Fluid.DXSystems.Cooling.WaterSource.VariableSpeed varSpeDX(
    redeclare package MediumEva = MediumAir,
    redeclare package MediumCon = MediumWater,
    dpEva_nominal=dpEva_nominal,
    dpCon_nominal=dpCon_nominal,
    datCoi=datCoi,
    show_T=true,
    minSpeRat=datCoi.minSpeRat,
    energyDynamics=Modelica.Fluid.Types.Dynamics.SteadyState)
                                 "Variable speed DX coil"

  parameter
    Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.DXCoil
    datCoi(nSta=1, sta={
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=1800/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-21000,
          COP_nominal=3,
          SHR_nominal=0.8,
          m_flow_nominal=1.5,
          mCon_flow_nominal=1,
          TEvaIn_nominal=273.15 + 26.67,
          TConIn_nominal=273.15 + 29.4),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I())})
    "Coil data"
  Modelica.Blocks.Sources.Ramp mCon_flow(
    duration=600,
    startTime=6000,
    height=0,
    offset=1) "Condenser inlet mass flow"
  Buildings.Fluid.Sources.MassFlowSource_T souWat(
    redeclare package Medium = MediumWater,
    nPorts=1,
    use_T_in=false,
    use_m_flow_in=true,
    T=298.15) "Source on water side"
  Buildings.Fluid.Sources.Boundary_pT sinWat(
    redeclare package Medium = MediumWater,
    nPorts=1,
    p(displayUnit="Pa")) "Sink on water side"
  Modelica.Blocks.Sources.TimeTable speRat(table=[0.0,0.0; 100,0.0; 900,0.2;
        1800,0.8; 2700,0.75; 3600,0.75]) "Speed ratio "
equation
  connect(TEvaIn.y, souAir.T_in)
  connect(varSpeDX.portCon_a, souWat.ports[1])
  connect(varSpeDX.portCon_b, sinWat.ports[1])
  connect(souAir.ports[1], varSpeDX.port_a)
  connect(sinAir.ports[1], varSpeDX.port_b)
  connect(mCon_flow.y, souWat.m_flow_in)
  connect(speRat.y, varSpeDX.speRat)
end VariableSpeed;


Package with sevral performance curves

within Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples;
package PerformanceCurves "Package with sevral performance curves"
end PerformanceCurves;


Collection of validation models

within Buildings.Fluid.DXSystems.Cooling.WaterSource;
package Validation "Collection of validation models"
  extends Modelica.Icons.ExamplesPackage;
end Validation;


within Buildings.Fluid.DXSystems.Cooling.WaterSource.Validation;
model VariableSpeedEnergyPlus
  "Validation model for variable speed DX coil "
  extends Modelica.Icons.Example;

  package MediumAir = Buildings.Media.Air;
  package MediumWater = Buildings.Media.Water;

  parameter Modelica.Units.SI.Power Q_flow_nominal=datCoi.sta[1].nomVal.Q_flow_nominal
    "Nominal power";
  parameter Modelica.Units.SI.MassFlowRate m_flow_nominal=datCoi.sta[1].nomVal.m_flow_nominal
    "Nominal mass flow rate";
  parameter Modelica.Units.SI.PressureDifference dpEva_nominal=1000
    "Pressure drop at m_flow_nominal";
  parameter Modelica.Units.SI.PressureDifference dpCon_nominal=40000
    "Pressure drop at mCon_flow_nominal";

  WaterSource.VariableSpeed varSpeDX(
    redeclare package MediumEva = MediumAir,
    redeclare package MediumCon = MediumWater,
    datCoi=datCoi,
    dpEva_nominal=dpEva_nominal,
    dpCon_nominal=dpCon_nominal,
    minSpeRat=0.05,
    TEva_start=datCoi.sta[1].nomVal.TEvaIn_nominal,
    energyDynamics=Modelica.Fluid.Types.Dynamics.FixedInitial)
    "Variable speed DX coil"

  parameter
    Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.DXCoil
    datCoi(
    nSta=10,
    minSpeRat=0.1,
    sta={
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=500/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-1524.1,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.16648632,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=1000/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-1877.9,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.1849848,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=1500/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-2226.6,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.20348328,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=2000/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-2911.3,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.24048024,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=2500/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-3581.7,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.2774772,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=3000/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-4239.5,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.31447416,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=3500/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-4885.7,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.35147112,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=4000/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-5520.7,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.38846808,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=4500/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-6144.8,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.42546504,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=5000/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-6758,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.462462,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I())})
    "Coil data"
  Modelica.Blocks.Sources.TimeTable speRat(table=[0,0; 3600,0; 3600,0; 7200,0;
        7200,0; 10800,0; 10800,0; 14400,0; 14400,0; 18000,0; 18000,0; 21600,0;
        21600,0; 25200,0; 25200,1; 28800,1; 28800,1; 32400,1; 32400,1; 36000,1;
        36000,1; 39600,1; 39600,1; 43200,1; 43200,1; 46800,1; 46800,1; 50400,1;
        50400,1; 54000,1; 54000,1; 57600,1; 57600,1; 61200,1; 61200,0; 64800,0;
        64800,0; 68400,0; 68400,0; 72000,0; 72000,0; 75600,0; 75600,0; 79200,0;
        79200,0; 82800,0; 82800,0; 86400,0])
    "EnergyPlus speed ratio for this model"
  Modelica.Blocks.Sources.TimeTable TCIn(table=[0,14.00192912; 3600,14.00192912;
        3600,14.00192912; 7200,14.00192912; 7200,14.00192912; 10800,14.00192912;
        10800,14.00192912; 14400,14.00192912; 14400,14.00192912; 18000,14.00192912;
        18000,14.00192912; 21600,14.00192912; 21600,14.00192912; 25200,14.00192912;
        25200,13.87116782; 28800,13.87116782; 28800,13.68940516; 32400,13.68940516;
        32400,13.61181349; 36000,13.61181349; 36000,13.56830163; 39600,13.56830163;
        39600,13.53753354; 43200,13.53753354; 43200,13.53223224; 46800,13.53223224;
        46800,13.544731; 50400,13.544731; 50400,13.56823102; 54000,13.56823102;
        54000,13.59838157; 57600,13.59838157; 57600,13.63238101; 61200,13.63238101;
        61200,13.66852976; 64800,13.66852976; 64800,13.66852976; 68400,13.66852976;
        68400,13.66852976; 72000,13.66852976; 72000,13.66852976; 75600,13.66852976;
        75600,13.66852976; 79200,13.66852976; 79200,13.66852976; 82800,13.66852976;
        82800,13.66852976; 86400,13.66852976])
    "Condenser inlet temperature"
  Modelica.Blocks.Sources.TimeTable TEvaIn(table=[0,29.12922216; 3600,29.12922216;
        3600,29.05566774; 7200,29.05566774; 7200,28.96128616; 10800,28.96128616;
        10800,28.85494074; 14400,28.85494074; 14400,28.74236486; 18000,28.74236486;
        18000,28.63355658; 21600,28.63355658; 21600,28.65392227; 25200,28.65392227;
        25200,25.92690623; 28800,25.92690623; 28800,24.32658509; 32400,24.32658509;
        32400,24.00495885; 36000,24.00495885; 36000,23.4817184; 39600,23.4817184;
        39600,23.50449436; 43200,23.50449436; 43200,23.48876996; 46800,23.48876996;
        46800,23.52144673; 50400,23.52144673; 50400,23.61388018; 54000,23.61388018;
        54000,23.64607195; 57600,23.64607195; 57600,23.58143828; 61200,23.58143828;
        61200,26.68408826; 64800,26.68408826; 64800,27.85802964; 68400,27.85802964;
        68400,28.17092771; 72000,28.17092771; 72000,28.78657683; 75600,28.78657683;
        75600,29.10487544; 79200,29.10487544; 79200,29.21730921; 82800,29.21730921;
        82800,29.18512017; 86400,29.18512017])
     "Coil inlet temperature"
  Modelica.Blocks.Sources.TimeTable XEvaIn(table=[0,0.00531; 3600,0.00531; 3600,
        0.0053; 7200,0.0053; 7200,0.00529; 10800,0.00529; 10800,0.00529; 14400,0.00529;
        14400,0.00528; 18000,0.00528; 18000,0.00527; 21600,0.00527; 21600,0.00527;
        25200,0.00527; 25200,0.00531; 28800,0.00531; 28800,0.00537; 32400,0.00537;
        32400,0.00539; 36000,0.00539; 36000,0.00542; 39600,0.00542; 39600,0.00541;
        43200,0.00541; 43200,0.0054; 46800,0.0054; 46800,0.00534; 50400,0.00534;
        50400,0.00534; 54000,0.00534; 54000,0.00536; 57600,0.00536; 57600,0.00538;
        61200,0.00538; 61200,0.00533; 64800,0.00533; 64800,0.00532; 68400,0.00532;
        68400,0.00532; 72000,0.00532; 72000,0.00532; 75600,0.00532; 75600,0.00531;
        79200,0.00531; 79200,0.00531; 82800,0.00531; 82800,0.0053; 86400,0.0053])
    "Water fraction of moist air"
  Modelica.Blocks.Routing.Multiplex2 mux "Converts in an array"
  Buildings.Utilities.IO.BCVTB.From_degC TCIn_K "Converts degC to K"
  Buildings.Utilities.IO.BCVTB.From_degC TEvaIn_K "Converts degC to K"
  Modelica.Blocks.Math.Mean TOutMea(f=1/3600)
  Buildings.Utilities.IO.BCVTB.To_degC TOutDegC
  Modelica.Blocks.Sources.RealExpression TOut(y=varSpeDX.eva.vol.T)
  Modelica.Blocks.Math.Mean XEvaOutMea(f=1/3600)
  Modelica.Blocks.Sources.RealExpression XEvaOut(y=sum(varSpeDX.eva.vol.Xi))
  Modelica.Blocks.Math.Mean Q_flowMea(f=1/3600) "Mean of cooling rate"
  Modelica.Blocks.Math.Mean Q_flowSenMea(f=1/3600)
    "Mean of sensible cooling rate"
  Modelica.Blocks.Math.Mean PMea(f=1/3600) "Mean of power"
  Modelica.Blocks.Math.Add add(k1=-1)
  Modelica.Blocks.Sources.Constant XEvaInMoiAir(k=1.0) "Moist air fraction = 1"
  Modelica.Blocks.Sources.TimeTable TOutEPlu(table=[0,29.12922216; 3600,29.12922216;
        3600,29.05566774; 7200,29.05566774; 7200,28.96128616; 10800,28.96128616;
        10800,28.85494074; 14400,28.85494074; 14400,28.74236486; 18000,28.74236486;
        18000,28.63355658; 21600,28.63355658; 21600,28.65392227; 25200,28.65392227;
        25200,9.511780529; 28800,9.511780529; 28800,8.096740784; 32400,8.096740784;
        32400,7.757702268; 36000,7.757702268; 36000,7.493011832; 39600,7.493011832;
        39600,7.487562491; 43200,7.487562491; 43200,7.469723021; 46800,7.469723021;
        46800,7.42876332; 50400,7.42876332; 50400,7.475557577; 54000,7.475557577;
        54000,7.520585945; 57600,7.520585945; 57600,7.513057428; 61200,7.513057428;
        61200,26.68408826; 64800,26.68408826; 64800,27.85802964; 68400,27.85802964;
        68400,28.17092771; 72000,28.17092771; 72000,28.78657683; 75600,28.78657683;
        75600,29.10487544; 79200,29.10487544; 79200,29.21730921; 82800,29.21730921;
        82800,29.18512017; 86400,29.18512017])
    "EnergyPlus result: outlet temperature"
  Modelica.Blocks.Sources.TimeTable Q_flowEPlu(table=[0,-1e-07; 3600,-1e-07; 3600,
        -1e-07; 7200,-1e-07; 7200,-1e-07; 10800,-1e-07; 10800,-1e-07; 14400,-1e-07;
        14400,-1e-07; 18000,-1e-07; 18000,-1e-07; 21600,-1e-07; 21600,-1e-07; 25200,
        -1e-07; 25200,-7101.486157; 28800,-7101.486157; 28800,-7065.001284; 32400,
        -7065.001284; 32400,-7062.147282; 36000,-7062.147282; 36000,-7059.359628;
        39600,-7059.359628; 39600,-7061.127967; 43200,-7061.127967; 43200,-7061.045112;
        46800,-7061.045112; 46800,-7058.99449; 50400,-7058.99449; 50400,-7058.349457;
        54000,-7058.349457; 54000,-7057.487529; 57600,-7057.487529; 57600,-7055.318157;
        61200,-7055.318157; 61200,-1e-07; 64800,-1e-07; 64800,-1e-07; 68400,-1e-07;
        68400,-1e-07; 72000,-1e-07; 72000,-1e-07; 75600,-1e-07; 75600,-1e-07; 79200,
        -1e-07; 79200,-1e-07; 82800,-1e-07; 82800,-1e-07; 86400,-1e-07])
        "EnergyPlus result: heat flow"
  Modelica.Blocks.Sources.TimeTable Q_flowSenEPlu(table=[0,0; 3600,0; 3600,0; 7200,
        0; 7200,0; 10800,0; 10800,0; 14400,0; 14400,0; 18000,0; 18000,0; 21600,0;
        21600,0; 25200,0; 25200,-7101.486157; 28800,-7101.486157; 28800,-7022.028225;
        32400,-7022.028225; 32400,-7029.813507; 36000,-7029.813507; 36000,-6918.432248;
        39600,-6918.432248; 39600,-6930.451185; 43200,-6930.451185; 43200,-6931.267635;
        46800,-6931.267635; 46800,-6962.40406; 50400,-6962.40406; 50400,-6982.045473;
        54000,-6982.045473; 54000,-6976.779585; 57600,-6976.779585; 57600,-6952.399564;
        61200,-6952.399564; 61200,0; 64800,0; 64800,0; 68400,0; 68400,0; 72000,0;
        72000,0; 75600,0; 75600,0; 79200,0; 79200,0; 82800,0; 82800,0; 86400,0])
    "EnergyPlus result: sensible heat flow "
  Modelica.Blocks.Math.Division shrEPlu "EnergyPlus result: SHR"
  Modelica.Blocks.Sources.TimeTable XEvaOutEPlu(table=[0,0.00531; 3600,0.00531;
        3600,0.0053; 7200,0.0053; 7200,0.00529; 10800,0.00529; 10800,0.00529; 14400,
        0.00529; 14400,0.00528; 18000,0.00528; 18000,0.00527; 21600,0.00527; 21600,
        0.00527; 25200,0.00527; 25200,0.00531; 28800,0.00531; 28800,0.00533; 32400,
        0.00533; 32400,0.00536; 36000,0.00536; 36000,0.00529; 39600,0.00529; 39600,
        0.00529; 43200,0.00529; 43200,0.00528; 46800,0.00528; 46800,0.00525; 50400,
        0.00525; 50400,0.00527; 54000,0.00527; 54000,0.00528; 57600,0.00528; 57600,
        0.00529; 61200,0.00529; 61200,0.00533; 64800,0.00533; 64800,0.00532; 68400,
        0.00532; 68400,0.00532; 72000,0.00532; 72000,0.00532; 75600,0.00532; 75600,
        0.00531; 79200,0.00531; 79200,0.00531; 82800,0.00531; 82800,0.0053; 86400,
        0.0053])
    "EnergyPlus result: outlet water mass fraction"
  Modelica.Blocks.Sources.TimeTable PEPlu(table=[0,0; 3600,0; 3600,0; 7200,0; 7200,
        0; 10800,0; 10800,0; 14400,0; 14400,0; 18000,0; 18000,0; 21600,0; 21600,
        0; 25200,0; 25200,1230.619384; 28800,1230.619384; 28800,1217.250881; 32400,
        1217.250881; 32400,1213.815124; 36000,1213.815124; 36000,1210.9557; 39600,
        1210.9557; 39600,1210.19962; 43200,1210.19962; 43200,1209.925786; 46800,
        1209.925786; 46800,1209.728055; 50400,1209.728055; 50400,1210.57757; 54000,
        1210.57757; 54000,1211.642811; 57600,1211.642811; 57600,1212.400701; 61200,
        1212.400701; 61200,0; 64800,0; 64800,0; 68400,0; 68400,0; 72000,0; 72000,
        0; 75600,0; 75600,0; 79200,0; 79200,0; 82800,0; 82800,0; 86400,0])
    "EnergyPlus result: electric power"
  Modelica.Blocks.Sources.RealExpression XEvaInMod(y=XEvaIn.y/(1 + XEvaIn.y))
    "Modified XEvaIn"
  Modelica.Blocks.Sources.RealExpression XEvaOutEPluMod(y=XEvaOutEPlu.y/(1 + XEvaOutEPlu.y))
    "Modified XEvaOut of energyPlus to comapre with the model results"
  Modelica.Blocks.Math.Add QCoo_flow "Total cooling heat flow rate"
  Sources.MassFlowSource_T souAir(
    redeclare package Medium = MediumAir,
    use_T_in=true,
    nPorts=1,
    m_flow=1.5,
    use_m_flow_in=true,
    use_X_in=true,
    T=299.85) "Source on air side"
  Sources.Boundary_pT sinAir(
    redeclare package Medium = MediumAir,
    nPorts=1,
    p(displayUnit="Pa"))
    "Sink on air side"
  Sources.Boundary_pT sinWat(
    redeclare package Medium = MediumWater,
    nPorts=1,
    p(displayUnit="Pa"))
    "Sink on water side"
  Sources.MassFlowSource_T souWat(
    redeclare package Medium = MediumWater,
    nPorts=1,
    use_m_flow_in=true,
    use_T_in=true,
    T=298.15) "Source on water side"
  Modelica.Blocks.Sources.TimeTable masEvaIn(table=[0,0; 3600,0; 3600,0; 7200,0;
        7200,0; 10800,0; 10800,0; 14400,0; 14400,0; 18000,0; 18000,0; 21600,0; 21600,
        0; 25200,0; 25200,0.42634453; 28800,0.42634453; 28800,0.42634453; 32400,
        0.42634453; 32400,0.42634453; 36000,0.42634453; 36000,0.42634453; 39600,
        0.42634453; 39600,0.42634453; 43200,0.42634453; 43200,0.42634453; 46800,
        0.42634453; 46800,0.42634453; 50400,0.42634453; 50400,0.42634453; 54000,
        0.42634453; 54000,0.42634453; 57600,0.42634453; 57600,0.42634453; 61200,
        0.42634453; 61200,0; 64800,0; 64800,0; 68400,0; 68400,0; 72000,0; 72000,
        0; 75600,0; 75600,0; 79200,0; 79200,0; 82800,0; 82800,0; 86400,0])
        "Mass flowrate at the evaporator"
  Modelica.Blocks.Sources.TimeTable masConIn(table=[0,0; 3600,0; 3600,0; 7200,0;
        7200,0; 10800,0; 10800,0; 14400,0; 14400,0; 18000,0; 18000,0; 21600,0; 21600,
        0; 25200,0; 25200,0.380079667; 28800,0.380079667; 28800,0.380079667; 32400,
        0.380079667; 32400,0.380079667; 36000,0.380079667; 36000,0.380079667; 39600,
        0.380079667; 39600,0.380079667; 43200,0.380079667; 43200,0.380079667; 46800,
        0.380079667; 46800,0.380079667; 50400,0.380079667; 50400,0.380079667; 54000,
        0.380079667; 54000,0.380079667; 57600,0.380079667; 57600,0.380079667; 61200,
        0.380079667; 61200,0; 64800,0; 64800,0; 68400,0; 68400,0; 72000,0; 72000,
        0; 75600,0; 75600,0; 79200,0; 79200,0; 82800,0; 82800,0; 86400,0])
       "Mass flowrate at the condenser"
  Sensors.TemperatureTwoPort TConOut(m_flow_nominal=0.38, redeclare package
      Medium = MediumWater) "Outlet temperature at the condenser"
  Modelica.Blocks.Sources.TimeTable TCOutEPlu(table=[0,14.00192912; 3600,14.00192912;
        3600,14.00192912; 7200,14.00192912; 7200,14.00192912; 10800,14.00192912;
        10800,14.00192912; 14400,14.00192912; 14400,14.00192912; 18000,14.00192912;
        18000,14.00192912; 21600,14.00192912; 21600,14.00192912; 25200,14.00192912;
        25200,19.03036339; 28800,19.03036339; 28800,18.81784119; 32400,18.81784119;
        32400,18.73639861; 36000,18.73639861; 36000,18.68945378; 39600,18.68945378;
        39600,18.65932416; 43200,18.65932416; 43200,18.65380814; 46800,18.65380814;
        46800,18.66492477; 50400,18.66492477; 50400,18.6885344; 54000,18.6885344;
        54000,18.71879002; 57600,18.71879002; 57600,18.75190471; 61200,18.75190471;
        61200,13.66852976; 64800,13.66852976; 64800,13.66852976; 68400,13.66852976;
        68400,13.66852976; 72000,13.66852976; 72000,13.66852976; 75600,13.66852976;
        75600,13.66852976; 79200,13.66852976; 79200,13.66852976; 82800,13.66852976;
        82800,13.66852976; 86400,13.66852976])
                            "Condenser outlet temperature"
  Modelica.Blocks.Sources.TimeTable Q_flowConEPlu(table=[0,0; 3600,0; 3600,0; 7200,
        0; 7200,0; 10800,0; 10800,0; 14400,0; 14400,0; 18000,0; 18000,0; 21600,0;
        21600,0; 25200,0; 25200,8209.043603; 28800,8209.043603; 28800,8160.527077;
        32400,8160.527077; 32400,8154.580894; 36000,8154.580894; 36000,8149.219757;
        39600,8149.219757; 39600,8150.307625; 43200,8150.307625; 43200,8149.978319;
        46800,8149.978319; 46800,8147.749739; 50400,8147.749739; 50400,8147.86927;
        54000,8147.86927; 54000,8147.96606; 57600,8147.96606; 57600,8146.478788;
        61200,8146.478788; 61200,0; 64800,0; 64800,0; 68400,0; 68400,0; 72000,0;
        72000,0; 75600,0; 75600,0; 79200,0; 79200,0; 82800,0; 82800,0; 86400,0])
    "Condenser heat flow"
equation
  connect(TEvaIn.y, TEvaIn_K.Celsius)
  connect(TOut.y, TOutMea.u)
  connect(TOutMea.y, TOutDegC.Kelvin)
  connect(XEvaOut.y, XEvaOutMea.u)
  connect(XEvaInMoiAir.y, add.u2)
  connect(add.y, mux.u2[1])
  connect(Q_flowSenEPlu.y, shrEPlu.u1)
  connect(Q_flowEPlu.y, shrEPlu.u2)
  connect(varSpeDX.P, PMea.u)
  connect(XEvaInMod.y, mux.u1[1])
  connect(XEvaInMod.y, add.u1)
  connect(QCoo_flow.y, Q_flowMea.u)
  connect(TEvaIn_K.Kelvin, souAir.T_in)
  connect(mux.y, souAir.X_in)
  connect(souAir.ports[1], varSpeDX.port_a)
  connect(varSpeDX.port_b, sinAir.ports[1])
  connect(souWat.ports[1], varSpeDX.portCon_a)
  connect(TCIn.y, TCIn_K.Celsius)
  connect(TCIn_K.Kelvin, souWat.T_in)
  connect(speRat.y, varSpeDX.speRat)
  connect(varSpeDX.QEvaSen_flow, QCoo_flow.u1)
  connect(varSpeDX.QEvaLat_flow, QCoo_flow.u2)
  connect(varSpeDX.QEvaSen_flow, Q_flowSenMea.u)
  connect(masConIn.y, souWat.m_flow_in)
  connect(masEvaIn.y, souAir.m_flow_in)
  connect(sinWat.ports[1], TConOut.port_b)
  connect(TConOut.port_a, varSpeDX.portCon_b)
end VariableSpeedEnergyPlus;


within Buildings.Fluid.DXSystems.Cooling.WaterSource.Validation;
model VariableSpeedEnergyPlusPartLoad
  "Validation model for variable speed DX coil under part load condition"
  extends Modelica.Icons.Example;

  package MediumAir = Buildings.Media.Air "Medium model for air";
  package MediumWater = Buildings.Media.Water "Medium model for water";

  parameter Modelica.Units.SI.Power Q_flow_nominal=datCoi.sta[1].nomVal.Q_flow_nominal
    "Nominal power";
  parameter Modelica.Units.SI.MassFlowRate m_flow_nominal=datCoi.sta[1].nomVal.m_flow_nominal
    "Nominal mass flow rate";
  parameter Modelica.Units.SI.PressureDifference dpEva_nominal=1000
    "Pressure drop at m_flow_nominal";
  parameter Modelica.Units.SI.PressureDifference dpCon_nominal=40000
    "Pressure drop at mCon_flow_nominal";

  WaterSource.VariableSpeed varSpeDX(
    redeclare package MediumEva = MediumAir,
    redeclare package MediumCon = MediumWater,
    datCoi=datCoi,
    dpEva_nominal=dpEva_nominal,
    dpCon_nominal=dpCon_nominal,
    minSpeRat=0.05,
    TEva_start=datCoi.sta[1].nomVal.TEvaIn_nominal,
    energyDynamics=Modelica.Fluid.Types.Dynamics.FixedInitial)
    "Variable speed DX coil"

  parameter
    Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.DXCoil
    datCoi(
    nSta=10,
    minSpeRat=0.1,
    sta={
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=500/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-1524.1,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.16648632,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=1000/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-1877.9,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.1849848,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=1500/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-2226.6,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.20348328,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=2000/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-2911.3,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.24048024,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=2500/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-3581.7,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.2774772,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=3000/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-4239.5,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.31447416,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=3500/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-4885.7,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.35147112,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=4000/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-5520.7,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.38846808,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=4500/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-6144.8,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.42546504,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I()),
        Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.Stage(
        spe=5000/60,
        nomVal=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=-6758,
          COP_nominal=4,
          SHR_nominal=0.75,
          m_flow_nominal=0.462462,
          mCon_flow_nominal=0.380079667,
          TEvaIn_nominal=273.15 + 19.44,
          TConIn_nominal=273.15 + 29.4,
          phiIn_nominal=0.5148),
        perCur=
          Buildings.Fluid.DXSystems.Cooling.WaterSource.Examples.PerformanceCurves.Curve_I())})
    "Coil data"
 Modelica.Blocks.Sources.TimeTable speRat(table=[0,0; 3600,0; 3600,0; 7200,0;
        7200,0; 10800,0; 10800,0; 14400,0; 14400,0; 18000,0; 18000,0; 21600,0;
        21600,0; 25200,0; 25200,0.835144907; 28800,0.835144907; 28800,
        0.725963941; 32400,0.725963941; 32400,0.701309629; 36000,0.701309629;
        36000,0.726095313; 39600,0.726095313; 39600,0.756509478; 43200,
        0.756509478; 43200,0.760310507; 46800,0.760310507; 46800,0.789773752;
        50400,0.789773752; 50400,0.785538594; 54000,0.785538594; 54000,
        0.762755548; 57600,0.762755548; 57600,0.705369369; 61200,0.705369369;
        61200,0; 64800,0; 64800,0; 68400,0; 68400,0; 72000,0; 72000,0; 75600,0;
        75600,0; 79200,0; 79200,0; 82800,0; 82800,0; 86400,0])
    "EnergyPlus speed ratio for this model"
  Modelica.Blocks.Sources.TimeTable TCIn(table=[0,13.86251622; 3600,13.86251622;
        3600,13.86251622; 7200,13.86251622; 7200,13.86251622; 10800,13.86251622;
        10800,13.86251622; 14400,13.86251622; 14400,13.86251622; 18000,13.86251622;
        18000,13.86251622; 21600,13.86251622; 21600,13.86251622; 25200,13.86251622;
        25200,13.76067467; 28800,13.76067467; 28800,13.62587063; 32400,13.62587063;
        32400,13.56765455; 36000,13.56765455; 36000,13.51654003; 39600,13.51654003;
        39600,13.49422548; 43200,13.49422548; 43200,13.49310136; 46800,13.49310136;
        46800,13.50453648; 50400,13.50453648; 50400,13.52335024; 54000,13.52335024;
        54000,13.54653059; 57600,13.54653059; 57600,13.57254285; 61200,13.57254285;
        61200,13.60087284; 64800,13.60087284; 64800,13.60087284; 68400,13.60087284;
        68400,13.60087284; 72000,13.60087284; 72000,13.60087284; 75600,13.60087284;
        75600,13.60087284; 79200,13.60087284; 79200,13.60087284; 82800,13.60087284;
        82800,13.60087284; 86400,13.60087284])
    "Condenser inlet temperature"
  Modelica.Blocks.Sources.TimeTable TEvaIn(table=[0,27.30566381; 3600,27.30566381;
        3600,27.21827779; 7200,27.21827779; 7200,27.11647599; 10800,27.11647599;
        10800,27.0058024; 14400,27.0058024; 14400,26.88972232; 18000,26.88972232;
        18000,26.77810041; 21600,26.77810041; 21600,26.79816007; 25200,26.79816007;
        25200,24.38258647; 28800,24.38258647; 28800,23.60631205; 32400,23.60631205;
        32400,23.35999766; 36000,23.35999766; 36000,23.35884656; 39600,23.35884656;
        39600,23.35829243; 43200,23.35829243; 43200,23.35804345; 46800,23.35804345;
        46800,23.35801611; 50400,23.35801611; 50400,23.35827236; 54000,23.35827236;
        54000,23.3583018; 57600,23.3583018; 57600,23.35814534; 61200,23.35814534;
        61200,25.62821073; 64800,25.62821073; 64800,26.495716; 68400,26.495716;
        68400,26.7309828; 72000,26.7309828; 72000,27.15903639; 75600,27.15903639;
        75600,27.26532973; 79200,27.26532973; 79200,27.38959551; 82800,27.38959551;
        82800,27.37275176; 86400,27.37275176])
     "Coil inlet temperature"
  Modelica.Blocks.Sources.TimeTable XEvaIn(table=[0,0.00538; 3600,0.00538; 3600,
        0.00538; 7200,0.00538; 7200,0.00537; 10800,0.00537; 10800,0.00536;
        14400,0.00536; 14400,0.00535; 18000,0.00535; 18000,0.00534; 21600,
        0.00534; 21600,0.00534; 25200,0.00534; 25200,0.00538; 28800,0.00538;
        28800,0.00544; 32400,0.00544; 32400,0.00546; 36000,0.00546; 36000,
        0.00551; 39600,0.00551; 39600,0.00551; 43200,0.00551; 43200,0.00551;
        46800,0.00551; 46800,0.00546; 50400,0.00546; 50400,0.00546; 54000,
        0.00546; 54000,0.00548; 57600,0.00548; 57600,0.00551; 61200,0.00551;
        61200,0.00541; 64800,0.00541; 64800,0.0054; 68400,0.0054; 68400,0.0054;
        72000,0.0054; 72000,0.0054; 75600,0.0054; 75600,0.00539; 79200,0.00539;
        79200,0.00539; 82800,0.00539; 82800,0.00538; 86400,0.00538])
    "Water fraction of moist air"
  Modelica.Blocks.Routing.Multiplex2 mux "Converts in an array"
  Buildings.Utilities.IO.BCVTB.From_degC TCIn_K "Converts degC to K"
  Buildings.Utilities.IO.BCVTB.From_degC TEvaIn_K "Converts degC to K"
  Modelica.Blocks.Math.Mean TOutMea(f=1/3600)
  Buildings.Utilities.IO.BCVTB.To_degC TOutDegC
  Modelica.Blocks.Sources.RealExpression TOut(y=varSpeDX.eva.vol.T)
  Modelica.Blocks.Math.Mean XEvaOutMea(f=1/3600)
  Modelica.Blocks.Sources.RealExpression XEvaOut(y=sum(varSpeDX.eva.vol.Xi))
  Modelica.Blocks.Math.Mean Q_flowMea(f=1/3600) "Mean of cooling rate"
  Modelica.Blocks.Math.Mean Q_flowSenMea(f=1/3600)
    "Mean of sensible cooling rate"
  Modelica.Blocks.Math.Mean PMea(f=1/3600) "Mean of power"
  Modelica.Blocks.Math.Add add(k1=-1)
  Modelica.Blocks.Sources.Constant XEvaInMoiAir(k=1.0) "Moist air fraction = 1"
  Modelica.Blocks.Sources.TimeTable TOutEPlu(table=[0,27.30566381; 3600,27.30566381;
        3600,27.21827779; 7200,27.21827779; 7200,27.11647599; 10800,27.11647599;
        10800,27.0058024; 14400,27.0058024; 14400,26.88972232; 18000,26.88972232;
        18000,26.77810041; 21600,26.77810041; 21600,26.79816007; 25200,26.79816007;
        25200,8.558164875; 28800,8.558164875; 28800,8.425553165; 32400,8.425553165;
        32400,8.090261936; 36000,8.090261936; 36000,8.014264329; 39600,8.014264329;
        39600,7.933216144; 43200,7.933216144; 43200,7.930680761; 46800,7.930680761;
        46800,7.787349788; 50400,7.787349788; 50400,7.794251378; 54000,7.794251378;
        54000,7.88577408; 57600,7.88577408; 57600,8.108580275; 61200,8.108580275;
        61200,25.62821073; 64800,25.62821073; 64800,26.495716; 68400,26.495716;
        68400,26.7309828; 72000,26.7309828; 72000,27.15903639; 75600,27.15903639;
        75600,27.26532973; 79200,27.26532973; 79200,27.38959551; 82800,27.38959551;
        82800,27.37275176; 86400,27.37275176])
    "EnergyPlus result: outlet temperature"
  Modelica.Blocks.Sources.TimeTable Q_flowEPlu(table=[0,-1e-07; 3600,-1e-07; 3600,
        -1e-07; 7200,-1e-07; 7200,-1e-07; 10800,-1e-07; 10800,-1e-07; 14400,-1e-07;
        14400,-1e-07; 18000,-1e-07; 18000,-1e-07; 21600,-1e-07; 21600,-1e-07; 25200,
        -1e-07; 25200,-5993.93491; 28800,-5993.93491; 28800,-5241.352381; 32400,
        -5241.352381; 32400,-5109.221236; 36000,-5109.221236; 36000,-5276.319149;
        39600,-5276.319149; 39600,-5479.659005; 43200,-5479.659005; 43200,-5505.142371;
        46800,-5505.142371; 46800,-5698.989696; 50400,-5698.989696; 50400,-5669.788807;
        54000,-5669.788807; 54000,-5517.891066; 57600,-5517.891066; 57600,-5136.545718;
        61200,-5136.545718; 61200,-1e-07; 64800,-1e-07; 64800,-1e-07; 68400,-1e-07;
        68400,-1e-07; 72000,-1e-07; 72000,-1e-07; 75600,-1e-07; 75600,-1e-07; 79200,
        -1e-07; 79200,-1e-07; 82800,-1e-07; 82800,-1e-07; 86400,-1e-07])
        "EnergyPlus result: heat flow"
  Modelica.Blocks.Sources.TimeTable Q_flowSenEPlu(table=[0,0; 3600,0; 3600,0; 7200,
        0; 7200,0; 10800,0; 10800,0; 14400,0; 14400,0; 18000,0; 18000,0; 21600,0;
        21600,0; 25200,0; 25200,-5964.489051; 28800,-5964.489051; 28800,-5206.664409;
        32400,-5206.664409; 32400,-5027.556976; 36000,-5027.556976; 36000,-5184.015367;
        39600,-5184.015367; 39600,-5373.829789; 43200,-5373.829789; 43200,-5395.020923;
        46800,-5395.020923; 46800,-5603.430852; 50400,-5603.430852; 50400,-5578.150113;
        54000,-5578.150113; 54000,-5423.518277; 57600,-5423.518277; 57600,-5042.653124;
        61200,-5042.653124; 61200,0; 64800,0; 64800,0; 68400,0; 68400,0; 72000,0;
        72000,0; 75600,0; 75600,0; 79200,0; 79200,0; 82800,0; 82800,0; 86400,0])
    "EnergyPlus result: sensible heat flow "
  Modelica.Blocks.Math.Division shrEPlu "EnergyPlus result: SHR"
  Modelica.Blocks.Sources.TimeTable XEvaOutEPlu(table=[0,0.00525; 3600,0.00525;
        3600,0.00524; 7200,0.00524; 7200,0.00523; 10800,0.00523; 10800,0.00523;
        14400,0.00523; 14400,0.00522; 18000,0.00522; 18000,0.00521; 21600,0.00521;
        21600,0.00521; 25200,0.00521; 25200,0.00521; 28800,0.00521; 28800,0.00522;
        32400,0.00522; 32400,0.00522; 36000,0.00522; 36000,0.00523; 39600,0.00523;
        39600,0.00524; 43200,0.00524; 43200,0.00525; 46800,0.00525; 46800,0.00522;
        50400,0.00522; 50400,0.00522; 54000,0.00522; 54000,0.00523; 57600,0.00523;
        57600,0.00524; 61200,0.00524; 61200,0.00527; 64800,0.00527; 64800,0.00526;
        68400,0.00526; 68400,0.00526; 72000,0.00526; 72000,0.00526; 75600,0.00526;
        75600,0.00526; 79200,0.00526; 79200,0.00525; 82800,0.00525; 82800,0.00525;
        86400,0.00525])
    "EnergyPlus result: outlet water mass fraction"
  Modelica.Blocks.Sources.TimeTable PEPlu(table=[0,0; 3600,0; 3600,0; 7200,0; 7200,
        0; 10800,0; 10800,0; 14400,0; 14400,0; 18000,0; 18000,0; 21600,0; 21600,
        0; 25200,0; 25200,1034.300037; 28800,1034.300037; 28800,900.2508689; 32400,
        900.2508689; 32400,875.7430481; 36000,875.7430481; 36000,903.2013238; 39600,
        903.2013238; 39600,937.554675; 43200,937.554675; 43200,941.9339651; 46800,
        941.9339651; 46800,975.1567966; 50400,975.1567966; 50400,970.6256299; 54000,
        970.6256299; 54000,945.3102202; 57600,945.3102202; 57600,880.6992676; 61200,
        880.6992676; 61200,0; 64800,0; 64800,0; 68400,0; 68400,0; 72000,0; 72000,
        0; 75600,0; 75600,0; 79200,0; 79200,0; 82800,0; 82800,0; 86400,0])
    "EnergyPlus result: electric power"
  Modelica.Blocks.Sources.RealExpression XEvaInMod(y=XEvaIn.y/(1 + XEvaIn.y))
    "Modified XEvaIn"
  Modelica.Blocks.Sources.RealExpression XEvaOutEPluMod(y=XEvaOutEPlu.y/(1 + XEvaOutEPlu.y))
    "Modified XEvaOut of energyPlus to comapre with the model results"
  Modelica.Blocks.Math.Add QCoo_flow "Total cooling heat flow rate"
  Sources.MassFlowSource_T souAir(
    redeclare package Medium = MediumAir,
    use_T_in=true,
    nPorts=1,
    m_flow=1.5,
    use_m_flow_in=true,
    use_X_in=true,
    T=299.85) "Source on air side"
  Sources.Boundary_pT sinAir(
    redeclare package Medium = MediumAir,
    nPorts=1,
    p(displayUnit="Pa"))
    "Sink on air side"
  Sources.Boundary_pT sinWat(
    redeclare package Medium = MediumWater,
    nPorts=1,
    p(displayUnit="Pa"))
    "Sink on water side"
  Sources.MassFlowSource_T souWat(
    redeclare package Medium = MediumWater,
    nPorts=1,
    use_m_flow_in=true,
    use_T_in=true,
    T=298.15) "Source on water side"
  Modelica.Blocks.Sources.TimeTable masEvaIn(table=[0,0; 3600,0; 3600,0; 7200,0;
        7200,0; 10800,0; 10800,0; 14400,0; 14400,0; 18000,0; 18000,0; 21600,0; 21600,
        0; 25200,0; 25200,0.370116476; 28800,0.370116476; 28800,0.33287751; 32400,
        0.33287751; 32400,0.324468525; 36000,0.324468525; 36000,0.332922318; 39600,
        0.332922318; 39600,0.343295848; 43200,0.343295848; 43200,0.344592286; 46800,
        0.344592286; 46800,0.354641481; 50400,0.354641481; 50400,0.353196972; 54000,
        0.353196972; 54000,0.34542623; 57600,0.34542623; 57600,0.325853203; 61200,
        0.325853203; 61200,0; 64800,0; 64800,0; 68400,0; 68400,0; 72000,0; 72000,
        0; 75600,0; 75600,0; 79200,0; 79200,0; 82800,0; 82800,0; 86400,0])
        "Mass flowrate at the evaporator"
  Modelica.Blocks.Sources.TimeTable masConIn(table=[0,0; 3600,0; 3600,0; 7200,0;
        7200,0; 10800,0; 10800,0; 14400,0; 14400,0; 18000,0; 18000,0; 21600,0; 21600,
        0; 25200,0; 25200,0.380079667; 28800,0.380079667; 28800,0.380079667; 32400,
        0.380079667; 32400,0.380079667; 36000,0.380079667; 36000,0.380079667; 39600,
        0.380079667; 39600,0.380079667; 43200,0.380079667; 43200,0.380079667; 46800,
        0.380079667; 46800,0.380079667; 50400,0.380079667; 50400,0.380079667; 54000,
        0.380079667; 54000,0.380079667; 57600,0.380079667; 57600,0.380079667; 61200,
        0.380079667; 61200,0; 64800,0; 64800,0; 68400,0; 68400,0; 72000,0; 72000,
        0; 75600,0; 75600,0; 79200,0; 79200,0; 82800,0; 82800,0; 86400,0])
       "Mass flowrate at the condenser"
  Sensors.TemperatureTwoPort TConOut(m_flow_nominal=0.38, redeclare package
      Medium = MediumWater) "Outlet temperature at the condenser"
  Modelica.Blocks.Sources.TimeTable TCOutEPlu(table=[0,13.86251622; 3600,
        13.86251622; 3600,13.86251622; 7200,13.86251622; 7200,13.86251622;
        10800,13.86251622; 10800,13.86251622; 14400,13.86251622; 14400,
        13.86251622; 18000,13.86251622; 18000,13.86251622; 21600,13.86251622;
        21600,13.86251622; 25200,13.86251622; 25200,18.11262715; 28800,
        18.11262715; 28800,17.42888551; 32400,17.42888551; 32400,17.27370577;
        36000,17.27370577; 36000,17.34307309; 39600,17.34307309; 39600,
        17.46794383; 43200,17.46794383; 43200,17.48530919; 46800,17.48530919;
        46800,17.63736299; 50400,17.63736299; 50400,17.63528608; 54000,
        17.63528608; 54000,17.5487206; 57600,17.5487206; 57600,17.29857374;
        61200,17.29857374; 61200,13.60087284; 64800,13.60087284; 64800,
        13.60087284; 68400,13.60087284; 68400,13.60087284; 72000,13.60087284;
        72000,13.60087284; 75600,13.60087284; 75600,13.60087284; 79200,
        13.60087284; 79200,13.60087284; 82800,13.60087284; 82800,13.60087284;
        86400,13.60087284]) "Condenser outlet temperature"
  Modelica.Blocks.Sources.TimeTable Q_flowConEPlu(table=[0,0; 3600,0; 3600,0;
        7200,0; 7200,0; 10800,0; 10800,0; 14400,0; 14400,0; 18000,0; 18000,0;
        21600,0; 21600,0; 25200,0; 25200,6924.804943; 28800,6924.804943; 28800,
        6051.578162; 32400,6051.578162; 32400,5897.389979; 36000,5897.389979;
        36000,6089.200341; 39600,6089.200341; 39600,6323.458213; 43200,
        6323.458213; 43200,6352.882939; 46800,6352.882939; 46800,6576.630813;
        50400,6576.630813; 50400,6543.351874; 54000,6543.351874; 54000,
        6368.670264; 57600,6368.670264; 57600,5929.175058; 61200,5929.175058;
        61200,0; 64800,0; 64800,0; 68400,0; 68400,0; 72000,0; 72000,0; 75600,0;
        75600,0; 79200,0; 79200,0; 82800,0; 82800,0; 86400,0])
    "Condenser heat flow"
equation
  connect(TEvaIn.y, TEvaIn_K.Celsius)
  connect(TOut.y, TOutMea.u)
  connect(TOutMea.y, TOutDegC.Kelvin)
  connect(XEvaOut.y, XEvaOutMea.u)
  connect(XEvaInMoiAir.y, add.u2)
  connect(add.y, mux.u2[1])
  connect(Q_flowSenEPlu.y, shrEPlu.u1)
  connect(Q_flowEPlu.y, shrEPlu.u2)
  connect(varSpeDX.P, PMea.u)
  connect(XEvaInMod.y, mux.u1[1])
  connect(XEvaInMod.y, add.u1)
  connect(QCoo_flow.y, Q_flowMea.u)
  connect(TEvaIn_K.Kelvin, souAir.T_in)
  connect(mux.y, souAir.X_in)
  connect(souAir.ports[1], varSpeDX.port_a)
  connect(varSpeDX.port_b, sinAir.ports[1])
  connect(souWat.ports[1], varSpeDX.portCon_a)
  connect(TCIn.y, TCIn_K.Celsius)
  connect(TCIn_K.Kelvin, souWat.T_in)
  connect(speRat.y, varSpeDX.speRat)
  connect(varSpeDX.QEvaSen_flow, QCoo_flow.u1)
  connect(varSpeDX.QEvaLat_flow, QCoo_flow.u2)
  connect(varSpeDX.QEvaSen_flow, Q_flowSenMea.u)
  connect(masConIn.y, souWat.m_flow_in)
  connect(masEvaIn.y, souAir.m_flow_in)
  connect(sinWat.ports[1], TConOut.port_b)
  connect(TConOut.port_a, varSpeDX.portCon_b)
end VariableSpeedEnergyPlusPartLoad;


Package with DX heating coil models

within Buildings.Fluid.DXSystems;
package Heating "Package with DX heating coil models"

end Heating;


User's Guide

within Buildings.Fluid.DXSystems.Heating;
package UsersGuide "User's Guide"
  extends Modelica.Icons.Information;
end UsersGuide;


Air source DX coils

within Buildings.Fluid.DXSystems.Heating;
package AirSource "Air source DX coils"
end AirSource;


Single speed DX heating coil. This model can be used to simulate an air-source DX heating coil with single speed compressor.

within Buildings.Fluid.DXSystems.Heating.AirSource;
model SingleSpeed
  "Single speed DX heating coil"
  extends Buildings.Fluid.DXSystems.Heating.BaseClasses.PartialDXHeatingCoil;

  Modelica.Blocks.Interfaces.BooleanInput on
    "Set to true to enable compressor, or false to disable compressor"

protected
  Modelica.Blocks.Sources.Constant speRat(
    final k=1)
    "Speed ratio 1 constant source"

  Buildings.Controls.OBC.CDL.Conversions.BooleanToReal booToReaOn
    "Convert Boolean enable signal to Real value 1, disable to Real value 0"

  Modelica.Blocks.Math.BooleanToInteger onSwi(
    final integerTrue=1,
    final integerFalse=0)
    "On/off switch"

initial equation
  assert(datCoi.nSta == 1, "Must have one stage only for single speed performance data.");

equation
  connect(speRat.y,dxCoi.speRat)
  connect(on, onSwi.u)
  connect(onSwi.y,dxCoi.stage)
  connect(on, booToReaOn.u)
  connect(booToReaOn.y, defCap.uSpe)
end SingleSpeed;


within Buildings.Fluid.DXSystems.Heating.AirSource;
package Data "Package with performance data for DX heating coils"
  extends Modelica.Icons.MaterialPropertiesPackage;
end Data;


Package with generic performance curves

within Buildings.Fluid.DXSystems.Heating.AirSource.Data;
package Generic "Package with generic performance curves"
  extends Modelica.Icons.MaterialPropertiesPackage;
end Generic;


within Buildings.Fluid.DXSystems.Heating.AirSource.Data.Generic;
package BaseClasses "Package with base classes for performance data"
  extends Modelica.Icons.BasesPackage;
end BaseClasses;


Generic data record for a stage of a air source DX coil. This is the base record for air source DX coil model at a particular compressor
  speed.
See the information section of

Buildings.Fluid.DXSystems.Heating.AirSource.Data.Generic.DXCoil for a description of the data.

within Buildings.Fluid.DXSystems.Heating.AirSource.Data.Generic.BaseClasses;
record Stage "Generic data record for a stage of a air source DX coil"
  extends Modelica.Icons.Record;

  parameter Modelica.Units.SI.AngularVelocity spe(displayUnit="1/min")
    "Rotational speed";

  replaceable parameter Buildings.Fluid.DXSystems.Heating.AirSource.Data.Generic.BaseClasses.NominalValues
    nomVal "Nominal values"

  replaceable parameter
    Buildings.Fluid.DXSystems.Heating.AirSource.Data.Generic.BaseClasses.PerformanceCurve
    perCur "Performance curves for this stage"
end Stage;


within Buildings.Fluid.DXSystems.Heating.AirSource;
package Examples "Collection of models that illustrate model use and test models"
  extends Modelica.Icons.ExamplesPackage;
end Examples;


within Buildings.Fluid.DXSystems.Heating.AirSource.Examples;
model SingleSpeed "Test model for single speed DX heating coil"
  extends Modelica.Icons.Example;

  package Medium = Buildings.Media.Air
    "Fluid medium for the model";

  parameter Modelica.Units.SI.MassFlowRate m_flow_nominal=datCoi.sta[datCoi.nSta].nomVal.m_flow_nominal
    "Nominal mass flow rate";

  parameter Modelica.Units.SI.PressureDifference dp_nominal=1000
    "Pressure drop at m_flow_nominal";

  parameter
    Buildings.Fluid.DXSystems.Heating.AirSource.Data.Generic.DXCoil
    datCoi(
    nSta=1,
    minSpeRat=0.2,
    sta={
        Buildings.Fluid.DXSystems.Heating.AirSource.Data.Generic.BaseClasses.Stage(
        spe=1800/60,
        nomVal=
          Buildings.Fluid.DXSystems.Heating.AirSource.Data.Generic.BaseClasses.NominalValues(
          Q_flow_nominal=16381.47714,
          COP_nominal=3.90494,
          m_flow_nominal=2,
          TEvaIn_nominal=273.15 - 5,
          TConIn_nominal=273.15 + 21),
        perCur=
          Buildings.Fluid.DXSystems.Heating.AirSource.Examples.PerformanceCurves.Curve_I())},
    final defOpe=Buildings.Fluid.DXSystems.Heating.BaseClasses.Types.DefrostOperation.resistive,
    final defTri=Buildings.Fluid.DXSystems.Heating.BaseClasses.Types.DefrostTimeMethods.timed,
    final tDefRun=1/6,
    final TDefLim=273.65,
    final QDefResCap=10500,
    final QCraCap=200)
    "DX heating coil data record"

  Buildings.Fluid.DXSystems.Heating.AirSource.SingleSpeed sinSpeDX(
    final datCoi=datCoi,
    redeclare package Medium = Medium,
    final dp_nominal=dp_nominal,
    final T_start=datCoi.sta[1].nomVal.TConIn_nominal,
    final show_T=true,
    final from_dp=true,
    final energyDynamics=Modelica.Fluid.Types.Dynamics.FixedInitial,
    final dTHys=1e-6)
    "Single speed DX coil"

  Buildings.Fluid.Sources.Boundary_pT sin(
    redeclare package Medium = Medium,
    final p(displayUnit="Pa") = 101325,
    final T=303.15,
    final nPorts=1)
    "Sink"

  Buildings.Fluid.Sources.Boundary_pT sou(
    redeclare package Medium = Medium,
    final use_T_in=true,
    final use_p_in=true,
    final nPorts=1)
    "Source"

  Modelica.Blocks.Sources.BooleanStep onOff(
    final startTime=600)
    "Compressor on-off signal"

  Modelica.Blocks.Sources.Ramp TConIn(
    final duration=600,
    final startTime=2400,
    final height=-3,
    final offset=273.15 + 23)
    "Temperature"

  Modelica.Blocks.Sources.Ramp p(
    final duration=600,
    final startTime=600,
    final height=dp_nominal,
    final offset=101325)
    "Pressure"

  Modelica.Blocks.Sources.Constant TEvaIn(
    final k=273.15 + 0)
    "Evaporator inlet temperature"

  Modelica.Blocks.Sources.Constant phi(final k=0.1)
    "Outside air relative humidity"

equation
  connect(TConIn.y, sou.T_in)
  connect(onOff.y, sinSpeDX.on)
  connect(p.y, sou.p_in)
  connect(sinSpeDX.port_a, sou.ports[1])
  connect(TEvaIn.y, sinSpeDX.TOut)
  connect(sinSpeDX.port_b, sin.ports[1])
  connect(sinSpeDX.phi, phi.y)
end SingleSpeed;


Package with several performance curves

within Buildings.Fluid.DXSystems.Heating.AirSource.Examples;
package PerformanceCurves "Package with several performance curves"
  extends Modelica.Icons.MaterialPropertiesPackage;
end PerformanceCurves;


Collection of validation models

within Buildings.Fluid.DXSystems.Heating.AirSource;
package Validation "Collection of validation models"
  extends Modelica.Icons.ExamplesPackage;
end Validation;


within Buildings.Fluid.DXSystems.Heating.AirSource.Validation;
model SingleSpeed_OnDemandResistiveDefrost
  "Validation model for single speed heating DX coil with defrost operation"
  extends Modelica.Icons.Example;
  extends
    Buildings.Fluid.DXSystems.Heating.AirSource.Validation.BaseClasses.SingleSpeedHeating(
    datRea(
      final fileName=ModelicaServices.ExternalReferences.loadResource("modelica://Buildings/Resources/Data/Fluid/DXSystems/Heating/AirSource/Validation/SingleSpeedHeating_OnDemandResistiveDefrost/DXCoilSystemAuto.dat")),
    datCoi(
      final defOpe=Buildings.Fluid.DXSystems.Heating.BaseClasses.Types.DefrostOperation.resistive,
      final defTri=Buildings.Fluid.DXSystems.Heating.BaseClasses.Types.DefrostTimeMethods.onDemand));
end SingleSpeed_OnDemandResistiveDefrost;


within Buildings.Fluid.DXSystems.Heating.AirSource.Validation;
model SingleSpeed_OnDemandReverseCycleDefrost
  "Validation model for single speed heating DX coil with defrost operation"
  extends Modelica.Icons.Example;
  extends
    Buildings.Fluid.DXSystems.Heating.AirSource.Validation.BaseClasses.SingleSpeedHeating(
    datRea(
      final fileName=ModelicaServices.ExternalReferences.loadResource("modelica://Buildings/Resources/Data/Fluid/DXSystems/Heating/AirSource/Validation/SingleSpeedHeating_OnDemandReverseCycleDefrost/DXCoilSystemAuto.dat")),
    datCoi(
      final defOpe=Buildings.Fluid.DXSystems.Heating.BaseClasses.Types.DefrostOperation.reverseCycle,
      final defTri=Buildings.Fluid.DXSystems.Heating.BaseClasses.Types.DefrostTimeMethods.onDemand));
end SingleSpeed_OnDemandReverseCycleDefrost;


within Buildings.Fluid.DXSystems.Heating.AirSource.Validation;
model SingleSpeed_TimedResistiveDefrost
  "Validation model for single speed heating DX coil with defrost operation"
  extends Modelica.Icons.Example;
  extends
    Buildings.Fluid.DXSystems.Heating.AirSource.Validation.BaseClasses.SingleSpeedHeating(
    datRea(
      final fileName=ModelicaServices.ExternalReferences.loadResource("modelica://Buildings/Resources/Data/Fluid/DXSystems/Heating/AirSource/Validation/SingleSpeedHeating_TimedResistiveDefrost/DXCoilSystemAuto.dat")),
    datCoi(
      final defOpe=Buildings.Fluid.DXSystems.Heating.BaseClasses.Types.DefrostOperation.resistive,
      final defTri=Buildings.Fluid.DXSystems.Heating.BaseClasses.Types.DefrostTimeMethods.timed));
end SingleSpeed_TimedResistiveDefrost;


within Buildings.Fluid.DXSystems.Heating.AirSource.Validation;
model SingleSpeed_TimedReverseCycleDefrost
  "Validation model for single speed heating DX coil with defrost operation"

  extends Modelica.Icons.Example;
  extends
    Buildings.Fluid.DXSystems.Heating.AirSource.Validation.BaseClasses.SingleSpeedHeating(
    datRea(
      final fileName=ModelicaServices.ExternalReferences.loadResource("modelica://Buildings/Resources/Data/Fluid/DXSystems/Heating/AirSource/Validation/SingleSpeedHeating_TimedReverseCycleDefrost/DXCoilSystemAuto.dat")),
    datCoi(
      final defOpe=Buildings.Fluid.DXSystems.Heating.BaseClasses.Types.DefrostOperation.reverseCycle,
      final defTri=Buildings.Fluid.DXSystems.Heating.BaseClasses.Types.DefrostTimeMethods.timed));
end SingleSpeed_TimedReverseCycleDefrost;


Baseclasses for validation models

within Buildings.Fluid.DXSystems.Heating.AirSource.Validation;
package BaseClasses "Baseclasses for validation models"
  extends Modelica.Icons.BasesPackage;
end BaseClasses;


within Buildings.Fluid.DXSystems.Heating.AirSource.Validation.BaseClasses;
model SingleSpeedHeating
  "Baseclass for validation models for single speed DX heating coil"

  package Medium = Buildings.Media.Air "Medium model";

  Buildings.Fluid.Sources.Boundary_pT sin(
    redeclare package Medium = Medium,
    final p(displayUnit="Pa") = 101325,
    final nPorts=1,
    final T=294.15)
    "Sink"

  Buildings.Fluid.DXSystems.Heating.AirSource.SingleSpeed sinSpeDX(
    redeclare package Medium = Medium,
    final dp_nominal=1141,
    final datCoi=datCoi,
    final T_start=datCoi.sta[1].nomVal.TEvaIn_nominal,
    final from_dp=true,
    final energyDynamics=Modelica.Fluid.Types.Dynamics.FixedInitial,
    final dTHys=1e-6) "Single speed DX heating coil"

  Buildings.Utilities.IO.BCVTB.From_degC TEvaIn_K
    "Converts degC to K"

  Buildings.Utilities.IO.BCVTB.From_degC TConIn_K
    "Converts degC to K"

  Modelica.Blocks.Math.Mean TOutMea(
    final f=1/3600)
    "Mean of measured outlet air temperature"

  Buildings.Utilities.IO.BCVTB.To_degC TOutDegC
    "Convert measured outlet air temperature to deg C"

  Modelica.Blocks.Sources.RealExpression TOut(
    final y=sinSpeDX.vol.T)
    "Measured temperature of outlet air"

  Modelica.Blocks.Math.Mean XConOutMea(
    final f=1/3600)
    "Mean of measured outlet air humidity ratio per kg total air"

  Modelica.Blocks.Sources.RealExpression XConOut(
    final y=sum(sinSpeDX.vol.Xi))
    "Measured humidity ratio of outlet air"

  Modelica.Blocks.Math.Mean Q_flowMea(
    final f=1/3600)
    "Mean of cooling rate"

  Modelica.Blocks.Math.Mean PMea(
    final f=1/3600)
    "Mean of power"

  Buildings.Controls.OBC.CDL.Discrete.UnitDelay PEPlu(
    final samplePeriod=3600)
    "Total power consumption from EnergyPlus"

  Buildings.Controls.OBC.CDL.Discrete.UnitDelay Q_flowEPlu(
    final samplePeriod=3600)
    "Heat transfer to airloop from EnergyPlus"

  Buildings.Controls.OBC.CDL.Discrete.UnitDelay TOutEPlu(
    final samplePeriod=3600)
    "Outlet temperature from EnergyPlus"

  Buildings.Controls.OBC.CDL.Discrete.UnitDelay XConOutEPlu(
    final samplePeriod=3600)
    "Outlet air humidity ratio from EnergyPlus"

  Buildings.Utilities.Psychrometrics.ToTotalAir toTotAirOut
    "Convert humidity ratio per kg dry air to humidity ratio per kg total air for outdoor air"

  Buildings.Utilities.Psychrometrics.ToTotalAir toTotAirEPlu
    "Convert humidity ratio per kg dry air from EnergyPlus to humidity ratio per kg total air"

  Buildings.Controls.OBC.CDL.Discrete.UnitDelay PDefEPlu(
    final samplePeriod=3600)
    "Defrost power from EnergyPlus"

  Buildings.Controls.OBC.CDL.Discrete.UnitDelay PCraEPlu(
    final samplePeriod=3600)
    "Cranckcase heater power from EnergyPlus"

  Modelica.Blocks.Sources.CombiTimeTable datRea(
    final tableOnFile=true,
    final columns=2:18,
    final tableName="EnergyPlus",
    final smoothness=Modelica.Blocks.Types.Smoothness.ConstantSegments)
    "Reader for EnergyPlus example results"

  Buildings.Fluid.Sources.MassFlowSource_T boundary(
    redeclare package Medium = Medium,
    final use_Xi_in=true,
    final use_m_flow_in=true,
    final use_T_in=true,
    final nPorts=1)
    "Mass flow source for coil inlet air"

  Buildings.Utilities.Psychrometrics.ToTotalAir toTotAirIn
    "Convert humidity ratio per kg dry air to humidity ratio per kg total air for coil inlet air"

  parameter Buildings.Fluid.DXSystems.Heating.AirSource.Validation.Data.SingleSpeedHeating datCoi
    "Heating coil data record"

  Buildings.Utilities.Psychrometrics.Phi_pTX phi
    "Conversion to relative humidity"
  Modelica.Blocks.Sources.Constant pAtm(final k=101325) "Atmospheric pressure"
  Controls.OBC.CDL.Logical.VariablePulse plrToPul(period=3600)
    "Convert part load ratio to pulse signal"
equation
  connect(sinSpeDX.port_b, sin.ports[1])
  connect(TEvaIn_K.Kelvin, sinSpeDX.TOut)
  connect(TOut.y, TOutMea.u)
  connect(TOutMea.y, TOutDegC.Kelvin)
  connect(XConOut.y,XConOutMea. u)
  connect(sinSpeDX.QSen_flow, Q_flowMea.u)
  connect(toTotAirEPlu.XiTotalAir, XConOutEPlu.u)
  connect(sinSpeDX.P, PMea.u)
  connect(datRea.y[1], TEvaIn_K.Celsius)
  connect(datRea.y[9], toTotAirOut.XiDry)
  connect(datRea.y[17], boundary.m_flow_in)
  connect(TConIn_K.Kelvin, boundary.T_in)
  connect(toTotAirIn.XiTotalAir, boundary.Xi_in[1])
  connect(datRea.y[5], TConIn_K.Celsius)
  connect(datRea.y[6], toTotAirIn.XiDry)
  connect(boundary.ports[1], sinSpeDX.port_a)
  connect(datRea.y[7], TOutEPlu.u)
  connect(datRea.y[8], toTotAirEPlu.XiDry)
  connect(datRea.y[3], PEPlu.u)
  connect(datRea.y[2], Q_flowEPlu.u)
  connect(datRea.y[15], PDefEPlu.u)
  connect(datRea.y[16], PCraEPlu.u)
  connect(phi.X_w, toTotAirOut.XiTotalAir)
  connect(TEvaIn_K.Kelvin, phi.T)
  connect(pAtm.y, phi.p)
  connect(sinSpeDX.phi, phi.phi)
  connect(plrToPul.u, datRea.y[14])
  connect(plrToPul.y, sinSpeDX.on)
end SingleSpeedHeating;


within Buildings.Fluid.DXSystems.Heating.AirSource.Validation;
package Data "Package with performance data used in validation models"
  extends Modelica.Icons.MaterialPropertiesPackage;
end Data;


Calculates defrost curve value at given temperature and mass flow rate. Block that calculates the defrost cycling time fraction tDefFra,
the heating capacity multiplier heaCapMul
and the input power multiplier inpPowMul.

within Buildings.Fluid.DXSystems.Heating.BaseClasses;
block CoilDefrostTimeCalculations
  "Calculates defrost curve value at given temperature and mass flow rate"
  extends Modelica.Blocks.Icons.Block;

  parameter Buildings.Fluid.DXSystems.Heating.BaseClasses.Types.DefrostTimeMethods
    defTri=Buildings.Fluid.DXSystems.Heating.BaseClasses.Types.DefrostTimeMethods.timed
    "Type of method to trigger the defrost cycle";

  parameter Real tDefRun(
    final unit="1") = 0.5
    "If defrost operation is timed, timestep fraction for which defrost cycle is run"

  parameter Modelica.Units.SI.ThermodynamicTemperature TDefLim
    "Maximum temperature at which defrost operation is activated";

  parameter Modelica.Units.SI.TemperatureDifference dTHys = 0.5
    "Temperature comparison hysteresis difference"

  Modelica.Blocks.Interfaces.RealInput TOut(
    final unit="K",
    displayUnit="degC",
    final quantity="ThermodynamicTemperature")
    "Temperature of outdoor air"

  Modelica.Blocks.Interfaces.RealInput XOut(
    final unit="kg/kg",
    final quantity="MassFraction")
    "Humidity ratio of outdoor air"

  Modelica.Blocks.Interfaces.RealOutput tDefFra(
    final unit="1")
    "Defrost operation timestep fraction"

  Modelica.Blocks.Interfaces.RealOutput heaCapMul(
    final unit="1")
    "Heating capacity multiplier"

  Modelica.Blocks.Interfaces.RealOutput inpPowMul(
    final unit="1")
    "Input power multiplier"

  Modelica.Units.SI.MassFraction delta_XCoilOut
    "Difference between outdoor air humidity ratio and the saturated air humidity
    ratio at estimated outdoor coil temperature";

  Modelica.Units.SI.ThermodynamicTemperature TCoiOut
    "Outdoor coil temperature";

  Modelica.Units.SI.MassFraction XOutDryAir
    "Outdoor air humidity ratio per kg total air";

  Buildings.Utilities.Psychrometrics.ToDryAir toDryAir
    "Convert outdoor air humidity ratio from total air to dry air";

  Buildings.Controls.OBC.CDL.Reals.Hysteresis hysTOut(
    final uLow=-dTHys,
    final uHigh=dTHys)
    "Check if outdoor air temperature is below maximum limit for defrost operation";

equation
  // Estimated outdoor coil surface temperature, based on outdoor air temperature.
  // 0.82 and 8.589 are empirical coefficients for this conversion, that are valid
  // only when outdoor air temperature is in degree Celsius.
  TCoiOut = Modelica.Units.Conversions.from_degC(0.82*Modelica.Units.Conversions.to_degC(TOut) - 8.589);

  // Provide outdoor air as input to conversion block
  XOut = toDryAir.XiTotalAir;

  // Get outdoor air humidity ratio per kg of dry air
  XOutDryAir = toDryAir.XiDry;

  // Calculate difference between outdoor air humidity ratio and saturated air humidity
  // ratio at estimated outdoor coil surface temperature, which will indicate frost formation
  delta_XCoilOut = Buildings.Utilities.Math.Functions.smoothMax(
    1e-6,
    XOutDryAir - Buildings.Utilities.Psychrometrics.Functions.X_pTphi(101325, TCoiOut, 1),
    0.5*1e-6);

  //  Use hysteresis block for comparison of outdoor air temperature with max limit
  //  for defrost operation
  hysTOut.u = TOut - TDefLim;

  // Check if defrost operation is allowed
  if not hysTOut.y then
    if defTri == Buildings.Fluid.DXSystems.Heating.BaseClasses.Types.DefrostTimeMethods.timed then
      tDefFra = tDefRun;
      // Empirical coefficients for calculating multipliers valid only for humidity
      // ratio difference per kg of dry air
      heaCapMul = 0.909 - 107.33*delta_XCoilOut;
      inpPowMul = 0.9 - 36.45*delta_XCoilOut;
    else
      // Empirical coefficients for calculating multipliers valid only for humidity
      // ratio per kg of dry air
      tDefFra = 1/(1 + (0.01446/delta_XCoilOut));
      heaCapMul =0.875*(1 - tDefFra);
      inpPowMul =0.954*(1 - tDefFra);
    end if;
  // Condition for when defrost is not operated
  else
    tDefFra = 0;
    heaCapMul = 1;
    inpPowMul = 1;
  end if;
end CoilDefrostTimeCalculations;


within Buildings.Fluid.DXSystems.Heating.BaseClasses;
block DefrostCapacity
  "Calculates defrost curve value at given temperature and mass flow rate"
  extends Modelica.Blocks.Icons.Block;

  replaceable package MediumA = Modelica.Media.Interfaces.PartialMedium
    "Fluid medium package";

  parameter Real tDefRun(
    final unit="1") = 0.5
    "If defrost operation is timed, timestep fraction for which defrost cycle is run"

  parameter Modelica.Units.SI.Power QDefResCap = defCur.QDefResCap
    "Capacity of resistive defrost element";

  parameter Buildings.Fluid.DXSystems.Heating.BaseClasses.Types.DefrostTimeMethods defTri=
    Buildings.Fluid.DXSystems.Heating.BaseClasses.Types.DefrostTimeMethods.timed
    "Type of method to trigger the defrost cycle";

  parameter Buildings.Fluid.DXSystems.Heating.BaseClasses.Types.DefrostOperation defOpe=
    Buildings.Fluid.DXSystems.Heating.BaseClasses.Types.DefrostOperation.resistive
    "Type of defrost method";

  parameter Buildings.Fluid.DXSystems.Heating.AirSource.Data.Generic.DXCoil defCur
    "Defrost curve parameter record";

  Modelica.Blocks.Interfaces.RealInput tDefFra(
    final unit="1")
    "Calculated fraction of timestep for which the defrost cycle is assumed to run"

  Modelica.Blocks.Interfaces.RealInput heaCapMul(
    final unit="1")
    "Heating capacity multiplier"

  Modelica.Blocks.Interfaces.RealInput inpPowMul(
    final unit="1")
    "Input power multiplier"

  Modelica.Blocks.Interfaces.RealInput TConIn(
    final unit="K",
    displayUnit="degC",
    final quantity="ThermodynamicTemperature")
    "Temperature of air entering indoor condenser unit"

  Modelica.Blocks.Interfaces.RealInput XConIn(
    final unit="kg/kg",
    final quantity="MassFraction")
    "Humidity ratio of air entering indoor condenser coil"

  Modelica.Blocks.Interfaces.RealInput pIn(
    final unit="Pa",
    final quantity="Pressure")
    "Pressure of air entering indoor condenser coil"

  Modelica.Blocks.Interfaces.RealInput TOut(
    final unit="K",
    displayUnit="degC",
    final quantity="ThermodynamicTemperature")
    "Temperature of outdoor air"

  Modelica.Blocks.Interfaces.RealInput QTot_flow(
    final unit="W",
    final quantity="Power")
    "Total heating capacity from heating coil curve calculations"

  Modelica.Blocks.Interfaces.RealInput EIR(
    final unit="1")
    "Total energy input ratio from heating coil curve calculations"

  Modelica.Blocks.Interfaces.RealInput uSpe(
    final unit="1")
    "Input speed signal"

  Modelica.Blocks.Interfaces.RealOutput QDef_flow(
    final unit="W",
    final quantity="Power")
    "Power extracted from airloop to defrost outdoor coil"

  Modelica.Blocks.Interfaces.RealOutput PDef(
    final unit="W",
    final quantity="Power")
    "Power input required for running defrost operation"

  Modelica.Blocks.Interfaces.RealOutput PTot(
    final unit="W",
    final quantity="Power")
    "Total power input required for running heating coil with defrost operation"

  Modelica.Blocks.Interfaces.RealOutput QTotDef_flow(
    final unit="W",
    final quantity="Power")
    "Total heat added to airloop after defrost"

  Modelica.Blocks.Interfaces.RealOutput PCra(
    final unit="W",
    final quantity="Power")
    "Crankcase heater power consumption"

  Real defMod_T(
    final unit="1")
    "Defrost modifier curve value";

  Real RTF(
    final unit="1")
    "Run-time fraction";

  Real PLR(
    final unit="1")
    "Part-load ratio";

  Real PLFra(
    final unit="1")
    "Part-load fraction";

  Modelica.Units.SI.ThermodynamicTemperature TConInWetBul
    "Wet bulb temperature of air entering indoor condenser coil";

  Buildings.Utilities.Psychrometrics.TWetBul_TDryBulXi wetBul(
    redeclare package Medium = MediumA)
    "Calculate wetbulb temperature of condensor inlet air";

equation
  //  Find wet-bulb temperature of inlet air
  pIn = wetBul.p;
  XConIn = wetBul.Xi[1];
  TConIn = wetBul.TDryBul;
  TConInWetBul=wetBul.TWetBul;

  // Calculate modifying factor for defrost operation power consumption
  defMod_T =Buildings.Utilities.Math.Functions.smoothMax(
    x1=Buildings.Utilities.Math.Functions.biquadratic(
      a=defCur.defEIRFunT,
      x1=Modelica.Units.Conversions.to_degC(TConInWetBul),
      x2=Modelica.Units.Conversions.to_degC(TOut)),
      x2=0.001,
      deltaX=0.0001);

  PLR = uSpe;

  //  Calculate heat transferred from airflow to outdoor coil, and power consumed by
  //  compressor for defrost operation.
  if defOpe == Buildings.Fluid.DXSystems.Heating.BaseClasses.Types.DefrostOperation.resistive then
    QDef_flow = 0;
    PDef =QDefResCap*tDefFra*RTF;
  else
    //  All coefficients are empirical values valid only for temperature in degree
    //  Celsius and heat flow rate in Watts.
    QDef_flow = 0.01*tDefFra*(7.222 - Modelica.Units.Conversions.to_degC(TOut))*
      QTot_flow/1.01667;
    PDef =defMod_T*QTot_flow*tDefFra*RTF/1.01667;
  end if;
  QTotDef_flow = QTot_flow*heaCapMul;

  // Partload fraction for heating coil
  PLFra = Buildings.Fluid.Utilities.extendedPolynomial(
      x=PLR,
      c=defCur.PLFraFunPLR,
      xMin=0,
      xMax=1);

  //  Calculate total power consumption, run-time fraction and crankcase heater
  //  power consumption.
  PTot =QTot_flow*EIR*PLR*inpPowMul/PLFra;
  RTF = PLR/PLFra;
  PCra = defCur.QCraCap * (1 - RTF);
end DefrostCapacity;


within Buildings.Fluid.DXSystems.Heating;
package BaseClasses "Package with base classes for Buildings.Fluid.DXSystems.Heating"
  extends Modelica.Icons.BasesPackage;
end BaseClasses;


Partial model for DX heating coil

within Buildings.Fluid.DXSystems.Heating.BaseClasses;
partial model PartialDXHeatingCoil
  "Partial model for DX heating coil"
  extends Buildings.Fluid.Interfaces.TwoPortHeatMassExchanger(
    redeclare replaceable package Medium =
        Modelica.Media.Interfaces.PartialCondensingGases,
    redeclare final Buildings.Fluid.MixingVolumes.MixingVolume vol(
      prescribedHeatFlowRate=true),
    final m_flow_nominal = datCoi.sta[nSta].nomVal.m_flow_nominal);

  parameter Buildings.Fluid.DXSystems.Heating.AirSource.Data.Generic.DXCoil datCoi
    "Performance data"

  parameter Modelica.Units.SI.TemperatureDifference dTHys(
    final min=0)=0.5
    "Temperature comparison for hysteresis"

  Modelica.Blocks.Interfaces.RealInput TOut(
    final unit="K",
    displayUnit="degC")
    "Outside air dry bulb temperature for an air cooled condenser or wetbulb temperature for an evaporative cooled condenser"

  Modelica.Blocks.Interfaces.RealInput phi(
    final unit="1")
    "Outdoor air relative humidity at evaporator inlet (0...1)"

  Modelica.Blocks.Interfaces.RealOutput P(
    final quantity="Power",
    final unit="W")
    "Electrical power consumed"

  Modelica.Blocks.Interfaces.RealOutput QSen_flow(
    final quantity="Power",
    final unit="W")
    "Sensible heat flow rate"

  Buildings.Fluid.DXSystems.BaseClasses.DryCoil dxCoi(
    final datCoi=datCoi,
    final variableSpeedCoil=false,
    final use_mCon_flow=false) "DX coil"

protected
  constant String substanceName="water"
    "Name of species substance";

  parameter Integer nSta=datCoi.nSta "Number of stages";

  parameter Integer i_x(fixed=false)
    "Index of substance";

  parameter Modelica.Units.SI.AbsolutePressure pAtm(fixed=false)
    "Atmospheric pressure";

  Modelica.Units.SI.SpecificEnthalpy hIn=inStream(port_a.h_outflow)
    "Enthalpy of air entering the DX coil";

  Modelica.Units.SI.Temperature TIn=Medium.temperature_phX(
    p=port_a.p,
    h=hIn,
    X=XIn)
    "Dry bulb temperature of air entering the DX coil";

  Modelica.Units.SI.MassFraction XIn[Medium.nXi]=inStream(
    port_a.Xi_outflow)
    "Mass fraction/absolute humidity of air entering the DX coil";

  Modelica.Blocks.Sources.RealExpression T(
    final y=TIn)
    "Inlet air temperature"

  Modelica.Blocks.Sources.RealExpression m(
    final y=port_a.m_flow) "Inlet air mass flow rate"

  Buildings.Fluid.DXSystems.Heating.BaseClasses.CoilDefrostTimeCalculations
    defTimFra(
    final defTri=datCoi.defTri,
    final tDefRun=datCoi.tDefRun,
    final TDefLim=datCoi.TDefLim,
    final dTHys=dTHys)
    "Block to compute defrost time fraction, heat transfer multiplier and input power multiplier"

  Buildings.Fluid.DXSystems.Heating.BaseClasses.DefrostCapacity defCap(
    redeclare package MediumA = Medium,
    final defTri=datCoi.defTri,
    final defOpe=datCoi.defOpe,
    final tDefRun=datCoi.tDefRun,
    final defCur=datCoi,
    final QDefResCap=datCoi.QDefResCap)
    "Block to compute actual heat transferred to medium and power input after accounting for defrost"

  Buildings.Utilities.Psychrometrics.X_pTphi x_pTphi(
    final use_p_in=false,
    final p=pAtm)
    "Conversion for relative humidity to water vapor mass fraction"

  Buildings.HeatTransfer.Sources.PrescribedHeatFlow q
    "Heat extracted by coil"

  Modelica.Blocks.Sources.RealExpression p_in(
    final y=port_a.p)
    "Inlet air pressure"

  Modelica.Blocks.Sources.RealExpression X(
    final y=XIn[i_x])
    "Inlet air mass fraction"

initial algorithm
  // Make sure that |Q_flow_nominal[nSta]| >= |Q_flow_nominal[i]| for all stages because the data
  // of nSta are used in the evaporation model
  for i in 1:(nSta-1) loop
    assert(datCoi.sta[i].nomVal.Q_flow_nominal >= datCoi.sta[nSta].nomVal.Q_flow_nominal,
    "Error in DX coil performance data: Q_flow_nominal of the highest stage must have
    the biggest value in magnitude. Obtained " + Modelica.Math.Vectors.toString(
    {datCoi.sta[i].nomVal.Q_flow_nominal for i in 1:nSta}, "Q_flow_nominal"));
  end for;

  // Compute index of species vector that carries the substance name
  i_x :=-1;
    for i in 1:Medium.nXi loop
      if Modelica.Utilities.Strings.isEqual(string1=Medium.substanceNames[i],
                                            string2=substanceName,
                                            caseSensitive=false) then
        i_x :=i;
      end if;
    end for;
  assert(i_x > 0, "Substance '" + substanceName + "' is not present in medium '"
                  + Medium.mediumName + "'.\n"
                  + "Change medium model to one that has '" + substanceName + "' as a substance.");

  pAtm :=port_a.p;
equation
  connect(m.y,dxCoi. m_flow)

  connect(q.port, vol.heatPort)

  connect(TOut,dxCoi.TEvaIn)
  connect(T.y,dxCoi.TConIn)
  connect(TOut, defTimFra.TOut)
  connect(defTimFra.tDefFra, defCap.tDefFra)
  connect(defTimFra.heaCapMul, defCap.heaCapMul)
  connect(defTimFra.inpPowMul, defCap.inpPowMul)
  connect(T.y, defCap.TConIn)
  connect(dxCoi.Q_flow, defCap.QTot_flow)
  connect(dxCoi.EIR, defCap.EIR)
  connect(TOut, defCap.TOut)
  connect(defCap.QTotDef_flow, q.Q_flow)
  connect(defCap.QTotDef_flow, QSen_flow)
  connect(defCap.PTot, P)
  connect(p_in.y, defCap.pIn)
  connect(X.y, defCap.XConIn)
  connect(defTimFra.XOut, x_pTphi.X[1])
  connect(x_pTphi.T, TOut)
  connect(x_pTphi.phi, phi)
end PartialDXHeatingCoil;


within Buildings.Fluid.DXSystems.Heating.BaseClasses;
package Types "Package with base classes for DX heating coils"
  extends Modelica.Icons.TypesPackage;
end Types;


within Buildings.Fluid;
package Examples "Example models integrating multiple components"
  extends Modelica.Icons.ExamplesPackage;
end Examples;


within Buildings.Fluid.Examples;
model ResistanceVolumeFlowReversal
  "System demonstrating impact of allowFlowReversal on number/size of non-linear systems"
  extends Modelica.Icons.Example;

  package Medium = Modelica.Media.Water.ConstantPropertyLiquidWater;
  parameter Real m_flow_nominal=0.1 "Gain value multiplied with input signal";
  Buildings.Fluid.Sources.Boundary_pT bou(
    redeclare package Medium = Medium,
    nPorts=1) "Boundary for pressure boundary condition"
  Movers.FlowControlled_m_flow pump(
    redeclare package Medium = Medium,
    m_flow_nominal=m_flow_nominal,
    use_riseTime=false,
    allowFlowReversal=allowFlowReversal.k,
    energyDynamics=Modelica.Fluid.Types.Dynamics.FixedInitial,
    nominalValuesDefineDefaultPressureCurve=true)
    "Pump model with unidirectional flow"
  Buildings.Fluid.HeatExchangers.Heater_T hea(
    redeclare package Medium = Medium,
    dp_nominal=1000,
    QMax_flow=1000,
    m_flow_nominal=m_flow_nominal,
    energyDynamics=Modelica.Fluid.Types.Dynamics.FixedInitial,
    allowFlowReversal=allowFlowReversal.k) "Heater"
  Modelica.Blocks.Sources.Pulse pulse(period=1000) "Pulse input"
  Modelica.Blocks.Math.Gain gain(k=m_flow_nominal) "Gain for m_flow_nominal"
  Buildings.Fluid.Actuators.Valves.ThreeWayLinear val(
    redeclare package Medium = Medium,
    m_flow_nominal=m_flow_nominal,
    dpValve_nominal=1000,
    l={0.002,0.002},
    energyDynamics=Modelica.Fluid.Types.Dynamics.SteadyState)
    "Three way valve with constant input"
  Modelica.Blocks.Sources.Constant const(k=0.5) "Constant valve set point"
  Modelica.Blocks.Sources.BooleanConstant allowFlowReversal(k=false)
    "Block for setting allowFlowReversal in components"
  Buildings.Fluid.FixedResistances.PressureDrop[nRes.k] res(
    redeclare package Medium = Medium,
    each allowFlowReversal=allowFlowReversal.k,
    each m_flow_nominal=m_flow_nominal/nRes.k,
    each dp_nominal=1000) "Fluid resistance for splitting flow"
  Modelica.Blocks.Sources.IntegerConstant nRes(k=10)
    "Number of parallel branches"
  Delays.DelayFirstOrder[nRes.k] vol(
    redeclare each package Medium = Medium,
    each m_flow_nominal=m_flow_nominal/nRes.k,
    each nPorts=2,
    each energyDynamics=Modelica.Fluid.Types.Dynamics.SteadyState,
    each allowFlowReversal=true) "Mixing volumes for enthalpy circuit"
equation
  connect(bou.ports[1],hea. port_a)
  connect(pulse.y,hea. TSet)
  connect(pump.m_flow_in, gain.y)
  connect(gain.u,pulse. y)
  connect(hea.port_b,val. port_1)
  connect(val.port_2, pump.port_a)
  connect(const.y,val. y)
  connect(val.port_3,hea. port_a)
  for i in 1:nRes.k loop
    connect(pump.port_b, res[i].port_a)
    connect(res[i].port_b, vol[i].ports[1])
    connect(vol[i].ports[2], val.port_3)
  end for;
end ResistanceVolumeFlowReversal;