From 577157b846ac6885251ade59456967e4102d6b24 Mon Sep 17 00:00:00 2001 From: shimwell Date: Sat, 24 Dec 2022 22:40:34 +0000 Subject: [PATCH 1/2] allowing xml file for materials --- openmc/geometry.py | 29 +++++++++++++++++++---------- tests/unit_tests/test_geometry.py | 19 ++++++++++++++++++- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/openmc/geometry.py b/openmc/geometry.py index a43899daa..eb7fa8200 100644 --- a/openmc/geometry.py +++ b/openmc/geometry.py @@ -1,3 +1,5 @@ +import os +import typing from collections import OrderedDict, defaultdict from collections.abc import Iterable from copy import deepcopy @@ -7,7 +9,7 @@ import warnings import openmc import openmc._xml as xml -from .checkvalue import check_type, check_less_than, check_greater_than +from .checkvalue import check_type, check_less_than, check_greater_than, PathLike class Geometry: @@ -254,16 +256,20 @@ class Geometry: raise ValueError('Error determining root universe.') @classmethod - def from_xml(cls, path='geometry.xml', materials=None): + def from_xml( + cls, + path: PathLike = 'geometry.xml', + materials: typing.Optional[typing.Union[PathLike, 'openmc.Materials']] = 'materials.xml' + ): """Generate geometry from XML file Parameters ---------- - path : str, optional + path : PathLike, optional Path to geometry XML file - materials : openmc.Materials or None - Materials used to assign to cells. If None, an attempt is made to - generate it from the materials.xml file. + materials : openmc.Materials or or PathLike + Materials used to assign to cells. If PathLike, an attempt is made + to generate materials from the provided xml file. Returns ------- @@ -271,10 +277,13 @@ class Geometry: Geometry object """ - # Create dictionary to easily look up materials - if materials is None: - filename = Path(path).parent / 'materials.xml' - materials = openmc.Materials.from_xml(str(filename)) + + # Using str and os.Pathlike here to avoid error when using just the imported PathLike + # TypeError: Subscripted generics cannot be used with class and instance checks + check_type('materials', materials, (str, os.PathLike, openmc.Materials)) + + if isinstance(materials, (str, os.PathLike)): + materials = openmc.Materials.from_xml(materials) tree = ET.parse(path) root = tree.getroot() diff --git a/tests/unit_tests/test_geometry.py b/tests/unit_tests/test_geometry.py index 9db112fd2..843c01def 100644 --- a/tests/unit_tests/test_geometry.py +++ b/tests/unit_tests/test_geometry.py @@ -1,4 +1,5 @@ import xml.etree.ElementTree as ET +from pathlib import Path import numpy as np import openmc @@ -274,7 +275,23 @@ def test_from_xml(run_in_tmpdir, mixed_lattice_model): # Export model mixed_lattice_model.export_to_xml() - # Import geometry + mats_from_xml = openmc.Materials.from_xml('materials.xml') + # checking string a Path are both acceptable + for path in ['geometry.xml', Path('geometry.xml')]: + for materials in [mats_from_xml, 'materials.xml']: + # Import geometry from file + geom = openmc.Geometry.from_xml(path=path, materials=materials) + assert isinstance(geom, openmc.Geometry) + ll, ur = geom.bounding_box + assert ll == pytest.approx((-6.0, -6.0, -np.inf)) + assert ur == pytest.approx((6.0, 6.0, np.inf)) + + with pytest.raises(TypeError) as excinfo: + geom = openmc.Geometry.from_xml(path='geometry.xml', materials=None) + assert 'Unable to set "materials" to "None"' in str(excinfo.value) + + + # checking that the default args also work geom = openmc.Geometry.from_xml() assert isinstance(geom, openmc.Geometry) ll, ur = geom.bounding_box From 2c9df1b77e5828ed410dbd00250856265ec211ca Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Wed, 11 Jan 2023 09:32:02 +0000 Subject: [PATCH 2/2] Apply typo fixes identified incode review by @paulromano Co-authored-by: Paul Romano --- openmc/geometry.py | 2 +- tests/unit_tests/test_geometry.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/openmc/geometry.py b/openmc/geometry.py index eb7fa8200..a9817fbbb 100644 --- a/openmc/geometry.py +++ b/openmc/geometry.py @@ -267,7 +267,7 @@ class Geometry: ---------- path : PathLike, optional Path to geometry XML file - materials : openmc.Materials or or PathLike + materials : openmc.Materials or PathLike Materials used to assign to cells. If PathLike, an attempt is made to generate materials from the provided xml file. diff --git a/tests/unit_tests/test_geometry.py b/tests/unit_tests/test_geometry.py index 843c01def..0e952ab0f 100644 --- a/tests/unit_tests/test_geometry.py +++ b/tests/unit_tests/test_geometry.py @@ -290,7 +290,6 @@ def test_from_xml(run_in_tmpdir, mixed_lattice_model): geom = openmc.Geometry.from_xml(path='geometry.xml', materials=None) assert 'Unable to set "materials" to "None"' in str(excinfo.value) - # checking that the default args also work geom = openmc.Geometry.from_xml() assert isinstance(geom, openmc.Geometry)