Lab course: Introduction to Finite Elements

Prof. Dr. T. Richter, C. Mehlmann
Analysis und Numerik, OVGU Magdeburg
[email protected]
SoSe 2017
Problem Sheet # 2
Lab course: Introduction to Finite Elements
Website https: // www. math. uni-magdeburg. de/ ~ richter/ SS17/ seminar
Lab exercises
L2.1 Structure arrays
The Matlab command
s = struct(’field1’, values1, ’field2’, value2, ...)
creates a so called structure array with the specified fields and values. The values, e.g.
values1 are then saved to field1. values1 can be a scalar or a matrix. In order to access the
entries of the structure array we write
s.field1
Write a function disc=Discretization(node,elem), which constructs a structure array with values of nodes, elements and boundary edges in the corresponding fields node and elem. The constructed array is to be returned as the result of the function.
L2.2 Mesh operations
We save the triangulation with the help of the function Discretization in a structure array named
mesh.
a) Each element of the triangulation has a local node numbering determined by the order in which
the node indices are stored in the matrix elem. The following line saves edge vectors between the
nodes 3 and 2 for each element:
ve(:, :, 1) = disc.node(disc.elem(:, 3), :)-disc.node(disc.elem(:, 2), :);
In this way we obtain the following structure
 (1)
(1)
x3 − x2
 (2)
(2)
 x3 − x2

ve(:,:,1) = 
..

.
(N T )
x3
(N T )
− x2
(1)
(1)
y3 − y2
(2)
(2)
y3 − y2
..
.
(N T )
y3
(N T )



,


− y2
with N T the number of elements.
Save the remaining edges in the same way and write a function ve=EdgeVectors(disc) that returns
all local edges for a given discretization with a set of nodes and elements.
b) Calculate the length of each edge without using for-loops. The result should be a function
getEdgeLength with the signature function edgelength = getEdgeLength(disc) which returns
a matrix with three rows where each line contains the length of the three edges of the corresponding
element. Use part a) of the exercise.
c) Write a function area = getArea(disc) that computes the area of each element in the triangulation.
Hint: The area can be computed for example from

1
1
|T | = det 1
2
1
the following formula

x1 y1
x2 y2  .
x3 y3
L2.3 Node ordering, part I
To simplify later refinement of our mesh, we need to enforce some conditions on the local node
ordering. First, we need the nodes for each element to be ordered counter-clockwise. We denote by
base the longest edge of a triangle. As a second requirement, the node lying opposite of the base
must have the local number one.
a) Check if your implementation of the function UnitSquareGrid from H1.1 returns the nodes
of each element ordered counter-clockwise. Modify it to do so if necessary.
b) Write a function elem = label(node,elem), which computes the longest edge of each triangle and changes the local node numbering accordingly.
L2.4 First steps towards global refinement
Our next aim is to implement global refinement. If given a relatively coarse mesh to start with, the
goal is to obtain finer grids by successively subdividing the elements.
For global refinement, we will subdivide each triangle into four equally sized smaller ones as shown
below.
3
3
6
5
2
1
1
4
2
This involves creating new nodes in the center of each edge and then updating the elements and
the boundary edges to the new structure.
Create a function disc = uniformrefine(disc) and write the part that computes the newly
created nodes and appends them to disc.node. Make sure the new nodes are created just once
each!
Homework
H2.1 L shaped domain
Discretize the following L-domain by six triangles of equal size
(1,1)
(1,1)
(-1,0)
(-1,1)
For this purpose, write a function disc=LDomain() that assembles the arrays node and elem
and combines them with the function Discretization from the first exercise. Make sure the nodes
of all elements are ordered counter-clockwise and that the node opposite of the longest edge has
local node number 1.
H2.2 Node ordering, part II
Above, we required that the local ordering of the nodes in each element is counter-clockwise.
a) Think about a way to determine whether the local node ordering of a given element fulfills
this condition or not. Elaborate your idea in some comments at the beginning of your .m-file
for part b).
b) Write a function disc=fixDirection(disc) that checks if the ordering is correct in each
element of the given mesh and changes it for those elements where this is not the case.
Homeworks are to be presented in the next week.