How to read the .inp file generated in gmsh for extracting the Nodal coordinates and element connectivity of the mesh.

12 次查看(过去 30 天)
I have a 2D structured mesh of a 2D domain, as shown, generated by gmsh, an open source meshing software. After generating the mesh, I exported the file as .inp file, which I have attached.
From the line no. 373 to 596, are the element nodal connectivity. which I have to extract
From line no. 4 to 272, are the (x,y) coordinate of each node. which also have to be extracted.
kindly help me on extracting these info and placing them in an array variables.

回答(1 个)

Paras Gupta
Paras Gupta 2024-5-20
Hello Nandha,
I understand that you want to read the INP file provided in the question and extract the coordiates of the nodes and element nodal connectivity information from the file. The INP file can be opened using the 'fopen' function in MATLAB. The following code shows one way to extract the required information from the INP file:
% Initialize arrays to hold node and element data
nodes = [];
elements = [];
% Open the file for reading
fid = fopen('mesh_doubt1.inp', 'rt');
% Flags to identify the sections
readingNodes = false;
readingElements = false;
while ~feof(fid)
line = strtrim(fgets(fid)); % Read a line and trim whitespace
% Check for the nodes section
if startsWith(line, '*NODE', 'IgnoreCase', true)
readingNodes = true;
readingElements = false;
continue;
end
% Check for the elements section of type CPS4 and ELSET=Surface1
if contains(line, 'type=CPS4', 'IgnoreCase', true) && contains(line, 'ELSET=Surface1', 'IgnoreCase', true)
readingElements = true;
readingNodes = false;
continue;
end
% Check if we are exiting a relevant section
if startsWith(line, '*') && ~startsWith(line, '*NODE') && ~contains(line, 'type=CPS4', 'IgnoreCase', true)
readingNodes = false;
readingElements = false;
end
% Read and store nodes
if readingNodes
nodeData = sscanf(line, '%f, %f, %f, %f')';
nodes = [nodes; nodeData];
end
% Read and store elements of type CPS4 and ELSET=Surface1
if readingElements
elementData = sscanf(line, '%f, %f, %f, %f, %f')';
elements = [elements; elementData];
end
end
% Close the file
fclose(fid);
For more information on the functions used in the above code, you can refer to the documentations links below:
Hope this answers the query.

产品


版本

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by