#! /bin/sh

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

# Bourne shell script: ode_r2m
# Reduce ODE to matlab  ODE  
# P.J.Gawthrop 14 June 1991, 12 Jan 1994, April 1994, Jan 95.
# Copyright (c) P.J.Gawthrop 1991, 1994, 1995, 1996

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
# Revision 1.6  1997/01/05  19:34:35  peterg
# Don't write globals which are already assigned to a number.
#
## Revision 1.5  1996/09/13 19:41:39  peter
## *** empty log message ***
##
## Revision 1.4  1996/09/12 18:33:32  peter
## Put back under rcs
##
## Revision 1.3  1996/08/30 11:04:28  peter
## Changed line length to 500.
##
## Revision 1.2  1996/08/24 14:09:41  peter
## Global parameter passing.
##
## Revision 1.1  1996/08/18 12:03:49  peter
## Initial revision
##
###############################################################

#Inform user
echo Creating $1_ode.m
echo Creating $1_odeo.m


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

# Use reduce to accomplish the transformation
reduce >ode_r2m.log << EOF

%Read the reduce definitions file
in "$1_def.r";

%Read the reduce  ODE   file
in "$1_ode.r";

%Set up the number of argument variables to zero in case the user has forgotten
MTTNVar := 0;

%Read the parameter file
in "$1_sympar.r";

ON NERO;        % Suppress zero elements

%Define the common part of the functions.

PROCEDURE common;
BEGIN
  IF MTTNvar>0 THEN
  BEGIN
    write "% Read in the parameters";
    write "global ...;;";
    FOR i := 1:MTTNvar DO
    IF numberp(MTTVar(i,1)) 
      THEN 
      BEGIN
      % Do nowt
      END
      ELSE  
      BEGIN
         IF i<MTTNvar THEN write MTTVar(i,1), " ..."
                      ELSE write MTTVar(i,1), ";"
      END;
  END;
  
  write "% Read in the input";
  write "u = $1_input(t)";

  write "% Read in the definitions";
  write "[nx,ny,nu,nz,nyz] = $1_def";

  write "% Read in the arguments";
  write "$1_args";
  
  write "% Set up the State variables";
  FOR i := 1:MTTNx DO
  BEGIN
    write "mttx", i, " = x(", i, ");";
  END;
  
  write "% Set up the Input variables";
  IF MTTNu>0 THEN
  FOR i := 1:MTTNu DO
  BEGIN
    write "mttu", i, " = u(", i, ");";
  END;
END;
  
% Firstly do the dx = f(x,t) function.
OUT "$1_ode.m";
  
write "function mttdx = $1_ode(x,t);";
write "% mttdx = $1_ode(x,t);";
write "%ODE in Simulab form for system $1;;";
write "%File $1_ode.m;;";
write "%Generated by MTT;;";

common();

%Fortran switches - one line expressions
OFF echo;
ON fort$
cardno!* := 1$
fortwidth!* := 10000$
OFF period$

MTTdx := MTTdx;

SHUT "$1_ode.m";
OFF fort;
% Now do the y = g(x,t) function.
OUT "$1_odeo.m";
  
write "function mtty = $1_odeo(x,t);";
write "% mtty = $1_odeo(x,t);";
write "%ODE in Simulab form for system $1;;";
write "%File $1_odeo.m;;";
write "%Generated by MTT;;";

common();

%Fortran switches - one line expressions
OFF echo;
ON fort$
cardno!* := 1$
fortwidth!* := 500$
OFF period$

MTTy := MTTy;

SHUT "$1_odeo.m";







