From c29fb407d5794089da62b7bf53049097fc74c725 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Wed, 31 Aug 2022 16:20:42 -0500 Subject: [PATCH] Add global configuration via openmc.config --- openmc/__init__.py | 1 + openmc/config.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 openmc/config.py diff --git a/openmc/__init__.py b/openmc/__init__.py index abcac899f6..214695e67f 100644 --- a/openmc/__init__.py +++ b/openmc/__init__.py @@ -32,6 +32,7 @@ from openmc.search import * from openmc.polynomial import * from openmc.tracks import * from . import examples +from .config import * # Import a few names from the model module from openmc.model import rectangular_prism, hexagonal_prism, Model diff --git a/openmc/config.py b/openmc/config.py new file mode 100644 index 0000000000..a883c74cc9 --- /dev/null +++ b/openmc/config.py @@ -0,0 +1,54 @@ +from collections.abc import MutableMapping +import os +from pathlib import Path + +from openmc.data import DataLibrary + + +class _Config(MutableMapping): + def __init__(self, data=()): + self._mapping = {} + self.update(data) + + def __getitem__(self, key): + return self._mapping[key] + + def __delitem__(self, key): + del self._mapping[key] + + def __setitem__(self, key, value): + if key == 'cross_sections': + # Force environment variable to match + self._mapping[key] = Path(value) + os.environ['OPENMC_CROSS_SECTIONS'] = str(value) + elif key == 'chain_file': + self._mapping[key] = Path(value) + else: + raise KeyError(f'Unrecognized config key: {key}') + + def __iter__(self): + return iter(self._mapping) + + def __len__(self): + return len(self._mapping) + + def __repr__(self): + return repr(self._mapping) + + +# Create default configuration +config = _Config() + +# Set cross sections using environment variable +config['cross_sections'] = os.environ.get("OPENMC_CROSS_SECTIONS") + +# Check for depletion chain in cross_sections.xml +# Set depletion chain +chain_file = os.environ.get("OPENMC_DEPLETE_CHAIN") +if chain_file is None and config['cross_sections'] is not None: + data = DataLibrary.from_xml(config['cross_sections']) + for lib in reversed(data.libraries): + if lib['type'] == 'depletion_chain': + chain_file = lib['path'] + break +config['chain_file'] = chain_file