Line data Source code
1 : 2 : #ifndef BL_ORIENTATION_H 3 : #define BL_ORIENTATION_H 4 : #include <AMReX_Config.H> 5 : 6 : #include <AMReX_BLassert.H> 7 : #include <AMReX_SPACE.H> 8 : #include <AMReX_GpuQualifiers.H> 9 : 10 : #include <iosfwd> 11 : 12 : namespace amrex { 13 : 14 : enum class Direction : int { AMREX_D_DECL(x = 0, y = 1, z = 2) }; 15 : 16 : class OrientationIter; 17 : 18 : /** 19 : * \brief Encapsulation of the Orientation of the Faces of a Box 20 : * 21 : * This class encapsulates the orientation of the faces of a Box by 22 : * providing an ordering of each of the faces of a Box in AMREX_SPACEDIM 23 : * dimensions. This allows iterating over all the faces of a Box. The 24 : * ordering first traverses the AMREX_SPACEDIM low sides from direction 0 .. 25 : * AMREX_SPACEDIM-1 and then the AMREX_SPACEDIM high sides from direction 0 .. 26 : * AMREX_SPACEDIM-1. 27 : */ 28 : class Orientation 29 : { 30 : public: 31 : 32 : friend class OrientationIter; 33 : //! In each dimension a face is either low or high. 34 : enum Side { low = 0, high = 1 }; 35 : //! The default constructor. 36 : constexpr Orientation () noexcept = default; 37 : //! Set the orientation of a side. 38 : AMREX_GPU_HOST_DEVICE 39 : Orientation (int dir, Side side) noexcept 40 : : 41 : val(AMREX_SPACEDIM*side + dir) 42 : { 43 : BL_ASSERT(0 <= dir && dir < AMREX_SPACEDIM); 44 : } 45 : AMREX_GPU_HOST_DEVICE 46 : constexpr Orientation (Direction dir, Side side) noexcept 47 : : val(AMREX_SPACEDIM*side + static_cast<int>(dir)) 48 : {} 49 : 50 : //! Logical equality. 51 : AMREX_GPU_HOST_DEVICE 52 : bool operator== (const Orientation& o) const noexcept { return val == o.val; } 53 : //! Logical inequality. 54 : AMREX_GPU_HOST_DEVICE 55 : bool operator!= (const Orientation& o) const noexcept { return val != o.val; } 56 : //! Less-than. 57 : AMREX_GPU_HOST_DEVICE 58 : bool operator< (const Orientation& o) const noexcept { return val < o.val; } 59 : //! Less-than or equal. 60 : AMREX_GPU_HOST_DEVICE 61 : bool operator<= (const Orientation& o) const noexcept { return val <= o.val; } 62 : //! Greater-than. 63 : AMREX_GPU_HOST_DEVICE 64 : bool operator> (const Orientation& o) const noexcept { return val > o.val; } 65 : //! Greater-than or equal. 66 : AMREX_GPU_HOST_DEVICE 67 : bool operator>= (const Orientation& o) const noexcept { return val >= o.val; } 68 : /** 69 : * \brief This conversion operator maps an orientation into a 70 : * unique integer in the range [0 .. 2*AMREX_SPACEDIM-1] 71 : * according to the above ordering. 72 : */ 73 : AMREX_GPU_HOST_DEVICE 74 : constexpr operator int () const noexcept { return val; } 75 : //! Return opposite orientation. 76 : [[nodiscard]] AMREX_GPU_HOST_DEVICE 77 : Orientation flip () const noexcept 78 : { 79 : return Orientation(val < AMREX_SPACEDIM ? val+AMREX_SPACEDIM : val-AMREX_SPACEDIM); 80 : } 81 : //! Returns the coordinate direction. 82 : [[nodiscard]] AMREX_GPU_HOST_DEVICE 83 0 : int coordDir () const noexcept { return val%AMREX_SPACEDIM; } 84 : //! Returns the orientation of the face -- low or high. 85 : [[nodiscard]] AMREX_GPU_HOST_DEVICE 86 : Side faceDir () const noexcept { return Side(val/AMREX_SPACEDIM); } 87 : //! Returns true if Orientation is low. 88 : [[nodiscard]] AMREX_GPU_HOST_DEVICE 89 0 : bool isLow () const noexcept { return val < AMREX_SPACEDIM; } 90 : //! Returns true if Orientation is high. 91 : [[nodiscard]] AMREX_GPU_HOST_DEVICE 92 : bool isHigh () const noexcept { return val >= AMREX_SPACEDIM; } 93 : //! Read from an istream. 94 : friend std::istream& operator>> (std::istream& is, Orientation& o); 95 : 96 : //! Int value of the x-lo-face 97 : AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE 98 : static constexpr int xlo () noexcept { return 0; } 99 : 100 : //! Int value of the x-hi-face 101 : AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE 102 : static constexpr int xhi () noexcept { return AMREX_SPACEDIM; } 103 : 104 : //! Int value of the y-lo-face 105 : AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE 106 : static constexpr int ylo () noexcept { return 1; } 107 : 108 : //! Int value of the y-hi-face 109 : AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE 110 : static constexpr int yhi () noexcept { return 1+AMREX_SPACEDIM; } 111 : 112 : //! Int value of the z-lo-face 113 : AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE 114 : static constexpr int zlo () noexcept { return 2; } 115 : 116 : //! Int value of the z-hi-face 117 : AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE 118 : static constexpr int zhi () noexcept { return 2+AMREX_SPACEDIM; } 119 : 120 : private: 121 : //! Used internally. 122 : AMREX_GPU_HOST_DEVICE 123 : Orientation (int v) noexcept : val(v) {} 124 : // 125 : //! The data. 126 : int val = -1; 127 : }; 128 : 129 : //! Write to an ostream in ASCII format. 130 : std::ostream& operator<< (std::ostream& os, const Orientation& o); 131 : 132 : 133 : //! An Iterator over the Orientation of Faces of a Box 134 : class OrientationIter 135 : { 136 : 137 : public: 138 : //! The default constructor. 139 : constexpr OrientationIter () noexcept = default; 140 : //! Construct an iterator on the Orientation. 141 : AMREX_GPU_HOST_DEVICE 142 : OrientationIter (const Orientation& _face) noexcept 143 : : 144 : face(_face) {} 145 : //! Reset (rewind) the iterator. 146 : AMREX_GPU_HOST_DEVICE 147 : void rewind () noexcept { face = 0; } 148 : //! Return the orientation of the face. 149 : AMREX_GPU_HOST_DEVICE 150 : Orientation operator() () const noexcept { BL_ASSERT(isValid()); return Orientation(face); } 151 : //! Cast to void*. Used to test if iterator is valid. 152 : AMREX_GPU_HOST_DEVICE 153 : operator void* () noexcept { return 0 <= face && face < 2*AMREX_SPACEDIM ? this : nullptr; } 154 : //! Is the iterator valid? 155 : [[nodiscard]] AMREX_GPU_HOST_DEVICE 156 : bool isValid () const noexcept { return 0 <= face && face < 2*AMREX_SPACEDIM; } 157 : //! Pre-decrement. 158 : AMREX_GPU_HOST_DEVICE 159 : OrientationIter& operator-- () noexcept { BL_ASSERT(isValid()); --face; return *this; } 160 : //! Pre-increment. 161 : AMREX_GPU_HOST_DEVICE 162 : OrientationIter& operator++ () noexcept { BL_ASSERT(isValid()); ++face; return *this; } 163 : //! Post-decrement. 164 : AMREX_GPU_HOST_DEVICE 165 : OrientationIter operator-- (int) noexcept 166 : { 167 : BL_ASSERT(isValid()); OrientationIter it(face); --face; return it; 168 : } 169 : //! Post-increment. 170 : AMREX_GPU_HOST_DEVICE 171 : OrientationIter operator++ (int) noexcept 172 : { 173 : BL_ASSERT(isValid()); OrientationIter it(face); ++face; return it; 174 : } 175 : //! The equality operator. 176 : AMREX_GPU_HOST_DEVICE 177 : bool operator== (const OrientationIter& oi) const noexcept 178 : { 179 : BL_ASSERT(isValid() && oi.isValid()); return face == oi.face; 180 : } 181 : //! The inequality operator. 182 : AMREX_GPU_HOST_DEVICE 183 : bool operator!= (const OrientationIter& oi) const noexcept 184 : { 185 : BL_ASSERT(isValid() && oi.isValid()); return face != oi.face; 186 : } 187 : 188 : private: 189 : 190 : int face = 0; 191 : //! Construct an iterator on the face. 192 : AMREX_GPU_HOST_DEVICE 193 : OrientationIter (int _face) noexcept : face(_face) {} 194 : }; 195 : 196 : } 197 : 198 : #endif /*BL_ORIENTATION_H*/