File indexing completed on 2025-07-14 08:11:01
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "Acts/MagneticField/SolenoidBField.hpp"
0010
0011 #include "Acts/Utilities/VectorHelpers.hpp"
0012
0013 #include <cmath>
0014 #include <numbers>
0015
0016 #define BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
0017
0018 #include <boost/exception/exception.hpp>
0019 #include <boost/math/special_functions/ellint_1.hpp>
0020 #include <boost/math/special_functions/ellint_2.hpp>
0021
0022 namespace Acts {
0023
0024 SolenoidBField::SolenoidBField(Config config) : m_cfg(config) {
0025 m_dz = m_cfg.length / m_cfg.nCoils;
0026 m_R2 = m_cfg.radius * m_cfg.radius;
0027
0028
0029 Vector2 field = multiCoilField({0, 0}, 1.);
0030 m_scale = m_cfg.bMagCenter / field.norm();
0031 }
0032
0033 MagneticFieldProvider::Cache SolenoidBField::makeCache(
0034 const MagneticFieldContext& mctx) const {
0035 return MagneticFieldProvider::Cache(std::in_place_type<Cache>, mctx);
0036 }
0037
0038 Vector3 SolenoidBField::getField(const Vector3& position) const {
0039 using VectorHelpers::perp;
0040 Vector2 rzPos(perp(position), position.z());
0041 Vector2 rzField = multiCoilField(rzPos, m_scale);
0042 Vector3 xyzField(0, 0, rzField[1]);
0043
0044 if (rzPos[0] != 0.) {
0045
0046 Vector3 rDir = Vector3(position.x(), position.y(), 0).normalized();
0047 xyzField += rDir * rzField[0];
0048 }
0049
0050 return xyzField;
0051 }
0052
0053 Result<Vector3> SolenoidBField::getField(
0054 const Vector3& position, MagneticFieldProvider::Cache& ) const {
0055 return Result<Vector3>::success(getField(position));
0056 }
0057
0058 Vector2 SolenoidBField::getField(const Vector2& position) const {
0059 return multiCoilField(position, m_scale);
0060 }
0061
0062 Vector2 SolenoidBField::multiCoilField(const Vector2& pos, double scale) const {
0063
0064 Vector2 resultField(0, 0);
0065 for (std::size_t coil = 0; coil < m_cfg.nCoils; coil++) {
0066 Vector2 shiftedPos =
0067 Vector2(pos[0], pos[1] + m_cfg.length * 0.5 - m_dz * (coil + 0.5));
0068 resultField += singleCoilField(shiftedPos, scale);
0069 }
0070
0071 return resultField;
0072 }
0073
0074 Vector2 SolenoidBField::singleCoilField(const Vector2& pos,
0075 double scale) const {
0076 return {B_r(pos, scale), B_z(pos, scale)};
0077 }
0078
0079 double SolenoidBField::B_r(const Vector2& pos, double scale) const {
0080
0081
0082
0083
0084 using boost::math::ellint_1;
0085
0086
0087
0088
0089 using boost::math::ellint_2;
0090
0091 double r = std::abs(pos[0]);
0092 double z = pos[1];
0093
0094 if (r == 0) {
0095 return 0.;
0096 }
0097
0098
0099
0100
0101
0102
0103
0104
0105 double k_2 = k2(r, z);
0106 double k = std::sqrt(k_2);
0107 double constant =
0108 scale * k * z /
0109 (4 * std::numbers::pi * std::sqrt(m_cfg.radius * r * r * r));
0110
0111 double B = (2. - k_2) / (2. - 2. * k_2) * ellint_2(k_2) - ellint_1(k_2);
0112
0113
0114 return r / pos[0] * constant * B;
0115 }
0116
0117 double SolenoidBField::B_z(const Vector2& pos, double scale) const {
0118
0119
0120
0121
0122 using boost::math::ellint_1;
0123
0124
0125
0126
0127 using boost::math::ellint_2;
0128
0129 double r = std::abs(pos[0]);
0130 double z = pos[1];
0131
0132
0133
0134
0135
0136
0137
0138
0139 if (r == 0) {
0140 double res = scale / 2. * m_R2 / (std::sqrt(m_R2 + z * z) * (m_R2 + z * z));
0141 return res;
0142 }
0143
0144 double k_2 = k2(r, z);
0145 double k = std::sqrt(k_2);
0146 double constant =
0147 scale * k / (4 * std::numbers::pi * std::sqrt(m_cfg.radius * r));
0148 double B = ((m_cfg.radius + r) * k_2 - 2. * r) / (2. * r * (1. - k_2)) *
0149 ellint_2(k_2) +
0150 ellint_1(k_2);
0151
0152 return constant * B;
0153 }
0154
0155 double SolenoidBField::k2(double r, double z) const {
0156
0157
0158
0159
0160 return 4 * m_cfg.radius * r /
0161 ((m_cfg.radius + r) * (m_cfg.radius + r) + z * z);
0162 }
0163
0164 }