Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:13:11

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Material/GridSurfaceMaterial.hpp"
0012 #include "Acts/Material/Material.hpp"
0013 #include "Acts/Material/MaterialSlab.hpp"
0014 #include "Acts/Plugins/Json/MaterialJsonConverter.hpp"
0015 #include "Acts/Tests/CommonHelpers/FloatComparisons.hpp"
0016 #include "Acts/Utilities/GridAccessHelpers.hpp"
0017 #include "Acts/Utilities/GridAxisGenerators.hpp"
0018 
0019 #include <array>
0020 #include <fstream>
0021 #include <memory>
0022 #include <numbers>
0023 #include <vector>
0024 
0025 #include <nlohmann/json.hpp>
0026 
0027 BOOST_AUTO_TEST_SUITE(MaterialJsonIO)
0028 
0029 BOOST_AUTO_TEST_CASE(IndexedSurfaceMaterial1DTests) {
0030   std::vector<Acts::MaterialSlab> material;
0031   material.emplace_back(Acts::Material(), 0.0);  // vacuum
0032   material.emplace_back(
0033       Acts::Material::fromMolarDensity(1.0, 2.0, 3.0, 4.0, 5.0), 1.0);
0034   material.emplace_back(
0035       Acts::Material::fromMolarDensity(11.0, 12.0, 13.0, 14.0, 15.0), 2.0);
0036   material.emplace_back(
0037       Acts::Material::fromMolarDensity(21.0, 22.0, 23.0, 24.0, 25.0), 3.0);
0038 
0039   using EqBound = Acts::GridAxisGenerators::EqBound;
0040   using EqGrid = EqBound::grid_type<std::size_t>;
0041   using Point = EqGrid::point_t;
0042 
0043   EqBound eqBound{{0., 5.}, 5};
0044   EqGrid eqGrid{eqBound()};
0045 
0046   eqGrid.atPosition(Point{0.5}) = 1u;  // material 1
0047   eqGrid.atPosition(Point{1.5}) = 0u;  // vacuum
0048   eqGrid.atPosition(Point{2.5}) = 2u;  // material 2
0049   eqGrid.atPosition(Point{3.5}) = 2u;  // material 2
0050   eqGrid.atPosition(Point{4.5}) = 3u;  // material 3
0051 
0052   auto localX = std::make_unique<const Acts::GridAccess::LocalSubspace<0u>>();
0053   Acts::IndexedSurfaceMaterial<EqGrid>::BoundToGridLocalDelegate bToX;
0054   bToX.connect<&Acts::GridAccess::LocalSubspace<0u>::toGridLocal>(
0055       std::move(localX));
0056 
0057   auto globalX = std::make_unique<
0058       const Acts::GridAccess::GlobalSubspace<Acts::AxisDirection::AxisX>>();
0059   Acts::IndexedSurfaceMaterial<EqGrid>::GlobalToGridLocalDelegate gToX;
0060   gToX.connect<&Acts::GridAccess::GlobalSubspace<
0061       Acts::AxisDirection::AxisX>::toGridLocal>(std::move(globalX));
0062 
0063   Acts::IndexedSurfaceMaterial<EqGrid> ism(
0064       std::move(eqGrid), Acts::IndexedMaterialAccessor{std::move(material)},
0065       std::move(bToX), std::move(gToX));
0066 
0067   nlohmann::json jMaterial = &ism;
0068 
0069   // Run a few tests
0070   BOOST_REQUIRE(jMaterial.find("material") != jMaterial.end());
0071 
0072   // Read it back in
0073   const Acts::ISurfaceMaterial* ismRead = nullptr;
0074   Acts::from_json(jMaterial, ismRead);
0075   BOOST_REQUIRE(ismRead != nullptr);
0076 
0077   // Check if it's the right type
0078   const Acts::IndexedSurfaceMaterial<EqGrid>* ismReadTyped =
0079       dynamic_cast<const Acts::IndexedSurfaceMaterial<EqGrid>*>(ismRead);
0080   BOOST_REQUIRE(ismReadTyped != nullptr);
0081 
0082   auto gridRead = ismReadTyped->grid();
0083   BOOST_CHECK(gridRead.atPosition(Point{0.5}) == 1u);  // material 1
0084   BOOST_CHECK(gridRead.atPosition(Point{1.5}) == 0u);  // vacuum
0085   BOOST_CHECK(gridRead.atPosition(Point{2.5}) == 2u);  // material 2
0086   BOOST_CHECK(gridRead.atPosition(Point{3.5}) == 2u);  // material 2
0087   BOOST_CHECK(gridRead.atPosition(Point{4.5}) == 3u);  // material 3
0088 
0089   // Check the accessor is there and the material is filled
0090   auto accessorRead = ismReadTyped->materialAccessor();
0091   CHECK_CLOSE_ABS(accessorRead.material[0].thickness(), 0.0, 1e-5);
0092   CHECK_CLOSE_ABS(accessorRead.material[1].thickness(), 1.0, 1e-5);
0093   CHECK_CLOSE_ABS(accessorRead.material[2].thickness(), 2.0, 1e-5);
0094   CHECK_CLOSE_ABS(accessorRead.material[3].thickness(), 3.0, 1e-5);
0095 }
0096 
0097 BOOST_AUTO_TEST_CASE(IndexedSurfaceMaterial2DTests) {
0098   std::vector<Acts::MaterialSlab> material;
0099   material.emplace_back(Acts::Material(), 1.0);  // vacuum
0100   material.emplace_back(
0101       Acts::Material::fromMolarDensity(1.0, 2.0, 3.0, 4.0, 5.0), 1.0);
0102   material.emplace_back(
0103       Acts::Material::fromMolarDensity(11.0, 12.0, 13.0, 14.0, 15.0), 1.0);
0104   material.emplace_back(
0105       Acts::Material::fromMolarDensity(21.0, 22.0, 23.0, 24.0, 25.0), 1.0);
0106 
0107   using EqBoundEqClosed = Acts::GridAxisGenerators::EqBoundEqClosed;
0108   using EqEqGrid = EqBoundEqClosed::grid_type<std::size_t>;
0109   using Point = EqEqGrid::point_t;
0110 
0111   EqBoundEqClosed eqeqBound{
0112       {-1., 1.}, 2, {-std::numbers::pi, std::numbers::pi}, 4};
0113   EqEqGrid eqeqGrid{eqeqBound()};
0114 
0115   eqeqGrid.atPosition(Point{-0.5, -std::numbers::pi * 0.75}) =
0116       1u;                                                          // material 1
0117   eqeqGrid.atPosition(Point{-0.5, -std::numbers::pi / 4.}) = 1u;   // material 1
0118   eqeqGrid.atPosition(Point{-0.5, std::numbers::pi / 4.}) = 0u;    // vacuum
0119   eqeqGrid.atPosition(Point{-0.5, std::numbers::pi * 0.75}) = 2u;  // material 2
0120 
0121   eqeqGrid.atPosition(Point{0.5, -std::numbers::pi * 0.75}) = 0u;  // vacuum
0122   eqeqGrid.atPosition(Point{0.5, -std::numbers::pi / 4.}) = 3u;    // material 3
0123   eqeqGrid.atPosition(Point{0.5, std::numbers::pi / 4.}) = 3u;     // material 3
0124   eqeqGrid.atPosition(Point{0.5, std::numbers::pi * 0.75}) = 0u;   // vacuum
0125 
0126   auto boundToGrid =
0127       std::make_unique<const Acts::GridAccess::LocalSubspace<0u, 1u>>();
0128   Acts::IndexedSurfaceMaterial<EqEqGrid>::BoundToGridLocalDelegate bToZPhi;
0129   bToZPhi.connect<&Acts::GridAccess::LocalSubspace<0u, 1u>::toGridLocal>(
0130       std::move(boundToGrid));
0131 
0132   // With z shift 10
0133   auto globalToGrid = std::make_unique<const Acts::GridAccess::GlobalSubspace<
0134       Acts::AxisDirection::AxisZ, Acts::AxisDirection::AxisPhi>>();
0135   Acts::IndexedSurfaceMaterial<EqEqGrid>::GlobalToGridLocalDelegate gToZphi;
0136   gToZphi.connect<&Acts::GridAccess::GlobalSubspace<
0137       Acts::AxisDirection::AxisZ, Acts::AxisDirection::AxisPhi>::toGridLocal>(
0138       std::move(globalToGrid));
0139 
0140   // Create the indexed material grid
0141   Acts::IndexedSurfaceMaterial<EqEqGrid> ism(
0142       std::move(eqeqGrid), Acts::IndexedMaterialAccessor{std::move(material)},
0143       std::move(bToZPhi), std::move(gToZphi));
0144 
0145   nlohmann::json jMaterial = &ism;
0146 
0147   // Run a few tests
0148   BOOST_REQUIRE(jMaterial.find("material") != jMaterial.end());
0149 
0150   // Read it back in
0151   const Acts::ISurfaceMaterial* ismRead = nullptr;
0152   Acts::from_json(jMaterial, ismRead);
0153   BOOST_REQUIRE(ismRead != nullptr);
0154 }
0155 
0156 BOOST_AUTO_TEST_SUITE_END()