File mttroot/mtt/bin/trans/make_ode2odes artifact 6ea960f185 part of check-in e6a1d0c9b1


#! /bin/sh

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

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.38  2000/10/11 09:08:08  peterg
## cse --> csex
##
## Revision 1.37  2000/08/01 12:25:06  peterg
## Now includes euler
##
## Revision 1.36  2000/05/19 17:48:16  peterg
## Argument to state
##
## Revision 1.35  2000/05/18 18:59:40  peterg
## Removed the First time stuff
##
## Revision 1.34  2000/05/16 18:56:14  peterg
## *** empty log message ***
##
## Revision 1.33  2000/05/11 19:33:18  peterg
## Uniform version for _sim.m
##
## Revision 1.32  2000/05/11 08:30:00  peterg

##
## Revision 1.31  2000/05/10 18:33:25  peterg
## Use smxa and smxax in place of smx
##
## Revision 1.30  2000/04/18 11:24:19  peterg
## Removed _numpar.
##
## Revision 1.29  2000/04/07 19:10:57  peterg
## *** empty log message ***
##
## Revision 1.28  1999/12/08 05:56:52  peterg
## Reordered the writing of the input and output.
## Note that last value now discarded.
##
## Revision 1.27  1999/11/15 22:47:53  peterg
## Generates method-specific code.
##
## Revision 1.26  1999/10/20 01:31:43  peterg
## *** empty log message ***
##
## Revision 1.25  1999/08/29 06:55:26  peterg
## Removed [MTTu]  = zero_input($Nu);	# Zero the input
## to avoide the p2c bug ????
##
## Revision 1.24  1999/08/27 06:02:16  peterg
## removed zero_input to avoid p2c bug
##
## Revision 1.23  1999/08/02 13:39:19  peterg
## Replaced zero_vector by zero_input
##
## Revision 1.22  1999/04/20 06:16:07  peterg
## Removed initialisation of AA and AAx
## Remove _switch calls -- uses _switchopen exclusively
##
## Revision 1.21  1999/04/02 06:29:25  peterg
## New implicit method - solves numerical prob with ISW
##
## Revision 1.20  1999/04/02 02:13:58  peterg
## Back to RCS
##
## Revision 1.19  1999/03/30 21:39:25  peterg
## In implicit approach, set derivatives to zero (when switch is off)
## before update. This seems to stop numerical leakage though non-return
## switches.
##
## Revision 1.18  1999/03/15 01:17:07  peterg
## Removed some spurious debugging code
##
## Revision 1.17  1999/03/15 01:09:15  peterg
## Fixed bugs when Nx=0 (no state)
##
## Revision 1.16  1999/03/06 02:28:38  peterg
## Rearranged evaluation to: state - input - output - write
##
## Revision 1.15  1999/03/06 02:19:43  peterg
## Changed args to _input
##
## Revision 1.14  1998/10/01 16:02:01  peterg
## Integration with switches handled separately fro Euler and Implicit.
##
## Revision 1.13  1998/09/30 17:41:24  peterg
## Implicit method now allows for switches via _switchA
##
## Revision 1.12  1998/08/27 08:55:18  peterg
## Mods to integration methods
##
## Revision 1.11  1998/08/25 12:28:31  peterg
## Move initila switch to after initial input
##
## Revision 1.10  1998/08/25 12:22:45  peterg
## Put _switch after update and also at initilisation
##
## Revision 1.9  1998/08/15 13:46:59  peterg
## New versions of integration routines
##
## Revision 1.8  1998/08/11 13:28:03  peterg
## Lowercase mttLAST etc
##
## Revision 1.7  1998/07/30 11:29:54  peterg
## Added implicit integration stuff
##
## Revision 1.6  1998/07/30 10:44:37  peterg
## INcluded othe integration methods.
##
## Revision 1.5  1998/07/26 11:02:20  peterg
## Put mtt or MTT  in front of variable names to avoid clashes with
## globals
##
## Revision 1.4  1998/07/25 20:14:00  peterg
## update code added for flexibility and octave efficiency
##
###############################################################


# Bourne shell script: make_ode2odes

# Copyright (c) P.J.Gawthrop July 1998.

# Tell user
Sys=$1

if [ -n "$2" ]; then
  method=$2    
else
  method=implicit  
fi

echo  "Creating $1_ode2odes.m with $method integration method"

# Find system constants
Nx=`mtt_getsize $Sys x` # States
Nu=`mtt_getsize $Sys u` # Inputs 
Ny=`mtt_getsize $Sys y` # Inputs  

if [ "$method" = "implicit" ]; then
    ode=csex
    odeo=cseo
    algorithm="mtt_implicit(x,dx,AA,AAx,ddt,$Nx,open_switches)"
else
    ode=ode
    odeo=odeo
    algorithm="mtt_euler(x,dx,ddt,$Nx,open_switches)"
fi

#cat << EOF > $1_ode2odes.m
# Program $1_ode2odes
#EOF

# Do the globals
#sympar2global_txt2m $1 >> $1_ode2odes.m
lang_header  $1 ode2odes m  'x,par,simpar' '[Y,X,t]' > $1_ode2odes.m

cat >> $1_ode2odes.m <<EOF
global MTT_data;

  if nargin<3
    simpar = $1_simpar; 
    [simpar.dt] = mtt_simpar_update;
  endif
  if nargin<2
    par = $1_numpar; 
    [par] = mtt_numpar_update(par);
  endif
  if nargin<1
    [x]  =  $1_state(par); 
    [x] = mtt_state_update(x);
  endif

  ## Initialise
  t = 0.0;
  ddt = simpar.dt/simpar.stepfactor;
  ilast = round(simpar.last/ddt)+1; # Total number of steps

## Following removed due to p2c bug
##  [u]  = zero_input($Nu);		# Zero the input
  for MTTi=1:$Nu
    u(MTTi) = 0;
  endfor;

  mttj = 0;
    for it = 1:ilast		#Integration loop
    [y] = $1_$odeo(x,u,t,par);  # Output 
    [u] = $1_input(x,y,t,par);	# Input
    if mttj==0
     mtt_write(t,x,y,$Nx,$Ny);   # Write it out
    endif
    [dx] = $1_$ode(x,u,t,par);	# State derivative
EOF

if [ "$method" = "implicit" ]; then
cat<<EOF >> $1_ode2odes.m

    [AA] = $1_smxa(x,u,ddt,par);	# (I-Adt) and (I-Adt)x
    [AAx] = $1_smxax(x,u,ddt,par); # (I-Adt) and (I-Adt)x

EOF
fi

cat <<EOF >> $1_ode2odes.m
    [open_switches] = $1_switchopen(x);	# Open switches
    [x] = $algorithm; # Integration update
    t = t + ddt;   # Time update
    mttj = mttj+1;    # Increment counter
    if mttj==simpar.stepfactor
       mttj = 0;      # Reset counter
    endif

  endfor;			# Integration loop

  t = MTT_data(:,1);
  Y = MTT_data(:,2);
  X  = MTT_data(:,4);

endfunction

EOF

exit

### old stuff follows
if [ "$method" = "euler" ]; then
cat << EOF >> $1_ode2odes.m
ddt = mttdt/mttstepfactor; # The small sample interval
EOF
fi


cat << EOF >> $1_ode2odes.m
for MTTit = 1:MTTilast		#Integration loop
  [MTTy] = $1_$odeo(MTTx,MTTu,MTTt,MTTpar);     # Output 
  [MTTu] = $1_input(MTTt,MTTx,MTTy);	# Input
  mtt_write(MTTt,MTTx,MTTy,$Nx,$Ny);    # Write it out

  if $Nx>0			# Dont if no states
EOF

if [ "$method" = "euler" ]; then
cat << EOF >> $1_ode2odes.m
#    if mttmethod==1		# Euler
      for MTTjt = 1:mttstepfactor
	[MTTdx] = $1_$ode(MTTx,MTTu,MTTt,MTTpar); # State derivative
        [MTTopen] = $1_switchopen(MTTx);        # Open switches
	    [MTTx] = mtt_euler(MTTx,MTTdx,ddt,$Nx,MTTopen); # Euler update
	MTTt = MTTt + ddt;
      endfor;
#    endif;
EOF
fi

if [ "$method" = "implicit" ]; then
cat << EOF >> $1_ode2odes.m
#    if mttmethod==2		# Implicit 
      [MTTdx] = $1_$ode(MTTx,MTTu,MTTt,MTTpar); # State derivative
      [mttAA] = $1_smxa(MTTx,MTTu,mttdt,MTTpar);	# (I-Adt) and (I-Adt)x
      [mttAAx] = $1_smxax(MTTx,MTTu,mttdt,MTTpar);	# (I-Adt) and (I-Adt)x
      [MTTopen] = $1_switchopen(MTTx);        # Open switches
      [MTTx] = $algorithm(MTTx,MTTdx,mttAA,mttAAx,mttdt,$Nx,MTTopen); # Implicit update
      MTTt = MTTt + mttdt;
#    endif;
EOF
fi

cat << EOF >> $1_ode2odes.m
    else # NX is 0 - no states
      MTTt = MTTt + mttdt;
  endif;			# $Nx>0

endfor;				# Integration loop

EOF


MTT: Model Transformation Tools
GitHub | SourceHut | Sourceforge | Fossil RSS ]