rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
cubegrid.m
1 classdef cubegrid < gridbase
2 % A hierarchical cubegrid of arbitrary dimension
3 %
4 % This is an axis parallel hexaedral grid, with non-conform refinement
5 % structure. All vertices are explicitly generated and stored. Only practical
6 % for low dimensions, i.e. 1 to 4 or perhaps 5.
7 
8 
9  properties
10  dimension; % dimension of grid/number of coordinates per vertex
11 
12  vertex; % 'vertex(i,j)' is the j-th coordinate of i-th vertex point
13 
14  % 'vertexindex(id,j)' is the index of j-th vertex of element 'id'
15  vertexindex;
16 
17  level; % 'level(id)' equals refinement level of element 'id'
18 
19  isleaf; % 'isleaf(id)' indicates, wether element 'id' is leaf or not
20 
21  firstchild; % 'firstchild(id)' indicates the lowest-index of a child element
22 
23  % number of refinement-steps, that have been performed on the grid so far
24  refine_steps;
25 
26  % 'creation_step(id)' stores the refinement step number that has led to the
27  % element 'id'.
28  creation_step;
29 
30  end
31 
32  methods
33 
34  function grid= cubegrid(varargin)
35  %function cubegrid(varargin)
36  % constructor of a hierarchical cubegrid
37  %
38  % Preliminaries:
39  % In the following
40  % - 'gid' denotes global element indices, i.e. also covering non-leaf
41  % elements,
42  % - 'lid' denotes indices for leaf indices.
43  % .
44  % Conversion can be performed by 'gids = lid2gid(grid,lid)'
45  % Conversion can be performed by 'lids = gid2lid(grid,gid)'
46  %
47  % The constructor has the following
48  % Synopsis:
49  %
50  % - 'cubegrid()' : construction of a default cubegrid (2d unit square)
51  % - 'cubegrid(cgrid)' : copy-constructor
52  % - 'cubegrid(params)': generate cubegrid with certain options. The
53  % argument 'params' requires the following fields:
54  % -# 'range' : cell array of intervals, where the array lengths
55  % determine the grid dimension
56  % -# 'numintervals': vector with number of intervals per dimension
57  %
58  % perhaps later: constructor by duneDGF-file?
59  %
60  % generated fields of grid:
61  % dimension : dimension of grid/number of coordinates per vertex
62  % nelements : number of overall elements (leaf + nonleaf);
63  % nvertices : number of vertices;
64  % vertex : 'vertex(i,j) = 'j-th coordinate of 'i'-th vertex point
65  % vertexindex : 'vertexindex(id,j) = ' index of 'j'-th vertex of element 'id'
66  % level : 'level(id) = ' level, i.e. refinement steps of element id
67  % isleaf : 'isleaf(id) = ' 0 or 1 whether element id is leaf or not
68  % refine_steps : number of refinement-steps, that have been performed
69  % on the grid so far
70  % creation_step : 'creation_step(id) ' stores the refinement step number
71  % that has led to the element 'id'.
72 
73  % Bernard Haasdonk 1.3.2007
74 
75  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76  % default constructor: unit square
77  if (nargin==0)
78  grid.dimension = 2;
79  %range = {[0,1],[0,1]};
80  %numintervals = {1,1};
81  grid.nelements = 1;
82  grid.nvertices = 4;
83  % list of vertex vectors: rowwise coordinates
84  % => vectex(i,j) : j-th coordinate of i-th vector
85  grid.vertex = [0 1 0 1; ...
86  0 0 1 1]';
87  % vertex-index vectors: i-th row contains indices of element i
88  % element_vertices(i,j): index of j-th vertex of element i
89  grid.vertexindex = [1 2 3 4];
90  grid.firstchild = 0;
91  grid.level = zeros(grid.nelements,1); % everything level 0 by default
92  grid.isleaf = ones(grid.nelements,1); % everything leaf by default
93  grid.creation_step = zeros(grid.nelements,1); % 0 by default
94  grid.refine_steps = 0; % initially set ref-counter to zero
95 
96  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97  % copy constructor
98  elseif isa(varargin{1},'cubegrid')
99  grid= varargin{1};
100 
101 
102  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103  % construct from params
104  else
105  if isnumeric(varargin{1}) && size(varargin{1},1)==1
106  partition = varargin{1};
107  % creat t partition
108  params.numintervals = length(partition)-1;
109  params.range = {partition([1,end])};
110  else
111  partition = [];
112 
113  params = varargin{1};
114  end
115  grid.dimension = length(params.range);
116  % abbreviation
117  dim = grid.dimension;
118  grid.nelements = prod(params.numintervals);
119  grid.nvertices = prod(params.numintervals+1);
120 
121  % generate vertices
122  rangevec ={};
123  for i=1:dim
124  ni = params.numintervals(i);
125  ra = params.range{i};
126  rangevec{i} = (0:ni) * (ra(2) - ra(1)) / ni + ra(1);
127  end;
128  vi = cell(1,dim);
129  if (dim > 1)
130  [vi{:}] = ndgrid(rangevec{:});
131  else
132  vi{1} = rangevec{1};
133  end;
134  ve = zeros(grid.nvertices,0);
135  for i=1:dim
136  ve = [ve, vi{i}(:)];
137  end;
138  grid.vertex = ve;
139 
140  % generate element vertex index lists
141  % list for first element is clear, others obtained by shifting
142  % suitably:
143  % 1D: [0, 1]
144  % 2D: [0, 1, intervals(1)+1, intervals(1)+2]
145  % 3D: [ 2D, 2D + (intervals(1)+1)*(intervals(2)+1)],
146  % ... doubling plus npoints-product
147 
148  ref_elem_indices = [0 1];
149  npoints = params.numintervals + 1;
150  for i=2:dim
151  ref_elem_indices = [ref_elem_indices, ...
152  ref_elem_indices + prod(npoints(1:(i-1)))];
153  end;
154 
155  % generate list of starting points
156  % volume of starting indices:
157  % generate vertices
158  indexvec ={};
159  for i=1:dim
160  indexvec{i} = 0:(params.numintervals(i)-1);
161  end;
162  vi = cell(1,dim);
163  if dim > 1
164  [vi{:}] = ndgrid(indexvec{:});
165  else
166  vi{1} = indexvec{1};
167  end;
168  % starting indices = 1+ vi{1} + npoints(1)*vi{2} + npoints(1)*npoints(2)*vi{3}
169  ve = ones(grid.nelements,1);
170  for i=1:dim
171  ve = ve + prod(npoints(1:(i-1))) * vi{i}(:);
172  end;
173 
174  % for each starting point: attach shifted array
175  grid.vertexindex = ...
176  repmat(ve,1,length(ref_elem_indices)) + ...
177  repmat(ref_elem_indices,length(ve),1);
178  grid.firstchild = zeros(grid.nelements,1);
179 
180  grid.level = zeros(grid.nelements,1); % everything level 0 by default
181  grid.isleaf = ones(grid.nelements,1); % everything leaf by default
182  grid.creation_step = zeros(grid.nelements,1); % 0 by default
183  grid.refine_steps = 0; % initially set ref-counter to zero
184 
185  if ~isempty(partition)
186  grid.vertex(grid.vertexindex(1:end-1,2)) = partition(2:end-1);
187  end
188 
189  end
190 
191  end
192 
193  function [compress,new] = tpart_refine(this, lids, new_midpoints)
194  % function [compress,new] = tpart_refine(this, lids, new_midpoints)
195  % refines certain grid entities
196  %
197  % Parameters:
198  % lids: ids of grid entities to be refined
199  % new_midpoints: the newly created vertices of the refined grid.
200  %
201  % Return values:
202  % compress: the ids of entities which are getting smaller due to the
203  % refinement.
204  % new: ids of the newly created elements
205 
206  assert(this.dimension == 1);
207  gids = lid2gid(this, lids);
208  refine(this, lids);
209  children = this.firstchild(gids);
210 
211  if nargin == 3 && ~isempty(new_midpoints)
212  this.vertex(this.vertexindex(children,2)) = new_midpoints;
213  end
214  compress = lids;
215  new = [children,children+1]';
216  end
217 
218 
219  res = check_consistency(grid);
220 
221  leaf_element = coord2leaf_element(grid,coord);
222 
223  demo(dummy);
224 
225  display(grid);
226 
227  ret = get(grid,propertyname, varargin);
228 
229  [p0, p1] = get_edges(grid,gid);
230 
231  gids = get_leafgids(grid);
232 
233  range = get_ranges_of_element(grid, element);
234 
235  vols = get_volume(grid,gids);
236 
237  lids = gid2lid(grid,gids);
238 
239  gids = lid2gid(grid,lids);
240 
241  p = plot(grid,params);
242 
243  p = plot_grid(grid,params);
244 
245  p = plot_leafelement_data(grid,data,params);
246 
247  p = plot_leafvertex_data(grid,data,params);
248 
249  ngrid = refine(grid, lids);
250 
251  ngrid = remove_duplicate_vertices(grid , epsilon );
252 
253  function gcopy=copy(grid)
254  % a deep copy of the cubegrid
255  %
256  % return values:
257  % gcopy: a deep copy of this instance also of type cubegrid.
258  gcopy = cubegrid(grid);
259  end
260 
261 
262  end
263 
264  methods ( Access = private )
265 
266  function gridtmp = gen_plot_data(grid)
267  %function gridtmp = gen_plot_data(grid)
268  %
269  % Auxiliary function for 2d plotting.
270  % generation of a structure gridtmp with fields
271  % nelements, nvertices, nneigh, VI, X, Y, CX and CY representing
272  % the leaf level of the grid
273  % as these quantities are required by the common 2d grid plot routines.
274 
275  % Bernard Haasdonk 9.5.2007
276 
277  elid = get(grid,'leafelements');
278 
279  gridtmp = cubegrid();
280 
281  gridtmp.nelements = length(elid);
282  gridtmp.nvertices = grid.nvertices;
283  gridtmp.nneigh = 4;
284  gridtmp.VI = grid.vertexindex(elid,[1 2 4 3]);
285  gridtmp.X = grid.vertex(:,1);
286  gridtmp.Y = grid.vertex(:,2);
287 
288  % get X coordinates of vertices as vector
289  XX = grid.vertex(gridtmp.VI(:),1);
290  % reshape to matrix
291  XX = reshape(XX,size(gridtmp.VI));
292 
293  % get Y coordinates of vertices as vector
294  YY = grid.vertex(gridtmp.VI(:),2);
295  % reshape to matrix
296  YY = reshape(YY,size(gridtmp.VI));
297 
298  gridtmp.CX = mean(XX,2);
299  gridtmp.CY = mean(YY,2);
300  end
301 
302 
303  end
304 end
305 
A hierarchical cubegrid of arbitrary dimension.
Definition: cubegrid.m:17
Definition: leaf.m:17
Base class for all grid classes.
Definition: gridbase.m:17