close all
clear all
filename=strcat(pwd,'\OPTdata\1DistSol_FixedPrice23-Apr-2013.mat');
aux=load(filename);
 sol=aux.sol;
 
finaltime=26*3600+50;
tf=sol.t(end);
pricesim.time= sol.t;
pricesim.signals.values=sol.price;
pricesim.signals.dimensions=[1];

pricemean=mean(sol.price);

qoutsim.time= sol.t;
qoutsim.signals.values=sol.qout;
qoutsim.signals.dimensions=[1];

Qsim.time= sol.t;
Qsim.signals.values=sol.Q;
Qsim.signals.dimensions=[1];

qinsim.time= sol.t;
qinsim.signals.values=sol.qin;
qqinsim.signals.dimensions=[1];


TminS=50;
TmaxS=90;
VminS=0.05;
VmaxS=0.2;
Vmax=0.2;
Vmin=0.05;
Tmin=TminS+273;
Tmax=TmaxS+273;
[x0,par]=getData;cp=par.cp;
Tin=par.Tin;

sim('closed_loop.slx')
%%

time=timeSim/3600;
        VscaledOpt=(Vopt-VminS)/(VmaxS-VminS);
        VscaledSimple=(Vsimple-VminS)/(VmaxS-VminS);
        TscaledOpt=(Topt+273-Tin)/(TmaxS+273-Tin);       
        TscaledSimple=(Tsimple+273-Tin)/(TmaxS+273-Tin);    
        Tinscaled=(Tin-Tin)/(TmaxS-Tin);

        
        capopt=Vopt*cp.*(Topt+273-Tin);
        capsimple=Vsimple*cp.*(Tsimple+273-Tin);
        capmax=cp*Vmax*(Tmax-Tin);
        capmin=cp*Vmin*(Tmin-Tin);
        
        capopt=(capopt-capmin)/(capmax-capmin);
        capsimple=(capsimple-capmin)/(capmax-capmin);
        
%           capopt=VscaledOpt.*cp.*(TscaledOpt-Tinscaled);
%         capsimple=VscaledSimple.*cp.*(TscaledSimple-Tinscaled);
%         capmax=cp*1*(1)*ones(size(capopt));
        
        
    
        beta=1/1000;
        cost_NOT_heating=beta*(objectiveOpt(end)-objectiveSimple(end))
        saving=(Jsimple(end)-Jopt(end))
        
        

        
% capopt=cp*Vopt.*(Topt+273-Tin);
% capsimple=cp*Vsimple.*(Tsimple+273-Tin);


figure(1)
subplot(3,1,1)
plot(time,Vopt,'r-.',time,Vsimple,'b')
set(gca,'LineWidth',2,'FontSize',14)
ylabel('Volume')
axis tight

subplot(3,1,2)
plot(time,Topt,'r-.',time,Tsimple,'b')
set(gca,'LineWidth',2,'FontSize',14)
ylabel('Temperature')
axis tight

subplot(313)
plot(time,capopt,'r-.',time,capsimple,'b')
set(gca,'LineWidth',2,'FontSize',14)
ylabel('Capacity')
xlabel('Time, h')
axis tight



figure(2)
subplot(3,1,1)
plot(time,Qopt,'r-.',time,Qsimple,'b')
set(gca,'LineWidth',2,'FontSize',14)
ylabel('Power, kW')
axis tight
subplot(3,1,2)
plot(time,qinopt,'r-.',time,qsimple,'b')
set(gca,'LineWidth',2,'FontSize',14)
ylabel('Flow in')
axis tight

subplot(3,1,3)
plot(time,Jopt,'r-.',time,Jsimple,'b')
set(gca,'LineWidth',2,'FontSize',14)
ylabel('Cost')
xlabel('Time, h')
axis tight

figure(3)
plot(time,objectiveOpt,'r-.',time,objectiveSimple,'b')
set(gca,'LineWidth',2,'FontSize',14)
ylabel('Objetive')
tilefigs