% ***************************************************************************
% COPYRIGHT BY DAVE JEWELL AND T.E. MARLIN 1994
% FILNAME : RUNSIM.M
% Version 2.0
% LAST UPDATED : December 1994
% DISCRIPTION OF FILE :
% THIS M-FILE IS ACCESSED FROM "twobytwo.m" ALLOWS THE USER TO ENTER THE 
% OPTIONS FOR RUNNING THE SIMULAION
% ***************************************************************************


SStime=max([max(max(DT)) max(DDT) max(max(DT_m))]); % INTIAL STEADY-STATE TIME FOR SIMULATION
nstart=round(SStime/delt)+3;                    % START OF SIMULATION
nmax=round(tend/delt)+nstart;                   % END OF SIMULATION
if nmax > maxstep
    nmax=maxstep;                               % SETS NUMBER OF ITERATIONS
    delt=(SStime+tend)/(maxstep-3);             % CALCULATES STEP SIZE
    nstart=round(SStime/delt)+3;                % START OF SIMULATION
end                                             % WHILE

while 1
clc;
disp('**********************************************************************')
disp('*                                                                    *')
disp('*                         SIMULATION OPTIONS                         *')
disp('*                                                                    *')
disp('**********************************************************************')
disp('WHAT PARAMETER WOULD YOU LIKE TO UPDATE ?')
disp('                                          Present Values')
fprintf('1) Total Simulation Time                       %-g\n',tend)
fprintf('2) Step Size                                   %-2.2f\n',delt)
if conalgo ==1
   fprintf('3) Select Control Algorithm                    PID\n');
   else
   fprintf('3) Select Control Algorithm                    DMC\n');
end
disp('4) Run Simulation WITH Disturbance')
disp('5) Run Simulation WITHOUT Disturbance')
disp('6) View Plots')
disp('7) Save Plots To A Metafile')
disp('8) Previous Menu')
mm = input('PLEASE ENTER A SELECTION : ');
if isempty(mm)==1
    break
end
mm = round(mm);                          % round off selection
if (mm<1) | (mm>7)
    disp('')                       % selection validation
    disp('NOT A VALID SELECTION')
    disp('PRESS ENTER TO CONTINUE')
    pause;
end


% A SERIES OF PROMPTS FOR OPTIONS
if mm == 1
TEMP=input('Duration : ');

if isempty(TEMP) ~= 1
    tend=TEMP;
SStime=max([max(max(DT)) max(DDT) max(max(DT_m))]); % INTIAL STEADY-STATE TIME FOR SIMULATION
nstart=round(SStime/delt)+3;                    % START OF SIMULATION
nmax=round(tend/delt)+nstart;                   % END OF SIMULATION
if nmax > maxstep
    nmax=maxstep;                               % SETS NUMBER OF ITERATIONS
    fprintf('The Maximum Iteration Limit Of %-g Has Been Violated.\n',maxstep)
    disp('The Correct Step Size Will Be Calculated')
    pause(2);
    delt=(SStime+tend)/(maxstep-3);             % CALCULATES STEP SIZE
    nstart=round(SStime/delt)+3;                % START OF SIMULATION
end                                             % IF
end                                             % IF

elseif mm == 2
TEMP=input('Time step size : ');
dmccalc = 0;                       %DMC must be redesigned if time step changed
if isempty(TEMP) == 1
   TEMP = delt;
end
if isempty(TEMP) ~= 1
     delt=TEMP;
	if delt<=0
	  disp('Step Size CANNOT be less than 0, Press enter to continue')
	  delt=1;
	  pause;
	end
SStime=max([max(max(DT)) max(DDT) max(max(DT_m))]); % INTIAL STEADY-STATE TIME FOR SIMULATION
nstart=round(SStime/delt)+3;                    % START OF SIMULATION
nmax=round(tend/delt)+nstart;                   % END OF SIMULATION
if nmax > maxstep
    nmax=maxstep;                               % SETS NUMBER OF ITERATIONS
    fprintf('The Maximum Iteration Limit Of %-g Has Been Violated.\n',maxstep)
    disp('The Correct Step Size Will Be Calculated')
    pause(2);
    delt=(SStime+tend)/(maxstep-3);             % CALCULATES STEP SIZE
    nstart=round(SStime/delt)+3;                % START OF SIMULATION
end                                             % IF
end                                             % IF

elseif mm == 3                % SELECT THE CONTROLLER ALGORITHM
 
    holdcon = conalgo;
    disp('  ');
    disp(' PID = 1  ; DMC = 2 ');  disp('  ');
    conxxx = input ('PLEASE ENTER SELECTION : ') ;
    conyyy = round (conxxx);
    conalgo = conyyy;
    if conyyy<1 | conyyy>2
       conalgo = holdcon;
       disp('  ');
       disp (' NOT VALID SELECTION');
       disp (' PRESS ENTER TO CONTINUE');
       pause
    end

elseif mm == 4                         % RUN W/O DISTURBANCE
    if dmccalc == 0 & conalgo == 2
       disp('  ');
       disp('  DMC NOT DESIGNED OR TIME STEP CHANGED SINCE LAST DESIGN');
       disp('  MUST DESIGN DMC CONTROLLER BEFORE SIMULATION ');
       disp('  PRESS ENTER TO CONTINUE ')
       pause
    else
       DISTYN = 1;
       sim_2by2;
    end
	
elseif mm == 5                          % RUN WITH DISTURBANCE
    if dmccalc == 0 & conalgo == 2
       disp('  ');        
       disp('  DMC NOT DESIGNED OR TIME STEP CHANGED SINCE LAST DESIGN'); 
       disp('  MUST DESIGN DMC CONTROLLER BEFORE SIMULATION ');
       disp('  PRESS ENTER TO CONTINUE ')
       pause
    else
       DISTYN = 0;
       sim_2by2;
    end 

elseif mm == 6                           % DISPLAY PLOT
    if EXECUTION == 1
	graph;
    else
	disp('You Must Run A Simulation Before You Can View The Plots')
	pause(3);
    end

elseif mm == 7                          % SAVE META FILE
     disp('This option not available ')   
     pause (2);

%    graphname=input('The name for the metafile (ex. a:filename) - no extension : ','s');
%if (isempty(graphname)~=1)
%        disp('')
%        disp('                  PRINTING GRAPHS AND SAVING TO A FILE');
%        pause(2);
%        SAVEGRAPH=1;
%        graph;
%end

elseif mm == 8
    return
end     % if
end     % while
