1 ## Copyright (C) 2006,2007,2008 Carlo de Falco, Culpo Massimiliano
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>
20 ## author: culpo@math.uni-wuppertal.de
24 ## @deftypefn{Function File} {[@var{A},@var{B},@var{C}] =}@
25 ## asm_initialize_system(@var{instruct},@var{x})
27 ## Cycle through the circuit description structure @var{instruct}
28 ## to build the system matrices @var{A}, @var{B}, @var{C} for
29 ## the linear and time-invariant part of the system.
31 ## @var{x} is the current value of the state variables.
33 ## See the @cite{IFF file format specifications} for details about
34 ## the output matrices.
36 ## @seealso{asm_build_system,prs_iff}
40 function [A,B,C] = asm_initialize_system(instruct,x);
44 error("asm_initialize_system: wrong number of input parameters.");
45 elseif !(isstruct(instruct) && isfield(instruct,"LCR") &&
46 isfield(instruct,"NLC"))
47 error("asm_initialize_system: first input is not a valid structure.");
49 error("asm_initialize_system: second input is not a valid vector.");
52 ## Build linear part of the system
53 n = instruct.totextvar + instruct.totintvar; # Number of variables
55 ## Initialize to zero any state variable that is not in the input argument
60 ## FIXME: should a warning be passed if lx != n ?
68 nblocks = length(instruct.LCR);
71 for iel = 1:instruct.LCR(ibl).nrows
74 if instruct.LCR(ibl).nintvar(iel)
75 intvars = instruct.totextvar + instruct.LCR(ibl).osintvar(iel) + [1:instruct.LCR(ibl).nintvar(iel)]';
80 il = instruct.LCR(ibl).vnmatrix(iel,:)';
84 y(nzil) = x(il(nzil));
87 [a,b,c] = feval(instruct.LCR(ibl).func,
88 instruct.LCR(ibl).section,
89 instruct.LCR(ibl).pvmatrix(iel,:),
90 instruct.LCR(ibl).parnames,
96 vars = [il(nzil);intvars];
99 lclvars = [nzil; instruct.LCR(ibl).nextvar + (1:length(intvars))' ];
101 ## Reshaping sparse stamps
102 a = a(lclvars,lclvars);
103 b = b(lclvars,lclvars);
104 c = reshape(c(lclvars),[],1);
106 [na,ma,va] = find(a);
107 [nb,mb,vb] = find(b);
108 [nc,mc,vc] = find(c);
111 A += sparse(vars(na),vars(ma),va,n,n);
112 B += sparse(vars(nb),vars(mb),vb,n,n);
113 C += sparse(vars(nc),1,vc,n,1);