This commit is contained in:
Guilhem Lavaux 2018-01-05 19:51:36 +01:00
parent e23bcd7df3
commit 4e1004f8f8

View File

@ -6,7 +6,37 @@ import h5py as h5
import numpy as np import numpy as np
import weakref import weakref
def smooth_particle_density(position, velocities=None, radius=1e6, boxsize=None, resolution=128, center=None ): def smooth_particle_density(
position,
velocities=None,
radius=1e6,
boxsize=None,
resolution=128,i
center=None, tmpprefix=None ):
"""Use adaptive smoothing to produce density and momentum fields.
Parameters:
position : numpy array NxQ
the particle positions
if Q==3, only positions. Q==6 means full space phase
velocities : Optional numpy array Nx3.
It is only optional if the above Q is 6.
radius : float
Maximum radius to which we need to compute fields
boxsize : float
Size of the box for the generated fields
resolution : int
Resolution of the output boxes
center : list of 3 floats
Center of the new box. It depends on the convention
for particles. If those are between [0, L], then [0,0,0]
is correct. If those are [-L/2,L/2] then you should set
[L/2,L/2,L/2].
tmpprefix : string
prefix of the temporary directory that will be used.
It needs to have a lot of space available. By default
'/tmp/ will be typically used.
"""
if len(position.shape) != 2: if len(position.shape) != 2:
raise ValueError("Invalid position array shape") raise ValueError("Invalid position array shape")
@ -19,7 +49,7 @@ def smooth_particle_density(position, velocities=None, radius=1e6, boxsize=None,
raise ValueError("Need a boxsize") raise ValueError("Need a boxsize")
cx,cy,cz=center cx,cy,cz=center
with TemporaryDirectory() as tmpdir: with TemporaryDirectory(prefix=tmpprefix) as tmpdir:
h5_file = os.path.join(tmpdir, 'particles.h5') h5_file = os.path.join(tmpdir, 'particles.h5')
with h5.File(h5_file, mode="w") as f: with h5.File(h5_file, mode="w") as f:
data = f.create_dataset('particles', shape=(position.shape[0],7), dtype=np.float32) data = f.create_dataset('particles', shape=(position.shape[0],7), dtype=np.float32)