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
TPWLLocalLipEstimator.m
Go to the documentation of this file.
1 namespace error{
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 
19  :public error.BaseEstimator {
39  public:
40 
41 
62  private:
63 
64  KernelLipschitzFcn;
65 
66  A;
67 
68  Ab;
69 
70  b;
71 
72  AB;
73 
74  bB;
75 
76  B;
77 
78  AV;
79 
80  neg_e1 = false;
81 
82  Ainorms;
83 
84  xi;
85 
86 
87  public:
88 
90  this.ExtraODEDims= 2;
91  }
92 
93 
94  function copy = clone() {
95  error(" not yet implemented ");
96  }
97 
98 
100 
101  /* Call superclass method to perform standard estimator
102  * computations */
103  p = prepareForReducedModel@error.BaseEstimator(this, rmodel);
104 
105  p.KernelLipschitzFcn= rmodel.System.f.GaussWeight.getLipschitzFunction;
106 
107  /* Get full model */
108  fm = rmodel.FullModel;
109  if ~isempty(fm.System.B)
110  try
111  fB = fm.System.B.evaluate([],[]);
112  catch ME/* #ok */
113 
114  fB = fm.System.B.evaluate(0,rmodel.getRandomParam);
115  warning(" Some:Id "," Error estimator for current system will not work correctly! (B is not linear and mu-independent! ");
116  end
117  end
118 
119  p.xi= fm.Approx.xi;
120 
121  Ai = fm.Approx.Ai;
122  bi = fm.Approx.bi;
123  G = rmodel.GScaled;
124  n = length(Ai);
125  /* Perform any offline computations/preparations
126  * Only prepare matrices if projection is used */
127  if ~isempty(rmodel.V) && ~isempty(rmodel.W)
128  /* Compute projected versions (tilde..) */
129  for i=1:n
130  Ai[i] = (Ai[i] - rmodel.V*(rmodel.W^t*Ai[i]))*rmodel.V;
131  end
132  bi = bi - rmodel.V*(rmodel.W^t*bi);
133 
134  p.A= cell(n,n);
135  p.Ab= cell(n,n);
136  p.b= zeros(n,n);
137  [x,y] = meshgrid(1:n);
138  all = [reshape(x,1,[]); reshape(y,1,[])];
139  for k = 1:n*n
140  i = all(1,k);
141  j = all(2,k);
142  p.A[i,j] = Ai[i]^t*(G*Ai[j]);
143  p.Ab[i,j] = Ai[i]^t*(G*bi(:,j));
144  /* p.b(i,j) = bi(:,i)'*G*bi(:,j); */
145  end
146  p.b= bi^t*(G*bi);
147 
148  if ~isempty(fB)
149  p.AB= cell(1,n);
150  for i=1:n
151  p.AB[i] = Ai[i]^t*(G*fB);
152  end
153  p.bB= bi^t*(G*fB);
154  p.B= fB^t*(G*fB);
155  end
156 
157  /* Compute AV_i`s for \beta(s) comp */
158  p.AV= cell(1,n);
159  p.Ainorms= zeros(1,n);
160  for i=1:n
161  p.AV[i] = Ai[i]^t*(G*Ai[i]);
162  p.Ainorms(i) = norm(Ai[i]);
163  end
164  else
165  error(" \todo ");
166 /* % No projection means no projection error!
167  * n = size(Ma,2);
168  *
169  * if ~isempty(B)
170  * b = size(B,2);
171  * p.M2 = zeros(n,b);
172  * p.M3 = zeros(b,b);
173  * end */
174  end
175  }
190  function e = evalODEPart(colvec x,double t,double ut) {
191 
192  eold = x(end-this.ExtraODEDims+1:end);
193  e = zeros(this.ExtraODEDims,1);
194  wf = this.ReducedModel.System.f.GaussWeight;
195 
196  /* Get actual z(s) */
197  z = x(1:end-this.ExtraODEDims);
198 
199  /* Get distances */
200  di = this.xi - repmat(this.ReducedModel.V*z,1,size(this.xi,2));
201  di = sqrt(sum(di.^2,1));
202  di(di == 0) = eps;
203  w = wf.evaluateScalar(di/min(di));
204  w = w / sum(w);
205  idx = 1:length(w);
206  /* Use same criterion as also used in normal approx evaluation */
207  idx = idx(w > this.ReducedModel.System.f.MinWeightValue);
208  m = length(idx);
209  [x,y] = meshgrid(idx);
210  all = [reshape(x,1,[]); reshape(y,1,[])];
211  e(1) = 0;
212  /* Run semi-quadratic loop as many weights hopefully are zero */
213  for k = 1:m*m
214  i = all(1,k);
215  j = all(2,k);
216  e(1) = e(1) + w(i)*w(j)*(z" *this.A{i,j}*z + 2*z "*this.Ab[i,j] + this.b(i,j));
217  end
218 
219  if nargin == 5 /* An input function u is set */
220 
221  for i=1:m
222  e(1) = e(1) + w(i)*(z^t*this.AB[i]*ut + this.bB(i,:)*ut);
223  end
224  e(1) = e(1) + ut^t*this.B*ut;
225  end
226 
227  if ~this.neg_e1 && e(1) < 0
228  this.neg_e1= true;
229  end
230  /* e(1) = sqrt(abs(e(1))); */
231  e(1) = sqrt(max(e(1),0));
232 
233  /* % Normal computations
234  * Standard (worst-) Case */
235  Ct = Inf;
236  /* Time-discrete computation */
237  if this.UseTimeDiscreteC
238  Ct = eold(1) + eold(2);
239  end
240 
241  /* Part one of beta */
242  beta = wf.evaluateScalar(max(0,di-Ct)) * this.Ainorms^t;
243  /* Part two of beta */
244  av = cellfun(@(A)sqrt(z^t*A*z),this.AV);
245  beta = beta + av * this.KernelLipschitzFcn(di,Ct,t,this.mu)^t;
246 
247  e(2) = beta*(eold(1) + eold(2));
248 
249 /* % Iteration stuff
250  * if this.Iterations > 0
251  * this.times(end+1) = t;
252  * this.e1vals(end+1) = e(1);
253  * this.divals(end+1,:) = di;
254  * end */
255  }
278  function ct = postProcess(double t,colvec<double> x,integer inputidx) {
279  ct = postProcess@error.BaseEstimator(this, t, x, mu, inputidx);
280  ts = tic;
281  eint = x(end-this.ExtraODEDims+1:end,:);
282  if all(eint == 0)
283  warning(" CompWiseErrorEstimator:process "," Integral part is all zero. Attention! ");
284  end
285  this.StateError= eint(1,:) + eint(2,:);
286 
287  if this.neg_e1
288  disp(" TPWLLocalLipEstimator: Negative alpha(t) norms occurred. Used zero instead. ");
289  this.neg_e1= false;
290  end
291 
292 /* % Iteration stuff
293  * if this.Iterations > 0
294  * this.times(end+1) = t(end);
295  * if this.UseTimeDiscreteC
296  * warning('error:TPWLLocalLipEstimator','Using Iterations together with TimeDiscreteC will yield no improvement. Not performing iterations.');
297  * else
298  * this.performIterations(t, mu);
299  * end
300  * end */
301 
302  /* Tranform to output error estimation */
303  C = this.ReducedModel.FullModel.System.C.evaluate(0,[]);
304  this.StateError= norm(C)*this.StateError;
305 
306  ct = ct + toc(ts);
307  }
308 
309 
310  function e0 = init(colvec<double> mu) {
311  e0 = [this.ReducedModel.getExo(mu); 0];
312  }
323  function clear() {
324  clear@error.BaseEstimator(this);
325  this.neg_e1= false;
326  }
327 
328 
329 
330 
331  public: /* ( Static ) */
332 
333  static function errmsg = validModelForEstimator(models.ReducedModel rmodel) {
334  errmsg = ;
335  if ~isa(rmodel.FullModel.System.C," dscomponents.LinearOutputConv ")
336  errmsg = " Local Lipschitz estimators work only for constant linear output conversion. ";
337  end
338  if isempty(errmsg) && ~isa(rmodel.FullModel.Approx," approx.TPWLApprox ")
339  errmsg = " The Approx class of the full model must be a subclass of approx.TPWLApprox for this error estimator. ";
340  end
341  }
355 };
356 }
357 
function p = prepareForReducedModel(models.ReducedModel rmodel)
Overrides the setReducedModel method from error.BaseEstimator and performs additional offline computa...
models.BaseFullModel FullModel
The full model this reduced model was created from.
Definition: ReducedModel.m:53
static function errmsg = validModelForEstimator(models.ReducedModel rmodel)
Validations.
models.BaseFirstOrderSystem System
The actual dynamical system used in the model.
Definition: BaseModel.m:102
dscomponents.AInputConv B
The input conversion.
A MatLab cell array or matrix.
UseTimeDiscreteC
Determines how many postprocessing iterations for the estimator are performed. For the local Lipschit...
StateError
The reduction state-space error from the last simulation.
Definition: BaseEstimator.m:82
reshape
hanges the dimensions of the handle object array to the specified dimensions. See the MATLAB reshape ...
An integer value.
The KerMor reduced model class.
Definition: ReducedModel.m:18
matrix< double > V
The matrix that has been used for projection.
Definition: ReducedModel.m:72
function ct = postProcess(double t,colvec< double > x,integer inputidx)
matrix< double > W
The biorthogonal matrix for V, i.e. .
Definition: ReducedModel.m:85
Base class for all error estimators.
Definition: BaseEstimator.m:18
A matlab column vector.
TPWLLocalLipEstimator: Local-Lipschitz estimation based error estimator for reduced models obtained u...
approx.BaseApprox Approx
The approximation method for the CoreFunction.
disp
Handle object disp method which is called by the display method. See the MATLAB disp function...
Speed test * all(1:3)
virtual function B = evaluate(colvec< double > mu)
Template method that evaluates the input conversion matrix at the current time and [optional] param...
dscomponents.LinearOutputConv C
The output conversion Defaults to an LinearOutputConv instance using a 1-matrix, which just forwards ...
function e = evalODEPart(colvec x,double t,double ut)
Evaluates the auxiliary ode part for the TPWL estimator.
dscomponents.ACoreFun f
The core f function from the dynamical system.
#define G(x, y, z)
Definition: CalcMD5.c:169
function C = evaluate(double t,colvec< double > mu)
Evaluates the output conversion matrix. In this simple case this is just the projection matrix...
models.ReducedModel ReducedModel
The reduced model associated with the error estimator.
ExtraODEDims
The dimensions added to the ODE function by the estimator.
Definition: BaseEstimator.m:97
function e0 = init(colvec< double > mu)
Returns the initial error at of the integral part.
function matrix< double > mu = getRandomParam(integer num,integer seed)
Gets a random parameter sample from the system's parameter domain P.
Definition: BaseModel.m:763