#! /bin/sh

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

# Bourne shell script: dm_r2m
# Reduce descriptor state-space A,B,C,D and E matrices to Matlab
# P.J.Gawthrop 29th July 1990, 6th Feb 1991, 28 May 1991, Dec 1993, Jan 94
# Copyright (c) P.J.Gawthrop, 1989, 1990, 1991, 1993,1994.

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
###############################################################

#Inform user
echo Creating $1_dm.m

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

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

ON BigFloat, NumVal;
PRECISION 16; %Compatible with Matlab

%Read in the definitions file
in "$1_def.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";

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


OUT "$1_dm.m";

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

IF MTTNvar>0 THEN
BEGIN
  write "function [MTTA,MTTB,MTTC,MTTD,MTTE] = $1_dm(...";
  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_dm;;";


IF MTTNvar>0 THEN
BEGIN
  write "%function [MTTA,MTTB,MTTC,MTTD,MTTE] = $1_dm(...";
  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_dm;;";


write "%Linearised descriptor matrices for system $1";
write "%File $1_dm.m";
write "%Generated by MTT";

ON NERO;	% Suppress zero elements.

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

MTTN := MTTNx + 2*MTTNz + MTTNyz;

write "MTTA = zeros(", MTTN, ",", MTTN, ");";
MTTA := MTTA;

write "MTTB = zeros(", MTTN, ",", MTTNu, ");";
MTTB := MTTB;

write "MTTC = zeros(", MTTNy, ",", MTTN, ");";
MTTC := MTTC;

write "MTTD = zeros(", MTTNy, ",", MTTNu, ");";
MTTD := MTTD;

write "MTTE = zeros(", MTTN, ",", MTTN, ");";
MTTE := MTTE;

SHUT "$1_dm.m";

