-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminima_mpi.py
1218 lines (1029 loc) · 47.8 KB
/
minima_mpi.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 as sp
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
from matplotlib import colors
import time
from sympy import *
from mpl_toolkits.axes_grid1 import make_axes_locatable
import sympy as sym
from scipy.optimize import minimize
from mpi4py import MPI
from scipy import optimize
import pandas as pd
import sympy as sym
#----------------------------------------general function---------------------------------------------
def magnet_force4(Kd, Rm, tm, x, r, upper_bound=200, interaction='repulsive'):
"""Get magnet force from current config.
PARAMETERS
Magnets parameters:
Kd: 2D arrary [num_edges, 2].
Rm: magnet radius, float (in mm).
tm: magnet thickness, float (in mm).
Orientation parameters:
x: magnets vertical gap (face-to-face distance), float (in mm).
r: magnets horizontal distance (center-to-center distance), float (in mm).
upper_bound: integral upper_bound, if too large, sinh(x) overflow.
RETURN
F_mag: magnet force output (negative magnet force), float (in kN).
"""
sign = 1
if interaction=='repulsive':
sign = -sign
Z_dist = x + tm
cosi = Z_dist/Rm
F_mag = sp.integrate.quad_vec(lambda q: sign*8*np.pi*Kd*(Rm/1000)**2*sp.special.jv(0, (r/1000)*q/(Rm/1000))*(sp.special.jv(1, q))**2/q*np.sinh(q*tao1)*np.sinh(q*tao2)*np.e**(-q*cosi),
0, upper_bound)[0]
return F_mag*0.001
def Force_from_vec(mag_m, n1, n2, r_vec12):
"""Get magnet force from magnets orientation and distance. Follow point dipole approximation, length of
cylindrical magnet much smaller than their distance.
PARAMETERS
Magnets parameters:
Kd: 2D arrary [num_edges, 2].
Rm: magnet radius, float (in mm).
tm: magnet thickness, float (in mm).
Orientation parameters:
n1: m1 dipole moment direction vector, 1D array [3,] (in A*m2).
n2: m2 dipole moment direction vector, 1D array [3,] (in A*m2).
r_vec: distance vector of two magnets, from point dipole 1 to point dipole 2 (pt2_vec - pt1_vej),
1D array [3,] (in mm).
RETURN
F_mag: magnet force output (negative magnet force) on point dipole 2, float (in kN).
"""
r_vec = r_vec12/1000
m1 = mag_m*n1
m2 = mag_m*n2
F_from_vec = -3*miu0/(np.pi*4*np.linalg.norm(r_vec)**5)*(np.inner(m1, r_vec)*m2 + np.inner(m2, r_vec)*m1
+ np.inner(m1, m2)*r_vec - 5*np.inner(m1, r_vec)*np.inner(m2, r_vec)*r_vec/np.linalg.norm(r_vec)**2)
return F_from_vec*0.001
# F_from_vec = -3*miu0/(np.pi*4*np.linalg.norm(r_vec)**5)*(np.inner(m1, r_vec)*m2 + np.inner(m2, r_vec)*m1
# + np.inner(m1, m2)*r_vec - 5*np.inner(m1, r_vec)*np.inner(m2, r_vec)*r_vec/np.linalg.norm(r_vec)**2)
# return F_from_vec*0.001
def get_mag_force(mag_m, mag_points, mag_arrange):
"""Get magnets force output at magnet centers based on position and dipole arrangement.
Assume point dipoles has fixed dipole arrangement. Sliding and overturning are prohibited.
If dipole tilts, need modification (include in-plane magnet-magnet interactions of each polygon).
PARAMETERS
Magnets parameters:
m: magnet moment, float (in A*m2).
mag_points: points of magnetic dipoles, 2D arrary [num_magnets x 3].
mag_arrange: magnetic dipole arrangements (pointing from N to S), 2D arrary [num_magnets x 3].
RETURN
forces: lower level magnets follwed by upper layer's, 2D array, [num_pts, 3] (in kN).
"""
forces = np.zeros([2*n, 3])
for j in range(n,2*n):
force_j = 0
for i in range(n):
force_j = force_j+Force_from_vec(mag_m, mag_arrange[i], mag_arrange[j],
r_vec12=mag_points[j]-mag_points[i])
forces[j] = force_j
for j in range(n):
force_j = 0
for i in range(n, 2*n):
force_j = force_j+Force_from_vec(mag_m, mag_arrange[i], mag_arrange[j],
r_vec12=mag_points[j]-mag_points[i])
forces[j] = force_j
return forces
def get_total_force(o_points, w, mag_m, mag_arrange):
"""Get magnets force output at magnet centers based on position and dipole arrangement.
Assume point dipoles with fixed dipole arrangement. Sliding and overturning are prohibited.
PARAMETERS
o_points: points before updating, 2D arrary [num_pts x 3].
w:[u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
Kd: 2D arrary [num_edges, 2].
Rm: magnet radius, float (in mm).
tm: magnet thickness, float (in mm).
mag_arrange: magnetic dipole arrangements (pointing from N to S), 2D arrary [num_magnets x 3].
RETURN
forces: lower level magnets follwed by upper layer's, 2D array, [num_pts, 3] (in kN).
"""
mag_points = update_pts(o_points, w)
tot_force = get_current_force(o_points, w) + get_mag_force(mag_m, mag_points, mag_arrange)
return tot_force
def upperlayer_total_force(o_points, w, mag_m, mag_arrange):
"""Get upper layer polygon truss and magnetic forces and moments based on reference pts and displcement vector, w.
PARAMETERS
o_points: points before updating, 2D arrary [num_pts x 3].
w:[u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
Kd: 2D arrary [num_edges, 2].
Rm: magnet radius, float (in mm).
tm: magnet thickness, float (in mm).
mag_arrange: magnetic dipole arrangements (pointing from N to S), 2D arrary [num_magnets x 3].
RETURN
Fx: resultant force of the upper layer/ upper layer force output in X-dir, float (in kN).
Fy: resultant force of the upper layer/ upper layer force output in Y-dir, float (in kN).
Fz: resultant force of the upper layer/ upper layer force output in Z-dir, float (in kN).
Mx: resultant moment of the upper layer/ upper layer moment output around pos. X-axis, float (in kN*m).
My: resultant moment of the upper layer/ upper layer moment output around pos. Y-axis, float (in kN*m).
Mz: resultant torque of the upper layer/ upper layer moment output around pos. Z-axis, float (in kN*m).
"""
n_points = update_pts(o_points, w)
Q = get_total_force(o_points, w, mag_m, mag_arrange)
Fx = np.sum(Q[n:,0])
Fy = np.sum(Q[n:,1])
Fz = np.sum(Q[n:,2])
Mx = np.sum(n_points[n:, 1]*Q[n:, 2])*0.001
My = -np.sum(n_points[n:, 0]*Q[n:, 2])*0.001
Mz = np.sum(n_points[n:, 0]*Q[n:, 1])*0.001 - np.sum(n_points[n:, 1]*Q[n:, 0])*0.001
# Mz2 = np.sum(n_points[:n, 0]*Q[:n, 1]) - np.sum(n_points[:n, 1]*Q[:n, 0])*0.001
return np.array([Fx, Fy, Fz, Mx, My, Mz])
def magnet_potential(Z_dist, r=0, upper_bound=200, norm=True):
"""Get magnetostatic interaction energy from current config.
PARAMETERS
Z_dist: magnets center-to-center distance, float (in mm).
upper_bound: integral upper_bound, if too large, sinh(x) overflow.
norm: if turned on, devide the potential by ks*h0**2, boolean.
RETURN
E_mag: magnetostatic interaction energy, float (in J or dimensionless).
"""
cosi = Z_dist/Rm
E_mag = sp.integrate.quad_vec(lambda q: 8*np.pi*Kd*(Rm/1000)**3*sp.special.jv(0, (r/1000)*q/(Rm/1000))*(sp.special.jv(1, q))**2/q**2*np.sinh(q*tao1)*np.sinh(q*tao2)*np.e**(-q*cosi),
0, upper_bound)[0]
if norm:
E_mag = E_mag/(ks*h0**2*0.001)
return E_mag
def Energy_from_vec(mag_m, n1, n2, r_vec12, norm=True):
"""Get magnet force from magnets orientation and distance. Follow point dipole approximation, length of
cylindrical magnet much smaller than their distance.
PARAMETERS
Magnets parameters:
m: magnet moment, float (in A*m2).
Orientation parameters:
n1: m1 dipole moment direction vector, 1D array [3,] (in A*m2).
n2: m2 dipole moment direction vector, 1D array [3,] (in A*m2).
r_vec12: distance vector of two magnets, from point dipole 1 to point dipole 2 (pt2_vec - pt1_vej),
1D array [3,] (in mm).
RETURN
E_from_vec: magnet interaction energy, float (in J or fimensionless).
"""
r_vec = r_vec12/1000
m1 = mag_m*n1
m2 = mag_m*n2
E_from_vec = miu0/(np.pi*4)*(np.inner(m1, m2)/np.linalg.norm(r_vec)**3
- 3*np.inner(m1, r_vec)*np.inner(m2, r_vec)/np.linalg.norm(r_vec)**5)
if norm:
E_from_vec = E_from_vec/(ks*h0**2*0.001)
return E_from_vec
@np.vectorize
def update_pts_vec(u, phi, size1=N, size2=N):
"""Update points positions by phi and u. Assumptions here are the upper layer is a rigid polygon
and points are on the same height. Can add another angle to simulate cases with pts not of the
same height. But the rigid polygon assumption should be strictly kept.
PARAMETERS
o_points: points need updating, 2D arrary [num_pts, 3] (in mm).
w: [u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
RETURN
all_points: 2D array, [num_pts, 3].
"""
points = points_ref
global special_i # vectorize loop has already started from here (u is not in [N,N], u is a scalar)
phi = phi/180*np.pi
rot = np.array([[np.cos(phi), -np.sin(phi), 0],[np.sin(phi), np.cos(phi), 0], [0, 0, 1]])
# print(rot)
points_rot = np.matmul(rot, points.T)
points_rot = points_rot.T
points_rot[:,2] = points_rot[:,2] + u
# print(points)
points_rot[:n] = points[:n].copy()
if special_i<size1*size2:
points_updated_set[special_i] = points_rot
# print(special_i)
special_i += 1
pass
# rotation against z-axis, about the origin
def rotation(ang, points):
"""Get points positions after rotating counter-clockwisely.
PARAMETERS
ang: rotation angle, counter-clockwise positive, float (in degree).
points: positions of points need rotating, 2D arrary [num_pts, 3].
RETURN:
positions of points after rotation, 2D arrary [num_pts, 3].
"""
# ang in degree
ang = ang/180*np.pi
rot = np.array([[np.cos(ang), -np.sin(ang), 0],[np.sin(ang), np.cos(ang), 0], [0, 0, 1]])
points_rot = np.matmul(rot, points.T)
return points_rot.T
# rotation against z-axis, about the origin
@np.vectorize
def rotation_vec(ang, size1=N, size2=N):
"""Get points positions after rotating counter-clockwisely.
PARAMETERS
ang: rotation angle, counter-clockwise positive, float (in degree).
points: positions of points need rotating, 2D arrary [num_pts, 3].
RETURN:
positions of points after rotation, 2D arrary [num_pts, 3].
"""
# ang in degree
points = points_ref
global special_i
ang = ang/180*np.pi
rot = np.array([[np.cos(ang), -np.sin(ang), 0],[np.sin(ang), np.cos(ang), 0], [0, 0, 1]])
points_rot = np.matmul(rot, points.T)
points_rot = points_rot.T
if special_i<size1*size2:
points_rot_set[special_i] = points_rot
# print(special_i)
special_i += 1
pass
def get_adj(connectivity):
"""Get adjacency matrix from connectivity.
PARAMETERS
connectivity: point i and point j of each truss/edge, 2D arrary [num_edges, 2].
RETURN
adj: 2D array, [num_pts, num_pts].
"""
adj = np.zeros([num_pts, num_pts])
for pair in connectivity:
adj[pair[0], pair[1]] = 1
adj[pair[1], pair[0]] = 1
return adj
def get_edge_vector(connectivity, points):
"""Get edge vectors from connectivity and point positions.
PARAMETERS
points: positions of points, 2D arrary [num_pts, 3].
connectivity: point i and point j of each truss/edge, 2D arrary [num_edges, 2].
RETURN
edge_vec: 2D array, [num_edges, 3].
"""
edge_vec = np.zeros([num_edges, 3])
i = 0
for pair in connectivity:
this_edge = points[pair[1]] - points[pair[0]]
edge_vec[i] = this_edge
i += 1
return edge_vec
def update_pts(o_points, w):
"""Update points positions by phi and u. Assumptions here are the upper layer is a rigid polygon
and points are on the same height. Can add another angle to simulate cases with pts not of the
same height. But the rigid polygon assumption should be strictly kept.
PARAMETERS
o_points: points need updating, 2D arrary [num_pts, 3] (in mm).
w: [phi, u]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
RETURN
all_points: 2D array, [num_pts, 3].
"""
phi = w[1]
u = w[0]
base = o_points.copy()[:n]
points = o_points.copy()[n:]
points[:,2] = points[:,2] + np.ones(len(points))*u
points = rotation(phi, points)
all_points = np.vstack([base, points])
return all_points
def get_length(n_points):
"""Get length of each truss element/edge based on current geometry (extension positive).
PARAMETERS
n_points: current positions of points, 2D arrary [num_pts, 3].
RETURN
length: 1D array, [num_edges, ] (in mm).
"""
edge_vec_new = get_edge_vector(connectivity, n_points)
length = np.linalg.norm(edge_vec_new, axis=1)
return length
def get_Qx(n_points):
"""Get basic forces of each truss element/edge based on current geometry (tension positive).
Here, use rotated engineering deformation (RE, Ln-L). Nonlinear kinematics is considered.
PARAMETERS
n_points: current positions of points, 2D arrary [num_pts, 3].
RETURN
Qx: 1D array, [num_edges, ] (in kN).
"""
length0 = np.linalg.norm(edge_vec0, axis=1)
edge_vec_new = get_edge_vector(connectivity, n_points)
length = np.linalg.norm(edge_vec_new, axis=1)
Qx = (length - length0)*ks*0.001
Qx = Qx.reshape([num_edges, 1])
return Qx
def get_Bx(n_points):
"""Get force influence matrix B based on current geometry. Nonliner statics is considered.
PARAMETERS
n_points: current positions of points, 2D arrary [num_pts, 3].
RETUTN
Bx: 2D array, [num_pts*3, num_edges].
"""
temp = np.linalg.norm(get_edge_vector(connectivity, n_points), axis=1)
b_vec = get_edge_vector(connectivity, n_points)/temp.reshape([len(temp),1])
b_vec = np.hstack([-b_vec, b_vec])
dof_id = np.array(range(num_pts*3)).reshape([num_pts, 3])
id_vec = np.zeros([num_edges, 3*2])
id_vec = id_vec.astype(int)
for bar in range(num_edges):
id_vec[bar] = np.hstack([dof_id[connectivity[bar][0]], dof_id[connectivity[bar][1]]])
Bx = np.zeros([num_edges, num_pts*3])
for ele in range(num_edges):
Bx[ele][id_vec[ele]] = b_vec[ele]
Bx = Bx.T
return Bx
def get_current_force(o_points, w):
"""Get truss forces at DOFs based on current geometry (positive force in positive direction).
Current geometry is obtained from old positions and displacements. Nonlinear statics is considered.
PARAMETERS
o_points: points before updating, 2D arrary [num_pts x 3].
w:[u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
RETURN
Q: 2D array, [num_pts, 3] (in kN).
"""
n_points = update_pts(o_points, w)
Qx = get_Qx(n_points)
Bx = get_Bx(n_points)
Q = np.matmul(Bx, Qx)
Q = Q.reshape([num_pts, 3])
return Q
def upperlayer_force(o_points, w):
"""Get upper layer polygon truss forces and moments based on reference pts and displcement vector, w.
PARAMETERS
o_points: points before updating, 2D arrary [num_pts x 3].
w:[u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
RETURN
Fx: resultant force of the upper layer/ upper layer force output in X-dir, float (in kN).
Fy: resultant force of the upper layer/ upper layer force output in Y-dir, float (in kN).
Fz: resultant force of the upper layer/ upper layer force output in Z-dir, float (in kN).
Mx: resultant moment of the upper layer/ upper layer moment output around pos. X-axis, float (in kN*m).
My: resultant moment of the upper layer/ upper layer moment output around pos. Y-axis, float (in kN*m).
Mz: resultant torque of the upper layer/ upper layer moment output around pos. Z-axis, float (in kN*m).
"""
n_points = update_pts(o_points, w)
Q = get_current_force(o_points, w)
Fx = np.sum(Q[n:,0])
Fy = np.sum(Q[n:,1])
Fz = np.sum(Q[n:,2])
Mx = np.sum(n_points[n:, 1]*Q[n:, 2])*0.001
My = -np.sum(n_points[n:, 0]*Q[n:, 2])*0.001
Mz = np.sum(n_points[n:, 0]*Q[n:, 1])*0.001 - np.sum(n_points[n:, 1]*Q[n:, 0])*0.001
# Mz2 = np.sum(n_points[:n, 0]*Q[:n, 1]) - np.sum(n_points[:n, 1]*Q[:n, 0])*0.001
return np.array([Fx, Fy, Fz, Mx, My, Mz])
def get_R(Q):
"""Get reaction force from current force. Current force Q current structure response.
First n entries are lower layer polygon points (fixed), then n entries for upper layer points.
PARAMETERS
Q: current force, 2D array [num_pts, 3].
RETURN
R: reaction force, 1D array [3,].
"""
R = sum(Q[:n])
return R
def get_P(Q):
"""Get applied force from current force. Current force Q is current structure response.
First n entries are lower layer polygon points (fixed), then n entries for upper layer points.
PARAMETERS
Q: current force, 2D array [num_pts, 3].
RETURN
P: applied force, 1D array [3,].
"""
P = sum(Q[n:])
return P
def elastic_U(w):
"""Get truss normalized elastic energy based on current geometry.
Analytical expression for truss length is used. [FAST]
Invalid if introducing out-of-plane rotation angle.
PARAMETERS
w:[u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, 'upward' positive, float (in mm).
RETUTN
U_norm: float (dimensionless).
"""
u0, phi0 = [0, 0]
a0 = np.sqrt((h0+u0)**2+4*R0**2*(np.sin(phi0/2/180*np.pi+theta1/2/180*np.pi-np.pi/2/n))**2)
b0 = np.sqrt((h0+u0)**2+4*R0**2*(np.sin(phi0/2/180*np.pi+theta1/2/180*np.pi+np.pi/2/n))**2)
u, phi = w
a = np.sqrt((h0+u)**2+4*R0**2*(np.sin(phi/2/180*np.pi+theta1/2/180*np.pi-np.pi/2/n))**2) # mm
b = np.sqrt((h0+u)**2+4*R0**2*(np.sin(phi/2/180*np.pi+theta1/2/180*np.pi+np.pi/2/n))**2) # mm
U = 0.5*n*ks*(a-a0)**2 + 0.5*n*ks*(b-b0)**2 # in kN/m*mm2 (or 1e-3J)
U_norm = U/ks/h0**2 # dimensionless
return U_norm
def Energy_from_vec(mag_m, n1, n2, r_vec12, norm=True):
"""Get magnet force from magnets orientation and distance. Follow point dipole approximation, length of
cylindrical magnet much smaller than their distance.
PARAMETERS
Magnets parameters:
m: magnet moment, float (in A*m2).
Orientation parameters:
n1: m1 dipole moment direction vector, 1D array [3,] (in A*m2).
n2: m2 dipole moment direction vector, 1D array [3,] (in A*m2).
r_vec12: distance vector of two magnets, from point dipole 1 to point dipole 2 (pt2_vec - pt1_vej),
1D array [3,] (in mm).
RETURN
E_from_vec: magnet interaction energy, float (in J or fimensionless).
"""
r_vec = r_vec12/1000
m1 = mag_m*n1
m2 = mag_m*n2
E_from_vec = miu0/(np.pi*4)*(np.inner(m1, m2)/np.linalg.norm(r_vec)**3
- 3*np.inner(m1, r_vec)*np.inner(m2, r_vec)/np.linalg.norm(r_vec)**5)
if norm:
E_from_vec = E_from_vec/(ks*h0**2*0.001)
return E_from_vec
def this_magent_energy(mag_m, mag_points, mag_arrange, norm=True):
"""Calculate normalized total potential energy from current config.
PARAMETERS
w:[u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
upper_bound: integral upper_bound, if too large, sinh(x) overflow.
RETUTN
total: float (dimensionless).
"""
temp = 0
for i in range(n):
for j in range(n,2*n):
temp = temp + Energy_from_vec(mag_m, mag_arrange[i], mag_arrange[j], r_vec12=mag_points[j]-mag_points[i], norm=norm)
return temp
def magnet_potential(o_points, w, mag_m, mag_arrange):
"""Calculate normalized total potential energy from current config.
PARAMETERS
w:[u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
upper_bound: integral upper_bound, if too large, sinh(x) overflow.
RETUTN
total: float (dimensionless).
"""
mag_points = update_pts(o_points, w)
magnet_E = this_magent_energy(mag_m, mag_points, mag_arrange, norm=True)
return magnet_E
def total_potential(o_points, w, mag_m, mag_arrange):
"""Calculate normalized total potential energy from current config.
PARAMETERS
w:[u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
upper_bound: integral upper_bound, if too large, sinh(x) overflow.
RETUTN
total: float (dimensionless).
"""
mag_points = update_pts(o_points, w)
total = elastic_U(w) + this_magent_energy(mag_m, mag_points, mag_arrange, norm=True)
return total
def magnet_potential_vec(u, phi, size1=N, size2=N, norm=True):
"""Calculate normalized total potential energy from current config.
PARAMETERS
w:[u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
upper_bound: integral upper_bound, if too large, sinh(x) overflow.
RETUTN
total: float (dimensionless).
"""
global special_i
global points_updated_set
special_i = -1
points_updated_set = np.zeros([size1*size2, 2*n, 3])
void = update_pts_vec(u, phi, size1, size2)
temp = np.zeros(size1*size2)
for ii in range(n):
for jj in range(n,2*n):
r_vec12 = points_updated_set[:, jj]-points_updated_set[:, ii]
r_vec12 = r_vec12/1000
m1 = mag_m*mag_arrange[ii]
m2 = mag_m*mag_arrange[jj]
this = miu0/(np.pi*4)*(np.inner(m1, m2)/np.linalg.norm(r_vec12, axis=1)**3
- 3*np.matmul(r_vec12,m1)*np.matmul(r_vec12,m2)/np.linalg.norm(r_vec12, axis=1)**5)
temp = temp + this/(ks*h0**2*0.001)
# print(points_updated_set[:,ii])
# print(points_updated_set[:,jj])
# print(r_vec12)
# print(this)
# print(temp)
# print('\n')
temp = temp.reshape([size1, size2])
# total = elastic_U([u, phi]) + temp
return temp
def total_potential_vec(u, phi, size1=N, size2=N, norm=True):
"""Calculate normalized total potential energy from current config.
PARAMETERS
w:[u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
upper_bound: integral upper_bound, if too large, sinh(x) overflow.
RETUTN
total: float (dimensionless).
"""
global special_i
global points_updated_set
special_i = -1
points_updated_set = np.zeros([size1*size2, 2*n, 3])
void = update_pts_vec(u, phi, size1, size2)
temp = np.zeros(size1*size2)
for ii in range(n):
for jj in range(n,2*n):
r_vec12 = points_updated_set[:, jj]-points_updated_set[:, ii]
r_vec12 = r_vec12/1000
m1 = mag_m*mag_arrange[ii]
m2 = mag_m*mag_arrange[jj]
this = miu0/(np.pi*4)*(np.inner(m1, m2)/np.linalg.norm(r_vec12, axis=1)**3
- 3*np.matmul(r_vec12,m1)*np.matmul(r_vec12,m2)/np.linalg.norm(r_vec12, axis=1)**5)
temp = temp + this/(ks*h0**2*0.001)
temp = temp.reshape([size1, size2])
total = elastic_U([u, phi]) + temp
return total
def magnet_Zforce_vec(u, phi, size1=N, size2=N):
"""Calculate normalized total potential energy from current config.
PARAMETERS
w:[u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
upper_bound: integral upper_bound, if too large, sinh(x) overflow.
RETUTN
total: float (dimensionless).
"""
global special_i
global points_updated_set
special_i = -1
points_updated_set = np.zeros([size1*size2, 2*n, 3])
void = update_pts_vec(u, phi, size1, size2)
temp = np.zeros(size1*size2)
for jj in range(n,2*n):
for ii in range(n):
r_vec = points_updated_set[:, jj]-points_updated_set[:, ii]
r_vec = r_vec/1000
m1 = mag_m*mag_arrange[ii]
m2 = mag_m*mag_arrange[jj]
this = -3*miu0/(np.pi*4*np.linalg.norm(r_vec, axis=1)**5)*(np.inner(m1, r_vec)*m2[2] + np.inner(m2, r_vec)*m1[2] + np.inner(m1, m2)*r_vec[:,2]
- 5*np.inner(m1, r_vec)*np.inner(m2, r_vec)*r_vec[:,2]/np.linalg.norm(r_vec,axis=1)**2)
temp = temp + this*0.001
temp = temp.reshape([size1, size2])
# total = elastic_U([u, phi]) + temp
return temp
def magnet_torque_vec(u, phi, size1=N, size2=N):
"""Calculate normalized total potential energy from current config.
PARAMETERS
w:[u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
upper_bound: integral upper_bound, if too large, sinh(x) overflow.
RETUTN
total: float (dimensionless).
"""
global special_i
global points_updated_set
special_i = -1
points_updated_set = np.zeros([size1*size2, 2*n, 3])
void = update_pts_vec(u, phi, size1, size2)
arm1 = np.zeros(size1*size2)
arm2 = np.zeros(size1*size2)
for jj in range(n, 2*n):
temp1 = np.zeros(size1*size2)
temp2 = np.zeros(size1*size2)
for ii in range(n):
r_vec = points_updated_set[:, jj]-points_updated_set[:, ii]
r_vec = r_vec/1000
m1 = mag_m*mag_arrange[ii]
m2 = mag_m*mag_arrange[jj]
this1 = -3*miu0/(np.pi*4*np.linalg.norm(r_vec, axis=1)**5)*(np.inner(m1, r_vec)*m2[0] + np.inner(m2, r_vec)*m1[0] + np.inner(m1, m2)*r_vec[:,0]
- 5*np.inner(m1, r_vec)*np.inner(m2, r_vec)*r_vec[:,0]/np.linalg.norm(r_vec,axis=1)**2)
this2 = -3*miu0/(np.pi*4*np.linalg.norm(r_vec, axis=1)**5)*(np.inner(m1, r_vec)*m2[1] + np.inner(m2, r_vec)*m1[1] + np.inner(m1, m2)*r_vec[:,1]
- 5*np.inner(m1, r_vec)*np.inner(m2, r_vec)*r_vec[:,1]/np.linalg.norm(r_vec,axis=1)**2)
temp1 = temp1 + this1*0.001
temp2 = temp2 + this2*0.001
arm1 = arm1 + temp1*points_updated_set[:, jj][:,1]*0.001
arm2 = arm2 + temp2*points_updated_set[:, jj][:,0]*0.001
torque = (arm2 - arm1).reshape([size1, size2])
# total = elastic_U([u, phi]) + temp
return torque
def truss_Zforce_vec(u, phi, size1=N, size2=N):
"""Calculate normalized total potential energy from current config.
PARAMETERS
w:[u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
upper_bound: integral upper_bound, if too large, sinh(x) overflow.
RETUTN
total: float (dimensionless).
"""
global special_i
global points_updated_set
special_i = -1
points_updated_set = np.zeros([size1*size2, 2*n, 3])
void = update_pts_vec(u, phi, size1, size2)
# truss force
temp = np.zeros(size1*size2)
for conn in connectivity:
conn_vec = points_updated_set[:, conn[1]] - points_updated_set[:, conn[0]]
current_len = np.linalg.norm(conn_vec, axis=1)
elong = current_len - np.linalg.norm(points_ref[conn[1]]-points_ref[conn[0]])
ele_force = elong * ks
ele_force = ele_force.reshape([size1*size2,1]) * conn_vec/current_len.reshape([size1*size2,1])
temp = temp + ele_force[:,2]*0.001
temp = temp.reshape([size1, size2])
return temp # kN
def truss_torque_vec(u, phi, size1=N, size2=N):
"""Calculate normalized total potential energy from current config.
PARAMETERS
w:[u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
upper_bound: integral upper_bound, if too large, sinh(x) overflow.
RETUTN
total: float (dimensionless).
"""
global special_i
global points_updated_set
special_i = -1
points_updated_set = np.zeros([size1*size2, 2*n, 3])
void = update_pts_vec(u, phi, size1, size2)
# truss torque
torque2 = np.zeros(size1*size2)
for conn in connectivity:
conn_vec = points_updated_set[:, conn[1]] - points_updated_set[:, conn[0]]
current_len = np.linalg.norm(conn_vec, axis=1)
elong = current_len - np.linalg.norm(points_ref[conn[1]]-points_ref[conn[0]])
ele_force = elong * ks
ele_force = ele_force.reshape([size1*size2,1]) * conn_vec/current_len.reshape([size1*size2,1])
torque2 = torque2 + ele_force[:,1]*0.001*points_updated_set[:, conn[1]][:, 0]*0.001 - ele_force[:,0]*0.001*points_updated_set[:, conn[1]][:, 1]*0.001
torque2 = torque2.reshape([size1, size2])
return torque2 # kN*m
def total_Zforce_vec(u, phi, size1=N, size2=N):
"""Calculate normalized total potential energy from current config.
PARAMETERS
w:[u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
upper_bound: integral upper_bound, if too large, sinh(x) overflow.
RETUTN
total: float (dimensionless).
"""
global special_i
global points_updated_set
special_i = -1
points_updated_set = np.zeros([size1*size2, 2*n, 3])
void = update_pts_vec(u, phi, size1, size2)
# magnet force
temp = np.zeros(size1*size2)
for jj in range(n,2*n):
for ii in range(n):
r_vec = points_updated_set[:, jj]-points_updated_set[:, ii]
r_vec = r_vec/1000
m1 = mag_m*mag_arrange[ii]
m2 = mag_m*mag_arrange[jj]
this = -3*miu0/(np.pi*4*np.linalg.norm(r_vec, axis=1)**5)*(np.inner(m1, r_vec)*m2[2] + np.inner(m2, r_vec)*m1[2] + np.inner(m1, m2)*r_vec[:,2]
- 5*np.inner(m1, r_vec)*np.inner(m2, r_vec)*r_vec[:,2]/np.linalg.norm(r_vec,axis=1)**2)
temp = temp + this*0.001
# truss force
for conn in connectivity:
conn_vec = points_updated_set[:, conn[1]] - points_updated_set[:, conn[0]]
current_len = np.linalg.norm(conn_vec, axis=1)
elong = current_len - np.linalg.norm(points_ref[conn[1]]-points_ref[conn[0]])
ele_force = elong * ks
ele_force = ele_force.reshape([size1*size2,1]) * conn_vec/current_len.reshape([size1*size2,1])
temp = temp + ele_force[:,2]*0.001
temp = temp.reshape([size1, size2])
return temp # kN
def total_torque_vec(u, phi, size1=N, size2=N):
"""Calculate normalized total potential energy from current config.
PARAMETERS
w:[u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
upper_bound: integral upper_bound, if too large, sinh(x) overflow.
RETUTN
total: float (dimensionless).
"""
global special_i
global points_updated_set
special_i = -1
points_updated_set = np.zeros([size1*size2, 2*n, 3])
void = update_pts_vec(u, phi, size1, size2)
part1 = np.zeros(size1*size2)
part2 = np.zeros(size1*size2)
# magnet torque
for jj in range(n, 2*n):
temp1 = np.zeros(size1*size2)
temp2 = np.zeros(size1*size2)
for ii in range(n):
r_vec = points_updated_set[:, jj]-points_updated_set[:, ii]
r_vec = r_vec/1000
m1 = mag_m*mag_arrange[ii]
m2 = mag_m*mag_arrange[jj]
this1 = -3*miu0/(np.pi*4*np.linalg.norm(r_vec, axis=1)**5)*(np.inner(m1, r_vec)*m2[0] + np.inner(m2, r_vec)*m1[0] + np.inner(m1, m2)*r_vec[:,0]
- 5*np.inner(m1, r_vec)*np.inner(m2, r_vec)*r_vec[:,0]/np.linalg.norm(r_vec,axis=1)**2)
this2 = -3*miu0/(np.pi*4*np.linalg.norm(r_vec, axis=1)**5)*(np.inner(m1, r_vec)*m2[1] + np.inner(m2, r_vec)*m1[1] + np.inner(m1, m2)*r_vec[:,1]
- 5*np.inner(m1, r_vec)*np.inner(m2, r_vec)*r_vec[:,1]/np.linalg.norm(r_vec,axis=1)**2)
temp1 = temp1 + this1*0.001
temp2 = temp2 + this2*0.001
part1 = part1 + temp1*points_updated_set[:, jj][:,1]*0.001
part2 = part2 + temp2*points_updated_set[:, jj][:,0]*0.001
torque1 = (part2 - part1).reshape([size1, size2])
# truss torque
torque2 = np.zeros(size1*size2)
for conn in connectivity:
conn_vec = points_updated_set[:, conn[1]] - points_updated_set[:, conn[0]]
current_len = np.linalg.norm(conn_vec, axis=1)
elong = current_len - np.linalg.norm(points_ref[conn[1]]-points_ref[conn[0]])
ele_force = elong * ks
ele_force = ele_force.reshape([size1*size2,1]) * conn_vec/current_len.reshape([size1*size2,1])
torque2 = torque2 + ele_force[:,1]*0.001*points_updated_set[:, conn[1]][:, 0]*0.001 - ele_force[:,0]*0.001*points_updated_set[:, conn[1]][:, 1]*0.001
torque2 = torque2.reshape([size1, size2])
return torque1 + torque2 # kN*m
def elastic_U_sym(w):
"""Get truss normalized elastic energy based on current geometry.
Analytical expression for truss length is used. [FAST]
Invalid if introducing out-of-plane rotation angle.
PARAMETERS
w:[u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, 'downward' positive, float (in mm).
RETUTN
U_norm: float (dimensionless).
"""
u0, phi0 = [0, 0]
a0 = sqrt((h0+u0)**2+4*R0**2*(sin(phi0/2/180*np.pi+theta1/2/180*np.pi-np.pi/2/n))**2)
b0 = sqrt((h0+u0)**2+4*R0**2*(sin(phi0/2/180*np.pi+theta1/2/180*np.pi+np.pi/2/n))**2)
u, phi = w
a = sqrt((h0+u)**2+4*R0**2*(sin(phi/2/180*np.pi+theta1/2/180*np.pi-np.pi/2/n))**2) # mm
b = sqrt((h0+u)**2+4*R0**2*(sin(phi/2/180*np.pi+theta1/2/180*np.pi+np.pi/2/n))**2) # mm
U = 0.5*n*ks*(a-a0)**2 + 0.5*n*ks*(b-b0)**2 # in kN/m*mm2 (or μJ)
U_norm = U/ks/h0**2 # dimensionless
return U_norm
def rotation_sym(ang, points):
"""Get points positions after rotating counter-clockwisely.
PARAMETERS
ang: rotation angle, counter-clockwise positive, float (in degree).
points: positions of points need rotating, 2D arrary [num_pts, 3].
RETURN:
positions of points after rotation, 2D arrary [num_pts, 3].
"""
# ang in degree
ang = ang/180*np.pi
rot = np.array([[cos(ang), -sin(ang), 0],[sin(ang), cos(ang), 0], [0, 0, 1]])
points_rot = rot @ points.T
return points_rot.T
def update_pts_sym(o_points, w):
"""Update points positions by phi and u. Assumptions here are the upper layer is a rigid polygon
and points are on the same height. Can add another angle to simulate cases with pts not of the
same height. But the rigid polygon assumption should be strictly kept.
PARAMETERS
o_points: points need updating, 2D arrary [num_pts, 3] (in mm).
w: [phi, u]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
RETURN
all_points: 2D array, [num_pts, 3].
"""
phi = w[1]
u = w[0]
base = o_points.copy()[:n]
points = o_points.copy()[n:]
points = np.hstack([points[:,0], points[:,1], np.ones(len(points))*Uz + points[:,2]]).reshape([3,n]).T
points = rotation_sym(phi, points)
all_points = np.vstack([base, points])
return all_points
def Energy_from_vec_sym(mag_m, n1, n2, r_vec12, norm=True):
"""Get magnet force from magnets orientation and distance. Follow point dipole approximation, length of
cylindrical magnet much smaller than their distance.
PARAMETERS
Magnets parameters:
m: magnet moment, float (in A*m2).
Orientation parameters:
n1: m1 dipole moment direction vector, 1D array [3,] (in A*m2).
n2: m2 dipole moment direction vector, 1D array [3,] (in A*m2).
r_vec12: distance vector of two magnets, from point dipole 1 to point dipole 2 (pt2_vec - pt1_vej),
1D array [3,] (in mm).
RETURN
E_from_vec: magnet interaction energy, float (in J or fimensionless).
"""
r_vec = r_vec12/1000
m1 = mag_m*n1
m2 = mag_m*n2
r_vec_norm = sqrt(r_vec[0]**2 + r_vec[1]**2 + r_vec[2]**2)
E_from_vec = miu0/(np.pi*4)*(np.inner(m1, m2)/r_vec_norm**3
- 3*np.inner(m1, r_vec)*np.inner(m2, r_vec)/r_vec_norm**5)
if norm:
E_from_vec = E_from_vec/(ks*h0**2*0.001)
return E_from_vec
def this_magent_energy_sym(mag_m, mag_points, mag_arrange, norm=True):
"""Calculate normalized total potential energy from current config.
PARAMETERS
w:[u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
upper_bound: integral upper_bound, if too large, sinh(x) overflow.
RETUTN
total: float (dimensionless).
"""
temp = 0
for i in range(n):
for j in range(n,2*n):
temp = temp + Energy_from_vec_sym(mag_m, mag_arrange[i], mag_arrange[j], r_vec12=mag_points[j]-mag_points[i], norm=norm)
return temp
def total_potential_sym(o_points, w, mag_m, mag_arrange):
"""Calculate normalized total potential energy from current config.
PARAMETERS
w:[u, phi]
phi: twisting angle, counter-clockwise positive, float (in degree).
u: vertival displacement, upward positive, float (in mm).
upper_bound: integral upper_bound, if too large, sinh(x) overflow.
RETUTN
total: float (dimensionless).
"""
mag_points = update_pts_sym(o_points, w)
total = elastic_U_sym(w) + this_magent_energy_sym(mag_m, mag_points, mag_arrange, norm=True)
return total