Computer Implementation For 1D and 2D Problems: 4.1 MATLAB Code For 1D FEM (Steady1d.m)
Computer Implementation For 1D and 2D Problems: 4.1 MATLAB Code For 1D FEM (Steady1d.m)
Computer Implementation For 1D and 2D Problems: 4.1 MATLAB Code For 1D FEM (Steady1d.m)
Chapter 4
In this chapter MATLAB codes for 1D and 2D problems are provided. Also a manual for 2D mesh generator is
given.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% steady1D.m %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% Middle East Technical University %
% Department of Mechanical Engineering %
% ME 582 Finite Element Analysis in Thermofluids %
% http://www.me.metu.edu.tr/courses/me582 %
% %
% Author : Dr. Cuneyt Sert %
% [email protected] %
% http://www.me.metu.edu.tr/people/cuneyt %
% Last Update : 19/03/2012 %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% FEATURES and LIMITATIONS %
% %
% This code %
% - is written for educational purposes. %
% - solves the following model DE in a 1D domain.
% 𝑑 𝑑𝑢 𝑑𝑢
% - d/dx [a(x) du/dx] + b(x) du/dx + c(x) * u = f(x) − 𝑎 +𝑏 + 𝑐𝑢 = 𝑓
% 𝑑𝑥 𝑑𝑥 𝑑𝑥
% with u(x) being the scalar unknown.
% - uses Galerkin Finite Element Method (GFEM). %
% - uses linear or quadratic, Lagrange type elements. %
% - automatically generates a mesh with clustering at the boundaries. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% HOW TO USE? %
% %
% Modify setupProblem function and run the code. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% VARIABLES %
% %
% NE : Number of elements %
% NN : Number of nodes in the mesh %
% NEN : Number of element nodes. Same for all elements %
% NGP : Number of GQ points %
% xMin : Minimum x value of the problem domain %
% xMax : Maximum x value of the problem domain %
% funcs : Structure to hold a(x), b(x), c(x), f(x) as strings %
% exactSol : String that holds the exact solution %
4-1
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
4-2
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%========================================================================
function [] = steady1D
%========================================================================
clc; % Clear MATLAB's command window
clear all; % Clear variables and functions in the memory
close all; % Close figure windows
disp('**************************************************');
disp('****** ME 582 - 1D, Steady Solver ******');
disp('**************************************************');
setupProblem();
generateMesh();
This is the main function
timeStart = tic; % Start measuring time.
that calls other functions
setupLtoG();
setupGQ();
calcShape();
calcGlobalSys();
applyBC();
solve();
postProcess();
4-3
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%========================================================================
function [] = setupProblem
%========================================================================
global funcs exactSol NE NEN xMin xMax isClustered clusterValue NGP
global bcType bcValue;
Problem of Section 2.10
% Remember that the DE we are solving is
% - d/dx [a(x) du/dx] + b(x) du/dx + c(x) * u = f(x)
𝑑2 𝑢 𝑑𝑢
funcs.a = '1.0'; % a(x) − 2
+3 =1, 𝑥 ∈ [0,1]
funcs.b = '3.0'; % b(x) 𝑑𝑥 𝑑𝑥
funcs.c = '0.0'; % c(x)
funcs.f = '1.0'; % f(x)
𝑢 0 =0, 𝑢 1 =0
NE = 5;
NEN = 2; % Only 2 and 3 are supported.
xMin = 0.0;
Known exact solution
xMax = 1.0;
isClustered = 0; % Provide 0 to generate equi-sized elements.
clusterValue = 0; % A positive/negative value will cluster the elements
% towards the positive/negative x axis.
NGP = 3;
4-4
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%========================================================================
function [] = generateMesh
%========================================================================
global NE NN NEN xMax xMin isClustered clusterValue coord;
%========================================================================
function [] = setupLtoG
%========================================================================
global NE NEN elem;
% Setup LtoG for each element. We assume that nodes are globally numbered
% consecutively starting from the left boundary node at xMin.
for e = 1:NE
for i = 1:NEN 1 2 3 4 5 6
elem(e).LtoG(i) = (e-1)*(NEN-1) + i;
end e=1 e=2 e=3 e=4 e=5
end
1 2
% End of function setupLtoG() 2 3
𝐿𝑡𝑜𝐺 = 3 4
4 5 4-5
5 6
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%========================================================================
function [] = setupGQ
%========================================================================
global NGP GQ;
%========================================================================
function calcShape()
%========================================================================
% Calculates the values of the shape functions and their derivatives with
% respect to ksi coordinate at GQ points.
global NEN NGP S dS GQ;
S = zeros(NEN, NGP);
dS = zeros(NEN, NGP);
4-6
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%========================================================================
function calcGlobalSys()
%========================================================================
% Calculates the global stiffness matrix [K] and force vector {F}.
global NE NN soln;
soln.K = zeros(NN,NN);
soln.F = zeros(NN,1); % {F} is a column vector, not a row vector.
for e = 1:NE
calcElemSys(e);
assemble(e);
end
%========================================================================
function [] = calcElemSys(e)
%========================================================================
% Calculates the element level stiffness matrix and force vector.
global NEN NGP coord GQ elem funcs S dS;
elem(e).Fe = zeros(NEN,1);
elem(e).Ke = zeros(NEN,NEN);
4-7
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%========================================================================
function [] = assemble(e)
%========================================================================
% Inserts [Ke] and {Fe} into proper locations of [K] and {F} by the help
% of LtoG arrays of elements.
% Assemble Ke into K.
for i = 1:NEN
I = elem(e).LtoG(i); % I is the global node corresponding to the
% i-th local node of element e
for j = 1:NEN
J = elem(e).LtoG(j); % J is similar to I.
soln.K(I,J) = soln.K(I,J) + elem(e).Ke(i,j);
end
𝐼 = 𝐿𝑡𝑜𝑔 𝑒, 𝑖
end 𝐾𝑖𝑗𝑒 → 𝐾𝐼𝐽 where
𝐽 = 𝐿𝑡𝑜𝑔 𝑒, 𝑗
% Assemble Fe into F.
for i = 1:NEN
I = elem(e).LtoG(i);
𝐹𝑖𝑒 → 𝐹𝐼 where 𝐼 = 𝐿𝑡𝑜𝑔 𝑒, 𝑖
soln.F(I) = soln.F(I) + elem(e).Fe(i);
end
%========================================================================
function [] = applyBC()
%========================================================================
global NN soln bcType bcValue;
% For EBCs reduction is NOT applied. Instead global [K] and {F} are
% modified. SV values specified for NBCs are added to {F}, there is no
% separate {B} vector for boundary integrals. For mixed type BCs both
% [K] and {F} are modified using the provided alpha and beta values.
4-8
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%========================================================================
function [] = solve()
%========================================================================
% Solves the system [K]{U}={F}. Note that this is generally the most time
% consuming part of the solution. Backslash operator that we use may be
% inefficient, especially for large problems. There are alternative
% techniques.
global soln;
{𝑈} = [𝐾]−1 {𝐹}
soln.U = soln.K \ soln.F;
Note that {𝐹} also includes {𝐵}
% End of function solve()
%========================================================================
function [] = postProcess()
%========================================================================
% Writes the calculated nodal unknowns to the screen. Plots the FEM
% solution and the error if the exact solution is known. Calculates the
% SVs at the boundaries where EBC or MBC is specified.
global NN NE NEN coord soln elem exactSol bcType funcs
% Create plot windows for the FEM solution and the error in case we know
% the exact solution.
if exactSol == '?'
subplot(1,1,1); % If exact solution is not known generate a single plot.
else
subplot(2,1,1); % If exact solution is known generate two plots. This
% one is for the FEM solution, and the other will be
% for the error.
end
if exactSol ~= '?'
subplot(2,1,2); % This plot is for the exact error.
secondPlot = gca; % Axes handle for the 2nd plot. Will be used later.
hold(secondPlot,'on');
end
% Plot the solution over each element as lines or curves. For high order
% elements, straight lines are not enough, curves are necessary. Here
% these curves are generated based on np equally spaced points over each
% element.
np = 20; % Number of points used to plot the solution over an element.
for e = 1:NE
for i = 1:NEN
X(i) = coord(elem(e).LtoG(i)); % NEN coordinates of element e.
Y(i) = soln.U(elem(e).LtoG(i)); % Nodal unknowns at X(i)
end
4-9
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
% Plot the exact solution and exact error on this element if the exact
% solution is known.
if exactSol ~= '?'
exact = eval(exactSol);
plot (firstPlot, x, exact, 'r:', 'LineWidth', 2);
plot(secondPlot, x, exact - pVal);
end
end
if exactSol ~= '?'
axes(secondPlot);
grid on;
title('Error = Exact solution - Approximate solution');
end
% Right boundary
if (bcType(2) ~= 2)
if NEN == 2
slope = (soln.U(NN) - soln.U(NN-1)) / (coord(NN) - coord(NN-1)); 𝑑𝑢
else First calculate 𝑑𝑥 𝑟𝑖𝑔ℎ𝑡
slope = (-3 * soln.U(NN-2) + 4 * soln.U(NN-1) - soln.U(NN)) / ...
(coord(3) - coord(1));
end 𝑑𝑢
x = coord(1); 𝑆𝑉 𝑟𝑖𝑔ℎ𝑡 = 𝑎
𝑑𝑥 𝑟𝑖𝑔ℎ𝑡
SV = eval(funcs.a) * slope;
fprintf(1,'\nSV at the right boundary : %f\n', SV);
end
4-10
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
4-11
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
4-12
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%========================================================================
function [] = steady2D
%========================================================================
clc;
clear all;
close all;
disp('**************************************************');
disp('****** ME 582 - 2D, Steady Solver ******');
disp('**************************************************');
readInputFile();
timeStart = tic;
setupGQ();
calcShape();
calcJacob();
calcGlobalSys();
applyBC();
solve();
timeTotal = toc(timeStart);
postProcess();
createTecplotOutput();
%========================================================================
function [] = readInputFile
%========================================================================
global prName eType NE NN NEN NGP funcs coord elem nBC BC
4-13
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
for i = 1:nBC
fscanf(inputFile, 'BC %*d :');
BC.type(i) = fscanf(inputFile, '%d', 1);
BC.str(i,1) = cellstr(str1);
BC.str(i,2) = cellstr(str2);
end
end
% Read the number of EBC nodes and number of NBC and MBC faces.
fgets(inputFile); % Read the next dummy line.
BC.nEBC = fscanf(inputFile, 'nEBCnodes :%d'); fgets(inputFile);
BC.nNBC = fscanf(inputFile, 'nNBCfaces :%d'); fgets(inputFile);
BC.nMBC = fscanf(inputFile, 'nMBCfaces :%d'); fgets(inputFile);
4-14
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
fclose(inputFile);
%========================================================================
function [] = setupGQ
%========================================================================
global eType NGP GQ;
4-15
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
4-16
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%========================================================================
function [] = calcShape()
%========================================================================
% Calculates the values of shape functions and their derivatives with
% respect to ksi and eta at GQ points.
global eType NEN NGP GQ S dS;
% eta derivatives of S
dS(2,1,k) = -0.25*(1-ksi);
dS(2,2,k) = -0.25*(1+ksi);
dS(2,3,k) = 0.25*(1+ksi);
dS(2,4,k) = 0.25*(1-ksi);
end
else
disp('ERROR: For quadratic elements only 4-node is supported.');
end
elseif eType == 2 % Triangular element
if NEN == 3 % Linear Lagrange shape functions
for k = 1:NGP
ksi = GQ.point(k,1);
eta = GQ.point(k,2);
% ksi derivatives of S
dS(1,1,k) = -1;
dS(1,2,k) = 1;
dS(1,3,k) = 0;
% eta derivatives of S
dS(2,1,k) = -1;
dS(2,2,k) = 0;
dS(2,3,k) = 1;
end
else
disp('ERROR: For triangular elements only 3-node is supported.');
end
end
4-17
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%========================================================================
function [] = calcJacob()
%========================================================================
% Calculates Jacobian matrix and its determinant for each element. Also
% calculates and stores the derivatives of shape functions wrt x and y
% coordinates at GQ points for each element.
global NE NEN NGP coord dS elem;
for e = 1:NE
% To calculate the Jacobian matrix first generate e_coord matrix of size
% NENx2. Each row of it stores x and y coords of the nodes of elem e.
for i = 1:NEN
iG = elem(e).LtoG(i);
e_coord(i,:) = coord(iG,:); % Copy both x and y cooordinates at once.
end
% For each GQ point calculate the 2x2 Jacobian matrix, its inverse and
% determinant. Only store the determinant for each element. Also
% calculate and store the shape function derivatives wrt x and y.
for k = 1:NGP
Jacob(:,:) = dS(:,:,k) * e_coord(:,:); Equation 3.15
elem(e).gDS(:,:,k) = inv(Jacob(:,:)) * dS(:,:,k); Equation 3.16
elem(e).detJacob(k) = det(Jacob);
end
end
%========================================================================
function calcGlobalSys()
%========================================================================
% Calculates the global stiffness matrix [K] and force vector {F}.
global NN NE soln;
soln.K = zeros(NN,NN);
soln.F = zeros(NN,1); % {F} is a column vector, not a row vector.
for e = 1:NE
calcElemSys(e);
assemble(e);
end
4-18
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%========================================================================
function [] = calcElemSys(e)
%========================================================================
% Calculates the element level stiffness matrix and force vector.
global NEN NGP coord S GQ elem funcs;
elem(e).Fe = zeros(NEN,1);
elem(e).Ke = zeros(NEN,NEN);
aValue = eval(funcs.a);
1
V1Value = eval(funcs.V1);
𝑑𝑆𝑖 𝑑𝑆𝑗 𝑑𝑆𝑖 𝑑𝑆𝑗 𝑑𝑆𝑗 𝑑𝑆𝑗
V2Value = eval(funcs.V2); 𝐾𝑖𝑗𝑒 = 𝑎 + + 𝑆𝑖 𝑉1 + 𝑉2 + 𝑐𝑆𝑖 𝑆𝑗 𝐽𝑒 𝑑𝜉
cValue = eval(funcs.c); 𝑑𝑥 𝑑𝑥 𝑑𝑦 𝑑𝑦 𝑑𝑥 𝑑𝑦
−1
fValue = eval(funcs.f);
for i = 1:NEN
for j = 1:NEN
elem(e).Ke(i,j) = elem(e).Ke(i,j) + ...
(aValue * (elem(e).gDS(1,i,k) * elem(e).gDS(1,j,k) + ...
elem(e).gDS(2,i,k) * elem(e).gDS(2,j,k)) + ...
S(i,k) * (V1Value * elem(e).gDS(1,j,k) + ...
V2Value * elem(e).gDS(2,j,k)) + ...
cValue * S(i,k) * S(j,k) ...
) * elem(e).detJacob(k) * GQ.weight(k); 1
end
end 𝐹𝑖𝑒 = 𝑓𝑆𝑖 𝐽𝑒 𝑑𝜉
for i = 1:NEN −1
elem(e).Fe(i) = elem(e).Fe(i) + ...
S(i,k) * fValue * elem(e).detJacob(k) * GQ.weight(k);
end
4-19
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%========================================================================
function [] = assemble(e)
%========================================================================
% Inserts [Ke] and {Fe} into proper locations of [K] and {F} by the help
% of LtoG arrays of elements.
% Assemble Ke into K.
for i = 1:NEN
I = elem(e).LtoG(i); % I is the global node corresponding to the i-th
% local node of element e
for j = 1:NEN
J = elem(e).LtoG(j); % J is similar to I.
soln.K(I,J) = soln.K(I,J) + elem(e).Ke(i,j);
end
end
% Assemble Fe into F.
for i = 1:NEN
I = elem(e).LtoG(i);
soln.F(I) = soln.F(I) + elem(e).Fe(i);
end
%========================================================================
function [] = applyBC()
%========================================================================
% For EBCs reduction is NOT applied. Instead, global [K] and {F} are
% modified. For NBCs only SVs specified as constants are supported.
% Provided SVs are added to {F}, there is no separate {B} vector. For
% MBCs only constant alpha and beta values are supported.
%===========================================
% Modify {F} for NBCs.
%===========================================
for i = 1:BC.nNBC % Loop over the faces with specified NBC
e = BC.NBCfaces(i,1); % This is the element where NBC is specified
f = BC.NBCfaces(i,2); % This is the face of element e
whichBC = BC.NBCfaces(i,3); % This is the no. of BC specified
4-20
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%===========================================
% Modify [K] and {F} for mixed type BCs.
%===========================================
for i = 1:BC.nMBC % Loop over the faces with specified mixed BC
e = BC.MBCfaces(i,1); % This is the element where mixed BC is specified
f = BC.MBCfaces(i,2); % This is the face of element e.
whichBC = BC.MBCfaces(i,3); % This is the no. of BC specified.
% For a mixed BC applied on a 2-node face with constant alpha and beta
% values, we derived the following two relations for the elemental
% boundary integral vector entries corresponding to the two nodes of
% the face.
%
% B1 = (3*beta + 2*alpha*T1 + alpha*T2) * Lface / 6
% B2 = (3*beta + alpha*T1 + 2*alpha*T2) * Lface / 6
%===========================================
% Modify [K] and {F} for EBCs.
%===========================================
for i = 1:BC.nEBC
node = BC.EBCnodes(i,1); % Node at which this EBC is specified
whichBC = BC.EBCnodes(i,2); % Number of the specified BC
4-21
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%========================================================================
function [nodes length] = getFaceDetails(e, f)
%========================================================================
% Finds the nodes located at face f of element e. Works only for faces
% with 2 nodes. For higher order elements this part should change. Length
% of the face is also calculated.
if eType == 1
nFace = 4; % Quadrilateral elements have 4 faces.
else
nFace = 3; % Triangular elements have 3 faces.
end
%========================================================================
function [] = solve()
%========================================================================
% Solves the system [K]{U}={F}. Note that this is generally the most time
% consuming part of the solution. Backslash operator that we use is an
% inefficient operator and there are alternative techniques.
global soln;
4-22
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%========================================================================
function [] = postProcess()
%========================================================================
% Generates contour plot of the scalar unknown.
global NN NE NEN coord elem soln
patch(x,y,z);
axis equal;
colorbar; % Put a colorbar next to the graph
xlabel('x');
ylabel('y');
zlabel('T');
title('Contour plot of the FEM solution');
calculateSV();
%========================================================================
function [] = calculateSV()
%========================================================================
% Calculates SV's at EBC and MBC boundaries.
4-23
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%========================================================================
function [] = createTecplotOutput()
%========================================================================
% Generates an output file that can be used to visualize the results using
% the commercial Tecplot software.
fclose(outputFile);
4-24
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
For 2D problems problem data is read from an input file, which has a very strict format. You can NOT make
arbitrary modifications to it without changing the MATLAB code that reads it. The following input file is for the
chimney problem that we solved in Section 3.7, but this one uses 8 triangular elements.
1 1 1 th st
face and 5 element’s 1 face has the 1
st
3 1 1 BC.
5 1 1
7 2 2 th nd nd
7 element’s 2 face has the 2 BC.
4-25
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
The main difficulty in using a third party mesh generator like mesh2d is converting 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 examples. Let's start with the
following heat conduction problem, which is Exercise E-3.4 of Chapter 3. Coordinates shown below are in mm.
(2,1)
𝑦 (5,1)
𝛼 = −200
(0,0) (2,0)
𝑥 𝛽 = 8 × 104
Insulated
generate2DinputFile.m code is divided into 5 parts. Beginning of each part is labeled with comments to be
detected easily. 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 , aFunc parameter is 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 types
are provided in the BCtype array. Boundary condition types supported are
4-26
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
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. We 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. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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'};
4-27
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
In the second part of generate2DinputFile.m, details about edges (line segments) that define the problem
domain are 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
Edge 4 Item 3
Edge 6 Item 5
Edge 3
Edge 2
Item 2
Edge 1 Item 1
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 items (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 points 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.
4-28
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% 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);
item = 2;
itemCoord(item,1,1) = 0.002; itemCoord(item,1,2) = 0.0;
itemCoord(item,2,1) = 0.002; itemCoord(item,2,2) = 0.001;
itemCoord(item,3,1) = 0.005; itemCoord(item,3,2) = 0.001;
item = 3;
itemCoord(item,1,1) = 0.005; itemCoord(item,1,2) = 0.001;
itemCoord(item,2,1) = 0.005; itemCoord(item,2,2) = 0.003;
item = 4;
itemCoord(item,1,1) = 0.005; itemCoord(item,1,2) = 0.003;
itemCoord(item,2,1) = 0.0; itemCoord(item,2,2) = 0.003;
item = 5;
itemCoord(item,1,1) = 0.0; itemCoord(item,1,2) = 0.003;
itemCoord(item,2,1) = 0.0; 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
elements will 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.
4-29
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% PART 3. MESH GENERATION %
% %
% The only problem dependent variable is hdata, which is used to control %
% size of the elements. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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;
% ......
% ...... 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.
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 1 st 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.
4-30
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% 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.
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 5th 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
hdata.edgeh(1,1) = 4;
hdata.edgeh(1,2) = 0.0002;
hdata.edgeh(2,1) = 6;
hdata.edgeh(2,2) = 0.0002;
Finally for the fifth mesh hdata.fun variable is used to specify element size as follows
4-31
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
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-32
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
𝜓 = 𝑈𝑜 𝑦
(-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.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PART 1. CONSTANTS and BOUNDARY CONDITION DATA %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
eType = 2;
NEN = 3;
NGP = 4;
aFunc = '1.0';
V1Func = '0.0';
V2Func = '0.0';
cFunc = '0.0';
fFunc = '0.0';
nBC = 2;
BCtype = [1 1];
BCstr = {'y' '0.0'; % Freestream velocity Uo is taken to be 1
'0.0' '0.0'};
4-33
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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);
% 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.
Third one is obtained with the following size function (Note the use of .* operator instead of *)
4-34
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
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 directly into
the code. However, coordinates of the airfoil are read from an input file named NACA4412.txt. Here we defined
4-35
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
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.
The generated mesh is shown below. As seen from the zoomed in view, very small elements are created at the
trailing edge 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 may be altered slightly and the sharpness of the trailing edge may be
reduced.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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);
for i = 1:nAirfoilEdge + 1
dummy = str2num(fgets(file)); Coordinates of
itemCoord(item,i,1) = dummy(1);
itemCoord(item,i,2) = dummy(2); the airfoil
end
fclose(file);
4-36
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
4.5 Exercises
E-4.1. Consider the following 2nd order ODE and boundary conditions
2
− 2
+2 + = 40 + 40 +2 , 0 5
a) Using steady1D.m code solve this problem with 4, 8, 16, 32, 64, 128, 256, 512, 1024 linear elements.
Find the maximum nodal error in absolute sense for each run and generate a plot of “Maximum
Absolute Nodal Error versus NN (number of nodes)”. Use logarithmic scales for both the horizontal
and vertical axes. You can use the error values already calculated in the postProcess function. Obtain
a relation on how fast the error drops as the element size is reduced, i.e. a statement like "Order of the
error is ℎ , i.e. the error drops by a factor of 2 as the element sizes are reduced by a factor of 2". In
order to keep errors due to numerical integration low, use 5 point GQ integration (NGP = 5).
b) Repeat the error analysis using 2, 4, 8, 16, 32, 64, 128, 256, 512 quadratic elements.
E-4.2. Consider the 2D heat conduction over a thin rectangular plate. Three sides of it are maintained at a
constant temperature 1 , while the fourth side is maintained at a constant temperature 2 1.
𝑦 𝑇 = 𝑇2
𝜃=1
𝑇 = 𝑇1 𝑇 = 𝑇1 𝑊
𝜃=0 𝜃=0
𝑥
𝑇 = 𝑇1
𝜃=0
4-37
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
− 1
=
2− 1
∇2 = 0
1
2 −1 +1
, = ( )
=1
Using steady2D.m solve this problem for = 1 and = 2 with a series of meshes from coarse to fine. For each
solution obtain the temperature profile at = 2 and compare them with each other and with the exact
solution.
To get the temperature profile at = 2 of the FEM solution you can write a function called extractData.
In this function first obtain the array of points at which you want to collect data. For example for this problem it
can be an array of 20 points on = 2 line. Then determine the elements in which these points are located
and perform an interpolation with the following equation to get the value of on the exact points of interest.
, = ,
Note that this equation requires a , → , switch to calculate the shape functions at the desired points.
E-4.3. Laplace's equation can be used to study potential flow problems. We want to solve the potential flow
over a circular cylinder as shown below.
𝑦
(-6, 4) (6, 4)
Center: (0,0)
Radius: 1
The relation between streamfunction and the velocity components are given as
= and =−
4-38
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
Using the symmetry of the flow field we'll only use a quarter of the domain as shown below. Necessary BCs are
also shown, where is the constant inlet velocity that we'll consider to be 1. Note that = 0 selection at the
bottom boundary and on the cylinder surface is an arbitrary selection. Streamfunction could be equated to
some other value here, but using zero is simple enough.
𝑦
𝜓 = 4𝑈𝑜
𝑢 = 𝑈𝑜 = 1 𝜕𝜓
𝑣=0 r − =0
r 𝜕𝑥
𝜃
𝜓 = 𝑦𝑈𝑜 = 𝑦
𝑥
𝜓=0
𝜓=0
Create an input file for the problem and use steady2D.m code to obtain the streamfunction distribution. Using
the relations between the velocity components and the streamfunction, calculate the velocity components and
the speed ( = √ 2 + 2 ) at the points on the cylinder (When you use generate2DinputFile.m code, it may be a
good idea to assign a unique BC number to the cylinder to identify the points located on them easily).
To calculate the velocity components and speed at the nodes you can add a new function to the code and call it
from the post processing function. Every node on the mesh is surrounded by a number of elements. For each
node you need to find the list of the surrounding elements, calculate the velocity components on that node
using the streamfunction distribution of each surrounding element and take the average of the values obtained
by all surrounding elements.
Plot these speed values of the nodes on the cylinder as a function of , which is shown above. Repeat the
solution for a coarse and a fine mesh and compare the results. Also compare the results with a reference
solution that you can find in any introductory level fluid mechanics textbook.
E-4.4. NACA 0012 is a symmetric airfoil. We want to use the potential flow theory to obtain the pressure
distribution over it. First obtain the coordinates of the NACA 0012 airfoil and if necessary modify them so that
the chord length of it is equal to 1 and its trailing edge is located at the origin of the coordinate system, as
shown below. Put the airfoil in a box of size 6x6 and generate a mesh for the flow field. The same BC of =
can be applied all over the outer boundaries, considering that the free stream velocity is 1. On the airfoil surface
the BC is = 0.
𝑦
(-3, 3) (3, 3)
𝑥
1
4-39
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
After obtaining the stremfunction values at the mesh nodes, use the technique explained in the previous
problem to determine the speed at each node. Speed values corresponding to the nodes of the airfoil can be
used in a Bernoulli equation to get the pressure distribution over the airfoil. Bernoulli equation needs to be
written between a point on the surrounding box, called point , and a point on the airfoil, called point A, as
seen below
2 2
+ = +
2 2
where = √ 2 + 2 is the speed of point A and = 1 is the free stream velocity. The above Bernoulli
equation can be arranged to get the following nondimensional pressure coefficient
− 2
= =1−
1 2
2
Using the speed values on the airfoil nodes calculate values on them plot vs. for both the upper and
lower surfaces of the airfoil. Compare the results with the reference values. For this symmetric airfoil at zero
angle of attack we expect to see the same pressure distribution on the upper and lower surfaces, resulting in no
lift.
E-4.5. A semiconductor industry roadmap for microlitography processing requires that a 300 mm diameter
silicon wafer be maintained at a steady-state temperature of 140 to within a uniformity of 0.1 . The design
of a hot-plate tool to hopefully meet this requirement is shown schematically. An equalizing block (EB), on which
the wafer would be placed, is fabricated from an aluminum alloy of thermal conductivity = 75 and is
heated by two ring-shaped electrical heaters. The two-zone heating arrangement allows for independent
control of a main heater (MH) and a trim heater (TH), which is used to improve the uniformity of the surface
temperature for the EB. Your assignment is to size the heaters, MH and TH, by specifying their applied heat
2
fluxes, and , and their radial extents, ℎ and ℎ . The constraints on radial positioning of
the heaters are imposed by manufacturing considerations and are shown in the schematic.
EB has a diameter of 340 mm (temperature uniformity is required only on 300 mm diameter portion of it) and
its top and lateral surfaces are exposed to convective heat transfer. Lower surface of the EB is adiabatic, except
the two heaters.
To obtain initial estimates of heater fluxes, first perform a solution with the upper surface of the EB fixed at 140
with heaters fully extending to their specified radial limits. Then use these estimates with convective heat
transfer BC at the upper surface of the EB and see if the uniformity is satisfied or not. If not change the heater
fluxes and their sizes and positions to get the desired uniformity. Plot the upper surface temperature of
different trials.
4-40
ME 582 Finite Element Analysis in Thermofluids
Dr. Cüneyt Sert
References
[1] F. P. Incropera, D. P. Dewitt, T. L. Bergman, A. S. Lavine, Fundamentals of Heat and Mass Transfer, 6 th ed.,
John Wiley and Sons, 2007.
4-41