Newer
Older
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
# Amira Abdel-Rahman
# (c) Massachusetts Institute of Technology 2020
function getDataFromSetup3D(setup,scale)
# -----------------------------------------------------------------------
# A(nel,1) = cross-sectional area of elements
# E(nel,1) = Young's modulus of elements
# f(ndf,nnp) = prescribed nodal forces
# g(ndf,nnp) = prescribed nodal displacements
# idb(ndf,nnp) = 1 if the degree of freedom is prescribed, 0 otherwise
# ien(nen,nel) = element connectivities
# ndf = number of degrees of freedom per node
# nel = number of elements
# nen = number of element equations
# nnp = number of nodes
# nsd = number of spacial dimensions
# xn(nsd,nnp) = nodal coordinates
# =======================================================================
nodes=setup["nodes"]
edges=setup["edges"]
# ---- MESH -------------------------------------------------------------
nsd = 3; # number of spacial dimensions
ndf = 6; # number of degrees of freedom per node
nen = 2; # number of element nodes
nel = length(edges); # number of elements
nnp = length(nodes); # number of nodal points
# ---- NODAL COORDINATES ------------------------------------------------
# xn(i,N) = coordinate i for node N
# N = 1,...,nnp
# i = 1,...,nsd
# -----------------------------------------------------------------------
xn = zeros(nsd,nnp);
for i in 1:nnp
xn[1:nsd, i] = [(nodes[i]["position"]["x"]/scale) (nodes[i]["position"]["y"]/scale) (nodes[i]["position"]["z"]/scale)]';
end
# ---- NODAL COORDINATES ------------------------------------------------
# ien(a,e) = N
# N: global node number - N = 1,...,nnp
# e: element number - e = 1,...,nel
# a: local node number - a = 1,...,nen
# -----------------------------------------------------------------------
ien = zeros(nen,nel);
for i in 1:nel
ien[1:2,i] = [(edges[i]["source"]+1) (edges[i]["target"]+1)]' ;
end
len=zeros(nel);
for i in 1:nel
x1=(nodes[(edges[i]["source"]+1)]["position"]["x"]/scale)
x2=(nodes[(edges[i]["target"]+1)]["position"]["x"]/scale)
y1=(nodes[(edges[i]["source"]+1)]["position"]["y"]/scale)
y2=(nodes[(edges[i]["target"]+1)]["position"]["y"]/scale)
z1=(nodes[(edges[i]["source"]+1)]["position"]["z"]/scale)
z2=(nodes[(edges[i]["target"]+1)]["position"]["z"]/scale)
len[i] = sqrt((x1-x2)^2+(y1-y2)^2+(z1-z2)^2);
end
# ---- MATERIAL PROPERTIES ----------------------------------------------
# E(e) = E_mat
# e: element number - e = 1,...,nel
# -----------------------------------------------------------------------
E_mat = 29000*1000; # Young's modulus # lbf/in^2
E = E_mat*ones(nel,1);
# todo change to make it parameter
# ---- GEOMETRIC PROPERTIES ---------------------------------------------
# A(e) = A_bar
# e: element number - e = 1,...,nel
# -----------------------------------------------------------------------
#A = ones(nel);
#A[:] .= 400; # mm^2
# ---- BOUNDARY CONDITIONS ----------------------------------------------
# prescribed displacement flags (essential boundary condition)
#
# idb(i,N) = 1 if the degree of freedom i of the node N is prescribed
# = 0 otherwise
#
# 1) initialize idb to 0
# 2) enter the flag for prescribed displacement boundary conditions
# -----------------------------------------------------------------------
idb = zeros(ndf,nnp);
for i in 1:nnp
if nodes[i]["restrained_degrees_of_freedom"][1]
idb[1,i]=1;
end
if nodes[i]["restrained_degrees_of_freedom"][2]
idb[2,i]=1;
end
if nodes[i]["restrained_degrees_of_freedom"][3]
idb[3,i]=1;
end
if nodes[i]["restrained_degrees_of_freedom"][4]
idb[4,i]=1;
end
if nodes[i]["restrained_degrees_of_freedom"][5]
idb[5,i]=1;
end
if nodes[i]["restrained_degrees_of_freedom"][6]
idb[6,i]=1;
end
end
# ---- BOUNDARY CONDITIONS: PRESCRIBED NODAL DISPLACEMENTS --------------
# g(i,N) = prescribed displacement magnitude
# N = 1,...,nnp
# i = 1,...,nsd
#
# 1) initialize g to 0
# 2) enter the values
# -----------------------------------------------------------------------
g = zeros(ndf,nnp);
# ---- BOUNDARY CONDITIONS: PRESCRIBED NODAL FORCES ---------------------
# f(i,N) = prescribed force magnitude
# N = 1,...,nnp
# i = 1,...,nsd
#
# 1) initialize f to 0
# 2) enter the values
# -----------------------------------------------------------------------
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
f = zeros(ndf,nnp);
for i in 1:nnp
f[1,i] = nodes[i]["force"]["x"]*P;
f[2,i] = nodes[i]["force"]["y"]*P;
f[3,i] = nodes[i]["force"]["z"]*P;
end
Ls=[]
for i in 1:nnp
if (nodes[i]["fixedDisplacement"]["x"]!=0)||(nodes[i]["fixedDisplacement"]["y"]!=0)||(nodes[i]["fixedDisplacement"]["z"]!=0)
L=zeros(ndf,nnp);
L[1,i] = nodes[i]["fixedDisplacement"]["x"];
L[2,i] = nodes[i]["fixedDisplacement"]["y"];
L[3,i] = nodes[i]["fixedDisplacement"]["z"];
append!(Ls,[L]);
end
end
A=ones(nel)
# ---- NUMBER THE EQUATIONS ---------------------------------------------
# line 380
id,neq = number_eq(idb,ndf,nnp);
# ---- FORM THE ELEMENT STIFFNESS MATRICES ------------------------------
# line 324
nee = ndf*nen; # number of element equations
Ke = zeros(nee,nee,nel);
# Te = zeros(nen*1,nen*nsd,nel); # *1 is specific to frame
Te = zeros(nee,nee,nel); # *1 is specific to frame
for i = 1:nel
Ke[:,:,i],Te[:,:,i] = Ke_frame(A[i],E[i],ien[:,i],nee,nsd,xn);
end
return E,f,g,idb,ien,ndf,nel,nen,nnp,nsd,xn,len,Ke,Te,Ls
end
# =======================================================================
function getSensitivites(Ke0,dcomp,ien,nel,len,λ)
dfdA=zeros(nel)
dgdA=zeros(nel)
for i in 1:nel
de=[dcomp[1,Int(ien[1,i])] ,dcomp[2,Int(ien[1,i])], dcomp[3,Int(ien[1,i])],dcomp[4,Int(ien[1,i])] ,dcomp[5,Int(ien[1,i])], dcomp[6,Int(ien[1,i])],
dcomp[1,Int(ien[2,i])] ,dcomp[2,Int(ien[2,i])] ,dcomp[3,Int(ien[2,i])],dcomp[4,Int(ien[2,i])] ,dcomp[5,Int(ien[2,i])] ,dcomp[6,Int(ien[2,i])]
];
# de=[dcomp[1,Int(ien[1,i])],dcomp[2,Int(ien[1,i])],dcomp[3,Int(ien[1,i])] ,dcomp[1,Int(ien[2,i])] ,dcomp[2,Int(ien[2,i])] ,dcomp[3,Int(ien[2,i])]]
λe=[λ[1,Int(ien[1,i])], λ[2,Int(ien[1,i])],λ[3,Int(ien[1,i])],λ[4,Int(ien[1,i])], λ[5,Int(ien[1,i])],λ[6,Int(ien[1,i])],
λ[1,Int(ien[2,i])] ,λ[2,Int(ien[2,i])],λ[3,Int(ien[2,i])],λ[4,Int(ien[2,i])] ,λ[5,Int(ien[2,i])],λ[6,Int(ien[2,i])]
]
# println(de)
# println(Ke0[:,:,i])
dKedA=Ke0[:,:,i]
dgdA[i]=(-1.0 .*λe)'*dKedA*de
dfdA[i]=len[i]
end
return dfdA,dgdA
end
# =======================================================================
function getAdjoint(K,dcomp,L,free)
L1=L[free]
λ1=K\L1[:]
λ=zeros(size(dcomp))
λ[free].=reshape(λ1,size(L1))
return λ,L1,λ1
end
# =======================================================================
function getSensitivitesSIMP(Ke0,dcomp,ien,nel,len,λ,η,X)
dfdA=zeros(nel)
dgdA=zeros(nel)
for i in 1:nel
de=[dcomp[1,Int(ien[1,i])] ,dcomp[2,Int(ien[1,i])], dcomp[3,Int(ien[1,i])],dcomp[4,Int(ien[1,i])] ,dcomp[5,Int(ien[1,i])], dcomp[6,Int(ien[1,i])],
dcomp[1,Int(ien[2,i])] ,dcomp[2,Int(ien[2,i])] ,dcomp[3,Int(ien[2,i])],dcomp[4,Int(ien[2,i])] ,dcomp[5,Int(ien[2,i])] ,dcomp[6,Int(ien[2,i])]
];
# de=[dcomp[1,Int(ien[1,i])],dcomp[2,Int(ien[1,i])],dcomp[3,Int(ien[1,i])] ,dcomp[1,Int(ien[2,i])] ,dcomp[2,Int(ien[2,i])] ,dcomp[3,Int(ien[2,i])]]
λe=[λ[1,Int(ien[1,i])], λ[2,Int(ien[1,i])],λ[3,Int(ien[1,i])],λ[4,Int(ien[1,i])], λ[5,Int(ien[1,i])],λ[6,Int(ien[1,i])],
λ[1,Int(ien[2,i])] ,λ[2,Int(ien[2,i])],λ[3,Int(ien[2,i])],λ[4,Int(ien[2,i])] ,λ[5,Int(ien[2,i])],λ[6,Int(ien[2,i])]
]
# println(de)
# println(Ke0[:,:,i])
dKedA=Ke0[:,:,i].*(η).*X[i]^(η-1)
dgdA[i]=(-1.0 .*λe)'*dKedA*de
dfdA[i]=len[i]
end
return dfdA,dgdA
end
# =======================================================================
function optimizeCompliantMechanism1(problem,Ls,free,dmax,totalVolFactor,maxeval=500)
E,f,g,idb,ien,ndf,nel,nen,nnp,nsd,xn,len,Ke,Te=problem
nel=length(len)
function FA(x::Vector, grad::Vector)
K,F,d,stress,dcomp,g=FEM_frame(problem,x);
λ,L1,λ1=getAdjoint(copy(K),copy(dcomp),copy(Ls[1]),copy(free))
grad[:] .=getSensitivites(Ke,dcomp,ien,nel,len,copy(λ))[2]
# g=(L1[:]'*d)[1] .- dmax - (λ1'*(K*d -F))[1]
goal=L1[:]'*d -λ1'*K*d +λ1'*F .-dmax
# display(goal )
# display(sum(x) )
return goal[1]
end
function GA(x::Vector, grad::Vector)
grad[:] .=len[:]
return (sum(x .* len )) - totalVolFactor
end
FA(ones(nel), fill(0.25,nel))
GA(ones(nel), fill(0.25,nel))
opt = Opt(:LD_MMA, nel)
opt.lower_bounds = fill(1e-3,nel)
opt.xtol_rel = 1e-6
opt.maxeval = maxeval
opt.min_objective = FA
inequality_constraint!(opt, (x,gg) -> GA(x,gg), 1e-6)
display(@time (minf,minx,ret) = optimize(opt, ones(nel)))
numevals = opt.numevals # the number of function evaluations
display("got $minf after $numevals iterations (returned $ret)")
return minx
end
# =======================================================================
function optimizeCompliantMechanism(problem,Ls,free,dmax,amax,maxeval=500,SIMP=false)
E,f,g,idb,ien,ndf,nel,nen,nnp,nsd,xn,len,Ke,Te=problem
nel=length(len)
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
function GA(x::Vector, grad::Vector,num::Int)
K,F,d,stress,dcomp,g=FEM_frame(problem,x);
λ,L1,λ1=getAdjoint(copy(K),copy(dcomp),copy(Ls[num]),copy(free))
grad[:] .=getSensitivites(Ke,dcomp,ien,nel,len,copy(λ))[2]
# g=(L1[:]'*d)[1] .- dmax - (λ1'*(K*d -F))[1]
goal=L1[:]'*d -λ1'*K*d +λ1'*F .-dmax
# display(goal )
# display(sum(x) )
return goal[1]
end
function GASIMP(x::Vector, grad::Vector,num::Int)
K,F,d,stress,dcomp,g=FEM_frame(problem,x.^η);
λ,L1,λ1=getAdjoint(copy(K),copy(dcomp),copy(Ls[num]),copy(free))
grad[:] .=getSensitivitesSIMP(Ke,dcomp,ien,nel,len,copy(λ),η,x)[2]
# g=(L1[:]'*d)[1] .- dmax - (λ1'*(K*d -F))[1]
goal=L1[:]'*d -λ1'*K*d +λ1'*F .-dmax
# display(goal )
# display(sum(x) )
return goal[1]
end
function FA(x::Vector, grad::Vector)
grad[:] .=len[:]
return (sum(x .* len ))
end
function FASIMP(x::Vector, grad::Vector)
grad[:] .=len[:]
return (sum(x.^η .* len ))
end
opt = Opt(:LD_MMA, nel)
opt.upper_bounds = fill(amax,nel)
opt.xtol_rel = 1e-8
if (SIMP)
opt.min_objective = FASIMP
println(size(Ls)[1])
for i =1:size(Ls)[1]
inequality_constraint!(opt, (x,gg) -> GASIMP(x,gg,i), 1e-8)
end
else
opt.min_objective = FA
println(size(Ls)[1])
for i =1:size(Ls)[1]
inequality_constraint!(opt, (x,gg) -> GA(x,gg,i), 1e-8)
end
display(@time (minf,minx,ret) = optimize(opt, ones(nel).*amax*0.5))
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
numevals = opt.numevals # the number of function evaluations
display("got $minf after $numevals iterations (returned $ret)")
return minx
end
# =======================================================================
function simulateAndExport(setup,X,dcomp,fileName,threshold)
nodes=setup["nodes"]
edges=setup["edges"]
nodesNew=[]
edgesNew=[]
nel = length(edges); # number of elements
nnp = length(nodes); # number of nodal points
for i in 1:nnp
nodes[i]["in"]=false
nodes[i]["displacement"]["x"]=dcomp[1,i];
nodes[i]["displacement"]["y"]=dcomp[2,i];
nodes[i]["displacement"]["z"]=dcomp[3,i];
end
idCount=0
for i in 1:nel
if X[i]>threshold
if (!nodes[(edges[i]["source"]+1)]["in"])
nodes[(edges[i]["source"]+1)]["in"]=true
nodes[(edges[i]["source"]+1)]["id"]="n$(idCount)"
nodes[(edges[i]["source"]+1)]["idd"]=idCount
append!(nodesNew,[nodes[(edges[i]["source"]+1)]])
id1=idCount
idCount=idCount+1
else
id1=nodes[(edges[i]["source"]+1)]["idd"]
end
if (!nodes[(edges[i]["target"]+1)]["in"])
nodes[(edges[i]["target"]+1)]["in"]=true
nodes[(edges[i]["target"]+1)]["id"]="n$(idCount)"
nodes[(edges[i]["target"]+1)]["idd"]=idCount
append!(nodesNew,[nodes[(edges[i]["target"]+1)]])
id2=idCount
idCount=idCount+1
else
id2=nodes[(edges[i]["target"]+1)]["idd"]
end
edges[i]["source"]=id1
edges[i]["target"]=id2
append!(edgesNew,[edges[i]])
end
end
setup["nodes"]=nodesNew;
setup["edges"]=edgesNew;
stringdata = JSON.json(setup)# pass data as a json string (how it shall be displayed in a file)
# write the file with the stringdata variable information
open(fileName, "w") do f
write(f, stringdata)
end
# E,f,g,idb,ien,ndf,nel,nen,nnp,nsd,xn,len,Ke,Te,Ls=getDataFromSetup3D(setup,scale);
# problem1=E,f,g,idb,ien,ndf,nel,nen,nnp,nsd,xn,len,Ke,Te;
# nel=length(edgesNew)
# display(a)
# X=ones(nel)
# K,F,d,stress,dcomp,g=FEM_frame(problem1,X);
# display(dcomp)
# display(plotTrussDeformed3D(problem1,copy(X),scale,0.1,1))
end