File mttroot/mtt/bin/trans/mtt_txt2m artifact b9da97b04a part of check-in a95f9980a7


#! /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.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

if [ "$representation" = "state" ]; then
    name='mttx';
    arg='';
fi
if [ "$representation" = "input" ]; then
    name='mttu';
    arg='(mttt,mttx,mtty)'
fi

# Inform user
echo Creating $1_$2.m

#Create the $2 file complete with headers.
case $representation in
    simpar)
      lang_header -noglobals $1 simpar m '' mttsimpar > $1_simpar.m
      ;;
    numpar)
      lang_header $1 numpar m '' mttpar > $1_numpar.m;
      echo "## Set parameters to zero" >> $1_numpar.m;
      sympar2par_txt2m $1 zero >> $1_numpar.m;
      ;;
    state)
      lang_header $1 $2 m 'mttpar' mttx > $1_$2.m;
      echo "  if nargin>0" >> $1_$2.m;
      echo "  ## Set parameters" >> $1_$2.m;
      sympar2par_txt2m $1 >> $1_$2.m;
      echo "  endif" >> $1_$2.m;
      ;;
    *)
      echo "function $name = $1_$2$arg" > $1_$2.m
      echo "%% $2 file ($1_$2.m)" >> $1_$2.m
      echo "%% Generated by MTT at `date`" >> $1_$2.m
      echo "% Global variable list" >>  $1_$2.m
      sympar2global_txt2m $1 >> $1_$2.m
      ;;
esac

#Write out the variables in m format.
cat  $1_$2.txt | grep -v METHOD |\
awk -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_]\)t\([^a-zA-Z_]\)/\1mttt\2/g' \
     -e 's/x(/mttx(/g' \
     -e 's/u(/mttu(/g' \
     -e 's/mttmtt/mtt/g' \
     -e 's/#/%/g' \
>> $1_$2.m


if [ "$2" = "numpar" ]; then
   echo "## Set up the parameter vector"  >> $1_numpar.m;
   echo "mttpar = []; # Default to empty matrix (Remove in mtt_m2p)"  >> $1_numpar.m;
   sympar2par_txt2m $1 set   >> $1_numpar.m;
fi


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