From 107aa5841f7fc9fa615ce6bee431194d8a9a47e0 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Thu, 14 Nov 2024 14:45:33 -0600 Subject: [PATCH 1/6] Apply weight windows at collisions for multigroup transport if specified --- src/physics_mg.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/physics_mg.cpp b/src/physics_mg.cpp index 361cf5affcf..3cc0532d2b1 100644 --- a/src/physics_mg.cpp +++ b/src/physics_mg.cpp @@ -19,6 +19,7 @@ #include "openmc/settings.h" #include "openmc/simulation.h" #include "openmc/tallies/tally.h" +#include "openmc/weight_windows.h" namespace openmc { @@ -27,6 +28,9 @@ void collision_mg(Particle& p) // Add to the collision counter for the particle p.n_collision()++; + if (settings::weight_window_checkpoint_collision) + apply_weight_windows(p); + // Sample the reaction type sample_reaction(p); From 06a8dad7dc2b2e5a0a96b1497d2021a778f1dbd9 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 19 Nov 2024 23:27:53 -0600 Subject: [PATCH 2/6] Adding a test and multigroup wws --- tests/unit_tests/weightwindows/test_ww_mg.py | 63 ++++++++++++++++++++ tests/unit_tests/weightwindows/ww_mg.txt | 27 +++++++++ 2 files changed, 90 insertions(+) create mode 100644 tests/unit_tests/weightwindows/test_ww_mg.py create mode 100644 tests/unit_tests/weightwindows/ww_mg.txt diff --git a/tests/unit_tests/weightwindows/test_ww_mg.py b/tests/unit_tests/weightwindows/test_ww_mg.py new file mode 100644 index 00000000000..65fcfac0e36 --- /dev/null +++ b/tests/unit_tests/weightwindows/test_ww_mg.py @@ -0,0 +1,63 @@ +import pytest + +import numpy as np +import openmc + + +def test_weight_windows_mg(run_in_tmpdir): + model = openmc.examples.random_ray_three_region_cube() + + mesh = openmc.RegularMesh.from_domain(model.geometry, (3, 3, 3)) + mesh_tally = openmc.Tally() + mesh_tally.filters = [openmc.MeshFilter(mesh)] + mesh_tally.scores = ['flux'] + model.tallies = [mesh_tally] + + settings = openmc.Settings() + settings.particles = 1000 + settings.batches = 10 + settings.energy_mode = 'multi-group' + settings.run_mode = 'fixed source' + space = openmc.stats.Point((1, 1, 1)) + energy = openmc.stats.delta_function(1e6) + source = openmc.IndependentSource(space=space) + settings.source = source + + model.settings = settings + statepoint = model.run() + + with openmc.StatePoint(statepoint) as sp: + tally_out = sp.get_tally(id=mesh_tally.id) + flux_analog = tally_out.mean + + # wwg = openmc.WeightWindowGenerator(mesh) + # model.settings.weight_window_generators = wwg + # model.settings.weight_window_checkpoints = {'surface': True, 'collision': True} + # model.settings.survival_biasing = False + + # statepoint = model.run() + + ww_lower_bnds = np.loadtxt('ww_mg.txt') + weight_windows = openmc.WeightWindows(mesh, lower_ww_bounds=ww_lower_bnds, upper_bound_ratio=5.0) + model.settings.weight_windows = weight_windows + model.settings.weight_windows_on = True + + # weight_windows = openmc.hdf5_to_wws('weight_windows.h5') + # for ww in weight_windows: + # ww.lower_ww_bounds *= 0.1 + # ww.upper_ww_bounds *= 0.1 + + # np.savetxt('ww_mg.txt', weight_windows[0].lower_ww_bounds.flatten()) + + settings.weight_windows = weight_windows + + statepoint = model.run() + + with openmc.StatePoint(statepoint) as sp: + tally_out = sp.get_tally(id=mesh_tally.id) + flux_ww = tally_out.mean + + print(flux_analog.sum()) + print(flux_ww.sum()) + assert np.allclose(flux_analog.sum(), flux_ww.sum(), rtol=1e-2) + diff --git a/tests/unit_tests/weightwindows/ww_mg.txt b/tests/unit_tests/weightwindows/ww_mg.txt new file mode 100644 index 00000000000..7bcc59d97b5 --- /dev/null +++ b/tests/unit_tests/weightwindows/ww_mg.txt @@ -0,0 +1,27 @@ +5.000000000000000278e-02 +1.023184121346435924e-02 +3.006624096325660397e-03 +1.030178532774538719e-02 +6.058877789444589400e-03 +2.216914234856166583e-03 +3.061186967456217597e-03 +2.148267952185671601e-03 +1.026171712186230980e-03 +1.040022203443005666e-02 +6.040633813799485378e-03 +2.364143118752318716e-03 +6.119726639841410569e-03 +4.329097093078606955e-03 +1.873104469085542763e-03 +2.246957229279350661e-03 +1.851165248260521617e-03 +7.825824911598703530e-04 +3.021300894848398706e-03 +2.286420236345795311e-03 +9.318583473482396160e-04 +2.234702678114806364e-03 +1.813664566119888152e-03 +7.969287848384389462e-04 +1.017895970086981662e-03 +7.707144532950136471e-04 +3.386087166633241791e-04 From 92390cfb0607c3dfc17e91e3d4a3ee03c1fa6f1b Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 19 Nov 2024 23:33:46 -0600 Subject: [PATCH 3/6] Test function cleanup --- tests/unit_tests/weightwindows/test_ww_mg.py | 34 +++++++------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/tests/unit_tests/weightwindows/test_ww_mg.py b/tests/unit_tests/weightwindows/test_ww_mg.py index 65fcfac0e36..97c0c77a3aa 100644 --- a/tests/unit_tests/weightwindows/test_ww_mg.py +++ b/tests/unit_tests/weightwindows/test_ww_mg.py @@ -4,17 +4,20 @@ import openmc -def test_weight_windows_mg(run_in_tmpdir): +def test_weight_windows_mg(request, run_in_tmpdir): + # import basic random ray model model = openmc.examples.random_ray_three_region_cube() + # create a mesh tally mesh = openmc.RegularMesh.from_domain(model.geometry, (3, 3, 3)) mesh_tally = openmc.Tally() mesh_tally.filters = [openmc.MeshFilter(mesh)] mesh_tally.scores = ['flux'] model.tallies = [mesh_tally] + # replace random ray settings with fixed source settings settings = openmc.Settings() - settings.particles = 1000 + settings.particles = 5000 settings.batches = 10 settings.energy_mode = 'multi-group' settings.run_mode = 'fixed source' @@ -26,38 +29,25 @@ def test_weight_windows_mg(run_in_tmpdir): model.settings = settings statepoint = model.run() + # extract flux from analog simulation with openmc.StatePoint(statepoint) as sp: tally_out = sp.get_tally(id=mesh_tally.id) flux_analog = tally_out.mean - # wwg = openmc.WeightWindowGenerator(mesh) - # model.settings.weight_window_generators = wwg - # model.settings.weight_window_checkpoints = {'surface': True, 'collision': True} - # model.settings.survival_biasing = False - - # statepoint = model.run() - - ww_lower_bnds = np.loadtxt('ww_mg.txt') + # load the weight windows for this problem, apply them, and re-run + ww_lower_bnds = np.loadtxt(request.path.parent / 'ww_mg.txt') weight_windows = openmc.WeightWindows(mesh, lower_ww_bounds=ww_lower_bnds, upper_bound_ratio=5.0) model.settings.weight_windows = weight_windows model.settings.weight_windows_on = True - - # weight_windows = openmc.hdf5_to_wws('weight_windows.h5') - # for ww in weight_windows: - # ww.lower_ww_bounds *= 0.1 - # ww.upper_ww_bounds *= 0.1 - - # np.savetxt('ww_mg.txt', weight_windows[0].lower_ww_bounds.flatten()) - settings.weight_windows = weight_windows statepoint = model.run() - with openmc.StatePoint(statepoint) as sp: tally_out = sp.get_tally(id=mesh_tally.id) flux_ww = tally_out.mean - print(flux_analog.sum()) - print(flux_ww.sum()) - assert np.allclose(flux_analog.sum(), flux_ww.sum(), rtol=1e-2) + # the sum of the fluxes should approach the same value + analog_sum = flux_analog.sum() + ww_sum = flux_ww.sum() + assert np.allclose(analog_sum, ww_sum, rtol=1e-2) From 466a06779f2e677f327a7041ba7f5bf03557c430 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 19 Nov 2024 23:34:58 -0600 Subject: [PATCH 4/6] Move weight window application after reaction sampling --- src/physics_mg.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/physics_mg.cpp b/src/physics_mg.cpp index 3cc0532d2b1..e967afd7995 100644 --- a/src/physics_mg.cpp +++ b/src/physics_mg.cpp @@ -28,12 +28,12 @@ void collision_mg(Particle& p) // Add to the collision counter for the particle p.n_collision()++; - if (settings::weight_window_checkpoint_collision) - apply_weight_windows(p); - // Sample the reaction type sample_reaction(p); + if (settings::weight_window_checkpoint_collision) + apply_weight_windows(p); + // Display information about collision if ((settings::verbosity >= 10) || p.trace()) { write_message(fmt::format(" Energy Group = {}", p.g()), 1); From f40473c58812f440f1b2c17d577c9a14051abdf9 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 20 Nov 2024 08:42:24 -0600 Subject: [PATCH 5/6] Improving comments --- tests/unit_tests/weightwindows/test_ww_mg.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/unit_tests/weightwindows/test_ww_mg.py b/tests/unit_tests/weightwindows/test_ww_mg.py index 97c0c77a3aa..52e0ff82f86 100644 --- a/tests/unit_tests/weightwindows/test_ww_mg.py +++ b/tests/unit_tests/weightwindows/test_ww_mg.py @@ -26,6 +26,7 @@ def test_weight_windows_mg(request, run_in_tmpdir): source = openmc.IndependentSource(space=space) settings.source = source + # perform analog simulation model.settings = settings statepoint = model.run() @@ -34,19 +35,20 @@ def test_weight_windows_mg(request, run_in_tmpdir): tally_out = sp.get_tally(id=mesh_tally.id) flux_analog = tally_out.mean - # load the weight windows for this problem, apply them, and re-run + # load the weight windows for this problem and apply them ww_lower_bnds = np.loadtxt(request.path.parent / 'ww_mg.txt') weight_windows = openmc.WeightWindows(mesh, lower_ww_bounds=ww_lower_bnds, upper_bound_ratio=5.0) model.settings.weight_windows = weight_windows model.settings.weight_windows_on = True settings.weight_windows = weight_windows + # re-run with weight windows statepoint = model.run() with openmc.StatePoint(statepoint) as sp: tally_out = sp.get_tally(id=mesh_tally.id) flux_ww = tally_out.mean - # the sum of the fluxes should approach the same value + # the sum of the fluxes should approach the same value (no bias introduced) analog_sum = flux_analog.sum() ww_sum = flux_ww.sum() assert np.allclose(analog_sum, ww_sum, rtol=1e-2) From e5472b7ff1d264973b10a1cecc8e668df72dce98 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 20 Nov 2024 10:30:24 -0600 Subject: [PATCH 6/6] add tmate --- .github/workflows/ci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5973c3cd48d..76ccba8991c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -153,6 +153,10 @@ jobs: CTEST_OUTPUT_ON_FAILURE=1 make test -C $GITHUB_WORKSPACE/build/ $GITHUB_WORKSPACE/tools/ci/gha-script.sh + - name: Setup tmate session + if: ${{ failure() }} + uses: mxschmitt/action-tmate@v3 + - name: after_success shell: bash run: |