rbmatlab 0.10.01
|
00001 function ENBI = get_enbi(grid, edge, tstep) 00002 %function ENBI = get_enbi(grid, edge) 00003 % function assembling a matrix with the 5 neighbour's cell indices that 00004 % are needed in order to compute the gradient over the edge given by 00005 % 'edge' in each row. see also the sketch below. 00006 % 00007 % arrangement of cell indices: 00008 % edges' indices of the main cell by which one gets the "right" 00009 % neighbours 00010 % @verbatim 00011 % Example: edge == 1: 00012 % N | NE ENBI(:,2) = N | ENBI(:,5) = NE 00013 % ------|------- <-- nb_ind(0) ----------------|----------------- 00014 % main | E ENBI(:,1) | ENBI(:,4) 00015 % ------|------- <-- nb_ind(1) ----------------|----------------- 00016 % S | SE ENBI(:,3) | ENBI(:,6) 00017 % ^ 00018 % edge 00019 % @endverbatim 00020 % 00021 % Martin Drohmann 05.03.2008 00022 00023 persistent Ecache hashes cache_filled; 00024 warning('off','enbi:cache:redundance'); 00025 00026 if nargin == 1 00027 Ecache = {}; 00028 hashes = {}; 00029 return; 00030 end 00031 00032 %ishashed = @(x)(min (cellfun(@(y)(all(y==x)), hashes)) == 1); 00033 hasharray = [size(grid.X), grid.X(1), grid.Y(1), edge]; 00034 00035 if tstep == 1 00036 if cache_filled 00037 % disp('erase cache values'); 00038 Ecache = {}; 00039 hashes = {}; 00040 end 00041 cache_filled = false; 00042 num_cells = size(grid.CX,1); 00043 00044 % TODO: this is a stupid hack: I don't like it 00045 temp = edge; 00046 if edge > 2; 00047 temp = edge - 2; 00048 end 00049 nb_ind = mod([ 2 0 ] + temp, 4) + 1; 00050 00051 00052 ENBI = zeros(num_cells, 6); 00053 ENBI(:,1) = 1:num_cells; 00054 ENBI(:,[2,3]) = grid.NBI(1:num_cells, nb_ind); 00055 00056 %neg_ind = find(ENBI < -1); 00057 %[ind,col] = ind2sub(size(ENBI), neg_ind); 00058 %ENBI(neg_ind) = ind; 00059 00060 for i = 1:3 00061 non_bnd_ind = ENBI(:,i) > 0; 00062 dir_bnd_ind = ENBI(:,i) == -1; 00063 ENBI(non_bnd_ind,i+3) = grid.NBI(ENBI(non_bnd_ind,i),edge); 00064 % TODO: Also assign to -1 if _one_ neighbour already is -1. It should be 00065 % enough to check for index 4 00066 ENBI(dir_bnd_ind,i+3) = -1; 00067 end 00068 00069 temp = logical(ENBI(:,4) == -1); 00070 corner_bnd_ind = temp & ENBI(:,5) == 0; 00071 ENBI(corner_bnd_ind, 5) = -1; 00072 corner_bnd_ind = temp & ENBI(:,6) == 0; 00073 ENBI(corner_bnd_ind, 6) = -1; 00074 %neg_ind = find(ENBI < -1); 00075 %[ind,col] = ind2sub(size(ENBI), neg_ind); 00076 %ENBI(neg_ind) = ENBI(sub2ind(size(ENBI), ind, col - 3)); 00077 00078 %ENBI(find(ENBI == 0)) = -1; 00079 00080 if(~isempty(hashes)) 00081 hashind = gethash(hasharray, hashes); 00082 else 00083 hashind = []; 00084 end 00085 if(~( isempty(hashind))) 00086 warning('enbi:cache:redundance','two identical hashvalues'); 00087 % if(max(max([P1cache.(hashvalue){:}] ~= [P1res{:}]))==1 || ... 00088 % max(max([P2cache.(hashvalue){:}] ~= [P2res{:}]))==1) 00089 if(max(max(Ecache{hashind} ~= ENBI)) == 1) 00090 error('WARNING: hashfunction is not good enough!!!'); 00091 end 00092 else 00093 hashind = length(Ecache)+1; 00094 hashes{hashind} = hasharray; 00095 Ecache{hashind} = ENBI; 00096 end 00097 00098 else 00099 00100 cache_filled = true; 00101 hashind = gethash(hasharray, hashes); 00102 ENBI = Ecache{hashind}; 00103 end 00104 end 00105 00106 function [ind]=gethash(X,hashes) 00107 ind = find(cellfun(@(y)(all(y==X)), hashes)); 00108 end 00109