celeritas icon indicating copy to clipboard operation
celeritas copied to clipboard

Use safety distance more wisely and improve cacheing of next step/safety

Open sethrj opened this issue 2 years ago • 2 comments

Add maximum limit to safety

If the safety is "large enough" we don't have to recalculate it. So, like find_next_step, we should add a maximum search distance to the safety calculation. (For VecGeom we'll have to modify the BVH navigator slightly.)

MSC step limit

  • If safety > max_step (where max_step is the dedx_range transformed by a heuristic) then we don't have to use it further: we skip displacement.
  • If the safety is less than the maximum range, it may reduce the MSC step limit

It makes sense that we don't need to use the safety distance if the maximum physics step is less than that distance. We could probably further reduce the bounds on the safety (or make it more accurate?) by using MscStepToGeo and comparing that.

Action: Calculate safety with max step = dedx_range.

Field propagation

  • If the overall step is less than the safety we don't have to update.
  • If substep.step is less than the safety we don't have to update.
  • If the chord is greater than the safety after updating then we have to intersect.

MSC scatter

  • If the displacement is less than some tiny constant we don't need safety.
  • If the safety is less than some small constant (i.e., the position is known to be close to an true geometry edge) then we don't need it
  • If the safety is larger than the maximum displacement we don't need it.

The maximum displacement is $0.73 * (\textrm{true path}^2 - \textrm{geom path}^2)$ which could be calculated outside the class.

Action: Calculate displacement first; if it's greater than the safety then update it.

Remove local cacheing in geometries

  • Rename find_next_step to calc_next_step.
  • Rename find_safety to calc_safety.
  • Delete conditionals on max_step being closer than next_step.

Add improved caching

Straight-line next step will only need to be cached if we're propagating in a field and have stop conditions other than geometry/interaction (e.g. electrons not in a field).

  • Safety distance only needs to be cached for charged particles if using MSC or field propagation.
  • Straight line distance only needs caching for charged particles if no field, and even then MSC scatters at basically every step so we don't even gain from that.
  • Instead of caching just the safety distance, cache a safety sphere so that it doesn't need to reset due to MSC
  • Reset cache by comparing square of max requested to square of distance to safety sphere boundary (sum of squares of difference in position)

Geant4 geometry calculates safety automatically as part of calc_next_step whereas ORANGE/VecGeom calculate only on request.

Always update safety at beginning of step

Initialized charged particles with a safety of zero. Use a placeholder (-1? nan? some other sentinel/bit flag?) for flagging tracks that do not need the safety distance.

For charged particles, always recalculate the safety after pre-step if the cached distance is less than the dedx_range. This takes care of the MSC requirement, though we should have an assertion in there (safety > dedx_range or safety_pos == pos). If Geant4, this also simultaneously calculates the next step length along the current direction, though it's not clear if that matters.

Then, to get the correct behavior for the scatter, we can update the safety distance if the cached version is less than the physics step (which is a generous upper bound for the maximum displacement needed by MSC).

Add safety to field propagator

Most interior steps (range limited) will not come close to the boundary, so we could have a separate field propagation loop that only uses the existing safety distance and does not interact with the geometry. A separate kernel/propagator could do "propagate to boundary of safety sphere" and then we could incorporate the safety distance + intersect into a second kernel.

sethrj avatar May 16 '23 19:05 sethrj

To implement next

Safety cache

  • update_safety(max safety)
  • safety
  • move_internal
  • move_to_boundary
  • set_dir

Not needed for our immediate applications

Linear step cache

  • update_next_step()
  • update_next_step(max step)
  • move_to_boundary
  • set_dir

sethrj avatar May 16 '23 19:05 sethrj

Removed next-distance cacheing in #776

sethrj avatar Jul 07 '23 11:07 sethrj