-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add safety visualization tool (#1614)
* 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
Showing
16 changed files
with
474 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.