Vlasiator ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
sort_refined_ids.py
Go to the documentation of this file.
1import numpy as np
2import pdb
3import time
4
5def findParent(id, gridSize, debug):
6
7 nIndicesInRefLvl = list()
8 for refLvl in np.arange(1,10):
9 nIndicesInRefLvl.append(gridSize * 2 ** ((refLvl - 1) * 3))
10
11 for i in np.arange(len(nIndicesInRefLvl)):
12 if id <= sum(nIndicesInRefLvl[:i+1]):
13 refLvl = i
14 break
15 if refLvl == 0:
16 if id > 0:
17 print("cell {:3d}".format(id)+" is not refined")
18 pass
19
20 return 0, refLvl
21
22 id2 = id - sum(nIndicesInRefLvl[:refLvl])
23 ix = (id2 - 1) % (xdim * 2 ** refLvl) + 1
24 iy = (id2 - 1) / (xdim * 2 ** refLvl) % (ydim * 2 ** refLvl) + 1
25 iz = (id2 - 1) / (xdim * 2 ** refLvl * ydim * 2 ** refLvl) + 1
26 parentId = (int(np.ceil(iz / 2.0) - 1) * xdim * 2 ** (refLvl - 1) * ydim * 2 ** (refLvl - 1) +
27 int(np.ceil(iy / 2.0) - 1) * xdim * 2 ** (refLvl - 1) +
28 int(np.ceil(ix / 2.0)) +
29 sum(nIndicesInRefLvl[:refLvl-1]))
30 if debug:
31 print("id = {:3d}".format(id)+", id2 = {:3d}".format(id2)+
32 ", col = {:2d}".format(ix)+", row = {:2d}".format(iy)+
33 ", plane = {:2d}".format(iz)+", parentId = {:2d}".format(parentId)+
34 ", refLvl = {:1d}".format(refLvl))
35 else:
36 print("cell {:3d}".format(id)+" is the child of cell {:2d}".format(parentId))
37 pass
38
39 return parentId, refLvl
40
41def getChildren(children, parentIds, dimension = 0, up = True, left = True):
42
43 down = not up
44 right = not left
45
46 N = 8
47
48 myChildren = list()
49 for id in parentIds:
50
51 # Select 2/8 children per parent according to the logical parameters up,down,left,right.
52 # The names are slightly unintuitive in other dimensions but they come from dimension == 0
53 if dimension == 0:
54 if up and left:
55 i1 = 0
56 i2 = 1
57 if down and left:
58 i1 = 2
59 i2 = 3
60 if up and right:
61 i1 = 4
62 i2 = 5
63 if down and right:
64 i1 = 6
65 i2 = 7
66
67 if dimension == 1:
68 if up and left:
69 i1 = 0
70 i2 = 2
71 if down and left:
72 i1 = 1
73 i2 = 3
74 if up and right:
75 i1 = 4
76 i2 = 6
77 if down and right:
78 i1 = 5
79 i2 = 7
80
81 if dimension == 2:
82 if up and left:
83 i1 = 0
84 i2 = 4
85 if down and left:
86 i1 = 1
87 i2 = 5
88 if up and right:
89 i1 = 2
90 i2 = 6
91 if down and right:
92 i1 = 3
93 i2 = 7
94
95 if id in children.keys():
96 myChildren.extend(children[id][i1::N])
97 myChildren.extend(children[id][i2::N])
98 else:
99 # If no children were found, return the parent
100 myChildren.append(id)
101
102 return myChildren
103
104
105debug = False
106
107#filename = "grid_test.out"
108filename = "refined_4.out"
109fh = open(filename)
110lines = fh.readlines()
111fh.close()
112
113ids = list()
114
115for i,line in enumerate(lines):
116 #print(line[:-1])
117 words = line.split()
118 if i == 0:
119 xdim = int(words[6])
120 ydim = int(words[8])
121 zdim = int(words[10])
122 else:
123 ids.append(int(words[3]))
124
125gridSize = xdim*ydim*zdim
126
127#debug = True
128
129t1 = time.time()
130
131parents = dict()
132children = dict()
133refLvls = dict()
134hasChildren = list()
135
136for id in ids:
137
138 # Find the parent of cell id
139 parentId, refLvl = findParent(id,gridSize,debug)
140
141 parents[id] = parentId
142 refLvls[id] = refLvl
143
144 # Parents are not stored in the id array by default, let's add them
145 # For completeness
146 if not parentId in ids and parentId > 0:
147 ids.append(parentId)
148
149 # Make a list of cells that have been refined at least once
150 if parentId > 0:
151 if not parentId in hasChildren:
152 children[parentId] = list()
153 hasChildren.append(parentId)
154
155 # Make a list of children for each cell
156 children[parentId].append(id)
157
158# Sort the id and children lists, this is needed when adding cells to pencils
159# to get the order right
160for key in children.keys():
161 children[key].sort()
162ids.sort()
163
164# Second pass to count how many times each cell has been refined
165isRefined = dict()
166for id in ids:
167 isRefined[id] = 0
168 if refLvls[id] > 0:
169 parentId = parents[id]
170 while parentId is not 0:
171 isRefined[parentId] = refLvls[id] - refLvls[parentId]
172 parentId = parents[parentId]
173
174# Begin sorting, select the dimension by which we sort
175dimension = 1
176
177#sortedIds = list()
178mapping = dict()
179for id in ids:
180 # Sort the mesh ids using Sebastians c++ code
181 if dimension == 0:
182
183 dims = (zdim, ydim, xdim)
184
185 idMapped = id
186
187 if dimension == 1:
188
189 dims = (zdim, xdim, ydim)
190
191 x_index = (id-1) % xdim
192 y_index = ((id-1) / xdim) % ydim
193 idMapped = id - (x_index + y_index * xdim) + y_index + x_index * ydim
194
195 if dimension == 2:
196
197 dims = (ydim, xdim, zdim)
198
199 x_index = (id-1) % xdim
200 y_index = ((id-1) / xdim) % ydim
201 z_index = ((id-1) / (xdim * ydim))
202 idMapped = 1 + z_index + y_index * zdim + x_index * ydim * zdim
203
204 #sortedIds.append((idMapped, id))
205 if refLvls[id] == 0:
206 mapping[idMapped] = id
207
208#sortedIds.sort()
209
210# Create a list of unrefined cells
211#sortedUnrefinedIds = dict()
212# for id in isRefined.keys():
213# if refLvls[id] == 0:
214# sortedUnrefinedIds[id] = isRefined[id]
215
216# Create pencils of unrefined cells, store the level of refinement for each cell
217unrefinedPencils = list()
218for i in np.arange(dims[0]):
219 for j in np.arange(dims[1]):
220 ibeg = 1 + i * dims[2] * dims[1] + j * dims[2]
221 iend = 1 + i * dims[2] * dims[1] + (j + 1) * dims[2]
222 myIsRefined = list()
223 myIds = list()
224 for k in np.arange(ibeg,iend):
225 myIds.append(mapping[k])
226 myIsRefined.append(isRefined[mapping[k]])
227 unrefinedPencils.append({'ids' : myIds,
228 'refLvl' : myIsRefined})
229 #unrefinedPencils.append({'ids' : sortedUnrefinedIds.keys()[ibeg:iend],
230 # 'refLvl' : sortedUnrefinedIds.values()[ibeg:iend]})
231
232# Refine the unrefined pencils that contain refined cells
233print
234#print('*** Refining ***')
235#print
236
237pencils = list()
238parentIds = list()
239up = True
240left = True
241
242# Loop over the unrefined pencils
243for row,unrefinedPencil in enumerate(unrefinedPencils):
244 # Refine each pencil according to its max refinement level, then remove duplicates
245 maxRefLvl = max(unrefinedPencil['refLvl'])
246 # We are creating pencils along the 'x' axis, loop over the 'y' and 'z' axes
247 # Assuming the refinement has been done equally in each dimension
248 for i in np.arange(2 ** maxRefLvl):
249 for j in np.arange(2 ** maxRefLvl):
250 if debug:
251 print('Starting new pencil, row = {:1d}, subrow = {:1d}, column = {:1d}'.format(row,i,j))
252 pencilIds = list()
253 # Walk along the unrefined pencil
254 for ix in np.arange(dims[2]):
255 maxLocalRefLvl = unrefinedPencil['refLvl'][ix]
256 if debug:
257 print(' ix = {:1d}, maxLocalRefLvl = {:1d}'.format(ix,maxLocalRefLvl))
258 # Walk down the refinement tree of the parent cell
259 parentIds.append(unrefinedPencil['ids'][ix])
260 offset = 0
261 nUnRefined = 0
262 iRefined = 0
263 for iref in np.arange(max(maxLocalRefLvl,1)):
264
265 # Logic for selecting cells for the pencil among the child cells
266 left = ( (j / 2 ** (maxRefLvl - iref - 1)) % 2 == 0 )
267 up = ( (i / 2 ** (maxRefLvl - iref - 1)) % 2 == 0 )
268 if debug:
269 print(' iref = {:1d}, up = {:b}, left = {:b}'.format(iref,up,left))
270 # The function getChildren returns the children of the parent, or the
271 # parent itself if it has no children
272 cells = getChildren(children, parentIds, dimension, up, left)
273 #print(cells)
274 parentIds = list()
275
276 offset = nUnRefined - iRefined
277 for k,icell in enumerate(cells):
278
279 #print(' icell = {:3d}').format(icell)
280
281 # Add cells that do not have further refinement to the pencil
282 if isRefined[icell] == 0:
283 # Count the number of unrefined cells that have been added during
284 # this iteration
285 nUnRefined += 1
286 # The offset is the number of unrefined cells from the last
287 # iteration minus the index of the refined cell.
288 if offset > 0:
289 pencilIds.insert(-offset,icell)
290 else:
291 pencilIds.append(icell)
292 else:
293 # Store the index of the refined cell
294 iRefined = k
295
296 # Add to cells to be processed on the next refinement level
297 parentIds.append(icell)
298
299 parentIds = list()
300
301 # Add to the list of pencils if ids are not a duplicate of the previous
302 # pencil. This gets rid of most duplicates, but not all of them. Needs fixing.
303 if len(pencils) == 0 or not pencilIds == pencils[-1]['ids']:
304 pencils.append({'ids' : pencilIds,
305 'length': len(pencilIds),
306 'width' : 2.0 ** -max(unrefinedPencil['refLvl']),
307 'row' : row,
308 'subrow' : i,
309 'subcolumn' : j})
310 else:
311 print('Removing duplicate pencil')
312 pass
313
314t2 = time.time()
315
316
317for i,pencil in enumerate(pencils):
318 print("pencil {:2d}, ids: ".format(i), pencil['ids'])
319print(t2-t1)
getChildren(children, parentIds, dimension=0, up=True, left=True)
findParent(id, gridSize, debug)
static ARCH_HOSTDEV VecSimple< T > max(VecSimple< T > const &l, VecSimple< T > const &r)