% Copyright 1997 Thomas Mc Avoy
% MATLAB Routine to configure the outer loops for the Tennessee Eastman
% plant
function [KCP,TRP,con,conlbl,conlbl1]=archit(kkk,KCP,TRP,con,conlbl,conlbl1)

chk=0;
disp(' ')
disp('NOTE THE G/H LOOP, AND PRODUCT FLOW LOOP SHOULD BE CLOSED FOR ')
disp('TESTING OUTER CONTROLLERS IN CLOSED LOOP')
disp(' ')
while chk==0
if con(1,1)~=0
disp('			CURRENT ARCHITECTURE')
[m,n]=size(con);
	if m>0
		for i=1:m
			s=sprintf('LOOP # %3.0f',i);
			disp([s,'  ',conlbl(i,:),conlbl1(i,:)])
		end
		disp(' ')
		disp('			CURRENT PI PARAMETERS')
		disp('			   KC          TR')
		for i=1:m
			s=sprintf('LOOP # %3.0f',i);
			s1=sprintf('   %6.3e',KCP(i));
			s2=sprintf('   %6.3f',TRP(i));
			disp([s,s1,s2])
		end
	end
end
disp(' ')
disp('ENTER LOOP # FOR WHICH YOU WANT TO CHANGE EITHER ARCHITECTURE OR PI PARAMETERS')
disp('	ENTER -1 TO BEGIN A NEW CONFIGURATION')
disp('	ENTER 0 WHEN FINISHED')
	count=input ('	');
	disp(' ')
	while (isempty(count)==1)
		disp(' ')
		disp('PLEASE ENTER LOOP AGAIN. ')
		count=input ('LOOP: ');
		disp(' ')
	end
	if count==0
		chk=1;
		break
	elseif count<0
		con=[0,0];
		conlbl1=zeros(1,16);
		conlbl=zeros(1,20);
		disp(' ')
		disp('BEGIN BY ENTERING INFORMATION FOR LOOP #1')
		count=1;
	end
disp(' ')
disp('THE FOLLOWING SET POINTS CAN BE USED FOR CONTROL') 
	disp(' ')
	disp('	VALVE-1: THE D/E RATIO SET POINT')
	disp('	VALVE-2: THE E-FEED FLOW SET POINT')
	disp('	VALVE-3: THE A-FEED FLOW SET POINT') 
	disp('	VALVE-4: THE C-FEED FLOW SET POINT')
	disp('	VALVE-6: THE PURGE FLOW SET POINT')
	disp('	VALVE-7: THE SEPARATOR EXIT FLOW SET POINT')
	disp('	VALVE-8: THE PRODUCT FLOW SET POINT')
	disp('	VALVE-9: THE STEAM FLOW SET POINT')
	disp('	VALVE-10: THE REACTOR CW TEMP SET POINT')
	disp('	VALVE-11: THE CONDENSER CW TEMP SET POINT')
	disp(' ')
check=0; 
while check==0
	disp(' ')
	disp('INPUT WHICH SET POINT TO MANIPULATE')
	k=input ('	INPUT SET POINT: ');
	disp(' ')
	while (isempty(k)==1)
		disp(' ')
		disp('PLEASE ENTER SETPOINT AGAIN. ')
		k=input ('SET POINT: ');
		disp(' ')
	end

	if k>11|k<1&k~=0
		disp('YOUR SELECTION IS OUTSIDE THE RANGE 1 TO 11')
		s=sprintf('YOU SELECTED SET POINT %3.0f',k);
		disp(s)
		disp(' ')
		disp('PLEASE START OVER')
	end
	if k==5|k==12

		s=sprintf('YOU SELECTED SET POINT %3.0f',k);
		disp(s)
		disp('THIS IS NOT A VALID CHOICE FOR THE SET POINT OF AN INNER CASCADE')
		disp(' ')
		disp('PLEASE START OVER')
	elseif k==kkk(1)|k==kkk(2)|k==kkk(3)
		s=sprintf('YOU SELECTED SETPOINT %3.0f',k);
		disp(s)
		disp('NOT A VALID CHOICE')

		disp('YOU HAVE ALREADY SPECIFIED THIS SET POINT FOR LEVEL CONTROL')
		disp(' ')
		disp('PLEASE START OVER')

	else
	check=1;
	end
end

		if k==1
		disp('THE D/E RATIO SET POINT WILL BE USED - MV#1')
		titl='D/E Ratio           ';
		elseif k==2
		disp('THE E-FEED SET POINT WILL BE USED - MV#2')
		titl='E-Feed Flow         ';
		elseif k==3
		disp('THE A-FEED SET POINT WILL BE USED - MV#3')
		titl='A-Feed Flow         ';
		elseif k==4
		disp('THE C-FEED SET POINT WILL BE USED - MV#4')
		titl='C-Feed Flow         ';
		elseif k==6
		disp('THE PURGE SET POINT WILL BE USED - MV#6')
		titl='Purge Flow          ';
		elseif k==7
		disp('THE SEPARATOR UNDERFLOW SET POINT WILL BE USED - MV#7')
		titl='Separator Exit Flow ';
		elseif k==8
		disp('THE PRODUCT SET POINT WILL BE USED - MV#8')
		titl='Product Flow        ';
		elseif k==9
		disp('THE STEAM SET POINT WILL BE USED - MV#9')
		titl='Steam Flow          ';
		elseif k==10
		titl='Reactor CW Temp     ';
		disp('THE REACTOR COOLING WATER TEMP SET POINT WILL BE USED - MV#10')
		elseif k==11
		disp('THE CONDENSER COOLING WATER SET POINT WILL BE USED - MV#11')
		titl='Condenser CW Temp   ';
		end
		con(count,1)=k;
		conlbl(count,:)=titl;
		disp(' ')
		[Glabel]=gainlbl([2 7 8]);% [2 7 8] is a dummy variable
		disp('THE FOLLOWING VARIABLES CAN BE CONTROLLED') 
		bl='                    ';
		MEASUREMENTS=[Glabel(2:22,1:22),[Glabel(23:42,1:20);bl]]
		s='42   G/H In Product     ';
		s1='43   A/C In Recator Feed';
		s2='44   D/E In Recator Feed';
		RATIOS=[s;s1;s2]
		disp(' ')
		while 1
			disp('INPUT NUMBER OF MEASUREMENT TO BE CONTROLLED')
			temp=input(' ');
			while (isempty(temp)==1)|temp<1|temp>44
				disp('PLEASE ENTER MEASUREMENT NUMBER AGAIN BETWEEN 1&44')
				temp=input(' ');
			end
			con(count,2)=temp;
			if temp<42
				conlbl1(count,:)=(Glabel(temp+1,5:20));
			elseif temp==42
				conlbl1(count,:)=' G/H In Product ';
			elseif temp==43
				conlbl1(count,:)=' A/C React Feed ';
			elseif temp==44
				conlbl1(count,:)=' D/E React Feed ';
			end
			disp(' ')
			disp(sprintf('			LOOP #%3.0f',count))
			ttt=[conlbl(count,:),conlbl1(count,:)];
			ttt=setstr(ttt);
			disp(ttt)
			disp(' ')
% Get Tuning Constants Now
			disp(' ')
			s=sprintf('INPUT THE CONTROLLER GAIN FOR LOOP %3.0f',count);
			disp(s)
			temp=input(' ');
			while (isempty(temp)==1)
				disp('PLEASE ENTER KC AGAIN')
				disp(s)
				temp=input(' ');
			end
			KCP(count)=temp;
			disp(' ')
			s=sprintf('INPUT THE RESET TIME (min) FOR LOOP %3.0f',count);
			disp(s)
			temp=input(' ');
			while (isempty(temp)==1)
				disp('PLEASE ENTER TR AGAIN')
				disp(s)
				temp=input(' ');
			end
			TRP(count)=temp;
			break
		end
	end

% display final scheme
[m,n]=size(con);
disp('			FINAL ARCHITECTURE')
for i=1:m
	s=sprintf('LOOP # %3.0f',i);
	disp([s,'  ',conlbl(i,:),conlbl1(i,:)])
end
disp(' ')
disp('			OUTER PI PARAMETERS')
disp('			   KC          TR')
for i=1:m
	s=sprintf('LOOP # %3.0f',i);
	s1=sprintf('   %6.3e',KCP(i));
	s2=sprintf('   %6.3f',TRP(i));
	disp([s,s1,s2])
end
disp(' ')
% check for multiple use of valves or measurements
[n,m]=size(con);
if n>1
	tem=sort(con);
	tem1=tem(2:n,:);
	if all(tem(1:n-1,1)-tem1(:,1))==0
	disp('WARNING - YOU HAVE USED THE SAME VALVE TWICE. PLEASE START OVER')
	end
	if all(tem(1:n-1,2)-tem1(:,2))==0
	disp('WARNING - YOU HAVE USED THE SAME MEASUREMENT TWICE. PLEASE START OVER')
	end
end
		

