【Matlab函数】提取inp文件中的节点、单元数据并保留集合信息

功能

  • 提取hypermesh2020(其他版本也可以)中的节点、单元信息,并保留elem set信息。
  • 返回的是一个结构体

输入输出参数

输入:

  • inp文件路径,如:'example.inp'、"inp\ex.inp"

输出:

  • 一个结构体,包含节点信息、单元信息、单元集合信息、部件名字。
Struct
 -- Name
 -- Nodes
 -- ElemSets

matlab代码

% --------------------------------------------------------------- %
% Loads the inp(generated by HM2020) data to a parts struct array %
% --------------------------------------------------------------- %

function part = loadinp_GeByHm2020(inp)
    % inp='SmallLXJ_TETmesh_size2mm-20230704.inp';

    % Get and process text from the file
    text = fileread(inp);         % read all the text
    text = strtrim(text);         % remove leading and trailing whitespaces
    text = strrep(text, ' ', ''); % remove any in-between whitespaces
    text = lower(text);           % lower the case
    text = inpremcom(text);       % remove comment lines from inp text data
    
    % get nodes infomation
    data =strsplit(text, '*element')';% split char vector at '*element'——>n X 1 的cell array;
    
    % create struct :part
    name=strsplit(inp,'.');
    part.Name=name{1};
    
    % Extract nodes data
    nodes=data{1};
    part.Nodes=getnodes(nodes);
    % Extract set data
    % create a cell array ,contain each elemset struct
    ElementSets=cell(size(data,1)-1,1);
    numOftet=0;

    for i=1:1:size(ElementSets,1)
        % a char vector including element define info: setname,elemtype,elem label,connectivity
        elemsetInfo=data{i+1};   
        ElementSets{i}=getelementOFset(elemsetInfo);
        numOftet=numOftet+size(ElementSets{i}.Elements,1);
    end
    part.ElemSets=ElementSets;
    %show number of tet elements
    disp(['**number of tet =',num2str(numOftet)])
end

function output = inpremcom(text) 
    
    % ---------------------------------------- %
    % Removes comment lines from inp text data %
    % ---------------------------------------- %
    
    text = splitlines(text);% split string at the actual \n ,rather than char \n。
    
    output = {};
    j = 1;
    
    for i = 1:1:size(text, 1)
        if ~startsWith(text{i}, '**')
            output{j, 1} = text{i};
            j = j + 1;
        end
    end
    % 文本\n 会转换为实际的换行符
    output = strjoin(output, '\n');%将元胞数组中的字符向量联接为一个字符向量。指定\n作为分隔符。
    
end

function Nodes = getnodes(nodes)
    
    % --------------------------------------------------------------------------- %
    % Gets the nodes from the *node data,return a containers.Map var called Nodes %
    % --------------------------------------------------------------------------- %
    %create a empty Map var
    Nodes=containers.Map('KeyType','double','ValueType','any');
    % loop *node data
    lines = splitlines(nodes);
    for i = 1:1:size(lines, 1)
        line = lines{i};
        if contains(line, '*node')
            for j = (i + 1):1:size(lines, 1)
                line = lines{j};
                if contains(line, '*') || isempty(line)
                    return
                else
                    data = str2num(line);
                    % key: node label
                    % value: 1X1 cell array ,contain coordinates value
                    Nodes(data(1)) = {data(2:end)};
                end
            end
        end
    end
    
end

function ElemSet = getelementOFset(elemdata)
    
    % --------------------------------------------------------- %
    % Gets the elements from the *element data ,return a struct %
    % ----------------------------------------------------------%
    
    ElemSet=struct;
    %create a empty Map var
    Els=containers.Map('KeyType','double','ValueType','any');
    
    lines = splitlines(elemdata);
    
    % read elemsert's name and elemtype
    if contains(lines{1},'elset')
        line_1=strsplit(lines{1},',');
        for i=2:1:size(line_1,2)
            tempcell=strsplit(line_1{i},'=');
            if strcmp( tempcell{1},'type')
                ElemSet.ElemType=tempcell{2};
            elseif strcmp(tempcell{1},'elset')
                ElemSet.SetName=tempcell{2};
            end
        end
    end
    
    % read element's label and connectivity 
    for i = 2:1:size(lines, 1)
        line = lines{i};
        if contains(line, '*') || isempty(line)
            break;
        else
            data = str2num(line);
            % key: elem label
            % value: 1X1 cell array ,contain elem connectivity
            Els(data(1)) = {data(2:end)};
        end
    end
    
    ElemSet.Elements=Els;
end
posted @ 2023-07-14 13:22  FE-有限元鹰  阅读(712)  评论(0编辑  收藏  举报