Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-31 08:33:01

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/ConstantBField.hpp"
0013 #include "Acts/MagneticField/MagneticFieldContext.hpp"
0014 #include "Acts/MagneticField/MagneticFieldError.hpp"
0015 #include "Acts/MagneticField/MagneticFieldProvider.hpp"
0016 #include "Acts/MagneticField/NullBField.hpp"
0017 
0018 using namespace Acts;
0019 
0020 namespace ActssTests {
0021 
0022 // Create a test context
0023 MagneticFieldContext mfContext = MagneticFieldContext();
0024 
0025 BOOST_AUTO_TEST_SUITE(MagneticFieldSuite)
0026 
0027 BOOST_AUTO_TEST_CASE(TypeErasedCacheType) {
0028   bool constructor_called = false;
0029   bool destructor_called = false;
0030 
0031   struct MyCache {
0032     MyCache(int value, bool* ctor, bool* dtor) : m_value{value}, m_dtor{dtor} {
0033       (*ctor) = true;
0034     }
0035     ~MyCache() { (*m_dtor) = true; }
0036     int m_value;
0037     bool* m_dtor;
0038   };
0039 
0040   BOOST_CHECK(!constructor_called);
0041   BOOST_CHECK(!destructor_called);
0042 
0043   {
0044     MagneticFieldProvider::Cache cache{
0045         MagneticFieldProvider::Cache(std::in_place_type<MyCache>, 42,
0046                                      &constructor_called, &destructor_called)};
0047     BOOST_CHECK(constructor_called);
0048     BOOST_CHECK(!destructor_called);
0049 
0050     MyCache& v = cache.as<MyCache>();
0051     BOOST_CHECK_EQUAL(v.m_value, 42);
0052     v.m_value = 65;
0053 
0054     MyCache& v2 = cache.as<MyCache>();
0055     BOOST_CHECK_EQUAL(v2.m_value, 65);
0056   }
0057 
0058   BOOST_CHECK(constructor_called);
0059   BOOST_CHECK(destructor_called);
0060 }
0061 
0062 /// Field that does not implement the gradient, so it uses the default of the
0063 /// base class
0064 class FieldWithoutGradient final : public MagneticFieldProvider {
0065  public:
0066   struct Cache {
0067     explicit Cache(const MagneticFieldContext& /*mctx*/) {}
0068   };
0069 
0070   MagneticFieldProvider::Cache makeCache(
0071       const MagneticFieldContext& mctx) const override {
0072     return MagneticFieldProvider::Cache(std::in_place_type<Cache>, mctx);
0073   }
0074 
0075   Result<Vector3> getField(
0076       const Vector3& /*position*/,
0077       MagneticFieldProvider::Cache& /*cache*/) const override {
0078     return Result<Vector3>::success(Vector3(0., 0., 2.));
0079   }
0080 };
0081 
0082 BOOST_AUTO_TEST_CASE(FieldGradientDefault) {
0083   const FieldWithoutGradient field;
0084   auto cache = field.makeCache(mfContext);
0085 
0086   BOOST_CHECK(!field.providesFieldGradient());
0087   auto res = field.getFieldAndGradient(Vector3(1., 2., 3.), cache);
0088   BOOST_REQUIRE(!res.ok());
0089   BOOST_CHECK(res.error() == MagneticFieldError::NotImplemented);
0090 }
0091 
0092 BOOST_AUTO_TEST_CASE(FieldGradientConstant) {
0093   const Vector3 b(0.1, -0.2, 2.);
0094   const Vector3 position(10., -20., 30.);
0095 
0096   const ConstantBField constant(b);
0097   auto constantCache = constant.makeCache(mfContext);
0098   BOOST_CHECK(constant.providesFieldGradient());
0099   auto constantRes = constant.getFieldAndGradient(position, constantCache);
0100   BOOST_REQUIRE(constantRes.ok());
0101   BOOST_CHECK_EQUAL(constantRes->field, b);
0102   BOOST_CHECK_EQUAL(constantRes->gradient, SquareMatrix3::Zero());
0103 
0104   const NullBField null;
0105   auto nullCache = null.makeCache(mfContext);
0106   BOOST_CHECK(null.providesFieldGradient());
0107   auto nullRes = null.getFieldAndGradient(position, nullCache);
0108   BOOST_REQUIRE(nullRes.ok());
0109   BOOST_CHECK_EQUAL(nullRes->field, Vector3::Zero());
0110   BOOST_CHECK_EQUAL(nullRes->gradient, SquareMatrix3::Zero());
0111 }
0112 
0113 BOOST_AUTO_TEST_SUITE_END()
0114 
0115 }  // namespace ActssTests