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

Deduplicate ORANGE geometry on insertion #956

Merged
merged 11 commits into from
Sep 27, 2023
7 changes: 7 additions & 0 deletions src/celeritas/global/CoreParams.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "corecel/sys/KernelRegistry.hh"
#include "corecel/sys/MemRegistry.hh"
#include "corecel/sys/ScopedMem.hh"
#include "orange/OrangeParams.hh"
#include "orange/OrangeParamsOutput.hh"
#include "celeritas/geo/GeoMaterialParams.hh" // IWYU pragma: keep
#include "celeritas/geo/GeoParams.hh" // IWYU pragma: keep
#include "celeritas/geo/GeoParamsOutput.hh"
Expand Down Expand Up @@ -300,6 +302,11 @@ CoreParams::CoreParams(Input input) : input_(std::move(input))
input_.output_reg->insert(
std::make_shared<ActionRegistryOutput>(input_.action_reg));

#if CELERITAS_CORE_GEO == CELERITAS_CORE_GEO_ORANGE
input_.output_reg->insert(
std::make_shared<OrangeParamsOutput>(input_.geometry));
#endif

CELER_LOG(status) << "Celeritas core setup complete";

CELER_ENSURE(host_ref_);
Expand Down
1 change: 1 addition & 0 deletions src/orange/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ list(APPEND SOURCES
BoundingBoxUtils.cc
MatrixUtils.cc
OrangeParams.cc
OrangeParamsOutput.cc
OrangeTypes.cc
construct/CsgTree.cc
construct/CsgTypes.cc
Expand Down
14 changes: 7 additions & 7 deletions src/orange/OrangeData.hh
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ struct SurfacesRecord
* This struct is associated with a specific surface; the \c neighbors range is
* a list of local volume IDs for that surface.
*/
struct Connectivity
struct ConnectivityRecord
{
ItemRange<LocalVolumeId> neighbors;
};
Expand Down Expand Up @@ -180,7 +180,7 @@ struct SimpleUnitRecord

// Surface data
SurfacesRecord surfaces;
ItemRange<Connectivity> connectivity; // Index by LocalSurfaceId
ItemRange<ConnectivityRecord> connectivity; // Index by LocalSurfaceId

// Volume data [index by LocalVolumeId]
ItemMap<LocalVolumeId, VolumeRecordId> volumes;
Expand Down Expand Up @@ -338,7 +338,7 @@ struct OrangeParamsData
Items<logic_int> logic_ints;
Items<real_type> reals;
Items<SurfaceType> surface_types;
Items<Connectivity> connectivities;
Items<ConnectivityRecord> connectivity_records;
Items<VolumeRecord> volume_records;
Items<Daughter> daughters;

Expand Down Expand Up @@ -378,7 +378,7 @@ struct OrangeParamsData
logic_ints = other.logic_ints;
reals = other.reals;
surface_types = other.surface_types;
connectivities = other.connectivities;
connectivity_records = other.connectivity_records;
volume_records = other.volume_records;
daughters = other.daughters;
universe_indexer_data = other.universe_indexer_data;
Expand Down Expand Up @@ -419,9 +419,9 @@ struct OrangeStateData
Items<LocalVolumeId> vol;
Items<UniverseId> universe;

// TODO: this is problem-dependent data and should eventually be removed
// max_depth defines the stride into the preceding pseudo-2D
// Collections (pos, dir, ..., etc.)
// Note: this is duplicated from the associated OrangeParamsData .
// It defines the stride into the preceding pseudo-2D Collections (pos,
// dir, ..., etc.)
size_type max_depth{0};

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

#include "celeritas_config.h"
#include "corecel/cont/Range.hh"
#include "corecel/io/JsonPimpl.hh"

#include "OrangeParams.hh" // IWYU pragma: keep
#if CELERITAS_USE_JSON
# include <nlohmann/json.hpp>

// TODO: Tolerance is defined in OrangeTypes but IO is here
# include "orange/construct/OrangeInputIO.json.hh"
#endif

namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* Construct from shared orange data.
*/
OrangeParamsOutput::OrangeParamsOutput(SPConstOrangeParams orange)
: orange_(std::move(orange))
{
CELER_EXPECT(orange_);
}

//---------------------------------------------------------------------------//
/*!
* Write output to the given JSON object.
*/
void OrangeParamsOutput::output(JsonPimpl* j) const
{
#if CELERITAS_USE_JSON
using json = nlohmann::json;

auto obj = json::object();
auto const& data = orange_->host_ref();

// Save param scalars
obj["scalars"] = [&sdata = data.scalars] {
auto scalars = json::object();
# define OPO_SAVE_SCALAR(NAME) scalars[#NAME] = sdata.NAME;
OPO_SAVE_SCALAR(max_depth);
OPO_SAVE_SCALAR(max_faces);
OPO_SAVE_SCALAR(max_intersections);
OPO_SAVE_SCALAR(max_logic_depth);
OPO_SAVE_SCALAR(tol);
# undef OPO_SAVE_SCALAR
return scalars;
}();

// Save sizes
obj["sizes"] = [&data] {
auto sizes = json::object();
# define OPO_SAVE_SIZE(NAME) sizes[#NAME] = data.NAME.size()
OPO_SAVE_SIZE(universe_types);
OPO_SAVE_SIZE(universe_indices);
OPO_SAVE_SIZE(simple_units);
OPO_SAVE_SIZE(rect_arrays);
OPO_SAVE_SIZE(transforms);
OPO_SAVE_SIZE(local_surface_ids);
OPO_SAVE_SIZE(local_volume_ids);
OPO_SAVE_SIZE(real_ids);
OPO_SAVE_SIZE(logic_ints);
OPO_SAVE_SIZE(reals);
OPO_SAVE_SIZE(surface_types);
OPO_SAVE_SIZE(connectivity_records);
OPO_SAVE_SIZE(volume_records);
OPO_SAVE_SIZE(daughters);
# undef OPO_SAVE_SIZE

// Save BIH sizes
sizes["bih"] = [&bihdata = data.bih_tree_data] {
auto bih = json::object();
# define OPO_SAVE_BIH_SIZE(NAME) bih[#NAME] = bihdata.NAME.size()
OPO_SAVE_BIH_SIZE(bboxes);
OPO_SAVE_BIH_SIZE(local_volume_ids);
OPO_SAVE_BIH_SIZE(inner_nodes);
OPO_SAVE_BIH_SIZE(leaf_nodes);
# undef OPO_SAVE_BIH_SIZE
return bih;
}();

return sizes;
}();

// TODO: make universe metadata accessible from ORANGE, and write it

j->obj = std::move(obj);
#else
(void)sizeof(j);
#endif
}

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

#include "corecel/io/OutputInterface.hh"

namespace celeritas
{
class OrangeParams;
//---------------------------------------------------------------------------//
/*!
* Save detailed debugging information about the ORANGE geometry.
*
* This is to be used in *addition* to the standard bbox/volume/surface data
* saved by GeoParamsOutput.
*
* \sa celeritas/geo/GeoParamsOutput.hh
*/
class OrangeParamsOutput final : public OutputInterface
{
public:
//!@{
//! \name Type aliases
using SPConstOrangeParams = std::shared_ptr<OrangeParams const>;
//!@}

public:
// Construct from shared geometry data
explicit OrangeParamsOutput(SPConstOrangeParams orange);

//! Category of data to write
Category category() const final { return Category::internal; }

//! Name of the entry inside the category.
std::string label() const final { return "orange"; }

// Write output to the given JSON object
void output(JsonPimpl*) const final;

private:
SPConstOrangeParams orange_;
};

//---------------------------------------------------------------------------//
} // namespace celeritas
17 changes: 17 additions & 0 deletions src/orange/construct/OrangeInputIO.json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,22 @@ void from_json(nlohmann::json const& j, OrangeInput& value)
}
}

//---------------------------------------------------------------------------//
// WRITERS
//---------------------------------------------------------------------------//
/*!
* Write tolerances.
*/
template<class T>
void to_json(nlohmann::json& j, Tolerance<T> const& value)
{
j = {
{"rel", value.rel},
{"abs", value.abs},
};
}

template void to_json(nlohmann::json&, Tolerance<real_type> const&);

//---------------------------------------------------------------------------//
} // namespace celeritas
3 changes: 3 additions & 0 deletions src/orange/construct/OrangeInputIO.json.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ void from_json(nlohmann::json const& j, UnitInput& value);
void from_json(nlohmann::json const& j, Tolerance<>& value);
void from_json(nlohmann::json const& j, OrangeInput& value);

template<class T>
void to_json(nlohmann::json& j, Tolerance<T> const& value);

//---------------------------------------------------------------------------//
} // namespace celeritas
21 changes: 10 additions & 11 deletions src/orange/detail/BIHData.hh
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ struct BIHInnerNode
size_
};

BIHNodeId parent;

Axis axis;
EnumArray<Edge, BoundingPlane> bounding_planes;
BIHNodeId parent; //!< Parent node ID
Axis axis; //!< Axis for "left" and "right"
EnumArray<Edge, BoundingPlane> bounding_planes; //!< Lower and upper bound

explicit CELER_FUNCTION operator bool() const
{
Expand All @@ -63,8 +62,7 @@ struct BIHInnerNode
*/
struct BIHLeafNode
{
BIHNodeId parent;

BIHNodeId parent; //!< Parent node ID
ItemRange<LocalVolumeId> vol_ids;

explicit CELER_FUNCTION operator bool() const { return !vol_ids.empty(); }
Expand All @@ -73,20 +71,21 @@ struct BIHLeafNode
//---------------------------------------------------------------------------//
/*!
* Bounding Interval Hierarchy tree.
*
* Infinite bounding boxes are not included in the main tree.
*/
struct BIHTree
{
// All bounding boxes managed by the BIH
//! All bounding boxes managed by the BIH
ItemMap<LocalVolumeId, FastBBoxId> bboxes;

// Inner nodes, the first being the root
//! Inner nodes, the first being the root
ItemRange<BIHInnerNode> inner_nodes;

// Leaf nodes
//! Leaf nodes
ItemRange<BIHLeafNode> leaf_nodes;

// VolumeIds for which bboxes have infinite extents, and are therefore
// note included in the tree
//! Local volumes that have infinite bounding boxes
ItemRange<LocalVolumeId> inf_volids;

explicit CELER_FUNCTION operator bool() const
Expand Down
Loading