Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions docs/source/pythonapi/capi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Functions
next_batch
num_realizations
plot_geometry
prn
property_map
reset
reset_timers
Expand Down
8 changes: 8 additions & 0 deletions include/openmc/random_lcg.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,13 @@ extern "C" uint64_t openmc_get_stride();

extern "C" void openmc_set_stride(uint64_t new_stride);

//==============================================================================
//! Generate a pseudo-random number using OpenMC's RNG.
//! @param seed Pseudorandom number seed pointer
//! @return A random number between 0 and 1
//==============================================================================

extern "C" double openmc_prn(uint64_t* seed);

} // namespace openmc
#endif // OPENMC_RANDOM_LCG_H
2 changes: 1 addition & 1 deletion openmc/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def _uwuw_enabled():
from .mesh import *
from .filter import *
from .tally import *
from .settings import settings
from .settings import settings, prn
from .math import *
from .plot import *
from .weight_windows import *
Expand Down
23 changes: 22 additions & 1 deletion openmc/lib/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ctypes import c_int, c_int32, c_int64, c_double, c_char_p, c_bool, POINTER
from ctypes import c_int, c_int32, c_int64, c_double, c_char_p, c_bool, POINTER, c_uint64, byref
Comment thread
shimwell marked this conversation as resolved.
Outdated

from . import _dll
from .core import _DLLGlobal
Expand All @@ -14,6 +14,8 @@
_dll.openmc_get_seed.restype = c_int64
_dll.openmc_set_stride.argtypes = [c_int64]
_dll.openmc_get_stride.restype = c_int64
_dll.openmc_prn.argtypes = [POINTER(c_uint64)]
_dll.openmc_prn.restype = c_double
_dll.openmc_get_n_batches.argtypes = [POINTER(c_int), c_bool]
_dll.openmc_get_n_batches.restype = c_int
_dll.openmc_get_n_batches.errcheck = _error_handler
Expand Down Expand Up @@ -116,4 +118,23 @@ def get_batches(self, get_max_batches=True):
return n_batches.value


def prn(seed):
"""Generate a pseudo-random number using OpenMC's RNG

Parameters
----------
seed : int
Pseudorandom number seed (will be modified in place)

Returns
-------
tuple of (float, int)
Random number between 0 and 1, and the updated seed

"""
c_seed = c_uint64(seed)
random_value = _dll.openmc_prn(byref(c_seed))
return random_value, c_seed.value


settings = _Settings()
5 changes: 5 additions & 0 deletions src/random_lcg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,9 @@ extern "C" void openmc_set_stride(uint64_t new_stride)
prn_stride = new_stride;
}

extern "C" double openmc_prn(uint64_t* seed)
{
return prn(seed);
}

} // namespace openmc
23 changes: 23 additions & 0 deletions tests/unit_tests/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,3 +1045,26 @@ def test_sample_external_source(run_in_tmpdir, mpi_intracomm):
openmc.lib.init(["-c"])
openmc.lib.sample_external_source(100)
openmc.lib.finalize()


def test_prn(pincell_model, mpi_intracomm):
openmc.lib.init()

seed = 12345
random_numbers = []
for _ in range(10):
random_value, seed = openmc.lib.prn(seed)
random_numbers.append(random_value)
# Check that random values are in [0, 1)
assert 0.0 <= random_value < 1.0

# Check that we got different values (not all the same)
assert len(set(random_numbers)) > 1

# Check that using the same initial seed produces the same sequence
seed2 = 12345
for expected_value in random_numbers:
random_value, seed2 = openmc.lib.prn(seed2)
assert random_value == pytest.approx(expected_value)

openmc.lib.finalize()
Loading