openmc-dev / openmc

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

Math domain error with plotting polygon region #2930

Open MicahGale opened 1 month ago

MicahGale commented 1 month ago

Bug Description

While trying to plot a region defined by two polygons I get a ValueError: math domain error while using openmc.Cell.plot. This is caused by a square-root, so I suspect something went negative that shouldn't have.

This was produced while trying to generate "i" in this plot: letter_i.

Steps to Reproduce

MWE:

import openmc

polygon_1 = openmc.model.Polygon([(23.945502, -11.479504), (25.086518, -11.479504), (25.086518, -18.424816), (23.945502, -18.424816)],"xy")
polygon_2 = openmc.model.Polygon([(23.945502, -8.775792800000001), (25.086518, -8.775792800000001), (25.086518, -10.220666000000001), (23.945502, -10.220666000000001)],"xy")
z_bounds = +openmc.ZPlane(0) & - openmc.ZPlane(1)
# I now realize that this is the Null set, but kept as is for error replications
region = -polygon_1 & -polygon_2 & z_bounds
cell = openmc.Cell(region=region)
cell.plot(basis="xy")

This then leads to:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[1], line 9
      7 region = -polygon_1 & -polygon_2 & z_bounds
      8 cell = openmc.Cell(region=region)
----> 9 cell.plot(basis="xy")

File ~/mambaforge/lib/python3.10/site-packages/openmc/cell.py:624, in Cell.plot(self, *args, **kwargs)
    622 u = openmc.Universe(cells=[self], universe_id=openmc.Universe.next_id + 1)
    623 openmc.Universe.used_ids.remove(u.id)
--> 624 return u.plot(*args, **kwargs)

File ~/mambaforge/lib/python3.10/site-packages/openmc/universe.py:411, in Universe.plot(self, origin, width, pixels, basis, color_by, colors, seed, openmc_exec, axes, legend, axis_units, legend_kwargs, outline, **kwargs)
    409 if isinstance(pixels, int):
    410     aspect_ratio = width[0] / width[1]
--> 411     pixels_y = math.sqrt(pixels / aspect_ratio)
    412     pixels = (int(pixels / pixels_y), int(pixels_y))
    414 axis_scaling_factor = {'km': 0.00001, 'm': 0.01, 'cm': 1, 'mm': 10}

ValueError: math domain error

Specifying cell.plot(basis="xy", pixels = [1000]*2) fixes this.

I suspect this is an error with how the bounding box was calculated as it is inverted: BoundingBox(lower_left=(23.945502, -10.220666000000001, 0.0), upper_right=(25.086518, -11.479504, 1.0))

Environment

Using Conda package in mamba:

openmc 0.14.0 dagmc_nompi_py310h27bda5d_101 conda-forge

On python 3.10.12 in Ubuntu 20.04.06 in WSL.