#! /bin/sh
######################################
##### Model Transformation Tools #####
######################################
# Bourne shell script: csm2sm_r
# Constrained-state equation to linear constrained-state matrices conversion
# P.J.Gawthrop 6th September 1991, May 1994
# Copyright (c) P.J.Gawthrop, 1991, 1994, 1996
###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.2.4.1 2002/09/03 23:44:43 geraint
## adding global optimisation (-optg).
##
## Revision 1.2 2001/07/27 23:29:10 geraint
## Optimises only when requested (-opt).
##
## Revision 1.1 2000/12/28 12:24:03 peterg
## Put under RCS
##
## Revision 1.2 1996/08/25 10:11:32 peter
## Added END in output file.
## Error handling.
##
## Revision 1.1 1996/08/19 15:06:16 peter
## Initial revision
##
###############################################################
optimise=''; optimise_msg=''
while [ -n "`echo $1 | grep '^-'`" ]; do
case $1 in
-optimise_global )
optimise='-optimise_global'
optimise_msg=' with global optimisation' ;;
-optimise_local )
optimise='-optimise_local'
optimise_msg=' with local optimisation' ;;
* )
echo "$1 is an invalid argument - ignoring" ;;
esac
shift
done
# Create the reduce output code
def2write_r $1 $2
# Inform user
echo Creating $1_$2.r $optimise_msg
case $2 in
sm)
rep=csm;
;;
ssm)
rep=scsm
;;
*)
echo Representation must be sm or ssm
esac
# Remove the old log file
rm -f csm2sm_r.log
# Use reduce to accomplish the transformation
$SYMBOLIC >csm2sm_r.log << EOF
in "$1_def.r";
in "$1_$rep.r";
in "$1_cr.r";
%%in "$1_sympar.r";
OFF Echo;
OFF Nat;
% Find MTTA and MTTB : the A and B matrices
MTTinvE := MTTE^(-1);
MTTA := MTTinvE*MTTA;
MTTB := MTTinvE*MTTB;
% Create the matrix declarations
OUT "$1_$2.r1";
write "MATRIX MTTA(", MTTNx, ",", MTTNx, ")$";
write "MATRIX MTTB(", MTTNx, ",", MTTNu, ")$";
write "MATRIX MTTC(", MTTNy, ",", MTTNx, ")$";
write "MATRIX MTTD(", MTTNy, ",", MTTNu, ")$";
SHUT "$1_$2.r1";
%Create the output file
OUT "$1_$2.r2";
write "%File: $1_ode.r";
write "% New constants";
write "MTTNx := ", MTTNx, ";";
write "MTTNy := ", MTTNy, ";";
in ("$1_sm_write.r");
%Write out the matrices.
% IF MTTNx>0 THEN
% BEGIN
% write "matrix MTTA(", MTTNx, ",", MTTNx, ");";
% FOR i := 1:MTTNx DO
% FOR j := 1:MTTNx DO IF MTTA(i,j) NEQ 0 THEN
% write "MTTA(", i, ",", j, ") := ", MTTA(i,j);
% END;
% IF MTTNx>0 THEN
% IF MTTNu>0 THEN
% BEGIN
% write "matrix MTTB(", MTTNx, ",", MTTNu, ");";
% FOR i := 1:MTTNx DO
% FOR j := 1:MTTNu DO IF MTTB(i,j) NEQ 0 THEN
% write "MTTB(", i, ",", j, ") := ", MTTB(i,j);
% END;
% %Write it out
% IF MTTNy>0 THEN
% IF MTTNx>0 THEN
% BEGIN
% write "matrix MTTC(", MTTNy, ",", MTTNx, ");";
% FOR i := 1:MTTNy DO
% FOR j := 1:MTTNx DO IF MTTC(i,j) NEQ 0 THEN
% write "MTTC(", i, ",", j, ") := ", MTTC(i,j);
% END;
% IF MTTNy>0 THEN IF MTTNu>0 THEN
% BEGIN
% write "matrix MTTD(", MTTNy, ",", MTTNu, ");";
% FOR i := 1:MTTNy DO
% FOR j := 1:MTTNu DO IF MTTD(i,j) NEQ 0 THEN
% write "MTTD(", i, ",", j, ") := ", MTTD(i,j);
% END;
write "END;";
SHUT "$1_$2.r2";
quit;
EOF
cat $1_$2.r1 $1_$2.r2 > $1_$2.r
if [ ${optimise:-""} = "-optimise_global" ]; then
mtt_optimise global $1 $2
elif [ ${optimise:-""} = "-optimise_local" ]; then
mtt_optimise local $1 $2
fi
# Now invoke the standard error handling.
mtt_error_r csm2sm_r.log