Effacer les filtres
Effacer les filtres

Cheking my code related to create 3D cube pourous material

15 vues (au cours des 30 derniers jours)
Aida
Aida le 13 Fév 2024
Réponse apportée : Aastha le 24 Sep 2024 à 6:46
This code is designed to generate a 3D volume output, but I am interested in incorporating a specific level of porosity for further research. I'm curious if this modification will serve my intended purpose.
"% Paramètres
dimension = 100; % Taille de l'image 3D
porosity = 0.2; % Porosité (e.g., 20%)
minPoreRadius = 2;
maxPoreRadius = 5;
% Création d'une matrice 3D initialement remplie de zéros
materialMatrix = zeros(dimension, dimension, dimension);
% Génération aléatoire des pores
numPores = round(porosity * dimension^3);
positionsX = randi([1, dimension], 1, numPores);
positionsY = randi([1, dimension], 1, numPores);
positionsZ = randi([1, dimension], 1, numPores);
poreRadii = randi([minPoreRadius, maxPoreRadius], 1, numPores);
% Ajout des pores à la matrice
for i = 1:numPores
pore = strel('sphere', poreRadii(i));
materialMatrix(positionsX(i), positionsY(i), positionsZ(i)) = 1;
materialMatrix = imdilate(materialMatrix, pore);
end
% Affichage de la matrice 3D
figure;
[x, y, z] = meshgrid(1:dimension, 1:dimension, 1:dimension);
scatter3(x(:), y(:), z(:), 5, 'filled', 'MarkerFaceColor', 'r'); % Afficher tous les points
hold on;
[px, py, pz] = ind2sub(size(materialMatrix), find(materialMatrix));
scatter3(px, py, pz, 50, 'filled', 'MarkerFaceColor', 'b'); % Afficher les pores en bleu
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Milieu de matériau poreux 3D');
grid on;
axis equal tight;
view(3);
hold off;
"

Réponses (1)

Aastha
Aastha le 24 Sep 2024 à 6:46
Hi @Aida,
I tried to reproduce the given code, and I have a few suggestions that can improve the code and create 3D cube porous material. Here are a few of them:
1. Relative Pore Radii:
You have set fixed values for the pore radii. This approach works well if the pore radii are always smaller than the matrix dimension, but it could lead to problems if the pore radii exceed the matrix dimension. To make the code more flexible, set the pore radii relative to the matrix size. The code snippet below illustrates this:
dimension = 20; % Size of the 3D matrix
porosity = 0.02; % Porosity (e.g., 2%)
minPoreRadius = 0.05 * dimension; % Relative to the matrix size
maxPoreRadius = 0.1 * dimension; % Relative to the matrix size
2. Avoiding Complete Matrix Dilation:
When you use imdilate function to create pores, the entire materialMatrix can become filled with ones by the end of the loop, which is not ideal for visualization. Instead, you can store the positions of the pores separately and then visualize those. This approach avoids cluttering the matrix and provides a clearer visualization. You can achieve this with the following code snippet:
p = [];
for i = 1:numPores
pore = strel('sphere', poreRadii(i));
materialMatrix(positionsX(i), positionsY(i), positionsZ(i)) = 1;
materialMatrix = imdilate(materialMatrix, pore);
[cpx, cpy, cpz] = ind2sub(size(materialMatrix), find(materialMatrix));
cp = [cpx, cpy, cpz];
p = [p; cp]; % Store the pore locations
p = unique(p, 'rows'); % Remove duplicate points
materialMatrix = zeros(dimension, dimension, dimension); % Reset the matrix
end
The above modifications to the code will help you create a 3D cube porous material.
For more information on “imdilate” function, you may refer to the MathWorks documentation mentioned below:
I hope this is helpful!

Catégories

En savoir plus sur Material Sciences dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by