Add global configuration via openmc.config

This commit is contained in:
Paul Romano 2022-08-31 16:20:42 -05:00
parent aea5628819
commit c29fb407d5
2 changed files with 55 additions and 0 deletions

View file

@ -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

54
openmc/config.py Normal file
View file

@ -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