1 ## Copyright (C) 2006,2007,2008 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},@var{niter}] =} tst_theta_method @
24 ## (@var{cirstruct},@var{x},@var{t},@var{tol},@
25 ## @var{maxit},@var{theta},@var{pltvars},@
28 ## Perform a transient simulation of the system described by
29 ## @var{cirstruct} over the time interval @var{t} using the
30 ## theta-method with parameter @var{theta}.
32 ## The initial value for the state vector is computed by solving a
33 ## steady state problem at @var{t}(1), with starting guess @var{x}.
35 ## @var{tol} and @var{maxit} are parameters passed to
36 ## @code{nls_newton_raphson}.
38 ## The output @var{out} will contain the value of the state vector at
39 ## each point of @var{t}.
41 ## The optional parameter @var{verbosity} controls the amount of
45 ## @item if verbosity(1) != 0, information on the progress
46 ## of the algorithm are output at runtime
47 ## @item if verbosity(2) != 0, the plot of the variables whose names
48 ## are listed in @var{pltvars} is
49 ## produced after the computation
52 ## The optional output @var{niter} returns the number of Newton iterations
53 ## needed to reach convergence.
55 ## @seealso{tst_backward_euler,tst_daspk,tst_odepkg,nls_newton_raphson}
59 function [out, varargout] = tst_theta_method(outstruct,x,t,tol,maxit,\
60 theta,pltvars,verbosity)
63 ## FIXME: add input check!
64 if ((nargin < 7) || (nargin > 8))
65 error("tst_theta_method: wrong number of input parameters.");
68 if ~exist("verbosity")
70 elseif length(verbosity)<2
74 out = zeros(rows(x),columns(t));
78 niter = zeros(length(t),1);
82 fprintf(1,"Initial value.\n");
86 [A0,B,C] = asm_initialize_system(outstruct,x);
88 [out(:,1),ii] = nls_stationary(outstruct,x,tol,maxit);
97 fprintf(1,"Timestep #%d.\n",it);
101 [A1old,Jacold,resold] = asm_build_system(outstruct, out(:,it-1), t(it-1));
103 JAC = @(x,A1,Jac,res) TSTTHETAFUNJAC1(outstruct, x, t(it-1),
106 RES = @(x,A1,Jac,res) TSTTHETAFUNRES1(outstruct, x, out(:,it-1),
107 t(it-1), t(it), A0, B, C,
108 resold, theta, A1, Jac, res);
109 UPDT = @(x) TSTTHETAFUNUP1 (outstruct, x, t(it));
111 [out(:,it),ii,resnrm] = nls_newton_raphson(out(:,it-1),RES,JAC,\
112 tol, maxit,verbosity(1),\
120 utl_plot_by_name(t(1:it),out(:,1:it),outstruct,pltvars);
124 if exist("~/.stop_ocs","file")
130 varargout{1} = niter;
135 ## Jacobian for transient problem
136 function lhs = TSTTHETAFUNJAC1(outstruct, x, t0, t1, A0, B, theta, A1, Jac, res)
140 [A1,Jac,res] = asm_build_system(outstruct,x,t1);
142 lhs = ( (A0+A1)/DT + theta*(B + Jac) );
145 ## Residual for transient problem
146 function rhs = TSTTHETAFUNRES1(outstruct, x, xold, t0, t1, A0, B, C,
147 resold, theta, A1, Jac, res)
150 [A1,Jac,res] = asm_build_system(outstruct,x,t1);
152 rhs = ( (A1+A0)*(x-xold)/DT + theta * (res + C + B*x) +
153 (1-theta) * (resold + C + B*xold) );
156 ## Update for transient problem
157 function update = TSTTHETAFUNUP1(outstruct,x,t1)
159 [A1,Jac,res] = asm_build_system(outstruct,x,t1);
160 update = {A1,Jac,res};