Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:31:31

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2020-2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file celeritas/track/detail/LocateAliveExecutor.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include "corecel/Assert.hh"
0011 #include "corecel/Macros.hh"
0012 #include "corecel/cont/Span.hh"
0013 #include "celeritas/Types.hh"
0014 #include "celeritas/global/CoreTrackData.hh"
0015 #include "celeritas/phys/PhysicsStepView.hh"
0016 #include "celeritas/phys/Secondary.hh"
0017 
0018 #include "Utils.hh"
0019 #include "../SimTrackView.hh"
0020 
0021 namespace celeritas
0022 {
0023 namespace detail
0024 {
0025 //---------------------------------------------------------------------------//
0026 /*!
0027  * Determine which tracks are alive and count secondaries.
0028  *
0029  * This finds empty slots in the track vector and counts the number of
0030  * secondaries created in each interaction. If the track was killed and
0031  * produced secondaries, the empty track slot is filled with the first
0032  * secondary.
0033  */
0034 struct LocateAliveExecutor
0035 {
0036     //// TYPES ////
0037 
0038     using ParamsPtr = CRefPtr<CoreParamsData, MemSpace::native>;
0039     using StatePtr = RefPtr<CoreStateData, MemSpace::native>;
0040 
0041     //// DATA ////
0042 
0043     ParamsPtr params;
0044     StatePtr state;
0045 
0046     //// FUNCTIONS ////
0047 
0048     // Determine which tracks are alive and count secondaries
0049     inline CELER_FUNCTION void operator()(TrackSlotId tid) const;
0050 
0051     CELER_FORCEINLINE_FUNCTION void operator()(ThreadId tid) const
0052     {
0053         // The grid size should be equal to the state size and no thread/slot
0054         // remapping should be performed
0055         return (*this)(TrackSlotId{tid.unchecked_get()});
0056     }
0057 };
0058 
0059 //---------------------------------------------------------------------------//
0060 CELER_FUNCTION void LocateAliveExecutor::operator()(TrackSlotId tid) const
0061 {
0062     CELER_EXPECT(tid < state->size());
0063 
0064     // Count the number of secondaries produced by each track
0065     size_type num_secondaries{0};
0066     SimTrackView sim(params->sim, state->sim, tid);
0067 
0068     if (sim.status() != TrackStatus::inactive)
0069     {
0070         PhysicsStepView phys(params->physics, state->physics, tid);
0071         for (auto const& secondary : phys.secondaries())
0072         {
0073             if (secondary)
0074             {
0075                 ++num_secondaries;
0076             }
0077         }
0078     }
0079 
0080     state->init.vacancies[tid] = [&] {
0081         if (sim.status() == TrackStatus::alive)
0082         {
0083             // The track is alive: mark this track slot as occupied
0084             return occupied();
0085         }
0086         else if (num_secondaries > 0
0087                  && params->init.track_order != TrackOrder::init_charge)
0088         {
0089             // The track was killed and produced secondaries: in this case, the
0090             // empty track slot will be filled with the first secondary. Mark
0091             // this slot as occupied even though the secondary has not been
0092             // initialized in it yet, and don't include the first secondary in
0093             // the count
0094             --num_secondaries;
0095             return occupied();
0096         }
0097         else
0098         {
0099             // The track is inactive/killed and did not produce secondaries:
0100             // store the index so it can be used later to initialize a new
0101             // track
0102             return tid;
0103         }
0104     }();
0105     state->init.secondary_counts[tid] = num_secondaries;
0106 }
0107 
0108 //---------------------------------------------------------------------------//
0109 }  // namespace detail
0110 }  // namespace celeritas