Skip to content

Commit cb2f1c2

Browse files
authored
Merge pull request #138 from zdaq12/utils
adding sphere_vol2rad utility function with test
2 parents 90db128 + 3e149ed commit cb2f1c2

File tree

4 files changed

+129
-1
lines changed

4 files changed

+129
-1
lines changed

src/pypartmc.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,26 @@ 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+
204+
m.def(
205+
"rad2diam", &rad2diam, py::return_value_policy::copy,
206+
"Convert radius (m) to diameter (m)."
207+
);
208+
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+
214+
m.def(
215+
"diam2rad", &diam2rad, py::return_value_policy::copy,
216+
"Convert diameter (m) to radius (m)."
217+
);
218+
199219
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
200220

201221
m.attr("__all__") = py::make_tuple(
@@ -212,6 +232,10 @@ PYBIND11_MODULE(_PyPartMC, m) {
212232
"run_part",
213233
"pow2_above",
214234
"histogram_1d",
215-
"histogram_2d"
235+
"histogram_2d",
236+
"sphere_vol2rad",
237+
"rad2diam",
238+
"sphere_rad2vol",
239+
"diam2rad"
216240
);
217241
}

src/util.F90

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,36 @@ 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+
30+
subroutine f_rad2diam(rad, d) bind(C)
31+
real(c_double), intent(in) :: rad
32+
real(c_double), intent(out) :: d
33+
34+
d = rad2diam(rad)
35+
36+
end subroutine
37+
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+
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+
2254
end module

src/util.hpp

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

99
extern "C" void py_pow2_above(int*, int*);
10+
extern "C" void f_sphere_vol2rad(const double*, double*);
11+
extern "C" void f_rad2diam(const double*, double*);
12+
extern "C" void f_sphere_rad2vol(const double*, double*);
13+
extern "C" void f_diam2rad(const double*, double*);
1014

1115
auto pow2_above(int n) {
1216
int res;
1317
py_pow2_above(&n, &res);
1418
return res;
1519
}
1620

21+
double sphere_vol2rad(double v) {
22+
double rad;
23+
f_sphere_vol2rad(&v, &rad);
24+
return rad;
25+
}
26+
27+
double rad2diam(double rad) {
28+
double d;
29+
f_rad2diam(&rad, &d);
30+
return d;
31+
}
32+
33+
double sphere_rad2vol(double rad) {
34+
double v;
35+
f_sphere_rad2vol(&rad, &v);
36+
return v;
37+
}
38+
39+
double diam2rad(double d) {
40+
double rad;
41+
f_diam2rad(&d, &rad);
42+
return rad;
43+
}
44+
1745
extern "C" double py_deg2rad(double);
1846

tests/test_util.py

Lines changed: 44 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,46 @@ 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)
37+
38+
@staticmethod
39+
def test_rad2diam():
40+
# arrange
41+
arg = 0.5e-6
42+
43+
# act
44+
diam = ppmc.rad2diam(arg)
45+
46+
# assert
47+
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)
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)