
% This script is used for evaluation of control structures and controller
% tuning for the slave temperature controller.
% It utilizes the function "AmmonReac_TCont.m" for set-up of the model.
% The temperature controller is incorporated into the model

% Clear of the command line and the workspace
clc, clear, close all

% Definition of the current path folder
FileName = mfilename('fullpath');
[directory,~,~] = fileparts(FileName);
[dirmain,~,~] = fileparts(directory);

% Import of CASADI
addpath([dirmain '\Util1. Casadi3.1.0'])
addpath([dirmain '\Util2. Functions'])

% Definition of the initial guesses and redefinition into x and z
load([dirmain '\Data\Init_opt.mat'])

% Definition of version
dec.plot = true;
dec.save = false;

% Definition of the total integration time and the number of time steps for
% the integrator
t.tot  = 100*60;
t.int = 1;
t.N  = t.tot/t.int;
t.samp = (0:t.int:t.tot)/60;

% Load of the function
[func,dae,~,~,~,n] = AmmonReac_NewDist(t.int);
N = size(dae.x,1);

% Predefinition of the structure
k_s = zeros(n.u,1);

% Startup of the waitbar
h = waitbar(0,'Integrating the system(s)');

for l = 1:3
    x.S{l} = zeros(n.x,t.N+1);   x.S{l}(:,1) = x0;
    z.S{l} = zeros(n.z,t.N+1);   z.S{l}(:,1) = z0;
    L.S{l} = zeros(1,t.N+1);
    p.S{l} = [p0(1:n.u); zeros(n.d,1)]*ones(1,t.N+1);
    p.S{l}(l,2*60/t.int+1:t.N+1) = p0(l)*.98;

    % Integration of the system.
    for j = 2:t.N+1
        func_eval = func('x0',x.S{l}(:,j-1),'z0',z.S{l}(:,j-1),'p',p.S{l}(:,j-1));
        x.S{l}(:,j) = full(func_eval.xf);
        z.S{l}(:,j) = full(func_eval.zf);
        L.S{l}(j)   = full(func_eval.qf);
    end

    k_s(l,1) = (z.S{l}(2*N+l,2*60/t.int+2)-z.S{l}(2*N+l,2*60/t.int+1))./...
               (p.S{l}(l,2*60/t.int+1)-p.S{l}(l,2*60/t.int+0));
    % Updating the waitbar
    if mod(j,100) == 0
        waitbar(j/(t.N+1))
        fprintf('\n')
    end
end

% Calculation of the integral gain
tau_c = 10*ones(n.u,1);
KS.I = 1./k_s./tau_c;

%% Plotting and saving of the values

% Definition of integer values
itg.fig = 1;
itg.lab = 1;
itg.fda = 1;
itg.axe = 1;

% Definition of figure and values
fig.dimw = 12;
fig.dimh = 8;
fig.Font = [10 8];
fig.label = [];

if dec.plot
    fig.in(itg.fig) = figure(itg.fig);
    set(fig.in(itg.fig),'units','centimeters','position', [5+itg.fig,5+itg.fig, fig.dimw, fig.dimh]);

    for l = 1:n.u
        subplot(2,n.u,l)
        fig.axes(itg.axe) = gca; itg.axe = itg.axe+1;
        fig.dat(itg.fda) = plot(t.samp,p.S{l}(l,:),'k'); itg.fda = itg.fda+1;
        if l == 1
            fig.label(itg.lab) = ylabel('Input $u_i$ [\%]'); itg.lab = itg.lab+1;
        end
        
        subplot(2,n.u,n.u+l)
        fig.axes(itg.axe) = gca; itg.axe = itg.axe+1;
        fig.dat(itg.fda) = plot(t.samp,z.S{l}(2*N+l,:),'k'); itg.fda = itg.fda+1;
        if l == 1
            fig.label(itg.lab) = ylabel('Temperature $T$ [C]'); itg.lab = itg.lab+1;
        end
        fig.label(itg.lab) = xlabel('Time $t$ [min]'); itg.lab = itg.lab+1;
    end
    itg.fig = itg.fig+1;
end


set(fig.label, 'Fontsize', fig.Font(1));
set(fig.label, 'Interpreter', 'Latex');
set(fig.axes,'Fontsize', fig.Font(2));
set(fig.axes,'TickLabelInterpreter', 'Latex');
set(fig.axes,'LineWidth', 1);

if dec.save
    save([dirmain '\Data\Tun_Slave.mat'],'k_s')
end