Skip to content

Commit 9d3cca3

Browse files
committed
Add cell center function to common python code.
Also convert to python int from uint64 to keep things as int: numpy/numpy#5745
1 parent 0043842 commit 9d3cca3

1 file changed

Lines changed: 51 additions & 20 deletions

File tree

‎tests/mhd/common.py‎

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,40 +36,40 @@
3636
def get_metadata(infile):
3737
from numpy import fromfile, int32
3838
ret_val = dict()
39-
file_version = fromfile(infile, dtype = 'uint64', count = 1)
40-
if file_version[0] != 3:
41-
exit('Unsupported file version: ' + str(file_version[0]))
42-
ret_val['file_version'] = file_version[0]
43-
ret_val['sim_step'] = fromfile(infile, dtype = 'uint64', count = 1)[0]
39+
file_version = int(fromfile(infile, dtype = 'uint64', count = 1)[0])
40+
if file_version != 3:
41+
exit('Unsupported file version: ' + str(file_version))
42+
ret_val['file_version'] = file_version
43+
ret_val['sim_step'] = int(fromfile(infile, dtype = 'uint64', count = 1)[0])
4444
ret_val['sim_time'], \
4545
ret_val['adiabatic_index'], \
4646
ret_val['proton_mass'], \
4747
ret_val['vacuum_permeability'] \
48-
= fromfile(infile, dtype = '4double', count = 1)[0]
49-
nr_variables = fromfile(infile, dtype = 'u1', count = 1)[0]
50-
var_offsets = list(fromfile(infile, dtype = 'uint64', count = nr_variables))
51-
endianness = fromfile(infile, dtype = 'uint64', count = 1)[0]
48+
= [float(i) for i in fromfile(infile, dtype = '4double', count = 1)[0]]
49+
nr_variables = int(fromfile(infile, dtype = 'u1', count = 1)[0])
50+
var_offsets = [int(i) for i in fromfile(infile, dtype = 'uint64', count = nr_variables)]
51+
endianness = int(fromfile(infile, dtype = 'uint64', count = 1)[0])
5252
if endianness != 0x1234567890abcdef:
5353
exit('Unsupported endianness: ' + str(endianness))
5454
ret_val['endianness'] = endianness
55-
ret_val['ref_lvl_0_cells'] = list(fromfile(infile, dtype = '3uint64', count = 1)[0])
56-
ret_val['max_ref_lvl'] = fromfile(infile, dtype = 'intc', count = 1)[0]
57-
ret_val['neighborhood_length'] = fromfile(infile, dtype = 'uintc', count = 1)[0]
58-
ret_val['periodicity'] = list(fromfile(infile, dtype = '3uint8', count = 1)[0])
59-
geometry_id = fromfile(infile, dtype = 'intc', count = 1)[0]
60-
if geometry_id != int32(1):
55+
ret_val['ref_lvl_0_cells'] = [int(i) for i in fromfile(infile, dtype = '3uint64', count = 1)[0]]
56+
ret_val['max_ref_lvl'] = int(fromfile(infile, dtype = 'intc', count = 1)[0])
57+
ret_val['neighborhood_length'] = int(fromfile(infile, dtype = 'uintc', count = 1)[0])
58+
ret_val['periodicity'] = [int(i) for i in fromfile(infile, dtype = '3uint8', count = 1)[0]]
59+
geometry_id = int(fromfile(infile, dtype = 'intc', count = 1)[0])
60+
if geometry_id != 1:
6161
exit('Unsupported geometry: ' + str(geometry_id))
6262
ret_val['geometry_id'] = geometry_id
63-
ret_val['grid_start'] = list(fromfile(infile, dtype = '3double', count = 1)[0])
64-
ret_val['lvl_0_cell_length'] = list(fromfile(infile, dtype = '3double', count = 1)[0])
65-
ret_val['total_cells'] = fromfile(infile, dtype = 'uint64', count = 1)[0]
63+
ret_val['grid_start'] = [float(i) for i in fromfile(infile, dtype = '3double', count = 1)[0]]
64+
ret_val['lvl_0_cell_length'] = [float(i) for i in fromfile(infile, dtype = '3double', count = 1)[0]]
65+
ret_val['total_cells'] = int(fromfile(infile, dtype = 'uint64', count = 1)[0])
6666
ret_val['cell_list_start'] = infile.tell()
6767
ret_val['var_data_start'] = dict()
6868
name_len = 8
6969
for offset in var_offsets:
7070
infile.seek(offset, 0)
7171
name = str(fromfile(infile, dtype = 'byte', count = name_len), 'ascii').strip()
72-
ret_val['var_data_start'][name] = int(offset + name_len)
72+
ret_val['var_data_start'][name] = offset + name_len
7373
infile.seek(ret_val['cell_list_start'], 0)
7474
return ret_val
7575

@@ -84,7 +84,7 @@ def get_metadata(infile):
8484
'''
8585
def get_cells(infile, total_cells):
8686
from numpy import dtype, fromfile
87-
return fromfile(infile, dtype = 'uint64', count = total_cells)
87+
return [int(i) for i in fromfile(infile, dtype = 'uint64', count = total_cells)]
8888

8989

9090
'''
@@ -134,6 +134,37 @@ def get_cell_data(infile, metadata, range_):
134134
exit('Unsupported variable: ' + varname)
135135
return ret_val
136136

137+
138+
def get_cell_center(metadata, cell_id):
139+
rl0c = metadata['ref_lvl_0_cells']
140+
l0cl = metadata['lvl_0_cell_length']
141+
gs = metadata['grid_start']
142+
mrl = metadata['max_ref_lvl']
143+
144+
ref_lvl = 0
145+
while True:
146+
last = rl0c[0] * rl0c[1] * rl0c[2] * 2**(3*ref_lvl)
147+
if cell_id <= last:
148+
break
149+
cell_id -= last
150+
ref_lvl += 1
151+
if ref_lvl > mrl:
152+
raise Exception('Impossibly large refinement level for cell: ' + str(cell_id))
153+
154+
cell_id -= 1
155+
cell_index = (
156+
(cell_id % (rl0c[0] * 2**ref_lvl)) * 2**(mrl-ref_lvl),
157+
((cell_id // (rl0c[0] * 2**ref_lvl)) % (rl0c[1] * 2**ref_lvl)) * 2**(mrl-ref_lvl),
158+
(cell_id // (rl0c[0]*rl0c[1] * 2**(2*ref_lvl))) * 2**(mrl-ref_lvl)
159+
)
160+
cell_center = (
161+
gs[0] + l0cl[0] * (cell_index[0] / 2**mrl + 1 / 2**(ref_lvl+1)),
162+
gs[1] + l0cl[1] * (cell_index[1] / 2**mrl + 1 / 2**(ref_lvl+1)),
163+
gs[2] + l0cl[2] * (cell_index[2] / 2**mrl + 1 / 2**(ref_lvl+1)),
164+
)
165+
return cell_center
166+
167+
137168
if __name__ == '__main__':
138169
import sys
139170
for arg in sys.argv[1:]:

0 commit comments

Comments
 (0)