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

Log and kill geometry/propagation errors #1290

Closed
wants to merge 3 commits into from
Closed
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 @@ -251,6 +251,7 @@ celeritas_polysource(em/model/RelativisticBremModel)
celeritas_polysource(em/model/SeltzerBergerModel)
celeritas_polysource(em/model/CoulombScatteringModel)
celeritas_polysource(geo/detail/BoundaryAction)
celeritas_polysource(geo/detail/GeoErrorAction)
celeritas_polysource(global/alongstep/AlongStepGeneralLinearAction)
celeritas_polysource(global/alongstep/AlongStepNeutralAction)
celeritas_polysource(global/alongstep/AlongStepUniformMscAction)
Expand Down
1 change: 1 addition & 0 deletions src/celeritas/geo/detail/BoundaryAction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <string>

#include "corecel/Assert.hh"
#include "corecel/Types.hh"
#include "celeritas/global/ActionLauncher.hh"
#include "celeritas/global/CoreParams.hh"
Expand Down
2 changes: 0 additions & 2 deletions src/celeritas/geo/detail/BoundaryAction.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
//---------------------------------------------------------------------------//
#pragma once

#include "corecel/Assert.hh"
#include "corecel/Macros.hh"
#include "celeritas/global/ActionInterface.hh"

namespace celeritas
Expand Down
27 changes: 20 additions & 7 deletions src/celeritas/geo/detail/BoundaryExecutor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,28 @@
#include "../GeoMaterialView.hh"
#include "../GeoTrackView.hh"

#if !CELER_DEVICE_COMPILE
# include "corecel/io/Logger.hh"
#endif

namespace celeritas
{
namespace detail
{
//---------------------------------------------------------------------------//
/*!
* Cross a geometry boundary.
*
* \pre The track must have already been physically moved to the correct point
* on the boundary.
*/
struct BoundaryExecutor
{
inline CELER_FUNCTION void
operator()(celeritas::CoreTrackView const& track);
};

//---------------------------------------------------------------------------//
/*!
* Cross a geometry boundary.
*
* \pre The track must have already been physically moved to the correct point
* on the boundary.
*/
CELER_FUNCTION void
BoundaryExecutor::operator()(celeritas::CoreTrackView const& track)
{
Expand All @@ -54,7 +58,16 @@ BoundaryExecutor::operator()(celeritas::CoreTrackView const& track)
// Update the material in the new region
auto geo_mat = track.make_geo_material_view();
auto matid = geo_mat.material_id(geo.volume_id());
CELER_ASSERT(matid);
if (CELER_UNLIKELY(!matid))
{
#if !CELER_DEVICE_COMPILE
CELER_LOG_LOCAL(error) << "Track entered a volume without an "
"associated material";
Comment on lines +64 to +65
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under what circumstances might this occur?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our GeoMaterial input takes a map of volume name/material IDs and fills the rest with invalid IDs. Those IDs can be legitimate if the volume isn't reachable by the tracking routine (e.g., the [EXTERIOR] volume, or other "imaginary" volumes defined for convenience by vecgeom/g4). However if there's an error in the input or importing or something, you can end up with undefined materials...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, makes sense. In what cases do you think we should be asserting/validating vs. logging an error and killing on the CPU/silently killing the track on the GPU?

#endif
auto sim = track.make_sim_view();
sim.post_step_action(track.geo_error_action());
return;
}
auto mat = track.make_material_view();
mat = {matid};

Expand Down
57 changes: 57 additions & 0 deletions src/celeritas/geo/detail/GeoErrorAction.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//----------------------------------*-C++-*----------------------------------//
// Copyright 2022-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/geo/detail/GeoErrorAction.cc
//---------------------------------------------------------------------------//
#include "GeoErrorAction.hh"

#include <string>

#include "corecel/Assert.hh"
#include "corecel/Types.hh"
#include "celeritas/global/ActionLauncher.hh"
#include "celeritas/global/CoreParams.hh"
#include "celeritas/global/CoreState.hh"
#include "celeritas/global/TrackExecutor.hh"

#include "GeoErrorExecutor.hh" // IWYU pragma: associated

namespace celeritas
{
namespace detail
{
//---------------------------------------------------------------------------//
/*!
* Construct with action ID.
*/
GeoErrorAction::GeoErrorAction(ActionId aid)
: ConcreteAction(aid, "geo-kill", "kill a track due to a navigation error")
{
}

//---------------------------------------------------------------------------//
/*!
* Launch the boundary action on host.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Launch the boundary action on host.
* Launch the geometry error action on host.

*/
void GeoErrorAction::execute(CoreParams const& params,
CoreStateHost& state) const
{
auto execute = make_action_track_executor(params.ptr<MemSpace::native>(),
state.ptr(),
this->action_id(),
GeoErrorExecutor{});
return launch_action(*this, params, state, execute);
}

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

//---------------------------------------------------------------------------//
} // namespace detail
} // namespace celeritas
39 changes: 39 additions & 0 deletions src/celeritas/geo/detail/GeoErrorAction.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//---------------------------------*-CUDA-*----------------------------------//
// Copyright 2022-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/geo/detail/GeoErrorAction.cu
//---------------------------------------------------------------------------//
#include "GeoErrorAction.hh"

#include "celeritas/global/ActionLauncher.device.hh"
#include "celeritas/global/CoreParams.hh"
#include "celeritas/global/CoreState.hh"
#include "celeritas/global/TrackExecutor.hh"

#include "GeoErrorExecutor.hh"

namespace celeritas
{
namespace detail
{
//---------------------------------------------------------------------------//
/*!
* Launch the boundary action on device.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Launch the boundary action on device.
* Launch the geometry error action on device.

*/
void GeoErrorAction::execute(CoreParams const& params,
CoreStateDevice& state) const
{
auto execute = make_action_track_executor(params.ptr<MemSpace::native>(),
state.ptr(),
this->action_id(),
GeoErrorExecutor{});

static ActionLauncher<decltype(execute)> const launch_kernel(*this);
launch_kernel(params, state, *this, execute);
}

//---------------------------------------------------------------------------//
} // namespace detail
} // namespace celeritas
41 changes: 41 additions & 0 deletions src/celeritas/geo/detail/GeoErrorAction.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//----------------------------------*-C++-*----------------------------------//
// Copyright 2022-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/geo/detail/GeoErrorAction.hh
//---------------------------------------------------------------------------//
#pragma once

#include "celeritas/global/ActionInterface.hh"

namespace celeritas
{
namespace detail
{
//---------------------------------------------------------------------------//
/*!
* Kill the track due to a geometry error.
*
* \sa CoreTrackView::geo_error_action
*/
class GeoErrorAction final : public ExplicitCoreActionInterface,
public ConcreteAction
{
public:
// Construct with ID
explicit GeoErrorAction(ActionId);

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

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

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

//---------------------------------------------------------------------------//
} // namespace detail
} // namespace celeritas
80 changes: 80 additions & 0 deletions src/celeritas/geo/detail/GeoErrorExecutor.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//----------------------------------*-C++-*----------------------------------//
// Copyright 2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/geo/detail/GeoErrorExecutor.hh
//---------------------------------------------------------------------------//
#pragma once

#include "corecel/Macros.hh"
#include "celeritas/Types.hh"
#include "celeritas/global/CoreTrackView.hh"
#include "celeritas/phys/ParticleTrackView.hh"
#include "celeritas/track/SimTrackView.hh"

#include "../GeoMaterialView.hh"
#include "../GeoTrackView.hh"

#if !CELER_DEVICE_COMPILE
# include "corecel/io/Logger.hh"
# include "corecel/io/Repr.hh"
#endif

namespace celeritas
{
namespace detail
{
//---------------------------------------------------------------------------//
/*!
* Kill the track due to a geometry error.
*/
struct GeoErrorExecutor
{
inline CELER_FUNCTION void
operator()(celeritas::CoreTrackView const& track);
};

//---------------------------------------------------------------------------//
CELER_FUNCTION void
GeoErrorExecutor::operator()(celeritas::CoreTrackView const& track)
{
using Energy = ParticleTrackView::Energy;

auto particle = track.make_particle_view();
auto sim = track.make_sim_view();

// Deposit the remaining energy locally
auto deposited = particle.energy().value();
if (particle.is_antiparticle())
{
// Energy conservation for killed positrons
deposited += 2 * particle.mass().value();
}
track.make_physics_step_view().deposit_energy(Energy{deposited});
particle.subtract_energy(particle.energy());

// Mark that this track was abandoned while looping
sim.status(TrackStatus::killed);

#if !CELER_DEVICE_COMPILE
auto geo = track.make_geo_view();
auto msg = CELER_LOG_LOCAL(error);
msg << "Tracking error at " << repr(geo.pos()) << " along "
<< repr(geo.dir()) << ": ";
if (!geo.is_outside())
{
msg << "depositing " << deposited << " [" << Energy::unit_type::label()
<< "] in "
<< "volume " << geo.volume_id().unchecked_get();
}
else
{
msg << "lost " << deposited << " energy";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
msg << "lost " << deposited << " energy";
msg << "lost " << deposited << " " << Energy::unit_type::label() << " energy";

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm apparently I forgot to push!

}
#endif
}

//---------------------------------------------------------------------------//
} // namespace detail
} // namespace celeritas
26 changes: 8 additions & 18 deletions src/celeritas/global/CoreParams.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "celeritas/geo/GeoMaterialParams.hh" // IWYU pragma: keep
#include "celeritas/geo/GeoParams.hh" // IWYU pragma: keep
#include "celeritas/geo/detail/BoundaryAction.hh"
#include "celeritas/geo/detail/GeoErrorAction.hh"
#include "celeritas/mat/MaterialParams.hh" // IWYU pragma: keep
#include "celeritas/mat/MaterialParamsOutput.hh"
#include "celeritas/phys/CutoffParams.hh" // IWYU pragma: keep
Expand Down Expand Up @@ -128,18 +129,6 @@ class PropagationLimitAction final : public ConcreteAction
}
};

//---------------------------------------------------------------------------//
class AbandonLoopingAction final : public ConcreteAction
{
public:
//! Construct with ID
explicit AbandonLoopingAction(ActionId id)
: ConcreteAction(
id, "kill-looping", "kill due to too many field substeps")
{
}
};

//---------------------------------------------------------------------------//
/*!
* Construct always-required actions and set IDs.
Expand Down Expand Up @@ -195,18 +184,19 @@ CoreScalars build_actions(ActionRegistry* reg)
reg->insert(
make_shared<PropagationLimitAction>(scalars.propagation_limit_action));

// Construct action for killed looping tracks
scalars.abandon_looping_action = reg->next_id();
reg->insert(
make_shared<AbandonLoopingAction>(scalars.abandon_looping_action));

//// POST-STEP ACTIONS ////

// Construct geometry action
// Construct geometry boundary action
scalars.boundary_action = reg->next_id();
reg->insert(make_shared<celeritas::detail::BoundaryAction>(
scalars.boundary_action));

// Construct action for killed looping tracks/error geometry
// NOTE: due to ordering by {start, ID}, GeoErrorAction *must*
// be after BoundaryAction
scalars.geo_error_action = reg->next_id();
reg->insert(make_shared<detail::GeoErrorAction>(scalars.geo_error_action));

//// END ACTIONS ////

// Construct extend from secondaries action
Expand Down
8 changes: 4 additions & 4 deletions src/celeritas/global/CoreTrackData.hh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct CoreScalars
{
ActionId boundary_action;
ActionId propagation_limit_action;
ActionId abandon_looping_action;
ActionId geo_error_action; //!< Fatal geometry error

// TODO: this is a hack until we improve the along-step interface
ActionId along_step_user_action;
Expand All @@ -44,9 +44,9 @@ struct CoreScalars
//! True if assigned and valid
explicit CELER_FUNCTION operator bool() const
{
return boundary_action && propagation_limit_action
&& abandon_looping_action && along_step_user_action
&& along_step_neutral_action && max_streams > 0;
return boundary_action && propagation_limit_action && geo_error_action
&& along_step_user_action && along_step_neutral_action
&& max_streams > 0;
}
};

Expand Down
Loading
Loading