rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
plot_trajectories.m
Go to the documentation of this file.
1 function plot_trajectories(rmodel, detailed_data, plot_params, mu_set, imsavepath, plot_name, opts)
2 % function plot_trajectories(rmodel, detailed_data, plot_params, mu_set, imsavepath, plot_name, opts)
3 % function generating a tikz graphic showing trajectories for certain selected
4 % parameters
5 %
6 % Parameters:
7 % plot_params: a structure coming with a field 'plot' for plotting
8 % mu_set: a cell array of parameter vectors for which a trajectory shall
9 % be generated.
10 % imsavepath: a string specifying the directory name, where the generated
11 % files are stored.
12 % plot_name: a string naming this plot.
13 % opts: optional settings.
14 %
15 % optional fields of opts:
16 % 'timeinstants': time instants to include into trajectory. Default is
17 % '[ 0, 1/3, 2/3, 1 ] * model.T'
18 % 'width': width of single snapshot. Default is 5cm;
19 % 'subline_coord': coordinate at which a subline plot shall be created
20 % 'subline_dir': either 'x' or 'y' specifying the direction of the subline plot.
21 % 'sim_type': simulation type (either 'reduced' or 'detailed')
22  @default 'reduced'
23 
24 % optimize plot_params
25 
26  if ~isfield(plot_params, 'plot_type')
27  plot_params.plot_type = 'patch';
28  end
29  if isequal(plot_params.plot_type, 'contour')
30  plot_params.no_lines = 0;
31  end
32  plot_params.no_axes = 1;
33  plot_params.axes_equal = 1;
34  plot_params.transparent_background = 1;
35  plot_params.show_colorbar = 0;
36  if isfield(opts, 'timeinstants')
37  timeinstants = opts.timeinstants;
38  else
39  timeinstants = [0, 1/3, 2/3, 1] * rmodel.descr.T;
40  end
41  if ~isfield(opts, 'field')
42  opts.field = @(sim_data, model_data, t) sim_data.U(:,t);
43  end
44 
45  if isfield(opts, 'sim_type')
46  sim_type = opts.sim_type;
47  else
48  sim_type = 'reduced';
49  end
50 
51  tsteps = max(1,floor(timeinstants/rmodel.descr.T .* rmodel.nt));
52 
53  if isfield(opts, 'width')
54  width = opts.width;
55  else
56  width = 3;
57  end
58 
59  if isfield(opts, 'pic_scale')
60  picscale = opts.pic_scale;
61  else
62  picscale = 3;
63  end
64 
65  if isfield(opts, 'subline_coord')
66  subline = true;
67  else
68  subline = false;
69  end
70 
71  % generate reduced_data
72  reduced_data = gen_reduced_data(rmodel, detailed_data);
73 
74  % disable error estimator
75  rmodel.Mstrich = 0;
76  rmodel.enable_error_estimator = 0;
77 
78  reduced_data = extract_reduced_data_subset(rmodel, reduced_data);
79 
80  if subline
81  g = detailed_data.model_data.grid;
82  if isequal(opts.subline_dir, 'x')
83  subline = find(g.CY > opts.subline_coord ...
84  & g.CY < opts.subline_coord+g.hmin);
85  subline_title = {'X'};
86  elseif isequal(opts.subline_dir, 'y')
87  subline = find(g.CY > opts.subline_coord ...
88  & g.CY < opts.subline_coord+g.hmin);
89  subline_title = {'Y'};
90  end
91  subline_values = g.CX(subline)';
92  end
93 
94  if ~exist(imsavepath, 'dir')
95  mkdir(imsavepath);
96  end
97 
98  sample_dir_name = [plot_name, '_data'];
99  sample_dir_path = fullfile(imsavepath, sample_dir_name);
100  if ~exist(sample_dir_path, 'dir')
101  mkdir(sample_dir_path);
102  end
103 
104  file = '\\pgfplotsset{width=6cm,compat=newest};';
105 
106  xshift = width+0.2;
107  yshift = -width-0.2;
108 
109  for mi=1:length(mu_set)
110  set_mu(rmodel, mu_set{mi});
111  set_mu(rmodel.detailed_model, mu_set{mi});
112  % newmodel.newton_epsilon = 1e-9;
113 
114  if isequal(sim_type, 'reduced')
115  rb_sim_data = rb_simulation(rmodel, reduced_data);
116  rb_sim_data = rb_reconstruction(rmodel, detailed_data, rb_sim_data);
117  else
118  rb_sim_data = detailed_simulation(rmodel.detailed_model, detailed_data.model_data);
119  end
120 
121  mu = mu_set{mi};
122 
123  mustring = strrep(strrep(strrep(strrep(mat2str(mu),' ', '_'),'[',''),']',''),'.','p');
124 
125  for nt=1:length(tsteps)
126 
127  fn = [plot_name, '_mu_', mustring, '_tstep_', num2str(nt)];
128 
129  disp(['Processing ', fn, '...']);
130 
131  file = add_to_tikzfile(file, plot_name, xshift, yshift, mi, mu, fn, nt, timeinstants(nt));
132 
133  f = figure;
134  set(f, 'Visible', 'off');
135  U = opts.field(rb_sim_data, detailed_data.model_data, tsteps(nt));
136  plot_params.plot(detailed_data.model_data.grid, U, plot_params);
137  set(gca,'PlotBoxAspectRatio',[1 1 1]);
138 
139  if subline
140  Uline = U(subline);
141  subline_title = [ subline_title, [mustring,'_t_',num2str(nt) ] ];
142  subline_values = [ subline_values; reshape(Uline, 1, length(Uline)) ];
143  end
144 
145  tikzparams.filename = fn;
146  tikzparams.filepath = sample_dir_path;
147  tikzparams.relpath = fullfile(['\', plot_name, 'TrajRelpath'], [plot_name, '_data']);
148  tikzparams.width = width;
149  tikzparams.print_axes = 0;
150  tikzparams.pic_scale = picscale;
151  if(nt == 1 && (mi == 1 || ~isfield(plot_params, 'clim')))
152  tikzparams.save_colorbar = 1;
153  cbfn = [fn,'colorbar'];
154  else
155  tikzparams.save_colorbar = 0;
156  end
157 
158 % tclim = [0 1];
159  tclim = Postprocess.plot_as_tikzfile(tikzparams);
160  close(f);
161  if verLessThan('matlab', '7.6.1')
162  pause(2);
163  end
164  pause(0.5);
165  end
166  colorbar_xshift = nt*xshift+1;
167  colorbar_yshift = (mi-1)/2*yshift;
168  file = create_colorbar_file(file, plot_name, colorbar_xshift, colorbar_yshift, 0.2, width+0.2, 5, tclim, imsavepath, cbfn);
169  end
170 
171  if subline
172  datafn = [plot_name, 'sublines.dat'];
173  f2=figure;
174  plot(subline_values(1,:), subline_values(2:end,:));
175  print_datatable(fullfile(imsavepath, datafn), subline_title, subline_values);
176  end
177 
178 
179  tfn = fullfile(imsavepath, [plot_name,'_illustration.tikz']);
180  fid = fopen(tfn, 'w');
181  fwrite(fid,file);
182  fclose(fid);
183 
184  relpathcmd = ['\', plot_name, 'TrajRelpath'];
185  tfntex = fullfile(imsavepath, [plot_name,'illustration_out.tex']);
186  fid = fopen(tfntex, 'w');
187  fprintf(fid, ['\\documentclass[a4paper,11pt]{article}\n',...
188  '\\usepackage[x11names,rgb]{xcolor}\n',...
189  '\\usepackage{tikz,pgfplots}\n',...
190  '\\pgfplotsset{plot coordinates/math parser=false}\n',...
191  '\\usepackage{amsfonts}\n',...
192  '\\usepackage{amsmath}\n',...
193  '\\usetikzlibrary{arrows,snakes,shapes,decorations.pathmorphing,backgrounds,fit}\n',...
194  '\\usepackage[active,tightpage]{preview}\n',...
195  '\\PreviewEnvironment{tikzpicture}\n',...
196  '\\oddsidemargin -10mm\n',...
197  '\\evensidemargin -10mm\n',...
198  '\\topmargin 5mm\n',...
199  '\\headheight 0mm\n',...
200  '\\headsep 0mm\n',...
201  '\\textheight 247mm\n',...
202  '\\textwidth 160mm\n',...
203  '\\begin{document}\n',...
204  ' \\newcommand{%s}{./}\n', ...
205  ' \\begin{tikzpicture}[scale=0.5]\n',...
206  ' \\input{%s}\n',...
207  ' \\end{tikzpicture}\n',...
208  '\\end{document}\n'],relpathcmd, tfn);
209  fclose(fid);
210 
211 end
212 
213 function file = create_colorbar_file(file, plot_name, xshift, yshift, cwidth, cheight, ticks, tclim, cbfp, cbfn)
214  % test
215 
216  % colorbar
217  sample_dir_name = [plot_name, '_data'];
218  relpathcmd = ['\', plot_name, 'TrajRelpath'];
219  ytstr = '';
220  for i = 1:ticks
221  ytstr = [ytstr, ...
222  sprintf('%.2fcm/%.1f', (cheight) * (i-1)/(ticks-1),...
223  (tclim(2)-tclim(1))*(i-1)/(ticks-1) + tclim(1))];
224  if i ~= ticks
225  ytstr = [ytstr, ', '];
226  end
227  end
228 
229  cbfrel = fullfile(relpathcmd, sample_dir_name, cbfn);
230  outfrel = fullfile(sample_dir_name, [cbfn, '.tikz']);
231  outfn = fullfile(cbfp, sample_dir_name, [cbfn, '.tikz']);
232 
233  cfid = fopen(outfn, 'w');
234  fprintf(cfid, [' \\pgfdeclareimage[width=%scm,height=%scm]{colorbar}{%s}\n',...
235  ' \\pgftext[at=\\pgfpoint{0}{0},left,base]{\\pgfuseimage{colorbar}};\n',...
236  ' \\foreach \\y/\\ytext in {%s}\n', ...
237  ' \\draw[shift={(0,\\y)}] (1pt,0pt) -- (-1pt,0pt) node[left] (coordsy) {${\\scriptscriptstyle \\ytext}$};\n'], ...
238  num2str(cwidth), num2str(cheight), cbfrel, ytstr);
239  fclose(cfid);
240 
241  file = [file, sprintf(['\n',...
242  '\\begin{scope}[xshift=%scm,yshift=%scm]\n',...
243  ' \\input{%s}\n',...
244  '\\end{scope}\n']',...
245  num2str(xshift), num2str(yshift), outfrel)] ;
246 end
247 
248 function file = add_to_tikzfile(file, plot_name, xshift, yshift, mi, mu, mufn, nt, ti)
249 
250  sample_dir_name = [plot_name, '_data'];
251  mufnrel = fullfile(sample_dir_name, mufn);
252 
253  xmulabel = -0.3;
254  ymulabeloff = -0.45*yshift;
255  ytimelabel = -yshift+0.1;
256  xtimelabeloff = 0.4*xshift;
257 
258 
259  ymulabel = (mi-1)*yshift+ymulabeloff;
260  muname = num2str(mu(1));
261  for mni=2:length(mu);
262  muname = [muname, ', ', num2str(mu(mni))];
263  end
264 
265  file = [file, sprintf(['\n',...
266  '\\draw (%scm,%scm) node [rotate=90]',...
267  '{$\\scriptstyle \\boldsymbol \\mu = (%s)$};'], ...
268  num2str(xmulabel), num2str(ymulabel), muname)];
269 
270  if mi == 1
271  xtimelabel = (nt-1)*xshift+xtimelabeloff;
272  file = [file, sprintf(['\n',...
273  '\\draw (%scm,%scm) node {$\\scriptstyle t = %.3f$};'], ...
274  num2str(xtimelabel), num2str(ytimelabel), ti )];
275  end
276 
277  xcoord = (nt-1) * xshift;
278  ycoord = (mi-1) * yshift;
279 
280  file = [file, sprintf(['\n\n',...
281  '\\begin{scope}[xshift=%scm,yshift=%scm]\n',...
282  ' \\input{%s}\n'],...
283  num2str(xcoord), num2str(ycoord), [mufnrel, '.tikz']),...
284  '\end{scope}'];
285 
286 end
function plot_trajectories(IReducedModel rmodel,IDetailedData detailed_data, plot_params, mu_set, imsavepath, plot_name, opts)
function generating a tikz graphic showing trajectories for certain selected parameters ...
function [ opt_data , model ] = optimize(model, model_data, detailed_data, reduced_data)
opt_data = optimize(model, model_data, detailed_data, reduced_data)
Definition: optimize.m:17