% Script will load data structure specified for either mass balances alone
% or for mass and energy balances.  It will then put the required data
% in arrays for use by the modules.  If desired it will also return into 
% a .mat file all the data for the compounds that were entered so the same
% simulation may be repeated.
clear all
disp('Copyright 1999 Rice University')
disp('All rights reserved')
disp(' ')

disp('Welcome to CENG301''s start301!!');
disp('You have three choices, Please read them carefully');
disp('Then click on the appropriate choice in the menu bar');
disp(' ');
disp('1: Click (1) to start a new session');
disp('2: Click (2) to enter the name of a datafile from')
disp('   a previous session, in the current directory');
disp('3: Click (3) to use compound names not in the CENG 301 database')
disp('   WARNING...if you hit (3) no compound data will be available');


pick = menu('Choose one of the following:','(1) New Session', '(2) Continue Existing Session','(3) Other'); 

global nc;
if pick == 3                % start if #1

 global cnms;
 disp('You have chosen to not use any compound data')
 disp(' ')
 disp('If you wish to enter data for a compound, exit (control-C)')
 disp('then execute "create", then run a "new session", choice (1)')
 disp(' ')
 nc = input ('Enter the number of compounds: ');
for f = 1:nc                %start for #1

  disp('Compound names must be less than 25 characters long!')
  cnm = input(['Give the name of compound # ',num2str(f),': '],'s');
  cnm1(f,:) = cext(cnm, 26);  
end                         % end for #1
cnms = deblank(cnm1);
disp(' ')
global nst;
nst = input('Enter the number of streams: ');
if nst>0 
  global ns;
  ns=zeros(nst,nc);
 else 
  clear nst
end
clear cnm cnm1 f 

elseif pick == 2           % case two of if #1

x = input ('Enter the name of the existing file: ','s');
   load (x)
  disp(' ')
  disp('Here are your compounds'' formulae and names:')
      [nr,nx]=size(form);
      shortf=cext('Formula',nx);
      len = length(shortf);
      disp(['No. ',shortf,'  Name'])
      disp('----------------------------------------')
      for k=1:nr            % start for #2
         fprintf(['%3.0f',' ',cext(form(k,:),len),'  ',cnms(k,:),'\n'],k)
      end                   % end for #2
      clear k nx nr shortf len;
  disp(' ')
      disp('Here are your reactions:')
      disp('----------------------------------------')
      if cfs == 0           % start if #2
       disp('No reactions given')
      else
      sizes=size(coef);
      for o=1:sizes(1)      % start for #3
         fprintf(['%3.0f)  ', chemeq(coef(o,:),form),'\n'],o)
      end                   % end for #3
      end                   % end if #2
  clear o p sizes cfs;
  disp(' ')
  disp (['The number of streams is: ',num2str(nst)])



elseif  pick == 1;             % case three of if #1
disp (' ')
disp ('Now choose from the above menu') 
pick2 = menu('Choose one of the following:', 'Mass Balances only','Energy & Mass Balances');
 if pick2 == 1;              % start if #3
   pick4 = menu('Select a Database','CENG 301`s','Your Own');
   if pick4 == 1;
   disp('Please wait while loading database...');
   load struct3
   elseif pick4 == 2
   base=input('Enter the name of your database: ','s');
   load (base)
   end 
 else 
   pick4 = menu('Select a Database','CENG 301`s','Your Own');
   if pick4 == 1;
   disp('Please wait while loading database...');
   load struct4
   elseif pick4 == 2
   base=input('Enter the name of your database: ','s');
   load (base)
   end 

disp(' ')

global Tdeg
disp('Which units do you want to use for temperature?')
disp('Choose one from the menu')
disp(' ')
pick3 = menu('Choose one of the following','Kelvin','Fahrenheit','Celsius','Rankine');
if pick3 == 1;
 Tdeg = 'K';
elseif pick3 == 2;
 Tdeg = 'F';
elseif pick3 == 3;
 Tdeg = 'C';
elseif pick3 == 4;
 Tdeg = 'R';
end

end                         % end if #3

clear pick4 base 

disp(' ')

file_name = input('Input the name for a new file or just hit a return: ','s'); 
if length(file_name)>0  
   file = fopen(file_name,'r+');
   disp(['The output file name is: ', file_name]);
   disp(' ')
end
num_comp = input('Input the number of compounds: ');
disp(['The number of compounds is: ', num2str(num_comp)]);
disp(' ')

for i=1:num_comp            % start for #4
  comp{i} = input(['Enter the name of compound # ', num2str(i) ': '],'s');
    for j = 1:length(stdat2);    %start for #5
      C = strcmp(deblank(comp{i}),deblank(stdat2(j).cnms));
        if C == 1                % start if #4
          X(i) = stdat2(j);
          break
	  else 
      
	  D = strcmp(deblank(comp{i}),deblank(stdat2(j).form));
           if D == 1             % start if #5
           X(i) = stdat2(j);
           break
           end                   % end if #5
         end                     % end if #4
     end                         % end for #5
  while C~=1 & D~=1              % start while #1
   disp(' ')
   disp('There is no data file for that compound.')
   disp('If you misspelled the compound, enter the name again')
   disp(' ')
   disp('Otherwise exit start301 (control-C)')
   disp('Then you can use option (3) to use no data,')
   disp('or execute "create" to create a data file for')
   disp('that compound then execute start301 choice (1) again')
   
   disp(' ')
    comp{i} = input(['Re-enter the name of compound # ', num2str(i) ': '],'s');
      for j = 1:length(stdat2);  % start for #6
        C = strcmp(deblank(comp{i}),deblank(stdat2(j).cnms));
         if C == 1               % start if #6
          X(i) = stdat2(j);
          break
	  else 
      
	  D = strcmp(deblank(comp{i}),deblank(stdat2(j).form));
          if D == 1              % start if #7
          X(i) = stdat2(j);
          break
          end                    % end if #7
         end                     % end if #6
       end                       % end for #6
   end                           % end while #1
end                              % end for #4

a = mkvars(X);
eval(a);

z = cellstr(form); 
  [yr,yc] = size(z);
  nms = ' ';
   for y = 1:yr                  % start for #7
    nms = [nms, z{y,1},'  '];
   end                           % end for #7


cfs=input ('Enter the number of reactions: ');
 disp(' ')                       
 for p=1:cfs                     % start for #8
  if cfs==0                      % start if #8
    break
  else                           % case two of if #8
  disp('Enter the coefficients for each compound in the same order that')
  disp('the compounds are listed.  Coefficients for reactants should be')
  disp('Negative, and coefficients for products should be positive')
  disp(['Enter the coefficients for each compound in reaction # ', num2str(p)])

   q=input([nms,'\n'],'s');
 coef(p,:)=eval(['[',q,']']);
 end                             %end if #8
end                          %end for #8
disp(' ')

if cfs > 0

disp('Do you want to check to see if the coefficients are correct?')
balc=input('type "y" for yes or simply press enter to move on: ','s');
c1='y';
c2=strcmp(balc,c1);
if c2 == 1;
 disp(['number of compounds = ',num2str(num_comp)])
 disp(['number of reactions = ',num2str(cfs)])
 disp('Here are your formulae...')
 disp(num2str(form))
 !balance
else
end
disp(' ')
disp('Your reactions are currently as follows: ')

sizes=size(coef);
for o=1:sizes(1)
fprintf(['%3.0f)  ', chemeq(coef(o,:),form),'\n'],o)
end
disp(' ')

change=input('Type "y" to change an equation, or press "enter" to continue: ','s');
c3=strcmp(change,c1);
if c3 == 1;
 numch=input('How many equations do you want to change: ');
  for w=1:numch
  which=input('Enter the equation number you wish to change: ');
  disp(' ')
  disp('Enter the coefficients for each compound in the same order that')
  disp('the compounds are listed.  Coefficients for reactants should be')
  disp('Negative, and coefficients for products should be positive')
  disp(['Enter the coefficients for each compound in reaction # ', num2str(which)])
  r=input([nms,'\n'],'s');
 coef(which,:)=eval(['[',r,']']);
 end
    disp(' ')
    disp('Do you want to check to see if the coefficients are correct?')
    balc=input('type "y" for yes or simply press enter to move on: ','s');
     c1='y';
     c2=strcmp(balc,c1);
      if c2 == 1;
       disp(['number of compounds = ',num2str(num_comp)])
       disp(['number of reactions = ',num2str(cfs)])
       disp('Here are your formulae...')
       disp(num2str(form))
        !balance
    else
    end
else 
end
clear balc w numch which c1 c2 c3 r change;                          
else
end

disp(' ')
disp('Here are your compounds'' formulae and names:')
     [nr,nx]=size(form);
      shortf=cext('Formula',nx);
      len = length(shortf);
      disp(['No. ',shortf,'  Name'])
      disp('----------------------------------------')
      for k=1:num_comp
         fprintf(['%3.0f',' ',cext(form(k,:),len),'  ',cnms(k,:),'\n'],k)
      end
      clear k nx nr shortf len;
disp(' ')


disp('Here are your reactions:')
disp('----------------------------------------')

if cfs == 0
 disp('No reactions given')
else
global stoic;
stoic = coef;

sizes=size(coef);
for o=1:sizes(1)
fprintf(['%3.0f)  ', chemeq(coef(o,:),form),'\n'],o)
end
end
 
clear  yc z yr y q o p ;
clear stdat2 sizes i j file comp;
clear X C a D ans nms; 

disp(' ')


nc = num_comp;
global nst;
nst = input('Enter the number of streams: ');
if nst>0; 
 if pick2 == 1;
  global ns;
  ns=zeros(nst,nc);
 else pick2 == 2; 
  global ns ne;
  ns=zeros(nst,nc);
  ne=zeros(nst,nc+3);
  end
else
end 
disp(' ');

clear num_comp pick2 pick3 ans;
disp(['The variables for your compounds have now been created,'])
if length(file_name)>0
    save(file_name);
    disp(['you may continue, or come back later and reload the same data.'])
end
end

clear b x c d e coef cfs file_name pick;














