Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-30 09:27:16

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/Units.hpp"
0012 #include "Acts/Geometry/Blueprint.hpp"
0013 #include "Acts/Geometry/ContainerBlueprintNode.hpp"
0014 #include "Acts/Geometry/CuboidVolumeBounds.hpp"
0015 #include "Acts/Geometry/CylinderVolumeBounds.hpp"
0016 #include "Acts/Geometry/GeometryContext.hpp"
0017 #include "Acts/Geometry/Portal.hpp"
0018 #include "Acts/Geometry/PortalLinkBase.hpp"
0019 #include "Acts/Geometry/TrackingGeometry.hpp"
0020 #include "Acts/Geometry/TrackingVolume.hpp"
0021 #include "Acts/Geometry/VolumeBounds.hpp"
0022 #include "Acts/Geometry/VolumePlacementBase.hpp"
0023 #include "Acts/Navigation/NavigationStream.hpp"
0024 #include "Acts/Propagator/Navigator.hpp"
0025 #include "Acts/Surfaces/CylinderBounds.hpp"
0026 #include "Acts/Surfaces/CylinderSurface.hpp"
0027 #include "Acts/Surfaces/PlaneSurface.hpp"
0028 #include "Acts/Surfaces/RectangleBounds.hpp"
0029 #include "Acts/Utilities/Logger.hpp"
0030 
0031 #include <memory>
0032 #include <numbers>
0033 #include <stdexcept>
0034 #include <string>
0035 #include <string_view>
0036 #include <vector>
0037 
0038 using namespace Acts;
0039 using namespace Acts::UnitLiterals;
0040 
0041 namespace ActsTests {
0042 
0043 namespace {
0044 
0045 const GeometryContext gctx = GeometryContext::dangerouslyDefaultConstruct();
0046 
0047 constexpr double rMin = 10_mm;
0048 constexpr double rMax = 20_mm;
0049 constexpr double halfZ = 30_mm;
0050 
0051 /// Placement with a fixed transform, which makes a volume alignable
0052 class FixedPlacement final : public VolumePlacementBase {
0053  public:
0054   explicit FixedPlacement(const Transform3& transform)
0055       : m_transform{transform}, m_inverse{transform.inverse()} {}
0056 
0057   const Transform3& localToGlobalTransform(
0058       const GeometryContext& /*gctx*/) const override {
0059     return m_transform;
0060   }
0061 
0062   const Transform3& globalToLocalTransform(
0063       const GeometryContext& /*gctx*/) const override {
0064     return m_inverse;
0065   }
0066 
0067   const Transform3& portalLocalToGlobal(const GeometryContext& /*gctx*/,
0068                                         std::size_t portalIdx) const override {
0069     return m_portals.at(portalIdx);
0070   }
0071 
0072   void makePortalsAlignable(const GeometryContext& context,
0073                             const std::vector<std::shared_ptr<RegularSurface>>&
0074                                 portalsToAlign) override {
0075     VolumePlacementBase::makePortalsAlignable(context, portalsToAlign);
0076     for (std::size_t i = 0; i < portalsToAlign.size(); ++i) {
0077       m_portals.push_back(alignPortal(context, i));
0078     }
0079   }
0080 
0081  private:
0082   Transform3 m_transform;
0083   Transform3 m_inverse;
0084   std::vector<Transform3> m_portals;
0085 };
0086 
0087 /// Three cylinders stacked along z, so their inner and outer cylinder portals
0088 /// are merged into portals three times as long as each volume.
0089 std::shared_ptr<TrackingGeometry> makeCylinderStack(const Logger& logger) {
0090   Blueprint::Config cfg;
0091   cfg.envelope[AxisDirection::AxisZ] = {20_mm, 20_mm};
0092   cfg.envelope[AxisDirection::AxisR] = {2_mm, 20_mm};
0093   Blueprint root{cfg};
0094 
0095   auto& stack = root.addCylinderContainer("Stack", AxisDirection::AxisZ);
0096   auto bounds = std::make_shared<CylinderVolumeBounds>(rMin, rMax, halfZ);
0097   for (int i = 0; i < 3; ++i) {
0098     stack.addStaticVolume(std::make_unique<TrackingVolume>(
0099         Transform3{Translation3{Vector3{0, 0, (i - 1) * 2 * halfZ}}}, bounds,
0100         "child" + std::to_string(i)));
0101   }
0102   return root.construct({}, gctx, logger);
0103 }
0104 
0105 const TrackingVolume& findVolume(const TrackingGeometry& geometry,
0106                                  std::string_view name) {
0107   const TrackingVolume* found = nullptr;
0108   geometry.visitVolumes([&](const TrackingVolume* volume) {
0109     if (volume->volumeName() == name) {
0110       found = volume;
0111     }
0112   });
0113   BOOST_REQUIRE(found != nullptr);
0114   return *found;
0115 }
0116 
0117 const Portal& outerCylinder(const TrackingVolume& volume) {
0118   for (const Portal& portal : volume.portals()) {
0119     const auto* bounds =
0120         dynamic_cast<const CylinderBounds*>(&portal.surface().bounds());
0121     if (bounds != nullptr &&
0122         std::abs(bounds->get(CylinderBounds::eR) - rMax) < 1e-6) {
0123       return portal;
0124     }
0125   }
0126   BOOST_FAIL("no outer cylinder portal");
0127   throw std::logic_error("unreachable");
0128 }
0129 
0130 }  // namespace
0131 
0132 BOOST_AUTO_TEST_SUITE(PropagatorSuite)
0133 BOOST_AUTO_TEST_SUITE(NavigatorPortalFace)
0134 
0135 // Merged portals know the face of each volume, others need none
0136 
0137 BOOST_AUTO_TEST_CASE(CylinderStackFaces) {
0138   auto logger = getDefaultLogger("PortalFace", Logging::INFO);
0139   auto geometry = makeCylinderStack(*logger);
0140 
0141   for (int i = 0; i < 3; ++i) {
0142     const TrackingVolume& volume =
0143         findVolume(*geometry, "child" + std::to_string(i));
0144     for (const Portal& portal : volume.portals()) {
0145       const Surface* face = volume.portalFace(portal);
0146       if (portal.surface().type() != Surface::Cylinder) {
0147         // Discs between the volumes are fused, the outer ones are not merged
0148         BOOST_CHECK(face == nullptr);
0149         continue;
0150       }
0151       BOOST_REQUIRE(face != nullptr);
0152       BOOST_CHECK(face != &portal.surface());
0153       BOOST_CHECK_EQUAL(face->type(), Surface::Cylinder);
0154       const auto& faceBounds =
0155           dynamic_cast<const CylinderBounds&>(face->bounds());
0156       const auto& portalBounds =
0157           dynamic_cast<const CylinderBounds&>(portal.surface().bounds());
0158       BOOST_CHECK_CLOSE(faceBounds.get(CylinderBounds::eR),
0159                         portalBounds.get(CylinderBounds::eR), 1e-9);
0160       BOOST_CHECK_CLOSE(faceBounds.get(CylinderBounds::eHalfLengthZ), halfZ,
0161                         1e-9);
0162       BOOST_CHECK_CLOSE(portalBounds.get(CylinderBounds::eHalfLengthZ),
0163                         3 * halfZ, 1e-9);
0164       BOOST_CHECK_SMALL((face->center(gctx) - volume.center(gctx)).norm(),
0165                         1e-9);
0166     }
0167   }
0168 }
0169 
0170 // The faces are found without a geometry context, so an alignable volume
0171 // keeps its full portal surfaces. Containers cannot stack alignable volumes,
0172 // so the oversized portal is attached by hand.
0173 
0174 BOOST_AUTO_TEST_CASE(AlignableVolumeHasNoFaces) {
0175   auto bounds = std::make_shared<CylinderVolumeBounds>(rMin, rMax, halfZ);
0176   auto makeGeometry = [&](const std::shared_ptr<TrackingVolume>& volume) {
0177     const auto volumeId = GeometryIdentifier().withVolume(1);
0178     volume->assignGeometryId(volumeId);
0179     auto surface = Surface::makeShared<CylinderSurface>(Transform3::Identity(),
0180                                                         rMax, 3 * halfZ);
0181     surface->assignGeometryId(volumeId.withBoundary(1));
0182     volume->addPortal(std::make_shared<Portal>(Direction::OppositeNormal(),
0183                                                std::move(surface), *volume));
0184     return std::make_shared<TrackingGeometry>(
0185         volume, nullptr, GeometryIdentifierHook{}, getDummyLogger(), false);
0186   };
0187 
0188   auto fixed =
0189       std::make_shared<TrackingVolume>(Transform3::Identity(), bounds, "fixed");
0190   auto fixedGeometry = makeGeometry(fixed);
0191   BOOST_CHECK(fixed->portalFace(*fixed->portals().begin()) != nullptr);
0192 
0193   auto placement = std::make_shared<FixedPlacement>(Transform3::Identity());
0194   auto alignable =
0195       std::make_shared<TrackingVolume>(*placement, bounds, "alignable");
0196   alignable->retainPlacement(placement);
0197   BOOST_REQUIRE(alignable->isAlignable());
0198   auto alignableGeometry = makeGeometry(alignable);
0199   BOOST_CHECK(alignable->portalFace(*alignable->portals().begin()) == nullptr);
0200 }
0201 
0202 // The two sector planes of a half cylinder lie in the same plane, on opposite
0203 // sides of the axis. Each portal gets the face on its own side.
0204 
0205 BOOST_AUTO_TEST_CASE(HalfCylinderSectorFaces) {
0206   using std::numbers::pi;
0207   auto bounds =
0208       std::make_shared<CylinderVolumeBounds>(rMin, rMax, halfZ, pi / 2.);
0209   auto volume =
0210       std::make_shared<TrackingVolume>(Transform3::Identity(), bounds, "half");
0211   const auto volumeId = GeometryIdentifier().withVolume(1);
0212   volume->assignGeometryId(volumeId);
0213 
0214   // Sector planes three times as long as the volume, as after merging
0215   std::vector<const Portal*> sectorPortals;
0216   std::size_t boundary = 0;
0217   for (const OrientedSurface& face :
0218        bounds->orientedSurfaces(Transform3::Identity())) {
0219     if (face.surface->type() != Surface::Plane) {
0220       continue;
0221     }
0222     auto surface = Surface::makeShared<PlaneSurface>(
0223         face.surface->localToGlobalTransform(gctx),
0224         std::make_shared<const RectangleBounds>(0.5 * (rMax - rMin),
0225                                                 3 * halfZ));
0226     surface->assignGeometryId(volumeId.withBoundary(++boundary));
0227     auto portal = std::make_shared<Portal>(Direction::OppositeNormal(),
0228                                            std::move(surface), *volume);
0229     sectorPortals.push_back(portal.get());
0230     volume->addPortal(std::move(portal));
0231   }
0232   BOOST_REQUIRE_EQUAL(sectorPortals.size(), 2u);
0233   // The planes coincide
0234   BOOST_REQUIRE(sectorPortals[0]->surface().isOnSurface(
0235       gctx, sectorPortals[1]->surface().center(gctx),
0236       BoundaryTolerance::Infinite()));
0237 
0238   auto geometry = std::make_shared<TrackingGeometry>(
0239       volume, nullptr, GeometryIdentifierHook{}, getDummyLogger(), false);
0240 
0241   for (const Portal* portal : sectorPortals) {
0242     const Surface* face = volume->portalFace(*portal);
0243     BOOST_REQUIRE(face != nullptr);
0244     BOOST_CHECK_SMALL(
0245         (face->center(gctx) - portal->surface().center(gctx)).norm(), 1e-9);
0246   }
0247   BOOST_CHECK(volume->portalFace(*sectorPortals[0]) !=
0248               volume->portalFace(*sectorPortals[1]));
0249 }
0250 
0251 BOOST_AUTO_TEST_CASE(CuboidStackFaces) {
0252   auto logger = getDefaultLogger("PortalFace", Logging::INFO);
0253   Blueprint::Config cfg;
0254   cfg.envelope = ExtentEnvelope{{
0255       .x = {20_mm, 20_mm},
0256       .y = {20_mm, 20_mm},
0257       .z = {20_mm, 20_mm},
0258   }};
0259   Blueprint root{cfg};
0260   constexpr double half = 50_mm;
0261   auto& stack = root.addCuboidContainer("Stack", AxisDirection::AxisX);
0262   auto bounds = std::make_shared<CuboidVolumeBounds>(half, half, half);
0263   for (int i = 0; i < 2; ++i) {
0264     stack.addStaticVolume(std::make_unique<TrackingVolume>(
0265         Transform3{Translation3{Vector3{(2 * i - 1) * half, 0, 0}}}, bounds,
0266         "box" + std::to_string(i)));
0267   }
0268   auto geometry = root.construct({}, gctx, *logger);
0269 
0270   for (int i = 0; i < 2; ++i) {
0271     const TrackingVolume& volume =
0272         findVolume(*geometry, "box" + std::to_string(i));
0273     int merged = 0;
0274     for (const Portal& portal : volume.portals()) {
0275       const Surface* face = volume.portalFace(portal);
0276       // The faces normal to the stacking axis are not merged
0277       const Vector3 normal = portal.surface().normal(
0278           gctx, portal.surface().center(gctx), Vector3::UnitX());
0279       if (std::abs(normal.x()) > 0.5) {
0280         BOOST_CHECK(face == nullptr);
0281         continue;
0282       }
0283       BOOST_REQUIRE(face != nullptr);
0284       ++merged;
0285       const auto& faceBounds =
0286           dynamic_cast<const RectangleBounds&>(face->bounds());
0287       BOOST_CHECK_CLOSE(faceBounds.halfLengthX(), half, 1e-9);
0288       BOOST_CHECK_CLOSE(faceBounds.halfLengthY(), half, 1e-9);
0289       BOOST_CHECK_SMALL(
0290           (face->center(gctx) - volume.center(gctx)).norm() - half, 1e-9);
0291     }
0292     BOOST_CHECK_EQUAL(merged, 4);
0293   }
0294 }
0295 
0296 // A straight line from inside the first volume reaches the merged outer
0297 // cylinder only beyond the end of that volume
0298 
0299 BOOST_AUTO_TEST_CASE(StreamRejectsHitOutsideTheFace) {
0300   auto logger = getDefaultLogger("PortalFace", Logging::INFO);
0301   auto geometry = makeCylinderStack(*logger);
0302   const TrackingVolume& volume = findVolume(*geometry, "child0");
0303   const Portal& portal = outerCylinder(volume);
0304 
0305   // Reaches r = rMax at z = -2 halfZ + 40 mm, beyond the face at -halfZ
0306   const NavigationStream::QueryPoint query{
0307       .position = Vector3{15_mm, 0, -2 * halfZ},
0308       .direction = Vector3{5_mm, 0, 40_mm}.normalized()};
0309 
0310   NavigationStream merged;
0311   AppendOnlyNavigationStream{merged}.addPortalCandidate(portal);
0312   merged.initialize(gctx, query, *logger);
0313   BOOST_CHECK_EQUAL(merged.candidates().size(), 1u);
0314 
0315   NavigationStream clipped;
0316   AppendOnlyNavigationStream{clipped, &volume}.addPortalCandidate(portal);
0317   clipped.initialize(gctx, query, *logger);
0318   BOOST_CHECK_EQUAL(clipped.candidates().size(), 0u);
0319 }
0320 
0321 // The navigator targets the face, but reports the portal surface on arrival
0322 
0323 BOOST_AUTO_TEST_CASE(NavigatorReachesPortalSurface) {
0324   auto logger = getDefaultLogger("PortalFace", Logging::INFO);
0325   auto geometry = makeCylinderStack(*logger);
0326   const TrackingVolume& volume = findVolume(*geometry, "child0");
0327   const Portal& portal = outerCylinder(volume);
0328 
0329   Navigator::Config cfg;
0330   cfg.trackingGeometry = geometry;
0331   Navigator navigator(cfg, logger->clone("Navigator"));
0332   Navigator::Options options(gctx);
0333   Navigator::State state = navigator.makeState(options);
0334 
0335   Vector3 position{15_mm, 0, -2 * halfZ};
0336   const Vector3 direction = Vector3::UnitX();
0337   BOOST_REQUIRE(
0338       navigator
0339           .initialize(state, {.position = position, .direction = direction})
0340           .ok());
0341   BOOST_REQUIRE_EQUAL(navigator.currentVolume(state), &volume);
0342 
0343   NavigationTarget target = navigator.nextTarget(state, position, direction);
0344   BOOST_REQUIRE(target.isPortalTarget());
0345   BOOST_CHECK_EQUAL(&target.portal(), &portal);
0346   BOOST_CHECK_EQUAL(&target.surface(), volume.portalFace(portal));
0347 
0348   position += target.intersection().pathLength() * direction;
0349   navigator.handleSurfaceReached(state, position, direction, target.surface());
0350   BOOST_CHECK_EQUAL(navigator.currentSurface(state), &portal.surface());
0351   BOOST_CHECK_NE(navigator.currentVolume(state), &volume);
0352 }
0353 
0354 BOOST_AUTO_TEST_SUITE_END()
0355 BOOST_AUTO_TEST_SUITE_END()
0356 
0357 }  // namespace ActsTests