ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ERF_EBPolygon.H
Go to the documentation of this file.
1 #ifndef ERF_EB_POLYGON_H_
2 #define ERF_EB_POLYGON_H_
3 
4 #include <AMReX_REAL.H>
5 #include <AMReX_RealVect.H>
6 
7 #include "ERF_Constants.H"
8 
9 class polygon_ {
10 
11  public:
12 
13  AMREX_GPU_HOST_DEVICE
14  polygon_ ( amrex::RealVect a_point,
15  amrex::RealVect a_normal )
16  : m_cell_face(1)
17  , m_eb_point(a_point)
18  , m_eb_normal(a_normal)
19  , m_defined(0)
20  , m_vertices(0)
21  , m_area(0.)
22  , m_sorted(0)
23  , m_vertex({amrex::RealVect(0.), amrex::RealVect(0.), amrex::RealVect(0.),
24  amrex::RealVect(0.), amrex::RealVect(0.), amrex::RealVect(0.)})
25  , m_zdir(0.)
26  {}
27 
28  AMREX_GPU_HOST_DEVICE
30  : m_cell_face(0)
31  , m_eb_point(0.)
32  , m_eb_normal(0.)
33  , m_defined(0)
34  , m_vertices(0)
35  , m_area(0.)
36  , m_sorted(0)
37  , m_vertex({amrex::RealVect(0.), amrex::RealVect(0.), amrex::RealVect(0.),
38  amrex::RealVect(0.), amrex::RealVect(0.), amrex::RealVect(0.)})
39  , m_zdir(0.)
40  {}
41 
42  AMREX_GPU_HOST_DEVICE
43  void add_vertex ( amrex::RealVect const& a_v ) {
44  AMREX_ASSERT( m_vertices < m_max_vertices );
45  m_vertex[m_vertices] = a_v;
46  m_vertices++;
47  }
48 
49  AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
50  void set_area ( amrex::Real const& a_area ) { m_area = a_area; }
51 
52  AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
53  void define () {
54 
55  AMREX_ALWAYS_ASSERT( m_defined == 0 ); // TODO ---------------------------- remove ALWAYS
56 
57  m_defined = 1;
58 
59  // We need at least 3 faces.
60  if (m_vertices < 3) { return; }
61 
62  // Check to see if the vertices of the face are inside or outside of the plane.
63  // If they are outside, this face doesn't belong to the volume.
64  if ( m_cell_face ) {
65  for ( int i(0); i<m_vertices; ++i) {
66  if ((m_eb_normal.dotProduct(m_vertex[i] - m_eb_point)) >= 0.) {
67  } else {
68  }
69  }
70  }
71 
72  // Calculate the centroid of the polygon.
73  amrex::RealVect centroid = get_centroid();
74 
75  // Shift the vertices relative to the centroid
76  amrex::Array<amrex::RealVect,m_max_vertices> vertex_cent;
77  for ( int i(0); i<m_vertices; ++i) { vertex_cent[i] = m_vertex[i] - centroid; }
78 
79  // Find a vertex that isn't inline with v[0] and the centroid so we can
80  // compute a vector normal to the face.
81  for ( int i(1); i<m_vertices; ++i ) {
82  m_zdir = vertex_cent[0].crossProduct(vertex_cent[i]);
83  if ( !amrex::almostEqual(m_zdir.vectorLength(),0.) ) { break; }
84  }
85  AMREX_ALWAYS_ASSERT(!amrex::almostEqual(m_zdir.vectorLength(), 0.));
86  m_zdir /= m_zdir.vectorLength();
87 
88  m_theta[0] = 0.0;
89  for ( int i(1); i<m_vertices; ++i) {
90 
91  m_theta[i] = std::atan2(m_zdir.dotProduct( vertex_cent[0].crossProduct(vertex_cent[i]) ),
92  vertex_cent[0].dotProduct(vertex_cent[i]));
93 
94  m_theta[i] += ((m_theta[i] >= 0.) ? 0.0 : 2.0*PI);
95  }
96 
97  // Sort counter clockwise based on theta.
98  for (int i(0); i<m_vertices; ++i) {
99  for (int j(0); j < m_vertices-i-1; ++j) {
100  if ( m_theta[j] > m_theta[j+1] ) {
101  amrex::Swap(m_theta[j], m_theta[j+1]);
102  amrex::Swap(m_vertex[j], m_vertex[j+1]);
103  amrex::Swap(vertex_cent[j], vertex_cent[j+1]);
104  }
105  } // j-loop
106  } // i-loop
107  m_sorted = 1;
108 
109  // Compute areas of triangles
110  for (int i(0); i<m_vertices; ++i) {
111  int const j( (i+1 == m_vertices) ? 0 : i+1 );
112  amrex::RealVect vi_cross_vj = vertex_cent[i].crossProduct(vertex_cent[j]);
113  m_area += 0.5*vi_cross_vj.vectorLength();
114  }
115  AMREX_ALWAYS_ASSERT( m_area > 0. ); // <------------------------------- TODO remove ALWAYS
116  }
117 
118  [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
119  int ok ( ) const noexcept
120  { return ((m_area > 0. || (m_area == 0. && m_defined == 1)) ? 1 : 0); }
121 
122  [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
123  amrex::Real area ( ) const noexcept {
124  AMREX_ALWAYS_ASSERT( ok() ); // <-------------------------------------- TODO remove ALWAYS
125  return m_area;
126  }
127 
128  // Distance from polygon to a_point
129  [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
130  amrex::Real distance ( amrex::RealVect const& a_point ) const noexcept {
131  AMREX_ALWAYS_ASSERT( m_defined == 1 ); // <--------------------------- TODO remove ALWAYS
132  amrex::RealVect x0 = a_point - m_vertex[0];
133  return amrex::Math::abs(x0.dotProduct(m_zdir));
134  }
135 
136  // Centroid of the polygon based on the sub-triangulation
137  [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
138  amrex::RealVect get_centroid ( ) const noexcept {
139  amrex::RealVect cent(0.);
140  if (m_vertices==3) {
141  cent = (m_vertex[0] + m_vertex[1] + m_vertex[2]) / 3.0;
142  } else {
143  // Loop over sub-triangles
144  amrex::Real area(0.);
145  for ( int i(0); i<m_vertices-2; ++i) {
146  amrex::RealVect const v0 (m_vertex[i+1] - m_vertex[0]);
147  amrex::RealVect const v1 (m_vertex[i+2] - m_vertex[0]);
148  amrex::RealVect v0_cross_v1 = v0.crossProduct(v1);
149  amrex::Real area_tri = 0.5 * v0_cross_v1.vectorLength();
150  amrex::RealVect cent_tri = (m_vertex[0] + m_vertex[i+1] + m_vertex[i+2]) / 3.0;
151  area += area_tri;
152  cent += area_tri * cent_tri;
153  }
154  cent = cent / area;
155  }
156  return cent;
157  }
158 
159  // Unit normal vector
160  [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
161  amrex::RealVect normal () const noexcept {
162  return m_zdir;
163  }
164 
165 #ifndef AMREX_USE_GPU
166  void report ( int const a_id, amrex::RealVect a_v0 )
167 #else
168  void report ( int const /*a_id*/, amrex::RealVect /*a_v0*/ )
169 #endif
170  {
171 
172 #ifndef AMREX_USE_GPU
173  amrex::RealVect centroid = get_centroid();
174  amrex::Print() << "Face " << a_id
175  << " -------------------------------------------\n"
176  << "\nok? " << (ok() ? "yes" : "no") << "\n\n";
177  for (int i(0); i<m_vertices; ++i) {
178  amrex::Print() << "v" << i << ": " << m_vertex[i] << '\n';
179  }
180  amrex::Print() << "\nvc: " << " " << centroid << "\n";
181 
182  amrex::Real const dist = distance( a_v0 );
183  // amrex::Real const vol = ok() ? m_area * dist : 0.;
184 
185  amrex::Print() << "\narea: " << m_area
186  << "\ndistance: " << dist
187  << "\nvolume: " << dist*m_area
188  << "\n==================================================\n\n";
189 #endif
190  }
191 
192  private:
193 
194  static int constexpr m_max_vertices = 6;
195 
196  int const m_cell_face;
197 
198  amrex::RealVect const m_eb_point;
199  amrex::RealVect const m_eb_normal;
200 
202 
204 
205  amrex::Real m_area;
206 
207  int m_sorted;
208 
209  amrex::Array<amrex::RealVect,m_max_vertices> m_vertex;
210 
211  amrex::GpuArray<amrex::Real,m_max_vertices> m_theta;
212 
213  amrex::RealVect m_zdir; // normal to polygon
214 };
215 
216 
217 #endif
constexpr amrex::Real PI
Definition: ERF_Constants.H:6
Definition: ERF_EBPolygon.H:9
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::RealVect get_centroid() const noexcept
Definition: ERF_EBPolygon.H:138
int m_defined
Definition: ERF_EBPolygon.H:201
amrex::Real m_area
Definition: ERF_EBPolygon.H:205
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int ok() const noexcept
Definition: ERF_EBPolygon.H:119
amrex::GpuArray< amrex::Real, m_max_vertices > m_theta
Definition: ERF_EBPolygon.H:211
AMREX_GPU_HOST_DEVICE void add_vertex(amrex::RealVect const &a_v)
Definition: ERF_EBPolygon.H:43
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real distance(amrex::RealVect const &a_point) const noexcept
Definition: ERF_EBPolygon.H:130
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real area() const noexcept
Definition: ERF_EBPolygon.H:123
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void set_area(amrex::Real const &a_area)
Definition: ERF_EBPolygon.H:50
int const m_cell_face
Definition: ERF_EBPolygon.H:196
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::RealVect normal() const noexcept
Definition: ERF_EBPolygon.H:161
int m_sorted
Definition: ERF_EBPolygon.H:207
int m_vertices
Definition: ERF_EBPolygon.H:203
amrex::Array< amrex::RealVect, m_max_vertices > m_vertex
Definition: ERF_EBPolygon.H:209
amrex::RealVect const m_eb_normal
Definition: ERF_EBPolygon.H:199
static constexpr int m_max_vertices
Definition: ERF_EBPolygon.H:194
void report(int const a_id, amrex::RealVect a_v0)
Definition: ERF_EBPolygon.H:166
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void define()
Definition: ERF_EBPolygon.H:53
AMREX_GPU_HOST_DEVICE polygon_()
Definition: ERF_EBPolygon.H:29
AMREX_GPU_HOST_DEVICE polygon_(amrex::RealVect a_point, amrex::RealVect a_normal)
Definition: ERF_EBPolygon.H:14
amrex::RealVect const m_eb_point
Definition: ERF_EBPolygon.H:198
amrex::RealVect m_zdir
Definition: ERF_EBPolygon.H:213