a 2D version
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

78 lines
2.9 KiB

function [nodes, eles, eleNum, nodeNum] = GenerateMesh(lx, ly, numx, numy, removedE)
% To Mesh a membrane using 4 noded Elements
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% Purpose:
% To Mesh a square/rectangular membrane to use in FEM Analysis
% Synopsis :
% [coordinates,nodes,nel,nnode] = GenerateMesh(lx,ly,numx,numy)
% Variable Description:
% Input :
% lx - length of the membrane along X-axes
% ly - breadth of the membrane along Y-axes
% numx - number of Elements along X-axes
% numy - number of Elements along Y-axes
% Output :
% nodes - The nodal coordinates of the mesh
% -----> nodes = [node X Y]
% arranged by cols: x = 0,y = 10:0 ...
% eles - The nodal connectivity of the elements
% arranged by cols: ex = 0,ey = 10:0 ...
% Nodes sequence in one element:
% _______ 0 _____ x
% | 1 2 | |
% | | |
% |_4___3_| y
% -----> nodes = [node1 node2......]
% nel- total number of elements
% nnodes- total number of nodes
%--------------------------------------------------------------------------
% lx = numx;
% ly = numy;
eleNum = numx * numy; % Total Number of Elements in the Mesh
nnel = 4; % Number of nodes per Element
% Number of points on the Length and Breadth
npx = numx + 1;
npy = numy + 1;
nodeNum = npx * npy; % Total Number of Nodes in the Mesh
% Discretizing the Length and Breadth of the membrane
nx = linspace(0, lx, npx);
ny = linspace(0, ly, npy);
[xx, yy] = meshgrid(nx, fliplr(ny));
XX = xx; YY = yy;
nodes = zeros(nodeNum, 2);
nodes(:, [1, 2]) = [XX(:) YY(:)];
% To get the Nodal Connectivity Matrix
NodeNo = 1:nodeNum;
eles = zeros(eleNum, nnel);
% small code for selecting nodal point number for specific element
NodeNo = reshape(NodeNo, npy, npx);
eles(:, 4) = reshape(NodeNo(1:npy - 1, 1:npx - 1), eleNum, 1);
eles(:, 3) = reshape(NodeNo(1:npy - 1, 2:npx), eleNum, 1);
eles(:, 2) = reshape(NodeNo(2:npy, 2:npx), eleNum, 1);
eles(:, 1) = reshape(NodeNo(2:npy, 1:npx - 1), eleNum, 1);
% if have removedE
if nargin > 4
removedN1 = eles(removedE, :);
eles(removedE, :) = [];
removedNodes = setdiff(removedN1(:), eles(:));
nodes(:, 3) = 1:nodeNum;
nodes(removedNodes, :) = [];
nodeNum = size(nodes, 1);
eleNum = size(eles, 1);
% eles still save old nodes idx, repair it
for i = 1:4
[~, r] = intersect(nodes(:, 3), eles(:, i));
eles(:, i) = r;
end
end
% figure;
% scatter(nodes(:,1),nodes(:,2),'filled');