Skip to content

Commit 25d9753

Browse files
committed
Add calculations of wake potential
1 parent ce231a3 commit 25d9753

File tree

7 files changed

+70
-21
lines changed

7 files changed

+70
-21
lines changed

lcode2dPy/diagnostics/diagnostics_3d.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ def dt(self, *parameters):
5454

5555
class Diagnostics_f_xi:
5656
__allowed_f_xi = ['Ex', 'Ey', 'Ez', 'Bx', 'By', 'Bz', 'ne', 'nb',
57-
'Ez2', 'Bz2', 'nb2']
58-
# 'Phi', 'ni']
57+
'Ez2', 'Bz2', 'nb2', 'Phi']
58+
# 'ni']
5959
# TODO: add them and functionality!
6060
__allowed_f_xi_type = ['numbers', 'pictures', 'both']
6161
#TODO: add 'pictures' and 'both' and functionality
@@ -125,7 +125,7 @@ def dxi(self, current_time, xi_plasma_layer,
125125
self.__data['xi'].append(xi_plasma_layer)
126126

127127
for name in self.__f_xi_names:
128-
if name in ['Ex', 'Ey', 'Ez', 'Bx', 'By', 'Bz']:
128+
if name in ['Ex', 'Ey', 'Ez', 'Bx', 'By', 'Bz', 'Phi']:
129129
field = getattr(pl_fields, name)[self.__ax_x, self.__ax_y]
130130
self.__data[name].append(field)
131131

@@ -172,8 +172,8 @@ def dt(self, *params):
172172

173173
class Diagnostics_colormaps:
174174
__allowed_colormaps = ['Ex', 'Ey', 'Ez', 'Bx', 'By', 'Bz', 'ne', 'nb',
175-
'px', 'py', 'pz']
176-
# 'Phi', 'ni']
175+
'px', 'py', 'pz', 'Phi']
176+
# 'ni']
177177
# TODO: add them and functionality!
178178
__allowed_colormaps_type = ['numbers']
179179
#TODO: add 'pictures' and 'both' and functionality
@@ -260,7 +260,7 @@ def dxi(self, current_time, xi_plasma_layer,
260260
self.__data['xi'].append(xi_plasma_layer)
261261

262262
for name in self.__colormaps_names:
263-
if name in ['Ex', 'Ey', 'Ez', 'Bx', 'By', 'Bz']:
263+
if name in ['Ex', 'Ey', 'Ez', 'Bx', 'By', 'Bz', 'Phi']:
264264
val = getattr(pl_fields, name)[
265265
self.__steps//2, self.__r_f:self.__r_t]
266266
self.__data[name].append(val)
@@ -362,7 +362,7 @@ def dt(self, current_time,
362362
px=pl_particles.px, py=pl_particles.py, pz=pl_particles.pz,
363363
Ex=pl_fields.Ex, Ey=pl_fields.Ey, Ez=pl_fields.Ez,
364364
Bx=pl_fields.Bx, By=pl_fields.By, Bz=pl_fields.Bz,
365-
ro=pl_currents.ro,
365+
Phi=pl_fields.Phi, ro=pl_currents.ro,
366366
jx=pl_currents.jx, jy=pl_currents.jy, jz=pl_currents.jz)
367367

368368

lcode2dPy/plasma3d/data.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
('Bx', _float_array),
1313
('By', _float_array),
1414
('Bz', _float_array),
15+
('Phi', _float_array),
1516
]
1617

1718

@@ -24,6 +25,7 @@ def __init__(self, n_cells: int) -> None:
2425
self.Bx = np.zeros((n_cells, n_cells), dtype=np.float64)
2526
self.By = np.zeros((n_cells, n_cells), dtype=np.float64)
2627
self.Bz = np.zeros((n_cells, n_cells), dtype=np.float64)
28+
self.Phi = np.zeros((n_cells, n_cells), dtype=np.float64)
2729

2830
# Average operation is necessary on intermediate steps to have
2931
# better approximations of all fields
@@ -35,6 +37,7 @@ def average(self, other):
3537
fields.Bx = (self.Bx + other.Bx) / 2
3638
fields.By = (self.By + other.By) / 2
3739
fields.Bz = (self.Bz + other.Bz) / 2
40+
fields.Phi = (self.Phi + other.Phi) / 2
3841
return fields
3942

4043
def copy(self):
@@ -45,6 +48,7 @@ def copy(self):
4548
new_fields.Bx = np.copy(self.Bx)
4649
new_fields.By = np.copy(self.By)
4750
new_fields.Bz = np.copy(self.Bz)
51+
new_fields.Phi = np.copy(self.Phi)
4852
return new_fields
4953

5054

@@ -113,4 +117,4 @@ def __init__(self, ro_initial, dirichlet_matrix,
113117
self.ro_initial = np.copy(ro_initial)
114118
self.dirichlet_matrix = np.copy(dirichlet_matrix)
115119
self.field_mixed_matrix = np.copy(field_mixed_matrix)
116-
self.neumann_matrix = np.copy(neumann_matrix)
120+
self.neumann_matrix = np.copy(neumann_matrix)

lcode2dPy/plasma3d/fields.py

+26-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from lcode2dPy.plasma3d.data import Fields, Currents, Const_Arrays
88

99

10-
# Solving Laplace equation with Dirichlet boundary conditions (Ez) #
10+
# Solving Laplace equation with Dirichlet boundary conditions (Ez and Phi) #
1111

1212
def calculate_Ez(grid_step_size, const: Const_Arrays, currents: Currents):
1313
"""
@@ -32,12 +32,30 @@ def calculate_Ez(grid_step_size, const: Const_Arrays, currents: Currents):
3232
# unnormalized DST-Type1 is its own inverse, up to a factor 2(N+1)
3333
# and we take all scaling matters into account with a single factor
3434
# hidden inside dirichlet_matrix.
35-
35+
3636
Ez_inner = scipy.fftpack.dstn(f, type=1)
3737
Ez = np.pad(Ez_inner, 1, 'constant', constant_values=0)
3838
return Ez
3939

4040

41+
def calculate_Phi(const: Const_Arrays, currents: Currents):
42+
"""
43+
Calculates Phi as iDST2D(dirichlet_matrix * DST2D(-ro + jz)).
44+
"""
45+
ro, jz = currents.ro, currents.jz
46+
47+
rhs_inner = (ro - jz)[1:-1, 1:-1]
48+
49+
f = scipy.fftpack.dstn(rhs_inner, type=1)
50+
51+
f *= const.dirichlet_matrix
52+
53+
Phi_inner = scipy.fftpack.dstn(f, type=1)
54+
Phi = np.pad(Phi_inner, 1, 'constant', constant_values=0)
55+
56+
return Phi
57+
58+
4159
# Solving Laplace or Helmholtz equation with mixed boundary conditions #
4260

4361
def mix2d(a):
@@ -178,15 +196,15 @@ def compute_fields(self, flds: Fields, flds_prev: Fields,
178196
curr_prev: Currents, curr: Currents):
179197
# Looks terrible! TODO: rewrite this function entirely
180198
new_flds = Fields((flds.Ex).shape[0])
181-
199+
182200
(new_flds.Ex,
183201
new_flds.Ey,
184202
new_flds.Bx,
185203
new_flds.By) = calculate_Ex_Ey_Bx_By(self.grid_step_size,
186204
self.xi_step_size,
187205
self.trick, self.variant_A, const,
188206
flds, rho_beam, curr, curr_prev)
189-
207+
190208
if self.variant_A:
191209
new_flds.Ex = 2 * new_flds.Ex - flds_prev.Ex
192210
new_flds.Ey = 2 * new_flds.Ey - flds_prev.Ey
@@ -195,5 +213,7 @@ def compute_fields(self, flds: Fields, flds_prev: Fields,
195213

196214
new_flds.Ez = calculate_Ez(self.grid_step_size, const, curr)
197215
new_flds.Bz = calculate_Bz(self.grid_step_size, const, curr)
198-
199-
return new_flds, new_flds.average(flds_prev)
216+
217+
new_flds.Phi = calculate_Phi(const, curr)
218+
219+
return new_flds, new_flds.average(flds_prev)

lcode2dPy/plasma3d/initialization.py

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ def load_plasma(config: Config, path_to_plasmastate: str):
173173
state['Ex'], state['Ey'], state['Ez']
174174
fields.Bx, fields.By, fields.Bz =\
175175
state['Bx'], state['By'], state['Bz']
176+
fields.Phi = state['Phi']
176177

177178
particles.x_offt, particles.x_offt =\
178179
state['x_offt'], state['y_offt']

lcode2dPy/plasma3d_gpu/data.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,6 @@ def fields_average(fields1: GPUArrays, fields2: GPUArrays):
8383
Ez = (fields1.Ez + fields2.Ez) / 2,
8484
Bx = (fields1.Bx + fields2.Bx) / 2,
8585
By = (fields1.By + fields2.By) / 2,
86-
Bz = (fields1.Bz + fields2.Bz) / 2
87-
)
86+
Bz = (fields1.Bz + fields2.Bz) / 2,
87+
Phi = (fields1.Phi + fields2.Phi) / 2
88+
)

lcode2dPy/plasma3d_gpu/fields.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from lcode2dPy.plasma3d_gpu.data import GPUArrays, fields_average
66

77

8-
# Solving Laplace equation with Dirichlet boundary conditions (Ez) #
8+
# Solving Laplace equation with Dirichlet boundary conditions (Ez and Phi) #
99

1010
def dst2d(a):
1111
"""
@@ -56,6 +56,24 @@ def calculate_Ez(grid_step_size, const, currents):
5656
return Ez
5757

5858

59+
def calculate_Phi(const, currents):
60+
"""
61+
Calculates Phi as iDST2D(dirichlet_matrix * DST2D(-ro + jz)).
62+
"""
63+
ro, jz = currents.ro, currents.jz
64+
65+
rhs_inner = (ro - jz)[1:-1, 1:-1]
66+
67+
f = dst2d(rhs_inner)
68+
69+
f *= const.dirichlet_matrix
70+
71+
Phi_inner = dst2d(f)
72+
Phi = cp.pad(Phi_inner, 1, 'constant', constant_values=0)
73+
74+
return Phi
75+
76+
5977
# Solving Laplace or Helmholtz equation with mixed boundary conditions #
6078

6179
def mix2d(a):
@@ -230,7 +248,7 @@ def compute_fields(self, fields, fields_prev, const,
230248
self.grid_step_size, self.xi_step_size,
231249
self.trick, self.variant_A, const,
232250
fields, rho_beam, currents, currents_prev)
233-
251+
234252
if self.variant_A:
235253
Ex = 2 * Ex - fields_prev.Ex
236254
Ey = 2 * Ey - fields_prev.Ey
@@ -240,7 +258,10 @@ def compute_fields(self, fields, fields_prev, const,
240258
Ez = calculate_Ez(self.grid_step_size, const, currents)
241259
Bz = calculate_Bz(self.grid_step_size, const, currents)
242260

261+
Phi = calculate_Phi(const, currents)
262+
243263
new_fields = GPUArrays(Ex=Ex, Ey=Ey, Ez=Ez,
244-
Bx=Bx, By=By, Bz=Bz)
264+
Bx=Bx, By=By, Bz=Bz,
265+
Phi=Phi)
245266

246267
return new_fields, fields_average(new_fields, fields_prev)

lcode2dPy/plasma3d_gpu/initialization.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ def zeros():
163163
return cp.zeros((grid_steps, grid_steps), dtype=cp.float64)
164164

165165
fields = GPUArrays(Ex=zeros(), Ey=zeros(), Ez=zeros(),
166-
Bx=zeros(), By=zeros(), Bz=zeros())
166+
Bx=zeros(), By=zeros(), Bz=zeros(),
167+
Phi=zeros())
167168

168169
particles = GPUArrays(x_init=x_init, y_init=y_init,
169170
x_offt=x_offt, y_offt=y_offt,
@@ -184,7 +185,8 @@ def load_plasma(config: Config, path_to_plasmastate: str):
184185
with np.load(file=path_to_plasmastate) as state:
185186
fields = GPUArrays(Ex=state['Ex'], Ey=state['Ey'],
186187
Ez=state['Ez'], Bx=state['Bx'],
187-
By=state['By'], Bz=state['Bz'])
188+
By=state['By'], Bz=state['Bz'],
189+
Phi=state['Phi'])
188190

189191
particles = GPUArrays(x_init=particles.x_init, y_init=particles.y_init,
190192
q=particles.q, m=particles.m,

0 commit comments

Comments
 (0)