/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 <algorithm>
4
5#include "AMReX_REAL.H"
6#include "AMReX_Vector.H"
7#include "AMReX_Box.H"
8#include "AMReX_RealBox.H"
9#include "AMReX_RealVect.H"
10#include "AMReX_Geometry.H"
11
12using namespace amrex::literals;
13
14namespace amr_wind::utils {
15
17AMREX_FORCE_INLINE int closest_index(
18 const amrex::Vector<amrex::Real>& vec,
19 const amrex::Real value,
20 const amrex::Real tol = 0.0_rt)
21{
22 auto it = std::ranges::upper_bound(vec, value);
23 if (it == vec.end()) {
24 // Try again with tolerance added
25 it = std::ranges::upper_bound(vec, value - tol);
26 }
27 AMREX_ALWAYS_ASSERT(it != vec.end());
28
29 const int idx = static_cast<int>(std::distance(vec.begin(), it));
30 return amrex::max(idx - 1, 0);
31}
32
34template <typename T = amrex::GpuArray<int, 2>>
35AMREX_FORCE_INLINE T perpendicular_idx(const int normal)
36{
37 switch (normal) {
38 case 0:
39 return T{1, 2};
40 case 1:
41 return T{0, 2};
42 case 2:
43 return T{0, 1};
44 default:
45 amrex::Abort("Invalid normal value to determine perpendicular indices");
46 }
47 return T{-1, -1};
48}
49
64 amrex::IntVect& shift_to_cc,
65 amrex::Box grown_interior_box,
66 const amrex::Box& domain_boundary_box,
67 const amrex::Orientation& ori)
68{
69 shift_to_cc = {0, 0, 0};
70 const auto& field_location_vector = grown_interior_box.type();
71 if (!grown_interior_box.cellCentered()) {
72 grown_interior_box.enclosedCells();
73 }
74 auto bx = grown_interior_box & domain_boundary_box;
75 if (bx.isEmpty()) {
76 return bx;
77 }
78
79 if (ori.isHigh() && field_location_vector[ori.coordDir()] == 1) {
80 bx.shift(field_location_vector);
81 shift_to_cc = -field_location_vector;
82 }
83 return bx;
84}
85
86// This version omits the shift to cell-centered data, often not needed
88 amrex::Box grown_interior_box,
89 const amrex::Box& domain_boundary_box,
90 const amrex::Orientation& ori)
91{
92 amrex::IntVect shift_to_cc = {0, 0, 0};
94 shift_to_cc, grown_interior_box, domain_boundary_box, ori);
95}
96
98AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool contains(
99 const amrex::Box& box,
100 const amrex::RealVect& pos,
101 const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& prob_lo,
102 const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& dxinv)
103{
104 const amrex::IntVect iv(AMREX_D_DECL(
105 static_cast<int>(amrex::Math::floor((pos[0] - prob_lo[0]) * dxinv[0])),
106 static_cast<int>(amrex::Math::floor((pos[1] - prob_lo[1]) * dxinv[1])),
107 static_cast<int>(
108 amrex::Math::floor((pos[2] - prob_lo[2]) * dxinv[2]))));
109 return box.contains(iv);
110}
111
119amrex::Box
120realbox_to_box(const amrex::RealBox& rbx, const amrex::Geometry& geom);
121
122} // namespace amr_wind::utils
123
124#endif /* INDEX_OPERATIONS_H */
Definition MultiParser.H:7
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:98
AMREX_INLINE 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:63
AMREX_FORCE_INLINE T perpendicular_idx(const int normal)
Return indices perpendicular to normal.
Definition index_operations.H:35
AMREX_FORCE_INLINE int closest_index(const amrex::Vector< amrex::Real > &vec, const amrex::Real value, const amrex::Real tol=0.0_rt)
Return closest index (from lower) of value in vector.
Definition index_operations.H:17
amrex::Box realbox_to_box(const amrex::RealBox &rbx, const amrex::Geometry &geom)
Definition index_operations.cpp:5