Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_EB_STL_utils.H
Go to the documentation of this file.
1#ifndef AMREX_EB_STL_UTILS_H_
2#define AMREX_EB_STL_UTILS_H_
3
4#include <AMReX_Config.H>
5#include <AMReX_Geometry.H>
6#include <AMReX_MultiFab.H>
7#include <AMReX_Dim3.H>
8#include <AMReX_EB2_Graph.H>
9
10#include <algorithm>
11#include <cstdint>
12#include <limits>
13#include <utility>
14
20
21namespace amrex
22{
23
26{
27public:
28
30 struct Triangle {
31 XDim3 v1, v2, v3;
32
33 [[nodiscard]] Real cent (int d) const
34 {
35 static_assert(sizeof(XDim3) == sizeof(Real)*3);
36 return Real(1./3.)*((&v1.x)[d] + (&v2.x)[d] + (&v3.x)[d]); // NOLINT(clang-analyzer-security.ArrayBound)
37 }
38
39 [[nodiscard]] std::pair<Real,Real> minmax (int d) const
40 {
41 static_assert(sizeof(XDim3) == sizeof(Real)*3);
42 return std::minmax({(&v1.x)[d], (&v2.x)[d], (&v3.x)[d]}); // NOLINT(clang-analyzer-security.ArrayBound)
43 }
44 };
46
48 template <int M, int N>
49 struct BVHNodeT
50 {
51 RealBox boundingbox{AMREX_D_DECL(std::numeric_limits<Real>::max(),
52 std::numeric_limits<Real>::max(),
53 std::numeric_limits<Real>::max()),
54 AMREX_D_DECL(std::numeric_limits<Real>::lowest(),
55 std::numeric_limits<Real>::lowest(),
56 std::numeric_limits<Real>::lowest())};
57 STLtools::Triangle triangles[M];
58 XDim3 trinorm[M];
59 int children[N];
60 std::int8_t ntriangles = 0;
61 std::int8_t nchildren = 0;
62 };
64
65 static constexpr int m_bvh_max_size = 4; // max # of triangles in a leaf node
66 static constexpr int m_bvh_max_splits = 4; // max # of children
67 static constexpr int m_bvh_max_stack_size = 12; // max depth of the tree
68 static constexpr int m_num_ref = 4; // # of reference points for inside/outside rays
69
70 using Node = BVHNodeT<m_bvh_max_size,m_bvh_max_splits>;
71
72 static constexpr int allregular = -1;
73 static constexpr int mixedcells = 0;
74 static constexpr int allcovered = 1;
75
78 void setBVHOptimization (bool flag) {
79 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(!flag || m_num_tri == 0 || !m_bvh_nodes.empty(),
80 "STLtools: BVH optimization must be enabled before reading the STL file");
81 m_bvh_optimization = flag;
82 }
83
92 void read_stl_file (std::string const& fname, Real scale, Array<Real,3> const& center,
93 int reverse_normal);
94
98 void fill (MultiFab& mf, IntVect const& nghost, Geometry const& geom,
99 Real outside_value = -1._rt, Real inside_value = 1._rt) const;
100
102 [[nodiscard]] int getBoxType (Box const& box, Geometry const& geom, RunOn, Box const&) const;
103
104 static constexpr bool isGPUable () noexcept { return true; }
105
107 void fillFab (BaseFab<Real>& levelset, const Geometry& geom, RunOn,
108 Box const& bounding_box) const;
109
111 void getIntercept (Array<Array4<Real>,AMREX_SPACEDIM> const& inter_arr,
112 Array<Array4<EB2::Type_t const>,AMREX_SPACEDIM> const& type_arr,
113 Array4<Real const> const& lst, Geometry const& geom, RunOn,
114 Box const& bounding_box) const;
115
117 static void updateIntercept (Array<Array4<Real>,AMREX_SPACEDIM> const& inter_arr,
118 Array<Array4<EB2::Type_t const>,AMREX_SPACEDIM> const& type_arr,
119 Array4<Real const> const& lst, Geometry const& geom) ;
120
122 void fillSignedDistance (MultiFab& mf, IntVect const& nghost, Geometry const& geom) const;
123
125 void prepare (Gpu::PinnedVector<Triangle> a_tri_pts); // public for cuda
126
127private:
128
129 bool m_bvh_optimization = true;
130
131 Gpu::DeviceVector<Triangle> m_tri_pts_d;
132 Gpu::DeviceVector<XDim3> m_tri_normals_d;
133 Gpu::DeviceVector<Node> m_bvh_nodes;
134
135 int m_num_tri=0;
136
137 XDim3 m_ptmin; // All triangles are inside the bounding box defined by
138 XDim3 m_ptmax; // m_ptmin and m_ptmax, grown by roundoff.
139 XDim3 m_bbmin; // The bounding box of the triangles
140 XDim3 m_bbmax;
141 // Reference points slightly outside the bounding box. The others are
142 // used only when the ray from the first one is ambiguous.
144 bool m_boundry_is_outside; // Is the bounding box boundary outside or inside the object?
145
146 void read_ascii_stl_file (std::string const& fname, Real scale,
147 Array<Real,3> const& center, int reverse_normal,
148 Gpu::PinnedVector<Triangle>& a_tri_pts);
149 void read_binary_stl_file (std::string const& fname, Real scale,
150 Array<Real,3> const& center, int reverse_normal,
151 Gpu::PinnedVector<Triangle>& a_tri_pts);
152
153 static void build_bvh (Triangle* begin, Triangle * end, Gpu::PinnedVector<Node>& bvh_nodes);
154 static void bvh_size (int ntri, std::size_t& nnodes);
155};
156
157}
158#endif
#define AMREX_ALWAYS_ASSERT_WITH_MESSAGE(EX, MSG)
Definition AMReX_BLassert.H:49
Simple structs for 3-component integer and real-valued tuples.
Problem-domain geometry: maps between index space and physical space.
#define AMREX_D_DECL(a, b, c)
Definition AMReX_SPACE.H:171
A FortranArrayBox(FAB)-like object.
Definition AMReX_BaseFab.H:222
Rectangular problem domain geometry.
Definition AMReX_Geometry.H:87
A collection (stored as an array) of FArrayBox objects.
Definition AMReX_MultiFab.H:40
A Box with real dimensions.
Definition AMReX_RealBox.H:28
Utility class that loads STL meshes and samples them onto AMReX grids.
Definition AMReX_EB_STL_utils.H:26
static constexpr int m_num_ref
Definition AMReX_EB_STL_utils.H:68
static constexpr int allregular
Definition AMReX_EB_STL_utils.H:72
static constexpr int m_bvh_max_stack_size
Definition AMReX_EB_STL_utils.H:67
static void updateIntercept(Array< Array4< Real >, 3 > const &inter_arr, Array< Array4< EB2::Type_t const >, 3 > const &type_arr, Array4< Real const > const &lst, Geometry const &geom)
Update intercepts.
Definition AMReX_EB_STL_utils.cpp:1460
static constexpr int m_bvh_max_size
Definition AMReX_EB_STL_utils.H:65
static constexpr int m_bvh_max_splits
Definition AMReX_EB_STL_utils.H:66
void fillSignedDistance(MultiFab &mf, IntVect const &nghost, Geometry const &geom) const
Fill mf with signed-distance values sampled from the STL mesh.
Definition AMReX_EB_STL_utils.cpp:1498
BVHNodeT< m_bvh_max_size, m_bvh_max_splits > Node
Definition AMReX_EB_STL_utils.H:70
void prepare(Gpu::PinnedVector< Triangle > a_tri_pts)
Upload triangle data to device memory (exposed for CUDA workflows).
Definition AMReX_EB_STL_utils.cpp:664
void fill(MultiFab &mf, IntVect const &nghost, Geometry const &geom, Real outside_value=-1._rt, Real inside_value=1._rt) const
Fill a level-set MultiFab sampled from the STL mesh.
Definition AMReX_EB_STL_utils.cpp:1026
void getIntercept(Array< Array4< Real >, 3 > const &inter_arr, Array< Array4< EB2::Type_t const >, 3 > const &type_arr, Array4< Real const > const &lst, Geometry const &geom, RunOn, Box const &bounding_box) const
Compute intercepts and face types for EB geometry based on the STL mesh.
Definition AMReX_EB_STL_utils.cpp:1277
void read_stl_file(std::string const &fname, Real scale, Array< Real, 3 > const &center, int reverse_normal)
Read an STL file, apply scaling/translation, and store triangles.
Definition AMReX_EB_STL_utils.cpp:511
void setBVHOptimization(bool flag)
Definition AMReX_EB_STL_utils.H:78
static constexpr int mixedcells
Definition AMReX_EB_STL_utils.H:73
static constexpr bool isGPUable() noexcept
Definition AMReX_EB_STL_utils.H:104
static constexpr int allcovered
Definition AMReX_EB_STL_utils.H:74
void fillFab(BaseFab< Real > &levelset, const Geometry &geom, RunOn, Box const &bounding_box) const
Fill a BaseFab with level-set data (host/device aware).
Definition AMReX_EB_STL_utils.cpp:1225
int getBoxType(Box const &box, Geometry const &geom, RunOn, Box const &) const
Return whether the EB is regular/covered/mixed inside box.
Definition AMReX_EB_STL_utils.cpp:1084
amrex_real Real
Floating Point Type for Fields.
Definition AMReX_REAL.H:80
ArrayND< T, 4, true > Array4
Convenient alias for a 4D ArrayND with three spatial dimensions plus a component dimension.
Definition AMReX_Array4.H:1340
PODVector< T, PinnedArenaAllocator< T > > PinnedVector
A PODVector that uses the pinned memory arena.
Definition AMReX_GpuContainers.H:48
std::array< T, N > Array
Definition AMReX_Array.H:31
PODVector< T, ArenaAllocator< T > > DeviceVector
A PODVector that uses the standard memory Arena. Note that the memory might or might not be managed d...
Definition AMReX_GpuContainers.H:24
Definition AMReX_Amr.cpp:50
RunOn
Definition AMReX_GpuControl.H:65
__host__ __device__ Dim3 begin(BoxND< dim > const &box) noexcept
Return the iterator begin coordinate of box as Dim3.
Definition AMReX_Box.H:2241
BoxND< 3 > Box
Box is an alias for amrex::BoxND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:35
__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:1128
IntVectND< 3 > IntVect
IntVect is an alias for amrex::IntVectND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:38
__host__ __device__ Dim3 end(BoxND< dim > const &box) noexcept
Return the iterator end coordinate of box as Dim3.
Definition AMReX_Box.H:2259
@ levelset
Definition AMReX_EBData.H:20
Fixed-size array that can be used on GPU.
Definition AMReX_Array.H:52
A simple struct holding 3 Real values for a 3D coordinate.
Definition AMReX_Dim3.H:32
Real x
Definition AMReX_Dim3.H:32