#! /bin/sh
######################################
##### Model Transformation Tools #####
######################################
# Bourne shell script: ese2rdae_r
# Reduce equations to raw differential-algebraic equation conversion
# P.J.Gawthrop 8th May 1991, May 1994, June 1996
# Copyright (c) P.J.Gawthrop, 1991, 1994, 1996
###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.12.2.1 2002/06/05 11:14:51 geraint
## ae.r now generated using def2write_r like cse?.r
## fix_c.r called at ese2rdae stage so that pow gets fixed in ae.r.
##
## These changes produce the desired result (optimised algebraic equations) but
## have highlighted a problem; when optimisation fails, Reduce does not write
## a result. For complicated systems, this can lead to missing assignments in
## the resultant code.
##
## Revision 1.12 2001/07/23 23:31:17 gawthrop
## Added -cr option to load CRs first - avoids alg. loops with R
## implementation of mutiports.
##
## Revision 1.11 2001/07/06 00:46:50 gawthrop
## Added -cr option -- forces cr to be loaded before the ese.r file
## This avoids causality problems when using multi-port Rs to represent
## arbitary equations
##
## Revision 1.10 2000/11/30 15:23:16 peterg
## Taken out all subsystem stuff - now done in mtt using
## mtt_make_subsystems
##
## Revision 1.9 2000/09/07 12:18:31 peterg
## Prettified layout of this file
##
## Revision 1.8 2000/09/07 12:07:26 peterg
## *** empty log message ***
##
## Revision 1.7 2000/09/01 13:34:15 peterg
## Exit on option error
##
## Revision 1.6 2000/09/01 13:30:32 peterg
## Added -partition switch
##
## Revision 1.5 2000/09/01 12:14:57 peterg
## Added connecti9ng inputs
##
## Revision 1.4 1998/04/07 15:06:46 peterg
## Correct log file name
##
## Revision 1.3 1998/03/07 12:57:19 peterg
## Fixed logname bug
##
## Revision 1.2 1998/03/07 12:51:20 peterg
## This is the new version of ese2dae - it does not do the CRs at this
## stage to give reduce an easier time.
##
## rese2ese_r does the constitutive relationship bit
##
## Revision 1.1 1998/03/07 12:49:31 peterg
## Initial revision
##
## Revision 1.12 1998/01/31 16:22:59 peterg
## Added IF MTTNx>0 THEN and IF MTTNy>0 THEN before the relevant
## assignements of MTTdX and MTTY to themselves.
##
## Revision 1.11 1997/09/18 09:45:08 peterg
## Canged the comment about linux reduce -- reduce reads this for some
## reason.
##
## Revision 1.10 1997/09/15 14:21:18 peterg
## Reads ese file twice to avoid a bug in Linux reduce !!!???
##
## Revision 1.9 1997/08/30 09:59:31 peterg
## Changed mehtod of writing out mtty to avoid a strange segmentation violation.
## Now uses MTTY := MTTY; approach.
##
## Revision 1.8 1997/04/23 09:31:20 peterg
## Now reads in the (top level) def file in the generated reduce code.
##
# Revision 1.7 1997/04/23 09:23:15 peterg
# Two argument version in preparation for heirarchical version
#
# Revision 1.6 1996/11/21 15:47:48 peterg
# Now inputs _params.r not _sympar.r
#
## Revision 1.5 1996/11/02 10:17:35 peterg
## Removed default constitutive relationship input.
##
# Revision 1.4 1996/09/12 12:10:05 peter
# Now reads the default constitutive relationship file.
#
## Revision 1.3 1996/08/25 09:43:26 peter
## General error handling.
##
## Revision 1.2 1996/08/25 08:31:28 peter
## Error handling added.
##
###############################################################
while [ -n "`echo $1 | grep '^-'`" ]; do
case $1 in
-I )
info=info;;
-cr )
load_cr=yes;
blurb2=' using cr and subs first';
;;
-fixcc )
include=`echo 'in "'$MTT_LIB'/reduce/fix_c.r";'`
blurb3='fixing c and cc code';
;;
-partition )
partition=yes;
blurb='with partitioning';
;;
*)
echo "$1 is an invalid argument - ignoring";
exit ;;
esac
shift
done
#Create the top-level system name
topname=$1
#Create the system names
if [ -z "$2" ];
then
sysname=$topname;
else
sysname=$1_$2;
fi
defname=$topname"_def.r"
crname=$topname"_cr.r"
subsname=$topname"_subs.r"
paramname=$topname"_params.r"
daename=$sysname"_rdae.r"
esename=$sysname"_ese.r"
logname=ese2rdae.log
if [ -n "${load_cr}" ]; then
load_cr_comm="in \"${crname}\";"
load_subs_comm="in \"${subsname}\";"
fi
# Inform user
echo Creating $daename $blurb $blurb2 $blurb3
# Remove the old log file
rm -f $logname
# Use symbolic algebra to accomplish the transformation
$SYMBOLIC >$logname << EOF
%Read in the cr and sub when -cr is set
$load_cr_comm
$load_subs_comm
%Read the formatting function
in "$MTTPATH/trans/reduce_matrix.r";
% Definitions
in "$defname";
% Elementary system equations
in "$esename";
% Fix c code if required
$include
OFF Echo;
OFF Nat;
%Create the output file
OUT "$daename";
write "in ""$defname""";
% State
MTT_Matrix := MTTdX$
MTT_Matrix_name := "MTTdX"$
MTT_Matrix_n := MTTNx$
MTT_Matrix_m := 1$
Reduce_Matrix()$
% Nonstate
MTT_Matrix := MTTZ$
MTT_Matrix_name := "MTTZ"$
MTT_Matrix_n := MTTNz$
MTT_Matrix_m := 1$
Reduce_Matrix()$
% Output
MTT_Matrix := MTTy$
MTT_Matrix_name := "MTTy"$
MTT_Matrix_n := MTTNy$
MTT_Matrix_m := 1$
Reduce_Matrix()$
% Zero outputs
MTT_Matrix := MTTYz$
MTT_Matrix_name := "MTTYz"$
MTT_Matrix_n := MTTNyz$
MTT_Matrix_m := 1$
Reduce_Matrix()$
SHUT "$daename";
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"
#
# Nu=`mtt_getsize -internal $subsystem u`
# echo Nu $Nu
#
# # Use symbolic algebra to accomplish the transformation
# $SYMBOLIC >$logname << EOF
#
# %Read the formatting function
# in "$MTTPATH/trans/reduce_matrix.r";
#
# % Definitions
# in "$defname";
#
# % Elementary system equations
# in "$esename";
#
# OFF Echo;
# OFF Nat;
#
# %Create the output file
# OUT "mtt_junk";
# % Connecting inputs
# MTT_Matrix := $ucname $
# MTT_Matrix_name := "$ucname" $
# MTT_Matrix_n := $Nu $
# MTT_Matrix_m := 1$
# Reduce_Matrix()$
#
# shut "mtt_junk";
#EOF
# echo >> $1_rdae.r
# echo %Connections for subsystem $subsystem >> $1_rdae.r
# echo >> $1_rdae.r
#
# cat mtt_junk >> $1_rdae.r
# rm -rf mtt_junk
# done
#fi
echo "END;" >> $1_rdae.r
# Now invoke the standard error handling.
mtt_error_r $logname