aryafallahi / mithra

MITHRA is a full-wave numerical solver for free-electron lasers.
17 stars 7 forks source link

[Feature Request] Warn when particles leave the domain #8

Closed arnaualba closed 4 years ago

arnaualba commented 4 years ago

Particles outside of the computational domain move in a straight line without feeling any fields, but are not deleted, and the user is given no warning.

I think that a warning should be issued to let the user know that some particles are out of the domain. It would be useful to know in which direction, such that one knows whether to increase the mesh along x, y or z, and maybe (if not too computationally demanding) the number of particles that are out of the domain

aryafallahi commented 4 years ago

I think a warning shall be written only if x and y coordinates are out of the computational range. z coordinate can be out and later particles jump in the domain.

arnaualba commented 4 years ago

I agree

arnaualba commented 4 years ago

I propose to add the warning in bunchUpdate(). In bunchUpdate() the position of the particle within the bounds is checked:

    for (iter = iterQB_; iter != iterQE_; ++iter )
      {
    /* If the particle does not belong to this processor continue the loop over particles           */
    if ( !( ( iter->rnp[2] < zp_[1] || rank_ == size_ - 1 ) && ( iter->rnp[2] >= zp_[0] || rank_ == 0 ) ) ) continue;

    /* Get the boolean flag determining if the particle resides in the computational domain.        */
    ubp.b1 = ( iter->rnp[0] < xmax_ - ub_.dx && iter->rnp[0] > xmin_ + ub_.dx &&
        iter->rnp[1] < ymax_ - ub_.dy && iter->rnp[1] > ymin_ + ub_.dy &&
        iter->rnp[2] < zp_[1]         && iter->rnp[2] >= zp_[0] );

The first if checks the z-coordinate, while the second statement checks for the x, y, and z-coordinates. This is is an extra unnecessary check.

I propose to change the second statement to

    /* Get the boolean flag determining if the particle resides in the computational domain.        */
    ubp.b1 = ( iter->rnp[0] < xmax_ - ub_.dx && iter->rnp[0] > xmin_ + ub_.dx &&
        iter->rnp[1] < ymax_ - ub_.dy && iter->rnp[1] > ymin_ + ub_.dy  );

And if ubp.b1 is false then the warning is emitted that some particles are out of the domain.

Do you agree? Or maybe there should be a separate flag that gets activated only once per time step, such taht the message is only written once.

aryafallahi commented 4 years ago

The variable ubp.b1 is not extra. This check is needed to make sure that the radiation field is calculated only if the particle is inside the computational domain.

I have revised this section and tested. Now, the warning appears if the transverse size is so big that particles go out of the computational domain. I have tested it with my FEL example, it was working fine.