rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
diffusivity_cached.m
Go to the documentation of this file.
1 function [diff] = diffusivity_cached(glob,params,callerid)
2 %function [diff] = diffusivity_cached(glob,params,callerid)
3 % compute diffusivity tensor for geometry transformation and store results in a
4 % cache
5 
6 persistent Dcache hashes cache_filled callerids;
7 warning('off', 'diffcached:cache:redundance');
8 
9 %% force clearance of cache
10 if nargin == 0
11  Dcache = {};
12  hashes = {};
13  callerids = {};
14  return;
15 end
16 % glob column check
17 if params.debug
18  if ~isempty(glob) && size(glob,1) < size(glob,2)
19  warning('coordinates in variable glob are given row-wise, but expected them to be column-wise');
20  if params.debug > 2
21  keyboard;
22  end
23  end
24 end
25 
26 X = glob(:,1);
27 Y = glob(:,2);
28 
29 %ishashed = @(x)(min (cellfun(@(y)(all(y==x)), hashes)) == 1);
30 
31 hasharray = [size(X), X(1), Y(1),callerid];
32 
33 if params.tstep == 1
34  if cache_filled
35  Dcache = {};
36  hashes = {};
37  callerids = {};
38  end
39  cache_filled = false;
40 
41  [res1, res2] = inv_geo_trans_derivative(params,glob,{(1), (2)},{(1), (2)},callerid);
42  row1 = [res1{1}, res1{2}];
43  row2 = [res2{1}, res2{2}];
44 
45  vlen = size(row1,1);
46  temp0 = reshape([ sum(row1 .* row1, 2), sum(row2 .* row2, 2) ]',2*vlen,1);
47  tempm1 = reshape([ sum(row2 .* row1, 2), zeros(vlen,1) ]', 2*vlen, 1);
48  tempp1 = reshape([ zeros(vlen,1), sum(row1 .* row2, 2) ]', 2*vlen, 1);
49  % temp1 = [ sum(row1 .* row1, 2), sum(row1 .* row2, 2) ];
50  % temp2 = [ sum(row2 .* row1, 2), sum(row2 .* row2, 2) ];
51  diff = spdiags([tempm1,temp0,tempp1],-1:1,2*vlen,2*vlen);
52 
53  if(~isempty(hashes))
54  hashind = gethash(hasharray, hashes);
55  else
56  hashind = [];
57  end
58  if(~(isempty(hashind)) && callerid == callerids{hashind})
59  warning('diffcached:cache:redundance', 'two identical hashvalues');
60  if(max(max(Dcache{hashind} ~= diff)) ==1 )
61  error('WARNING: hashfunction in diffusivity_cached is not good enough!!!');
62  end
63  else
64  % fill cache
65  hashind = length(Dcache)+1;
66  hashes{hashind} = hasharray;
67  Dcache{hashind} = diff;
68  callerids{hashind} = callerid;
69  end
70 else
71  cache_filled = true;
72  hashind = gethash(hasharray, hashes);
73  diff = Dcache{hashind};
74 end
75 
76 end
77 
78 function [ind]=gethash(X,hashes)
79 % function [ind]=gethash(X,hashes)
80 % compute a hash for the cache function in diffusivity_cached()
81 ind = find(cellfun(@(y)(all(y==X)), hashes),1);
82 end
83 
84 %| \docupdate
function diff = diffusivity_cached(glob, params, callerid)
compute diffusivity tensor for geometry transformation and store results in a cache ...
function [ P1res , P2res ] = inv_geo_trans_derivative(model, glob, P1derivates, P2derivates, callerid)
computes entries of a geometry transformation function's inverse transposed jacobian ...