From 4ef1faf766818a7c01569386d27451774406d542 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Fri, 16 Aug 2024 11:33:59 -0500 Subject: [PATCH] Add delta_function convenience function (#3090) --- docs/source/pythonapi/stats.rst | 1 + openmc/stats/univariate.py | 21 +++++++++++++++++++++ tests/unit_tests/test_stats.py | 7 +++++++ 3 files changed, 29 insertions(+) diff --git a/docs/source/pythonapi/stats.rst b/docs/source/pythonapi/stats.rst index ffb4fcda2ba..b72896c1860 100644 --- a/docs/source/pythonapi/stats.rst +++ b/docs/source/pythonapi/stats.rst @@ -28,6 +28,7 @@ Univariate Probability Distributions :nosignatures: :template: myfunction.rst + openmc.stats.delta_function openmc.stats.muir Angular Distributions diff --git a/openmc/stats/univariate.py b/openmc/stats/univariate.py index a47f4f32030..10822c06fa7 100644 --- a/openmc/stats/univariate.py +++ b/openmc/stats/univariate.py @@ -312,6 +312,27 @@ def clip(self, tolerance: float = 1e-6, inplace: bool = False) -> Discrete: return type(self)(new_x, new_p) +def delta_function(value: float, intensity: float = 1.0) -> Discrete: + """Return a discrete distribution with a single point. + + .. versionadded:: 0.15.1 + + Parameters + ---------- + value : float + Value of the random variable. + intensity : float, optional + When used for an energy distribution, this can be used to assign an + intensity. + + Returns + ------- + Discrete distribution with a single point + + """ + return Discrete([value], [intensity]) + + class Uniform(Univariate): """Distribution with constant probability over a finite interval [a,b] diff --git a/tests/unit_tests/test_stats.py b/tests/unit_tests/test_stats.py index 5451c0a2cac..0414fc22559 100644 --- a/tests/unit_tests/test_stats.py +++ b/tests/unit_tests/test_stats.py @@ -50,6 +50,13 @@ def test_discrete(): assert_sample_mean(samples, exp_mean) +def test_delta_function(): + d = openmc.stats.delta_function(14.1e6) + assert isinstance(d, openmc.stats.Discrete) + np.testing.assert_array_equal(d.x, [14.1e6]) + np.testing.assert_array_equal(d.p, [1.0]) + + def test_merge_discrete(): x1 = [0.0, 1.0, 10.0] p1 = [0.3, 0.2, 0.5]