File mttroot/mtt/bin/trans/cbg2ese_m2r artifact f8d94a9670 part of check-in 6c46f9f457


#! /bin/sh

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

# Bourne shell script: cbg2ese_m
#
#
# P.J.Gawthrop June 1996
# Copyright (c) P.J.Gawthrop, 1996.

###############################################################
## Version control history
###############################################################
## $Id$
##
## $Log$
## Revision 1.31  2001/07/13 04:54:04  geraint
## Branch merge: numerical-algebraic-solution back to main.
##
## Revision 1.30  2001/07/12 04:02:53  gawthrop
## Now fixes multiports for input and output as well as state
##
## Revision 1.29.2.1  2001/06/26 22:29:05  geraint
## mtt_xargs.sh eliminates Arg list too long error for large models.
## (UNIX xargs does not work if the environment is too large).
##
## Revision 1.29  2001/03/29 19:24:14  gawthrop
## Can now use c representations of crs when using -c option
##
## Revision 1.28  2001/02/05 17:19:52  gawthrop
## Now gives unique names to the states of multiports. Second name
## onwards labeled with port number
##
## Revision 1.27  2000/12/05 12:09:56  peterg
## Changed function name to name()
##
## Revision 1.26  2000/12/05 09:04:54  peterg
## *** empty log message ***
##
## Revision 1.25  2000/12/01 14:51:26  peterg
## Major changes towards partioning
##
## - generates sub ese, def and struc
## - generates indices for conversion of subsystem in/out/state - but not used yet
##
## Revision 1.24  2000/11/30 15:12:55  peterg
## Zapped the declaration of subsystem connections - now in individual
## .def files
##
## Revision 1.23  2000/11/30 15:07:28  peterg
## Sorted out mtt_getsize
##
## Revision 1.22  2000/11/12 17:18:30  peterg
## Changed ' to "
##
## Revision 1.21  2000/10/16 08:40:35  peterg
## Whoops! Must touch mtt_aliased
##
## Revision 1.20  2000/10/16 08:36:31  peterg
## Creat empty _aliased file if it doesn't exist
##
## Revision 1.19  2000/10/12 19:25:39  peterg
## Now generateds the _aliased.txt file
##
## Revision 1.18  2000/09/15 08:09:42  peterg
## Removed another debugging line
##
## Revision 1.17  2000/09/15 08:06:17  peterg
## Removed debug lines
##
## Revision 1.16  2000/09/02 15:55:51  peterg
## *** empty log message ***
##
## Revision 1.15  1998/07/08 14:43:12  peterg
## Restored inforfile
##
## Revision 1.14  1998/07/08 08:26:26  peterg
## Added -I option -- if set prints out the information messages
##
## Revision 1.13  1998/05/12 14:49:10  peterg
## Don't write END; any more - ese-tidy does this now!
##
## Revision 1.12  1998/01/23 13:30:46  peterg
## $RMATRIX --> $MATRIX
##
## Revision 1.11  1997/12/16 18:05:03  peterg
## Increased size of structure vector by on to include unknown_inputs as
## 6th element
##
## Revision 1.10  1997/04/15 09:18:26  peterg
## Added structure file
##
# Revision 1.9  1996/12/07  18:21:25  peterg
# Now uses fopen + file number
#
## Revision 1.8  1996/12/05 10:05:28  peterg
## Removed the Octave switch: empty_list_elements_ok = 1;
## This is now in .octaverc
##
# Revision 1.7  1996/08/30  11:29:49  peter
# Added constitutive relationship to argument list.
# Put in explicit argument list to cbg2ese.
#
# Revision 1.6  1996/08/26  13:25:07  peterg
# Corrected error handling.
#
# Revision 1.5  1996/08/24  14:38:11  peter
# Error handling included.
#
## Revision 1.4  1996/08/19 09:02:12  peter
## Errors go to user - not log.
##
## Revision 1.3  1996/08/18 20:05:55  peter
## Included zero outputs.
##
## Revision 1.2  1996/08/08 18:08:37  peter
## Sorted out file naming scheme.
##
## Revision 1.1  1996/08/08 15:57:15  peter
## 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" ;;
  esac
  shift
done


infofile='mtt_info.txt';
structurefile="$1_struc.txt";
eqnfile="$1_ese.r";
deffile="$1_def.r";


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

# Remove the info and error files
rm -f mtt_info.txt
rm -f mtt_error.txt
rm -f mtt_aliased.txt

# Remove relevent ese files
rm -f $1_ese.r $1_*_ese.r 

#Inform user
echo Creating $eqnfile $blurb
echo Creating $deffile
echo Creating $structurefile

# Use matrix manipulation to accomplish the transformation
# This creates an ese file for every subsystem.
$MATRIX << EOF > cbg2ese_m2r.log 2>mtt_error.txt

infofile = "$infofile";;
infofilenum = fopen(infofile,"w");
structurefile = "$structurefile";;
structurefilenum = fopen(structurefile,"w");
deffile = "$1_def.r";
deffilenum = fopen(deffile,"w");

system_name = "$1";
system_type = "";
system_cr = "";
system_args = "";
full_name = "";
full_name_repetition = "";

% Structure matrix [states,nonstates,inputs,outputs,zero_outputs]

structure = zeros(1,7);  # Initialise structure vector

structure = cbg2ese(system_name, system_type, system_cr, ...
                     system_args, full_name, full_name_repetition, ...
                     1, structure, structurefilenum, infofilenum);
makedef(structure,deffilenum);
EOF

echo "END;" >> $1_def.r

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

if [ -z "$partition" ]; then
  ## Don't partition

  # Create the composite ese file
  mtt_xargs.sh cat "$1_ese.r $1_*_ese.r $1_modpar.r"  > $1_ese.tmp 2>> /dev/null

  mv $1_ese.tmp $1_ese.r

  # Zap the sub ese files
  mtt_xargs.sh "rm -f" "$1_*_ese.r"

  echo "END;" >> $1_ese.r

else # Partition the system

  # Save up the full def file
  mv $1_def.r mtt_all_def.r

## Recreate def file from the ese file directly
makedef ()
{

echo Creating $1_def.r

Nx=`mtt_getsize -external $1 dX`
Nz=`mtt_getsize -external $1 z`
Ny=`mtt_getsize -external $1 y`
Nu=`mtt_getsize -external $1 u`
Nui=`mtt_getsize -external $1 ui`
Nuc=`mtt_getsize -internal $1 u`

# echo Ny $Ny Nu $Nu Nx $Nx Nui $Nui Nuc $Nuc

## Definitions file
octave -q <<EOF
  deffile = '$1_def.r';
  deffilenum = fopen(deffile,'w');

  ## Set up the structure vector
  structure = zeros(1,7);  
  structure(1) = $Nx; # States
  structure(2) = $Nz; # Non-states
  structure(3) = $Nu; # Inputs
  structure(4) = $Ny; # Outputs
  structure(5) = 0;   # Zero outputs
  structure(6) = $Nui; #Internal inputs 
  structure(7) = $Nuc; # Connecting inputs

  makedef(structure,deffilenum);  #Create definitions
EOF

echo "END;" >> $1_def.r
}


get_indices ()
{
  sys=$1
  which=$2
  gawk '{if ($1==which) print $2}' which=$which < ${sys}_struc.txt
}

convert_indices ()
{
  sys=$1
  Which="input output state"
  for which in $Which; do
  
    indices=`get_indices ${sys} $which`
  
    case $which in
      input)
  	name=u
  	;;
      output)
  	name=y
  	;;
      state)
  	name=x
  	;;
      *)
  	
    esac
  
    new=0
    for old in $indices; do
      new=`expr $new + 1`
      pattern="MTT${name}(${old},1)"
      replacement="MTT${name}(${new},1)"
      mv ${sys}_ese.r mtt_junk
      sed "s/$pattern/$replacement/" <mtt_junk >${sys}_ese.r
    done
  done
}
  # Find subsystems
  subsystems=`mtt_get_subsystems $1`

  # Top level  
  makedef $1;
  echo "END;" >> $1_ese.r

  ## Subsystems
  for subsystem in $subsystems; do
    echo Connecting $subsystem
#    Ny=`mtt_getsize -internal ${subsystem}_1 y`
#    Nu=`mtt_getsize -internal ${subsystem}_1 u`
#    Nx=`mtt_getsize -internal ${subsystem}_1 u`
#    echo Ny $Ny Nu $Nu Nx $Nx


#    echo MATRIX "MTT_"$subsystem"_uc("$Nu",1);"  >> $1_def.r

    echo >> $1_ese.r
    echo %Subsystem input connections for $subsystem  >> $1_ese.r
    octave -q <<EOF                            >> $1_ese.r
    for i=1:$Nu
      printf("MTT_%s_uc(%i,1) := %s_1_MTTu%i;\n", "$subsystem", i, "$subsystem", i);
    endfor;
EOF
  ## Subsystems (Only works when no repetitions at this level)
  esefile=${subsystem}_ese.r
  echo Creating $esefile
  mtt_xargs.sh cat "${subsystem}_1_ese.r ${subsystem}_1_*_ese.r" > $esefile 2> /dev/null
  echo "END;" >> $esefile

  ## Def file for subsystem
  makedef ${subsystem}
  
  echo Creating ${subsystem}_struc.txt
  grep $subsystem < $1_struc.txt > ${subsystem}_struc.txt

  echo Converting subsystem indices
  convert_indices ${subsystem}

  done

fi


# Create the _alised file
touch mtt_aliased.txt
echo Creating $1_aliased.txt
mv mtt_aliased.txt $1_aliased.txt

# Sort the struc file
mv $structurefile junk
sort -k 1,1 -k 2,2n junk >$structurefile

# Relabel states with multiple ports
relabel_multiple_definitions(){
  mv $structurefile junk
  gawk '
  BEGIN{
     var_type_index=1;  
     oldname="";
  }
  {
     name=$4;
     newname=name;
     if ($1==var_type) {
       if (name==oldname)
         newname = sprintf("%s_%i",name, ++var_type_index);
       else
         var_type_index=1; 
   }
     $4 = newname
     for (i=1;i<NF;i++) printf("%s\t",$i);
     printf("%s\n", $NF)
     oldname = name;
  '} < junk var_type=$1> $structurefile
}

relabel_multiple_definitions input;
relabel_multiple_definitions output;
relabel_multiple_definitions state;

# Now invoke the standard error handling.
mtt_error mtt_error.txt









MTT: Model Transformation Tools
GitHub | SourceHut | Sourceforge | Fossil RSS ]