-
Notifications
You must be signed in to change notification settings - Fork 2
/
StarCatalouger_MPI.py
83 lines (67 loc) · 2.1 KB
/
StarCatalouger_MPI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import glob
import logging
import numpy as np
import _pickle as pl
from mpi4py import MPI
from configparser import ConfigParser as cnf
pwd = os.getcwd()
sys.path.insert(0, pwd + '/Modules')
import gad_snapread as snr
conf = cnf()
conf.read('abe.ini')
snapshot_dir = conf.get('inputs', 'snapshot')
delete = conf.get('misc', 'delete_dump')
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
if rank == 0:
if size%2 ==0:
pass
else:
print("""Assuming the no of snapshot files is 1024,
the no of processor for MPI job is not a
total divisor of 1024, better to run with
8 or 4""")
exit(2)
star_mass_arr, star_pos_x, star_pos_y, star_pos_z = [], [], [], []
def localread(file):
snap = snapread(file)
star_mass = snap.read_mass('STAR')
star_position = snap.read_pos('STAR')
for i in range(len(star_mass)):
star_mass_arr.append(star_mass[i])
star_pos_x.append(star_position[i,0])
star_pos_y.append(star_position[i,1])
star_pos_z.append(star_position[i,2])
files = glob.glob(snapshot_dir + '/snap*')
local_n = int(len(files)/size)
local_i = rank*int(local_n)
local_f = local_i + local_n
local_file = files[local_i:local_f]
for file in local_file:
localread(file)
star_catalog = np.array([star_pos_x, star_pos_y, star_pos_z, star_mass_arr])
with open('star_cat%d.p' % rank, 'wb') as fp:
pl.dump(star_catalog.T, fp)
if rank == 0:
i = 0
star_cat_files = glob.glob('star_cat*')
for file in star_cat_files:
with open(file, 'rb') as reader:
globals()['data%d' % i] = pl.load(reader)
i += 1
for j in range(i):
if j == 0:
data = np.concatenate((globals()['data%d' % j], globals()['data%d' % (j+1)]), axis=0)
elif j == 1:
pass
else:
data = np.concatenate((data, globals()['data%d' % j]), axis=0)
np.savetxt('star_cat.txt', data)
if delete is 'T':
for file in star_cat_files:
os.remove(file)