#! /bin/sh

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

# Bourne shell script: abg2cbg_m
#
# Acausal bond graph to causal bond graph: mfile format

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.15  2001/07/23 23:24:02  gawthrop
## Now only writes to type.sh and cbg.m when causality is complete
##
## Revision 1.14  2000/12/28 12:16:13  peterg
## *** empty log message ***
##
## Revision 1.13  1998/07/27 20:30:12  peterg
## *** empty log message ***
##
## Revision 1.12  1998/07/10 08:43:46  peterg
## Added -I switch
##
## Revision 1.11  1998/01/23 13:29:54  peterg
## $RMATRIX --> $MATRIX
##
# Revision 1.10  1996/12/04  21:51:02  peterg
# Now uses filenum instead of line name.
# Uses fopen
#
# Revision 1.9  1996/11/06  15:49:33  peterg
# Cahged $MATRIX to $RMATRIX
#
# Revision 1.8  1996/08/25  09:31:49  peter
# More error handling.
#
## Revision 1.7  1996/08/25 09:22:55  peter
## # Print errors, if any.
## mtt_error error.txt
##
## Revision 1.6  1996/08/24 14:36:01  peter
## Error handling included.
##
## Revision 1.5  1996/08/19 15:38:31  peter
## Removed bug work round.
##
## Revision 1.4  1996/08/16 14:28:45  peter
## Some debugging lines removed.
##
## Revision 1.3  1996/08/08 18:06:55  peter
## Unified naming scheme.
##
## Revision 1.2  1996/08/05 11:25:00  peter
## Removed bonds argument from abg2cbg.
## Removed call to $1_abg.
##
## Revision 1.1  1996/08/04 17:45:11  peter
## Initial revision
##
###############################################################

# The causal BG is described by four matrices
# (the same structure as the acausal BG except that Causality is 1 ot -1):
#   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
#
#    cjbonds:    describes the true bonds -
#     Column 1     Index of component (see components)
#     Column 2     Direction
#                    1 away from junction
#                   -1 towards junction
#     Column 3    Effort causality 
#                     1 stroke at arrow end
#                    -1 stroke at other end
#                     0 no stroke (ie acausal)
#     Column 4    Flow causality 
#                     1 stroke at arrow end
#                    -1 stroke at other end
#                     0 no stroke (ie acausal)
#     Columns 5:6  x,y coordinates of non-arrow end (in Fig)
#     Columns 7:8  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)
#
# Lists of under- and over-causal junctions are displayed.
#
# P.J.Gawthrop May 1996
# Copyright (c) P.J.Gawthrop, 1996.


# P.J.Gawthrop May 1996
# Copyright (c) P.J.Gawthrop, 1996.

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

infofile='mtt_info.txt'
errorfile='mtt_error.txt'
typefile="$1_type.sh"

# Remove the old log file
rm -f abg2cbg_m.log
rm -f $1_cbg.m
rm -f $typefile
rm -f $infofile


#Inform user
echo "Creating $1_cbg.m (maximise $blurb causality)"
echo Creating $1_type.sh

## Start the type.sh file
mtt_header $1 type sh > $typefile

# Use matrix manipulation to accomplish the transformation
$MATRIX << EOF  >abg2cbg_m.log  #2>mtt_error.txt

  infofilenum  = fopen("$infofile",'w');
  errorfilenum = fopen("$errorfile",'w');
  typefilenum  = fopen("$typefile",'w');

  %Convert from acausal to causal bond graph in m-file form.
  system_name='$1';

  port_bonds = [];
  port_direction = [];
  port_status = [];
  [cbonds,status] = abg2cbg(system_name, '', '', port_bonds, port_direction, port_status, ...
      $derivative,typefilenum, infofilenum, errorfilenum);

    ## close the files
    fclose(infofilenum);
    fclose(errorfilenum);
    fclose(typefilenum);

EOF



if [ "$info" = "info" ]; then
  cat $infofile
fi

# cp $1_$1_cbg.m $1_cbg.m 2>> mtt_error.txt 

# Print errors, if any.
mtt_error mtt_error.txt

