Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support track file writing for particle restart runs. #2957

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/particle_restart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ void run_particle_restart()
read_particle_restart(p, previous_run_mode);

// write track if that was requested on command line
if (settings::write_all_tracks)
if (settings::write_all_tracks) {
open_track_file();
p.write_track() = true;
}

// Set all tallies to 0 for now (just tracking errors)
model::tallies.clear();
Expand Down Expand Up @@ -123,6 +125,10 @@ void run_particle_restart()

// Write output if particle made it
print_particle(p);

if (settings::write_all_tracks) {
close_track_file();
}
}

} // namespace openmc
33 changes: 33 additions & 0 deletions tests/unit_tests/test_tracks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pathlib import Path

import h5py
import numpy as np
import openmc
import pytest
Expand Down Expand Up @@ -157,3 +158,35 @@ def test_write_to_vtk(sphere_model):

assert isinstance(polydata, vtk.vtkPolyData)
assert Path('tracks.vtp').is_file()


def test_restart_track(run_in_tmpdir, sphere_model):
# cut the sphere model in half with an improper boundary condition
plane = openmc.XPlane(x0=-1.0)
for cell in sphere_model.geometry.get_all_cells().values():
cell.region &= +plane

# generate lost particle files
with pytest.raises(RuntimeError, match='Maximum number of lost particles has been reached.'):
sphere_model.run(output=False)

lost_particle_files = list(Path.cwd().glob('particle_*.h5'))
assert len(lost_particle_files) > 0
particle_file = lost_particle_files[0]
# restart the lost particle with tracks enabled
sphere_model.run(tracks=True, restart_file=particle_file)
tracks_file = Path('tracks.h5')
assert tracks_file.is_file()

# check that the last track of the file matches the lost particle file
tracks = openmc.Tracks(tracks_file)
initial_state = tracks[0].particle_tracks[0].states[0]
restart_r = np.array(initial_state['r'])
restart_u = np.array(initial_state['u'])

with h5py.File(particle_file, 'r') as lost_particle_file:
lost_r = np.array(lost_particle_file['xyz'][()])
lost_u = np.array(lost_particle_file['uvw'][()])

pytest.approx(restart_r, lost_r)
pytest.approx(restart_u, lost_u)