From 47e94ca79b97c1e910cdf752e7d4c0df6beed452 Mon Sep 17 00:00:00 2001 From: Xylar Asay-Davis Date: Sun, 15 Nov 2026 17:03:13 +0100 Subject: [PATCH 1/3] Order edges on vertices by the direction to the next vertex orderVertexArrays sorted the edges around each vertex by the direction from the vertex to each edge's location, and builds cellsOnVertex and the kites in kiteAreasOnVertex from that order. Where a triangle is obtuse, its circumcenter lies outside it, and the location of the edge opposite the obtuse angle -- where that edge crosses the line between its cells -- lies behind the vertex. That edge then sorted into the wrong place, so each kite paired a cell with an edge it doesn't share. areaTriangle, kiteAreasOnVertex and weightsOnEdge were wrong at those vertices, and NaN where a mismatched kite was degenerate enough for round-off to make its area NaN. Sort by the direction to the vertex at the other end of each edge instead, falling back to the edge's location on a boundary. On a mesh without obtuse triangles the two directions agree and the output is unchanged. Co-Authored-By: Claude Opus 5.5 --- .../mpas_mesh_converter.cpp | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/mesh_tools/mesh_conversion_tools_netcdf_c/mpas_mesh_converter.cpp b/mesh_tools/mesh_conversion_tools_netcdf_c/mpas_mesh_converter.cpp index 00cc155d4..7deb59b1a 100755 --- a/mesh_tools/mesh_conversion_tools_netcdf_c/mpas_mesh_converter.cpp +++ b/mesh_tools/mesh_conversion_tools_netcdf_c/mpas_mesh_converter.cpp @@ -92,6 +92,7 @@ int buildUnorderedCellConnectivity(); int firstOrderingVerticesOnCell(); int buildCompleteCellMask(); int buildEdges(); +pnt edgeDirectionPoint(const int iVertex, const int iEdge); int orderVertexArrays(); int orderCellArrays(); int buildAreas(); @@ -1377,6 +1378,32 @@ int buildEdges(){/*{{{*/ return 0; }/*}}}*/ +pnt edgeDirectionPoint(const int iVertex, const int iEdge){/*{{{*/ + /* + * edgeDirectionPoint returns a point in the direction of edge iEdge as seen + * from vertex iVertex, for ordering the edges around the vertex: the + * vertex at the other end of the edge, or the edge's location if there + * is no other vertex. + * + * The edge's location will not do in general. It is where the edge + * crosses the line between its cells, and if the triangle the vertex + * is the circumcenter of is obtuse, that point lies behind the vertex + * for the edge opposite the obtuse angle. Ordering by it then + * reverses that edge's place around the vertex, and each kite in + * kiteAreasOnVertex pairs a cell with an edge it doesn't share. + */ + int otherVertex; + + otherVertex = verticesOnEdge.at(iEdge).at(0); + if(otherVertex == iVertex){ + otherVertex = verticesOnEdge.at(iEdge).at(1); + } + + if(otherVertex != -1){ + return vertices.at(otherVertex); + } + return edges.at(iEdge); +}/*}}}*/ int orderVertexArrays(){/*{{{*/ /* * orderVertexArrays builds and orders the connectivity arrays for vertices. @@ -1431,7 +1458,7 @@ int orderVertexArrays(){/*{{{*/ for(j = 0; j < edgesOnVertex.at(iVertex).size(); j++){ edge1 = edgesOnVertex.at(iVertex).at(j); - edge_loc1 = edges.at(edge1); + edge_loc1 = edgeDirectionPoint(iVertex, edge1); // Fix edge1 periodicity if(!spherical){ @@ -1448,7 +1475,7 @@ int orderVertexArrays(){/*{{{*/ for(k = j+1; k < edgesOnVertex.at(iVertex).size(); k++){ edge2 = edgesOnVertex.at(iVertex).at(k); - edge_loc2 = edges.at(edge2); + edge_loc2 = edgeDirectionPoint(iVertex, edge2); // Fix edge2 periodicity if(!spherical){ From 6de2f24a5552d91702b35ea09043fdeade0473e5 Mon Sep 17 00:00:00 2001 From: Xylar Asay-Davis Date: Sun, 15 Nov 2026 17:03:13 +0100 Subject: [PATCH 2/3] Keep degenerate triangle areas from becoming NaN Heron's formula and its spherical analog take the square root of a product that is zero for a degenerate triangle, and round-off can make it slightly negative, giving NaN. Clamp the product at zero. Co-Authored-By: Claude Opus 5.5 --- mesh_tools/mesh_conversion_tools_netcdf_c/pnt.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mesh_tools/mesh_conversion_tools_netcdf_c/pnt.h b/mesh_tools/mesh_conversion_tools_netcdf_c/pnt.h index ca2a35d42..6cbe1e62d 100755 --- a/mesh_tools/mesh_conversion_tools_netcdf_c/pnt.h +++ b/mesh_tools/mesh_conversion_tools_netcdf_c/pnt.h @@ -420,7 +420,9 @@ double planarTriangleArea(const pnt &A, const pnt &B, const pnt &C){/*{{{*/ // Semi-perimeter of TRI(ABC) s = (a + b + c) * 0.5; - return sqrt(s * (s - a) * (s - b) * (s - c)); + // A degenerate triangle can give a product that round-off makes slightly + // negative, and the square root of that is NaN rather than zero. + return sqrt(std::max(s * (s - a) * (s - b) * (s - c), 0.0)); }/*}}}*/ double sphericalTriangleArea(const pnt &A, const pnt &B, const pnt &C){/*{{{*/ /* @@ -435,7 +437,9 @@ double sphericalTriangleArea(const pnt &A, const pnt &B, const pnt &C){/*{{{*/ c = C.sphereDistance(A); s = 0.5*(a+b+c); - tanqe = sqrt(tan(0.5*s)*tan(0.5*(s-a))*tan(0.5*(s-b))*tan(0.5*(s-c))); + // A degenerate triangle can give a product that round-off makes slightly + // negative, and the square root of that is NaN rather than zero. + tanqe = sqrt(std::max(tan(0.5*s)*tan(0.5*(s-a))*tan(0.5*(s-b))*tan(0.5*(s-c)), 0.0)); e = 4.*atan(tanqe); return e; From cc1a077b94f481941089142b982c3967f62913c4 Mon Sep 17 00:00:00 2001 From: Xylar Asay-Davis Date: Sun, 15 Nov 2026 17:46:44 +0100 Subject: [PATCH 3/3] Rename the points that orderVertexArrays sorts edges by They are now usually the vertex at the other end of each edge rather than the edge's location, so call them dir_point1 and dir_point2. Co-Authored-By: Claude Opus 5.5 --- .../mpas_mesh_converter.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/mesh_tools/mesh_conversion_tools_netcdf_c/mpas_mesh_converter.cpp b/mesh_tools/mesh_conversion_tools_netcdf_c/mpas_mesh_converter.cpp index 7deb59b1a..4ca0ae959 100755 --- a/mesh_tools/mesh_conversion_tools_netcdf_c/mpas_mesh_converter.cpp +++ b/mesh_tools/mesh_conversion_tools_netcdf_c/mpas_mesh_converter.cpp @@ -1416,7 +1416,7 @@ int orderVertexArrays(){/*{{{*/ pnt normal; pnt cross; pnt vec1, vec2; - pnt cell_loc, edge_loc1, edge_loc2; + pnt cell_loc, dir_point1, dir_point2; double mag1, mag2, min_angle, angle; double dvEdge, dot, area; bool fixed_area; @@ -1458,13 +1458,13 @@ int orderVertexArrays(){/*{{{*/ for(j = 0; j < edgesOnVertex.at(iVertex).size(); j++){ edge1 = edgesOnVertex.at(iVertex).at(j); - edge_loc1 = edgeDirectionPoint(iVertex, edge1); + dir_point1 = edgeDirectionPoint(iVertex, edge1); - // Fix edge1 periodicity + // Fix dir_point1 periodicity if(!spherical){ - edge_loc1.fixPeriodicity(vertices.at(iVertex), xPeriodicFix, yPeriodicFix); + dir_point1.fixPeriodicity(vertices.at(iVertex), xPeriodicFix, yPeriodicFix); } - vec1 = edge_loc1 - vertices.at(iVertex); + vec1 = dir_point1 - vertices.at(iVertex); mag1 = vec1.magnitude(); min_angle = 2.0*M_PI; @@ -1475,13 +1475,13 @@ int orderVertexArrays(){/*{{{*/ for(k = j+1; k < edgesOnVertex.at(iVertex).size(); k++){ edge2 = edgesOnVertex.at(iVertex).at(k); - edge_loc2 = edgeDirectionPoint(iVertex, edge2); + dir_point2 = edgeDirectionPoint(iVertex, edge2); - // Fix edge2 periodicity + // Fix dir_point2 periodicity if(!spherical){ - edge_loc2.fixPeriodicity(vertices.at(iVertex), xPeriodicFix, yPeriodicFix); + dir_point2.fixPeriodicity(vertices.at(iVertex), xPeriodicFix, yPeriodicFix); } - vec2 = edge_loc2 - vertices.at(iVertex); + vec2 = dir_point2 - vertices.at(iVertex); mag2 = vec2.magnitude(); cross = vec1.cross(vec2);