Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:11:22

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2017-2018 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 http://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/data/test_case.hpp>
0010 #include <boost/test/unit_test.hpp>
0011 
0012 #include "Acts/Definitions/Algebra.hpp"
0013 #include "Acts/Definitions/Units.hpp"
0014 #include "Acts/MagneticField/ConstantBField.hpp"
0015 #include "Acts/MagneticField/MagneticFieldContext.hpp"
0016 #include "Acts/Utilities/Result.hpp"
0017 
0018 #include <utility>
0019 
0020 namespace bdata = boost::unit_test::data;
0021 using namespace Acts::UnitLiterals;
0022 
0023 namespace Acts::Test {
0024 
0025 // Create a test context
0026 MagneticFieldContext mfContext = MagneticFieldContext();
0027 
0028 /// @brief unit test for construction of constant magnetic field
0029 ///
0030 /// Tests the correct behavior and consistency of
0031 /// -# ConstantBField::ConstantBField(double Bx,double By,double Bz)
0032 /// -# ConstantBField::ConstantBField(Vector3 B)
0033 /// -# ConstantBField::getField(const double* xyz, double* B) const
0034 /// -# ConstantBField::getField(const Vector3& pos) const
0035 BOOST_DATA_TEST_CASE(
0036     ConstantBField_components,
0037     bdata::random((bdata::engine = std::mt19937(), bdata::seed = 1,
0038                    bdata::distribution =
0039                        std::uniform_real_distribution<double>(-2_T, 2_T))) ^
0040         bdata::random((bdata::engine = std::mt19937(), bdata::seed = 2,
0041                        bdata::distribution =
0042                            std::uniform_real_distribution<double>(-1_T, 4_T))) ^
0043         bdata::random((bdata::engine = std::mt19937(), bdata::seed = 3,
0044                        bdata::distribution =
0045                            std::uniform_real_distribution<double>(0_T, 10_T))) ^
0046         bdata::random((bdata::engine = std::mt19937(), bdata::seed = 4,
0047                        bdata::distribution =
0048                            std::uniform_real_distribution<double>(-10_m,
0049                                                                   10_m))) ^
0050         bdata::random((bdata::engine = std::mt19937(), bdata::seed = 5,
0051                        bdata::distribution =
0052                            std::uniform_real_distribution<double>(-10_m,
0053                                                                   10_m))) ^
0054         bdata::random((bdata::engine = std::mt19937(), bdata::seed = 6,
0055                        bdata::distribution =
0056                            std::uniform_real_distribution<double>(-10_m,
0057                                                                   10_m))) ^
0058         bdata::xrange(10),
0059     x, y, z, bx, by, bz, index) {
0060   (void)index;
0061   const Vector3 Btrue(bx, by, bz);
0062   const Vector3 pos(x, y, z);
0063   const ConstantBField BField(Btrue);
0064 
0065   auto bCache = BField.makeCache(mfContext);
0066 
0067   BOOST_CHECK_EQUAL(Btrue, BField.getField());
0068 
0069   BOOST_CHECK_EQUAL(Btrue, BField.getField(pos, bCache).value());
0070   BOOST_CHECK_EQUAL(Btrue, BField.getField(Vector3(0, 0, 0), bCache).value());
0071   BOOST_CHECK_EQUAL(Btrue, BField.getField(-2 * pos, bCache).value());
0072 }
0073 
0074 /// @brief unit test for update of constant magnetic field
0075 ///
0076 /// Tests the correct behavior and consistency of
0077 /// -# ConstantBField::setField(double Bx, double By, double Bz)
0078 /// -# ConstantBField::setField(const Vector3& B)
0079 /// -# ConstantBField::getField(const double* xyz, double* B) const
0080 /// -# ConstantBField::getField(const Vector3& pos) const
0081 BOOST_DATA_TEST_CASE(
0082     ConstantBField_update,
0083     bdata::random((bdata::engine = std::mt19937(), bdata::seed = 1,
0084                    bdata::distribution =
0085                        std::uniform_real_distribution<double>(-2_T, 2_T))) ^
0086         bdata::random((bdata::engine = std::mt19937(), bdata::seed = 2,
0087                        bdata::distribution =
0088                            std::uniform_real_distribution<double>(-1_T, 4_T))) ^
0089         bdata::random((bdata::engine = std::mt19937(), bdata::seed = 3,
0090                        bdata::distribution =
0091                            std::uniform_real_distribution<double>(0_T, 10_T))) ^
0092         bdata::random((bdata::engine = std::mt19937(), bdata::seed = 4,
0093                        bdata::distribution =
0094                            std::uniform_real_distribution<double>(-10_m,
0095                                                                   10_m))) ^
0096         bdata::random((bdata::engine = std::mt19937(), bdata::seed = 5,
0097                        bdata::distribution =
0098                            std::uniform_real_distribution<double>(-10_m,
0099                                                                   10_m))) ^
0100         bdata::random((bdata::engine = std::mt19937(), bdata::seed = 6,
0101                        bdata::distribution =
0102                            std::uniform_real_distribution<double>(-10_m,
0103                                                                   10_m))) ^
0104         bdata::xrange(10),
0105     x, y, z, bx, by, bz, index) {
0106   (void)index;
0107 
0108   ConstantBField BField{Vector3{0, 0, 0}};
0109   const Vector3 Btrue(bx, by, bz);
0110   const Vector3 pos(x, y, z);
0111   BField.setField(Vector3{bx, by, bz});
0112 
0113   auto bCache = BField.makeCache(mfContext);
0114 
0115   BOOST_CHECK_EQUAL(Btrue, BField.getField());
0116 
0117   BOOST_CHECK_EQUAL(Btrue, BField.getField(pos, bCache).value());
0118   BOOST_CHECK_EQUAL(Btrue, BField.getField(Vector3(0, 0, 0), bCache).value());
0119   BOOST_CHECK_EQUAL(Btrue, BField.getField(-2 * pos, bCache).value());
0120 }
0121 
0122 }  // namespace Acts::Test