From dacf650eaf74ce6b138c0225cfcc83d4c622a39d Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Thu, 8 Feb 2018 14:32:28 -0600 Subject: [PATCH] Fix bug with clearing k_generation and entropy --- src/simulation.F90 | 2 ++ tests/__init__.py | 15 +++++++++++++++ tests/unit_tests/test_capi.py | 33 ++++++++++++++++++++------------- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/simulation.F90 b/src/simulation.F90 index 424c55e9bd..0204aaf796 100644 --- a/src/simulation.F90 +++ b/src/simulation.F90 @@ -454,6 +454,8 @@ contains ! Reset global variables current_batch = 0 + call k_generation % clear() + call entropy % clear() need_depletion_rx = .false. ! Set flag indicating initialization is done diff --git a/tests/__init__.py b/tests/__init__.py index e69de29bb2..5c21cd9380 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1,15 @@ +from contextlib import contextmanager +import os +import tempfile + + +@contextmanager +def cdtemp(): + """Context manager to change to/return from a tmpdir.""" + with tempfile.TemporaryDirectory() as tmpdir: + cwd = os.getcwd() + try: + os.chdir(tmpdir) + yield + finally: + os.chdir(cwd) diff --git a/tests/unit_tests/test_capi.py b/tests/unit_tests/test_capi.py index e1e87e27b3..618f54e4f9 100644 --- a/tests/unit_tests/test_capi.py +++ b/tests/unit_tests/test_capi.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - from collections import Mapping import os @@ -8,12 +6,15 @@ import pytest import openmc import openmc.capi +from tests import cdtemp + @pytest.fixture(scope='module') def pincell_model(): """Set up a model to test with and delete files when done""" openmc.reset_auto_ids() pincell = openmc.examples.pwr_pin_cell() + pincell.settings.verbosity = 1 # Add a tally filter1 = openmc.MaterialFilter(pincell.materials) @@ -24,17 +25,10 @@ def pincell_model(): mat_tally.scores = ['total', 'elastic', '(n,gamma)'] pincell.tallies.append(mat_tally) - # Write XML files - pincell.export_to_xml() - - yield - - # Delete generated files - files = ['geometry.xml', 'materials.xml', 'settings.xml', 'tallies.xml', - 'statepoint.10.h5', 'summary.h5', 'test_sp.h5'] - for f in files: - if os.path.exists(f): - os.remove(f) + # Write XML files in tmpdir + with cdtemp(): + pincell.export_to_xml() + yield @pytest.fixture(scope='module') @@ -229,6 +223,19 @@ def test_by_batch(capi_run): openmc.capi.simulation_finalize() +def test_reproduce_keff(capi_init): + # Get k-effective after run + openmc.capi.hard_reset() + openmc.capi.run() + keff0 = openmc.capi.keff() + + # Reset, run again, and get k-effective again. they should match + openmc.capi.hard_reset() + openmc.capi.run() + keff1 = openmc.capi.keff() + assert keff0 == pytest.approx(keff1) + + def test_find_cell(capi_init): cell, instance = openmc.capi.find_cell((0., 0., 0.)) assert cell is openmc.capi.cells[1]