Skip to content

Commit

Permalink
Mat ids reset (#3125)
Browse files Browse the repository at this point in the history
Co-authored-by: azimgivron <[email protected]>
Co-authored-by: azim_givron <[email protected]>
Co-authored-by: Paul Romano <[email protected]>
  • Loading branch information
4 people authored Sep 27, 2024
1 parent 8b77a8d commit 1645e3b
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 33 deletions.
1 change: 0 additions & 1 deletion openmc/deplete/independent_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ def _consolidate_nuclides_to_material(nuclides, nuc_units, volume):
"""Puts nuclide list into an openmc.Materials object.
"""
openmc.reset_auto_ids()
mat = openmc.Material()
if nuc_units == 'atom/b-cm':
for nuc, conc in nuclides.items():
Expand Down
5 changes: 0 additions & 5 deletions openmc/deplete/openmc_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ def __init__(
# Determine which nuclides have cross section data
# This nuclides variables contains every nuclides
# for which there is an entry in the micro_xs parameter
openmc.reset_auto_ids()

self.nuclides_with_data = self._get_nuclides_with_data(
self.cross_sections)

Expand Down Expand Up @@ -396,9 +394,6 @@ def _update_materials_and_nuclides(self, vec):
self.number.set_density(vec)
self._update_materials()

# Prevent OpenMC from complaining about re-creating tallies
openmc.reset_auto_ids()

# Update tally nuclides data in preparation for transport solve
nuclides = self._get_reaction_nuclides()
self._rate_helper.nuclides = nuclides
Expand Down
9 changes: 7 additions & 2 deletions openmc/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,17 @@ def id(self, uid):
cls.used_ids.add(uid)
self._id = uid

@classmethod
def reset_ids(cls):
"""Reset counters"""
cls.used_ids.clear()
cls.next_id = 1


def reset_auto_ids():
"""Reset counters for all auto-generated IDs"""
for cls in IDManagerMixin.__subclasses__():
cls.used_ids.clear()
cls.next_id = 1
cls.reset_ids()


def reserve_ids(ids, cls=None):
Expand Down
11 changes: 11 additions & 0 deletions tests/regression_tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
}


def assert_same_mats(res_ref, res_test):
for mat in res_ref[0].index_mat:
assert mat in res_test[0].index_mat, f"Material {mat} not in new results."
for nuc in res_ref[0].index_nuc:
assert nuc in res_test[0].index_nuc, f"Nuclide {nuc} not in new results."
for mat in res_test[0].index_mat:
assert mat in res_ref[0].index_mat, f"Material {mat} not in old results."
for nuc in res_test[0].index_nuc:
assert nuc in res_ref[0].index_nuc, f"Nuclide {nuc} not in old results."


def assert_atoms_equal(res_ref, res_test, tol=1e-5):
for mat in res_test[0].index_mat:
for nuc in res_test[0].index_nuc:
Expand Down
24 changes: 4 additions & 20 deletions tests/regression_tests/deplete_no_transport/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from openmc.deplete import IndependentOperator, MicroXS

from tests.regression_tests import config, assert_atoms_equal, \
assert_reaction_rates_equal
assert_reaction_rates_equal, assert_same_mats


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -101,7 +101,7 @@ def test_against_self(run_in_tmpdir,
res_ref = openmc.deplete.Results(path_reference)

# Assert same mats
_assert_same_mats(res_test, res_ref)
assert_same_mats(res_ref, res_test)

tol = 1.0e-14
assert_atoms_equal(res_ref, res_test, tol)
Expand Down Expand Up @@ -155,7 +155,7 @@ def test_against_coupled(run_in_tmpdir,
res_ref = openmc.deplete.Results(path_reference)

# Assert same mats
_assert_same_mats(res_test, res_ref)
assert_same_mats(res_test, res_ref)

assert_atoms_equal(res_ref, res_test, atom_tol)
assert_reaction_rates_equal(res_ref, res_test, rx_tol)
Expand All @@ -172,6 +172,7 @@ def _create_operator(from_nuclides,
for nuc, dens in fuel.get_nuclide_atom_densities().items():
nuclides[nuc] = dens

openmc.reset_auto_ids()
op = IndependentOperator.from_nuclides(fuel.volume,
nuclides,
flux,
Expand All @@ -187,20 +188,3 @@ def _create_operator(from_nuclides,
normalization_mode=normalization_mode)

return op


def _assert_same_mats(res_ref, res_test):
for mat in res_ref[0].index_mat:
assert mat in res_test[0].index_mat, \
f"Material {mat} not in new results."
for nuc in res_ref[0].index_nuc:
assert nuc in res_test[0].index_nuc, \
f"Nuclide {nuc} not in new results."

for mat in res_test[0].index_mat:
assert mat in res_ref[0].index_mat, \
f"Material {mat} not in old results."
for nuc in res_test[0].index_nuc:
assert nuc in res_ref[0].index_nuc, \
f"Nuclide {nuc} not in old results."

6 changes: 4 additions & 2 deletions tests/regression_tests/deplete_with_transfer_rates/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
from openmc.deplete import CoupledOperator

from tests.regression_tests import config, assert_reaction_rates_equal, \
assert_atoms_equal
assert_atoms_equal, assert_same_mats


@pytest.fixture
def model():
openmc.reset_auto_ids()
f = openmc.Material(name="f")
f.add_element("U", 1, percent_type="ao", enrichment=4.25)
f.add_element("O", 2)
Expand Down Expand Up @@ -66,7 +67,7 @@ def test_transfer_rates(run_in_tmpdir, model, rate, dest_mat, power, ref_result)
integrator = openmc.deplete.PredictorIntegrator(
op, [1], power, timestep_units = 'd')
integrator.add_transfer_rate('f', transfer_elements, rate,
destination_material=dest_mat)
destination_material=dest_mat)
integrator.integrate()

# Get path to test and reference results
Expand All @@ -82,5 +83,6 @@ def test_transfer_rates(run_in_tmpdir, model, rate, dest_mat, power, ref_result)
res_ref = openmc.deplete.Results(path_reference)
res_test = openmc.deplete.Results(path_test)

assert_same_mats(res_ref, res_test)
assert_atoms_equal(res_ref, res_test, 1e-6)
assert_reaction_rates_equal(res_ref, res_test)
7 changes: 4 additions & 3 deletions tests/unit_tests/test_deplete_decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ def test_deplete_decay_products(run_in_tmpdir):

# Get concentration of H1 and He4
results = openmc.deplete.Results('depletion_results.h5')
_, h1 = results.get_atoms("1", "H1")
_, he4 = results.get_atoms("1", "He4")
mat_id = op.materials[0].id
_, h1 = results.get_atoms(f"{mat_id}", "H1")
_, he4 = results.get_atoms(f"{mat_id}", "He4")

# Since we started with 1e24 atoms of Li5, we should have 1e24 atoms of both
# H1 and He4
Expand Down Expand Up @@ -78,6 +79,6 @@ def test_deplete_decay_step_fissionable(run_in_tmpdir):

# Get concentration of U238. It should be unchanged since this chain has no U238 decay.
results = openmc.deplete.Results('depletion_results.h5')
_, u238 = results.get_atoms("1", "U238")
_, u238 = results.get_atoms(f"{mat.id}", "U238")

assert u238[1] == pytest.approx(original_atoms)
1 change: 1 addition & 0 deletions tests/unit_tests/test_deplete_transfer_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def model():
def test_get_set(model, case_name, transfer_rates):
"""Tests the get/set methods"""

openmc.reset_auto_ids()
op = CoupledOperator(model, CHAIN_PATH)
transfer = TransferRates(op, model)

Expand Down

0 comments on commit 1645e3b

Please sign in to comment.