Skip to content

Commit 992188a

Browse files
committed
style: add RUFFs suggestions + remove the if False at the end of the file
1 parent 5371c96 commit 992188a

File tree

1 file changed

+111
-115
lines changed

1 file changed

+111
-115
lines changed

examples/003_MPI_wakefield_simulation.py

Lines changed: 111 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
sys.path.append('../')
1818

1919
from wakis import SolverFIT3D
20-
from wakis import GridFIT3D
20+
from wakis import GridFIT3D
2121
from wakis import WakeSolver
2222

2323
from tqdm import tqdm
2424

2525
# ---------- MPI setup ------------
2626
# can be skipped since it is handled inside GridFIT3D
27-
from mpi4py import MPI
27+
from mpi4py import MPI
2828

2929
comm = MPI.COMM_WORLD # Get MPI communicator
3030
rank = comm.Get_rank() # Process ID
@@ -36,11 +36,11 @@
3636
solid_1 = 'data/001_vacuum_cavity.stl'
3737
solid_2 = 'data/001_lossymetal_shell.stl'
3838

39-
stl_solids = {'cavity': solid_1,
39+
stl_solids = {'cavity': solid_1,
4040
'shell': solid_2
4141
}
4242

43-
stl_materials = {'cavity': 'vacuum',
43+
stl_materials = {'cavity': 'vacuum',
4444
'shell': [30, 1.0, 30] #[eps_r, mu_r, sigma[S/m]]
4545
}
4646

@@ -53,10 +53,10 @@
5353
Ny = 80
5454
NZ = 140
5555

56-
grid = GridFIT3D(xmin, xmax, ymin, ymax, ZMIN, ZMAX,
57-
Nx, Ny, NZ,
56+
grid = GridFIT3D(xmin, xmax, ymin, ymax, ZMIN, ZMAX,
57+
Nx, Ny, NZ,
5858
use_mpi=True, # Enables MPI subdivision of the domain
59-
stl_solids=stl_solids,
59+
stl_solids=stl_solids,
6060
stl_materials=stl_materials,
6161
stl_scale=1.0,
6262
stl_rotate=[0,0,0],
@@ -69,12 +69,12 @@
6969
# Beam parameters
7070
sigmaz = 10e-2 #[m] -> 2 GHz
7171
q = 1e-9 #[C]
72-
beta = 1.0 # beam beta
72+
beta = 1.0 # beam beta
7373
xs = 0. # x source position [m]
7474
ys = 0. # y source position [m]
7575
xt = 0. # x test position [m]
7676
yt = 0. # y test position [m]
77-
# [DEFAULT] tinj = 8.53*sigmaz/c_light # injection time offset [s]
77+
# [DEFAULT] tinj = 8.53*sigmaz/c_light # injection time offset [s]
7878

7979
from wakis.sources import Beam
8080
from scipy.constants import c
@@ -90,19 +90,19 @@
9090

9191
# Solver setup
9292
solver = SolverFIT3D(grid,
93-
bc_low=bc_low,
94-
bc_high=bc_high,
95-
use_stl=True,
93+
bc_low=bc_low,
94+
bc_high=bc_high,
95+
use_stl=True,
9696
use_mpi=True, # Activate MPI
9797
bg='pec' # Background material
9898
)
9999

100100
img_folder = '003_img/'
101-
if not os.path.exists(img_folder):
101+
if not os.path.exists(img_folder):
102102
os.mkdir(img_folder)
103-
103+
104104
# -------------- Custom time loop -----------------
105-
run_timeloop = False
105+
run_timeloop = True
106106
if run_timeloop:
107107
Nt = 3000
108108
# Plot beam current vs time
@@ -128,32 +128,32 @@
128128

129129
# Plot E abs in 2D every 20 timesteps
130130
if n%20 == 0 and plot_2D:
131-
solver.plot2D(field='E', component='Abs',
132-
plane='YZ', pos=0.5,
131+
solver.plot2D(field='E', component='Abs',
132+
plane='YZ', pos=0.5,
133133
cmap='rainbow', vmin=0, vmax=500., interpolation='hanning',
134134
off_screen=True, title=img_folder+'Ez2d', n=n)
135135

136136
# Plot E z in 1D at diferent transverse positions `pos` every 20 timesteps
137137
if n%20 == 0 and plot_1D:
138-
solver.plot1D(field='E', component='z',
139-
line='z', pos=[0.45, 0.5, 0.55],
138+
solver.plot1D(field='E', component='z',
139+
line='z', pos=[0.45, 0.5, 0.55],
140140
xscale='linear', yscale='linear',
141141
off_screen=True, title=img_folder+'Ez1d', n=n)
142-
142+
143143
# -------------- using Wakefield routine -----------------
144-
run_wakefield = True
144+
run_wakefield = False
145145
if run_wakefield:
146146

147147
# ------------ Beam source ----------------
148148
# Beam parameters
149149
sigmaz = 10e-2 #[m] -> 2 GHz
150150
q = 1e-9 #[C]
151-
beta = 1.0 # beam beta
151+
beta = 1.0 # beam beta
152152
xs = 0. # x source position [m]
153153
ys = 0. # y source position [m]
154154
xt = 0. # x test position [m]
155155
yt = 0. # y test position [m]
156-
# [DEFAULT] tinj = 8.53*sigmaz/c_light # injection time offset [s]
156+
# [DEFAULT] tinj = 8.53*sigmaz/c_light # injection time offset [s]
157157

158158

159159
# ----------- Wake Solver setup ----------
@@ -171,7 +171,7 @@
171171
solver.reset_fields()
172172

173173
# Solver run
174-
plotkw2D = {'title': img_folder+'Ez',
174+
plotkw2D = {'title': img_folder+'Ez',
175175
'add_patch':'cavity', 'patch_alpha':1.0,
176176
'patch_reverse' : True, # patch logical_not('cavity')
177177
'vmin':-1e3, 'vmax':1e3, # colormap limits
@@ -180,7 +180,7 @@
180180
slice(0, Ny), # y
181181
slice(skip_cells, -skip_cells)]} # z
182182

183-
solver.wakesolve(wakelength=wakelength,
183+
solver.wakesolve(wakelength=wakelength,
184184
wake=wake,
185185
plot=False, # turn False for speedup
186186
plot_every=30, plot_until=3000, **plotkw2D
@@ -240,8 +240,8 @@
240240
#plt.show()
241241

242242
# Plot Electric field component in 1D for several transverse positions
243-
solver.plot1D(field='E', component='z',
244-
line='z', pos=[0.4, 0.5, 0.6],
243+
solver.plot1D(field='E', component='z',
244+
line='z', pos=[0.4, 0.5, 0.6],
245245
xscale='linear', yscale='linear',
246246
off_screen=True, title=img_folder+'Ez1d')
247247
#plt.show()
@@ -251,10 +251,10 @@
251251
cmap = LinearSegmentedColormap.from_list('name', plt.cm.jet(np.linspace(0.1, 0.9))) # CST's colormap
252252

253253
# Plot Electric field component in 2D using imshow
254-
solver.plot2D(field='E', component='z',
255-
plane='XY', pos=0.5,
254+
solver.plot2D(field='E', component='z',
255+
plane='XY', pos=0.5,
256256
cmap=cmap, vmin=-500, vmax=500., interpolation='hanning',
257-
add_patch='cavity', patch_reverse=True, patch_alpha=0.8,
257+
add_patch='cavity', patch_reverse=True, patch_alpha=0.8,
258258
off_screen=True, title=img_folder+'Ez2d')
259259
#plt.show()
260260

@@ -265,22 +265,97 @@
265265

266266
# ----------- 3d plots results --------------------
267267
# Plot Electric field component in 3D using pyvista.plotter
268-
solver.plot3D('E', component='Abs',
268+
solver.plot3D('E', component='Abs',
269269
cmap='rainbow', clim=[0, 500],
270270
add_stl=['cavity', 'shell'], stl_opacity=0.1,
271271
clip_interactive=True, clip_normal='-y')
272272

273273
# Plot Abs Electric field on STL solid `cavity`
274-
solver.plot3DonSTL('E', component='Abs',
274+
solver.plot3DonSTL('E', component='Abs',
275275
cmap='rainbow', clim=[0, 500],
276276
stl_with_field='cavity', field_opacity=1.0,
277277
stl_transparent='shell', stl_opacity=0.1, stl_colors='white',
278278
#clip_plane=True, clip_normal='-y', clip_origin=[0,0,0], #coming in v0.5.0
279279
off_screen=False, zoom=1.2, title='003_img/Ez3d')
280-
280+
281281

282282
# --------------------- Extra -----------------------
283-
# # Check global material tensors
283+
# Callback plotting functions for debugging
284+
# Need to gather field before, e.g.: Ez = solver.mpi_gather('Ez', x=Nx//2)
285+
# Need to be plotted in rank 0 only e.g.: if rank == 0: plot1D_field(Ez, 'Ez', n=n, results_folder=img_folder, vmin=-800, vmax=800)
286+
def plot1D_field(field, name='E', y=Ny//2, n=None, results_folder='img/', vmin=None, vmax=None,):
287+
if vmin is None:
288+
vmin = -field.max()
289+
if ymax is None:
290+
vmax = field.max()
291+
292+
fig, ax = plt.subplots()
293+
ax.plot(field[y, :], c='g')
294+
ax.set_title(f'{name} at timestep={n}')
295+
ax.set_xlabel('z [m]')
296+
ax.set_ylabel('Field amplitude')
297+
ax.set_ylim((vmin, vmax))
298+
299+
#plot vertical lines at subdomain borders
300+
for r in range(size):
301+
if r == 0:
302+
ax.axvline(NZ//size*(r+1)+grid.n_ghosts, c='red', alpha=0.5)
303+
ax.axvline(NZ//size*(r), c='blue', alpha=0.5)
304+
elif r == (size-1):
305+
ax.axvline(NZ//size*(r+1), c='red', alpha=0.5)
306+
ax.axvline(NZ//size*(r)-grid.n_ghosts, c='blue', alpha=0.5)
307+
else: #outside subdomains
308+
ax.axvline(NZ//size*(r+1)+grid.n_ghosts, c='red', alpha=0.5)
309+
ax.axvline(NZ//size*(r)-grid.n_ghosts, c='blue', alpha=0.5)
310+
311+
fig.tight_layout()
312+
fig.savefig(results_folder+name+'1d_'+str(n).zfill(4)+'.png')
313+
314+
plt.clf()
315+
plt.close(fig)
316+
317+
def plot2D_field(field, name='E', n=None, results_folder='img/', vmin=None, vmax=None,):
318+
extent = (0, NZ, 0, Ny)
319+
if vmin is None:
320+
vmin = -field.max()
321+
if vmax is None:
322+
vmax = field.max()
323+
324+
fig, ax = plt.subplots()
325+
im = ax.imshow(field, cmap='rainbow', extent=extent, vmin=vmin, vmax=vmax)
326+
fig.colorbar(im, cax=make_axes_locatable(ax).append_axes('right', size='5%', pad=0.05))
327+
ax.set_title(f'{name} at timestep={n}')
328+
ax.set_xlabel('z [id]')
329+
ax.set_ylabel('y [id]')
330+
331+
#plot vertical lines at subdomain borders
332+
for r in range(size):
333+
if r == 0:
334+
ax.axvline(NZ//size*(r+1)+grid.n_ghosts, c='red', alpha=0.5)
335+
ax.axvline(NZ//size*(r), c='blue', alpha=0.5)
336+
elif r == (size-1):
337+
ax.axvline(NZ//size*(r+1), c='red', alpha=0.5)
338+
ax.axvline(NZ//size*(r)-grid.n_ghosts, c='blue', alpha=0.5)
339+
else: #outside subdomains
340+
ax.axvline(NZ//size*(r+1)+grid.n_ghosts, c='red', alpha=0.5)
341+
ax.axvline(NZ//size*(r)-grid.n_ghosts, c='blue', alpha=0.5)
342+
343+
#plot fill rectangles for each subdomain
344+
# for r in range(size):
345+
# if r == 0:
346+
# ax.axvspan(NZ//size*(r), NZ//size*(r+1)+grid.n_ghosts, color='darkorange', alpha=0.1)
347+
# elif r == (size-1):
348+
# ax.axvspan(NZ//size*(r)-grid.n_ghosts, NZ//size*(r+1), color='darkorange', alpha=0.1)
349+
# else: #outside subdomains
350+
# ax.axvspan(NZ//size*(r)-grid.n_ghosts, NZ//size*(r+1)+grid.n_ghosts, color='green', alpha=0.1)
351+
352+
fig.tight_layout(h_pad=0.3)
353+
fig.savefig(results_folder+name+'2d_'+str(n).zfill(4)+'.png')
354+
355+
plt.clf()
356+
plt.close(fig)
357+
358+
# # Check global material tensors inside loop
284359
if False:
285360
ieps = solver.mpi_gather_asField(solver.ieps)
286361
sigma = solver.mpi_gather(solver.sigma, 'z', x=Nx//2)
@@ -290,84 +365,5 @@
290365
fig.savefig(img_folder+'ieps_inspect.png')
291366
plt.close(fig)
292367

293-
# Plot
294-
plot2D_field(sigma, 'sigmaz_', results_folder=img_folder)
295-
296-
297-
# Callback plotting functions for debugging
298-
# Need to gather field before, e.g.: Ez = solver.mpi_gather('Ez', x=Nx//2)
299-
# Need to be plotted in rank 0 only e.g.: if rank == 0: plot1D_field(Ez, 'Ez', n=n, results_folder=img_folder, vmin=-800, vmax=800)
300-
if False:
301-
def plot1D_field(field, name='E', y=Ny//2, n=None, results_folder='img/', vmin=None, vmax=None,):
302-
if vmin is None:
303-
vmin = -field.max()
304-
if ymax is None:
305-
vmax = field.max()
306-
307-
fig, ax = plt.subplots()
308-
ax.plot(field[y, :], c='g')
309-
ax.set_title(f'{name} at timestep={n}')
310-
ax.set_xlabel('z [m]')
311-
ax.set_ylabel('Field amplitude')
312-
ax.set_ylim((vmin, vmax))
313-
314-
#plot vertical lines at subdomain borders
315-
for r in range(size):
316-
if r == 0:
317-
ax.axvline(NZ//size*(r+1)+grid.n_ghosts, c='red', alpha=0.5)
318-
ax.axvline(NZ//size*(r), c='blue', alpha=0.5)
319-
elif r == (size-1):
320-
ax.axvline(NZ//size*(r+1), c='red', alpha=0.5)
321-
ax.axvline(NZ//size*(r)-grid.n_ghosts, c='blue', alpha=0.5)
322-
else: #outside subdomains
323-
ax.axvline(NZ//size*(r+1)+grid.n_ghosts, c='red', alpha=0.5)
324-
ax.axvline(NZ//size*(r)-grid.n_ghosts, c='blue', alpha=0.5)
325-
326-
fig.tight_layout()
327-
fig.savefig(results_folder+name+'1d_'+str(n).zfill(4)+'.png')
328-
329-
plt.clf()
330-
plt.close(fig)
331-
332-
def plot2D_field(field, name='E', n=None, results_folder='img/', vmin=None, vmax=None,):
333-
extent = (0, NZ, 0, Ny)
334-
if vmin is None:
335-
vmin = -field.max()
336-
if vmax is None:
337-
vmax = field.max()
338-
339-
fig, ax = plt.subplots()
340-
im = ax.imshow(field, cmap='rainbow', extent=extent, vmin=vmin, vmax=vmax)
341-
fig.colorbar(im, cax=make_axes_locatable(ax).append_axes('right', size='5%', pad=0.05))
342-
ax.set_title(f'{name} at timestep={n}')
343-
ax.set_xlabel('z [id]')
344-
ax.set_ylabel('y [id]')
345-
346-
#plot vertical lines at subdomain borders
347-
if True:
348-
for r in range(size):
349-
if r == 0:
350-
ax.axvline(NZ//size*(r+1)+grid.n_ghosts, c='red', alpha=0.5)
351-
ax.axvline(NZ//size*(r), c='blue', alpha=0.5)
352-
elif r == (size-1):
353-
ax.axvline(NZ//size*(r+1), c='red', alpha=0.5)
354-
ax.axvline(NZ//size*(r)-grid.n_ghosts, c='blue', alpha=0.5)
355-
else: #outside subdomains
356-
ax.axvline(NZ//size*(r+1)+grid.n_ghosts, c='red', alpha=0.5)
357-
ax.axvline(NZ//size*(r)-grid.n_ghosts, c='blue', alpha=0.5)
358-
359-
#plot fill rectangles for each subdomain
360-
if False:
361-
for r in range(size):
362-
if r == 0:
363-
ax.axvspan(NZ//size*(r), NZ//size*(r+1)+grid.n_ghosts, color='darkorange', alpha=0.1)
364-
elif r == (size-1):
365-
ax.axvspan(NZ//size*(r)-grid.n_ghosts, NZ//size*(r+1), color='darkorange', alpha=0.1)
366-
else: #outside subdomains
367-
ax.axvspan(NZ//size*(r)-grid.n_ghosts, NZ//size*(r+1)+grid.n_ghosts, color='green', alpha=0.1)
368-
369-
fig.tight_layout(h_pad=0.3)
370-
fig.savefig(results_folder+name+'2d_'+str(n).zfill(4)+'.png')
371-
372-
plt.clf()
373-
plt.close(fig)
368+
# Plot
369+
plot2D_field(sigma, 'sigmaz_', results_folder=img_folder)

0 commit comments

Comments
 (0)