Professional Documents
Culture Documents
A 99 Line Topology Optimization Code Written in Matlab PDF
A 99 Line Topology Optimization Code Written in Matlab PDF
Abstract The paper presents a compact Matlab im- fraction and then the program shows the correct optimal
plementation of a topology optimization code for com- topology for comparison.
pliance minimization of statically loaded structures. The In the literature, one can nd a multitude of approaches
total number of Matlab input lines is 99 including opti- for the solving of topology optimization problems. In the
mizer and Finite Element subroutine. The 99 lines are original paper Bendse and Kikuchi (1988) a so-called
divided into 36 lines for the main program, 12 lines for the microstructure or homogenization based approach was
Optimality Criteria based optimizer, 16 lines for a mesh- used, based on studies of existence of solutions.
independency lter and 35 lines for the nite element The homogenization based approach has been adopted
code. In fact, excluding comment lines and lines associ- in many papers but has the disadvantage that the deter-
ated with output and nite element analysis, it is shown mination and evaluation of optimal microstructures and
that only 49 Matlab input lines are required for solving their orientations is cumbersome if not unresolved (for
a well-posed topology optimization problem. By adding noncompliance problems) and furthermore, the resulting
three additional lines, the program can solve problems structures cannot be built since no denite length-scale
with multiple load cases. The code is intended for edu- is associated with the microstructures. However, the ho-
cational purposes. The complete Matlab code is given in mogenization approach to topology optimization is still
the Appendix and can be down-loaded from the web-site important in the sense that it can provide bounds on the
http://www.topopt.dtu.dk. theoretical performance of structures.
An alternative approach to topology optimization is
Key words topology optimization, education, optimal- the so-called power-law approach or SIMP approach
ity criteria, world-wide web, Matlab code (Solid Isotropic Material with Penalization) (Bendse
1989; Zhou and Rozvany 1991; Mlejnek 1992). Here, ma-
terial properties are assumed constant within each elem-
ent used to discretize the design domain and the variables
are the element relative densities. The material proper-
1 ties are modelled as the relative material density raised
Introduction to some power times the material properties of solid ma-
terial. This approach has been criticized since it was ar-
The Matlab code presented in this paper is intended gued that no physical material exists with properties de-
for engineering education. Students and newcomers to scribed by the power-law interpolation. However, a recent
the eld of topology optimization can down-load the paper by Bendse and Sigmund (1999) proved that the
code from the web-page http://www.topopt.dtu.dk. power-law approach is physically permissible as long as
The code may be used in courses in structural optimiza- simple conditions on the power are satised (e.g. p 3
tion where students may be assigned to do extensions for Poissons ratio equal to 13 ). To ensure existence of so-
such as multiple load-cases, alternative mesh-independ- lutions, the power-law approach must be combined with
ency schemes, passive areas, etc. Another possibility is to a perimeter constraint, a gradient constraint or with l-
use the program to develop students intuition for optimal tering techniques (see Sigmund and Petersson 1998, for
design. Advanced students may be asked to guess the op- an overview). The power-law approach to topology op-
timal topology for given boundary condition and volume timization has been applied to problems with multiple
constraints, multiple physics and multiple materials.
Received October 22, 1999
Whereas the solution of the above mentioned ap-
O. Sigmund proaches is based on mathematical programming tech-
Department of Solid Mechanics, Building 404, Technical Uni- niques and continuous design variables, a number of pa-
versity of Denmark, DK-2800 Lyngby, Denmark pers have appeared on solving the topology optimization
e-mail: sigmund@fam.dtu.dk problem as an integer problem. Beckers (1999) success-
121
fully solved large-scale compliance minimization prob- A topology optimization problem based on the power-
lems using a dual-approach but other approaches based law approach, where the objective is to minimize compli-
on genetic algorithms or other semi-random approaches ance can be written as
require thousands of function evaluations even for small
N
number of elements and must be considered impractical. min T
: c(x) = U KU = (xe ) ue k0 ue
p T
x
Apart from above mentioned approaches, which all e=1
solve well dened problems (e.g. minimization of com- V (x)
subject to : V0 = f ,
pliance) a number of heuristic or intuition based ap-
proaches have been shown to decrease compliance or : KU = F
other objective functions. Among these methods are so-
called evolutionary design methods (see e.g. Xie and : 0 < xmin x 1
Steven 1997; Baumgartner et al. 1992). Apart from be- (1)
ing very easy to understand and implement (at least
where U and F are the global displacement and force
for the compliance minimization case), the main moti-
vectors, respectively, K is the global stiness matrix, ue
vation for the evolutionary approaches seems to be that
and ke are the element displacement vector and stiness
mathematically based or continuous variable approaches
matrix, respectively, x is the vector of design variables,
involve some complex calculus operations and mathe-
xmin is a vector of minimum relative densities (non-zero
matical programming (citation from Li et al. 1999) and
to avoid singularity), N (= nelx nely) is the number
they contain mathematical methods of some complex-
of elements used to discretize the design domain, p is the
ity (citation from Zhao et al. 1998) whereas the evo-
penalization power (typically p = 3), V (x) and V0 is the
lutionary approach takes advantage of powerful com-
material volume and design domain volume, respectively
puting technology and intuitive concepts of evolution
and f (volfrac) is the prescribed volume fraction.
processes in nature (citation from Li et al. 1999). Two
The optimization problem (1) could be solved using
things can be argued against this. First, the evolutionary
several dierent approaches such as Optimality Criteria
approaches become complicated themselves, once more
(OC) methods, Sequential Linear Programming (SLP)
complex objectives than compliance minimization are
methods or the Method of Moving Asymptotes (MMA by
considered and second, as shown in this paper, the math-
Svanberg 1987) and others. For simplicity, we will here
ematically based approaches for compliance minimiza-
use a standard OC-method.
tion are simple to implement as well and are compu-
Following Bendse (1995) a heuristic updating scheme
tationally equally ecient. Furthermore, mathematical
for the design variables can be formulated as
programming based methods can easily be extended to
other non-compliance objectives such as non-self-adjoint xnew =
e
and multiphysics problems and to problems with multiple
constraints (e.g. Sigmund 1999). Extensions of the evolu-
max(xmin , xe m)
tionary approach to such cases seem more questionable.
if xe Be max(xmin , xe m) ,
The complete Matlab code is given in the Appendix.
The remainder of the paper consists of denition and
x B
e e
discussion of the optimization problem (Sect. 2), com-
if max(xmin , xe m) < xe Be < min(1, xe + m) ,
ments about the Matlab implementation (Sect. 3) fol-
lowed by a discussion of extensions (Sect. 4) and a conclu-
min(1, xe + m)
sion (Sect. 5).
if min(1, xe + m) xe Be ,
(2)
2
where m (move) is a positive move-limit, (= 1/2) is
The topology optimization problem
a numerical damping coecient and Be is found from the
optimality condition as
A number of simplications are introduced to simplify the
Matlab code. First, the design domain is assumed to be c
rectangular and discretized by square nite elements. In xe
this way, the numbering of elements and nodes is simple Be = , (3)
V
(column by column starting in the upper left corner) and
the aspect ratio of the structure is given by the ratio of xe
elements in the horizontal (nelx) and the vertical direc- where is a Lagrangian multiplier that can be found by
tion (nely).1 a bi-sectioning algorithm.
The sensitivity of the objective function is found as
1
Names in type-writer style refer to Matlab variable names
c
that dier from the obvious (see the Matlab code in the = p(xe )p1 uTe k0 ue . (4)
Appendix) xe
122
Hf = rmin dist(e, f ) ,
The default boundary conditions correspond to half of the
{f N | dist(e, f ) rmin }, e = 1, . . . , N , (6) MBB-beam (Fig. 1). The load is applied vertically in
the upper left corner and there is symmetric boundary
where the operator dist(e, f ) is dened as the distance be- conditions along the left edge and the structure is sup-
tween centre of element e and centre of element f . The ported horizontally in the lower right corner.
convolution operator Hf is zero outside the lter area. Important details of the Matlab code are discussed in
The convolution operator decays linearly with the dis- the following subsections.
tance from element f . Instead of the original sensitivities
(4), the modied sensitivities (5) are used in the Optimal- 3.1
ity Criteria update (3). Main program (lines 136)
3.2 11
00 01
00
11 10
Optimality criteria based optimizer (lines 3748) 00
11 1010
The updated design variables are found by the opti-
00
11
00
11
00
11
00
11
? 10
1010
mizer (lines 3748). Knowing that the material volume 00
11 10
(sum(sum(xnew))) is a monotonously decreasing func-
tion of the Lagrange multiplier (lag), the value of the
Fig. 2 Topology optimization of a cantilever beam. Left: de-
Lagrangian multiplier that satises the volume constraint sign domain and right: topology optimized beam
can be found by a bi-sectioning algorithm (lines 40-48).
The bi-sectioning algorithm is initialized by guessing
a lower l1 and an upper l2 bound for the Lagrangian
multiplier (line 39). The interval which bounds the La- the dierence between all degrees of freedom and the xed
grangian multiplier is repeatedly halved until its size is degrees of freedom (line 82).
less than the convergence criteria (line 40). The element stiness matrix is calculated in lines 86
99. The 8 by 8 matrix for a square bi-linear 4-node elem-
ent was determined analytically using a symbolic manip-
3.3 ulation software. The Youngs modulus E and the Pois-
Mesh-independency ltering (lines 4964) sons ratio nu can be altered in lines 88 and 89.
84 U(freedofs,:) = K(freedofs,freedofs) \
F(freedofs,:); 4.2
Multiple load cases
where freedofs indicate the degrees of freedom which
are unconstrained. Mostly, it is easier to dene the de- It is also very simple to extend the algorithm to account
grees of freedom that are xed (fixeddofs) thereafter the for multiple load cases. In fact, this can be done by adding
freedofs are found automatically using the Matlab oper- only three additional lines and making minor changes to
ator setdiff which nds the free degrees of freedoms as another 4 lines.
124
01 01
thus lines 2022 are substituted with the lines 1010 1010
79 F(2*(nelx+1)*(nely+1),1) = -1.;
F(2*(nelx)*(nely+1)+2,2) = 1.; when the following 10 lines were added to the main
program (after line 4) in order to nd passive elem-
ents within a circle with radius nely/3. and center
The input line for Fig. 3 is (nely/2., nelx/3.)
This paper has presented a very simple implementation Bendse, M.P. 1989: Optimal shape design as a material dis-
of a mathematical programming base topology optimiza- tribution problem. Struct. Optim. 1, 193202
tion algorithm. The code is implemented using only 99
Bendse, M.P. 1995: Optimization of structural topology,
Matlab input lines and includes optimizer, mesh-indepe- shape and material . Berlin, Heidelberg, New York: Springer
ndency ltering and Finite Element code.
The Matlab code can be down-loaded from the web- Bendse, M.P.; Kikuchi, N. 1988: Generating optimal topolo-
page http://www.topopt.dtu.dk and is intended for ed- gies in optimal design using a homogenization method. Comp.
ucational purposes. The code can easily be extended to Meth. Appl. Mech. Engrg. 71, 197224
include multi load problems and the denition of passive Bendse, M.P.; Sigmund, O. 1999: Material interpolations in
areas. topology optimization. Arch. Appl. Mech. 69, 635654
Running the code in Matlab is rather slow compared
to a Fortran implementation of the same code which can Li, Q.; Steven, G.P.; Xie, Y. M. 1999: On equivalence between
be tested at the web-site http://www.topopt.dtu.dk. stress criterion and stiness criterion in evolutionary struc-
However, an add-on package to Matlab (MATLAB Com- tural optimization. Struct. Optim. 18, 6773
piler) allows for the generation of more ecient C-code Mlejnek, H.P. 1992: Some aspects of the genesis of structures.
that can be optimized for run-time (this option, how- Struct. Optim. 5, 6469
ever, has not been tested by the author). It should be
noted that speed can be gained by modifying the Mat- Sigmund, O. 1994: Design of material structures using top-
lab code itself, however the speed is gained on the cost of ology optimization. Ph.D. Thesis, Department of Solid Me-
chanics, Technical University of Denmark
simplicity of the program. The modication is suggested
by Andreas Rietz from Linkoping University who uses Sigmund, O. 1997: On the design of compliant mechanisms
sparsity options in the assembly of the global stiness ma- using topology optimization. Mech. Struct. Mach. 25, 495
trix. The reader may down-load his code at the web-page: 526
126