File mttroot/mtt/bin/trans/mtt_txt2m artifact 46dbec9921 part of check-in fd75fa927a


#! /bin/sh

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

# Bourne shell script: mtt_txt2m
# Converts txt file to matlab file (for numpar and state)

# Copyright (c) P.J.Gawthrop 1998


# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# %% Version control history
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# %% $Id$
# %% $Log$
# %% Revision 1.17  2002/04/28 18:41:27  geraint
# %% Fixed [ 549658 ] awk should be gawk.
# %% Replaced calls to awk with call to gawk.
# %%
# %% Revision 1.16  2002/04/18 17:51:11  gawthrop
# %% Put default values in _simpar.m in case of missing values in txt file
# %%
# %% Revision 1.15  2002/03/26 12:00:37  geraint
# %% Escaped characters to eliminate awk warnings.
# %%
# %% Revision 1.14  2001/07/13 04:54:04  geraint
# %% Branch merge: numerical-algebraic-solution back to main.
# %%
# %% Revision 1.13.4.2  2001/07/09 00:24:58  geraint
# %% input rep: Removed input name mapping, left state name mapping.
# %%
# %% Revision 1.13.4.1  2001/06/26 01:01:28  geraint
# %% Makes input and state names available for input rep.
# %%
# %% Revision 1.13  2001/02/05 03:33:18  geraint
# %% Convert user-defined txt to lower-case in translation.
# %% Required by logic.m (Octave is case sensitive).
# %%
# %% Revision 1.12  2000/11/08 11:20:49  peterg
# %% removed the empty matrix stuff
# %%
# %% Revision 1.11  2000/10/17 09:55:21  peterg
# %% Added logic rep
# %%
# %% Revision 1.10  2000/10/15 09:51:50  peterg
# %% Set array code in input rep
# %%
# %% Revision 1.9  2000/10/13 11:07:33  peterg
# %% Started conversion to new style state, input ext
# %%
# %% Revision 1.8  2000/09/30 13:53:07  peterg
# %% Default to empty matix in numpar.m -- avoids probs for parameter-free systems
# %%
# %% Revision 1.7  2000/05/19 17:46:17  peterg
# %% Give argument to state
# %%
# %% Revision 1.6  2000/05/19 14:59:03  peterg
# %% MTT --> mtt in fun output
# %%
# %% Revision 1.5  2000/05/19 14:05:10  peterg
# %% Zero parameters in the numpar.m file
# %%
# %% Revision 1.4  2000/05/18 12:05:42  peterg
# %% Replaced sympar by sympars in numpar rep
# %%
# %% Revision 1.3  2000/05/11 16:09:24  peterg
# %% Put in simpar.first parameter
# %%
# %% Revision 1.2  2000/05/11 12:16:46  peterg
# %% New version of simpar - with record
# %%
# %% Revision 1.1  2000/05/11 10:55:57  peterg
# %% Initial revision
# %%
# %% Revision 1.19  2000/02/11 13:35:16  peterg
# %% Added the new MTTpar generation
# %%
# %% Revision 1.18  1999/11/14 22:22:17  peterg
# %% Removed checks for implicit - now done at command-line level.
# %%
# %% Revision 1.17  1999/11/04 04:54:24  peterg
# %% Removed recreation of smx file.
# %%
# %% Revision 1.16  1999/03/06 02:18:10  peterg
# %% Changed argument list.
# %%
# %% Revision 1.15  1999/02/16 21:44:38  peterg
# %% Revised smx generation
# %%
# %% Revision 1.14  1999/02/16 04:38:22  peterg
# %% Now forces creation of _smx file if METHOD=IMPLICIT in simpar.txt
# %%
# %% Revision 1.13  1998/08/31 11:43:37  peterg
# %% Now lower cases globals in numpar files
# %%
# %% Revision 1.12  1998/08/14 10:47:31  peterg
# %% Put ImplicitS sub before Implicit!!
# %%
# %% Revision 1.11  1998/08/14 10:43:44  peterg
# %% Added ImplicitS - sparse integration
# %%
# %% Revision 1.10  1998/08/11 13:27:51  peterg
# %% Lowercase mttLAST etc
# %%
# %% Revision 1.9  1998/07/30 15:07:17  peterg
# %% Added _ to the disallowed chars around t
# %%
# %% Revision 1.8  1998/07/30 12:52:38  peterg
# %% Adds ; to end of statements
# %% Translates ' to " before removal
# %%
# %% Revision 1.7  1998/07/30 09:32:33  peterg
# %% Replaces:
# %% 	euler by 1
# %% 	implicitl by 1
# %% 	implicit  by 3
# %%
# %% Revision 1.6  1998/07/27 18:59:11  peterg
# %% Added WMIN etc
# %%
# %% Revision 1.5  1998/07/26 11:54:20  peterg
# %% Added  mtt to variables
# %%
# %% Revision 1.4  1998/07/26 09:50:12  peterg
# %% More forgiving of txt sytax.
# %%
# %% -- can use () or ()
# %% -- can use mttx() or x()
# %% -- can use mttu() or u()
# %%
# %% Revision 1.3  1998/07/25 20:40:35  peterg
# %% All vars in lower case now
# %%
# %% Revision 1.2  1998/07/25 09:48:31  peterg
# %% Tidied up for Pascal version
# %%
# %% Revision 1.1  1998/02/25 22:10:25  peterg
# %% Initial revision
# %%
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

system=$1
representation=$2
outfile=$1_$2.m

tolower () { tr [A-Z] [a-z] ;}

# Inform user
echo Creating $outfile

#Create the rep  file complete with headers.
case $representation in
    input)
      name=mttu
      arraycode=$representation
      ;;
    logic)
      name=mttx
      ;;
    numpar)
       name=mttpar
      arraycode=$representation
      ;;
    simpar)
      ;;
    state)
      name=mttx
      arraycode=$representation
      ;;
    *)
      echo Representation $representation not supported
      exit
      #echo "function $name = $1_$2$arg" > $outfile
      #echo "%% $2 file ($1_$2.m)" >> $outfile
      #echo "%% Generated by MTT at `date`" >> $outfile
      #echo "% Global variable list" >>  $outfile
      #sympar2global_txt2m $1 >> $outfile
      ;;
esac

# Generate the header
mtt_header $system $representation m  > $outfile

## Special for logic rep
if [ "$representation" = "logic" ]; then
  echo >> $outfile
  echo "## Inputs" >> $outfile
  mtt_name2array $system input >> $outfile # Set up input by name
  echo >> $outfile
  echo "## States" >>$outfile
  mtt_name2array $system state >> $outfile # Set up state by name
fi

## Special for input rep
if [ "$representation" = "input" ]; then
  echo >> $outfile
  echo "## States" >>$outfile
  mtt_name2array $system state >> $outfile # Set up state by name
fi

## Special for simpar rep
if [ "$representation" = "simpar" ]; then
  cat >> $outfile<<EOF

## Defaults (in case of missing values in txt file)
  mttsimpar.first       =  0.0;      
  mttsimpar.last        =  10.0;      
  mttsimpar.dt          =  0.1;        
  mttsimpar.stepfactor  =  1;          
  mttsimpar.wmin        =  -1;         
  mttsimpar.wmax        =  2;          
  mttsimpar.wsteps      =  100;        
  mttsimpar.input       =  1; 

EOF
fi

# Write out the code from the txt file
echo >> $outfile
echo "## User defined code from $1_$2.txt" >> $outfile
cat  $1_$2.txt | strip_comments | grep -v METHOD |\
gawk -F#  'BEGIN{
quote = "\047";
doublequote = "\042";
}
{
  N=split($1,a,"=");
  if (N==2) {
    LHS = a[1];
    RHS = a[2];
    gsub(quote, doublequote, RHS);
    sub(/^[ 	]*FIRST/, "MTTsimpar.FIRST", LHS);
    sub(/^[ 	]*DT/, "MTTsimpar.DT", LHS);
    sub(/^[ 	]*LAST/, "MTTsimpar.LAST", LHS);
    sub(/^[ 	]*STEPFACTOR/, "MTTsimpar.STEPFACTOR", LHS);
    sub(/^[ 	]*METHOD/, "MTTsimpar.METHOD", LHS);
    sub(/^[ 	]*WMIN/, "MTTsimpar.WMIN", LHS);
    sub(/^[ 	]*WMAX/, "MTTsimpar.WMAX", LHS);
    sub(/^[ 	]*WSTEPS/, "MTTsimpar.WSTEPS", LHS);
    sub(/^[ 	]*INPUT/, "MTTsimpar.input", LHS);
    LHS = tolower(LHS);
    RHS = tolower(RHS);
    sub(/["]*euler["]*/, 1, RHS);   
    sub(/["]*implicit["]*/, 2, RHS);  
    statement = sprintf("%s= %s",LHS,RHS);
    if ( (match(statement,";")==0)&&\
         ((match(statement,"if ")==0))&&\
         ((match(statement,"for ")==0)) )
      statement = sprintf("%s;", statement);
  }
  else if (match($1,"global")==1) {
    statement = tolower($1); # Lower case globals as well
  }
  else
    statement = $1;

  if (NF<2) print "  " statement
  if (NF>1)  print statement " # " $2
}' | sed\
     -e 's/\[\([0-9]*\)\]/(\1)/g' \
     -e 's/\([^a-zA-Z_0-9]\)t\([^a-zA-Z_0-9]\)/\1mttt\2/g' \
     -e 's/\([^a-zA-Z_0-9]\)x(/\1mttx(/g' \
     -e 's/\([^a-zA-Z_0-9]\)u(/\1mttu(/g' \
     -e 's/mttmtt/mtt/g' \
| tolower >> $outfile

if [ -n "$arraycode" ]; then
  new_style=`strip_comments < $1_$2.txt| grep "$name(">/dev/null; echo $?`

  if [ "$new_style" = "1" ]; then
    echo >> $outfile
    echo "  ## Set up the $name vector"  >> $outfile
    ## echo "  $name = []; # Default to empty matrix (Remove in mtt_m2p)"  >> $outfile
    mtt_name2array $1 $arraycode set   >> $outfile
  else
    echo "Old-style file detected: not creating array translations"  
  fi
fi

## Special for logic rep
if [ "$representation" = "logic" ]; then
  echo >> $outfile
  echo "## Open switches" >> $outfile
  switch_txt2m $system | tolower >> $outfile
fi

# End of the function
echo '## END Code' >> $outfile
echo 'endfunction' >> $outfile



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