generating random particles in a cylinder
Show older comments
I am trying to generate random particles in a cylinder corresponds to a finite volume fraction. Therefore, each volume fraction results in different number of particles. But, as volume fraction increases, say number of particles increases, the simulation time increases dramatically. For example, for 1100 particles (about volume fraction equal to 0.3) it takes just a minute to generate them in a way that they maintain within the cylinder boundaries and also do not overlap. However, if I want 1400 particles corresponds to volume fraction 0.4 it took about 24 hours and still not finished. Any suggestion is appreciated. The code is as below;
vol_frac=0.400831;
lz=0.04;
lx=lz;ly=lx;
x(1)=(2*lz-2*R)*rand(1)-lz+R;
y(1)=(2*sqrt(lz^2-x(1)^2)-2*R)*rand(1)-sqrt(lz^2-x(1)^2)+R;
z(1)=(2*lz-2*R)*rand(1)-lz+R;
%no. of particles
Npart=round(6*0.08^3*vol_frac/4/0.006^3);
%Npart=1100;
for i=2:Npart;
x(i)=(2*lz-2*R)*rand(1)-lz+R;
y(i)=(2*sqrt(lz^2-x(i)^2)-2*R)*rand(1)-sqrt(lz^2-x(i)^2)+R;
z(i)=(2*lz-2*R)*rand(1)-lz+R;
k=i-1;
while k>=1;
c=((y(k)-y(i))^2+(x(k)-x(i))^2+(z(k)-z(i))^2)^0.5;
if c<=2*R
x(i)=(2*lz-2*R)*rand(1)-lz+R;
y(i)=(2*sqrt(lz^2-x(i)^2)-2*R)*rand(1)-sqrt(lz^2-x(i)^2)+R;
z(i)=(2*lz-2*R)*rand(1)-lz+R;
k=i-1;
else
k=k-1;
end
end
end
7 Comments
Image Analyst
on 11 Jun 2015
What is R, and is it a constant? Please add comments. Also you should know that typing control-i in the MATLAB editor will fix up the indenting so that when you paste it in here, it does not look all messed up like it does now.
hamed
on 11 Jun 2015
Guillaume
on 12 Jun 2015
The issue is not with rand which does generate uniformly distributed numbers but most likely with your mapping from a cube (which is what is generated by rand) to a cylinder.
Can you comment your code? Explain each equation and particularly the purpose of the while loop. Also which direction is the axis of the cylinder?
I would hope that x, y and z have been predeclared with zero. Otherwise, the resizing / copying with each iteration of the for loop would be a reason for slowdowns.
hamed
on 14 Jun 2015
Denisse Campos Muñoz
on 17 Feb 2016
I used your code to generating particles in cylindrical coordinates and that these follow uniform distribution using random ('unif', ..., ...) and would like to know how to graph the particles.
How I can plot the distributed along the center of each particle?
Answers (0)
Categories
Find more on Probability Distributions and Hypothesis Tests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
