Particles get wrong charge if input file has different number of particles
Summary
When inserting a distribution FROMFILE
, if NPART
is different from the number of lines in the input file, the total charge is wrongly modified.
This is because the charge-per-particle is set as totalCharge / NPART
in Beam.cpp
separately from Distribution.cpp
, which simply gives an error:
if (numberOfDistParticles != numberOfParticles) {
*gmsg << "\n--------------------------------------------------" << endl
<< "Warning!! The number of particles in the initial" << endl
<< "distribution is " << numberOfDistParticles << "." << endl << endl
<< "This is different from the number of particles" << endl
<< "defined by the BEAM command: " << numberOfParticles << endl << endl
<< "This is often happens when using a FROMFILE type" << endl
<< "distribution and not matching the number of" << endl
<< "particles in the particles file(s) with the number" << endl
<< "given in the BEAM command." << endl << endl
<< "The number of particles in the initial distribution" << endl
<< "(" << numberOfDistParticles << ") "
<< "will take precedence." << endl
<< "---------------------------------------------------\n" << endl;
The simulation then continues, but you have more or less particles than NPART
, and hence the charge-per-particle is too high or too low, and the total charge is different that what the user specified
Steps to reproduce
For example if initialDistro.in
has 5e5 particles, and you use
// INITIAL DISTRIBUTION
Dist: DISTRIBUTION, TYPE = FROMFILE, FNAME = "initialDistro.in",
EMITTED = FALSE;
// Electron Beam Definition
REAL beam_bunch_charge = 2e-9;
BEAM1: BEAM, PARTICLE = ELECTRON, GAMMA = gamma, NPART = 1e6,
BFREQ = rf_freq, BCURRENT = beam_bunch_charge * rf_freq * 1e6 , CHARGE = -1;
then you will get a warning, and the simulation will continue with a total charge of 1 nC instead of the 2 nC that the user specified.
Possible fixes
I think the best solution is to simply output an error message and quit the simulation if NPART != particles_in_file
.
The user can either correct NPART
to be as in the file, or modify the file himself.