From 8ecfdf079675cb694e524614536740ee04387f55 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Thu, 19 May 2022 14:15:37 -0500 Subject: [PATCH] Update Settings.track to be list of 3-tuple --- openmc/settings.py | 25 +++++++++++++------------ openmc/trackfile.py | 6 +++--- tests/unit_tests/test_settings.py | 4 ++-- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/openmc/settings.py b/openmc/settings.py index d11a577d1..8d8e2eba9 100644 --- a/openmc/settings.py +++ b/openmc/settings.py @@ -2,6 +2,7 @@ import os import typing # imported separately as py3.8 requires typing.Iterable from collections.abc import Iterable, Mapping, MutableSequence from enum import Enum +import itertools from math import ceil from numbers import Integral, Real from pathlib import Path @@ -187,7 +188,7 @@ class Settings: integers: the batch number, generation number, and particle number track : tuple or list Specify particles for which track files should be written. Each particle - is identified by a triplet with the batch number, generation number, and + is identified by a tuple with the batch number, generation number, and particle number. trigger_active : bool Indicate whether tally triggers are used @@ -776,16 +777,15 @@ class Settings: self._trace = trace @track.setter - def track(self, track: typing.Iterable[int]): - cv.check_type('track', track, Iterable, Integral) - if len(track) % 3 != 0: - msg = f'Unable to set the track to "{track}" since its length is ' \ - 'not a multiple of 3' - raise ValueError(msg) - for t in zip(track[::3], track[1::3], track[2::3]): + def track(self, track: typing.Iterable[typing.Iterable[int]]): + cv.check_type('track', track, Iterable) + for t in track: + if len(t) != 3: + msg = f'Unable to set the track to "{t}" since its length is not 3' + raise ValueError(msg) cv.check_greater_than('track batch', t[0], 0) - cv.check_greater_than('track generation', t[0], 0) - cv.check_greater_than('track particle', t[0], 0) + cv.check_greater_than('track generation', t[1], 0) + cv.check_greater_than('track particle', t[2], 0) self._track = track @ufs_mesh.setter @@ -1110,7 +1110,7 @@ class Settings: def _create_track_subelement(self, root): if self._track is not None: element = ET.SubElement(root, "track") - element.text = ' '.join(map(str, self._track)) + element.text = ' '.join(map(str, itertools.chain(*self._track))) def _create_ufs_mesh_subelement(self, root): if self.ufs_mesh is not None: @@ -1424,7 +1424,8 @@ class Settings: def _track_from_xml_element(self, root): text = get_text(root, 'track') if text is not None: - self.track = [int(x) for x in text.split()] + values = [int(x) for x in text.split()] + self.track = list(zip(values[::3], values[1::3], values[2::3])) def _ufs_mesh_from_xml_element(self, root): text = get_text(root, 'ufs_mesh') diff --git a/openmc/trackfile.py b/openmc/trackfile.py index 51527818c..8c2d02cd1 100644 --- a/openmc/trackfile.py +++ b/openmc/trackfile.py @@ -84,9 +84,9 @@ class Track: @property def sources(self): sources = [] - for track_history in self.tracks: - particle_type = ParticleType(track_history.particle) - state = track_history.states[0] + for particle_track in self.particles: + particle_type = ParticleType(particle_track.particle) + state = particle_track.states[0] sources.append( SourceParticle( r=state['r'], u=state['u'], E=state['E'], diff --git a/tests/unit_tests/test_settings.py b/tests/unit_tests/test_settings.py index e2c7259e7..72b930b29 100644 --- a/tests/unit_tests/test_settings.py +++ b/tests/unit_tests/test_settings.py @@ -42,7 +42,7 @@ def test_export_to_xml(run_in_tmpdir): s.temperature = {'default': 293.6, 'method': 'interpolation', 'multipole': True, 'range': (200., 1000.)} s.trace = (10, 1, 20) - s.track = [1, 1, 1, 2, 1, 1] + s.track = [(1, 1, 1), (2, 1, 1)] s.ufs_mesh = mesh s.resonance_scattering = {'enable': True, 'method': 'rvs', 'energy_min': 1.0, 'energy_max': 1000.0, @@ -99,7 +99,7 @@ def test_export_to_xml(run_in_tmpdir): assert s.temperature == {'default': 293.6, 'method': 'interpolation', 'multipole': True, 'range': [200., 1000.]} assert s.trace == [10, 1, 20] - assert s.track == [1, 1, 1, 2, 1, 1] + assert s.track == [(1, 1, 1), (2, 1, 1)] assert isinstance(s.ufs_mesh, openmc.RegularMesh) assert s.ufs_mesh.lower_left == [-10., -10., -10.] assert s.ufs_mesh.upper_right == [10., 10., 10.]