openmc-dev / openmc

OpenMC Monte Carlo Code
https://docs.openmc.org
Other
699 stars 444 forks source link

Polygon can't handle colinear points. #2931

Closed MicahGale closed 3 weeks ago

MicahGale commented 1 month ago

Bug Description

When multiple points for openmc.model.Polygon are colinear the polygon decomposes in part into a line and breaks the underlying geometry engine. The question though is why would you ever specify colinear points? I was working on hackily creating polygons from an SVG file. In this case SVG interpreters don't care about colinearity, and this can happen.

I was attempting to create this: letter_h

The problematic polygon looks like: letter_h2

Steps to Reproduce

MWE:

import openmc
openmc.model.Polygon([
(43.08936, -13.206429),
(43.08936, -22.855452),
(44.236576, -22.855452),
(44.236576, -18.930111),
(44.236576, -16.989144),
(44.236576, -13.206429),
(43.08936, -13.206429),
])

This leads to:

QhullError                                Traceback (most recent call last)
Cell In[1], line 2
      1 import openmc
----> 2 openmc.model.Polygon([
      3 (43.08936, -13.206429),
      4 (43.08936, -22.855452),
      5 (44.236576, -22.855452),
      6 (44.236576, -18.930111),
      7 (44.236576, -16.989144),
      8 (44.236576, -13.206429),
      9 (43.08936, -13.206429),
     10 ])

File ~/mambaforge/lib/python3.10/site-packages/openmc/model/surface_composite.py:818, in Polygon.__init__(self, points, basis)
    814 self._constrain_triangulation(self._validate_points(points))
    816 # Decompose the polygon into groups of simplices forming convex subsets
    817 # and get the sets of (surface, operator) pairs defining the polygon
--> 818 self._surfsets = self._decompose_polygon_into_convex_sets()
    820 # Set surface names as required by CompositeSurface protocol
    821 surfnames = []

File ~/mambaforge/lib/python3.10/site-packages/openmc/model/surface_composite.py:1190, in Polygon._decompose_polygon_into_convex_sets(self)
   1188 groups = []
   1189 while neighbor_map:
-> 1190     groups.append(self._group_simplices(neighbor_map))
   1191 self._groups = groups
   1193 # Generate lists of (surface, operator) pairs for each convex
   1194 # sub-region.

File ~/mambaforge/lib/python3.10/site-packages/openmc/model/surface_composite.py:1075, in Polygon._group_simplices(self, neighbor_map, group)
   1073 if group is None:
   1074     sidx = next(iter(neighbor_map))
-> 1075     return self._group_simplices(neighbor_map, group=[sidx])
   1076 # Otherwise use the last simplex in the group
   1077 else:
   1078     sidx = group[-1]

File ~/mambaforge/lib/python3.10/site-packages/openmc/model/surface_composite.py:1090, in Polygon._group_simplices(self, neighbor_map, group)
   1088     test_points = self.points[test_point_idx]
   1089     # If test_points are convex keep adding to this group
-> 1090     if len(test_points) == len(ConvexHull(test_points).vertices):
   1091         group = self._group_simplices(neighbor_map, group=test_group)
   1092 return group

File _qhull.pyx:2431, in scipy.spatial._qhull.ConvexHull.__init__()

File _qhull.pyx:353, in scipy.spatial._qhull._Qhull.__init__()

QhullError: QH6013 qhull input error: input is less than 2-dimensional since all points have the same x coordinate 44.24

While executing:  | qhull i Qt
Options selected for Qhull 2019.1.r 2019/06/21:
  run-id 179452238  incidence  Qtriangulate  _pre-merge  _zero-centrum
  _max-width 9.6  Error-roundoff 3.8e-14  _one-merge 1.9e-13
  _near-inside 9.5e-13  Visible-distance 7.6e-14  U-max-coplanar 7.6e-14
  Width-outside 1.5e-13  _wide-facet 4.5e-13  _maxoutside 2.3e-13

Environment

Openmc 0.14. See #2930 for details.