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

Implement detector action for optical physics #1648

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/celeritas/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ celeritas_polysource(optical/action/BoundaryAction)
celeritas_polysource(optical/action/InitializeTracksAction)
celeritas_polysource(optical/action/PreStepAction)
celeritas_polysource(optical/action/TrackingCutAction)
celeritas_polysource(optical/action/CaloAction)
celeritas_polysource(optical/action/detail/TrackInitAlgorithms)
celeritas_polysource(optical/detail/CherenkovGeneratorAction)
celeritas_polysource(optical/detail/CherenkovOffloadAction)
Expand Down
26 changes: 26 additions & 0 deletions src/celeritas/optical/CoreParams.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
//---------------------------------------------------------------------------//
#include "CoreParams.hh"

#include "corecel/io/Join.hh"
#include "corecel/io/Logger.hh"
#include "corecel/sys/ActionRegistry.hh"
#include "corecel/sys/ScopedMem.hh"
#include "geocel/GeoVolumeFinder.hh"
#include "celeritas/geo/GeoParams.hh"
#include "celeritas/mat/MaterialParams.hh"
#include "celeritas/random/RngParams.hh"
Expand All @@ -20,6 +22,7 @@
#include "TrackInitParams.hh"
#include "action/AlongStepAction.hh"
#include "action/BoundaryAction.hh"
#include "action/CaloAction.hh"
#include "action/InitializeTracksAction.hh"
#include "action/LocateVacanciesAction.hh"
#include "action/PreStepAction.hh"
Expand Down Expand Up @@ -89,6 +92,8 @@ CoreScalars build_actions(ActionRegistry* reg)
scalars.tracking_cut_action = reg->next_id();
reg->insert(make_shared<TrackingCutAction>(scalars.tracking_cut_action));

reg->insert(make_shared<CaloAction>(reg->next_id()));

//// END ACTIONS ////

reg->insert(make_shared<LocateVacanciesAction>(reg->next_id()));
Expand Down Expand Up @@ -129,6 +134,27 @@ CoreParams::CoreParams(Input&& input) : input_(std::move(input))
// Save maximum number of streams
scalars.max_streams = input_.max_streams;

// Map detector labels to volume IDs
VecLabel const& labels = detector_labels();
std::vector<std::reference_wrapper<Label const>> missing;
detector_ids_.resize(labels.size());

auto geo = this->geometry();
GeoVolumeFinder find_volume(*geo);
for (auto i : range(labels.size()))
{
detector_ids_[i] = find_volume(labels[i]);
if (!detector_ids_[i])
{
missing.emplace_back(labels[i]);
}
}

CELER_VALIDATE(missing.empty(),
<< "failed to find " << celeritas_core_geo
<< " volume(s) for labels '"
<< join(missing.begin(), missing.end(), "', '"));

// Save host reference
host_ref_ = build_params_refs<MemSpace::host>(input_, scalars);
if (celeritas::device())
Expand Down
9 changes: 9 additions & 0 deletions src/celeritas/optical/CoreParams.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "corecel/data/DeviceVector.hh"
#include "corecel/data/ObserverPtr.hh"
#include "corecel/data/ParamsDataInterface.hh"
#include "corecel/io/Label.hh"
#include "celeritas/geo/GeoFwd.hh"
#include "celeritas/random/RngParamsFwd.hh"

Expand Down Expand Up @@ -41,6 +42,7 @@ class CoreParams final : public ParamsDataInterface<CoreParamsData>
using SPConstRng = std::shared_ptr<RngParams const>;
using SPConstTrackInit = std::shared_ptr<TrackInitParams const>;
using SPActionRegistry = std::shared_ptr<ActionRegistry>;
using VecLabel = std::vector<Label>;

template<MemSpace M>
using ConstRef = CoreParamsData<Ownership::const_reference, M>;
Expand All @@ -58,6 +60,8 @@ class CoreParams final : public ParamsDataInterface<CoreParamsData>

SPActionRegistry action_reg;

VecLabel detector_labels;

//! Maximum number of simultaneous threads/tasks per process
StreamId::size_type max_streams{1};

Expand Down Expand Up @@ -89,6 +93,8 @@ class CoreParams final : public ParamsDataInterface<CoreParamsData>
SPConstRng const& rng() const { return input_.rng; }
SPConstTrackInit const& init() const { return input_.init; }
SPActionRegistry const& action_reg() const { return input_.action_reg; }
VecLabel const& detector_labels() const { return input_.detector_labels; }
std::vector<VolumeId> const& detector_ids() const { return detector_ids_; }
//!@}

// Access host pointers to core data
Expand All @@ -106,6 +112,9 @@ class CoreParams final : public ParamsDataInterface<CoreParamsData>
HostRef host_ref_;
DeviceRef device_ref_;

// detector volume ids
std::vector<VolumeId> detector_ids_;

// Copy of DeviceRef in device memory
DeviceVector<DeviceRef> device_ref_vec_;
};
Expand Down
1 change: 1 addition & 0 deletions src/celeritas/optical/OpticalCollector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ OpticalCollector::OpticalCollector(CoreParams const& core, Input&& inp)
la_inp.offload = offload_params_;
la_inp.num_track_slots = inp.num_track_slots;
la_inp.initializer_capacity = inp.initializer_capacity;
la_inp.detector_labels = inp.detector_labels;
launch_action_ = detail::OpticalLaunchAction::make_and_insert(
core, std::move(la_inp));

Expand Down
4 changes: 4 additions & 0 deletions src/celeritas/optical/OpticalCollector.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <memory>

#include "corecel/data/AuxInterface.hh"
#include "corecel/io/Label.hh"
#include "celeritas/Types.hh"

#include "OffloadData.hh"
Expand Down Expand Up @@ -87,6 +88,9 @@ class OpticalCollector
//! Threshold number of initializers for launching optical loop
size_type auto_flush{};

//! Labels of volumes corresponding to optical detectors
std::vector<Label> detector_labels{};

//! True if all input is assigned and valid
explicit operator bool() const
{
Expand Down
55 changes: 55 additions & 0 deletions src/celeritas/optical/action/CaloAction.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//------------------------------- -*- C++ -*- -------------------------------//
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/optical/action/CaloAction.cc
//---------------------------------------------------------------------------//
#include "CaloAction.hh"

#include "celeritas/optical/CoreParams.hh"
#include "celeritas/optical/CoreState.hh"

#include "ActionLauncher.hh"
#include "TrackSlotExecutor.hh"

#include "detail/CaloExecutor.hh"

namespace celeritas
{
namespace optical
{
//---------------------------------------------------------------------------//
/*!
* Construct with action ID.
*/
CaloAction::CaloAction(ActionId aid)
: StaticConcreteAction(aid, "track-calo", "contribute to detector")
{
}

//---------------------------------------------------------------------------//
/*!
* Launch the boundary action on host.
*/
void CaloAction::step(CoreParams const& params, CoreStateHost& state) const
{
auto detector_ids = params.detector_ids();
auto detect_and_update
= [](CoreTrackView& track, std::vector<VolumeId>& detector_ids) {
detail::CaloExecutor{}(track, detector_ids);
};
auto execute = make_active_thread_executor(
params.ptr<MemSpace::native>(), state.ptr(), detect_and_update);
return launch_action(state, execute);
}

#if !CELER_USE_DEVICE
void CaloAction::step(CoreParams const&, CoreStateDevice&) const
{
CELER_NOT_CONFIGURED("CUDA OR HIP");
}
#endif

//---------------------------------------------------------------------------//
} // namespace optical
} // namespace celeritas
39 changes: 39 additions & 0 deletions src/celeritas/optical/action/CaloAction.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//------------------------------- -*- C++ -*- -------------------------------//
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/optical/action/CaloAction.hh
//---------------------------------------------------------------------------//
#pragma once

#include "ActionInterface.hh"

namespace celeritas
{
namespace optical
{
//---------------------------------------------------------------------------//
/*!
* Check track interaction with optical detector, deposit (print) energy and
* kill track
*/
class CaloAction final : public OpticalStepActionInterface,
public StaticConcreteAction
{
public:
// Construct with ID
explicit CaloAction(ActionId);

// Launch kernel with host data
void step(CoreParams const&, CoreStateHost&) const final;

// Launch kernel with device data
void step(CoreParams const&, CoreStateDevice&) const final;

//! Dependency ordering of the action
StepActionOrder order() const final { return StepActionOrder::user_post; }
};

//---------------------------------------------------------------------------//
} // namespace optical
} // namespace celeritas
74 changes: 74 additions & 0 deletions src/celeritas/optical/action/detail/CaloExecutor.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//------------------------------- -*- C++ -*- -------------------------------//
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/optical/action/detail/AlongStepExecutor.hh
//---------------------------------------------------------------------------//
#pragma once

#include "corecel/Assert.hh"
#include "corecel/Macros.hh"
#include "celeritas/Types.hh"
#include "celeritas/optical/CoreTrackView.hh"
#include "celeritas/optical/SimTrackView.hh"

namespace celeritas
{
namespace optical
{
namespace detail
{
//---------------------------------------------------------------------------//
/*!
* Complete end-of-step activity for a track.
*
* - Update track time
* - Update number of steps
* - Update remaining MFPs to interaction
*/
struct CaloExecutor
{
inline CELER_FUNCTION void
operator()(CoreTrackView& track, std::vector<VolumeId>& detector_ids);
};

//---------------------------------------------------------------------------//
CELER_FUNCTION void
CaloExecutor::operator()(CoreTrackView& track,
std::vector<VolumeId>& detector_ids)
{
auto sim = track.sim();
// If track previously killed in step, don't contribute
if (sim.status() == TrackStatus::killed)
{
return;
}

// If track is alive, check for detection
if (sim.status() == TrackStatus::alive)
{
// Extract track geometry and current volume ID
auto geo = track.geometry();
auto v_id = geo.volume_id();

// check for track geometry in optical detector list
// TODO:: fix below pseudo-code
for (auto det_id : range(detector_ids.size()))
{
if (v_id == detector_ids[det_id])
{
auto energy = track.particle().energy().value();
std::cout << "Killing track in Volume " << v_id.get()
<< " with energy " << energy << std::endl;
sim.status(TrackStatus::killed);
}
}

// If found, print energy, print volume, and kill track
}
}

//---------------------------------------------------------------------------//
} // namespace detail
} // namespace optical
} // namespace celeritas
4 changes: 4 additions & 0 deletions src/celeritas/optical/detail/OpticalLaunchAction.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "corecel/Macros.hh"
#include "corecel/data/AuxInterface.hh"
#include "corecel/io/Label.hh"
#include "celeritas/global/ActionInterface.hh"

namespace celeritas
Expand Down Expand Up @@ -51,6 +52,7 @@ class OpticalLaunchAction : public AuxParamsInterface,
//! \name Type aliases
using SPOffloadParams = std::shared_ptr<detail::OffloadParams>;
using SPConstMaterial = std::shared_ptr<optical::MaterialParams const>;
using VecLabel = std::vector<Label>;
//!@}

struct Input
Expand All @@ -60,6 +62,8 @@ class OpticalLaunchAction : public AuxParamsInterface,
size_type num_track_slots{};
size_type initializer_capacity{};

VecLabel detector_labels{};

//! True if all input is assigned and valid
explicit operator bool() const
{
Expand Down
Loading