Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Oct 26, 2023
1 parent d01b6cb commit 67e6be9
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 137 deletions.
117 changes: 74 additions & 43 deletions lasy/examples/FBPICbeam.py
Original file line number Diff line number Diff line change
@@ -1,103 +1,134 @@
import numpy as np
from scipy.constants import c, e, m_e, m_p, k

# Import the relevant structures from fbpic
from fbpic.main import Simulation
from fbpic.lpa_utils.laser import add_laser, add_laser_pulse
from fbpic.lpa_utils.laser.laser_profiles import FlattenedGaussianLaser, GaussianLaser
from fbpic.openpmd_diag import FieldDiagnostic, ParticleDiagnostic, \
set_periodic_checkpoint, restart_from_checkpoint, BoostedFieldDiagnostic, BoostedParticleDiagnostic
from fbpic.openpmd_diag import (
FieldDiagnostic,
ParticleDiagnostic,
set_periodic_checkpoint,
restart_from_checkpoint,
BoostedFieldDiagnostic,
BoostedParticleDiagnostic,
)
from fbpic.lpa_utils.boosted_frame import BoostConverter
from fbpic.lpa_utils.laser import add_laser_pulse, FromLasyFileLaser

import os
from mpi4py.MPI import COMM_WORLD as comm


# Whether to use the GPU
use_cuda = True

# The simulation box
Nz = 6000 #4000 #3200 # Number of gridpoints along z
zmax = 0.e-6 # Right end of the simulation box (meters)
#equal to the size of LASY box for the laser to be centered
zmin = -2*90.e-15*c # Left end of the simulation box (meters)
Nr = 500 #405 #270 # Number of gridpoints along r
rmax = 200.e-6 # Length of the box along r (meters)
Nm = 2 # Number of modes used
Nz = 6000 # 4000 #3200 # Number of gridpoints along z
zmax = 0.0e-6 # Right end of the simulation box (meters)
# equal to the size of LASY box for the laser to be centered
zmin = -2 * 90.0e-15 * c # Left end of the simulation box (meters)
Nr = 500 # 405 #270 # Number of gridpoints along r
rmax = 200.0e-6 # Length of the box along r (meters)
Nm = 2 # Number of modes used
# Boost factor
gamma_boost = 5.
gamma_boost = 5.0
# Maximum simulation length
Lmax = 5.e-3 #LASY propagation distance of a pulse
Lmax = 5.0e-3 # LASY propagation distance of a pulse
# The simulation timestep
dt = min( rmax/(2*gamma_boost*Nr), (zmax-zmin)/Nz/c ) # Timestep (seconds)
dt = min(rmax / (2 * gamma_boost * Nr), (zmax - zmin) / Nz / c) # Timestep (seconds)

n_order = 16

boost = BoostConverter(gamma_boost)



# The moving window
v_window = c

# Velocity of the Galilean frame (for suppression of the NCI)
v_comoving = -np.sqrt(gamma_boost**2-1.)/gamma_boost * c
v_comoving = -np.sqrt(gamma_boost**2 - 1.0) / gamma_boost * c

# The diagnostics
diag_period = 500 # Period of the diagnostics in number of timesteps
diag_period = 500 # Period of the diagnostics in number of timesteps

# Whether to write the fields in the lab frame
Ntot_snapshot_lab = 40
Ntot_snapshot_lab = 40
dt_snapshot_lab = (Lmax) / v_window / (Ntot_snapshot_lab - 1)

track_bunch = False # Whether to tag and track the particles of the bunch

# The interaction length of the simulation (meters)
L_interact = Lmax # the plasma length
L_interact = Lmax # the plasma length
# Interaction time (seconds) (to calculate number of PIC iterations)
T_interact = boost.interaction_time( L_interact, (zmax-zmin), v_window )
T_interact = boost.interaction_time(L_interact, (zmax - zmin), v_window)
# (i.e. the time it takes for the moving window to slide across the plasma)

#take the first file
PathToTheLasyFile = './DiagLASYforFBPIC/laser00000001_00001.h5'
laser_profile = FromLasyFileLaser(PathToTheLasyFile)
# take the first file
PathToTheLasyFile = "./DiagLASYforFBPIC/laser00000001_00001.h5"
laser_profile = FromLasyFileLaser(PathToTheLasyFile)


# ---------------------------
# Carrying out the simulation
# ---------------------------

if __name__ == '__main__':

if __name__ == "__main__":
# Initialize the simulation object
sim = Simulation( Nz, zmax, Nr, rmax, Nm, dt,
zmin=zmin, boundaries={'z':'open', 'r':'open'}, initialize_ions=False,
n_order=n_order, use_cuda=use_cuda, v_comoving=v_comoving,
gamma_boost=gamma_boost, verbose_level=2, particle_shape='cubic',
use_galilean=True)
sim = Simulation(
Nz,
zmax,
Nr,
rmax,
Nm,
dt,
zmin=zmin,
boundaries={"z": "open", "r": "open"},
initialize_ions=False,
n_order=n_order,
use_cuda=use_cuda,
v_comoving=v_comoving,
gamma_boost=gamma_boost,
verbose_level=2,
particle_shape="cubic",
use_galilean=True,
)
# By default the simulation initializes an electron species (sim.ptcl[0])
# Because we did not pass the arguments `n`, `p_nz`, `p_nr`, `p_nz`,
# this electron species does not contain any macroparticles.
# It is okay to just remove it from the list of species.
sim.ptcl = []


add_laser_pulse( sim, laser_profile, gamma_boost=gamma_boost,
method='antenna', z0_antenna=0.e-6, v_antenna=0.)
add_laser_pulse(
sim,
laser_profile,
gamma_boost=gamma_boost,
method="antenna",
z0_antenna=0.0e-6,
v_antenna=0.0,
)
# Convert parameter to boosted frame
v_window, = boost.velocity( [ v_window ] )
(v_window,) = boost.velocity([v_window])
# Configure the moving window
sim.set_moving_window( v=v_window )
sim.set_moving_window(v=v_window)

# Add a diagnostics
write_dir = 'DiagsFBPIC'
write_dir = "DiagsFBPIC"
sim.diags = [
BoostedFieldDiagnostic( zmin, zmax, c,
dt_snapshot_lab, Ntot_snapshot_lab, gamma_boost,
period=diag_period, fldobject=sim.fld, comm=sim.comm,
fieldtypes=["E", "B", "rho"], write_dir=write_dir)
]

N_step = int(T_interact/sim.dt)
BoostedFieldDiagnostic(
zmin,
zmax,
c,
dt_snapshot_lab,
Ntot_snapshot_lab,
gamma_boost,
period=diag_period,
fldobject=sim.fld,
comm=sim.comm,
fieldtypes=["E", "B", "rho"],
write_dir=write_dir,
)
]

N_step = int(T_interact / sim.dt)
### Run the simulation
sim.step( N_step )
sim.step(N_step)
42 changes: 21 additions & 21 deletions lasy/examples/LASYbeam.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ def add_parabola(laser, f0):
omega0 = laser.profile.omega0
Nt = laser.grid.field.shape[-1]
omega = 2 * np.pi * np.fft.fftfreq(Nt, dt) + omega0

#on-axis parabola

r = laser.grid.axes[0]
#transform the field from temporal to frequency domain
field_fft = np.fft.ifft(laser.grid.field, axis=-1, norm="backward")
field_fft = np.fft.ifft(laser.grid.field, axis=-1, norm="backward")

#multiply spectral image by a parabolic mirror phase
field_fft *= mirror_parabolic(f0, omega/c, r).T
field_fft *= mirror_parabolic(f0, omega/c, r).T

#rewrite laser field
laser.grid.field[:, :, :] = np.fft.fft(field_fft, axis=-1, norm="backward"
)
Expand All @@ -37,12 +37,12 @@ def mirror_parabolic(f0, kz, r):
"""
Generate array with spectra-radial phase
representing the on-axis Parabolic Mirror
f0: focal length of a parabolic mirror
kz = omega/c
r: radial axis
r: radial axis
"""

s_ax = r**2/4/f0

val = np.exp( -2j * s_ax[None,:] * \
Expand All @@ -62,7 +62,7 @@ def mirror_parabolic(f0, kz, r):
w0 = 20.e-3
#pulse duration (s)
tau = 30e-15
#maximum intensity time
#maximum intensity time
t_peak = 0

pulseProfile = GaussianProfile(wavelength, pol, laser_energy, w0, tau, t_peak, z_foc=0.e-3)
Expand All @@ -72,14 +72,14 @@ def mirror_parabolic(f0, kz, r):
R_max = 60.e-3

dim = "rt"
lo = (0e-6, -90e-15)
hi = (R_max, 90e-15)
lo = (0e-6, -90e-15)
hi = (R_max, 90e-15)

npoints = (Nr, 500)

laser = Laser(dim=dim, lo=lo, hi=hi, npoints=npoints, profile=pulseProfile)

#add sperical abberations
#add sperical abberations
R = np.linspace(0, R_max, Nr)
#(n, m) = (2, 0)
jthree = 4
Expand All @@ -106,14 +106,14 @@ def mirror_parabolic(f0, kz, r):
#add parabolic mirror
add_parabola(laser, 2000.e-3)

#define a new grid for a focused beam
new_grid = Grid(dim, lo, (200.e-6, 90e-15), npoints, n_azimuthal_modes=laser.grid.n_azimuthal_modes)
#define a new grid for a focused beam
new_grid = Grid(dim, lo, (200.e-6, 90e-15), npoints, n_azimuthal_modes=laser.grid.n_azimuthal_modes)
laser.propagate((1995.e-3), grid=new_grid, nr_boundary = 128, backend = 'NP') #resample the grid

laser_fbpic = copy.deepcopy(laser)

#propagate laser by a box size to align LASY and FBPIC
#laser pulse in FBPIC is emitted first and then propagated
#laser pulse in FBPIC is emitted first and then propagated
#in FBPIC laser is emitted by antenna and consequently it is fully iniallized after a full box size
#initiallised laser should be at the same position as in LASY after propagation over one box
!rm -rf './DiagLASYforFBPIC'
Expand All @@ -130,7 +130,7 @@ def mirror_parabolic(f0, kz, r):


#propagating in a small grid to a focal point, step after step
propDist = 1995.e-3
propDist = 1995.e-3

N = 40
prop_dz = (5.e-3)/(N-1)
Expand All @@ -143,17 +143,17 @@ def mirror_parabolic(f0, kz, r):

file_format ='h5'
filename = "laser%08d"%0
laser.write_to_file(0, propDist, filename, file_format) #writing propagation from 0 to 1995e-3
laser.write_to_file(0, propDist, filename, file_format) #writing propagation from 0 to 1995e-3

#write a file for separate iteration
for j in range(N-1):
laser.propagate(prop_dz)
propDist += prop_dz

i = j+1
file_format ='h5'
filename = "laser%08d"%i
currentit = i
laser.write_to_file(currentit, propDist, filename, file_format)
os.chdir('../')

os.chdir('../')
Loading

0 comments on commit 67e6be9

Please sign in to comment.