-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheavy_computing.py
189 lines (129 loc) · 6.25 KB
/
heavy_computing.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
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 26 23:38:10 2025
@author: mengshutang
"""
import numpy as np
import beam_info as bi
import visual as vis
from openpmd_viewer import OpenPMDTimeSeries
import matplotlib.pyplot as plt
import time
import multiprocessing as mp
from multiprocessing import Pool
import os
def get_traj_info(params):
"""
get single trajectory plot info
"""
dirs = params[0]
beam_info = params[1]
L_plot = params[2]
ts = OpenPMDTimeSeries(dirs + '/diags/hdf5')
beam = bi.get_beam_info(ts = ts,beam = 'driver',iteration = beam_info['iteration'],tail_charge = beam_info['tail_c'],tot_charge = beam_info['tot_c'],dt = beam_info['dt'],num_bins = 100)
# 2D plot info
plt_traj = vis.plot_trajectory(beam = beam)
z_sim,beta_sim = plt_traj.bf_in_sims(section = 'tail',axes = None,color = None,linestyle = None,linewidth = None,label = None)
z_vac,beta_vac = plt_traj.bf_in_vac(section = 'tail',L = L_plot,axes = None,color = None,linestyle = None,linewidth = None,label = None)
z,sig = plt_traj.full_beam_size_evln(section = 'tail', L = L_plot, axes = None,color = None,linestyle = None,linewidth = None,label = None)
return [np.concatenate((z_sim,z_vac)),np.concatenate((beta_sim,beta_vac)),z,sig]
def get_traj_info2(params):
"""
get single trajectory plot info
"""
q,dirs,beam_info,L_plot = params
ts = OpenPMDTimeSeries(dirs + '/diags/hdf5')
beam = bi.get_beam_info(ts = ts,beam = 'driver',iteration = beam_info['iteration'],tail_charge = beam_info['tail_c'],tot_charge = beam_info['tot_c'],dt = beam_info['dt'],num_bins = 100)
# 2D plot info
plt_traj = vis.plot_trajectory(beam = beam)
z_sim,beta_sim = plt_traj.bf_in_sims(section = 'tail',axes = None,color = None,linestyle = None,linewidth = None,label = None)
z_vac,beta_vac = plt_traj.bf_in_vac(section = 'tail',L = L_plot,axes = None,color = None,linestyle = None,linewidth = None,label = None)
z,sig = plt_traj.full_beam_size_evln(section = 'tail', L = L_plot, axes = None,color = None,linestyle = None,linewidth = None,label = None)
q.put([np.concatenate((z_sim,z_vac)),np.concatenate((beta_sim,beta_vac)),z,sig])
def multiple_beam_sizes(dirs,data_lst,c_lst,beam_params,L_plot):
start = time.time()
# number of processes
cores = len(data_lst)
with Pool(cores) as p:
trajs = p.map(get_traj_info, [(dirs+dat, beam_params,L_plot) for dat in data_lst])
fig_beta,axes_beta = plt.subplots(1,2,figsize = (12,5))
for count in range(len(data_lst)):
data = data_lst[count]
axes_beta[0].plot(trajs[count][0],trajs[count][1],c = c_lst[count],label = data)
axes_beta[1].plot(trajs[count][2],trajs[count][3],c = c_lst[count],label = data)
axes_beta[0].legend()
axes_beta[0].set_yscale('log')
axes_beta[1].yaxis.tick_right()
axes_beta[1].yaxis.set_label_position('right')
axes_beta[0].set_title('Betafunction')
axes_beta[1].set_title('Beam transverse size')
axes_beta[0].set_ylabel(r'$\beta$ (cm)')
axes_beta[0].set_xlabel(r'$z$ (cm)')
axes_beta[1].set_ylabel(r'$\sigma$ ($\mu$m)')
axes_beta[1].set_xlabel(r'$z$ (cm)')
axes_beta[0].tick_params(right=True,direction = 'in')
axes_beta[1].tick_params(left=True,direction = 'in')
axes_beta[1].set_ylim((0,50))
end = time.time()
print(end-start)
def use_process(dirs,data_lst,c_lst,beam_params,L_plot):
start = time.time()
print('program starting...')
processes = []
trajs = []
for data in data_lst:
#ctx = mp.get_context('spawn')
q = mp.Queue()
params = (q,dirs+data, beam_params,L_plot)
p = mp.Process(target = get_traj_info2, args = (params,))
processes.append(p)
p.start()
for p in processes:
p.join()
if not q.empty():
print(q.get())
trajs.append(q.get())
fig_beta,axes_beta = plt.subplots(1,2,figsize = (12,5))
for count in range(len(data_lst)):
data = data_lst[count]
axes_beta[0].plot(trajs[count][0],trajs[count][1],c = c_lst[count],label = data)
axes_beta[1].plot(trajs[count][2],trajs[count][3],c = c_lst[count],label = data)
axes_beta[0].legend()
axes_beta[0].set_yscale('log')
axes_beta[1].yaxis.tick_right()
axes_beta[1].yaxis.set_label_position('right')
axes_beta[0].set_title('Betafunction')
axes_beta[1].set_title('Beam transverse size')
axes_beta[0].set_ylabel(r'$\beta$ (cm)')
axes_beta[0].set_xlabel(r'$z$ (cm)')
axes_beta[1].set_ylabel(r'$\sigma$ ($\mu$m)')
axes_beta[1].set_xlabel(r'$z$ (cm)')
axes_beta[0].tick_params(right=True,direction = 'in')
axes_beta[1].tick_params(left=True,direction = 'in')
axes_beta[1].set_ylim((0,50))
end = time.time()
print(end-start)
def serial(dirs,data_lst,c_lst,beam_params,L_plot):
start = time.time()
fig_beta,axes_beta = plt.subplots(1,2,figsize = (12,5))
for count in range(len(data_lst)):
data = data_lst[count]
trajs = get_traj_info(params = (dirs+data, beam_params,L_plot))
axes_beta[0].plot(trajs[0],trajs[1],c = c_lst[count],label = data)
axes_beta[1].plot(trajs[2],trajs[3],c = c_lst[count],label = data)
axes_beta[0].legend()
axes_beta[0].set_yscale('log')
axes_beta[1].yaxis.tick_right()
axes_beta[1].yaxis.set_label_position('right')
axes_beta[0].set_title('Betafunction')
axes_beta[1].set_title('Beam transverse size')
axes_beta[0].set_ylabel(r'$\beta$ (cm)')
axes_beta[0].set_xlabel(r'$z$ (cm)')
axes_beta[1].set_ylabel(r'$\sigma$ ($\mu$m)')
axes_beta[1].set_xlabel(r'$z$ (cm)')
axes_beta[0].tick_params(right=True,direction = 'in')
axes_beta[1].tick_params(left=True,direction = 'in')
axes_beta[1].set_ylim((0,50))
end = time.time()
print(end-start)