48 this =
this@dscomponents.ACompEvalCoreFun(sys);
54 this.data= rand(dim,1);
61 fx = bsxfun(@mtimes,this.data,this.
mu(1,:)) + repmat(t,this.
fDim,1);
73 fx = bsxfun(@mtimes,this.data(pts),
mu(1,:)) + repmat(t,length(pts),1);
82 f = testing.DEIM([],dim);
85 d.MaxOrder= round(dim/2);
89 atd = data.ApproxTrainData(x,[],[]);
93 d.computeDEIM(f,atd.fxi);
95 x =
double.empty(dim,0);
99 fxall = f.evaluateMulti(x,
t,
mu);
101 for k = 1:5:d.MaxOrder
103 afx = d.evaluateMulti(x,
t,
mu);
104 fprintf(
" Relative errors for Order k=%d: %s\n ",
k,...
105 sprintf(
" %g ",sum(
Norm.
L2(fxall-afx))));
115 mu = m.Data.ParamSamples(:,5);
122 res = testing.DEIM.computeDEIMErrors(d, m.Data.ApproxTrainData);
125 pm.FilePrefix=
" DEIM_errors ";
126 testing.DEIM.plotDEIMErrs(res, pm);
128 pm.savePlots(
" . ",[
" fig ",
" jpg "],
true);
130 [etrue, EE, ED] = ma.getTrajApproxErrorDEIMEstimates(
mu,[]);
133 pm.FilePrefix=
" traj_approx_err ";
134 ma.getTrajApproxErrorDEIMEstimates_plots(m.scaledTimes, etrue, EE, ED, pm);
136 pm.savePlots(
" . ",[
" fig ",
" jpg "],
true);
138 [minreq, relerrs, orders,
t] = ma.getMeanRequiredErrorOrders;
140 t.saveToFile(
" meanrequirederrororders.tex ");
154 for i=1:length(orders)
156 errorders = [errorders
new];
158 neworders = [neworders orders(i)*ones(1,length(
new))];
162 elseif ~isempty(errorders) && length(orders) ~= length(errorders)
163 error(
" If both the orders and error orders are given they must have same number of elements ");
168 res(2,:) = errorders;
173 pi =
ProcessIndicator(sprintf(
" Computing DEIM errors and estimates for %d Order/ErrOrder settings over %d xi-Blocks ",n,nb),n*nb);
179 fxi = atd.
fxi(:,pos);
188 deim.
Order= [res(1,i) res(2,i)];
190 if isempty(co) || res(1,i) ~= co
193 etrue = efun(fxi-afxi);
195 emaxorder = efun(afximaxorder-afxi);
197 res(3,i) = max(res(3,i),max(etrue));
199 hlp = etrue./fxinorm;
201 res(4,i) = max(res(4,i),max(hlp));
203 res(5,i) = res(5,i) + sum(etrue)/m;
205 res(6,i) = res(6,i) + sum(hlp)/m;
209 res(3:6,i) = res(3:6,i-1);
215 res(7,i) = max(res(7,i),max(eest));
219 res(8,i) = max(res(8,i),max(hlp));
221 res(9,i) = res(9,i) + sum(eest)/m;
223 res(10,i) = res(10,i) + sum(hlp)/m;
225 hlp = abs(eest-etrue)./etrue;
227 res(11,i) = max(res(11,i),max(hlp));
229 res(12,i) = res(12,i) + sum(hlp)/m;
231 hlp = abs(eest-emaxorder)./emaxorder;
233 res(13,i) = max(res(13,i),max(hlp));
235 res(14,i) = res(14,i) + sum(hlp)/m;
287 pm.FilePrefix=
" comp_m_mdash ";
296 [orders, idx] = unique(res(1,:));
298 tri = delaunay(res(1,:),res(2,:));
301 h = pm.nextPlot(
" true_abs ",
" Linf-L2 absolute error ",
" m ",
" m ");
302 semilogy(h,orders,res(3,idx));
305 h = pm.nextPlot(
" est_abs ",
" Estimated absolute error ",
" m ",
" m ");
306 doplot(h,tri,res(1,:),res(2,:),res(7,:),
" EdgeColor ",
" none ");
309 h = pm.nextPlot(
" true_rel ",
" Linf-L2 relative error ",
" m ",
" m ");
310 semilogy(h,orders,res(4,idx));
313 h = pm.nextPlot(
" est_rel ",
" Estimated relative error ",
" m ",
" m ");
314 doplot(h,tri,res(1,:),res(2,:),res(8,:),
" EdgeColor ",
" none ");
320 sel = [1 2 3 4 5 7 9 11 13 15:3:30 31:20:n];
325 tri = delaunay(res(1,:),res(2,:));
329 h = pm.nextPlot(
" max_relerr_est_true_totrue ",
" 'max (true - estimated)/true' relative error ",
" m ",
" m ");
330 doplot(h,tri,res(1,:),res(2,:),res(11,:));
332 sh = doplot(h,tri,res(1,:),res(2,:),1e-2*ones(1,n),...
333 " EdgeColor ",
" none ",
" FaceColor ",
" k ");
337 h = pm.nextPlot(
" mean_relerr_est_true_totrue ",
" 'mean (true - estimated)/true' relative error ",
" m ",
" m ");
338 doplot(h,tri,res(1,:),res(2,:),res(12,:));
340 sh = doplot(h,tri,res(1,:),res(2,:),1e-2*ones(1,n),...
341 " EdgeColor ",
" none ",
" FaceColor ",
" k ");
349 h = pm.nextPlot(
" max_relerr_est_emaxo_toemaxo ",
" 'max (maxordererror - estimated)/maxordererror' relative error ",
" m ",
" m ");
350 doplot(h,tri,res(1,:),res(2,:),res(13,:));
352 sh = doplot(h,tri,res(1,:),res(2,:),1e-2*ones(1,n),...
353 " EdgeColor ",
" none ",
" FaceColor ",
" k ");
357 h = pm.nextPlot(
" mean_relerr_est_emaxo_toemaxo ",
" 'mean (maxordererror - estimated)/maxordererror' relative error ",
" m ",
" m ");
358 doplot(h,tri,res(1,:),res(2,:),res(14,:));
360 sh = doplot(h,tri,res(1,:),res(2,:),1e-2*ones(1,n),...
361 " EdgeColor ",
" none ",
" FaceColor ",
" k ");
367 h = pm.nextPlot(
" abs_diff_est_true ",
" Absolute difference of estimated to true error ",
" m ",
" m ");
368 doplot(h,tri,res(1,:),res(2,:),abs(res(3,:)-res(7,:)));
373 function p = doplot(h, tri, x, y, z,
varargin)
385 atd = m.Data.JacobianTrainData;
386 n = min(size(atd.xi,2),500);
387 sp =
logical(m.System.f.JSparsityPattern);
389 o = round(linspace(1,m.Approx.MaxOrder,M));
393 r = m.buildReducedModel;
395 m.Approx.Order= o(
k);
396 r.System.f.Order= o(
k);
401 aj = m.Approx.getStateJacobian(x,atd.ti(i),atd.mui(:,i));
402 j = m.System.f.getStateJacobian(x,atd.ti(i),atd.mui(:,i));
403 rj = r.System.f.getStateJacobian(z,atd.ti(i),atd.mui(:,i));
404 hlp = abs((aj-j)./j);
406 t(
k,i) = max(hlp(:));
407 nof(
k,i) = norm(full(j));
414 h = pm.nextPlot(
" max_rel_err ",sprintf(
" Maximum relative errors of DEIM jacobian vs. original one\nmasked to sparsity pattern "));
416 lbl = arrayfun(@(e)sprintf(
" %d ",e),o,
" Uniform ",
false);
419 h = pm.nextPlot(
" jfull_norm ",
" Full Jacobian norms ",
" snapshot ",
" L2 matrix norm ");
423 h = pm.nextPlot(
" jred_norm ",
" Reduced Jacobian norms ",
" snapshot ",
" L2 matrix norm ");
448 fm.Approx.Order= r.System.f.Order;
449 fx = fm.System.f.evaluate(V*xr,t,mu);
450 efull =
Norm.
L2(fx-r.FullModel.Approx.evaluate(V*xr,t,mu));
451 ered =
Norm.
L2(fx-V*r.System.f.evaluate(xr,t,mu));
459 inputidx = fm.TrainingInputs;
461 mu = fm.Data.ParamSamples;
470 num_mu = max(1,size(mu,2));
471 num_in = max(1,length(inputidx));
472 etrue = zeros(num_mu*num_in,length(fm.Times));
473 EE = zeros(mo-o,length(fm.Times),num_mu*num_in);
489 if ~isempty(inputidx)
490 curin = inputidx(inidx);
497 [curetrue, ~,
t, x] = ma.getTrajApproxError(curmu, curin);
498 eest =
Norm.
L2(f.getEstimatedError(x,
t, repmat(curmu,1,length(
t))));
500 ED(eo,:,cnt) = abs(eest - curetrue);
501 etrue(cnt,:) = curetrue;
510 pm = testing.DEIM.getTrajApproxErrorDEIMEstimates_plots(r, etrue(1,:), EE(:,:,1), ED(:,:,1));
544 f = r.FullModel.Approx;
551 h = pm.nextPlot(
" true_err ",
" True approximation error on trajectory ",
" time ",
" error ");
553 h = pm.nextPlot(
" est_err ",sprintf(
" Estimated approximation errors on trajectory\nDEIM order = %d / max %d ",f.Order(1),f.MaxOrder),...
554 " time ",
" DEIM error order ");
555 [T,O] = meshgrid(t,1:mo-o);
559 h = pm.nextPlot(
" abs_diff ",sprintf(
" Difference true to estimated error\nDEIM order = %d / max %d ",f.Order(1),f.MaxOrder),...
560 " time ",
" DEIM error order ");
564 h = pm.nextPlot(
" rel_diff ",sprintf(
" Relative error between true to estimated error\nDEIM order = %d / max %d ",f.Order(1),f.MaxOrder),...
565 " time ",
" DEIM error order ");
566 LogPlot.
logsurf(h,T,O,ED./repmat(etrue,mo-o,1),
" EdgeColor ",
" none ",
" FaceColor ",
" interp ");
568 p = surf(h,T,O,-ones(size(T))*2,
" EdgeColor ",
" red ",
" FaceColor ",
" k ");
589 txt = sprintf(
" DEIM MaxOrder %d error ",maxorder);
593 txt =
" true DEIM error ";
597 t =
PrintTable(
" Required M "" over %d training samples for min/mean errors relative to %s ",...
600 title = arrayfun(@(e)sprintf(
" %g ",e),relerrs,
" Unif ",
false);
601 t.addRow(
" m / rel. error ",title[:]);
602 nr = length(relerrs);
603 orders = unique(errordata(1,:));
604 values = zeros(length(orders),1+2*nr);
605 for i = 1:length(orders)
608 pos = errordata(1,:) == o;
609 maxv = errordata(maxvidx,pos);
610 meanv = errordata(meanvidx,pos);
611 hlp =
cell.empty(0,nr);
614 maxidx = find(maxv <= relerrs(j),1,
" first ");
619 meanidx = find(meanv <= relerrs(j),1,
" first ");
621 meanidx = min(meanv);
623 hlp[j] = sprintf(
" %g/%g ",maxidx,meanidx);
624 values(i,1+[j j+nr]) = [maxidx meanidx];
652 s = sampling.RandomSampler;
654 s.Samples= numExtraSamples;
655 mui = [mui s.generateSamples(m)];
656 xi = repmat(x,1,size(mui,2));
657 ti = zeros(1,size(mui,2));
682 tri = delaunay(mui(1,:),mui(2,:));
685 h = pm.nextPlot(
" abserr ",
" Absolute errors over mu range ");
688 n = m.Data.SampleCount;
690 plot3(mui(1,1:n),mui(2,1:n),log10(err(1:n)),
" rx ",
" MarkerSize ",16);
695 h = pm.nextPlot(
" relerr ",
" Relative errors over mu range ");
698 n = m.Data.SampleCount;
700 plot3(mui(1,1:n),mui(2,1:n),log10(err(1:n)),
" rx ",
" MarkerSize ",16);
709 deim_orders = 1:(d.MaxOrder-r.
System.
f.Order(2));
712 no = length(deim_orders);
713 errs = zeros(no,length(r.
Times));
715 times = zeros(no+1,1);
718 pi =
ProcessIndicator(
" Computing DEIM-reduced model simulation errors for %d orders ",no,
false,no);
723 relerrs(m,:) = errs(m,:)./
Norm.
L2(y);
753 [
X,
Y] = meshgrid(r.Times, deim_orders);
754 h = pm.nextPlot([ftag
" abserr "],sprintf([
" L2-absolute reduction errors\n "...
755 " (Linf in time for original view), tag: " tag]),...
756 " time ",
" DEIM order ");
760 h = pm.nextPlot([ftag
" relerr "],[
" L2-relative reduction errors, tag: " tag],...
761 " time ",
" DEIM order ");
783 orders = 1:deim.MaxOrder;
794 deim.setSimilarityTransform([]);
795 pi =
ProcessIndicator(
" Computing matrix DEIM approximation error and log norm errors for %d orders ",mo,
false,mo);
800 ti = atd.ti(i); mui = atd.mui(:,i);
802 DJ =
reshape(deim.evaluate(m.
Data.V^
t*atd.xi(:,i),ti,mui),...
806 e(o,i,1) = max(max(abs(diff)));
808 e(o,i,3) = mean(abs(diff));
809 e(o,i,4) = var(diff);
818 deim.setSimilarityTransform(Qk);
837 if nargin < 5 || isempty(pm)
842 [
X,
Y] = meshgrid(td,orders);
844 h = pm.nextPlot(
" max_diff ",
" Maximum absolute difference ",...
845 " trajectory point ",
" DEIM order ");
847 h = pm.nextPlot(
" max_diff_mean ",
" Mean maximum absolute difference ",
" DEIM order ");
848 semilogy(h,orders,mean(e(:,:,1),2));
850 h = pm.nextPlot(
" l2_diff ",
" L2 vector difference ",...
851 " trajectory point ",
" DEIM order ");
853 h = pm.nextPlot(
" l2_diff_mean ",
" Mean L2 vector difference ",
" DEIM order ");
854 semilogy(h,orders,mean(e(:,:,2),2));
856 h = pm.nextPlot(
" mean_diff ",
" Mean difference value ",...
857 " trajectory point ",
" DEIM order ");
859 h = pm.nextPlot(
" mean_diff_mean ",
" Mean mean difference value ",
" DEIM order ");
860 semilogy(h,orders,mean(e(:,:,3),2));
862 h = pm.nextPlot(
" var_diff ",
" Variance of difference ",...
863 " trajectory point ",
" DEIM order ");
865 h = pm.nextPlot(
" var_diff_mean ",
" Mean variance of difference ",
" DEIM order ");
866 semilogy(h,orders,mean(e(:,:,4),2));
868 ln = m.Data.JacSimTransData.LogNorms;
869 if length(ln) ~= size(aln,2)
872 ln = repmat(ln,length(orders),1);
873 err =
sort(abs(ln-aln),2);
874 h = pm.nextPlot(
" log_norm_diff ",
" Differences of logarithmic norms ",...
875 " trajectory point ",
" DEIM order ");
878 err =
sort(abs((ln-aln) ./ ln),2);
879 h = pm.nextPlot(
" log_norm_diff ",
" Differences of logarithmic norms ",...
880 " trajectory point ",
" DEIM order ");
891 pm.SingleSize= [720 540];
896 h = pm.nextPlot(
" errors ",
" True and estimated error ",
" time ",
" error ");
898 h = pm.nextPlot(
" errors ",
" True and estimated error ",
" time ",
" error ");
925 for i = 1:length(est)
926 li.excludeColor(est(i).Color);
931 for j = 1:length(jdorders)
932 cl = li.nextLineStyle;
934 for k = 1:length(stsizes)
935 str = sprintf(
" e.JacMDEIM.Order = %d; e.JacSimTransSize = %d; ",...
936 jdorders(j),stsizes(
k));
937 est(end+1).Name = sprintf(
" $m_J:%d, k:%d$ ",...
938 jdorders(j),stsizes(
k));
940 est(end).Estimator = e;
941 est(end).Callback = @(e)eval(str);
942 est(end).MarkerStyle = li2.nextMarkerStyle;
943 est(end).LineStyle = cl;
944 est(end).Color = li.nextColor;
967 errororders = [1 2 5];
970 for i = 1:length(est)
971 m.excludeColor(est(i).Color);
976 for j = 1:length(errororders)
977 str = sprintf(
" e.ReducedModel.System.f.Order = [e.ReducedModel.System.f.Order(1) %d]; ",errororders(j));
978 est(end+1).Name = sprintf(
" $m "" =%d$ ",errororders(j));
980 est(end).Estimator = e;
981 est(end).Callback = @(e)eval(str);
982 est(end).MarkerStyle = m.nextMarkerStyle;
983 est(end).LineStyle =
" -. ";
984 est(end).Color = m.nextColor;
ModelAnalyzer: Analysis tools for reduced models and approximations.
Collection of generally useful functions.
static function res = computeDEIMErrors(general.DEIM deim,data.ApproxTrainData atd,rowvec< integer > orders,rowvec< integer > errorders)
% DEIM approximation analysis over training data Computes the DEIM approximation error over the given...
function copy = clone(copy)
Creates a copy of this error estimator. Call only allowed from subclasses.
static function [ matrix< double > mui , fxi , afxi ] = getDEIMErrorsAtXForParams(models.BaseFullModel m,colvec< double > x,integer numExtraSamples)
% DEIM approximation analysis over parameters for specific state space location
integer fDim
The current output dimension of the function.
DEIM: Implements the DEIM-Algorithm from .
data.FileMatrix xi
The state space samples , stored row-wise in a data.FileMatrix instance.
LogPlot: Class with static functions for logarithmic plotting.
function err = getEstimatedError(colvec< double > x,double t,colvec< double > mu)
data.FileMatrix fxi
The evaluations of , stored row-wise in a data.FileMatrix.
function pos = getBlockPos(nr)
Returns the column indices of the block "nr" within the full matrix.
error.BaseEstimator ErrorEstimator
The associated error estimator for this model.
static function est = getDEIMEstimators_ErrOrders(models.ReducedModel rmodel, est, errororders)
The base class for any KerMor detailed model.
models.BaseFullModel FullModel
The full model this reduced model was created from.
function J = getStateJacobian(x, t)
Default implementation of jacobian matrix evaluation via finite differences.
models.BaseFirstOrderSystem System
The actual dynamical system used in the model.
Times
Evaluation points of the model.
A MatLab cell array or matrix.
integer MaxOrder
The maximum order up to which the DEIM approximation should be computed.
OutputError
The output error from the last simulation.
static function pm = effectivityAnalysis(models.ReducedModel r,colvec< double > mu,integer inputidx)
% Effectivity analysis of error estimators Plots an effectivity graph for the current error estimator...
function fx = evaluate(colvec< double > x,double t)
sort
ort the handle objects in any array in ascending or descending order.
reshape
hanges the dimensions of the handle object array to the specified dimensions. See the MATLAB reshape ...
static function handle p = logtrisurf(handle h, tri,rowvec< double > x,rowvec< double > y,rowvec< double > z, varargin)
Creates a surface plot from a 2D triangulation in a logarithmic scale.
matrix ParamSamples
A Model's parameter samples as column vector collection.
The KerMor reduced model class.
static function pm = getTrajApproxErrorDEIMEstimates_plots(r, etrue, EE, ED, pm)
Visualizes the results of the ModelAnalyzer.getTrajApproxErrorDEIMEstimates method.
static function [ double t , nof , nor , o , pm ] = jacobian_tests(m, pm)
Tis function computes the jacobians of both the full and DEIM approximated system functions...
rowvec< integer > Order
The actual order for the current DEIM approximation.
PlotManager: Small class that allows the same plots generated by some script to be either organized a...
static function handle p = logsurf(handle h,matrix< double > X,matrix< double > Y,matrix< double > Z, varargin)
Creates a nice surface plot in a logarithmic scale with the given data.
rowvec ti
The time samples .
function fx = evaluateComponents(pts, ends, idx, self,colvec< double > x,double t)
logical LeaveOpen
Flag indicating if the plots should be left open once the PlotManager is deleted (as variable) ...
A variable number of input arguments.
integer xDim
The current state space dimension of the function's argument .
error.BaseEstimator ErrorEstimator
The error estimator for the reduced model.
static function [ etrue , EE , ED , pm ] = getTrajApproxErrorDEIMEstimates(models.ReducedModel r,colvec< double > mu,integer inputidx)
[etrue, EE, ED, pm] = getTrajApproxErrorDEIMEstimates(this, mu, inputidx) Computes the DEIM approxima...
V
The matrix of the biorthogonal pair .
colvec< double > mu
The current model parameter mu for evaluations. Will not be persisted as only valid for runtime durin...
static function [ e , aln , rowvec< integer > orders ] = compareDEIM_Full_Jacobian(models.BaseFullModel m,data.ApproxTrainData atd,rowvec< integer > orders)
% Matrix DEIM approximation analysis Compares the MDEIM approximation with the full jacobian ...
approx.BaseApprox Approx
The approximation method for the CoreFunction.
data.ModelData Data
The full model's data container. Defaults to an empty container.
function fx = evaluate(x, t)
Evaluates the f-approximation. Depending on a possible projection and the CustomProjection-property t...
static function pm = plotDEIMErrs(res, pm)
integer m
The total number of columns.
PrintTable: Class that allows table-like output spaced by tabs for multiple rows. ...
static function [ errs , relerrs , times , rowvec< integer > deim_orders ] = getDEIMReducedModelErrors(models.ReducedModel r,colvec< double > mu,integer inidx,rowvec< integer > deim_orders)
% Model DEIM reduction quality assessment pics
sparse< logical > JSparsityPattern
Sparsity pattern for the jacobian matrix.
matrix mui
The parameter samples used computing the parent trajectories of .
static function [ double t , values ] = getMinReqErrorOrdersTable(errordata, relerrs, tsize, maxorder)
Return values: t: A PrintTable containing the results. If not specified as a nargout argument...
static function analysis_DEIM_approx(m)
dscomponents.ACoreFun f
The core f function from the dynamical system.
ACompEvalCoreFun: A normal CoreFun which supports single-component evaluation.
static function struct est = getDEIMEstimators_MDEIM_ST(models.ReducedModel rmodel,struct est,rowvec< integer > jdorders,rowvec< integer > stsizes)
% Error estimator struct compilation Computes a set of DEIMEstimators for given MDEIM orders and simt...
integer nBlocks
The number of blocks into which this matrix is separated.
static function pm = compareDEIM_Full_Jacobian_plots(m, e, aln, orders, pm, atdsubset)
data.ApproxTrainData JacobianTrainData
Training data for the jacobian approximation.
static function rowvec< double > n = L2(matrix< double > x)
Returns the discrete norm for each column vector in x.
function fx = evaluateCoreFun(unused1,double t)
static function [ double ln , colvec< double > v ] = logNorm(matrix< double > A,matrix< double > G,colvec< double > v0)
Computes the logarithmic norm of a matrix , optionally using a positive definite symmetric matrix in...
function fx = evaluateComponentsMulti(pts, ends, idx, self,colvec< double > x,double t,colvec< double > mu)
ApproxTrainData: Data class for approximation training data, containing several useful bounding box p...
DEIM: Tests regarding the DEIM method.
Norm: Static class for commonly used norms on sets of vectors.
LinSpecIterator: Small helper class to automatically iterate through different line styles/markers/co...
static function res = test_DEIMNoSpatialDependence()
static function pm = getDEIMReducedModelErrors_plots(r, errs, relerrs, times, deim_orders, pm, tag)
static function [ efull , ered , fxno ] = getApproxErrorFullRed(r, xr,double t,colvec< double > mu, V)
function [ rowvec< double > t , matrix< double > y , double sec , x ] = simulate(colvec< double > mu,integer inputidx)
Simulates the system and produces the system's output.
ProcessIndicator: A simple class that indicates process either via waitbar or text output...
static function pm = getDEIMErrorsAtXForParams_plots(m,matrix< double > mui, fxi, afxi, pm)