rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
lin_ds_detailed_simulation_euler_forward.m
1 function sim_data = lin_ds_detailed_simulation_euler_forward(model,model_data)
2 %function sim_data = lin_ds_detailed_simulation_euler_forward(model,model_data)
3 %
4 % function performing a general time evolution of a linear dynamical
5 % system by euler forward time discretization
6 %
7 % @f[ d/dt x = A(t,\mu) x + B(t,\mu) u @f]
8 % @f[ y = C(t,\mu) x + D(t,\mu) u \quad \mbox{ for } t = [0,T] @f]
9 % @f[ x(0) = x_0(\mu) @f]
10 %
11 % required fields of model:
12 % T : final time
13 % nt : number of time-intervals until T, i.e. nt+1
14 % solution slices are computed
15 % x0_function_ptr: pointer to function for computing the
16 % initvalues-DOF with arguments (model)
17 % A_function_ptr: pointer to function for computing the matrix A
18 % B_function_ptr: pointer to function for computing the matrix B
19 % C_function_ptr: pointer to function for computing the matrix C
20 % D_function_ptr: pointer to function for computing the matrix D
21 % u_function_ptr: pointer to input function u(t)
22 % data_const_in_time : if this optional field is 1, the time
23 % evolution is performed with constant operators,
24 % i.e. only the initial-time-operators are computed
25 % and used throughout the time simulation.
26 % affinely_decomposed: if this optional field is set, model_data is
27 % assumed to contain the components of the matrices and online
28 % assembly is performed by coefficient-component linear combinaton
29 % save_time_indices: list of integer time indices between 0 and nt,
30 % which are to be computed, stored and returned
31 %
32 % fields of dsim:
33 % X: solution DOF-vector X(:,t) for all times t=1,...,nt+1
34 % Y: output Y(:,t)
35 
36 % Bernard Haasdonk 2.4.2009
37 
38 if model.verbose >= 9
39  disp('entered detailed simulation ');
40 end;
41 
42 %model.affine_decomp_mode = 'complete';
43 model.decomp_mode = 0;
44 model = model.set_time(model,0);
45 %model.t = 0;
46 
47 if isfield(model,'save_time_indices')
48  save_time_index = zeros(1,model.nt+1);
49  save_time_index(model.save_time_indices(:)+1) = 1;
50  save_time_indices = find(save_time_index==1)-1;
51 else
52  save_time_index = ones(1,model.nt+1);
53  save_time_indices = 1:(model.nt+1);
54 end;
55 
56 % initial values by midpoint evaluation
57 
58 if ~model.affinely_decomposed
59  xt = model.x0_function_ptr(model,model_data);
60  C = model.C_function_ptr(model,model_data);
61 else
62  old_decomp_mode = model.decomp_mode;
63  model.decomp_mode = 2;
64  x0_coeff = model.x0_function_ptr(model,[]);
65  xt = lincomb_sequence(model_data.x0_components,x0_coeff);
66  C_coeff = model.C_function_ptr(model,[]);
67  C = lincomb_sequence(model_data.C_components,C_coeff);
68  model.decomp_mode = old_decomp_mode;
69 end;
70 D = model.D_function_ptr(model,model_data); % no decomposition required
71 X = zeros(length(xt(:)),length(save_time_indices));
72 Y = zeros(size(C,1),length(save_time_indices));
73 time_indices = zeros(1,length(save_time_indices));
74 time = zeros(1,length(save_time_indices));
75 
76 t_column = 1; % next column index to be filled in output
77 t_ind = 0; % current t_ind between 0 and nt
78 t = 0; % absolute time between 0 and T
79 
80 % store init data
81 if save_time_index(t_ind+1)
82  X(:,t_column) = xt;
83  time_indices(t_column) = t_ind;
84  time(t_column) = t;
85  u = model.u_function_ptr(model);
86  Y(:,t_column) = C * xt + D * u;
87  t_column = t_column + 1;
88 end;
89 
90 model.dt = model.T/model.nt;
91 operators_required = 1;
92 
93 % loop over time-steps
94 for t_ind = 1:model.nt
95  t = model.dt * (t_ind-1); % compute new x and y at time t
96  if model.verbose >= 10
97  disp(['entered time-loop step ',num2str(t_ind)]);
98  end;
99  if model.verbose == 9
100  fprintf('.');
101  end;
102  % get matrices and bias-vector
103  model = model.set_time(model,t);
104 % model.t = t;
105  if operators_required
106  if ~model.affinely_decomposed
107  A = model.A_function_ptr(model,model_data);
108  B = model.B_function_ptr(model,model_data);
109  % C and D required at time t+dt;
110  model = model.set_time(model,t+model.dt);
111 % model.t = t+model.dt;
112  C = model.C_function_ptr(model,model_data);
113  D = model.D_function_ptr(model,model_data);
114  model = model.set_time(model,t);
115  % model.t = t;
116  else
117  model.decomp_mode = 2;
118  A_coeff = model.A_function_ptr(model,[]);
119  B_coeff = model.B_function_ptr(model,[]);
120  model = model.set_time(model,t+model.dt);
121  %model.t = t+model.dt;
122  C_coeff = model.C_function_ptr(model,[]);
123  model.decomp_mode = old_decomp_mode;
124  D = model.D_function_ptr(model,[]); % no decomposition of D
125  model = model.set_time(model,t);
126  %model.t = t;
127  A = lincomb_sequence(model_data.A_components,A_coeff);
128  B = lincomb_sequence(model_data.B_components,B_coeff);
129  C = lincomb_sequence(model_data.C_components,C_coeff);
130  end;
131 
132  if model.data_const_in_time
133  operators_required = 0;
134  end;
135  end;
136  u = model.u_function_ptr(model);
137  xt = xt + model.dt * A * xt + model.dt * B * u;
138  % X(:,t_ind+1) = X(:,t_ind) + model.dt * (A * X(:,t_ind) + B * u);
139  %Y(:,t_ind+1) = C * X(:,t_ind) + D * u;
140 
141  if save_time_index(t_ind+1)
142  X(:,t_column) = xt;
143  time_indices(t_column) = t_ind;
144  % y evaluation requires + dt !!
145  model = model.set_time(model,t+model.dt);
146  %model.t = t + model.dt;
147  u = model.u_function_ptr(model);
148  Y(:,t_column) = C * xt + D * u;
149  time(t_column) = t;
150  t_column = t_column + 1;
151  fprintf('.');
152  end;
153 
154 end;
155 
156 if model.verbose == 9
157  fprintf('\n');
158 end;
159 
160 sim_data = [];
161 sim_data.X = X;
162 sim_data.Y = Y;
163 sim_data.time = time;
164 sim_data.time_indices = time_indices;
function r = verbose(level, message, messageId)
This function displays messages depending on a message-id and/or a level. Aditionally you can set/res...
Definition: verbose.m:17