diff --git a/src/openmc_mcnp_adapter/openmc_conversion.py b/src/openmc_mcnp_adapter/openmc_conversion.py index 7e4c576..3898646 100644 --- a/src/openmc_mcnp_adapter/openmc_conversion.py +++ b/src/openmc_mcnp_adapter/openmc_conversion.py @@ -301,15 +301,17 @@ def flip_sense(surf): a, b, c, d, e, f, g, h, j, k = coeffs surf = openmc.Quadric(surface_id=s['id'], a=a, b=b, c=c, d=d, e=e, f=f, g=g, h=h, j=j, k=k) - elif s['mnemonic'] == 'tx': + elif s['mnemonic'] in ('tx', 'ty', 'tz'): x0, y0, z0, a, b, c = coeffs - surf = openmc.XTorus(surface_id=s['id'], x0=x0, y0=y0, z0=z0, a=a, b=b, c=c) - elif s['mnemonic'] == 'ty': - x0, y0, z0, a, b, c = coeffs - surf = openmc.YTorus(surface_id=s['id'], x0=x0, y0=y0, z0=z0, a=a, b=b, c=c) - elif s['mnemonic'] == 'tz': - x0, y0, z0, a, b, c = coeffs - surf = openmc.ZTorus(surface_id=s['id'], x0=x0, y0=y0, z0=z0, a=a, b=b, c=c) + if isclose(a, 0.0, abs_tol=1e-12) and isclose(b, c): + warnings.warn( + f"Degenerate torus surface {s['id']} (A=0, B=C) converted " + f"to an openmc.Sphere of radius {b}." + ) + surf = openmc.Sphere(surface_id=s['id'], x0=x0, y0=y0, z0=z0, r=b) + else: + cls = getattr(openmc, f"{s['mnemonic'][1].upper()}Torus") + surf = cls(surface_id=s['id'], x0=x0, y0=y0, z0=z0, a=a, b=b, c=c) elif s['mnemonic'] in ('x', 'y', 'z'): axis = s['mnemonic'].upper() cls_plane = getattr(openmc, f'{axis}Plane') diff --git a/tests/test_surfaces.py b/tests/test_surfaces.py index 727c054..32df317 100644 --- a/tests/test_surfaces.py +++ b/tests/test_surfaces.py @@ -7,7 +7,7 @@ RectangularParallelepiped, RightCircularCylinder, ConicalFrustum, \ XConeOneSided, YConeOneSided, ZConeOneSided from openmc_mcnp_adapter import mcnp_str_to_model, get_openmc_surfaces -from pytest import approx, mark, raises +from pytest import approx, mark, raises, warns def convert_surface(mnemonic: str, params: Sequence[float]) -> openmc.Surface: @@ -323,6 +323,14 @@ def test_torus(mnemonic, expected_type): assert getattr(surf, name) == approx(val) +@mark.parametrize("mnemonic", ["tx", "ty", "tz"]) +def test_torus_degenerate_sphere(mnemonic): + with warns(UserWarning, match="Degenerate torus"): + surf = convert_surface(mnemonic, (1.0, 2.0, 3.0, 0.0, 0.5, 0.5)) + assert isinstance(surf, openmc.Sphere) + assert (surf.x0, surf.y0, surf.z0, surf.r) == approx((1.0, 2.0, 3.0, 0.5)) + + @mark.parametrize( "mnemonic, params, expected_type, attr, value", [