Skip to content

Commit 95bd0f2

Browse files
committed
adding sphere_rad2vol utility function with test
1 parent d7bb9e0 commit 95bd0f2

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
@@ -206,6 +206,11 @@ PYBIND11_MODULE(_PyPartMC, m) {
206206
"Convert radius (m) to diameter (m)."
207207
);
208208

209+
m.def(
210+
"sphere_rad2vol", &sphere_rad2vol, py::return_value_policy::copy,
211+
"Convert geometric radius (m) to mass-equivalent volume for spherical particles."
212+
);
213+
209214
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
210215

211216
m.attr("__all__") = py::make_tuple(
@@ -224,6 +229,7 @@ PYBIND11_MODULE(_PyPartMC, m) {
224229
"histogram_1d",
225230
"histogram_2d",
226231
"sphere_vol2rad",
227-
"rad2diam"
232+
"rad2diam",
233+
"sphere_rad2vol"
228234
);
229235
}

src/util.F90

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,12 @@ subroutine f_rad2diam(rad, d) bind(C)
3535

3636
end subroutine
3737

38+
subroutine f_sphere_rad2vol(rad, v) bind(C)
39+
real(c_double), intent(in) :: rad
40+
real(c_double), intent(out) :: v
41+
42+
v = sphere_rad2vol(rad)
43+
44+
end subroutine
45+
3846
end module

src/util.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
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*);
12+
extern "C" void f_sphere_rad2vol(const double*, double*);
1213

1314
auto pow2_above(int n) {
1415
int res;
@@ -28,5 +29,11 @@ double rad2diam(double rad) {
2829
return d;
2930
}
3031

32+
double sphere_rad2vol(double rad) {
33+
double v;
34+
f_sphere_rad2vol(&rad, &v);
35+
return v;
36+
}
37+
3138
extern "C" double py_deg2rad(double);
3239

tests/test_util.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,14 @@ def test_rad2diam():
4545

4646
# assert
4747
assert diam == 2*arg
48+
49+
@staticmethod
50+
def test_sphere_rad2vol():
51+
# arrange
52+
arg = 1e-6
53+
54+
# act
55+
vol = ppmc.sphere_rad2vol(arg)
56+
57+
# assert
58+
np.testing.assert_almost_equal(vol, (4/3)*np.pi*(arg)**3)

0 commit comments

Comments
 (0)