#! /bin/sh
######################################
##### Model Transformation Tools #####
######################################
# Bourne shell script: ode_r2c
# Reduce ordinary differential equations to c differential-algebraic
# equations
# Euler integration of the state is included.
# NB Arrays should be defined to be one larger than expected
# - the 0 element is not used.
# Copyright (c) P.J.Gawthrop 1997.
###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.1 1997/01/21 10:52:23 peterg
## Initial revision
##
###############################################################
# Inform user
echo Creating $1_ode.c
# Remove the old files
rm -f $1_ode.c1 $1_ode.c2 $1_ode.c3 $1_ode.c
# Remove the old log file
rm -f ode_r2c.log
# Use reduce to accomplish the transformation
reduce >ode_r2c.log << EOF
%Read the reduce definitions file
in "$1_def.r";
%Set up the number of argument variables to zero in case the user has forgotten
MTTNVar := 0;
%Read the symbolic parameters file
in "$1_sympar.r";
%Read the reduce state-space equations file
in "$1_ode.r";
ON BigFloat, NumVal;
PRECISION 16; %Compatible with Matlab
%OFF Nat;
ON NERO; % Suppress zero elements
%Generate the Header part
OUT "$1_ode.c1";
write "/*"$
write "Differential algebraic eqns in c form for system $1"$
write "NB Arrays should be defined to be one larger than expected"$
write " - the 0 element is not used."$
write "File $1_ode.c"$
write "Generated by MTT"$
write "*/"$
write " "$
%Function heading - c style
write "void $1_ode()"$
write " "$
write "{"$
write "/* Declare standard arrays */"$
write " extern float y[", MTTNy+1, "]; /* $1_ode output */"$
write " extern float dx[", MTTNx+1, "]; /* $1_ode state derivative */"$
write " extern float x[", MTTNx+1, "]; /* $1_ode state */"$
write " extern float u[", MTTNu+1, "]; /* $1_ode input */"$
%External (global) variable list
write "/* External (global) variable list */ "$
IF MTTNvar>0 THEN
BEGIN
FOR i := 1:MTTNvar DO
IF numberp(MTTVar(i,1))
THEN
BEGIN
% Do nowt
END
ELSE
BEGIN
write "extern float ", MTTVar(i,1), ";"$
END$
END$
%Declare the dummy variables t0--t9
write "/* Dummy variable list */ "$
write " float t0;"$
FOR i := 1:9 DO
BEGIN
write " float t", i, ";"$
END$
%Declarations$
write "/* State variable list */ "$
FOR i := 1:MTTNx DO
BEGIN
write " float mttx", i, ";"$
END$
write "/* Input variable list */ "$
FOR i := 1:MTTNu DO
BEGIN
write " float mttu", i, ";"$
END$
write "/* Counter */ "$
write " int i;"$
write " "$
write " /*====== Set up the state variables ======*/"$
FOR i := 1:MTTNx DO
BEGIN
write " mttx", i, " = x[", i, "];"$
END$
write " "$
write " /*====== Set up the input variables ======*/"$
IF MTTNu>0 THEN
BEGIN
FOR i := 1:MTTNu DO
BEGIN
write " mttu", i, " = u[", i, "];"$
END$
END$
write " "$
write " /*====== Compute the state derivative and output ======*/"$
SHUT "$1_ode.c1";
% Load the general translator package
LOAD GENTRAN;
GENTRANLANG!* := 'C;
ON GENTRANSEG;
MAXEXPPRINTLEN!* := 80;
% let it know that sign is a function
GENTRAN DECLARE sign : function;
GENTRANOUT "$1_ode.c2";
%Do the translation
%State
IF MTTNx>0 THEN
BEGIN
FOR i := 1:MTTNx DO
BEGIN
GENTRAN dx(i) ::=: mttdx(i,1)$
END
END$
%Output
IF MTTNy>0 THEN
BEGIN
FOR i := 1:MTTNy DO
BEGIN
GENTRAN y(i) ::=: mtty(i,1)$
END
END$
GENTRANSHUT "$1_ode.c2";
EOF
echo '};' > $1_ode.c3
cat $1_ode.c1 $1_ode.c2 $1_ode.c3> $1_ode.c