Problem in my script to generate random spheres with Comsol matlab
Afficher commentaires plus anciens
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
Geoff Hayes
le 22 Avr 2020
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...
sadedbouta
le 22 Avr 2020
Geoff Hayes
le 22 Avr 2020
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.
sadedbouta
le 22 Avr 2020
Robert U
le 23 Avr 2020
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.
sadedbouta
le 23 Avr 2020
Geoff Hayes
le 23 Avr 2020
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.
TRAN Quang Vu
le 28 Juil 2020
Hi Said,
Can you send me the code file? Thanks
sadedbouta
le 28 Juil 2020
Pattawee Chutimasakul
le 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...
Réponses (0)
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
