Professional Documents
Culture Documents
MECA0027-1
S TRUCTURAL AND M ULTIDISCIPLINARY O PTIMIZATION
L IÈGE
December 2020
CONTENTS 0
Contents
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).
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
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.
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
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.
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
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
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.
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".
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
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 :
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.
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
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
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
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
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
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
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
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
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
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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%