Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/orange/orangeinp/UnitProto.hh was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file orange/orangeinp/UnitProto.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <optional>
0010 #include <string_view>
0011 #include <variant>
0012 #include <vector>
0013 
0014 #include "corecel/OpaqueId.hh"
0015 #include "geocel/Types.hh"
0016 #include "orange/OrangeTypes.hh"
0017 #include "orange/transform/VariantTransform.hh"
0018 
0019 #include "CsgTypes.hh"
0020 #include "ProtoInterface.hh"
0021 
0022 namespace celeritas
0023 {
0024 namespace orangeinp
0025 {
0026 class Transformed;
0027 
0028 namespace detail
0029 {
0030 struct CsgUnit;
0031 }  // namespace detail
0032 
0033 //---------------------------------------------------------------------------//
0034 /*!
0035  * Construct a general CSG universe, aka a "unit".
0036  *
0037  * A "unit" is a region of space (with an outer boundary specified by the \c
0038  * BoundaryInput::interior object) that is divided up into multiple smaller
0039  * regions:
0040  * - A "material" (aka \em media in SCALE) is a single homogeneous CSG object
0041  *   filled with a particular material ID. This is equivalent to a leaf
0042  *   "physical volume" in a GDML/Geant4 volume hierarchy.
0043  * - A "daughter" (aka \em hole in SCALE) is another unit that is transformed
0044  *   and placed into this universe.
0045  *
0046  * Additional metadata about the structure can be provided when converting
0047  * Geant4 geometry. When building, labels are volume instance IDs: each
0048  * material and daughter is a volume placement, and the background input uses
0049  * an empty instance ID as a sentinel indicating a background volume is
0050  * present. (Note the "background" is structurally a \em volume, not \em
0051  * instance, since it is locally the top level of the geometry.)
0052  * In addition to the labels, a special \c local_parent field denotes a
0053  * structural relationship between one input object and another. These will be
0054  * always set when building from Geant4 but never set when building from SCALE.
0055  * The value is a null ID when the parent canonical volume is rendered as the
0056  * background, or the index in the list of \c materials when the parent volume
0057  * is \em inlined into this unit.
0058  */
0059 class UnitProto : public ProtoInterface
0060 {
0061   public:
0062     //!@{
0063     //! \name Types
0064     using Unit = detail::CsgUnit;
0065     using Tol = Tolerance<>;
0066     using VariantLabel = std::variant<Label, VolumeInstanceId>;
0067     using MaterialInputId = OpaqueId<struct MaterialInput>;
0068     using LocalParent = std::optional<MaterialInputId>;
0069     //!@}
0070 
0071     //! Optional "background" inside of exterior, outside of all mat/daughter
0072     struct BackgroundInput
0073     {
0074         GeoMatId fill{};
0075         VariantLabel label;
0076 
0077         // True if fill or label is specified
0078         explicit inline operator bool() const;
0079     };
0080 
0081     //! A homogeneous physical material
0082     struct MaterialInput
0083     {
0084         SPConstObject interior;
0085         GeoMatId fill;
0086         VariantLabel label;
0087         //! Mark this material as being structurally inside another local one
0088         LocalParent local_parent;
0089 
0090         // True if fully defined
0091         explicit inline operator bool() const;
0092     };
0093 
0094     //! Another universe embedded within this one
0095     struct DaughterInput
0096     {
0097         SPConstProto fill;  //!< Daughter unit
0098         VariantTransform transform;  //!< Daughter-to-parent
0099         ZOrder zorder{ZOrder::media};  //!< Overlap control
0100         VariantLabel label;  //!< Placement name
0101 
0102         //! Mark this daughter as being inside another local volume
0103         LocalParent local_parent;
0104 
0105         // True if fully defined
0106         explicit inline operator bool() const;
0107 
0108         // Construct the daughter's shape in this unit's reference frame
0109         SPConstObject make_interior() const;
0110     };
0111 
0112     //! Boundary conditions for the unit
0113     struct BoundaryInput
0114     {
0115         SPConstObject interior;  //!< Bounding shape for the unit
0116         ZOrder zorder{ZOrder::exterior};  //!< Overlap control
0117 
0118         // True if fully defined
0119         explicit inline operator bool() const;
0120     };
0121 
0122     //! Required input data to create a unit proto
0123     struct Input
0124     {
0125         BackgroundInput background;
0126         std::vector<MaterialInput> materials;
0127         std::vector<DaughterInput> daughters;
0128         BoundaryInput boundary;
0129         Label label;
0130 
0131         // True if fully defined
0132         explicit inline operator bool() const;
0133     };
0134 
0135     //! Inputs passed from \c OrangeGeoFromCsg via \c ProtoBuilder
0136     struct BuildOptions
0137     {
0138         //! Construction tolerance
0139         Tol tol;
0140         //! Replace "interior" node with "true"
0141         bool assume_inside{};
0142         //! Logic notation
0143         LogicNotation logic{LogicNotation::size_};
0144     };
0145 
0146   public:
0147     // Construct with required input data
0148     explicit UnitProto(Input&& inp);
0149 
0150     virtual ~UnitProto() = default;
0151 
0152     // Short unique name of this object
0153     std::string_view label() const final;
0154 
0155     // Get the boundary of this universe as an object
0156     SPConstObject interior() const final;
0157 
0158     // Get a list of all daughters
0159     VecProto daughters() const final;
0160 
0161     // Construct a universe input from this object
0162     void build(ProtoBuilder&) const final;
0163 
0164     // Write the proto to a JSON object
0165     void output(JsonPimpl*) const final;
0166 
0167     //// ACCESSORS ////
0168 
0169     //! Get the input, primarily for unit testing
0170     Input const& input() const { return input_; }
0171 
0172     //// HELPER FUNCTIONS ////
0173 
0174     // Construct a standalone unit for testing and external interface
0175     Unit build(BuildOptions const& opts) const;
0176 
0177   private:
0178     Input input_;
0179 };
0180 
0181 //---------------------------------------------------------------------------//
0182 /*!
0183  * True if either material or label is provided.
0184  */
0185 UnitProto::BackgroundInput::operator bool() const
0186 {
0187     return static_cast<bool>(this->fill);
0188 }
0189 
0190 //---------------------------------------------------------------------------//
0191 /*!
0192  * True if fully defined.
0193  */
0194 UnitProto::MaterialInput::operator bool() const
0195 {
0196     return static_cast<bool>(this->interior);
0197 }
0198 
0199 //---------------------------------------------------------------------------//
0200 /*!
0201  * True if fully defined.
0202  */
0203 UnitProto::DaughterInput::operator bool() const
0204 {
0205     return static_cast<bool>(this->fill);
0206 }
0207 
0208 //---------------------------------------------------------------------------//
0209 /*!
0210  * True if fully defined.
0211  */
0212 UnitProto::BoundaryInput::operator bool() const
0213 {
0214     return static_cast<bool>(this->interior);
0215 }
0216 
0217 //---------------------------------------------------------------------------//
0218 /*!
0219  * True if fully defined.
0220  *
0221  * The unit proto must have at least one material, daughter, or background
0222  * fill.
0223  */
0224 UnitProto::Input::operator bool() const
0225 {
0226     return (!this->materials.empty() || !this->daughters.empty()
0227             || this->background)
0228            && this->boundary;
0229 }
0230 
0231 //---------------------------------------------------------------------------//
0232 }  // namespace orangeinp
0233 }  // namespace celeritas