Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:05:55

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file orange/orangeinp/detail/ProtoBuilder.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <functional>
0011 
0012 #include "orange/OrangeInput.hh"
0013 #include "orange/OrangeTypes.hh"
0014 
0015 #include "ProtoMap.hh"
0016 
0017 namespace celeritas
0018 {
0019 struct JsonPimpl;
0020 namespace orangeinp
0021 {
0022 class ProtoInterface;
0023 
0024 namespace detail
0025 {
0026 //---------------------------------------------------------------------------//
0027 /*!
0028  * Manage data about the universe construction.
0029  *
0030  * On construction this builds a breadth-first ordered list of protos:
0031  * the input "global" universe will always be at the front of the list, and
0032  * universes may only depend on a universe with a larger ID.
0033  *
0034  * This is passed to \c ProtoInterface::build. It acts like a two-way map
0035  * between universe IDs and pointers to Proto interfaces. It \em must not
0036  * exceed the lifetime of any of the protos.
0037  *
0038  * The bounding box for a universe starts as "null" and is expanded by the
0039  * universes that use it: this allows, for example, different masked components
0040  * of an array to be used in multiple universes.
0041  */
0042 class ProtoBuilder
0043 {
0044   public:
0045     //!@{
0046     //! \name Type aliases
0047     using Tol = Tolerance<>;
0048     using SaveUnivJson = std::function<void(UniverseId, JsonPimpl&&)>;
0049     //!@}
0050 
0051     //! Input options for construction
0052     struct Options
0053     {
0054         //! Manually specify a tracking/construction tolerance
0055         Tolerance<> tol;
0056         //! Save metadata during construction for each universe
0057         SaveUnivJson save_json;
0058     };
0059 
0060   public:
0061     // Construct with output pointer, geometry construction options, and protos
0062     ProtoBuilder(OrangeInput* inp, ProtoMap const& protos, Options const& opts);
0063 
0064     //! Get the tolerance to use when constructing geometry
0065     Tol const& tol() const { return inp_->tol; }
0066 
0067     //! Whether output should be saved for each
0068     bool save_json() const { return static_cast<bool>(save_json_); }
0069 
0070     // Find a universe ID
0071     inline UniverseId find_universe_id(ProtoInterface const*) const;
0072 
0073     //! Get the next universe ID
0074     UniverseId next_id() const { return UniverseId(inp_->universes.size()); }
0075 
0076     // Get the bounding box of a universe
0077     inline BBox const& bbox(UniverseId) const;
0078 
0079     // Expand the bounding box of a universe
0080     void expand_bbox(UniverseId, BBox const& local_box);
0081 
0082     // Save debugging data for a universe
0083     void save_json(JsonPimpl&&) const;
0084 
0085     // Construct a universe (to be called *once* per proto)
0086     void insert(VariantUniverseInput&& unit);
0087 
0088   private:
0089     OrangeInput* inp_;
0090     ProtoMap const& protos_;
0091     SaveUnivJson save_json_;
0092     std::vector<BBox> bboxes_;
0093 };
0094 
0095 //---------------------------------------------------------------------------//
0096 // INLINE DEFINITIONS
0097 //---------------------------------------------------------------------------//
0098 /*!
0099  * Find a universe ID.
0100  */
0101 UniverseId ProtoBuilder::find_universe_id(ProtoInterface const* p) const
0102 {
0103     return protos_.find(p);
0104 }
0105 
0106 //---------------------------------------------------------------------------//
0107 /*!
0108  * Get the bounding box of a universe.
0109  */
0110 BBox const& ProtoBuilder::bbox(UniverseId uid) const
0111 {
0112     CELER_EXPECT(uid < bboxes_.size());
0113     return bboxes_[uid.get()];
0114 }
0115 
0116 //---------------------------------------------------------------------------//
0117 }  // namespace detail
0118 }  // namespace orangeinp
0119 }  // namespace celeritas