How can i calculate very large distance matrix?
조회 수: 3 (최근 30일)
이전 댓글 표시
I am working on 2D simulation of particles moving in a plane. The particles interact with other particles in some interaction radius, r. Thus i have to calculate interparticle distances, which gets messy as number of particles gets large. (I need large number of particles for better statistics and less boundary effects.)
I am currently doing this:
N=1024*1024;
x=rand(1,N); y=rand(1,N); velocity=2*pi*(rand(1,N)-0.5); %unit magnitude
for t=1:1000
D = pdist([x' y'],'euclidean'); M=sqaureform(D);
[I,J]=find(0<M & M<r);
%then using loop to add the interaction:
for i = 1:N
interacting_particles = I(J==i);
if ~isempty(interacting_particles)
average_velocity(i) = atan2(mean(sin(velocity(interacting_particles))),mean(cos(velocity(interacting_particles))));
else
average_velocity(i) = velocity(i);
end
end
velocity=average_velocity;
%modify x and y
x=x+cos(velocity);
y=y+sin(velocity);
relevant_quantities(t)=stat_mech(L,t,x,y,theta);
end
relevant_quantities is a well defined function.
I hope my problem is clear. Also, I have access to an HPC server, but I really wish to do something cheaper than calculating D array of size 4096 GB.
댓글 수: 2
답변 (1개)
Raunak Gupta
2020년 12월 30일
Hi Ashutosh,
I understand you want to calculate the number of particles and its locations from a particular particle. Since the bottleneck here is to store the whole distance matrix into the workspace, I think in that case rangesearch can be helpful which can return the points within a distance r. I think the inner for loop running from 1 to N can also be used for calculating these distances so no overhead with this approach. Also, I don’t see an explicit need of D matrix anywhere except for finding the interacting particles.
Hope this helps!
댓글 수: 1
참고 항목
카테고리
Help Center 및 File Exchange에서 Sequence and Numeric Feature Data Workflows에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!