Skip to content

Commit

Permalink
Add delta_function convenience function (openmc-dev#3090)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulromano authored Aug 16, 2024
1 parent 54c28b7 commit 4ef1faf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/source/pythonapi/stats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Univariate Probability Distributions
:nosignatures:
:template: myfunction.rst

openmc.stats.delta_function
openmc.stats.muir

Angular Distributions
Expand Down
21 changes: 21 additions & 0 deletions openmc/stats/univariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 7 additions & 0 deletions tests/unit_tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 4ef1faf

Please sign in to comment.