Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-03 07:53:57

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 <memory>
0020 #include <numbers>
0021 #include <vector>
0022 
0023 #include <nlohmann/json.hpp>
0024 
0025 BOOST_AUTO_TEST_SUITE(MaterialJsonIO)
0026 
0027 BOOST_AUTO_TEST_CASE(IndexedSurfaceMaterial1DTests) {
0028   std::vector<Acts::MaterialSlab> material;
0029   material.emplace_back(Acts::Material::Vacuum(), 0.0);  // vacuum
0030   material.emplace_back(
0031       Acts::Material::fromMolarDensity(1.0, 2.0, 3.0, 4.0, 5.0), 1.0);
0032   material.emplace_back(
0033       Acts::Material::fromMolarDensity(11.0, 12.0, 13.0, 14.0, 15.0), 2.0);
0034   material.emplace_back(
0035       Acts::Material::fromMolarDensity(21.0, 22.0, 23.0, 24.0, 25.0), 3.0);
0036 
0037   using EqBound = Acts::GridAxisGenerators::EqBound;
0038   using EqGrid = EqBound::grid_type<std::size_t>;
0039   using Point = EqGrid::point_t;
0040 
0041   EqBound eqBound{{0., 5.}, 5};
0042   EqGrid eqGrid{eqBound()};
0043 
0044   eqGrid.atPosition(Point{0.5}) = 1u;  // material 1
0045   eqGrid.atPosition(Point{1.5}) = 0u;  // vacuum
0046   eqGrid.atPosition(Point{2.5}) = 2u;  // material 2
0047   eqGrid.atPosition(Point{3.5}) = 2u;  // material 2
0048   eqGrid.atPosition(Point{4.5}) = 3u;  // material 3
0049 
0050   auto localX = std::make_unique<const Acts::GridAccess::LocalSubspace<0u>>();
0051   Acts::IndexedSurfaceMaterial<EqGrid>::BoundToGridLocalDelegate bToX;
0052   bToX.connect<&Acts::GridAccess::LocalSubspace<0u>::toGridLocal>(
0053       std::move(localX));
0054 
0055   auto globalX = std::make_unique<
0056       const Acts::GridAccess::GlobalSubspace<Acts::AxisDirection::AxisX>>();
0057   Acts::IndexedSurfaceMaterial<EqGrid>::GlobalToGridLocalDelegate gToX;
0058   gToX.connect<&Acts::GridAccess::GlobalSubspace<
0059       Acts::AxisDirection::AxisX>::toGridLocal>(std::move(globalX));
0060 
0061   Acts::IndexedSurfaceMaterial<EqGrid> ism(
0062       std::move(eqGrid), Acts::IndexedMaterialAccessor{std::move(material)},
0063       std::move(bToX), std::move(gToX));
0064 
0065   nlohmann::json jMaterial = &ism;
0066 
0067   // Run a few tests
0068   BOOST_REQUIRE(jMaterial.find("material") != jMaterial.end());
0069 
0070   // Read it back in
0071   const Acts::ISurfaceMaterial* ismRead = nullptr;
0072   Acts::from_json(jMaterial, ismRead);
0073   BOOST_REQUIRE(ismRead != nullptr);
0074 
0075   // Check if it's the right type
0076   const Acts::IndexedSurfaceMaterial<EqGrid>* ismReadTyped =
0077       dynamic_cast<const Acts::IndexedSurfaceMaterial<EqGrid>*>(ismRead);
0078   BOOST_REQUIRE(ismReadTyped != nullptr);
0079 
0080   const auto& gridRead = ismReadTyped->grid();
0081   BOOST_CHECK(gridRead.atPosition(Point{0.5}) == 1u);  // material 1
0082   BOOST_CHECK(gridRead.atPosition(Point{1.5}) == 0u);  // vacuum
0083   BOOST_CHECK(gridRead.atPosition(Point{2.5}) == 2u);  // material 2
0084   BOOST_CHECK(gridRead.atPosition(Point{3.5}) == 2u);  // material 2
0085   BOOST_CHECK(gridRead.atPosition(Point{4.5}) == 3u);  // material 3
0086 
0087   // Check the accessor is there and the material is filled
0088   const auto& accessorRead = ismReadTyped->materialAccessor();
0089 
0090   auto materialRead = accessorRead.material;
0091   BOOST_REQUIRE(materialRead.size() == 4);
0092   CHECK_CLOSE_ABS(accessorRead.material[0].thickness(), 0.0, 1e-5);
0093   CHECK_CLOSE_ABS(accessorRead.material[1].thickness(), 1.0, 1e-5);
0094   CHECK_CLOSE_ABS(accessorRead.material[2].thickness(), 2.0, 1e-5);
0095   CHECK_CLOSE_ABS(accessorRead.material[3].thickness(), 3.0, 1e-5);
0096 }
0097 
0098 BOOST_AUTO_TEST_CASE(IndexedSurfaceMaterial2DTests) {
0099   std::vector<Acts::MaterialSlab> material;
0100   material.emplace_back(Acts::Material::Vacuum(), 1.0);  // vacuum
0101   material.emplace_back(
0102       Acts::Material::fromMolarDensity(1.0, 2.0, 3.0, 4.0, 5.0), 1.0);
0103   material.emplace_back(
0104       Acts::Material::fromMolarDensity(11.0, 12.0, 13.0, 14.0, 15.0), 1.0);
0105   material.emplace_back(
0106       Acts::Material::fromMolarDensity(21.0, 22.0, 23.0, 24.0, 25.0), 1.0);
0107 
0108   using EqBoundEqClosed = Acts::GridAxisGenerators::EqBoundEqClosed;
0109   using EqEqGrid = EqBoundEqClosed::grid_type<std::size_t>;
0110   using Point = EqEqGrid::point_t;
0111 
0112   EqBoundEqClosed eqeqBound{
0113       {-1., 1.}, 2, {-std::numbers::pi, std::numbers::pi}, 4};
0114   EqEqGrid eqeqGrid{eqeqBound()};
0115 
0116   eqeqGrid.atPosition(Point{-0.5, -std::numbers::pi * 0.75}) =
0117       1u;                                                          // material 1
0118   eqeqGrid.atPosition(Point{-0.5, -std::numbers::pi / 4.}) = 1u;   // material 1
0119   eqeqGrid.atPosition(Point{-0.5, std::numbers::pi / 4.}) = 0u;    // vacuum
0120   eqeqGrid.atPosition(Point{-0.5, std::numbers::pi * 0.75}) = 2u;  // material 2
0121 
0122   eqeqGrid.atPosition(Point{0.5, -std::numbers::pi * 0.75}) = 0u;  // vacuum
0123   eqeqGrid.atPosition(Point{0.5, -std::numbers::pi / 4.}) = 3u;    // material 3
0124   eqeqGrid.atPosition(Point{0.5, std::numbers::pi / 4.}) = 3u;     // material 3
0125   eqeqGrid.atPosition(Point{0.5, std::numbers::pi * 0.75}) = 0u;   // vacuum
0126 
0127   auto boundToGrid =
0128       std::make_unique<const Acts::GridAccess::LocalSubspace<0u, 1u>>();
0129   Acts::IndexedSurfaceMaterial<EqEqGrid>::BoundToGridLocalDelegate bToZPhi;
0130   bToZPhi.connect<&Acts::GridAccess::LocalSubspace<0u, 1u>::toGridLocal>(
0131       std::move(boundToGrid));
0132 
0133   // With z shift 10
0134   auto globalToGrid = std::make_unique<const Acts::GridAccess::GlobalSubspace<
0135       Acts::AxisDirection::AxisZ, Acts::AxisDirection::AxisPhi>>();
0136   Acts::IndexedSurfaceMaterial<EqEqGrid>::GlobalToGridLocalDelegate gToZphi;
0137   gToZphi.connect<&Acts::GridAccess::GlobalSubspace<
0138       Acts::AxisDirection::AxisZ, Acts::AxisDirection::AxisPhi>::toGridLocal>(
0139       std::move(globalToGrid));
0140 
0141   // Create the indexed material grid
0142   Acts::IndexedSurfaceMaterial<EqEqGrid> ism(
0143       std::move(eqeqGrid), Acts::IndexedMaterialAccessor{std::move(material)},
0144       std::move(bToZPhi), std::move(gToZphi));
0145 
0146   nlohmann::json jMaterial = &ism;
0147 
0148   // Run a few tests
0149   BOOST_REQUIRE(jMaterial.find("material") != jMaterial.end());
0150 
0151   // Read it back in
0152   const Acts::ISurfaceMaterial* ismRead = nullptr;
0153   Acts::from_json(jMaterial, ismRead);
0154   BOOST_REQUIRE(ismRead != nullptr);
0155 }
0156 
0157 BOOST_AUTO_TEST_SUITE_END()