Skip to content

Commit a534d2b

Browse files
committed
adding sphere_vol2rad utility function with test
1 parent 90db128 commit a534d2b

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

src/pypartmc.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ PYBIND11_MODULE(_PyPartMC, m) {
196196
"Return the least power-of-2 that is at least equal to n."
197197
);
198198

199+
m.def(
200+
"sphere_vol2rad", &sphere_vol2rad, py::return_value_policy::copy,
201+
"Convert mass-equivalent volume (m^3) to geometric radius (m) for spherical particles."
202+
);
203+
199204
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
200205

201206
m.attr("__all__") = py::make_tuple(
@@ -212,6 +217,7 @@ PYBIND11_MODULE(_PyPartMC, m) {
212217
"run_part",
213218
"pow2_above",
214219
"histogram_1d",
215-
"histogram_2d"
220+
"histogram_2d",
221+
"sphere_vol2rad"
216222
);
217223
}

src/util.F90

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,12 @@ subroutine py_pow2_above(n, res) bind(C)
1919
res = pow2_above(n)
2020
end subroutine
2121

22+
subroutine f_sphere_vol2rad(v, rad) bind(C)
23+
real(c_double), intent(in) :: v
24+
real(c_double), intent(out) :: rad
25+
26+
rad = sphere_vol2rad(v)
27+
28+
end subroutine
29+
2230
end module

src/util.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@
77
#pragma once
88

99
extern "C" void py_pow2_above(int*, int*);
10+
extern "C" void f_sphere_vol2rad(const double*, double*);
1011

1112
auto pow2_above(int n) {
1213
int res;
1314
py_pow2_above(&n, &res);
1415
return res;
1516
}
1617

18+
double sphere_vol2rad(double v) {
19+
double rad;
20+
f_sphere_vol2rad(&v, &rad);
21+
return rad;
22+
}
23+
1724
extern "C" double py_deg2rad(double);
1825

tests/test_util.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Authors: https://github.com/open-atmos/PyPartMC/graphs/contributors #
55
####################################################################################################
66

7+
import numpy as np
78
import PyPartMC as ppmc
89

910

@@ -23,3 +24,13 @@ def test_pow2_above():
2324
def test_deg2rad():
2425
pass
2526

27+
@staticmethod
28+
def test_sphere_vol2rad():
29+
# arrange
30+
arg = (4/3)*np.pi*(1e-6)**3
31+
32+
# act
33+
rad = ppmc.sphere_vol2rad(arg)
34+
35+
# assert
36+
np.testing.assert_almost_equal(1e-6, rad)

0 commit comments

Comments
 (0)