MATLAB/Simulink code update

This commit is contained in:
Radu C. Martin 2021-06-02 10:43:38 +02:00
parent d2179071db
commit d6b69acb17
28 changed files with 956 additions and 266 deletions

View file

@ -3,74 +3,58 @@ close all
clc
%%%%%%%%%%%%%%%%%%%%%%%
%% Load the experimental data
exp_id = "Exp1";
exp_path = strcat("../Data/Luca_experimental_data/", exp_id,".mat");
wdb_path = strcat("../Data/Experimental_data_WDB/", exp_id, "_WDB.mat");
Exp_data = load(exp_path);
load(wdb_path);
% Save the current WDB to the Simulink model import (since Carnot's input file is hardcoded)
save('../Data/input_WDB.mat', 'Exp_WDB');
tin = Exp_WDB(:,1);
% The power trick: when the setpoint is larger than the actual temperature
% the HVAC system is heating the room, otherwise it is cooling the room
Setpoint = Exp_data.(exp_id).Setpoint.values;
InsideTemp = mean([Exp_data.(exp_id).InsideTemp.values, Exp_data.(exp_id).LakeTemp.values], 2);
OutsideTemp = Exp_data.(exp_id).OutsideTemp.values;
HVAC_COP = 3;
Heating_coeff = sign(Setpoint - InsideTemp);
Heating_coeff(Heating_coeff == -1) = -1 * HVAC_COP;
%% Set the run parameters
air_exchange_rate = tin;
air_exchange_rate(:,2) = 1.0;
% Set the initial temperature to be the measured initial temperature
t0 = Exp_data.(exp_id).InsideTemp.values(1);
t0 = 23;
power = Exp_data.(exp_id).Power.values - 1.67 * 1000;
runtime1 = 161400;
runtime2 = 136200;
runtime3 = 208200;
runtime4 = 208200;
runtime5 = 208200;
runtime6 = 208200;
runtime7 = 553800;
power = [tin Heating_coeff .* power];
runtime = 24 * 3600;
set_param('polydome', 'StopTime', int2str(runtime))
Tsample = 900;
steps = runtime/Tsample;
tin = Tsample *(0:steps)';
prbs_sig = 2*prbs(8, steps+1)' - 1;
COP = 5.0;
Pel = 6300;
% Turn down the air exchange rate when the HVAC is not running
night_air_exchange_rate = 0.5;
air_exchange_rate(abs(power(:, 2)) < 100, 2) = night_air_exchange_rate;
power = [tin COP*Pel*prbs_sig(1:steps+1)];
%% Run the simulation
% Note: The simlulink model loads the data separately, includes the
% calculated solar position and radiations from pvlib
load_system("polydome");
set_param('polydome', 'StopTime', int2str(tin(end)));
simout = sim("polydome");
%% Simulate the model
out = sim('polydome');
SimulatedTemp = simout.SimulatedTemp;
%% Compare the simulation results with the measured values
figure; hold on; grid minor;
plot(tin, InsideTemp);
plot(tin, OutsideTemp);
plot(SimulatedTemp, 'LineWidth', 2);
legend('InsideTemp', 'OutsideTemp', 'SimulatedTemp');
%% For manual simulation running
WeatherMeasurement = struct;
WeatherMeasurement.data = squeeze(out.WeatherMeasurement.data)';
WeatherMeasurement.time = out.WeatherMeasurement.time;
input = [power(:, 2:end) WeatherMeasurement.data];
x0=500;
y0=300;
width=1500;
height=500;
set(gcf,'position',[x0,y0,width,height]);
title(exp_id);
%title(sprintf('Night Air exchange rate %f', night_air_exchange_rate));
Exp7_data = iddata(out.SimulatedTemp.data, input);
hold off;
Exp7_table = array2table([input out.SimulatedTemp.data], 'VariableNames', {'Power', 'SolRad', 'OutsideTemp', 'SimulatedTemp'});
saveas(gcf, strcat(exp_id, '_simulation'), 'svg')
writetable(Exp7_table, 'Exp7_table.csv')
%% Export simulated temperature to a .mat file for further use
carnot_output_dir = strcat("../Data/CARNOT_output/",exp_id,"_carnot_temp.mat");
save(carnot_output_dir, 'SimulatedTemp');
%%
save('Exp_CARNOT.mat', ...
'Exp1_data', 'Exp1_table', ...
'Exp2_data', 'Exp2_table', ...
'Exp3_data', 'Exp3_table', ...
'Exp4_data', 'Exp4_table', ...
'Exp5_data', 'Exp5_table', ...
'Exp6_data', 'Exp6_table', ...
'Exp7_data', 'Exp7_table' ...
)
data_train = merge(Exp1_data, Exp3_data, Exp5_data);
data_test = merge(Exp2_data, Exp4_data, Exp6_data, Exp7_data);