From 123fefe08d615de6b9b246330985e7616420a11b Mon Sep 17 00:00:00 2001 From: agnelson Date: Mon, 25 Oct 2021 05:15:34 -0500 Subject: [PATCH 1/4] Added the import to openmc.lib to model.py while leaving it optiona l --- openmc/model/model.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/openmc/model/model.py b/openmc/model/model.py index bc0796ea74..22fa475dae 100644 --- a/openmc/model/model.py +++ b/openmc/model/model.py @@ -14,8 +14,17 @@ import openmc from openmc.dummy_comm import DummyCommunicator from openmc.executor import _process_CLI_arguments from openmc.checkvalue import check_type, check_value + from openmc.exceptions import InvalidIDError +# Establish whether openmc.lib is available for downstream uses; +# this avoids the need for extraneous try/import/except statements +try: + import openmc.lib + _openmc_lib_present = True +except ImportError: + _openmc_lib_present = False + @contextmanager def _change_directory(working_dir): @@ -137,7 +146,10 @@ class Model: @property def is_initialized(self): - return openmc.lib.is_initialized + if _openmc_lib_present: + return openmc.lib.is_initialized + else: + return False @geometry.setter def geometry(self, geometry): @@ -512,7 +524,7 @@ class Model: for arg_name, arg, default in zip( ['threads', 'geometry_debug', 'restart_file', 'tracks'], [threads, geometry_debug, restart_file, tracks], - [None, False, None, False]): + [None, False, None, False]): if arg != default: msg = f"{arg_name} must be set via Model.is_initialized(...)" raise ValueError(msg) From f3dee1fe002f602b25f99155b750176d1369cc47 Mon Sep 17 00:00:00 2001 From: agnelson Date: Tue, 26 Oct 2021 12:22:07 -0500 Subject: [PATCH 2/4] Updated model per the review suggestions --- openmc/model/model.py | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/openmc/model/model.py b/openmc/model/model.py index 22fa475dae..5747ae2ca7 100644 --- a/openmc/model/model.py +++ b/openmc/model/model.py @@ -1,11 +1,8 @@ from collections.abc import Iterable -import operator import os from pathlib import Path from numbers import Integral import time -import warnings -import subprocess from contextlib import contextmanager import h5py @@ -17,14 +14,6 @@ from openmc.checkvalue import check_type, check_value from openmc.exceptions import InvalidIDError -# Establish whether openmc.lib is available for downstream uses; -# this avoids the need for extraneous try/import/except statements -try: - import openmc.lib - _openmc_lib_present = True -except ImportError: - _openmc_lib_present = False - @contextmanager def _change_directory(working_dir): @@ -146,9 +135,10 @@ class Model: @property def is_initialized(self): - if _openmc_lib_present: + try: + import openmc.lib return openmc.lib.is_initialized - else: + except ImportError: return False @geometry.setter @@ -524,7 +514,8 @@ class Model: for arg_name, arg, default in zip( ['threads', 'geometry_debug', 'restart_file', 'tracks'], [threads, geometry_debug, restart_file, tracks], - [None, False, None, False]): + [None, False, None, False] + ): if arg != default: msg = f"{arg_name} must be set via Model.is_initialized(...)" raise ValueError(msg) From 4fe524ad257f16865977deb50ac7ba5e803bb7b1 Mon Sep 17 00:00:00 2001 From: agnelson Date: Tue, 2 Nov 2021 08:17:47 -0500 Subject: [PATCH 3/4] Added appropriate try/import for openmc.lib around all places in Model where it is necessasry --- openmc/model/model.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/openmc/model/model.py b/openmc/model/model.py index 5747ae2ca7..67541215cc 100644 --- a/openmc/model/model.py +++ b/openmc/model/model.py @@ -11,7 +11,6 @@ import openmc from openmc.dummy_comm import DummyCommunicator from openmc.executor import _process_CLI_arguments from openmc.checkvalue import check_type, check_value - from openmc.exceptions import InvalidIDError @@ -237,6 +236,12 @@ class Model: MPI intracommunicator """ + try: + import openmc.lib + except ImportError: + raise ImportError('openmc.lib must be available to ' + 'execute Model.init_lib(...)') from None + # TODO: right now the only way to set most of the above parameters via # the C API are at initialization time despite use-cases existing to # set them for individual runs. For now this functionality is exposed @@ -275,6 +280,12 @@ class Model: """ + try: + import openmc.lib + except ImportError: + raise ImportError('openmc.lib must be available to ' + 'execute Model.finalize_lib(...)') from None + openmc.lib.finalize() def deplete(self, timesteps, method='cecm', final_step=True, @@ -411,6 +422,12 @@ class Model: openmc.lib.export_properties """ + try: + import openmc.lib + except ImportError: + raise ImportError('openmc.lib must be available to ' + 'execute Model.import_properties(...)') from None + cells = self.geometry.get_all_cells() materials = self.geometry.get_all_materials() @@ -691,12 +708,13 @@ class Model: if obj_type == 'cell': by_name = self._cells_by_name by_id = self._cells_by_id - obj_by_id = openmc.lib.cells + if self.is_initialized: + obj_by_id = openmc.lib.cells else: by_name = self._materials_by_name by_id = self._materials_by_id - obj_by_id = openmc.lib.materials - + if self.is_initialized: + obj_by_id = openmc.lib.materials # Get the list of ids to use if converting from names and accepting # only values that have actual ids ids = [] From 579b0a8f2eff0681fbd75b9ce26427337df64f8d Mon Sep 17 00:00:00 2001 From: agnelson Date: Tue, 9 Nov 2021 09:19:50 -0600 Subject: [PATCH 4/4] Removed the try/except clause around import openmc.lib --- openmc/model/model.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/openmc/model/model.py b/openmc/model/model.py index 67541215cc..669a19a0bd 100644 --- a/openmc/model/model.py +++ b/openmc/model/model.py @@ -236,11 +236,7 @@ class Model: MPI intracommunicator """ - try: - import openmc.lib - except ImportError: - raise ImportError('openmc.lib must be available to ' - 'execute Model.init_lib(...)') from None + import openmc.lib # TODO: right now the only way to set most of the above parameters via # the C API are at initialization time despite use-cases existing to @@ -280,11 +276,7 @@ class Model: """ - try: - import openmc.lib - except ImportError: - raise ImportError('openmc.lib must be available to ' - 'execute Model.finalize_lib(...)') from None + import openmc.lib openmc.lib.finalize() @@ -422,11 +414,7 @@ class Model: openmc.lib.export_properties """ - try: - import openmc.lib - except ImportError: - raise ImportError('openmc.lib must be available to ' - 'execute Model.import_properties(...)') from None + import openmc.lib cells = self.geometry.get_all_cells() materials = self.geometry.get_all_materials()