#! /bin/sh

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

# Bourne shell script: csm_r2m
# Reduce constained state-space A,B,C,D and E matrices to Matlab
# P.J.Gawthrop July 1990, Feb 1991, May 1991, Sep 91, Dec 93, Jan 94, May 94.
# Copyright (c) P.J.Gawthrop, 1989, 1990, 1991, 1993, 1994.

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.2  1996/08/19 18:40:02  peter
## read in the nuim_par function etc.
##
## Revision 1.1  1996/08/19 16:27:04  peter
## Initial revision
##
###############################################################

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

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

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

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

%Read the reduce state-space A,B,C and D matrices file
in "$1_$2.r";

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

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


OUT "$1_$2.m";

%Headings - Matlab style
%(Note. The ;; are deleted by for2mat)

IF MTTNvar>0 THEN
BEGIN
  write "function [MTTA,MTTB,MTTC,MTTD,MTTE] = $1_$2(...";
  FOR i := 1:MTTNvar DO
  BEGIN
     IF i<MTTNvar THEN write MTTVar(i,1), ",..."
                  ELSE write MTTVar(i,1), ");;";
  END;
END
ELSE write "function [MTTA,MTTB,MTTC,MTTD,MTTE] = $1_$2;;";


IF MTTNvar>0 THEN
BEGIN
  write "%function [MTTA,MTTB,MTTC,MTTD,MTTE] = $1_$2(...";
  FOR i := 1:MTTNvar DO
  BEGIN
     IF i<MTTNvar THEN write "%", MTTVar(i,1), ",..."
                  ELSE write "%", MTTVar(i,1), ");;";
  END;
END
ELSE write "%function [MTTA,MTTB,MTTC,MTTD,MTTE] = $1_$2;;";


write "%Linearised constrained-state matrices for system $1";
write "%File $1_$2.m";
write "%Generated by MTT";

IF MTTNvar>0 THEN
BEGIN
  write "% Read in the parameters";
  write "[ ...;;";
  FOR i := 1:MTTNvar DO
  BEGIN
     IF i<MTTNvar THEN write MTTVar(i,1), ",..."
                  ELSE write MTTVar(i,1), "] = $1_numpar"
  END;
  write "% Read in the arguments";
  write "$1_args";
END;

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

write "MTTA = zeros(", MTTNx, ",", MTTNx, ");";
write "MTTB = zeros(", MTTNx, ",", MTTNu, ");";
write "MTTC = zeros(", MTTNy, ",", MTTNx, ");";
write "MTTD = zeros(", MTTNy, ",", MTTNu, ");";
write "MTTE = zeros(", MTTNx, ",", MTTNx, ");";

ON NERO;	% Suppress zero elements.
MTTA := MTTA;
MTTB := MTTB;
MTTC := MTTC;
MTTD := MTTD;
MTTE := MTTE;

SHUT "$1_$2.m";

