Analysis Software
Documentation for sPHENIX simulation 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 
20 
21 #include <cmath>
22 #include <fstream>
23 #include <iostream>
24 #include <random>
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 
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 map =
58  solenoidFieldMap({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 = map.getGrid();
64  using index_t = Grid_t::index_t;
65  using point_t = Grid_t::point_t;
66 
67  for (size_t i = 0; i <= nBinsR + 1; i++) {
68  for (size_t j = 0; j <= nBinsZ + 1; j++) {
69  // std::cout << "(i,j) = " << i << "," << j << std::endl;
70  index_t index({i, j});
71  if (i == 0 || j == 0 || i == nBinsR + 1 || j == nBinsZ + 1) {
72  // under or overflow bin
73  } else {
74  point_t lowerLeft = grid.lowerLeftBinEdge(index);
75  Vector2 B = grid.atLocalBins(index);
76  ostr << i << ";" << j << ";" << lowerLeft[0] << ";" << lowerLeft[1];
77  ostr << ";" << B[0] << ";" << B[1] << std::endl;
78  }
79  }
80  }
81 
82  return map;
83 }
84 
88 
89 struct StreamWrapper {
90  StreamWrapper(std::ofstream ofstr) : m_ofstr(std::move(ofstr)) {
91  m_ofstr << "x;y;z;B_x;B_y;B_z;Bm_x;Bm_y;Bm_z" << std::endl;
92  }
93 
94  std::ofstream m_ofstr;
95 };
96 
97 StreamWrapper valid(std::ofstream("magfield_lookup.csv"));
98 
99 const int ntests = 10000;
101  solenoid_interpolated_bfield_comparison,
102  bdata::random((bdata::seed = 1, bdata::engine = std::mt19937(),
103  bdata::distribution = std::uniform_real_distribution<>(
104  1.5 * (-L / 2.), 1.5 * L / 2.))) ^
105  bdata::random((bdata::seed = 2, bdata::engine = std::mt19937(),
106  bdata::distribution =
107  std::uniform_real_distribution<>(0, R * 1.5))) ^
108  bdata::random((bdata::seed = 3, bdata::engine = std::mt19937(),
109  bdata::distribution =
110  std::uniform_real_distribution<>(-M_PI, M_PI))) ^
111  bdata::xrange(ntests),
112  z, r, phi, index) {
113  (void)index;
114  if (index % 1000 == 0) {
115  std::cout << index << std::endl;
116  }
117 
118  Vector3 pos(r * std::cos(phi), r * std::sin(phi), z);
120  Vector3 Bm = bFieldMap.getField(pos, bCache).value() / Acts::UnitConstants::T;
121 
122  // test less than 5% deviation
123  if (std::abs(r - R) > 10 && (std::abs(z) < L / 3. || r > 20)) {
124  // only if more than 10mm away from coil for all z
125  // only if not close to r=0 for large z
126  CHECK_CLOSE_REL(Bm.norm(), B.norm(), 0.05);
127  }
128 
129  std::ofstream& ofstr = valid.m_ofstr;
130  ofstr << pos.x() << ";" << pos.y() << ";" << pos.z() << ";";
131  ofstr << B.x() << ";" << B.y() << ";" << B.z() << ";";
132  ofstr << Bm.x() << ";" << Bm.y() << ";" << Bm.z() << std::endl;
133 }
134 
135 } // namespace IntegrationTest
136 } // namespace Acts