Skip to content

Commit

Permalink
Fixing Universe.plot seed. (#2647)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Romano <[email protected]>
  • Loading branch information
pshriwise and paulromano authored Aug 19, 2023
1 parent 0964024 commit fa245b0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
10 changes: 9 additions & 1 deletion docs/source/io_formats/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,15 @@ either "false" or "true".

*Default*: false

-----------------------
``<plot_seed>`` Element
-----------------------

The ``<plot_seed>`` element is used to set the seed for the pseudorandom number
generator during generation of colors in plots.

*Default*: 1

---------------------
``<ptables>`` Element
---------------------
Expand Down Expand Up @@ -1175,4 +1184,3 @@ mesh-based weight windows.

The ``weight_windows_file`` element has no attributes and contains the path to
a weight windows HDF5 file to load during simulation initialization.

25 changes: 25 additions & 0 deletions openmc/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ class Settings:
Number of particles per generation
photon_transport : bool
Whether to use photon transport.
plot_seed : int
Initial seed for randomly generated plot colors.
ptables : bool
Determine whether probability tables are used.
resonance_scattering : dict
Expand Down Expand Up @@ -270,6 +272,7 @@ def __init__(self, **kwargs):
self._confidence_intervals = None
self._electron_treatment = None
self._photon_transport = None
self._plot_seed = None
self._ptables = None
self._seed = None
self._survival_biasing = None
Expand Down Expand Up @@ -504,6 +507,16 @@ def photon_transport(self, photon_transport: bool):
cv.check_type('photon transport', photon_transport, bool)
self._photon_transport = photon_transport

@property
def plot_seed(self):
return self._plot_seed

@plot_seed.setter
def plot_seed(self, seed):
cv.check_type('random plot color seed', seed, Integral)
cv.check_greater_than('random plot color seed', seed, 0)
self._plot_seed = seed

@property
def seed(self) -> int:
return self._seed
Expand Down Expand Up @@ -1118,6 +1131,11 @@ def _create_photon_transport_subelement(self, root):
element = ET.SubElement(root, "photon_transport")
element.text = str(self._photon_transport).lower()

def _create_plot_seed_subelement(self, root):
if self._plot_seed is not None:
element = ET.SubElement(root, "plot_seed")
element.text = str(self._plot_seed)

def _create_ptables_subelement(self, root):
if self._ptables is not None:
element = ET.SubElement(root, "ptables")
Expand Down Expand Up @@ -1491,6 +1509,11 @@ def _photon_transport_from_xml_element(self, root):
if text is not None:
self.photon_transport = text in ('true', '1')

def _plot_seed_from_xml_element(self, root):
text = get_text(root, 'plot_seed')
if text is not None:
self.plot_seed = int(text)

def _ptables_from_xml_element(self, root):
text = get_text(root, 'ptables')
if text is not None:
Expand Down Expand Up @@ -1706,6 +1729,7 @@ def to_xml_element(self, mesh_memo=None):
self._create_energy_mode_subelement(element)
self._create_max_order_subelement(element)
self._create_photon_transport_subelement(element)
self._create_plot_seed_subelement(element)
self._create_ptables_subelement(element)
self._create_seed_subelement(element)
self._create_survival_biasing_subelement(element)
Expand Down Expand Up @@ -1802,6 +1826,7 @@ def from_xml_element(cls, elem, meshes=None):
settings._energy_mode_from_xml_element(elem)
settings._max_order_from_xml_element(elem)
settings._photon_transport_from_xml_element(elem)
settings._plot_seed_from_xml_element(elem)
settings._ptables_from_xml_element(elem)
settings._seed_from_xml_element(elem)
settings._survival_biasing_from_xml_element(elem)
Expand Down
2 changes: 1 addition & 1 deletion openmc/universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def plot(self, origin=None, width=None, pixels=40000,
model = openmc.Model()
model.geometry = openmc.Geometry(self)
if seed is not None:
model.settings.seed = seed
model.settings.plot_seed = seed

# Determine whether any materials contains macroscopic data and if
# so, set energy mode accordingly
Expand Down
7 changes: 7 additions & 0 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "openmc/mesh.h"
#include "openmc/message_passing.h"
#include "openmc/output.h"
#include "openmc/plot.h"
#include "openmc/random_lcg.h"
#include "openmc/simulation.h"
#include "openmc/source.h"
Expand Down Expand Up @@ -390,6 +391,12 @@ void read_settings_xml(pugi::xml_node root)
}
}

// Copy plotting random number seed if specified
if (check_for_node(root, "plot_seed")) {
auto seed = std::stoll(get_node_value(root, "plot_seed"));
model::plotter_seed = seed;
}

// Copy random number seed if specified
if (check_for_node(root, "seed")) {
auto seed = std::stoll(get_node_value(root, "seed"));
Expand Down
4 changes: 3 additions & 1 deletion tests/unit_tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def test_export_to_xml(run_in_tmpdir):
s.surf_source_write = {'surface_ids': [2], 'max_particles': 200}
s.confidence_intervals = True
s.ptables = True
s.plot_seed = 100
s.survival_biasing = True
s.cutoff = {'weight': 0.25, 'weight_avg': 0.5, 'energy_neutron': 1.0e-5,
'energy_photon': 1000.0, 'energy_electron': 1.0e-5,
Expand Down Expand Up @@ -82,6 +83,7 @@ def test_export_to_xml(run_in_tmpdir):
assert s.surf_source_write == {'surface_ids': [2], 'max_particles': 200}
assert s.confidence_intervals
assert s.ptables
assert s.plot_seed == 100
assert s.seed == 17
assert s.survival_biasing
assert s.cutoff == {'weight': 0.25, 'weight_avg': 0.5,
Expand All @@ -108,7 +110,7 @@ def test_export_to_xml(run_in_tmpdir):
'energy_min': 1.0, 'energy_max': 1000.0,
'nuclides': ['U235', 'U238', 'Pu239']}
assert s.create_fission_neutrons
assert not s.create_delayed_neutrons
assert not s.create_delayed_neutrons
assert s.log_grid_bins == 2000
assert not s.photon_transport
assert s.electron_treatment == 'led'
Expand Down

0 comments on commit fa245b0

Please sign in to comment.