Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def run_cpu_gpu_deposition(show=False, particle_shape='cubic'):
# Skip this test if cuda is not installed
if not cuda_installed:
return
# Perform deposition for a few timesteps, with both the CPU and GPU
for hardware in ['cpu', 'gpu']:
if hardware=='cpu':
use_cuda = False
elif hardware=='gpu':
use_cuda = True
# Initialize the simulation object
sim = Simulation( Nz, zmax, Nr, rmax, Nm, dt,
zmin=zmin, use_cuda=use_cuda, particle_shape=particle_shape )
sim.ptcl = []
# Add an electron bunch (set the random seed first)
np.random.seed(0)
# License: 3-Clause-BSD-LBNL
"""
This file is part of FBPIC (Fourier-Bessel Particle-In-Cell code).
It defines the class that performs the Hankel transform.
Definition of the Hankel forward and backward transform of order p:
g(\nu) = 2 \pi \int_0^\infty f(r) J_p( 2 \pi \nu r) r dr
f( r ) = 2 \pi \int_0^\infty g(\nu) J_p( 2 \pi \nu r) \nu d\nu d
"""
import numpy as np
from scipy.special import jn, jn_zeros
# Check if CUDA is available, then import CUDA functions
from fbpic.utils.cuda import cuda_installed
from .numba_methods import numba_copy_2dC_to_2dR, numba_copy_2dR_to_2dC
if cuda_installed:
from pyculib import blas as cublas
from fbpic.utils.cuda import cuda, cuda_tpb_bpg_2d
from .cuda_methods import cuda_copy_2dC_to_2dR, cuda_copy_2dR_to_2dC
class DHT(object):
"""
Class that allows to perform the Discrete Hankel Transform.
"""
def __init__(self, p, m, Nr, Nz, rmax, use_cuda=False ):
"""
Calculate the r (position) and nu (frequency) grid
on which the transform will operate.
Also store auxiliary data needed for the transform.
"""
This file is part of the Fourier-Bessel Particle-In-Cell code (FB-PIC)
It defines the SpectralGrid class.
"""
import numpy as np
from scipy.constants import epsilon_0
from .numba_methods import numba_push_eb_standard, numba_push_eb_comoving, \
numba_push_eb_pml_standard, numba_push_eb_pml_comoving, \
numba_correct_currents_curlfree_standard, \
numba_correct_currents_crossdeposition_standard, \
numba_correct_currents_curlfree_comoving, \
numba_correct_currents_crossdeposition_comoving, \
numba_filter_scalar, numba_filter_vector
# Check if CUDA is available, then import CUDA functions
from fbpic.utils.cuda import cuda_installed
if cuda_installed:
from fbpic.utils.cuda import cuda_tpb_bpg_2d
from .cuda_methods import cuda, \
cuda_correct_currents_curlfree_standard, \
cuda_correct_currents_crossdeposition_standard, \
cuda_correct_currents_curlfree_comoving, \
cuda_correct_currents_crossdeposition_comoving, \
cuda_filter_scalar, cuda_filter_vector, \
cuda_push_eb_standard, cuda_push_eb_comoving, \
cuda_push_eb_pml_standard, cuda_push_eb_pml_comoving, \
cuda_push_rho
class SpectralGrid(object) :
"""
Contains the fields and coordinates of the spectral grid.
"""
"""
This file is part of the Fourier-Bessel Particle-In-Cell code (FB-PIC)
It defines functions that can save checkpoints,
as well as reload a simulation from a set of checkpoints.
"""
import os, re
import numpy as np
from scipy.constants import e
from .field_diag import FieldDiagnostic
from .particle_diag import ParticleDiagnostic
from fbpic.utils.mpi import comm
# Check if CUDA is available, then import CUDA
from fbpic.utils.cuda import cuda_installed
if cuda_installed:
from fbpic.utils.cuda import cuda
def set_periodic_checkpoint( sim, period, checkpoint_dir='./checkpoints' ):
"""
Set up periodic checkpoints of the simulation
The checkpoints are saved in openPMD format, in the specified
directory, with one subdirectory per process.
The E and B fields and particle information of each processor is saved.
NB: Checkpoints are registered in the list `checkpoints` of the Simulation
object `sim`, and written at the end of the PIC loop (whereas regular
diagnostics are written at the beginning of the PIC loop).
Parameters
----------
if sim.comm.size > 1:
message += "with %d MPI processes " %sim.comm.size
if sim.use_threading and not sim.use_cuda:
message += "(%d threads per process) " %sim.cpu_threads
# Detailed information
elif verbose_level == 2:
# Information on MPI
if mpi_installed:
message += '\nMPI available: Yes'
message += '\nMPI processes used: %d' %sim.comm.size
message += '\nMPI Library Information: \n%s' \
%MPI.Get_library_version()
else:
message += '\nMPI available: No'
# Information on Cuda
if cuda_installed:
message += '\nCUDA available: Yes'
else:
message += '\nCUDA available: No'
# Information about the architecture and the node used
if sim.use_cuda:
message += '\nCompute architecture: GPU (CUDA)'
if mpi_installed:
if gpudirect_enabled:
message += '\nCUDA GPUDirect (MPI) enabled: Yes'
else:
message += '\nCUDA GPUDirect (MPI) enabled: No'
node_message = get_gpu_message()
else:
message += '\nCompute architecture: CPU'
if sim.use_threading:
message += '\nCPU multi-threading enabled: Yes'
It defines the structure necessary to implement the boundary exchanges.
"""
import numpy as np
from scipy.constants import c
from fbpic.utils.mpi import MPI, comm, mpi_type_dict, \
mpi_installed, gpudirect_enabled
from fbpic.fields.fields import InterpolationGrid
from fbpic.fields.utility_methods import get_stencil_reach
from fbpic.particles.particles import Particles
from .field_buffer_handling import BufferHandler
from .pml_damping import PMLDamper
from .particle_buffer_handling import remove_outside_particles, \
add_buffers_to_particles, shift_particles_periodic_subdomain
# Check if CUDA is available, then import CUDA functions
from fbpic.utils.cuda import cuda_installed
if cuda_installed:
from fbpic.utils.cuda import cuda, cuda_tpb_bpg_2d
from .cuda_methods import cuda_damp_EB_left, cuda_damp_EB_right, \
cuda_damp_EB_left_pml, cuda_damp_EB_right_pml
class BoundaryCommunicator(object):
"""
Class that handles the boundary conditions along z, esp.
the moving window and MPI communication between domains.
It also handles the initial domain decomposition.
The functions of this object are:
- At each timestep, to exchange the fields between MPI domains
in the guard cells (n_guard) and damp the E and B fields in the damping
guard cells (nz_damp)
# Copyright 2016, FBPIC contributors
# Authors: Remi Lehe, Manuel Kirchen
# License: 3-Clause-BSD-LBNL
"""
This file is part of the Fourier-Bessel Particle-In-Cell code (FB-PIC)
It defines the SpectralTransformer class, which handles conversion of
the fields from the interpolation grid to the spectral grid and vice-versa.
"""
import numpy as np
from .hankel import DHT
from .fourier import FFT
from .numba_methods import numba_rt_to_pm, numba_pm_to_rt
# Check if CUDA is available, then import CUDA functions
from fbpic.utils.cuda import cuda_installed, cuda
if cuda_installed:
from fbpic.utils.cuda import cuda_tpb_bpg_2d
from .cuda_methods import cuda_rt_to_pm, cuda_pm_to_rt
class SpectralTransformer(object) :
"""
Object that allows to transform the fields back and forth between the
spectral and interpolation grid.
Attributes :
- dht0, dhtm, dhtp : the discrete Hankel transform objects
that operates along r
- fft : the discrete Fourier transform object that operates along z
Main methods :
- spect2interp_scal :
converts a scalar field from the spectral to the interpolation grid
self.Nr = Nr
self.rmax = rmax
self.Nm = Nm
self.dt = dt
self.n_order = n_order
self.v_comoving = v_comoving
self.use_galilean = use_galilean
# Set the default smoother
if smoother is None:
smoother = BinomialSmoother( n_passes=1, compensator=False )
self.smoother = smoother
# Define wether or not to use the GPU
self.use_cuda = use_cuda
if (self.use_cuda==True) and (cuda_installed==False) :
warnings.warn(
'Cuda not available for the fields.\n'
'Performing the field operations on the CPU.' )
self.use_cuda = False
# Register the current correction type
if current_correction in ['curl-free', 'cross-deposition']:
self.current_correction = current_correction
else:
raise ValueError('Unkown current correction:%s'%current_correction)
# Create the list of the transformers, which convert the fields
# back and forth between the spatial and spectral grid
# (one object per azimuthal mode)
self.trans = []
for m in range(Nm) :
Number of grid points along the r axis (axis -1)
Nz: int
Number of grid points along the z axis (axis 0)
use_cuda: bool, optional
Whether to perform the Fourier transform on the z axis
nthreads : int, optional
Number of threads for the FFTW transform.
If None, the default number of threads of numba is used
(environment variable NUMBA_NUM_THREADS)
"""
# Check whether to use cuda
self.use_cuda = use_cuda
if (self.use_cuda is True) and (cuda_installed is False) :
self.use_cuda = False
print('** Cuda not available for Fourier transform.')
print('** Performing the Fourier transform on the CPU.')
# Check whether to use MKL
self.use_mkl = mkl_installed
# Initialize the object for calculation on the GPU
if self.use_cuda:
# Initialize the dimension of the grid and blocks
self.dim_grid, self.dim_block = cuda_tpb_bpg_2d( Nz, Nr)
# Initialize 1d buffer for cufft
self.buffer1d_in = cuda.device_array(
(Nz*Nr,), dtype=np.complex128)
self.buffer1d_out = cuda.device_array(
Major features:
- The class reuses the existing methods of ParticleDiagnostic
as much as possible through class inheritance
- The class implements memory buffering of the slices, so as
not to write to disk at every timestep
"""
import os
import math
import numpy as np
from scipy.constants import c, e
from .particle_diag import ParticleDiagnostic
# Check if CUDA is available, then import CUDA functions
from fbpic.utils.cuda import cuda_installed
if cuda_installed:
from .cuda_methods import extract_slice_from_gpu
class BackTransformedParticleDiagnostic(ParticleDiagnostic):
"""
Class that writes the particles *in the lab frame*,
from a simulation in the boosted frame
Particles are extracted from the simulation in slices each time step
and buffered in memory before writing to disk. On the CPU, slices of
particles are directly selected from the particle arrays of the species.
On the GPU, first particles within an area of cells surrounding the
output planes are extracted from the GPU particle arrays and stored in
a smaller GPU array, which is then copied to the CPU for selection.
The mechanism of extracting the particles within the outputplane-area
on the GPU relies on particle arrays being sorted on the GPU. For the
back-transformation to the Lab frame, interpolation in space is applied,