File indexing completed on 2026-12-03 10:57:48
0001
0002
0003
0004
0005
0006
0007 #pragma once
0008
0009 #include "corecel/Assert.hh"
0010 #include "corecel/math/Algorithms.hh"
0011 #include "corecel/math/ArrayUtils.hh"
0012 #include "orange/OrangeData.hh"
0013 #include "orange/OrangeTypes.hh"
0014 #include "orange/SenseUtils.hh"
0015 #include "orange/detail/BvhEnclosingVolFinder.hh"
0016 #include "orange/detail/BvhIntersectingVolFinder.hh"
0017 #include "orange/surf/LocalSurfaceVisitor.hh"
0018
0019 #include "detail/InfixEvaluator.hh"
0020 #include "detail/LogicEvaluator.hh"
0021 #include "detail/SenseCalculator.hh"
0022 #include "detail/SurfaceFunctors.hh"
0023 #include "detail/Types.hh"
0024 #include "detail/Utils.hh"
0025
0026 #if !CELER_DEVICE_COMPILE
0027 # include "corecel/io/Logger.hh"
0028 # include "corecel/io/Repr.hh"
0029 #endif
0030
0031 namespace celeritas
0032 {
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 class SimpleUnitTracker
0047 {
0048 public:
0049
0050
0051 using ParamsRef = NativeCRef<OrangeParamsData>;
0052 using Initialization = detail::Initialization;
0053 using Intersection = detail::Intersection;
0054 using LocalState = detail::LocalState;
0055 using LogicEvaluator
0056 = std::conditional_t<orange_tracking_logic == LogicNotation::infix,
0057 detail::InfixEvaluator,
0058 detail::PostfixEvaluator>;
0059 using VolumeView = detail::LocalVolumeView;
0060
0061
0062 public:
0063
0064 inline CELER_FUNCTION SimpleUnitTracker(ParamsRef const& params,
0065 SimpleUnitId id);
0066
0067
0068
0069
0070 CELER_FUNCTION LocalVolumeId::size_type num_volumes() const
0071 {
0072 return unit_record_.volumes.size();
0073 }
0074
0075
0076 CELER_FUNCTION LocalSurfaceId::size_type num_surfaces() const
0077 {
0078 return unit_record_.surfaces.size();
0079 }
0080
0081
0082 CELER_FUNCTION SimpleUnitRecord const& unit_record() const
0083 {
0084 return unit_record_;
0085 }
0086
0087
0088 inline CELER_FUNCTION DaughterId daughter(LocalVolumeId vol) const;
0089
0090
0091 inline CELER_FUNCTION vol_level_uint local_vol_level(
0092 LocalVolumeId vol) const;
0093
0094
0095 inline CELER_FUNCTION LocalVolumeId local_parent(LocalVolumeId vol) const;
0096
0097
0098
0099
0100 inline CELER_FUNCTION Initialization initialize(
0101 LocalState const& state) const;
0102
0103
0104 inline CELER_FUNCTION Initialization cross_boundary(
0105 LocalState const& state) const;
0106
0107
0108 inline CELER_FUNCTION Intersection intersect(LocalState const& state) const;
0109
0110
0111 inline CELER_FUNCTION Intersection intersect(LocalState const& state,
0112 real_type max_dist) const;
0113
0114
0115 inline CELER_FUNCTION real_type safety(Real3 const& pos,
0116 LocalVolumeId vol) const;
0117
0118
0119 inline CELER_FUNCTION Real3 normal(Real3 const& pos,
0120 LocalSurfaceId surf) const;
0121
0122 private:
0123
0124
0125 ParamsRef const& params_;
0126 SimpleUnitRecord const& unit_record_;
0127
0128
0129
0130
0131 inline CELER_FUNCTION LdgSpan<LocalVolumeId const> get_neighbors(
0132 LocalSurfaceId) const;
0133
0134 template<class F>
0135 inline CELER_FUNCTION LocalVolumeId find_volume_where(Real3 const& pos,
0136 F&& predicate) const;
0137
0138 inline CELER_FUNCTION Intersection simple_intersect(
0139 LocalState const&, VolumeView const&, size_type) const;
0140 inline CELER_FUNCTION Intersection complex_intersect(
0141 LocalState const&, VolumeView const&, size_type, Sense, real_type) const;
0142 inline CELER_FUNCTION Intersection background_intersect(LocalState const&,
0143 real_type) const;
0144
0145
0146 inline CELER_FUNCTION LocalSurfaceVisitor make_surface_visitor() const;
0147
0148
0149 inline CELER_FUNCTION VolumeView make_local_volume(
0150 LocalVolumeId vol_id) const;
0151 };
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163 CELER_FUNCTION SimpleUnitTracker::SimpleUnitTracker(ParamsRef const& params,
0164 SimpleUnitId suid)
0165 : params_(params), unit_record_(params.simple_units[suid])
0166 {
0167 CELER_EXPECT(params_);
0168 }
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181 CELER_FUNCTION auto SimpleUnitTracker::initialize(
0182 LocalState const& state) const -> Initialization
0183 {
0184 CELER_EXPECT(params_);
0185 CELER_EXPECT(!state.surface && !state.volume);
0186
0187
0188
0189 detail::OnFace on_surface;
0190 auto is_inside = [this, &state, &on_surface](LocalVolumeId id) -> bool {
0191 on_surface = {};
0192 VolumeView vol = this->make_local_volume(id);
0193 auto calc_senses = detail::SenseCalculator(
0194 this->make_surface_visitor(), vol, state.pos, on_surface);
0195 return LogicEvaluator(vol.logic())(calc_senses);
0196 };
0197 LocalVolumeId id = this->find_volume_where(state.pos, is_inside);
0198
0199 if (on_surface)
0200 {
0201
0202 id = {};
0203 }
0204 else if (!id)
0205 {
0206
0207 id = unit_record_.background;
0208 }
0209
0210 return Initialization{id, {}};
0211 }
0212
0213
0214
0215
0216
0217 CELER_FUNCTION auto SimpleUnitTracker::cross_boundary(
0218 LocalState const& state) const -> Initialization
0219 {
0220 CELER_EXPECT(state.surface && state.volume);
0221
0222 detail::OnLocalSurface on_surface;
0223 auto is_inside
0224 = [this, &state, &on_surface](LocalVolumeId const& id) -> bool {
0225 if (id == state.volume)
0226 {
0227
0228 return false;
0229 }
0230
0231 VolumeView vol = this->make_local_volume(id);
0232 detail::OnFace face{detail::find_face(vol, state.surface)};
0233 auto calc_senses = detail::SenseCalculator(
0234 this->make_surface_visitor(), vol, state.pos, face);
0235
0236 if (LogicEvaluator(vol.logic())(calc_senses))
0237 {
0238
0239 on_surface = get_surface(vol, face);
0240 return true;
0241 }
0242 return false;
0243 };
0244
0245 auto neighbors = this->get_neighbors(state.surface.id());
0246
0247
0248
0249 if (neighbors.size() < 3)
0250 {
0251 for (LocalVolumeId id : neighbors)
0252 {
0253 if (is_inside(id))
0254 {
0255 return {id, on_surface};
0256 }
0257 }
0258 }
0259 else
0260 {
0261 if (LocalVolumeId id = this->find_volume_where(state.pos, is_inside))
0262 {
0263 return {id, on_surface};
0264 }
0265 }
0266
0267 return {unit_record_.background, state.surface};
0268 }
0269
0270
0271
0272
0273
0274
0275
0276
0277 CELER_FORCEINLINE_FUNCTION auto SimpleUnitTracker::intersect(
0278 LocalState const& state) const -> Intersection
0279 {
0280 return this->intersect(state, NumericLimits<real_type>::max());
0281 }
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305 CELER_FUNCTION auto SimpleUnitTracker::intersect(
0306 LocalState const& state, real_type max_dist) const -> Intersection
0307 {
0308 CELER_EXPECT(state.volume);
0309 CELER_EXPECT(max_dist > 0);
0310
0311
0312 VolumeView vol = this->make_local_volume(state.volume);
0313 CELER_ASSERT(state.temp_next.size >= vol.max_intersections());
0314
0315 if (vol.implicit_vol())
0316 {
0317
0318 return this->background_intersect(state, max_dist);
0319 }
0320
0321
0322
0323
0324 detail::CalcIntersections calc_intersections{
0325 max_dist,
0326 state.pos,
0327 state.dir,
0328 state.surface ? vol.find_face(state.surface.id()) : FaceId{},
0329 vol.simple_intersection(),
0330 state.temp_next};
0331 LocalSurfaceVisitor visit_surface(params_, unit_record_.surfaces);
0332 for (LocalSurfaceId surface : vol.faces())
0333 {
0334 visit_surface(calc_intersections, surface);
0335 }
0336 CELER_ASSERT(calc_intersections.face_idx() == vol.num_faces());
0337 size_type num_isect = calc_intersections.isect_idx();
0338 CELER_ASSERT(num_isect <= vol.max_intersections());
0339
0340 if (num_isect == 0)
0341 {
0342
0343
0344 return {};
0345 }
0346 else if (vol.simple_intersection())
0347 {
0348
0349
0350 return this->simple_intersect(state, vol, num_isect);
0351 }
0352 else if (vol.internal_surfaces())
0353 {
0354
0355
0356 celeritas::sort(state.temp_next.isect,
0357 state.temp_next.isect + num_isect,
0358 [&state](size_type a, size_type b) {
0359 return state.temp_next.distance[a]
0360 < state.temp_next.distance[b];
0361 });
0362
0363
0364 return this->complex_intersect(state,
0365 vol,
0366 num_isect,
0367 Sense::outside,
0368 numeric_limits<real_type>::infinity());
0369 }
0370
0371 CELER_ASSERT_UNREACHABLE();
0372 }
0373
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385 CELER_FUNCTION real_type SimpleUnitTracker::safety(Real3 const& pos,
0386 LocalVolumeId vol_id) const
0387 {
0388 CELER_EXPECT(vol_id);
0389
0390 VolumeView vol = this->make_local_volume(vol_id);
0391 if (!vol.simple_safety())
0392 {
0393
0394
0395 return 0;
0396 }
0397
0398
0399 real_type result = numeric_limits<real_type>::infinity();
0400 LocalSurfaceVisitor visit_surface(params_, unit_record_.surfaces);
0401 detail::CalcSafetyDistance calc_safety{pos};
0402 for (LocalSurfaceId surface : vol.faces())
0403 {
0404 result = celeritas::min(result, visit_surface(calc_safety, surface));
0405 }
0406
0407 CELER_ENSURE(result >= 0);
0408 return result;
0409 }
0410
0411
0412
0413
0414
0415 CELER_FUNCTION auto SimpleUnitTracker::normal(
0416 Real3 const& pos, LocalSurfaceId surf) const -> Real3
0417 {
0418 CELER_EXPECT(surf);
0419
0420 LocalSurfaceVisitor visit_surface(params_, unit_record_.surfaces);
0421 return visit_surface(detail::CalcNormal{pos}, surf);
0422 }
0423
0424
0425
0426
0427
0428
0429
0430 CELER_FUNCTION auto SimpleUnitTracker::get_neighbors(LocalSurfaceId surf) const
0431 -> LdgSpan<LocalVolumeId const>
0432 {
0433 CELER_EXPECT(surf < this->num_surfaces());
0434
0435 ConnectivityRecord const& conn
0436 = params_.connectivity_records[unit_record_.connectivity[surf]];
0437
0438 CELER_ENSURE(!conn.neighbors.empty());
0439 return params_.local_volume_ids[conn.neighbors];
0440 }
0441
0442
0443
0444
0445
0446
0447
0448 template<class F>
0449 CELER_FUNCTION LocalVolumeId SimpleUnitTracker::find_volume_where(
0450 Real3 const& pos, F&& predicate) const
0451 {
0452 detail::BvhEnclosingVolFinder find_volume{unit_record_.bvh_tree,
0453 params_.bvh_tree_data};
0454 return find_volume(pos, predicate);
0455 }
0456
0457
0458
0459
0460
0461 CELER_FUNCTION auto SimpleUnitTracker::simple_intersect(
0462 LocalState const& state, VolumeView const& vol, size_type num_isect) const
0463 -> Intersection
0464 {
0465 CELER_EXPECT(num_isect > 0);
0466
0467
0468
0469 size_type distance_idx
0470 = celeritas::min_element(state.temp_next.distance,
0471 state.temp_next.distance + num_isect,
0472 Less<real_type>{})
0473 - state.temp_next.distance;
0474 CELER_ASSERT(distance_idx < num_isect);
0475
0476
0477 LocalSurfaceId surface;
0478 {
0479 FaceId face = state.temp_next.face[distance_idx];
0480 CELER_ASSERT(face);
0481 surface = vol.get_surface(face);
0482 CELER_ASSERT(surface);
0483 }
0484
0485 Sense cur_sense;
0486 if (surface == state.surface.id())
0487 {
0488
0489
0490
0491 cur_sense = state.surface.sense();
0492 }
0493 else
0494 {
0495 LocalSurfaceVisitor visit_surface(params_, unit_record_.surfaces);
0496 SignedSense ss = visit_surface(detail::CalcSense{state.pos}, surface);
0497 CELER_ASSERT(ss != SignedSense::on);
0498 cur_sense = to_sense(ss);
0499 }
0500
0501
0502 Intersection result;
0503 result.surface = {surface, cur_sense};
0504 result.distance = state.temp_next.distance[distance_idx];
0505 return result;
0506 }
0507
0508
0509
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523
0524
0525 CELER_FUNCTION auto SimpleUnitTracker::complex_intersect(
0526 LocalState const& state,
0527 VolumeView const& vol,
0528 size_type num_isect,
0529 Sense target_sense,
0530 real_type max_distance) const -> Intersection
0531 {
0532 CELER_ASSERT(num_isect > 0);
0533
0534
0535
0536
0537
0538 Real3 pos{state.pos};
0539 detail::OnFace on_face(detail::find_face(vol, state.surface));
0540
0541 detail::SenseCalculator calc_sense{
0542 this->make_surface_visitor(), vol, pos, on_face};
0543
0544
0545
0546 LogicEvaluator is_inside(vol.logic());
0547
0548
0549 #if !CELER_DEVICE_COMPILE
0550 if constexpr (CELERITAS_DEBUG)
0551 {
0552 if (is_inside(calc_sense) == (target_sense == Sense::inside))
0553 {
0554 CELER_LOG_LOCAL(warning)
0555 << "Calculated surface sense at position " << repr(pos)
0556 << " already matches target sense";
0557 }
0558 }
0559 #endif
0560
0561
0562 real_type previous_distance{0};
0563
0564
0565
0566
0567
0568 for (size_type isect_idx = 0; isect_idx != num_isect; ++isect_idx)
0569 {
0570
0571 size_type const isect = state.temp_next.isect[isect_idx];
0572 real_type const distance = state.temp_next.distance[isect];
0573
0574 if (distance >= max_distance)
0575 {
0576
0577 return {};
0578 }
0579
0580
0581 on_face = [&] {
0582 FaceId face{state.temp_next.face[isect]};
0583
0584
0585 return detail::OnFace{face, flip_sense(calc_sense(face))};
0586 }();
0587 axpy(distance - previous_distance, state.dir, &pos);
0588
0589
0590
0591 if (is_inside(calc_sense) == (target_sense == Sense::inside))
0592 {
0593
0594
0595
0596
0597 CELER_ENSURE(distance > 0 && !std::isinf(distance));
0598
0599 return {
0600 {vol.get_surface(on_face.id()), flip_sense(on_face.sense())},
0601 distance};
0602 }
0603 previous_distance = distance;
0604 }
0605
0606
0607
0608 return {};
0609 }
0610
0611
0612
0613
0614
0615
0616
0617
0618
0619
0620
0621
0622
0623
0624
0625
0626
0627
0628
0629
0630
0631
0632
0633
0634
0635
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645
0646 CELER_FUNCTION auto SimpleUnitTracker::background_intersect(
0647 LocalState const& state, real_type max_distance) const -> Intersection
0648 {
0649
0650
0651 auto is_intersecting
0652 = [this, &state](LocalVolumeId vol_id,
0653 real_type cur_max_dist) -> Intersection {
0654 VolumeView vol = this->make_local_volume(vol_id);
0655
0656
0657 constexpr bool is_simple = false;
0658 detail::CalcIntersections calc_intersections{
0659 cur_max_dist,
0660 state.pos,
0661 state.dir,
0662 state.surface ? vol.find_face(state.surface.id()) : FaceId{},
0663 is_simple,
0664 state.temp_next};
0665
0666 LocalSurfaceVisitor visit_surface(params_, unit_record_.surfaces);
0667 for (LocalSurfaceId surface : vol.faces())
0668 {
0669 visit_surface(calc_intersections, surface);
0670 }
0671
0672 size_type num_isect = calc_intersections.isect_idx();
0673 if (num_isect == 0)
0674 {
0675
0676 return {};
0677 }
0678
0679
0680 celeritas::sort(state.temp_next.isect,
0681 state.temp_next.isect + num_isect,
0682 [&state](size_type a, size_type b) {
0683 return state.temp_next.distance[a]
0684 < state.temp_next.distance[b];
0685 });
0686
0687
0688
0689
0690 return this->complex_intersect(
0691 state, vol, num_isect, Sense::inside, cur_max_dist);
0692 };
0693
0694 detail::BvhIntersectingVolFinder find_intersection{unit_record_.bvh_tree,
0695 params_.bvh_tree_data};
0696
0697 return find_intersection(
0698 {state.pos, state.dir}, is_intersecting, max_distance);
0699 }
0700
0701
0702
0703
0704
0705 CELER_FORCEINLINE_FUNCTION LocalSurfaceVisitor
0706 SimpleUnitTracker::make_surface_visitor() const
0707 {
0708 return LocalSurfaceVisitor{params_, unit_record_.surfaces};
0709 }
0710
0711
0712
0713
0714
0715 CELER_FORCEINLINE_FUNCTION auto SimpleUnitTracker::make_local_volume(
0716 LocalVolumeId vol_id) const -> VolumeView
0717 {
0718 return VolumeView{params_, unit_record_, vol_id};
0719 }
0720
0721
0722
0723
0724
0725 CELER_FUNCTION DaughterId SimpleUnitTracker::daughter(LocalVolumeId vol) const
0726 {
0727 CELER_EXPECT(vol < unit_record_.volumes.size());
0728 return params_.volume_records[unit_record_.volumes[vol]].daughter_id;
0729 }
0730
0731
0732
0733
0734
0735
0736
0737
0738
0739 CELER_FUNCTION auto SimpleUnitTracker::local_vol_level(LocalVolumeId vol) const
0740 -> vol_level_uint
0741 {
0742 CELER_EXPECT(unit_record_.local_vol_level.empty()
0743 || vol < unit_record_.volumes.size());
0744 if (unit_record_.local_vol_level.empty())
0745 return 0;
0746
0747 OpaqueId<vol_level_uint> level_ptr = unit_record_.local_vol_level[vol];
0748 vol_level_uint local_vol_level = params_.vl_uints[level_ptr];
0749 return local_vol_level;
0750 }
0751
0752
0753
0754
0755
0756
0757
0758
0759
0760
0761 CELER_FUNCTION LocalVolumeId SimpleUnitTracker::local_parent(
0762 LocalVolumeId vol) const
0763 {
0764 CELER_EXPECT(unit_record_.local_parent.empty()
0765 || vol < unit_record_.volumes.size());
0766 if (unit_record_.local_parent.empty())
0767 return {};
0768
0769 OpaqueId<LocalVolumeId> lv_ptr = unit_record_.local_parent[vol];
0770 return params_.local_volume_ids[lv_ptr];
0771 }
0772
0773
0774 }