OpenMC/openmc/config.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

94 lines
3 KiB
Python
Raw Permalink Normal View History

from collections.abc import MutableMapping
import os
from pathlib import Path
2022-09-06 16:00:51 -05:00
import warnings
from openmc.data import DataLibrary
from openmc.data.decay import _DECAY_ENERGY, _DECAY_PHOTON_ENERGY
2022-09-01 09:37:05 -05:00
__all__ = ["config"]
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]
2022-09-06 16:00:51 -05:00
if key == 'cross_sections':
del os.environ['OPENMC_CROSS_SECTIONS']
elif key == 'mg_cross_sections':
del os.environ['OPENMC_MG_CROSS_SECTIONS']
elif key == 'chain_file':
del os.environ['OPENMC_CHAIN_FILE']
# Reset photon source data since it relies on chain file
2022-09-28 09:31:12 -05:00
_DECAY_PHOTON_ENERGY.clear()
def __setitem__(self, key, value):
if key == 'cross_sections':
# Force environment variable to match
2022-09-06 16:00:51 -05:00
self._set_path(key, value)
os.environ['OPENMC_CROSS_SECTIONS'] = str(value)
2022-08-31 22:23:29 -05:00
elif key == 'mg_cross_sections':
2022-09-06 16:00:51 -05:00
self._set_path(key, value)
2022-08-31 22:23:29 -05:00
os.environ['OPENMC_MG_CROSS_SECTIONS'] = str(value)
elif key == 'chain_file':
2022-09-06 16:00:51 -05:00
self._set_path(key, value)
2022-09-07 16:36:33 -05:00
os.environ['OPENMC_CHAIN_FILE'] = str(value)
# Reset photon source data since it relies on chain file
2022-09-28 09:31:12 -05:00
_DECAY_PHOTON_ENERGY.clear()
_DECAY_ENERGY.clear()
else:
2022-11-13 16:11:23 +00:00
raise KeyError(f'Unrecognized config key: {key}. Acceptable keys '
'are "cross_sections", "mg_cross_sections" and '
'"chain_file"')
def __iter__(self):
return iter(self._mapping)
def __len__(self):
return len(self._mapping)
def __repr__(self):
return repr(self._mapping)
2022-09-06 16:00:51 -05:00
def _set_path(self, key, value):
self._mapping[key] = p = Path(value)
if not p.exists():
warnings.warn(f"'{value}' does not exist.")
2022-09-01 09:37:05 -05:00
def _default_config():
"""Return default configuration"""
config = _Config()
# Set cross sections using environment variable
if "OPENMC_CROSS_SECTIONS" in os.environ:
config['cross_sections'] = os.environ["OPENMC_CROSS_SECTIONS"]
if "OPENMC_MG_CROSS_SECTIONS" in os.environ:
config['mg_cross_sections'] = os.environ["OPENMC_MG_CROSS_SECTIONS"]
# Set depletion chain
2022-09-07 16:36:33 -05:00
chain_file = os.environ.get("OPENMC_CHAIN_FILE")
2022-09-06 16:00:51 -05:00
if (chain_file is None and
config.get('cross_sections') is not None and
2022-09-06 16:00:51 -05:00
config['cross_sections'].exists()
):
2022-09-06 16:00:51 -05:00
# Check for depletion chain in cross_sections.xml
2022-09-01 09:37:05 -05:00
data = DataLibrary.from_xml(config['cross_sections'])
for lib in reversed(data.libraries):
if lib['type'] == 'depletion_chain':
chain_file = lib['path']
break
if chain_file is not None:
config['chain_file'] = chain_file
return config
config = _default_config()