Вы находитесь на странице: 1из 12

Middle East Technical University

Mechanical Engineering Department


ME 582 Finite Element Analysis in Thermofluids
Fall 2014 (Dr. Sert)
How to Generate 2D Input Files
steady2d.m code needs an input file, which is not easy to generate by hand. mesh2d is a mesh generator writteninMATLAB
by Darren Engwirda. It can freely be downloaded at http://www.mathworks.com/matlabcentral/fileexchange/25555mesh2d-automatic-mesh-generation . It generates meshes of triangular elements over 2D domains. To see sample meshes
created by it you can run meshdemo and mesh_collection(#) files that come with it. Latest version of mesh2d is 2.4
and it needs two small tweks to work with newer versions of MATLAB. They are explained in the above downloadwebpage.
The main difficulty in using a third party mesh generator like mesh2d is to convert its output into the format of the input
files that is understood by our FEM solvers. For this purpose we'll use a MATLAB code called generate2DinputFile.m. It is
available at the course web site.
This document explains how to use generate2DinputFile.m code by the help of the Chinmey problem that was introduced
in a previous handout. Details of the problem are as follows
This document explains how to use generate2DinputFile.m code by the help of examples. Let's start with the followingheat
conduction problem, which was a part of a previous homework. Coordinates shown below are in mm.

generate2DinputFile.m code is divided into 5 parts. Beginning of each part is labeled with comments to be detectedeasily.
Problem dependent parameters that need to be modified for each problem are also mentioned at the beginning of each
part.
In Part 1 shown below, we first select number of Gauss Quadrature Points, NGP. Also functions of the differential equation
are specified as strings. For solving a conduction problem with a conductivity of 25 W/(mK) , kxxFunc and kyyFunc
parameters are equated to this value. All other functions are zero.
We then provide the number of BCs. For this problem there are 3 different boundary conditions and their typesare provided
in the BCtype array. Boundary condition types supported are
1: Essential BC (EBC)

2: Natural BC (NBC)

3: Mixed BC (MBC)
1

First BC is selected to be the insulated boundary, which is of type 2. The other two BCs are of mixed type, i.e. of type 3. W e
do not have any EBCs for this problem.
In Part 1 we also provide the specified values/functions for these three BCs in the BCstr cell array. Cell arrays are defined
using braces instead of brackets. BCstr has two strings for each BC. For EBCs and NBCs only the first string is used to
specify either the primary variable or the secondary variable. Here the first BC is of type NBC and we need to specify the
known secondary variable, which is zero for an insulated boundary. Therefore the first string of the first BC is '0.0'.
Second and third BCs are of mixed type and both strings are used to specify and values. The numbering of BCs is a user
choice. It is possible to change their order in the way we want.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% PART 1. CONSTANTS and BOUNDARY CONDITION DATA
%
%
%
% Problem dependent parameters are NGP, functions of the DE, nBC, BCtype %
% and BCstr.
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global LtoG coord itemCoord
eType = 2;
NEN = 3;
NGP = 4;
kxxFunc = '25.0';
kyyFunc = '25.0';
fFunc = '0.0';
nBC = 3;

%
%
%
%

mesh2d
Number
have 3
Number

generates only triangular elements.


of an element's node. Bi-linear triangles
nodes.
of GQ points that'll appear in the input file.

% Functions of the Differential Equation.

% Number of specified BCs.

% Specify BC type in an array of length nBC. 1: EBC, 2: NBC, 3: MBC


BCtype = [2 3 3];
% Specify PVs, SVs or alpha and beta values for BCs as strings in a cell
% array of size nBCx2.
BCstr = {'0.0'
'0.0';
'-200'
'8e4';
'-1000'
'1.7e6'};

In the second part of generate2DinputFile.m, details about edges (line segments) that define the problem domainare given.
The problem we are working on, shown below, is drawn using 6 line segments, i.e. it has 6 edges. We also use the concept
of item which is different than an edge. An item can either be a single edge or multiple, connected edges. Later we'll assign
BCs to these items, such that on each item only a single BC type can be specified. For the problem of interest we'll use the
5 items shown below on the right.
Edge 5

Item 4

Item 3

Edge 4
Edge 6

Item 5

Edge 2
Edge 1

Edge 3
Item 1

Item 2

It is possible to treat each edge as a separate item, but for problem domains with 10s of edges grouping connected edges
with the same BC into a single item simplifies the code and the BC specification part.
In Part 2 of generate2DinputFile.m shown below, we first specify the number of i tems (nItem) and the number of edges
that form each item (nItemEdges). For the problem we are working on there are 5 items and only the second item has
two edges, all the other items have only one edge.
Next the user provides the coordinates of the end poi nts of each edge of each item using itemCoord variable. If an item
has a single edge we need to specify the coordinates of two end points of the edge. For an item with N edges we need to
specify the coordinates of N+1 points that define these edges.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% PART 2. ITEM DETAILS
%
%
%
% Problem dependent parameters are nItem, nItemEdges and itemCoord.
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nItem = 5;
nItemEdges = [1 2 1 1 1];
nEdge = sum(nItemEdges);
nItemEdgesMax = max(nItemEdges);
itemCoord = zeros(nItem, nItemEdgesMax+1, 2);
% Provide coordinates of the nodes of each item
item = 1;
itemCoord(item,1,1) = 0.0;
itemCoord(item,1,2) = 0.0;
itemCoord(item,2,1) = 0.002;
itemCoord(item,2,2) = 0.0;
item = 2;
itemCoord(item,1,1) = 0.002;
itemCoord(item,2,1) = 0.002;
itemCoord(item,3,1) = 0.005;

itemCoord(item,1,2) = 0.0;
itemCoord(item,2,2) = 0.001;
itemCoord(item,3,2) = 0.001;

item = 3;
itemCoord(item,1,1) = 0.005;
itemCoord(item,2,1) = 0.005;

itemCoord(item,1,2) = 0.001;
itemCoord(item,2,2) = 0.003;

item = 4;
itemCoord(item,1,1) = 0.005;
itemCoord(item,2,1) = 0.0;

itemCoord(item,1,2) = 0.003;
itemCoord(item,2,2) = 0.003;

item = 5;
itemCoord(item,1,1) = 0.0;
itemCoord(item,2,1) = 0.0;

itemCoord(item,1,2) = 0.003;
itemCoord(item,2,2) = 0.0;

In Part 3 of generate2DinputFile.m shown below, we specify variables of hdata structure to control size of the elements.
hdata.hmax is used to provide a single maximum element size for the whole mesh, so that all the elementswill be smaller
than the specified size. hdata.edgeh variable is used to specify the size of elements that are in contact with an edge.
Finally hdata.fun variable is used to control the distribution of element size using functions of space coordinates and
. For the code segment shown below the parts related to hdata variable are commented out, meaning that we are not
controlling the size of elements in any way and accept the default behavior.
Towards the end of Part 3, mesh2d function is called to generate a mesh of triangular elements. Output of mesh2d is two
matrices named coord and LtoG. The first one stores the coordinates of generated mesh nodes and the second one
stores the connectivity information of the elements. mesh2d function plots the created mesh so that the user can see it
before generating the input file. The user does not need to change anything in calling the mesh2d function.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% PART 3. MESH GENERATION
%
%
%
% The only problem dependent variable is hdata, which is used to control %
% size of the elements.
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% hdata structure is used by mesh2d to control element sizes. It has three


% main variables as hmax, edgeh and fun.
hdata = [];
% hdata.max and hdata.edgeh are are used to control element size for the
% whole mesh and for certain edges.
% hdata.hmax = 0.00025;
% hdata.edgeh(1,1) = 5;

hdata.edgeh(1,2) = 0.0002;

% hdata.fun can be used to specify functions for controling mesh size


% at different regions of the domain. Following is one line function, but
% you can also write separate, more complicated functions.
% hdata.fun = @(x,y) 0.000025 + 0.1 * sqrt((x-0.002).^2 + (y-0.001).^2);
% ......
% ......
% ......

Omitted lines...

% mesh2d function creates the mesh. First output is a matrix of NNx2 that
% holds x and y coordinates of mesh nodes. Second output is a NEx3 matrix
% that stores corner node numbers of NE elements.
[coord, LtoG] = mesh2d(nodeCoord, edgeNodes, hdata, []);

In Part 4 of generate2DinputFile.m shown below, we only provide the itemBC variable, which is used to store the BC of
each item. For the sample problem of interest 1st BC is used for the 1st, 3rd and 5th items. 2nd BC is used for the 2nd item and
3rd BC is used for the 4th item.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% PART 4. BC DETERMINATION
%
%
%
% The only problem dependent parameter is itemBC.
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf ('\nFinding BCs. This may take some time for large meshes.\n');
% Specify the BC number for each geometrical item. Length of itemBC array
% is nItem.
itemBC(1) = 1;
itemBC(2) = 2;
itemBC(3) = 1;
itemBC(4) = 3;
itemBC(5) = 1;

The last part, Part 5, of the code is used to generate the input file that can be used with our 2D FEM solvers. There is nothing
in this part that we need to change. In this part we are asked to enter a file name at the command line and a file with the
specified name with an INP extension is created. If the user does not want to generate an input file for the created mesh
he/she can press Ctrl-C to abort the running code.

Now we'll present 5 different meshes created for the sample problem that is being discussed by specifying different size
control options through the use of the variables of hdata structure.
First mesh given below uses no mesh size control at all.
For the second mesh hdata.hmax is specified as 0.00025. Therefore smaller elements are created everywhere and the
mesh is uniform, i.e. all elements have similar size.
Third mesh uses hdata.edgeh variable only to specify the size of elements on the 5 th edge to be 0.0002 using the
following code.
hdata.edgeh(1,1) = 5;
hdata.edgeh(1,2) = 0.0002;
edgeh is a matrix with 2 columns. Its first column is used to specify the edge numbers on which we want to control the
element size. Above we entered it to be 5. Second column of edgeh variable is used to store the desired element size.We
entered it to be a small value of 0.0002.
For the fourth mesh again hdata.edgeh is used but this time to specify element size at two edges, the 4th and the 6th
edge. Code segment to do this is shown below
6

hdata.edgeh(1,1)
hdata.edgeh(1,2)
hdata.edgeh(2,1)
hdata.edgeh(2,2)

=
=
=
=

4;
0.0002;
6;
0.0002;

Finally for the fifth mesh hdata.fun variable is used to specify element size as follows
hdata.fun = @(x,y) 0.000025 + 0.1 * sqrt((x-0.002).^2 + (y-0.001).^2);

Here we provide a size distribution function. "@(x,y)" part says that the function we provide is a function of and . The
rest of the line is the function itself. Its value is 0.000025 at point (0.002, 0.001), which is the smallest value that the function
can take. As we radially go out from this point the function gets larger. Therefore this function is used to generate fine mesh
near point (0.002, 0.001) and the mesh gradually gets coarser as we move away from this point. Much more complicated
functions can be written to control the size of the elements in various different ways.

Mesh 1

Mesh 2

Mesh 3

Mesh 4

Mesh 5

As a second problem let's consider the potential flow over a cylinder shown below.
=

(-4,2)

(4,2)

=0

Radius = 1
=

(-4,-2)

(4,-2)

There are two different BCs, one for the rectangular box and one for the circle. The same BC is specified on all 4 edges of
the rectangular box, therefore we can consider the box as a single item. Similarly the circle is another item. In total there
are 2 items.
The circular hole needs to be represented by a number of straight edges (line segments). Creation of these edges can be
simplified by the use of a for loop as seen in Part 2 of the code given below. In the provided code the circle is drawn as a
combination of 16 edges.
Here it is important to note that both the first and the second item are closed line segments, i.e. their first and second
points should exactly match. In other words the coordinates of the first and last points of itemCoord variable should be
the same.
In the mesh generation part let's not use any hdata variable for mesh size control.
In Part 4 we only specify itemBC variable.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PART 1. CONSTANTS and BOUNDARY CONDITION DATA
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global LtoG coord itemCoord
eType = 2;
NEN = 3;
NGP = 4;
kxxFunc = '1.0';
kyyFunc = '1.0';
fFunc
= '0.0';
nBC = 2;
BCtype = [1 1];
BCstr = {'y'
'0.0'

'0.0';
'0.0'};

% Freestream velocity Uo is taken to be 1

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PART 2. ITEM DETAILS
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nItem = 2;
nCircleEdge = 16;
% We'll divide the circle into 16 edges
nItemEdges = [4 nCircleEdge];
nEdge = sum(nItemEdges);
nItemEdgesMax = max(nItemEdges);
itemCoord = zeros(nItem, nItemEdgesMax+1, 2);
% Provide coordinates
item = 1; % 1st item
itemCoord(item,1,1) =
itemCoord(item,2,1) =
itemCoord(item,3,1) =
itemCoord(item,4,1) =
itemCoord(item,5,1) =

of the nodes of each item


is the rectangular box
-4.0;
itemCoord(item,1,2)
4.0;
itemCoord(item,2,2)
4.0;
itemCoord(item,3,2)
-4.0;
itemCoord(item,4,2)
-4.0;
itemCoord(item,5,2)

=
=
=
=
=

-2.0;
-2.0;
2.0;
2.0;
-2.0;

item = 2;
% 2nd item is the circular hole
for i = 1:nCircleEdge
angle = pi/(nCircleEdge/2) * (i-1);
itemCoord(item,i,1) = cos(angle);
itemCoord(item,i,2) = sin(angle);
end

Coordinates of
the rectangular
box

Coordinates of
the circular
hole

% Make sure that the last coordinate that defines the circle is the same
% as the first coordinate. If we do this calculation inside the above for
% loop the two points may not match due to round-off errors.
itemCoord(item,nCircleEdge+1,1) = itemCoord(item,1,1);
itemCoord(item,nCircleEdge+1,2) = itemCoord(item,1,2);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PART 4. BC DETERMINATION
%
% The only problem dependent parameter is itemBC.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
itemBC(1) = 1;
itemBC(2) = 2;

The generated mesh without any element size control is shown below as the first mesh.
Second mesh is created by using a size control of hdata.hmax = 0.2.
Third one is obtained with the following size function (Note the use of .* operator instead of *)
hdata.fun = @(x,y) 0.05 + 0.1 * sqrt(x.*x + y.*y);

Mesh 1

Mesh 2

Mesh 3

Finally let's discuss how to generate a mesh for the solution of potential flow around an airfoil to demonstrate how
coordinates of an item can be read from an input file. Problem domain is shown below. Outside box and the airfoil are
selected to be the first and second items, respectively.
(5,5)

(-5,5)

(-5,-5)

(5,-5)

Second part of generate2DinputFile.m is shown below. Coordinates of the outside box are entered direc tly into the code.
However, coordinates of the airfoil are read from an input file named NACA4412.txt. Here we defined the airfoil in terms
of 34 edges connecting 35 nodes and this input file contains and coordinates of these 35 nodes, first and last point
being the same to form a closed airfoil shape. Each line in the file has the coordinates of one point.
10

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PART 2. ITEM DETAILS
%
% Problem dependent parameters are nItem, nItemEdges and itemCoord.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nItem = 2;
nAirfoilEdge = 34;
% Airfoil is defined using 34 edges.
nItemEdges = [4 nAirfoilEdge];
nEdge = sum(nItemEdges);
nItemEdgesMax = max(nItemEdges);
itemCoord = zeros(nItem, nItemEdgesMax+1, 2);

% Provide coordinates
item = 1; % 1st item
itemCoord(item,1,1) =
itemCoord(item,2,1) =
itemCoord(item,3,1) =
itemCoord(item,4,1) =
itemCoord(item,5,1) =

of the nodes of each item


is the outside box
-5.0;
itemCoord(item,1,2)
5.0;
itemCoord(item,2,2)
5.0;
itemCoord(item,3,2)
-5.0;
itemCoord(item,4,2)
-5.0;
itemCoord(item,5,2)

=
=
=
=
=

-5.0;
-5.0;
5.0;
5.0;
-5.0;

Coordinates of
the outside box

item = 2; % 1nd item is the airfoil


% Instead of entering the coordinates of all points of the airfoil one
% by one, we can read them from a text file. We assume that each line of
% the file has x and y coordinates of nAirfoilEdge+1 points.
file = fopen('NACA4412.txt', 'r');
for i = 1:nAirfoilEdge + 1
dummy = str2num(fgets(file));
itemCoord(item,i,1) = dummy(1);
itemCoord(item,i,2) = dummy(2);
end

Coordinates of
the airfoil

fclose(file);

The generated mesh is shown below. As seen from the zoomed in view, very small elements are created at the trailingedge
of the airfoil, although no constraint is provided for the element size. The reason of this unnecessary mesh refinement is
the sharp and pointed geometry of the trailing edge and the way these type of geometries are handled by the mesh
generation technique used in mesh2d. To avoid such unnecessarily fine elements the geometry of the airfoil maybe altered
slightly and the sharpness of the trailing edge may be reduced.

11

12