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