Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-18 08:23:07

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