Skip to content

Commit ff8b73f

Browse files
Mini-PR: Improve Python particle attribute access CI test (#2581)
* updated the Python particle attribute access CI test * additional fixes needed for tests to pass * changes requested by Axel during code review * Apply suggestions from code review Co-authored-by: Axel Huebl <axel.huebl@plasma.ninja>
1 parent 430c972 commit ff8b73f

File tree

3 files changed

+41
-29
lines changed

3 files changed

+41
-29
lines changed

Examples/Tests/ParticleDataPython/PICMI_inputs_2d.py

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
from mpi4py import MPI
21
from pywarpx import picmi
32
import numpy as np
43

5-
##########################
6-
# MPI communicator setup
7-
##########################
4+
import argparse
5+
import sys
86

9-
# split processor 0 into separate communicator from others
10-
comm_world = MPI.COMM_WORLD
11-
rank = comm_world.Get_rank()
12-
if rank == 0:
13-
color = 0
14-
else:
15-
color = 1
16-
new_comm = comm_world.Split(color)
7+
# Create the parser and add the argument
8+
parser = argparse.ArgumentParser()
9+
parser.add_argument(
10+
'-u', '--unique', action='store_true',
11+
help="Whether injected particles should be treated as unique"
12+
)
13+
14+
# Parse the input
15+
args, left = parser.parse_known_args()
16+
sys.argv = sys.argv[:1] + left
1717

1818
##########################
1919
# numerics parameters
@@ -75,7 +75,7 @@
7575
period = 10,
7676
data_list = ['phi'],
7777
write_dir = '.',
78-
warpx_file_prefix = f'Python_particle_attr_access_plt_{color}'
78+
warpx_file_prefix = f"Python_particle_attr_access_{'unique_' if args.unique else ''}plt"
7979
)
8080

8181
##########################
@@ -98,7 +98,7 @@
9898
sim.add_diagnostic(field_diag)
9999

100100
sim.initialize_inputs()
101-
sim.initialize_warpx(mpi_comm=new_comm)
101+
sim.initialize_warpx()
102102

103103
##########################
104104
# python particle data access
@@ -108,9 +108,11 @@
108108

109109
_libwarpx.add_real_comp('electrons', 'newPid')
110110

111+
my_id = _libwarpx.libwarpx.warpx_getMyProc()
112+
111113
def add_particles():
112114

113-
nps = 10
115+
nps = 10 * (my_id + 1)
114116
x = np.random.rand(nps) * 0.03
115117
y = np.zeros(nps)
116118
z = np.random.random(nps) * 0.03
@@ -122,7 +124,7 @@ def add_particles():
122124

123125
_libwarpx.add_particles(
124126
species_name='electrons', x=x, y=y, z=z, ux=ux, uy=uy, uz=uz,
125-
w=w, newPid=newPid, unique_particles=(not color)
127+
w=w, newPid=newPid, unique_particles=args.unique
126128
)
127129

128130
callbacks.installbeforestep(add_particles)
@@ -131,16 +133,14 @@ def add_particles():
131133
# simulation run
132134
##########################
133135

134-
sim.step(max_steps - 1, mpi_comm=new_comm)
136+
sim.step(max_steps - 1)
135137

136138
##########################
137-
# check that the new PIDs are properly set
139+
# check that the new PIDs
140+
# are properly set
138141
##########################
139142

140-
if color == 0:
141-
assert (_libwarpx.get_particle_count('electrons') == 90)
142-
else:
143-
assert (_libwarpx.get_particle_count('electrons') == 90)
143+
assert (_libwarpx.get_particle_count('electrons') == 270 / (2 - args.unique))
144144
assert (_libwarpx.get_particle_comp_index('electrons', 'w') == 0)
145145
assert (_libwarpx.get_particle_comp_index('electrons', 'newPid') == 4)
146146

Examples/Tests/ParticleDataPython/analysis.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@
77
# This script just checks that the PICMI file executed successfully.
88
# If it did there will be a plotfile for the final step.
99

10-
import yt
10+
import sys
1111

12-
plotfile = 'Python_particle_attr_access_plt_000010'
13-
ds = yt.load( plotfile ) # noqa
12+
step = int(sys.argv[1][-5:])
1413

15-
plotfile = 'Python_particle_attr_access_plt_100010'
16-
ds = yt.load( plotfile ) # noqa
17-
18-
assert True
14+
assert step == 10

Regression/WarpX-tests.ini

+16
Original file line numberDiff line numberDiff line change
@@ -2619,6 +2619,22 @@ compileTest = 0
26192619
doVis = 0
26202620
analysisRoutine = Examples/Tests/ParticleDataPython/analysis.py
26212621

2622+
[Python_particle_attr_access_unique]
2623+
buildDir = .
2624+
inputFile = Examples/Tests/ParticleDataPython/PICMI_inputs_2d.py
2625+
runtime_params =
2626+
customRunCmd = python PICMI_inputs_2d.py --unique
2627+
dim = 2
2628+
addToCompileString = USE_PYTHON_MAIN=TRUE PYINSTALLOPTIONS="--user --prefix="
2629+
restartTest = 0
2630+
useMPI = 1
2631+
numprocs = 2
2632+
useOMP = 1
2633+
numthreads = 1
2634+
compileTest = 0
2635+
doVis = 0
2636+
analysisRoutine = Examples/Tests/ParticleDataPython/analysis.py
2637+
26222638
[Python_prev_positions]
26232639
buildDir = .
26242640
inputFile = Examples/Tests/ParticleDataPython/PICMI_inputs_prev_pos_2d.py

0 commit comments

Comments
 (0)