-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rigidity.m
2296 lines (2024 loc) · 84.6 KB
/
Rigidity.m
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
(* ::Package:: *)
(* ::Section:: *)
(*Rigidity and stuff*)
(*Bryan Gin-ge Chen, August 2014*)
BeginPackage["Rigidity`"]
Begin["Global`"]
(* ::Section:: *)
(*Internal utilities*)
getatomindex[m_,n_,ind_,xx_,yy_,size_]:=(size yy(m-1)+size (n-1)+ind)
getatomindex2[cellcoords_,ind_,cover_,size_]:=
Module[{i,dim=Length[cellcoords],increment=1,cover2=Join[cover,{size}]},
ind+
Sum[increment=increment*cover2[[-i]];
increment*(cellcoords[[-i]]-1)
,{i,dim}]];
Doublemat[tab_]:=Module[{L=Length[tab],j},Flatten[Table[{2tab[[j]]-1,2tab[[j]]},{j,L}]]];
PosVecIndices[tab_,d_]:=Module[{L=Length[tab],j,k},Flatten[Table[Table[d tab[[j]]-d+k,{k,d}],{j,L}]]];
(* Compute intersection "times" between two lines moving at unit speed, presented as point, angle *)
computeintersection[p1_,th1_,p3_,th3_]:=
Module[{u1,u2,p2=p1+{Cos[th1],Sin[th1]},p4=p3+{Cos[th3],Sin[th3]}},
u1=((p4[[1]]-p3[[1]])(p1[[2]]-p3[[2]])-(p4[[2]]-p3[[2]])(p1[[1]]-p3[[1]]))/((p4[[2]]-p3[[2]])(p2[[1]]-p1[[1]])-(p4[[1]]-p3[[1]])(p2[[2]]-p1[[2]]));
u2=((p2[[1]]-p1[[1]])(p1[[2]]-p3[[2]])-(p2[[2]]-p1[[2]])(p1[[1]]-p3[[1]]))/((p4[[2]]-p3[[2]])(p2[[1]]-p1[[1]])-(p4[[1]]-p3[[1]])(p2[[2]]-p1[[2]]));
(*pos=p1+u1{Cos[th1],Sin[th1]};*)
{u1,u2(*,pos*)}
];
(* Compute intersection point between two lines moving at unit speed, presented as point, angle *)
computeintersectionpos[p1_,th1_,p3_,th3_]:=
Module[{u1,u2,p2=p1+{Cos[th1],Sin[th1]},p4=p3+{Cos[th3],Sin[th3]},pos},
u1=((p4[[1]]-p3[[1]])(p1[[2]]-p3[[2]])-(p4[[2]]-p3[[2]])(p1[[1]]-p3[[1]]))/((p4[[2]]-p3[[2]])(p2[[1]]-p1[[1]])-(p4[[1]]-p3[[1]])(p2[[2]]-p1[[2]]));
u2=((p2[[1]]-p1[[1]])(p1[[2]]-p3[[2]])-(p2[[2]]-p1[[2]])(p1[[1]]-p3[[1]]))/((p4[[2]]-p3[[2]])(p2[[1]]-p1[[1]])-(p4[[1]]-p3[[1]])(p2[[2]]-p1[[2]]));
pos=p1+u1{Cos[th1],Sin[th1]};
pos
];
(* Smith Normal Form code from Daniel Lichtblau
http://mathematica.stackexchange.com/a/40930
*)
diagonalQ[mat_?MatrixQ]:=With[{posns=Flatten[Map[Position[#,_?(#!=0&)]&,mat]]},
Length[Union[posns]]==Length[posns]]
diagonalize[mat_?MatrixQ]:=Module[{hnf=mat,umat=IdentityMatrix[Length[mat]],
vmat=IdentityMatrix[Length[mat[[1]]]],tmpu,tmpv},
While[Not[diagonalQ[hnf]],{tmpu,hnf}=HermiteDecomposition[hnf];
umat=tmpu.umat;
{tmpv,hnf}=HermiteDecomposition[Transpose[hnf]];
vmat=vmat.Transpose[tmpv];
hnf=Transpose[hnf];];
{umat,hnf,vmat}]
divides[a_,b_]:=GCD[a,b]===a
smithNormalForm[mat_?MatrixQ]:=Module[{uu,dd,vv,diags,gcd,col=0,dim,tmpu,tmpv},
{uu,dd,vv}=diagonalize[mat];
diags=Select[Flatten[dd],#!=0&];
dim=Length[diags];
While[col+1<dim,col++;
If[divides[diags[[col]],GCD[Apply[Sequence,Drop[diags,col]]]],Continue[]];
vv=Transpose[vv];
Do[dd[[j,col]]=diags[[j]];
vv[[col]]+=vv[[j]],{j,col+1,dim}];
vv=Transpose[vv];
{tmpu,dd,tmpv}=diagonalize[dd];
uu=tmpu.uu;
vv=vv.tmpv;
diags=Select[Flatten[dd],#!=0&];];
{uu,dd,vv}]
(* ::Section:: *)
(*Rigidity Matrix Functions*)
(* Fixed lattice matrix code *)
(* if fixedperiodic is true how do we deal with z's?? *)
(* convention for "lattice" columns agrees with papers *)
RigidityMatrix[z_,posns_,basis_,edgedat_,fixedperiodic_:False]:=
Module[{numbonds=Length[edgedat],qdim=Length[z],dim=Length[posns[[1]]],bcomponent,bvec,
numpart=Length[posns],part1,part2,ebond,kb,lattchange,zm,edatExtend,k,j},
SparseArray[Flatten[Table[part1=edgedat[[j,1,1]];
part2=edgedat[[j,1,2]];
kb=1;(*edgedat[[j,3]];*)
edatExtend=Join[edgedat[[j,2,1;;Min[Length[edgedat[[j,2]]],qdim]]],
Table[0,{qdim-Length[edgedat[[j,2]]]}]];
lattchange=If[qdim>0,edatExtend.basis,0];
zm=Product[z[[k]]^edatExtend[[k]],{k,qdim}];
ebond=-posns[[part1]]+(posns[[part2]]+lattchange); (* sign convention from malestein theran *)
(* ebond=ebond/Norm[ebond];*)
If[part1!=part2,(Join[
Table[{j,dim (part1-1)+k}->-ebond[[k]],{k,dim}],
Table[{j,dim (part2-1)+k}->ebond[[k]] zm,{k,dim}],
If[fixedperiodic,Flatten[
Table[{j,dim numpart+(bvec-1) dim+bcomponent}->edatExtend[[bvec]] (ebond[[bcomponent]])
,{bvec,qdim},{bcomponent,dim}]]
,{}]
]),
(* part1\[Equal]part2*)
Join[Table[{j,dim (part1-1)+k}->-ebond[[k]](1-zm),{k,dim}],
If[fixedperiodic,Flatten[
Table[{j,dim numpart+(bvec-1) dim+bcomponent}->edatExtend[[bvec]](ebond[[bcomponent]])
,{bvec,qdim},{bcomponent,dim}]]
,{}]]
],
{j,numbonds}]]
,{numbonds,dim numpart+If[fixedperiodic,qdim*dim,0]}]
];
CompatibilityMatrix[z_,posns_,basis_,edgedat_,fixedperiodic_:False]:=
Module[{numbonds=Length[edgedat],qdim=Length[z],dim=Length[posns[[1]]],bcomponent,bvec,
numpart=Length[posns],part1,part2,ebond,lattchange,zm,edatExtend,k,j},
SparseArray[Flatten[Table[part1=edgedat[[j,1,1]];
part2=edgedat[[j,1,2]];
edatExtend=Join[edgedat[[j,2,1;;Min[Length[edgedat[[j,2]]],qdim]]],
Table[0,{qdim-Length[edgedat[[j,2]]]}]];
lattchange=If[qdim>0,edatExtend.basis,0];
zm=Product[z[[k]]^edatExtend[[k]],{k,qdim}];
ebond=-posns[[part1]]+(posns[[part2]]+lattchange); (* sign convention from malestein theran *)
(* unit vectors *)
ebond=ebond/Norm[ebond];
If[part1!=part2,(Join[
Table[{j,dim (part1-1)+k}->-ebond[[k]],{k,dim}],
Table[{j,dim (part2-1)+k}->ebond[[k]] zm,{k,dim}],
If[fixedperiodic,Flatten[
Table[{j,dim numpart+(bvec-1) dim+bcomponent}->edatExtend[[bvec]] (ebond[[bcomponent]])
,{bvec,qdim},{bcomponent,dim}]]
,{}]
]),
(* part1\[Equal]part2*)
Join[Table[{j,dim (part1-1)+k}->-ebond[[k]](1-zm),{k,dim}],
If[fixedperiodic,Flatten[
Table[{j,dim numpart+(bvec-1) dim+bcomponent}->edatExtend[[bvec]](ebond[[bcomponent]])
,{bvec,qdim},{bcomponent,dim}]]
,{}]]
],
{j,numbonds}]]
,{numbonds,dim numpart+If[fixedperiodic,qdim*dim,0]}]
];
NRigidityMatrix[z_?(VectorQ[#,NumericQ]&),posns_,basis_,edgedat_,fixedperiodic_:False]:=
RigidityMatrix[z,posns,basis,edgedat,fixedperiodic];
NCompatibilityMatrix[z_?(VectorQ[#,NumericQ]&),posns_,basis_,edgedat_,fixedperiodic_:False]:=
CompatibilityMatrix[z,posns,basis,edgedat,fixedperiodic];
IncidenceMat[z_,edgedat_,dim_,fixedperiodic_:False]:=
Module[{numbonds=Length[edgedat],qdim=Length[z],bvec,
numpart=Max[edgedat[[All,1]]],part1,part2,zm,edatExtend,k,j},
ArrayFlatten[Table[part1=edgedat[[j,1,1]];
part2=edgedat[[j,1,2]];
edatExtend=Join[edgedat[[j,2,1;;Min[Length[edgedat[[j,2]]],qdim]]],
Table[0,{qdim-Length[edgedat[[j,2]]]}]];
zm=Product[z[[k]]^edatExtend[[k]],{k,qdim}];
If[part1!=part2,Join[Table[
If[k==part1,-IdentityMatrix[dim],
If[k==part2, zm IdentityMatrix[dim],0]],{k,numpart}],
If[fixedperiodic,
Table[edatExtend[[bvec]]IdentityMatrix[dim],{bvec,qdim}],{}]
],
(* part1\[Equal]part2*)
Join[Table[If[k==part1,-(1-zm)IdentityMatrix[dim],0],{k,numpart}],
If[fixedperiodic,
Table[edatExtend[[bvec]]IdentityMatrix[dim]
,{bvec,qdim}]
,{}]]
],
{j,numbonds}]]
];
(* Generalized periodic Rigidity matrix*)
(* for fixed periodicity, the derivative of the transformation matrices is encoded in the last
qdim(dim(dim+1)) columns of the matrix as the nonredundant components of an element of
the Lie algebra to the dim-dimensional affine group
Since T1.T2 = T2.T1 for all pairs, if t1 and t2 are the infinitesimal changes in T1 and T2,
we must have:
t1.T2 + T1.t2 = T2.t1 + t2.T1
Fortunately, this is a linear condition on t1 and t2
Order of components: for each j = 1 to qdim,
first the dim components of the translation
then the dim^2 components of the transformation matrix
*)
(* With fixedperiodic, before taking the nullspace, may want to adjoin rows that constrain
the entries of the derivatives of the transformation matrices, e.g. the commutation condition above,
forcing them to be antisymmetric, etc.
*)
Clear[RigidityMatrixT]
RigidityMatrixT[z_,posns_,transformations_,edgedat_,fixedperiodic_:False]:=
Module[{numbonds=Length[edgedat],qdim=Length[transformations],dim=Length[posns[[1]]],bcomponent,
bvec,bmatrixcomponent,bcomponent2,bcoeff,tebond,
numpart=Length[posns],part1,part2,ebond,kb,lattchange,zm,edatExtend,tmat,pv,
k,j,i,ebondrot},
SparseArray[Flatten[Table[part1=edgedat[[j,1,1]];
part2=edgedat[[j,1,2]];
kb=1;(*edgedat[[j,3]];*)
edatExtend=Join[edgedat[[j,2,1;;Min[Length[edgedat[[j,2]]],qdim]]],
Table[0,{qdim-Length[edgedat[[j,2]]]}]];
zm=Product[z[[k]]^edatExtend[[k]],{k,qdim}];
(* assuming that all matrices in transf commute... *)
tmat=Dot@@Table[MatrixPower[transformations[[i]],edatExtend[[i]]],{i,qdim}]; (* maybe could memoize *)
pv=tmat.Join[posns[[part2]],{1}];
ebond=-posns[[part1]]+pv[[1;;dim]]; (* sign convention from malestein theran *)
ebondrot=ebond.tmat[[;;dim,;;dim]];
(* ebond=ebond/Norm[ebond];*)
If[part1!=part2,
Join[Table[{j,dim (part1-1)+k}->-ebond[[k]],{k,dim}],
Table[{j,dim (part2-1)+k}->zm ebondrot[[k]],{k,dim}],
If[fixedperiodic,Flatten[
Join[(* translational components *)
tebond=Sum[MatrixPower[tmat[[;;dim,;;dim]],l],{l,edatExtend[[bvec]]-1}].tmat[[;;dim,dim+1]]
+tmat[[;;dim,dim+1]];
Table[{j,dim numpart+(bvec-1) dim(dim+1)+bcomponent}->edatExtend[[bvec]] (tebond[[bcomponent]])
,{bvec,qdim},{bcomponent,dim}],
(* transformation matrix components *)
Table[
bcomponent2=Ceiling[bmatrixcomponent/dim];
bcomponent=Mod[bmatrixcomponent,dim,1];
bcoeff=(ebond[[bcomponent]])(posns[[part2,bcomponent2]]);
{j,dim numpart+(bvec-1) dim(dim+1)+dim+bmatrixcomponent}->edatExtend[[bvec]]bcoeff
,{bvec,qdim},{bmatrixcomponent,dim^2}]
]]
,{}]]
,
(* part1\[Equal]part2*)
Join[Table[{j,dim (part1-1)+k}->-ebond[[k]]+zm ebondrot[[k]],{k,dim}],
If[fixedperiodic,Flatten[
Join[(* translational components *)
Table[{j,dim numpart+(bvec-1) dim(dim+1)+bcomponent}->edatExtend[[bvec]] (ebond[[bcomponent]])
,{bvec,qdim},{bcomponent,dim}],
(* transformation matrix components *)
Table[
bcomponent2=Ceiling[bmatrixcomponent/dim];
bcomponent=Mod[bmatrixcomponent,dim,1];
bcoeff=(ebond[[bcomponent]])(posns[[part2,bcomponent2]]);
{j,dim numpart+(bvec-1) dim(dim+1)+dim+bmatrixcomponent}->edatExtend[[bvec]]bcoeff
,{bvec,qdim},{bmatrixcomponent,dim^2}]
]]
,{}]]
],
{j,numbonds}]]
,{numbonds,dim numpart+If[fixedperiodic,qdim*dim(dim+1),0]}]
];
NRigidityMatrixT[z_?(VectorQ[#,NumericQ]&),posns_,transformations_,edgedat_,fixedperiodic_:False]:=
RigidityMatrixT[z,posns,transformations,edgedat,fixedperiodic];
RigidityMatrix3I[z_,posns_,rodrig_,transl_,edgedat_,fixedperiodic_:False]:=Module[{transformations,A},
(* cauchy transform *)
transformations=Table[
A={{0,rodrig[[j,3]],-rodrig[[j,2]]},{-rodrig[[j,3]],0,rodrig[[j,1]]},
{rodrig[[j,2]],-rodrig[[j,1]],0}};
ArrayFlatten[{{(IdentityMatrix[3]-A).Inverse[(IdentityMatrix[3]+A)],
Transpose[{transl[[j]]}]},{0,1}}],{j,Length[rodrig]}];
RigidityMatrixT[z,posns,transformations,edgedat,fixedperiodic]]
EdgeLengthsSq[posns_,basis_,edgedat_]:=Module[{part1,part2,kb,lattchange,qdim=Length[basis],ebond,
numbonds=Length[edgedat],edatExtend,j},
Table[part1=edgedat[[j,1,1]];
part2=edgedat[[j,1,2]];
kb=edgedat[[j,3]];
edatExtend=Join[edgedat[[j,2,1;;Min[Length[edgedat[[j,2]]],qdim]]],
Table[0,{qdim-Length[edgedat[[j,2]]]}]];
lattchange=If[qdim>0,edatExtend.basis,0];
ebond=-posns[[part1]]+(posns[[part2]]+lattchange); (* sign convention from malestein theran *)
Norm[ebond]^2
,
{j,numbonds}]];
EdgeLengthsSqT[posns_,transf_,edgedat_]:=Module[{part1,part2,kb,qdim=Length[transf],ebond,
numbonds=Length[edgedat],edatExtend,tmat,pv,dim=Length[posns[[1]]],j,i},
Table[part1=edgedat[[j,1,1]];
part2=edgedat[[j,1,2]];
kb=edgedat[[j,3]];
edatExtend=Join[edgedat[[j,2,1;;Min[Length[edgedat[[j,2]]],qdim]]],Table[0,{qdim-Length[edgedat[[j,2]]]}]];
tmat=Dot@@Table[MatrixPower[transf[[i]],edatExtend[[i]]],{i,qdim}]; (* maybe could memoize *)
pv=tmat.Join[posns[[part2]],{1}];
ebond=-posns[[part1]]+pv[[;;dim]]; (* sign convention from malestein theran *)
Norm[ebond]^2
,
{j,numbonds}]];
EdgeLengthsSq3I[posns_,rodrig_,transl_,edgedat_]:=Module[{transformations,A},
(* cauchy transform *)
transformations=Table[
A={{0,rodrig[[j,3]],-rodrig[[j,2]]},{-rodrig[[j,3]],0,rodrig[[j,1]]},
{rodrig[[j,2]],-rodrig[[j,1]],0}};
ArrayFlatten[{{(IdentityMatrix[3]-A).Inverse[(IdentityMatrix[3]+A)],
Transpose[{transl[[j]]}]},{0,1}}],{j,Length[rodrig]}];
EdgeLengthsSqT[posns,transformations,edgedat]]
(* don't trust this right now in full generality - gauge choices made in rigidity matrix functions may be inconsistent with it *)
NetDipole[posns_,basis_,edgedat_]:=Module[{vertcont,edgecont,dim=Length[posns[[1]]],qdim=Length[basis],
nsp=NullSpace[basis],edatExtend,j},
vertcont=dim(Sum[posns[[j]],{j,Length[posns]}]);
edgecont=Sum[
edatExtend=Join[edgedat[[j,2,1;;Min[Length[edgedat[[j,2]]],qdim]]],Table[0,{qdim-Length[edgedat[[j,2]]]}]];
(posns[[edgedat[[j,1,1]]]]+posns[[edgedat[[j,1,2]]]]+edatExtend.basis)/2
,{j,Length[edgedat]}];
(Inverse[Transpose[Join[basis,nsp]]].(vertcont-edgecont))[[1;;qdim]]
];
KLWinding[z_,latticep_,basis_,latticeE_,cycle_,t_]:=Module[{powmat,w1,w2,zz},
powmat=RigidityMatrix[z,latticep,basis,latticeE];
zz=Det[powmat]/.cycle;
w1=NIntegrate[Im[D[Log[zz],t]],{t,0,2\[Pi]}];
-w1/(2\[Pi])
];
KLPolarization[latticep_,basis_,latticeEdat_,cyclesorigin0_:{}]:=
Module[{powmat,w,zz,k,qdim=Length[basis],z,cycle,t,cyclesorigin},
(* need both cyclesorigin0 and cyclesorigin because Mathematica's optional arguments are no longer variables *)
If[Length[cyclesorigin0]!=qdim, cyclesorigin = Table[-1,{qdim}],cyclesorigin=cyclesorigin0];
powmat=RigidityMatrix[Table[z[j],{j,qdim}],latticep,basis,latticeEdat];
w=Table[
cycle=Table[z[k]->If[k!=j,cyclesorigin[[k]],Exp[I t]],{k,qdim}];
zz=Det[powmat]/.cycle;
NIntegrate[Im[D[Log[zz],t]],{t,0,2\[Pi]}]
,{j,qdim}];
-w/(2\[Pi])
];
(* these are not correct: the true dynamical matrix should be R^T(-z).R(z) *)
(* "dynamical matrix" shorthand function *)
DynMat[rig_,kmat_:{}]:=Module[{},If[kmat=={},ConjugateTranspose[rig].rig,
ConjugateTranspose[rig].kmat.rig]];
(* "hamiltonian matrix" shorthand function *)
HMat[rig_]:=ArrayFlatten[{{0,ConjugateTranspose[rig]},{rig,0}}];
(* ::Section:: *)
(*Stress matrices*)
(* stress matrix function *)
StressMatrix[stress_,edat_,dim_,fixedperiodic_:False]:=Module[{j,inc,
qdim=Max[Length/@edat[[All,2]]]},
inc=IncidenceMat[Table[1,{qdim}],edat,dim,fixedperiodic];
Transpose[inc].DiagonalMatrix[Flatten[Table[{stress[[j]]}
,{j,Length[stress]},{dim}]]].inc
];
(* ::Section:: *)
(*Elastic tensor*)
symmetricindexlist[dim_]:=Flatten[Table[
Table[{j,j+(i-1)},{j,dim-i+1}]
,{i,dim}],1]
(* ordering of indices in elastic tensor in dim dimensions *)
fourthrankindices[dim_]:=Module[{sil=symmetricindexlist[dim]},Table[
Subscript[\[Epsilon], sil[[is]]] Subscript[\[Epsilon], sil[[js]]]
,{is,dim (dim+1)/2},{js,dim (dim+1)/2}]]
(* let k0 be a list of spring constants, possibly including 0's *)
ElasticTensor[pos_,basis_,edat0_,k0_:{}]:=Module[{k,qdim=Length[basis],
dim=Length[pos[[1]]],E=Length[edat0],numparts=Length[pos],C,U,\[CapitalSigma],Vs,is,js,
presym,Eafflist,Eafflist2,
symmetricindex,i,j,pair1,pair2,kk,matA,matB,matC,matD,proj,
part1,part2,edatExtend,lattchange,ebond,ebondlist,eps=10^-14,numss,nonzeros,edat},
(* kk is a matrix! *)
kk=If[Length[k0]!=E||Depth[k0]!=2,Print["Assuming all spring constants are 1"];
edat=edat0;IdentityMatrix[E],
(* remove edges with sufficiently small spring constant *)
nonzeros=Position[k0,(x_/;(x>eps)||(Not[NumericQ[x]]))][[2;;-2]];
edat=edat0[[Flatten[nonzeros]]];
E=Length[nonzeros];
DiagonalMatrix[Extract[k0,nonzeros]]
];
ebondlist=Table[Null,{E}];
(* compute compatibility matrix at q=0 and save the vector of extensions "ebondlist" *)
C=SparseArray[Flatten[Table[
part1=edat[[j,1,1]];
part2=edat[[j,1,2]];
edatExtend=Join[edat[[j,2,1;;Min[Length[edat[[j,2]]],qdim]]],
Table[0,{qdim-Length[edat[[j,2]]]}]];
lattchange=If[qdim>0,edatExtend.basis,0];
ebond=-pos[[part1]]+(pos[[part2]]+lattchange); (* sign convention from malestein theran *)
(* unit vectors *)
ebondlist[[j]]=ebond;
ebond=ebond/Norm[ebond];
If[part1!=part2,
Join[
Table[{j,dim (part1-1)+k}->-ebond[[k]],{k,dim}],Table[{j,dim (part2-1)+k}->ebond[[k]] ,{k,dim}]
],
(* part1\[Equal]part2*)
Table[{j,dim (part1-1)+k}->0,{k,dim}]
],
{j,E}]]
,{E,dim numparts}];
(* get basis for space of ss *)
U=NullSpace[Transpose[C]];(*Print[Length[U]];*)
(* now we project k^-1 onto ss space, creates a LinearSolveFunction which can act on a vector *)
(* hopefully a better way of computing MatrixInverse[U.MatrixInverse[k].Transpose[U]]*)
kk=LinearSolve[U.LinearSolve[kk,Transpose[U]]];
(*Print[kk[IdentityMatrix[Length[U]]]];*)
(* a list of pairs of indices *)
symmetricindex=Flatten[Table[
Table[{j,j+(i-1)},{j,dim-i+1}]
,{i,dim}],1];
(* compute coefficients of Subscript[\[Epsilon], ij] in the components of Subscript[E, aff] (indexed by j) *)
(* separate into different lists, indexed by "is" *)
Eafflist=Table[
pair1=symmetricindex[[is]];
ebond=ebondlist[[j]];
ebond[[pair1[[1]]]]ebond[[pair1[[2]]]]/Norm[ebond]
,{is,dim (dim+1)/2},{j,E}];
(* projection onto self-stress space *)
(*proj=ArrayFlatten[{Join[Table[0,{E-numss}],{IdentityMatrix[numss]}]}].Transpose[U];*)
proj=U;
Eafflist2=Table[proj.Eafflist[[is]],{is,dim (dim+1)/2}];
(*Eafflist2=Transpose[proj.Transpose[Eafflist]];*)
(* store components of the elastic tensor in an upper triangular matrix *)
presym=Table[
If[is<=js,
Eafflist2[[is]].kk[Eafflist2[[js]]]
,0]
,{is,dim (dim+1)/2},{js,dim (dim+1)/2}];
(* reorganize upper triangular matrix into a symmetric matrix; probably inefficient here *)
Table[presym[[Min[is,js],Max[is,js]]]
,{is,dim (dim+1)/2},{js,dim (dim+1)/2}]
]
(* currently broken for undercoordinated / unstable systems *)
(* let k0 be a list of spring constants, possibly including 0's *)
ElasticTensor2[pos_,basis_,edat0_,k0_:{}]:=Module[{k,qdim=Length[basis],
dim=Length[pos[[1]]],E=Length[edat0],numparts=Length[pos],C,U,\[CapitalSigma],Vs,is,js,
presym,Eafflist,Eafflist2,
symmetricindex,i,j,pair1,pair2,kk,matA,matB,matC,matD,proj,
part1,part2,edatExtend,lattchange,ebond,ebondlist,eps=10^-14,numss,nonzeros,edat},
(* kk is a matrix! *)
kk=If[Length[k0]!=E||Depth[k0]!=2,Print["Assuming all spring constants are 1"];
edat=edat0;IdentityMatrix[E],
(* remove edges with sufficiently small spring constant *)
nonzeros=Position[k0,(x_/;(x>eps)||(Not[NumericQ[x]]))][[2;;-2]];
edat=edat0[[Flatten[nonzeros]]];
E=Length[nonzeros];
DiagonalMatrix[Extract[k0,nonzeros]]
];
ebondlist=Table[Null,{E}];
(* compute compatibility matrix at q=0 and save the vector of extensions "ebondlist" *)
C=SparseArray[Flatten[Table[
part1=edat[[j,1,1]];
part2=edat[[j,1,2]];
edatExtend=Join[edat[[j,2,1;;Min[Length[edat[[j,2]]],qdim]]],
Table[0,{qdim-Length[edat[[j,2]]]}]];
lattchange=If[qdim>0,edatExtend.basis,0];
ebond=-pos[[part1]]+(pos[[part2]]+lattchange); (* sign convention from malestein theran *)
(* unit vectors *)
ebondlist[[j]]=ebond;
ebond=ebond/Norm[ebond];
If[part1!=part2,
Join[
Table[{j,dim (part1-1)+k}->-ebond[[k]],{k,dim}],Table[{j,dim (part2-1)+k}->ebond[[k]] ,{k,dim}]
],
(* part1\[Equal]part2*)
Table[{j,dim (part1-1)+k}->0,{k,dim}]
],
{j,E}]]
,{E,dim numparts}];
(* previous try using Schur complement by hand *)
(* compute SVD of compatibility matrix *)
{U,\[CapitalSigma],Vs}=SingularValueDecomposition[C];
(* get number of self-stresses at q = 0*)
(* numss can end up negative, which is bad *)
numss=Length[Select[Diagonal[Normal[\[CapitalSigma]]],Abs[N[#]]<eps&]]+E-dim numparts;
kk=If[Length[k0]==0,IdentityMatrix[numss],
(* Schur complement of k0, after rotating into stress eigenvector basis with Vs and Transpose[Vs].
The Schur complement for a matrix in block form {{A,B},{C,D}} is
A - B D^{-1} C .
*)
kk=Transpose[U].kk.U;
(*Print[kk];
Print[\[CapitalSigma]];
Print[E];
Print[numss];*)
matA=kk[[E-numss+1;;E,E-numss+1;;E]];
If[E==numss,matA,
matB=kk[[E-numss+1;;E,;;E-numss]];
matC=kk[[;;E-numss,E-numss+1;;E]];
matD=kk[[;;E-numss,;;E-numss]];
matA-matB.LinearSolve[matD,matC]]
];
(* don't we need to rotate back? *)
(* something above was wrong. kk could end up with negative eigenvalues?? *)
(* I believe this is fixed now. Unclear which is more efficient *)
(*Print[kk];*)
(* a list of pairs of indices *)
symmetricindex=Flatten[Table[
Table[{j,j+(i-1)},{j,dim-i+1}]
,{i,dim}],1];
(* compute coefficients of Subscript[\[Epsilon], ij] in the components of Subscript[E, aff] (indexed by j) *)
(* separate into different lists, indexed by "is" *)
Eafflist=Table[
pair1=symmetricindex[[is]];
ebond=ebondlist[[j]];
ebond[[pair1[[1]]]]ebond[[pair1[[2]]]]/Norm[ebond]
,{is,dim (dim+1)/2},{j,E}];
(* projection onto self-stress space *)
proj=ArrayFlatten[{Join[Table[0,{E-numss}],{IdentityMatrix[numss]}]}].Transpose[U];
(*proj=U;*)
Eafflist2=Table[proj.Eafflist[[is]],{is,dim (dim+1)/2}];
(*Eafflist2=Transpose[proj.Transpose[Eafflist]];*)
(* store components of the elastic tensor in an upper triangular matrix *)
presym=Table[
If[is<=js,
Eafflist2[[is]].kk.Eafflist2[[js]]
,0]
,{is,dim (dim+1)/2},{js,dim (dim+1)/2}];
(* reorganize upper triangular matrix into a symmetric matrix; probably inefficient here *)
Table[presym[[Min[is,js],Max[is,js]]]
,{is,dim (dim+1)/2},{js,dim (dim+1)/2}]
]
(* ::Section:: *)
(*Constraints and Pebble related*)
ConstraintRows[numverts_,pinnedverts_,template_]:=Module[{j,k,dim=Length[template[[1]]]},
Table[Flatten[Table[
If[k==pinnedverts[[j]],template[[j]],
Table[0,{dim}]]
,{k,numverts}]]
,{j,Length[pinnedverts]}]];
PinnedRows[numverts_,verts_,dim_]:=Module[{k,unitvec},
Flatten[Table[
unitvec=Table[UnitVector[dim,k],{Length[verts]}];
ConstraintRows[numverts,verts,unitvec],
{k,dim}],1]];
(* create basis for zero modes localized on certain vertices *)
(* pebble basis generator;
peb is a list of ordered pairs of vertex indices {vertex that pebble is on, vertex that outgoing edge points to} *)
(*pebblebasismatrix[p_,E_,peb_]:=Module[{v=Length[p],e=Length[E],R=Normal[RigidityMatrix[{1,1},p,{{1,0},{0,1}},E]],pebloc,outvert,edgevec,perpvec},
If[MatrixRank[R]<e,Print["rows not independent"];,
If[Length[peb]!=2v-e,Print["wrong number of pebbles specified"];,Join[R,
Table[pebloc=peb[[j,1]];
outvert=peb[[j,2]];
edgevec=p[[pebloc]]-p[[outvert]];
perpvec={-edgevec[[2]],edgevec[[1]]};
Join[Table[0,{2pebloc-2}],perpvec,Table[0,{2v-2pebloc}]]
,{j,2v-e}]]]]
]*)
(* specify explicitly direction on each pebble ; peb here is a list, first component is location of pebble, second component is direction of mode *)
PebbleBasisModes[p_,E_,peb_,template_]:=Module[{v=Length[p],dim=Length[p[[1]]],e=Length[E],
R=Normal[RigidityMatrix[{},p,{},E]],pebloc,M,pebrows},
If[MatrixRank[R]<e,Print["edges not independent!"];,
If[Length[peb]!=dim v-e,Print["wrong number of pebbles specified"];,
pebrows=ConstraintRows[v,peb,template];
If[MatrixRank[pebrows]<Length[peb],Print["pebble constraints not independent!"];,
M=Join[R,pebrows];
Transpose[LinearSolve[M,ArrayFlatten[{{Table[0,{Length[peb]},{Length[peb]}]},{IdentityMatrix[Length[peb]]}}]]]]
]]
];
(* silly test with taking normal of constrained direction parallel rather than perpendicular to outgoing edge *)
(*pebblebasismatrixpar[p_,E_,peb_]:=Module[{v=Length[p],e=Length[E],R=Normal[RigidityMatrix[{1,1},p,{{1,0},{0,1}},E]],pebloc,outvert,edgevec,perpvec},
If[MatrixRank[R]<e,Print["rows not independent"];,
If[Length[peb]!=2v-e,Print["wrong number of pebbles specified"];,Join[R,
Table[pebloc=peb[[j,1]];
outvert=peb[[j,2]];
edgevec=p[[pebloc]]-p[[outvert]];
perpvec=edgevec;
Join[Table[0,{2pebloc-2}],perpvec,Table[0,{2v-2pebloc}]]
,{j,2v-e}]]]]
]*)
(* ::Section:: *)
(*Mode extraction / computation*)
(* give infinitesimal mode corresponding to CCW rotation about cent *)
infinitesimal2drotationmode[pos_,basis_:{},cent_:{0,0}]:=Module[{j,numparts=Length[pos],basisrot,posrot},
posrot=Table[{(pos[[j,2]]-cent[[2]]),-(pos[[j,1]]-cent[[1]])},{j,numparts}];
basisrot=Table[{basis[[j,2]],-basis[[j,1]]},{j,Length[basis]}];
Join[Flatten[posrot],If[Length[basis]>0,
Flatten[Table[basisrot[[i]],
{i,Length[basis]}]]
,{}
]]
];
infinitesimalrotation[origin_,posns_]:=Module[{j,diff},
Flatten[Table[
diff=posns[[j]]-origin;
{-diff[[2]],diff[[1]]},{j,Length[posns]}]]];
getnontriv2D[pos_,basis_,edgedat_]:=Module[{pmtestk,testrotk,transx,transy,numatoms=Length[pos]},
pmtestk=Normal[RigidityMatrix[Table[1,{Length[basis]}],pos,basis,edgedat,True]];
transx=Flatten[Join[Table[{1,0},{numatoms}],Table[0,{2Length[basis]}]]];
transy=Flatten[Join[Table[{0,1},{numatoms}],Table[0,{2Length[basis]}]]];
testrotk=infinitesimal2drotationmode[pos,basis];
NullSpace[Join[pmtestk,{transx,transy,testrotk}]][[1]]];
getnontriv2Dall[pos_,basis_,edgedat_]:=Module[{pmtestk,testrotk,transx,transy,numatoms=Length[pos]},
pmtestk=Normal[RigidityMatrix[Table[1,{Length[basis]}],pos,basis,edgedat,True]];
transx=Flatten[Join[Table[{1,0},{numatoms}],Table[0,{2Length[basis]}]]];
transy=Flatten[Join[Table[{0,1},{numatoms}],Table[0,{2Length[basis]}]]];
testrotk=infinitesimal2drotationmode[pos,basis];
NullSpace[Join[pmtestk,{transx,transy,testrotk}]]];
getnontriv2Dmat[pos_,basis_,edgedat_]:=Module[{pmtestk,testrotk,transx,transy,numatoms=Length[pos]},
pmtestk=Normal[RigidityMatrix[Table[1,{Length[basis]}],pos,basis,edgedat,True]];
transx=Flatten[Join[Table[{1,0},{numatoms}],Table[0,{2Length[basis]}]]];
transy=Flatten[Join[Table[{0,1},{numatoms}],Table[0,{2Length[basis]}]]];
testrotk=infinitesimal2drotationmode[pos,basis];
Join[pmtestk,{transx,transy,testrotk}]];
getnontriv2Dflat[flatvec_?(VectorQ[#,NumericQ]&),edgedat_]:=Module[{pmtestk,testrotk,transx,transy,
numatoms,pos,basis,nsp,nspp,bigrig,bigrig2,j},
numatoms=(Length[flatvec]-4)/2;
pos=Partition[flatvec[[1;;2numatoms]],2];
basis={flatvec[[-4;;-3]],flatvec[[-2;;-1]]};
pmtestk=Normal[RigidityMatrix[{1,1},pos,basis,edgedat,True]];
transx=Flatten[Join[Table[{1,0},{numatoms}],Table[0,{4}]]];
transy=Flatten[Join[Table[{0,1},{numatoms}],Table[0,{4}]]];
testrotk=infinitesimal2drotationmode[pos,basis];
bigrig=Join[pmtestk,{transx,transy,testrotk}];
bigrig2=bigrig[[1;;-2,1;;2numatoms]];
nspp=NullSpace[bigrig2];
If[Length[nspp]>0,bigrig=Join[bigrig,nspp=Table[Join[nspp[[j]],{0,0,0,0}],{j,Length[nspp]}]]];
nsp=NullSpace[bigrig];
Join[nspp,nsp]
];
(* some functions giving constraint rows on the deformations of transformation matrices *)
(* ordering of columns, first the dim components of translations, then the dim^2 components of the
transformation matrix;
{i,j} are the indices of a matrix entry (row, column), 1\[LessEqual]i\[LessEqual]dim, 1\[LessEqual]j\[LessEqual]dim+1
*)
coeffindexT[i_,j_,dim_]:=Module[{},
If[j==dim+1,i,
i dim+j
]
];
(*
t1.T2 + T1.t2 = T2.t1 + t2.T1
a_kl.B_lm + A_kl.b_lm - B_kl.a_lm - b_kl.A_lm = 0
*)
commuteconstraintT[numparts_,transformations_]:=Module[
{qdim=Length[transformations],dim,pairtab,temp,datavec
i1,i2,j,k,l,m,coeffindex,rowind,colind,maxcol},
temp=Dimensions[transformations[[1]]];
If[(temp[[1]]!=temp[[2]]),Print["transformation matrix has wrong shape"];Abort[];,
dim=temp[[1]]-1];(*Print[dim];*)
maxcol=dim numparts+qdim dim (dim+1);
(* one set of rows for each pair *)
pairtab=Subsets[Range[qdim],{2}];Print[pairtab];
Table[{i1,i2}=pairtab[[j]];
Table[rowind=(j-1)dim(dim+1)+(m-1)(dim)+k;
datavec=Table[0,{dim numparts+qdim dim(dim+1)}];
Do[
{(* a_kl entry *)
If[l!=m,colind=dim numparts+(i1-1)dim(dim+1)+coeffindexT[k,l,dim];
datavec[[colind]]+=transformations[[i2,l,m]];
If[colind>maxcol,Print["too big akl ",i1,i2,k,l,m];];,
(* a_km entry special *)
colind=dim numparts+(i1-1)dim(dim+1)+coeffindexT[k,m,dim];
datavec[[colind]]+=
transformations[[i2,m,m]]-transformations[[i2,k,k]];
If[colind>maxcol,Print["too big akm ",i1," ",i2," ",k," ",l," ",m];];
],
(* a_lm entry *)
If[l!=k,colind=dim numparts+(i1-1)dim(dim+1)+coeffindexT[l,m,dim];
datavec[[colind]]+=transformations[[i2,k,l]],{}];
If[colind>maxcol,Print["too big alm ",i1," ",i2," ",k," ",l," ",m];];,
(* b_kl entry *)
If[l!=m,colind=dim numparts+(i2-1)dim(dim+1)+coeffindexT[k,l,dim];
datavec[[colind]]+=transformations[[i1,l,m]];
If[colind>maxcol,Print["too big bkl ",i1," ",i2," ",k," ",l," ",m];];,
(* b_km entry special *)
colind=dim numparts+(i2-1)dim(dim+1)+coeffindexT[k,m,dim];
datavec[[colind]]+=
transformations[[i1,m,m]]-transformations[[i1,k,k]];
If[colind>maxcol,Print["too big bkm ",i1," ",i2," ",k," ",l," ",m];];
],
(* b_lm entry *)
If[l!=k,colind=dim numparts+(i2-1)dim(dim+1)+coeffindexT[l,m,dim];
datavec[[colind]]+=transformations[[i1,k,l]],{}];
If[colind>maxcol,Print["too big blm ",i1," ",i2," ",k," ",l," ",m];];
}
,{l,dim+1}];datavec
,{k,dim},{m,dim+1}]
,{j,Length[pairtab]}]]
(*,
{Length[pairtab]dim(dim+1),dim numparts+qdim dim (dim+1)}
]]*)
(* for isometries, need a function to ensure that the dim^2 components correspond to
entries of a matrix in so(d) *)
(* need this to be cromulent with CoveringFrameworkVerts and CoveringFrameworkEdges *)
CoveringMotion[mode_,cover_,dim0_:0,periodic_:False]:=Module[{i,j,dim,qdim=Length[cover],
numparts,tabspec,m,ind},
dim=If[dim0!=0,dim0,qdim];
(* dim*number of particles + dim qdim = length of mode vector*)
If[Mod[(Length[mode]-dim qdim),dim]!=0,Print["bad mode length"];Abort[]];
numparts=(Length[mode]-dim qdim)/dim; (* y then x *)
tabspec=Table[{m[i],0,cover[[i]]-1},{i,qdim}];
Join[Flatten[
Table[
Table[
(* affine contribution from last components of mode *)
(* infinitesimal change of jth basis vector *)
Sum[ind=dim numparts+dim (j-1);
mode[[ind+1;;ind+dim]]*m[j],{j,qdim}]
(* internal components of mode *)
+mode[[dim (i-1)+1;;dim (i-1)+dim]],
{i,numparts}],
##]&@@tabspec (* specification of table, dim copies of loops from 0 to cover-1 *)
],
If[periodic,
Flatten[Table[
ind=dim numparts+dim(j-1);
cover[[j]]mode[[ind+1;;ind+dim]],{j,qdim}]],{}]
]
];
(* need this to be cromulent with CoveringFrameworkVerts and CoveringFrameworkEdges *)
CoveringMotionz[z_,mode_,cover_,dim0_:0]:=Module[{i,j,dim,qdim=Length[cover],
numparts,tabspec,m,ind,mult},
dim=If[dim0!=0,dim0,qdim];
(* dim*number of particles = length of mode vector*)
If[Mod[(Length[mode]),dim]!=0,Print["bad mode length"];Abort[]];
numparts=(Length[mode])/dim; (* y then x *)
tabspec=Table[{m[i],0,cover[[i]]-1},{i,qdim}];
Flatten[
Table[
Table[
(* contribution from z; take real part by default *)
mult=Re[Product[z[[j]]^m[j],{j,qdim}]];
(* internal components of mode *)
mult mode[[dim (i-1)+1;;dim (i-1)+dim]],
{i,numparts}],
##]&@@tabspec (* specification of table, dim copies of loops from 0 to cover-1 *)
]
];
SubtractTranslations[mode_,dim_,periodicdim_:0]:=Module[{modeinternal,modeguest,avgtranslation,numparts},
If[periodicdim>0,
modeinternal=mode[[;;-periodicdim dim-1]];
modeguest=mode[[-periodicdim dim;;]];
,
modeinternal=mode;
modeguest={};
];
numparts=(Length[modeinternal]/dim);
avgtranslation=Total[Partition[modeinternal,dim]]/numparts;
Join[modeinternal-Flatten[Table[avgtranslation,{numparts}]],modeguest]
]
(* this should yield the elastic displacements in a spring network *)
(* Lap is the dynamical matrix *)
(* bvtr is a list of indices of particles whose displacements will be specified by u *)
(* u is the list of applied displacements *)
HarmonicExtension[Lap_,bvtr_,u_,dim_:2]:=Module[{j,l=Dimensions[Lap][[2]]/dim,
inter,dinter,cinter,matB,matC,fI,ans},
(* need to be careful with "Complement" because does not preserve ordering *)
inter=Complement[Table[j,{j,l}],bvtr];
dinter=PosVecIndices[inter,dim];
cinter=PosVecIndices[bvtr,dim];
(* somehow slower if u is sparse????*)
(*matB=If[toggle\[Equal]1,SparseArray[Lap[[dinter,cinter]].u],Lap[[dinter,cinter]].u];*)
matB=Lap[[dinter,cinter]].u;
(* dissatisfying that taking determinant here is the only way to see whether we have rigidity inside*)
matC=Lap[[dinter,dinter]];
If[Chop[Det[matC]]==0,
Print["Rank deficient; system is floppy"];,
(*fI=-Inverse[matC].matB.u;*)
(*fI=If[toggle\[Equal]1,-LinearSolve[matC,matB,Method\[Rule]"Krylov"],-LinearSolve[matC,matB]];*)
fI=-LinearSolve[matC,matB];
ans=Table[0,{dim l}];
Do[ans[[dinter[[j]]]]=fI[[j]];,{j,Length[fI]}];
Do[ans[[cinter[[j]]]]=u[[j]];,{j,Length[u]}];
ans]
];
(* Dirichlet to Neumann operator *)
DtN[Lap_,bvtr_,dim_:2]:=Module[{j,l=Dimensions[Lap][[1]]/dim,inter,dinter,cinter,matA,matB,matC,matD},
inter=Complement[Table[j,{j,l}],bvtr];
dinter=PosVecIndices[inter,dim];
cinter=PosVecIndices[bvtr,dim];
matA=Lap[[cinter,cinter]];
matB=Lap[[cinter,dinter]];
matC=Lap[[dinter,cinter]];
matD=Lap[[dinter,dinter]];
(* Schur complement *)
(*matA-matB.Inverse[matD].matC *)
matA-matB.LinearSolve[matD,matC]
];
(*pos slope, zero slope, neg slope line-localized modes for untwisted kagome*)
poslinemode[i_,m_,n_]:=Module[{j,d=Table[0,{6m n}],loc},
Do[loc=3n(i-1)+3(j-1)+2;
d[[2loc-1]]=3/4;
d[[2loc]]=Sqrt[3]/4;
d[[2loc+1]]=0;
d[[2loc+2]]=Sqrt[3]/2;,{j,n}];d];
zerolinemode[i_,m_,n_]:=Module[{j,d=Table[0,{6m n}],loc},
Do[loc=3(i-1)+3n(j-1)+1;
d[[2loc-1]]=-(3/4);
d[[2loc]]=-(Sqrt[3]/4);
d[[2loc+3]]=-(3/4);
d[[2loc+4]]=Sqrt[3]/4;,{j,m}];d];
neglinemode[i_,m_,n_]:=Module[{j,d=Table[0,{6m n}],loc},
Do[loc=If[i<m+n-i,3(i-1)+3(n-1)(j-1)+1,3m n-3(m+n-i)-3(n-1)(j-1)+1];
d[[2loc-1]]=0;
d[[2loc]]=-(Sqrt[3]/2);
d[[2loc+1]]=3/4;
d[[2loc+2]]=-(Sqrt[3]/4);,{j,Min[m+n-i,i]}];d];
(* ::Section:: *)
(*Some basic 2 D lattices*)
(* ::Subsection:: *)
(*vertex positions*)
(*square lattice*)
SqLattice[xx_,yy_,r_:0]:=(* yy better be even? *) Module[{m,n},
Flatten[Table[
{m,n}+If[r>0,RandomReal[{-r,r},2],{0,0}]
,{m,0,xx-1},{n,0,yy-1}],1]];
SqLatticeSlopes[xx_,yy_,r_:0]:=(* yy better be even? *)
Module[{m,n,slopesx=RandomReal[{-r,r},xx],slopesy=RandomReal[{-r,r},yy],time},
Flatten[Table[
time=computeintersection[{m,0},\[Pi]/2+slopesx[[m+1]],{0,n},slopesy[[n+1]]];
{0,n}+time [[2]]{Cos[slopesy[[n+1]]],Sin[slopesy[[n+1]]]}
,{m,0,xx-1},{n,0,yy-1}],1]];
(* Twist angle convention for kagome lattices has the opposite sign from that of K Sun et al PNAS 2012 *)
(* kagome in the form of a rhombus *)
KagLatticeRho[xx_,yy_,th_:0,r_:0]:=(* yy better be even? *)Module[{m,n,i},
Flatten[Table[
{(*Mod[m+1/2.n,xx]*)m+1/2 n,Sqrt[3]/2 n}+{-1/4,-Sqrt[3]/12}
+If[r>0,RandomReal[{-r,r},2],{0,0}]
+1/(2 Sqrt[3]Cos[th]) {Cos[\[Pi]/6+2 \[Pi]/3 i+th],Sin[\[Pi]/6+2 \[Pi]/3 i+th]}
,{m,1,xx},{n,0,yy-1},{i,3}],2]];
(* perturbed kagome in the form of a rhombus. The three vertices per unit cell are inscribed
in a unit equilateral triangle (if t=0)
notation:
{s1,s2,s3} are parameters along the three edges of the equilateral triangle parallel to {1,0},
{-1/2,Sqrt[3]/2} and {-1/2,-Sqrt[3]/2}, respectively.
t displaces the s2 vertex in a direction perpendicular to that edge of the triangle
*)
PKagLatticeRho[xx_,yy_,s1_,s2_,s3_,t_:0,r_:0]:=(* yy better be even? *)Module[{m,n,i},
Flatten[Table[
{(*Mod[m+1/2.n,xx]*)m+1/2 n,Sqrt[3]/2 n}
+If[r>0,RandomReal[{-r,r},2],{0,0}]
+If[i==1,(1-s3){1/2,Sqrt[3]/2},{0,0}]
+If[i==2,{s1,0},{0,0}]
+If[i==3,{1,0}+s2{-1/2,Sqrt[3]/2}+t{Sqrt[3]/2,1/2},{0,0}]
,{m,0,xx-1},{n,0,yy-1},{i,3}],2]];
(* kagome in the form of a rectangle *)
KagLatticeRec[xx_,yy_,th_:0,r_:0]:=(* yy better be even? *) Module[{i,m,n},
Flatten[Table[
{m+1/2 Mod[n,2],Sqrt[3]/2 n}+{-1/4,-Sqrt[3]/12}
+If[r>0,RandomReal[{-r,r},2],{0,0}]
+1/(2 Sqrt[3]Cos[th]) {Cos[\[Pi]/6+2 \[Pi]/3 i+th],Sin[\[Pi]/6+2 \[Pi]/3 i+th]}
,{m,1,xx},{n,0,yy-1},{i,3}],2]];
(* triangular lattice in the form of rhombus *)
TriLatticeRho[xx_,yy_,r_:0]:=(* yy better be even? *) Module[{m,n},
Flatten[Table[
{(*Mod[m+1/2.n,xx]*)m+1/2. n,Sqrt[3]/2. n}
+If[r>0,RandomReal[{-r,r},2],{0,0}]
,{m,1,xx},{n,0,yy-1}],1]];
(*triangular lattice in the form of rectangle *)
TriLatticeRec[xx_,yy_,r_:0]:=(* yy better be even? *) Module[{m,n},
Flatten[Table[
{Mod[m+1/2. n,xx],Sqrt[3]/2. n}
+If[r>0,RandomReal[{-r,r},2],{0,0}]
,{m,1,xx},{n,0,yy-1}],1]];
(* honeycomb in the form of a rectangle *)
HoneycombLattice[xx_,yy_,r_:0]:=(* yy better be even? *) Module[{temp,x,m,n,i},
temp=Flatten[Table[
If[((Mod[m+1/2 n,xx]==0)&&(i==1))||((Mod[m+1/2 n,xx]==xx-1/2)&&(i==2)),
{},
{Mod[m+1/2 n,xx],Sqrt[3]/2 n}
+If[r>0,RandomReal[{-r,r},2],{0,0}]+(-1)^i /(2Sqrt[3]){Sqrt[3]/2,1/2}]
,{m,1,xx},{n,0,yy-1},{i,2}],2];
Replace[temp,x_List:>DeleteCases[x,{}],{0,Infinity}]
];
makekaglatticerunit[xx_,yy_,th_:0,r_:0]:=(* yy better be even? *) Module[{m,n},
Flatten[Table[
{(*Mod[m+1/2.n,xx]*)m+1/2 n,Sqrt[3]/2 n}
+{-1/4,-Sqrt[3]/12}+If[r>0,RandomReal[{-r,r},2],{0,0}]
,{m,1,xx},{n,0,yy-1}],1]];
makekaglatticeunit[xx_,yy_,th_:0,r_:0]:=(* yy better be even? *) Module[{m,n},
Flatten[Table[
{Mod[m+1/2 n,xx],Sqrt[3]/2 n}
+{-1/4,-Sqrt[3]/12}+If[r>0,RandomReal[{-r,r},2],{0,0}]
,{m,1,xx},{n,0,yy-1}],1]];
(* ::Subsection:: *)
(*edge stuff*)
(* return a list of pairs of vertices corresponding to edges, first list is NN, second list is NNN *)
SqLatticeEdges[xx_,yy_]:=(* yy better be even? *)
Module[{m,n,index=0,bondlist=Table[Null,{2xx*yy-xx-yy}],numbonds=0,
nextbondlist=Table[Null,{(xx-1)*(yy-1)}],numnextbonds=0,nextbondlistother=Table[Null,{(xx-1)*(yy-1)}]},
Do[
index++;
If[n>1,
numbonds++;
bondlist[[numbonds]]={{index,index-1},{},1};
];
If[m>1,
numbonds++;
bondlist[[numbonds]]={{index,index-yy},{},1};
];
If[(m>1)&&(n>1),
numnextbonds++;
nextbondlist[[numnextbonds]]={{index,index-yy-1},{},1};
nextbondlistother[[numnextbonds]]={{index-1,index-yy},{},1};
];
,{m,xx},{n,yy}];
bondlist=bondlist[[1;;numbonds]];
nextbondlist=nextbondlist[[1;;numnextbonds]];
{bondlist,nextbondlist,nextbondlistother}
];