%
% RTD Simulator - Edit an Object Definition
%
% Bob Newell, January 1996
%
function [newobjects, change] = rtdedit( objects )
[nobjects, n] = size( objects );
%
% what to do ?
%
change = 0;
action = 0;
while action < 4,
  clc;
  disp( 'Edit Objects Menu' );
  disp( '-----------------' );
  disp( '1. Add objects' );
  disp( '2. Remove object' );
  disp( '3. Change object' );
  disp( '4. Finished' );
  action = input( '? ' );
%
  if action == 4,
    break;
  elseif action == 1,
%
% proceed to ask for new objects
%
    i = nobjects + 1;
    type = input( 'Object (2=well-mixed,3=plug-flow,4=mixing-tee,5=sink,6=end) > ' );
    while type < 6,
      if type == 2,
        objects = [ objects ; [type 0 0 0 0 0 0 0 0 0 0 1] ];
        disp( ['Object ' num2str(i) ': Well-Mixed Tank'] );
        objects(i,2) = input( '  Enter vessel volume (Ml) > ' );
        change = 1;
      elseif type == 3,
        objects = [ objects ; [type 0 0 0 0 0 0 0 0 0 0 1] ];
        disp( ['Object ' num2str(i) ': Plug-Flow Tank'] );
        objects(i,2) = input( '  Enter vessel volume (Ml) > ' );
        objects(i,3) = input( '  Enter dispersion constant (0-1) > ' );
        if objects(i,3) < 0, objects(i,3) = 0; end;
        if objects(i,3) > 1, objects(i,3) = 1; end;
        change = 1;
      elseif type == 4,
        objects = [ objects ; [type 0 0 0 0 0 0 0 0 0 0 1] ];
        disp( ['Object ' num2str(i) ': Mixing Tee'] );
        change = 1;
      elseif type == 5,
        objects = [ objects ; [type 0 0 0 0 0 0 0 0 0 0 0] ];
        disp( ['Object ' num2str(i) ': Sink'] );
        change = 1;
      end;
%
% get connectivity and flowrates
%
      if type < 5,
        for j = 2:5,
          objects(i,2*j) = input( ['  Output ' ...
                    num2str(j-1) ' to object (0=end) > '] );
          if objects(i,2*j) == 0,
            break;
          end;
          objects(i,2*j+1) = input( '  Flowrate (Ml/d) > ' );
        end;
	  end;
%
      i = i + 1;
      type = input( 'Object (2=well-mixed,3=plug-flow,4=mixing-tee,5=end) > ' );
    end;
    [nobjects, n] = size( objects );
%
  else
%
% find out which object
%
    disp( 'Existing objects:' );
    for i = 1:nobjects,
      text = [ '  ' num2str(i) '. ' ]; 
      if objects(i,1) == 1,
        text = [ text 'Tracer Injection' ];
      elseif objects(i,1) == 2,
        text = [ text 'Well-Mixed Tank' ];
      elseif objects(i,1) == 3,
        text = [ text 'Plug-Flow Tank' ];
      elseif objects(i,1) == 4,
        text = [ text 'Mixing Tee' ];
      elseif objects(i,1) == 5,
        text = [ text 'Sink' ];
      end;
      disp( text );
    end;
    i = input( 'Object number > ' );
%
    if action == 2,
%
% remove the object, cannot remove pulse object
%
      if i > 1,
        if i <= nobjects,
          objects(i,:) = [];
          disp( ['Object ' num2str(i) ' removed'] );
          nobjects = nobjects - 1;
          change = 1;
        end;
      end;
%
    elseif action == 3,
%
% change object parameter
%
      if objects(i,1) == 1,
        disp( ['Object ' num2str(i) ': Tracer Injection'] );
        newvalue = input( ['  Type (0=impulse,1=pulse,2=step) [' ...
                 num2str(objects(i,2)) '] > '] );
        if length(newvalue) > 0,
          objects(i,2) = newvalue;
          change = 1;
        end;
        newvalue = input( ['  Enter tracer amount (height) [' ...
                 num2str(objects(i,3)) '] > '] );
        if length(newvalue) > 0,
          objects(i,3) = newvalue;
          change = 1;
        end;
      elseif objects(i,1) == 2,
        disp( ['Object ' num2str(i) ': Well-Mixed Tank'] );
        newvalue = input( ['  Enter vessel volume (Ml) [' ...
                 num2str(objects(i,2)) '] > '] );
        if length(newvalue) > 0,
          objects(i,2) = newvalue;
          change = 1;
        end;
      elseif objects(i,1) == 3,
        disp( ['Object ' num2str(i) ': Plug-Flow Tank'] );
        newvalue = input( ['  Enter vessel volume (Ml) > [' ...
                 num2str(objects(i,2)) '] > '] );
        if length(newvalue) > 0,
          objects(i,2) = newvalue;
          change = 1;
        end;
        newvalue = input( ['  Enter dispersion constant > [' ...
                 num2str(objects(i,3)) '] > '] );
        if length(newvalue) > 0,
          objects(i,3) = newvalue;
          if objects(i,3) < 0, objects(i,3) = 0; end;
          if objects(i,3) > 1, objects(i,3) = 1; end;
          change = 1;
        end;
      end;
%
% change connectivity and flowrates
%
      if objects(i,1) < 5,
        for j = 2:5,
          newvalue = input( ['  Output ' ...
                 num2str(j-1) ' to object [' ...
                 num2str(objects(i,2*j)) '] > '] );
          if length(newvalue) > 0,
            objects(i,2*j) = newvalue;
            change = 1;
          end;
          if objects(i,2*j) > 0,
            newvalue = input( ['  Output ' ...
                 num2str(j-1) ' Flowrate (Ml) [' ...
                 num2str(objects(i,2*j+1)) '] > '] );
            if length(newvalue) > 0,
              objects(i,2*j+1) = newvalue;
              change = 1;
            end;
          end;
		end;
      end;
%
    end;
  end;
%
end;
newobjects = objects;
%
return;
