Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_IntVect.H
Go to the documentation of this file.
1#ifndef AMREX_INTVECT_H_
2#define AMREX_INTVECT_H_
3#include <AMReX_Config.H>
4
5#include <AMReX_INT.H>
6#include <AMReX_SPACE.H>
7#include <AMReX_Array.H>
8#include <AMReX_Vector.H>
9#include <AMReX_Dim3.H>
10#include <AMReX_BLassert.H>
11#include <AMReX_Extension.H>
12#include <AMReX_GpuQualifiers.H>
13#include <AMReX_Math.H>
14#include <AMReX_Tuple.H>
15#include <AMReX_TypeTraits.H>
16
17#include <iosfwd>
18#include <cstdlib>
19#include <cmath>
20#include <limits>
21#include <climits>
22#include <algorithm>
23
24namespace amrex {
25
27int coarsen (int i, int ratio) noexcept
28{
29 switch (ratio) {
30 case 1: return i;
31 case 2: return (i<0) ? -std::abs(i+1)/2 -1 : i/2;
32 case 4: return (i<0) ? -std::abs(i+1)/4 -1 : i/4;
33 default: return (i<0) ? -std::abs(i+1)/ratio-1 : i/ratio;
34 }
35}
36
37template <int ratio>
39int coarsen (int i) noexcept
40{
41 return (i<0) ? -std::abs(i+1)/ratio-1 : i/ratio;
42}
43
55template<int dim>
57{
58public:
59 static_assert(dim >= 1, "The number of dimensions of IntVectND must be positive");
60
62 struct shift_hasher {
63 std::size_t operator()(const IntVectND<dim>& vec) const noexcept
64 {
65 static constexpr unsigned shift1 = sizeof(size_t)>=8 ? 20 : 10;
66 static constexpr unsigned shift2 = sizeof(size_t)>=8 ? 40 : 20;
67 if constexpr (dim == 1) {
70 return static_cast<std::size_t>(vec[0]);
71 } else if constexpr (dim == 2) {
73 return static_cast<std::size_t>(vec[0]) ^
74 (static_cast<std::size_t>(vec[1]) << shift1);
75 } else if constexpr (dim == 3) {
76 return static_cast<std::size_t>(vec[0]) ^
77 (static_cast<std::size_t>(vec[1]) << shift1) ^
78 (static_cast<std::size_t>(vec[2]) << shift2);
79 } else {
80 std::size_t seed = dim;
81 // hash function from
82 // https://stackoverflow.com/questions/20511347/a-good-hash-function-for-a-vector
83 for (int i=0; i<dim; ++i) {
84 auto x = static_cast<unsigned int>(vec[i]);
85 x = ((x >> 16) ^ x) * 0x45d9f3b;
86 x = ((x >> 16) ^ x) * 0x45d9f3b;
87 x = (x >> 16) ^ x;
88 seed ^= x + 0x9e3779b9 + (seed << 6) + (seed >> 2);
89 }
90 return seed;
91 }
92 }
93 };
94
96
98
101 constexpr IntVectND () noexcept {} // cannot use = default due to Clang bug // NOLINT
102
108 template <class...Args,
109 std::enable_if_t<
110 (sizeof...(Args)+2 == dim) &&
111 IsConvertible_v<int, Args...>,
112 int> = 0>
114 constexpr IntVectND (int i, int j, Args...ks) noexcept : vect{i, j, static_cast<int>(ks)...} {}
115
120 explicit constexpr IntVectND (int s) noexcept {
121 for (int i=0; i<dim; ++i) {
122 vect[i] = s;
123 }
124 }
125
131 explicit IntVectND (const int* a) noexcept {
132 for (int i=0; i<dim; ++i) {
133 vect[i] = a[i];
134 }
135 }
136
142 explicit IntVectND (const Vector<int>& a) noexcept {
143 BL_ASSERT(a.size() == dim);
144 for (int i=0; i<dim; ++i) {
145 vect[i] = a[i];
146 }
147 }
148
152 explicit constexpr IntVectND (const Array<int,dim>& a) noexcept {
153 for (int i=0; i<dim; ++i) {
154 vect[i] = a[i];
155 }
156 }
157
158 template <int N=dim, std::enable_if_t<( 1<=N && N<=3 ), int> = 0>
159 explicit constexpr IntVectND (Dim3 const& a) noexcept {
160 vect[0] = a.x;
161 if constexpr (dim >= 2) {
162 vect[1] = a.y;
163 }
164 if constexpr (dim == 3) {
165 vect[2] = a.z;
166 }
167 }
168
169 // dtor, copy-ctor, copy-op=, move-ctor, and move-op= are compiler generated.
170
171 template <int N=dim, std::enable_if_t<( 1<=N && N<=3 ), int> = 0>
172 [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
173 constexpr Dim3 dim3 () const noexcept {
174 if constexpr (dim == 1) {
175 return Dim3{vect[0],0,0};
176 } else if constexpr (dim == 2) {
177 return Dim3{vect[0],vect[1],0};
178 } else {
179 return Dim3{vect[0],vect[1],vect[2]};
180 }
181 }
182
183 template <int N=dim, std::enable_if_t<( 1<=N && N<=3 ), int> = 0>
184 [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
185 constexpr Dim3 dim3 ([[maybe_unused]] int fill_extra) const noexcept {
186 if constexpr (dim == 1) {
187 return Dim3{vect[0],fill_extra,fill_extra};
188 } else if constexpr (dim == 2) {
189 return Dim3{vect[0],vect[1],fill_extra};
190 } else {
191 return Dim3{vect[0],vect[1],vect[2]};
192 }
193 }
194
195 template< typename T = int >
197 constexpr Array<T, dim> toArray () const noexcept {
198 Array<T, dim> ret {};
199 for (int i=0; i<dim; ++i) {
200 ret[i] = T(vect[i]);
201 }
202 return ret;
203 }
204
206
210 constexpr int sum () const noexcept
211 {
212 int retval = vect[0];
213 for (int i=1; i<dim; ++i) {
214 retval += vect[i];
215 }
216 return retval;
217 }
218
221 constexpr int max () const noexcept
222 {
223 int retval = vect[0];
224 for (int i=1; i<dim; ++i) {
225 retval = retval > vect[i] ? retval : vect[i];
226 }
227 return retval;
228 }
229
232 constexpr int min () const noexcept
233 {
234 int retval = vect[0];
235 for (int i=1; i<dim; ++i) {
236 retval = retval < vect[i] ? retval : vect[i];
237 }
238 return retval;
239 }
240
241 //return coordinate with largest value
243 int maxDir(bool a_doAbsValue) const noexcept;
244
246 [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE constexpr
247 int& operator[] (int i) noexcept { BL_ASSERT(i>=0 && i < dim); return vect[i]; }
248
250 [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE constexpr
251 const int& operator[] (int i) const noexcept { BL_ASSERT(i>=0 && i < dim); return vect[i]; }
252
254 template<std::size_t i>
255 [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE constexpr
256 int& get () noexcept {static_assert(0<=i && i<dim); return vect[i];}
257
259 template<std::size_t i>
260 [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE constexpr
261 const int& get () const noexcept {static_assert(0<=i && i<dim); return vect[i];}
262
264 [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE constexpr
265 int* begin () noexcept { return &vect[0]; }
266
268 [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE constexpr
269 const int* begin () const noexcept { return &vect[0]; }
270
272 [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE constexpr
273 int* end () noexcept { return &vect[dim]; }
274
276 [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE constexpr
277 const int* end () const noexcept { return &vect[dim]; }
278
281 IntVectND& setVal (int i, int val) noexcept
282 {
283 BL_ASSERT(i>=0 && i<dim); vect[i] = val; return *this;
284 }
285
291 const int* getVect () const& noexcept { return vect; }
293 int* getVect () & noexcept { return vect; }
295 int* getVect () && = delete;
296
299 constexpr bool operator== (int val) const noexcept
300 {
301 bool retval = vect[0] == val;
302 for (int i=1; i<dim; ++i) {
303 retval = retval && vect[i] == val;
304 }
305 return retval;
306 }
307
310 constexpr bool operator!= (int val) const noexcept
311 {
312 bool retval = vect[0] != val;
313 for (int i=1; i<dim; ++i) {
314 retval = retval || vect[i] != val;
315 }
316 return retval;
317 }
318
321 constexpr bool operator== (const IntVectND<dim>& rhs) const noexcept
322 {
323 bool retval = vect[0] == rhs[0];
324 for (int i=1; i<dim; ++i) {
325 retval = retval && vect[i] == rhs[i];
326 }
327 return retval;
328 }
331 constexpr bool operator!= (const IntVectND<dim>& rhs) const noexcept
332 {
333 bool retval = vect[0] != rhs[0];
334 for (int i=1; i<dim; ++i) {
335 retval = retval || vect[i] != rhs[i];
336 }
337 return retval;
338 }
341 constexpr bool operator< (const IntVectND<dim>& rhs) const noexcept
342 {
343 for (int i=dim-1; i>=0; --i) {
344 if (vect[i] < rhs[i]) {
345 return true;
346 } else if (vect[i] > rhs[i]) {
347 return false;
348 }
349 }
350 return false;
351 }
354 constexpr bool operator<= (const IntVectND<dim>& rhs) const noexcept
355 {
356 return !(rhs < *this);
357 }
360 constexpr bool operator> (const IntVectND<dim>& rhs) const noexcept
361 {
362 return rhs < *this;
363 }
366 constexpr bool operator>= (const IntVectND<dim>& rhs) const noexcept
367 {
368 return !(*this < rhs);
369 }
375 constexpr bool allLT (const IntVectND<dim>& rhs) const noexcept
376 {
377 bool retval = vect[0] < rhs[0];
378 for (int i=1; i<dim; ++i) {
379 retval = retval && vect[i] < rhs[i];
380 }
381 return retval;
382 }
387 constexpr bool allLT (int rhs) const noexcept
388 {
389 bool retval = vect[0] < rhs;
390 for (int i=1; i<dim; ++i) {
391 retval = retval && vect[i] < rhs;
392 }
393 return retval;
394 }
400 constexpr bool allLE (const IntVectND<dim>& rhs) const noexcept
401 {
402 bool retval = vect[0] <= rhs[0];
403 for (int i=1; i<dim; ++i) {
404 retval = retval && vect[i] <= rhs[i];
405 }
406 return retval;
407 }
412 constexpr bool allLE (int rhs) const noexcept
413 {
414 bool retval = vect[0] <= rhs;
415 for (int i=1; i<dim; ++i) {
416 retval = retval && vect[i] <= rhs;
417 }
418 return retval;
419 }
425 constexpr bool allGT (const IntVectND<dim>& rhs) const noexcept
426 {
427 bool retval = vect[0] > rhs[0];
428 for (int i=1; i<dim; ++i) {
429 retval = retval && vect[i] > rhs[i];
430 }
431 return retval;
432 }
437 constexpr bool allGT (int rhs) const noexcept
438 {
439 bool retval = vect[0] > rhs;
440 for (int i=1; i<dim; ++i) {
441 retval = retval && vect[i] > rhs;
442 }
443 return retval;
444 }
450 constexpr bool allGE (const IntVectND<dim>& rhs) const noexcept
451 {
452 bool retval = vect[0] >= rhs[0];
453 for (int i=1; i<dim; ++i) {
454 retval = retval && vect[i] >= rhs[i];
455 }
456 return retval;
457 }
462 constexpr bool allGE (int rhs) const noexcept
463 {
464 bool retval = vect[0] >= rhs;
465 for (int i=1; i<dim; ++i) {
466 retval = retval && vect[i] >= rhs;
467 }
468 return retval;
469 }
472 constexpr IntVectND<dim> operator+ () const noexcept { return *this; }
475 constexpr IntVectND<dim> operator- () const noexcept {
476 IntVectND<dim> retval(0);
477 for (int i=0; i<dim; ++i) {
478 retval[i] = -vect[i];
479 }
480 return retval;
481 }
484 constexpr IntVectND<dim>& operator+= (int s) noexcept
485 {
486 for (int i=0; i<dim; ++i) {
487 vect[i] += s;
488 }
489 return *this;
490 }
493 constexpr IntVectND<dim>& operator+= (const IntVectND<dim>& p) noexcept
494 {
495 for (int i=0; i<dim; ++i) {
496 vect[i] += p[i];
497 }
498 return *this;
499 }
502 constexpr IntVectND<dim>& operator*= (int s) noexcept
503 {
504 for (int i=0; i<dim; ++i) {
505 vect[i] *= s;
506 }
507 return *this;
508 }
511 constexpr IntVectND<dim>& operator*= (const IntVectND<dim>& p) noexcept
512 {
513 for (int i=0; i<dim; ++i) {
514 vect[i] *= p[i];
515 }
516 return *this;
517 }
520 constexpr IntVectND<dim>& operator/= (int s) noexcept
521 {
522 for (int i=0; i<dim; ++i) {
523 vect[i] /= s;
524 }
525 return *this;
526 }
529 constexpr IntVectND<dim>& operator/= (const IntVectND<dim>& p) noexcept
530 {
531 for (int i=0; i<dim; ++i) {
532 vect[i] /= p[i];
533 }
534 return *this;
535 }
538 constexpr IntVectND<dim>& operator-= (int s) noexcept
539 {
540 for (int i=0; i<dim; ++i) {
541 vect[i] -= s;
542 }
543 return *this;
544 }
547 constexpr IntVectND<dim>& operator-= (const IntVectND<dim>& p) noexcept
548 {
549 for (int i=0; i<dim; ++i) {
550 vect[i] -= p[i];
551 }
552 return *this;
553 }
556 constexpr IntVectND<dim> operator+ (const IntVectND<dim>& p) const noexcept
557 {
558 IntVectND<dim> retval = *this;
559 return retval += p;
560 }
563 constexpr IntVectND<dim> operator+ (int s) const noexcept
564 {
565 IntVectND<dim> retval = *this;
566 return retval += s;
567 }
570 constexpr IntVectND<dim> operator- (const IntVectND<dim>& p) const noexcept
571 {
572 IntVectND<dim> retval = *this;
573 return retval -= p;
574 }
577 constexpr IntVectND<dim> operator- (int s) const noexcept
578 {
579 IntVectND<dim> retval = *this;
580 return retval -= s;
581 }
584 constexpr IntVectND<dim> operator* (const IntVectND<dim>& p) const noexcept
585 {
586 IntVectND<dim> retval = *this;
587 return retval *= p;
588 }
591 constexpr IntVectND<dim> operator* (int s) const noexcept
592 {
593 IntVectND<dim> retval = *this;
594 return retval *= s;
595 }
598 constexpr IntVectND<dim> operator/ (const IntVectND<dim>& p) const noexcept
599 {
600 IntVectND<dim> retval = *this;
601 return retval /= p;
602 }
605 constexpr IntVectND<dim> operator/ (int s) const noexcept
606 {
607 IntVectND<dim> retval = *this;
608 return retval /= s;
609 }
612 constexpr IntVectND<dim>& min (const IntVectND<dim>& p) noexcept
613 {
614 for (int i=0; i<dim; ++i) {
615 vect[i] = (vect[i] < p.vect[i] ? vect[i] : p.vect[i]);
616 }
617 return *this;
618 }
621 constexpr IntVectND<dim>& max (const IntVectND<dim>& p) noexcept
622 {
623 for (int i=0; i<dim; ++i) {
624 vect[i] = (vect[i] > p.vect[i] ? vect[i] : p.vect[i]);
625 }
626 return *this;
627 }
630 constexpr IntVectND<dim>& scale (int s) noexcept {
631 for (int i=0; i<dim; ++i) {
632 vect[i] *= s;
633 }
634 return *this;
635 }
641 constexpr IntVectND<dim>& reflect (int ref_ix, int idir) noexcept
642 {
643 BL_ASSERT(idir >= 0 && idir < dim);
644 vect[idir] = -vect[idir] + 2*ref_ix;
645 return *this;
646 }
649 constexpr IntVectND<dim>& shift (int coord, int s) noexcept
650 {
651 BL_ASSERT(coord >= 0 && coord < dim); vect[coord] += s; return *this;
652 }
655 constexpr IntVectND<dim>& shift (const IntVectND<dim>& iv) noexcept { *this += iv; return *this; }
658 constexpr IntVectND<dim>& diagShift (int s) noexcept
659 {
660 for (int i=0; i<dim; ++i) {
661 vect[i] += s;
662 }
663 return *this;
664 }
670 IntVectND<dim>& coarsen (int p) noexcept;
671
679 static constexpr IntVectND<dim> TheZeroVector () noexcept {
680 return IntVectND<dim>(0);
681 }
689 static constexpr IntVectND<dim> TheUnitVector () noexcept {
690 return IntVectND<dim>(1);
691 }
698 static constexpr IntVectND<dim> TheDimensionVector (int d) noexcept {
699 IntVectND<dim> retval(0);
700 retval[d] = 1;
701 return retval;
702 }
709 static constexpr IntVectND<dim> TheNodeVector () noexcept {
710 return IntVectND<dim>(1);
711 }
718 static constexpr IntVectND<dim> TheCellVector () noexcept {
719 return IntVectND<dim>(0);
720 }
721
723 static constexpr IntVectND<dim> TheMaxVector () noexcept {
724 return IntVectND<dim>(std::numeric_limits<int>::max());
725 }
727 static constexpr IntVectND<dim> TheMinVector () noexcept {
728 return IntVectND<dim>(std::numeric_limits<int>::lowest());
729 }
730
732 static constexpr std::size_t size () noexcept {
733 return static_cast<std::size_t>(dim);
734 }
735
737 static constexpr int isize () noexcept {
738 return dim;
739 }
740
742
747 template<int new_dim>
749 constexpr IntVectND<new_dim> shrink () const noexcept {
750 static_assert(new_dim <= dim);
751 return IntVectND<new_dim>(this->begin());
752 }
753
758 template<int new_dim>
760 constexpr IntVectND<new_dim> expand (int fill_extra=0) const noexcept {
761 static_assert(new_dim >= dim);
762 IntVectND<new_dim> retval(fill_extra);
763 for (int i=0; i<dim; ++i) {
764 retval[i] = vect[i];
765 }
766 return retval;
767 }
768
773 template<int new_dim>
775 constexpr IntVectND<new_dim> resize (int fill_extra=0) const noexcept {
776 if constexpr (new_dim > dim) {
777 return expand<new_dim>(fill_extra);
778 } else {
779 return shrink<new_dim>();
780 }
781 }
782
786 static const IntVectND<dim> Zero;
787
791 static const IntVectND<dim> Unit;
792
793 int vect[dim] = {};
794};
795
796template <int dim>
797inline constexpr const IntVectND<dim> IntVectND<dim>::Zero{0};
798
799template <int dim>
800inline constexpr const IntVectND<dim> IntVectND<dim>::Unit{1};
801
802// Template deduction guide for IntVectND
803template<std::size_t dim>
804AMREX_GPU_HOST_DEVICE // __device__ for HIP
806
807// Template deduction guide for IntVectND
808template <class...Args,
809 std::enable_if_t<
810 IsConvertible_v<int, Args...>,
811 int> = 0>
812AMREX_GPU_HOST_DEVICE // __device__ for HIP
813IntVectND(int, int, Args...) -> IntVectND<sizeof...(Args)+2>;
814
818
819template<int dim>
824{
825 BL_ASSERT(s > 0);
826 switch (s) {
827 case 1:
828 break;
829 case 2:
830 for (int i=0; i<dim; ++i) {
831 vect[i] = (vect[i]<0) ? -std::abs(vect[i]+1)/2-1 : vect[i]/2;
832 }
833 break;
834 case 4:
835 for (int i=0; i<dim; ++i) {
836 vect[i] = (vect[i]<0) ? -std::abs(vect[i]+1)/4-1 : vect[i]/4;
837 }
838 break;
839 default:
840 for (int i=0; i<dim; ++i) {
841 vect[i] = (vect[i]<0) ? -std::abs(vect[i]+1)/s-1 : vect[i]/s;
842 }
843 }
844 return *this;
845}
846
847template<int dim>
852{
853 BL_ASSERT(p.allGT(0));
854 for (int i=0; i<dim; ++i) {
855 vect[i] = amrex::coarsen(vect[i], p.vect[i]);
856 }
857 return *this;
858}
859
860template<int dim>
863int
864IntVectND<dim>::maxDir(bool a_doAbsValue) const noexcept
865{
866 int retval = 0;
867 if(a_doAbsValue)
868 {
869 int maxval = std::abs((*this)[0]);
870 for(int idir = 1; idir < dim; idir++)
871 {
872 int curval = std::abs((*this)[idir]);
873 if(curval > maxval)
874 {
875 maxval = curval;
876 retval = idir;
877 }
878 }
879 }
880 else
881 {
882 int maxval = (*this)[0];
883 for(int idir = 1; idir < dim; idir++)
884 {
885 int curval = (*this)[idir];
886 if(curval > maxval)
887 {
888 maxval = curval;
889 retval = idir;
890 }
891 }
892 }
893 return retval;
894}
895
897template<int dim>
900constexpr IntVectND<dim> operator+ (int s, const IntVectND<dim>& p) noexcept
901{
902 IntVectND<dim> retval = p;
903 for (int i=0; i<dim; ++i) {
904 retval[i] = s + retval[i];
905 }
906 return retval;
907}
908
910template<int dim>
914constexpr IntVectND<dim> operator- (int s, const IntVectND<dim>& p) noexcept
915{
916 IntVectND<dim> retval = p;
917 for (int i=0; i<dim; ++i) {
918 retval[i] = s - retval[i];
919 }
920 return retval;
921}
922
924template<int dim>
927constexpr IntVectND<dim> operator* (int s, const IntVectND<dim>& p) noexcept
928{
929 IntVectND<dim> retval = p;
930 for (int i=0; i<dim; ++i) {
931 retval[i] = s * retval[i];
932 }
933 return retval;
934}
935
940template<int dim>
943constexpr IntVectND<dim>
944min (const IntVectND<dim>& p1, const IntVectND<dim>& p2) noexcept
945{
946 IntVectND<dim> p(p1);
947 p.min(p2);
948 return p;
949}
950
951template<int dim>
954constexpr IntVectND<dim>
955elemwiseMin (const IntVectND<dim>& p1, const IntVectND<dim>& p2) noexcept
956{
957 IntVectND<dim> p(p1);
958 p.min(p2);
959 return p;
960}
961
966template<int dim>
969constexpr IntVectND<dim>
970max (const IntVectND<dim>& p1, const IntVectND<dim>& p2) noexcept
971{
972 IntVectND<dim> p(p1);
973 p.max(p2);
974 return p;
975}
976
977template<int dim>
980constexpr IntVectND<dim>
981elemwiseMax (const IntVectND<dim>& p1, const IntVectND<dim>& p2) noexcept
982{
983 IntVectND<dim> p(p1);
984 p.max(p2);
985 return p;
986}
987
993template<int dim = AMREX_SPACEDIM>
996IntVectND<dim>
997BASISV (int dir) noexcept
998{
999 BL_ASSERT(dir >= 0 && dir < dim);
1000 IntVectND<dim> tmp(0);
1001 tmp[dir] = 1;
1002 return tmp;
1003}
1004
1009template<int dim>
1012constexpr IntVectND<dim>
1013scale (const IntVectND<dim>& p, int s) noexcept
1014{
1015 return IntVectND<dim>(p).scale(s);
1016}
1017
1023template<int dim>
1026constexpr IntVectND<dim>
1027reflect (const IntVectND<dim>& a, int ref_ix, int idir) noexcept
1028{
1029 return IntVectND<dim>(a).reflect(ref_ix, idir);
1030}
1031
1036template<int dim>
1039constexpr IntVectND<dim>
1040diagShift (const IntVectND<dim>& p, int s) noexcept
1041{
1042 return p + s;
1043}
1044
1049template<int dim>
1052IntVectND<dim>
1053coarsen (const IntVectND<dim>& p, int s) noexcept
1054{
1055 BL_ASSERT(s > 0);
1056 IntVectND<dim> v = p;
1057 v.coarsen(s);
1058 return v;
1059}
1060
1065template<int dim>
1068IntVectND<dim>
1069coarsen (const IntVectND<dim>& p1, const IntVectND<dim>& p2) noexcept
1070{
1071 IntVectND<dim> v = p1;
1072 v.coarsen(p2);
1073 return v;
1074}
1075
1076template<int dim, std::enable_if_t<( 1<=dim && dim<=3 ), int> = 0>
1077AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
1078constexpr Dim3 refine (Dim3 const& coarse, IntVectND<dim> const& ratio) noexcept
1079{
1080 if constexpr (dim == 1) {
1081 return Dim3{coarse.x*ratio[0], coarse.y, coarse.z};
1082 } else if constexpr (dim == 2) {
1083 return Dim3{coarse.x*ratio[0], coarse.y*ratio[1], coarse.z};
1084 } else {
1085 return Dim3{coarse.x*ratio[0], coarse.y*ratio[1], coarse.z*ratio[2]};
1086 }
1087}
1088
1089template<int dim, std::enable_if_t<( 1<=dim && dim<=3 ), int> = 0>
1090AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
1091Dim3 coarsen (Dim3 const& fine, IntVectND<dim> const& ratio) noexcept
1092{
1093 if constexpr (dim == 1) {
1094 return Dim3{amrex::coarsen(fine.x, ratio[0]),
1095 fine.y,
1096 fine.z};
1097 } else if constexpr (dim == 2) {
1098 return Dim3{amrex::coarsen(fine.x, ratio[0]),
1099 amrex::coarsen(fine.y, ratio[1]),
1100 fine.z};
1101 } else {
1102 return Dim3{amrex::coarsen(fine.x, ratio[0]),
1103 amrex::coarsen(fine.y, ratio[1]),
1104 amrex::coarsen(fine.z, ratio[2])};
1105 }
1106}
1107
1109namespace detail {
1110 std::ostream& int_vector_write (std::ostream& os, const int* iv, int dim);
1111 std::istream& int_vector_read (std::istream& is, int* iv, int dim);
1112
1113 template<int dim>
1115 void IntVectCat_imp (int*& dst, const IntVectND<dim>& src) noexcept {
1116 for (int i=0; i<dim; ++i) {
1117 dst[i] = src[i];
1118 }
1119 dst += dim;
1120 }
1121
1122 template<int dim>
1124 void IntVectSplit_imp2 (IntVectND<dim>& dst, const int*& src) noexcept {
1125 for (int i=0; i<dim; ++i) {
1126 dst[i] = src[i];
1127 }
1128 src += dim;
1129 }
1130
1131 template<class T, std::size_t...Ns>
1133 T IntVectSplit_imp (T& retval, std::index_sequence<Ns...>, const int * src) noexcept {
1134 (IntVectSplit_imp2(amrex::get<Ns>(retval), src), ...);
1135 return retval;
1136 }
1137
1138 template<int...dims>
1140 int get_sum () {
1141 return (0 + ... + dims);
1142 }
1143}
1145
1146template<int dim>
1147std::ostream&
1148operator<< (std::ostream& os, const IntVectND<dim>& iv)
1149{
1150 return detail::int_vector_write(os, iv.begin(), dim);
1151}
1152
1153template<int dim>
1154std::istream&
1155operator>> (std::istream& is, IntVectND<dim>& iv)
1156{
1157 return detail::int_vector_read(is, iv.begin(), dim);
1158}
1159
1164template<int d, int...dims>
1167constexpr IntVectND<detail::get_sum<d, dims...>()>
1168IntVectCat (const IntVectND<d>& v, const IntVectND<dims>&...vects) noexcept {
1169 IntVectND<detail::get_sum<d, dims...>()> retval (0);
1170 int* dst = retval.begin();
1171 detail::IntVectCat_imp(dst, v);
1172 (detail::IntVectCat_imp(dst, vects), ...);
1173 return retval;
1174}
1175
1180template<int d, int...dims>
1183constexpr GpuTuple<IntVectND<d>, IntVectND<dims>...>
1184IntVectSplit (const IntVectND<detail::get_sum<d, dims...>()>& v) noexcept {
1186 return detail::IntVectSplit_imp(retval,
1187 std::make_index_sequence<1 + sizeof...(dims)>(),
1188 v.begin());
1189}
1190
1195template<int new_dim, int old_dim>
1198constexpr IntVectND<new_dim>
1199IntVectShrink (const IntVectND<old_dim>& iv) noexcept {
1200 return iv.template shrink<new_dim>();
1201}
1202
1207template<int new_dim, int old_dim>
1210constexpr IntVectND<new_dim>
1211IntVectExpand (const IntVectND<old_dim>& iv, int fill_extra=0) noexcept {
1212 return iv.template expand<new_dim>(fill_extra);
1213}
1214
1219template<int new_dim, int old_dim>
1222constexpr IntVectND<new_dim>
1223IntVectResize (const IntVectND<old_dim>& iv, int fill_extra=0) noexcept {
1224 return iv.template resize<new_dim>(fill_extra);
1225}
1226
1228template <std::size_t I, int dim>
1230constexpr int get (IntVectND<dim> const& iv) noexcept { return iv.vect[I]; }
1231
1232namespace detail {
1233 template <typename F, int dim, std::size_t... N>
1235 constexpr auto apply_impl (F&& f, IntVectND<dim> const& iv,
1236 std::index_sequence<N...> /*is*/)
1237 {
1238 return std::forward<F>(f)(amrex::get<N>(iv)...);
1239 }
1240}
1241
1244template <typename F, int dim>
1246constexpr auto Apply (F&& f, IntVectND<dim> const& iv)
1247{
1248 return detail::apply_impl(std::forward<F>(f), iv, std::make_index_sequence<dim>());
1249}
1250
1251} // namespace amrex
1252
1253// Spcialize std::tuple_size for IntVectND. Used by structured bindings.
1254template<int dim>
1255struct std::tuple_size<amrex::IntVectND<dim>> {
1256 static constexpr std::size_t value = dim;
1257};
1258
1259// Spcialize std::tuple_element for IntVectND. Used by structured bindings.
1260template<std::size_t s, int dim>
1261struct std::tuple_element<s, amrex::IntVectND<dim>> {
1262 using type = int;
1263};
1264
1265#endif /*AMREX_INTVECT_H*/
#define BL_ASSERT(EX)
Definition AMReX_BLassert.H:39
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:119
#define AMREX_GPU_HOST_DEVICE
Definition AMReX_GpuQualifiers.H:20
int idir
Definition AMReX_HypreMLABecLap.cpp:1093
GPU-compatible tuple.
Definition AMReX_Tuple.H:98
An Integer Vector in dim-Dimensional Space.
Definition AMReX_IntVect.H:57
__host__ __device__ constexpr IntVectND< dim > & operator*=(int s) noexcept
Modifies IntVectND by multiplication of a scalar to each component.
Definition AMReX_IntVect.H:502
__host__ __device__ IntVectND(const int *a) noexcept
Construct an IntVectND setting the coordinates to the corresponding values in the integer array a.
Definition AMReX_IntVect.H:131
__host__ __device__ int * getVect() &noexcept
Definition AMReX_IntVect.H:293
__host__ __device__ constexpr IntVectND< dim > operator*(const IntVectND< dim > &p) const noexcept
Returns component-wise product of IntVectND and argument.
Definition AMReX_IntVect.H:584
__host__ __device__ constexpr int & operator[](int i) noexcept
Returns a reference to the i'th coordinate of the IntVectND.
Definition AMReX_IntVect.H:247
__host__ __device__ constexpr bool allGT(const IntVectND< dim > &rhs) const noexcept
Returns true if this is greater than argument for all components. NOTE: This is NOT a strict weak ord...
Definition AMReX_IntVect.H:425
__host__ __device__ constexpr bool operator>(const IntVectND< dim > &rhs) const noexcept
Return true if this is lexicographically greater than rhs.
Definition AMReX_IntVect.H:360
__host__ __device__ constexpr IntVectND< dim > & shift(int coord, int s) noexcept
Modify IntVectND by adding s to given coordinate.
Definition AMReX_IntVect.H:649
__host__ __device__ constexpr int min() const noexcept
minimum (no absolute values) value
Definition AMReX_IntVect.H:232
__host__ __device__ constexpr IntVectND< new_dim > expand(int fill_extra=0) const noexcept
Returns a new IntVectND of size new_dim and assigns all values of this IntVectND to it and fill_extra...
Definition AMReX_IntVect.H:760
__host__ __device__ constexpr IntVectND< dim > & operator+=(int s) noexcept
Modifies IntVectND by addition of a scalar to each component.
Definition AMReX_IntVect.H:484
__host__ __device__ constexpr IntVectND< dim > & shift(const IntVectND< dim > &iv) noexcept
Equivalent to shift(0,iv[0]).shift(1,iv[1]) ...
Definition AMReX_IntVect.H:655
int value_type
Definition AMReX_IntVect.H:741
__host__ __device__ constexpr Array< T, dim > toArray() const noexcept
Definition AMReX_IntVect.H:197
__host__ __device__ constexpr int & get() noexcept
Returns a reference to the i'th coordinate of the IntVectND. Used by structured bindings.
Definition AMReX_IntVect.H:256
__host__ __device__ int * getVect() &&=delete
constexpr IntVectND(const Array< int, dim > &a) noexcept
Construct an IntVectND from an Array<int,dim>.
Definition AMReX_IntVect.H:152
__host__ static __device__ constexpr IntVectND< dim > TheMaxVector() noexcept
Definition AMReX_IntVect.H:723
__host__ static __device__ constexpr std::size_t size() noexcept
Definition AMReX_IntVect.H:732
__host__ __device__ IntVectND & setVal(int i, int val) noexcept
Set i'th coordinate of IntVectND to val.
Definition AMReX_IntVect.H:281
IntVectND(const Vector< int > &a) noexcept
Construct an IntVectND from an Vector<int>. It is an error if the Vector<int> doesn't have the same d...
Definition AMReX_IntVect.H:142
__host__ __device__ constexpr bool allGT(int rhs) const noexcept
Returns true if this is greater than argument for all components.
Definition AMReX_IntVect.H:437
__host__ __device__ constexpr int * begin() noexcept
Returns a pointer to the first element of the IntVectND.
Definition AMReX_IntVect.H:265
__host__ __device__ IntVectND< dim > & coarsen(const IntVectND< dim > &p) noexcept
Modify IntVectND<dim> by component-wise integer projection.
Definition AMReX_IntVect.H:851
__host__ static __device__ constexpr IntVectND< dim > TheUnitVector() noexcept
This static member function returns a reference to a constant IntVectND object, all of whose dim argu...
Definition AMReX_IntVect.H:689
__host__ __device__ int maxDir(bool a_doAbsValue) const noexcept
Definition AMReX_IntVect.H:864
__host__ __device__ const int * getVect() const &noexcept
Returns a const pointer to an array of coordinates of the IntVectND. Useful for arguments to FORTRAN ...
Definition AMReX_IntVect.H:291
static const IntVectND< dim > Unit
Definition AMReX_IntVect.H:791
__host__ __device__ constexpr IntVectND< dim > & min(const IntVectND< dim > &p) noexcept
Modifies IntVectND by taking component-wise min with argument.
Definition AMReX_IntVect.H:612
__host__ __device__ constexpr int sum() const noexcept
Definition AMReX_IntVect.H:210
__host__ __device__ constexpr IntVectND< dim > & scale(int s) noexcept
Modify IntVectND by multiplying each coordinate by s.
Definition AMReX_IntVect.H:630
__host__ __device__ constexpr const int * begin() const noexcept
Returns a pointer to the first element of the IntVectND.
Definition AMReX_IntVect.H:269
__host__ __device__ constexpr bool allGE(const IntVectND< dim > &rhs) const noexcept
Returns true if this is greater than or equal to argument for all components. NOTE: This is NOT a str...
Definition AMReX_IntVect.H:450
__host__ __device__ constexpr IntVectND< dim > & reflect(int ref_ix, int idir) noexcept
Modify IntVectND by reflecting it in the plane defined by the index ref_ix and with normal in the dir...
Definition AMReX_IntVect.H:641
__host__ static __device__ constexpr IntVectND< dim > TheCellVector() noexcept
This static member function returns a reference to a constant IntVectND object, all of whose dim argu...
Definition AMReX_IntVect.H:718
__host__ __device__ constexpr IntVectND(int i, int j, Args...ks) noexcept
Construct an IntVectND given the specific values for its coordinates. The inputs for this constructor...
Definition AMReX_IntVect.H:114
__host__ __device__ constexpr bool operator==(int val) const noexcept
Returns true if all components are equal to the argument val.
Definition AMReX_IntVect.H:299
__host__ __device__ constexpr IntVectND< dim > & diagShift(int s) noexcept
Modify IntVectND by adding s to each coordinate.
Definition AMReX_IntVect.H:658
__host__ __device__ constexpr IntVectND< dim > operator-() const noexcept
Unary Minus – negates all components.
Definition AMReX_IntVect.H:475
__host__ __device__ constexpr IntVectND< new_dim > resize(int fill_extra=0) const noexcept
Returns a new IntVectND of size new_dim by either shrinking or expanding this IntVectND.
Definition AMReX_IntVect.H:775
__host__ static __device__ constexpr IntVectND< dim > TheZeroVector() noexcept
This static member function returns a reference to a constant IntVectND object, all of whose dim argu...
Definition AMReX_IntVect.H:679
__host__ __device__ constexpr IntVectND< dim > operator+() const noexcept
Unary plus – for completeness.
Definition AMReX_IntVect.H:472
__host__ __device__ IntVectND< dim > & coarsen(int p) noexcept
Modify IntVectND<dim> by component-wise integer projection.
Definition AMReX_IntVect.H:823
__host__ __device__ constexpr int max() const noexcept
maximum (no absolute values) value
Definition AMReX_IntVect.H:221
__host__ __device__ constexpr bool allLT(const IntVectND< dim > &rhs) const noexcept
Returns true if this is less than argument for all components. NOTE: This is NOT a strict weak orderi...
Definition AMReX_IntVect.H:375
__host__ static __device__ constexpr IntVectND< dim > TheNodeVector() noexcept
This static member function returns a reference to a constant IntVectND object, all of whose dim argu...
Definition AMReX_IntVect.H:709
static const IntVectND< dim > Zero
Definition AMReX_IntVect.H:786
__host__ __device__ constexpr bool allLE(int rhs) const noexcept
Returns true if this is less than or equal to argument for all components.
Definition AMReX_IntVect.H:412
__host__ __device__ constexpr IntVectND< dim > & operator-=(int s) noexcept
Modifies IntVectND by subtraction of a scalar to each component.
Definition AMReX_IntVect.H:538
__host__ __device__ constexpr IntVectND< dim > operator/(const IntVectND< dim > &p) const noexcept
Returns component-wise division of IntVectND by argument.
Definition AMReX_IntVect.H:598
__host__ __device__ constexpr IntVectND< dim > & operator/=(int s) noexcept
Modifies IntVectND by division by a scalar to each component.
Definition AMReX_IntVect.H:520
__host__ __device__ constexpr int * end() noexcept
Returns a pointer to the (last+1) element of the IntVectND.
Definition AMReX_IntVect.H:273
__host__ static __device__ constexpr int isize() noexcept
Definition AMReX_IntVect.H:737
__host__ __device__ constexpr bool operator<=(const IntVectND< dim > &rhs) const noexcept
Return true if this is lexicographically less than or equal to rhs.
Definition AMReX_IntVect.H:354
__host__ __device__ constexpr IntVectND(int s) noexcept
Construct an IntVectND whose components are all the same.
Definition AMReX_IntVect.H:120
__host__ __device__ constexpr const int * end() const noexcept
Returns a pointer to the (last+1) element of the IntVectND.
Definition AMReX_IntVect.H:277
__host__ __device__ constexpr bool operator<(const IntVectND< dim > &rhs) const noexcept
Return true if this is lexicographically less than rhs.
Definition AMReX_IntVect.H:341
__host__ __device__ constexpr const int & get() const noexcept
Returns a reference to the i'th coordinate of the IntVectND. Used by structured bindings.
Definition AMReX_IntVect.H:261
__host__ __device__ constexpr bool operator!=(int val) const noexcept
Returns true if any component is not equal to the argument val.
Definition AMReX_IntVect.H:310
__host__ __device__ constexpr bool allLE(const IntVectND< dim > &rhs) const noexcept
Returns true if this is less than or equal to argument for all components. NOTE: This is NOT a strict...
Definition AMReX_IntVect.H:400
__host__ __device__ constexpr bool allGE(int rhs) const noexcept
Returns true if this is greater than or equal to argument for all components.
Definition AMReX_IntVect.H:462
__host__ __device__ constexpr IntVectND< dim > & max(const IntVectND< dim > &p) noexcept
Modifies IntVectND by taking component-wise max with argument.
Definition AMReX_IntVect.H:621
__host__ __device__ constexpr bool allLT(int rhs) const noexcept
Returns true if this is less than argument for all components.
Definition AMReX_IntVect.H:387
constexpr IntVectND() noexcept
Construct an IntVectND whose components are all zero.
Definition AMReX_IntVect.H:101
int vect[dim]
Definition AMReX_IntVect.H:793
__host__ static __device__ constexpr IntVectND< dim > TheMinVector() noexcept
Definition AMReX_IntVect.H:727
__host__ __device__ constexpr bool operator>=(const IntVectND< dim > &rhs) const noexcept
Return true if this is lexicographically greater than or equal to rhs.
Definition AMReX_IntVect.H:366
__host__ __device__ constexpr IntVectND< new_dim > shrink() const noexcept
Returns a new IntVectND of size new_dim and assigns the first new_dim values of this IntVectND to it.
Definition AMReX_IntVect.H:749
__host__ static __device__ constexpr IntVectND< dim > TheDimensionVector(int d) noexcept
This static member function returns a reference to a constant IntVectND object, all of whose dim argu...
Definition AMReX_IntVect.H:698
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:28
__host__ __device__ BoxND< dim > coarsen(const BoxND< dim > &b, int ref_ratio) noexcept
Coarsen BoxND by given (positive) coarsening ratio.
Definition AMReX_Box.H:1409
std::array< T, N > Array
Definition AMReX_Array.H:26
Definition AMReX_Amr.cpp:49
__host__ __device__ void ignore_unused(const Ts &...)
This shuts up the compiler about unused variables.
Definition AMReX.H:138
__host__ __device__ constexpr IntVectND< new_dim > IntVectResize(const IntVectND< old_dim > &iv, int fill_extra=0) noexcept
Returns a new IntVectND of size new_dim by either shrinking or expanding iv.
Definition AMReX_IntVect.H:1223
__host__ __device__ constexpr GpuTuple< IntVectND< d >, IntVectND< dims >... > IntVectSplit(const IntVectND< detail::get_sum< d, dims... >()> &v) noexcept
Returns a tuple of IntVectND obtained by splitting the input IntVectND according to the dimensions sp...
Definition AMReX_IntVect.H:1184
__host__ __device__ IntVectND< dim > BASISV(int dir) noexcept
Returns a basis vector in the given coordinate direction; eg. IntVectND<3> BASISV<3>(1) == (0,...
Definition AMReX_IntVect.H:997
__host__ __device__ constexpr T elemwiseMax(T const &a, T const &b) noexcept
Return the element-wise maximum of the given values for types like XDim3.
Definition AMReX_Algorithm.H:77
__host__ __device__ GpuComplex< T > operator*(const GpuComplex< T > &a_x, const GpuComplex< U > &a_y) noexcept
Multiply two complex numbers.
Definition AMReX_GpuComplex.H:257
__host__ __device__ constexpr IntVectND< dim > diagShift(const IntVectND< dim > &p, int s) noexcept
Returns IntVectND obtained by adding s to each of the components of this IntVectND.
Definition AMReX_IntVect.H:1040
__host__ __device__ constexpr const T & min(const T &a, const T &b) noexcept
Definition AMReX_Algorithm.H:24
constexpr bool IsConvertible_v
Definition AMReX_TypeTraits.H:276
__host__ __device__ constexpr IntVectND< dim > reflect(const IntVectND< dim > &a, int ref_ix, int idir) noexcept
Returns an IntVectND that is the reflection of input in the plane which passes through ref_ix and nor...
Definition AMReX_IntVect.H:1027
__host__ __device__ constexpr IntVectND< new_dim > IntVectShrink(const IntVectND< old_dim > &iv) noexcept
Returns a new IntVectND of size new_dim and assigns the first new_dim values of iv to it.
Definition AMReX_IntVect.H:1199
__host__ __device__ constexpr auto Apply(F &&f, IntVectND< dim > const &iv)
Definition AMReX_IntVect.H:1246
__host__ __device__ constexpr IntVectND< dim > scale(const IntVectND< dim > &p, int s) noexcept
Returns a IntVectND obtained by multiplying each of the components of this IntVectND by s.
Definition AMReX_IntVect.H:1013
__host__ __device__ constexpr const T & max(const T &a, const T &b) noexcept
Definition AMReX_Algorithm.H:44
__host__ __device__ constexpr IntVectND< new_dim > IntVectExpand(const IntVectND< old_dim > &iv, int fill_extra=0) noexcept
Returns a new IntVectND of size new_dim and assigns all values of iv to it and fill_extra to the rema...
Definition AMReX_IntVect.H:1211
const int[]
Definition AMReX_BLProfiler.cpp:1664
__host__ __device__ XDim3 operator-(XDim3 const &a, XDim3 const &b)
Definition AMReX_Dim3.H:34
__host__ __device__ XDim3 operator+(XDim3 const &a, XDim3 const &b)
Definition AMReX_Dim3.H:28
__host__ __device__ constexpr T elemwiseMin(T const &a, T const &b) noexcept
Return the element-wise minimum of the given values for types like XDim3.
Definition AMReX_Algorithm.H:62
__host__ __device__ constexpr int get(IntVectND< dim > const &iv) noexcept
Get I'th element of IntVectND<dim>
Definition AMReX_IntVect.H:1230
Hash function for IntVectND.
Definition AMReX_IntVect.H:62
std::size_t operator()(const IntVectND< dim > &vec) const noexcept
Definition AMReX_IntVect.H:63
int type
Definition AMReX_IntVect.H:1262