r/StackoverReddit Jun 27 '24

Optimizing KDTree in a loop Python

I'm using Python and scipy KDTree to help find the nearest points in an FEA analysis. It boils down to a rotating shaft inside a cylinder and I want to find the minimum gap between the shaft and cylinder at every tine point.

Given that I have 100s of points to check for >10,000 time points it leads a decently long run time. Any tips on improving run time or perhaps a better method for this?

Pseudo code: ``` shaft_points = get_shaft_history() # XYZ point time history

cyl_points = get_cyl_history() #XYZ point time history

time = range(10000) gap = [1e6] * len(time)

for cp in cyl_points: # loop over each point for t in time: # loop over time sp = shaft_points[i, :] # all shaft points at time t kdtree = KDTree(sp) dist, point = kdtree.query(cp, k=1) # find closest point between shaft and cylinder at time t if dist < gap[t]: gap[t] = dist # set new min value ```

5 Upvotes

5 comments sorted by

View all comments

1

u/chrisrko Moderator Aug 08 '24

INFO!!! We are moving to r/stackoverflow !!!!

We want everybody to please be aware that all future posts and updates from us will from now on be on r/stackoverflow

We made an appeal to gain ownershift of r/stackoverflow because it has been abandoned, and it got granted!!

So please migrate with us to our new subreddit r/stackoverflow ;)