rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
get_enbi.m
1 function ENBI = get_enbi(grid, edge, tstep)
2 %function ENBI = get_enbi(grid, edge)
3 % function assembling a matrix with the 5 neighbour's cell indices that
4 % are needed in order to compute the gradient over the edge given by
5 % 'edge' in each row. see also the sketch below.
6 %
7 % arrangement of cell indices:
8 % edges' indices of the main cell by which one gets the "right"
9 % neighbours
10 % @verbatim
11 % Example: edge == 1:
12 % N | NE ENBI(:,2) = N | ENBI(:,5) = NE
13 % ------|------- <-- nb_ind(0) ----------------|-----------------
14 % main | E ENBI(:,1) | ENBI(:,4)
15 % ------|------- <-- nb_ind(1) ----------------|-----------------
16 % S | SE ENBI(:,3) | ENBI(:,6)
17 % ^
18 % edge
19 % @endverbatim
20 %
21 % Martin Drohmann 05.03.2008
22 
23 persistent Ecache hashes cache_filled;
24 warning('off','enbi:cache:redundance');
25 
26 if nargin == 1
27  Ecache = {};
28  hashes = {};
29  return;
30 end
31 
32 %ishashed = @(x)(min (cellfun(@(y)(all(y==x)), hashes)) == 1);
33 hasharray = [size(grid.X), grid.X(1), grid.Y(1), edge];
34 
35 if tstep == 1
36  if cache_filled
37 % disp('erase cache values');
38  Ecache = {};
39  hashes = {};
40  end
41  cache_filled = false;
42  num_cells = size(grid.CX,1);
43 
44  % TODO: this is a stupid hack: I don't like it
45  temp = edge;
46  if edge > 2;
47  temp = edge - 2;
48  end
49  nb_ind = mod([ 2 0 ] + temp, 4) + 1;
50 
51 
52  ENBI = zeros(num_cells, 6);
53  ENBI(:,1) = 1:num_cells;
54  ENBI(:,[2,3]) = grid.NBI(1:num_cells, nb_ind);
55 
56  %neg_ind = find(ENBI < -1);
57  %[ind,col] = ind2sub(size(ENBI), neg_ind);
58  %ENBI(neg_ind) = ind;
59 
60  for i = 1:3
61  non_bnd_ind = ENBI(:,i) > 0;
62  dir_bnd_ind = ENBI(:,i) == -1;
63  ENBI(non_bnd_ind,i+3) = grid.NBI(ENBI(non_bnd_ind,i),edge);
64  % TODO: Also assign to -1 if _one_ neighbour already is -1. It should be
65  % enough to check for index 4
66  ENBI(dir_bnd_ind,i+3) = -1;
67  end
68 
69  temp = logical(ENBI(:,4) == -1);
70  corner_bnd_ind = temp & ENBI(:,5) == 0;
71  ENBI(corner_bnd_ind, 5) = -1;
72  corner_bnd_ind = temp & ENBI(:,6) == 0;
73  ENBI(corner_bnd_ind, 6) = -1;
74  %neg_ind = find(ENBI < -1);
75  %[ind,col] = ind2sub(size(ENBI), neg_ind);
76  %ENBI(neg_ind) = ENBI(sub2ind(size(ENBI), ind, col - 3));
77 
78  %ENBI(find(ENBI == 0)) = -1;
79 
80  if(~isempty(hashes))
81  hashind = gethash(hasharray, hashes);
82  else
83  hashind = [];
84  end
85  if(~( isempty(hashind)))
86  warning('enbi:cache:redundance','two identical hashvalues');
87  % if(max(max([P1cache.(hashvalue){:}] ~= [P1res{:}]))==1 || ...
88  % max(max([P2cache.(hashvalue){:}] ~= [P2res{:}]))==1)
89  if(max(max(Ecache{hashind} ~= ENBI)) == 1)
90  error('WARNING: hashfunction is not good enough!!!');
91  end
92  else
93  hashind = length(Ecache)+1;
94  hashes{hashind} = hasharray;
95  Ecache{hashind} = ENBI;
96  end
97 
98 else
99 
100  cache_filled = true;
101  hashind = gethash(hasharray, hashes);
102  ENBI = Ecache{hashind};
103 end
104 end
105 
106 function [ind]=gethash(X,hashes)
107  ind = find(cellfun(@(y)(all(y==X)), hashes));
108 end
109