-
Notifications
You must be signed in to change notification settings - Fork 2
/
para_poynt_2d.py
176 lines (145 loc) · 4.69 KB
/
para_poynt_2d.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import sys
# including my path and Han Wen's path
sys.path.append('/Users/franktsung/Documents/codes/python-tsung/')
sys.path.append('/Volumes/Lacie-5TB/codes/pyVisOS/')
#
import osh5io
import osh5def
import osh5vis
import osh5utils
# *****************************************************************************
# *****************************************************************************
# MPI related
import mpi4py
from mpi4py import MPI
# MPI related
# *****************************************************************************
# *****************************************************************************
from h5_utilities import *
import matplotlib.pyplot as plt
import sys
# command line options (argc, argv stuff)
import getopt
#
# glob -> finding files in a directory
import glob
#
import numpy as np
def print_help():
print('mpirun -n X python para_poynt.py [options] <InputDir> <OutputName>')
print('options:')
print(' -n: average over n grid points at entrance (32 by default)')
print(' -s: only process every S files')
print(' --avg: look into the -savg directory')
print(' --env: look into the -senv directory')
# *****************************************************************************
# *****************************************************************************
# MPI initialization
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
# *****************************************************************************
# *****************************************************************************
# *****************************************************************************
argc = len(sys.argv)
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], "hxyn:", ['avg', 'env'])
except getopt.GetoptError:
print_help()
sys.exit(2)
if len(args) < 2:
print_help()
sys.exit(2)
dirName = args[0]
outFilename = args[1]
dir_ext = ''
n_avg = 50
# default skip is 1
skip = 1
#
# sumdir = 0 summing over the transverse direction
sumdir = 0
#
#
tags = ''
for opt, arg in opts:
if opt == '-h':
print_help()
sys.exit()
elif opt == '--avg':
dir_ext = '-savg'
elif opt == '--env':
dir_ext = '-senv'
elif opt == '-n':
n_avg = arg
elif opt == '-s':
skip = arg
else:
print(print_help())
sys.exit(2)
e2 = sorted(glob.glob(dirName + '/FLD/e2' + dir_ext + '/*.h5'))
e3 = sorted(glob.glob(dirName + '/FLD/e3' + dir_ext + '/*.h5'))
b2 = sorted(glob.glob(dirName + '/FLD/b2' + dir_ext + '/*.h5'))
b3 = sorted(glob.glob(dirName + '/FLD/b3' + dir_ext + '/*.h5'))
total_time = len(e2)
my_share = total_time // size
i_begin = rank * my_share
if rank < (size - 1):
i_end = (rank + 1) * my_share
else:
i_end = total_time
avg_array=np.ones(n_avg)/n_avg
#
# read the second file to get the time-step
#
h5_filename = e2[1]
h5_data = osh5io.read_h5(h5_filename)
array_dims = h5_data.shape
nx = array_dims[0]
ny = array_dims[1]
time_step = h5_data.run_attrs['TIME'][0]
# h5_output = hdf_data()
# h5_output.shape = [total_time, nx]
print('nx=' + repr(nx))
print('ny=' + repr(ny))
print('time_step=' + repr(time_step))
print('total_time=' + repr(total_time))
# Here we initialize 2 variables
h5_output = np.zeros((total_time, ny))
total = np.zeros((total_time,ny))
#total = 0
total2 = 0
xaxis=h5_data.axes[1]
taxis=osh5def.DataAxis(0, time_step * (total_time -1), total_time,
attrs={'NAME':'t', 'LONG_NAME':'time', 'UNITS':'1 / \omega_p'})
data_attrs = { 'UNITS': osh5def.OSUnits('m_e \omega_p^3'), 'NAME': 's1', 'LONG_NAME': 'S_1' }
print(repr(xaxis.min))
print(repr(xaxis.max))
run_attrs = {'XMAX' : np.array( [time_step * (total_time-1), xaxis.max] ) ,
'XMIN' : np.array( [0, xaxis.min ] ) }
# h5_output.run_attrs['TIME'] = 0.0
# h5_output.run_attrs['UNITS'] = 'm_e /T'
file_number = 0
# skip = 10
for file_number in range(i_begin, i_end,skip):
# print(file_number)
e2_filename = e2[file_number]
e3_filename = e3[file_number]
b2_filename = b2[file_number]
b3_filename = b3[file_number]
e2_data = osh5io.read_h5(e2_filename)
e3_data = osh5io.read_h5(e3_filename)
b2_data = osh5io.read_h5(b2_filename)
b3_data = osh5io.read_h5(b3_filename)
s1_data = e2_data * b3_data - e3_data * b2_data
#if(file_number % 10 == 0):
#print(s1_data.shape)
temp=np.sum(s1_data,axis=0) / nx
# print(temp.shape)
h5_output[file_number, 1:ny] = np.abs(np.convolve(temp[1:ny],avg_array,mode='same'))
comm.Reduce(h5_output, total, op=MPI.SUM, root=0)
if rank == 0:
h5_output = total
if rank == 0:
b=osh5def.H5Data(h5_output, timestamp='x', data_attrs=data_attrs,run_attrs=run_attrs, axes=[taxis, xaxis])
osh5io.write_h5(b,filename=outFilename)