-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathspace_charge_class.py
216 lines (179 loc) · 8.52 KB
/
space_charge_class.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#-Begin-preamble-------------------------------------------------------
#
# CERN
#
# European Organization for Nuclear Research
#
#
# This file is part of the code:
#
# PyECLOUD Version 8.7.1
#
#
# Main author: Giovanni IADAROLA
# BE-ABP Group
# CERN
# CH-1211 GENEVA 23
# SWITZERLAND
#
# Contributors: Eleonora Belli
# Philipp Dijkstal
# Lorenzo Giacomel
# Lotta Mether
# Annalisa Romano
# Giovanni Rumolo
# Eric Wulff
#
#
# Copyright CERN, Geneva 2011 - Copyright and any other
# appropriate legal protection of this computer program and
# associated documentation reserved in all countries of the
# world.
#
# Organizations collaborating with CERN may receive this program
# and documentation freely and without charge.
#
# CERN undertakes no obligation for the maintenance of this
# program, nor responsibility for its correctness, and accepts
# no liability whatsoever resulting from its use.
#
# Program and documentation are provided solely for the use of
# the organization to which they are distributed.
#
# This program may not be copied or otherwise distributed
# without permission. This message must be retained on this and
# any other authorized copies.
#
# The material cannot be sold. CERN should be given credit in
# all references.
#
#-End-preamble---------------------------------------------------------
import numpy as np
from scipy.constants import epsilon_0
from scipy.constants import e as qe
na = lambda x: np.array([x])
class space_charge:
#@profile
def __init__(self, chamb, Dh, Dt_sc=None, PyPICmode='FiniteDifferences_ShortleyWeller' , sparse_solver='scipy_slu',
f_telescope=None, target_grid=None, N_nodes_discard=None, N_min_Dh_main=None, Dh_U_eV=None):
print('Start space charge init.')
if PyPICmode == 'FiniteDifferences_ShortleyWeller':
import PyPIC.FiniteDifferences_ShortleyWeller_SquareGrid as PIC_FDSW
self.PyPICobj = PIC_FDSW.FiniteDifferences_ShortleyWeller_SquareGrid(chamb=chamb, Dh=Dh, sparse_solver=sparse_solver)
#To be replaced by a property to make it general (from PyPIC modules not having xn, yn)
self.xn = self.PyPICobj.xn
self.yn = self.PyPICobj.yn
elif PyPICmode == 'ShortleyWeller_WithTelescopicGrids':
import PyPIC.FiniteDifferences_ShortleyWeller_SquareGrid as PIC_FDSW
PyPICmain = PIC_FDSW.FiniteDifferences_ShortleyWeller_SquareGrid(chamb=chamb, Dh=Dh, sparse_solver=sparse_solver)
import PyPIC.MultiGrid as PIC_MG
self.PyPICobj = PIC_MG.AddTelescopicGrids(pic_main=PyPICmain, f_telescope=f_telescope, target_grid=target_grid,
N_nodes_discard=N_nodes_discard, N_min_Dh_main=N_min_Dh_main, sparse_solver=sparse_solver)
self.xn = None # not implemented in this mode (for now)
self.yn = None # not implemented in this mode (for now)
elif PyPICmode == 'FiniteDifferences_Staircase':
import PyPIC.FiniteDifferences_Staircase_SquareGrid as PIC_FDSQ
self.PyPICobj = PIC_FDSQ.FiniteDifferences_Staircase_SquareGrid(chamb=chamb, Dh=Dh, sparse_solver=sparse_solver)
#To be replaced by a property to make it general (from PyPIC modules not having xn, yn)
self.xn = self.PyPICobj.xn
self.yn = self.PyPICobj.yn
elif PyPICmode == 'FFT_PEC_Boundary':
if chamb.chamb_type != 'rect':
raise ValueError('''PyPICmode = 'FFT_PEC_Boundary' can be used only if chamb_type = 'rect' ''' )
import PyPIC.FFT_PEC_Boundary_SquareGrid as PIC_FFT_PEC
self.PyPICobj = PIC_FFT_PEC.FFT_PEC_Boundary_SquareGrid(x_aper=chamb.x_aper, y_aper=chamb.y_aper, Dh=Dh)
#To be replaced by a property to make it general (from PyPIC modules not having xn, yn)
self.xn = None # not implemented in this mode (for now)
self.yn = None # not implemented in this mode (for now)
elif PyPICmode == 'FFT_OpenBoundary':
if chamb.chamb_type != 'rect':
raise ValueError('''PyPICmode = 'FFT_OpenBoundary' can be used only if chamb_type = 'rect' ''' )
import PyPIC.FFT_OpenBoundary as PIC_FFT_Open
if len(np.atleast_1d(Dh)) == 2:
self.PyPICobj = PIC_FFT_Open.FFT_OpenBoundary(x_aper=chamb.x_aper, y_aper=chamb.y_aper, dx=Dh[0], dy=Dh[1])
else:
self.PyPICobj = PIC_FFT_Open.FFT_OpenBoundary(x_aper=chamb.x_aper, y_aper=chamb.y_aper, Dh=Dh)
#To be replaced by a property to make it general (from PyPIC modules not having xn, yn)
self.xn = None # not implemented in this mode (for now)
self.yn = None # not implemented in this mode (for now)
else:
raise ValueError('PyPICmode not recognized')
self.Dh = self.PyPICobj.Dh
self.xg = self.PyPICobj.xg
self.Nxg = self.PyPICobj.Nxg
self.bias_x = self.PyPICobj.bias_x
self.yg = self.PyPICobj.yg
self.Nyg = self.PyPICobj.Nyg
self.bias_y = self.PyPICobj.bias_y
self.Dt_sc = Dt_sc
self.t_last_recom = 0.
self.U_sc_eV_stp = 0.
self.flag_decimate = (self.Dt_sc is not None)
self.last_recomputation_check = False
self.flag_em_tracking = False
if Dh_U_eV is not None:
self.evaluate_U_eV = True
self.Dh_U_eV = Dh_U_eV
self.xn_U_eV, self.yn_U_eV=np.meshgrid(
np.arange(-chamb.x_aper*1.01, chamb.x_aper*1.01, Dh_U_eV),
np.arange(-chamb.y_aper*1.01, chamb.y_aper*1.01, Dh_U_eV))
self.xn_U_eV = self.xn_U_eV.T.flatten()
self.yn_U_eV = self.yn_U_eV.T.flatten()
else:
self.evaluate_U_eV = False
self.comm = None
print('Done space charge init.')
@property
def rho(self):
return self.PyPICobj.rho
@property
def phi(self):
return self.PyPICobj.phi
@property
def efx(self):
return self.PyPICobj.efx
@property
def efy(self):
return self.PyPICobj.efy
def check_for_recomputation(self, t_curr=None):
flag_recompute = True
if self.flag_decimate:
flag_recompute = (t_curr - self.t_last_recom) >= self.Dt_sc
if flag_recompute:
self.t_last_recom = t_curr
self.last_recomputation_check = flag_recompute
return flag_recompute
#@profile
def recompute_spchg_efield(self, MP_e, flag_solve=True, flag_reset=True):
# scatter
self.PyPICobj.scatter(MP_e.x_mp[0:MP_e.N_mp], MP_e.y_mp[0:MP_e.N_mp], MP_e.nel_mp[0:MP_e.N_mp], charge=MP_e.charge, flag_add=not(flag_reset))
# solve
if flag_solve:
if self.comm is not None:
n_sims = self.comm.Get_size()
myid = self.comm.Get_rank()
sendbuf = self.PyPICobj.rho.flatten()
recvbuf = np.zeros((len(sendbuf)*n_sims))
self.comm.Gather(sendbuf, recvbuf) # assumes root is 0
if myid == 0:
avgrho = np.mean(
recvbuf.reshape(n_sims, len(sendbuf)),
axis = 0)
else:
avgrho = 0 * sendbuf # prepare buffer for receive
self.comm.Bcast(avgrho)
self.PyPICobj.rho = avgrho.reshape(self.PyPICobj.rho.shape)
self.PyPICobj.solve()
#@profile
def compute_spchg_efield_from_rho(self, rho, flag_verbose=True):
self.PyPICobj.solve(rho=rho, flag_verbose=flag_verbose)
def get_sc_eletric_field(self, MP_e):
Ex_sc_n, Ey_sc_n = self.PyPICobj.gather(MP_e.x_mp[0:MP_e.N_mp], MP_e.y_mp[0:MP_e.N_mp])
return Ex_sc_n, Ey_sc_n
def get_potential_electric_energy(self):
if not self.evaluate_U_eV:
raise ValueError('Cannot compute potential energy, please provide Dh_U_eV!')
Ex_sc_n, Ey_sc_n = self.PyPICobj.gather(self.xn_U_eV, self.yn_U_eV)
U_eV = 0.5 * np.sum(Ex_sc_n[:]**2 + Ey_sc_n[:]**2) * self.Dh_U_eV**2 * epsilon_0 / qe
return U_eV