#! /bin/sh

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

# Bourne shell script: dae_r2tex
# Reduce differential-algebraic to LaTex differential-algebraic equations.
# P.J.Gawthrop 10th May 1991, April 1994
# Copyright (c) P.J.Gawthrop, 1991, 1994.

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.4  1998/01/23 16:08:09  peterg
## changed eqnarray to equation + aligned
##
## Revision 1.3  1997/06/13 13:30:43  peterg
## Removed eqn labels.
##
# Revision 1.2  1996/12/19  10:25:15  peterg
# Now writes out mtti instead of mtt_{i} -- lets latex_tidy do the
# conversion.
#
# Revision 1.1  1996/10/21  08:50:15  peterg
# Initial revision
#
###############################################################


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

# Inform user
echo Creating $1_dae.tex $blurb

logname=dae_r2tex.log

# Remove the old log file
rm -f dae_r2tex.log

# Use reduce to accomplish the transformation
$SYMBOLIC >$logname << EOF

%Read the definitions file
in "$1_def.r";

%Read the differential-algebraic equations file
in "$1_dae.r";

%Read the symbolic parameter file
%%in "$1_sympar.r";


OFF Echo;
OFF Nat;


%Read the substitution file
in "$1_subs.r";

%Read the simplifying expressions file
IN "$1_simp.r";

% Read the formating function
in "$MTTPATH/trans/latex_equations.r";

OUT "$1_dae.tex";

%Write out the  differential-algebraic equations.

write "%File: $1_dae.tex";

write "%differential-algebraic equations";
%ON Rounded;
%Precision 5;

latex_equations();

SHUT "$1_dae.tex";
quit;
EOF

if [ -n "$partition" ]; then
  rm -f mtt_junk
  # Now for the subsystems
  # Find subsystems
  subsystems=`mtt_get_subsystems $1`

  for subsystem in $subsystems; do
  echo Doing $subsystem
  ucname="MTT_"$subsystem"_uc"
  subsystem_dae=$subsystem"_dae"
  #subname=`echo $subsystem | mtt_remove_underscore`

  Nu=`mtt_getsize -internal $subsystem"_1" u`
  echo Nu $Nu

  # Use symbolic algebra to accomplish the transformation
  $SYMBOLIC >$logname  << EOF


  %Read the definitions file
  in "$1_def.r";
  
  
  %Read the symbolic parameter file
  %%in "$1_sympar.r";
  
  
  % Read the formating function
  in  "$MTTPATH/trans/latex_equations.r";

  OFF Echo;
  OFF Nat;
  
  
  %Read the substitution file
  in "$1_subs.r";
  
  %Read the simplifying expressions file
  IN "$1_simp.r";

  %Read the differential-algebraic equations file
  in "$1_dae.r";

  %Create the output file
  OUT "mtt_junk_1";

  % Connecting inputs
  IF $Nu>0 THEN
  BEGIN
    write "\section{Subsystem $subsystem}";

    write "\subsection{Connections}";
    write"\begin{itemize}";
  FOR Row := 1:$Nu DO
    BEGIN
  	write "\item Input ", Row, " is connected to ";
  	write $ucname(Row,1);
    END;
    write"\end{itemize}";
  END;
  SHUT "mtt_junk_1";

  %Read the differential-algebraic equations file
  in "$subsystem_dae.r";

  %Create the output file
  OUT "mtt_junk_2";
  write "\subsection{Dynamics}";
   latex_equations();
  SHUT "mtt_junk_2";

EOF
  echo >> $1_dae.tex
  echo %Connections for subsystem $subsystem >> $1_dae.tex

  # Sort out underscores and remove mtt to avoide post processor
  cat mtt_junk_1 | mtt_remove_underscore | sed 's/mtt//g'>> $1_dae.tex
  cat mtt_junk_2  >> $1_dae.tex
  rm -rf mtt_junk_?
  done
fi


# Now invoke the standard error handling.
mtt_error_r dae_r2tex.log











