rbmatlab 0.10.01
datafunc/diffusivity_cached.m
Go to the documentation of this file.
00001 function [diff] = diffusivity_cached(glob,params,callerid)
00002 %function [diff] = diffusivity_cached(glob,params,callerid)
00003 % compute diffusivity tensor for geometry transformation and store results in a
00004 % cache
00005 
00006 persistent Dcache hashes cache_filled callerids;
00007 warning('off', 'diffcached:cache:redundance');
00008 
00009 %% force clearance of cache
00010 if nargin == 0
00011   Dcache    = {};
00012   hashes    = {};
00013   callerids = {};
00014   return;
00015 end
00016 % glob column check
00017 if params.debug
00018   if ~isempty(glob) && size(glob,1) < size(glob,2)
00019     warning('coordinates in variable glob are given row-wise, but expected them to be column-wise');
00020     if params.debug > 2
00021       keyboard;
00022     end
00023   end
00024 end
00025 
00026 X = glob(:,1);
00027 Y = glob(:,2);
00028 
00029 %ishashed = @(x)(min (cellfun(@(y)(all(y==x)), hashes)) == 1);
00030 
00031 hasharray = [size(X), X(1), Y(1),callerid];
00032 
00033 if params.tstep == 1
00034   if cache_filled
00035     Dcache    = {};
00036     hashes    = {};
00037     callerids = {};
00038   end
00039   cache_filled = false;
00040   
00041   [res1, res2] = inv_geo_trans_derivative(params,glob,{(1), (2)},{(1), (2)},callerid);
00042   row1 = [res1{1}, res1{2}];
00043   row2 = [res2{1}, res2{2}];
00044 
00045   vlen = size(row1,1);
00046   temp0  = reshape([ sum(row1 .* row1, 2), sum(row2 .* row2, 2) ]',2*vlen,1);
00047   tempm1 = reshape([ sum(row2 .* row1, 2), zeros(vlen,1) ]', 2*vlen, 1);
00048   tempp1 = reshape([ zeros(vlen,1), sum(row1 .* row2, 2) ]', 2*vlen, 1);
00049   %  temp1 = [ sum(row1 .* row1, 2), sum(row1 .* row2, 2) ];
00050   %  temp2 = [ sum(row2 .* row1, 2), sum(row2 .* row2, 2) ];
00051   diff  = spdiags([tempm1,temp0,tempp1],-1:1,2*vlen,2*vlen);
00052 
00053   if(~isempty(hashes))
00054     hashind = gethash(hasharray, hashes);
00055   else
00056     hashind = [];
00057   end
00058   if(~(isempty(hashind)) && callerid == callerids{hashind})
00059     warning('diffcached:cache:redundance', 'two identical hashvalues');
00060     if(max(max(Dcache{hashind} ~= diff)) ==1 )
00061         error('WARNING: hashfunction in diffusivity_cached is not good enough!!!');
00062     end
00063   else
00064     % fill cache
00065     hashind            = length(Dcache)+1;
00066     hashes{hashind}    = hasharray;
00067     Dcache{hashind}    = diff;
00068     callerids{hashind} = callerid;
00069   end
00070 else
00071   cache_filled = true;
00072   hashind = gethash(hasharray, hashes);
00073   diff = Dcache{hashind};
00074 end
00075 
00076 end
00077 
00078 function [ind]=gethash(X,hashes)
00079 % function [ind]=gethash(X,hashes)
00080 % compute a hash for the cache function in diffusivity_cached()
00081 ind = find(cellfun(@(y)(all(y==X)), hashes),1);
00082 end
00083 
00084 %| \docupdate 
 All Classes Namespaces Files Functions Variables