You are on page 1of 4

MENUM Lab

Finite Elements

The objective of this practical work is to be able to program 2D finite elements


using python. We restrict ourselves to stationary thermal problems.
Reports : They have to be done by groups of two, and the next rules have to be followed :
The work has to be submitted on the course website (one submission for each group).
The report has to be a pdf file plus an archive containing your scripts.
The name of the pdf file has to be : MENUM_FE_Name1_Name2.pdf
The name of the archive has to be : MENUM_FE_SCRIPTS_Name1_Name2.zip

1 Introduction
The objective of this project is to write a small finite element code in python, which is able
to solve thermal analysis problems. The features of the code are the following:
We assume 2D domains
The elements are 3 nodes isoparametric triangular elements
The conductivity is assumed to be isotropic
Only zero (homogeneous) Dirichlet boundary conditions are considered
A program skeleton is given in the file solveFE.py: you have to adapt this file for the
project. This file together with necessary python and mesh files are given in an archive
named menumFemProject2016.zip that you can uncompress in your work directory.

2 Description of the script files


A set of script files are given to you:
solveFE.py: the core of the finite element program that reads the input, built the
finite element linear system, solve it and save the results;
export.py: contains functions used to export the results;
gmshParser.py: contains a mesh class and functions to read mesh files (Gmsh file
format)
integrationRule.py: contains Gauss quadrature rules for triangles and quadrangles
Additional .msh and .geo files that you can use to test your program. .geo files
contain the definition of the geometry and .msh files the associated mesh. These files
can be opened with Gmsh. If you want to test your program with another number of
elements, just edit the .geo file (parameter nbelem), open it and click on Mesh/2D
then Save mesh.
For Gmsh documentation, see http://geuz.org/gmsh.

3 Problem description
The problem will be specified by refering to geometric entities defined on the mesh. Any
entity in the mesh belongs to a geometrical entity than can be a point, a curve or a surface.
All the geometries given in the archive obeys the following convention:

Node 1

Line 103

Surface 1000

Line 101

Node 3

Line 102

Line 104

Node 4

Node 2

If you want to prescribe a boundary condition on the bottom line of the square, you will have
to refer to line 101. Adding a source term on the domain ends-up to refer to surface 1000.

4 Program usage
You can run the finite element code by specifying the problem you want to solve:
The mesh on which you want to solve the thermal equations;

The conductivity;
The Neumann boundary conditions;
The Dirichlet boundary conditions;
The source term;
The name of the output file.
An example of such definition, and the call to solveFE are given in testFE.py:
#Import finite element solver function
from solveFE import solveFE
#Mesh file
meshName = 'square.msh'
# Setup the problem we want to solve
# Neumann BCs : as a dictionary (~ stl map) (physicalId: qN value)
BCNs = {101: -1, 103: 1}

# Dirichlet BCs for lines : as a dictionary (~ stl map) (physicalId: uD value)


# BCD_lns = {111:0,113:0}
#Or : (if none)
BCD_lns = {}
# Dirichlet BCs for nodes : as a dictionary (~ stl map) (physicalId: uD value)
BCD_nds = {1: 0}
# Conductivity (isotropic for example) : as a dictionary (physicalId: Kfourier)
conductivities = {1000: 1.}
# Source term (constant for example) : as a lambda function
sourceTerm = lambda xyz: 1.
#Output file name
exportName = 'temperature.pos'
#Call the solver
solveFE(meshName, conductivities, BCNs, BCD_lns, BCD_nds, sourceTerm, exportName)
In this case, one tries to solve the problem on square.msh with unit conductivity, unit source
term, qN = 1. on the top line (number 103), qN = 1. on the bottom line (number 101),
u = 0 at the bottom left corner (node 1). The results are stored in temperature.pos.

5 Work
You are asked to :
Finish the implementation of the proposed finite element code. Look at solveFE.py:
the code is commented and some guidance is given;
Basically, you need to fill the source file where # SMA to code comments are given;
Fill the dof manager;
Compute the elementary stiffness matrix;
Compute the elementary volume force vector;
Compute the elementary Neumann force vector;
Do the assembly.
You can compare your results to analytical solutions (for instance the case of a square
with zero temperature on line 101, and a prescribed flux on line 103)
Compare your solution with the one from the FD lab.

6 Bonus questions
Only if you have finished properly the other questions. Otherwise it may lead
to malus points...
1. Modify your program for a domain with two isotropic materials (see squareBimat.msh);
2. Check your implementation against analytical solutions.

7 Tips
To check your elementary stiffness matrix, here is the stiffness matrix of a unique triangular
element with unit conductivity and whose shape is homothetic to the parent element:

1.0 0.5 0.5


[Ke ] = 0.5 0.5 0.0
0.5 0.0 0.5
Note that the terms can be ordered in a different way in your program (depending on
the connectivity of the element). Two triangular meshes are given with the source files :
tri1x1.msh and tri2x2.msh.

You might also like