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
MLWrapper.m
Go to the documentation of this file.
1 namespace solvers{
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 MLWrapper
19  :public solvers.BaseSolver,
58  public: /* ( setObservable ) */
59 
98  private:
99 
100  fED;
108  public:
109 
110 
111  MLWrapper(solver) {
112  this = this@solvers.BaseSolver;
113  this.registerProps(" MLSolver ");
114 
115  this.SolverType= solvers.SolverTypes.Explicit;
116  if nargin > 0
117  this.MLSolver= solver;
118  slvname = char(solver);
119  if ~isempty(strfind(" ode15s ",slvname))
120  this.SolverType= solvers.SolverTypes.Implicit;
121  end
122  end
123  this.odeopts= odeset;
124  }
125 
126 
127  function [doublet , matrix<double>y ] = solve(odefun,double t,x0) {
128  opts = this.odeopts;
129  if ~isempty(this.MaxStep)
130  opts = odeset(opts, " MaxStep ", this.MaxStep);
131  end
132  if ~isempty(this.InitialStep)
133  opts = odeset(opts, " InitialStep ", this.InitialStep);
134  end
135 
136  /* Use AJacobianSolver properties */
137  opts = odeset(opts, " Jacobian ", this.JacFun);
138  JP = [];
139  /* Matlab solvers only use patterns if the jacobian function is
140  * not directly given */
141  if isempty(this.JacFun)
142  JP = this.JPattern;
143  end
144  opts = odeset(opts, " JPattern ", JP);
145 
146  /* Pass Mass Matrix and initial slope to solver (if set) */
147  M = []; yp0 = odefun(0,x0); MS = [];
148  if ~isempty(this.M)
149  if ~this.M.TimeDependent
150  M = this.M.evaluate(0);
151  Mt0 = M;
152  else
153  M = @(t,notused)this.M.evaluate(t);
154  Mt0 = M(0);
155  end
156 
157  /* See if we have a "simple" singular mass matrix */
158  algdims = this.M.AlgebraicEquationDims;
159  if ~isempty(algdims)
160  invertible = 1:size(Mt0,1);
161  invertible(algdims) = [];
162  yp0(invertible) = Mt0(invertible,invertible)\yp0(invertible);
163  else
164  /* Compute initial slope (considering possibly
165  * rank-deficient M) */
166  if issparse(Mt0)
167  r = sprank(Mt0);
168  else
169  r = rank(Mt0);
170  end
171  if r < size(Mt0,1)
172  odeset(opts," MassSingular "," yes ");
173  warning(" off "," MATLAB:singularMatrix ");
174  end
175  yp0 = Mt0\yp0;
176  if r < size(Mt0,1)
177  warning(" on "," MATLAB:singularMatrix ");
178  yp0(isinf(yp0)) = 0;
179  if any(isnan(yp0(:)))
180  error(" Failed to automatically produce a consistent initial slope. ");
181  end
182  end
183  end
184  MS = " none ";
185  end
186  opts = odeset(opts," Mass ",M," MStateDependence ",MS,...
187  " InitialSlope ",yp0);
188 
189  if this.RealTimeMode
190  opts = odeset(opts," OutputFcn ",@this.ODEOutputFcn);
191  /* Bug in Matlab 2009a: direct assignment crashes Matlab!
192  * Seems also not to work if created within the constructor. */
193  ed = solvers.SolverEventData;
194  this.fED= ed;
195  this.solverCall(odefun, t, x0, opts);
196  t = []; y = [];
197  else
198  [t,y] = this.solverCall(odefun, t, x0, opts);
199  y = y^t;
200  t = t^t;
201  end
202  }
203 
204 
205 
206 #if 0 //mtoc++: 'set.MLSolver'
207 function MLSolver(value) {
208  if isa(value," function_handle ")
209  this.MLSolver= value;
210  this.Name= sprintf(" Matlab Solver wrapper using %s ",func2str(value));
211  else
212  error(" Invalid function handle! ");
213  end
214  }
215 
216 #endif
217 
218 
219  protected:
220 
221  function varargout = solverCall(function_handle odefun,rowvec<double> t,x0,struct opts) {
222  [varargout[1:nargout]] = this.MLSolver(odefun, t, x0, opts);
223  }
239  protected: /* ( Sealed ) */
240 
241  function status = ODEOutputFcn(t,y,flag) {
242  if ~strcmp(flag," init ")
243  /* For some reason the t and y args have more than one
244  * entry, so loop over all of them. */
245  for idx=1:length(t)
246  this.fED.Times= t(idx); /* when flag==init the t var is larger than one */
247 
248  this.fED.States= y(:,idx);
249  this.notify(" StepPerformed ",this.fED);
250  end
251  end
252  status = 0;
253  }
277 };
278 }
279 
280 
281 
AImplSolver: Base abstract class for solvers that can use Jacobian information for faster computation...
notify
Broadcast a notice that a specific event is occurring on a specified handle object or array of handle...
logical RealTimeMode
Determines if the solver's StepPerformed event should be used upon solving instead of returning the f...
Definition: BaseSolver.m:85
MLWrapper(solver)
Definition: MLWrapper.m:111
virtual function M = evaluate(double t,colvec< double > mu)
Allows to wrap a MatLab ODE solver into the KerMor framework.
Definition: MLWrapper.m:18
Name
The solver's name.
Definition: BaseSolver.m:116
sparsematrix JPattern
The sparsity pattern of the jacobian .
function_handle JacFun
A function handle to compute the core function's jacobian.
function registerProps(varargin)
Call this method at any class that defines DPCM observed properties.
Definition: DPCMObject.m:125
double InitialStep
The initial step size for the solver. So far only used by the MLWrapper class for the native matlab s...
Definition: BaseSolver.m:68
A MatLab function handle.
function status = ODEOutputFcn(t, y, flag)
Wraps the OutputFcn of the Matlab ODE solver into the StepPerformed event.
Definition: MLWrapper.m:241
function_handle MLSolver
The wrapped Matlab-Solver.
Definition: MLWrapper.m:60
Matlab's base handle class (documentation generation substitute)
solvers.SolverTypes SolverType
The type of the solver.
Definition: BaseSolver.m:130
double MaxStep
Maximum time step for solver.
Definition: BaseSolver.m:48
dscomponents.AMassMatrix M
The mass matrix of the ODE .
Definition: BaseSolver.m:101
function [ double t , matrix< double > y ] = solve(odefun,double t, x0)
Definition: MLWrapper.m:127
Base class for all KerMor ODE solvers.
Definition: BaseSolver.m:18
function varargout = solverCall(function_handle odefun,rowvec< double > t, x0,struct opts)
Default solver call for all builtin ode solvers except ode15i. This method gets overridden in MLode15...
Definition: MLWrapper.m:221
struct odeopts
Additional ODE options.
Definition: MLWrapper.m:80
A MatLab struct.
A MatLab character array.
A variable number of output arguments.