#! /bin/sh

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

# Bourne shell script: sspar2ss_r
# Reduce steady steady-state parameter file to reduce steady-state:
#	X0, and Y0 and U0;
# P.J.Gawthrop 10 July 1990, 22 April 1991, Dec 1993, April 1994
# Copyright (c) P.J.Gawthrop, 1990, 1994.

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.2  1998/07/22 15:47:28  peterg
## Now operates on ordinary differential equation file.
##
## Revision 1.1  1996/08/25 09:52:20  peter
## Initial revision
##
###############################################################

#Inform user
echo Creating $1_ss.r

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

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

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

%Read the constitutive relationships (May be used in rss file). 
%in "$1_cr.r";

%Read the steady-state values 
in "$1_sspar.r";

%Read in the constrained-state equation
%in "$1_cse.r";

%Read in the ode
in "$1_ode.r";

%Read the formatting function
in "$MTTPATH/trans/reduce_matrix.r";

OFF Echo;
OFF Nat;
OUT "$1_ss.r";

IF MTTNx>0 THEN
  MTTdx:= MTTdx;

%Write out the steady-state values (if any)
write "%Steady-state values";


% State
MTT_Matrix := MTTX$ 
MTT_Matrix_name := "MTTx"$
MTT_Matrix_n := MTTNx$
MTT_Matrix_m := 1$
Reduce_Matrix()$

% State derivative
MTT_Matrix := MTTdX$ 
MTT_Matrix_name := "MTTdX"$
MTT_Matrix_n := MTTNx$
MTT_Matrix_m := 1$
Reduce_Matrix()$

% Nonstate
MTT_Matrix := MTTZ$ 
MTT_Matrix_name := "MTTZ"$
MTT_Matrix_n := MTTNz$
MTT_Matrix_m := 1$
Reduce_Matrix()$

% Output
MTT_Matrix := MTTy$ 
MTT_Matrix_name := "MTTy"$
MTT_Matrix_n := MTTNy$
MTT_Matrix_m := 1$
Reduce_Matrix()$

% Zero outputs
MTT_Matrix := MTTYz$ 
MTT_Matrix_name := "MTTYz"$
MTT_Matrix_n := MTTNyz$
MTT_Matrix_m := 1$
Reduce_Matrix()$

% Input
MTT_Matrix := MTTu$ 
MTT_Matrix_name := "MTTu"$
MTT_Matrix_n := MTTNu$
MTT_Matrix_m := 1$
Reduce_Matrix()$


%Write them out by component - but only if values have been set
%(This assumes that if element 1 not set then the rest aren't either)
%ON Rounded; %Make numbers have dec. points-- nice for c
%
%clear MTTx1;
%IF MTTNx>0 THEN
%IF MTTX0(1,1) NEQ MTTX1 THEN
%FOR i := 1:MTTNx DO
%BEGIN
%  write "MTTx", i, " := ", MTTX0(i,1);
%END;
%
%clear MTTu1;
%IF MTTNu>0 THEN
%IF MTTu0(1,1) NEQ MTTu1 THEN
%FOR i := 1:MTTNu DO
%BEGIN
%  write "MTTu", i, " := ", MTTu0(i,1);
%END;
%
%%Derivatives are zero in the steady-state
%IF MTTNz>0 THEN
%FOR i := 1:MTTNz DO
%BEGIN
%  write "MTTdz", i, " := 0";
%END;

write "END;";

SHUT "$1_ss.r";
quit;
EOF

# Now invoke the standard error handling.
mtt_error_r sspar2ss_r.log
