%Main file to simulate room temperature
%October 2018
%This simulates the temperataure of a room where:
%MVs:
%QH1= heated water [kJ/s]
%QH2= electric heating [kJ/s]
%QAC= air conditioning [kJ/s]
%m_in= ventilation

%Disturbance:
%Tin= outdoor temperature that goes through ventilation

%States:
%T= indoor temperature; x1
%Tfl= floor temperature; x2

%The model is in the file called "Room". 
%The Simulink file is called RoomT
%Open loop Simulink file is called RoomT_OL

clc
clear
close all

%Parameters
m=180;          %mass of air in the room [kg]
Cp=1;           %Cp of air [kJ/(kg K)]
Ufl=10/1000;    %heat transfer coefficient for floor [kW/m^2K]=10 W/m^2K
Afl=50;         %area of floor [m^2]
mfl=600;        %mass of floor [kg]
Cpfl=1;         %Cp of floor - concrete [kJ/(kg k] 

m_in0=0.4;      %mass flow for ventilation; kg/s %it was originally an MV, but it is now a parameter

p=[m,Cp, Ufl, Afl, mfl, Cpfl]';

%Disturbance: ambient temperature
Tin0=21;        %°C / going from -10 to 30°C

%Nominal values
QH10=0;         %at steady-state, no need of heating water
QH20=0;         %at steady-state, no need of electric heating
QAC0=0;         %at steady-state, no need of air conditioning


Qhfl0=0;        %at steady-state, no heat transfer through the floor

%Nominal values for states
T0=21;          %room temperature, °C
Tfl0=21;        %floor temperature, °C
x0=[T0; Tfl0]; 

% %% Steps in MVs
% %Heating water
% QH1_1= 1;      
% QH1_2= -1;
% tQH1_1= 10;
% tQH1_2= 65000;
% 
% %Electric heating
% QH2_1= 1;     
% QH2_2= -1;
% tQH2_1= 20000;
% tQH2_2= 40000;
% 
% %Air conditioning
% QAC_1= 1;
% QAC_2= -1;
% tQAC_1= 85000;
% tQAC_2= 140000;
% 
% %Ventilation
% m_in_1= m_in0*0.1;
% m_in_2= 0;
% tm_in_1= 190000;
% tm_in_2= 2;
% 
% %Steps in disturbance Tin
% Tin_1= 5;
% Tin_2= 0;
% tTin_1= 190000;
% tTin_2= 2;

%% Steps in MVs
%Heating water
QH1_1= 0;      
QH1_2= 0; %-1;
tQH1_1= 10;
tQH1_2= 65000;

%Electric heating
QH2_1= 1; %1;     
QH2_2= 0; %-1;
tQH2_1= 10;
tQH2_2= 40000;

%Air conditioning
QAC_1= 0; %2; %1;
QAC_2= 0; %-1;
tQAC_1= 10;
tQAC_2= 140000;

%Ventilation
m_in_1= 0; %1; %m_in0*0.1;
m_in_2= 0;
tm_in_1= 40000;
tm_in_2= 2;

%Steps in disturbance Tin
Tin_1= 0;
Tin_2= 0;
tTin_1= 190000;
tTin_2= 2;

%% Simulate
tend=15000; %max([tQH1_2,tQAC_2,tQH2_2,tm_in_2,tTin_2,tm_in_1,tm_in_2])+50000;  %simulation time
sim('RoomT_OL')

%% Plot
%t is in seconds
tplot=t;


set(0,'DefaultTextFontName','Times',...
'DefaultTextFontSize',24,...
'DefaultAxesFontName','Times',...
'DefaultAxesFontSize',24,...
'DefaultLineLineWidth',2.5,...
'DefaultLineMarkerSize',12)
figure
subplot(2,1,1)
plot(tplot,T)
hold on
plot(tplot,Tfl)
ylabel('Temperature, [°C]')
legend('$T_{room}$', '$T_{floor}$','Interpreter','Latex');
hold off

subplot(2,1,2)
plot(tplot,Tin)
ylabel('Temperature, [°C]')
xlabel('time, [min]')
legend('$T_{in}$','Interpreter','Latex'); 

figure
subplot(2,1,1)
plot(tplot,QAC)
hold on
plot(tplot,QH2)
plot(tplot,QH1)
ylabel('Heat, [kW]')
legend('$Q_{AC}$', '$Q_{EL}$','$Q_{HW}$','Interpreter','Latex');
hold off

subplot(2,1,2)
plot(tplot,m_in)
ylabel('mass flow, [kg/s]')
xlabel('time, [min]')
legend('$m_{in}$','Interpreter','Latex');

figure
subplot(2,2,1)
plot(tplot,T)
hold on
plot(tplot,Tfl)
ylabel('Temperature, [°C]')
legend('$T_{room}$', '$T_{floor}$','Interpreter','Latex');
hold off

subplot(2,2,3)
plot(tplot,QAC,':')
hold on
plot(tplot,QH2)
plot(tplot,QH1,'--')
ylabel('Heat, [kW]')
legend('$Q_{AC}$', '$Q_{EL}$','$Q_{HW}$','Interpreter','Latex');
hold off
xlabel('time, [min]')

subplot(2,2,4)
plot(tplot,m_in)
ylabel('mass flow, [kg/s]')

legend('$m_{in}$','Interpreter','Latex');

subplot(2,2,2)
plot(tplot,Tin)
ylabel('Temperature, [°C]')
xlabel('time, [min]')
legend('$T^{amb}$','Interpreter','Latex'); 
