Re: Pynobody filt.Spheres are very slow all of a sudden?

26 views
Skip to first unread message

Nolan McPhaul

unread,
Apr 5, 2023, 9:32:37 AM4/5/23
to pynbody-users
Hello,

This is in response to and a continuation of my previous thread, since any time I would try and respond to Andrew's response it would delete my message on that conversation.

Thank you for responding. I agree that there's definitely a more efficient way of doing what I'm trying to do. My main goal is to find the nearest gas particle for each dark matter particle in a halo (and eventually multiple halos). I was doing this by looping through every dark matter particle in a halo and centering a sphere of 0.3 kpc radius on it (to only work with relatively nearby particles to begin with). I would then loop through the gas particles in the sphere (if there were any), convert the xyz position into radial distance, and find the nearest gas particle to that dark matter particle. I would then use the properties of the dark matter and closest gas particle for my analysis (but that part was working fine). The code I wrote is shown below:

while i < len(halo.d):

   ...:     radius = "0.3 kpc"

   ...:     center = np.reshape(halo.dm['pos'][i], 3)

   ...:     sphere = halo[pynbody.filt.Sphere(radius, center)]

   ...:     if (len(sphere.gas) > 0):

   ...:         sphere.gas['dist'] = center - sphere.gas['pos']

   ...:         j = 0

   ...:         while j < len(sphere.gas['dist']):

   ...:             sphere.gas['dist'][j] = np.sqrt(sphere.gas['dist'][j].dot(sphere.gas['dist'][j]))

   ...:             j = j + 1

   ...:         index = np.where(sphere.gas['dist'] == sphere.gas['dist'].min())[0][0]

                . . . 


Like you said, there is likely a faster way of doing this, I'm just relatively new to python and pynbody, so any help would be greatly appreciated. Basically my two main questions are:
1) How can you find the nearest gas particle for each dark matter particle in a halo?
2) Also what is a fast way of converting xyz position coords into radial distance (because right now I'm just going through and using the dot product and finding the minimum radial distance in my filtering spheres)?

Thank you very much,
Nolan McPhaul

Greg Stinson

unread,
Apr 7, 2023, 2:45:28 PM4/7/23
to pynbody-users
Apologies for the flurry of messages, group.
I guess I set up this mailing list years ago.
Google marked the conversation as spam for some reason and didn't notify me until just now.
I've approved you as a legitimate sender henceforth, Nolan.

Cheers,

Greg

Reply all
Reply to author
Forward
0 new messages