Skip to content

Commit

Permalink
Add safety visualization tool (#1614)
Browse files Browse the repository at this point in the history
* Extend FourLevel tests
* Revert orange fourlevels...
* Add rough safety imager
* Add pincell geometry
* Add safety tests
* Fix units
* Add build output from celer-geo
* Fix col/row swap
* Add CMSE imager test
* Fix build of vecgeom without geant4
* Explicitly instantiate SafetyImager and add nljson dependency
* Reduce sizes of raytrace images
* Fix capitalization
* Fix tests
  • Loading branch information
sethrj authored Feb 12, 2025
1 parent aad2d08 commit 7facca9
Show file tree
Hide file tree
Showing 16 changed files with 474 additions and 13 deletions.
5 changes: 3 additions & 2 deletions app/celer-geo/celer-geo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
#include <nlohmann/json.hpp>

#include "corecel/Config.hh"
#include "corecel/Version.hh"

#include "corecel/io/ExceptionOutput.hh"
#include "corecel/io/BuildOutput.hh"
#include "corecel/io/Logger.hh"
#include "corecel/io/JsonPimpl.hh"
#include "corecel/io/Repr.hh"
#include "corecel/io/StringUtils.hh"
#include "corecel/sys/Device.hh"
Expand Down Expand Up @@ -228,9 +229,9 @@ void run(std::istream& is)
{
"runtime",
{
{"version", std::string(celeritas_version)},
{"device", device()},
{"kernels", kernel_registry()},
{"build", json_pimpl_output(BuildOutput{})},
},
},
} << std::endl;
Expand Down
4 changes: 3 additions & 1 deletion src/geocel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ if(CELERITAS_USE_Geant4)
ScopedGeantLogger.cc
g4/GeantGeoParams.cc
g4/RaytraceImager.cc
g4/SafetyImager.cc
g4/detail/GeantGeoNavCollection.cc
)

celeritas_get_g4libs(_cg4_libs global geometry materials particles
persistency intercoms)
list(APPEND _cg4_libs Celeritas::corecel XercesC::XercesC)
list(APPEND _cg4_libs Celeritas::corecel XercesC::XercesC nlohmann_json::nlohmann_json)
celeritas_target_link_libraries(geocel_geant4 PRIVATE ${_cg4_libs})

list(APPEND SOURCES $<TARGET_OBJECTS:geocel_geant4>)
Expand All @@ -65,6 +66,7 @@ if(CELERITAS_USE_VecGeom)
vg/VecgeomParamsOutput.cc
vg/detail/VecgeomNavCollection.cc
vg/RaytraceImager.cc
vg/SafetyImager.cc
)
if(VecGeom_GDML_FOUND)
list(APPEND PRIVATE_DEPS VecGeom::vgdml)
Expand Down
21 changes: 21 additions & 0 deletions src/geocel/g4/SafetyImager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//------------------------------- -*- C++ -*- -------------------------------//
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file geocel/g4/SafetyImager.cc
//---------------------------------------------------------------------------//
#include "geocel/rasterize/SafetyImager.t.hh"

#include "GeantGeoData.hh"
#include "GeantGeoParams.hh"
#include "GeantGeoTrackView.hh"
#include "GeantGeoTraits.hh"

namespace celeritas
{
//---------------------------------------------------------------------------//

template class SafetyImager<GeantGeoParams>;

//---------------------------------------------------------------------------//
} // namespace celeritas
2 changes: 1 addition & 1 deletion src/geocel/rasterize/ImageData.hh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct ImageParamsScalars
Real3 down{}; //!< Downward basis vector
Real3 right{}; //!< Rightward basis vector (increasing i, track movement)
real_type pixel_width{}; //!< Width of a pixel
Size2 dims{}; //!< Image dimensions (rows, columns)
Size2 dims{}; //!< Image dimensions (row, column) = (y, x)
real_type max_length{}; //!< Maximum distance along rightward to trace

//! Whether the interface is initialized
Expand Down
58 changes: 58 additions & 0 deletions src/geocel/rasterize/SafetyImager.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//------------------------------- -*- C++ -*- -------------------------------//
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file geocel/rasterize/SafetyImager.hh
//---------------------------------------------------------------------------//
#pragma once

#include "corecel/data/CollectionStateStore.hh"
#include "geocel/GeoTraits.hh"
#include "geocel/rasterize/Image.hh"

namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* Write safety distances from a geometry.
*
* The file format is JSON lines:
* - first line: metadata
* - each further line: progressive y coordinates
*
* \note This is a very rough-and-ready class that should be restructured and
* integrated with the ray tracer so that it can be executed in parallel on
* GPU. The interface will change and this will be added to the \c celer-geo
* app someday!
*/
template<class G>
class SafetyImager
{
static_assert(std::is_base_of_v<GeoParamsInterface, G>);

public:
//!@{
//! \name Type aliases
using SPConstGeo = std::shared_ptr<G const>;
//!@}

public:
// Construct with geometry
explicit SafetyImager(SPConstGeo geo);

// Save an image
void operator()(ImageParams const& image, std::string filename);

private:
using TraitsT = GeoTraits<G>;
template<Ownership W, MemSpace M>
using StateData = typename TraitsT::template StateData<W, M>;
using HostStateStore = CollectionStateStore<StateData, MemSpace::host>;
using GeoTrackView = typename TraitsT::TrackView;

SPConstGeo geo_;
HostStateStore host_state_;
};

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

#include "SafetyImager.hh"

#include <fstream>
#include <nlohmann/json.hpp>

#include "geocel/rasterize/ImageIO.json.hh"

#include "detail/SafetyCalculator.hh"

namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* Construct with geometry and build a single state.
*/
template<class G>
SafetyImager<G>::SafetyImager(SPConstGeo geo) : geo_{std::move(geo)}
{
CELER_EXPECT(geo_);

host_state_ = {geo_->host_ref(), 1};
}

//---------------------------------------------------------------------------//
/*!
* Write an image to a file.
*/
template<class G>
void SafetyImager<G>::operator()(ImageParams const& image, std::string filename)
{
std::ofstream out{filename, std::ios::out | std::ios::trunc};
CELER_VALIDATE(out, << "failed to open '" << filename << "'");
out << nlohmann::json(image).dump() << std::endl;

auto const& scalars = image.scalars();
real_type max_distance = celeritas::max(scalars.dims[0], scalars.dims[1])
* scalars.pixel_width;

detail::SafetyCalculator calc_safety{
GeoTrackView{geo_->host_ref(), host_state_.ref(), TrackSlotId{0}},
image.host_ref(),
max_distance};

std::vector<double> line;
for (auto i : range(scalars.dims[0]))
{
line.clear();
for (auto j : range(scalars.dims[1]))
{
line.push_back(calc_safety(j, i)); // Note: col is 'x' position
}
out << nlohmann::json(line).dump() << std::endl;
}
}

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

#include "corecel/math/Algorithms.hh"
#include "corecel/math/ArrayUtils.hh"

#include "../ImageData.hh"

namespace celeritas
{
namespace detail
{
//---------------------------------------------------------------------------//
/*!
* Calculate the safety distance at an x,y position.
*
* The direction of the initialized state is "out of the page". The maximum
* distance should generally be the length scale of the image.
*/
template<class GTV>
class SafetyCalculator
{
public:
//!@{
//! \name Type aliases
using ParamsRef = NativeCRef<ImageParamsData>;
using StateRef = NativeRef<ImageStateData>;
//!@}

public:
// Construct with geo track view
inline CELER_FUNCTION
SafetyCalculator(GTV&&, ParamsRef const&, real_type max_distance);

// Calculate safety at an x, y index
inline CELER_FUNCTION real_type operator()(size_type x, size_type y);

private:
GTV geo_;
ImageParamsScalars const& scalars_;
Real3 dir_;
real_type max_distance_;
};

//---------------------------------------------------------------------------//
// DEDUCTION GUIDES
//---------------------------------------------------------------------------//

template<class GTV>
CELER_FUNCTION SafetyCalculator(GTV&&,
NativeCRef<ImageParamsData> const&,
real_type) -> SafetyCalculator<GTV>;

//---------------------------------------------------------------------------//
// INLINE DEFINITIONS
//---------------------------------------------------------------------------//
/*!
* Construct with geo track view.
*/
template<class GTV>
CELER_FUNCTION SafetyCalculator<GTV>::SafetyCalculator(GTV&& geo,
ParamsRef const& params,
real_type max_distance)
: geo_{celeritas::forward<GTV>(geo)}
, scalars_{params.scalars}
, dir_{make_unit_vector(cross_product(scalars_.down, scalars_.right))}
, max_distance_{max_distance}
{
CELER_ENSURE(max_distance_ > 0);
}

//---------------------------------------------------------------------------//
/*!
* Calculate safety at an x, y coordinate.
*/
template<class GTV>
CELER_FUNCTION real_type SafetyCalculator<GTV>::operator()(size_type x,
size_type y)
{
auto calc_offset = [pw = scalars_.pixel_width](size_type i) {
return pw * (static_cast<real_type>(i) + real_type(0.5));
};

GeoTrackInitializer init;
init.pos = scalars_.origin;
axpy(calc_offset(y), scalars_.down, &init.pos);
axpy(calc_offset(x), scalars_.right, &init.pos);
init.dir = dir_;

geo_ = init;
return geo_.find_safety(max_distance_);
}

//---------------------------------------------------------------------------//
} // namespace detail
} // namespace celeritas
21 changes: 21 additions & 0 deletions src/geocel/vg/SafetyImager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//------------------------------- -*- C++ -*- -------------------------------//
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file geocel/vg/SafetyImager.cc
//---------------------------------------------------------------------------//
#include "geocel/rasterize/SafetyImager.t.hh"

#include "VecgeomData.hh"
#include "VecgeomGeoTraits.hh"
#include "VecgeomParams.hh"
#include "VecgeomTrackView.hh"

namespace celeritas
{
//---------------------------------------------------------------------------//

template class SafetyImager<VecgeomParams>;

//---------------------------------------------------------------------------//
} // namespace celeritas
1 change: 1 addition & 0 deletions src/orange/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ list(APPEND SOURCES
OrangeParams.cc
OrangeParamsOutput.cc
OrangeTypes.cc
SafetyImager.cc
detail/BIHBuilder.cc
detail/BIHPartitioner.cc
detail/DepthCalculator.cc
Expand Down
21 changes: 21 additions & 0 deletions src/orange/SafetyImager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//------------------------------- -*- C++ -*- -------------------------------//
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file orange/SafetyImager.cc
//---------------------------------------------------------------------------//
#include "geocel/rasterize/SafetyImager.t.hh"

#include "OrangeData.hh"
#include "OrangeGeoTraits.hh"
#include "OrangeParams.hh"
#include "OrangeTrackView.hh"

namespace celeritas
{
//---------------------------------------------------------------------------//

template class SafetyImager<OrangeParams>;

//---------------------------------------------------------------------------//
} // namespace celeritas
2 changes: 1 addition & 1 deletion test/geocel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ if(CELERITAS_USE_VecGeom)
set(_vecgeom_tests DISABLE)
endif()
celeritas_add_device_test(vg/Vecgeom
LINK_LIBRARIES VecGeom::vecgeom ${_g4_geo_libs}
LINK_LIBRARIES VecGeom::vecgeom ${_g4_geo_libs} nlohmann_json::nlohmann_json
FILTER ${_vecgeom_tests}
${_fixme_cgs}
)
Expand Down
Loading

0 comments on commit 7facca9

Please sign in to comment.