%% Header
% Purpose:  Calculation and graphical representation of pareto 
%           optimal PID controller tuning.
% Author:   Axel Lodemel Holene
% E-mail:   axel.holene@gmail.com
% Date:     13. February 2013
% About:    ctrl = [Kc I D]


%% Initialising
clear all
% close all
% clc

% Adding commonFiles to search path
mDir   = fullfile(...
'~/Documents/NTNU/Master_thesis/matlab/newWorldOrder') ;
shared = fullfile(mDir,'commonFiles') ;
m2tkz  = fullfile(mDir,'matlab2tikz/src') ;
addpath(shared)
addpath(m2tkz) 

warning('off','Control:combination:SingularRightDivision');
warning('off','Simulink:blocks:TDelayBufferTooSmall');
runTime = cputime ;                % Start total computation time

%% Simulation, process and controller  parameters
modelName = {'case1', 'case2', 'case3', 'case4', 'case5', ...
             'case6', 'case7', 'case8', 'case9', 'case10', ...
             'case11', 'case12', 'case13'} ;
cTypes  = {'po', 'simc', 'smith', 'smithDist'} ;
ctrlrs  = {'pid', 'pi'} ;
cParm   = {'Chriss', 'Parallel', 'AltParallel', 'Cascade'} ;

cType   = 3 ;
ctrlr   = 2 ;
cNum    = 3 ;
simTime = 50 ;

parm.cType = cType ;
parm.ctrlr = ctrlr ;
parm.cNum  = cNum  ;
parm.simTime = simTime ;

for modelId = 11:11 ;                                  % Model ID
    tic;
    % fprintf('\n')
    % fprintf('SMITH DISTURBANCE SENSITIVITY\n')
    % fprintf('------------------------------------------------\n')
    % if ctrlr == 1
    %     fprintf('Kc\tI\tD\tMs\tMs_s\td\tJ\n')
    % else
    %     fprintf('Kc\tI\tMs\tMs_s\td\tJ\n')
    % end
    % fprintf('------------------------------------------------\n')
    % d_span  = dMin:dRes:dMax ;
    G = model(modelId) ;
    parm.G = G ;
    parm.modelId = modelId ;
    d_s  = 0.5 ;
    Ms_s = 1.7 ;
    % Msc     = zeros(length(d_span),length(Ms_span)) ;         % Values for Ms
    % Mtc     = zeros(length(d_span),length(Ms_span)) ;       % Init. Mt vector
    % sTime   = zeros(length(d_span),length(Ms_span)) ;% Init. time consumption
    % Jtune   = zeros(length(d_span),length(Ms_span)) ;     % Init. cost vector
    load([mDir, '/results/smith/',          ...
            num2str(ctrlrs{ctrlr}), '/', ...
            num2str(cParm{cNum}),   '/', ...
            num2str(modelName{modelId}),'.mat'], 'res')
    this = getfield(res,strcat('SMITH',...
                    upper(ctrlrs{ctrlr}))) ;
    ctrl = this.minTune(this.Ms_span==Ms_s,:) ;
    % G = modelDist(G,d_s) ;
    % C = controller(ctrl,parm) ;
    % C = controller([4 0 0], parm) ;
    % Ms([4 0 0], parm)
    % Mt([4 0 0], parm)
    C = controller(ctrl, parm) ;
    Ms(ctrl, parm) 
    Mt(ctrl, parm)
    sys = G.tf*C.tf ;
    nyquist(sys) ;

    loc = strcat('results/tex/nyquist', ...
            num2str(cTypes{cType}), '/',...
            num2str(ctrlrs{ctrlr}), '/', ...
            num2str(cParm{cNum}),   '/', ...
            num2str(modelName{modelId}),'.tex') ;
end







% j = 1 ;                                   % Iteration counter
% for Ms_s = Ms_span 
%     parm.Ms_s = Ms_s ;
%     load([mDir, '/results/smith/',          ...
%             num2str(ctrlrs{ctrlr}), '/', ...
%             num2str(cParm{cNum}),   '/', ...
%             num2str(modelName{modelId}),'.mat'], 'res')
%     this = getfield(res,strcat('SMITH',...
%                     upper(ctrlrs{ctrlr}))) ;
%     ctrl = this.minTune(this.Ms_span==Ms_s,:) ;
%     i = 1 ;                               % Iteration counter
%     for d = d_span
%         parm.G     = modelDist(G,d) ;    % Disturbing process
%         parm.C     = controller(ctrl, parm) ;
%         Jtune(i,j) = costFun(ctrl, parm) ;
%         Msc(i,j)   = Ms(ctrl, parm) ;
%         Mtc(i,j)   = Mt(ctrl, parm) ;
%         if ctrlr == 1
%             fprintf('%.2f\t%.2f\t%.2f\t%.3f\t%.3f\t%.3f\t%.4f\n',...
%                 ctrl,Msc(i,j), Ms_s, d, Jtune(i,j))
%         else
%             fprintf('%.2f\t%.2f\t%.3f\t%.3f\t%.3f\t%.4f\n',...
%                 ctrl,Msc(i,j), Ms_s, d, Jtune(i,j))
%         end
%         i = i + 1 ;
%     end
%     j = j + 1 ;
% end
% calcTime = toc ;















