Update Settings.track to be list of 3-tuple

This commit is contained in:
Paul Romano 2022-05-19 14:15:37 -05:00
parent e2de81045f
commit 8ecfdf0796
3 changed files with 18 additions and 17 deletions

View file

@ -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')

View file

@ -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'],

View file

@ -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.]