Merge pull request #2211 from paulromano/config

Allow configuration of data sources via openmc.config
This commit is contained in:
Jonathan Shimwell 2022-09-21 16:47:20 +01:00 committed by GitHub
commit ffe336c2ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 299 additions and 172 deletions

View file

@ -0,0 +1,43 @@
from collections.abc import Mapping
import os
import openmc
import pytest
@pytest.fixture(autouse=True, scope='module')
def reset_config():
config = dict(openmc.config)
try:
yield
finally:
openmc.config.clear()
openmc.config.update(config)
def test_config_basics():
assert isinstance(openmc.config, Mapping)
for key, value in openmc.config.items():
assert isinstance(key, str)
assert isinstance(value, os.PathLike)
# Set and delete
openmc.config['cross_sections'] = '/path/to/cross_sections.xml'
del openmc.config['cross_sections']
assert 'cross_sections' not in openmc.config
assert 'OPENMC_CROSS_SECTIONS' not in os.environ
# Can't use any key
with pytest.raises(KeyError):
openmc.config['🐖'] = '/like/to/eat/bacon'
def test_config_set_envvar():
openmc.config['cross_sections'] = '/path/to/cross_sections.xml'
assert os.environ['OPENMC_CROSS_SECTIONS'] == '/path/to/cross_sections.xml'
openmc.config['mg_cross_sections'] = '/path/to/mg_cross_sections.h5'
assert os.environ['OPENMC_MG_CROSS_SECTIONS'] == '/path/to/mg_cross_sections.h5'
openmc.config['chain_file'] = '/path/to/chain_file.xml'
assert os.environ['OPENMC_CHAIN_FILE'] == '/path/to/chain_file.xml'

View file

@ -1,37 +1,15 @@
"""Basic unit tests for openmc.deplete.Operator instantiation
Modifies and resets environment variable OPENMC_CROSS_SECTIONS
to a custom file with new depletion_chain node
"""
from pathlib import Path
import pytest
from openmc.deplete.abc import TransportOperator
from openmc.deplete.chain import Chain, _find_chain_file
from openmc.deplete.chain import Chain
BARE_XS_FILE = "bare_cross_sections.xml"
CHAIN_PATH = Path(__file__).parents[1] / "chain_simple.xml"
@pytest.fixture()
def bare_xs(run_in_tmpdir):
"""Create a very basic cross_sections file, return simple Chain.
"""
bare_xs_contents = """<?xml version="1.0"?>
<cross_sections>
<depletion_chain path="{}" />
</cross_sections>
""".format(CHAIN_PATH)
with open(BARE_XS_FILE, "w") as out:
out.write(bare_xs_contents)
yield BARE_XS_FILE
class BareDepleteOperator(TransportOperator):
"""Very basic class for testing the initialization."""
@ -52,10 +30,10 @@ class BareDepleteOperator(TransportOperator):
pass
def test_operator_init(bare_xs):
def test_operator_init():
"""The test uses a temporary dummy chain. This file will be removed
at the end of the test, and only contains a depletion_chain node."""
bare_op = BareDepleteOperator(_find_chain_file(bare_xs))
bare_op = BareDepleteOperator(CHAIN_PATH)
act_chain = bare_op.chain
ref_chain = Chain.from_xml(CHAIN_PATH)
assert len(act_chain) == len(ref_chain)
@ -73,8 +51,7 @@ def test_operator_init(bare_xs):
def test_operator_fiss_q():
"""Make sure fission q values can be set"""
new_q = {"U235": 2.0E8, "U238": 2.0E8, "U234": 5.0E7}
chain_file = Path(__file__).parents[1] / "chain_simple.xml"
operator = BareDepleteOperator(chain_file=chain_file, fission_q=new_q)
operator = BareDepleteOperator(chain_file=CHAIN_PATH, fission_q=new_q)
mod_chain = operator.chain
for name, q in new_q.items():
chain_nuc = mod_chain[name]