/home/runner/work/amr-wind/amr-wind/amr-wind/utilities/index_operations.H Source File

AMR-Wind API: /home/runner/work/amr-wind/amr-wind/amr-wind/utilities/index_operations.H Source File
AMR-Wind API v0.1.0
CFD solver for wind plant simulations
Loading...
Searching...
No Matches
index_operations.H
Go to the documentation of this file.
1#ifndef INDEX_OPERATIONS_H
2#define INDEX_OPERATIONS_H
3#include <AMReX_REAL.H>
4#include <AMReX_Vector.H>
5#include <AMReX_Box.H>
6#include <AMReX_RealBox.H>
7#include <AMReX_RealVect.H>
8#include <AMReX_Geometry.H>
9
10namespace amr_wind::utils {
11
13AMREX_FORCE_INLINE int closest_index(
14 const amrex::Vector<amrex::Real>& vec,
15 const amrex::Real value,
16 const amrex::Real tol = 0.0)
17{
18 auto it = std::upper_bound(vec.begin(), vec.end(), value);
19 if (it == vec.end()) {
20 // Try again with tolerance added
21 it = std::upper_bound(vec.begin(), vec.end(), value - tol);
22 }
23 AMREX_ALWAYS_ASSERT(it != vec.end());
24
25 const int idx = static_cast<int>(std::distance(vec.begin(), it));
26 return std::max(idx - 1, 0);
27}
28
30template <typename T = amrex::GpuArray<int, 2>>
31AMREX_FORCE_INLINE T perpendicular_idx(const int normal)
32{
33 switch (normal) {
34 case 0:
35 return T{1, 2};
36 case 1:
37 return T{0, 2};
38 case 2:
39 return T{0, 1};
40 default:
41 amrex::Abort("Invalid normal value to determine perpendicular indices");
42 }
43 return T{-1, -1};
44}
45
60 amrex::IntVect& shift_to_cc,
61 amrex::Box grown_interior_box,
62 const amrex::Box& domain_boundary_box,
63 const amrex::Orientation& ori)
64{
65 shift_to_cc = {0, 0, 0};
66 const auto& field_location_vector = grown_interior_box.type();
67 if (!grown_interior_box.cellCentered()) {
68 grown_interior_box.enclosedCells();
69 }
70 auto bx = grown_interior_box & domain_boundary_box;
71 if (bx.isEmpty()) {
72 return bx;
73 }
74
75 if (ori.isHigh() && field_location_vector[ori.coordDir()] == 1) {
76 bx.shift(field_location_vector);
77 shift_to_cc = -field_location_vector;
78 }
79 return bx;
80}
81
82// This version omits the shift to cell-centered data, often not needed
84 amrex::Box grown_interior_box,
85 const amrex::Box& domain_boundary_box,
86 const amrex::Orientation& ori)
87{
88 amrex::IntVect shift_to_cc = {0, 0, 0};
90 shift_to_cc, grown_interior_box, domain_boundary_box, ori);
91}
92
94AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool contains(
95 const amrex::Box& box,
96 const amrex::RealVect& pos,
97 const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& prob_lo,
98 const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& dxinv)
99{
100 const amrex::IntVect iv(AMREX_D_DECL(
101 static_cast<int>(amrex::Math::floor((pos[0] - prob_lo[0]) * dxinv[0])),
102 static_cast<int>(amrex::Math::floor((pos[1] - prob_lo[1]) * dxinv[1])),
103 static_cast<int>(
104 amrex::Math::floor((pos[2] - prob_lo[2]) * dxinv[2]))));
105 return box.contains(iv);
106}
107
115amrex::Box
116realbox_to_box(const amrex::RealBox& rbx, const amrex::Geometry& geom);
117
118} // namespace amr_wind::utils
119
120#endif /* INDEX_OPERATIONS_H */
Definition MultiParser.H:7
AMREX_FORCE_INLINE int closest_index(const amrex::Vector< amrex::Real > &vec, const amrex::Real value, const amrex::Real tol=0.0)
Return closest index (from lower) of value in vector.
Definition index_operations.H:13
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool contains(const amrex::Box &box, const amrex::RealVect &pos, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &prob_lo, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &dxinv)
Check if a point is inside a box.
Definition index_operations.H:94
amrex::Box face_aware_boundary_box_intersection(amrex::IntVect &shift_to_cc, amrex::Box grown_interior_box, const amrex::Box &domain_boundary_box, const amrex::Orientation &ori)
Definition index_operations.H:59
AMREX_FORCE_INLINE T perpendicular_idx(const int normal)
Return indices perpendicular to normal.
Definition index_operations.H:31
amrex::Box realbox_to_box(const amrex::RealBox &rbx, const amrex::Geometry &geom)
Definition index_operations.cpp:5