-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pandas:
ImpactXParticleContainer.to_df()
(#458)
* Pandas: `ImpactXParticleContainer.to_df()` Copy all particles into a `pandas.DataFrame`. Supports local and MPI-gathered results. * AMReX & pyAMReX: latest development X-ref: AMReX-Codes/pyamrex#220 * DataFrame: Add Test
- Loading branch information
Showing
6 changed files
with
182 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
This file is part of ImpactX | ||
Copyright 2023 ImpactX contributors | ||
Authors: Axel Huebl | ||
License: BSD-3-Clause-LBNL | ||
""" | ||
|
||
|
||
def ix_pc_to_df(self, local=True, comm=None, root_rank=0): | ||
""" | ||
Copy all particles into a pandas.DataFrame | ||
Parameters | ||
---------- | ||
self : amrex.ParticleContainer_* | ||
A ParticleContainer class in pyAMReX | ||
local : bool | ||
MPI-local particles | ||
comm : MPI Communicator | ||
if local is False, this defaults to mpi4py.MPI.COMM_WORLD | ||
root_rank : MPI root rank to gather to | ||
if local is False, this defaults to 0 | ||
Returns | ||
------- | ||
A concatenated pandas.DataFrame with particles from all levels. | ||
Returns None if no particles were found. | ||
If local=False, then all ranks but the root_rank will return None. | ||
""" | ||
df = super(type(self), self).to_df(local=local, comm=comm, root_rank=root_rank) | ||
|
||
# rename columns according to our attribute names | ||
if df is not None: | ||
# todo: check if currently in fixed s or fixed t and pick name accordingly | ||
|
||
names = [] | ||
for n in self.RealAoS.names_s: | ||
names.append(n) | ||
names.append("cpuid") | ||
for n in self.RealSoA.names_s: | ||
names.append(n) | ||
|
||
df.columns.values[0 : len(names)] = names | ||
|
||
# todo: also rename runtime attributes (e.g., "s_lost") | ||
# https://github.com/ECP-WarpX/impactx/pull/398 | ||
|
||
return df | ||
|
||
|
||
def register_ImpactXParticleContainer_extension(ixpc): | ||
"""ImpactXParticleContainer helper methods""" | ||
|
||
# register member functions for ImpactXParticleContainer | ||
ixpc.to_df = ix_pc_to_df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# Copyright 2022-2023 The ImpactX Community | ||
# | ||
# Authors: Axel Huebl | ||
# License: BSD-3-Clause-LBNL | ||
# | ||
# -*- coding: utf-8 -*- | ||
|
||
from impactx import ImpactX, RefPart, distribution, elements | ||
|
||
|
||
def test_df_pandas(): | ||
""" | ||
This tests using ImpactX and Pandas Dataframes | ||
""" | ||
sim = ImpactX() | ||
|
||
sim.particle_shape = 2 | ||
sim.slice_step_diagnostics = True | ||
sim.init_grids() | ||
|
||
# init particle beam | ||
kin_energy_MeV = 2.0e3 | ||
bunch_charge_C = 1.0e-9 | ||
npart = 10000 | ||
|
||
# reference particle | ||
pc = sim.particle_container() | ||
ref = pc.ref_particle() | ||
ref.set_charge_qe(-1.0).set_mass_MeV(0.510998950).set_kin_energy_MeV(kin_energy_MeV) | ||
|
||
# particle bunch | ||
distr = distribution.Waterbag( | ||
sigmaX=3.9984884770e-5, | ||
sigmaY=3.9984884770e-5, | ||
sigmaT=1.0e-3, | ||
sigmaPx=2.6623538760e-5, | ||
sigmaPy=2.6623538760e-5, | ||
sigmaPt=2.0e-3, | ||
muxpx=-0.846574929020762, | ||
muypy=0.846574929020762, | ||
mutpt=0.0, | ||
) | ||
sim.add_particles(bunch_charge_C, distr, npart) | ||
|
||
assert pc.TotalNumberOfParticles() == npart | ||
|
||
# init accelerator lattice | ||
fodo = [ | ||
elements.Drift(0.25), | ||
elements.Quad(1.0, 1.0), | ||
elements.Drift(0.5), | ||
elements.Quad(1.0, -1.0), | ||
elements.Drift(0.25), | ||
] | ||
sim.lattice.extend(fodo) | ||
|
||
# simulate | ||
sim.evolve() | ||
|
||
# compare number of global particles | ||
df = pc.to_df(local=False) | ||
if df is not None: | ||
assert npart == len(df) | ||
|
||
|
||
if __name__ == "__main__": | ||
test_df_pandas() |