Topology optimization codes written in Python

The Python code presented in this page is intended for engineering education and is an open-source alternative to the 99- and 88 line MATLAB codes. Students and newcomers to the field of topology optimization can find the code here and download it. The code may be used in courses in structural optimization where students may be assigned to do extensions such as multiple load-cases, alternative mesh-independency schemes, passive areas, etc..

The Python packages required for running the code are:

  • numpy
  • scipy
  • matplotlib

The basic Python code is here (7K, Shift+click to save). Save the script and run the code by typing for example:

$ python 180 60 0.4 5.4 3.0 1

in a terminal.

A faster code utilizing the sparse Cholesky factorization routine in the CVXOPT Python package (downloadable from the cvxopt homepage) is found here (7K, Shift+click to save). Similarly to the basic code, the script can be run by typing the following in a terminal

$ python 180 60 0.4 5.4 3.0 1


The implementation is done in agreement with the 88-line MATLAB code, and thus the following paper can be used to decipher the Python implementation Efficient topology optimization in MATLAB using 88 lines of code, E. Andreassen, A. Clausen, M. Schevenels, B. S. Lazarov and O. Sigmund, Struct Multidisc Optim, Volume 43, Issue 1, p.1 - 16, (2011) 

An uncorrected version of the paper can be downloaded here


Since July 1, 2013 the basic Python code has been downloaded 10902 times by 3105 unique users.


Since July 1, 2013 the optimized Python code has been downloaded 6436 times by 1598 unique users.


Windows and Mac users:

We have tested the python scripts on Windows using the freely available Python(x,y) distribution (Python(x,y) homepage). Though the codes works fine, there can be an issue with on-the-fly visualization.

We have not tested the scripts on Mac.


Other open-source TopOpt works in Python:

The Topy-project (topy homepage) is a package written solely in Python which is capable of compliance problems, heat conduction problems and mechanism design problems.