Mercurial > hg > ltpda
comparison m-toolbox/classes/@ssm/simulate.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 % SIMULATE simulates a discrete ssm with given inputs | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % DESCRIPTION: SIMULATE simulates a discrete ssm with given inputs. | |
5 % | |
6 % CALL: [mat_out pl_out] = simulate(sys, pl) | |
7 % | |
8 % INPUTS: | |
9 % - sys, (array of) ssm object | |
10 % | |
11 % OUTPUTS: | |
12 % _ mat_out contains specified returned aos | |
13 % _ pl_out contains 'lastX', the last state position | |
14 % | |
15 % <a href="matlab:utils.helper.displayMethodInfo('ssm', 'simulate')">Parameters Description</a> | |
16 % | |
17 % VERSION: $Id: simulate.m,v 1.104 2011/07/11 10:44:38 adrien Exp $ | |
18 % | |
19 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
20 | |
21 % TO DO: options to be defined (NL case) | |
22 % allow use of other LTPDA functions to generate white noise | |
23 | |
24 | |
25 function varargout = simulate(varargin) | |
26 | |
27 %% starting initial checks | |
28 | |
29 % use the caller is method flag | |
30 callerIsMethod = utils.helper.callerIsMethod; | |
31 | |
32 % Check if this is a call for parameters | |
33 if utils.helper.isinfocall(varargin{:}) | |
34 varargout{1} = getInfo(varargin{3}); | |
35 return | |
36 end | |
37 | |
38 utils.helper.msg(utils.const.msg.PROC3, ['running ', mfilename]); | |
39 | |
40 % Collect input variable names | |
41 in_names = cell(size(varargin)); | |
42 for ii = 1:nargin,in_names{ii} = inputname(ii);end | |
43 | |
44 % Collect all SSMs and plists | |
45 [sys, ssm_invars, rest] = utils.helper.collect_objects(varargin(:), 'ssm', in_names); | |
46 [pl, invars2, rest] = utils.helper.collect_objects(rest(:), 'plist'); | |
47 if ~isempty(rest) | |
48 pl = combine(pl, plist(rest{:})); | |
49 end | |
50 pl = combine(pl, getDefaultPlist()); | |
51 | |
52 | |
53 %% begin function body | |
54 | |
55 tini = pl.find('t0'); | |
56 if isempty(tini) | |
57 tini = pl.find('tini'); % check for the old name | |
58 if isempty(tini) | |
59 tini = time; | |
60 else | |
61 warning('The use of the parameter ''tini'' is deprecated and will be removed in future releases. Use ''t0'' instead'); | |
62 end | |
63 end | |
64 if isa(tini,'double') | |
65 tini = time(tini); | |
66 end | |
67 | |
68 | |
69 %% retrieve system infos | |
70 if numel(sys)~=1 | |
71 error('simulate needs exactly one ssm as an input') | |
72 end | |
73 if ~sys.isnumerical | |
74 error(['error because system ',sys.name,' is not numerical']); | |
75 end | |
76 timestep = sys.timestep; | |
77 if timestep==0 | |
78 error('timestep should not be 0 in simulate!!') | |
79 end | |
80 | |
81 if callerIsMethod | |
82 % we don't need the history of the input | |
83 else | |
84 inhist = sys.hist; | |
85 end | |
86 | |
87 if pl.isparam('noise variable names') | |
88 error('The noise option used must be split between "covariance" and "cpsd". "noise variable names" does not exist anymore!') | |
89 end | |
90 | |
91 %% display time ? | |
92 displayTime = find(pl, 'displayTime'); | |
93 | |
94 %% initial state | |
95 ssini = find(pl,'ssini'); | |
96 if isempty(ssini) | |
97 initialize = find(pl, 'initialize'); | |
98 if initialize | |
99 ssini = sys.steadyState(pl); | |
100 ssini = find(ssini, 'state'); | |
101 else | |
102 ssini = cell(sys.Nss,1); | |
103 for i=1:sys.Nss | |
104 ssini{i} = zeros(sys.sssizes(i),1); | |
105 end | |
106 end | |
107 end | |
108 ssSizesIni = sys.statesizes; | |
109 SSini = double(ssm.blockMatFusion(ssini,ssSizesIni,1)); | |
110 | |
111 %% collecting simulation i/o data | |
112 | |
113 % values | |
114 aos_in = find(pl, 'aos'); | |
115 constants_in = find(pl, 'constants'); | |
116 cov_in = find(pl, 'covariance'); | |
117 cpsd_in = find(pl, 'CPSD'); | |
118 noise_in = blkdiag(cov_in, cpsd_in/(timestep*2)); | |
119 [U1,S1,V1] = svd(noise_in.'); %#ok<NASGU> | |
120 if (sum(S1<0)>0) | |
121 error('Covariance matrix is not positive definite') | |
122 end | |
123 noise_mat = U1*sqrt(S1); | |
124 | |
125 %% modifying system's ordering | |
126 if find(pl, 'reorganize') | |
127 sys = reorganize(sys, pl, 'set', 'for simulate', 'internal', 'internal'); | |
128 end | |
129 | |
130 %% getting system's i/o sizes | |
131 inputSizes = sys.inputsizes; | |
132 outputSizes = sys.outputsizes; | |
133 | |
134 Naos_in = inputSizes(1); | |
135 Nnoise = inputSizes(2); | |
136 Nconstants = inputSizes(3); | |
137 NstatesOut = outputSizes(1); | |
138 NoutputsOut = outputSizes(2); | |
139 | |
140 if numel(aos_in)~=Naos_in | |
141 error(['There are ' num2str(numel(aos_in)) ' input aos and ' num2str(Naos_in) ' corresponding inputs indexed.' ]) | |
142 elseif numel(diag(noise_in))~=Nnoise | |
143 error(['There are ' num2str(numel(diag(noise_in))) ' input noise variances and ' num2str(Nnoise) ' corresponding inputs indexed.' ]) | |
144 elseif numel(constants_in)~=Nconstants | |
145 error(['There are ' num2str(numel(constants_in)) ' input constants and ' num2str(Nconstants) ' corresponding inputs indexed.' ]) | |
146 end | |
147 | |
148 A = sys.amats{1,1}; | |
149 Coutputs = sys.cmats{2,1}; | |
150 Cstates = sys.cmats{1,1}; | |
151 Baos = sys.bmats{1,1}; | |
152 Daos = sys.dmats{2,1}; | |
153 Bnoise = sys.bmats{1,2} * noise_mat; | |
154 Dnoise = sys.dmats{2,2} * noise_mat; | |
155 Bcst = sys.bmats{1,3} * reshape(constants_in, Nconstants, 1); | |
156 Dcst = sys.dmats{2,3} * reshape(constants_in, Nconstants, 1); | |
157 | |
158 %% getting correct number of samples | |
159 Nsamples = find(pl, 'Nsamples'); | |
160 f0 = 1/timestep; | |
161 for i=1:Naos_in | |
162 Nsamples = min(Nsamples,length(aos_in(i).y)); | |
163 try | |
164 if ~(f0==aos_in(i).fs) | |
165 str = ['WARNING : ssm frequency is ',num2str(f0),... | |
166 ' but sampling frequency of ao named ',... | |
167 aos_in(i).name, ' is ', num2str(aos_in(i).fs) ]; | |
168 utils.helper.msg(utils.const.msg.MNAME, str); | |
169 end | |
170 end | |
171 % maybe tdata should be retrieved and verified to be equal, rather than this. | |
172 end | |
173 if Nsamples == inf % case there is no input! | |
174 error('warning : no input option ''Nsamples'' providing simulation duration is available!!') | |
175 Nsamples = 0; | |
176 end | |
177 | |
178 %% termination condition | |
179 if strcmp(find(pl, 'termincond'),''); | |
180 doTerminate = false; | |
181 terminationCond = ''; | |
182 else | |
183 doTerminate = true; | |
184 terminationCond = find(pl, 'termincond'); | |
185 end | |
186 | |
187 %% ao vector | |
188 aos_vect = zeros(Naos_in, Nsamples); | |
189 for j = 1:Naos_in | |
190 aos_vect(j,:) = aos_in(j).y(1:Nsamples).'; | |
191 end | |
192 | |
193 %% simulation loop | |
194 [x, y, lastX] = ssm.doSimulate(... | |
195 SSini, Nsamples, ... | |
196 A, Baos, Coutputs, Cstates, Daos, Bnoise, Dnoise, Bcst, Dcst,... | |
197 aos_vect, doTerminate, terminationCond, displayTime, timestep, pl.find('force complete')); | |
198 | |
199 %% saving in aos | |
200 fs = 1/timestep; | |
201 isysStr = sys.name; | |
202 | |
203 ao_out = ao.initObjectWithSize(1,NstatesOut+NoutputsOut); | |
204 for i=1:NstatesOut | |
205 ao_out(i).setData(tsdata( x(i,:), fs )); | |
206 ao_out(i).setName(sys.outputs(1).ports(i).name); | |
207 ao_out(i).setXunits('s'); | |
208 ao_out(i).setYunits(sys.outputs(1).ports(i).units); | |
209 ao_out(i).setDescription(... | |
210 ['simulation for ' isysStr, ' : ', sys.outputs(1).ports(i).name,... | |
211 ' ' sys.outputs(1).ports(i).description]); | |
212 ao_out(i).setT0(tini); | |
213 end | |
214 for i=1:NoutputsOut | |
215 ao_out(NstatesOut+i).setData(tsdata( y(i,:), fs )); | |
216 ao_out(NstatesOut+i).setName(sys.outputs(2).ports(i).name); | |
217 ao_out(NstatesOut+i).setXunits('s'); | |
218 ao_out(NstatesOut+i).setYunits(sys.outputs(2).ports(i).units); | |
219 ao_out(NstatesOut+i).setDescription(... | |
220 ['simulation for, ' isysStr, ' : ', sys.outputs(2).ports(i).name, ... | |
221 ' ', sys.outputs(2).ports(i).description]); | |
222 ao_out(NstatesOut+i).setT0(tini); | |
223 end | |
224 | |
225 %% construct output plist | |
226 plist_out = plist('lastX', ssm.blockMatRecut(lastX,ssSizesIni,1) ); | |
227 | |
228 %% construct output matrix object | |
229 out = matrix(ao_out); | |
230 if callerIsMethod | |
231 % do nothing | |
232 else | |
233 myinfo = getInfo('None'); | |
234 out.addHistory(myinfo, pl , ssm_invars(1), inhist ); | |
235 end | |
236 | |
237 %% Set output depending on nargout | |
238 if nargout == 1; | |
239 varargout = {out}; | |
240 elseif nargout == 2; | |
241 varargout = {out plist_out}; | |
242 elseif nargout == 0; | |
243 iplot(ao_out); | |
244 else | |
245 error('Wrong number of outputs') | |
246 end | |
247 end | |
248 | |
249 %-------------------------------------------------------------------------- | |
250 % Get Info Object | |
251 %-------------------------------------------------------------------------- | |
252 function ii = getInfo(varargin) | |
253 | |
254 if nargin == 1 && strcmpi(varargin{1}, 'None') | |
255 sets = {}; | |
256 pl = []; | |
257 else | |
258 sets = {'Default'}; | |
259 pl = getDefaultPlist; | |
260 end | |
261 % Build info object | |
262 ii = minfo(mfilename, 'ssm', 'ltpda', utils.const.categories.op, '$Id: simulate.m,v 1.104 2011/07/11 10:44:38 adrien Exp $', sets, pl); | |
263 end | |
264 | |
265 %-------------------------------------------------------------------------- | |
266 % Get Default Plist | |
267 %-------------------------------------------------------------------------- | |
268 function pl = getDefaultPlist() | |
269 pl = ssm.getInfo('reorganize', 'for simulate').plists; | |
270 pl.remove('set'); | |
271 | |
272 p = param({'covariance', 'The covariance of this noise between input ports for the <i>time-discrete</i> noise model.'}, []); | |
273 pl.append(p); | |
274 | |
275 p = param({'CPSD', 'The one sided cross-psd of the white noise between input ports.'}, []); | |
276 pl.append(p); | |
277 | |
278 p = param({'aos', 'An array of input AOs.'}, ao.initObjectWithSize(1,0)); | |
279 pl.append(p); | |
280 | |
281 p = param({'constants', 'Array of DC values for the different corresponding inputs.'}, paramValue.DOUBLE_VALUE(zeros(0,1))); | |
282 pl.append(p); | |
283 | |
284 p = param({'Nsamples', 'The maximum number of samples to simulate (AO length(s) overide this).'}, paramValue.DOUBLE_VALUE(inf)); | |
285 pl.append(p); | |
286 | |
287 p = param({'ssini', 'A cell-array of vectors that give the initial position for simulation.'}, {}); | |
288 pl.append(p); | |
289 | |
290 p = param({'initialize', 'When set to 1, a random state value is computed for the initial point.'}, paramValue.FALSE_TRUE); | |
291 pl.append(p); | |
292 | |
293 p = param({'tini', 'Same as t0; kept for backwards compatibility.'}, paramValue.EMPTY_DOUBLE ); | |
294 pl.append(p); | |
295 | |
296 p = param({'t0', 'The initial simulation time (seconds).'}, paramValue.EMPTY_DOUBLE ); | |
297 pl.append(p); | |
298 | |
299 p = param({'displayTime', 'Switch off/on the display'}, paramValue.TRUE_FALSE); | |
300 pl.append(p); | |
301 | |
302 p = param({'termincond', 'A string to evaluate a termination condition on the states in x (''lastX'') or outputs in y (''lastY'')'}, paramValue.EMPTY_STRING); | |
303 pl.append(p); | |
304 | |
305 p = param({'reorganize', 'When set to 0, this means the ssm does not need be modified to match the requested i/o. Faster but dangerous!'}, paramValue.TRUE_FALSE); | |
306 pl.append(p); | |
307 | |
308 p = param({'force complete', 'Force the use of the complete simulation code.'}, paramValue.FALSE_TRUE); | |
309 pl.append(p); | |
310 | |
311 end | |
312 |