File mttroot/mtt/bin/trans/ode2odes_m artifact e593903528 part of check-in fa0e62a51a


#! /bin/sh

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

# Bourne shell script: ode2odes_m

# Transforms descriptor matrix rep to step response

# Copyright (c) P.J.Gawthrop, 1996.

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.17  1998/06/27 14:49:16  peterg
## No change
##
## Revision 1.16  1998/06/25 08:47:23  peterg
## Put correct arguments for _input -- (x,t)
##
## Revision 1.15  1998/05/21 16:20:27  peterg
## Modified to include explicit algebraic loop solution
##
## Revision 1.14  1998/05/19 19:48:02  peterg
## Read the simpar file now.
##
## Revision 1.13  1998/05/14 08:05:10  peterg
## Put back under RCS
##
## Revision 1.12  1998/02/25 18:02:39  peterg
## Removed the argument passing stuff .
## Replaced by the simpar.m method.
##
## Revision 1.11  1997/08/29 07:56:54  peterg
## Minor updates
##
# Revision 1.10  1997/01/07  09:16:03  peterg
# Added step_factor parameter - gives that number of integration steps
# per sample.
#
## Revision 1.9  1997/01/06 21:36:44  peterg
## Fixed bug mtt_error --> mtt_error.txt
## Replaced lsode by Euler integration.
##
## Revision 1.8  1996/09/13 17:54:08  peter
## Now writes default $PARAMS to $1_args.m - $1_ode may use it.
##
## Revision 1.7  1996/09/12 18:41:48  peter
## Standard error handling added.
##
## Revision 1.6  1996/08/24 14:11:04  peter
## Global parameter passing.
##
## Revision 1.5  1996/08/18 12:01:26  peter
## Unified format of time responses.
##
## Revision 1.4  1996/08/16 13:04:46  peter
## Fixed problem with more than one output (y vector).
##
## Revision 1.3  1996/08/16 06:36:03  peter
## Removed u from default arg list.
##
## Revision 1.2  1996/08/15 16:24:43  peter
## Uses T in place of t to avoid name clash within function.
##
## Revision 1.1  1996/08/15 11:56:38  peter
## Initial revision
##
###############################################################

echo Creating $1_odes.m
echo Creating $1_odeso.m

rm -f ode2odes_m.log
rm -f mtt_error.txt

#if [ "$2" = "" ]; 
#then
#  PARAMS='T=[0:0.1:10]; x0=zeros(nx,1);'
#  echo Using default parameter $PARAMS
#  echo $PARAMS>$1_args.m
#else
#  PARAMS=$2;
#fi

# PARAMS="$PARAMS ;"


$MATRIX --verbose << EOF > ode2odes_m.log 2>mtt_error.txt

  %System structure
  [nx,ny,nu,nz,nyz] = $1_def;

  %Read in parameters
  $1_numpar; 

  %Read in state
  x = $1_state;

  %Set the initial output
  %if ny>0
  %  y = $1_odeo(x,0);
  %end; 
 
  %Read in simulation parameters
  $1_simpar;
  T = [0:DT:LAST];

  t=0;	%Just in case it appears in the parameter list.


  %Defaults
  if exist('T')==0
    T=[0:1:100]
  end;

  if exist('METHOD')==0
    METHOD = 'Euler'
  end;

  if exist('x')==0
    x = zeros(nx,1);
   end;

  % xx is the composite vector containing x and the internal inputs.
  xx = [x; zeros(nyz,1)];

  [n,m]=size(T);
  if m>n
    T=T';
  end;

method = tolower(METHOD)

if nx>0
  if strcmp(method,'lsode')
    X = lsode('$1_ode', x, T);
  elseif strcmp(method,'euler')
    %Euler integration
    disp("Euler")
    X=[];
    dt = (T(2)-T(1))/STEPFACTOR;
    for t=T'
      X = [X; xx'];
      ts = t;
      for i=1:STEPFACTOR
        x = xx(1:nx);
        u = $1_input(xx,ts);
        xx = $1_ode(xx,u,ts);
        ts = ts + dt;
        dx = xx(1:nx);
        x = x + dx*dt;
        xx(1:nx) = x;
      end;
    end;
  elseif strcmp(method,'implicitl')
    %Euler integration
    X=[];
    dt = (T(2)-T(1))/STEPFACTOR;
    u = $1_input(x,u,t);
    A = $1_smx(x,u); 
    inverse = inv(eye(nx) - dt*A);
    for t=T'
      X = [X; xx'];
      ts = t;
      u = $1_input(x,t);
      for i=1:STEPFACTOR
        x = xx(1:nx);
        xx = $1_ode(xx,u,ts);
        ts = ts + dt;
        dx = xx(1:nx);
        x = inverse*(x + dt*(dx - A*x));
        xx(1:nx) = x;
      end;
    end;
  elseif strcmp(method,'implicit')
    %Euler integration
    X=[];
    dt = (T(2)-T(1))/STEPFACTOR;
    One = eye(nx);
    for t=T'
      X = [X; xx'];
      ts = t;
      for i=1:STEPFACTOR
        x = xx(1:nx);
        u = $1_input(x,t);
    	A = $1_smx(x,u); 
        xx = $1_ode(xx,u,ts);
        ts = ts + dt;
        dx = xx(1:nx);
        x = (One-A*dt)\(x + dt*(dx - A*x));
        xx(1:nx) = x;
      end;
    end;
  else
    error('Method %s not available here', METHOD);
    return;
  end;
  write_matrix([T,X], '$1_odes');
else
  X = zeros(size(T));
end;

if ny>0 % compute y and print it
  i = 0; Y=[];
  for t=T'
    i = i+1; X(i,:);
    u = $1_input(x,t);
    y = $1_odeo(X(i,:)',u,t);
    Y = [Y; y'];
  end;
  write_matrix([T,Y], '$1_odeso');
end;


EOF

# Now invoke the standard error handling.
mtt_error mtt_error.txt









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