Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tests for backward propagation #896

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 53 additions & 10 deletions tests/integration_tests/cpu/propagator/backward_propagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "detray/propagator/actor_chain.hpp"
#include "detray/propagator/actors/parameter_resetter.hpp"
#include "detray/propagator/actors/parameter_transporter.hpp"
#include "detray/propagator/actors/pointwise_material_interactor.hpp"
#include "detray/propagator/propagator.hpp"
#include "detray/propagator/rk_stepper.hpp"
#include "detray/tracks/tracks.hpp"
Expand All @@ -37,40 +38,50 @@ using point2 = test::point2;
using vector3 = test::vector3;
using matrix_operator = test::matrix_operator;

constexpr test::scalar tol{5e-3f};
// Test class for the backward propagation
// Input tuple: < std::vector<plane positions>, tolerance >
class BackwardPropagation
: public ::testing::TestWithParam<
std::tuple<std::vector<test::scalar>, test::scalar>> {};

GTEST_TEST(detray_propagator, backward_propagation) {
TEST_P(BackwardPropagation, backward_propagation) {

const test::scalar tol = std::get<1>(GetParam());

vecmem::host_memory_resource host_mr;

// Build in x-direction from given module positions
detail::ray<algebra_t> traj{{0.f, 0.f, 0.f}, 0.f, {1.f, 0.f, 0.f}, -1.f};
std::vector<test::scalar> positions = {0.f, 10.f, 20.f, 30.f, 40.f, 50.f,
60.f, 70.f, 80.f, 90.f, 100.f};
std::vector<test::scalar> positions = std::get<0>(GetParam());

tel_det_config<rectangle2D> tel_cfg{200.f * unit<test::scalar>::mm,
200.f * unit<test::scalar>::mm};
tel_cfg.positions(positions).pilot_track(traj);
tel_cfg.positions(positions).pilot_track(traj).mat_thickness(
10.f * unit<test::scalar>::mm);

// Build telescope detector with rectangular planes
const auto [det, names] = build_telescope_detector(host_mr, tel_cfg);

// Create b field
using bfield_t = bfield::const_field_t;
vector3 B{1.f * unit<test::scalar>::T, 1.f * unit<test::scalar>::T,
vector3 B{0.f * unit<test::scalar>::T, 0.f * unit<test::scalar>::T,
1.f * unit<test::scalar>::T};
const bfield_t hom_bfield = bfield::create_const_field(B);

using navigator_t = navigator<decltype(det)>;
using rk_stepper_t = rk_stepper<bfield_t::view_t, algebra_t>;
using actor_chain_t = actor_chain<dtuple, parameter_transporter<algebra_t>,
pointwise_material_interactor<algebra_t>,
parameter_resetter<algebra_t>>;
using propagator_t = propagator<rk_stepper_t, navigator_t, actor_chain_t>;

// Particle hypothesis
pdg_particle<test::scalar> ptc = muon<test::scalar>();

// Bound vector
bound_parameters_vector<algebra_t> bound_vector{};
bound_vector.set_theta(constant<test::scalar>::pi_2);
bound_vector.set_qop(-1.f);
bound_vector.set_qop(ptc.charge() / (1.f * unit<test::scalar>::GeV));

// Bound covariance
typename bound_track_parameters<algebra_t>::covariance_type bound_cov =
Expand All @@ -82,6 +93,7 @@ GTEST_TEST(detray_propagator, backward_propagation) {

// Actors
parameter_transporter<algebra_t>::state bound_updater{};
pointwise_material_interactor<algebra_t>::state interactor{};
parameter_resetter<algebra_t>::state rst{};

propagation::config prop_cfg{};
Expand All @@ -92,10 +104,11 @@ GTEST_TEST(detray_propagator, backward_propagation) {
// Forward state
propagator_t::state fw_state(bound_param0, hom_bfield, det,
prop_cfg.context);
fw_state.set_particle(ptc);
fw_state.do_debug = true;

// Run propagator
p.propagate(fw_state, detray::tie(bound_updater, rst));
p.propagate(fw_state, detray::tie(bound_updater, interactor, rst));

// Print the debug stream
// std::cout << fw_state.debug_stream.str() << std::endl;
Expand All @@ -108,16 +121,17 @@ GTEST_TEST(detray_propagator, backward_propagation) {
EXPECT_EQ(bound_param0.surface_link().index(), 0u);
EXPECT_EQ(bound_param1.surface_link().volume(), 0u);
EXPECT_EQ(bound_param1.surface_link().id(), surface_id::e_sensitive);
EXPECT_EQ(bound_param1.surface_link().index(), 10u);
EXPECT_EQ(bound_param1.surface_link().index(), positions.size() - 1u);

// Backward state
propagator_t::state bw_state(bound_param1, hom_bfield, det,
prop_cfg.context);
bw_state.set_particle(ptc);
bw_state.do_debug = true;
bw_state._navigation.set_direction(navigation::direction::e_backward);

// Run propagator
p.propagate(bw_state, detray::tie(bound_updater, rst));
p.propagate(bw_state, detray::tie(bound_updater, interactor, rst));

// Print the debug stream
// std::cout << bw_state.debug_stream.str() << std::endl;
Expand All @@ -140,6 +154,7 @@ GTEST_TEST(detray_propagator, backward_propagation) {
}

const auto bound_cov0 = bound_param0.covariance();
const auto bound_cov1 = bound_param1.covariance();
const auto bound_cov2 = bound_param2.covariance();

// Check covaraince
Expand All @@ -149,4 +164,32 @@ GTEST_TEST(detray_propagator, backward_propagation) {
matrix_operator().element(bound_cov2, i, j), tol);
}
}

// Some sanity checks
EXPECT_TRUE(bound_param0.p(ptc.charge()) > bound_param1.p(ptc.charge()));
EXPECT_TRUE(bound_param2.p(ptc.charge()) > bound_param1.p(ptc.charge()));

EXPECT_TRUE(getter::element(bound_cov1, e_bound_qoverp, e_bound_qoverp) >
getter::element(bound_cov0, e_bound_qoverp, e_bound_qoverp));
EXPECT_TRUE(getter::element(bound_cov1, e_bound_theta, e_bound_theta) >
getter::element(bound_cov0, e_bound_theta, e_bound_theta));
EXPECT_TRUE(getter::element(bound_cov1, e_bound_phi, e_bound_phi) >
getter::element(bound_cov0, e_bound_phi, e_bound_phi));

EXPECT_TRUE(getter::element(bound_cov1, e_bound_qoverp, e_bound_qoverp) >
getter::element(bound_cov2, e_bound_qoverp, e_bound_qoverp));
EXPECT_TRUE(getter::element(bound_cov1, e_bound_theta, e_bound_theta) >
getter::element(bound_cov2, e_bound_theta, e_bound_theta));
EXPECT_TRUE(getter::element(bound_cov1, e_bound_phi, e_bound_phi) >
getter::element(bound_cov2, e_bound_phi, e_bound_phi));
}

INSTANTIATE_TEST_SUITE_P(
telescope, BackwardPropagation,
::testing::Values(
std::make_tuple(std::vector<test::scalar>{0.f}, 1e-5f),
std::make_tuple(std::vector<test::scalar>{0.f, 10.f}, 1e-3f),
std::make_tuple(std::vector<test::scalar>{0.f, 10.f, 20.f, 30.f, 40.f,
50.f, 60.f, 70.f, 80.f, 90.f,
100.f},
1e-2f)));
Loading