How to place random CNT's in a box domain with random curves and orientation ?
6 views (last 30 days)
Show older comments
Hi all, what i want is a code that can actually place random carbon nanotubes in a cube domain so that i can use it in comsol livelink MATLAB bellow is a code that puts spheres which i found in this forum i need it based on that.. :
function P = GetRandomSpheres(nWant, Width, Radius)
% INPUT:
% nWant: Number of spheres
% Width: Dimension of 3d box as [1 x 3] double vector
% Radius: Radius of spheres
% OUTPUT:
% P: [nWant x 3] matrix, centers
P = zeros(nWant, 3);
R2 = (2 * Radius) ^ 2; % Squared once instead of SQRT each time
W = Width - 2 * Radius; % Avoid interesction with borders
iLoop = 1; % Security break to avoid infinite loop
nValid = 0;
while nValid < nWant && iLoop < 1e12
newP = rand(1, 3) .* W + Radius;
% Auto-expanding, need Matlab >= R2016b. For earlier versions:
% Dist2 = sum(bsxfun(@minus, P(1:nValid, :), newP) .^ 2, 2);
Dist2 = sum((P(1:nValid, :) - newP) .^ 2, 2);
if all(Dist2 > R2)
% Success: The new point does not touch existing sheres:
nValid = nValid + 1; % Append this point
P(nValid, :) = newP;
end
iLoop = iLoop + 1;
end
% Stop if too few values have been found:
if nValid < nWant
error('Cannot find wanted number of points in %d iterations.', iLoop)
end
end
n = 100;
R = 5;
P = GetRandomSpheres(n, [100, 100, 100], R);
figure
axes('NextPlot', 'add', ...
'XLim', [0, 100], 'YLim', [0, 100], 'ZLim', [0, 100]);
view(3);
[X, Y, Z] = sphere();
for k = 1:n
surf(X * R + P(k, 1), Y * R + P(k, 2), Z * R + P(k, 3));
end
0 Comments
Answers (1)
Torsten
on 14 Jan 2025
2 Comments
Torsten
on 15 Jan 2025
Edited: Torsten
on 15 Jan 2025
I have no experience with this MATLAB code. I think the best you can get is a list of points that define the cylinders. Maybe loading it in a CAD program can help to get DXF or STL files from it.
If you want to use this as geometry in a fluid-dynamics application, I can only advice against it. You will never get a converged flow field in such a random structure, and the mesh must be that fine to resolve the geometry that usual RAM capacity will not suffice.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!