#! /bin/sh

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

# Bourne shell script: ode2obs_r
# Odrinary differential equations to observer function equations
# P.J.Gawthrop 14 June 1991, 8 Aug 1991, 2 April 1992, 14 April 1994, 28 Dec 94,
#		12th July 1995, April 1996
# Copyright (c) P.J.Gawthrop 1991, 1992, 1994, 1995, 1996.

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

#Inform user
echo Creating $1_obs.r

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

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

OFF Echo;
OFF Nat;
ON NERO;

in "$1_def.r";
MTTdxs := MTTdX;  %Save the symbolic form of dX

%Set default values - reset by sympar file.
MTTGPCNy := 2;
MTTGPCNu := 0;

in "$1_sympar.r";
in "$1_ode.r";


%Create the U vector of input derivatives.
MTTNuu := (MTTGPCNu+1)*MTTNu;
MTTNuu1 := MTTGPCNu*MTTNu;
Matrix MTTUU(MTTNuu,1);

FOR i := 1:MTTNu DO
  MTTUU(i,1) := MTTU(i,1);
END;

IF MTTGPCNu>0 THEN
FOR i := 1:MTTNu DO
  MTTUU(i+MTTNu,1) := MTTdU(i,1);
END;

MTTU := MTTU;
MTTdU := MTTdU;
MTTUU := MTTUU;

%Create the Y vector of output derivatives.
MTTNyy := (MTTGPCNy+1)*MTTNy;
Matrix MTTYY(MTTNyy,1);

FOR i := 1:MTTNy DO
  MTTYY(i,1) := MTTY(i,1);
END;

l := MTTNy;
FOR i := 1:MTTGPCNy DO
    FOR j := 1:MTTNy DO
    BEGIN
    l := l+1;
    MTTYY(l,1) := 0;
    FOR k := 1:MTTNx DO %Derivatives wrt x
    BEGIN
      xk := MTTX(k,1);
      MTTYY(l,1) := MTTYY(l,1) + df(MTTYY(l-MTTNy,1), xk, 1)*MTTdX(k,1);
    END;
    IF MTTNuu1>0 THEN
    FOR k := 1:MTTNuu1 DO %Derivatives wrt u
    BEGIN
      uk := MTTUU(k,1);
      MTTYY(l,1) := MTTYY(l,1) + df(MTTYY(l-MTTNy,1), uk, 1)*MTTUU(k+MTTNu,1);
    END;
  END;
END;

%Create O_x - derivative of YY wrt x
Matrix MTTO_x(MTTNyy,MTTNx);
FOR j := 1:MTTNx DO
  BEGIN
  xj := MTTX(j,1);
  FOR i := 1:MTTNyy DO
    BEGIN
    MTTO_x(i,j) := df(MTTYY(i,1), xj);
    END;
  END;

%Create O_u - derivative of YY wrt u (Assumes GPC Nu = 0)
MTTNNu := (MTTGPCNu+1)*MTTNu;
Matrix MTTO_u(MTTNyy,MTTNNu);
FOR j := 1:MTTNNu DO
  BEGIN
  uj := MTTu(j,1);	
  FOR i := 1:MTTNyy DO
    BEGIN
    MTTO_u(i,j) := df(MTTYY(i,1), uj);
    END;
  END;


%Create O_uu - derivative of O_u wrt u (Assumes GPC Nu = 0)
%This is a multi-dimensional matrix kth elements stacked sideways.
Matrix MTTO_uu(MTTNyy,MTTNNu*MTTNNu);
FOR k := 1:MTTNNu DO
  BEGIN
  uk := MTTu(k,1);	
  FOR j := 1:MTTNNu DO
    BEGIN
    FOR i := 1:MTTNyy DO
      BEGIN
      jk := j+(k-1)*MTTNu;
      MTTO_uu(i,jk) := df(MTTO_u(i,j), uk);
      END;
    END;
  END;

%Create O_ux - derivative of O_u wrt x 
%This is a multi-dimensional matrix kth elements stacked sideways.
Matrix MTTO_ux(MTTNyy,MTTNu*MTTNx);
FOR k := 1:MTTNx DO
  BEGIN
  xk := MTTx(k,1);	
  FOR j := 1:MTTNu DO
    BEGIN
    FOR i := 1:MTTNyy DO
      BEGIN
      jk := j+(k-1)*MTTNu;
      MTTO_ux(i,jk) := df(MTTO_u(i,j), xk);
      END;
    END;
  END;



%%Create the _obs.r file
OUT "$1_obs.r";

MTTYY := MTTYY;
MTTO_x := MTTO_x;
MTTO_u := MTTO_u;
MTTO_uu := MTTO_uu;
MTTO_ux := MTTO_ux;

write ";END;";

SHUT "$1_obs.r";
quit;

EOF


#Reduce writes errors to std output with ***** in front.
grep '\*\*\*\*\*' ode2obs_r.log >mtt_error
# Test for errors and print if any
err_length=$(wc -c <mtt_error)
if [ $err_length != "0" ]
then
  echo MTT has failed with the following errors '...'
  cat mtt_error
  exit 1
else
  exit 0
fi






