ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
polygon_ Class Reference

Polygon helper used to accumulate cut-cell face geometry. More...

#include <ERF_EBPolygon.H>

Collaboration diagram for polygon_:

Public Member Functions

AMREX_GPU_HOST_DEVICE polygon_ (amrex::RealVect a_point, amrex::RealVect a_normal)
 Construct a cell-face polygon clipped by an EB plane. More...
 
AMREX_GPU_HOST_DEVICE polygon_ ()
 Construct an empty EB-boundary polygon. More...
 
AMREX_GPU_HOST_DEVICE void add_vertex (amrex::RealVect const &a_v)
 Add a unique vertex to the polygon. More...
 
AMREX_GPU_HOST_DEVICE int get_num_vertices ()
 Return the number of stored vertices. More...
 
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void set_area (amrex::Real const &a_area)
 Set the polygon area directly. More...
 
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void define ()
 Finalize polygon ordering and compute its area. More...
 
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int ok () const noexcept
 Return whether the polygon has been defined with a valid area state. More...
 
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real area () const noexcept
 Return the polygon area. More...
 
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. More...
 
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::RealVect get_centroid () const noexcept
 Return the polygon centroid from its sub-triangulation. More...
 
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::RealVect normal () const noexcept
 Return the unit normal vector of the polygon plane. More...
 
void report (int const a_id, amrex::RealVect a_v0)
 Print a diagnostic report for the polygon on CPU builds. More...
 
void debug (int const a_id)
 Print polygon vertices with a face identifier. More...
 
void debug ()
 Print polygon vertices without a face identifier. More...
 

Private Attributes

int const m_cell_face
 
amrex::RealVect const m_eb_point
 
amrex::RealVect const m_eb_normal
 
int m_defined
 
int m_num_vertices
 
amrex::Real m_area
 
int m_sorted
 
amrex::Array< amrex::RealVect, m_max_verticesm_vertices
 
amrex::GpuArray< amrex::Real, m_max_verticesm_theta
 
amrex::RealVect m_zdir
 

Static Private Attributes

static constexpr int m_max_vertices = 6
 

Detailed Description

Polygon helper used to accumulate cut-cell face geometry.

The polygon stores up to six vertices, sorts them in a local plane, and computes area, centroid, distance, and normal data used by eb_cut_cell_.

Constructor & Destructor Documentation

◆ polygon_() [1/2]

AMREX_GPU_HOST_DEVICE polygon_::polygon_ ( amrex::RealVect  a_point,
amrex::RealVect  a_normal 
)
inline

Construct a cell-face polygon clipped by an EB plane.

Parameters
a_pointPoint on the EB plane.
a_normalUnit normal for the EB plane.
32  : m_cell_face(1)
33  , m_eb_point(a_point)
34  , m_eb_normal(a_normal)
35  , m_defined(0)
36  , m_num_vertices(0)
37  , m_area(zero)
38  , m_sorted(0)
39  , m_vertices({amrex::RealVect(zero), amrex::RealVect(zero), amrex::RealVect(zero),
40  amrex::RealVect(zero), amrex::RealVect(zero), amrex::RealVect(zero)})
41  , m_zdir(zero)
42  {}
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:29
int m_defined
Definition: ERF_EBPolygon.H:289
amrex::Real m_area
Definition: ERF_EBPolygon.H:293
amrex::Array< amrex::RealVect, m_max_vertices > m_vertices
Definition: ERF_EBPolygon.H:297
int const m_cell_face
Definition: ERF_EBPolygon.H:284
int m_sorted
Definition: ERF_EBPolygon.H:295
amrex::RealVect const m_eb_normal
Definition: ERF_EBPolygon.H:287
int m_num_vertices
Definition: ERF_EBPolygon.H:291
amrex::RealVect const m_eb_point
Definition: ERF_EBPolygon.H:286
amrex::RealVect m_zdir
Definition: ERF_EBPolygon.H:301

◆ polygon_() [2/2]

AMREX_GPU_HOST_DEVICE polygon_::polygon_ ( )
inline

Construct an empty EB-boundary polygon.

47  : m_cell_face(0)
48  , m_eb_point(zero)
49  , m_eb_normal(zero)
50  , m_defined(0)
51  , m_num_vertices(0)
52  , m_area(zero)
53  , m_sorted(0)
54  , m_vertices({amrex::RealVect(zero), amrex::RealVect(zero), amrex::RealVect(zero),
55  amrex::RealVect(zero), amrex::RealVect(zero), amrex::RealVect(zero)})
56  , m_zdir(zero)
57  {}

Member Function Documentation

◆ add_vertex()

AMREX_GPU_HOST_DEVICE void polygon_::add_vertex ( amrex::RealVect const &  a_v)
inline

Add a unique vertex to the polygon.

Parameters
a_vVertex coordinates.
64  {
65  for ( int i(0); i<m_num_vertices; ++i) {
66  if ( amrex::almostEqual(m_vertices[i][0],a_v[0]) &&
67  amrex::almostEqual(m_vertices[i][1],a_v[1]) &&
68  amrex::almostEqual(m_vertices[i][2],a_v[2]) ) {
69  return;
70  }
71  }
72  AMREX_ASSERT( m_num_vertices < m_max_vertices );
75  }
static constexpr int m_max_vertices
Definition: ERF_EBPolygon.H:282

Referenced by eb_cut_cell_::calc_edge_intersections(), and eb_cut_cell_::set_covered_regular_cell_vertices().

Here is the caller graph for this function:

◆ area()

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real polygon_::area ( ) const
inlinenoexcept

Return the polygon area.

188  {
189  AMREX_ALWAYS_ASSERT( ok() ); // <-------------------------------------- TODO remove ALWAYS
190  return m_area;
191  }
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
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:183

Referenced by eb_cut_cell_::areaBoun(), eb_cut_cell_::centVol(), get_centroid(), and eb_cut_cell_::volume().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ debug() [1/2]

void polygon_::debug ( )
inline

Print polygon vertices without a face identifier.

276  {
277  debug(-1);
278  }
void debug()
Print polygon vertices without a face identifier.
Definition: ERF_EBPolygon.H:276

◆ debug() [2/2]

void polygon_::debug ( int const  a_id)
inline

Print polygon vertices with a face identifier.

Parameters
a_idFace identifier used in the diagnostic output.
269  {
270  amrex::Print() << "EBPolygon: id = " << a_id << ", m_num_vertices = " << m_num_vertices << '\n';
271  for (int i(0); i<m_num_vertices; ++i) {
272  amrex::Print() << "EBPolygon: v" << i << ": " << m_vertices[i] << '\n';
273  }
274  }

◆ define()

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void polygon_::define ( )
inline

Finalize polygon ordering and compute its area.

Vertices are sorted counter-clockwise in the polygon plane before area is computed by a triangle fan.

97  {
98 
99  AMREX_ALWAYS_ASSERT( m_defined == 0 ); // TODO ---------------------------- remove ALWAYS
100 
101  m_defined = 1;
102 
103  // We need at least 3 vertices.
104  if (m_num_vertices < 3) { return; }
105 
106  // Check to see if the vertices of the face are inside or outside of the plane.
107  // If they are outside, this face doesn't belong to the volume.
108  if ( m_cell_face ) {
109  for ( int i(0); i<m_num_vertices; ++i) {
110  if ((m_eb_normal.dotProduct(m_vertices[i] - m_eb_point)) >= zero) {
111  } else {
112  }
113  }
114  }
115 
116  // Calculate the centroid of the polygon.
117  amrex::RealVect centroid = get_centroid();
118 
119  // Shift the vertices relative to the centroid
120  amrex::Array<amrex::RealVect,m_max_vertices> vertex_cent;
121  for ( int i(0); i<m_num_vertices; ++i) {
122  vertex_cent[i] = m_vertices[i] - centroid;
123  }
124 
125  // Compute the normal vector m_zdir by cross product of two vectors in vertex_cent.
126  // Choose the vectors with the largest cross-product magnitude.
127  amrex::RealVect v_normal;
128  amrex::Real max_norm2 = -one;
129  const amrex::RealVect& vertex_cent_0 = vertex_cent[0];
130 
131  for (int i = 1; i < m_num_vertices; ++i)
132  {
133  amrex::RealVect vi = vertex_cent[i];
134  amrex::RealVect v_cross = vertex_cent_0.crossProduct(vi);
135  amrex::Real n2 = v_cross.radSquared();
136  if (n2 > max_norm2)
137  {
138  max_norm2 = n2;
139  v_normal = v_cross;
140  }
141  }
142  if (!amrex::almostEqual(max_norm2, zero))
143  {
144  v_normal /= std::sqrt(max_norm2);
145  }
146  m_zdir = v_normal;
147 
148  //
149 
150  m_theta[0] = zero;
151  for ( int i(1); i<m_num_vertices; ++i) {
152 
153  m_theta[i] = std::atan2(m_zdir.dotProduct( vertex_cent[0].crossProduct(vertex_cent[i]) ),
154  vertex_cent[0].dotProduct(vertex_cent[i]));
155 
156  m_theta[i] += ((m_theta[i] >= zero) ? zero : two*PI);
157  }
158 
159  // Sort counter clockwise based on theta.
160  for (int i(0); i<m_num_vertices; ++i) {
161  for (int j(0); j < m_num_vertices-i-1; ++j) {
162  if ( m_theta[j] > m_theta[j+1] ) {
163  amrex::Swap(m_theta[j], m_theta[j+1]);
164  amrex::Swap(m_vertices[j], m_vertices[j+1]);
165  amrex::Swap(vertex_cent[j], vertex_cent[j+1]);
166  }
167  } // j-loop
168  } // i-loop
169  m_sorted = 1;
170 
171  // Compute areas of triangles
172 
173  for (int i(0); i<m_num_vertices; ++i) {
174  int const j( (i+1 == m_num_vertices) ? 0 : i+1 );
175  amrex::RealVect vi_cross_vj = vertex_cent[i].crossProduct(vertex_cent[j]);
176  m_area += myhalf*vi_cross_vj.vectorLength();
177  }
178  AMREX_ALWAYS_ASSERT( m_area > zero ); // <------------------------------- TODO remove ALWAYS
179  } // void define
constexpr amrex::Real two
Definition: ERF_NumericalConstants.H:31
constexpr amrex::Real one
Definition: ERF_NumericalConstants.H:30
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:34
constexpr amrex::Real PI
Definition: ERF_NumericalConstants.H:39
amrex::Real Real
Definition: ERF_ShocInterface.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:206
amrex::GpuArray< amrex::Real, m_max_vertices > m_theta
Definition: ERF_EBPolygon.H:299
real(c_double), private vi
Definition: ERF_module_mp_morr_two_moment.F90:219
Here is the call graph for this function:

◆ distance()

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real polygon_::distance ( amrex::RealVect const &  a_point) const
inlinenoexcept

Return the perpendicular distance from this polygon to a point.

Parameters
a_pointPoint used in the distance calculation.
198  {
199  AMREX_ALWAYS_ASSERT( m_defined == 1 ); // <--------------------------- TODO remove ALWAYS
200  amrex::RealVect x0 = a_point - m_vertices[0];
201  return amrex::Math::abs(x0.dotProduct(m_zdir));
202  }

Referenced by report(), and eb_cut_cell_::volume().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_centroid()

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::RealVect polygon_::get_centroid ( ) const
inlinenoexcept

Return the polygon centroid from its sub-triangulation.

206  {
207  amrex::RealVect cent(zero);
208  if (m_num_vertices==3) {
209  cent = (m_vertices[0] + m_vertices[1] + m_vertices[2]) / three;
210  } else {
211  // Loop over sub-triangles
213  for ( int i(0); i<m_num_vertices-2; ++i) {
214  amrex::RealVect const v0 (m_vertices[i+1] - m_vertices[0]);
215  amrex::RealVect const v1 (m_vertices[i+2] - m_vertices[0]);
216  amrex::RealVect v0_cross_v1 = v0.crossProduct(v1);
217  amrex::Real area_tri = myhalf * v0_cross_v1.vectorLength();
218  amrex::RealVect cent_tri = (m_vertices[0] + m_vertices[i+1] + m_vertices[i+2]) / three;
219  area += area_tri;
220  cent += area_tri * cent_tri;
221  }
222  cent = cent / area;
223  }
224  return cent;
225  }
constexpr amrex::Real three
Definition: ERF_NumericalConstants.H:32
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real area() const noexcept
Return the polygon area.
Definition: ERF_EBPolygon.H:188

Referenced by eb_cut_cell_::centBoun(), eb_cut_cell_::centVol(), define(), and report().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_num_vertices()

AMREX_GPU_HOST_DEVICE int polygon_::get_num_vertices ( )
inline

Return the number of stored vertices.

79  {
80  return m_num_vertices;
81  }

◆ normal()

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::RealVect polygon_::normal ( ) const
inlinenoexcept

Return the unit normal vector of the polygon plane.

229  {
230  return m_zdir;
231  }

◆ ok()

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int polygon_::ok ( ) const
inlinenoexcept

Return whether the polygon has been defined with a valid area state.

184  { return ((m_area > zero || (m_area == zero && m_defined == 1)) ? 1 : 0); }

Referenced by area(), report(), and eb_cut_cell_::volume().

Here is the caller graph for this function:

◆ report()

void polygon_::report ( int const  a_id,
amrex::RealVect  a_v0 
)
inline

Print a diagnostic report for the polygon on CPU builds.

Parameters
a_idFace identifier used in the diagnostic output.
a_v0Reference point used to compute the reported distance.
243  {
244 
245 #ifndef AMREX_USE_GPU
246  amrex::RealVect centroid = get_centroid();
247  amrex::Print() << "Face " << a_id
248  << " -------------------------------------------\n"
249  << "\nok? " << (ok() ? "yes" : "no") << "\n\n";
250  for (int i(0); i<m_num_vertices; ++i) {
251  amrex::Print() << "v" << i << ": " << m_vertices[i] << '\n';
252  }
253  amrex::Print() << "\nvc: " << " " << centroid << "\n";
254 
255  amrex::Real const dist = distance( a_v0 );
256  // amrex::Real const vol = ok() ? m_area * dist : zero;
257 
258  amrex::Print() << "\narea: " << m_area
259  << "\ndistance: " << dist
260  << "\nvolume: " << dist*m_area
261  << "\n==================================================\n\n";
262 #endif
263  }
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:198
Here is the call graph for this function:

◆ set_area()

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void polygon_::set_area ( amrex::Real const &  a_area)
inline

Set the polygon area directly.

Parameters
a_areaArea value assigned to the polygon.
88 { m_area = a_area; }

Referenced by eb_cut_cell_::set_regular().

Here is the caller graph for this function:

Member Data Documentation

◆ m_area

amrex::Real polygon_::m_area
private

Referenced by area(), define(), ok(), report(), and set_area().

◆ m_cell_face

int const polygon_::m_cell_face
private

Referenced by define().

◆ m_defined

int polygon_::m_defined
private

Referenced by define(), distance(), and ok().

◆ m_eb_normal

amrex::RealVect const polygon_::m_eb_normal
private

Referenced by define().

◆ m_eb_point

amrex::RealVect const polygon_::m_eb_point
private

Referenced by define().

◆ m_max_vertices

constexpr int polygon_::m_max_vertices = 6
staticconstexprprivate

Referenced by add_vertex().

◆ m_num_vertices

int polygon_::m_num_vertices
private

◆ m_sorted

int polygon_::m_sorted
private

Referenced by define().

◆ m_theta

amrex::GpuArray<amrex::Real,m_max_vertices> polygon_::m_theta
private

Referenced by define().

◆ m_vertices

amrex::Array<amrex::RealVect,m_max_vertices> polygon_::m_vertices
private

◆ m_zdir

amrex::RealVect polygon_::m_zdir
private

Referenced by define(), distance(), and normal().


The documentation for this class was generated from the following file: