0% found this document useful (0 votes)
5 views

whatcwhat

The document outlines a MATLAB script for generating and analyzing microstructures, including creating random microstructures, calculating grain sizes, and generating histograms of grain size distributions. It involves operations such as Voronoi diagram generation, image processing, and data output to files. The script also includes sections for grain fraction calculations and visualizations of the results.

Uploaded by

All Hindi Songs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

whatcwhat

The document outlines a MATLAB script for generating and analyzing microstructures, including creating random microstructures, calculating grain sizes, and generating histograms of grain size distributions. It involves operations such as Voronoi diagram generation, image processing, and data output to files. The script also includes sections for grain fraction calculations and visualizations of the results.

Uploaded by

All Hindi Songs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

% THIS IS FINAL FOR THE MICROSTRUCTURE GENERATION

% STEP
%1. RUN THIS FILE
%COPY THE "L" variable
%Paste in LNEW excel fil
%Run the size calculation file to get the actual value of grain size using
%Mean grain intercept size method
%Happy coding
%poly2mask The poly2mask function sets pixels that are inside the polygon to 1 and
sets pixels outside the polygon to 0
%L = bwlabel(BW,conn) returns a label matrix, where conn specifies the connectivity
%[L,n] = bwlabel(___) also returns n, the number of connected objects
clc
clear all
for image_no=10:10
%close all
clear D Fig2 L X Y bins c col count filename graindata h i j m num one_pixel_area
outname output_hist range_x range_y sum_pix temp_x temp_x_low temp_y temp_y_low tol
v vx vy x y y1 y1_1 yy1 z
hold off
% this creates a random microstructure and outputs the image to a file
%r=rand('state',7);
RandStream.setGlobalStream ...
(RandStream('mt19937ar','seed',sum(100*clock)));
%rand('state',10);
rand(1,10);
%rand(2,100);
x = rand(1,100); y = rand(1,100);
figure(1)
[vx,vy] = voronoi(x,y); % vx vy each contain two rows
%: start and end point of every line
%plot(x,y,'r+',vx,vy,'b-'); axis equal
%plot(vx(:,:),vy(:,:)); axis equal
h = plot(vx,vy,'-',x,y,'.','LineWidth',0.001);
set(h(1:end-1),'xliminclude','off','yliminclude','off')
%voronoi(x,y)
%rand('state',10);
figure(2)
axis off;
hold on
z=rand(1000,2); % change the seed no. to control the size of grain % change here
change here

[v,c]=voronoin(z);
col =[1 1 1];
for i = 1:length(c)
%if all(c{i}~=1) % If at least one of the indices is 1,
% then it is an open region and we can't
% patch that.
patch(v(c{i},1),v(c{i},2),col,'LineWidth',0.0001); % use color i.
%end
end
axis equal
xlim([0 1.0])
ylim([0 1.0])
%showaxes('on')
Fig2=figure(2);
%figure('visible', 'off');
filename=strcat('image',char(floor(image_no/10)+48),char(mod(image_no,10)+48),
'.tiff');

print(Fig2, '-dtiff','-r1600', filename);


%clf(2)
%% **************************checking if the image file is OK**************
X = imread(filename);

count=0;

m=size(X);
temp_x=0;
temp_y=0;
temp_x_low=m(1);
temp_y_low=m(2);
for i=1:m(1)
for j=1:m(2)
if(X(i,j)==0)
if(temp_x<i)
temp_x=i;
end
if(temp_y<j)
temp_y=j;
end
if(temp_x_low>i)
temp_x_low=i;
end
if(temp_y_low>j)
temp_y_low=j;
end
end
end
end
% tol=0;
% range_x=temp_x-temp_x_low-tol;
% range_y=temp_y-temp_y_low-tol;
% % range_x=range_y;
% for i=temp_x_low: range_x+temp_x_low-1
% for j=temp_y_low+2: range_y-1+temp_y_low+1
% if(X(i,j)>0)
% Y(i-temp_x_low+1,j-temp_y_low+1-2)=1;
% count=count+1;
% else
% Y(i-temp_x_low+1,j-temp_y_low+1-2)=0;
% end
% end
% end
% 'grain fraction='
% count/(size(Y,1)*size(Y,2))
Ynew=im2bw(X, 0.5);
Y=Ynew(temp_x_low:temp_x,temp_y_low:temp_y);
figure(3)
imshow(0.68.*Y, 'colormap', jet(256))

% outname = input('Enter name of outputfilename : ','s');


% outname='image1.dat';

outname=strcat('datafile_image',char(floor(image_no/
10)+48),char(mod(image_no,10)+48),'.dat');
dlmwrite(outname,strcat('Image file is: ',filename),'');
dlmwrite(outname,size(Y,2),'-append');
dlmwrite(outname,size(Y,1),'-append');
'size of the material matrix'
size(Y)
dlmwrite(outname,Y,'-append');
%% **********************grain size distribution***************************
%% **********************grain size distribution***************************
Ynew1=Y(500:4000,500:4000);
[L,num] = bwlabel(Ynew1,4);
lin=L;

figure(4)
imagesc(L); figure(gcf)
% making binder fraction 0
count_bind=100;
while(count_bind>0)
for i=2:size(L,1)
for j=1:size(L,2)
if(L(i-1,j)==0)
L(i-1,j)=L(i,j);
L(i,j);
i;
j;
end
end
end
for i=size(L,1):2
for j=size(L,2):1
if(L(i+1,j)==0)
L(i+1,j)=L(i,j);
L(i,j);
i;
j;
end
end
end
for i=1:size(L,1)
for j=2:size(L,2)
if(L(i,j-1)==0)
L(i,j-1)=L(i,j);
L(i,j);
i;
j;
end
end
end
for i=size(L,1):1
for j=size(L,2):2
if(L(i,j+1)==0)
L(i,j+1)=L(i,j);
L(i,j);
i;
j;
end
end
end
count_bind=0;
for i=1:size(L,1)
for j=1:size(L,2)
if(L(i,j)==0)
count_bind=count_bind+1;
end
end
end
count_bind;

'grain fraction=',1-count_bind/(size(L,1)*size(L,2));
end
% outputting the datafile with image
outname=strcat('datafile_vol_fr_100_',char(floor(image_no/
10)+48),char(mod(image_no,10)+48),'.dat');
dlmwrite(outname,strcat('Image file is: ',filename),'');
dlmwrite(outname,size(L,2),'-append');
dlmwrite(outname,size(L,1),'-append');
dlmwrite(outname,max(max(L)),'-append');
'size of the material matrix'
size(L)
dlmwrite(outname,L,'-append');
%%
%lnew=L(500:4000,1000:4000);
graindata = regionprops(L,'basic');
title('This is the image showing the detected grains');
'no of grains detected =',num
for i=1: num
D(i,1,:)=graindata(i).Centroid;
end
%one_pixel_area=(3e-3)^2/(size(L,2))^2; % area of one pixel
one_pixel_area=(20e-6)/(40*80); % area of one pixel according to meshing
sum_pix=0; %sum of all pixels in grains
for i=1:num
sum_pix=sum_pix+graindata(i).Area; %graindata(i).Area: Actual number of pixels in
the region, returned as a scalar.
%graindata(i).Area=(4/pi*graindata(i).Area*one_pixel_area)^0.5/1.0e-6;
graindata(i).Area=((4/pi*graindata(i).Area*one_pixel_area)^0.5)*1.0e+6;
end
grain_fr(image_no)=sum_pix/(size(L,1)*size(L,2));
'grain fraction of microstructure: ',sum_pix/(size(L,1)*size(L,2))
figure(5)
bins=linspace(0,700,20);
[y1,y1_1]=hist([graindata.Area],bins);

bar(y1_1,y1);
%axis([0 2.0 0 80])

ylabel('Frequency','FontSize',12)
xlabel('Grain Size in microns','FontSize',12)
hold on
%plot(y1_1,y1)
hold off

yy1(:,1)=y1_1(1,:);
yy1(:,2)=y1(1,:);
output_hist=strcat('histogram_image',char(floor(image_no/
10)+48),char(mod(image_no,10)+48), '.txt');
dlmwrite(output_hist,yy1,'newline','pc')
'file no done',image_no
end
dlmwrite('grain_fraction_for_all_data.dat',grain_fr)
figure(6)
clear bins
bins=linspace(mean(grain_fr)-0.01,mean(grain_fr)+0.01,15);
[gr1,gr1_1]=hist([grain_fr],bins);
bar(gr1_1,gr1);
%axis([0 2.0 0 80])
title('frequency distribution of grain volume fractions')
ylabel('Frequency','FontSize',12)
xlabel('Grain Volume Fraction','FontSize',12)

%% extracting all the grains separately in different .dat files

% for k = 1:num
%
outname=strcat('datafile_grain_',char(floor(k/10)+48),char(mod(k,10)+48),'.dat');
% for i = 1:size(L,1)
% for j = 1:size(L,2)
% if L(i,j) == k
% A(i,j)=L(i,j);
% dlmwrite(outname,A,'-append');
% end
% end
% end
% end

{
"cells": [
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"from multiprocessing.sharedctypes import Value\n",
"import cv2 as cv\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt\n",
"from pandas import value_counts\n",
"from scipy import ndimage\n",
"from skimage import io,color,measure\n",
"import random"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 50]\n",
" [ 0 50]\n",
" [ 2 50]\n",
" ...\n",
" [49 1]\n",
" [49 1]\n",
" [49 1]]\n"
]
}
],
"source": [
"Nodes=([])\n",
"layer=1\n",
"x=0\n",
"y=50 # 2 pixel is equal to 1 element \n",
"for j in range (0,25): \n",
" for i in range (0,25): # microstructure total no. of cell (column wise)
#macro length size / size of a element = 1mm/5micron\n",
" Nodes.append([x,y])\n",
" Nodes.append([x,y])\n",
" Nodes.append([x+2,y])\n",
" Nodes.append([x+2,y])\n",
" Nodes.append([x+2,y-2])\n",
" Nodes.append([x+2,y-2])\n",
" Nodes.append([x,y-2])\n",
" Nodes.append([x,y-2])\n",
" Nodes.append([x+1,y-1])\n",
" Nodes.append([x+1,y-1])\n",
" Nodes.append([x+1,y-1])\n",
" Nodes.append([x+1,y-1])\n",
" x=x+2\n",
" y=y-2\n",
" x=0 \n",
"\n",
"Points=np.array(Nodes)\n",
"print(Points)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"n=25#no. of rows \n",
"m=25 #no. of column\n",
"TNOE=10*(n*m)-(n+m)\n",
"NOET=4*n*m\n",
"NOEQ=6*(n*m)-(n+m)\n",
"NPE1=3 #node per emlement\n",
"NPE2=4 #node per element\n",
"\n",
"k1=8 \n",
"\n",
"\n",
"EL1=np.zeros([NOET,NPE1]) # Triangular element \n",
"EL2=np.zeros([NOEQ,NPE2]) # Quadratic cohesive element \n",
"\n",
"for i in range(1,(n*m)+1):\n",
"\n",
"# for the first triangular element \n",
" EL1[2*(i-1)*2,0]=(k1+1)+(i-1)*12\n",
" EL1[2*(i-1)*2,1]=EL1[2*(i-1)*2,0]-6\n",
" EL1[2*(i-1)*2,2]=EL1[2*(i-1)*2,1]-1\n",
"# for the second traingular element \n",
" EL1[2*(i-1)*2+1,0]=EL1[2*(i-1)*2,0]+1\n",
" EL1[2*(i-1)*2+1,1]=EL1[2*(i-1)*2+1,0]-5\n",
" EL1[2*(i-1)*2+1,2]=EL1[2*(i-1)*2+1,1]-1\n",
"# for the third triangular element \n",
" EL1[2*(i-1)*2+2,0]=EL1[2*(i-1)*2,0]+2\n",
" EL1[2*(i-1)*2+2,1]=EL1[2*(i-1)*2+2,0]-4\n",
" EL1[2*(i-1)*2+2,2]=EL1[2*(i-1)*2+2,1]-1\n",
"# for the fourth triangular element \n",
" EL1[2*(i-1)*2+3,0]=EL1[2*(i-1)*2,0]+3\n",
" EL1[2*(i-1)*2+3,1]=EL1[2*(i-1)*2+3,0]-11\n",
" EL1[2*(i-1)*2+3,2]=EL1[2*(i-1)*2+3,1]+7\n",
"\n",
"# for the first inside cohesive (Quadratic) element \n",
" EL2[2*(i-1)*2,3]=(k1+1)+(i-1)*12\n",
" EL2[2*(i-1)*2,0]=EL2[2*(i-1)*2,3]+1\n",
" EL2[2*(i-1)*2,1]=EL2[2*(i-1)*2,0]-6\n",
" EL2[2*(i-1)*2,2]=EL2[2*(i-1)*2,1]-1\n",
"\n",
"# for the second inside cohesive (Quadratic) element\n",
" EL2[2*(i-1)*2+1,3]=EL2[2*(i-1)*2,3]+1\n",
" EL2[2*(i-1)*2+1,0]=EL2[2*(i-1)*2+1,3]+1\n",
" EL2[2*(i-1)*2+1,1]=EL2[2*(i-1)*2+1,0]-5\n",
" EL2[2*(i-1)*2+1,2]=EL2[2*(i-1)*2+1,1]-1\n",
"\n",
"# for the third inside cohesive (Quadratic) element\n",
" EL2[2*(i-1)*2+2,3]=EL2[2*(i-1)*2+1,3]+1\n",
" EL2[2*(i-1)*2+2,0]=EL2[2*(i-1)*2+2,3]+1\n",
" EL2[2*(i-1)*2+2,1]=EL2[2*(i-1)*2+2,0]-4\n",
" EL2[2*(i-1)*2+2,2]=EL2[2*(i-1)*2+2,1]-1\n",
"\n",
"# for the fourth inside cohesive (Quadratic) element\n",
" EL2[2*(i-1)*2+3,3]=EL2[2*(i-1)*2+2,3]+1\n",
" EL2[2*(i-1)*2+3,0]=EL2[2*(i-1)*2+3,3]-3\n",
" EL2[2*(i-1)*2+3,1]=EL2[2*(i-1)*2+3,0]-7\n",
" EL2[2*(i-1)*2+3,2]=EL2[2*(i-1)*2+3,1]-1\n",
"k=1\n",
"for i in range(1,(n*(m-1))+1):\n",
" if k%m==0:\n",
" k=k+1\n",
" # for the verticle intermediate cohesive (Quadratic) element\n",
" EL2[4*n*m+i-1,0]=EL1[2*(k-1)*2+1,2]\n",
" EL2[4*n*m+i-1,1]=EL2[4*n*m+i-1,0]+1\n",
" EL2[4*n*m+i-1,2]=EL2[4*n*m+i-1,1]+15\n",
" EL2[4*n*m+i-1,3]=EL2[4*n*m+i-1,2]-7\n",
" k=k+1\n",
"#for the transverse intermediate cohesive (Quadratic) element\n",
"for i in range(1,(m*(n-1))+1):\n",
" EL2[(5*n*m-n)+i-1,3]=EL1[2*(i-1)*2+2,0]-4\n",
" EL2[(5*n*m-n)+i-1,0]=(k1+1)+(i+m-1)*12-7\n",
" EL2[(5*n*m-n)+i-1,1]=EL2[(5*n*m-n)+i-1,0]+1\n",
" EL2[(5*n*m-n)+i-1,2]=EL2[(5*n*m-n)+i-1,3]-1"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"# Read the Excel file into a pandas dataframe\n",
"excel_file_path = 'LNEW.xlsx' # Replace with the path to your Excel file\n",
"df = pd.read_excel(excel_file_path, header=None)\n",
"matrix_data = df.values\n",
"EL1 = EL1.astype(int)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3501, 3501)"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"matrix_data.shape"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"# Segregation of traingular element in their respective set \n",
"set = [[] for _ in range(50000)]\n",
"set_array = [None]*50000\n",
"set_array = [0 for _ in set_array]\n",
"for i in range(0,NOET):\n",
" k=0\n",
" a1=Points[EL1[i][0]-1]\n",
" a2=Points[EL1[i][1]-1]\n",
" a3=Points[EL1[i][2]-1]\n",
" lar = max(matrix_data[abs(a1[1]-400),abs(a1[0])], matrix_data[abs(a2[1]-
400),abs(a2[0])], matrix_data[abs(a3[1]-400),abs(a3[0])])\n",
" if matrix_data[abs(a1[1]-400),abs(a1[0])]==matrix_data[abs(a2[1]-
400),abs(a2[0])] and matrix_data[abs(a1[1]-400),abs(a1[0])]==matrix_data[abs(a3[1]-
400),abs(a3[0])] : ### for indexing in open cv pls refer
https://datahacker.rs/how-to-access-and-edit-pixel-values-in-opencv-with-python/ or
refer excel sheet\n",
" k=1\n",
" elif matrix_data[abs(a1[1]-400),abs(a1[0])]==matrix_data[abs(a2[1]-
400),abs(a2[0])] or matrix_data[abs(a1[1]-400),abs(a1[0])]==matrix_data[abs(a3[1]-
400),abs(a3[0])] :\n",
" k=1\n",
" elif matrix_data[abs(a2[1]-400),abs(a2[0])]==matrix_data[abs(a1[1]-
400),abs(a1[0])] or matrix_data[abs(a2[1]-400),abs(a2[0])]==matrix_data[abs(a3[1]-
400),abs(a3[0])] :\n",
" k=2\n",
" elif matrix_data[abs(a3[1]-400),abs(a3[0])]==matrix_data[abs(a1[1]-
400),abs(a1[0])] or matrix_data[abs(a3[1]-400),abs(a3[0])]==matrix_data[abs(a2[1]-
400),abs(a2[0])] :\n",
" k=3\n",
" else:\n",
" k=lar\n",
" if k==1:\n",
" set[matrix_data[abs(a1[1]-400),abs(a1[0])]-1].append(i+1)\n",
" set_array[matrix_data[abs(a1[1]-
400),abs(a1[0])]-1]=set_array[matrix_data[abs(a1[1]-400),abs(a1[0])]-1]+1\n",
" elif k==2:\n",
" set[matrix_data[abs(a2[1]-400),abs(a2[0])]-1].append(i+1)\n",
" set_array[matrix_data[abs(a2[1]-
400),abs(a2[0])]-1]=set_array[matrix_data[abs(a2[1]-400),abs(a2[0])]-1]+1\n",
" elif k==3:\n",
" set[matrix_data[abs(a3[1]-400),abs(a3[0])]-1].append(i+1)\n",
" set_array[matrix_data[abs(a3[1]-
400),abs(a3[0])]-1]=set_array[matrix_data[abs(a3[1]-400),abs(a3[0])]-1]+1\n",
" else:\n",
" set[lar-1].append(i+1)\n",
" set_array[lar-1]=set_array[lar-1]+1\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"# Segregation of cohesive element in between the triangular element \n",
"EL1 = EL1.astype(int)\n",
"setc = [[] for _ in range(50000)] # cohesive element set\n",
"set_arrayc = [None]*50000\n",
"set_arrayc = [0 for _ in set_arrayc]\n",
"set3=([]) # quadratic element same as set1 \n",
"set4=([]) # quadratic element same as set2 \n",
"set5=([]) # Boundary of the grain \n",
"set3n=0\n",
"set4n=0\n",
"set5n=0\n",
"k=1\n",
"for i in range(0,n*m):\n",
" for j in range(0,4):\n",
" if j==3: # for checking the first element eg.:01 & the last element
eg.: 04 \n",
" outer_index1, inner_index1 = next(((index, sublist.index(k+j)) for
index, sublist in enumerate(set) if k+j in sublist))\n",
" outer_index2, inner_index2 = next(((index, sublist.index(k+j-3))
for index, sublist in enumerate(set) if k+j-3 in sublist))\n",
" if outer_index1==outer_index2:\n",
" setc[outer_index1].append(k+j)\n",
" set_arrayc[outer_index1]=set_arrayc[outer_index1]+1\n",
" else:\n",
" set5.append(k+j)\n",
" set5n=set5n+1 \n",
" else:\n",
" outer_index1, inner_index1 = next(((index, sublist.index(k+j)) for
index, sublist in enumerate(set) if k+j in sublist))\n",
" outer_index2, inner_index2 = next(((index, sublist.index(k+j+1))
for index, sublist in enumerate(set) if k+j+1 in sublist))\n",
" if outer_index1==outer_index2:\n",
" setc[outer_index1].append(k+j)\n",
" set_arrayc[outer_index1]=set_arrayc[outer_index1]+1\n",
" else:\n",
" set5.append(k+j)\n",
" set5n=set5n+1\n",
" k=k+4\n",
"#for segregation of verticle intermediate cohesive (Quadratic) elements\n",
"k=2\n",
"p=1\n",
"for i in range(1,(n+1)):\n",
" for j in range(0,m-1):\n",
" outer_index1, inner_index1 = next(((index, sublist.index(k)) for
index, sublist in enumerate(set) if k in sublist))\n",
" outer_index2, inner_index2 = next(((index, sublist.index(k+6)) for
index, sublist in enumerate(set) if k+6 in sublist))\n",
" if outer_index1==outer_index2:\n",
" setc[outer_index1].append(n*m*4+(p))\n",
" set_arrayc[outer_index1]=set_arrayc[outer_index1]+1\n",
" p=p+1\n",
" else:\n",
" set5.append(n*m*4+(p))\n",
" set5n=set5n+1\n",
" p=p+1\n",
" k=k+4\n",
" k=k+4\n",
"\n",
"#for segregation of transverse intermediate cohesive (Quadratic) elements\n",
"k=3\n",
"p=1\n",
"for i in range(0,(n-1)):\n",
" for j in range(1,m+1):\n",
" outer_index1, inner_index1 = next(((index, sublist.index(k)) for
index, sublist in enumerate(set) if k in sublist))\n",
" outer_index2, inner_index2 = next(((index, sublist.index(k+(m-1)*4+2))
for index, sublist in enumerate(set) if k+(m-1)*4+2 in sublist))\n",
" if outer_index1==outer_index2:\n",
" setc[outer_index1].append(n*m*5-n+(p))\n",
" set_arrayc[outer_index1]=set_arrayc[outer_index1]+1\n",
" p=p+1\n",
" else:\n",
" set5.append(n*m*5-n+(p))\n",
" set5n=set5n+1\n",
" p=p+1\n",
" k=k+4"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#### this section of code is to write the INP file size normalizing ####\n",
"output_file = open('voronoiwithsizecontrol20MICRONupdate1x1.txt','w')\n",
"output_file.write('*Heading\\n')\n",
"output_file.write('**Preprint, echo=NO, model=NO, history=NO, contact=NO \\n')
\n",
"output_file.write('*Part, name=Part-1 \\n')\n",
"output_file.write('*Node \\n')\n",
"k=0\n",
"nor=0.0025 #### size normalization faction 1/400 THAT means (2800 is equal to
7mm) \n",
"for i in Points: # nodes\n",
" k=k+1\n",
" output_file.write(str(k),)\n",
" output_file.write(',')\n",
" output_file.write(str(round(i[0]*nor,5)),)\n",
" output_file.write(',')\n",
" output_file.write(str(round(i[1]*nor,5))+'\\n')\n",
"\n",
"output_file.write('*Element, type = CPS3 \\n')\n",
"k=0\n",
"for i in EL1: \n",
" k=k+1\n",
" output_file.write(str(k),)\n",
" output_file.write(',')\n",
" output_file.write(str(i[0]),)\n",
" output_file.write(',')\n",
" output_file.write(str(i[1]),)\n",
" output_file.write(',')\n",
" output_file.write(str(i[2])+'\\n')\n",
"\n",
"output_file.write('*Element, type = COH2D4 \\n')\n",
"k=0 \n",
"for i in EL2: \n",
" k=k+1\n",
" output_file.write(str(n*m*4+k),)\n",
" output_file.write(',')\n",
" output_file.write(str(int(EL2[k-1][0])),)\n",
" output_file.write(',')\n",
" output_file.write(str(int(EL2[k-1][1])),)\n",
" output_file.write(',')\n",
" output_file.write(str(int(EL2[k-1][2])),)\n",
" output_file.write(',')\n",
" output_file.write(str(int(EL2[k-1][3]))+'\\n')\n",
"non_empty_set_count = 0\n",
"for sublist in set: # traingular element set segregation \n",
" if sublist:\n",
" k=0\n",
" output_file.write('\\n')\n",
" non_empty_set_count += 1\n",
" # print(f\"set{non_empty_set_count}:\", end=\" \")\n",
" output_file.write(f\"*Elset, elset=Set-{non_empty_set_count}\\n\")\n",
" for value in sublist:\n",
" output_file.write(str(value),)\n",
" output_file.write(',')\n",
" k=k+1\n",
" if k%12==0: \n",
" output_file.write('\\n')\n",
"non_empty_set_count = 0\n",
"for sublist in setc: # quadratic element set segregation \n",
" if sublist:\n",
" k=0\n",
" output_file.write('\\n')\n",
" non_empty_set_count += 1\n",
" # print(f\"set{non_empty_set_count}:\", end=\" \")\n",
" output_file.write(f\"*Elset, elset=Setc-{non_empty_set_count}\\n\")
\n",
" for value in sublist:\n",
" output_file.write(str(value+n*m*4),)\n",
" output_file.write(',')\n",
" k=k+1\n",
" if k%12==0:\n",
" output_file.write('\\n')\n",
" \n",
"output_file.write('\\n') \n",
"output_file.write('*Elset, elset=Setg-5 \\n') #cohesive set Grain boundary\n",
"k=0\n",
"for value in set5:\n",
"\n",
" output_file.write(str(value+n*m*4),)\n",
" output_file.write(',')\n",
" k=k+1\n",
" if k%12==0:\n",
" output_file.write('\\n')\n",
"# new addition \n",
"output_file.write('\\n') \n",
"output_file.write('*Elset, elset=Setallt \\n') #all the triangular element in
a \"setallt\"\n",
"non_empty_set_count = 0\n",
"k=0\n",
"for sublist in set: \n",
" if sublist:\n",
" for value in sublist:\n",
" output_file.write(str(value),)\n",
" output_file.write(',')\n",
" k=k+1\n",
" if k%12==0:\n",
" k=0\n",
" output_file.write('\\n') \n",
"output_file.write('\\n') \n",
"output_file.write('*Elset, elset=Setallc \\n')\n",
"non_empty_set_count = 0\n",
"k=0\n",
"for sublist in setc: #all the triangular element in a \"setallc\" \n",
" if sublist:\n",
" for value in sublist:\n",
" output_file.write(str(value+n*m*4),)\n",
" output_file.write(',')\n",
" k=k+1\n",
" if k%12==0:\n",
" k=0\n",
" output_file.write('\\n')\n",
"# node set segregation \n",
"output_file.write('\\n')\n",
"output_file.write('*Nset, nset=specimentop \\n')\n",
"non_empty_set_count = 0\n",
"k=0\n",
"a=-9\n",
"l=1\n",
"for i in range(0,(m)):\n",
" a=a+9\n",
" for j in range(1,5):\n",
" output_file.write(str(l+a),)\n",
" output_file.write(',')\n",
" if j<4:\n",
" l=l+1\n",
" k=k+1\n",
" if k%12==0:\n",
" k=0\n",
" output_file.write('\\n') \n",
"output_file.write('\\n')\n",
"output_file.write('*Nset, nset=specimenbottom \\n')\n",
"non_empty_set_count = 0\n",
"k=0\n",
"a=-9\n",
"l=m*(n-1)*12+5\n",
"for i in range(0,(m)):\n",
" a=a+9\n",
" for j in range(1,5):\n",
" output_file.write(str(l+a),)\n",
" output_file.write(',')\n",
" if j<4:\n",
" l=l+1\n",
" k=k+1\n",
" if k%12==0:\n",
" k=0\n",
" output_file.write('\\n')\n",
"output_file.write('\\n')\n",
"output_file.write('*Nset, nset=specimenright \\n')\n",
"non_empty_set_count = 0\n",
"k=0\n",
"a=1\n",
"for i in range(0,(n)):\n",
" for j in range(1,5):\n",
" output_file.write(str((a*m-1)*12+2+j),)\n",
" output_file.write(',')\n",
" k=k+1\n",
" if k%12==0:\n",
" k=0\n",
" output_file.write('\\n')\n",
" a=a+1 \n",
"output_file.write('\\n')\n",
"output_file.write('*Nset, nset=specimenleft \\n')\n",
"non_empty_set_count = 0\n",
"k=0\n",
"a=0\n",
"for i in range(0,(n)):\n",
" for j in range(1,5):\n",
" if j<3:\n",
" output_file.write(str((a*m)*12+j),)\n",
" output_file.write(',')\n",
" if j>2:\n",
" output_file.write(str((a*m)*12+j+4),)\n",
" output_file.write(',')\n",
" k=k+1\n",
" if k%12==0:\n",
" k=0\n",
" output_file.write('\\n')\n",
" a=a+1 \n",
"output_file.write('\\n')\n",
"output_file.write('*Solid Section, elset=Set-1, material=Material-1 \\n')\n",
"output_file.write(', \\n')\n",
"output_file.write('*Solid Section, elset=Set-2, material=Material-2 \\n')\n",
"output_file.write(', \\n')\n",
"output_file.write('*Solid Section, elset=Set-3, material=Material-3 \\n')\n",
"output_file.write(', \\n')\n",
"output_file.write('*End Part \\n')\n",
"output_file.write('*Material, name = Material-1 \\n')\n",
"output_file.write('*Elastic \\n')\n",
"output_file.write('1e+6,0.3 \\n')\n",
"output_file.write('*Material, name = Material-2 \\n')\n",
"output_file.write('*Elastic \\n')\n",
"output_file.write('2e+6,0.25 \\n')\n",
"output_file.write('*Material, name = Material-3 \\n')\n",
"output_file.write('*Elastic \\n')\n",
"output_file.write('2e+6,0.25 \\n')\n",
"output_file.write('**Output, field, variable=PRESELECT \\n')\n",
"output_file.write('**Output, history, variable=PRESELECT \\n')\n",
"output_file.write('*End Step \\n')"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"25"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Successfully converted voronoiwithsizecontrol20MICRONupdate1x1.txt to
output.inp.\n"
]
}
],
"source": [
"def convert_txt_to_inp(txt_file, inp_file):\n",
" \"\"\"\n",
" Convert a text file into an Abaqus .inp input file.\n",
"\n",
" Parameters:\n",
" txt_file (str): Path to the input text file.\n",
" inp_file (str): Path to the output Abaqus .inp file.\n",
" \"\"\"\n",
" try:\n",
" # Open the text file for reading\n",
" with open(txt_file, 'r') as txt:\n",
" # Read the content of the text file\n",
" content = txt.readlines()\n",
"\n",
" # Open the .inp file for writing\n",
" with open(inp_file, 'w') as inp:\n",
" # Write a standard Abaqus header\n",
" inp.write('*Heading\\n')\n",
" inp.write('** Converted from text file\\n')\n",
"\n",
" # Write the contents of the .txt file\n",
" for line in content:\n",
" inp.write(line)\n",
"\n",
" print(f\"Successfully converted {txt_file} to {inp_file}.\")\n",
" except FileNotFoundError:\n",
" print(f\"Error: {txt_file} not found.\")\n",
" except Exception as e:\n",
" print(f\"An error occurred: {e}\")\n",
"\n",
"\n",
"# Specify the input and output file names\n",
"txt_filename = 'input.txt' # Replace with your .txt file\n",
"inp_filename = 'output.inp' # Replace with your desired .inp file name\n",
"\n",
"# Call the function to perform the conversion\n",
"convert_txt_to_inp('voronoiwithsizecontrol20MICRONupdate1x1.txt',
inp_filename)\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'abaqus'",
"output_type": "error",
"traceback": [

"\u001b[1;31m----------------------------------------------------------------------
-----\u001b[0m",
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback
(most recent call last)",
"Cell \u001b[1;32mIn[24], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m
\u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mabaqus\u001b[39;00m
\u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;241m*\u001b[39m\n\u001b[0;32m
2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m
\u001b[38;5;21;01mabaqusConstants\u001b[39;00m
\u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;241m*\u001b[39m\n\u001b[0;32m
3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m
\u001b[38;5;21;01mcaeModules\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m
\u001b[38;5;241m*\u001b[39m\n",
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'abaqus'"
]
}
],
"source": [
"from abaqus import *\n",
"from abaqusConstants import *\n",
"from caeModules import *\n",
"\n",
"def txt_to_cae(txt_file, cae_file):\n",
" # Create a new model\n",
" myModel = mdb.Model(name=\"MyModel\")\n",
" part = myModel.Part(name=\"MyPart\", dimensionality=THREE_D,
type=DEFORMABLE_BODY)\n",
"\n",
" # Read the .txt file\n",
" nodes = []\n",
" elements = []\n",
" with open(txt_file, 'r') as txt:\n",
" lines = txt.readlines()\n",
" for line in lines:\n",
" if line.startswith(\"N\"): # Nodes\n",
" data = line[1:].strip().split(\",\")\n",
" nodes.append((int(data[0]), float(data[1]), float(data[2]),
float(data[3])))\n",
" elif line.startswith(\"E\"): # Elements\n",
" data = line[1:].strip().split(\",\")\n",
" elements.append((int(data[0]), tuple(map(int, data[1:]))))\n",
"\n",
" # Add nodes to the part\n",
" part.addNodes(\n",
" nodeData=[(n[1], n[2], n[3]) for n in nodes],\n",
" nodeSetName=\"AllNodes\"\n",
" )\n",
"\n",
" # Add elements to the part\n",
" part.addElements(\n",
" elementData=[(e[0], e[1]) for e in elements],\n",
" elemType=mesh.ElemType(elemCode=C3D4) # Example: tetrahedral
elements\n",
" )\n",
"\n",
" # Save as .cae\n",
" mdb.saveAs(pathName=cae_file)\n",
"\n",
"# Call the function\n",
"txt_to_cae('voronoiwithsizecontrol20MICRONupdate1x1.txt', \"mesh_data.cae\")
\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy