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
PODGreedy.m
Go to the documentation of this file.
1 namespace spacereduction{
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 PODGreedy
20  public IParallelizable {
58  public: /* ( setObservable ) */
59 
60  double Eps = 1e-6;
129  public:
130 
132 
133 
134  public:
135 
137  this.registerProps(" Eps "," MinRelImprovement "," MaxSubspaceSize "," InitialSpace ");
138  }
139 
140 
141  function plotSummary(pm,context) {
142  if nargin < 2
143  pm = PlotManager;
144  pm.LeaveOpen= true;
145  context = " POD-Greedy ";
146  end
147  plotSummary@spacereduction.BaseSpaceReducer(this, pm, context);
148  if ~isempty(this.ErrorImprovements)
149  str = sprintf(" %s: Error decay over training data\nEps:%g, MaxSize:%d, FXI: %d, FD: %d, BSpan: %d ",...
150  context,this.Eps,this.MaxSubspaceSize,this.IncludeTrajectoryFxiData,...
152  h = pm.nextPlot(" podgreedy_gains ",str," subspace size "," Gain ");
153  semilogy(h,this.ErrorImprovements," LineWidth ",2);
154  else
155  warning(" spacereduction:PODGreedy ",...
156  " Error data empty. Not providing summary. ");
157  end
158  }
159 
160 
161 
162  protected:
163 
164 
165  function [V , W ] = generateReducedSpaceImpl(models.BaseFullModel model,subset) {
166  bdata = model.Data.TrajectoryData;
167 
168  /* Wrap in finite difference adder */
170  bdata = data.JoinedBlockData(bdata, data.FinDiffBlockData(bdata));
171  end
172  if this.IncludeAxData
173  bdata = data.JoinedBlockData(bdata, ...
174  data.AxBlockData(bdata, model.System.A, model.scaledTimes));
175  end
176  /* Augment block data with fxi values */
178  if isempty(model.Data.TrajectoryFxiData)
179  error(" No training fxi data found in ModelData. ");
180  end
181  bdata = data.JoinedBlockData(bdata, model.Data.TrajectoryFxiData);
182  end
183 
184  if KerMor.App.Verbose > 2
185  fprintf(" POD-Greedy: Starting subspace computation using %d trajectories...\n ",model.Data.TrajectoryData.getNumTrajectories);
186  end
187 
188  pod = general.POD;
189  pod.Value= 1; /* MUST stay at 1 or getInitialSpace will fail. */
190 
191  pod.Mode= " abs ";
192 
193  o = general.Orthonormalizer;
194  o.Algorithm= " gs ";
195 
196  /* Compute initial space */
197  if KerMor.App.Verbose > 2
198  fprintf(" POD-Greedy: Computing initial space...\n ");
199  end
200  if ~isempty(this.InitialSpace)
201  V = this.InitialSpace(subset,:);
202  if this.IncludeBSpan
203  V = o.orthonormalize([V bdata.InputSpaceSpan(subset,:)]);
204  end
205  elseif this.IncludeBSpan
206  V = model.Data.InputSpaceSpan(subset,:);
207  else
208  V = this.getInitialSpace(bdata, pod, subset);
209  end
210  /* Compute initial space errors */
211  err = [];
212  for k=1:size(V,2)
213  [err(end+1), idx] = this.getMaxErr(V(:,1:k), bdata, subset);/* #ok */
214 
215  end
216 
217  cnt = 1;
218  /* Maximum possible subspace size */
219  ss = size(V,1);
220  olderr = err;
221  impr = 1;
222  while (err(end) > this.Eps) && cnt < this.MaxSubspaceSize && size(V,2) < ss && impr(end) > this.MinRelImprovement
223  x = bdata.getBlock(idx); /* get trajectory */
224 
225  x = x(subset,:);
226  e = x - V*(V^t*x);
227  Vn = pod.computePOD(e);
228  V = o.orthonormalize([V Vn]);
229  [err(end+1), idx] = this.getMaxErr(V, bdata, subset);/* #ok */
230 
231  impr(end+1) = (olderr-err(end))/olderr;/* #ok */
232 
233  olderr = err(end);
234  cnt = cnt+1;
235  end
236  this.ErrorImprovements= impr;
237  this.ProjectionError= err;
238  if cnt == this.MaxSubspaceSize
239  fprintf(" POD-Greedy: Maximum predefined subspace size %d reached. Aborting.\n ",this.MaxSubspaceSize);
240  end
241  if size(V,2) == ss
242  fprintf(" POD-Greedy: Maximum possible subspace size %d reached. Aborting.\n ",ss);
243  end
244  if impr(end) <= this.MinRelImprovement
245  fprintf(" POD-Greedy: Minimum relative improvement of %f not satisfied. Aborting.\n ",this.MinRelImprovement);
246  end
247  if KerMor.App.Verbose > 1
248  fprintf(" POD-Greedy: Finished with subspace size %d and error %e.\n ",size(V,2),err(end));
249  if KerMor.App.Verbose > 2
250  figure; subplot(1,2,1);
251  semilogy(err);
252  xlabel(" POD-Greedy step "); ylabel(" Error norms ");
253  title(" L_2 norm sum over whole projection error trajectory vectors ");
254  subplot(1,2,2);
255  plot(impr);
256  xlabel(" POD-Greedy step "); ylabel(" Relative improvement ");
257  title(" Error improvement relative to previous error ");
258  end
259  end
260  /* Galerkin projection! */
261  W = [];
262  }
263 
264 
265 
266 
267  private:
268 
269 
270  function [maxerr , midx ] = getMaxErr(V,md,reducable) {
271  midx = -1;
272  maxerr = 0;
273  if this.ComputeParallel
274  n = md.getNumBlocks;
275  if KerMor.App.Verbose > 3
276  fprintf(" POD-Greedy: Computing maximum error over %d trajectories on %d workers for subspace size %d...\n ",n,matlabpool(" size "),size(V,2));
277  end
278  err = zeros(1,n);
279  parfor i=1:n
280  x = md.getBlock(i); /* #ok<PFBNS> */
281 
282  x = x(reducable,:);
283  hlp = sum(Norm.L2(x - V*(V^t*x)));
284  if KerMor.App.Verbose > 4
285  fprintf(" POD-Greedy: Error for block %d: %e\n ",i,hlp);
286  end
287  err(i) = hlp;
288  end
289  [maxerr, midx] = max(err);
290  else
291  for k=1:md.getNumBlocks;
292  x = md.getBlock(k);
293  x = x(reducable,:);
294  e = sum(Norm.L2(x - V*(V^t*x)));
295  if maxerr < e
296  maxerr = e;
297  midx = k;
298  end
299  end
300  end
301  if KerMor.App.Verbose > 0
302  fprintf(" POD-Greedy: Max error with subspace size %d: %e\n ",size(V,2),maxerr);
303  end
304  }
305 
306 
307  public: /* ( Static ) */
308 
309  static function test_PODGreedy() {
310 
311  sv = 15;
312  dim = 100;
313  dt = .05;
314  T = 1;
315 
316  m = models.BaseFullModel;
317  m.dt= dt;
318  m.T= T;
319 
320  m.Sampler= sampling.GridSampler;
321 
322  m.Approx= [];
323 
324  tg = spacereduction.PODGreedy;
325  tg.Eps= 1e-5;
326  m.SpaceReducer= tg;
327 
328  m.ODESolver= solvers.ExplEuler(dt);
329 
330  s = models.BaseFirstOrderSystem(m);
331  s.B= [];
332  s.addParam(" mu1 ",.5," Range ",[0 1]," Desired ",4);
333  s.addParam(" mu2 ",.6," Range ",[0 1]," Desired ",5);
334  s.x0= dscomponents.ConstInitialValue(rand(dim,1)*5);
335  s.MaxTimestep= dt;
336  m.System= s;
337 
338  f = dscomponents.ParamTimeKernelCoreFun(m.System);
339  kexp = kernels.ParamTimeKernelExpansion;
340  kexp.Kernel= kernels.GaussKernel(40);
341  kexp.TimeKernel= kernels.NoKernel;
342  kexp.ParamKernel= kernels.GaussKernel(2);
343  kexp.Ma= rand(dim,sv);
344  kexp.Centers.xi= repmat(linspace(0,10,sv),dim,1);
345  kexp.Centers.ti= 1:sv;
346  kexp.Centers.mui= rand(2,sv);
347  f.Expansion= kexp;
348  s.f= f;
349 
350  m.offlineGenerations;
351  }
352 
353 
354 
355 };
356 }
357 
double Eps
The desired accuracy over all training trajectories.
Definition: PODGreedy.m:60
matrix< double > InitialSpace
An initial space to use.
Definition: PODGreedy.m:108
matrix< double > InputSpaceSpan
The orthonormalized vectors spanning the space of the input components.
Definition: ModelData.m:97
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
A double value.
function registerProps(varargin)
Call this method at any class that defines DPCM observed properties.
Definition: DPCMObject.m:125
An integer value.
PODGreedy: Greedy subspace computation over a fixed set of trajectories.
Definition: PODGreedy.m:18
data.FileDataCollection TrajectoryFxiData
Evaluations of the system's nonlinearity on the trajectoriy data.
Definition: ModelData.m:85
PlotManager: Small class that allows the same plots generated by some script to be either organized a...
Definition: PlotManager.m:17
function plotSummary(pm, context)
Definition: PODGreedy.m:141
ComputeParallel
Flag whether the code should be run in parallel or not.
logical LeaveOpen
Flag indicating if the plots should be left open once the PlotManager is deleted (as variable) ...
Definition: PlotManager.m:213
integer MaxSubspaceSize
The maximum subspace size, EXCLUDING any non-reducable dimensions.
Definition: PODGreedy.m:93
double MinRelImprovement
The minimum required relative error improvement compared to the previous error.
Definition: PODGreedy.m:76
virtual function n = getNumTrajectories()
Gets the total number of trajectories.
data.ModelData Data
The full model's data container. Defaults to an empty container.
logical IncludeTrajectoryFxiData
Flag if to include values for each value, too.
IParallelizable Interface to indicate parallel computation capability of the implementing classes...
function [ V , W ] = generateReducedSpaceImpl(models.BaseFullModel model, subset)
Definition: PODGreedy.m:165
rowvec< double > scaledTimes
The time steps Times in scaled time units .
Definition: BaseModel.m:218
static function test_PODGreedy()
Definition: PODGreedy.m:309
static function rowvec< double > n = L2(matrix< double > x)
Returns the discrete norm for each column vector in x.
Definition: Norm.m:39
Global configuration class for all KerMor run-time settings.
Definition: KerMor.m:17
static function KerMor theinstance = App()
The singleton KerMor instance.
Definition: KerMor.m:910
Norm: Static class for commonly used norms on sets of vectors.
Definition: Norm.m:17
Base class for all space reduction algorithms.
function V = getInitialSpace(blockdata, pod, subset)
Computes the initial space, which is the first POD mode of the initial values!
dscomponents.LinearCoreFun A
Represents a linear or affine-linear component of the dynamical system.
data.ATrajectoryData TrajectoryData
The trajectory training data for the full model (only complete trajectories!)
Definition: ModelData.m:63