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
FileTrajectoryData.m
Go to the documentation of this file.
1 namespace data{
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 data.ATrajectoryData,
56  public:
57 
84  private:
85 
86  sizes = "[]";
95  bbmin = "[]";
104  bbmax = "[]";
113  trajlen = "[]";
114 
115 
116  width = 0;
126  public:
127 
129  if ~usejava(" jvm ")
130  error(" FileTrajectoryData cannot be used as java is not enabled. ");
131  end
132  if nargin < 1
133  data_dir = fullfile(KerMor.App.TempDirectory,sprintf(" temp_ftd_%s ",...
135  elseif ischar(datadir)
136  data_dir = datadir;
137  else
138  error(" Invalid argument: %s ",class(datadir));
139  end
140  this = this@data.FileDataCollection(data_dir);
141  this.clearTrajectories;
142  }
152  function [colvec<double>x , doublectime ] = getTrajectory(colvec<double> mu,integer inputidx) {
153 
154  if nargin < 3
155  inputidx = [];
156  if nargin < 2
157  mu = [];
158  end
159  end
160  x = []; ctime = Inf;
161  s = this.getData([mu; inputidx]," x "," ctime ");
162  if ~isempty(s)
163  ctime = s.ctime;
164  x = s.x;
165  end
166  }
181  function n = getNumTrajectories() {
182  n = this.getCollectionSize;
183  }
184 
185 
186  function [colvec<double>x , colvec<double>mu , integerinputidx , doublectime ] = getTrajectoryNr(nr) {
187  s = this.getDataNr(nr," x "," mu "," inputidx "," ctime ");
188  x = s.x;
189  mu = s.mu;
190  inputidx = s.inputidx;
191  ctime = s.ctime;
192  }
201  function addTrajectory(colvec<double> x,colvec<double> mu,integer inputidx,double ctime) {
202 
203  if nargin < 4
204  inputidx = [];
205  if nargin < 3
206  mu = [];
207  end
208  end
209 
210  newd = size(x,1); newmu = length(mu);
211  if isempty(this.sizes) || all(this.sizes == 0)
212  this.sizes= [newd newmu];
213  elseif newd ~= this.sizes(1)
214  error(" Invalid trajectory dimension. Existing: %d, new: %d ",this.sizes(1),newd);
215  end
216 
217  exists = this.hasData([mu; inputidx]);
218  if this.UniformTrajectories
219  if isempty(this.trajlen)
220  this.trajlen= size(x,2);
221  elseif this.trajlen ~= size(x,2)
222  error(" Invalid trajectory length. Existing: %d, new: %d ",this.trajlen,size(x,2));
223  end
224  else
225  if ~exists
226  this.width= this.width + size(x,2);
227  end
228  end
229 
230  if ~this.ReplaceExisting && exists
231  warning(" KerMor:FileTrajectoryData "," Trajectory already present and replacing is disabled. ");
232  return;
233  end
234  traj.x= x;
235  traj.ctime= ctime;
236  traj.inputidx= inputidx;
237  traj.mu= mu;
238  this.addData([mu; inputidx], traj);
239  this.updateBB(x);
240  }
252  function l = getTotalLength() {
253  if this.UniformTrajectories
254  l = this.getNumBlocks * this.trajlen;
255  else
256  l = this.width;
257  end
258  }
259 
260 
261  function clearTrajectories() {
262  this.clear;
263  this.bbmin= [];
264  this.bbmax= [];
265  this.sizes= [];
266  this.trajlen= [];
267  }
268 
269 
271  this.bbmin= [];
272  this.bbmax= [];
273 
274  ni = max(model.TrainingInputCount,1);
275  ns = model.Data.SampleCount;
276  pi = ProcessIndicator(" Consolidating FileTrajectoryData ",ni*ns);
277  for u=1:ni
278  ui = u;
279  if model.TrainingInputCount == 0
280  ui = [];
281  end
282  for n=1:ns
283  mu = model.Data.ParamSamples(:,n);
284  /* GetData has a "backup" feature to also find files if
285  * not enlisted in the hash map; if so, the hash map is
286  * updated automatically! */
287  s = this.getData([mu; ui]);
288  if ~isempty(s)
289  this.updateBB(s.x);
290  if this.UniformTrajectories && isempty(this.trajlen)
291  this.trajlen= size(s.x,2);
292  end
293  end
294  pi.step;
295  end
296  end
297  pi.stop;
298  }
322  function [colvec<double>x , X ] = getBoundingBox() {
323  x = this.bbmin;
324  X = this.bbmax;
325  }
333  function [d , mud ] = getTrajectoryDoFs() {
334  if isempty(this.sizes)
335  if this.getNumTrajectories > 0
336  [x,mu] = this.getTrajectoryNr(1);
337  d = size(x,1);
338  mud = length(mu);
339  else
340  d = 0; mud = 0;
341  end
342  this.sizes= [d mud];
343  end
344  d = this.sizes(1);
345  mud = this.sizes(2);
346  }
347 
348 
349  private:
350 
351  function updateBB(colvec<double> x) {
352  [m,M] = Utils.getBoundingBox(x);
353  if isempty(this.bbmin)
354  this.bbmin= m;
355  this.bbmax= M;
356  else
357  this.bbmin= min(this.bbmin,m);
358  this.bbmax= max(this.bbmin,M);
359  end
360  }
369  protected: /* ( Static ) */
370 
371  static function this = loadobj(this,initfrom) {
372 
373  created = false;
374  if ~isa(this," data.FileTrajectoryData ")
375  initfrom = this;
376  this = data.FileTrajectoryData(initfrom.DataDirectory);
377  created = true;
378  end
379  if nargin == 2 || created
380  this.sizes= initfrom.sizes;
381  this.bbmin= initfrom.bbmin;
382  this.bbmax= initfrom.bbmax;
383  this.trajlen= initfrom.trajlen;
384  this.UniformTrajectories= initfrom.UniformTrajectories;
385  this.ReplaceExisting= initfrom.ReplaceExisting;
386  /* property from ABlockedData */
387  this.MinRelSingularValueSize= initfrom.MinRelSingularValueSize;
388  this = loadobj@data.FileDataCollection(this, initfrom);
389  else
390  this = loadobj@data.FileDataCollection(this);
391  end
392  }
407  public: /* ( Static ) */
408 
409  static function res = test_FileTrajectoryData() {
410 
411  m = data.FileTrajectoryData;
412 
413  T = 10;
414  res = true;
415  for i=1:T;
416  tr(:,:,i) = rand(30,50);/* #ok */
417 
418  p(:,i) = rand(4,1);/* #ok */
419 
420  in(i) = i;/* #ok */
421 
422  end
423 
424  /* Params only */
425  for i=1:T;
426  m.addTrajectory(tr(:,:,i),p(:,i),[],1);
427  end
428  res = res && m.getNumTrajectories == T;
429 
430  pos = false(1,1,T);
431  ppos = false(1,T);
432  for i=1:T;
433  [x, pi] = m.getTrajectoryNr(i);
434  pos = pos | sum(sum(repmat(x,[1 1 T]) - tr,1),2) == 0;
435  ppos = ppos | sum(repmat(pi,1,T) - p,1) == 0;
436 
437  x = m.getTrajectory(p(:,i),[]);
438  res = res && isequal(x,tr(:,:,i));
439  end
440  res = res && all(pos) && all(ppos);
441  m.clearTrajectories;
442 
443  /* Inputs only */
444  for i=1:T;
445  m.addTrajectory(tr(:,:,i),[],in(i),1);
446  end
447  res = res && m.getNumTrajectories == T;
448 
449  pos = false(1,1,T);
450  ipos = false(1,T);
451  for i=1:T
452  [x, ~, ini] = m.getTrajectoryNr(i);
453  pos = pos | sum(sum(repmat(x,[1 1 T]) - tr,1),2) == 0;
454  ipos = ipos | repmat(ini,1,T)-in == 0;
455 
456  x = m.getTrajectory([],in(i));
457  res = res && isequal(x,tr(:,:,i));
458  end
459  res = res && all(pos) && all(ipos);
460  m.clearTrajectories;
461 
462  /* Both */
463  for i=1:T;
464  m.addTrajectory(tr(:,:,i),p(:,i),in(i),1);
465  end
466  res = res && m.getNumTrajectories == T;
467 
468  pos = false(1,1,T);
469  ppos = false(1,T);
470  ipos = false(1,T);
471  for i=1:T
472  [x, pi, ini] = m.getTrajectoryNr(i);
473  pos = pos | sum(sum(repmat(x,[1 1 T]) - tr,1),2) == 0;
474  ppos = ppos | sum(repmat(pi,1,T) - p,1) == 0;
475  ipos = ipos | repmat(ini,1,T)-in == 0;
476 
477  x = m.getTrajectory(p(:,i),in(i));
478  res = res && isequal(x,tr(:,:,i));
479  end
480  res = res && all(pos) && all(ppos) && all(ipos);
481  m.clearTrajectories;
482  }
483 
484 
485 
486 };
487 }
488 
function res = hasData(keydata)
function [ colvec< double > x , colvec< double > mu , integer inputidx , double ctime ] = getTrajectoryNr(nr)
Gets the trajectory with the number nr.
Collection of generally useful functions.
Definition: Utils.m:17
logical ReplaceExisting
Flag that indicates if a warning should be issued if a trajectory already exists and is about to be r...
function [ n , m ] = size(dim)
function [ d , mud ] = getTrajectoryDoFs()
Returns the degrees of freedom for the trajectories and parameter size.
function consolidate(models.BaseFullModel model)
Rebuild the hashmap for the current FileData using the current ParamSamples and the models training i...
static function gen = generateID()
Generates a new unique ID.
Definition: IDGenerator.m:90
static function this = loadobj(this, initfrom)
Loads a FileTrajectoryData instance.
The base class for any KerMor detailed model.
Definition: BaseFullModel.m:18
SampleCount
The number of samples contained in the model data.
Definition: ModelData.m:167
function [ colvec< double > x , double ctime ] = getTrajectory(colvec< double > mu,integer inputidx)
Gets a system's trajectory for the given and inputindex. Returns [] if no trajectory is found in the...
function struct data = getDataNr(nr, varargin)
Retrieves data from the nr-st collection element.
static function res = test_FileTrajectoryData()
logical UniformTrajectories
Flag that checks if newly added trajectories must have the size of already existing trajectories...
function addTrajectory(colvec< double > x,colvec< double > mu,integer inputidx,double ctime)
Adds a trajectory to the ModelData instance.
matrix ParamSamples
A Model's parameter samples as column vector collection.
Definition: ModelData.m:53
An integer value.
function clearTrajectories()
Clears all stored trajectory data.
A boolean value.
function [ colvec< double > x , X ] = getBoundingBox()
Gets the bounding box of the state space of all trajectories.
function n = getCollectionSize()
FileDataCollection: Basic class for storing data given a hashable key value.
#define X(i, j)
data.ModelData Data
The full model's data container. Defaults to an empty container.
Speed test * all(1:3)
function n = getNumTrajectories()
Gets the total number of trajectories.
integer TrainingInputCount
Gets the number of inputs used for training.
double MinRelSingularValueSize
The minimum relative value of singular values that triggers selection of the compared to the largest ...
Definition: ABlockedData.m:42
function addData(keydata, data)
Global configuration class for all KerMor run-time settings.
Definition: KerMor.m:17
FileTrajectoryData(datadir)
Creates a new filesystem-based trajectory data container.
function n = getNumBlocks()
% data.ABlockedData implementations
Generates unique IDs.
Definition: IDGenerator.m:17
function struct data = getData(rowvec< double > keydata, varargin)
methods(Access=protected) Retrieves data from the collection element of a given key.
static function KerMor theinstance = App()
The singleton KerMor instance.
Definition: KerMor.m:910
FileTrajectoryData: Trajectory data stored in external files.
Data class that contains a model's large data, including subspace matrices, trajectories and approxim...
function l = getTotalLength()
Length of the stored trajectories.
ProcessIndicator: A simple class that indicates process either via waitbar or text output...
static function [ double bmin , double bmax ] = getBoundingBox(double vectors)
Gets the bounding box for a matrix containing column vectors.
Definition: Utils.m:96