# Problem in my script to generate random spheres with Comsol matlab

22 views (last 30 days)
said boutaani on 22 Apr 2020
Hey, I have a script qui marche avec Comsol-Matlab, when i run this script i get an error every time after the modification
Can you help me to correct this script matlab with Comsol:
The dots ... mean the special lines of the Comsol code
function out = packing_spheres
%
% packing_spheres.m
...
...
...
%%%%%% Algorith to generate random positions%%%%%%%%%%%%
blk_size = 1 % Length Of Block (cube)
...
n = 100;
Vsum = 0;
% Generate position vectors
Pos = zeros(n,3) ; % XYZ
R = zeros(n,3);
idx = 1; % index for sphere
flag = 0;
while (Vsum < Vsq * vf)
r = abs( normrnd(miu,sigma) );
% generates a random number from the normal distribution with mean parameter mu and
% standard deviation parameter sigma.
pos = [blk_size * rand(1,1) blk_size * rand(1,1) blk_size * rand(1,1)];
for k = 1:idx %Check the distance between the randomly generated sphere and all existing spheres.
Distance = sqrt((pos(1)-Pos(k,1))^2+(pos(2)-Pos(k,2))^2+(pos(3)-Pos(k,3))^2);
%rsum = r
rsum = r+R(k);
if Distance < 2*rsum
flag = 1;
break;
end
end
if flag == 1 % If the newly generated sphere overlaps an existing sphere, it enters the next loop cycle, abandoning the generated sphere this time.
flag = 0;
continue;
end
if (pos(1)-r < 0) || (pos(1)+r > blk_size)
continue;
end
if (pos(2)-r < 0) || (pos(2)+r > blk_size)
continue;
end
if (pos(3)-r < 0) || (pos(3)+r > blk_size)
continue;
end
V = Vsum + 3/4 * pi * r * r * r;
if V > vf * Vsq %Evaluation of the volume fraction condition
break;
end
sph = ['sph',num2str(idx)];% N° of sphere
model.component('comp1').geom('geom1').create(sph, 'Sphere'); %Create a sphere based on its current index value
model.component('comp1').geom('geom1').feature(sph).set('r', r); %Specify the radius and position of the newly created sphere.
Pos(idx,:) = pos;
R(idx) = r;
idx = idx +1;
Vsum = Vsum + 3/4 * pi * r * r * r;
end
....
....
Pattawee Chutimasakul on 23 Oct 2021
At moment , I have problem to create RVE model with sphere in cube with not overlapping sphere. I do the code in comsol application, so can you send me the code via email wee34402@gmail.com Thank you...

R2017b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!