KerMor  0.9
Model order reduction for nonlinear dynamical systems and nonlinear approximation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LogNorm.m
Go to the documentation of this file.
1 namespace testing{
2 
3 
4 /* (Autoinserted by mtoc++)
5  * This source code has been filtered by the mtoc++ executable,
6  * which generates code that can be processed by the doxygen documentation tool.
7  *
8  * On the other hand, it can neither be interpreted by MATLAB, nor can it be compiled with a C++ compiler.
9  * Except for the comments, the function bodies of your M-file functions are untouched.
10  * Consequently, the FILTER_SOURCE_FILES doxygen switch (default in our Doxyfile.template) will produce
11  * attached source files that are highly readable by humans.
12  *
13  * Additionally, links in the doxygen generated documentation to the source code of functions and class members refer to
14  * the correct locations in the source code browser.
15  * However, the line numbers most likely do not correspond to the line numbers in the original MATLAB source files.
16  */
17 
18 class LogNorm {
25  public: /* ( Static ) */
26 
27 
28  static function [aln , times , rowvec<integer>st_sizes ] = compareSimTransJac_FullJac(models.BaseFullModel m,rowvec<integer> st_sizes) {
29 
30  /* Get required data */
31  jtd = m.Data.JacobianTrainData;
32  QFull = m.ErrorEstimator.QFull;
33  f = m.System.f;
34 
35  /* Input checks */
36  if nargin < 2
37  st_sizes = 1:size(QFull,2);
38  end
39 
40  /* Preps */
41  stn = length(st_sizes);
42  n = size(jtd.xi,2);
43  aln = zeros(stn,n);
44  times = zeros(stn,n);
45  pi = ProcessIndicator(" Computing approximated log norms for %d full jacobians and %d sim. trans. orders ",...
46  n*stn,false,n,stn);
47  for nr = 1:n
48  J = f.getStateJacobian(jtd.xi(:,nr),jtd.ti(nr),jtd.mui(:,nr));
49  for snr = 1:stn
50  Qk = QFull(:,1:st_sizes(snr));
51  t = tic;
52  aln(snr,nr) = Utils.logNorm(Qk^t*J*Qk);
53  times(snr,nr) = toc(t);
54  pi.step;
55  end
56  end
57  pi.stop;
58  if nargout == 0
59  testing.DEIM.compareSimTransJac_FullJac_plots(m, aln, times, st_sizes);
60  end
61  }
87  static function pm = compareSimTransJac_FullJac_plots(m,aln,times,st_sizes,pm) {
88  if nargin < 5
89  pm = PlotManager(false,2,2);
90  end
91  jstd = m.Data.JacSimTransData;
92  pm.nextPlot(" correct_ln ",sprintf(" Full logarithmic norms\nJacobian full dimension %dx%d ",...
93  m.System.f.fDim,m.System.f.xDim));
94  plot(jstd.LogNorms);
95 
96  if size(aln,1) > 1
97  [X,Y] = meshgrid(1:size(aln,2),st_sizes);
98  LN = repmat(jstd.LogNorms,size(aln,1),1);
99  end
100 
101  if size(aln,1) == 1
102  h = pm.nextPlot(" approx_ln ",...
103  sprintf(" Approx logarithmic norms\nSimilarity transformation size %d ",...
104  st_sizes));
105  plot(h, aln);
106  else
107  h = pm.nextPlot(" approx_ln "," Approx logarithmic norms ",...
108  " training sample "," similarity transform size ");
109  LogPlot.nicesurf(h, X, Y, aln);
110  end
111 
112  h = pm.nextPlot(" rel_err "," Relative error "," training sample ",...
113  " similarity transform size ");
114  LogPlot.logsurf(h,X,Y,abs((LN-aln)./LN));
115 
116  sv = m.ErrorEstimator.QSingVals;
117  s = m.ErrorEstimator.JacSimTransMaxSize;
118  h = pm.nextPlot(" sing_vals ",...
119  sprintf(" Singular values of SVD of eigenvector matrix\nBlack line: Max size of sim. trans., singular value= %g ",...
120  sv(s))," POD Modes "," Singular values ");
121  semilogy(h,sv);
122  hold on;
123  plot(h,[s s+eps],[min(sv) max(sv)]," k ");
124  hold off;
125 
126  h = pm.nextPlot(" comp_times ",...
127  sprintf(" Average computation times (over %d values)\nfor log norms of similarity transformed matrices ",...
128  size(times,2))," similarity transformation size "," mean computation time ");
129  me = mean(times,2);
130  semilogy(h,st_sizes,me," -s ");
131  if nargout < 1
132  pm.done;
133  end
134  }
135 
136 
137  static function [aln , times , jtimes , rowvec<integer>deim_orders , rowvec<integer>st_sizes ] = compareSimTransDEIMJac_FullJac(models.BaseFullModel m,rowvec<integer> deim_orders,rowvec<integer> st_sizes) {
138 
139  /* Get required data */
140  jtd = m.Data.JacobianTrainData;
141  /* zi = m.Data.W'*jtd.xi; */
142  e = m.ErrorEstimator;
143  jd = e.JacMDEIM;
144 
145  /* Input checks */
146  if nargin < 3
147  st_sizes = [1:e.JacSimTransMaxSize 0];
148  if nargin < 2
149  deim_orders = 1:jd.MaxOrder;
150  end
151  end
152 
153  /* Preps */
154  stn = length(st_sizes);
155  n = size(jtd.xi,2);
156  no = length(deim_orders);
157 
158  aln = zeros(no,stn,n);
159  times = aln;
160  jtimes = aln;
161  pi = ProcessIndicator(" Computing approximated log norms over %d DEIM orders and %d sim. trans. sizes on %d training values ",...
162  numel(aln),false,no,stn,n);
163  for onr = 1:no
164  jd.Order= deim_orders(onr);
165  for snr = 1:stn
166  jd.setSimilarityTransform(e.QFull(:,1:st_sizes(snr)));
167  for nr = 1:n
168  t = tic;
169  J = jd.evaluate(jtd.xi(:,nr),jtd.ti(nr),jtd.mui(:,nr));
170  jtimes(onr,snr,nr) = toc(t);
171  t = tic;
172  aln(onr,snr,nr) = Utils.logNorm(J);
173  times(onr,snr,nr) = toc(t);
174  pi.step;
175  end
176  end
177  end
178  pi.stop;
179  }
211  static function pm = compareSimTransDEIMJac_FullJac_plots(m,aln,times,jtimes,deim_orders,st_sizes,pm) {
212  if nargin < 7
213  pm = PlotManager(false,2,2);
214  end
215 
216  jstd = m.Data.JacSimTransData;
217  n = size(aln,3);
218  ln = reshape(jstd.LogNorms,1,1,[]);
219  ln = repmat(ln,[length(deim_orders), length(st_sizes), 1]);
220 
221  abserr = abs(aln-ln);
222  relerr = abs(abserr ./ ln);
223  relerr(isnan(relerr)) = 0;
224 
225  /* Aggregate errors over trajectory sample data
226  *aln = sqrt(sum(aln.^2,3)); */
227  abserr = mean(abserr,3);
228  relerr = mean(relerr,3);
229 
230  /* Mean the times too */
231  ttimes = times + jtimes;
232  times = mean(times,3);
233  jtimes = mean(jtimes,3);
234  ttimes = mean(ttimes,3);
235 
236  [X,Y] = meshgrid(st_sizes,deim_orders);
237 
238  h = pm.nextPlot(" abs_err ",sprintf(" Mean absolute approximation error over %d samples ",n),...
239  " Similarity transformation size "," DEIM order ");
240  LogPlot.logsurf(h,X,Y,abserr);
241 
242  h = pm.nextPlot(" rel_err ",sprintf(" Mean relative approximation error over %d samples ",n),...
243  " Similarity transformation size "," DEIM order ");
244  LogPlot.logsurf(h,X,Y,relerr);
245 
246  h = pm.nextPlot(" comp_times_j ",...
247  sprintf(" Average computation times over %d values\nfor matrix DEIM jacobian evaluation ",...
248  n)," Similarity transformation size "," DEIM order ");
249  LogPlot.nicesurf(h,X,Y,jtimes);
250 
251  h = pm.nextPlot(" comp_times ",...
252  sprintf(" Average computation times over %d values\nfor log norm computation of sim.trans. matrix DEIM jacobian ",...
253  n)," Similarity transformation size "," DEIM order ");
254  LogPlot.nicesurf(h,X,Y,times);
255 
256  h = pm.nextPlot(" comp_times_total ",...
257  sprintf(" Average total computation times over %d values\n(jac comp + log norm comp) ",...
258  n)," Similarity transformation size "," DEIM order ");
259  LogPlot.nicesurf(h,X,Y,ttimes);
260 
261  if nargout < 1
262  pm.done;
263  end
264  }
277  static function structres = getApproxLogNormsAtPos(models.BaseFullModel mo,colvec<double> x,double t,matrix<double> mui) {
278 
279  /* Get required data */
280  e = mo.ErrorEstimator;
281  if isa(mo," models.ReducedModel ")
282  fm = mo.FullModel;
283  else
284  fm = mo;
285  end
286  jd = e.JacMDEIM;
287 
288  /* Input checks */
289  if nargin < 4
290  mui = fm.Data.ParamSamples;
291  end
292 
293  /* Preps */
294  n = size(x,2);
295  if ~isempty(t)
296  if length(t) == 1
297  t = ones(1,n)*t;
298  elseif length(t) ~= n
299  error(" Size of t vector must match column count of x ");
300  end
301  end
302  m = size(mui,2);
303  aln = zeros(n,m);
304  times = aln;
305  jtimes = aln;
306  pi = ProcessIndicator(" Computing approximated log norms for %d parameter values at %d locations ",n*m,false,m,n);
307  for i = 1:n
308  for j = 1:m
309  tc = tic;
310  J = jd.evaluate(x(:,i),t(i),mui(:,j));
311  jtimes(i,j) = toc(tc);
312  tc = tic;
313  aln(i,j) = Utils.logNorm(J);
314  times(i,j) = toc(tc);
315  pi.step;
316  end
317  end
318  pi.stop;
319  res.aln= aln;
320  res.times= times;
321  res.jtimes= jtimes;
322  res.mJ= e.JacMatDEIMOrder;
323  res.k= e.JacSimTransSize;
324  res.mui= mui;
325  res.munames= [mo.System.Params(:).Name];
326  }
354  static function getApproxLogNormsAtPos_plots(res,pm) {
355  str = sprintf(" \nm_J = %d, k=%d, %d locations ",res.mJ, res.k, size(res.aln,1));
356  if size(res.mui,1) == 1
357  if size(res.aln,1) == 1
358  h = pm.nextPlot(" aln_min ",[" Log norms at given location " str],res.munames[1]);
359  LogPlot.cleverPlot(h,res.mui,res.aln," b ");
360  else
361  [X,Y] = meshgrid(res.mui,1:size(res.aln,1));
362  h = pm.nextPlot(" aln_min ",[" Log norms over all locations " str],res.munames[1]," location nr ");
363  surf(h,X,Y,res.aln," EdgeColor "," none "," FaceColor "," interp ");
364  end
365  elseif size(res.mui,1) == 2
366  tri = delaunay(res.mui(1,:),res.mui(2,:));
367  if size(res.aln,1) == 1
368  h = pm.nextPlot(" aln_min ",[" Log norms at given location " str],res.munames[:]);
369  trisurf(tri,res.mui(1,:),res.mui(2,:),res.aln,...
370  " EdgeColor "," none "," FaceColor "," interp "," Parent ",h);
371  else
372  /* % Animation
373  * h = pm.nextPlot('aln','Approximated Jacobian log norms over sample parameters');
374  * for k=1:size(res.aln,1)
375  * trisurf(tri,res.mui(1,:),res.mui(2,:),res.aln(k,:),...
376  * 'EdgeColor','none','FaceColor','interp','Parent',h);
377  * title(h,sprintf('Approximated Jacobian log norms over sample parameters, pos: %d',k));
378  * view(90,0);
379  * pause;
380  * end
381  *% Min/Max plot */
382  alnmin = min(res.aln,[],1);
383  alnmax = max(res.aln,[],1);
384  h = pm.nextPlot(" aln_min ",[" Minimum log norms over all locations " str],res.munames[:]);
385  trisurf(tri,res.mui(1,:),res.mui(2,:),alnmin,...
386  " EdgeColor "," none "," FaceColor "," interp "," Parent ",h);
387  h = pm.nextPlot(" aln_min ",[" Max log norms over all locations " str],res.munames[:]);
388  trisurf(tri,res.mui(1,:),res.mui(2,:),alnmax,...
389  " EdgeColor "," none "," FaceColor "," interp "," Parent ",h);
390  h = pm.nextPlot(" aln_min ",[" Max-Min log norms over all locations " str],res.munames[:]);
391  trisurf(tri,res.mui(1,:),res.mui(2,:),alnmax-alnmin,...
392  " EdgeColor "," none "," FaceColor "," interp "," Parent ",h);
393  end
394  else
395  error(" Cannot plot results for more than two parameters ");
396  end
397  }
398 
399 
400  static function [res , mScale , MScale , pos , l , sel , seli ] = CompLogNorms(m,numt) {
401 
402 
403  atd = m.Data.ApproxTrainData;
404  N = size(atd.xi,2);
405 
406  if nargin < 2
407  numt = N;
408  else
409  numt = min(numt, N);
410  end
411  seed = 1;
412  /* Show plots for each numt value */
413  doplot = 4==5;
414  /* Sort training points after norm values (ascending) */
415  dosort = 4==5;
416  /* Plot negative local logarithmic norms */
417  plotneg = 4==4;
418 
419  r = RandStream(" mt19937ar "," Seed ",seed);
420 
421  /* sel = unique(r.randi(N,1,num)); */
422  sel = 1:N;
423  seli = unique(r.randi(N,1,numt));
424  n = length(seli);
425 
426  pi = ProcessIndicator(" Computing %d local log norms, maxed over %d training points ",n,false,n,length(sel));
427  ln = zeros(1,n);
428  l = ln;
429  pos = l;
430 
431  if doplot
432  pm = PlotManager(false, 2, 2);
433  end
434 
435  xi = atd.xi(:,sel);
436  fxi = atd.fxi(:,sel);
437  V = m.Data.V;
438  W = m.Data.W;
439 
440  /* % Compute constant terms */
441  xinsq = sum(xi.^2);
442 
443  /* Optional sorting of values */
444  if doplot && dosort
445  [xinsq sortidx] = sort(xinsq);
446  xi = xi(:,sortidx);
447  fxi = fxi(:,sortidx);
448  end
449 
450  xifxi = sum(xi.*fxi);
451  Vfxi = V^t*fxi;
452  Vxi = V^t*xi;
453  VV = 1;/* V'*V;
454  * if norm(VV) - 1 < 1e-12
455  * VV = 1;
456  * end */
457 
458 
459  /* Locally selected "Vz" terms */
460  zi = W^t*atd.xi(:,seli);
461  MU = atd.mui(:,seli);
462  fVzi = m.System.f.evaluate(V*zi,atd.ti(seli),MU);
463 
464  /* Only needed for efficient local lipschitz constant computation
465  * fxinsq = sum(fxi.*fxi); */
466 
467  /* % Main loop */
468  for i = 1:n
469  z = zi(:,i);
470  fz = fVzi(:,i);
471  nom = xifxi - z" *Vfxi + z "*(V^t*fz);
472  nom = nom - fz^t*xi;
473  denom = xinsq - 2*z" *Vxi + z "*(VV*z);
474  Ln = nom ./ denom;
475  /* denom(zer) = sum((xi(:,zer)-V*z(:,ones(1, length(zer)))).^2); */
476 
477  /* L = sqrt(abs((fxinsq - 2*fz'*fxi + fz'*fz) ./ denom));
478  * L2 = Norm.L2(fxi - fVzi(:,ones(1,length(sel))*i)) ./ sqrt(denom); */
479 
480  [~, idx] = sort(denom);
481  Ln = Ln(idx);
482 
483  zer = denom == 0;
484  Ln(zer) = [];
485 
486  [ln(i), pos(i)] = max(Ln);
487 
488  /* [~, sortidx] = sort(denom);
489  * Ln = Ln(sortidx); */
490  if doplot
491  ti = sprintf(" Idx %d: loc Lip const %g, loc log norm %g ",seli(i),l(i),ln(i));
492  ax = pm.nextPlot(" blah ",ti," training point "," local [lip const (b), log norm (g)] ");
493  neg = Ln <= 0;
494  if plotneg
495  Lnn = -Ln;
496  end
497  Ln(neg) = 0;
498  hlp = [L; Ln];
499  if plotneg
500  Lnn(~neg) = 0;
501  hlp = [hlp; Lnn];/* #ok */
502 
503  end
504  semilogy(ax, hlp^t);
505  end
506  pi.step;
507  end
508  if doplot
509  pm.done;
510  end
511  pi.stop;
512 
513  res = data.ApproxTrainData(zi,[],[]);
514  res.fxi= ln;
515  res = data.ApproxTrainData.makeUniqueXi(res);
516  [res, mScale, MScale] = data.ApproxTrainData.scaleXiZeroOne(res);
517  }
542 };
543 }
544 
Collection of generally useful functions.
Definition: Utils.m:17
static function pm = compareSimTransDEIMJac_FullJac_plots(m, aln, times, jtimes, deim_orders, st_sizes, pm)
See WSH12 tests_burgers for likely better plot routine.
Definition: LogNorm.m:211
LogPlot: Class with static functions for logarithmic plotting.
Definition: LogPlot.m:17
Params
The parameters usable for the dynamical system.
error.BaseEstimator ErrorEstimator
The associated error estimator for this model.
The base class for any KerMor detailed model.
Definition: BaseFullModel.m:18
models.BaseFirstOrderSystem System
The actual dynamical system used in the model.
Definition: BaseModel.m:102
static function [ aln , times , rowvec< integer > st_sizes ] = compareSimTransJac_FullJac(models.BaseFullModel m,rowvec< integer > st_sizes)
% Comparison of similarity transformed jacobian log norms to full log norms Computes logarithmic norm...
Definition: LogNorm.m:28
static function [ res , mScale , MScale , pos , l , sel , seli ] = CompLogNorms(m, numt)
LogNorm:
Definition: LogNorm.m:400
static function pm = compareSimTransJac_FullJac_plots(m, aln, times, st_sizes, pm)
Definition: LogNorm.m:87
PlotManager: Small class that allows the same plots generated by some script to be either organized a...
Definition: PlotManager.m:17
static function handle p = logsurf(handle h,matrix< double > X,matrix< double > Y,matrix< double > Z, varargin)
Creates a nice surface plot in a logarithmic scale with the given data.
Definition: LogPlot.m:79
static function handle p = cleverPlot(handle ax,rowvec< double > x,rowvec< double > y, varargin)
Calls corresponding plot routines depending on the scale of data.
Definition: LogPlot.m:158
static function struct res = getApproxLogNormsAtPos(models.BaseFullModel mo,colvec< double > x,double t,matrix< double > mui)
Computes logarithmic norms of similarity transformed AND matrix DEIM approximated jacobians at the po...
Definition: LogNorm.m:277
static function [ aln , times , jtimes , rowvec< integer > deim_orders , rowvec< integer > st_sizes ] = compareSimTransDEIMJac_FullJac(models.BaseFullModel m,rowvec< integer > deim_orders,rowvec< integer > st_sizes)
% Comparison of similarity transformed DEIM-approximated jacobian log norms to full log norms Compute...
Definition: LogNorm.m:137
#define X(i, j)
data.ModelData Data
The full model's data container. Defaults to an empty container.
static function handle p = nicesurf(handle h,matrix< double > X,matrix< double > Y,matrix< double > Z, varargin)
Creates a nice surface plot with the given data.
Definition: LogPlot.m:41
dscomponents.ACoreFun f
The core f function from the dynamical system.
data.ApproxTrainData JacobianTrainData
Training data for the jacobian approximation.
Definition: ModelData.m:120
#define Y(i, j)
static function getApproxLogNormsAtPos_plots(res, pm)
Definition: LogNorm.m:354
static function [ double ln , colvec< double > v ] = logNorm(matrix< double > A,matrix< double > G,colvec< double > v0)
Computes the logarithmic norm of a matrix , optionally using a positive definite symmetric matrix in...
Definition: Utils.m:551
ProcessIndicator: A simple class that indicates process either via waitbar or text output...