Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-02 09:09:15

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/Units.hpp"
0013 #include "Acts/Geometry/CylinderPortalShell.hpp"
0014 #include "Acts/Geometry/CylinderVolumeBounds.hpp"
0015 #include "Acts/Geometry/GeometryContext.hpp"
0016 #include "Acts/Geometry/TrackingVolume.hpp"
0017 #include "Acts/Navigation/CylinderNavigationPolicy.hpp"
0018 #include "Acts/Navigation/NavigationStream.hpp"
0019 #include "Acts/Navigation/TryAllNavigationPolicy.hpp"
0020 #include "Acts/Surfaces/CylinderSurface.hpp"
0021 #include "Acts/Utilities/Logger.hpp"
0022 #include "ActsTests/CommonHelpers/BenchmarkTools.hpp"
0023 
0024 #include <array>
0025 #include <chrono>
0026 #include <cmath>
0027 #include <iostream>
0028 #include <memory>
0029 #include <numbers>
0030 #include <random>
0031 #include <string_view>
0032 #include <vector>
0033 
0034 using namespace Acts;
0035 using namespace Acts::UnitLiterals;
0036 
0037 namespace ActsTests {
0038 namespace {
0039 
0040 constexpr double rMin = 100_mm;
0041 constexpr double rMax = 400_mm;
0042 constexpr double halfLengthZ = 300_mm;
0043 constexpr std::size_t nInputs = 4096;
0044 constexpr std::size_t nRuns = 2000;
0045 constexpr auto warmupTime = std::chrono::milliseconds(500);
0046 
0047 struct Scenario {
0048   std::string_view name;
0049   Transform3 transform;
0050 };
0051 
0052 std::array<Scenario, 3> scenarios() {
0053   Transform3 translated = Transform3::Identity();
0054   translated.translation() = Vector3(30_mm, -20_mm, 50_mm);
0055   Transform3 rotated = translated;
0056   rotated.rotate(AngleAxis3(0.35, Vector3(1., 2., 3.).normalized()));
0057   return {{{"aligned", Transform3::Identity()},
0058            {"translated", translated},
0059            {"rotated", rotated}}};
0060 }
0061 
0062 std::vector<NavigationArguments> makeInputs(const Transform3& transform) {
0063   std::mt19937 generator(42);
0064   std::uniform_real_distribution<double> uniform(0., 1.);
0065   std::vector<NavigationArguments> inputs;
0066   inputs.reserve(nInputs);
0067   for (std::size_t i = 0; i < nInputs; ++i) {
0068     // Sample points uniformly in the shell volume. The same local rays are
0069     // used for all transforms, keeping the geometric workload comparable.
0070     const double radius = std::sqrt(
0071         rMin * rMin + uniform(generator) * (rMax * rMax - rMin * rMin));
0072     const double phi = 2. * std::numbers::pi * uniform(generator);
0073     const double z = halfLengthZ * (2. * uniform(generator) - 1.);
0074     const Vector3 position(radius * std::cos(phi), radius * std::sin(phi), z);
0075 
0076     // Mix outward tracks from the interaction region with isotropic tracks
0077     // to cover inner/outer cylinder hits, misses, and exits through discs.
0078     const double dirPhi = 2. * std::numbers::pi * uniform(generator);
0079     const double cosTheta = 2. * uniform(generator) - 1.;
0080     const double sinTheta = std::sqrt(1. - cosTheta * cosTheta);
0081     Vector3 direction(sinTheta * std::cos(dirPhi), sinTheta * std::sin(dirPhi),
0082                       cosTheta);
0083     if (i % 4 == 0) {
0084       direction = position.normalized();
0085     }
0086     inputs.push_back({.position = transform * position,
0087                       .direction = transform.linear() * direction});
0088   }
0089   return inputs;
0090 }
0091 
0092 template <bool InitializeStream, typename Policy>
0093 MicroBenchmarkResult benchmarkPolicy(
0094     const GeometryContext& gctx, const Policy& policy,
0095     const std::vector<NavigationArguments>& inputs, const Logger& logger) {
0096   NavigationStream stream;
0097   stream.reserve(8);
0098   AppendOnlyNavigationStream appendStream(stream);
0099   NavigationPolicyStateManager stateManager;
0100   policy.createState(gctx, inputs.front(), stateManager, logger);
0101   auto state = stateManager.currentState();
0102   auto iteration = [&](const NavigationArguments& args) {
0103     stream.reset();
0104     policy.initializeCandidates(gctx, args, state, appendStream, logger);
0105     if constexpr (InitializeStream) {
0106       // Both policies add each portal only once, just as when the navigator
0107       // initializes candidates for a new volume.
0108       stream.initialize(gctx, {args.position, args.direction}, logger,
0109                         s_onSurfaceTolerance, true);
0110     }
0111     return stream.candidates().size();
0112   };
0113 
0114   // Prewarm every query so both the main and additional-intersection buffers
0115   // have reached their maximum capacity before any timed measurements.
0116   for (const auto& input : inputs) {
0117     iteration(input);
0118   }
0119   return microBenchmark(iteration, inputs, nRuns, warmupTime);
0120 }
0121 
0122 }  // namespace
0123 
0124 BOOST_AUTO_TEST_CASE(cylinder_intersections) {
0125   const auto gctx = GeometryContext::dangerouslyDefaultConstruct();
0126   for (const auto& [name, transform] : scenarios()) {
0127     const auto inputs = makeInputs(transform);
0128     const auto inner =
0129         Surface::makeShared<CylinderSurface>(transform, rMin, halfLengthZ);
0130     const auto outer =
0131         Surface::makeShared<CylinderSurface>(transform, rMax, halfLengthZ);
0132     for (bool bounded : {true, false}) {
0133       const auto tolerance =
0134           bounded ? BoundaryTolerance::None() : BoundaryTolerance::Infinite();
0135       const auto result = microBenchmark(
0136           [&](const NavigationArguments& args) {
0137             return std::array{inner->intersect(gctx, args.position,
0138                                                args.direction, tolerance),
0139                               outer->intersect(gctx, args.position,
0140                                                args.direction, tolerance)};
0141           },
0142           inputs, nRuns, warmupTime);
0143       std::cout << name << "/" << (bounded ? "None" : "Infinite")
0144                 << " (inner+outer intersection pair): " << result << '\n';
0145     }
0146   }
0147 }
0148 
0149 BOOST_AUTO_TEST_CASE(cylinder_navigation) {
0150   const auto gctx = GeometryContext::dangerouslyDefaultConstruct();
0151   const auto logger =
0152       getDefaultLogger("CylinderNavigationBenchmark", Logging::WARNING);
0153   for (const auto& [name, transform] : scenarios()) {
0154     const auto inputs = makeInputs(transform);
0155     const auto bounds =
0156         std::make_shared<CylinderVolumeBounds>(rMin, rMax, halfLengthZ);
0157     TrackingVolume volume(transform, bounds, "Cylinder");
0158     SingleCylinderPortalShell shell(gctx, volume);
0159     shell.applyToVolume();
0160     const CylinderNavigationPolicy cylinder(gctx, volume, *logger);
0161     const TryAllNavigationPolicy tryAll(
0162         gctx, volume, *logger,
0163         {.portals = true, .sensitives = false, .passives = false});
0164     auto benchmark = [&](const auto& policy, std::string_view policyName) {
0165       std::cout << name << "/" << policyName << " (candidates): "
0166                 << benchmarkPolicy<false>(gctx, policy, inputs, *logger)
0167                 << '\n';
0168       std::cout << name << "/" << policyName << " (candidates+stream): "
0169                 << benchmarkPolicy<true>(gctx, policy, inputs, *logger) << '\n';
0170     };
0171     benchmark(cylinder, "Cylinder");
0172     benchmark(tryAll, "TryAll");
0173   }
0174 }
0175 
0176 }  // namespace ActsTests