Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SolenoidBField.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SolenoidBField.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-2018 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
12 
13 #include <algorithm>
14 #include <cmath>
15 
16 #define BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
17 
18 #include <boost/exception/exception.hpp>
19 #include <boost/math/special_functions/ellint_1.hpp>
20 #include <boost/math/special_functions/ellint_2.hpp>
21 
25  // we need to scale so we reproduce the expected B field strength
26  // at the center of the solenoid
27  Vector2 field = multiCoilField({0, 0}, 1.); // scale = 1
28  m_scale = m_cfg.bMagCenter / field.norm();
29 }
30 
32  const MagneticFieldContext& mctx) const {
33  return MagneticFieldProvider::Cache::make<Cache>(mctx);
34 }
35 
37  using VectorHelpers::perp;
38  Vector2 rzPos(perp(position), position.z());
39  Vector2 rzField = multiCoilField(rzPos, m_scale);
40  Vector3 xyzField(0, 0, rzField[1]);
41 
42  if (rzPos[0] != 0.) {
43  // add xy field component, radially symmetric
44  Vector3 rDir = Vector3(position.x(), position.y(), 0).normalized();
45  xyzField += rDir * rzField[0];
46  }
47 
48  return xyzField;
49 }
50 
52  const Vector3& position, MagneticFieldProvider::Cache& /*cache*/) const {
53  return Result<Vector3>::success(getField(position));
54 }
55 
57  return multiCoilField(position, m_scale);
58 }
59 
61  const Vector3& position, ActsMatrix<3, 3>& /*derivative*/,
62  MagneticFieldProvider::Cache& /*cache*/) const {
63  return Result<Vector3>::success(getField(position));
64 }
65 
67  double scale) const {
68  // iterate over all coils
69  Vector2 resultField(0, 0);
70  for (size_t coil = 0; coil < m_cfg.nCoils; coil++) {
71  Vector2 shiftedPos =
72  Vector2(pos[0], pos[1] + m_cfg.length * 0.5 - m_dz * (coil + 0.5));
73  resultField += singleCoilField(shiftedPos, scale);
74  }
75 
76  return resultField;
77 }
78 
80  double scale) const {
81  return {B_r(pos, scale), B_z(pos, scale)};
82 }
83 
84 double Acts::SolenoidBField::B_r(const Vector2& pos, double scale) const {
85  // _
86  // 2 / pi / 2 2 2 - 1 / 2
87  // E (k ) = | ( 1 - k sin {theta} ) dtheta
88  // 1 _/ 0
89  using boost::math::ellint_1;
90  // _ ____________________
91  // 2 / pi / 2| / 2 2
92  // E (k ) = | |/ 1 - k sin {theta} dtheta
93  // 2 _/ 0
94  using boost::math::ellint_2;
95 
96  double r = std::abs(pos[0]);
97  double z = pos[1];
98 
99  if (r == 0) {
100  return 0.;
101  }
102 
103  // _ _
104  // mu I | / 2 \ |
105  // 0 kz | |2 - k | 2 2 |
106  // B (r, z) = ----- ------ | |-------|E (k ) - E (k ) |
107  // r 4pi ___ | | 2| 2 1 |
108  // | / 3 |_ \2 - 2k / _|
109  // |/ Rr
110  double k_2 = k2(r, z);
111  double k = std::sqrt(k_2);
112  double constant =
113  scale * k * z / (4 * M_PI * std::sqrt(m_cfg.radius * r * r * r));
114 
115  double B = (2. - k_2) / (2. - 2. * k_2) * ellint_2(k_2) - ellint_1(k_2);
116 
117  // pos[0] is still signed!
118  return r / pos[0] * constant * B;
119 }
120 
121 double Acts::SolenoidBField::B_z(const Vector2& pos, double scale) const {
122  // _
123  // 2 / pi / 2 2 2 - 1 / 2
124  // E (k ) = | ( 1 - k sin {theta} ) dtheta
125  // 1 _/ 0
126  using boost::math::ellint_1;
127  // _ ____________________
128  // 2 / pi / 2| / 2 2
129  // E (k ) = | |/ 1 - k sin {theta} dtheta
130  // 2 _/ 0
131  using boost::math::ellint_2;
132 
133  double r = std::abs(pos[0]);
134  double z = pos[1];
135 
136  // _ _
137  // mu I | / 2 \ |
138  // 0 k | | (R + r)k - 2r | 2 2 |
139  // B (r,z) = ----- ---- | | -------------- | E (k ) + E (k ) |
140  // z 4pi __ | | 2 | 2 1 |
141  // |/Rr |_ \ 2r(1 - k ) / _|
142 
143  if (r == 0) {
144  double res = scale / 2. * m_R2 / (std::sqrt(m_R2 + z * z) * (m_R2 + z * z));
145  return res;
146  }
147 
148  double k_2 = k2(r, z);
149  double k = std::sqrt(k_2);
150  double constant = scale * k / (4 * M_PI * std::sqrt(m_cfg.radius * r));
151  double B = ((m_cfg.radius + r) * k_2 - 2. * r) / (2. * r * (1. - k_2)) *
152  ellint_2(k_2) +
153  ellint_1(k_2);
154 
155  return constant * B;
156 }
157 
158 double Acts::SolenoidBField::k2(double r, double z) const {
159  // 2 4Rr
160  // k = ---------------
161  // 2 2
162  // (R + r) + z
163  return 4 * m_cfg.radius * r /
164  ((m_cfg.radius + r) * (m_cfg.radius + r) + z * z);
165 }