% RESIDENCE TIME DISTRIBUTION SIMULATOR
%
% The simulator will calculate simple tracer responses
% to an impulse, pulse or step in tracer concentration
% at the inlet.  Most flowsheets can be configured from
% a combination of instances of four types of object:
% tracer-injection, well-mixed-tank, plug-flow-tank 
% (with adjustable dispersion) and mixing-tee.
%
% Each object can direct up to four flows to other objects
% allowing for flowsheets with parallel objects, bypasses 
% and recycles.
%
% The output concentration of each object is plotted
% and the plot can be tailored by adjusting a scaling
% attribute for each object (a zero scaling removes
% the object from the plot).
%
% The flowsheet definition can be loaded from and/or
% saved to a file with an "rtd" extension.  The simulation
% results can be saved to a file with a "dat" extension.
% They are saved in ACSII format for compatibility
% with other software.
%
% A compare function allows plotting against a file of
% external data with a "dat" extension (time then tracer
% concentration data in columns).
%
% See also help on RTDREAD, RTDSAVE, RTDCOMP

%------------------ above is help info -------------------
%
% RTD Simulator - Main Program
%
% Bob Newell, January 1996
%
% Find some objects definition
%
[objects, fname] = rtdread;
change = 0;
results = [];
[nobjects, n] = size( objects );
if nobjects == 0,
  objects = rtdentry;
  [nobjects, n] = size( objects );
  change = 1;
end;
if nobjects > 0,
%
% Ask for required action
%
  action = 0;
  while action < 8,
    clc;
    disp( 'RTD Program Main Menu' );
    disp( '---------------------' );
    disp( '1. New Flowsheet' );
    disp( '2. Edit Flowsheet' );
    disp( '3. Display Objects' );
    disp( '4. Calculate RTD' );
    disp( '5. Replot RTD' );
    disp( '6. Save RTD result' );
    disp( '7. Compare RTDs' );
    disp( '8. Quit' );
    action = input( '? ' );
    while length(action) == 0,
      action = input( '? ' );
    end;
    if action == 1,
%
% set up a new flowsheet
%
      objects = rtdentry;
      [nobjects, n] = size( objects );
      change = 1;
%
    elseif action == 2,
%
% edit an existing flowsheet
%
      [objects, modified] = rtdedit( objects );
      [nobjects, n] = size( objects );
      if modified > 0,
        change = 1;
      end;
%
    elseif action == 3,
%
% just display what object data we have
%
      rtddisp( fname, objects );
%
    elseif action == 4,
%
% do the calculations and plot the result
%
      if rtdmbal( objects ) > 0,
        results = rtdcalc( objects );
        [ objects modified ] = rtdplot( fname, results, objects );
        if modified > 0,
           change = 1;
        end;
	  end;
%
    elseif action == 5,
%
% do a replot of the result
%
      if length(results) > 0,
        [ objects modified ] = rtdplot( fname, results, objects );
        if modified > 0,
          change = 1;
        end;
      end;
%
    elseif action == 6,
%
% save the RTD results to a file
%
      if length(results) > 0,
        rtdsave( fname, results );
      end;
%
    elseif action == 7,
%
% compare 
%
      if length(results) > 0,
        rtdcomp( fname, results, objects );
      end;
%
    end;
  end;
%
% quiting the program, check save objects first
%
  if change > 0,
    n = input( 'Save Flowsheet (0=no,1=yes) > ' );
    if n > 0,
      rtdwrite( objects, fname );
    end;
  end;
end;
