Skip to content

Commit 3e149ed

Browse files
committed
adding diam2rad utility function with test
1 parent 95bd0f2 commit 3e149ed

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
@@ -211,6 +211,11 @@ PYBIND11_MODULE(_PyPartMC, m) {
211211
"Convert geometric radius (m) to mass-equivalent volume for spherical particles."
212212
);
213213

214+
m.def(
215+
"diam2rad", &diam2rad, py::return_value_policy::copy,
216+
"Convert diameter (m) to radius (m)."
217+
);
218+
214219
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
215220

216221
m.attr("__all__") = py::make_tuple(
@@ -230,6 +235,7 @@ PYBIND11_MODULE(_PyPartMC, m) {
230235
"histogram_2d",
231236
"sphere_vol2rad",
232237
"rad2diam",
233-
"sphere_rad2vol"
238+
"sphere_rad2vol",
239+
"diam2rad"
234240
);
235241
}

src/util.F90

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,12 @@ subroutine f_sphere_rad2vol(rad, v) bind(C)
4343

4444
end subroutine
4545

46+
subroutine f_diam2rad(d, rad) bind(C)
47+
real(c_double), intent(in) :: d
48+
real(c_double), intent(out) :: rad
49+
50+
rad = diam2rad(d)
51+
52+
end subroutine
53+
4654
end module

src/util.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern "C" void py_pow2_above(int*, int*);
1010
extern "C" void f_sphere_vol2rad(const double*, double*);
1111
extern "C" void f_rad2diam(const double*, double*);
1212
extern "C" void f_sphere_rad2vol(const double*, double*);
13+
extern "C" void f_diam2rad(const double*, double*);
1314

1415
auto pow2_above(int n) {
1516
int res;
@@ -35,5 +36,11 @@ double sphere_rad2vol(double rad) {
3536
return v;
3637
}
3738

39+
double diam2rad(double d) {
40+
double rad;
41+
f_diam2rad(&d, &rad);
42+
return rad;
43+
}
44+
3845
extern "C" double py_deg2rad(double);
3946

tests/test_util.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,14 @@ def test_sphere_rad2vol():
5656

5757
# assert
5858
np.testing.assert_almost_equal(vol, (4/3)*np.pi*(arg)**3)
59+
60+
@staticmethod
61+
def test_diam2rad():
62+
# arrange
63+
arg = 1e-6
64+
65+
# act
66+
rad = ppmc.diam2rad(arg)
67+
68+
# assert
69+
assert rad == arg/2

0 commit comments

Comments
 (0)