ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
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_num_vertices(0)
21  , m_area(0.)
22  , m_sorted(0)
23  , m_vertices({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_num_vertices(0)
35  , m_area(0.)
36  , m_sorted(0)
37  , m_vertices({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  for ( int i(0); i<m_num_vertices; ++i) {
45  if ( amrex::almostEqual(m_vertices[i][0],a_v[0]) &&
46  amrex::almostEqual(m_vertices[i][1],a_v[1]) &&
47  amrex::almostEqual(m_vertices[i][2],a_v[2]) ) {
48  return;
49  }
50  }
51  AMREX_ASSERT( m_num_vertices < m_max_vertices );
54  }
55 
56  AMREX_GPU_HOST_DEVICE
57  int get_num_vertices ( ) {
58  return m_num_vertices;
59  }
60 
61  AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
62  void set_area ( amrex::Real const& a_area ) { m_area = a_area; }
63 
64  AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
65  void define () {
66 
67  AMREX_ALWAYS_ASSERT( m_defined == 0 ); // TODO ---------------------------- remove ALWAYS
68 
69  m_defined = 1;
70 
71  // We need at least 3 vertices.
72  if (m_num_vertices < 3) { return; }
73 
74  // Check to see if the vertices of the face are inside or outside of the plane.
75  // If they are outside, this face doesn't belong to the volume.
76  if ( m_cell_face ) {
77  for ( int i(0); i<m_num_vertices; ++i) {
78  if ((m_eb_normal.dotProduct(m_vertices[i] - m_eb_point)) >= 0.) {
79  } else {
80  }
81  }
82  }
83 
84  // Calculate the centroid of the polygon.
85  amrex::RealVect centroid = get_centroid();
86 
87  // Shift the vertices relative to the centroid
88  amrex::Array<amrex::RealVect,m_max_vertices> vertex_cent;
89  for ( int i(0); i<m_num_vertices; ++i) {
90  vertex_cent[i] = m_vertices[i] - centroid;
91  }
92 
93  // Compute the normal vector m_zdir by cross product of two vectors in vertex_cent.
94  // Choose the vectors with the largest cross-product magnitude.
95  amrex::RealVect v_normal;
96  amrex::Real max_norm2 = -1.0;
97  const amrex::RealVect& vertex_cent_0 = vertex_cent[0];
98 
99  for (int i = 1; i < m_num_vertices; ++i)
100  {
101  amrex::RealVect vi = vertex_cent[i];
102  amrex::RealVect v_cross = vertex_cent_0.crossProduct(vi);
103  amrex::Real n2 = v_cross.radSquared();
104  if (n2 > max_norm2)
105  {
106  max_norm2 = n2;
107  v_normal = v_cross;
108  }
109  }
110  if (!amrex::almostEqual(max_norm2, 0.0))
111  {
112  v_normal /= std::sqrt(max_norm2);
113  }
114  m_zdir = v_normal;
115 
116  //
117 
118  m_theta[0] = 0.0;
119  for ( int i(1); i<m_num_vertices; ++i) {
120 
121  m_theta[i] = std::atan2(m_zdir.dotProduct( vertex_cent[0].crossProduct(vertex_cent[i]) ),
122  vertex_cent[0].dotProduct(vertex_cent[i]));
123 
124  m_theta[i] += ((m_theta[i] >= 0.) ? 0.0 : 2.0*PI);
125  }
126 
127  // Sort counter clockwise based on theta.
128  for (int i(0); i<m_num_vertices; ++i) {
129  for (int j(0); j < m_num_vertices-i-1; ++j) {
130  if ( m_theta[j] > m_theta[j+1] ) {
131  amrex::Swap(m_theta[j], m_theta[j+1]);
132  amrex::Swap(m_vertices[j], m_vertices[j+1]);
133  amrex::Swap(vertex_cent[j], vertex_cent[j+1]);
134  }
135  } // j-loop
136  } // i-loop
137  m_sorted = 1;
138 
139  // Compute areas of triangles
140 
141  for (int i(0); i<m_num_vertices; ++i) {
142  int const j( (i+1 == m_num_vertices) ? 0 : i+1 );
143  amrex::RealVect vi_cross_vj = vertex_cent[i].crossProduct(vertex_cent[j]);
144  m_area += 0.5*vi_cross_vj.vectorLength();
145  }
146  AMREX_ALWAYS_ASSERT( m_area > 0. ); // <------------------------------- TODO remove ALWAYS
147  } // void define
148 
149  [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
150  int ok ( ) const noexcept
151  { return ((m_area > 0. || (m_area == 0. && m_defined == 1)) ? 1 : 0); }
152 
153  [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
154  amrex::Real area ( ) const noexcept {
155  AMREX_ALWAYS_ASSERT( ok() ); // <-------------------------------------- TODO remove ALWAYS
156  return m_area;
157  }
158 
159  // Distance from polygon to a_point
160  [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
161  amrex::Real distance ( amrex::RealVect const& a_point ) const noexcept {
162  AMREX_ALWAYS_ASSERT( m_defined == 1 ); // <--------------------------- TODO remove ALWAYS
163  amrex::RealVect x0 = a_point - m_vertices[0];
164  return amrex::Math::abs(x0.dotProduct(m_zdir));
165  }
166 
167  // Centroid of the polygon based on the sub-triangulation
168  [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
169  amrex::RealVect get_centroid ( ) const noexcept {
170  amrex::RealVect cent(0.);
171  if (m_num_vertices==3) {
172  cent = (m_vertices[0] + m_vertices[1] + m_vertices[2]) / 3.0;
173  } else {
174  // Loop over sub-triangles
175  amrex::Real area(0.);
176  for ( int i(0); i<m_num_vertices-2; ++i) {
177  amrex::RealVect const v0 (m_vertices[i+1] - m_vertices[0]);
178  amrex::RealVect const v1 (m_vertices[i+2] - m_vertices[0]);
179  amrex::RealVect v0_cross_v1 = v0.crossProduct(v1);
180  amrex::Real area_tri = 0.5 * v0_cross_v1.vectorLength();
181  amrex::RealVect cent_tri = (m_vertices[0] + m_vertices[i+1] + m_vertices[i+2]) / 3.0;
182  area += area_tri;
183  cent += area_tri * cent_tri;
184  }
185  cent = cent / area;
186  }
187  return cent;
188  }
189 
190  // Unit normal vector
191  [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
192  amrex::RealVect normal () const noexcept {
193  return m_zdir;
194  }
195 
196 #ifndef AMREX_USE_GPU
197  void report ( int const a_id, amrex::RealVect a_v0 )
198 #else
199  void report ( int const /*a_id*/, amrex::RealVect /*a_v0*/ )
200 #endif
201  {
202 
203 #ifndef AMREX_USE_GPU
204  amrex::RealVect centroid = get_centroid();
205  amrex::Print() << "Face " << a_id
206  << " -------------------------------------------\n"
207  << "\nok? " << (ok() ? "yes" : "no") << "\n\n";
208  for (int i(0); i<m_num_vertices; ++i) {
209  amrex::Print() << "v" << i << ": " << m_vertices[i] << '\n';
210  }
211  amrex::Print() << "\nvc: " << " " << centroid << "\n";
212 
213  amrex::Real const dist = distance( a_v0 );
214  // amrex::Real const vol = ok() ? m_area * dist : 0.;
215 
216  amrex::Print() << "\narea: " << m_area
217  << "\ndistance: " << dist
218  << "\nvolume: " << dist*m_area
219  << "\n==================================================\n\n";
220 #endif
221  }
222 
223  void debug( int const a_id ) {
224  amrex::Print() << "EBPolygon: id = " << a_id << ", m_num_vertices = " << m_num_vertices << '\n';
225  for (int i(0); i<m_num_vertices; ++i) {
226  amrex::Print() << "EBPolygon: v" << i << ": " << m_vertices[i] << '\n';
227  }
228  }
229  void debug() {
230  debug(-1);
231  }
232 
233  private:
234 
235  static int constexpr m_max_vertices = 6;
236 
237  int const m_cell_face;
238 
239  amrex::RealVect const m_eb_point;
240  amrex::RealVect const m_eb_normal;
241 
243 
245 
247 
248  int m_sorted;
249 
250  amrex::Array<amrex::RealVect,m_max_vertices> m_vertices;
251 
252  amrex::GpuArray<amrex::Real,m_max_vertices> m_theta;
253 
254  amrex::RealVect m_zdir; // normal to polygon
255 };
256 
257 
258 #endif
constexpr amrex::Real PI
Definition: ERF_Constants.H:6
amrex::Real Real
Definition: ERF_ShocInterface.H:19
Definition: ERF_EBPolygon.H:9
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::RealVect get_centroid() const noexcept
Definition: ERF_EBPolygon.H:169
int m_defined
Definition: ERF_EBPolygon.H:242
AMREX_GPU_HOST_DEVICE int get_num_vertices()
Definition: ERF_EBPolygon.H:57
amrex::Real m_area
Definition: ERF_EBPolygon.H:246
amrex::Array< amrex::RealVect, m_max_vertices > m_vertices
Definition: ERF_EBPolygon.H:250
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int ok() const noexcept
Definition: ERF_EBPolygon.H:150
amrex::GpuArray< amrex::Real, m_max_vertices > m_theta
Definition: ERF_EBPolygon.H:252
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:161
void debug(int const a_id)
Definition: ERF_EBPolygon.H:223
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real area() const noexcept
Definition: ERF_EBPolygon.H:154
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void set_area(amrex::Real const &a_area)
Definition: ERF_EBPolygon.H:62
void debug()
Definition: ERF_EBPolygon.H:229
int const m_cell_face
Definition: ERF_EBPolygon.H:237
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::RealVect normal() const noexcept
Definition: ERF_EBPolygon.H:192
int m_sorted
Definition: ERF_EBPolygon.H:248
amrex::RealVect const m_eb_normal
Definition: ERF_EBPolygon.H:240
static constexpr int m_max_vertices
Definition: ERF_EBPolygon.H:235
void report(int const a_id, amrex::RealVect a_v0)
Definition: ERF_EBPolygon.H:197
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void define()
Definition: ERF_EBPolygon.H:65
int m_num_vertices
Definition: ERF_EBPolygon.H:244
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:239
amrex::RealVect m_zdir
Definition: ERF_EBPolygon.H:254
real(c_double), private vi
Definition: ERF_module_mp_morr_two_moment.F90:219