ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_EBPolygon.H
Go to the documentation of this file.
1 /**
2  * \file ERF_EBPolygon.H
3  * \brief Defines polygon geometry utilities for EB cut-cell reconstruction.
4  */
5 #ifndef ERF_EB_POLYGON_H_
6 #define ERF_EB_POLYGON_H_
7 
8 #include <AMReX_REAL.H>
9 #include <AMReX_RealVect.H>
10 
11 #include "ERF_Constants.H"
12 
13 /**
14  * \brief Polygon helper used to accumulate cut-cell face geometry.
15  *
16  * The polygon stores up to six vertices, sorts them in a local plane, and
17  * computes area, centroid, distance, and normal data used by eb_cut_cell_.
18  */
19 class polygon_ {
20 
21  public:
22 
23  /**
24  * \brief Construct a cell-face polygon clipped by an EB plane.
25  * \param a_point Point on the EB plane.
26  * \param a_normal Unit normal for the EB plane.
27  */
28  AMREX_GPU_HOST_DEVICE
29  polygon_ ( amrex::RealVect a_point,
30  amrex::RealVect a_normal )
31  : m_cell_face(1)
32  , m_eb_point(a_point)
33  , m_eb_normal(a_normal)
34  , m_defined(0)
35  , m_num_vertices(0)
36  , m_area(zero)
37  , m_sorted(0)
38  , m_vertices({amrex::RealVect(zero), amrex::RealVect(zero), amrex::RealVect(zero),
39  amrex::RealVect(zero), amrex::RealVect(zero), amrex::RealVect(zero)})
40  , m_zdir(zero)
41  {}
42 
43  //! Construct an empty EB-boundary polygon.
44  AMREX_GPU_HOST_DEVICE
46  : m_cell_face(0)
47  , m_eb_point(zero)
48  , m_eb_normal(zero)
49  , m_defined(0)
50  , m_num_vertices(0)
51  , m_area(zero)
52  , m_sorted(0)
53  , m_vertices({amrex::RealVect(zero), amrex::RealVect(zero), amrex::RealVect(zero),
54  amrex::RealVect(zero), amrex::RealVect(zero), amrex::RealVect(zero)})
55  , m_zdir(zero)
56  {}
57 
58  /**
59  * \brief Add a unique vertex to the polygon.
60  * \param a_v Vertex coordinates.
61  */
62  AMREX_GPU_HOST_DEVICE
63  void add_vertex ( amrex::RealVect const& a_v ) {
64  for ( int i(0); i<m_num_vertices; ++i) {
65  if ( amrex::almostEqual(m_vertices[i][0],a_v[0]) &&
66  amrex::almostEqual(m_vertices[i][1],a_v[1]) &&
67  amrex::almostEqual(m_vertices[i][2],a_v[2]) ) {
68  return;
69  }
70  }
71  AMREX_ASSERT( m_num_vertices < m_max_vertices );
74  }
75 
76  //! Return the number of stored vertices.
77  AMREX_GPU_HOST_DEVICE
78  int get_num_vertices ( ) {
79  return m_num_vertices;
80  }
81 
82  /**
83  * \brief Set the polygon area directly.
84  * \param a_area Area value assigned to the polygon.
85  */
86  AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
87  void set_area ( amrex::Real const& a_area ) { m_area = a_area; }
88 
89  /**
90  * \brief Finalize polygon ordering and compute its area.
91  *
92  * Vertices are sorted counter-clockwise in the polygon plane before area is
93  * computed by a triangle fan.
94  */
95  AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
96  void define () {
97 
98  AMREX_ALWAYS_ASSERT( m_defined == 0 ); // TODO ---------------------------- remove ALWAYS
99 
100  m_defined = 1;
101 
102  // We need at least 3 vertices.
103  if (m_num_vertices < 3) { return; }
104 
105  // Check to see if the vertices of the face are inside or outside of the plane.
106  // If they are outside, this face doesn't belong to the volume.
107  if ( m_cell_face ) {
108  for ( int i(0); i<m_num_vertices; ++i) {
109  if ((m_eb_normal.dotProduct(m_vertices[i] - m_eb_point)) >= zero) {
110  } else {
111  }
112  }
113  }
114 
115  // Calculate the centroid of the polygon.
116  amrex::RealVect centroid = get_centroid();
117 
118  // Shift the vertices relative to the centroid
119  amrex::Array<amrex::RealVect,m_max_vertices> vertex_cent;
120  for ( int i(0); i<m_num_vertices; ++i) {
121  vertex_cent[i] = m_vertices[i] - centroid;
122  }
123 
124  // Compute the normal vector m_zdir by cross product of two vectors in vertex_cent.
125  // Choose the vectors with the largest cross-product magnitude.
126  amrex::RealVect v_normal;
127  amrex::Real max_norm2 = -one;
128  const amrex::RealVect& vertex_cent_0 = vertex_cent[0];
129 
130  for (int i = 1; i < m_num_vertices; ++i)
131  {
132  amrex::RealVect vi = vertex_cent[i];
133  amrex::RealVect v_cross = vertex_cent_0.crossProduct(vi);
134  amrex::Real n2 = v_cross.radSquared();
135  if (n2 > max_norm2)
136  {
137  max_norm2 = n2;
138  v_normal = v_cross;
139  }
140  }
141  if (!amrex::almostEqual(max_norm2, zero))
142  {
143  v_normal /= std::sqrt(max_norm2);
144  }
145  m_zdir = v_normal;
146 
147  //
148 
149  m_theta[0] = zero;
150  for ( int i(1); i<m_num_vertices; ++i) {
151 
152  m_theta[i] = std::atan2(m_zdir.dotProduct( vertex_cent[0].crossProduct(vertex_cent[i]) ),
153  vertex_cent[0].dotProduct(vertex_cent[i]));
154 
155  m_theta[i] += ((m_theta[i] >= zero) ? zero : two*PI);
156  }
157 
158  // Sort counter clockwise based on theta.
159  for (int i(0); i<m_num_vertices; ++i) {
160  for (int j(0); j < m_num_vertices-i-1; ++j) {
161  if ( m_theta[j] > m_theta[j+1] ) {
162  amrex::Swap(m_theta[j], m_theta[j+1]);
163  amrex::Swap(m_vertices[j], m_vertices[j+1]);
164  amrex::Swap(vertex_cent[j], vertex_cent[j+1]);
165  }
166  } // j-loop
167  } // i-loop
168  m_sorted = 1;
169 
170  // Compute areas of triangles
171 
172  for (int i(0); i<m_num_vertices; ++i) {
173  int const j( (i+1 == m_num_vertices) ? 0 : i+1 );
174  amrex::RealVect vi_cross_vj = vertex_cent[i].crossProduct(vertex_cent[j]);
175  m_area += myhalf*vi_cross_vj.vectorLength();
176  }
177  AMREX_ALWAYS_ASSERT( m_area > zero ); // <------------------------------- TODO remove ALWAYS
178  } // void define
179 
180  //! Return whether the polygon has been defined with a valid area state.
181  [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
182  int ok ( ) const noexcept
183  { return ((m_area > zero || (m_area == zero && m_defined == 1)) ? 1 : 0); }
184 
185  //! Return the polygon area.
186  [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
187  amrex::Real area ( ) const noexcept {
188  AMREX_ALWAYS_ASSERT( ok() ); // <-------------------------------------- TODO remove ALWAYS
189  return m_area;
190  }
191 
192  /**
193  * \brief Return the perpendicular distance from this polygon to a point.
194  * \param a_point Point used in the distance calculation.
195  */
196  [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
197  amrex::Real distance ( amrex::RealVect const& a_point ) const noexcept {
198  AMREX_ALWAYS_ASSERT( m_defined == 1 ); // <--------------------------- TODO remove ALWAYS
199  amrex::RealVect x0 = a_point - m_vertices[0];
200  return amrex::Math::abs(x0.dotProduct(m_zdir));
201  }
202 
203  //! Return the polygon centroid from its sub-triangulation.
204  [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
205  amrex::RealVect get_centroid ( ) const noexcept {
206  amrex::RealVect cent(zero);
207  if (m_num_vertices==3) {
208  cent = (m_vertices[0] + m_vertices[1] + m_vertices[2]) / three;
209  } else {
210  // Loop over sub-triangles
212  for ( int i(0); i<m_num_vertices-2; ++i) {
213  amrex::RealVect const v0 (m_vertices[i+1] - m_vertices[0]);
214  amrex::RealVect const v1 (m_vertices[i+2] - m_vertices[0]);
215  amrex::RealVect v0_cross_v1 = v0.crossProduct(v1);
216  amrex::Real area_tri = myhalf * v0_cross_v1.vectorLength();
217  amrex::RealVect cent_tri = (m_vertices[0] + m_vertices[i+1] + m_vertices[i+2]) / three;
218  area += area_tri;
219  cent += area_tri * cent_tri;
220  }
221  cent = cent / area;
222  }
223  return cent;
224  }
225 
226  //! Return the unit normal vector of the polygon plane.
227  [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
228  amrex::RealVect normal () const noexcept {
229  return m_zdir;
230  }
231 
232  /**
233  * \brief Print a diagnostic report for the polygon on CPU builds.
234  * \param a_id Face identifier used in the diagnostic output.
235  * \param a_v0 Reference point used to compute the reported distance.
236  */
237 #ifndef AMREX_USE_GPU
238  void report ( int const a_id, amrex::RealVect a_v0 )
239 #else
240  void report ( int const /*a_id*/, amrex::RealVect /*a_v0*/ )
241 #endif
242  {
243 
244 #ifndef AMREX_USE_GPU
245  amrex::RealVect centroid = get_centroid();
246  amrex::Print() << "Face " << a_id
247  << " -------------------------------------------\n"
248  << "\nok? " << (ok() ? "yes" : "no") << "\n\n";
249  for (int i(0); i<m_num_vertices; ++i) {
250  amrex::Print() << "v" << i << ": " << m_vertices[i] << '\n';
251  }
252  amrex::Print() << "\nvc: " << " " << centroid << "\n";
253 
254  amrex::Real const dist = distance( a_v0 );
255  // amrex::Real const vol = ok() ? m_area * dist : zero;
256 
257  amrex::Print() << "\narea: " << m_area
258  << "\ndistance: " << dist
259  << "\nvolume: " << dist*m_area
260  << "\n==================================================\n\n";
261 #endif
262  }
263 
264  /**
265  * \brief Print polygon vertices with a face identifier.
266  * \param a_id Face identifier used in the diagnostic output.
267  */
268  void debug( int const a_id ) {
269  amrex::Print() << "EBPolygon: id = " << a_id << ", m_num_vertices = " << m_num_vertices << '\n';
270  for (int i(0); i<m_num_vertices; ++i) {
271  amrex::Print() << "EBPolygon: v" << i << ": " << m_vertices[i] << '\n';
272  }
273  }
274  //! Print polygon vertices without a face identifier.
275  void debug() {
276  debug(-1);
277  }
278 
279  private:
280 
281  static int constexpr m_max_vertices = 6;
282 
283  int const m_cell_face;
284 
285  amrex::RealVect const m_eb_point;
286  amrex::RealVect const m_eb_normal;
287 
289 
291 
293 
294  int m_sorted;
295 
296  amrex::Array<amrex::RealVect,m_max_vertices> m_vertices;
297 
298  amrex::GpuArray<amrex::Real,m_max_vertices> m_theta;
299 
300  amrex::RealVect m_zdir; // normal to polygon
301 };
302 
303 
304 #endif
constexpr amrex::Real three
Definition: ERF_Constants.H:11
constexpr amrex::Real two
Definition: ERF_Constants.H:10
constexpr amrex::Real one
Definition: ERF_Constants.H:9
constexpr amrex::Real zero
Definition: ERF_Constants.H:8
constexpr amrex::Real myhalf
Definition: ERF_Constants.H:13
constexpr amrex::Real PI
Definition: ERF_Constants.H:42
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
amrex::Real Real
Definition: ERF_ShocInterface.H:19
Polygon helper used to accumulate cut-cell face geometry.
Definition: ERF_EBPolygon.H:19
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::RealVect get_centroid() const noexcept
Return the polygon centroid from its sub-triangulation.
Definition: ERF_EBPolygon.H:205
int m_defined
Definition: ERF_EBPolygon.H:288
AMREX_GPU_HOST_DEVICE int get_num_vertices()
Return the number of stored vertices.
Definition: ERF_EBPolygon.H:78
amrex::Real m_area
Definition: ERF_EBPolygon.H:292
amrex::Array< amrex::RealVect, m_max_vertices > m_vertices
Definition: ERF_EBPolygon.H:296
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int ok() const noexcept
Return whether the polygon has been defined with a valid area state.
Definition: ERF_EBPolygon.H:182
amrex::GpuArray< amrex::Real, m_max_vertices > m_theta
Definition: ERF_EBPolygon.H:298
AMREX_GPU_HOST_DEVICE void add_vertex(amrex::RealVect const &a_v)
Add a unique vertex to the polygon.
Definition: ERF_EBPolygon.H:63
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real distance(amrex::RealVect const &a_point) const noexcept
Return the perpendicular distance from this polygon to a point.
Definition: ERF_EBPolygon.H:197
void debug(int const a_id)
Print polygon vertices with a face identifier.
Definition: ERF_EBPolygon.H:268
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real area() const noexcept
Return the polygon area.
Definition: ERF_EBPolygon.H:187
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void set_area(amrex::Real const &a_area)
Set the polygon area directly.
Definition: ERF_EBPolygon.H:87
void debug()
Print polygon vertices without a face identifier.
Definition: ERF_EBPolygon.H:275
int const m_cell_face
Definition: ERF_EBPolygon.H:283
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::RealVect normal() const noexcept
Return the unit normal vector of the polygon plane.
Definition: ERF_EBPolygon.H:228
int m_sorted
Definition: ERF_EBPolygon.H:294
amrex::RealVect const m_eb_normal
Definition: ERF_EBPolygon.H:286
static constexpr int m_max_vertices
Definition: ERF_EBPolygon.H:281
void report(int const a_id, amrex::RealVect a_v0)
Print a diagnostic report for the polygon on CPU builds.
Definition: ERF_EBPolygon.H:238
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void define()
Finalize polygon ordering and compute its area.
Definition: ERF_EBPolygon.H:96
int m_num_vertices
Definition: ERF_EBPolygon.H:290
AMREX_GPU_HOST_DEVICE polygon_()
Construct an empty EB-boundary polygon.
Definition: ERF_EBPolygon.H:45
AMREX_GPU_HOST_DEVICE polygon_(amrex::RealVect a_point, amrex::RealVect a_normal)
Construct a cell-face polygon clipped by an EB plane.
Definition: ERF_EBPolygon.H:29
amrex::RealVect const m_eb_point
Definition: ERF_EBPolygon.H:285
amrex::RealVect m_zdir
Definition: ERF_EBPolygon.H:300
real(c_double), private vi
Definition: ERF_module_mp_morr_two_moment.F90:219