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
PCDISystem2D.m
Go to the documentation of this file.
1 namespace models{
2 namespace pcdi{
3 
4 
5 /* (Autoinserted by mtoc++)
6  * This source code has been filtered by the mtoc++ executable,
7  * which generates code that can be processed by the doxygen documentation tool.
8  *
9  * On the other hand, it can neither be interpreted by MATLAB, nor can it be compiled with a C++ compiler.
10  * Except for the comments, the function bodies of your M-file functions are untouched.
11  * Consequently, the FILTER_SOURCE_FILES doxygen switch (default in our Doxyfile.template) will produce
12  * attached source files that are highly readable by humans.
13  *
14  * Additionally, links in the doxygen generated documentation to the source code of functions and class members refer to
15  * the correct locations in the source code browser.
16  * However, the line numbers most likely do not correspond to the line numbers in the original MATLAB source files.
17  */
18 
40  public:
41 
54  Gammas = "[.01 .02 .03 .04 .05]";
65 
67 
68 
69  public:
70 
72  this = this@models.pcdi.BasePCDISystem(model);
73 
74  /* Set core function */
75  if model.WithInhibitors
76  this.f= models.pcdi.InhibitCoreFun2D(this);
77  else
78  this.f= models.pcdi.CoreFun2D(this);
79  end
80 
81  /* Assemble kernel expansion */
82  s = RandStream(" mt19937ar "," Seed ",2);
83  k = kernels.KernelExpansion;
84 
85  nc = 5;
86  k.Centers.xi= [.25 1 1.2 .8 .4
87  .45 .25 .65 .9 .8];
88  k.Ma= s.rand(1,nc)*1e5;
89  this.Kexp= k;
90 
91  /* Spatial area (unscaled!) */
92  this.Omega= [0 1.5; 0 1] * this.Model.L;
93 
94  /* Scaled! */
95  this.h= .5 * this.Model.L;
96  }
97 
98 
100  setConfig@models.pcdi.BasePCDISystem(this, mu, inputidx)
101  /* % Precompute diffusivity c(x,mu) on current grid */
102  cxmu = this.DiscreteCXMU.compose(0,mu);
103  /* Use same linear indexing as other quantities on grid */
104  this.CurCXMU= cxmu(:);
105  }
106 
107 
108  function varargout = plot(models.BaseFullModel model,double t,matrix<double> y,varargin) {
109  if ~isempty(varargin) && isa(varargin[1]," PlotManager ")
110  pm = varargin[1];
111  else
112  pm = PlotManager;
113  if nargout == 0
114  pm.LeaveOpen= true;
115  else
116  varargout[1] = pm;
117  end
118  end
119  h = pm.nextPlot([model.SaveTag " _outputplot "],...
120  sprintf(" Output plot for model %s ",model.Name)," Time "," Caspase-3 Concentration ");
121  /* plot(h,t,y,'r','LineWidth',2); */
122  semilogy(h,t,y," r "," LineWidth ",2);
123  if isempty(varargin)
124  pm.done;
125  end
126  }
127 
128 
129  function varargout = plotState(models.BaseFullModel model,double t,matrix<double> y,mode) {
130 
131  if nargin < 5
132 /* mode = [2 3]; */
133  mode = 2;
134  end
135 
136  /* Plot all fields into the same figure */
137 
138  varargout = [];
139  if any(mode == 1)
140  this.plot1DState(model, t, y, getPM);
141  end
142  if any(mode == 2) || any(mode == 3)
143  pm1 = [];
144  if any(mode == 2)
145  pm1 = getPM;
146  end
147  pm2 = [];
148  if any(mode == 3)
149  pm2 = getPM;
150  end
151  this.plot2DState(t, y, pm1, pm2);
152  end
153  for k=1:length(varargout)
154  varargout[k].done;
155  end
156 
157  function pm = getPM
158  if model.WithInhibitors
159  np = 4;
160  else
161  np = 2;
162  end
163  pm = PlotManager(false,2,np);
164  /* Use max 2 figures */
165  pm.MaxFigures= 1;
166  pm.LeaveOpen= true;
167  varargout[end+1] = pm;
168  end
169  }
170 
171 
172  function pm = plotDiffusionCoeff(colvec<double> mu,pm,allparts) {
173  if nargin < 4
174  allparts = false;
175  end
176  if nargin < 3 || isempty(pm)
177  if allparts
178  pm = PlotManager(false,2,2);
179  else
180  pm = PlotManager;
181  end
182  if nargout == 0
183  pm.LeaveOpen= true;
184  end
185  if nargin < 2
186  mu = linspace(0,1,10);
187  end
188  end
189  o = this.Omega / this.Model.L;
190  fineness = 1;
191  [x,y] = meshgrid(0:this.hs/fineness:o(1,2),0:this.hs/fineness:o(2,2));
192  apm = this.getAffParamCX(x,y," mu ");
193  if allparts
194  for k=1:length(this.Gammas)
195  h = pm.nextPlot(sprintf(" diff_coeff_%d ",k),...
196  sprintf(" Spatial diffusion coefficient base c_%d(x) for \\gamma=%g ",...
197  k,this.Gammas(k))," x "," y ");
198  surf(h,x,y,log10(apm.getMatrix(k))," EdgeColor "," interp "," FaceColor "," interp ");
199  zlabel(" Log-scaled diffusivity ");
200  end
201  end
202  for k = 1:length(mu)
203  h = pm.nextPlot(sprintf(" diff_coeff_mu_%d ",k),...
204  sprintf(" Spatial diffusion coefficient c(x) for mu(5)=%g ",...
205  mu(k))," x "," y ");
206  surf(h,x,y,log10(apm.compose(0,mu(k)))," EdgeColor "," interp "," FaceColor "," interp ");
207  zlabel(" Log-scaled diffusivity ");
208  end
209  if nargin < 3
210  pm.done;
211  end
212  }
213 
214 
215  protected:
216 
217  function newSysDimension() {
218  m = prod(this.Dims);
219 
220  /* % Compute c(x,mu) on current grid */
221  o = this.Omega / this.Model.L;
222  [x,y] = meshgrid(0:this.hs:o(1,2),0:this.hs:o(2,2));
223  this.DiscreteCXMU= this.getAffParamCX(x,y," mu(5) ");
224 
225  /* % Initial conditions */
226  x0 = dscomponents.AffineInitialValue;
227  x0part = zeros(this.NumStateDofs,1);
228  for k = 1:this.DiscreteCXMU.N
229  M = this.DiscreteCXMU.getMatrix(k);
230  x0part(1:2*m) = [M(:); M(:)]*5e-13;
231  x0part(2*m+1:4*m) = [M(:); M(:)]*3e-8;
232  if this.Model.WithInhibitors
233  x0part(4*m+1:6*m) = [M(:); M(:)]*3e-8;
234  x0part(6*m+1:end) = [M(:); M(:)]*5e-13;
235  end
236  x0.addMatrix(this.DiscreteCXMU.funStr[k],x0part);
237  end
238  this.x0= x0;
239 
240  /* % Diffusion part */
241  this.A= this.assembleA;
242 
243  /* % Output extraction */
244  p = .1; /* 10% of each dimensions span, centered in geometry. */
245 
246  d = this.Dims(1);
247  d1idx = find(abs((1:d) - d/2) <= d/2 * p);
248  if isempty(d1idx)
249  d1idx = 1;
250  end
251  d = this.Dims(2);
252  d2idx = find(abs((1:d) - d/2) <= d/2 * p);
253  if isempty(d2idx)
254  d2idx = 1;
255  end
256  [d1,d2] = meshgrid(d1idx,d2idx);
257  sel = reshape(sub2ind(this.Dims,d1,d2),1,[]);
258  C = sparse(1,this.NumStateDofs);
259  /* Pick the Caspase-3 concentration */
260  ca3 = m+1:2*m;
261  C(ca3(sel)) = 1/length(sel);
262  this.C= dscomponents.LinearOutputConv(C);
263  }
264 
265 
266  private:
267 
268 
269  function res = assembleA() {
270 
271  D = this.Diff;
272 
273  res = dscomponents.AffLinCoreFun(this);
274  res.TimeDependent= false;
275 
276  /* Add constant diffusion on [0, .2] interval */
277  A = MatUtils.laplacemat(this.hs, this.Dims(1), this.Dims(2));
278 
279  /* Use coefficient functions that split 1 into n linear
280  * functions (equally spaced) */
281  lso = LinearSplitOfOne(" mu(5) ",length(this.Gammas));
282  add(lso.getFunStr(0),A);
283 
284  /* Then use all specified gamma widths and compute affine-linear
285  * interpolation */
286  for k=1:length(this.Gammas)
287  /* Set gamma (will influence the functions diffusionCoeff
288  * etc) */
289  this.Kexp.Kernel.Gamma= this.Gammas(k);
290  /* Compile matrix */
291  A = this.assembleASpatialDiff;
292  add(lso.getFunStr(k),A);
293  end
294 
295  function add(str, A)
296  if this.Model.WithInhibitors
297  augA = blkdiag(A,D(1)*A,D(2)*A,D(3)*A,...
298  D(4)*A,D(5)*A,D(6)*A,D(7)*A);
299  else
300  augA = blkdiag(A,D(1)*A,D(2)*A,D(3)*A);
301  end
302  res.addMatrix(str, augA);
303  end
304 
305  }
306 
307 
308  function A = assembleASpatialDiff() {
309  o = this.Omega / this.Model.L;
310  [x,y] = meshgrid(0:this.hs:o(1,2),0:this.hs:o(2,2));
311  x = x" ; y = y ";
312  if size(x,1) ~= this.Dims(1) || size(x,2) ~= this.Dims(2)
313  error(" Boo ");
314  end
315 
316  /* % c(x) * \laplace u part
317  * laplace matrix */
318  A1 = MatUtils.laplacemat(this.hs, this.Dims(1), this.Dims(2));
319  /* times c(x) */
320  A1 = bsxfun(@times,A1,this.diffusionCoeff([x(:) y(:)]" ) ");
321 
322  /* % grad c(x) . grad u part */
323  A2 = MatUtils.divcdivumat(x,y,@this.nablaC);
324 
325  A = A1 + A2;
326  }
334  function apm = getAffParamCX(colvec<double> x,matrix<double> y,muarg) {
335  c = zeros(size(x,1),size(x,2),length(this.Gammas));
336  apm = general.AffParamMatrix;
337  lso = LinearSplitOfOne(muarg,length(this.Gammas));
338  apm.addMatrix(lso.getFunStr(0),ones(size(x)));
339  for k=1:length(this.Gammas)
340  this.Kexp.Kernel.Gamma= this.Gammas(k);
341  hlp = this.diffusionCoeff([x(:) y(:)]^t);
342  c(:,:,k) = reshape(hlp,size(x,1),[]);
343  apm.addMatrix(lso.getFunStr(k),c(:,:,k));
344  end
345  }
357  function c = diffusionCoeff(colvec<double> x) {
358  c = 1./(1+this.Kexp.evaluate(x));
359  }
360 
361 
362  function nc = nablaC(colvec<double> x) {
363  nc = -1/(1+this.Kexp.evaluate(x))^2 * this.Kexp.getStateJacobian(x);
364  }
365 
366 
367  function plot1DState(models.BaseFullModel model,double t,matrix<double> y,pm) {
368  m = prod(this.Dims);
369 
370  /* Select cell center values */
371  idxmat = zeros(this.Dims);
372  idxmat(:) = 1:m;
373  sel = idxmat(:,round(this.Dims(2)/2));
374  sel = [sel; sel+m; sel+2*m; sel+3*m;...
375  sel+4*m; sel+5*m; sel+6*m; sel+7*m];
376  m = length(sel)/8;
377  y = y(sel,:);
378 
379  if length(t) > 150
380  idx = round(linspace(1,length(t),150));
381  t = t(idx);
382  y = y(:,idx);
383  end
384  states = [" dead ", " alive ", " unstable "];
385  ss = this.Model.getSteadyStates(this.mu(4))^t;
386 
387  X = t;
388  Y = (this.Omega(1,1):this.h:this.Omega(1,2))/model.L;
389  pos = reshape(1:8*m,[],8)^t;
390  doplot(" c8 "," Caspase-8 (x_a) ",1);
391  doplot(" c3 "," Caspase-3 (y_a) ",2);
392  doplot(" pc8 "," Pro-Caspase-8 (x_i) ",3);
393  doplot(" pc3 "," Pro-Caspase-3 (y_i) ",4);
394  if this.Model.WithInhibitors
395  doplot(" iap "," IAP (iap) ",5);
396  doplot(" bar "," BAR (bar) ",6);
397  doplot(" yb "," Caspase-3+IAP (yb) ",7);
398  doplot(" xb "," Caspase-8+BAR (xb) ",8);
399  end
400 
401  function doplot(tag, thetitle, pnr)
402  yl = y(pos(pnr,:),:);
403  perc = (yl(end) - ss(2,pnr)) / (ss(1,pnr) - ss(2,pnr));
404  if perc < .5
405  id = 2;
406  else
407  id = 1;
408  end
409 
410 /* if any(reldi > .1) || any(reldi < 10)
411  * [~, id] = min(perc); */
412  tit = sprintf(" %s concentrations\nCell state at T=%.4g: %s\n%.5g (%5.2f%%) ", thetitle,...
413  max(t),states[id],yl(end),perc*100);
414 /* else
415  * tit = sprintf('%s concentrations\n%s', thetitle,reldistr);
416  * end */
417  if pm.Single
418  tit = sprintf(" Model '%s'\n%s ",model.Name,tit);
419  end
420  h = pm.nextPlot(tag,tit," Time [s] "," Cell slice ");
421  surf(h,X,Y,yl," EdgeColor "," none ");
422  zlabel(h,thetitle);
423  end
424  }
425 
426 
427  function plot2DState(rowvec t,matrix v,pm1,pm2) {
428 
429  m = prod(this.Dims);
430  pos = reshape(1:8*m,[],8)^t;
431 
432  /* % Plot timesteps */
433  a = this.Omega;
434  x = a(1,1):this.h:a(1,2);
435  y = a(2,1):this.h:a(2,2);
436  [X,Y] = meshgrid(x,y);
437  X = X^t;
438  Y = Y^t;
439 
440  step = round(length(t)/40);
441  for idx=1:step:length(t)
442  /* % Mode 2 plots */
443  if ~isempty(pm1)
444  imagescplot(1);
445  imagescplot(2);
446  imagescplot(3);
447  imagescplot(4);
448  if this.Model.WithInhibitors
449  imagescplot(5);
450  imagescplot(6);
451  imagescplot(7);
452  imagescplot(8);
453  end
454  pm1.done;
455  end
456  set(gcf," Name ",sprintf(" Concentration top view at t=%f ",t(idx)));
457  /* % Mode 3 plots */
458  if ~isempty(pm2)
459  surfplot(1);
460  surfplot(2);
461  surfplot(3);
462  surfplot(4);
463  if this.Model.WithInhibitors
464  surfplot(5);
465  surfplot(6);
466  surfplot(7);
467  surfplot(8);
468  end
469  pm2.done;
470  rotate3d on;
471  end
472  set(gcf," Name ",sprintf(" Augmented concentration differences at t=%f ",t(idx)));
473  if idx ~= length(t)
474  pause(.1);
475  if any(~ishandle(h))
476  return;
477  end
478  end
479  end
480 
481  function imagescplot(dim)
482  V = reshape(v(pos(dim,:),idx),this.Dims(1),[])^t;
483  h = pm1.nextPlot(sprintf(" PCDI2D_plot2D_%d ",this.Tags[dim]),...
484  sprintf(" %s concentrations ", this.Labels[dim])," x "," y ");
485  imagesc(x,y,V," Parent ",h);
486  set(h," DataAspectRatio ",[1 1 1]);
487  axis(h," xy ");
488  colorbar(" peer ",h," SouthOutside ");
489  end
490 
491  function surfplot(dim)
492  /* Extract the data for the current cell */
493  V = reshape(v(pos(dim,:),idx),this.Dims(1),[]);
494 
495  /* Augment the differences */
496  mv = mean(V(:));
497  diff = V - mv;
498  diffnorm = norm(diff);
499  V = mv + diff/diffnorm;
500 
501  h = pm2.nextPlot(sprintf(" PCDI2D_plot2D_%d ",this.Tags[dim]),...
502  sprintf(" %s concentration difference\n Mean %g, augmented by %g ",...
503  this.Labels[dim],mv,diffnorm)," x "," y ");
504  /* delete(get(h,'Children')); */
505  surf(h,X,Y,V," FaceColor "," interp "," EdgeColor "," none ");
506  ar = get(h," DataAspectRatio ");
507  set(h," DataAspectRatio ",[1 1 ar(3)]);
508  axis(h," tight ");
509  end
510  }
526 };
527 }
528 }
529 
530 
531 
function varargout = plotState(models.BaseFullModel model,double t,matrix< double > y, mode)
Definition: PCDISystem2D.m:129
Dims
The system's dimensions.
char Name
The name of the Model.
Definition: BaseModel.m:117
Labels
The concentration labels.
Gammas
Gamma values to use as kernel width for diffusivity Gammas = .08;.
Definition: PCDISystem2D.m:54
Tags
The concentration image tags.
LinearSplitOfOne: Computes a sequence of hat functions at equidistant nodes from [0,len] to enable an efficient, easy way of division of unity.
hs
scaled spatial stepwidth
The base class for any KerMor detailed model.
Definition: BaseFullModel.m:18
PCDISystem2D(models.BaseFullModel model)
Definition: PCDISystem2D.m:71
Model
The Model this System is attached to.
h
Spatial stepwidth (in unscaled size units!) is set in subclasses.
function setConfig(colvec< double > mu,integer inputidx)
Definition: PCDISystem2D.m:99
function pm = plotDiffusionCoeff(colvec< double > mu, pm, allparts)
Definition: PCDISystem2D.m:172
reshape
hanges the dimensions of the handle object array to the specified dimensions. See the MATLAB reshape ...
An integer value.
PlotManager: Small class that allows the same plots generated by some script to be either organized a...
Definition: PlotManager.m:17
inputidx
The current inputindex of the function .
function fx = evaluate(matrix x, varargin)
Evaluates the kernel expansion.
dscomponents.AInitialValue x0
Function handle to initial state evaluation.
function newSysDimension()
Custom updates for new system dimension.
Definition: PCDISystem2D.m:217
PCDISystem2D The inhibited version programmed cell death model for 2D geometry.
Definition: PCDISystem2D.m:19
logical LeaveOpen
Flag indicating if the plots should be left open once the PlotManager is deleted (as variable) ...
Definition: PlotManager.m:213
A variable number of input arguments.
Omega
The spatial width/area/region (in unscaled size units!)
static function [ A , idxmat ] = laplacemat(h, d1, d2)
Computes a 2D diffusion sparse matrix with zero neuman boundary conditions.
Definition: MatUtils.m:34
mu
The current parameter for simulations, [] is none used.
function J = getStateJacobian(x, varargin)
Evaluates the jacobian matrix of this function at the given point.
BasePCDISystem The base dynamical system class for the the Programmed Cell Death Model by Markus Daub...
#define X(i, j)
kernels.KernelExpansion Kexp
The kernel expansion used to construct the diffusion coefficient distribution.
Definition: PCDISystem2D.m:42
function varargout = plot(models.BaseFullModel model,double t,matrix< double > y, varargin)
Definition: PCDISystem2D.m:108
dscomponents.LinearOutputConv C
The output conversion Defaults to an LinearOutputConv instance using a 1-matrix, which just forwards ...
dscomponents.ACoreFun f
The core f function from the dynamical system.
dscomponents.ACoreFun g
The system's algebraic constraints function.
MatUtils: Matrix utility functions.
Definition: MatUtils.m:17
#define Y(i, j)
A matlab row vector.
dscomponents.AMassMatrix M
The system's mass matrix.
Diff
Relative diffusion coefficients ([d2/d1, d3/d1, d4/d1])
dscomponents.LinearCoreFun A
Represents a linear or affine-linear component of the dynamical system.
char SaveTag
A custom tag that can be used as a prefix to files for corresponding model identification.
KernelExpansion: Base class for state-space kernel expansions.
A variable number of output arguments.