This educational code written for FEniCS is for compliance minimization in structural optimization, in two dimensions. The code is based on a level set formulation and on the distributed expression of the shape derivative, and exploits the powerful features of FEniCS to solve complicated PDEs with a simple implementation. The code is written for compliance minimization in the framework of linearized elasticity, but can be adapted for other functionals or other PDEs. I have also added an extension of the code for compliant mechanisms.

FEniCS Project

The code consists of two files: and The main file is The file is used to initialize one of the six following cases:

half_wheel, bridge, cantilever, cantilever_asymmetric, MBB_beam, cantilever_twoforces.

For instance, to run the cantilever case, the command line is

python cantilever

I have also added an extension of the code for compliant mechanisms, see the file To run this case, the command line is

python inverter

The code comes with a paper explaining in details the mathematical framework used for the code, and the implementation. The PDF file is included in the .tar.gz file for downloading here:

FEniCS 2017.1 code for structural optimization and PDF file

I wrote an update for FEniCS 2018.1, a few lines were modified but the structure of the code is the same. The line references in the PDF file are still valid for this update. Note that FEniCS 2018.1 requires Python 3, therefore the command line to run the codes are now:
python3 cantilever

python3 inverter

FEniCS 2018.1 code for structural optimization and PDF file

Questions, suggestions and feedback are welcome, please send me an email at the following address: laurain at ime dot usp dot br

- 08/05/2017 First upload
- 30/05/2017 Update 1. Line 94, replaced print('Volume fraction : %.2f' % vol/(lx*ly))
by print('Volume fraction : %.2f' % (vol/(lx*ly)))
PDF file updated.
- 25/08/2017 Update 2. Code and PDF file updated. The file gives the same result but is shorter.
The PDF file has been significantly modified compared to the previous version.
Added a file for compliant mechanisms.
- 29/01/2018 Update 3. The file was updated, corrected the sign in the Robin boundary condition.
Corrected a few errors in the PDF file.
- 19/06/2019 Update 4. This is an update for FEniCS 2018.1, which now requires Python 3.
The command lines to run the codes are now python3 cantilever
and python3 inverter.

set_log_level(ERROR) was removed on line 10 of

All instances of boundaries = FacetFunction("size_t", mesh) were replaced by
boundaries = MeshFunction("size_t", mesh, mesh.topology().dim() - 1) in the file

domains = CellFunction("size_t", mesh) was replaced by
domains = MeshFunction("size_t", mesh, mesh.topology().dim()) on line 45 of

solverav.parameters['reuse_factorization'] = True was removed on line 53 of

th_array = th.vector().array() was replaced by
th_array = th.vector().get_local() on line 90 of

xrange(0, dofsVvec_max,2) was changed to range(0, dofsVvec_max,2) on line 93 of

the section 'Plot Geometry' (lines 106 to 113) was rewritten using subplot() in

The same modifications were implemented in
Symmetric cantilever
Asymmetric cantilever
MBB beam
Cantilever with two loads