File indexing completed on 2026-12-01 10:04:56
0001
0002
0003
0004
0005
0006
0007 #pragma once
0008
0009 #include "corecel/math/Atomics.hh"
0010 #include "corecel/random/engine/RngEngine.hh"
0011 #include "corecel/sys/ThreadId.hh"
0012 #include "geocel/AllVolumesView.hh"
0013 #include "celeritas/geo/CoreGeoTrackView.hh"
0014 #include "celeritas/geo/GeoMaterialView.hh"
0015 #include "celeritas/mat/MaterialTrackView.hh"
0016 #include "celeritas/phys/CutoffView.hh"
0017 #include "celeritas/phys/ParticleTrackView.hh"
0018 #include "celeritas/phys/PhysicsStepView.hh"
0019 #include "celeritas/phys/PhysicsTrackView.hh"
0020 #include "celeritas/track/SimTrackView.hh"
0021
0022 #include "CoreTrackData.hh"
0023
0024 #if !CELER_DEVICE_COMPILE
0025 # include "corecel/io/Logger.hh"
0026 #endif
0027
0028 namespace celeritas
0029 {
0030
0031
0032
0033
0034
0035
0036 class CoreTrackView
0037 {
0038 public:
0039
0040
0041 using ParamsRef = NativeCRef<CoreParamsData>;
0042 using StateRef = NativeRef<CoreStateData>;
0043
0044
0045 public:
0046
0047 inline CELER_FUNCTION CoreTrackView(
0048 ParamsRef const& params, StateRef const& states, ThreadId thread);
0049
0050
0051 inline CELER_FUNCTION CoreTrackView(
0052 ParamsRef const& params, StateRef const& states, TrackSlotId slot);
0053
0054
0055 inline CELER_FUNCTION CoreTrackView& operator=(TrackInitializer const&);
0056
0057
0058 inline CELER_FUNCTION SimTrackView sim() const;
0059
0060
0061 inline CELER_FUNCTION GeoTrackView geometry() const;
0062
0063
0064 inline CELER_FUNCTION GeoMaterialView geo_material() const;
0065
0066
0067 inline CELER_FUNCTION MaterialTrackView material() const;
0068
0069
0070 inline CELER_FUNCTION ParticleTrackView particle() const;
0071
0072
0073 inline CELER_FUNCTION ParticleView particle_record(ParticleId) const;
0074
0075
0076 inline CELER_FUNCTION CutoffView cutoff() const;
0077
0078
0079 inline CELER_FUNCTION PhysicsTrackView physics() const;
0080
0081
0082 inline CELER_FUNCTION PhysicsStepView physics_step() const;
0083
0084
0085 inline CELER_FUNCTION RngEngine rng() const;
0086
0087
0088 inline CELER_FUNCTION ThreadId thread_id() const;
0089
0090
0091 inline CELER_FUNCTION TrackSlotId track_slot_id() const;
0092
0093
0094 inline CELER_FUNCTION ActionId boundary_action() const;
0095
0096
0097 inline CELER_FUNCTION ActionId propagation_limit_action() const;
0098
0099
0100 inline CELER_FUNCTION ActionId tracking_cut_action() const;
0101
0102
0103 inline CELER_FUNCTION CoreScalars const& core_scalars() const;
0104
0105
0106 inline CELER_FUNCTION AllVolumesView volumes() const;
0107
0108
0109
0110
0111 inline CELER_FUNCTION void apply_errored();
0112
0113
0114 inline CELER_FUNCTION void apply_cut();
0115
0116
0117 inline CELER_FUNCTION CoreStateCounters& counters();
0118 inline CELER_FUNCTION CoreStateCounters const& counters() const;
0119
0120 private:
0121 ParamsRef const& params_;
0122 StateRef const& states_;
0123 ThreadId const thread_id_;
0124 TrackSlotId track_slot_id_;
0125 };
0126
0127
0128
0129
0130
0131
0132
0133 CELER_FUNCTION CoreTrackView::CoreTrackView(
0134 ParamsRef const& params, StateRef const& states, ThreadId thread)
0135 : params_(params), states_(states), thread_id_(thread)
0136 {
0137 CELER_EXPECT(states_.track_slots.empty()
0138 || thread_id_ < states_.track_slots.size());
0139 track_slot_id_ = TrackSlotId{states_.track_slots.empty()
0140 ? thread_id_.get()
0141 : states_.track_slots[thread_id_]};
0142 CELER_ENSURE(track_slot_id_ < states_.size());
0143 }
0144
0145
0146
0147
0148
0149
0150
0151
0152 CELER_FUNCTION CoreTrackView::CoreTrackView(
0153 ParamsRef const& params, StateRef const& states, TrackSlotId track_slot)
0154 : params_(params), states_(states), track_slot_id_(track_slot)
0155 {
0156 CELER_EXPECT(track_slot_id_ < states_.size());
0157 }
0158
0159
0160
0161
0162
0163 CELER_FUNCTION CoreTrackView& CoreTrackView::operator=(
0164 TrackInitializer const& init)
0165 {
0166 CELER_EXPECT(init);
0167
0168
0169 this->sim() = init.sim;
0170
0171
0172 if constexpr (CELERITAS_RESEED == CELERITAS_RESEED_TRACK)
0173 {
0174 this->rng() = init.rng;
0175 }
0176
0177
0178 this->particle() = init.particle;
0179
0180
0181 auto geo = this->geometry();
0182 geo = init.geo;
0183 if (CELER_UNLIKELY(geo.failed() || geo.is_outside()))
0184 {
0185 #if !CELER_DEVICE_COMPILE
0186 if (!geo.failed())
0187 {
0188
0189
0190 CELER_LOG_LOCAL(error) << "Track " << this->track_slot_id().get()
0191 << " started outside the geometry";
0192 }
0193 else
0194 {
0195
0196
0197 }
0198 #endif
0199 this->apply_errored();
0200 return *this;
0201 }
0202
0203
0204 auto matid = this->geo_material().material_id(geo.impl_volume_id());
0205 if (CELER_UNLIKELY(!matid))
0206 {
0207 #if !CELER_DEVICE_COMPILE
0208 CELER_LOG_LOCAL(error) << "Track " << this->track_slot_id().get()
0209 << " started in an unknown material";
0210 #endif
0211 this->apply_errored();
0212 return *this;
0213 }
0214 this->material() = {matid};
0215
0216
0217 this->physics() = {};
0218
0219 return *this;
0220 }
0221
0222
0223
0224
0225
0226 CELER_FUNCTION SimTrackView CoreTrackView::sim() const
0227 {
0228 return SimTrackView{params_.sim, states_.sim, this->track_slot_id()};
0229 }
0230
0231
0232
0233
0234
0235 CELER_FUNCTION auto CoreTrackView::geometry() const -> GeoTrackView
0236 {
0237 return GeoTrackView{
0238 params_.geometry, states_.geometry, this->track_slot_id()};
0239 }
0240
0241
0242
0243
0244
0245 CELER_FUNCTION auto CoreTrackView::geo_material() const -> GeoMaterialView
0246 {
0247 return GeoMaterialView{params_.geo_mats};
0248 }
0249
0250
0251
0252
0253
0254 CELER_FUNCTION auto CoreTrackView::material() const -> MaterialTrackView
0255 {
0256 return MaterialTrackView{
0257 params_.materials, states_.materials, this->track_slot_id()};
0258 }
0259
0260
0261
0262
0263
0264 CELER_FUNCTION auto CoreTrackView::particle() const -> ParticleTrackView
0265 {
0266 return ParticleTrackView{
0267 params_.particles, states_.particles, this->track_slot_id()};
0268 }
0269
0270
0271
0272
0273
0274 CELER_FUNCTION auto CoreTrackView::particle_record(ParticleId pid) const
0275 -> ParticleView
0276 {
0277 return ParticleView{params_.particles, pid};
0278 }
0279
0280
0281
0282
0283
0284 CELER_FUNCTION auto CoreTrackView::cutoff() const -> CutoffView
0285 {
0286 PhysMatId mat_id = this->material().material_id();
0287 CELER_ASSERT(mat_id);
0288 return CutoffView{params_.cutoffs, mat_id};
0289 }
0290
0291
0292
0293
0294
0295 CELER_FUNCTION auto CoreTrackView::physics() const -> PhysicsTrackView
0296 {
0297 PhysMatId mat_id = this->material().material_id();
0298 CELER_ASSERT(mat_id);
0299 auto par = this->particle();
0300 return PhysicsTrackView{
0301 params_.physics, states_.physics, par, mat_id, this->track_slot_id()};
0302 }
0303
0304
0305
0306
0307
0308 CELER_FUNCTION auto CoreTrackView::physics_step() const -> PhysicsStepView
0309 {
0310 return PhysicsStepView{
0311 params_.physics, states_.physics, this->track_slot_id()};
0312 }
0313
0314
0315
0316
0317
0318 CELER_FUNCTION auto CoreTrackView::rng() const -> RngEngine
0319 {
0320 return RngEngine{params_.rng, states_.rng, this->track_slot_id()};
0321 }
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335 CELER_FORCEINLINE_FUNCTION ThreadId CoreTrackView::thread_id() const
0336 {
0337 CELER_EXPECT(thread_id_);
0338 return thread_id_;
0339 }
0340
0341
0342
0343
0344
0345 CELER_FORCEINLINE_FUNCTION TrackSlotId CoreTrackView::track_slot_id() const
0346 {
0347 return track_slot_id_;
0348 }
0349
0350
0351
0352
0353
0354 CELER_FUNCTION ActionId CoreTrackView::boundary_action() const
0355 {
0356 return params_.scalars.boundary_action;
0357 }
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368 CELER_FUNCTION ActionId CoreTrackView::propagation_limit_action() const
0369 {
0370 return params_.scalars.propagation_limit_action;
0371 }
0372
0373
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383 CELER_FUNCTION ActionId CoreTrackView::tracking_cut_action() const
0384 {
0385 return params_.scalars.tracking_cut_action;
0386 }
0387
0388
0389
0390
0391
0392
0393
0394
0395 CELER_FUNCTION CoreScalars const& CoreTrackView::core_scalars() const
0396 {
0397 return params_.scalars;
0398 }
0399
0400
0401
0402
0403
0404 CELER_FUNCTION AllVolumesView CoreTrackView::volumes() const
0405 {
0406 return AllVolumesView{params_.volumes};
0407 }
0408
0409
0410
0411
0412
0413 CELER_FUNCTION CoreStateCounters& CoreTrackView::counters()
0414 {
0415 return *states_.init.counters.data();
0416 }
0417
0418
0419
0420 CELER_FUNCTION CoreStateCounters const& CoreTrackView::counters() const
0421 {
0422 return *states_.init.counters.data();
0423 }
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434 CELER_FUNCTION void CoreTrackView::apply_errored()
0435 {
0436 using namespace celeritas::literals;
0437 auto sim = this->sim();
0438 CELER_EXPECT(is_track_valid(sim.status()));
0439 sim.status(TrackStatus::errored);
0440 sim.along_step_action({});
0441 sim.post_step_action(this->tracking_cut_action());
0442 atomic_add(&this->counters().num_errored, 1_sz);
0443 }
0444
0445
0446
0447
0448
0449 CELER_FUNCTION void CoreTrackView::apply_cut()
0450 {
0451 using namespace celeritas::literals;
0452 auto sim = this->sim();
0453 CELER_EXPECT(is_track_valid(sim.status()));
0454 sim.post_step_action(this->tracking_cut_action());
0455 atomic_add(&this->counters().num_cut, 1_sz);
0456 }
0457
0458
0459 }