54 if ~isa(rmodel,
" models.ReducedModel ")
55 error(
" rmodel must be a models.ReducedModel subclass. ");
65 seed = round(cputime*100);
71 params = this.rm.FullModel.getRandomParam(num, seed);
94 fm = this.rm.FullModel;
98 params = fm.Data.ParamSamples;
101 num = size(params,2);
103 details.params= params;
104 details.L2Errs= zeros(num,length(fm.Times));
105 details.L2Fullsol= details.L2Errs;
106 details.L2OutErrs= details.L2Errs;
107 details.L2OutFullsol= details.L2Errs;
108 details.Linferrs= details.L2Errs;
109 details.Linftruesolnorm= details.L2Errs;
110 details.Times= zeros(num,2);
112 haveerrest = ~isempty(this.rm.ErrorEstimator) && this.rm.ErrorEstimator.Enabled;
114 details.Estimates= details.L2Errs;
115 details.OutEstimates= details.L2Errs;
120 pi =
ProcessIndicator(
" Computing reduction error details for %d param samples ",...
124 [~, y, tf, x] = fm.simulate(mu, in);
125 [~, yr, tr, xr] = this.rm.simulate(mu, in);
126 details.Times(pidx,:) = [tf tr];
127 details.L2Errs(pidx,:) =
Norm.
L2(x-this.rm.V*xr);
128 details.L2Fullsol(pidx,:) =
Norm.
L2(x);
129 details.L2OutErrs(pidx,:) =
Norm.
L2(y-yr);
130 details.L2OutFullsol(pidx,:) =
Norm.
L2(y);
131 details.Linferrs(pidx,:) =
Norm.
Linf(x-this.rm.V*xr);
132 details.Linftruesolnorm(pidx,:) =
Norm.
Linf(x);
133 errs(1,pidx) = max(details.L2Errs(pidx,:));
135 errs(2,pidx) = errs(1,pidx) ./ max(details.L2Fullsol(pidx,:));
137 errs(3,pidx) = max(details.L2OutErrs(pidx,:));
139 errs(4,pidx) = errs(3,pidx) ./ max(details.L2OutFullsol(pidx,:));
141 errs(5,pidx) = max(details.Linferrs(pidx,:));
143 errs(6,pidx) = errs(5,pidx) ./ max(details.Linftruesolnorm(pidx,:));
148 details.Estimates(pidx,:) = this.rm.ErrorEstimator.StateError;
149 errs(5,pidx) = details.Estimates(pidx,end)/details.L2Errs(pidx,end);
151 details.OutEstimates(pidx,:) = this.rm.ErrorEstimator.OutputError;
152 errs(6,pidx) = details.OutEstimates(pidx,end)/details.L2OutErrs(pidx,end);
192 fm = this.rm.FullModel;
193 [~, y, ftime] = fm.simulate(mu,inputidx);
194 [ti,yr, rtime] = this.rm.simulate(mu,inputidx);
196 str = sprintf(
" %s ",fm.Name);
198 str = sprintf(
" %s, mu=[%s] ",str,
Utils.
implode(mu,
" , ",
" %2.3f "));
200 if ~isempty(inputidx)
201 str = sprintf(
" %s, u_%d ",str,inputidx);
204 t.HasRowHeader=
true;
205 t.addRow(
" Full model ",ftime,[
" %2.4fs "]);
206 t.addRow(
" Reduced model ",rtime,[
" %2.4fs "]);
207 t.addRow(
" Speedup ",ftime/rtime,[
" x%2.4f "]);
215 l2relyl2 = l2 ./
Norm.
L2(y);
216 l2l2relyl2 =
Norm.
L2(l2relyl2^t);
217 lil2relyl2 = max(l2relyl2);
218 meanrell2 = mean(l2relyl2);
220 t =
PrintTable(
" Error comparison for %s: ",str);
221 t.addRow(
" L2 time and space error ",
" L^2(||y(t) - yr(t)||_2,[0,T]) ",l2l2);
222 t.addRow(
" Linf time and L2 space error ",
" L^inf(||y(t) - yr(t)||_2,[0,T]) ",lil2);
223 t.addRow(
" Relative L2 time and space error ",
" L^2(||(y(t) - yr(t)) / y(t)||_2,[0,T]) ",l2l2relyl2);
224 t.addRow(
" Relative Linf time and L2 space error ",
" L^inf(||(y(t) - yr(t)) / y(t)||_2,[0,T]) ",lil2relyl2);
225 t.addRow(
" Mean L2 error ",
" Mean(||y(t) - yr(t)||_2,[0,T]) ",meanl2);
226 t.addRow(
" Mean relative L2 error ",
" Mean(||(y(t) - yr(t)) / y(t)||_2,[0,T]) ",meanrell2);
234 l2lirelyli =
Norm.
L2(lirelyli^t);
235 lilirelyli = max(lirelyli);
236 meanrelli = mean(lirelyli);
237 t.addRow(
" Linf time and space error ",
" L^inf(||y(t) - yr(t)||_inf,[0,T]) ",lili);
238 t.addRow(
" L2 time and Linf space error ",
" L^2(||y(t) - yr(t)||_inf,[0,T]) ",l2li);
239 t.addRow(
" Relative Linf time and space error ",
" L^inf(||(y(t) - yr(t)) / y(t)||_inf,[0,T]) ",lilirelyli);
240 t.addRow(
" Relative L2 time and Linf space error ",
" L^2(||(y(t) - yr(t)) / y(t)||_inf,[0,T]) ",l2lirelyli);
241 t.addRow(
" Mean Linf error ",
" Mean(||y(t) - yr(t)||_inf,[0,T]) ",meanli);
242 t.addRow(
" Mean relative Linf error ",
" Mean(||(y(t) - yr(t)) / y(t)||_inf,[0,T]) ",meanrelli);
249 fm.plot(ti, y, pm.nextPlot(
" full_sim ",sprintf(
" Full simulation\n%s ",str)));
250 fm.plot(ti, yr, pm.nextPlot(
" red_sim ",sprintf(
" Reduced simulation\n%s ",str)));
251 fm.plot(ti, log10(abs(y-yr)), pm.nextPlot(
" abs_err ",sprintf(
" Absolute error (log scale)\n%s ",str)));
253 fm.plot(ti, log10(abs(y-yr)./hlp), pm.nextPlot(
" rel_err ",sprintf(
" Relative error (log scale)\n%s ",str)));
277 fm = this.rm.FullModel;
278 if ~isempty(fm.Approx)
284 [
t,x] = fm.computeTrajectory(mu, inputidx);
285 if ~isempty(this.rm.V)
286 x = this.rm.V*(this.rm.W^t*x);
288 mu = repmat(mu,1,numel(t));
289 fx = fm.Approx.evaluateMulti(x,t,mu);
290 afx = fm.System.f.evaluateMulti(x,t,mu);
294 error(
" The approximation error can only be computed for models with an approx.BaseApprox instance present. ");
313 if ~isempty(fm.Approx)
318 atd = fm.Data.ApproxTrainData;
319 afx = fm.Approx.evaluateMulti(atd.xi.toMemoryMatrix,atd.ti,atd.mui);
320 s = 1:size(atd.xi,2);
321 fx = atd.fxi.toMemoryMatrix;
325 h = pm.nextPlot(
" abs_l2 ",
" Absolute L2 error ",
" x_i ",
" L2 ");
327 h = pm.nextPlot(
" rel_l2 ",
" Relative L2 error ",
" x_i ",
" L2 ");
333 apfx = this.rm.System.f.evaluateMulti(rm.W^
t*atd.xi,atd.ti,atd.mui);
335 pel2 =
Norm.
L2(afx-rm.V*apfx);
336 h = pm.nextPlot(
" abs_proj_l2 ",...
337 sprintf(
" Absolute L2 error projected vs full approx, %d/%d dims ",rd(2),rd(1)),...
340 h = pm.nextPlot(
" rel_proj_l2 ",...
341 sprintf(
" Relative L2 error projected vs full approx, %d/%d dims ",rd(2),rd(1)),...
351 error(
" The approximation error can only be computed for models with an approx.BaseApprox instance present. ");
369 if isempty(this.rm.ErrorEstimator)
370 error(
" Error analysis only available for models with error estimator ");
376 inputidx = rmodel.DefaultInput;
378 mu = rmodel.DefaultMu;
385 [~, y, time, x] = rmodel.FullModel.simulate(mu, inputidx);
386 rmodel.ErrorEstimator.Enabled=
false;
387 tic; rmodel.simulate(mu, inputidx); timer_noerr = toc;
388 rmodel.ErrorEstimator.Enabled=
true;
389 [
t, yr, timer, xr] = rmodel.simulate(mu, inputidx);
394 est = rmodel.ErrorEstimator.OutputError;
396 if ~isempty(rmodel.V)
399 est = rmodel.ErrorEstimator.StateError;
407 estrelr = est./xrnorm;
410 xrmin = xr-repmat(est,size(xr,1),1); xrplus = xr+repmat(est,size(xr,1),1);
411 ymax = max([max(x(:)) max(xr(:)) max(xrmin(:)) max(xrplus(:))]);
412 ymin = min([min(x(:)) min(xr(:)) min(xrmin(:)) min(xrplus(:))]);
415 ti = sprintf(
" The full system "" s output (m=%d,time=%.3f) ",size(x,1),time);
417 ti = sprintf(
" The full system (d=%d,time=%.3f) ",size(x,1),time);
419 h = pm.nextPlot(
" fullsys ",ti,
" time ",
" error ");
421 axis([
t(1)
t(end) ymin ymax]);
424 h = pm.nextPlot(
" outputs ",...
425 sprintf(
" Full+reduced system outputs with error bounds\n(r=%d,self time:%.3f, time with err est:%.3f) ",...
426 size(rmodel.V,2),timer_noerr,timer),...
427 " time ",
" y(t) / y^r(t) ");
432 legend(
" Full system ",
" Reduced system ",
" Lower bound ",
" Upper bound ");
434 h = pm.nextPlot(
" redsys ",sprintf(
" Reduced system (r=%d,self time:%.3f,\ntime with err est:%.3f) ",size(rmodel.V,2),timer_noerr,timer),...
435 " time ",
" x^r(t) ");
438 axis([
t(1)
t(end) ymin ymax]);
441 h = pm.nextPlot(
" norms ",
" The state variable norms ",
" time ",
" norms ");
442 plot(h,
t, xnorm,
" b ",
t,xrnorm,
" r ",
t,xrnorm-est,
" r-- ",
t,xrnorm+est,
" r-- ");
443 le = legend(
" Full system ",
" Reduced system ",
" Lower bound ",
" Upper bound ");
444 set(le,
" Location ",
" NorthWest ");
447 h = pm.nextPlot(
" abserrors ",sprintf(
" The state variable absolute errors.\nmean(e)=%g, mean(est)=%g ",mean(e),mean(est)),...
448 " time ",
" errors ");
449 semilogy(h,
t,est,
" b ",
t,e,
" r ");
451 legend(
" Estimated error ",
" True error ");
455 h = pm.nextPlot(
" relerrors ",sprintf([
" The state variable relative errors (comp. to "...
456 " full solution)\nmean(e_{rel})=%g, mean(est_{rel})=%g "],...
457 mean(erel),mean(estrel)),...
458 " time ",
" errors ");
459 semilogy(h,
t,estrel,
" b ",
t,erel,
" r ");
460 legend(
" Estimated error ",
" True error ");
463 h = pm.nextPlot(
" relerrors_red ",sprintf([
" The state variable relative errors (comp. to "...
464 " reduced solution)\nmean(ered_{rel})=%g, mean(estred_{rel})=%g "],...
465 mean(erelr),mean(estrelr)),...
466 " time ",
" errors ");
467 semilogy(h,
t,erelr,
" r ",
t,estrelr,
" b ");
468 legend(
" True error ",
" Estimated error ");
481 [~,m,~,
l] =
FindInstance(this.rm.FullModel,
" IReductionSummaryPlotProvider ");
488 extra = sprintf(
" -%d ",i);
490 context = sprintf(
" %s%s (%s) ",
l[
k],extra,
class(objs(i)));
491 objs(i).plotSummary(pm,context);
ModelAnalyzer: Analysis tools for reduced models and approximations.
Collection of generally useful functions.
function pm = analyzeError(colvec< double > mu,integer inputidx, pm)
LogPlot: Class with static functions for logarithmic plotting.
function [ el2 , elinf , pm ] = getATDError(pm)
Computes the approximation training error on the full system's trajectory for given mu and inputidx...
function [ PrintTable t , cell matches , parents , cell locations ] = FindInstance(handle obj,char type, varargin)
FindInstance: Locate instances of certain classes within a class or a struct.
The KerMor reduced model class.
PlotManager: Small class that allows the same plots generated by some script to be either organized a...
Matlab's base handle class (documentation generation substitute)
static function handle p = cleverPlot(handle ax,rowvec< double > x,rowvec< double > y, varargin)
Calls corresponding plot routines depending on the scale of data.
function [ t , pm ] = compareRedFull(mu, inputidx)
Compares the solutions of the reduced model and the associated full model by calling the BaseModel...
static function rowvec< double > n = Linf(matrix< double > x)
Returns the discrete norm for each column vector in x.
ModelAnalyzer(models.ReducedModel rmodel)
function [ el2 , elinf , double t , colvec< double > x , fx , afx ] = getTrajApproxError(colvec< double > mu,integer inputidx)
Computes the approximation training error on the full system's trajectory for given mu and inputidx...
PrintTable: Class that allows table-like output spaced by tabs for multiple rows. ...
static function char str = implode(char|rowvec data,char glue,char format)
Implodes the elements of data using glue.
static function rowvec< double > n = L2(matrix< double > x)
Returns the discrete norm for each column vector in x.
function [ errs , details ] = getRedErrForParamSamples(integer params,integer in)
Computes the simulation errors (output) for the given model parameters.
Norm: Static class for commonly used norms on sets of vectors.
function pm = plotReductionOverview(pm)
function [ matrix< double > params , errs , details ] = getRedErrForRandomParamSamples(integer num,integer seed, in)
Computes the simulation errors (output) for num random model parameters.
ProcessIndicator: A simple class that indicates process either via waitbar or text output...
static function y = preparePlainPlot(y)
Memory-saving plotting for plain result plots.
function plotRedErrForParamSamples(errs, pm)
\todo implement, so far only plotting in DEIM testing with multiple DEIM m Orders.