Skip to content

Commit

Permalink
Add Python interface and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RemiLehe committed Jun 25, 2024
1 parent c7be0ab commit 1bcf7b6
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 3 deletions.
140 changes: 140 additions & 0 deletions Examples/Tests/LoadExternalField/PICMI_inputs_3d_particle_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#!/usr/bin/env python3
#
# --- Input file for loading initial field from openPMD file.

from pywarpx import picmi

constants = picmi.constants

#################################
####### GENERAL PARAMETERS ######
#################################

max_steps = 300

max_grid_size = 40
nx = max_grid_size
ny = max_grid_size
nz = max_grid_size

xmin = -1
xmax = 1
ymin = xmin
ymax = xmax
zmin = 0
zmax = 5

number_per_cell = 200

#################################
############ NUMERICS ###########
#################################

verbose = 1
dt = 4.4e-7
use_filter = 0

# Order of particle shape factors
particle_shape = 1

#################################
############ PLASMA #############
#################################

ion_dist = picmi.ParticleListDistribution(
x=0.0,
y=0.2,
z=2.5,
ux=9.5e-05*constants.c,
uy=0.0*constants.c,
uz=1.34e-4*constants.c,
weight=1.0
)

ions = picmi.Species(
particle_type='H',
name='proton', charge='q_e',mass="m_p",
warpx_do_not_deposit=1,
initial_distribution=ion_dist
)

#################################
######## INITIAL FIELD ##########
#################################

applied_field = picmi.LoadAppliedField(
read_fields_from_path="../../../../openPMD-example-datasets/example-femm-3d.h5",
load_E=False
)

#################################
###### GRID AND SOLVER ##########
#################################

grid = picmi.Cartesian3DGrid(
number_of_cells=[nx, ny, nz],
warpx_max_grid_size=max_grid_size,
lower_bound=[xmin, ymin, zmin],
upper_bound=[xmax, ymax, zmax],
lower_boundary_conditions=['dirichlet', 'dirichlet', 'dirichlet'],
upper_boundary_conditions=['dirichlet', 'dirichlet', 'dirichlet'],
lower_boundary_conditions_particles=['absorbing', 'absorbing', 'absorbing'],
upper_boundary_conditions_particles=['absorbing', 'absorbing', 'absorbing']
)
solver = picmi.ElectrostaticSolver(grid=grid)

#################################
######### DIAGNOSTICS ###########
#################################

particle_diag = picmi.ParticleDiagnostic(
name='diag1',
period=300,
species=[ions],
data_list = ['ux', 'uy', 'uz', 'x', 'y', 'z', 'weighting'],
write_dir='.',
warpx_file_prefix='Python_LoadExternalField3D_plt'
)
field_diag = picmi.FieldDiagnostic(
name='diag1',
grid=grid,
period=300,
data_list = ['Bx', 'By', 'Bz', 'Ex', 'Ey', 'Ez', 'Jx', 'Jy', 'Jz'],
write_dir='.',
warpx_file_prefix='Python_LoadExternalField3D_plt'
)

#################################
####### SIMULATION SETUP ########
#################################

sim = picmi.Simulation(
solver=solver,
max_steps=max_steps,
verbose=verbose,
warpx_serialize_initial_conditions=False,
warpx_grid_type='collocated',
warpx_do_dynamic_scheduling=False,
warpx_use_filter=use_filter,
time_step_size=dt,
particle_shape=particle_shape
)

sim.add_applied_field(applied_field)

sim.add_species(
ions,
layout=picmi.PseudoRandomLayout(
n_macroparticles_per_cell=number_per_cell, grid=grid
)
)

sim.add_diagnostic(field_diag)
sim.add_diagnostic(particle_diag)

#################################
##### SIMULATION EXECUTION ######
#################################

#sim.write_input_file('PICMI_inputs_3d')
sim.step(max_steps)
7 changes: 7 additions & 0 deletions Python/pywarpx/picmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,13 @@ def applied_field_initialize_inputs(self):
expression = pywarpx.my_constants.mangle_expression(expression, self.mangle_dict)
pywarpx.warpx.__setattr__(f'B{sdir}_external_grid_function(x,y,z)', expression)

class LoadAppliedField(picmistandard.PICMI_LoadAppliedField):
def applied_field_initialize_inputs(self):
pywarpx.warpx.read_fields_from_path = self.read_fields_from_path
if self.load_E:
pywarpx.particles.E_ext_particle_init_style = 'read_from_file'
if self.load_B:
pywarpx.particles.B_ext_particle_init_style = 'read_from_file'

class ConstantAppliedField(picmistandard.PICMI_ConstantAppliedField):
def applied_field_initialize_inputs(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"lev=0": {
"Bx": 29.8448958859583,
"By": 29.844895885958305,
"Bz": 197.55124310523018,
"Ex": 0.0,
"Ey": 0.0,
"Ez": 0.0,
"jx": 0.0,
"jy": 0.0,
"jz": 0.0
},
"proton": {
"particle_momentum_x": 1.2118953253556959e-23,
"particle_momentum_y": 7.822512598009088e-23,
"particle_momentum_z": 2.2761898274347412e-23,
"particle_position_x": 0.12238072371983327,
"particle_position_y": 0.009653941154598592,
"particle_position_z": 4.317544769595657,
"particle_weight": 1.0
}
}
26 changes: 23 additions & 3 deletions Regression/WarpX-tests.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3467,11 +3467,31 @@ compileTest = 0
doVis = 0
analysisRoutine = Examples/analysis_default_openpmd_regression.py

[Python_LoadExternalField3D]
[Python_LoadExternalGridField3D]
buildDir = .
inputFile = Examples/Tests/LoadExternalField/PICMI_inputs_3d.py
inputFile = Examples/Tests/LoadExternalField/PICMI_inputs_3d_grid_fields.py
runtime_params =
customRunCmd = python3 PICMI_inputs_3d.py
customRunCmd = python3 PICMI_inputs_3d_grid_fields.py
dim = 3
addToCompileString = USE_PYTHON_MAIN=TRUE
cmakeSetupOpts = -DWarpX_DIMS=3 -DWarpX_APP=OFF -DWarpX_PYTHON=ON -DWarpX_OPENPMD=ON
target = pip_install
restartTest = 0
useMPI = 1
numprocs = 1
useOMP = 1
numthreads = 1
compileTest = 0
doVis = 0
compareParticles = 1
particleTypes = proton
analysisRoutine = Examples/Tests/LoadExternalField/analysis_3d.py

[Python_LoadExternalParticleField3D]
buildDir = .
inputFile = Examples/Tests/LoadExternalField/PICMI_inputs_3d_particle_fields.py
runtime_params =
customRunCmd = python3 PICMI_inputs_3d_particle_fields.py
dim = 3
addToCompileString = USE_PYTHON_MAIN=TRUE
cmakeSetupOpts = -DWarpX_DIMS=3 -DWarpX_APP=OFF -DWarpX_PYTHON=ON -DWarpX_OPENPMD=ON
Expand Down

0 comments on commit 1bcf7b6

Please sign in to comment.