Skip to content

Commit 4b15c82

Browse files
committed
renamed calc_ang_dist() to calc_ang_scat() and removed unit checking
1 parent cdc925a commit 4b15c82

File tree

3 files changed

+38
-46
lines changed

3 files changed

+38
-46
lines changed

pymie/mie.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,9 @@
5858
# User-facing functions for the most often calculated quantities (form factor,
5959
# efficiencies, asymmetry parameter)
6060

61-
# all arguments should be dimensionless
62-
@ureg.check('[]', '[]', '[]', None, None)
63-
def calc_ang_dist(m, x, angles, mie = True, check = False):
61+
def calc_ang_scat(m, x, angles, mie = True, check = False):
6462
"""
65-
Calculates the angular distribution of light intensity for parallel and
63+
Calculates the angular scattering of light intensity for parallel and
6664
perpendicular polarization for a sphere.
6765
6866
Parameters
@@ -71,9 +69,8 @@ def calc_ang_dist(m, x, angles, mie = True, check = False):
7169
complex particle relative refractive index, n_part/n_med
7270
x : complex or float, array-like
7371
size parameter, x = ka = 2*pi*n_med/lambda * a (sphere radius a)
74-
angles : ndarray(structcol.Quantity [dimensionless])
75-
array of angles. Must be entered as a Quantity to allow specifying
76-
units (degrees or radians) explicitly
72+
angles : array-like
73+
array of angles. Must be specified in radians
7774
mie : Boolean (optional)
7875
if true (default) does full Mie calculation; if false, uses RG
7976
approximation
@@ -88,13 +85,6 @@ def calc_ang_dist(m, x, angles, mie = True, check = False):
8885
parallel and perpendicular to scattering plane, respectively. See
8986
Bohren & Huffman ch. 3 for details.)
9087
"""
91-
# convert to radians from whatever units the user specifies
92-
if isinstance(angles, Quantity):
93-
angles = angles.to('rad').magnitude
94-
95-
if isinstance(x, Quantity):
96-
x = x.to('').magnitude
97-
9888
if mie:
9989
# Mie scattering preliminaries
10090
nstop = _nstop(np.array(x).max())
@@ -238,7 +228,7 @@ def calc_integrated_cross_section(m, x, wavelen_media, thetas):
238228
Dimensional integrated cross-section
239229
"""
240230
angles = thetas.to('rad')
241-
form_factor = calc_ang_dist(m, x, angles)
231+
form_factor = calc_ang_scat(m, x, angles)
242232

243233
integrand_par = (form_factor[0]*np.sin(angles)).magnitude
244234
integrand_perp = (form_factor[1]*np.sin(angles)).magnitude
@@ -1185,7 +1175,7 @@ def diff_scat_intensity_complex_medium(m, x, thetas, kd, phis=None,
11851175
To get dimensional cross-sections, multiply by d^2, where d is the distance
11861176
at which the differential cross-sections are calculated. To compare
11871177
differential cross-sections for non-absorbing media and the far-field to
1188-
those reported by calc_ang_dist(), which are nondimensionalized by k^2,
1178+
those reported by calc_ang_scat(), which are nondimensionalized by k^2,
11891179
multiply the results from this function by kd^2.
11901180
11911181
References

pymie/tests/test_mie.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def test_form_factor():
7171
m = index_ratio(n_particle, n_matrix)
7272
x = size_parameter(wavelen, n_matrix, radius)
7373

74-
angles = Quantity(np.linspace(0, 180., 19), 'deg')
74+
angles = Quantity(np.linspace(0, 180., 19), 'deg').to("rad").magnitude
7575
# these values are calculated from MiePlot
7676
# (http://www.philiplaven.com/mieplot.htm), which uses BHMIE
7777
iperp_bhmie = np.array([2046.60203864487, 1282.28646423634, 299.631502275208,
@@ -89,7 +89,7 @@ def test_form_factor():
8989
7.24176462105438, 76.2910238480798, 54.1983836607738,
9090
93.5508557840006])
9191

92-
iparperp = mie.calc_ang_dist(m, x, angles)
92+
iparperp = mie.calc_ang_scat(m, x, angles)
9393
assert_array_almost_equal(iparperp[0], ipar_bhmie)
9494
assert_array_almost_equal(iparperp[1], iperp_bhmie)
9595

@@ -156,7 +156,7 @@ def test_absorbing_materials():
156156
m = index_ratio(n_particle, n_matrix)
157157
x = 10.0
158158

159-
angles = Quantity(np.linspace(0, 90., 10), 'deg')
159+
angles = Quantity(np.linspace(0, 90., 10), 'deg').to("rad").magnitude
160160
# these values are calculated from MiePlot
161161
# (http://www.philiplaven.com/mieplot.htm), which uses BHMIE
162162
iperp_bhmie = np.array([4830.51401095968, 2002.39671236719,
@@ -170,7 +170,7 @@ def test_absorbing_materials():
170170
24.9801217735053, 53.2319915708624,
171171
8.26505988320951, 47.4736966179677])
172172

173-
iparperp = mie.calc_ang_dist(m, x, angles)
173+
iparperp = mie.calc_ang_scat(m, x, angles)
174174
assert_array_almost_equal(iparperp[0], ipar_bhmie)
175175
assert_array_almost_equal(iparperp[1], iperp_bhmie)
176176

@@ -186,7 +186,7 @@ def test_multilayer_spheres():
186186
radius = Quantity('100.0 nm')
187187
x = size_parameter(wavelen, n_sample, radius)
188188

189-
f_parperp = mie.calc_ang_dist(m, x, angles)
189+
f_parperp = mie.calc_ang_scat(m, x, angles)
190190
cscat, cext, cabs, cback, asym = mie.calc_cross_sections(m, x, wavelen)
191191

192192
# form factor and cross section for a multilayer particle with a core that
@@ -195,7 +195,7 @@ def test_multilayer_spheres():
195195
multi_radius = Quantity(np.array([100.0, 100.0]),'nm')
196196
xarray = size_parameter(wavelen, n_sample, multi_radius)
197197

198-
f_parperp_multi = mie.calc_ang_dist(marray, xarray, angles)
198+
f_parperp_multi = mie.calc_ang_scat(marray, xarray, angles)
199199
cscat_multi, cext_multi, cabs_multi, cback_multi, asym_multi = mie.calc_cross_sections(marray, xarray, wavelen)
200200

201201
assert_array_almost_equal(f_parperp, f_parperp_multi)
@@ -212,7 +212,7 @@ def test_multilayer_spheres():
212212
multi_radius2 = Quantity(np.array([100.0, 110.0]),'nm')
213213
xarray2 = size_parameter(wavelen, n_sample, multi_radius2)
214214

215-
f_parperp_multi2 = mie.calc_ang_dist(marray2, xarray2, angles)
215+
f_parperp_multi2 = mie.calc_ang_scat(marray2, xarray2, angles)
216216
cscat_multi2, cext_multi2, cabs_multi2, cback_multi2, asym_multi2 = mie.calc_cross_sections(marray2, xarray2, wavelen)
217217

218218
assert_array_almost_equal(f_parperp, f_parperp_multi2)
@@ -228,7 +228,7 @@ def test_multilayer_spheres():
228228
multi_radius3 = Quantity(np.array([100.0, 100.0, 100.0]),'nm')
229229
xarray3 = size_parameter(wavelen, n_sample, multi_radius3)
230230

231-
f_parperp_multi3 = mie.calc_ang_dist(marray3, xarray3, angles)
231+
f_parperp_multi3 = mie.calc_ang_scat(marray3, xarray3, angles)
232232
cscat_multi3, cext_multi3, cabs_multi3, cback_multi3, asym_multi3 = mie.calc_cross_sections(marray3, xarray3, wavelen)
233233

234234
assert_array_almost_equal(f_parperp, f_parperp_multi3)
@@ -245,7 +245,7 @@ def test_multilayer_spheres():
245245
multi_radius4 = Quantity(np.array([100, 110, 120]),'nm')
246246
xarray4 = size_parameter(wavelen, n_sample, multi_radius4)
247247

248-
f_parperp_multi4 = mie.calc_ang_dist(marray4, xarray4, angles)
248+
f_parperp_multi4 = mie.calc_ang_scat(marray4, xarray4, angles)
249249
cscat_multi4, cext_multi4, cabs_multi4, cback_multi4, asym_multi4 = mie.calc_cross_sections(marray4, xarray4, wavelen)
250250

251251
assert_array_almost_equal(f_parperp, f_parperp_multi4)
@@ -266,8 +266,8 @@ def test_multilayer_absorbing_spheres():
266266
xarray = size_parameter(wavelen, n_sample, multi_radius)
267267
angles = Quantity(np.linspace(np.pi/2, np.pi, 20), 'rad')
268268

269-
f_parperp_multi_real = mie.calc_ang_dist(marray_real, xarray, angles)
270-
f_parperp_multi_imag = mie.calc_ang_dist(marray_imag, xarray, angles)
269+
f_parperp_multi_real = mie.calc_ang_scat(marray_real, xarray, angles)
270+
f_parperp_multi_imag = mie.calc_ang_scat(marray_imag, xarray, angles)
271271

272272
cross_sections_multi_real = mie.calc_cross_sections(marray_real, xarray, wavelen)
273273
cross_sections_multi_imag = mie.calc_cross_sections(marray_imag, xarray, wavelen)
@@ -440,7 +440,7 @@ def test_pis_taus():
440440
def test_differential_cross_section():
441441
"""
442442
Tests that the differential cross-sections from diff_scat_complex_medium()
443-
and calc_ang_dist() are the same for a non-absorbing medium.
443+
and calc_ang_scat() are the same for a non-absorbing medium.
444444
"""
445445
# set parameters
446446
wavelen = Quantity("400.0 nm")
@@ -455,7 +455,7 @@ def test_differential_cross_section():
455455
x = size_parameter(wavelen, n_matrix, radius)
456456

457457
# With far-field Mie solutions
458-
I_parperp_cad = mie.calc_ang_dist(m, x, theta)
458+
I_parperp_cad = mie.calc_ang_scat(m, x, theta)
459459

460460
# With Mie solutions at surface of particle (but neglecting near-fields)
461461
kd = (k*distance).to("").magnitude
@@ -465,7 +465,7 @@ def test_differential_cross_section():
465465
incident_vector,
466466
cartesian=False)
467467

468-
# calc_ang_dist returns dimensionless differential cross-sections (times
468+
# calc_ang_scat returns dimensionless differential cross-sections (times
469469
# k^2). As noted in diff_scat_intensity_complex_medium(), this function
470470
# returns dimensionless values (scaled by k^2) muliplied by a factor of
471471
# 1/kd^2 for a non-absorbing medium. Therefore the
@@ -474,7 +474,7 @@ def test_differential_cross_section():
474474
# calculation is done. In short, both functions return dimensionless
475475
# cross-sections, but the ones returned by
476476
# diff_scat_intensity_complex_medium() need to be multiplied by a factor of
477-
# kd^2 to compare them to those of calc_ang_dist()
477+
# kd^2 to compare them to those of calc_ang_scat()
478478
#
479479
# since both of these functions rely on the same routine to calculate the
480480
# amplitude scattering matrix, they should give results to within

pymie/tests/test_mie_vectorized.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ class TestVectorizedInternalFunctions():
349349
350350
_pis_and_taus() :
351351
not tested explicitly here, but tested implicitly in
352-
`test_vectorized_calc_ang_dist()`. Vectorization over angles is tested
352+
`test_vectorized_calc_ang_scat()`. Vectorization over angles is tested
353353
in `test_mie.py::test_pis_taus()`
354354
_scatcoeffs() :
355355
tested by `test_vectorized_scatcoeffs()`
@@ -391,10 +391,10 @@ class TestVectorizedInternalFunctions():
391391
tested by `test_vectorized_angular_functions()`
392392
_amplitude_scattering_matrix() :
393393
not tested explicitly here, but tested implicitly in
394-
`test_vectorized_calc_ang_dist()`
394+
`test_vectorized_calc_ang_scat()`
395395
_amplitude_scattering_matrix_RG() :
396396
not tested explicitly here, but tested implicitly in
397-
`test_vectorized_calc_ang_dist()`
397+
`test_vectorized_calc_ang_scat()`
398398
399399
"""
400400
mxargs = {"start_wavelen": 400,
@@ -723,8 +723,8 @@ class TestVectorizedUserFunctions():
723723
724724
User functions and corresponding tests of vectorization are as follows:
725725
726-
calc_ang_dist() :
727-
tested by test_vectorized_calc_ang_dist()
726+
calc_ang_scat() :
727+
tested by test_vectorized_calc_ang_scat()
728728
calc_cross_sections() :
729729
tested by test_vectorized_cross_sections()
730730
calc_efficiencies() :
@@ -750,7 +750,8 @@ class TestVectorizedUserFunctions():
750750
"n_matrix": Quantity(1.00, '')}
751751

752752
num_angle = 19
753-
angles = Quantity(np.linspace(0, 180., num_angle), 'deg')
753+
angles = Quantity(np.linspace(0, 180., num_angle),
754+
'deg').to("rad").magnitude
754755

755756
@pytest.mark.parametrize("num_wavelen, num_layer",
756757
[(10, 1), (1, 5), (10, 5)])
@@ -863,16 +864,16 @@ def test_vectorized_calc_efficiencies(self, num_wavelen, num_layer):
863864

864865
@pytest.mark.parametrize("num_wavelen, num_layer",
865866
[(10, 1), (1, 5), (10, 5)])
866-
def test_vectorized_calc_ang_dist(self, num_wavelen, num_layer):
867-
"""Tests that mie.calc_ang_dist() vectorizes properly. Also implicitly
867+
def test_vectorized_calc_ang_scat(self, num_wavelen, num_layer):
868+
"""Tests that mie.calc_ang_scat() vectorizes properly. Also implicitly
868869
checks that _amplitude_scattering_matrix() and
869870
_amplitude_scattering_matrix_RG() vectorize properly. Also checks for
870871
correctness of RG calculations by comparing against Mie calculations
871872
for small refractive index difference.
872873
873874
"""
874875
m, x = mx(num_wavelen, num_layer, **self.mxargs)
875-
form_factor = mie.calc_ang_dist(m, x, self.angles)
876+
form_factor = mie.calc_ang_scat(m, x, self.angles)
876877
if num_wavelen == 1:
877878
expected_shape = (2, self.num_angle,)
878879
assert form_factor.shape == expected_shape
@@ -885,19 +886,19 @@ def test_vectorized_calc_ang_dist(self, num_wavelen, num_layer):
885886
# we should get same values from loop
886887
iparperp_loop = np.zeros(expected_shape, dtype=float)
887888
for i in range(num_wavelen):
888-
iparperp = mie.calc_ang_dist(m[i], x[i], self.angles)
889+
iparperp = mie.calc_ang_scat(m[i], x[i], self.angles)
889890
iparperp_loop[:, i] = iparperp
890891
assert_equal(form_factor, iparperp_loop)
891892

892893
# check vectorization for Rayleigh-Gans approximation
893894
if num_layer > 1:
894895
with pytest.raises(ValueError,
895896
match="Rayleigh-Gans approximation cannot"):
896-
form_factor_RG = mie.calc_ang_dist(m, x, self.angles,
897+
form_factor_RG = mie.calc_ang_scat(m, x, self.angles,
897898
mie=False)
898899
return
899900

900-
form_factor_RG = mie.calc_ang_dist(m, x, self.angles,
901+
form_factor_RG = mie.calc_ang_scat(m, x, self.angles,
901902
mie=False)
902903

903904
expected_shape = (2, num_wavelen, self.num_angle)
@@ -921,9 +922,10 @@ def test_vectorized_calc_ang_dist(self, num_wavelen, num_layer):
921922
# 0 degree scattering may give differences between RG and Mie, so we
922923
# compare at a few degrees and higher; also we do a lot of angles to
923924
# capture the sharp dips in the form factor
924-
angles = Quantity(np.linspace(10, 180., num_angle), 'deg')
925-
form_factor_RG = mie.calc_ang_dist(m, x, angles, mie=False)
926-
form_factor_mie = mie.calc_ang_dist(m, x, angles)
925+
angles = Quantity(np.linspace(10, 180., num_angle),
926+
'deg').to("rad").magnitude
927+
form_factor_RG = mie.calc_ang_scat(m, x, angles, mie=False)
928+
form_factor_mie = mie.calc_ang_scat(m, x, angles)
927929

928930
# Since we are comparing small numbers at the dips of the form factor,
929931
# the Mie and RG solutions may have a relative difference of up to a

0 commit comments

Comments
 (0)