1 function model = oscillator_model(params);
2 %
function model = oscillator_model;
4 % Steffen
's oscillator model
6 % B.Haasdonk and S. Waldherr 5.8.2010
7 % Time-stamp: <Last change 2010-09-16 15:21:48 by Steffen Waldherr>
13 model = lin_ds_model_default;
14 model.data_const_in_time = 1;
17 model.clim = [0,0.00005];
19 % useful range and number of parameters:
20 model.mu_names = {'k3
'};
21 model.mu_ranges = {[0.001,1]};
23 %model.nt = 200; % number of timesteps
25 model.nt = 100; % number of timesteps
29 model.range = [300, 300];
30 %model.range = [51, 51];
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;
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]);
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;
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;
65 %model.rb_reconstruction = @lin_ds_rb_reconstruction;
66 % determin model data, i.e. matrix components, matrix G
68 model.orthonormalize = @model_orthonormalize_qr;
70 model_data = gen_model_data(model); % matrix components
72 model.dim_x = size(model_data.X,2);
73 model.dim_y = size(model_data.C_components{1},1);
75 if isfield(params,'output_plot_indices
')
76 model.output_plot_indices = params.output_plot_indices;
78 model.output_plot_indices = 1:model.dim_y;
81 model.theta = 1; % implicit time discretization
83 % be sure to determine correct constants once, if anything in the
84 % problem setting has changed!!
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;
91 % the following only needs to be performed, if the model is changed.
92 if (params.estimate_bounds == 0)
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;
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;
106 model.estimate_lin_ds_nmu = 3;
107 model.estimate_lin_ds_nX = 10;
108 model.estimate_lin_ds_nt = 10;
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;
115 model.inner_product_matrix_algorithm =@(model,model_data) ...
116 speye(model.dim_x,model.dim_x);
118 model.error_algorithm = @(u1,u2,dummy1,dummy2) ...
119 sqrt(max(sum((u1-u2).^2),0));
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
128 %model.RB_error_indicator = 'estimator
'; % Delta from rb_simulation
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;
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,...
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);
148 function Acomp = Acomp_assembly(s,k2,k4,X,numstates);
149 % define reaction rate functions without linear parameters
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];
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
166 % Acoeff contains the coefficient matrices (without parameters)
169 Acoeff{j} = sparse([],[],[],numstates,numstates,...
170 size(S,2)*numstates);
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));
182 Acomp = {Acoeff{1},Acoeff{2},Acoeff{3},Acoeff{4},Acoeff{5}};
186 function Acoeff = A_coefficients(model)
187 Acoeff = [model.kp,model.k1,model.kq/model.epsilon,1/model.epsilon,model.k3/model.epsilon];
189 function B = B_func(model,model_data)
190 B = eval_affine_decomp(@B_components,@B_coefficients,model,model_data);
192 function Bcomp = B_components(model,model_data)
193 Bcomp = {zeros(model_data.numstates,1)};
195 function Bcoeff = B_coefficients(model)
198 function C = C_func(model,model_data)
199 C = eval_affine_decomp(@C_components,...
200 @C_coefficients,model,model_data);
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);
207 Ccomp = {ones(1,model_data.numstates)};
208 %Ccomp = {[2,0,1],[0,2,1]};
210 function Ccoeff = C_coefficients(model)
212 %Ccoeff = [model.C_weight,1-model.C_weight];
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
218 function x0 = x0_func(model,model_data)
219 x0 = eval_affine_decomp(@x0_components,@x0_coefficients,model,model_data);
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]
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);
228 %weights = sum(ic(:,3));
230 % ind = find((model_data.X(1,:) == ic(i,1)) & (X(2,:) == ic(i,2)));
231 % Pic(ind) = ic(i,3)/weights;
234 function x0coeff = x0_coefficients(model)
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);
242 for x1=0:model.range(1)
243 for x2=0:model.range(2)
249 model_data.numstates = numstates;
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);
261 % model_data.D_components = model.D_function_ptr(model);
262 model_data.x0_components = model.x0_function_ptr(model,model_data);
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);
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];
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(:);
282 function detailed_data = my_ds_gen_detailed_data(model,model_data)
283 detailed_data = lin_ds_gen_detailed_data(model,model_data);
285 detailed_data.plot_grid = model_data.plot_grid;
286 %detailed_data.plot_nm_index_vector = model_data.plot_nm_index_vector;
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);
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)
303 if ~isfield(model,'plot_title')
304 title('state probabilities');
306 title(model.plot_title);
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;
314 p = plot_element_data(sim_data.X(model_data.plot_permind,params.timestep+1),model_data.plot_grid,params);
318 title('state probabilities');
323 %%%%% code from Steffen:
325 % generate and reduce CME model for stochastic resonance oscillator
326 % El-Samad and Khammash, CDC 2006
328 % File initiated 2010-07-29
329 % Time-stamp: <Last change 2010-07-29 17:44:34 by Steffen Waldherr>
331 % define linear reaction parameters
336 p{3} = 1/epsilon; % kq
337 p{4} = 1/epsilon; % implicit parameter in original model
338 p{5} = 0.1/epsilon; % k3
340 % define non-linear (constant) reaction parameters
344 % define reaction rate functions without linear parameters
347 a{2} = @(x) 1/(x(2)+k2);
349 a{4} = @(x) x(1)*x(2)^2/(x(2)^2+k4);
352 % define stoichiometric matrix
353 S = [-1 1 0 0 0; 0 0 -1 1 1];
355 % define the initial condition
356 % each row in ic is of the form [x1, x2, weight of (x1,x2) in ic]
359 % define range of discrete network configurations
363 % compute state vectors using a rectangular shape
364 numstates = (range(1)+1)*(range(2)+1);
365 X = zeros(2,numstates);
374 % TODO: define relevant outputs: average, probability of being near deterministic equilibrium?
376 % Acoeff contains the coefficient matrices (without parameters)
378 Acoeff{j} = sparse([],[],[],numstates,numstates,size(S,2)*numstates);
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));
390 % compute transition matrix A
391 A = sparse([],[],[],numstates,numstates,size(S,2)*numstates);
393 A = A + p{j}*Acoeff{j};
396 Pic = zeros(numstates,1);
397 weights = sum(ic(:,3));
399 ind = find((X(1,:) == ic(i,1)) & (X(2,:) == ic(i,2)));
400 Pic(ind) = ic(i,3)/weights;
403 [t,P] = ode15s(@(t,x) A*x,[0 1], Pic);