Fix division by 0 for empty simulations and particle allocation in flattop
Two problems that this merge should fix:
1. Flattop does not allocate particle correctly.
Problem: When running generateParticles(...)
, pc_m->setLocalNum(0);
is not enough to reset the particle count, since it does not recalculate totalNum_m
in ippl::ParticleBase
.
Solution: Call ParticleBase::destroy(...)
. When called with localNum_m
(so ALL particles are destroyed), it simply resets the counts without additional computation (exactly what we want).
Alternative: Create a setter for totalNum_m
, but this would require to change ippl.
DistributionMoments
class has problem for empty simulations.
2. Problem: For empty simulations (Np = 0
), it will divide by 0 when averaging over all particles. This leads to some values being nan which should be 0 instead.
Solution: Simply do something like Np = (Np == 0) ? 1 : Np;
when necessary to divide by 1 instead of 0 whenever moments are calculated.