Dear Matlab Community,
how can I export a FEMesh created in Matlab?
Alternatively, it would also help me to just regularise the imported stl-file and then export it again as a stl-file.
Here is my code so far:
meshSize=0.05;
meshGrowthRate=1.1;
meshOrder='quadratic';
% import any unordered stl-file
model = createpde(1);
importGeometry(model,'geometry.stl');
% generation of a regular FE mesh
mesh=generateMesh(model,'Hmax',meshSize,'Hmin',meshSize,'Hgrad',meshGrowthRate,'GeometricOrder',meshOrder);
pdeplot3D(model)
I am very grateful for any help.
Best regards,
FG

 Accepted Answer

Wan Ji
Wan Ji on 23 Aug 2021
The mesh is now the field of model
you can get mesh by
Mesh = model.Mesh;
% nodes are obtained by
Nodes = Mesh.Nodes; % d-by-n matrix where d is the number of dimension
% elements are obtained by
Elements = Mesh.Elements; % s-by-m matrix where s is the number of nodes of one element

7 Comments

Wan Ji
Wan Ji on 23 Aug 2021
Edited: Wan Ji on 23 Aug 2021
I donot know what kind of format you want to export the mesh. Now that you have got the nodes and elements of this mesh. You can do anything you like to perform the fem analysis.
Fabian Günther
Fabian Günther on 23 Aug 2021
Edited: Fabian Günther on 23 Aug 2021
I would like to export the mesh in order to import it afterwards in another FE program (Abaqus), edit it and finally solve it there.
However, it would also be possible, if the entire model cannot be exported, to export only the surface as an .stl file.
Use the following function to write to an abaqus inp file
function toinp(Mesh, fileName, ElementType)
Mesh.Elements = Mesh.Elements';
Mesh.Nodes = Mesh.Nodes';
if(~strcmpi(fileName(end-3:end),'.inp'))
fileName = [fileName,'.inp'];
end
while(exist(fileName,'file'))
s = input('File name already exists, are you sure to overwrite it (1 for yes and 0 for no)?');
switch s
case {'yes', 1}
break;
otherwise
fileName = input('Please input a new file name:');
end
end
fid = fopen(fileName,'wt');
fprintf(fid,'*HEADING\n');
fprintf(fid,'%s\n**\n',fileName);
fprintf(fid,'*Node\n');
for i = 1:1:size(Mesh.Nodes,1)
ndim = size(Mesh.Nodes,2);
fprintf(fid, ['%d',repmat(',%f', 1, ndim),'\n'], i, Mesh.Nodes(i,:));
end
switch lower(ElementType)
case{'c2d3','c2d4','c2d6','c2d8'}
eType = ElementType;
eType(2:3) = 'ps';
otherwise
eType = ElementType;
end
fprintf(fid,'*Element, Type=%s\n', upper(eType));
for i = 1:1:size(Mesh.Elements,1)
nenode = size(Mesh.Elements,2);
fprintf(fid,['%d',repmat(',%d', 1, nenode),'\n'], i, Mesh.Elements(i,:));
end
fclose(fid);
end
For example
toinp(Mesh, 'myinp.inp', 'c3d10')
Thank you for this powerful function!
Unfortunately, I get the error message when I use it:
Unable to set the 'Elements' property of class 'FEMesh' because it is read-only.
Do you have any idea how I can fix this?
Mesh = model.Mesh;
% nodes are obtained by
Nodes = Mesh.Nodes; % d-by-n matrix where d is the number of dimension
% elements are obtained by
Elements = Mesh.Elements; % s-by-m matrix where s is the number of nodes of one element
myMesh.Nodes = Nodes;
myMesh.Elements = Elements; % Now myMesh is a struct, not femMesh class
toinp(myMesh, 'myinp.inp', 'c3d10')
Wan Ji
Wan Ji on 23 Aug 2021
Edited: Wan Ji on 23 Aug 2021
Or just have a little modification on the function
function toinp(MeshIn, fileName, ElementType)
Mesh.Elements = MeshIn.Elements';
Mesh.Nodes = MeshIn.Nodes';
if(~strncmpi(fileName(end:-1:1),'pni.',4))
fileName = [fileName,'.inp'];
end
while(exist(fileName,'file'))
s = input('File name already exists, are you sure to overwrite it (1 for yes and 0 for no)?');
switch s
case {'yes', 1}
break;
otherwise
fileName = input('Please input a new file name:','s');
end
end
if(~strncmpi(fileName(end:-1:1),'pni.',4))
fprintf('File name not with suffix ''.inp'', program will add it\n')
fileName = [fileName,'.inp'];
end
fid = fopen(fileName,'wt');
fprintf(fid,'*HEADING\n');
fprintf(fid,'%s\n**\n',fileName);
fprintf(fid,'*Node\n');
for i = 1:1:size(Mesh.Nodes,1)
ndim = size(Mesh.Nodes,2);
fprintf(fid, ['%d',repmat(',%f', 1, ndim),'\n'], i, Mesh.Nodes(i,:));
end
switch lower(ElementType)
case{'c2d3','c2d4','c2d6','c2d8'}
eType = ElementType;
eType(2:3) = 'ps';
otherwise
eType = ElementType;
end
fprintf(fid,'*Element, Type=%s\n', upper(eType));
for i = 1:1:size(Mesh.Elements,1)
nenode = size(Mesh.Elements,2);
fprintf(fid,['%d',repmat(',%d', 1, nenode),'\n'], i, Mesh.Elements(i,:));
end
fclose(fid);
fprintf('Output Inp File Successfully!\n')
end
Then use
Mesh = model.Mesh;
toinp(Mesh, 'myinp.inp', 'c3d10')
It is OK now
Thanks alot man!

Sign in to comment.

More Answers (0)

Products

Release

R2021a

Asked:

on 23 Aug 2021

Edited:

on 23 Aug 2021

Community Treasure Hunt

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

Start Hunting!