You are on page 1of 33

University of Liège

1st Master in Mechanical engineering


Year 2020-2021

MECA0027-1
S TRUCTURAL AND M ULTIDISCIPLINARY O PTIMIZATION

Project 3 : Topology Optimization

MECA0027: Structural and Multidisciplinary


Optimization
Pierre Duysinx
Patricia Tossings
D ELECLOZ Victor S171715
Pablo Alarcón
Denis Trillet

L IÈGE
December 2020
CONTENTS 0

Contents

1 Influence of the numerical parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1


1.1 The filter size 1
1.2 The penalization parameter 1
1.3 Volume fraction 2
1.4 The minimum gauge of the density 2
1.5 FE discretization 3

2 Michel cantilever beam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3


2.1 Magnitude of load 3
2.2 Support conditions 3
2.3 Direction of the forces 4

3 Other design problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4


4 Bridge problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
5 Passive elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5.0.1 Optimal compliance with passive element . . . . . . . . . . . . . . . . . . . . . . . 7
6 Matlab’s code and extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
6.1 Top.m 7
6.2 Q3_4.m 7
6.3 Bridge.m 8
6.4 Top_extended.m 8
6.5 Piece.m 10
7 Appendix - Codes MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1

1 Influence of the numerical parameters

For each iteration in the topology optimization loop, the code generates a picture of the current density
distribution. The one shown in this report is the one obtained for the last iteration.

This section used theses argument with one which is modified at a time
• nelx = 60, the number of elements in the horizontal directions.
• nely = 20, the number of elements in the vertical directions.
• volfrac = 0.5, the volume fraction.
• penal = 3.0, the penalization power.
• rmin = 1.1, the filter size (divided by element size).

1.1 The filter size


The first figure shows a filter size too small that induce numerical instabilities where the intermediate
densities are replaced by a checkerboard patterns. The filter size must therefore be sufficiently large to
avoid theses numerical instabilities. But not too large either to avoid this last blurred figure because the
solution is non discrete with the intermediate densities.

Take a filter size to 1.1 is the best to minimise the compliance

F IGURE 1: MBB-beam problem : Filter size = {1, 1.6, 2.0, 4.0}

1.2 The penalization parameter


Penalization parameter is used to end-up with black and white solutions whereas there is a continuous
distribution of densities. We have therefore to take this parameter high enough to penalise intermediate
densities and end up with a topology made only of solids and voids.

If it is too low, the first figure shows that there is not enough distinction between void and solid because
the intermediate densities have not been penalized enough.

This parameter can influence the mesh. For example if the penalization parameter is taken large, it
means that the intermediate densities are very small and therefore the structure itself changes. Another
way to think is that if there are lower densities somewhere, these densities can be used elsewhere to
minimized the compliance since the fraction of solid is fixed.
Something interesting is that when penalization parameter is too large, (here p = 8.0), Matlab returns
an error due to matrix singularity. This is in fact due to the SIMP method whose intermediate densities
due to the power law are too close to zero.
1.3 Volume fraction 2

F IGURE 2: MBB-beam problem : Penalization parameter = {1, 2.0, 4.0, 8.0}

1.3 Volume fraction


In the code provided, compliance is minimized and is subject to an equality constraint regarding the
fraction volume of solids.
It can be noticed that the change of the volume fraction has no major influence on the topology itself.
It is more a sizing influence with the thickness of the structure that changes. Fraction volume of 30%
is in our case a good value. Whereas with a fraction volume of 12%, the algorithm fail to converge as
it can be seen in the first figure. It can be concluded from figure below that a fraction volume lower
that 15% must be to be avoided to ensure convergence.

Note that the compliance decrease with a higher volume fraction. This can be deduced because if one
increase the fraction of solid, the structure will be more rigid and the displacement under the loads will
be smaller. Compliance will therefore be smaller since it can be interpreted as the displacement under
the loads.
Effects of volume fraction on compliance is large. For instance, from a volume fraction from 50% to
60%, compliance decreases from 195 to 164.

F IGURE 3: MBB-beam problem : Volume fraction = {0.12, 0.15 , 0.30 , 0.45 }

1.4 The minimum gauge of the density


The minimum gauge of the density is the minimum relatives densities of elements. It must obviously
be larger than 0 to avoid singularities. In our case, instability appear when this parameter is lower than
10−4

The optimal compliance value decrease with the minimum gauge of the density. This is because a low
minimum gauge of the density allows more fine and complex mesh that are optimized to sustain loads.
1.5 FE discretization 3

F IGURE 4: MBB-beam problem : min density = {0.001 , 0.01 , 0.1 , 0.2}

1.5 FE discretization
The compliance is generally lower when the discretization of the design space is higher. As it will be
shown in the section Bridge problem, a more discretized mesh allow more fine and complex structure
which allows a more optimised structure to support the loads.

2 Michel cantilever beam

F IGURE 5: Michel cantilever beam problem

2.1 Magnitude of load


The magnitude of the load does not affect the design as can be seen in the figure below were the vertical
load was set to {0.1 , 1 , 2} respectively.

F IGURE 6: Michel cantilever beam problem : Load = {0.1 , 1 , 2}

2.2 Support conditions


The clamping is reduced by half to the top in order notice that it has affect the solution. The modification
of support conditions modify the layout as it should.
2.3 Direction of the forces 4

F IGURE 7: Michel cantilever beam problem - Half clamped side

2.3 Direction of the forces


The design found below with a force that is horizontal is very different. One can conclude that direction
of forces have a strong effect on the design.

F IGURE 8: Michel cantilever beam problem - Horizontal load

3 Other design problems

The following figures show on the left the loads as well as support conditions. While the figures on the
right show the optimization of the topology.
Case 1

Case 2

Case 3
5

Case 4

Case 5

Case 6

One can feel a physical logic of the six cases presented through topological optimization.
• In the first case, the center load is retained by cross-connections that connect the center load to
the upper structure.
• This is the same for the second case, but this time there is no horizontal connection at the bottom
due to the full clamping at both ends
• Case three and four show that the cross-connections are more vertical due to the more uniform
load distribution.
• Cases five and six show once again that the position of the loads has an impact on the design.

4 Bridge problem

F IGURE 9: Bridge problem

The two figures below show the problem of the bridge with two layers of elements to enforce the
presence of the road.

All parameters fixed, the two figures have a different topology due to the difference in the FE discrati-
sation. The one on the right has a more discretized mesh, which allows it to take a more complex shape
with rounded corners at the top for example. There is also a completely different topology with more
cross-links. This raises some concerns about the uniqueness of the solution as the solution depends on
6

the mesh. The problem may not be as well posed as it seems.

What we have done is to take a filter of size 1.1 because it avoids the checkerboard patern. And we
didn’t take bigger to avoid blurred layout due to the intermediate density. But this choice of filter size
still gives a dependence of the mesh. In fact, we could have used a heaviside filter to avoid the blurry
figures and also avoid the dependency of the mesh (as well as the checkerboard pattern).

To sum up, there is a mesh dependency, new members appear when refining the mesh as well as the
number of holes and features. For future development, density filtering can be better introduced to
have a problem that avoid this mesh dependency.

F IGURE 10: Bridge problem (nelx*nely) = F IGURE 11: Bridge problem (nelx*nely) =
(60*20) (120*20)

5 Passive elements

The following topology has forced some elements to be passive and active. On the left of the topology
there is a hole with solid layers around it. This was done in Matlab code with the equation of a circle
and by forcing the elements to be 0.001 density inside the circle and on the other hand, set density to
one on some layer outside the circle.
A second hole has been made to the right of the topology with the equation of an ellipse. Once again,
the elements inside the ellipse were fixed with a density of 0.001.

The first iteration is shown in figure 13 to see the elliptical hole, because it is not visible at the last
iteration.

F IGURE 12: Loads and


supports F IGURE 13: First iteration F IGURE 14: Last iteration
7

Optimal compliance with passive element


Optimal compliance of problem figure 12 ( With holes ): 523,8 Nm
Optimal compliance of problem figure 15 ( Without holes ) : 72.6 Nm

F IGURE 15: Loads and supports F IGURE 16: Last iteration


When using passive or active prescribed zone, the compliance is higher. Indeed, the densities are
forced in some places and this does not allow to use the material in the most efficient way everywhere.
The compliance are then higher.

6 Matlab’s code and extensions

This section explains the matlab code implemented, that can be found in the annex. All scripts can be
easily modified with the parameters initialized at the beginning.

The modification made are identified between the line : "%% My Code %%" and "%% End %%".
And after some lines with "My Lines".

List of the codes :


• Top.m
• Q3_4.m
• Bridge.m
• Top_extended.m
• Piece.m

6.1 Top.m
The first code is a simple script that solve the main designs in this report. It is simply asked in the
command line wich cases to solve. I.e the 6 cases of load & support or the michel cantilever beam
with different configurations or the initial design.

6.2 Q3_4.m
This code solves the problem with passive element of question 3.4 in the instruction. ( figure 12 ) It
consists of 4 loads applied at the end of a beam with 1 elliptical hole and 1 reinforced circular hole.
6.3 Bridge.m 8

6.3 Bridge.m
The second code solves the bridge problem. Where it is asked in the command how many lower layers
the topology should have.
One can notice that by increasing the number of layers, the compliance is increasing. Indeed, as
mentioned above, compliance increases with the use of active/passive elements. The figure on the right
shows that when too many layers are put on, no topological optimization is made. Because In the case
shown, the discretization is done according to 20 horizontal layers ( and 60 vertical), therefore if we
force 10 layers to be active, the fraction of solid volume has therefore already reached 50% and no
other evolution can occur because volume fraction was set to 50%.

F IGURE 17: Bridge problem F IGURE 18: Bridge problem F IGURE 19: Bridge problem
2 lower layers 5 lower layers 10 lower layers

6.4 Top_extended.m
The third code is the most interesting since it is a large extension of the initial code. It is first asked the
type of load to impose. Then, it is ask which clamping must be put on both side of the structure. The
topology is then solved according to the support and loads imposed. As a result, there are many more
possible configurations than those presented in this report.
6.4 Top_extended.m 9

Central point
load

Three bottom
Load  point load
Top

Distributed Elevation ?
Mid
load

Bottom

Supported end

Clamped end

Left side
Clamped side

Free

Support

Supported end

Clamped end
Right side

Clamped side

Free

F IGURE 20: Charts of all possible design

Note that when choosing the supports, attention must be paid to take sufficient support to block the
movement of both translations and the rotation movement in order to avoid hypo-static structure.
For example, when choosing three bottom points load, both ends clamped, one can found :

F IGURE 21: Three bottom point loads, both ends clamped


6.5 Piece.m 10

6.5 Piece.m
To go further, the last code is a script that solve a problem of a specific mechanical part of engineering.
A vertical load is applied in the center of the left hole. The clamping are on the two holes on the right.

F IGURE 22: Loads and supports F IGURE 23: Topology optimization

There are also 4 interesting parameters in this script for each hole:

pos1_x is the position of the center of the hole along the x-axis.
pos1_y is the position of the center of the hole along the y-axis.
r1 is the radius of the internal hole.
t1 is the relative thickness of the layer around the hole (relative according to the radius)
This script is easily editable and can be easily modified to make other holes with other loads.
11

7 Appendix - Codes MATLAB


Top.m

1 %%%% A 99 LINE TOPOLOGY OPTIMIZATION CODE BY OLE SIGMUND, JANUARY 2000 %%%
2 %%%% CODE MODIFIED FOR INCREASED SPEED, September 2002, BY OLE SIGMUND %%%
3 function top();
4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% My Code %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 nelx = 60; % number of elements in the horizontal directions.
6 nely = 20; % number of elements in the vertical directions.
7 volfrac = 0.5; % the volume fraction.
8 penal = 3.0; % the penalization power.
9 rmin = 1.1; % the filter size (divided by element size).
10
11 disp('Which case do you want to solve ? Press:')
12 disp(' 1 to 6 for one of the six first designs')
13 disp(' 7 for the Michel cantilever beam with a vertical load ')
14 disp(' 8 for the Michel cantilever beam with a horizontal load ')
15 disp(' 9 for the Michel cantilever beam with horizontal&vertical ...
loads at both end ')
16 disp(' 10 for inital top code design')
17 prompt = '';
18 cases = input(prompt);
19

20 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% End %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


21 % INITIALIZE
22 x(1:nely,1:nelx) = volfrac;
23 loop = 0;
24 change = 1.;
25 % START ITERATION
26 while change > 0.01
27 loop = loop + 1;
28 xold = x;
29 % FE−ANALYSIS
30 [U]=FE(nelx,nely,x,penal,cases);
31 % OBJECTIVE FUNCTION AND SENSITIVITY ANALYSIS
32 [KE] = lk;
33 c = 0.;
34 for ely = 1:nely
35 for elx = 1:nelx
36 n1 = (nely+1)*(elx−1)+ely;
37 n2 = (nely+1)* elx +ely;
38 Ue = U([2*n1−1;2*n1; 2*n2−1;2*n2; 2*n2+1;2*n2+2; 2*n1+1;2*n1+2],1);
39 c = c + x(ely,elx)^penal*Ue'*KE*Ue;
40 dc(ely,elx) = −penal*x(ely,elx)^(penal−1)*Ue'*KE*Ue;
41 end
42 end
43 % FILTERING OF SENSITIVITIES
44 [dc] = check(nelx,nely,rmin,x,dc);
45 % DESIGN UPDATE BY THE OPTIMALITY CRITERIA METHOD
46 [x] = OC(nelx,nely,x,volfrac,dc);
47 % PRINT RESULTS
48 change = max(max(abs(x−xold)));
49 disp([' It.: ' sprintf('%4i',loop) ' Obj.: ' sprintf('%10.4f',c) ...
50 ' Vol.: ' sprintf('%6.3f',sum(sum(x))/(nelx*nely)) ...
51 ' ch.: ' sprintf('%6.3f',change )])
52 % PLOT DENSITIES
53 colormap(gray); imagesc(−x); axis equal; axis tight; axis off;pause(1e−6);
12

54 end
55 %%%%%%%%%% OPTIMALITY CRITERIA UPDATE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 function [xnew]=OC(nelx,nely,x,volfrac,dc)
57 l1 = 0; l2 = 100000; move = 0.2;
58 while (l2−l1 > 1e−4)
59 lmid = 0.5*(l2+l1);
60 xnew = max(0.001,max(x−move,min(1.,min(x+move,x.*sqrt(−dc./lmid)))));
61 if sum(sum(xnew)) − volfrac*nelx*nely > 0;
62 l1 = lmid;
63 else
64 l2 = lmid;
65 end
66 end
67 %%%%%%%%%% MESH−INDEPENDENCY FILTER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 function [dcn]=check(nelx,nely,rmin,x,dc)
69 dcn=zeros(nely,nelx);
70 for i = 1:nelx
71 for j = 1:nely
72 sum=0.0;
73 for k = max(i−floor(rmin),1):min(i+floor(rmin),nelx)
74 for l = max(j−floor(rmin),1):min(j+floor(rmin),nely)
75 fac = rmin−sqrt((i−k)^2+(j−l)^2);
76 sum = sum+max(0,fac);
77 dcn(j,i) = dcn(j,i) + max(0,fac)*x(l,k)*dc(l,k);
78 end
79 end
80 dcn(j,i) = dcn(j,i)/(x(j,i)*sum);
81 end
82 end
83 %%%%%%%%%% FE−ANALYSIS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 function [U]=FE(nelx,nely,x,penal,cases)
85 [KE] = lk;
86 K = sparse(2*(nelx+1)*(nely+1), 2*(nelx+1)*(nely+1));
87 F = sparse(2*(nely+1)*(nelx+1),1); U = zeros(2*(nely+1)*(nelx+1),1);
88 for elx = 1:nelx
89 for ely = 1:nely
90 n1 = (nely+1)*(elx−1)+ely;
91 n2 = (nely+1)* elx +ely;
92 edof = [2*n1−1; 2*n1; 2*n2−1; 2*n2; 2*n2+1; 2*n2+2; 2*n1+1; 2*n1+2];
93 K(edof,edof) = K(edof,edof) + x(ely,elx)^penal*KE;
94 end
95 end
96 %DEFINE LOADS AND SUPPORTS (HALF MBB−BEAM)
97 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% My Code %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 F = sparse(2*(nely+1)*(nelx+1),1);
99
100 switch cases
101

102 % One end supported. One end clamped. Poind load.


103 case 1
104 F(2*(nely+1)*ceil((nelx+1)/2),1) = −1 ;
105 fixeddofs = union([2*(nely+1),2*(nely+1)−1] ,[2*(nelx+1)*(nely+1)]);
106
107 % Both ends are clamped. Poind load.
108 case 2
109 F(2*(nely+1)*ceil((nelx+1)/2),1) = −1 ;
110 fixeddofs = union([2*(nely+1),2*(nely+1)−1] , ...
[2*(nelx+1)*(nely+1), 2*(nelx+1)*(nely+1)−1]);
111
13

112 % 3 point loads applied simultaneously. One end supported. One end clamped.
113 case 3
114
115 F(2*(nely+1)*ceil((nelx+1)/4),1) = −1/3 ;
116 F(2*(nely+1)*ceil((nelx+1)/2),1) = −1/3 ;
117 F(2*(nely+1)*ceil((nelx+1)*3/4),1) = −1/3 ;
118 fixeddofs = union([2*(nely+1),2*(nely+1)−1] ,[2*(nelx+1)*(nely+1)]);
119
120 % Distributed load. One end supported. One end clamped.
121 case 4
122
123 F(2*(nely+1),1) = −1/(nelx+1);
124 for i = 1:nelx
125 F(2*(nely+1)*ceil(i*(nelx+1)/nelx),1) = −1/(nelx+1);
126 end
127 fixeddofs = union([2*(nely+1),2*(nely+1)−1] ,[2*(nelx+1)*(nely+1)]);
128
129 %Distributed load at mid elevation. One end supported. Clamped boundaries
130 case 5
131
132 F(2*ceil(nely/2+1),1) = −1/(nelx+1);
133 for i = 1:nelx
134 F(2*(nely+1)*ceil(i*(nelx+1)/nelx)−nely) = −1/(nelx+1);
135 end
136 fixeddofs = union([1:2*(nely+1)] , ...
[(2*(nelx)*(nely+1)+1):2*(nelx+1)*(nely+1)]);
137
138 %Distributed load at top elevation. Clamped boundaries.
139 case 6
140

141 F(2,1) = −1/(nelx+1);


142 for i = 1:nelx
143 F(2*(nely+1)*ceil(i*(nelx+1)/nelx)−2*nely) = −1/(nelx+1);
144 end
145 fixeddofs = union([1:2*(nely+1)] , ...
[(2*(nelx)*(nely+1)+1):2*(nelx+1)*(nely+1)]);
146
147 %Michel cantilever beam problem. Vertical load
148 case 7
149
150 F(2*(nely+1)*(nelx+1),1) = −1 ;
151 fixeddofs = [1:2*(nely+1)];
152
153 %Michel cantilever beam problem. Horizontal load
154 case 8
155
156 F(2*(nely+1)*(nelx+1)−1,1) = 1 ;
157 fixeddofs = [1:2*(nely+1)];
158
159 %Michel cantilever beam problem. Horizontal and vertical load at both end
160 case 9
161
162 %Horizontal upper load
163 F(2*(nely+1)*(nelx)+1,1) = 1;
164
165 %Vertical upper load
166 F(2*(nely+1)*(nelx)+2,1) = 1 ;
167
168 %Horizontal lower load
14

169 F(2*(nely+1)*(nelx+1)−1,1) = 1;
170
171 %Vertical lower load
172 F(2*(nely+1)*(nelx+1),1) = −1 ;
173 fixeddofs = [1:2*(nely+1)];
174

175 case 10
176 % Initial design of MBB−Beam problem.
177 F(2,1) = −1;
178 fixeddofs = union([1:2:2*(nely+1)],[2*(nelx+1)*(nely+1)]);
179
180 end
181
182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% End %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 alldofs = [1:2*(nely+1)*(nelx+1)];
184 freedofs = setdiff(alldofs,fixeddofs);
185 % SOLVING
186 U(freedofs,:) = K(freedofs,freedofs) \ F(freedofs,:);
187 U(fixeddofs,:)= 0;
188 %%%%%%%%%% ELEMENT STIFFNESS MATRIX %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189 function [KE]=lk
190 E = 1.;
191 nu = 0.3;
192 k=[ 1/2−nu/6 1/8+nu/8 −1/4−nu/12 −1/8+3*nu/8 ...
193 −1/4+nu/12 −1/8−nu/8 nu/6 1/8−3*nu/8];
194 KE = E/(1−nu^2)*[ k(1) k(2) k(3) k(4) k(5) k(6) k(7) k(8)
195 k(2) k(1) k(8) k(7) k(6) k(5) k(4) k(3)
196 k(3) k(8) k(1) k(6) k(7) k(4) k(5) k(2)
197 k(4) k(7) k(6) k(1) k(8) k(3) k(2) k(5)
198 k(5) k(6) k(7) k(8) k(1) k(2) k(3) k(4)
199 k(6) k(5) k(4) k(3) k(2) k(1) k(8) k(7)
200 k(7) k(4) k(5) k(2) k(3) k(8) k(1) k(6)
201 k(8) k(3) k(2) k(5) k(4) k(7) k(6) k(1)];
202 %
203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204 % This Matlab code was written by Ole Sigmund, Department of Solid ...
%
205 % Mechanics, Technical University of Denmark, DK−2800 Lyngby, Denmark. ...
%
206 % Please sent your comments to the author: sigmund@fam.dtu.dk ...
%
207 % ...
...
%
208 % The code is intended for educational purposes and theoretical details ...
%
209 % are discussed in the paper ...
%
210 % "A 99 line topology optimization code written in Matlab" ...
%
211 % by Ole Sigmund (2001), Structural and Multidisciplinary Optimization, ...
%
212 % Vol 21, pp. 120−−127. ...
%
213 % ...
...
%
214 % The code as well as a postscript version of the paper can be ...
%
15

215 % downloaded from the web−site: http://www.topopt.dtu.dk ...


%
216 % ...
...
%
217 % Disclaimer: ...
%
218 % The author reserves all rights but does not guaranty that the code is ...
%
219 % free from errors. Furthermore, he shall not be liable in any event ...
%
220 % caused by the use of the program. ...
%
221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Q3_4.m

1 %%%% A 99 LINE TOPOLOGY OPTIMIZATION CODE BY OLE SIGMUND, JANUARY 2000 %%%
2 %%%% CODE MODIFIED FOR INCREASED SPEED, September 2002, BY OLE SIGMUND %%%
3 function Q3_4();
4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% My Code %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 nelx = 90; % number of elements in the horizontal directions.
6 nely = 60; % number of elements in the vertical directions.
7 volfrac = 0.5; % the volume fraction.
8 penal = 3.0; % the penalization power.
9 rmin = 1.1; % the filter size (divided by element size).
10

11 % INITIALIZE
12 x(1:nely,1:nelx) = volfrac;
13 passive(1:nely,1:nelx) = 0;
14 active(1:nely,1:nelx) = 0;
15 for ely = 1:nely
16 for elx = 1:nelx
17 if sqrt((ely−nely/2.)^2+(elx−nelx/4.)^2) < nely/4.
18 active(ely,elx) = 1;
19 x(ely,elx) = 1;
20 end
21 end
22 end
23
24 for ely = 1:nely
25 for elx = 1:nelx
26 if sqrt((ely−nely/2.)^2+(elx−nelx/4.)^2) < nely/5.
27 passive(ely,elx) = 1;
28 x(ely,elx) = 0.001;
29 end
30 end
31 end
32
33
34 for ely = 1:nely
35 for elx = 1:nelx
36 if sqrt(((ely−nely/2.)/2)^2+((elx−nelx/1.3)^2)/8) < nely/12
37 passive(ely,elx) = 1;
38 x(ely,elx) = 0.001;
39 end
40 end
16

41 end
42
43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% End %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44
45 loop = 0;
46 change = 1.;
47 % START ITERATION
48 while change > 0.01
49 loop = loop + 1;
50 xold = x;
51 % FE−ANALYSIS
52 [U]=FE(nelx,nely,x,penal);
53 % OBJECTIVE FUNCTION AND SENSITIVITY ANALYSIS
54 [KE] = lk;
55 c = 0.;
56 for ely = 1:nely
57 for elx = 1:nelx
58 n1 = (nely+1)*(elx−1)+ely;
59 n2 = (nely+1)* elx +ely;
60 Ue = U([2*n1−1;2*n1; 2*n2−1;2*n2; 2*n2+1;2*n2+2; 2*n1+1;2*n1+2],1);
61 c = c + x(ely,elx)^penal*Ue'*KE*Ue;
62 dc(ely,elx) = −penal*x(ely,elx)^(penal−1)*Ue'*KE*Ue;
63 end
64 end
65 % FILTERING OF SENSITIVITIES
66 [dc] = check(nelx,nely,rmin,x,dc);
67 % DESIGN UPDATE BY THE OPTIMALITY CRITERIA METHOD
68 [x] = OC(nelx,nely,x,volfrac,dc,passive,active); %My line
69 % PRINT RESULTS
70 change = max(max(abs(x−xold)));
71 disp([' It.: ' sprintf('%4i',loop) ' Obj.: ' sprintf('%10.4f',c) ...
72 ' Vol.: ' sprintf('%6.3f',sum(sum(x))/(nelx*nely)) ...
73 ' ch.: ' sprintf('%6.3f',change )])
74 % PLOT DENSITIES
75 colormap(gray); imagesc(−x); axis equal; axis tight; axis off;pause(1e−6);
76 end
77 %%%%%%%%%% OPTIMALITY CRITERIA UPDATE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 function [xnew]=OC(nelx,nely,x,volfrac,dc,passive,active)
79 l1 = 0; l2 = 100000; move = 0.2;
80 while (l2−l1 > 1e−4)
81 lmid = 0.5*(l2+l1);
82 xnew = max(0.001,max(x−move,min(1.,min(x+move,x.*sqrt(−dc./lmid))))); ...
%My line
83 xnew(find(active)) = 1; %My line
84 xnew(find(passive)) = 0.001; %My line
85 if sum(sum(xnew)) − volfrac*nelx*nely > 0;
86 l1 = lmid;
87 else
88 l2 = lmid;
89 end
90 end
91 %%%%%%%%%% MESH−INDEPENDENCY FILTER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 function [dcn]=check(nelx,nely,rmin,x,dc)
93 dcn=zeros(nely,nelx);
94 for i = 1:nelx
95 for j = 1:nely
96 sum=0.0;
97 for k = max(i−floor(rmin),1):min(i+floor(rmin),nelx)
98 for l = max(j−floor(rmin),1):min(j+floor(rmin),nely)
17

99 fac = rmin−sqrt((i−k)^2+(j−l)^2);
100 sum = sum+max(0,fac);
101 dcn(j,i) = dcn(j,i) + max(0,fac)*x(l,k)*dc(l,k);
102 end
103 end
104 dcn(j,i) = dcn(j,i)/(x(j,i)*sum);
105 end
106 end
107 %%%%%%%%%% FE−ANALYSIS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 function [U]=FE(nelx,nely,x,penal)
109 [KE] = lk;
110 K = sparse(2*(nelx+1)*(nely+1), 2*(nelx+1)*(nely+1));
111 F = sparse(2*(nely+1)*(nelx+1),1); U = zeros(2*(nely+1)*(nelx+1),1);
112 for elx = 1:nelx
113 for ely = 1:nely
114 n1 = (nely+1)*(elx−1)+ely;
115 n2 = (nely+1)* elx +ely;
116 edof = [2*n1−1; 2*n1; 2*n2−1; 2*n2; 2*n2+1; 2*n2+2; 2*n1+1; 2*n1+2];
117 K(edof,edof) = K(edof,edof) + x(ely,elx)^penal*KE;
118 end
119 end
120 % DEFINE LOADS AND SUPPORTS
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% My Code %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122

123 %Horizontal upper load


124 F(2*(nely+1)*(nelx)+1,1) = 1;
125
126 %Vertical upper load
127 F(2*(nely+1)*(nelx)+2,1) = 1 ;
128

129 %Horizontal lower load


130 F(2*(nely+1)*(nelx+1)−1,1) = 1;
131
132 %Vertical lower load
133 F(2*(nely+1)*(nelx+1),1) = −1 ;
134 fixeddofs = [1:2*(nely+1)];
135
136
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% End %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 alldofs = [1:2*(nely+1)*(nelx+1)];
139 freedofs = setdiff(alldofs,fixeddofs);
140 % SOLVING
141 U(freedofs,:) = K(freedofs,freedofs) \ F(freedofs,:);
142 U(fixeddofs,:)= 0;
143 %%%%%%%%%% ELEMENT STIFFNESS MATRIX %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 function [KE]=lk
145 E = 1.;
146 nu = 0.3;
147 k=[ 1/2−nu/6 1/8+nu/8 −1/4−nu/12 −1/8+3*nu/8 ...
148 −1/4+nu/12 −1/8−nu/8 nu/6 1/8−3*nu/8];
149 KE = E/(1−nu^2)*[ k(1) k(2) k(3) k(4) k(5) k(6) k(7) k(8)
150 k(2) k(1) k(8) k(7) k(6) k(5) k(4) k(3)
151 k(3) k(8) k(1) k(6) k(7) k(4) k(5) k(2)
152 k(4) k(7) k(6) k(1) k(8) k(3) k(2) k(5)
153 k(5) k(6) k(7) k(8) k(1) k(2) k(3) k(4)
154 k(6) k(5) k(4) k(3) k(2) k(1) k(8) k(7)
155 k(7) k(4) k(5) k(2) k(3) k(8) k(1) k(6)
156 k(8) k(3) k(2) k(5) k(4) k(7) k(6) k(1)];
157 %
18

158 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159 % This Matlab code was written by Ole Sigmund, Department of Solid ...
%
160 % Mechanics, Technical University of Denmark, DK−2800 Lyngby, Denmark. ...
%
161 % Please sent your comments to the author: sigmund@fam.dtu.dk ...
%
162 % ...
...
%
163 % The code is intended for educational purposes and theoretical details ...
%
164 % are discussed in the paper ...
%
165 % "A 99 line topology optimization code written in Matlab" ...
%
166 % by Ole Sigmund (2001), Structural and Multidisciplinary Optimization, ...
%
167 % Vol 21, pp. 120−−127. ...
%
168 % ...
...
%
169 % The code as well as a postscript version of the paper can be ...
%
170 % downloaded from the web−site: http://www.topopt.dtu.dk ...
%
171 % ...
...
%
172 % Disclaimer: ...
%
173 % The author reserves all rights but does not guaranty that the code is ...
%
174 % free from errors. Furthermore, he shall not be liable in any event ...
%
175 % caused by the use of the program. ...
%
176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Bridge.m

1 %%%% A 99 LINE TOPOLOGY OPTIMIZATION CODE BY OLE SIGMUND, JANUARY 2000 %%%
2 %%%% CODE MODIFIED FOR INCREASED SPEED, September 2002, BY OLE SIGMUND %%%
3 function bridge();
4
5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% My Code %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 nelx = 60; % number of elements in the horizontal directions.
7 nely = 20; % number of elements in the vertical directions.
8 volfrac = 0.5; % the volume fraction.
9 penal = 3.0; % the penalization power.
10 rmin = 1.1; % the filter size (divided by element size).
11
12 disp('How many lower layers do you want to have ')
13 prompt = '';
14 layer = input(prompt);
15
19

16
17 % INITIALIZE
18 x(1:nely,1:nelx) = volfrac;
19 active(1:nely,1:nelx) = 0;
20
21 for ely = 1:nely
22 for elx = 1:nelx
23 if any(ely == (nely−layer:nely))
24 active(ely,elx) = 1;
25 x(ely,elx) = 1;
26 else
27 active(ely,elx) = 0;
28 end
29 end
30 end
31 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% End %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32 loop = 0;
33 change = 1.;
34 % START ITERATION
35 while change > 0.01
36 loop = loop + 1;
37 xold = x;
38 % FE−ANALYSIS
39 [U]=FE(nelx,nely,x,penal);
40 % OBJECTIVE FUNCTION AND SENSITIVITY ANALYSIS
41 [KE] = lk;
42 c = 0.;
43 for ely = 1:nely
44 for elx = 1:nelx
45 n1 = (nely+1)*(elx−1)+ely;
46 n2 = (nely+1)* elx +ely;
47 Ue = U([2*n1−1;2*n1; 2*n2−1;2*n2; 2*n2+1;2*n2+2; 2*n1+1;2*n1+2],1);
48 c = c + x(ely,elx)^penal*Ue'*KE*Ue;
49 dc(ely,elx) = −penal*x(ely,elx)^(penal−1)*Ue'*KE*Ue;
50 end
51 end
52 % FILTERING OF SENSITIVITIES
53 [dc] = check(nelx,nely,rmin,x,dc);
54 % DESIGN UPDATE BY THE OPTIMALITY CRITERIA METHOD
55 [x] = OC(nelx,nely,x,volfrac,dc,active);
56 % PRINT RESULTS
57 change = max(max(abs(x−xold)));
58 disp([' It.: ' sprintf('%4i',loop) ' Obj.: ' sprintf('%10.4f',c) ...
59 ' Vol.: ' sprintf('%6.3f',sum(sum(x))/(nelx*nely)) ...
60 ' ch.: ' sprintf('%6.3f',change )])
61 % PLOT DENSITIES
62 colormap(gray); imagesc(−x); axis equal; axis tight; axis off;pause(1e−6);
63 end
64 %%%%%%%%%% OPTIMALITY CRITERIA UPDATE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 function [xnew]=OC(nelx,nely,x,volfrac,dc,passive)
66 l1 = 0; l2 = 100000; move = 0.2;
67 while (l2−l1 > 1e−4)
68 lmid = 0.5*(l2+l1);
69 xnew = max(0.001,max(x−move,min(1.,min(x+move,x.*sqrt(−dc./lmid))))); ...
%My line
70 xnew(find(passive)) = 1; %My line
71 if sum(sum(xnew)) − volfrac*nelx*nely > 0;
72 l1 = lmid;
73 else
20

74 l2 = lmid;
75 end
76 end
77 %%%%%%%%%% MESH−INDEPENDENCY FILTER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 function [dcn]=check(nelx,nely,rmin,x,dc)
79 dcn=zeros(nely,nelx);
80 for i = 1:nelx
81 for j = 1:nely
82 sum=0.0;
83 for k = max(i−floor(rmin),1):min(i+floor(rmin),nelx)
84 for l = max(j−floor(rmin),1):min(j+floor(rmin),nely)
85 fac = rmin−sqrt((i−k)^2+(j−l)^2);
86 sum = sum+max(0,fac);
87 dcn(j,i) = dcn(j,i) + max(0,fac)*x(l,k)*dc(l,k);
88 end
89 end
90 dcn(j,i) = dcn(j,i)/(x(j,i)*sum);
91 end
92 end
93 %%%%%%%%%% FE−ANALYSIS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94 function [U]=FE(nelx,nely,x,penal)
95 [KE] = lk;
96 K = sparse(2*(nelx+1)*(nely+1), 2*(nelx+1)*(nely+1));
97 F = sparse(2*(nely+1)*(nelx+1),1); U = zeros(2*(nely+1)*(nelx+1),1);
98 for elx = 1:nelx
99 for ely = 1:nely
100 n1 = (nely+1)*(elx−1)+ely;
101 n2 = (nely+1)* elx +ely;
102 edof = [2*n1−1; 2*n1; 2*n2−1; 2*n2; 2*n2+1; 2*n2+2; 2*n1+1; 2*n1+2];
103 K(edof,edof) = K(edof,edof) + x(ely,elx)^penal*KE;
104 end
105 end
106 % DEFINE LOADS AND SUPPORTS
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% My Code %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108
109 F(2*(nely+1)*ceil((nelx+1)/2),1) = −1 ;
110 fixeddofs = union([2*(nely+1),2*(nely+1)−1] ,[2*(nelx+1)*(nely+1)]);
111 alldofs = [1:2*(nely+1)*(nelx+1)];
112 freedofs = setdiff(alldofs,fixeddofs);
113
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% End %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 % SOLVING
116 U(freedofs,:) = K(freedofs,freedofs) \ F(freedofs,:);
117 U(fixeddofs,:)= 0;
118 %%%%%%%%%% ELEMENT STIFFNESS MATRIX %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 function [KE]=lk
120 E = 1.;
121 nu = 0.3;
122 k=[ 1/2−nu/6 1/8+nu/8 −1/4−nu/12 −1/8+3*nu/8 ...
123 −1/4+nu/12 −1/8−nu/8 nu/6 1/8−3*nu/8];
124 KE = E/(1−nu^2)*[ k(1) k(2) k(3) k(4) k(5) k(6) k(7) k(8)
125 k(2) k(1) k(8) k(7) k(6) k(5) k(4) k(3)
126 k(3) k(8) k(1) k(6) k(7) k(4) k(5) k(2)
127 k(4) k(7) k(6) k(1) k(8) k(3) k(2) k(5)
128 k(5) k(6) k(7) k(8) k(1) k(2) k(3) k(4)
129 k(6) k(5) k(4) k(3) k(2) k(1) k(8) k(7)
130 k(7) k(4) k(5) k(2) k(3) k(8) k(1) k(6)
131 k(8) k(3) k(2) k(5) k(4) k(7) k(6) k(1)];
132 %
21

133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134 % This Matlab code was written by Ole Sigmund, Department of Solid ...
%
135 % Mechanics, Technical University of Denmark, DK−2800 Lyngby, Denmark. ...
%
136 % Please sent your comments to the author: sigmund@fam.dtu.dk ...
%
137 % ...
...
%
138 % The code is intended for educational purposes and theoretical details ...
%
139 % are discussed in the paper ...
%
140 % "A 99 line topology optimization code written in Matlab" ...
%
141 % by Ole Sigmund (2001), Structural and Multidisciplinary Optimization, ...
%
142 % Vol 21, pp. 120−−127. ...
%
143 % ...
...
%
144 % The code as well as a postscript version of the paper can be ...
%
145 % downloaded from the web−site: http://www.topopt.dtu.dk ...
%
146 % ...
...
%
147 % Disclaimer: ...
%
148 % The author reserves all rights but does not guaranty that the code is ...
%
149 % free from errors. Furthermore, he shall not be liable in any event ...
%
150 % caused by the use of the program. ...
%
151 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Top_extended.m

1 %%%% A 99 LINE TOPOLOGY OPTIMIZATION CODE BY OLE SIGMUND, JANUARY 2000 %%%
2 %%%% CODE MODIFIED FOR INCREASED SPEED, September 2002, BY OLE SIGMUND %%%
3 function top_extended();
4
5
6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% My Code %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 nelx = 60; % number of elements in the horizontal directions.
8 nely = 20; % number of elements in the vertical directions.
9 volfrac = 0.5; % the volume fraction.
10 penal = 3.0; % the penalization power.
11 rmin = 1.1; % the filter size (divided by element size).
12
13 elevation = 0;
14 disp('What type of loads do you want')
15 disp(' 1 Central point load')
22

16 disp(' 2 Three central point load ')


17 disp(' 3 Distributed load')
18 prompt = '';
19 cases1 = input(prompt);
20
21 if cases1 == 3
22 disp(' Wich elevation of the distributed load do you want')
23 disp(' 1 Bottom')
24 disp(' 2 Mid')
25 disp(' 3 Top')
26 prompt = '';
27 elevation = input(prompt);
28 end
29
30
31
32 disp('Which support do you want to have for the first end (at the left)')
33 disp(' 1 Supported end')
34 disp(' 2 Clamped end ')
35 disp(' 3 Clamped side')
36 disp(' 4 Free ')
37 prompt = '';
38 cases2 = input(prompt);
39

40 disp('Which support do you want to have for the second end (at the right)')
41 disp(' 1 Supported end')
42 disp(' 2 Clamped end ')
43 disp(' 3 Clamped side')
44 disp(' 4 Free ')
45 prompt = '';
46 cases3 = input(prompt);
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% End %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48
49 % INITIALIZE
50 x(1:nely,1:nelx) = volfrac;
51 loop = 0;
52 change = 1.;
53 % START ITERATION
54 while change > 0.01
55 loop = loop + 1;
56 xold = x;
57 % FE−ANALYSIS
58 [U]=FE(nelx,nely,x,penal,cases1,cases2,cases3,elevation); %My line
59 % OBJECTIVE FUNCTION AND SENSITIVITY ANALYSIS
60 [KE] = lk;
61 c = 0.;
62 for ely = 1:nely
63 for elx = 1:nelx
64 n1 = (nely+1)*(elx−1)+ely;
65 n2 = (nely+1)* elx +ely;
66 Ue = U([2*n1−1;2*n1; 2*n2−1;2*n2; 2*n2+1;2*n2+2; 2*n1+1;2*n1+2],1);
67 c = c + x(ely,elx)^penal*Ue'*KE*Ue;
68 dc(ely,elx) = −penal*x(ely,elx)^(penal−1)*Ue'*KE*Ue;
69 end
70 end
71 % FILTERING OF SENSITIVITIES
72 [dc] = check(nelx,nely,rmin,x,dc);
73 % DESIGN UPDATE BY THE OPTIMALITY CRITERIA METHOD
74 [x] = OC(nelx,nely,x,volfrac,dc);
23

75 % PRINT RESULTS
76 change = max(max(abs(x−xold)));
77 disp([' It.: ' sprintf('%4i',loop) ' Obj.: ' sprintf('%10.4f',c) ...
78 ' Vol.: ' sprintf('%6.3f',sum(sum(x))/(nelx*nely)) ...
79 ' ch.: ' sprintf('%6.3f',change )])
80 % PLOT DENSITIES
81 colormap(gray); imagesc(−x); axis equal; axis tight; axis off;pause(1e−6);
82 end
83 %%%%%%%%%% OPTIMALITY CRITERIA UPDATE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 function [xnew]=OC(nelx,nely,x,volfrac,dc)
85 l1 = 0; l2 = 100000; move = 0.2;
86 while (l2−l1 > 1e−4)
87 lmid = 0.5*(l2+l1);
88 xnew = max(0.001,max(x−move,min(1.,min(x+move,x.*sqrt(−dc./lmid)))));
89 if sum(sum(xnew)) − volfrac*nelx*nely > 0;
90 l1 = lmid;
91 else
92 l2 = lmid;
93 end
94 end
95 %%%%%%%%%% MESH−INDEPENDENCY FILTER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 function [dcn]=check(nelx,nely,rmin,x,dc)
97 dcn=zeros(nely,nelx);
98 for i = 1:nelx
99 for j = 1:nely
100 sum=0.0;
101 for k = max(i−floor(rmin),1):min(i+floor(rmin),nelx)
102 for l = max(j−floor(rmin),1):min(j+floor(rmin),nely)
103 fac = rmin−sqrt((i−k)^2+(j−l)^2);
104 sum = sum+max(0,fac);
105 dcn(j,i) = dcn(j,i) + max(0,fac)*x(l,k)*dc(l,k);
106 end
107 end
108 dcn(j,i) = dcn(j,i)/(x(j,i)*sum);
109 end
110 end
111 %%%%%%%%%% FE−ANALYSIS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 function [U]=FE(nelx,nely,x,penal,cases1,cases2,cases3,elevation) %My line
113 [KE] = lk;
114 K = sparse(2*(nelx+1)*(nely+1), 2*(nelx+1)*(nely+1));
115 F = sparse(2*(nely+1)*(nelx+1),1); U = zeros(2*(nely+1)*(nelx+1),1);
116 for elx = 1:nelx
117 for ely = 1:nely
118 n1 = (nely+1)*(elx−1)+ely;
119 n2 = (nely+1)* elx +ely;
120 edof = [2*n1−1; 2*n1; 2*n2−1; 2*n2; 2*n2+1; 2*n2+2; 2*n1+1; 2*n1+2];
121 K(edof,edof) = K(edof,edof) + x(ely,elx)^penal*KE;
122 end
123 end
124 %DEFINE LOADS AND SUPPORTS
125
126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% My Code %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127 F = sparse(2*(nely+1)*(nelx+1),1);
128

129
130 switch cases1
131 %Poind load.
132 case 1
133 F(2*(nely+1)*ceil((nelx+1)/2),1) = −1 ;
24

134 %Three point load


135 case 2
136 F(2*(nely+1)*ceil((nelx+1)/4),1) = −1/3 ;
137 F(2*(nely+1)*ceil((nelx+1)/2),1) = −1/3 ;
138 F(2*(nely+1)*ceil((nelx+1)*3/4),1) = −1/3 ;
139 %Distributed load
140 case 3
141
142 %Bottom distributed load
143 if elevation == 1
144
145 F(2*(nely+1),1) = −1/(nelx+1);
146 for i = 1:nelx
147 F(2*(nely+1)*ceil(i*(nelx+1)/nelx),1) = −1/(nelx+1);
148 end
149 %Mid elevation distributed load
150 elseif elevation == 2
151 F(2*ceil(nely/2+1),1) = −1/(nelx+1);
152 for i = 1:nelx
153 F(2*(nely+1)*ceil(i*(nelx+1)/nelx)−nely) = −1/(nelx+1);
154 end
155 %Top elevation distributed load
156 else
157

158 F(2,1) = −1/(nelx+1);


159 for i = 1:nelx
160 F(2*(nely+1)*ceil(i*(nelx+1)/nelx)−2*nely) = ...
−1/(nelx+1);
161 end
162 end %end if
163
164 end
165
166
167 switch cases2
168 % Supported left end
169 case 1
170 fixeddofs1 = [2*(nely+1)];
171 % Clamped left end
172 case 2
173 fixeddofs1 = [2*(nely+1),2*(nely+1)−1];
174 % Clamped left side
175 case 3
176 fixeddofs1 = [1:2*(nely+1)];
177 % Free left side
178 case 4
179 fixeddofs1 =[];
180

181 end
182
183
184 switch cases3
185 % Supported right end
186 case 1
187 fixeddofs2 = [2*(nelx+1)*(nely+1)];
188 % Clamped right end
189 case 2
190 fixeddofs2 = [2*(nelx+1)*(nely+1), 2*(nelx+1)*(nely+1)−1];
191 % Clamped right side
25

192 case 3
193 fixeddofs2 = [(2*(nelx)*(nely+1)+1):2*(nelx+1)*(nely+1)];
194 % Free right side
195 case 4
196 fixeddofs2 =[];
197

198 end
199
200 fixeddofs = union([fixeddofs1],[fixeddofs2]);
201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% End %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202
203 alldofs = [1:2*(nely+1)*(nelx+1)];
204 freedofs = setdiff(alldofs,fixeddofs);
205 % SOLVING
206 U(freedofs,:) = K(freedofs,freedofs) \ F(freedofs,:);
207 U(fixeddofs,:)= 0;
208 %%%%%%%%%% ELEMENT STIFFNESS MATRIX %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 function [KE]=lk
210 E = 1.;
211 nu = 0.3;
212 k=[ 1/2−nu/6 1/8+nu/8 −1/4−nu/12 −1/8+3*nu/8 ...
213 −1/4+nu/12 −1/8−nu/8 nu/6 1/8−3*nu/8];
214 KE = E/(1−nu^2)*[ k(1) k(2) k(3) k(4) k(5) k(6) k(7) k(8)
215 k(2) k(1) k(8) k(7) k(6) k(5) k(4) k(3)
216 k(3) k(8) k(1) k(6) k(7) k(4) k(5) k(2)
217 k(4) k(7) k(6) k(1) k(8) k(3) k(2) k(5)
218 k(5) k(6) k(7) k(8) k(1) k(2) k(3) k(4)
219 k(6) k(5) k(4) k(3) k(2) k(1) k(8) k(7)
220 k(7) k(4) k(5) k(2) k(3) k(8) k(1) k(6)
221 k(8) k(3) k(2) k(5) k(4) k(7) k(6) k(1)];
222 %
223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224 % This Matlab code was written by Ole Sigmund, Department of Solid ...
%
225 % Mechanics, Technical University of Denmark, DK−2800 Lyngby, Denmark. ...
%
226 % Please sent your comments to the author: sigmund@fam.dtu.dk ...
%
227 % ...
...
%
228 % The code is intended for educational purposes and theoretical details ...
%
229 % are discussed in the paper ...
%
230 % "A 99 line topology optimization code written in Matlab" ...
%
231 % by Ole Sigmund (2001), Structural and Multidisciplinary Optimization, ...
%
232 % Vol 21, pp. 120−−127. ...
%
233 % ...
...
%
234 % The code as well as a postscript version of the paper can be ...
%
235 % downloaded from the web−site: http://www.topopt.dtu.dk ...
%
26

236 % ...
...
%
237 % Disclaimer: ...
%
238 % The author reserves all rights but does not guaranty that the code is ...
%
239 % free from errors. Furthermore, he shall not be liable in any event ...
%
240 % caused by the use of the program. ...
%
241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Piece.m

1 %%%% A 99 LINE TOPOLOGY OPTIMIZATION CODE BY OLE SIGMUND, JANUARY 2000 %%%
2 %%%% CODE MODIFIED FOR INCREASED SPEED, September 2002, BY OLE SIGMUND %%%
3 function piece();
4
5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% My Code %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 nelx = 90; % number of elements in the horizontal directions.
7 nely = 60; % number of elements in the vertical directions.
8 volfrac = 0.2; % the volume fraction.
9 penal = 3.0; % the penalization power.
10 rmin = 1.1; % the filter size (divided by element size).
11
12

13 % INITIALIZE
14 x(1:nely,1:nelx) = volfrac;
15 passive(1:nely,1:nelx) = 0;
16 active(1:nely,1:nelx) = 0;
17
18 %Position, radius, relative thickness of the first hole
19 pos1_x = nelx/6 ;
20 pos1_y = nely/2;
21 r1 = nely/14;
22 t1 = 1.5;
23
24 %Position, radius, relative thickness of the second hole
25 pos2_x = nely/4.;
26 pos2_y = nelx/1.3;
27 r2 = nely/14;
28 t2 = 1.5;
29
30 %Position, radius, relative thickness of the third hole
31 pos3_x = nelx/1.3;
32 pos3_y = nely/1.2;
33 r3 = nely/14;
34 t3 = 1.5;
35
36 %First Hole
37 for ely = 1:nely
38 for elx = 1:nelx
39 if sqrt((ely−pos1_y)^2+(elx−pos1_x)^2) < r1*t1
40 active(ely,elx) = 1;
41 x(ely,elx) = 1;
42 end
27

43 end
44 end
45
46 for ely = 1:nely
47 for elx = 1:nelx
48 if sqrt((ely−pos1_y)^2+(elx−pos1_x)^2) < r1
49 passive(ely,elx) = 1;
50 x(ely,elx) = 0.001;
51 end
52 end
53 end
54

55
56 %Second hole
57 for ely = 1:nely
58 for elx = 1:nelx
59 if sqrt((ely−pos2_x)^2+(elx−pos2_y)^2) < r2*t2
60 active(ely,elx) = 1;
61 x(ely,elx) = 1;
62 end
63 end
64 end
65
66 for ely = 1:nely
67 for elx = 1:nelx
68 if sqrt((ely−pos2_x)^2+(elx−pos2_y)^2) < r2
69 passive(ely,elx) = 1;
70 x(ely,elx) = 0.001;
71 end
72 end
73 end
74
75
76 %Third hole
77 for ely = 1:nely
78 for elx = 1:nelx
79 if sqrt((ely−pos3_y)^2+(elx−pos3_x)^2) < r3*t3
80 active(ely,elx) = 1;
81 x(ely,elx) = 1;
82 end
83 end
84 end
85
86 for ely = 1:nely
87 for elx = 1:nelx
88 if sqrt((ely−pos3_y)^2+(elx−pos3_x)^2) < r3
89 passive(ely,elx) = 1;
90 x(ely,elx) = 0.001;
91 end
92 end
93 end
94
95
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% End %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97
98
99 loop = 0;
100 change = 1.;
101 % START ITERATION
28

102 while change > 0.01


103 loop = loop + 1;
104 xold = x;
105 % FE−ANALYSIS
106 [U]=FE(nelx,nely,x,penal);
107 % OBJECTIVE FUNCTION AND SENSITIVITY ANALYSIS
108 [KE] = lk;
109 c = 0.;
110 for ely = 1:nely
111 for elx = 1:nelx
112 n1 = (nely+1)*(elx−1)+ely;
113 n2 = (nely+1)* elx +ely;
114 Ue = U([2*n1−1;2*n1; 2*n2−1;2*n2; 2*n2+1;2*n2+2; 2*n1+1;2*n1+2],1);
115 c = c + x(ely,elx)^penal*Ue'*KE*Ue;
116 dc(ely,elx) = −penal*x(ely,elx)^(penal−1)*Ue'*KE*Ue;
117 end
118 end
119 % FILTERING OF SENSITIVITIES
120 [dc] = check(nelx,nely,rmin,x,dc);
121 % DESIGN UPDATE BY THE OPTIMALITY CRITERIA METHOD
122 [x] = OC(nelx,nely,x,volfrac,dc,passive,active);
123 % PRINT RESULTS
124 change = max(max(abs(x−xold)));
125 disp([' It.: ' sprintf('%4i',loop) ' Obj.: ' sprintf('%10.4f',c) ...
126 ' Vol.: ' sprintf('%6.3f',sum(sum(x))/(nelx*nely)) ...
127 ' ch.: ' sprintf('%6.3f',change )])
128 % PLOT DENSITIES
129 colormap(gray); imagesc(−x); axis equal; axis tight; axis off;pause(1e−6);
130 end
131 %%%%%%%%%% OPTIMALITY CRITERIA UPDATE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132 function [xnew]=OC(nelx,nely,x,volfrac,dc,passive,active) %My line
133 l1 = 0; l2 = 100000; move = 0.2;
134 while (l2−l1 > 1e−4)
135 lmid = 0.5*(l2+l1);
136 xnew = max(0.001,max(x−move,min(1.,min(x+move,x.*sqrt(−dc./lmid)))));
137 xnew(find(active)) = 1; %My line
138 xnew(find(passive)) = 0.001; %My line
139 if sum(sum(xnew)) − volfrac*nelx*nely > 0;
140 l1 = lmid;
141 else
142 l2 = lmid;
143 end
144 end
145 %%%%%%%%%% MESH−INDEPENDENCY FILTER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146 function [dcn]=check(nelx,nely,rmin,x,dc)
147 dcn=zeros(nely,nelx);
148 for i = 1:nelx
149 for j = 1:nely
150 sum=0.0;
151 for k = max(i−floor(rmin),1):min(i+floor(rmin),nelx)
152 for l = max(j−floor(rmin),1):min(j+floor(rmin),nely)
153 fac = rmin−sqrt((i−k)^2+(j−l)^2);
154 sum = sum+max(0,fac);
155 dcn(j,i) = dcn(j,i) + max(0,fac)*x(l,k)*dc(l,k);
156 end
157 end
158 dcn(j,i) = dcn(j,i)/(x(j,i)*sum);
159 end
160 end
29

161 %%%%%%%%%% FE−ANALYSIS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


162 function [U]=FE(nelx,nely,x,penal)
163 [KE] = lk;
164 K = sparse(2*(nelx+1)*(nely+1), 2*(nelx+1)*(nely+1));
165 F = sparse(2*(nely+1)*(nelx+1),1); U = zeros(2*(nely+1)*(nelx+1),1);
166 for elx = 1:nelx
167 for ely = 1:nely
168 n1 = (nely+1)*(elx−1)+ely;
169 n2 = (nely+1)* elx +ely;
170 edof = [2*n1−1; 2*n1; 2*n2−1; 2*n2; 2*n2+1; 2*n2+2; 2*n1+1; 2*n1+2];
171 K(edof,edof) = K(edof,edof) + x(ely,elx)^penal*KE;
172 end
173 end
174 %DEFINE LOADS AND SUPPORTS
175
176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% My Code %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177
178 for ely = 1:nely
179 for elx = 1:nelx
180 if ely == nely/2 && elx == nelx/6
181 F(2*((elx−1)*(nely+1)+ely)) = 1;
182 end
183 end
184 end
185
186
187 fixeddofs1(1:nely,1:nelx) = 0;
188
189 fixeddofs2(1:nely,1:nelx) = 0;
190 fixeddofs3(1:nely,1:nelx) = 0;
191 fixeddofs4(1:nely,1:nelx) = 0;
192 for ely = 1:nely
193 for elx = 1:nelx
194 if sqrt((ely−nely/4.)^2+(elx−nelx/1.3)^2) > nely/14. && ...
sqrt((ely−nely/4.)^2+(elx−nelx/1.3)^2) < nely/10.
195 fixeddofs1(ely,elx) = 2*((elx−1)*(nely+1)+ely);
196 fixeddofs2(ely,elx) = 2*((elx−1)*(nely+1)+ely)+1;
197 end
198 end
199 end
200
201 for ely = 1:nely
202 for elx = 1:nelx
203 if sqrt((ely−nely/1.2)^2+(elx−nelx/1.3)^2) > nely/14. && ...
sqrt((ely−nely/1.2)^2+(elx−nelx/1.3)^2) < nely/10.
204 fixeddofs3(ely,elx) = 2*((elx−1)*(nely+1)+ely);
205 fixeddofs4(ely,elx) = 2*((elx−1)*(nely+1)+ely)+1;
206 end
207 end
208 end
209
210
211
212 fixeddofs1 = union([fixeddofs1],[fixeddofs2])';
213
214 fixeddofs2 = union([fixeddofs3],[fixeddofs4])';
215 fixeddofs = union([fixeddofs1],[fixeddofs2])';
216
217 fixeddofs = fixeddofs(fixeddofs~=0);
30

218
219
220
221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% End %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222
223 alldofs = [1:2*(nely+1)*(nelx+1)];
224 freedofs = setdiff(alldofs,fixeddofs);
225 % SOLVING
226 U(freedofs,:) = K(freedofs,freedofs) \ F(freedofs,:);
227 U(fixeddofs,:)= 0;
228 %%%%%%%%%% ELEMENT STIFFNESS MATRIX %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
229 function [KE]=lk
230 E = 1.;
231 nu = 0.3;
232 k=[ 1/2−nu/6 1/8+nu/8 −1/4−nu/12 −1/8+3*nu/8 ...
233 −1/4+nu/12 −1/8−nu/8 nu/6 1/8−3*nu/8];
234 KE = E/(1−nu^2)*[ k(1) k(2) k(3) k(4) k(5) k(6) k(7) k(8)
235 k(2) k(1) k(8) k(7) k(6) k(5) k(4) k(3)
236 k(3) k(8) k(1) k(6) k(7) k(4) k(5) k(2)
237 k(4) k(7) k(6) k(1) k(8) k(3) k(2) k(5)
238 k(5) k(6) k(7) k(8) k(1) k(2) k(3) k(4)
239 k(6) k(5) k(4) k(3) k(2) k(1) k(8) k(7)
240 k(7) k(4) k(5) k(2) k(3) k(8) k(1) k(6)
241 k(8) k(3) k(2) k(5) k(4) k(7) k(6) k(1)];
242 %
243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 % This Matlab code was written by Ole Sigmund, Department of Solid ...
%
245 % Mechanics, Technical University of Denmark, DK−2800 Lyngby, Denmark. ...
%
246 % Please sent your comments to the author: sigmund@fam.dtu.dk ...
%
247 % ...
...
%
248 % The code is intended for educational purposes and theoretical details ...
%
249 % are discussed in the paper ...
%
250 % "A 99 line topology optimization code written in Matlab" ...
%
251 % by Ole Sigmund (2001), Structural and Multidisciplinary Optimization, ...
%
252 % Vol 21, pp. 120−−127. ...
%
253 % ...
...
%
254 % The code as well as a postscript version of the paper can be ...
%
255 % downloaded from the web−site: http://www.topopt.dtu.dk ...
%
256 % ...
...
%
257 % Disclaimer: ...
%
258 % The author reserves all rights but does not guaranty that the code is ...
%
31

259 % free from errors. Furthermore, he shall not be liable in any event ...
%
260 % caused by the use of the program. ...
%
261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

You might also like