From 160b16da0f0eff2fe926596749157fd63ed307f8 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Sat, 9 Nov 2024 10:36:52 -0600 Subject: [PATCH 1/3] Ensure MicroXS.from_multigroup_flux creates 3D data array --- openmc/deplete/microxs.py | 10 ++++++---- tests/unit_tests/test_deplete_microxs.py | 4 ++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/openmc/deplete/microxs.py b/openmc/deplete/microxs.py index 5be9875f304..bbe9b2a4322 100644 --- a/openmc/deplete/microxs.py +++ b/openmc/deplete/microxs.py @@ -198,6 +198,8 @@ class MicroXS: """ def __init__(self, data: np.ndarray, nuclides: list[str], reactions: list[str]): # Validate inputs + if len(data.shape) != 3: + raise ValueError('Data array must be 3D.') if data.shape[:2] != (len(nuclides), len(reactions)): raise ValueError( f'Nuclides list of length {len(nuclides)} and ' @@ -291,11 +293,11 @@ def from_multigroup_flux( mts = [REACTION_MT[name] for name in reactions] # Normalize multigroup flux - multigroup_flux = np.asarray(multigroup_flux) + multigroup_flux = np.array(multigroup_flux) multigroup_flux /= multigroup_flux.sum() - # Create 2D array for microscopic cross sections - microxs_arr = np.zeros((len(nuclides), len(mts))) + # Create 3D array for microscopic cross sections + microxs_arr = np.zeros((len(nuclides), len(mts), 1)) # Create a material with all nuclides mat_all_nucs = openmc.Material() @@ -327,7 +329,7 @@ def from_multigroup_flux( xs = lib_nuc.collapse_rate( mt, temperature, energies, multigroup_flux ) - microxs_arr[nuc_index, mt_index] = xs + microxs_arr[nuc_index, mt_index, 0] = xs return cls(microxs_arr, nuclides, reactions) diff --git a/tests/unit_tests/test_deplete_microxs.py b/tests/unit_tests/test_deplete_microxs.py index ad54026f014..18a4b077a0a 100644 --- a/tests/unit_tests/test_deplete_microxs.py +++ b/tests/unit_tests/test_deplete_microxs.py @@ -96,9 +96,13 @@ def test_multigroup_flux_same(): energies = [0., 6.25e-1, 5.53e3, 8.21e5, 2.e7] flux_per_ev = [0.3, 0.3, 1.0, 1.0] flux = flux_per_ev * np.diff(energies) + flux_sum = flux.sum() microxs_4g = MicroXS.from_multigroup_flux( energies=energies, multigroup_flux=flux, chain_file=chain_file) + # from_multigroup_flux should not modify the flux + assert flux.sum() == flux_sum + # Generate micro XS based on 2-group flux, where the boundaries line up with # the 4 group flux and have the same flux per eV across the full energy # range From c89c89f707762e521efd1839e1ad3173c36c3375 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Sat, 9 Nov 2024 10:42:24 -0600 Subject: [PATCH 2/3] Allow IndependentOperator to accept list of materials --- openmc/deplete/independent_operator.py | 5 +++-- tests/unit_tests/test_deplete_independent_operator.py | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/openmc/deplete/independent_operator.py b/openmc/deplete/independent_operator.py index cc1a05bd99f..226682ffa53 100644 --- a/openmc/deplete/independent_operator.py +++ b/openmc/deplete/independent_operator.py @@ -44,7 +44,7 @@ class IndependentOperator(OpenMCOperator): Parameters ---------- - materials : openmc.Materials + materials : iterable of openmc.Material Materials to deplete. fluxes : list of numpy.ndarray Flux in each group in [n-cm/src] for each domain @@ -127,8 +127,9 @@ def __init__(self, reduce_chain_level=None, fission_yield_opts=None): # Validate micro-xs parameters - check_type('materials', materials, openmc.Materials) + check_type('materials', materials, Iterable, openmc.Material) check_type('micros', micros, Iterable, MicroXS) + materials = openmc.Materials(materials) if not (len(fluxes) == len(micros) == len(materials)): msg = (f'The length of fluxes ({len(fluxes)}) should be equal to ' diff --git a/tests/unit_tests/test_deplete_independent_operator.py b/tests/unit_tests/test_deplete_independent_operator.py index 9129cf0642f..c765d065009 100644 --- a/tests/unit_tests/test_deplete_independent_operator.py +++ b/tests/unit_tests/test_deplete_independent_operator.py @@ -6,7 +6,7 @@ import pytest -from openmc import Material, Materials +from openmc import Material from openmc.deplete import IndependentOperator, MicroXS CHAIN_PATH = Path(__file__).parents[1] / "chain_simple.xml" @@ -34,7 +34,7 @@ def test_operator_init(): fuel.set_density("g/cc", 10.4) fuel.depletable = True fuel.volume = 1 - materials = Materials([fuel]) + materials = [fuel] fluxes = [1.0] micros = [micro_xs] IndependentOperator(materials, fluxes, micros, CHAIN_PATH) @@ -47,7 +47,7 @@ def test_error_handling(): fuel.set_density("g/cc", 1) fuel.depletable = True fuel.volume = 1 - materials = Materials([fuel]) + materials = [fuel] fluxes = [1.0, 2.0] micros = [micro_xs] with pytest.raises(ValueError, match=r"The length of fluxes \(2\)"): From d5fca183bb34110b502f3317ea3c37abd6b3156d Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Sat, 9 Nov 2024 16:59:45 -0600 Subject: [PATCH 3/3] Fix failing tests --- tests/unit_tests/test_deplete_decay.py | 4 ++-- tests/unit_tests/test_deplete_microxs.py | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/unit_tests/test_deplete_decay.py b/tests/unit_tests/test_deplete_decay.py index cebbc16fa79..6e7b0b101ec 100644 --- a/tests/unit_tests/test_deplete_decay.py +++ b/tests/unit_tests/test_deplete_decay.py @@ -20,7 +20,7 @@ def test_deplete_decay_products(run_in_tmpdir): """) # Create MicroXS object with no cross sections - micro_xs = openmc.deplete.MicroXS(np.empty((0, 0)), [], []) + micro_xs = openmc.deplete.MicroXS(np.empty((0, 0, 0)), [], []) # Create depletion operator with no reactions op = openmc.deplete.IndependentOperator.from_nuclides( @@ -59,7 +59,7 @@ def test_deplete_decay_step_fissionable(run_in_tmpdir): """ # Set up a pure decay operator - micro_xs = openmc.deplete.MicroXS(np.empty((0, 0)), [], []) + micro_xs = openmc.deplete.MicroXS(np.empty((0, 0, 0)), [], []) mat = openmc.Material() mat.name = 'I do not decay.' mat.add_nuclide('U238', 1.0, 'ao') diff --git a/tests/unit_tests/test_deplete_microxs.py b/tests/unit_tests/test_deplete_microxs.py index 18a4b077a0a..073b3f162d1 100644 --- a/tests/unit_tests/test_deplete_microxs.py +++ b/tests/unit_tests/test_deplete_microxs.py @@ -46,9 +46,7 @@ def test_from_array(): data.shape = (12, 2, 1) MicroXS(data, nuclides, reactions) - with pytest.raises(ValueError, match=r'Nuclides list of length \d* and ' - r'reactions array of length \d* do not ' - r'match dimensions of data array of shape \(\d*\, \d*\)'): + with pytest.raises(ValueError, match='Data array must be 3D'): MicroXS(data[:, 0], nuclides, reactions)