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
ReducedModel.m
Go to the documentation of this file.
1 namespace models{
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 models.BaseModel {
51  public:
52 
116  public: /* ( Dependent ) */
117 
131  private:
132 
133  error.BaseEstimator fErrorEstimator;
143  public:
144 
145 
146  ReducedModel(fullmodel) {
147  this = this@models.BaseModel;
148  this.FullModel= fullmodel;
149  }
150 
151 
152  function delete() {
153  this.ErrorEstimator= [];
154  this.V= [];
155  this.W= [];
156  /* Delete the full model with it's ModelData instance at last (V, W might have data
157  * there) */
158  this.FullModel= [];
159  }
160 
161 
162 
163  public: /* ( Sealed ) */
164 
165 
166  function build(varargin) {
167 
168  disp(" Building reduced model... ");
169  fullmodel = this.FullModel;
170  fd = fullmodel.Data;
171  if isempty(fullmodel.Approx) && isempty(fullmodel.SpaceReducer)
172  error(" No reduction methods found on full model. No use in building a reduced model from it. ");
173  end
174 
175 
176  /* Update name */
177  this.Name= [" Reduced: " fullmodel.Name];
178 
179  /* % Copy common values from the full model */
180  this.T= fullmodel.T;
181  this.dt= fullmodel.dt;
182  this.tau= fullmodel.tau;
183  this.isStatic= fullmodel.isStatic;
184  this.DefaultMu= fullmodel.DefaultMu;
185  this.DefaultInput= fullmodel.DefaultInput;
186  this.G= fullmodel.G;
187 
188  /* % Get projection matrix */
189  [this.V, this.W] = fullmodel.assembleProjectionMatrices(varargin[:]);
190  this.ParamSamples= fd.ParamSamples;
191 
192  /* % Create a new reducedSystem passing this reduced model */
193  fsys = fullmodel.System;
194  this.System= fsys.getReducedSystemInstance(this);
195  this.System.build;
196 
197  /* % Solver stuff */
198  fms = fullmodel.ODESolver;
199  if isa(fms," solvers.SemiImplicitEuler ")
200  this.ODESolver= solvers.SemiImplicitEuler(this);
201  elseif isa(fms," solvers.FullyImplEuler ")
202  this.ODESolver= solvers.FullyImplEuler(this);
203  else
204  this.ODESolver= fms;
205  end
206 
207  /* Use the error estimator that has been precomputed in
208  * the full model */
209  if ~isempty(fullmodel.ErrorEstimator)
210  this.ErrorEstimator= fullmodel.ErrorEstimator.prepareForReducedModel(this);
211  end
212  }
239  function [rowvec<double>t , matrix<double>x , doublectime ] = computeTrajectory(colvec mu,integer inputidx) {
240 
241  /* Call constat pre-computations */
242  cpre = 0;
243  if ~isempty(this.ErrorEstimator)
244  this.ErrorEstimator.clear;
245  if this.ErrorEstimator.Enabled
246  cpre = this.ErrorEstimator.prepareConstants(mu, inputidx);
247  end
248  end
249 
250  /* Call inherited method (actual work) */
251  [t, xext, ctime] = computeTrajectory@models.BaseModel(this, mu, inputidx);
252 
253  /* Split up results; the last rows of the ode solution contain
254  * any online-computable errors */
255  cpost = 0;
257  x = xext(1:end-this.ErrorEstimator.ExtraODEDims,:);
258  cpost = this.ErrorEstimator.postProcess(xext, t, inputidx);
259  else
260  x = xext;
261  end
262  ctime = ctime + cpre + cpost;
263  fprintf(" Finished after %gs\n ",ctime);
264  }
284  function saveFinal(filename) {
285  a = this.FullModel.Approx;
286  d = this.FullModel.Data;
287 
288  this.FullModel.Approx= [];
289  this.FullModel.Data= [];
290 
291  save(filename, " this ");
292 
293  this.FullModel.Approx= a;
294  this.FullModel.Data= d;
295  }
310  protected: /* ( Sealed ) */
311 
312 
313  function [JacFun , JPattern ] = getJacobianInfo() {
314  est = this.fErrorEstimator;
315  [JacFun, JPattern] = getJacobianInfo@models.BaseModel(this);
316  /* Need extra care if we have an error estimator set */
317  if ~isempty(est) && est.Enabled
318  edims = est.ExtraODEDims;
319  sys = this.System;
320  if ~isempty(sys.A) && ~isempty(sys.f)
321  JacFun_ = @(t, x)sys.A.getStateJacobian(x(1:end-edims), t) + sys.f.getStateJacobian(x(1:end-edims), t);
322  elseif ~isempty(sys.A)
323  JacFun_ = @(t, x)sys.A.getStateJacobian(x(1:end-edims), t);
324  elseif ~isempty(sys.f)
325  JacFun_ = @(t, x)sys.f.getStateJacobian(x(1:end-edims), t);
326  end
327  /* Simply pad with zero entries for the error estimator -
328  * not ideal, but must do for the moment. */
329  pad = zeros(edims,edims);
330  JacFun = @(t, x)blkdiag(JacFun_(t,x),pad);
331  end
332  }
333 
334 
335  public: /* ( Sealed ) */
336 
337  function [f , ax ] = plot(double t,matrix<double> y,varargin) {
338  [f,ax] = this.FullModel.plot(t, y, varargin[:]);
339  }
340 
341 
342  function [f , ax ] = plotState(double t,matrix<double> y,varargin) {
343  [f,ax] = this.FullModel.plotState(t, y, varargin[:]);
344  }
345 
346 
347  function [f , ax ] = plotSingle(double t,matrix<double> y,varargin) {
348  [f,ax] = this.FullModel.plotSingle(t, y, varargin[:]);
349  }
350 
351 
352  public:
353 
354 
355  function [file , folder ] = createImage() {
356 
357  n = this.Name;
358  h = KerMorLogo;
359  text(-6,5,1.05,n," FontSize ",14," FontWeight "," bold ");
360  folder = KerMor.App.TempDirectory;
361  file = " modelimage ";
362  Utils.saveFigure(h, fullfile(folder,file)," png ");
363  file = [file " .png "];
364  close(h);
365  }
378 #if 0 //mtoc++: 'get.ErrorEstimator'
379 function e = ErrorEstimator() {
380  e = this.fErrorEstimator;
381  }
382 
383 #endif
384 
385 
386 
387 #if 0 //mtoc++: 'set.ErrorEstimator'
388 function ErrorEstimator(error.BaseEstimator value) {
389  if ~isempty(value)
390  if ~isa(value," error.BaseEstimator ")
391  error(" The ErrorEstimator property must be a subclass of the error.BaseEstimator class. ");
392  end
393  msg = value.validModelForEstimator(this.FullModel);
394  if ~isempty(msg)
395  error(msg);
396  end
397  end
398  this.fErrorEstimator= value;
399  }
400 
401 #endif
402 
403 
404 
405 #if 0 //mtoc++: 'set.FullModel'
406 function FullModel(value) {
407  if ~isempty(value) && ~isa(value," models.BaseFullModel ")
408  error(" ReducedModel instances require a full model to build from. ");
409  end
410  this.FullModel= value;
411  }
412 
413 #endif
414 
415 
416 /* /* methods(Static,Access=protecte*/
417  methods(Static,Access=protected)
418  * function obj = loadobj(s)
419  * % Creates a new reduced model instance and loads its properties
420  * % from the given struct.
421  * %
422  * % This method is only implemented as the property assignment
423  * % order is important for a reduced model (example: cannot set
424  * % an error estimator before having set the models system)
425  * obj = models.ReducedModel;
426  *
427  * % Load BaseModel's properties
428  * obj = loadobj@models.BaseModel(s, obj);
429  *
430  * % Load local properties
431  * di = ALoadable.getObjectDict;
432  * ex = di(s.FullModel.ID);
433  * if ~isempty(ex)
434  * obj.FullModel = ex;
435  * else
436  * obj.FullModel = s.FullModel;
437  * end
438  * obj.V = s.V; obj.W = s.W;
439  * obj.ParamSamples = s.ParamSamples;
440  * obj.ErrorEstimator = s.ErrorEstimator;
441  * end
442  * end */
443 
453 };
454 }
455 
456 
457 
char Name
The name of the Model.
Definition: BaseModel.m:117
Collection of generally useful functions.
Definition: Utils.m:17
**Load BaseModel s properties * obj
Definition: ReducedModel.m:428
BaseModel: Base class for both full and reduced models.
Definition: BaseModel.m:18
logical isStatic
Determines if the model is time dependent or static.
Definition: BaseModel.m:174
The base class for any KerMor detailed model.
Definition: BaseFullModel.m:18
models.BaseFullModel FullModel
The full model this reduced model was created from.
Definition: ReducedModel.m:53
double dt
The desired time-stepsize for simulations.
Definition: BaseModel.m:291
function [ f , ax ] = plotState(double t,matrix< double > y, varargin)
Definition: ReducedModel.m:342
*if ~isempty(ex)*obj.FullModel
models.BaseFirstOrderSystem System
The actual dynamical system used in the model.
Definition: BaseModel.m:102
methods(Static, Access=protected)*function obj
matrix< double > ParamSamples
The originally used parameter samples.
Definition: ReducedModel.m:100
function build(varargin)
Creates a new reduced model instance from a full model.
Definition: ReducedModel.m:166
function rsys = getReducedSystemInstance(models.ReducedModel rmodel)
Creates a reduced system given the current system and the reduced model.
matrix< double > G
The custom scalar product matrix .
Definition: BaseModel.m:132
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
matrix< double > W
The biorthogonal matrix for V, i.e. .
Definition: ReducedModel.m:85
Base class for all error estimators.
Definition: BaseEstimator.m:18
Enabled
Flag that indicates whether error estimation is used or not.
Definition: BaseEstimator.m:67
A variable number of input arguments.
error.BaseEstimator ErrorEstimator
The error estimator for the reduced model.
Definition: ReducedModel.m:118
double tau
Time scaling factor .
Definition: BaseModel.m:252
solvers.BaseSolver ODESolver
The solver to use for the ODE. Must be an instance of any solvers.BaseSolver subclass.
Definition: BaseModel.m:315
function double ct = postProcess(unused1,rowvec t, unused2)
Post-processes the error estimator ODE part after reduced simulation computation. ...
static function saveFigure(fig, filename, ext)
Opens a matlab save dialog and saves the given figure to the file selected.
Definition: Utils.m:324
function [ f , ax ] = plotSingle(double t,colvec< double > y, varargin)
Plots a single solution. Override in subclasses for specific plot behaviour.
Definition: BaseModel.m:595
integer DefaultInput
The default input to use if none is given.
Definition: BaseModel.m:189
A matlab column vector.
function [ rowvec< double > t , matrix< double > x , double ctime ] = computeTrajectory(colvec mu,integer inputidx)
Computes an approximated solution/trajectory for the given mu and inputidx in the coefficient space...
Definition: ReducedModel.m:239
ReducedModel(fullmodel)
Definition: ReducedModel.m:146
function prepared = prepareForReducedModel(models.ReducedModel rmodel)
Default implementation which simply clones this (subclass!)instance and returns the copy to use in re...
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...
double T
The final timestep up to which to simulate.
Definition: BaseModel.m:271
data.ModelData Data
The full model's data container. Defaults to an empty container.
function [ file , folder ] = createImage()
This method can be invoked to obtain an image for the current model.
Definition: ReducedModel.m:355
function ct = prepareConstants(colvec< double > mu, unused1)
function [ f , ax ] = plotSingle(double t,matrix< double > y, varargin)
Definition: ReducedModel.m:347
colvec< double > DefaultMu
The default parameter value if none is given.
Definition: BaseModel.m:355
function saveFinal(filename)
Saves this reduced model for final use.
Definition: ReducedModel.m:284
static function this = loadobj(this, s)
Definition: BaseModel.m:814
function [ f , ax ] = plot(double t,matrix< double > y, varargin)
Definition: ReducedModel.m:337
function clear()
Clears the last error set by the estimator.
function [ handle f , handle ax ] = plot(rowvec< double > t,matrix< double > y, varargin)
Plots the results of the simulation. Override in subclasses for a more specific plot if desired...
Definition: BaseModel.m:539
Global configuration class for all KerMor run-time settings.
Definition: KerMor.m:17
function [ handle f , handle ax ] = plotState(rowvec t,matrix< double > x, varargin)
Plots the results of the simulation. Override in subclasses for a more specific plot if desired...
Definition: BaseModel.m:567
static function KerMor theinstance = App()
The singleton KerMor instance.
Definition: KerMor.m:910
ExtraODEDims
The dimensions added to the ODE function by the estimator.
Definition: BaseEstimator.m:97
function [ JacFun , JPattern ] = getJacobianInfo()
Definition: ReducedModel.m:313