Skip to content
Browse files

Py: Generate ids for bulk particle creation more efficiently (#3598)

Description of changes:
When adding many particles without specifying the ids
generate all new particle ids at once. This avoids n-1 calls to get_maximal_particle_id() which gets expensive for big systems (100k particles).
  • Loading branch information
kodiakhq committed Mar 26, 2020
2 parents 492040f + d4aac65 commit bf30a6cc3a4d6ef6ccb8c4caf71b19e322e7b36d
Showing with 11 additions and 5 deletions.
  1. +11 −5 src/python/espressomd/particle_data.pyx
@@ -154,7 +154,7 @@ cdef class ParticleHandle:

def __get__(self):
return make_array_locked(unfolded_position( < Vector3d > self.particle_data.r.p, < Vector3i > self.particle_data.l.i, box_geo.length()))
return make_array_locked(unfolded_position(< Vector3d > self.particle_data.r.p, < Vector3i > self.particle_data.l.i, box_geo.length()))

property pos_folded:
@@ -1862,15 +1862,21 @@ Set quat and scalar dipole moment (dipm) instead.")
raise ValueError(
"When adding several particles at once, all lists of attributes have to have the same size")

# Place new particles and collect ids
ids = []
# If particle ids haven't been provided, use free ones
# beyond the highest existing one
if not "id" in Ps:
first_id = get_maximal_particle_id() + 1
Ps["id"] = range(first_id, first_id + n_parts)

# Place the particles
for i in range(n_parts):
P = {}
for k in Ps:
P[k] = Ps[k][i]

return self[ids]
# Return slice of added particles
return self[Ps["id"]]

# Iteration over all existing particles
def __iter__(self):

0 comments on commit bf30a6c

Please sign in to comment.
You can’t perform that action at this time.