Merge pull request #2081 from paulromano/region-parenthesis-fix

Fix a corner case in Region.from_expression
This commit is contained in:
Patrick Shriwise 2022-06-10 15:04:13 -05:00 committed by GitHub
commit fc3820191b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 1 deletions

View file

@ -1403,7 +1403,7 @@ given analytically by
.. math::
:label: coherent-elastic-angle
\mu = 1 - \frac{E_i}{E}
\mu = 1 - \frac{2E_i}{E}
where :math:`E_i` is the energy of the Bragg edge that scattered the neutron.

View file

@ -106,6 +106,11 @@ class Region(ABC):
# If special character appears immediately after a non-operator,
# create a token with the appropriate half-space
if i_start >= 0:
# When an opening parenthesis appears after a non-operator,
# there's an implicit intersection operator between them
if expression[i] == '(':
tokens.append(' ')
j = int(expression[i_start:i])
if j < 0:
tokens.append(-surfaces[abs(j)])

View file

@ -204,3 +204,7 @@ def test_from_expression(reset):
# Make sure ")(" is handled correctly
r = openmc.Region.from_expression('(-1|2)(2|-3)', surfs)
assert str(r) == '((-1 | 2) (2 | -3))'
# Opening parenthesis immediately after halfspace
r = openmc.Region.from_expression('1(2|-3)', surfs)
assert str(r) == '(1 (2 | -3))'