Skip to content

Commit

Permalink
core: Reset type tracker on particle clear
Browse files Browse the repository at this point in the history
  • Loading branch information
jngrad committed Jan 10, 2023
1 parent 4b290de commit 8c8e6c0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/core/particle_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,12 @@ int get_particle_node(int p_id) {

void clear_particle_node() { particle_node.clear(); }

static void clear_particle_type_map() {
for (auto &kv : ::particle_type_map) {
kv.second.clear();
}
}

/**
* @brief Calculate the largest particle id.
* Traversing the @ref particle_node to find the largest particle id
Expand Down Expand Up @@ -404,6 +410,7 @@ REGISTER_CALLBACK(mpi_remove_all_particles_local)
void remove_all_particles() {
mpi_call_all(mpi_remove_all_particles_local);
clear_particle_node();
clear_particle_type_map();
}

void remove_particle(int p_id) {
Expand Down
9 changes: 8 additions & 1 deletion testsuite/python/reaction_methods_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def check_reaction_parameters(reactions, parameters):
else:
self.assertEqual(getattr(reaction, key), params[key])

def count_by_type(types):
return [self.system.number_of_particles(type=x) for x in types]

reaction_forward = {
'gamma': gamma,
'reactant_types': [5],
Expand Down Expand Up @@ -156,11 +159,15 @@ def check_reaction_parameters(reactions, parameters):
potential_energy = method.calculate_particle_insertion_potential_energy(
reaction_id=0)
self.assertEqual(potential_energy, 0.)
self.assertEqual(count_by_type([5, 2, 3, 0]), [1, 1, 1, 0])
method.delete_particle(p_id=p3.id)
self.assertEqual(count_by_type([5, 2, 3, 0]), [1, 1, 0, 0])
self.assertEqual(len(self.system.part), 2)
method.delete_particle(p_id=p1.id)
p1.remove()
self.assertEqual(count_by_type([5, 2, 3, 0]), [0, 1, 0, 0])
self.assertEqual(len(self.system.part), 1)
self.system.part.clear()
self.assertEqual(count_by_type([5, 2, 3, 0]), [0, 0, 0, 0])

# check reaction deletion
method.delete_reaction(reaction_id=0)
Expand Down

0 comments on commit 8c8e6c0

Please sign in to comment.