Problem in my script to generate random spheres with Comsol matlab

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
....
....

10 commentaires

said - please clarify what you mean by when i run this script i get an error every time after the modification. If you are observing an error, please copy and paste the full error message to this question. Make sure that you include the line of code that is throwing the error. Include as much information as possible...
I have this error:
COMSOL Model Object
Name: packing_spheres.mph
Tag: Block
Identifier: root
>> Block_w_spheres03
Undefined function or variable 'r'.
Error in Block_w_spheres03 (line 88)
rsum = r+R(k);
said - The error message is telling you that the r in
rsum = r+R(k);
is undefined though I do see the line (above)
r = abs( normrnd(miu,sigma) );
. Is there perhaps another line of code that calculates rsum in the same way as above? I don't think that you have posted all of your code so please either do so or confirm that there is only one line of code where rsum is initialized.
No, this is the only line that I have defined
in this script, rsum = r + R (k), were r is radius of sphere and R (k) is on function to: R = zeros (n, 1);
I inform you that I use a script for the 2D case that I attached here, this script is working for the case of circles in a square.
Running the code without comsol commands is not possible, since not all variables are provided. I used the variables of the function you provided to fill the gaps.
Apparently the variable "n" does not have any influence on the number of iterations of the while loop leading to errors for indices greater "n". The described error could not be reproduced.
Are you sure the error is occuring in matlab? Maybe the block is not defined correctly in Comsol (missing variable, faulty dimension). It would be helpful to have a code snippet that is executable and lets us reproduce the described error.
Here, when I make some modifications of this script as follows, I get a cube with spheres of the same center and a half different diameters, while the problem is always in the loop of generation of the spheres with the condition of not overlapping,
I attached the script modified to generate spheres (Block_w_spheres2.m)
for the 2D case, here are the results
said - are you still having a problem with the error message Undefined function or variable 'r'. or is that no longer the case? Is the code "working" but just not doing what you expect it to do? Please clarify.
Hi Said,
Can you send me the code file? Thanks
Ok, your email?
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...

Connectez-vous pour commenter.

Réponses (0)

Produits

Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by