close all; clear; clc; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % This code has been created by Alireza Karimi and Seyed Mohammad Ali Rahmati % % from Downs lab at the Department of Ophthalmology and Visual Sciences, % % The University of Alabama at Birmingham. All rights of this work is reserved for % Professor Crawford Downs. % % Email: smarahmati@gmail.com (Seyed Mohammad Ali Rahmati) % % Email: akarimi@uabmc.edu (Alireza Karimi) % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % This M-file reads inp file of tetrahedral mesh (C3D4), divides each tetrahedral element % to 4 Hexagonal elements and creats the corresponding inp file %************** % After running the M-file, it is requested to insert: % The name of tetrahedral mesh as an inp file (with the suffix inp) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% prompt = {'Enter the name of model in inp format (Exp: Example_Tetrahedral.inp):'}; dlg_title = 'Input'; num_lines = 1; defaultans = {'Example_Tetrahedral.inp'}; answer = inputdlg(prompt,dlg_title,num_lines,defaultans); if isempty(answer)==1 return end %% Read fname = answer{1,1} ; fid = fopen(fname,'rt') ; S = textscan(fid,'%s','Delimiter','\n'); S = S{1} ; % Get the line number of NODE & ElEMENT idxS = strfind(S, 'NODE'); idx1 = find(not(cellfun('isempty', idxS))); idxS = strfind(S, 'ELEMENT'); idx2 = find(not(cellfun('isempty', idxS))); % Pick nodes nodes = S(idx1+1:idx2-1) ; nodes = cell2mat(cellfun(@str2num,nodes,'UniformOutput',false)); % Pick elements elements = S(idx2+1:end) ; elements = cell2mat(cellfun(@str2num,elements,'UniformOutput',false)); %% Additional nodes % length of added nodes [LAddedN, col] = size(elements); % original nodes Org_nodes = nodes; % node centers of 1 & 2 (node 5) nodes5(:,2:4) = (Org_nodes(elements(:,1+1),2:4) + Org_nodes(elements(:,2+1),2:4))/2; nodes5(:,1) = nodes(end,1)+1:nodes(end,1)+LAddedN; nodes = [nodes; nodes5]; % node centers of 2 & 3 (node 6) nodes6(:,2:4) = (Org_nodes(elements(:,2+1),2:4) + Org_nodes(elements(:,3+1),2:4))/2; nodes6(:,1) = nodes(end,1)+1:nodes(end,1)+LAddedN; nodes = [nodes; nodes6]; % node centers of 1 & 3 (node 7) nodes7(:,2:4) = (Org_nodes(elements(:,1+1),2:4) + Org_nodes(elements(:,3+1),2:4))/2; nodes7(:,1) = nodes(end,1)+1:nodes(end,1)+LAddedN; nodes = [nodes; nodes7]; % node centers of 1 & 4 (node 8) nodes8(:,2:4) = (Org_nodes(elements(:,1+1),2:4) + Org_nodes(elements(:,4+1),2:4))/2; nodes8(:,1) = nodes(end,1)+1:nodes(end,1)+LAddedN; nodes = [nodes; nodes8]; % node centers of 2 & 4 (node 9) nodes9(:,2:4) = (Org_nodes(elements(:,2+1),2:4) + Org_nodes(elements(:,4+1),2:4))/2; nodes9(:,1) = nodes(end,1)+1:nodes(end,1)+LAddedN; nodes = [nodes; nodes9]; % node centers of 3 & 4 (node 10) nodes10(:,2:4) = (Org_nodes(elements(:,3+1),2:4) + Org_nodes(elements(:,4+1),2:4))/2; nodes10(:,1) = nodes(end,1)+1:nodes(end,1)+LAddedN; nodes = [nodes; nodes10]; % node centers of 1, 2 & 3 (node 11) nodes11(:,2:4) = (Org_nodes(elements(:,1+1),2:4) + Org_nodes(elements(:,2+1),2:4)+ Org_nodes(elements(:,3+1),2:4))/3; nodes11(:,1) = nodes(end,1)+1:nodes(end,1)+LAddedN; nodes = [nodes; nodes11]; % node centers of 1, 2 & 4 (node 12) nodes12(:,2:4) = (Org_nodes(elements(:,1+1),2:4) + Org_nodes(elements(:,2+1),2:4)+ Org_nodes(elements(:,4+1),2:4))/3; nodes12(:,1) = nodes(end,1)+1:nodes(end,1)+LAddedN; nodes = [nodes; nodes12]; % node centers of 1, 3 & 4 (node 13) nodes13(:,2:4) = (Org_nodes(elements(:,1+1),2:4) + Org_nodes(elements(:,3+1),2:4)+ Org_nodes(elements(:,4+1),2:4))/3; nodes13(:,1) = nodes(end,1)+1:nodes(end,1)+LAddedN; nodes = [nodes; nodes13]; % node centers of 2, 3 & 4 (node 14) nodes14(:,2:4) = (Org_nodes(elements(:,2+1),2:4) + Org_nodes(elements(:,3+1),2:4)+ Org_nodes(elements(:,4+1),2:4))/3; nodes14(:,1) = nodes(end,1)+1:nodes(end,1)+LAddedN; nodes = [nodes; nodes14]; % node centers of 1, 2, 3 & 4 (node 15) nodes15(:,2:4) = (Org_nodes(elements(:,1+1),2:4) + Org_nodes(elements(:,2+1),2:4)+ Org_nodes(elements(:,3+1),2:4)+ Org_nodes(elements(:,4+1),2:4))/4; nodes15(:,1) = nodes(end,1)+1:nodes(end,1)+LAddedN; nodes = [nodes; nodes15]; elements2 = [elements, nodes5(:,1), nodes6(:,1), nodes7(:,1), nodes8(:,1), nodes9(:,1), ... nodes10(:,1), nodes11(:,1), nodes12(:,1), nodes13(:,1), nodes14(:,1), nodes15(:,1)]; %% Tetrahedral to 4 hexagonal % Qubic 1 Hex1_elements(:,2) = elements2(:,1+1); Hex1_elements(:,3) = elements2(:,5+1); Hex1_elements(:,4) = elements2(:,11+1); Hex1_elements(:,5) = elements2(:,7+1); Hex1_elements(:,6) = elements2(:,8+1); Hex1_elements(:,7) = elements2(:,12+1); Hex1_elements(:,8) = elements2(:,15+1); Hex1_elements(:,9) = elements2(:,13+1); % Qubic 2 Hex2_elements(:,2) = elements(:,2+1); Hex2_elements(:,3) = elements2(:,6+1); Hex2_elements(:,4) = elements2(:,11+1); Hex2_elements(:,5) = elements2(:,5+1); Hex2_elements(:,6) = elements2(:,9+1); Hex2_elements(:,7) = elements2(:,14+1); Hex2_elements(:,8) = elements2(:,15+1); Hex2_elements(:,9) = elements2(:,12+1); % Qubic 3 Hex3_elements(:,2) = elements(:,3+1); Hex3_elements(:,3) = elements2(:,7+1); Hex3_elements(:,4) = elements2(:,11+1); Hex3_elements(:,5) = elements2(:,6+1); Hex3_elements(:,6) = elements2(:,10+1); Hex3_elements(:,7) = elements2(:,13+1); Hex3_elements(:,8) = elements2(:,15+1); Hex3_elements(:,9) = elements2(:,14+1); % Qubic 4 Hex4_elements(:,2) = elements(:,4+1); Hex4_elements(:,3) = elements2(:,9+1); Hex4_elements(:,4) = elements2(:,12+1); Hex4_elements(:,5) = elements2(:,8+1); Hex4_elements(:,6) = elements2(:,10+1); Hex4_elements(:,7) = elements2(:,14+1); Hex4_elements(:,8) = elements2(:,15+1); Hex4_elements(:,9) = elements2(:,13+1); Hex_elements = [Hex1_elements; Hex2_elements; Hex3_elements; Hex4_elements]; [Row_Hex, Col_Hex] = size(Hex_elements); Hex_elements(:,1) = (1:Row_Hex)'; %% Creating inp file Nodes = nodes(:,2:end); Elements_D = Hex_elements(:,2:end); Elements = []; for i=1:Row_Hex Elements{i} = Elements_D(i,:); end Elements_Sets{1}.Name='Set1'; %Element set name Elements_Sets{1}.Elements_Type='C3D8'; %Type of elements in the element set Elements_Sets{1}.Elements=1:Row_Hex; %Elements indices vectors in the element set currentFolder = pwd; Filename=append('\',erase(answer{1,1},"_Tetrahedral.inp"),'_','_Hexagonal','.inp'); SaveFolder = strcat(currentFolder,Filename); Matlab2Abaqus(Nodes,Elements,Elements_Sets,SaveFolder) %% Matlab to Abaqus function Matlab2Abaqus(Nodes,Elements,Elements_Sets,Filename) fileID = fopen(Filename, 'w'); %Generate Nodes in Input File fprintf(fileID,'*NODE, NSET=NODE\n'); [NNode, ND]=size(Nodes); if ND==2 %2D for i=1:1:NNode fprintf(fileID,[num2str(i) ', ' num2str(Nodes(i,1)) ', ' num2str(Nodes(i,2)) '\n']); end elseif ND==3 %3D for i=1:1:NNode fprintf(fileID,[num2str(i) ', ' num2str(Nodes(i,1)) ', ' num2str(Nodes(i,2)) ', ' num2str(Nodes(i,3)) '\n']); end end fprintf(fileID,'\n'); %Generate Elements in Input File for i=1:1:length(Elements_Sets) fprintf(fileID,strcat('*ELEMENT, ELSET=',Elements_Sets{i}.Name,', TYPE=',Elements_Sets{i}.Elements_Type,'\n')); for j=1:1:length(Elements_Sets{i}.Elements) %Loop for the elements in the elements set IE=Elements_Sets{i}.Elements(j); %Elements indices in elements sets NNN=[num2str(IE) ', ']; for k=1:1:length(Elements{IE}) NNN=[NNN num2str(Elements{IE}(k)) ', ']; end NNN=NNN(1:end-2); fprintf(fileID,[NNN '\n']); end fprintf(fileID,'\n'); end fclose(fileID); end