Builds a k-d tree that allows effecient nearest neighbor queries
Iterates over atoms and their squared distances from the source position.