rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
oscillator_model.m
1 function model = oscillator_model(params);
2 %function model = oscillator_model;
3 %
4 % Steffen's oscillator model
5 
6 % B.Haasdonk and S. Waldherr 5.8.2010
7 % Time-stamp: <Last change 2010-09-16 15:21:48 by Steffen Waldherr>
8 
9 if (nargin < 1)
10  params = [];
11 end;
12 
13 model = lin_ds_model_default;
14 model.data_const_in_time = 1;
15 model.verbose = 0;
16 model.debug = 0;
17 model.clim = [0,0.00005];
18 
19 % useful range and number of parameters:
20 model.mu_names = {'k3'};
21 model.mu_ranges = {[0.001,1]};
22 %model.T = 20;
23 %model.nt = 200; % number of timesteps
24 model.T = 10;
25 model.nt = 100; % number of timesteps
26 
27 % model parameters
28 model.epsilon = 0.01;
29 model.range = [300, 300];
30 %model.range = [51, 51];
31 model.kp = 1;
32 model.k1 = 15;
33 model.kq = 1;
34 model.k2 = 0.2;
35 model.k3 = 0.1;
36 model.k4 = 6.5;
37 model.s = 10;
38 
39 model.affinely_decomposed = 1;
40 model.u_function_ptr = @(model) 0; % define u constant
41 model.A_function_ptr = @A_func;
42 model.x0_function_ptr = @x0_func;
43 model.B_function_ptr = @B_func;
44 model.C_function_ptr = @C_func;
45 model.D_function_ptr = @D_func;
46 
47 model.G_matrix_function_ptr = ...
48  @(model,model_data) speye(model.dim_x,model.dim_x);
49 % the following lead to both larger C1 and C2!!!
50 %model.G_matrix_function_ptr = @(model) diag([2,0.5,1]);
51 %model.G_matrix_function_ptr = @(model) diag([0.5,2,1]);
52 
53 % set function pointers to main model methods
54 model.gen_model_data = @my_ds_gen_model_data;
55 %model.detailed_simulation = @lin_ds_detailed_simulation;
56 
57 model.gen_detailed_data = @my_ds_gen_detailed_data;
58 %model.gen_reduced_data = @lin_ds_gen_reduced_data;
59 %model.rb_simulation = @lin_ds_rb_simulation;
60 %model.plot_sim_data = @my_ds_plot_sim_data;
61 model.plot_sim_data_state = @plot_state_probabilities;
62 model.plot_slice = @my_plot_slice;
63 model.axis_tight = 1;
64 
65 %model.rb_reconstruction = @lin_ds_rb_reconstruction;
66 % determin model data, i.e. matrix components, matrix G
67 
68 model.orthonormalize = @model_orthonormalize_qr;
69 
70 model_data = gen_model_data(model); % matrix components
71 
72 model.dim_x = size(model_data.X,2);
73 model.dim_y = size(model_data.C_components{1},1);
74 
75 if isfield(params,'output_plot_indices')
76  model.output_plot_indices = params.output_plot_indices;
77 else
78  model.output_plot_indices = 1:model.dim_y;
79 end;
80 
81 model.theta = 1; % implicit time discretization
82 
83 % be sure to determine correct constants once, if anything in the
84 % problem setting has changed!!
85 
86 %model.error_estimation = 1; % model is capable of error_estimation
87 model.enable_error_estimator = 0;
88 if ~isfield(params,'estimate_bounds')
89  params.estimate_bounds = 0;
90 end;
91 % the following only needs to be performed, if the model is changed.
92 if (params.estimate_bounds == 0)
93  % for G = Id
94  warning('please adjust the computation of the error estimator constants!')
95  model.state_bound_constant_C1 = 1 ;
96  model.output_bound_constant_C2 = sqrt(model.dim_x);
97 elseif params.estimate_bounds == 1
98  model.estimate_lin_ds_nmu = 3;
99  model.estimate_lin_ds_nX = 10;
100  model.estimate_lin_ds_nt = 10;
101  format long;
102  [C1,C2] = lin_ds_estimate_bound_constants(model,model_data)
103  model.state_bound_constant_C1 = C1;
104  model.output_bound_constant_C2 = C2;
105 else
106  model.estimate_lin_ds_nmu = 3;
107  model.estimate_lin_ds_nX = 10;
108  model.estimate_lin_ds_nt = 10;
109  format long;
110  [C1,C2] = lin_ds_estimate_bound_constants(model,model_data)
111  model.state_bound_constant_C1 = C1;
112  model.output_bound_constant_C2 = C2;
113 end;
114 
115 model.inner_product_matrix_algorithm =@(model,model_data) ...
116  speye(model.dim_x,model.dim_x);
117 
118 model.error_algorithm = @(u1,u2,dummy1,dummy2) ...
119  sqrt(max(sum((u1-u2).^2),0));
120 % [4,3]* [3,2];
121 
122 %model.error_norm = 'l2';
123 %% 'RB_extension_max_error_snapshot'};
124 model.RB_stop_timeout = 4*60*60; % 4 hours
125 model.RB_stop_epsilon = 1e-10;
126 model.RB_error_indicator = 'error'; % true error
127 
128 %model.RB_error_indicator = 'estimator'; % Delta from rb_simulation
129 
130 model.RB_stop_Nmax = 200;
131 model.RB_generation_mode = 'greedy_uniform_fixed';
132 model.RB_numintervals = [4];
133 model.RB_detailed_train_savepath = 'follicle_model_detailed_train';
134 model.orthonormalize = @model_orthonormalize_qr;
135 model.RB_extension_algorithm = @RB_extension_PCA_fixspace;
136 
137 
138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 % functions defining the dynamical system and input/initial data
140 function A = A_func(model,model_data);
141 A = eval_affine_decomp(@A_components,@A_coefficients,...
142  model,model_data);
143 
144 function Acomp = A_components(model,model_data)
145 Acomp = filecache_function(...
146  @Acomp_assembly,model.s,model.k2, model.k4,model_data.X, model_data.numstates);
147 
148 function Acomp = Acomp_assembly(s,k2,k4,X,numstates);
149 % define reaction rate functions without linear parameters
150 % $$$ reactions
151 % $$$ degradep, P -> , kp;
152 % $$$ producep, -> P, [k1*s^2/(Q+s*k2)];
153 % $$$ degradeq, Q -> , kq/epsilon;
154 % $$$ produceq, -> Q, [(Q^2/(Q^2+k4*s^2)*P + k3*s)/epsilon];
155 
156 a{1} = @(x) x(1); % degrade x1
157 a{2} = @(x) s^2/(x(2)+s*k2); % produce x1 regulated
158 a{3} = @(x) x(2); % degrade x2
159 a{4} = @(x) x(1)*x(2)^2/(x(2)^2+k4*s^2); % produce x2 regulated
160 a{5} = @(x) s; % produce x2 constitutively
161 % define stoichiometric matrix
162 S = [-1 1 0 0 0; 0 0 -1 1 1];
163 % define the initial condition
164 % define range of discrete network configurations
165 
166 % Acoeff contains the coefficient matrices (without parameters)
167 %Acoeff = cell(5);
168 for j=1:size(S,2)
169  Acoeff{j} = sparse([],[],[],numstates,numstates,...
170  size(S,2)*numstates);
171 end;
172 for i=1:numstates
173  for j=1:size(S,2)
174  Acoeff{j}(i,i) = - a{j}(X(:,i));
175  % find the index of the state that the system goes to with reaction j
176  ind = find((X(1,:) == X(1,i)+S(1,j)) & ...
177  (X(2,:) == X(2,i)+S(2,j)));
178 % assert(isscalar(ind) || isempty(ind), 'Found multiple occurences of target state in reaction, network structure may be incorrect.');
179  Acoeff{j}(ind,i) = a{j}(X(:,i));
180  end;
181 end;
182 Acomp = {Acoeff{1},Acoeff{2},Acoeff{3},Acoeff{4},Acoeff{5}};
183 
184 
185 
186 function Acoeff = A_coefficients(model)
187 Acoeff = [model.kp,model.k1,model.kq/model.epsilon,1/model.epsilon,model.k3/model.epsilon];
188 
189 function B = B_func(model,model_data)
190 B = eval_affine_decomp(@B_components,@B_coefficients,model,model_data);
191 
192 function Bcomp = B_components(model,model_data)
193 Bcomp = {zeros(model_data.numstates,1)};
194 
195 function Bcoeff = B_coefficients(model)
196 Bcoeff = 1;
197 
198 function C = C_func(model,model_data)
199 C = eval_affine_decomp(@C_components,...
200  @C_coefficients,model,model_data);
201 
202 function Ccomp = C_components(model,model_data)
203 %i = find((model_data.Ms+model_data.Ns)<=model.output_range);
204 %C = zeros(1,model.dim_x);
205 %C(i) = 1;
206 %Ccomp = {C};
207 Ccomp = {ones(1,model_data.numstates)};
208 %Ccomp = {[2,0,1],[0,2,1]};
209 
210 function Ccoeff = C_coefficients(model)
211 Ccoeff = [1];
212 %Ccoeff = [model.C_weight,1-model.C_weight];
213 
214 function D = D_func(model,model_data)
215 %D = [0;0]; % no decomposition required for scheme
216 D = [0]; % no decomposition required for scheme
217 
218 function x0 = x0_func(model,model_data)
219 x0 = eval_affine_decomp(@x0_components,@x0_coefficients,model,model_data);
220 
221 function x0comp = x0_components(model,model_data)
222 % each row in ic is of the form [x1, x2, weight of (x1,x2) in ic]
223 ic = [0 0 1];
224 Pic = zeros(model_data.numstates,1);
225 i = find((model_data.X(1,:)<=50) & (model_data.X(2,:)<=50));
226 Pic(i) = 1/length(i);
227 x0comp = {Pic};
228 %weights = sum(ic(:,3));
229 %for i=1:size(ic,1)
230 % ind = find((model_data.X(1,:) == ic(i,1)) & (X(2,:) == ic(i,2)));
231 % Pic(ind) = ic(i,3)/weights;
232 %end;
233 
234 function x0coeff = x0_coefficients(model)
235 x0coeff = 1;
236 
237 function model_data = my_ds_gen_model_data(model)
238 % compute state vectors using a rectangular shape
239 numstates = (model.range(1)+1)*(model.range(2)+1);
240 X = zeros(2,numstates);
241 s = 1;
242 for x1=0:model.range(1)
243  for x2=0:model.range(2)
244  X(:,s) = [x1; x2];
245  s = s+1;
246  end;
247 end;
248 model_data.X = X;
249 model_data.numstates = numstates;
250 
251 if model.affinely_decomposed
252  model.decomp_mode = 1;
253  % the following call is extremely expensive: 30 seconds!!!!
254  % therefore caching is activated
255  % model_data.A_components = model.A_function_ptr(model,model_data);
256  model_data.A_components = filecache_function(...
257  model.A_function_ptr,model,model_data);
258  model_data.B_components = model.B_function_ptr(model,model_data);
259  model_data.C_components = model.C_function_ptr(model,model_data);
260  % no D components
261  % model_data.D_components = model.D_function_ptr(model);
262  model_data.x0_components = model.x0_function_ptr(model,model_data);
263 end;
264 
265 warning('please use G_matrix_function_ptr')
266 %model_data.G = model.G_matrix_function_ptr(model,[]);
267 model_data.G = speye(model_data.numstates);
268 
269 % only required for plotting:
270 params.xnumintervals = model.range(1)+1;
271 params.ynumintervals = model.range(2)+1;
272 params.xrange = [-0.5,model.range+0.5];
273 params.yrange = [-0.5,model.range+0.5];
274 params.verbose = 0;
275 model_data.plot_grid = rectgrid(params);
276 %model_data.plot_nm_index_vector = ...
277 % model_data.Ms + model_data.Ns*(model.range+1)+1;
278 plot_permind = reshape(1:(model.range(1)+1)*(model.range(2)+1),...
279  model.range(2)+1,model.range(1)+1)';
280 model_data.plot_permind = plot_permind(:);
281 
282 function detailed_data = my_ds_gen_detailed_data(model,model_data)
283 detailed_data = lin_ds_gen_detailed_data(model,model_data);
284 % only for plotting:
285 detailed_data.plot_grid = model_data.plot_grid;
286 %detailed_data.plot_nm_index_vector = model_data.plot_nm_index_vector;
287 
288 function p = plot_state_probabilities(model,model_data,sim_data,params)
289 % plot of trajectory and output
290 %p = lin_ds_plot_sim_data(model,model_data,sim_data,params);
291 %close(gcf-1);
292 p = []; % no handles
293 % for debug:
294 %U(nm_index_vector,1) = model_data.Ns;
295 %U(nm_index_vector,2) = model_data.Ms;
296 %params.plot_function = @plot_element_data;
297 merged_plot_params = merge_model_plot_params(model, params);
298 merged_plot_params.plot_function = 'plot_element_data';
299 plot_data_sequence(model,model_data.plot_grid,sim_data.X(model_data.plot_permind,:),merged_plot_params)
300 axis tight;
301 xlabel('species p');
302 ylabel('species q');
303 if ~isfield(model,'plot_title')
304  title('state probabilities');
305 else
306  title(model.plot_title);
307 end;
308 
309 function p = my_plot_slice(model,model_data,sim_data,params)
310 params.plot_function = 'plot_element_data';
311 if ~isfield(params,'clim') && isfield(model,'clim')
312  params.clim = model.clim;
313 end;
314 p = plot_element_data(sim_data.X(model_data.plot_permind,params.timestep+1),model_data.plot_grid,params);
315 %axis tight;
316 xlabel('species p');
317 ylabel('species q');
318 title('state probabilities');
319 
320 return;
321 
322 
323 %%%%% code from Steffen:
324 
325 % generate and reduce CME model for stochastic resonance oscillator
326 % El-Samad and Khammash, CDC 2006
327 %
328 % File initiated 2010-07-29
329 % Time-stamp: <Last change 2010-07-29 17:44:34 by Steffen Waldherr>
330 
331 % define linear reaction parameters
332 clear p
333 epsilon = 0.01;
334 p{1} = 1; % kp
335 p{2} = 15; % k1
336 p{3} = 1/epsilon; % kq
337 p{4} = 1/epsilon; % implicit parameter in original model
338 p{5} = 0.1/epsilon; % k3
339 
340 % define non-linear (constant) reaction parameters
341 k2 = 0.2;
342 k4 = 6.5;
343 
344 % define reaction rate functions without linear parameters
345 clear a;
346 a{1} = @(x) x(1);
347 a{2} = @(x) 1/(x(2)+k2);
348 a{3} = @(x) x(2);
349 a{4} = @(x) x(1)*x(2)^2/(x(2)^2+k4);
350 a{5} = @(x) 1;
351 
352 % define stoichiometric matrix
353 S = [-1 1 0 0 0; 0 0 -1 1 1];
354 
355 % define the initial condition
356 % each row in ic is of the form [x1, x2, weight of (x1,x2) in ic]
357 ic = [0 0 1];
358 
359 % define range of discrete network configurations
360 range = [50, 50];
361 % range = [3, 3];
362 
363 % compute state vectors using a rectangular shape
364 numstates = (range(1)+1)*(range(2)+1);
365 X = zeros(2,numstates);
366 s = 1;
367 for x1=0:range(1)
368  for x2=0:range(2)
369  X(:,s) = [x1; x2];
370  s = s+1;
371  end;
372 end;
373 
374 % TODO: define relevant outputs: average, probability of being near deterministic equilibrium?
375 
376 % Acoeff contains the coefficient matrices (without parameters)
377 for j=1:size(S,2)
378  Acoeff{j} = sparse([],[],[],numstates,numstates,size(S,2)*numstates);
379 end;
380 for i=1:numstates
381  for j=1:size(S,2)
382  Acoeff{j}(i,i) = - a{j}(X(:,i));
383  % find the index of the state that the system goes to with reaction j
384  ind = find((X(1,:) == X(1,i)+S(1,j)) & (X(2,:) == X(2,i)+S(2,j)));
385  assert(isscalar(ind) || isempty(ind), 'Found multiple occurences of target state in reaction, network structure may be incorrect.');
386  Acoeff{j}(ind,i) = a{j}(X(:,i));
387  end;
388 end;
389 
390 % compute transition matrix A
391 A = sparse([],[],[],numstates,numstates,size(S,2)*numstates);
392 for j=1:size(S,2)
393  A = A + p{j}*Acoeff{j};
394 end;
395 
396 Pic = zeros(numstates,1);
397 weights = sum(ic(:,3));
398 for i=1:size(ic,1)
399  ind = find((X(1,:) == ic(i,1)) & (X(2,:) == ic(i,2)));
400  Pic(ind) = ic(i,3)/weights;
401 end;
402 
403 [t,P] = ode15s(@(t,x) A*x,[0 1], Pic);
404 
405 
406