Skip to content

Commit 91f4387

Browse files
committed
Use XyzComponents as return type of vector field functions and xyz_grid()
1 parent 03439e9 commit 91f4387

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

sfs/mono/source.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ def point(omega, x0, n0, grid, c=None):
6262
def point_velocity(omega, x0, n0, grid, c=None):
6363
"""Velocity of a point source.
6464
65+
Returns
66+
-------
67+
XyzComponents
68+
Particle velocity at positions given by `grid`.
69+
See :class:`sfs.util.XyzComponents`.
70+
6571
"""
6672
k = util.wavenumber(omega, c)
6773
x0 = util.asarray_1d(x0)
@@ -70,7 +76,7 @@ def point_velocity(omega, x0, n0, grid, c=None):
7076
r = np.linalg.norm(offset)
7177
v = point(omega, x0, n0, grid, c=c)
7278
v *= (1+1j*k*r) / (defs.rho0 * defs.c * 1j*k*r)
73-
return [v * o / r for o in offset]
79+
return util.XyzComponents([v * o / r for o in offset])
7480

7581

7682
def point_modal(omega, x0, n0, grid, L, N=None, deltan=0, c=None):
@@ -87,6 +93,7 @@ def point_modal(omega, x0, n0, grid, L, N=None, deltan=0, c=None):
8793
compatibility).
8894
grid : triple of numpy.ndarray
8995
The grid that is used for the sound field calculations.
96+
See :func:`sfs.util.xyz_grid`.
9097
L : (3,) array_like
9198
Dimensionons of the rectangular room.
9299
N : (3,) array_like or int, optional
@@ -157,6 +164,7 @@ def point_modal_velocity(omega, x0, n0, grid, L, N=None, deltan=0, c=None):
157164
compatibility).
158165
grid : triple of numpy.ndarray
159166
The grid that is used for the sound field calculations.
167+
See :func:`sfs.util.xyz_grid`.
160168
L : (3,) array_like
161169
Dimensionons of the rectangular room.
162170
N : (3,) array_like or int, optional
@@ -172,8 +180,9 @@ def point_modal_velocity(omega, x0, n0, grid, L, N=None, deltan=0, c=None):
172180
173181
Returns
174182
-------
175-
numpy.ndarray
183+
XyzComponents
176184
Particle velocity at positions given by `grid`.
185+
See :class:`sfs.util.XyzComponents`.
177186
178187
"""
179188
k = util.wavenumber(omega, c)
@@ -214,9 +223,7 @@ def point_modal_velocity(omega, x0, n0, grid, L, N=None, deltan=0, c=None):
214223
vx = vx - 8*1j / (ksquared - km) * p0
215224
vy = vy - 8*1j / (ksquared - km) * p1
216225
vz = vz - 8*1j / (ksquared - km) * p2
217-
218-
219-
return vx, vy, vz
226+
return util.XyzComponents([vx, vy, vz])
220227

221228

222229
def line(omega, x0, n0, grid, c=None):
@@ -261,6 +268,12 @@ def line(omega, x0, n0, grid, c=None):
261268
def line_velocity(omega, x0, n0, grid, c=None):
262269
"""Velocity of line source parallel to the z-axis.
263270
271+
Returns
272+
-------
273+
XyzComponents
274+
Particle velocity at positions given by `grid`.
275+
See :class:`sfs.util.XyzComponents`.
276+
264277
"""
265278
k = util.wavenumber(omega, c)
266279
x0 = util.asarray_1d(x0)
@@ -277,7 +290,7 @@ def line_velocity(omega, x0, n0, grid, c=None):
277290
if len(grid) > 2:
278291
v.append(np.zeros_like(v[0]))
279292

280-
return [_duplicate_zdirection(vi, grid) for vi in v]
293+
return util.XyzComponents([_duplicate_zdirection(vi, grid) for vi in v])
281294

282295

283296
def line_dipole(omega, x0, n0, grid, c=None):
@@ -330,15 +343,21 @@ def plane(omega, x0, n0, grid, c=None):
330343

331344

332345
def plane_velocity(omega, x0, n0, grid, c=None):
333-
"""Vecolity of a plane wave.
346+
"""Velocity of a plane wave.
334347
335348
::
336349
337350
V(x, w) = 1/(rho c) e^(-i w/c n x) n
338351
352+
Returns
353+
-------
354+
XyzComponents
355+
Particle velocity at positions given by `grid`.
356+
See :class:`sfs.util.XyzComponents`.
357+
339358
"""
340359
v = plane(omega, x0, n0, grid, c=c) / (defs.rho0 * defs.c)
341-
return [v * n for n in n0]
360+
return util.XyzComponents([v * n for n in n0])
342361

343362

344363
def _duplicate_zdirection(p, grid):

sfs/util.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,16 @@ def xyz_grid(x, y, z, spacing, endpoint=True, **kwargs):
150150
endpoint : bool, optional
151151
If ``True`` (the default), the endpoint of each range is
152152
included in the grid. Use ``False`` to get a result similar to
153-
:func:`numpy.arange`. See :func:`strict_arange`.
153+
:func:`numpy.arange`. See :func:`sfs.util.strict_arange`.
154154
**kwargs
155-
All further arguments are forwarded to :func:`strict_arange`.
155+
All further arguments are forwarded to
156+
:func:`sfs.util.strict_arange`.
156157
157158
Returns
158159
-------
159-
list of numpy.ndarrays
160+
XyzComponents
160161
A grid that can be used for sound field calculations.
162+
See :class:`sfs.util.XyzComponents`.
161163
162164
See Also
163165
--------
@@ -178,7 +180,7 @@ def xyz_grid(x, y, z, spacing, endpoint=True, **kwargs):
178180
grid = np.meshgrid(*ranges, sparse=True, copy=False)
179181
for i, s in scalars:
180182
grid.insert(i, s)
181-
return grid
183+
return XyzComponents(grid)
182184

183185

184186
def normalize(p, grid, xnorm):
@@ -237,7 +239,7 @@ def db(x, power=False):
237239
class XyzComponents(np.ndarray):
238240
"""See __init__()."""
239241

240-
def __init__(self, arg, **kwargs):
242+
def __init__(self, components, **kwargs):
241243
"""Triple (or pair) of arrays: x, y, and optionally z.
242244
243245
Instances of this class can be used to store coordinate grids
@@ -260,19 +262,20 @@ def __init__(self, arg, **kwargs):
260262
261263
Parameters
262264
----------
263-
arg : triple or pair of array_like
265+
components : triple or pair of array_like
264266
The values to be used as X, Y and Z arrays. Z is optional.
265267
**kwargs
266268
All further arguments are forwarded to
267-
:func:`numpy.asarray`.
269+
:func:`numpy.asarray`, which is applied to the elements of
270+
`components`.
268271
269272
"""
270273
# This method does nothing, it's only here for the documentation!
271274

272-
def __new__(cls, arg, **kwargs):
275+
def __new__(cls, components, **kwargs):
273276
# object arrays cannot be created and populated in a single step:
274-
obj = np.ndarray.__new__(cls, len(arg), dtype=object)
275-
for i, component in enumerate(arg):
277+
obj = np.ndarray.__new__(cls, len(components), dtype=object)
278+
for i, component in enumerate(components):
276279
obj[i] = np.asarray(component, **kwargs)
277280
return obj
278281

0 commit comments

Comments
 (0)