#! /bin/sh

     ###################################### 
     ##### Model Transformation Tools #####
     ######################################

# Bourne shell script: rbg2abg_m
#
###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.8  1998/02/19 08:57:02  peterg
## Fixed mtt-info bug -- confused filename with number
##
## Revision 1.7  1997/08/05 08:38:23  peterg
## Added n-ports to the argument list in the comment.
##
## Revision 1.6  1996/12/04 21:52:01  peterg
## Uses filenum and fopen instead of filename.
##
## Revision 1.5  1996/08/25 09:20:32  peter
## General error handling.
##
## Revision 1.4  1996/08/24 17:57:41  peter
## Removed `touch mtt_info.txt'
##
## Revision 1.3  1996/08/24 14:58:56  peter
## Fixed missprint - mtt_err --> mtt_error
##
## Revision 1.2  1996/08/24 14:33:07  peter
## Error handling included.
##
## Revision 1.1  1996/08/05 12:22:16  peter
## Initial revision
##
###############################################################

# Raw bond graph to structured acausal bond graph: mfile format
# The structured BG is described by four matrices:
#   junctions: describes the junctions -
#     Column 1   Index of component (see components)
#     Column 2.. Indices of bonds on junction (see jbonds)
#                  rows may be padded with zeros
#
#   jbonds:    describes the true bonds -
#     Column 1     Index of component (see components)
#     Column 2     Direction
#                    1 away from junction
#                   -1 towards junction
#     Column 3     Causality 
#                     1 stroke at arrow end
#                    -1 stroke at other end
#                     0 no stroke (ie acausal)
#     Columns 4:5  x,y coordinates of non-arrow end (in Fig)
#     Columns 6:7  x,y coordinates of arrow end (in Fig)
#
#   mbonds:    describes the modulation bonds -
#     Column 1     Index of component at non-arrow end
#     Column 2     Index of component at arrow end
#     Columns 4:5  x,y coordinates of non-arrow end (in Fig)
#     Columns 6:7  x,y coordinates of arrow end (in Fig)
#
#   components: describes the comonents -
#     Column 1     Type of component
#                    0  0-junction
#                    1  1-junction
#                    2  R component
#                    3  C component
#                    4  I component
#                    11 TF component
#                    12 GY component
#                    13 A  component
#     Column 2     Flag for explicit junction
#                    4  Explicit component (4 means text type)
#                    0  Implicit component
#     Column 3-14    fig file information (see fig documentation)
#
# P.J.Gawthrop May 1996
# Copyright (c) P.J.Gawthrop, 1996.

while [ -n "`echo $1 | grep '^-'`" ]; do
  case $1 in
	-I )
                info=info;;
	*)
		echo "$1 is an invalid argument - ignoring" ;;
  esac
  shift
done



# Remove the old log file
rm -f rbg2abg_m.log
rm -f $1_abg.m

#Inform user
echo Creating $1_abg.m

# Use matrix manipulation to accomplish the transformation
$MATRIX > rbg2abg_m.log  2>mtt_error.txt << EOF
  name = '$1'
  infofile = 'mtt_info.txt';

  %Convert from the fig version of the bonds to a structured version
  [rbonds,rstrokes,rcomponents,port_coord,port_name,port_list] = $1_rbg;
  [n_ports, junk] = size(port_list);
 
  [bonds,components] = rbg2abg(name,rbonds,rstrokes,rcomponents,port_coord,port_name,infofile);

  %Write the function m-file for the causal bond graph
  filename = '$1_abg.m';
  filenum = fopen(filename,'w');
  c = '%';
  fprintf(filenum, 'function [bonds,components,n_ports] = $1_abg\n');
  fprintf(filenum, '%s [bonds,components,n_ports] = $1_abg\n', c);
  fprintf(filenum, '%s Acausal bond graph created by MTT on %s\n\n', ...
                     c, date);
  mat2mfile(bonds, 'bonds', filenum);
  mat2mfile(components, 'components', filenum);
  fprintf(filenum, 'n_ports = %1.0f;\n', n_ports);

EOF

if [ "$info" = "info" ]; then
  cat mtt_info.txt    
fi

mtt_error mtt_error.txt






