within ${mod_prj}.${bldg.name};
model ${bldg.name}
  "This is the simulation model of ${bldg.name}"

% if model == "Multizone":
	AixLib.Building.LowOrder.Multizone.Multizone multizone(
      buildingParam=${mod_prj}.${bldg.name}.${bldg.name}_DataBase.${bldg.name}_base()

% if zone == "ThermalZone":
  ,redeclare AixLib.Building.LowOrder.ThermalZone.ThermalZone zone
% else:
  ,redeclare AixLib.Building.LowOrder.ThermalZone.ThermalZoneEquipped zone
% endif

% if physics == "vdi":
  (redeclare 
        AixLib.Building.LowOrder.BaseClasses.BuildingPhysics.BuildingPhysicsVDI
        buildingPhysics
% else:
  (redeclare 
        AixLib.Building.LowOrder.BaseClasses.BuildingPhysics.BuildingPhysics
        buildingPhysics
% endif 

% if gFac is True:
  (redeclare 
          AixLib.Building.Components.WindowsDoors.BaseClasses.CorrectionSolarGain.CorG_VDI6007
          corG)
% else:
   (redeclare 
          AixLib.Building.Components.WindowsDoors.BaseClasses.CorrectionSolarGain.NoCorG
          corG)
% endif
))

    annotation (Placement(transformation(extent={{-26,-14},{28,36}})));

  AixLib.Building.Components.Weather.Weather weather(
    Outopt=1,
    Air_temp=true,
    Mass_frac=true,
    Sky_rad=true,
    Ter_rad=true,
    fileName=Modelica.Utilities.Files.loadResource(
      "${weather}"),
    tableName="${weather_header}",
    % if bldg.latitude is None:
      Latitude=49.5,
    % else:
      Latitude=${bldg.latitude},
    % endif
    % if bldg.longitude is None:
      Longitude=8.5,
    % else:
      Longitude=${bldg.longitude},
    % endif
    SOD=AixLib.DataBase.Weather.SurfaceOrientation.SurfaceOrientationBaseDataDefinition
    (nSurfaces=${len(bldg.orientation_bldg)},
    name={${orientation_name(bldg.orientation_bldg)}},
    Azimut={${get_azimut(bldg.orientation_bldg)}},
    Tilt={${list_to_string(bldg.tilt_bldg)}}))
    annotation (Placement(transformation(extent={{-24,68},{6,88}})));

  Modelica.Blocks.Sources.CombiTimeTable tableInternalGains(
    tableOnFile=true,
    extrapolation=Modelica.Blocks.Types.Extrapolation.Periodic,
    tableName="Internals",
    columns=2:${(3*get_n(bldg.thermal_zones))+1},
    fileName=Modelica.Utilities.Files.loadResource(
        "modelica://${mod_prj}/${bldg.name}/${bldg.file_internal_gains}"))
    annotation (Placement(transformation(extent={{84,18},{64,38}})));

  Modelica.Blocks.Sources.CombiTimeTable combiTimeTable(table=[
  0
  % for tz in bldg.thermal_zones:
    ,${tz.infiltration_rate} ${';' if (loop.last) else ''}
  %endfor
  3600
  % for tz in bldg.thermal_zones:
    ,${tz.infiltration_rate}
  %endfor
  ])
    annotation (Placement(transformation(extent={{-74,-2},{-54,18}})));


equation
  connect(weather.SolarRadiation_OrientedSurfaces, multizone.radIn)
    annotation (Line(points={{-16.8,67},{-16.8,50.5},{-15.74,50.5},{-15.74,33.5}},
        color={255,128,0}));

  connect(weather.WeatherDataVector, multizone.weather) annotation (
     Line(points={{-9.1,67},{-9.1,52},{-3.32,52},{-3.32,34.5}}, color={0,0,127}));

  connect(tableInternalGains.y, multizone.internalGains)
    annotation (Line(points={{63,28},{46,28},{46,27.25},{26.11,27.25}},
        color={0,0,127}));
  % for i, zo in enumerate(bldg.thermal_zones):
  connect(weather.WeatherDataVector[${i+1}], multizone.ventilationTemperature[${i+1}])
    annotation (Line(points={{-9.1,67},{-9.1,52.5},{-24.38,52.5},{-24.38,15}},
        color={0,0,127}));
  % endfor

  connect(combiTimeTable.y, multizone.ventilationRate) annotation (Line(points={{-51,8},
          {-24.38,8},{-24.38,7.5}}, color={0,0,127}));

  annotation (experiment(
      StopTime=31536000,
      Interval=3600,
      __Dymola_Algorithm="Dassl"), __Dymola_experimentSetupOutput(events=false),
    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};




% elif model == "MultizoneEquipped":
	AixLib.Building.LowOrder.Multizone.MultizoneEquipped multizoneEquipped(
      buildingParam=${mod_prj}.${bldg.name}.${bldg.name}_DataBase.${bldg.name}_base()


% if zone == "ThermalZone":
	,redeclare AixLib.Building.LowOrder.ThermalZone.ThermalZone zone
% else:
  ,redeclare AixLib.Building.LowOrder.ThermalZone.ThermalZoneEquipped zone
% endif

% if physics == "vdi":
	(redeclare
        AixLib.Building.LowOrder.BaseClasses.BuildingPhysics.BuildingPhysicsVDI
        buildingPhysics
% else:
  (redeclare
        AixLib.Building.LowOrder.BaseClasses.BuildingPhysics.BuildingPhysics
        buildingPhysics
% endif

% if gFac is True:
	(redeclare
          AixLib.Building.Components.WindowsDoors.BaseClasses.CorrectionSolarGain.CorG_VDI6007
          corG)
% else:
	 (redeclare
          AixLib.Building.Components.WindowsDoors.BaseClasses.CorrectionSolarGain.NoCorG
          corG)
% endif
)

% if check_AHU(bldg.thermal_zones) == False:
    ,redeclare AixLib.Airflow.AirHandlingUnit.NoAHU AirHandlingUnit "AHU does
    not exist"
% endif
)

    annotation (Placement(transformation(extent={{-26,-14},{28,36}})));

  AixLib.Building.Components.Weather.Weather weather(
    Outopt=1,
    Air_temp=true,
    Mass_frac=true,
    Sky_rad=true,
    Ter_rad=true,
    fileName=Modelica.Utilities.Files.loadResource(
      "${weather}"),
    tableName="${weather_header}",
    % if bldg.latitude is None:
      Latitude=49.5,
    % else:
      Latitude=${bldg.latitude},
    % endif
    % if bldg.longitude is None:
      Longitude=8.5,
    % else:
      Longitude=${bldg.longitude},
    % endif
    SOD=AixLib.DataBase.Weather.SurfaceOrientation.SurfaceOrientationBaseDataDefinition
    (nSurfaces=${len(bldg.orientation_bldg)},
    name={${orientation_name(bldg.orientation_bldg)}},
    Azimut={${get_azimut(bldg.orientation_bldg)}},
    Tilt={${list_to_string(bldg.tilt_bldg)}}))
    annotation (Placement(transformation(extent={{-24,68},{6,88}})));

  Modelica.Blocks.Sources.CombiTimeTable tableTSet(
    tableOnFile=true,
    tableName="Tset",
    extrapolation=Modelica.Blocks.Types.Extrapolation.HoldLastPoint,
    columns=2:${get_n(bldg.thermal_zones)+1},
    fileName=Modelica.Utilities.Files.loadResource(
        "modelica://${mod_prj}/${bldg.name}/${bldg.file_set_t}"))
    annotation (Placement(transformation(extent={{-92,8},{-72,28}})));

  Modelica.Blocks.Sources.CombiTimeTable tableAHU(
    tableOnFile=true,
    extrapolation=Modelica.Blocks.Types.Extrapolation.Periodic,
    tableName="AHU",
    columns=2:5,
    fileName=Modelica.Utilities.Files.loadResource(
        "modelica://${mod_prj}/${bldg.name}/${bldg.file_ahu}"))
    annotation (Placement(transformation(extent={{66,-50},{46,-30}})));

  Modelica.Blocks.Sources.CombiTimeTable tableInternalGains(
    tableOnFile=true,
    extrapolation=Modelica.Blocks.Types.Extrapolation.Periodic,
    tableName="Internals",
    columns=2:${(3*get_n(bldg.thermal_zones))+1},
    fileName=Modelica.Utilities.Files.loadResource(
        "modelica://${mod_prj}/${bldg.name}/${bldg.file_internal_gains}"))
    annotation (Placement(transformation(extent={{84,18},{64,38}})));

  Modelica.Blocks.Sources.Constant const[${get_n(bldg.thermal_zones)}](each k=0)
    annotation (Placement(transformation(extent={{-92,-22},{-72,-2}})));

equation
  connect(weather.SolarRadiation_OrientedSurfaces, multizoneEquipped.radIn)
    annotation (Line(points={{-16.8,67},{-16.8,50.5},{-15.74,50.5},{-15.74,33.5}},
        color={255,128,0}));

  connect(weather.WeatherDataVector, multizoneEquipped.weather) annotation (
     Line(points={{-9.1,67},{-9.1,52},{-3.32,52},{-3.32,34.5}}, color={0,0,127}));

  connect(tableTSet.y, multizoneEquipped.TSetHeater) annotation (Line(
        points={{-71,18},{-48,18},{-48,12.5},{-24.38,12.5}}, color={0,0,127}));

  connect(tableAHU.y, multizoneEquipped.AHU) annotation (Line(points={{45,-40},
          {12.61,-40},{12.61,-12.25}}, color={0,0,127}));

  connect(tableInternalGains.y, multizoneEquipped.internalGains)
    annotation (Line(points={{63,28},{46,28},{46,27.25},{26.11,27.25}},
        color={0,0,127}));

  connect(const.y, multizoneEquipped.TSetCooler) annotation (Line(points={{-71,
          -12},{-66,-12},{-66,-14},{-48,-14},{-48,5.5},{-24.38,5.5}}, color=
         {0,0,127}));

  annotation (experiment(
      StopTime=31536000,
      Interval=3600,
      __Dymola_Algorithm="Dassl"), __Dymola_experimentSetupOutput(events=false),
    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};
% endif

##list to string
<%def name="list_to_string(list)", filter="trim">
  <%
  string_of_list = ""
  for item in list:
    string_of_list += str(item) + ", "
  string_of_list = string_of_list[:-2]
  return string_of_list
  %>
</%def>
##get n
<%def name="get_n(list)", filter="trim">
  <%
  n=0
  for element in list:
    if type(element).__name__ == "ThermalZone":
      n += 1
    else:
      pass
  return n
  %>
</%def>

##get azimut for modelica
<%def name="get_azimut(list)", filter="trim">
  <%
  newlist = []
  for element in list:
    if 0 < element < 360:
        newlist.append(-180.0+element)
    elif element == 0:
        newlist.append(180.0)
    elif element == -1:
        newlist.append(0.0)
    elif element == -2:
        newlist.append(0.0)

  string_of_list = ""
  for item in newlist:
    string_of_list += str(item) + ", "
  string_of_list = string_of_list[:-2]
  return string_of_list
  %>
</%def>

<%def name="orientation_name(list)", filter="trim">
  <%
  string_of_list = ""
  for item in list:
    string_of_list += '\"' + str(item) + '\"' + ", "
  string_of_list = string_of_list[:-2]
  return string_of_list
  %>
</%def>

##check if AHU is active
<%def name="check_AHU(list)", filter="trim">
  <%
  check = False
  for element in list:
    if element.use_conditions.with_ahu == True:
      check = True
      return check
    else:
      pass
return check
  %>
</%def>

