Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
InterpolatedBFieldMapTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file InterpolatedBFieldMapTests.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 
11 #include <boost/test/unit_test.hpp>
12 
25 
26 #include <array>
27 #include <cstddef>
28 #include <functional>
29 #include <optional>
30 #include <tuple>
31 #include <utility>
32 #include <vector>
33 
34 namespace tt = boost::test_tools;
35 
37 
38 namespace Acts {
39 
40 namespace Test {
41 
42 // Create a test context
44 
45 BOOST_AUTO_TEST_CASE(InterpolatedBFieldMap_rz) {
46  // definition of dummy BField
47  struct BField {
48  static Vector3 value(const std::array<double, 2>& rz) {
49  double r = rz.at(0);
50  double z = rz.at(1);
51  // linear in r and z so interpolation should be exact
52  return Vector3(r * z, 3 * r, -2 * z);
53  }
54  };
55 
56  // map (x,y,z) -> (r,z)
57  auto transformPos = [](const Vector3& pos) {
58  return Vector2(perp(pos), pos.z());
59  };
60 
61  // map (Bx,By,Bz) -> (Bx,By,Bz)
62  auto transformBField = [](const Vector3& field, const Vector3&) {
63  return field;
64  };
65 
66  // magnetic field known on grid in (r,z)
67  detail::EquidistantAxis r(0.0, 4.0, 4u);
68  detail::EquidistantAxis z(-5, 7, 6u);
69 
70  using Grid_t =
72  using BField_t = InterpolatedBFieldMap<Grid_t>;
73 
74  Grid_t g(std::make_tuple(std::move(r), std::move(z)));
75 
76  // set grid values
77  for (size_t i = 1; i <= g.numLocalBins().at(0) + 1; ++i) {
78  for (size_t j = 1; j <= g.numLocalBins().at(1) + 1; ++j) {
79  Grid_t::index_t indices = {{i, j}};
80  const auto& llCorner = g.lowerLeftBinEdge(indices);
81  g.atLocalBins(indices) = BField::value(llCorner);
82  }
83  }
84 
85  // create BField service
86  BField_t b{{transformPos, transformBField, std::move(g)}};
87 
88  auto bCacheAny = b.makeCache(mfContext);
89  BField_t::Cache& bCache = bCacheAny.get<BField_t::Cache>();
90 
91  auto check = [&](double i) {
92  BOOST_CHECK(b.isInside({0, 0, i * 4.9}));
93  BOOST_CHECK(!b.isInside({0, 0, i * 5.1}));
94 
95  BOOST_CHECK(b.isInside({i * 2.9, 0, 0}));
96  BOOST_CHECK(!b.isInside({i * 3.1, 0, 0}));
97 
98  BOOST_CHECK(b.isInside({0, i * 2.9, 0}));
99  BOOST_CHECK(!b.isInside({0, i * 3.1, 0}));
100 
101  BOOST_CHECK(b.isInside({2, 2.2, 0}));
102  BOOST_CHECK(!b.isInside({2, 3, 0}));
103 
104  BOOST_CHECK(b.isInside({i * 2, 2.2, 0}));
105  BOOST_CHECK(!b.isInside({i * 2, 3, 0}));
106 
107  BOOST_CHECK(b.isInside({2, i * 2.2, 0}));
108  BOOST_CHECK(!b.isInside({2, i * 3, 0}));
109 
110  BOOST_CHECK(b.isInside({i * 2, i * 2.2, 0}));
111  BOOST_CHECK(!b.isInside({i * 2, i * 3, 0}));
112  };
113 
114  check(1);
115  check(-1);
116 
117  Vector3 pos;
118  pos << -3, 2.5,
119  1.7; // this was previously checked, but is actually out of bounds
120  BOOST_CHECK(!b.isInside(pos));
121  BOOST_CHECK(!b.getField(pos, bCacheAny).ok());
122 
123  pos << -1.6, 2.5, 1.7;
124  BOOST_CHECK(b.isInside(pos));
125  CHECK_CLOSE_REL(b.getField(pos, bCacheAny).value(),
126  BField::value({{perp(pos), pos.z()}}), 1e-6);
127 
128  auto& c = *bCache.fieldCell;
129  BOOST_CHECK(c.isInside(transformPos(pos)));
130  CHECK_CLOSE_REL(c.getField(transformPos(pos)),
131  BField::value({{perp(pos), pos.z()}}), 1e-6);
132 
133  ActsMatrix<3, 3> deriv;
134 
135  pos << 1, 1, -5.5; // this position is outside the grid
136  BOOST_CHECK(!b.isInside(pos));
137  BOOST_CHECK(!b.getField(pos, bCacheAny).ok());
138  BOOST_CHECK(!b.getFieldGradient(pos, deriv, bCacheAny).ok());
139 
140  pos << 1, 6, -1.7; // this position is outside the grid
141  BOOST_CHECK(!b.isInside(pos));
142  BOOST_CHECK(!b.getField(pos, bCacheAny).ok());
143  BOOST_CHECK(!b.getFieldGradient(pos, deriv, bCacheAny).ok());
144 
145  pos << 0, 1.5, -2.5;
146  BOOST_CHECK(b.isInside(pos));
147  bCacheAny = b.makeCache(mfContext);
148  BField_t::Cache& bCache2 = bCacheAny.get<BField_t::Cache>();
149  CHECK_CLOSE_REL(b.getField(pos, bCacheAny).value(),
150  BField::value({{perp(pos), pos.z()}}), 1e-6);
151  c = *bCache2.fieldCell;
152  BOOST_CHECK(c.isInside(transformPos(pos)));
153  CHECK_CLOSE_REL(c.getField(transformPos(pos)),
154  BField::value({{perp(pos), pos.z()}}), 1e-6);
155 
156  pos << 2, 3, -4;
157  BOOST_CHECK(!b.isInside(pos));
158 
159  pos << 2, 2.2, -4;
160  BOOST_CHECK(b.isInside(pos));
161  bCacheAny = b.makeCache(mfContext);
162  BField_t::Cache& bCache3 = bCacheAny.get<BField_t::Cache>();
163  CHECK_CLOSE_REL(b.getField(pos, bCacheAny).value(),
164  BField::value({{perp(pos), pos.z()}}), 1e-6);
165  c = *bCache3.fieldCell;
166  BOOST_CHECK(c.isInside(transformPos(pos)));
167  CHECK_CLOSE_REL(c.getField(transformPos(pos)),
168  BField::value({{perp(pos), pos.z()}}), 1e-6);
169 
170  // some field cell tests
171  BOOST_CHECK(not c.isInside(transformPos((pos << 3, 2, -3.7).finished())));
172  BOOST_CHECK(not c.isInside(transformPos((pos << -2, 3, -4.7).finished())));
173  BOOST_CHECK(not c.isInside(transformPos((pos << -2, 3, 4.7).finished())));
174  BOOST_CHECK(c.isInside(transformPos((pos << 0, 2, -4.7).finished())));
175  BOOST_CHECK(not c.isInside(transformPos((pos << 5, 2, 14.).finished())));
176 }
177 } // namespace Test
178 
179 } // namespace Acts