<%namespace file="/modelica_language/" import="get_true_false"/>
within ${bldg.parent.name}.${bldg.name};
model ${bldg.name}
  "This is the simulation model of ${bldg.name} with traceable ID ${bldg.building_id}"

% if bldg.internal_gains_mode==1 or bldg.internal_gains_mode==2:
  AixLib.ThermalZones.ReducedOrder.Multizone.MultizoneEquipped multizone(
    redeclare package Medium = Modelica.Media.Air.SimpleAir,
    buildingID=${bldg.building_id},
    energyDynamics=Modelica.Fluid.Types.Dynamics.FixedInitial,
    T_start = ${bldg.thermal_zones[0].t_inside},
    VAir = ${bldg.volume},
    ABuilding=${bldg.net_leased_area},
    ASurTot=${bldg.library_attr.total_surface_area},
    numZones = ${len(bldg.thermal_zones)},
    internalGainsMode = ${bldg.internal_gains_mode},
    zoneParam = {
      %for zone in bldg.thermal_zones:
      ${bldg.name}_DataBase.${bldg.name}_${zone.name}()${',' if not loop.last else ''}
      %endfor
      },
%else:
  AixLib.ThermalZones.ReducedOrder.Multizone.MultizoneMoistAirEquipped multizone(
    redeclare package Medium = AixLib.Media.Air,
    buildingID=${bldg.building_id},
    energyDynamics=Modelica.Fluid.Types.Dynamics.FixedInitial,
    T_start = ${bldg.thermal_zones[0].t_inside},
    VAir = ${bldg.volume},
    ABuilding=${bldg.net_leased_area},
    ASurTot=${bldg.library_attr.total_surface_area},
    numZones = ${len(bldg.thermal_zones)},
    internalGainsMode = ${bldg.internal_gains_mode},
    zoneParam = {
      %for zone in bldg.thermal_zones:
      ${bldg.name}_DataBase.${bldg.name}_${zone.name}()${',' if not loop.last else ''}
      %endfor
      },
% endif
% if bldg.with_ahu:
  heatAHU=${get_true_false(bldg.central_ahu.heating)},
  coolAHU=${get_true_false(bldg.central_ahu.cooling)},
  dehuAHU=${get_true_false(bldg.central_ahu.dehumidification)},
  huAHU=${get_true_false(bldg.central_ahu.humidification)},
  BPFDehuAHU=${bldg.central_ahu.by_pass_dehumidification},
  HRS=${get_true_false(bldg.central_ahu.heat_recovery)},
  sampleRateAHU=${bldg.central_ahu.sample_rate},
  effFanAHU_sup=${bldg.central_ahu.efficiency_fan_supply},
  effFanAHU_eta=${bldg.central_ahu.efficiency_fan_return},
  effHRSAHU_enabled=${bldg.central_ahu.efficiency_recovery},
  effHRSAHU_disabled=${bldg.central_ahu.efficiency_recovery_false},
  dpAHU_sup=${bldg.central_ahu.pressure_drop_fan_supply},
  dpAHU_eta=${bldg.central_ahu.pressure_drop_fan_return},
% else:
  heatAHU = false,
  coolAHU = false,
  dehuAHU = false,
  huAHU = false,
  BPFDehuAHU = 0.2,
  HRS = false,
  sampleRateAHU=1800,
  effFanAHU_sup=0.7,
  effFanAHU_eta=0.7,
  effHRSAHU_enabled = 0.8,
  effHRSAHU_disabled = 0.2,
  dpAHU_sup=800,
  dpAHU_eta=800,
% endif
  zone(ROM(extWallRC(thermCapExt(each der_T(fixed=true))),
           intWallRC(thermCapInt(each der_T(fixed=true))),floorRC
           (thermCapExt(each der_T(fixed=true))),roofRC(thermCapExt(each
           der_T(fixed=true))))),
% if bldg.internal_gains_mode==1 or bldg.internal_gains_mode==2:
   redeclare model thermalZone =
        AixLib.ThermalZones.ReducedOrder.ThermalZone.ThermalZoneAirExchange,
% else:
   redeclare model thermalZone =
        AixLib.ThermalZones.ReducedOrder.ThermalZone.ThermalZoneMoistAirExchange,
% endif
   redeclare model corG =
        AixLib.ThermalZones.ReducedOrder.SolarGain.CorrectionGDoublePane,
% if bldg.central_ahu:
  redeclare model AHUMod = AixLib.Airflow.AirHandlingUnit.AHU)
% else:
  redeclare model AHUMod = AixLib.Airflow.AirHandlingUnit.NoAHU)
% endif
    "Multizone"
    annotation (Placement(transformation(extent={{32,-8},{52,12}})));

  AixLib.BoundaryConditions.WeatherData.ReaderTMY3 weaDat(
    calTSky=AixLib.BoundaryConditions.Types.SkyTemperatureCalculation.HorizontalRadiation,
    computeWetBulbTemperature=false,
    filNam=
      "${weather.replace("\\", "/")}")
    "Weather data reader"
    annotation (Placement(transformation(extent={{-82,30},{-62,50}})));

  Modelica.Blocks.Sources.CombiTimeTable tableInternalGains(
    tableOnFile=true,
    extrapolation=Modelica.Blocks.Types.Extrapolation.Periodic,
    tableName="Internals",
    fileName=Modelica.Utilities.Files.loadResource(
        "modelica://${bldg.parent.name}/${bldg.name}/${bldg.library_attr.file_internal_gains}"),
    columns=2:${(3*len(bldg.thermal_zones))+1})
    "Profiles for internal gains"
    annotation (Placement(transformation(extent={{72,-42},{56,-26}})));

  Modelica.Blocks.Sources.CombiTimeTable tableAHU(
    tableOnFile=true,
    extrapolation=Modelica.Blocks.Types.Extrapolation.Periodic,
    tableName="AHU",
    columns=2:5,
    fileName=Modelica.Utilities.Files.loadResource(
        "modelica://${bldg.parent.name}/${bldg.name}/${bldg.library_attr.file_ahu}"))
    "Boundary conditions for air handling unit"
    annotation (Placement(transformation(extent={{-64,-6},{-48,10}})));

  Modelica.Blocks.Sources.CombiTimeTable tableTSet(
    tableOnFile=true,
    tableName="Tset",
    extrapolation=Modelica.Blocks.Types.Extrapolation.Periodic,
    fileName=Modelica.Utilities.Files.loadResource(
        "modelica://${bldg.parent.name}/${bldg.name}/${bldg.library_attr.file_set_t_heat}"),
    columns=2:${len(bldg.thermal_zones)+1})
    "Set points for heater"
    annotation (Placement(transformation(extent={{72,-66},{56,-50}})));

    Modelica.Blocks.Sources.CombiTimeTable tableTSetCool(
      tableOnFile=true,
      tableName="Tset",
      extrapolation=Modelica.Blocks.Types.Extrapolation.Periodic,
      fileName=Modelica.Utilities.Files.loadResource(
          "modelica://${bldg.parent.name}/${bldg.name}/${bldg.library_attr.file_set_t_cool}"),
      columns=2:${len(bldg.thermal_zones)+1})
      "Set points for cooler"
    annotation (Placement(transformation(extent={{72,-90},{56,-74}})));

equation
  connect(weaDat.weaBus, multizone.weaBus) annotation (Line(
      points={{-62,40},{-32,40},{-32,6},{34,6}},
      color={255,204,51},
      thickness=0.5));

  connect(tableInternalGains.y, multizone.intGains)
    annotation (Line(points={{55.2,-34},{48,-34},{48,-9}}, color={0,0,127}));

  connect(tableAHU.y, multizone.AHU)
    annotation (Line(points={{-47.2,2},{14,2},{33,2}}, color={0,0,127}));

  connect(tableTSet.y, multizone.TSetHeat) annotation (Line(points={{55.2,-58},
          {36.8,-58},{36.8,-9}}, color={0,0,127}));

  connect(tableTSetCool.y, multizone.TSetCool) annotation (Line(points={{55.2,-58},
          {36.8,-58},{36.8,-9}}, color={0,0,127}));

  annotation (experiment(
      StartTime=${str(zone.parent.parent.modelica_info.start_time)},
      StopTime=${str(zone.parent.parent.modelica_info.stop_time)},
      Interval=${modelica_info.interval_output},
      __Dymola_Algorithm="${modelica_info.current_solver}"),
      __Dymola_experimentSetupOutput(equidistant=${get_true_false(modelica_info.equidistant_output)},
      events=${get_true_false(modelica_info.results_at_events)}),
    Icon(coordinateSystem(preserveAspectRatio=false, extent={{-100,-100},{100,100}}),
        graphics={
        Line(points={{80,-82}}, color={28,108,200}),
        Rectangle(
          extent={{-80,20},{80,-80}},
          lineColor={0,0,0},
          lineThickness=0.5),
        Line(
          points={{-80,20},{0,100},{80,20}},
          color={0,0,0},
          thickness=0.5),
        Text(
          extent={{-52,-10},{62,-48}},
          lineColor={0,0,0},
          lineThickness=0.5,
          fillColor={0,0,255},
          fillPattern=FillPattern.Solid,
          textString="TB")}));
end ${bldg.name};
