Index: mttroot/mtt/bin/trans/cse2ode_r ================================================================== --- mttroot/mtt/bin/trans/cse2ode_r +++ mttroot/mtt/bin/trans/cse2ode_r @@ -12,10 +12,13 @@ ############################################################### ## Version control history ############################################################### ## $Id$ ## $Log$ +## Revision 1.3.4.1 2002/09/03 23:44:43 geraint +## adding global optimisation (-optg). +## ## Revision 1.3 2001/07/27 23:29:10 geraint ## Optimises only when requested (-opt). ## ## Revision 1.2 2001/07/13 04:54:04 geraint ## Branch merge: numerical-algebraic-solution back to main. @@ -50,12 +53,12 @@ esac shift done # Create the reduce output code -def2write_r $optimise $1 ode -def2write_r $optimise $1 odeo +def2write_r $1 ode +def2write_r $1 odeo #Inform user echo Creating $1_ode.r $optimise_msg echo Creating $1_odeo.r $optimise_msg @@ -134,11 +137,14 @@ SHUT "$1_odeo.r"; quit; EOF if [ ${optimise:-""} = "-optimise_global" ]; then - mtt_optimise_global $1 ode - mtt_optimise_global $1 odeo + mtt_optimise global $1 ode + mtt_optimise global $1 odeo +elif [ ${optimise:-""} = "-optimise_local" ]; then + mtt_optimise local $1 ode + mtt_optimise local $1 odeo fi # Now invoke the standard error handling. mtt_error_r cse2ode_r.log Index: mttroot/mtt/bin/trans/csm2sm_r ================================================================== --- mttroot/mtt/bin/trans/csm2sm_r +++ mttroot/mtt/bin/trans/csm2sm_r @@ -12,10 +12,13 @@ ############################################################### ## 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 @@ -43,11 +46,11 @@ esac shift done # Create the reduce output code -def2write_r $optimise $1 $2 +def2write_r $1 $2 # Inform user echo Creating $1_$2.r $optimise_msg case $2 in @@ -143,10 +146,12 @@ EOF cat $1_$2.r1 $1_$2.r2 > $1_$2.r if [ ${optimise:-""} = "-optimise_global" ]; then - mtt_optimise_global $1 $2 + 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 Index: mttroot/mtt/bin/trans/dae2cse_r ================================================================== --- mttroot/mtt/bin/trans/dae2cse_r +++ mttroot/mtt/bin/trans/dae2cse_r @@ -13,10 +13,13 @@ ############################################################### ## Version control history ############################################################### ## $Id$ ## $Log$ +## Revision 1.23.2.1 2002/09/03 23:44:43 geraint +## adding global optimisation (-optg). +## ## Revision 1.23 2002/07/10 17:43:05 geraint ## Added feature [ 562453 ] Optimisation of algebraic equations. ## ## Revision 1.22 2002/06/28 15:35:47 geraint ## Commented out aej.r generation (not used yet). @@ -148,14 +151,14 @@ esac shift done # Create the reduce output code -def2write_r $optimise $fixcc $1 ae -def2write_r $optimise $fixcc $1 cse -def2write_r $optimise $fixcc $1 csex # Version without E matrix -def2write_r $optimise $fixcc $1 cseo +def2write_r $fixcc $1 ae +def2write_r $fixcc $1 cse +def2write_r $fixcc $1 csex # Version without E matrix +def2write_r $fixcc $1 cseo echo "Creating $1_ae.r $optimise_msg" echo "Creating $1_cse.r $solve_msg $optimise_msg $fix_msg" echo "Creating $1_csex.r $optimise_msg" echo "Creating $1_cseo.r $optimise_msg" @@ -478,14 +481,19 @@ cat $1_cse.r1 $1_cse.r2 > $1_cse.r cat $1_csex.r1 $1_csex.r2 > $1_csex.r cat $1_cseo.r1 $1_cseo.r2 > $1_cseo.r if [ ${optimise:-""} = "-optimise_global" ]; then - mtt_optimise_global $1 ae -# TODO: mtt_optimise_global $1 cse -# TODO: mtt_optimise_global $1 cseo -# TODO: mtt_optimise_global $1 csex + mtt_optimise global $1 ae +# TODO: mtt_optimise global $1 cse +# TODO: mtt_optimise global $1 cseo +# TODO: mtt_optimise global $1 csex +elif [ ${optimise:-""} = "-optimise_local" ]; then + mtt_optimise local $1 ae +# TODO: mtt_optimise local $1 cse +# TODO: mtt_optimise local $1 cseo +# TODO: mtt_optimise local $1 csex fi if [ "$solve" = "1" ]; then echo "Setting MTTNyz=0 in $1_def.r and updating other $1_def files" gawk '{ Index: mttroot/mtt/bin/trans/dae2lde_r ================================================================== --- mttroot/mtt/bin/trans/dae2lde_r +++ mttroot/mtt/bin/trans/dae2lde_r @@ -11,10 +11,13 @@ ############################################################### ## 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:25:13 peterg ## Initial revision @@ -35,12 +38,12 @@ esac shift done # Create the reduce output code -def2write_r $optimise $1 lde -def2write_r $optimise $1 ldeo +def2write_r $1 lde +def2write_r $1 ldeo #Inform user echo Creating $1_lde.r $optimise_msg echo Creating $1_ldeo.r $optimise_msg @@ -124,12 +127,15 @@ SHUT "$1_ldeo.r"; quit; EOF if [ ${optimise:-""} = "-optimise_global" ]; then -# TODO: global_optimise $1 lde -# TODO: global_optimise $1 ldeo +# TODO: mtt_optimise global $1 lde +# TODO: mtt_optimise global $1 ldeo +elif [ ${optimise:-""} = "-optimise_local" ]; then +# TODO: mtt_optimise local $1 lde +# TODO: mtt_optimise local $1 ldeo fi # Now invoke the standard error handling. mtt_error_r cse2lde_r.log Index: mttroot/mtt/bin/trans/def2write_r ================================================================== --- mttroot/mtt/bin/trans/def2write_r +++ mttroot/mtt/bin/trans/def2write_r @@ -11,10 +11,13 @@ ############################################################### ## Version control history ############################################################### ## $Id$ ## $Log$ +## Revision 1.16.2.2 2002/09/10 22:09:14 geraint +## Fixed presentation of globally optimised equations (ode view). +## ## Revision 1.16.2.1 2002/09/03 23:44:43 geraint ## adding global optimisation (-optg). ## ## Revision 1.16 2002/09/03 19:34:15 geraint ## Write EdX regardless - csex is needed to create ode when not optimised. @@ -80,16 +83,10 @@ ############################################################### optimise='' while [ -n "`echo $1 | grep '^-'`" ]; do case $1 in - -optimise_global ) - opt='' - optimise_msg='' ;; - -optimise_local ) - opt='-optimise_local' - optimise_msg=' with local optimisation' ;; -fixcc ) include=`echo 'in "'$MTT_LIB'/reduce/fix_c.r";'` ;; *) echo "$1 is an invalid argument - ignoring" ;; esac @@ -98,11 +95,11 @@ sys=$1 # System name rep=$2 # System representation # Inform User -echo Creating $1_$2_write.r $optimise_msg +echo Creating $1_$2_write.r # Find system constants Nx=`mtt_getsize $sys x` # States Nxx=`mtt_getsize $sys xx` # States x States Nu=`mtt_getsize $sys u` # Inputs @@ -159,73 +156,19 @@ ;; *) echo def2write_r: representation $rep not recognised exit esac - -mtt_optimise_local () -{ - sys="$1" - lhs="$2" - rhs="$3" - - dae="${sys}_dae.r" - tmp="mtt_optimise.tmp" - tmp1="mtt_optimise1.tmp" - tmp2="mtt_optimise2.tmp" - tmp3="mtt_optimise3.tmp" - - grep -i -e "^$lhs" $dae |\ - sed -e 's/;/\ INAME\ mtt_tmp/g' |\ - sed -e 's/:=/:=/g' > $tmp1 - nlines=`wc -l $tmp1 | gawk '{print $1}'` - if [ $nlines -gt 0 ]; then - { - cat < $tmp - cat $tmp1 >> $tmp - { - cat <> $tmp - echo "%%% $lhs %%%" >> def2write_r1.log - ${SYMBOLIC:-reduce} < $tmp >> def2write_r1.log 2>> def2write_r2.log - cat $tmp2 | gawk -v RS=';' '($2 == ":=") {print $0}' > $tmp3 - cat $tmp3 | mtt_fix_integers - fi - rm -f $tmp $tmp1 $tmp2 $tmp3 - return -} - # Remove log files rm -f def2write_r1.log def2write_r2.log # Write out the code echo "" > $1_$2_write.r -if [ ${opt:-""} = "-optimise_local" ]; then - echo 'off nat$' -# echo 'on echo$' -else - echo 'off echo$' - echo 'load gentran$' -fi >> $1_$2_write.r +echo 'off echo$' >> $1_$2_write.r +echo 'load gentran$' >> $1_$2_write.r for matrix in $matrices; do matrix_exists=`grep -i MTT${matrix} ${sys}_dae.r | wc -l | gawk '{print $1}'` if [ "$matrix" = "EdX" -o $matrix_exists -gt 0 ]; then n=`first "$ns"`; ns=`rest "$ns"` @@ -234,21 +177,15 @@ js=`n2m 1 $m`; echo "write \"% Begin Matrix MTT${matrix}\"$" >> $1_$2_write.r if [ $n -ge 1 ]; then for i in $is; do for j in $js; do - if [ ${opt:-""} = "-optimise_local" ]; then - name=`echo MTT$matrix'('$i','$j')'` - mtt_optimise_local $1 "$comma$name" "$name" - comma='' - else - echo 'write' - name=`echo MTT$matrix'('$i','$j')'` - echo ' '$comma$name ':=' $name '$' - fi >> $1_$2_write.r + echo 'write' + name=`echo MTT$matrix'('$i','$j')'` + echo ' '$comma$name ':=' $name '$' done done - fi + fi >> $1_$2_write.r echo "write \"% End Matrix MTT${matrix}\"$" >> $1_$2_write.r fi done echo ';END;' >>$1_$2_write.r ADDED mttroot/mtt/bin/trans/mtt_optimise Index: mttroot/mtt/bin/trans/mtt_optimise ================================================================== --- mttroot/mtt/bin/trans/mtt_optimise +++ mttroot/mtt/bin/trans/mtt_optimise @@ -0,0 +1,121 @@ +#! /bin/sh + +# script uses Reduce + Scope to optimise a vector +# either global (an entire vector) or local (line-by-line) + +extent=$1 # global or local +system=$2 +representation=$3 + +# error codes +E_REP_NOT_SUPPORTED=-19 +E_FILE_NOT_EXIST=-20 + +case $representation in + ae) + matrix=yz ; + nmatrix=`mtt_getsize $system yz` ;; + ode) + matrix=dx ; + nmatrix=`mtt_getsize $system x` ;; + cseo | odeo) + matrix=y ; + nmatrix=`mtt_getsize $system y` ;; + *) + exit $E_REP_NOT_SUPPORTED;; +esac + +# Global optimisation: Generate a command of the form +# optimise mtt?(1,1) :=: mtt?(1,1), mtt?(2,1) :=: mtt?(2,1), ..., INAME mtt_tmp$ +# +# Local optimisation: Generate a command of the form +# optimise mtt?(1,1) :=: mtt?(1,1) INAME mtt_tmp$ \ +# optimise mtt?(2,1) :=: mtt?(2,1) INAME mtt_tmp$ ... +case $extent in + "global" ) + delimiter=", " + terminate="INAME mtt_tmp$" + ;; + "local" ) + delimiter=" INAME mtt_tmp$ optimize" + terminate="$" + ;; + * ) + echo "*** unknown scope for optimisation" + echo "*** should be global or local" + exit -1 + ;; +esac + +if [ $nmatrix -gt 0 ]; then + command="optimize" + counter=1 + while [ $counter -le $nmatrix ]; do + command="$command mtt$matrix($counter,1) :=: mtt$matrix($counter,1)$delimiter" + counter=`expr $counter + 1` + done + command="$command $terminate" +else + command="" +fi + +logfile=${system}_${representation}_optimisation.log +tmpfile=${system}_${representation}_optimisation.tmp +outfile=${system}_${representation}.r + +if [ ! -f $outfile ]; then + exit $E_FILE_NOT_EXIST +fi + +find_code () +{ + file_in=${1:-${IN}} + portion=${2:-"body"} + head=`cat ${file_in} | gawk '($2 == "Begin" && $3 == "Matrix") { print NR }'` + foot=`cat ${file_in} | gawk '($2 == "End" && $3 == "Matrix") { print NR }'` + case ${portion} in + head) + start=0 + end=${head} + ;; + body) + start=${head} + end=${foot} + ;; + foot) + start=${foot} + end=end + ;; + *) + echo "Error in find_code: portion unknown" + return -1 + ;; + esac + cat ${file_in} |\ + gawk --assign start=${start} --assign end=${end} ' + (start < NR && NR < end) { print $0 }' +}; + + +# Use Reduce to perform the optimisation +${SYMBOLIC:-reduce} < $logfile 2>&1 +off nat; +in "${system}_def.r"; +in "$outfile"; +load scope; +out "$tmpfile"; +$command; +shut "$tmpfile"; +$end; +EOF + +cp $outfile $outfile.unoptimised +cp $tmpfile $outfile.tmp + +find_code $outfile head > $tmpfile.head +cat $tmpfile | mtt_fix_integers > $tmpfile.body +find_code $outfile foot > $tmpfile.foot +cat $tmpfile.head $tmpfile.body $tmpfile.foot > $outfile +echo ";end;" >> $outfile + +mtt_error_r $logfile DELETED mttroot/mtt/bin/trans/mtt_optimise_global Index: mttroot/mtt/bin/trans/mtt_optimise_global ================================================================== --- mttroot/mtt/bin/trans/mtt_optimise_global +++ mttroot/mtt/bin/trans/mtt_optimise_global @@ -1,99 +0,0 @@ -#! /bin/sh - -# script uses Reduce + Scope to optimise an entire vector - -system=$1 -representation=$2 - -# error codes -E_REP_NOT_SUPPORTED=-19 -E_FILE_NOT_EXIST=-20 - -case $representation in - ae) - matrix=yz ; - nmatrix=`mtt_getsize $system yz` ;; - ode) - matrix=dx ; - nmatrix=`mtt_getsize $system x` ;; - cseo | odeo) - matrix=y ; - nmatrix=`mtt_getsize $system y` ;; - *) - exit $E_REP_NOT_SUPPORTED;; -esac - -# Generate a command of the form -# optimise mtt?(1,1) :=: mtt?(1,1), mtt?(2,1) :=: mtt?(2,1), ..., INAME mtt_tmp$ -if [ $nmatrix -gt 0 ]; then - command="optimize" - counter=1 - while [ $counter -le $nmatrix ]; do - command="$command mtt$matrix($counter,1) :=: mtt$matrix($counter,1)," - counter=`expr $counter + 1` - done - command="$command INAME mtt_tmp" -else - command="" -fi - -logfile=${system}_${representation}_global_optimisation.log -tmpfile=${system}_${representation}_global_optimisation.tmp -outfile=${system}_${representation}.r - -if [ ! -f $outfile ]; then - exit $E_FILE_NOT_EXIST -fi - -find_code () -{ - file_in=${1:-${IN}} - portion=${2:-"body"} - head=`cat ${file_in} | gawk '($2 == "Begin" && $3 == "Matrix") { print NR }'` - foot=`cat ${file_in} | gawk '($2 == "End" && $3 == "Matrix") { print NR }'` - case ${portion} in - head) - start=0 - end=${head} - ;; - body) - start=${head} - end=${foot} - ;; - foot) - start=${foot} - end=end - ;; - *) - echo "Error in find_code: portion unknown" - return -1 - ;; - esac - cat ${file_in} |\ - gawk --assign start=${start} --assign end=${end} ' - (start < NR && NR < end) { print $0 }' -}; - - -# Use Reduce to perform the optimisation -${SYMBOLIC:-reduce} < $logfile 2>&1 -off nat; -in "${system}_def.r"; -in "$outfile"; -load scope; -out "$tmpfile"; -$command; -shut "$tmpfile"; -$end; -EOF - -cp $outfile $outfile.unoptimised -cp $tmpfile $outfile.tmp - -find_code $outfile head > $tmpfile.head -cat $tmpfile | mtt_fix_integers > $tmpfile.body -find_code $outfile foot > $tmpfile.foot -cat $tmpfile.head $tmpfile.body $tmpfile.foot > $outfile -echo ";end;" >> $outfile - -mtt_error_r $logfile