Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-28 09:14:57

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/MagneticField/InterpolatedBFieldMap.hpp"
0013 #include "Acts/MagneticField/MagneticFieldContext.hpp"
0014 #include "Acts/Utilities/Axis.hpp"
0015 #include "Acts/Utilities/AxisDefinitions.hpp"
0016 #include "Acts/Utilities/Grid.hpp"
0017 #include "Acts/Utilities/Result.hpp"
0018 #include "Acts/Utilities/VectorHelpers.hpp"
0019 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0020 
0021 #include <array>
0022 #include <cmath>
0023 #include <cstddef>
0024 #include <numbers>
0025 #include <optional>
0026 #include <stdexcept>
0027 #include <utility>
0028 
0029 using Acts::VectorHelpers::perp;
0030 
0031 using namespace Acts;
0032 
0033 namespace ActsTests {
0034 
0035 // Create a test context
0036 MagneticFieldContext mfContext = MagneticFieldContext();
0037 
0038 BOOST_AUTO_TEST_SUITE(MagneticFieldSuite)
0039 
0040 BOOST_AUTO_TEST_CASE(InterpolatedBFieldMap_rz) {
0041   // definition of dummy BField
0042   struct BField {
0043     static Vector3 value(const std::array<double, 2>& rz) {
0044       double r = rz.at(0);
0045       double z = rz.at(1);
0046       // linear in r and z so interpolation should be exact
0047       return Vector3(r * z, 3 * r, -2 * z);
0048     }
0049   };
0050 
0051   // map (x,y,z) -> (r,z)
0052   auto transformPos = [](const Vector3& pos) {
0053     return Vector2(perp(pos), pos.z());
0054   };
0055 
0056   // map (Bx,By,Bz) -> (Bx,By,Bz)
0057   auto transformBField = [](const Vector3& field, const Vector3&) {
0058     return field;
0059   };
0060 
0061   // magnetic field known on grid in (r,z)
0062   Axis r(0.0, 4.0, 4u);
0063   Axis z(-5, 7, 6u);
0064 
0065   Grid g(Type<Vector3>, std::move(r), std::move(z));
0066 
0067   using Grid_t = decltype(g);
0068   using BField_t = InterpolatedBFieldMap<Grid_t>;
0069 
0070   // set grid values
0071   for (std::size_t i = 1; i <= g.multiAxis().getNBins().at(0) + 1; ++i) {
0072     for (std::size_t j = 1; j <= g.multiAxis().getNBins().at(1) + 1; ++j) {
0073       Grid_t::index_t indices = {{i, j}};
0074       const auto& llCorner = g.multiAxis().getLowerLeftBinEdge(indices);
0075       g.atLocalBins(indices) = BField::value(llCorner);
0076     }
0077   }
0078 
0079   // create BField service
0080   BField_t b{{transformPos, transformBField, std::move(g)}};
0081 
0082   auto bCacheAny = b.makeCache(mfContext);
0083   BField_t::Cache& bCache = bCacheAny.as<BField_t::Cache>();
0084 
0085   auto check = [&](double i) {
0086     BOOST_CHECK(b.isInside({0, 0, i * 4.9}));
0087     BOOST_CHECK(!b.isInside({0, 0, i * 5.1}));
0088 
0089     BOOST_CHECK(b.isInside({i * 2.9, 0, 0}));
0090     BOOST_CHECK(!b.isInside({i * 3.1, 0, 0}));
0091 
0092     BOOST_CHECK(b.isInside({0, i * 2.9, 0}));
0093     BOOST_CHECK(!b.isInside({0, i * 3.1, 0}));
0094 
0095     BOOST_CHECK(b.isInside({2, 2.2, 0}));
0096     BOOST_CHECK(!b.isInside({2, 3, 0}));
0097 
0098     BOOST_CHECK(b.isInside({i * 2, 2.2, 0}));
0099     BOOST_CHECK(!b.isInside({i * 2, 3, 0}));
0100 
0101     BOOST_CHECK(b.isInside({2, i * 2.2, 0}));
0102     BOOST_CHECK(!b.isInside({2, i * 3, 0}));
0103 
0104     BOOST_CHECK(b.isInside({i * 2, i * 2.2, 0}));
0105     BOOST_CHECK(!b.isInside({i * 2, i * 3, 0}));
0106   };
0107 
0108   check(1);
0109   check(-1);
0110 
0111   Vector3 pos;
0112   pos << -3, 2.5,
0113       1.7;  // this was previously checked, but is actually out of bounds
0114   BOOST_CHECK(!b.isInside(pos));
0115   BOOST_CHECK(!b.getField(pos, bCacheAny).ok());
0116 
0117   pos << -1.6, 2.5, 1.7;
0118   BOOST_CHECK(b.isInside(pos));
0119   CHECK_CLOSE_REL(b.getField(pos, bCacheAny).value(),
0120                   BField::value({{perp(pos), pos.z()}}), 1e-6);
0121 
0122   auto& c = *bCache.fieldCell;
0123   BOOST_CHECK(c.isInside(transformPos(pos)));
0124   CHECK_CLOSE_REL(c.getField(transformPos(pos)),
0125                   BField::value({{perp(pos), pos.z()}}), 1e-6);
0126 
0127   pos << 1, 1, -5.5;  // this position is outside the grid
0128   BOOST_CHECK(!b.isInside(pos));
0129   BOOST_CHECK(!b.getField(pos, bCacheAny).ok());
0130 
0131   pos << 1, 6, -1.7;  // this position is outside the grid
0132   BOOST_CHECK(!b.isInside(pos));
0133   BOOST_CHECK(!b.getField(pos, bCacheAny).ok());
0134 
0135   pos << 0, 1.5, -2.5;
0136   BOOST_CHECK(b.isInside(pos));
0137   bCacheAny = b.makeCache(mfContext);
0138   BField_t::Cache& bCache2 = bCacheAny.as<BField_t::Cache>();
0139   CHECK_CLOSE_REL(b.getField(pos, bCacheAny).value(),
0140                   BField::value({{perp(pos), pos.z()}}), 1e-6);
0141   c = *bCache2.fieldCell;
0142   BOOST_CHECK(c.isInside(transformPos(pos)));
0143   CHECK_CLOSE_REL(c.getField(transformPos(pos)),
0144                   BField::value({{perp(pos), pos.z()}}), 1e-6);
0145 
0146   pos << 2, 3, -4;
0147   BOOST_CHECK(!b.isInside(pos));
0148 
0149   pos << 2, 2.2, -4;
0150   BOOST_CHECK(b.isInside(pos));
0151   bCacheAny = b.makeCache(mfContext);
0152   BField_t::Cache& bCache3 = bCacheAny.as<BField_t::Cache>();
0153   CHECK_CLOSE_REL(b.getField(pos, bCacheAny).value(),
0154                   BField::value({{perp(pos), pos.z()}}), 1e-6);
0155   c = *bCache3.fieldCell;
0156   BOOST_CHECK(c.isInside(transformPos(pos)));
0157   CHECK_CLOSE_REL(c.getField(transformPos(pos)),
0158                   BField::value({{perp(pos), pos.z()}}), 1e-6);
0159 
0160   // some field cell tests
0161   BOOST_CHECK(!c.isInside(transformPos((pos << 3, 2, -3.7).finished())));
0162   BOOST_CHECK(!c.isInside(transformPos((pos << -2, 3, -4.7).finished())));
0163   BOOST_CHECK(!c.isInside(transformPos((pos << -2, 3, 4.7).finished())));
0164   BOOST_CHECK(c.isInside(transformPos((pos << 0, 2, -4.7).finished())));
0165   BOOST_CHECK(!c.isInside(transformPos((pos << 5, 2, 14.).finished())));
0166 }
0167 
0168 // The field transformation of an (r,z) map depends on the azimuth of the
0169 // lookup position. A cached field cell must give the field at the azimuth of
0170 // the current lookup, not at the one of the position that filled the cache.
0171 BOOST_AUTO_TEST_CASE(InterpolatedBFieldMap_rz_cache_azimuth) {
0172   // (Br,Bz) bilinear in r and z so interpolation should be exact
0173   auto value = [](double r, double z) {
0174     return Vector2(0.5 * r * z, 2. + 0.1 * z);
0175   };
0176 
0177   // map (x,y,z) -> (r,z)
0178   auto transformPos = [](const Vector3& pos) {
0179     return Vector2(perp(pos), pos.z());
0180   };
0181 
0182   // map (Br,Bz) -> (Bx,By,Bz)
0183   auto transformBField = [](const Vector2& field, const Vector3& pos) {
0184     const double r = perp(pos);
0185     const double cosPhi = r > 0. ? pos.x() / r : 1.;
0186     const double sinPhi = r > 0. ? pos.y() / r : 0.;
0187     return Vector3(field.x() * cosPhi, field.x() * sinPhi, field.y());
0188   };
0189 
0190   // cells of size 1 in r and 2 in z
0191   Axis rAxis(0.0, 4.0, 4u);
0192   Axis zAxis(-5, 7, 6u);
0193   Grid g(Type<Vector2>, std::move(rAxis), std::move(zAxis));
0194   using Grid_t = decltype(g);
0195   using BField_t = InterpolatedBFieldMap<Grid_t>;
0196 
0197   for (std::size_t i = 1; i <= g.multiAxis().getNBins().at(0) + 1; ++i) {
0198     for (std::size_t j = 1; j <= g.multiAxis().getNBins().at(1) + 1; ++j) {
0199       Grid_t::index_t indices = {{i, j}};
0200       const auto& llCorner = g.multiAxis().getLowerLeftBinEdge(indices);
0201       g.atLocalBins(indices) = value(llCorner.at(0), llCorner.at(1));
0202     }
0203   }
0204 
0205   BField_t b{{transformPos, transformBField, std::move(g)}};
0206 
0207   auto expected = [&](const Vector3& pos) {
0208     return transformBField(value(perp(pos), pos.z()), pos);
0209   };
0210 
0211   // fill the cache at phi = 0, in the cell r in [1,2), z in [-1,1)
0212   auto bCacheAny = b.makeCache(mfContext);
0213   BField_t::Cache& bCache = bCacheAny.as<BField_t::Cache>();
0214   const Vector3 start(1.5, 0., 0.5);
0215   CHECK_CLOSE_ABS(b.getField(start, bCacheAny).value(), expected(start), 1e-12);
0216 
0217   // reuse the cached cell at other azimuths
0218   for (double phi : {0.3, 1., 2., std::numbers::pi, -2.5, -0.7}) {
0219     for (double r : {1.2, 1.5, 1.9}) {
0220       const Vector3 pos(r * std::cos(phi), r * std::sin(phi), 0.8);
0221       BOOST_CHECK(bCache.fieldCell->isInside(transformPos(pos)));
0222       const Vector3 cached = b.getField(pos, bCacheAny).value();
0223       CHECK_CLOSE_ABS(cached, expected(pos), 1e-12);
0224       CHECK_CLOSE_ABS(cached, b.getField(pos).value(), 1e-12);
0225     }
0226   }
0227 }
0228 
0229 // Same for a (r,phi,z) map with cylindrical field components: a cached cell
0230 // spans a range in phi, so the rotation into Cartesian components must use
0231 // the azimuth of the current lookup.
0232 BOOST_AUTO_TEST_CASE(InterpolatedBFieldMap_rphiz_cache_azimuth) {
0233   // (Br,Bphi,Bz) linear in r and z, constant in phi
0234   auto value = [](double r, double /*phi*/, double z) {
0235     return Vector3(1. + 0.1 * r, 0.5 + 0.2 * z, 2.);
0236   };
0237 
0238   // map (x,y,z) -> (r,phi,z)
0239   auto transformPos = [](const Vector3& pos) {
0240     return Vector3(perp(pos), std::atan2(pos.y(), pos.x()), pos.z());
0241   };
0242 
0243   // map (Br,Bphi,Bz) -> (Bx,By,Bz)
0244   auto transformBField = [](const Vector3& field, const Vector3& pos) {
0245     const double r = perp(pos);
0246     const double cosPhi = r > 0. ? pos.x() / r : 1.;
0247     const double sinPhi = r > 0. ? pos.y() / r : 0.;
0248     return Vector3(field.x() * cosPhi - field.y() * sinPhi,
0249                    field.x() * sinPhi + field.y() * cosPhi, field.z());
0250   };
0251 
0252   // cells of size 1 in r, pi/2 in phi and 2 in z; the last bin of each axis
0253   // only provides the upper corner values of the lookup domain
0254   Axis rAxis(0.0, 4.0, 4u);
0255   Axis phiAxis(-std::numbers::pi, 1.5 * std::numbers::pi, 5u);
0256   Axis zAxis(-5, 7, 6u);
0257   Grid g(Type<Vector3>, std::move(rAxis), std::move(phiAxis), std::move(zAxis));
0258   using Grid_t = decltype(g);
0259   using BField_t = InterpolatedBFieldMap<Grid_t>;
0260 
0261   for (std::size_t i = 1; i <= g.multiAxis().getNBins().at(0) + 1; ++i) {
0262     for (std::size_t j = 1; j <= g.multiAxis().getNBins().at(1) + 1; ++j) {
0263       for (std::size_t k = 1; k <= g.multiAxis().getNBins().at(2) + 1; ++k) {
0264         Grid_t::index_t indices = {{i, j, k}};
0265         const auto& llCorner = g.multiAxis().getLowerLeftBinEdge(indices);
0266         g.atLocalBins(indices) =
0267             value(llCorner.at(0), llCorner.at(1), llCorner.at(2));
0268       }
0269     }
0270   }
0271 
0272   BField_t b{{transformPos, transformBField, std::move(g)}};
0273 
0274   auto expected = [&](const Vector3& pos) {
0275     const Vector3 local = transformPos(pos);
0276     return transformBField(value(local.x(), local.y(), local.z()), pos);
0277   };
0278 
0279   // fill the cache at phi = -1.5, in the cell phi in [-pi/2, 0)
0280   auto bCacheAny = b.makeCache(mfContext);
0281   BField_t::Cache& bCache = bCacheAny.as<BField_t::Cache>();
0282   const double r = 1.5;
0283   const Vector3 start(r * std::cos(-1.5), r * std::sin(-1.5), 0.5);
0284   CHECK_CLOSE_ABS(b.getField(start, bCacheAny).value(), expected(start), 1e-12);
0285 
0286   for (double phi : {-1.2, -0.8, -0.1}) {
0287     const Vector3 pos(r * std::cos(phi), r * std::sin(phi), 0.8);
0288     BOOST_CHECK(bCache.fieldCell->isInside(transformPos(pos)));
0289     const Vector3 cached = b.getField(pos, bCacheAny).value();
0290     CHECK_CLOSE_ABS(cached, expected(pos), 1e-12);
0291     CHECK_CLOSE_ABS(cached, b.getField(pos).value(), 1e-12);
0292   }
0293 }
0294 
0295 // A Cartesian map with a position-independent transformation can apply it
0296 // once per cached cell instead of once per lookup, with the same result.
0297 BOOST_AUTO_TEST_CASE(InterpolatedBFieldMap_xyz_position_independent) {
0298   // (Bx,By,Bz) linear in x, y and z so interpolation should be exact
0299   auto value = [](double x, double y, double z) {
0300     return Vector3(1. + 0.1 * x, -0.5 + 0.2 * y, 2. + 0.05 * z);
0301   };
0302 
0303   auto transformPos = [](const Vector3& pos) { return pos; };
0304 
0305   // identity, counting its calls
0306   std::size_t nTransforms = 0;
0307   auto transformBField = [&nTransforms](const Vector3& field,
0308                                         const Vector3& /*pos*/) {
0309     ++nTransforms;
0310     return field;
0311   };
0312 
0313   // cells of size 1 in x, y and z
0314   Axis xAxis(-2., 3., 5u);
0315   Axis yAxis(-2., 3., 5u);
0316   Axis zAxis(-2., 3., 5u);
0317   Grid g(Type<Vector3>, std::move(xAxis), std::move(yAxis), std::move(zAxis));
0318   using Grid_t = decltype(g);
0319   using BField_t = InterpolatedBFieldMap<Grid_t>;
0320 
0321   for (std::size_t i = 1; i <= g.multiAxis().getNBins().at(0) + 1; ++i) {
0322     for (std::size_t j = 1; j <= g.multiAxis().getNBins().at(1) + 1; ++j) {
0323       for (std::size_t k = 1; k <= g.multiAxis().getNBins().at(2) + 1; ++k) {
0324         Grid_t::index_t indices = {{i, j, k}};
0325         const auto& llCorner = g.multiAxis().getLowerLeftBinEdge(indices);
0326         g.atLocalBins(indices) =
0327             value(llCorner.at(0), llCorner.at(1), llCorner.at(2));
0328       }
0329     }
0330   }
0331 
0332   const std::array<Vector3, 4> positions = {
0333       Vector3(0.1, 0.2, 0.3), Vector3(0.9, 0.5, 0.1), Vector3(0.4, 0.8, 0.7),
0334       Vector3(0.6, 0.1, 0.95)};
0335 
0336   for (bool dependsOnPosition : {true, false}) {
0337     BField_t::Config cfg{transformPos, transformBField, g};
0338     cfg.transformDependsOnPosition = dependsOnPosition;
0339     BField_t b{std::move(cfg)};
0340 
0341     auto bCacheAny = b.makeCache(mfContext);
0342     BField_t::Cache& bCache = bCacheAny.as<BField_t::Cache>();
0343 
0344     nTransforms = 0;
0345     for (const Vector3& pos : positions) {
0346       const Vector3 cached = b.getField(pos, bCacheAny).value();
0347       BOOST_CHECK(bCache.fieldCell->isInside(pos));
0348       CHECK_CLOSE_ABS(cached, value(pos.x(), pos.y(), pos.z()), 1e-12);
0349     }
0350     // all positions share one cell: one transformation per corner when the
0351     // cell is filled, or one per lookup
0352     BOOST_CHECK_EQUAL(nTransforms, dependsOnPosition ? positions.size() : 8u);
0353 
0354     for (const Vector3& pos : positions) {
0355       CHECK_CLOSE_ABS(b.getField(pos, bCacheAny).value(),
0356                       b.getField(pos).value(), 1e-12);
0357     }
0358   }
0359 }
0360 
0361 // Applying the transformation once per cell requires Cartesian field values.
0362 BOOST_AUTO_TEST_CASE(InterpolatedBFieldMap_position_independent_vector3) {
0363   auto transformPos = [](const Vector3& pos) {
0364     return Vector2(perp(pos), pos.z());
0365   };
0366   auto transformBField = [](const Vector2& field, const Vector3& /*pos*/) {
0367     return Vector3(0., field.x(), field.y());
0368   };
0369 
0370   Grid g(Type<Vector2>, Axis(0.0, 4.0, 4u), Axis(-5., 7., 6u));
0371   using BField_t = InterpolatedBFieldMap<decltype(g)>;
0372 
0373   BField_t::Config cfg{transformPos, transformBField, std::move(g)};
0374   cfg.transformDependsOnPosition = false;
0375   BOOST_CHECK_THROW(BField_t{std::move(cfg)}, std::invalid_argument);
0376 }
0377 
0378 BOOST_AUTO_TEST_SUITE_END()
0379 
0380 }  // namespace ActsTests