ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
InterpolatedSolenoidBFieldTest.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file InterpolatedSolenoidBFieldTest.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 
9 #include <boost/test/data/test_case.hpp>
10 #include <boost/test/unit_test.hpp>
11 
12 #include <cmath>
13 #include <fstream>
14 #include <iostream>
15 #include <random>
16 
22 #include "Acts/Utilities/Units.hpp"
25 
26 using namespace Acts::UnitLiterals;
29 
30 namespace bdata = boost::unit_test::data;
31 namespace tt = boost::test_tools;
32 
33 namespace Acts {
34 namespace IntegrationTest {
35 
36 const double L = 5.8_m;
37 const double R = (2.56 + 2.46) * 0.5 * 0.5_m;
38 const size_t nCoils = 1154;
39 const double bMagCenter = 2_T;
40 const size_t nBinsR = 150;
41 const size_t nBinsZ = 200;
42 
43 auto makeFieldMap(const SolenoidBField& field) {
44  std::ofstream ostr("solenoidmap.csv");
45  ostr << "i;j;r;z;B_r;B_z" << std::endl;
46 
47  double rMin = 0;
48  double rMax = R * 2.;
49  double zMin = 2 * (-L / 2.);
50  double zMax = 2 * (L / 2.);
51 
52  std::cout << "rMin = " << rMin << std::endl;
53  std::cout << "rMax = " << rMax << std::endl;
54  std::cout << "zMin = " << zMin << std::endl;
55  std::cout << "zMax = " << zMax << std::endl;
56 
57  auto mapper =
58  solenoidFieldMapper({rMin, rMax}, {zMin, zMax}, {nBinsR, nBinsZ}, field);
59  // I know this is the correct grid type
60  using Grid_t =
62  Acts::detail::EquidistantAxis>;
63  const Grid_t& grid = mapper.getGrid();
64  using index_t = Grid_t::index_t;
65  using point_t = Grid_t::point_t;
67 
68  for (size_t i = 0; i <= nBinsR + 1; i++) {
69  for (size_t j = 0; j <= nBinsZ + 1; j++) {
70  // std::cout << "(i,j) = " << i << "," << j << std::endl;
71  index_t index({i, j});
72  if (i == 0 || j == 0 || i == nBinsR + 1 || j == nBinsZ + 1) {
73  // under or overflow bin
74  } else {
75  point_t lowerLeft = grid.lowerLeftBinEdge(index);
76  Vector2D B = grid.atLocalBins(index);
77  ostr << i << ";" << j << ";" << lowerLeft[0] << ";" << lowerLeft[1];
78  ostr << ";" << B[0] << ";" << B[1] << std::endl;
79  }
80  }
81  }
82 
83  BField_t::Config cfg(std::move(mapper));
84  return BField_t(std::move(cfg));
85 }
86 
89 
90 struct StreamWrapper {
91  StreamWrapper(std::ofstream ofstr) : m_ofstr(std::move(ofstr)) {
92  m_ofstr << "x;y;z;B_x;B_y;B_z;Bm_x;Bm_y;Bm_z" << std::endl;
93  }
94 
95  std::ofstream m_ofstr;
96 };
97 
98 StreamWrapper valid(std::ofstream("magfield_lookup.csv"));
99 
100 const int ntests = 10000;
102  solenoid_interpolated_bfield_comparison,
103  bdata::random((bdata::seed = 1, bdata::engine = std::mt19937(),
104  bdata::distribution = std::uniform_real_distribution<>(
105  1.5 * (-L / 2.), 1.5 * L / 2.))) ^
106  bdata::random((bdata::seed = 2, bdata::engine = std::mt19937(),
107  bdata::distribution =
108  std::uniform_real_distribution<>(0, R * 1.5))) ^
109  bdata::random((bdata::seed = 3, bdata::engine = std::mt19937(),
110  bdata::distribution =
111  std::uniform_real_distribution<>(-M_PI, M_PI))) ^
112  bdata::xrange(ntests),
113  z, r, phi, index) {
114  (void)index;
115  if (index % 1000 == 0) {
116  std::cout << index << std::endl;
117  }
118 
119  Vector3D pos(r * std::cos(phi), r * std::sin(phi), z);
121  Vector3D Bm = bFieldMap.getField(pos) / Acts::UnitConstants::T;
122 
123  // test less than 5% deviation
124  if (std::abs(r - R) > 10 && (std::abs(z) < L / 3. || r > 20)) {
125  // only if more than 10mm away from coil for all z
126  // only if not close to r=0 for large z
127  CHECK_CLOSE_REL(Bm.norm(), B.norm(), 0.05);
128  }
129 
130  std::ofstream& ofstr = valid.m_ofstr;
131  ofstr << pos.x() << ";" << pos.y() << ";" << pos.z() << ";";
132  ofstr << B.x() << ";" << B.y() << ";" << B.z() << ";";
133  ofstr << Bm.x() << ";" << Bm.y() << ";" << Bm.z() << std::endl;
134 }
135 
136 } // namespace IntegrationTest
137 } // namespace Acts