Newer
Older
Amira Abdel-Rahman
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# Amira Abdel-Rahman
# (c) Massachusetts Institute of Technology 2020
# Topology Optimization Implementation in Julia from various sources
# based on https://paulino.ce.gatech.edu/conferences/papers/11pereira_anefficientandcompact.pdf and http://www.topopt.mek.dtu.dk/apps-and-software and https://github.com/blademwang11/Topopt/blob/master/top.jl
function topologyOptimization(nelx,nely,volfrac,rmin,penal)
anim=Animation()
display("Minimum compliance problem with OC")
display("ndes: $nelx x $nely")
display("volfrac: $volfrac rmin: $rmin penal: $penal")
# Max and min stiffness
Emin=1e-9
Emax=1.0
# dofs:
ndof = 2*(nelx+1)*(nely+1)
# Allocate design variables (as array), initialize and allocate sens.
x=volfrac * ones(Float64,nely,nelx)
xold=copy(x)
xPhys=copy(x)
g=0 # must be initialized to use the NGuyen/Paulino OC approach
dc=zeros(Float64,(nely,nelx))
# FE: Build the index vectors for the for coo matrix format.
KE=lk()
nodenrs = reshape(1:(1+nelx)*(1+nely),1+nely,1+nelx)
edofVec = reshape(2*nodenrs[1:end-1,1:end-1].+1,nelx*nely,1)
edofMat = repeat(edofVec,1,8).+repeat([0 1 2*nely.+[2 3 0 1] -2 -1],nelx*nely,1)
iK = convert(Array{Int},reshape(kron(edofMat,ones(8,1))',64*nelx*nely,1))
jK = convert(Array{Int},reshape(kron(edofMat,ones(1,8))',64*nelx*nely,1))
# DEFINE LOADS AND SUPPORTS (HALF MBB-BEAM)
F = sparse([2],[1],[-1.0],2*(nely+1)*(nelx+1),1)
U = zeros(2*(nely+1)*(nelx+1),1)
fixeddofs = union(1:2:2*(nely+1),2*(nelx+1)*(nely+1))
alldofs = 1:(2*(nely+1)*(nelx+1))
freedofs = setdiff(alldofs,fixeddofs)
# Prepare filter
iH = ones(convert(Int,nelx*nely*(2*(ceil(rmin)-1)+1)^2),1)
jH = ones(Int,size(iH))
sH = zeros(size(iH))
k = 0;
for i1 = 1:nelx
for j1 = 1:nely
e1 = (i1-1)*nely+j1
for i2 = max(i1-(ceil(rmin)-1),1):min(i1+(ceil(rmin)-1),nelx)
for j2 = max(j1-(ceil(rmin)-1),1):min(j1+(ceil(rmin)-1),nely)
e2 = (i2-1)*nely+j2
k = k+1
iH[k] = e1
jH[k] = e2
sH[k] = max(0,rmin-sqrt((i1-i2)^2+(j1-j2)^2))
end
end
end
end
H = sparse(vec(iH),vec(jH),vec(sH))
Hs = sum(H,dims=2)
###################################################
loop = 0
change = 1
maxIter=1000
# Start iteration
for i =1:maxIter
if (change > 0.01)
# Start iteration
loop += 1
# FE-ANALYSIS
sK = reshape(KE[:]*(Emin.+xPhys[:]'.^penal*(Emax-Emin)),64*nelx*nely,1)
K = sparse(vec(iK),vec(jK),vec(sK)); K = (K+K')/2
@timed U[freedofs] = K[freedofs,freedofs] \ Array(F[freedofs])
# Objective function and sensitivity analysis
ce = reshape(sum((U[edofMat]*KE).*U[edofMat],dims=2),nely,nelx)
c = sum(sum((Emin.+xPhys.^penal*(Emax-Emin)).*ce))
dc = -penal*(Emax-Emin)*xPhys.^(penal-1).*ce
dv = ones(nely,nelx)
dc[:] = H*(dc[:]./Hs)
dv[:] = H*(dv[:]./Hs)
# OPTIMALITY CRITERIA UPDATE OF DESIGN VARIABLES AND PHYSICAL DENSITIES
l1 = 0; l2 = 1e9; move = 0.2; xnew = 0
while (l2-l1)/(l1+l2) > 1e-3
lmid = 0.5*(l2+l1)
xnew = max.(0,max.(x.-move,min.(1,min.(x.+move,x.*sqrt.((0.0.-dc)./dv./lmid)))))
xPhys[:] = (H*xnew[:])./Hs
if sum(xPhys[:]) > volfrac*nelx*nely
l1 = lmid
else
l2 = lmid
end
end
change = maximum(abs.(xnew[:].-x[:]))
x = xnew
m=mean(xPhys[:])
display(" It:$loop Obj:$c Vol:$m ch:$change ")
if loop<20||mod(loop,10)==0
xxx=1 .- clamp.(xPhys,0,1)
display(heatmap(xxx,xaxis=nothing,yaxis=nothing,legend=nothing,fc=:grays,clims=(0.0, 1.0),aspect_ratio=:equal))
heatmap(xxx,xaxis=nothing,yaxis=nothing,legend=nothing,fc=:grays,clims=(0.0, 1.0),aspect_ratio=:equal)
frame(anim)
end
xPhys = copy(x)
end
end
return xPhys,anim
end
function topologyOptimizationMMA(nelx,nely,volfrac,rmin,penal,maxEval)
display("Minimum compliance problem with MMA")
display("ndes: $nelx x $nely")
display("volfrac: $volfrac rmin: $rmin penal: $penal")
# Max and min stiffness
Emin=1e-9
Emax=1.0
# dofs:
ndof = 2*(nelx+1)*(nely+1)
# Allocate design variables (as array), initialize and allocate sens.
x=volfrac * ones(Float64,nely,nelx)
xold=copy(x)
xPhys=copy(x)
g=0 # must be initialized to use the NGuyen/Paulino OC approach
dc=zeros(Float64,(nely,nelx))
# FE: Build the index vectors for the for coo matrix format.
KE=lk()
nodenrs = reshape(1:(1+nelx)*(1+nely),1+nely,1+nelx)
edofVec = reshape(2*nodenrs[1:end-1,1:end-1].+1,nelx*nely,1)
edofMat = repeat(edofVec,1,8).+repeat([0 1 2*nely.+[2 3 0 1] -2 -1],nelx*nely,1)
iK = convert(Array{Int},reshape(kron(edofMat,ones(8,1))',64*nelx*nely,1))
jK = convert(Array{Int},reshape(kron(edofMat,ones(1,8))',64*nelx*nely,1))
# DEFINE LOADS AND SUPPORTS (HALF MBB-BEAM)
F = sparse([2],[1],[-1.0],2*(nely+1)*(nelx+1),1)
U = zeros(2*(nely+1)*(nelx+1),1)
fixeddofs = union(1:2:2*(nely+1),2*(nelx+1)*(nely+1))
alldofs = 1:(2*(nely+1)*(nelx+1))
freedofs = setdiff(alldofs,fixeddofs)
# Prepare filter
iH = ones(convert(Int,nelx*nely*(2*(ceil(rmin)-1)+1)^2),1)
jH = ones(Int,size(iH))
sH = zeros(size(iH))
k = 0;
for i1 = 1:nelx
for j1 = 1:nely
e1 = (i1-1)*nely+j1
for i2 = max(i1-(ceil(rmin)-1),1):min(i1+(ceil(rmin)-1),nelx)
for j2 = max(j1-(ceil(rmin)-1),1):min(j1+(ceil(rmin)-1),nely)
e2 = (i2-1)*nely+j2
k = k+1
iH[k] = e1
jH[k] = e2
sH[k] = max(0,rmin-sqrt((i1-i2)^2+(j1-j2)^2))
end
end
end
end
H = sparse(vec(iH),vec(jH),vec(sH))
Hs = sum(H,dims=2);
nel=nely*nelx
function FA(x::Vector, grad::Vector)
xPhys=reshape(x,nely,nelx)
sK = reshape(KE[:]*(Emin.+xPhys[:]'.^penal*(Emax-Emin)),64*nelx*nely,1)
K = sparse(vec(iK),vec(jK),vec(sK)); K = (K+K')/2
@timed U[freedofs] = K[freedofs,freedofs] \ Array(F[freedofs])
# Objective function and sensitivity analysis
ce = reshape(sum((U[edofMat]*KE).*U[edofMat],dims=2),nely,nelx)
c = sum(sum((Emin.+xPhys.^penal*(Emax-Emin)).*ce))
dc = -penal*(Emax-Emin)*xPhys.^(penal-1).*ce
dc[:] = H*(dc[:]./Hs)
grad[:] .= dc[:]
return c
end
function G(x::Vector, grad::Vector)
dv = ones(nely,nelx)
dv[:] = H*(dv[:]./Hs)
grad[:] .= dv[:]
return (sum(x) - volfrac*nel)
end
FA(ones(nel)*volfrac, fill(volfrac,nel))
G(ones(nel)*volfrac, fill(volfrac,nel))
opt = Opt(:LD_MMA, nel)
opt.lower_bounds = fill(1e-9,nel)
opt.upper_bounds = fill(1,nel)
opt.xtol_rel = 1e-4
opt.maxeval = maxEval
opt.min_objective = FA
inequality_constraint!(opt, (x,gg) -> G(x,gg), 1e-4)
display(@time (minf,minx,ret) = optimize(opt, ones(nel).*volfrac))
numevals = opt.numevals # the number of function evaluations
display("got $minf after $numevals iterations (returned $ret)")
xPhys=reshape(minx,nely,nelx)
display(heatmap(xPhys, aspect_ratio=:equal, legend=false, axis=nothing, foreground_color_subplot=colorant"white",fc=:grays))
return xPhys
end
#########################################################################################################
function CompliantTopologyOptimization(nelx,nely,volfrac,rmin,penal,maxIter,Load,Support,Spring,DOut)
anim=Animation()
display("Minimum compliance problem with OC")
display("ndes: $nelx x $nely")
display("volfrac: $volfrac rmin: $rmin penal: $penal")
# Max and min stiffness
Emin=1e-9
Emax=1.0
change = 1
# dofs:
ndof = 2*(nelx+1)*(nely+1)
# Allocate design variables (as array), initialize and allocate sens.
x=volfrac * ones(Float64,nely,nelx)
xold=copy(x)
xPhys=copy(x)
g=0 # must be initialized to use the NGuyen/Paulino OC approach
dc=zeros(Float64,(nely,nelx))
# FE: Build the index vectors for the for coo matrix format.
KE=lk()
nodenrs = reshape(1:(1+nelx)*(1+nely),1+nely,1+nelx)
edofVec = reshape(2*nodenrs[1:end-1,1:end-1].+1,nelx*nely,1)
edofMat = repeat(edofVec,1,8).+repeat([0 1 2*nely.+[2 3 0 1] -2 -1],nelx*nely,1)
iK = convert(Array{Int},reshape(kron(edofMat,ones(8,1))',64*nelx*nely,1))
jK = convert(Array{Int},reshape(kron(edofMat,ones(1,8))',64*nelx*nely,1))
# DEFINE LOADS AND SUPPORTS
F = sparse(2 .*Load[:,1] .-2 .+ Load[:,2],ones(size(Load,1)),Load[:,3],2*(nely+1)*(nelx+1),2)
DofDOut = 2 * DOut[1] - 2 +DOut[2]; #only one
F=Array(F)
F[DofDOut,2]=-1
fixeddofs = 2 .*Support[:,1] .-2 .+ Support[:,2]
NSpring = size(Spring,1);
s = sparse(2 .*Spring[:,1] .-2 .+ Spring[:,2],ones(size(Spring,1)),Spring[:,3],2*(nely+1)*(nelx+1),2)
m=Array(s)[:,1]
S= sparse(diagm(m))
U = zeros(2*(nely+1)*(nelx+1),2)
alldofs = 1:(2*(nely+1)*(nelx+1))
freedofs = setdiff(alldofs,fixeddofs)
# Prepare filter
iH = ones(convert(Int,nelx*nely*(2*(ceil(rmin)-1)+1)^2),1)
jH = ones(Int,size(iH))
sH = zeros(size(iH))
k = 0;
for i1 = 1:nelx
for j1 = 1:nely
e1 = (i1-1)*nely+j1
for i2 = max(i1-(ceil(rmin)-1),1):min(i1+(ceil(rmin)-1),nelx)
for j2 = max(j1-(ceil(rmin)-1),1):min(j1+(ceil(rmin)-1),nely)
e2 = (i2-1)*nely+j2
k = k+1
iH[k] = e1
jH[k] = e2
sH[k] = max(0,rmin-sqrt((i1-i2)^2+(j1-j2)^2))
end
end
end
end
H = sparse(vec(iH),vec(jH),vec(sH))
Hs = sum(H,dims=2)
change= 1
loop = 0
changeStable=0
# Start iteration
for penal in 1.0:0.5:4
display(" Penalty: $penal")
loop = 0
for i =1:maxIter
if (change < 0.01)
changeStable+=1
display(" Change Stable for $changeStable iterations")
else
changeStable=0
end
if (changeStable<10)
# Start iteration
loop += 1
# FE-ANALYSIS
sK = reshape(KE[:]*(Emin.+xPhys[:]'.^penal*(Emax-Emin)),64*nelx*nely,1)
K = sparse(vec(iK),vec(jK),vec(sK))+S;K = (K+K')/2
@timed U[freedofs,:] = K[freedofs,freedofs] \ Array(F[freedofs,:])
U1=U[:,1]
U2=U[:,2]
# Objective function and sensitivity analysis
#ce = reshape(sum((U1[edofMat]*KE).*U2[edofMat],dims=2),nely,nelx)
ce=reshape(-sum((U1[edofMat]*KE).*U2[edofMat],dims=2),nely,nelx)
#c = sum((Emin.+xPhys[:].^penal*(Emax-Emin)).*ce)
c=U[DofDOut,1]
dc = -penal.*(Emax-Emin).*xPhys.^(penal-1).*ce
dv = ones(nely,nelx)
dc[:] = H*(dc[:]./Hs)
dv[:] = H*(dv[:]./Hs)
# OPTIMALITY CRITERIA UPDATE OF DESIGN VARIABLES AND PHYSICAL DENSITIES
l1 = 0; l2 = 1e9; move = 0.05; xnew = 0 #move=0.2
while (l2-l1)/(l2+l1) > 1e-4 && l2>1e-40
#while (l2-l1)/(l1+l2) > 1e-3
lmid = 0.5*(l2+l1)
xnew = max.(0,max.(x.-move,min.(1,min.(x.+move,x.*sqrt.(max.(1e-10,-dc./dv./lmid))))))
#xnew = max.(0,max.(x.-move,min.(1,min.(x.+move,x.*sqrt.(-dc./dv./lmid)))))
xPhys[:] = (H*xnew[:])./Hs
if sum(xPhys[:]) > volfrac*nelx*nely
l1 = lmid
else
l2 = lmid
end
end
change = maximum(abs.(xnew[:].-x[:]))
x = xnew
# print result
m=mean(xPhys[:])
display(" It:$loop Obj:$c Vol:$m ch:$change ")
if loop<20||mod(loop,5)==0
xxx=vcat(xPhys[end:-1:1,:],xPhys)
xxx=1 .- xxx
display(heatmap(xxx,xaxis=nothing,yaxis=nothing,legend=nothing,fc=:grays,clims=(0.0, 1.0)))
heatmap(xxx,xaxis=nothing,yaxis=nothing,legend=nothing,fc=:grays,clims=(0.0, 1.0))
frame(anim)
end
xPhys = copy(x)
end
end
end
# heatmap(xPhys, legend=false, axis=nothing, foreground_color_subplot=colorant"white",fc=:grays,clims=(0.0, 1.0))
# heatmap(xPhys, legend=false,fc=:grays,clims=(0.0, 1.0))
return xPhys,anim
end