1 ## Copyright (C) 2006,2007,2008,2011 Carlo de Falco
3 ## This file is part of:
4 ## OCS - A Circuit Simulator for Octave
6 ## OCS is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation.
10 ## This program is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ## GNU General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with this program (see the file LICENSE); if not,
17 ## see <http://www.gnu.org/licenses/>.
19 ## author: Carlo de Falco <cdf _AT_ users.sourceforge.net>
23 ## @deftypefn{Function File} {[@var{out}] =} tst_daspk @
24 ## (@var{cirstruct},@var{x},@var{t},@var{tol},@var{maxit},@
25 ## @var{pltvars},@var{verbosity},@var{daskopts},@var{dae_fun});
27 ## Perform a transient simulation of the system described by
28 ## @var{cirstruct} over the time interval @var{t} using @code{daspk}.
30 ## The initial value for the state vector is computed by solving a
31 ## steady state problem at @var{t}(1), with starting guess @var{x}.
33 ## @var{tol} and @var{maxit} are parameters passed to
34 ## @code{nls_newton_raphson}.
36 ## The output @var{out} will contain the value of the state vector
37 ## at each point of @var{t}.
39 ## Extra options for @code{daspk} can be passed as name/value pairs in
40 ## the cellarray @var{daskopts}.
42 ## The optional parameter @var{verbosity} controls the amount of
46 ## @item if verbosity(1) != 0, information on the progress
47 ## of the algorithm are output at runtime
48 ## @item if verbosity(2) != 0, the plot of the variables whose names
49 ## are listed in @var{pltvars} is
50 ## produced after the computation
53 ## For special purposes one may need to pass modified jacobian and
54 ## residual functions. This can be done via the cell array of function
55 ## handles @var{dae_fun}.
57 ## Such functions should have the same input and output
58 ## parameter list as the default sub-functions
59 ## TSTBWEFUNJAC0,TSTBWEFUNRES0, TSTBWEFUNJAC,TSTBWEFUNRES.
61 ## @seealso{tst_backward_euler,tst_odepkg,tst_theta_method,nls_newton_raphson,daspk}
65 function [out] = tst_daspk (outstruct, x, t, tol, maxit, pltvars, verbosity, daspkopts, dae_fun)
67 ## FIXME: add input check!
68 if ((nargin < 6) || (nargin > 9))
69 error ("tst_daspk: wrong number of input parameters.");
72 if (! exist ("verbosity"))
74 elseif (length (verbosity) < 2)
79 fprintf (1, "initial value:\n");
82 daspk_options ("print initial condition info",1);
83 daspk_options ("maximum order",2);
84 daspk_options ("initial step size",t(2)-t(1));
85 daspk_options ("relative tolerance",1e-3);
88 for ii = 1:2:length (daspkopts)
89 daspk_options (daspkopts{ii}, daspkopts{ii+1});
94 [A0, B, C] = asm_initialize_system (outstruct, x);
97 JAC = @(x) dae_fun{1} (outstruct, x, t(1), B);
98 RES = @(x) dae_fun{2} (outstruct, x, t(1), B, C);
99 [x, ii, resnrm] = nls_newton_raphson (x, RES, JAC, tol, maxit, verbosity);
101 [out(:,1), ii] = nls_stationary (outstruct, x, tol, maxit);
105 JAC = @(x) dae_fun{3} (outstruct, x, t(1), B);
106 RES = @(x) dae_fun{4} (outstruct, x, t(1), B, C);
108 JAC = @(x,xdot,t,c) TSTDASPKFUNJAC (outstruct, x, xdot, A0, B, t, c);
109 RES = @(x,xdot,t) TSTDASPKFUNRES (outstruct, x, xdot, A0, B, C, t);
112 [out, xdot, istate, msg] = daspk ({RES,JAC}, out(:,1), zeros (size (x)), t);
116 utl_plot_by_name (t, out, outstruct, pltvars)
121 ## Jacobian for transient problem
122 function lhs = TSTDASPKFUNJAC (outstruct, x, xdot, A0, B, t, c)
124 [A1, Jac, res] = asm_build_system (outstruct, x, t);
125 lhs = (c*(A0+A1) + B + Jac);
129 ## Residual for transient problem
130 function rhs = TSTDASPKFUNRES (outstruct, x, xdot, A0, B, C, t)
132 [A1, Jac, res] = asm_build_system (outstruct, x, t);
133 rhs = (A0+A1)*xdot + B*x + C + res;