File indexing completed on 2026-11-18 10:33:20
0001
0002
0003
0004
0005
0006
0007
0008 #pragma once
0009
0010 #include <algorithm>
0011 #include <G4LogicalVolume.hh>
0012 #include <G4Navigator.hh>
0013 #include <G4TouchableHandle.hh>
0014 #include <G4TouchableHistory.hh>
0015 #include <G4VPhysicalVolume.hh>
0016
0017 #include "corecel/Macros.hh"
0018 #include "corecel/cont/Array.hh"
0019 #include "corecel/cont/Span.hh"
0020 #include "corecel/io/Logger.hh"
0021 #include "corecel/math/Algorithms.hh"
0022 #include "corecel/math/ArrayUtils.hh"
0023 #include "geocel/Types.hh"
0024 #include "geocel/detail/GeantVolumeInstanceMapper.hh"
0025
0026 #include "Convert.hh"
0027 #include "GeantGeoData.hh"
0028
0029 namespace celeritas
0030 {
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059 class GeantGeoTrackView
0060 {
0061 public:
0062
0063
0064 using Initializer_t = GeoTrackInitializer;
0065 using ParamsRef = NativeCRef<GeantGeoParamsData>;
0066 using StateRef = NativeRef<GeantGeoStateData>;
0067 using real_type = double;
0068 using Real3 = Array<real_type, 3>;
0069
0070
0071 public:
0072
0073 inline GeantGeoTrackView(
0074 ParamsRef const& params, StateRef const& state, TrackSlotId tid);
0075
0076
0077 inline GeantGeoTrackView& operator=(Initializer_t const& init);
0078
0079
0080
0081
0082
0083 CELER_FORCEINLINE Real3 const& pos() const { return pos_; }
0084 CELER_FORCEINLINE Real3 const& dir() const { return dir_; }
0085
0086
0087
0088 inline VolumeId volume_id() const;
0089
0090 inline VolumeInstanceId volume_instance_id() const;
0091
0092 inline VolumeLevelId volume_level() const;
0093
0094 inline void volume_instance_id(Span<VolumeInstanceId> levels) const;
0095
0096 template<class F>
0097 inline void foreach_volume_path(F&& visit) const;
0098
0099
0100 inline ImplVolumeId impl_volume_id() const;
0101
0102
0103 inline GeoStatus geo_status() const;
0104
0105 inline bool is_outside() const;
0106
0107 inline bool is_on_boundary() const;
0108
0109 inline bool failed() const;
0110
0111 inline Real3 const& normal() const;
0112
0113
0114
0115
0116 inline Propagation find_next_step(real_type max_step);
0117
0118
0119 inline real_type find_safety();
0120
0121
0122 inline real_type find_safety(real_type max_step);
0123
0124
0125 inline void move_to_boundary();
0126
0127
0128 inline void move_internal(real_type step);
0129
0130
0131 inline void move_internal(Real3 const& pos);
0132
0133
0134 inline void cross_boundary();
0135
0136
0137 inline void set_dir(Real3 const& newdir);
0138
0139
0140
0141
0142 inline G4NavigationHistory const* nav_history() const;
0143
0144 private:
0145
0146
0147
0148 struct DetailedInitializer
0149 {
0150 TrackSlotId parent;
0151 ::celeritas::Real3 const& dir;
0152 };
0153 using ClhepLength = lengthunits::ClhepLength;
0154
0155
0156
0157
0158 ParamsRef const& params_;
0159
0160
0161 StateRef const& state_;
0162 TrackSlotId tid_;
0163
0164
0165
0166 Real3& pos_;
0167 Real3& dir_;
0168 Real3& normal_;
0169 real_type& next_step_;
0170 real_type& safety_radius_;
0171 G4TouchableHandle& touch_handle_;
0172 G4Navigator& navi_;
0173
0174
0175
0176 G4ThreeVector g4pos_;
0177 G4ThreeVector g4dir_;
0178 real_type g4safety_;
0179
0180
0181
0182
0183 inline GeantGeoTrackView& operator=(DetailedInitializer const& init);
0184
0185
0186 inline bool has_next_step() const;
0187
0188
0189 inline bool is_dir_exiting() const;
0190
0191
0192 inline void geo_status(GeoStatus);
0193
0194
0195 inline G4LogicalVolume const* volume() const;
0196 };
0197
0198
0199
0200
0201
0202
0203
0204 GeantGeoTrackView::GeantGeoTrackView(
0205 ParamsRef const& params, StateRef const& states, TrackSlotId tid)
0206 : params_{params}
0207 , state_(states)
0208 , tid_(tid)
0209 , pos_(states.pos[tid])
0210 , dir_(states.dir[tid])
0211 , normal_(states.normal[tid])
0212 , next_step_(states.next_step[tid])
0213 , safety_radius_(states.safety_radius[tid])
0214 , touch_handle_(states.nav_state.touch_handle(tid))
0215 , navi_(states.nav_state.navigator(tid))
0216 , g4pos_(native_to_geant<ClhepLength>(pos_))
0217 , g4dir_(to_g4vector(dir_))
0218 , g4safety_(native_to_geant<ClhepLength>(safety_radius_))
0219 {
0220 }
0221
0222
0223
0224
0225
0226 GeantGeoTrackView& GeantGeoTrackView::operator=(Initializer_t const& init)
0227 {
0228 CELER_EXPECT(is_soft_unit_vector(init.dir));
0229
0230 if (init.parent)
0231 {
0232
0233 *this = DetailedInitializer{init.parent, init.dir};
0234 return *this;
0235 }
0236
0237
0238 std::copy(init.pos.begin(), init.pos.end(), pos_.begin());
0239 std::copy(init.dir.begin(), init.dir.end(), dir_.begin());
0240 next_step_ = -1;
0241 safety_radius_ = -1;
0242
0243 g4pos_ = native_to_geant<ClhepLength>(pos_);
0244 g4dir_ = to_g4vector(dir_);
0245 g4safety_ = -1;
0246
0247 navi_.LocateGlobalPointAndUpdateTouchable(g4pos_,
0248 g4dir_,
0249 touch_handle_(),
0250 false);
0251 this->geo_status(this->is_outside() ? GeoStatus::invalid
0252 : GeoStatus::interior);
0253
0254 CELER_ENSURE(!this->has_next_step());
0255 return *this;
0256 }
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266 GeantGeoTrackView& GeantGeoTrackView::operator=(DetailedInitializer const& init)
0267 {
0268 CELER_EXPECT(is_soft_unit_vector(init.dir));
0269
0270 if (tid_ != init.parent)
0271 {
0272
0273 GeantGeoTrackView other(ParamsRef{}, state_, init.parent);
0274 pos_ = other.pos_;
0275 safety_radius_ = other.safety_radius_;
0276 g4pos_ = other.g4pos_;
0277 g4dir_ = other.g4dir_;
0278 g4safety_ = other.g4safety_;
0279
0280
0281 touch_handle_ = other.touch_handle_;
0282 navi_.ResetHierarchyAndLocate(
0283 g4pos_,
0284 g4dir_,
0285 dynamic_cast<G4TouchableHistory&>(*touch_handle_()));
0286 this->geo_status(state_.status[init.parent]);
0287 }
0288
0289
0290 std::copy(init.dir.begin(), init.dir.end(), dir_.begin());
0291 next_step_ = -1;
0292
0293 CELER_ENSURE(!this->has_next_step());
0294 return *this;
0295 }
0296
0297
0298
0299
0300
0301
0302
0303 VolumeId GeantGeoTrackView::volume_id() const
0304 {
0305 CELER_EXPECT(!this->is_outside());
0306 return id_cast<VolumeId>(this->impl_volume_id().get());
0307 }
0308
0309
0310
0311
0312
0313 VolumeInstanceId GeantGeoTrackView::volume_instance_id() const
0314 {
0315 CELER_EXPECT(!this->is_outside());
0316 G4VPhysicalVolume* pv = touch_handle_()->GetVolume(0);
0317 CELER_ASSERT(pv);
0318 return params_.vi_mapper->geant_to_id(*pv);
0319 }
0320
0321
0322
0323
0324
0325 VolumeLevelId GeantGeoTrackView::volume_level() const
0326 {
0327 auto* touch = touch_handle_();
0328 return id_cast<VolumeLevelId>(touch->GetHistoryDepth());
0329 }
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339 void GeantGeoTrackView::volume_instance_id(Span<VolumeInstanceId> levels) const
0340 {
0341 this->foreach_volume_path(
0342 [levels](VolumeLevelId lev, VolumeInstanceId vol_inst) {
0343 CELER_EXPECT(lev < levels.size());
0344 CELER_EXPECT(vol_inst);
0345 levels[*lev] = vol_inst;
0346 });
0347 }
0348
0349
0350
0351
0352
0353
0354
0355
0356 template<class F>
0357 void GeantGeoTrackView::foreach_volume_path(F&& visit) const
0358 {
0359 auto* touch = touch_handle_();
0360 auto const num_vol_levels
0361 = id_cast<VolumeLevelId>(touch->GetHistoryDepth());
0362 for (auto lev : range(num_vol_levels + 1))
0363 {
0364 VolumeInstanceId vi_id;
0365 G4VPhysicalVolume* pv = touch->GetVolume(num_vol_levels - lev);
0366 CELER_ASSERT(pv);
0367 vi_id = params_.vi_mapper->geant_to_id(*pv);
0368 visit(lev, vi_id);
0369 }
0370 }
0371
0372
0373
0374
0375
0376 ImplVolumeId GeantGeoTrackView::impl_volume_id() const
0377 {
0378 CELER_EXPECT(!this->is_outside());
0379 return id_cast<ImplVolumeId>(
0380 this->volume()->GetInstanceID() - params_.lv_offset);
0381 }
0382
0383
0384
0385
0386
0387 CELER_FORCEINLINE bool GeantGeoTrackView::is_outside() const
0388 {
0389 return this->volume() == nullptr;
0390 }
0391
0392
0393
0394
0395
0396 CELER_FORCEINLINE bool GeantGeoTrackView::is_on_boundary() const
0397 {
0398 return safety_radius_ == 0.0;
0399 }
0400
0401
0402
0403
0404
0405 CELER_FORCEINLINE_FUNCTION bool GeantGeoTrackView::failed() const
0406 {
0407 return this->geo_status() == GeoStatus::error;
0408 }
0409
0410
0411
0412
0413
0414 GeoStatus GeantGeoTrackView::geo_status() const
0415 {
0416 return state_.status[tid_];
0417 }
0418
0419
0420
0421
0422
0423
0424
0425 auto GeantGeoTrackView::normal() const -> Real3 const&
0426 {
0427 CELER_EXPECT(this->is_on_boundary());
0428 return normal_;
0429 }
0430
0431
0432
0433
0434
0435
0436
0437
0438 Propagation GeantGeoTrackView::find_next_step(real_type max_step)
0439 {
0440 CELER_EXPECT(!this->is_outside());
0441 CELER_EXPECT(max_step > 0);
0442
0443 if (this->geo_status() == GeoStatus::boundary_inc)
0444 {
0445
0446
0447 next_step_ = 0;
0448 return {0, true};
0449 }
0450
0451 if (this->geo_status() == GeoStatus::boundary_out)
0452 {
0453 if (!this->is_dir_exiting())
0454 {
0455
0456
0457
0458
0459
0460 navi_.LocateGlobalPointWithinVolume(g4pos_);
0461 }
0462 }
0463
0464
0465 real_type g4step = native_to_geant<ClhepLength>(max_step);
0466 g4step = navi_.ComputeStep(g4pos_, g4dir_, g4step, g4safety_);
0467
0468 if (g4safety_ != 0 && !this->is_on_boundary())
0469 {
0470
0471
0472 safety_radius_ = native_value_from(ClhepLength{g4safety_});
0473 CELER_ASSERT(!this->is_on_boundary());
0474 }
0475
0476
0477 Propagation result;
0478 result.distance = native_value_from(ClhepLength{g4step});
0479 if (result.distance <= max_step)
0480 {
0481 result.boundary = true;
0482 }
0483 else
0484 {
0485
0486 result.distance = max_step;
0487 }
0488
0489
0490 next_step_ = result.distance;
0491
0492 CELER_ENSURE(result.distance >= 0);
0493 CELER_ENSURE(result.distance > 0 || result.boundary);
0494 CELER_ENSURE(result.distance <= max_step);
0495 CELER_ENSURE(result.boundary || result.distance == max_step);
0496 CELER_ENSURE(this->has_next_step());
0497 return result;
0498 }
0499
0500
0501
0502
0503
0504 CELER_FORCEINLINE auto GeantGeoTrackView::find_safety() -> real_type
0505 {
0506 return this->find_safety(numeric_limits<real_type>::infinity());
0507 }
0508
0509
0510
0511
0512
0513
0514
0515
0516 auto GeantGeoTrackView::find_safety(real_type max_step) -> real_type
0517 {
0518 CELER_EXPECT(!this->is_on_boundary());
0519 CELER_EXPECT(max_step > 0);
0520 if (safety_radius_ < max_step)
0521 {
0522 real_type g4step = native_to_geant<ClhepLength>(max_step);
0523 g4safety_ = navi_.ComputeSafety(g4pos_, g4step);
0524 safety_radius_ = max(native_value_from(ClhepLength{g4safety_}), 0.0);
0525 }
0526
0527 return safety_radius_;
0528 }
0529
0530
0531
0532
0533
0534 void GeantGeoTrackView::move_to_boundary()
0535 {
0536 CELER_EXPECT(this->has_next_step());
0537
0538 if (this->geo_status() == GeoStatus::boundary_inc)
0539 {
0540
0541 CELER_ASSERT(this->is_on_boundary());
0542 CELER_ASSERT(next_step_ == 0);
0543 next_step_ = -1;
0544 return;
0545 }
0546
0547
0548 axpy(next_step_, dir_, &pos_);
0549 axpy(native_to_geant<ClhepLength>(next_step_), g4dir_, &g4pos_);
0550 next_step_ = -1;
0551 safety_radius_ = 0;
0552 g4safety_ = 0;
0553
0554
0555
0556 bool normal_valid{false};
0557 G4ThreeVector g4norm = navi_.GetGlobalExitNormal(g4pos_, &normal_valid);
0558 CELER_ASSERT(normal_valid);
0559 normal_ = to_array(g4norm);
0560 CELER_ASSERT(this->is_dir_exiting());
0561 this->geo_status(GeoStatus::boundary_inc);
0562
0563 CELER_ENSURE(this->is_on_boundary());
0564 }
0565
0566
0567
0568
0569
0570
0571
0572 void GeantGeoTrackView::cross_boundary()
0573 {
0574 CELER_EXPECT(this->is_on_boundary());
0575
0576 if (this->geo_status() == GeoStatus::boundary_out)
0577 {
0578
0579
0580 return;
0581 }
0582 if (!this->is_dir_exiting())
0583 {
0584
0585
0586 navi_.ComputeStep(g4pos_, g4dir_, 1e-9, g4safety_);
0587 }
0588
0589 navi_.SetGeometricallyLimitedStep();
0590 navi_.LocateGlobalPointAndUpdateTouchableHandle(
0591 g4pos_,
0592 g4dir_,
0593 touch_handle_,
0594 true);
0595
0596 this->geo_status(GeoStatus::boundary_out);
0597
0598 CELER_ENSURE(this->is_on_boundary());
0599 }
0600
0601
0602
0603
0604
0605
0606
0607
0608 void GeantGeoTrackView::move_internal(real_type dist)
0609 {
0610 CELER_EXPECT(this->has_next_step());
0611 CELER_EXPECT(dist > 0 && dist <= next_step_);
0612
0613
0614 axpy(dist, dir_, &pos_);
0615 axpy(native_to_geant<ClhepLength>(dist), g4dir_, &g4pos_);
0616 next_step_ -= dist;
0617 if (next_step_ == 0)
0618 {
0619
0620 next_step_ = -1;
0621 }
0622 navi_.LocateGlobalPointWithinVolume(g4pos_);
0623
0624 safety_radius_ = -1;
0625 g4safety_ = 0;
0626 this->geo_status(GeoStatus::interior);
0627 }
0628
0629
0630
0631
0632
0633
0634
0635
0636 void GeantGeoTrackView::move_internal(Real3 const& pos)
0637 {
0638 pos_ = pos;
0639 g4pos_ = native_to_geant<ClhepLength>(pos_);
0640 next_step_ = -1;
0641 navi_.LocateGlobalPointWithinVolume(g4pos_);
0642
0643 safety_radius_ = -1;
0644 g4safety_ = 0;
0645 this->geo_status(GeoStatus::interior);
0646 }
0647
0648
0649
0650
0651
0652
0653
0654
0655 void GeantGeoTrackView::set_dir(Real3 const& newdir)
0656 {
0657 CELER_EXPECT(is_soft_unit_vector(newdir));
0658
0659 GeoStatus status{this->geo_status()};
0660 if (::celeritas::is_on_boundary(status))
0661 {
0662
0663
0664 Real3 const& norm = this->normal();
0665
0666
0667
0668 auto new_dot = dot_product(norm, newdir);
0669 if (CELER_UNLIKELY(new_dot == 0))
0670 {
0671 CELER_LOG_LOCAL(error)
0672 << "track direction cannot change to " << newdir
0673 << " which is perpendicular to the current surface normal";
0674 this->geo_status(GeoStatus::error);
0675 return;
0676 }
0677 else if ((new_dot > 0) != (dot_product(norm, this->dir()) > 0))
0678 {
0679
0680
0681 this->geo_status(flip_boundary(this->geo_status()));
0682 }
0683 }
0684
0685 dir_ = newdir;
0686 g4dir_ = to_g4vector(newdir);
0687 next_step_ = -1;
0688 }
0689
0690
0691
0692
0693
0694 G4NavigationHistory const* GeantGeoTrackView::nav_history() const
0695 {
0696 auto* touch = touch_handle_();
0697 CELER_ASSERT(touch);
0698 return touch->GetHistory();
0699 }
0700
0701
0702
0703
0704
0705
0706
0707 CELER_FORCEINLINE bool GeantGeoTrackView::has_next_step() const
0708 {
0709 return next_step_ >= 0;
0710 }
0711
0712
0713
0714
0715
0716
0717
0718
0719 bool GeantGeoTrackView::is_dir_exiting() const
0720 {
0721 CELER_EXPECT(this->is_on_boundary());
0722 return dot_product(normal_, dir_) > 0;
0723 }
0724
0725
0726
0727 void GeantGeoTrackView::geo_status(GeoStatus gs)
0728 {
0729 state_.status[tid_] = gs;
0730 }
0731
0732
0733
0734
0735
0736 auto GeantGeoTrackView::volume() const -> G4LogicalVolume const*
0737 {
0738 CELER_EXPECT(touch_handle_());
0739 G4VPhysicalVolume* pv = touch_handle_()->GetVolume(0);
0740 if (!pv)
0741 return nullptr;
0742
0743 return pv->GetLogicalVolume();
0744 }
0745
0746
0747 }