Skip to content

Commit

Permalink
Fixes in MicroXS.from_multigroup_flux (#3192)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulromano authored Nov 11, 2024
1 parent c05132c commit c920779
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
5 changes: 3 additions & 2 deletions openmc/deplete/independent_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 '
Expand Down
10 changes: 6 additions & 4 deletions openmc/deplete/microxs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/test_deplete_decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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')
Expand Down
6 changes: 3 additions & 3 deletions tests/unit_tests/test_deplete_independent_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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\)"):
Expand Down
8 changes: 5 additions & 3 deletions tests/unit_tests/test_deplete_microxs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -96,9 +94,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
Expand Down

0 comments on commit c920779

Please sign in to comment.