File indexing completed on 2025-02-22 10:31:29
0001
0002
0003
0004
0005
0006
0007
0008 #pragma once
0009
0010 #include <cmath>
0011
0012 #include "corecel/Assert.hh"
0013 #include "corecel/Macros.hh"
0014 #include "corecel/Types.hh"
0015 #include "corecel/math/Algorithms.hh"
0016 #include "corecel/sys/ThreadId.hh"
0017 #include "celeritas/Quantities.hh"
0018
0019 #include "ParticleData.hh"
0020 #include "ParticleView.hh"
0021
0022 namespace celeritas
0023 {
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 class ParticleTrackView
0036 {
0037 public:
0038
0039
0040 using ParticleParamsRef = celeritas::NativeCRef<ParticleParamsData>;
0041 using ParticleStateRef = celeritas::NativeRef<ParticleStateData>;
0042 using Energy = units::MevEnergy;
0043 using Initializer_t = ParticleTrackInitializer;
0044
0045
0046 public:
0047
0048 inline CELER_FUNCTION ParticleTrackView(ParticleParamsRef const& params,
0049 ParticleStateRef const& states,
0050 TrackSlotId id);
0051
0052
0053 inline CELER_FUNCTION ParticleTrackView&
0054 operator=(Initializer_t const& other);
0055
0056
0057 inline CELER_FUNCTION void energy(Energy);
0058
0059
0060 inline CELER_FUNCTION void subtract_energy(Energy);
0061
0062
0063
0064
0065 CELER_FORCEINLINE_FUNCTION ParticleId particle_id() const;
0066
0067
0068 CELER_FORCEINLINE_FUNCTION Energy energy() const;
0069
0070
0071 CELER_FORCEINLINE_FUNCTION bool is_stopped() const;
0072
0073
0074
0075 CELER_FORCEINLINE_FUNCTION ParticleView particle_view() const;
0076
0077
0078 CELER_FORCEINLINE_FUNCTION units::MevMass mass() const;
0079
0080
0081 CELER_FORCEINLINE_FUNCTION units::ElementaryCharge charge() const;
0082
0083
0084 CELER_FORCEINLINE_FUNCTION real_type decay_constant() const;
0085
0086
0087 CELER_FORCEINLINE_FUNCTION bool is_antiparticle() const;
0088
0089
0090 CELER_FORCEINLINE_FUNCTION bool is_stable() const;
0091
0092
0093
0094
0095 inline CELER_FUNCTION Energy total_energy() const;
0096
0097
0098 inline CELER_FUNCTION real_type beta_sq() const;
0099
0100
0101 inline CELER_FUNCTION units::LightSpeed speed() const;
0102
0103
0104 inline CELER_FUNCTION real_type lorentz_factor() const;
0105
0106
0107 inline CELER_FUNCTION units::MevMomentum momentum() const;
0108
0109
0110 inline CELER_FUNCTION units::MevMomentumSq momentum_sq() const;
0111
0112 private:
0113 ParticleParamsRef const& params_;
0114 ParticleStateRef const& states_;
0115 TrackSlotId const track_slot_;
0116 };
0117
0118
0119
0120
0121
0122
0123
0124 CELER_FUNCTION
0125 ParticleTrackView::ParticleTrackView(ParticleParamsRef const& params,
0126 ParticleStateRef const& states,
0127 TrackSlotId tid)
0128 : params_(params), states_(states), track_slot_(tid)
0129 {
0130 CELER_EXPECT(track_slot_ < states_.size());
0131 }
0132
0133
0134
0135
0136
0137 CELER_FUNCTION ParticleTrackView&
0138 ParticleTrackView::operator=(Initializer_t const& other)
0139 {
0140 CELER_EXPECT(other.particle_id < params_.size());
0141 CELER_EXPECT(other.energy >= zero_quantity());
0142 states_.particle_id[track_slot_] = other.particle_id;
0143 states_.particle_energy[track_slot_] = other.energy.value();
0144 return *this;
0145 }
0146
0147
0148
0149
0150
0151
0152
0153
0154 CELER_FUNCTION
0155 void ParticleTrackView::energy(Energy quantity)
0156 {
0157 CELER_EXPECT(this->particle_id());
0158 CELER_EXPECT(quantity >= zero_quantity());
0159 states_.particle_energy[track_slot_] = quantity.value();
0160 }
0161
0162
0163
0164
0165
0166 CELER_FUNCTION void ParticleTrackView::subtract_energy(Energy eloss)
0167 {
0168 CELER_EXPECT(eloss >= zero_quantity());
0169 CELER_EXPECT(eloss <= this->energy());
0170
0171 states_.particle_energy[track_slot_] -= eloss.value();
0172 }
0173
0174
0175
0176
0177
0178
0179
0180 CELER_FUNCTION ParticleId ParticleTrackView::particle_id() const
0181 {
0182 return states_.particle_id[track_slot_];
0183 }
0184
0185
0186
0187
0188
0189 CELER_FUNCTION auto ParticleTrackView::energy() const -> Energy
0190 {
0191 return Energy{states_.particle_energy[track_slot_]};
0192 }
0193
0194
0195
0196
0197
0198 CELER_FUNCTION bool ParticleTrackView::is_stopped() const
0199 {
0200 return this->energy() == zero_quantity();
0201 }
0202
0203
0204
0205
0206
0207
0208
0209 CELER_FUNCTION ParticleView ParticleTrackView::particle_view() const
0210 {
0211 return ParticleView(params_, states_.particle_id[track_slot_]);
0212 }
0213
0214
0215
0216
0217
0218 CELER_FUNCTION units::MevMass ParticleTrackView::mass() const
0219 {
0220 return this->particle_view().mass();
0221 }
0222
0223
0224
0225
0226
0227 CELER_FUNCTION units::ElementaryCharge ParticleTrackView::charge() const
0228 {
0229 return this->particle_view().charge();
0230 }
0231
0232
0233
0234
0235
0236 CELER_FUNCTION real_type ParticleTrackView::decay_constant() const
0237 {
0238 return this->particle_view().decay_constant();
0239 }
0240
0241
0242
0243
0244
0245 CELER_FUNCTION bool ParticleTrackView::is_antiparticle() const
0246 {
0247 return this->particle_view().is_antiparticle();
0248 }
0249
0250
0251
0252
0253
0254 CELER_FUNCTION bool ParticleTrackView::is_stable() const
0255 {
0256 return this->decay_constant() == constants::stable_decay_constant;
0257 }
0258
0259
0260
0261
0262
0263
0264
0265 CELER_FUNCTION auto ParticleTrackView::total_energy() const -> Energy
0266 {
0267 return Energy(value_as<Energy>(this->energy())
0268 + value_as<units::MevMass>(this->mass()));
0269 }
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303 CELER_FUNCTION real_type ParticleTrackView::beta_sq() const
0304 {
0305
0306 real_type const mcsq = this->mass().value();
0307
0308 real_type inv_gamma = mcsq / (this->energy().value() + mcsq);
0309
0310 return 1 - ipow<2>(inv_gamma);
0311 }
0312
0313
0314
0315
0316
0317
0318
0319
0320 CELER_FUNCTION units::LightSpeed ParticleTrackView::speed() const
0321 {
0322 return units::LightSpeed{std::sqrt(this->beta_sq())};
0323 }
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348 CELER_FUNCTION real_type ParticleTrackView::lorentz_factor() const
0349 {
0350 CELER_EXPECT(this->mass() > zero_quantity());
0351
0352 real_type k_over_mc2 = this->energy().value() / this->mass().value();
0353 return 1 + k_over_mc2;
0354 }
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377 CELER_FUNCTION units::MevMomentumSq ParticleTrackView::momentum_sq() const
0378 {
0379 real_type const energy = this->energy().value();
0380 real_type result = ipow<2>(energy) + 2 * this->mass().value() * energy;
0381 CELER_ENSURE(result >= 0);
0382 return units::MevMomentumSq{result};
0383 }
0384
0385
0386
0387
0388
0389
0390
0391 CELER_FUNCTION units::MevMomentum ParticleTrackView::momentum() const
0392 {
0393 return units::MevMomentum{std::sqrt(this->momentum_sq().value())};
0394 }
0395
0396
0397 }