Mean bunch momentum == 0 leads to "nan" position values during transformation
When the bunch is empty or has only newly sampled particles (which happens e.g. during the first timestep when sampling with flattop), the mean velocity is always exactly 0. This has a few effects leading a nan rotation matrix that propagates through the functions until positions are all nan.
Here is the program trace:
- The loop in
ParallelTracker::executecallscomputeSpaceChargeFields. - Printing
Quaternion alignment = ...outputs "( -nan , -nan , -nan , -nan )". - The variable is generated with
getQuaternion(itsBunch_m->get_pmean(), Vector_t<double, 3>(0, 0, 1)). - Inside
getQuaternion, we have the lineu = normalize(u);, whereuis the first argument of the call, so equal toitsBunch_m->get_pmean(). -
normalizecallsvec / length, where in formeanP == 0, we getlength = 0\rightarrow this makesu == nan. - This propagates through the
getQuaternionfunction and makesalignmentequal tonan. - This initializes a CoordinateSystemTrafo object with a
nanQuaternion instance, from which it gets thenanrotationMatrix_mmember. - This
nanrotation matrix is copied to aKokkos::Viewthat is used to transformbunch_m->R, resulting in onlynanposition values. - Then, the rotation itself is performed like this:
Rview(j) = Rot(i,j) * x(i);. This seems to be wrong, sinceRviewis aKokkos::Viewofippl::Vectorelements, it would need to be something likeRview[particles_index](j) = Rot(i,j) * x(i);, however the indexiis used twice. Once in theKokkos::parallel_forand once in the loop over dimensions inside the kernel. - Finally, as soon as there is any sort of overallocation,
ippl::getRangePolicyiterates in my opinion over ALL elements of the view, not over all initialized elements of the container. This is becauseview.size()insidegetRangePolicyreturns the size of the view, not the particle container. - Something similar happens to the
originafter callingbeamToReferenceCSTrafo.inverted();. More specific: the issue arises whenCoordinateSystemTrafo::invert()rotates the origin on ananorientation vector.
So what is the underlying issue?
The issue seems to four-fold:
- The
Quaternionclass returnsnan, might need to check for0length. -
Rviewis indexed in the wrong way (and alsoiis used twice as a variable). - The transformation kernel iterates over ALL "particle memory", not just the allocated particles in the container (however, this should not change values itself).
- After inverting the
beamToReferenceCSTrafo, the origin evaluates tonan.