-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipopt_routines.py
1884 lines (1585 loc) · 85.5 KB
/
ipopt_routines.py
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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import scipy.sparse as sp
#################################
## IPOPT classes definition
#################################
#https://stackoverflow.com/questions/68266571/how-to-speed-up-the-ipopt-solver
class IPOPT_Problem():
""" Parent class used to store common methods for the different IPOPT classes """
def calc_reduction_pattern_mapped(self):
""" This method calculates the reduced mapping pattern of the distribution of the area variables """
if self.myBCs.isReduced:
reduction_pattern_map = np.zeros(self.myBCs.ncel_str*self.myBCs.N_cell_max)
for i in range(self.n_topologies_cell): # starting from the lower left corner
reduction_pattern_map += np.kron(self.cell_mapping_matrix[:,i], self.myBCs.reduction_pattern[i,:])
return reduction_pattern_map==1
else:
raise ValueError("This method can't be used on unreduced problems")
def calc_area_phys(self, area):
""" This function is used to calculate the true physical distribution of the sections of the truss starting from the mapping matrix and the areas of the cells """
if self.myBCs.isReduced:
reduction_pattern_map = self.calc_reduction_pattern_mapped()
a_cell = np.zeros(self.myBCs.reduction_pattern.size).ravel()
tmp = np.zeros(self.myBCs.ncel_str*self.myBCs.N_cell_max)
a_cell[self.myBCs.reduction_pattern.ravel()] = area
a_cell = a_cell.reshape((-1,self.myBCs.N_cell_max))
a_cell_out = a_cell.copy()
for i in range(self.n_topologies_cell): # starting from the lower left corner
tmp += np.kron(self.cell_mapping_matrix[:,i], a_cell[i,:])
area_phys = tmp[reduction_pattern_map] # initialize a_phys
else:
area_phys = np.zeros(self.N)
a_cell_out = np.zeros(self.n_topologies_cell, dtype='object_')
for i in range(self.n_topologies_cell): # starting from the lower left corner
area_phys += np.kron(self.cell_mapping_matrix[:,i], area[i*self.N_cell[0]:(i+1)*self.N_cell[0]]) # initialize a_phys
a_cell_out[i] = np.array(area[i*self.N_cell[0]:(i+1)*self.N_cell[0]])
return area_phys, a_cell_out
def calc_area_phys_id(self):
""" This function is used to calculate the true physical distribution of the sections IDs of the truss starting from the mapping matrix """
if self.myBCs.isReduced:
reduction_pattern_map = self.calc_reduction_pattern_mapped()
IDs = np.ones(self.myBCs.reduction_pattern.size, dtype='int').ravel() * -1
tmp = np.zeros(self.myBCs.ncel_str*self.myBCs.N_cell_max, dtype='int')
IDs[self.myBCs.reduction_pattern.ravel()] = np.arange(self.NN, dtype='int')
IDs = IDs.reshape((-1,self.myBCs.N_cell_max))
for i in range(self.n_topologies_cell): # starting from the lower left corner
tmp += np.kron(self.cell_mapping_matrix[:,i], IDs[i,:]) # initialize a_phys
area_phys_id = tmp[reduction_pattern_map]
else:
area_phys_id = np.zeros(self.N, dtype='int')
for i in range(self.n_topologies_cell): # starting from the lower left corner
area_phys_id += np.kron(self.cell_mapping_matrix[:,i], np.arange(self.NN, dtype='int')[i*self.N_cell[0]:(i+1)*self.N_cell[0]])
return area_phys_id
def calc_len_phys(self):
""" This function is used to calculate the sum of the true physical distribution of the length of the truss starting from the mapping matrix.
Used for gradient calculation """
if self.myBCs.isReduced:
g = []
for i in range(self.n_topologies_cell):
su = np.sum(np.kron(self.cell_mapping_matrix[:,i].T, (self.l_cell[i][self.myBCs.reduction_pattern_len[i,:]]).reshape((-1, 1))), axis=1)
g = np.append(g,su[su>0])
else:
g = []
for i in range(self.n_topologies_cell):
su = np.sum(np.kron(self.cell_mapping_matrix[:,i].T, self.l_cell.reshape((-1, 1))), axis=1)
g = np.append(g,su[su>0])
return g
class Layopt_IPOPT(IPOPT_Problem):
def __init__(self, N, M, l_true, joint_cost, B, f, s_c, s_t, E):
# Use the class constructor to declare the variable used by the optimizer during the optimization
self.N = N # Number of member of the Ground Structure
self.M = M # Number of DOFs of the Groud Structure
self.l_true = l_true # Physical member lenghts [mm]
self.l = l_true + joint_cost * np.max(l_true) # indipendency from the number of cells [mm]
self.B = B # Equilibrium forces matrix
self.f = f # External forces [N]
self.s_c = s_c # Max compression allowable stress [MPa]
self.s_t = s_t # Max tension allowable stress [MPa]
self.E = E # Young's modulus [MPa]
# Sparsity pattern of B matrix
self.B_coo = self.B.tocoo()
self.B_row, self.B_column = self.B_coo.row,self.B_coo.col
self.B_T_coo = self.B.T.tocoo() # Horrible but necessary to conserve the very same order of dComp_dU
self.B_T_row, self.B_T_column = self.B_T_coo.row,self.B_T_coo.col
# Create the indexing variables used for splitting the design variables vector x
self.area = slice(0,N)
self.force = slice(N,2*N)
self.U = slice(2*N,2*N+M)
# History values
self.it = 0
self.obj_hist = []
def objective(self, x):
"""Returns the scalar value of the objective given x."""
self.volume_phys = self.l_true.T @ x[self.area]
return self.l.T @ x[self.area]
def gradient(self, x):
"""Returns the gradient of the objective with respect to x."""
grad = np.zeros(x.size)
grad[self.area] = self.l
return grad
def constraints(self, x):
"""Returns the constraints."""
### Equilibrium (M eq)
equilibrium = self.B @ x[self.force] - self.f
### Stress (2*N eq)
stress_c = x[self.force] - self.s_c * x[self.area]
stress_t = x[self.force] - self.s_t * x[self.area]
stress = np.append(stress_c, stress_t)
### Compatibility (N eq)
compatibility = x[self.area] * self.E/self.l * (self.B.T @ x[self.U]) - x[self.force]
cons = np.concatenate((equilibrium, stress, compatibility))
return cons
def jacobianstructure(self):
""" Returns the row and column indices for non-zero vales of the
Jacobian. """
# DIM = Number of constraint functions X number of design variables
### Equilibrium (M eq)
row, column = self.B_row, self.B_column+self.N # the indexes are translated to the corresponding design variables (force)
### Stress (2*N eq)
row = np.append(row,range(self.M,self.M+self.N*2))
column = np.append(column,np.r_[self.area,self.area])
row = np.append(row,range(self.M,self.M+self.N*2))
column = np.append(column,np.r_[self.force,self.force])
### Compatibility (N eq)
row = np.append(row,range(self.M+self.N*2,self.M+self.N*3))
column = np.append(column,np.r_[self.area])
row = np.append(row,range(self.M+self.N*2,self.M+self.N*3))
column = np.append(column,np.r_[self.force])
row = np.append(row,self.B_T_row + self.M+self.N*2) # B is transposed
column = np.append(column,self.B_T_column+self.N*2)
return row,column
def jacobian(self, x):
""" Returns the Jacobian of the constraints with respect to x. """
# DIM = Number of constraint functions X number of design variables
### Equilibrium (M eq)
Jacobian = self.B_coo.data
### Stress (2*N eq)
Jacobian = np.append(Jacobian,np.ones(self.N)*(- self.s_c))
Jacobian = np.append(Jacobian,np.ones(self.N)*(- self.s_t))
Jacobian = np.append(Jacobian,np.ones(self.N))
Jacobian = np.append(Jacobian,np.ones(self.N))
### Compatibility (N eq)
dComp_dA = self.B.T @ x[self.U] * self.E / self.l # Diagonal term, i==j
dComp_dU = (sp.diags(x[self.area] * self.E / self.l) @ self.B.T) # Multiply the column of B.T and the main diagnonal of diag
dComp_dU.sort_indices() # Needed to match the order of B.T given in jacobianstructure
dComp_dU = dComp_dU.tocoo()
# Area
Jacobian = np.append(Jacobian,dComp_dA)
# Force
Jacobian = np.append(Jacobian,np.ones(self.N)*-1)
# Displacements
Jacobian = np.append(Jacobian,dComp_dU.data)
return Jacobian
""" def hessianstructure(self):
Returns the row and column indices for non-zero vales of the
Hessian.
# NOTE: The default hessian structure is of a lower triangular matrix,
# therefore this function is redundant. It is included as an example
# for structure callback.
return np.nonzero(np.tril(np.ones((4, 4))))
def hessian(self, x, lagrange, obj_factor):
Returns the non-zero values of the Hessian.
H = obj_factor*np.array((
(2*x[3], 0, 0, 0),
(x[3], 0, 0, 0),
(x[3], 0, 0, 0),
(2*x[0]+x[1]+x[2], x[0], x[0], 0)))
H += lagrange[0]*np.array((
(0, 0, 0, 0),
(x[2]*x[3], 0, 0, 0),
(x[1]*x[3], x[0]*x[3], 0, 0),
(x[1]*x[2], x[0]*x[2], x[0]*x[1], 0)))
H += lagrange[1]*2*np.eye(4)
row, col = self.hessianstructure()
return H[row, col] """
def intermediate(self, alg_mod, iter_count, obj_value, inf_pr, inf_du, mu,
d_norm, regularization_size, alpha_du, alpha_pr,
ls_trials):
"""Prints information at every Ipopt iteration."""
self.it = iter_count
self.obj_hist = np.append(self.obj_hist, self.volume_phys)
class Layopt_IPOPT_Buck(IPOPT_Problem):
def __init__(self, N, M, l_true, joint_cost, B, f, s_c, s_t, E, s_buck):
# Use the class constructor to declare the variable used by the optimizer during the optimization
self.N = N # Number of member of the Ground Structure
self.M = M # Number of DOFs of the Groud Structure
self.l_true = l_true # Physical member lenghts [mm]
self.l = l_true + joint_cost * np.max(l_true) # indipendency from the number of cells [mm]
self.B = B # Equilibrium forces matrix
self.f = f # External forces [N]
self.s_c = s_c # Max compression allowable stress [MPa]
self.s_t = s_t # Max tension allowable stress [MPa]
self.E = E # Young's modulus [MPa]
# Buckling specific parameters
self.s_buck = s_buck
self.a_cr = -s_c * l_true**2 / self.s_buck # Evaluation of the critical section for the members
# Sparsity pattern of B matrix
self.B_coo = self.B.tocoo()
self.B_row, self.B_column = self.B_coo.row,self.B_coo.col
self.B_T_coo = self.B.T.tocoo() # Horrible but necessary to conserve the very same order of dComp_dU
self.B_T_row, self.B_T_column = self.B_T_coo.row,self.B_T_coo.col
# Create the indexing variables used for splitting the design variables vector x
self.area = slice(0,N)
self.force = slice(N,2*N)
self.U = slice(2*N,2*N+M)
# History values
self.it = 0
self.obj_hist = []
def objective(self, x):
"""Returns the scalar value of the objective given x."""
self.volume_phys = self.l_true.T @ x[self.area]
return self.l.T @ x[self.area]
def gradient(self, x):
"""Returns the gradient of the objective with respect to x."""
grad = np.zeros(x.size)
grad[self.area] = self.l
return grad
def constraints(self, x):
"""Returns the constraints."""
### Equilibrium (M eq)
equilibrium = self.B @ x[self.force] - self.f
### Stress (2*N eq)
stress_c = x[self.force] - self.s_c * x[self.area]
stress_t = x[self.force] - self.s_t * x[self.area]
stress = np.append(stress_c, stress_t)
### Compatibility (N eq)
compatibility = x[self.area] * self.E/self.l_true * (self.B.T @ x[self.U]) - x[self.force]
### Buckling (N eq)
buckling = x[self.force] + (self.s_buck/self.l_true**2) * x[self.area]**2
cons = np.concatenate((equilibrium, stress, compatibility, buckling))
return cons
def jacobianstructure(self):
""" Returns the row and column indices for non-zero vales of the
Jacobian. """
# DIM = Number of constraint functions X number of design variables
### Equilibrium (M eq)
row, column = self.B_row, self.B_column+self.N # the indexes are translated to the corresponding design variables (force)
### Stress (2*N eq)
row = np.append(row,range(self.M,self.M+self.N*2))
column = np.append(column,np.r_[self.area,self.area])
row = np.append(row,range(self.M,self.M+self.N*2))
column = np.append(column,np.r_[self.force,self.force])
### Compatibility (N eq)
row = np.append(row,range(self.M+self.N*2,self.M+self.N*3))
column = np.append(column,np.r_[self.area])
row = np.append(row,range(self.M+self.N*2,self.M+self.N*3))
column = np.append(column,np.r_[self.force])
row = np.append(row,self.B_T_row + self.M+self.N*2) # B is transposed
column = np.append(column,self.B_T_column+self.N*2)
### Buckling (N eq)
row = np.append(row,range(self.M+self.N*3,self.M+self.N*4))
column = np.append(column,np.r_[self.area])
row = np.append(row,range(self.M+self.N*3,self.M+self.N*4))
column = np.append(column,np.r_[self.force])
return row,column
def jacobian(self, x):
""" Returns the Jacobian of the constraints with respect to x. """
# DIM = Number of constraint functions X number of design variables
### Equilibrium (M eq)
Jacobian = self.B_coo.data
### Stress (2*N eq)
Jacobian = np.append(Jacobian,np.ones(self.N)*(- self.s_c))
Jacobian = np.append(Jacobian,np.ones(self.N)*(- self.s_t))
Jacobian = np.append(Jacobian,np.ones(self.N))
Jacobian = np.append(Jacobian,np.ones(self.N))
### Compatibility (N eq) x[self.area] * self.E/self.l_true * (self.B.T @ x[self.U]) - x[self.force]
dComp_dA = self.B.T @ x[self.U] * self.E / self.l_true # Diagonal term, i==j
dComp_dU = (sp.diags(x[self.area] * self.E / self.l_true) @ self.B.T) # Multiply the column of B.T and the main diagnonal of diag
dComp_dU.sort_indices() # Needed to match the order of B.T given in jacobianstructure
dComp_dU = dComp_dU.tocoo()
# Area
Jacobian = np.append(Jacobian,dComp_dA)
# Force
Jacobian = np.append(Jacobian,np.ones(self.N)*-1)
# Displacements
Jacobian = np.append(Jacobian,dComp_dU.data)
### Buckling (N eq)
# Area
Jacobian = np.append(Jacobian,2*x[self.area]*self.s_buck/self.l_true**2)
# Force
Jacobian = np.append(Jacobian,np.ones(self.N))
return Jacobian
def hessianstructure(self):
""" Returns the row and column indices for non-zero vales of the
Hessian. """
# DIM = Number of design variables X number of design variables
M = self.M
N = self.N
### Compatibility (N eq) x[self.area] * self.E/self.l_true * (self.B.T @ x[self.U]) - x[self.force]
## Area self.B.T @ x[self.U] * self.E / self.l_true
# Area -
# Force -
# Displacements
i, j = np.meshgrid(np.arange(2*N,2*N+M, dtype='int'), np.arange(N, dtype='int'))
### Buckling (N eq) buckling = x[self.force] + (self.s_buck/self.l_true**2) * x[self.area]**2
## Area 2*x[self.area]*self.s_buck/self.l_true**2
# Area
i = np.append(i.ravel(),np.arange(N, dtype='int'))
j = np.append(j.ravel(),np.arange(N, dtype='int'))
return i, j
def hessian(self, x, l, obj_factor):
""" Returns the non-zero values of the Hessian. """
# DIM = Number of design variables X number of design variables
M = self.M
N = self.N
# ID of the constraints
comp_id = slice(M+N*2, M+N*3)
buck_id = slice(M+N*3, M+N*4)
### Objective function (N eq) the hessian of the obj function is empty
# Hessian = obj_factor * NULL
### Equilibrium (M eq) self.B @ x[self.force] - self.f
## Area -
## Force self.B_coo.data
## Displacements -
### Stress (2*N eq)
## Area -
## Force -
## Displacements -
### Compatibility (N eq) x[self.area] * self.E/self.l_true * (self.B.T @ x[self.U]) - x[self.force]
## Area self.B.T @ x[self.U] * self.E / self.l_true
# Area -
# Force -
# Displacements
temp = (sp.diags(l[comp_id] * self.E / self.l_true) @ self.B.T)
temp.sort_indices() # Needed to match the order of B.T given in jacobianstructure
temp = temp.tocoo()
j, i = temp.row, temp.col + (2*N)
data = temp.data
## Force np.ones(self.N)*-1
## Displacements (sp.diags(x[self.area] * self.E / self.l_true) @ self.B.T)
# Area
# Symmetric, already considered
# Force -
# Displacements -
### Buckling (N eq) buckling = x[self.force] + (self.s_buck/self.l_true**2) * x[self.area]**2
## Area 2*x[self.area]*self.s_buck/self.l_true**2
# Area
i = np.append(i.ravel(),np.arange(N, dtype='int'))
j = np.append(j.ravel(),np.arange(N, dtype='int'))
data = np.append(data,2*self.s_buck/self.l_true**2 * l[buck_id])
# Force -
# Displacements -
## Force np.ones(self.N)
## Displacements -
Hessian = sp.coo_matrix((data, (i, j)), shape=(x.size, x.size)).tocsc() # check which one is better
row, col = self.hessianstructure()
out = np.array(Hessian[row, col]).ravel()
return out
def intermediate(self, alg_mod, iter_count, obj_value, inf_pr, inf_du, mu,
d_norm, regularization_size, alpha_du, alpha_pr,
ls_trials):
"""Prints information at every Ipopt iteration."""
self.it = iter_count
self.obj_hist = np.append(self.obj_hist, self.volume_phys)
class Layopt_IPOPT_VL(IPOPT_Problem):
def __init__(self, NN, N_cell, N, M, myBCs, joint_cost, B, f, s_c, s_t, E, cell_mapping_matrix):
# Use the class constructor to declare the variable used by the optimizer during the optimization
self.NN = NN # Number of members of the cells
self.N_cell = N_cell # Number of members of a single cell
self.N = N # Number of members of the Ground Structure
self.M = M # Number of DOFs of the Groud Structure
self.myBCs = myBCs # BC class
self.l_true = myBCs.ground_structure_length # Physical member lenghts [mm]
self.l = self.l_true + joint_cost * np.max(self.l_true) # indipendency from the number of cells [mm]
self.B = B # Equilibrium forces matrix
self.f = f # External forces [N]
self.s_c = s_c # Max compression allowable stress [MPa]
self.s_t = s_t # Max tension allowable stress [MPa]
self.E = E # Young's modulus [MPa]
self.cell_mapping_matrix = cell_mapping_matrix # Mapping function used to distribute the cells over the structure domain
self.n_topologies_cell = np.size(cell_mapping_matrix, 1) # Number of different cell topologies
# VL parameters
if self.myBCs.isReduced:
self.l_cell = self.myBCs.l_cell_full.copy() + joint_cost * np.max(self.l_true) # change, different cell size per different reduced cell
else:
self.l_cell = self.l[:N_cell[0]].copy() # Length of every member of one cell
# Sparsity pattern of B matrix
self.B_coo = self.B.tocoo()
self.B_row, self.B_column = self.B_coo.row,self.B_coo.col
self.B_T_coo = self.B.T.tocoo() # Horrible but necessary to conserve the very same order of dComp_dU
self.B_T_row, self.B_T_column = self.B_T_coo.row,self.B_T_coo.col
# Create the indexing variables used for splitting the design variables vector x
self.area = slice(0,NN)
self.force = slice(NN,NN+N)
self.U = slice(NN+N,NN+N+M)
# History values
self.it = 0
self.obj_hist = []
def objective(self, x):
"""Returns the scalar value of the objective given x."""
area_phys,_ = self.calc_area_phys(x[self.area])
self.volume_phys = self.l_true.T @ area_phys
return self.l.T @ area_phys
def gradient(self, x):
"""Returns the gradient of the objective with respect to x."""
g = self.calc_len_phys()
grad = np.zeros(x.size)
grad[self.area] = g
return grad
def constraints(self, x):
"""Returns the constraints."""
area_phys, = self.calc_area_phys(x[self.area]) # initialize a_phys
### Equilibrium (M eq)
equilibrium = self.B @ x[self.force] - self.f
### Stress (2*N eq)
stress_c = x[self.force] - self.s_c * area_phys
stress_t = x[self.force] - self.s_t * area_phys
stress = np.append(stress_c, stress_t)
### Compatibility (N eq)
compatibility = area_phys * self.E/self.l_true * (self.B.T @ x[self.U]) - x[self.force]
cons = np.concatenate((equilibrium, stress, compatibility))
return cons
def jacobianstructure(self):
""" Returns the row and column indices for non-zero vales of the
Jacobian. """
# DIM = Number of constraint functions X number of design variables
area_phys_id = self.calc_area_phys_id()
### Equilibrium (M eq)
row, column = self.B_row, self.B_column+self.NN # the indexes are translated to the corresponding design variables (force)
### Stress (2*N eq)
row = np.append(row,range(self.M,self.M+self.N*2))
column = np.append(column,np.tile(area_phys_id,2))
row = np.append(row,range(self.M,self.M+self.N*2))
column = np.append(column,np.r_[self.force,self.force])
### Compatibility (N eq)
row = np.append(row,range(self.M+self.N*2,self.M+self.N*3))
column = np.append(column,area_phys_id)
row = np.append(row,range(self.M+self.N*2,self.M+self.N*3))
column = np.append(column,np.r_[self.force])
row = np.append(row,self.B_T_row + self.M+self.N*2) # B is transposed
column = np.append(column,self.B_T_column+self.N+self.NN)
return row,column
def jacobian(self, x):
""" Returns the Jacobian of the constraints with respect to x. """
# DIM = Number of constraint functions X number of design variables
# Physical areas
area_phys,_ = self.calc_area_phys(x[self.area]) # initialize a_phys
### Equilibrium (M eq)
Jacobian = self.B_coo.data
### Stress (2*N eq)
Jacobian = np.append(Jacobian,np.ones(self.N)*(- self.s_c))
Jacobian = np.append(Jacobian,np.ones(self.N)*(- self.s_t))
Jacobian = np.append(Jacobian,np.ones(self.N))
Jacobian = np.append(Jacobian,np.ones(self.N))
### Compatibility (N eq)
dComp_dA = []
dComp_dA = self.B.T @ x[self.U] * self.E / self.l_true # Diagonal term, i==j
dComp_dU = (sp.diags(area_phys * self.E / self.l_true) @ self.B.T) # Multiply the column of B.T and the main diagnonal of diag
dComp_dU.sort_indices() # Needed to match the order of B.T given in jacobianstructure
dComp_dU = dComp_dU.tocoo()
# Area
Jacobian = np.append(Jacobian,dComp_dA)
# Force
Jacobian = np.append(Jacobian,np.ones(self.N)*-1)
# Displacements
Jacobian = np.append(Jacobian,dComp_dU.data)
return Jacobian
def intermediate(self, alg_mod, iter_count, obj_value, inf_pr, inf_du, mu,
d_norm, regularization_size, alpha_du, alpha_pr,
ls_trials):
"""Prints information at every Ipopt iteration."""
self.it = iter_count
self.obj_hist = np.append(self.obj_hist, self.volume_phys)
class Layopt_IPOPT_VL_Buck(IPOPT_Problem):
def __init__(self, NN, N_cell, N, M, myBCs, joint_cost, B, f, s_c, s_t, E, s_buck, cell_mapping_matrix):
# Use the class constructor to declare the variable used by the optimizer during the optimization
self.NN = NN # Number of members of the cells
self.N_cell = N_cell # Number of members of a single cell
self.N = N # Number of members of the Ground Structure
self.M = M # Number of DOFs of the Groud Structure
self.myBCs = myBCs # BC class
self.l_true = myBCs.ground_structure_length # Physical member lengths [mm]
self.l = self.l_true + joint_cost * np.max(self.l_true) # indipendency from the number of cells [mm]
self.B = B # Equilibrium forces matrix
self.f = f # External forces [N]
self.s_c = s_c # Max compression allowable stress [MPa]
self.s_t = s_t # Max tension allowable stress [MPa]
self.E = E # Young's modulus [MPa]
self.cell_mapping_matrix = cell_mapping_matrix # Mapping function used to distribute the cells over the structure domain
self.n_topologies_cell = np.size(cell_mapping_matrix, 1) # Number of different cell topologies
# VL parameters
if self.myBCs.isReduced:
self.l_cell = self.myBCs.l_cell_full.copy() + joint_cost * np.max(self.l_true) # change, different cell size per different reduced cell
else:
self.l_cell = self.l[:N_cell[0]].copy() # Length of every member of one cell
# Buckling specific parameters
self.s_buck = s_buck
self.a_cr = -s_c * self.l_true**2 / self.s_buck # Evaluation of the critical section for the members
# Sparsity pattern of B matrix
self.B_coo = self.B.tocoo()
self.B_row, self.B_column = self.B_coo.row,self.B_coo.col
self.B_T_coo = self.B.T.tocoo() # Horrible but necessary to keep the very same order of dComp_dU
self.B_T_row, self.B_T_column = self.B_T_coo.row,self.B_T_coo.col
# Create the indexing variables used for splitting the design variables vector x
self.area = slice(0,NN)
self.force = slice(NN,NN+N)
self.U = slice(NN+N,NN+N+M)
# History values
self.it = 0
self.obj_hist = []
def objective(self, x):
"""Returns the scalar value of the objective given x."""
area_phys,_ = self.calc_area_phys(x[self.area])
self.volume_phys = self.l_true.T @ area_phys
return self.l.T @ area_phys
def gradient(self, x):
"""Returns the gradient of the objective with respect to x."""
g = self.calc_len_phys()
grad = np.zeros(x.size)
grad[self.area] = g
return grad
def constraints(self, x):
"""Returns the constraints."""
area_phys,_ = self.calc_area_phys(x[self.area]) # initialize a_phys
### Equilibrium (M eq)
equilibrium = self.B @ x[self.force] - self.f
### Stress (2*N eq)
stress_c = x[self.force] - self.s_c * area_phys
stress_t = x[self.force] - self.s_t * area_phys
stress = np.append(stress_c, stress_t)
### Compatibility (N eq)
compatibility = area_phys * self.E/self.l_true * (self.B.T @ x[self.U]) - x[self.force]
### Buckling (N eq)
buckling = x[self.force] + (self.s_buck/self.l_true**2) * area_phys**2
cons = np.concatenate((equilibrium, stress, compatibility, buckling))
return cons
def jacobianstructure(self):
""" Returns the row and column indices for non-zero vales of the
Jacobian. """
# DIM = Number of constraint functions X number of design variables
area_phys_id = self.calc_area_phys_id()
### Equilibrium (M eq)
row, column = self.B_row, self.B_column+self.NN # the indexes are translated to the corresponding design variables (force)
### Stress (2*N eq)
row = np.append(row,range(self.M,self.M+self.N*2))
column = np.append(column,np.tile(area_phys_id,2))
row = np.append(row,range(self.M,self.M+self.N*2))
column = np.append(column,np.r_[self.force,self.force])
### Compatibility (N eq)
row = np.append(row,range(self.M+self.N*2,self.M+self.N*3))
column = np.append(column,area_phys_id)
row = np.append(row,range(self.M+self.N*2,self.M+self.N*3))
column = np.append(column,np.r_[self.force])
row = np.append(row,self.B_T_row + self.M+self.N*2) # B is transposed
column = np.append(column,self.B_T_column+self.N+self.NN)
### Buckling (N eq)
row = np.append(row,range(self.M+self.N*3,self.M+self.N*4))
column = np.append(column,area_phys_id)
row = np.append(row,range(self.M+self.N*3,self.M+self.N*4))
column = np.append(column,np.r_[self.force])
return row,column
def jacobian(self, x):
""" Returns the Jacobian of the constraints with respect to x. """
# DIM = Number of constraint functions X number of design variables
# Physical areas
area_phys,_ = self.calc_area_phys(x[self.area]) # initialize a_phys
### Equilibrium (M eq)
Jacobian = self.B_coo.data
### Stress (2*N eq)
Jacobian = np.append(Jacobian,np.ones(self.N)*(- self.s_c))
Jacobian = np.append(Jacobian,np.ones(self.N)*(- self.s_t))
Jacobian = np.append(Jacobian,np.ones(self.N))
Jacobian = np.append(Jacobian,np.ones(self.N))
### Compatibility (N eq)
dComp_dA = []
dComp_dA = self.B.T @ x[self.U] * self.E / self.l_true # Diagonal term, i==j
dComp_dU = (sp.diags(area_phys * self.E / self.l_true) @ self.B.T) # Multiply the column of B.T and the main diagnonal of diag
dComp_dU.sort_indices() # Needed to match the order of B.T given in jacobianstructure
dComp_dU = dComp_dU.tocoo()
# Area
Jacobian = np.append(Jacobian,dComp_dA)
# Force
Jacobian = np.append(Jacobian,np.ones(self.N)*-1)
# Displacements
Jacobian = np.append(Jacobian,dComp_dU.data)
### Buckling (N eq)
# Area
Jacobian = np.append(Jacobian,2*area_phys*self.s_buck/self.l_true**2)
# Force
Jacobian = np.append(Jacobian,np.ones(self.N))
return Jacobian
def hessianstructure(self):
""" Returns the row and column indices for non-zero vales of the
Hessian. """
# DIM = Number of design variables X number of design variables
M = self.M
N = self.N
NN = self.NN
### Compatibility (N eq) x[self.area] * self.E/self.l_true * (self.B.T @ x[self.U]) - x[self.force]
## Area self.B.T @ x[self.U] * self.E / self.l_true
# Area -
# Force -
# Displacements
temp_i, temp_j = np.meshgrid(np.arange(NN+N,NN+N+M, dtype='int'), np.arange(NN, dtype='int'))
i = temp_i.ravel()
j = temp_j.ravel()
### Buckling (N eq) buckling = x[self.force] + (self.s_buck/self.l_true**2) * x[self.area]**2
## Area 2*x[self.area]*self.s_buck/self.l_true**2
# Area
i = np.append(i,np.arange(NN, dtype='int'))
j = np.append(j,np.arange(NN, dtype='int'))
# here you have to sort and to eliminate redundant variables
return i, j
def hessian(self, x, l, obj_factor):
""" Returns the non-zero values of the Hessian. """
# DIM = Number of design variables X number of design variables
M = self.M
N = self.N
NN = self.NN
# ID of the constraints
comp_id = slice(M+N*2, M+N*3)
buck_id = slice(M+N*3, M+N*4)
### Objective function (N eq) the hessian of the obj function is empty
# Hessian = obj_factor * NULL
### Equilibrium (M eq) self.B @ x[self.force] - self.f
## Area -
## Force self.B_coo.data
## Displacements -
### Stress (2*N eq)
## Area -
## Force -
## Displacements -
### Compatibility (N eq) x[self.area] * self.E/self.l_true * (self.B.T @ x[self.U]) - x[self.force]
## Area self.B.T @ x[self.U] * self.E / self.l_true
# Area -
# Force -
# Displacements
temp = (sp.diags(l[comp_id] * self.E / self.l_true) @ self.B.T).tocoo()
j, i = temp.row, temp.col + (NN+N)
try:
j[j>=NN] = j % NN # Only the first cellule is important for sensitivity
except:
pass
data = temp.data
## Force np.ones(self.N)*-1
## Displacements (sp.diags(x[self.area] * self.E / self.l_true) @ self.B.T)
# Area
# Symmetric, already considered
# Force -
# Displacements -
### Buckling (N eq) buckling = x[self.force] + (self.s_buck/self.l_true**2) * x[self.area]**2
## Area 2*x[self.area]*self.s_buck/self.l_true**2
# Area
i = np.append(i.ravel(),np.tile(np.arange(NN, dtype='int'),self.myBCs.ncel_str))
j = np.append(j.ravel(),np.tile(np.arange(NN, dtype='int'),self.myBCs.ncel_str))
data = np.append(data, 2*self.s_buck/self.l_true**2 * l[buck_id])
# Force -
# Displacements -
## Force np.ones(self.N)
## Displacements -
Hessian = sp.coo_matrix((data, (i, j)), shape=(x.size, x.size)).tocsc() # check which one is better
row, col = self.hessianstructure()
out = np.array(Hessian[row, col]).ravel()
return out
def intermediate(self, alg_mod, iter_count, obj_value, inf_pr, inf_du, mu,
d_norm, regularization_size, alpha_du, alpha_pr,
ls_trials):
"""Prints information at every Ipopt iteration."""
self.it = iter_count
self.obj_hist = np.append(self.obj_hist, self.volume_phys)
class Layopt_IPOPT_Buck_multiload(IPOPT_Problem):
def __init__(self, N, M, l_true, joint_cost, B, f, dofs, s_c, s_t, E, s_buck):
# Use the class constructor to declare the variable used by the optimizer during the optimization
self.N = N # Number of member of the Ground Structure
self.M = M # Number of DOFs of the Groud Structure
self.l_true = l_true # Physical member lenghts [mm]
self.l = l_true + joint_cost * np.max(l_true) # indipendency from the number of cells [mm]
self.B = B # Equilibrium forces matrix
self.f = f # External forces [N]
self.dofs = dofs
self.s_c = s_c # Max compression allowable stress [MPa]
self.s_t = s_t # Max tension allowable stress [MPa]
self.E = E # Young's modulus [MPa]
self.n_load_cases = f.shape[-1]
# Buckling specific parameters
self.s_buck = s_buck
self.a_cr = -s_c * l_true**2 / self.s_buck # Evaluation of the critical section for the members
# Sparsity pattern of B matrix
self.B_coo = self.B.tocoo()
self.B_row, self.B_column = self.B_coo.row,self.B_coo.col
self.B_T_coo = self.B.T.tocoo() # Horrible but necessary to conserve the very same order of dComp_dU
self.B_T_row, self.B_T_column = self.B_T_coo.row,self.B_T_coo.col
# Create the indexing variables used for splitting the design variables vector x
self.area = slice(0,N)
self.force = []
self.U = []
for p in range(self.n_load_cases):
self.force.append(slice(N+(p)*N,N+(p+1)*N))
self.U.append(slice(N+self.n_load_cases*N+(p)*M,N+self.n_load_cases*N+(p+1)*M))
# History values
self.it = 0
self.obj_hist = []
def objective(self, x):
"""Returns the scalar value of the objective given x."""
self.volume_phys = self.l_true.T @ x[self.area]
return self.l.T @ x[self.area]
def gradient(self, x):
"""Returns the gradient of the objective with respect to x."""
grad = np.zeros(x.size)
grad[self.area] = self.l
return grad
def constraints(self, x):
"""Returns the constraints."""
### Equilibrium (M*p eq)
equilibrium = np.array([])
for p in range(self.n_load_cases):
eq = self.B @ x[self.force[p]] - self.f[:,p]*self.dofs
equilibrium = np.append(equilibrium, eq)
### Stress (2*N*p eq)
stress = np.array([])
for p in range(self.n_load_cases):
stress_c = x[self.force[p]] - self.s_c * x[self.area]
stress = np.append(stress, stress_c)
for p in range(self.n_load_cases):
stress_t = x[self.force[p]] - self.s_t * x[self.area]
stress = np.append(stress, stress_t)
### Compatibility (N*p eq)
compatibility = np.array([])
for p in range(self.n_load_cases):
comp = x[self.area] * self.E/self.l_true * (self.B.T @ x[self.U[p]]) - x[self.force[p]]
compatibility = np.append(compatibility, comp)
### Buckling (N eq)
buckling = np.array([])
for p in range(self.n_load_cases):
buck = x[self.force[p]] + (self.s_buck/self.l_true**2) * x[self.area]**2
buckling = np.append(buckling, buck)
cons = np.concatenate((equilibrium, stress, compatibility, buckling))
return cons
def jacobianstructure(self):
""" Returns the row and column indices for non-zero vales of the
Jacobian. """
# DIM = Number of constraint functions X number of design variables
### Equilibrium (M*p eq)
row, column = self.B_row, self.B_column+self.N # the indexes are translated to the corresponding design variables (force)
for p in range(self.n_load_cases-1):
row = np.append(row,self.B_row+self.M+p*self.M)
column = np.append(column,self.B_column+self.N+(p+1)*self.N)
### Stress (2*N eq)
for p in range(self.n_load_cases):
row = np.append(row,range(self.M*self.n_load_cases+p*self.N, self.M*self.n_load_cases+(p+1)*self.N))
column = np.append(column,np.r_[self.area])
row = np.append(row,range(self.M*self.n_load_cases+p*self.N, self.M*self.n_load_cases+(p+1)*self.N))
column = np.append(column,np.r_[self.force[p]])
for p in range(self.n_load_cases):
row = np.append(row,range(self.M*self.n_load_cases+self.n_load_cases*self.N+p*self.N, self.M*self.n_load_cases+self.n_load_cases*self.N+(p+1)*self.N))
column = np.append(column,np.r_[self.area])
row = np.append(row,range(self.M*self.n_load_cases+self.n_load_cases*self.N+p*self.N, self.M*self.n_load_cases+self.n_load_cases*self.N+(p+1)*self.N))
column = np.append(column,np.r_[self.force[p]])
### Compatibility (N eq)
for p in range(self.n_load_cases):
row = np.append(row,range(self.M*self.n_load_cases+self.N*2*self.n_load_cases+p*self.N, self.M*self.n_load_cases+self.N*2*self.n_load_cases+(p+1)*self.N))
column = np.append(column,np.r_[self.area])
row = np.append(row,range(self.M*self.n_load_cases+self.N*2*self.n_load_cases+p*self.N, self.M*self.n_load_cases+self.N*2*self.n_load_cases+(p+1)*self.N))
column = np.append(column,np.r_[self.force[p]])
row = np.append(row,self.B_T_row + self.M*self.n_load_cases+self.N*2*self.n_load_cases+p*self.N) # B is transposed
column = np.append(column,self.B_T_column+self.N+self.N*self.n_load_cases+p*self.M) # U
### Buckling (N eq)
for p in range(self.n_load_cases):
row = np.append(row,range(self.M*self.n_load_cases+self.N*3*self.n_load_cases+p*self.N, self.M*self.n_load_cases+self.N*3*self.n_load_cases+(p+1)*self.N))
column = np.append(column,np.r_[self.area])
row = np.append(row,range(self.M*self.n_load_cases+self.N*3*self.n_load_cases+p*self.N, self.M*self.n_load_cases+self.N*3*self.n_load_cases+(p+1)*self.N))
column = np.append(column,np.r_[self.force[p]])
return row,column
def jacobian(self, x):
""" Returns the Jacobian of the constraints with respect to x. """
# DIM = Number of constraint functions X number of design variables
### Equilibrium (M eq)
Jacobian = self.B_coo.data
for p in range(self.n_load_cases-1):
Jacobian = np.append(Jacobian,self.B_coo.data)
### Stress (2*N eq)
for p in range(self.n_load_cases):
Jacobian = np.append(Jacobian,np.ones(self.N)*(- self.s_c))
Jacobian = np.append(Jacobian,np.ones(self.N))
for p in range(self.n_load_cases):
Jacobian = np.append(Jacobian,np.ones(self.N)*(- self.s_t))
Jacobian = np.append(Jacobian,np.ones(self.N))
### Compatibility (N eq) x[self.area] * self.E/self.l_true * (self.B.T @ x[self.U]) - x[self.force]
for p in range(self.n_load_cases):
dComp_dA = self.B.T @ x[self.U[p]] * self.E / self.l_true # Diagonal term, i==j
dComp_dU = (sp.diags(x[self.area] * self.E / self.l_true) @ self.B.T) # Multiply the column of B.T and the main diagnonal of diag
dComp_dU.sort_indices() # Needed to match the order of B.T given in jacobianstructure
dComp_dU = dComp_dU.tocoo()
# Area
Jacobian = np.append(Jacobian,dComp_dA)
# Force
Jacobian = np.append(Jacobian,np.ones(self.N)*-1)
# Displacements
Jacobian = np.append(Jacobian,dComp_dU.data)
### Buckling (N eq)
for p in range(self.n_load_cases):
# Area
Jacobian = np.append(Jacobian,2*x[self.area]*self.s_buck/self.l_true**2)
# Force
Jacobian = np.append(Jacobian,np.ones(self.N))
return Jacobian
def hessianstructure(self):
""" Returns the row and column indices for non-zero vales of the
Hessian. NO HESSIAN, NOT WORKING"""
# DIM = Number of design variables X number of design variables
M = self.M
N = self.N
### Compatibility (N eq) x[self.area] * self.E/self.l_true * (self.B.T @ x[self.U]) - x[self.force]
## Area self.B.T @ x[self.U] * self.E / self.l_true
# Area -
# Force -
# Displacements
i, j = np.meshgrid(np.arange(2*N,2*N+M, dtype='int'), np.arange(N, dtype='int'))
### Buckling (N eq) buckling = x[self.force] + (self.s_buck/self.l_true**2) * x[self.area]**2
## Area 2*x[self.area]*self.s_buck/self.l_true**2
# Area
i = np.append(i.ravel(),np.arange(N, dtype='int'))
j = np.append(j.ravel(),np.arange(N, dtype='int'))
return i, j
def hessian(self, x, l, obj_factor):
""" Returns the non-zero values of the Hessian. """
# DIM = Number of design variables X number of design variables
M = self.M
N = self.N
# ID of the constraints
comp_id = slice(M+N*2, M+N*3)
buck_id = slice(M+N*3, M+N*4)
### Objective function (N eq) the hessian of the obj function is empty
# Hessian = obj_factor * NULL
### Equilibrium (M eq) self.B @ x[self.force] - self.f
## Area -
## Force self.B_coo.data
## Displacements -
### Stress (2*N eq)
## Area -
## Force -
## Displacements -
### Compatibility (N eq) x[self.area] * self.E/self.l_true * (self.B.T @ x[self.U]) - x[self.force]
## Area self.B.T @ x[self.U] * self.E / self.l_true
# Area -
# Force -
# Displacements
temp = (sp.diags(l[comp_id] * self.E / self.l_true) @ self.B.T)
temp.sort_indices() # Needed to match the order of B.T given in jacobianstructure
temp = temp.tocoo()
j, i = temp.row, temp.col + (2*N)
data = temp.data
## Force np.ones(self.N)*-1
## Displacements (sp.diags(x[self.area] * self.E / self.l_true) @ self.B.T)
# Area
# Symmetric, already considered
# Force -
# Displacements -