-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumba_numeric.py
executable file
·1620 lines (1371 loc) · 60.8 KB
/
numba_numeric.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Module for numeric functions accelerated by numba.
Split to small njitted functions to re-derive trisurf properties
FUNCTIONS:
- numpy_sum_extend(A,B,C): equivalent to A[B]+=D
- connected_components(nvtx, blist, v_keep): clustering algorithm
- 5 permutation of calculate_statistic_[new/old][/_w/_ww]:
get trisurf statistics from vtu-derived numpy array as numpy arrays
Created on Thu Aug 12 14:26:31 2021
@author: yoav
"""
import numpy as np
try:
from numba import njit
except ImportError:
def njit(f=None, *args, **kwargs):
"""Fake do-nothing njit since we have no numba"""
def decorator(func):
return func
if callable(f):
return f
else:
return decorator
#%%
############################
# small njitted functions: #
############################
# extension of numpy sum
# and implementation of connected component that works with numba
@njit
def numpy_sum_extend(array_to_add_to, array_extend_indices, array_to_add_from):
"""Apply A[B] += D, even when B and D are larger than A."""
for i, j in enumerate(array_extend_indices):
array_to_add_to[j, ...] += array_to_add_from[i, ...]
@njit
def _strong_connect(i, c_idx, S, CC, v_idx, v_lowlink,
v_on_stack, blist, v_keep=None):
"""Recursive subfunction of connected_components."""
v_idx[i] = c_idx+0
v_lowlink[i] = c_idx+0
c_idx += 1
S.append(i)
v_on_stack[i] = True
for bond in blist:
if bond[0] == i:
j = bond[1]
elif bond[1] == i:
j = bond[0]
else:
continue
if v_keep is not None and not v_keep[j]:
continue
#
if v_idx[j] == 0:
_strong_connect(j, c_idx, S, CC,
v_idx, v_lowlink, v_on_stack, blist, v_keep)
v_lowlink[i] = min(v_lowlink[i], v_lowlink[j])
elif v_on_stack[j]:
v_lowlink[i] = min(v_lowlink[i], v_idx[j])
# if v is root:
if v_idx[i] == v_lowlink[i]:
currCC = [] # new component
while True:
j = S.pop()
v_on_stack[j] = False
v_lowlink[j] = v_lowlink[i]
currCC.append(j)
if j == i:
break
CC.append(currCC)
@njit
def connected_components(nvtx, blist, v_keep=None):
"""Get connected components for nvtx nodes in v_keep; connected by blist.
vtx are assumed to be their indices: vertex[i]==i.
Mostly copied from Wikipedia: "Trajan's strongly connected-
components algorithm"
(which can't be too good: we're not having any strong connection)
blist: mx2 array connecting vertices
blist[m,:]==[i,j] is the mth edge connecting vertices i,j
v_keep: boolean arrays size nvtx, saying if this is a relevant vertex
irrelevent vertices are skipped
returns list of list of nodes i.e. list of clusters, and a cluster_id array
"""
v_idx = np.zeros(nvtx, dtype=np.int64)
v_lowlink = np.zeros(nvtx, dtype=np.int64)
v_on_stack = np.zeros(nvtx, dtype=np.bool_)
c_idx = np.array(1) # current index
S = []
S.append(1)
S.pop()
CC = []
CC.append(S)
CC.pop()
for i in range(len(v_idx)):
if v_keep is not None and not v_keep[i]:
continue
if v_idx[i] == 0:
_strong_connect(i, c_idx, S, CC, v_idx, v_lowlink,
v_on_stack, blist, v_keep)
for i, clst in enumerate(CC):
for node in clst:
v_lowlink[node] = i
return CC, v_lowlink
#%%
####################################
# individual processing functions: #
####################################
# get various quantities from PyVtu arrays
# not bundled into the big functions
def clusterize_blist(blist):
"""Clusterize blist"""
blist = blist.copy()
vtx = np.unique(blist)
n = vtx.max()
# convert "real" indices to range [1,4,900]->[0,1,2]
idx = np.arange(len(vtx))
vtx_idx = np.zeros(n+1, int)
vtx_idx[vtx] = idx
blist = vtx_idx[blist]
CC, _ = connected_components(len(vtx), blist)
# map cluster back from range to the real vertex indices
clusters = [[vtx[i] for i in clst] for clst in CC]
return clusters
@njit(cache=True, error_model='numpy')
def bonding_ratio(blist, bonding):
"""Get bonding ratio from blist and bonding list."""
# get bonds with energy
# bonds_with_e = bonding[bond->vtx[0]] and bonding[bond->vtx[1]]
# nbw = sum(bonds_with_e)
nbw_nb = (bonding[blist[:, 0]] & bonding[blist[:, 1]]).sum()
nbw_nb /= (blist.shape[0])
return nbw_nb
@njit(cache=True, error_model='numpy')
def gyration_eigenvalues(pos):
"""Get gyration eigenvalues from position."""
nvtx = pos.shape[0]
CM_pos = pos.sum(axis=0)/nvtx # remove Center Mass
pos = pos-CM_pos
# get gyration eigenvalues G_mn = 1/N sum(r_n r_m)
# which is equivalent to G = (pos.T @ pos) / nvtx
gy_eig = np.linalg.eigvalsh((pos.T @ pos) / nvtx)
gy_eig.sort()
return gy_eig
@njit(cache=True, error_model='numpy')
def bounding_radius(pos):
"""Get bounding radius from position."""
nvtx = pos.shape[0]
CM_pos = pos.sum(axis=0)/nvtx # remove Center Mass
pos = pos-CM_pos
return np.sqrt(max((pos**2).sum(axis=1)))
@njit(cache=True, error_model='numpy')
def area_volume(pos, tlist):
"""Get area and volume from position and tlist."""
nvtx = pos.shape[0]
t_normals = np.zeros((tlist.shape[0], 3))
CM_pos = pos.sum(axis=0)/nvtx # remove Center Mass
pos = pos-CM_pos
######################
# get volume and area:
xyz0 = pos[tlist[:, 0], :]
xyz1 = pos[tlist[:, 1], :]
xyz2 = pos[tlist[:, 2], :]
t_normals = np.cross(xyz1 - xyz0, xyz2 - xyz0)
# area = parallelogram/2 = |cross(AB,AC)|/2
double_areas = np.sqrt((t_normals**2).sum(axis=-1))
# volume: copy from c calculation
# (triangle_area * norm * radius = signed area?)
total_area = double_areas.sum()/2
total_volume = -((xyz0 + xyz1 + xyz2)*t_normals).sum()/18
return total_area, total_volume
@njit(cache=True, error_model='numpy')
def voroni_bonds(pos, tlist):
"""Calculate voronoi (dual) bonds from position and connectivity list"""
nvtx = pos.shape[0]
CM_pos = pos.sum(axis=0)/nvtx # remove Center Mass
pos = pos-CM_pos
# shorthand to simplify (n)*(mxn) array operations addim1(x[:n])*y[:n,:]
def addim1(x): return np.expand_dims(x, 1) # inline plz
xyz0 = pos[tlist[:, 0], :]
xyz1 = pos[tlist[:, 1], :]
xyz2 = pos[tlist[:, 2], :]
sigma = 0.0*xyz0
area = sigma + 0.0
# To get cotan, we will need bond length square
bond_sqr01 = ((xyz1-xyz0)**2).sum(axis=1)
bond_sqr02 = ((xyz2-xyz0)**2).sum(axis=1)
bond_sqr12 = ((xyz2-xyz1)**2).sum(axis=1)
# on 0th vtx of each triangle:
# numpy vectorized version of the c calculation
# cot[q] = |a||b|cos/sqrt(|a|^2|b|^2 - |a|^2|b|^2cos^2)
# |a||b|cos = a @ b
dot_prod_at = ((xyz1-xyz0)*(xyz2-xyz0)).sum(axis=-1)
cot_at = dot_prod_at / np.sqrt(bond_sqr01*bond_sqr02 - dot_prod_at**2)
# dual bond
sigma[:, 0] = addim1(cot_at) * (xyz2 - xyz1)
area[:, 0] = (cot_at*bond_sqr12)/8
# on 1th vtx of each triangle
dot_prod_at = ((xyz2-xyz1)*(xyz0-xyz1)).sum(axis=-1)
cot_at = dot_prod_at / np.sqrt(bond_sqr12*bond_sqr01 - dot_prod_at**2)
sigma[:,1] = addim1(cot_at) * (xyz0 - xyz2)
area[:,1] = (cot_at*bond_sqr02)/8
# contributions to 2 and 0:
# on 2th vtx
dot_prod_at = ((xyz0-xyz2)*(xyz1-xyz2)).sum(axis=-1)
cot_at = dot_prod_at / np.sqrt(bond_sqr12*bond_sqr02 - dot_prod_at**2)
sigma[:,2] = addim1(cot_at) * (xyz1 - xyz0)
area[:,2] = (cot_at*bond_sqr01)/8
# contributions to 1 and 2:
return sigma, area
@njit(cache=True, error_model='numpy')
def curvature(pos, tlist):
"""Calculate curvature from position and connectivity list.
Return curvature per vertex and area per vertex"""
nvtx = pos.shape[0]
t_normals = np.zeros((tlist.shape[0], 3))
CM_pos = pos.sum(axis=0)/nvtx # remove Center Mass
pos = pos-CM_pos
# shorthand to simplify (n)*(mxn) array operations addim1(x[:n])*y[:n,:]
def addim1(x): return np.expand_dims(x, 1) # inline plz
xyz0 = pos[tlist[:, 0], :]
xyz1 = pos[tlist[:, 1], :]
xyz2 = pos[tlist[:, 2], :]
t_normals = np.cross(xyz1 - xyz0, xyz2 - xyz0)
# area = parallelogram/2 = |cross(AB,AC)|/2
areas = np.sqrt((t_normals**2).sum(axis=-1))/2
# the components of summation are,
# for each vertex i:
# sum all l_ij * cotan(theta_opposite)/2 --> rh[i]
# sum normal of triangles (to determine h sign) --> tnh[i]
# this can be done on the triangle, which have well-determined neighbors
rh = np.zeros(pos.shape)
tnh = np.zeros(pos.shape)
s = np.zeros(nvtx)
# summing the normals is easy, since we have them from volume/area,
# but we didn't normalize them
t_normals /= addim1(2*areas) # normalizing vectors was skipped
# add the normal to each vertex in the triangle:
# vtx_normal[tri->vtx[0]] += tri->normal. then for 1 and 2
# problematic due to repeated indices in triangles- two triangles can
# have the same vertex in 0, screwing the +=
numpy_sum_extend(tnh, tlist[:, 0], t_normals)
numpy_sum_extend(tnh, tlist[:, 1], t_normals)
numpy_sum_extend(tnh, tlist[:, 2], t_normals)
# we only need direction, tnh*rh<0, so no need to normalize
# Summing the other part is more difficult
# we go on each vertex of the triangle k=[0,1,2]
# calculate cotan(theta[k])
# and add the relevant lij*cotan(theta[k])/2 vector to rh[i!=k]
# To get cotan, we will beed bond length square
bond_sqr01 = ((xyz1-xyz0)**2).sum(axis=1)
bond_sqr02 = ((xyz2-xyz0)**2).sum(axis=1)
bond_sqr12 = ((xyz2-xyz1)**2).sum(axis=1)
# on 0th vtx of each triangle:
# numpy vectorized version of the c calculation
# cot[q] = |a||b|cos/sqrt(|a|^2|b|^2 - |a|^2|b|^2cos^2)
# |a||b|cos = a @ b
dot_prod_at = ((xyz1-xyz0)*(xyz2-xyz0)).sum(axis=-1)
cot_at = dot_prod_at / np.sqrt(bond_sqr01*bond_sqr02 - dot_prod_at**2)
# dual bond
sigma_12 = addim1(cot_at) * (xyz2 - xyz1)
area_12 = (cot_at*bond_sqr12)/8
# contributions to 1 and 2: +-l_12 * cot(theta[0])=+-sigma12
# (divide by 2 later)
numpy_sum_extend(rh, tlist[:, 1], sigma_12)
numpy_sum_extend(rh, tlist[:, 2], -sigma_12)
numpy_sum_extend(s, tlist[:, 1], area_12)
numpy_sum_extend(s, tlist[:, 2], area_12)
# on 1th vtx of each triangle
dot_prod_at = ((xyz2-xyz1)*(xyz0-xyz1)).sum(axis=-1)
cot_at = dot_prod_at / np.sqrt(bond_sqr12*bond_sqr01 - dot_prod_at**2)
sigma_20 = addim1(cot_at) * (xyz0 - xyz2)
area_20 = (cot_at*bond_sqr02)/8
# contributions to 2 and 0:
numpy_sum_extend(rh, tlist[:, 2], sigma_20)
numpy_sum_extend(rh, tlist[:, 0], -sigma_20)
numpy_sum_extend(s, tlist[:, 2], area_20)
numpy_sum_extend(s, tlist[:, 0], area_20)
# on 2th vtx
dot_prod_at = ((xyz0-xyz2)*(xyz1-xyz2)).sum(axis=-1)
cot_at = dot_prod_at / np.sqrt(bond_sqr12*bond_sqr02 - dot_prod_at**2)
sigma_01 = addim1(cot_at) * (xyz1 - xyz0)
area_01 = (cot_at*bond_sqr01)/8
# contributions to 1 and 2:
numpy_sum_extend(rh, tlist[:, 0], sigma_01)
numpy_sum_extend(rh, tlist[:, 1], -sigma_01)
numpy_sum_extend(s, tlist[:, 0], area_01)
numpy_sum_extend(s, tlist[:, 1], area_01)
# h per vertex, do the division by 2 we didn't do before
h = np.sqrt((rh**2).sum(axis=-1))/2
# -h if pointing the other way (maybe triangle vertex order: maybe -?)
h[(rh*tnh).sum(axis=-1) < 0] *= -1
h /= s
# few! that was not nice
return h, s
@njit(cache=True, error_model='numpy')
def perimeter(vtype, pos, blist, tlist):
"""Get perimeter based on nodetype"""
nvtx = pos.shape[0]
CM_pos = pos.sum(axis=0)/nvtx # remove Center Mass
pos = pos-CM_pos
# shorthand to simplify (n)*(mxn) array operations addim1(x[:n])*y[:n,:]
def addim1(x): return np.expand_dims(x, 1) # inline plz
# position of each point in triangle
xyz0 = pos[tlist[:, 0], :]
xyz1 = pos[tlist[:, 1], :]
xyz2 = pos[tlist[:, 2], :]
######################################################
# perimeter:
# we need the dual bond in each triangle
# To get cotan, we will beed bond length square
bond_sqr01 = ((xyz1-xyz0)**2).sum(axis=1)
bond_sqr02 = ((xyz2-xyz0)**2).sum(axis=1)
bond_sqr12 = ((xyz2-xyz1)**2).sum(axis=1)
# on 0th vtx of each triangle:
dot_prod_at = ((xyz1-xyz0)*(xyz2-xyz0)).sum(axis=-1)
cot_at = dot_prod_at / np.sqrt(bond_sqr01*bond_sqr02 - dot_prod_at**2)
# dual bond
sigma_12 = addim1(cot_at) * (xyz2 - xyz1)
# on 1th vtx of each triangle
dot_prod_at = ((xyz2-xyz1)*(xyz0-xyz1)).sum(axis=-1)
cot_at = dot_prod_at / np.sqrt(bond_sqr12*bond_sqr01 - dot_prod_at**2)
sigma_20 = addim1(cot_at) * (xyz0 - xyz2)
# on 2th vtx
dot_prod_at = ((xyz0-xyz2)*(xyz1-xyz2)).sum(axis=-1)
cot_at = dot_prod_at / np.sqrt(bond_sqr12*bond_sqr02 - dot_prod_at**2)
sigma_01 = addim1(cot_at) * (xyz1 - xyz0)
#######################
# typed clusters:
type_blist = blist[vtype[blist[:, 0]] == vtype[blist[:, 1]], :]
CC2, labeled_vtx = connected_components(nvtx, type_blist)
############################################################
# horrifying monstrosity: associate every vertex with perimeter
# of the non-clustered neighbors.
# correct sigmas:
sig_12 = np.sqrt((sigma_12**2).sum(axis=-1))/2
sig_20 = np.sqrt((sigma_20**2).sum(axis=-1))/2
sig_01 = np.sqrt((sigma_01**2).sum(axis=-1))/2
vtx_perim = np.zeros(nvtx, dtype=np.float64)
add_to_1_2 = labeled_vtx[tlist[:, 1]] != labeled_vtx[tlist[:, 2]]
numpy_sum_extend(vtx_perim, tlist[add_to_1_2, 1],
sig_12[add_to_1_2])
numpy_sum_extend(vtx_perim, tlist[add_to_1_2, 2],
sig_12[add_to_1_2])
add_to_2_0 = labeled_vtx[tlist[:, 2]] != labeled_vtx[tlist[:, 0]]
numpy_sum_extend(vtx_perim, tlist[add_to_2_0, 2],
sig_20[add_to_2_0])
numpy_sum_extend(vtx_perim, tlist[add_to_2_0, 0],
sig_20[add_to_2_0])
add_to_0_1 = labeled_vtx[tlist[:, 0]] != labeled_vtx[tlist[:, 1]]
numpy_sum_extend(vtx_perim, tlist[add_to_0_1, 0],
sig_01[add_to_0_1])
numpy_sum_extend(vtx_perim, tlist[add_to_0_1, 1],
sig_01[add_to_0_1])
perim = vtx_perim.sum()/2 # every boundary is shared between two vertices
return perim
@njit(cache=True, error_model='numpy')
def segregation_factor(vtype, blist, ignore_type=4):
"""Calculate segregation factor.
p_same is the fraction of bonds between vertices of the same type, ignoring
connections to ignore_type. segregation factor is 2*p_same-1:
[0,1) represent mixed and seperated types
[-1,0) represent anti-correlated ("chekerboard") arrangement.
"""
btype = blist.copy()
btype[:, 0] = vtype[blist[:, 0]]
btype[:, 1] = vtype[blist[:, 1]]
b = btype[((btype[:, 0] != ignore_type) & (btype[:, 1] != ignore_type)), :]
p_same = (b[:, 0] == b[:, 1]).mean()
return 2*p_same-1
def segregation_factor2(vtype, blist, keep_mask=None, placeholder=999):
"""Calculate segregation factor for vertices keep_mask[v]==True.
A utility function for segregation_factor
"""
if keep_mask is None:
return segregation_factor(vtype, blist)
else:
vtype2 = vtype.copy()
vtype2[~keep_mask] = placeholder
return segregation_factor(vtype2, blist, ignore_type=placeholder)
@njit(cache=True, error_model='numpy')
def nematic_order(vtype, blist, director, is_anisotropic_vtx=8):
"""Calculate nematic order
Adds and averages CMC neighbors over their orientation
S = (3 d1@d2 - 1)/2
"""
nematic_order = np.zeros(vtype.shape, dtype=np.float64)
n = np.zeros(vtype.shape, dtype=np.int64)
for i,j in blist:
if vtype[i]&is_anisotropic_vtx and vtype[j]&is_anisotropic_vtx:
l = director[i,:]@director[j,:]
nematic_order[i]+=0.5*(3*(l**2)-1)
nematic_order[j]+=0.5*(3*(l**2)-1)
n[i]+=1
n[j]+=1
n[n==0]=1 # prevent division by 0
return nematic_order/n
#%%
###################################################
# Large numeric functions for getting statistics: #
###################################################
# njitted calculate_statistics [new, old] x [_ w ww], nonjitted get_statistics
# njitted functions: take numpy arrays extracted from vtu and return
# various statistics
# incredibly repetetive: five function due to
# slightly different argument and return types
# but they are all broadly the same
@njit(cache=True, error_model='numpy')
def calculate_statistic_new_ww(node_type, pos, blist, tlist,
bending_E, force):
"""Get statistics and typed cluster info from arrays of a new-typed .vtu.
Takes node_type[nvtx], position[nvtx,3], blist[nbonds,2], tlist[ntri, 3],
and bending_energy[nvtx], extract statistics:
returns (out, df1, df2) where
out: main statistics (volume, area, lambda1,2,3, bonding ratio,
mean curvature, perimeter,
mean cluster size, std cluster size,
force per vertex)
df1: clusters data (size, energy, lambda1 2,3, perimeter,
total force x,y,z,
sample id of one of the vertices)
df2: typed cluster data (type, size, energy, lambda1,2,3, perimeter,
total force x,y,z,
sample id of one of the vertices)
main statistics is meant to be concatenated to a cumulative dataframe
The dfs are meant to be constructed into individual pandas dataframes
(sample id: make it possible to find each cluster in the simulation later)
"""
nvtx = len(node_type)
bonding = node_type & 1 != 0
active = node_type & 2 != 0
t_normals = np.zeros((tlist.shape[0], 3))
CM_pos = pos.sum(axis=0)/nvtx # remove Center Mass
pos = pos-CM_pos
# shorthand to simplify (n)*(mxn) array operations addim1(x[:n])*y[:n,:]
def addim1(x): return np.expand_dims(x, 1) # inline plz
######################
# get volume and area:
xyz0 = pos[tlist[:, 0], :]
xyz1 = pos[tlist[:, 1], :]
xyz2 = pos[tlist[:, 2], :]
t_normals = np.cross(xyz1 - xyz0, xyz2 - xyz0)
# area = parallelogram/2 = |cross(AB,AC)|/2
double_areas = np.sqrt((t_normals**2).sum(axis=-1))
# volume: copy from c calculation
# (triangle_area * norm * radius = signed area?)
total_area = double_areas.sum()/2
total_volume = -((xyz0 + xyz1 + xyz2)*t_normals).sum()/18
##################################################
# get bounding sphere radius
bounding_R = np.sqrt(max((pos**2).sum(axis=1)))
##################################################
# get gyration eigenvalues G_mn = 1/N sum(r_n r_m)
# which is equivalent to G = (pos.T @ pos) / nvtx
gy_eig = np.linalg.eigvalsh((pos.T @ pos) / nvtx)
gy_eig.sort()
##############################################################
# get bonds with energy
# bonds_with_e = bonding[bond->vtx[0]] and bonding[bond->vtx[1]]
# nbw = sum(bonds_with_e)
nbw_nb = (bonding[blist[:, 0]] & bonding[blist[:, 1]]).sum()
nbw_nb /= (blist.shape[0])
######################################################
# mean curvature:
# new version saves it, but we need sigmas for the perimeters
# a lot harder, since we don't have the neighbors directly.
# the components of summation are,
# for each vertex i:
# sum all l_ij * cotan(theta_opposite)/2 --> rh[i]
# sum normal of triangles (to determine h sign) --> tnh[i]
# this can be done on the triangle, which have well-determined neighbors
rh = np.zeros(pos.shape)
tnh = np.zeros(pos.shape)
# summing the normals is easy, since we have them from volume/area,
# but we didn't normalize them
t_normals /= addim1(double_areas) # normalizing vectors was skipped
# add the normal to each vertex in the triangle:
# vtx_normal[tri->vtx[0]] += tri->normal. then for 1 and 2
# problematic due to repeated indices in triangles- two triangles can
# have the same vertex in 0, screwing the +=
numpy_sum_extend(tnh, tlist[:, 0], t_normals)
numpy_sum_extend(tnh, tlist[:, 1], t_normals)
numpy_sum_extend(tnh, tlist[:, 2], t_normals)
# we only need direction, tnh*rh<0, so no need to normalize
# Summing the other part is more difficult
# we go on each vertex of the triangle k=[0,1,2]
# calculate cotan(theta[k])
# and add the relevant lij*cotan(theta[k])/2 vector to rh[i!=k]
# To get cotan, we will beed bond length square
bond_sqr01 = ((xyz1-xyz0)**2).sum(axis=1)
bond_sqr02 = ((xyz2-xyz0)**2).sum(axis=1)
bond_sqr12 = ((xyz2-xyz1)**2).sum(axis=1)
# on 0th vtx of each triangle:
# numpy vectorized version of the c calculation
# cot[q] = |a||b|cos/sqrt(|a|^2|b|^2 - |a|^2|b|^2cos^2)
# |a||b|cos = a @ b
dot_prod_at = ((xyz1-xyz0)*(xyz2-xyz0)).sum(axis=-1)
cot_at = dot_prod_at / np.sqrt(bond_sqr01*bond_sqr02 - dot_prod_at**2)
# dual bond
sigma_12 = addim1(cot_at) * (xyz2 - xyz1)
# contributions to 1 and 2: +-l_12 * cot(theta[0])=+-sigma12
# (divide by 2 later)
numpy_sum_extend(rh, tlist[:, 1], sigma_12)
numpy_sum_extend(rh, tlist[:, 2], -sigma_12)
# on 1th vtx of each triangle
dot_prod_at = ((xyz2-xyz1)*(xyz0-xyz1)).sum(axis=-1)
cot_at = dot_prod_at / np.sqrt(bond_sqr12*bond_sqr01 - dot_prod_at**2)
sigma_20 = addim1(cot_at) * (xyz0 - xyz2)
# contributions to 2 and 0:
numpy_sum_extend(rh, tlist[:, 2], sigma_20)
numpy_sum_extend(rh, tlist[:, 0], -sigma_20)
# on 2th vtx
dot_prod_at = ((xyz0-xyz2)*(xyz1-xyz2)).sum(axis=-1)
cot_at = dot_prod_at / np.sqrt(bond_sqr12*bond_sqr02 - dot_prod_at**2)
sigma_01 = addim1(cot_at) * (xyz1 - xyz0)
# contributions to 1 and 2:
numpy_sum_extend(rh, tlist[:, 0], sigma_01)
numpy_sum_extend(rh, tlist[:, 1], -sigma_01)
# h per vertex, do the division by 2 we didn't do before
h = np.sqrt((rh**2).sum(axis=-1))/2
# -h if pointing the other way (maybe triangle vertex order: maybe -?)
h[(rh*tnh).sum(axis=-1) < 0] *= -1
hmean = h.sum() / (2 * total_area)
# few! that was not nice
####################################
# get force per vertex
nactive = active.sum()
if nactive == 0:
force_mag = 0
else:
force_mag = np.sqrt((force[active]**2).sum(axis=1)).sum()
force_mag /= nactive
####################################
# cluster size distribution:
if not bonding.any() and active.any():
CC, labeled_vtx = connected_components(nvtx, blist, active)
n_clusters = len(CC)
labeled_vtx[~active] = n_clusters
else:
CC, labeled_vtx = connected_components(nvtx, blist, bonding)
n_clusters = len(CC)
labeled_vtx[~bonding] = n_clusters
if n_clusters == 0:
mean_cluster_size = 0
mean_cluster_size_per_vertex = 0
std_cluster_size = np.nan
std_cluster_size_per_vertex = np.nan
perim = 0
else:
mean_cluster_size = 0.
mean_cluster_size_per_vertex = 0
std_cluster_size = 0.
std_cluster_size_per_vertex = 0.
clustered = 0
for clst in CC:
mean_cluster_size_per_vertex += len(clst)*len(clst)
mean_cluster_size += len(clst)
clustered += len(clst)
mean_cluster_size_per_vertex /= clustered
mean_cluster_size /= n_clusters
for clst in CC:
std_cluster_size_per_vertex += ((len(clst)
- mean_cluster_size_per_vertex)**2
) * len(clst)
std_cluster_size += ((len(clst) - mean_cluster_size)**2)
std_cluster_size_per_vertex /= clustered-1
std_cluster_size_per_vertex = np.sqrt(std_cluster_size_per_vertex)
std_cluster_size /= n_clusters-1
std_cluster_size = np.sqrt(std_cluster_size)
# horrifying monstrosity: associate every vertex with perimeter
# of the non-clustered neighbors
# correct sigmas:
sig_12 = np.sqrt((sigma_12**2).sum(axis=-1))/2
sig_20 = np.sqrt((sigma_20**2).sum(axis=-1))/2
sig_01 = np.sqrt((sigma_01**2).sum(axis=-1))/2
vtx_perim = np.zeros(nvtx, dtype=np.float64)
add_to_1_2 = labeled_vtx[tlist[:, 1]] != labeled_vtx[tlist[:, 2]]
numpy_sum_extend(vtx_perim, tlist[add_to_1_2, 1],
sig_12[add_to_1_2])
numpy_sum_extend(vtx_perim, tlist[add_to_1_2, 2],
sig_12[add_to_1_2])
add_to_2_0 = labeled_vtx[tlist[:, 2]] != labeled_vtx[tlist[:, 0]]
numpy_sum_extend(vtx_perim, tlist[add_to_2_0, 2],
sig_20[add_to_2_0])
numpy_sum_extend(vtx_perim, tlist[add_to_2_0, 0],
sig_20[add_to_2_0])
add_to_0_1 = labeled_vtx[tlist[:, 0]] != labeled_vtx[tlist[:, 1]]
numpy_sum_extend(vtx_perim, tlist[add_to_0_1, 0],
sig_01[add_to_0_1])
numpy_sum_extend(vtx_perim, tlist[add_to_0_1, 1],
sig_01[add_to_0_1])
perim = vtx_perim[bonding].sum()
##############################
# get statistics of individual clusters
# stats: size, perim, l, E
clst_size = np.zeros(n_clusters, dtype=np.int64)
clst_perim = np.zeros(n_clusters, dtype=np.float64)
lam = np.zeros((n_clusters, 3), dtype=np.float64)
clst_E = np.zeros(n_clusters, dtype=np.float64)
clst_sample_id = np.zeros(n_clusters, dtype=np.int64)
clst_force = np.zeros((n_clusters, 3), dtype=np.float64)
for i, clst_list in enumerate(CC):
clst = np.array(clst_list)
clst_size[i] = len(clst)
clst_E[i] = bending_E[clst].sum()
clst_pos = pos[clst]
clst_pos = clst_pos - clst_pos.sum(axis=0)/clst_size[i]
clst_gy_eig = np.linalg.eigvalsh(
(clst_pos.T @ clst_pos) / clst_size[i])
clst_gy_eig.sort()
lam[i, :] = clst_gy_eig
clst_perim[i] = vtx_perim[clst].sum()
clst_sample_id[i] = clst.min()+nvtx*clst.max()
# getitem doesn't work
for v in clst_list:
clst_force[i, :] += force[v, :]
#######################
# typed clusters:
type_blist = blist[node_type[blist[:, 0]] == node_type[blist[:, 1]], :]
CC2, labeled_vtx = connected_components(nvtx, type_blist)
n_clusters = len(CC2)
##############################
# get statistics of individual clusters
# stats: size, perim, l, E
clst_type = np.zeros(n_clusters, dtype=np.int64)
clst_size_typed = np.zeros(n_clusters, dtype=np.int64)
clst_perim_typed = np.zeros(n_clusters, dtype=np.float64)
lam_typed = np.zeros((n_clusters, 3), dtype=np.float64)
clst_E_typed = np.zeros(n_clusters, dtype=np.float64)
clst_force_typed = np.zeros((n_clusters, 3), dtype=np.float64)
clst_sample_id_typed = np.zeros(n_clusters, dtype=np.int64)
############################################################
# horrifying monstrosity: associate every vertex with perimeter
# of the non-clustered neighbors. now with types!
# correct sigmas:
# sig_12 = np.sqrt((sigma_12**2).sum(axis=-1))/2
# sig_20 = np.sqrt((sigma_20**2).sum(axis=-1))/2
# sig_01 = np.sqrt((sigma_01**2).sum(axis=-1))/2
vtx_perim = np.zeros(nvtx, dtype=np.float64)
add_to_1_2 = labeled_vtx[tlist[:, 1]] != labeled_vtx[tlist[:, 2]]
numpy_sum_extend(vtx_perim, tlist[add_to_1_2, 1],
sig_12[add_to_1_2])
numpy_sum_extend(vtx_perim, tlist[add_to_1_2, 2],
sig_12[add_to_1_2])
add_to_2_0 = labeled_vtx[tlist[:, 2]] != labeled_vtx[tlist[:, 0]]
numpy_sum_extend(vtx_perim, tlist[add_to_2_0, 2],
sig_20[add_to_2_0])
numpy_sum_extend(vtx_perim, tlist[add_to_2_0, 0],
sig_20[add_to_2_0])
add_to_0_1 = labeled_vtx[tlist[:, 0]] != labeled_vtx[tlist[:, 1]]
numpy_sum_extend(vtx_perim, tlist[add_to_0_1, 0],
sig_01[add_to_0_1])
numpy_sum_extend(vtx_perim, tlist[add_to_0_1, 1],
sig_01[add_to_0_1])
for i, clst_list in enumerate(CC2):
clst = np.array(clst_list)
clst_size_typed[i] = len(clst)
clst_E_typed[i] = bending_E[clst].sum()
clst_pos = pos[clst]
clst_pos = clst_pos - clst_pos.sum(axis=0)/clst_size_typed[i]
clst_gy_eig = np.linalg.eigvalsh(
(clst_pos.T @ clst_pos) / clst_size_typed[i])
clst_gy_eig.sort()
lam_typed[i, :] = clst_gy_eig
clst_perim_typed[i] = vtx_perim[clst].sum()
# getitem doesn't work
for v in clst_list:
clst_force_typed[i, :] += force[v, :]
clst_sample_id_typed[i] = clst.min()+nvtx*clst.max()
clst_type[i] = node_type[clst[0]]
return (
(total_volume, total_area, bounding_R,
gy_eig[0], gy_eig[1], gy_eig[2],
nbw_nb, hmean, perim, mean_cluster_size, std_cluster_size,
force_mag,
mean_cluster_size_per_vertex, std_cluster_size_per_vertex),
(clst_size, clst_E, lam[:, 0], lam[:, 1], lam[:, 2], clst_perim,
clst_force[:, 0], clst_force[:, 1], clst_force[:, 2],
clst_sample_id),
(clst_type, clst_size_typed, clst_E_typed,
lam_typed[:, 0], lam_typed[:, 1], lam_typed[:, 2],
clst_perim_typed, clst_force_typed[:, 0], clst_force_typed[:, 1],
clst_force_typed[:, 2], clst_sample_id_typed)
)
@njit(cache=True, error_model='numpy')
def calculate_statistic_new_w(node_type, pos, blist, tlist,
bending_E, force):
"""Get statistics and cluster info from arrays of a new-typed .vtu.
Takes node_type[nvtx], position[nvtx,3], blist[nbonds,2], tlist[ntri, 3],
and bending_energy[nvtx], extract statistics:
returns (out, df) where
out: main statistics (volume, area, lambda1,2,3, bonding ratio,
mean curvaturem, perimeter,
mean cluster size, std cluster size,
force per vertex)
df: clusters data (size, energy, lambda1 2,3, perimeter,
total force x, y, z,
sample id of one of the vertices)
main statistics is meant to be concatenated to a cumulative dataframe
The df is meant to be constructed into individual pandas dataframe
(sample id: make it possible to find each cluster in the simulation later)
"""
nvtx = len(node_type)
bonding = node_type & 1 != 0
active = node_type & 2 != 0
t_normals = np.zeros((tlist.shape[0], 3))
CM_pos = pos.sum(axis=0)/nvtx # remove Center Mass
pos = pos-CM_pos
# helper function (macro-like)
def addim1(x): return np.expand_dims(x, 1) # inline plz
#################################################
# get volume and area:
xyz0 = pos[tlist[:, 0], :]
xyz1 = pos[tlist[:, 1], :]
xyz2 = pos[tlist[:, 2], :]
t_normals = np.cross(xyz1 - xyz0, xyz2 - xyz0)
# area:
double_areas = np.sqrt((t_normals**2).sum(axis=-1))
# volume:
total_area = double_areas.sum()/2
total_volume = -((xyz0 + xyz1 + xyz2)*t_normals).sum()/18
##################################################
# get bounding sphere radius
bounding_R = np.sqrt(max((pos**2).sum(axis=1)))
#################################################
# get gyration eigenvalues G_mn = 1/N sum(r_n r_m)
gy_eig = np.linalg.eigvalsh((pos.T @ pos) / nvtx)
gy_eig.sort()
#################################################
# get bonds with energy
nbw_nb = (bonding[blist[:, 0]] & bonding[blist[:, 1]]).sum()
nbw_nb /= (blist.shape[0])
#################################################
# mean curvature:
rh = np.zeros(pos.shape)
tnh = np.zeros(pos.shape)
t_normals /= addim1(double_areas) # normalizing vectors was skipped
# tnh: add the normal to each vertex in the triangle:
numpy_sum_extend(tnh, tlist[:, 0], t_normals)
numpy_sum_extend(tnh, tlist[:, 1], t_normals)
numpy_sum_extend(tnh, tlist[:, 2], t_normals)
# rh:
bond_sqr01 = ((xyz1-xyz0)**2).sum(axis=1)
bond_sqr02 = ((xyz2-xyz0)**2).sum(axis=1)
bond_sqr12 = ((xyz2-xyz1)**2).sum(axis=1)
# on 0th vtx of each triangle:
dot_prod_at = ((xyz1-xyz0)*(xyz2-xyz0)).sum(axis=-1)
cot_at = dot_prod_at / np.sqrt(bond_sqr01*bond_sqr02 - dot_prod_at**2)
sigma_12 = addim1(cot_at) * (xyz2 - xyz1)
numpy_sum_extend(rh, tlist[:, 1], sigma_12)
numpy_sum_extend(rh, tlist[:, 2], -sigma_12)
# on 1th vtx of each triangle
dot_prod_at = ((xyz2-xyz1)*(xyz0-xyz1)).sum(axis=-1)
cot_at = dot_prod_at / np.sqrt(bond_sqr12*bond_sqr01 - dot_prod_at**2)
sigma_20 = addim1(cot_at) * (xyz0 - xyz2)
numpy_sum_extend(rh, tlist[:, 2], sigma_20)
numpy_sum_extend(rh, tlist[:, 0], -sigma_20)
# on 2th vtx
dot_prod_at = ((xyz0-xyz2)*(xyz1-xyz2)).sum(axis=-1)
cot_at = dot_prod_at / np.sqrt(bond_sqr12*bond_sqr02 - dot_prod_at**2)
sigma_01 = addim1(cot_at) * (xyz1 - xyz0)
numpy_sum_extend(rh, tlist[:, 0], sigma_01)
numpy_sum_extend(rh, tlist[:, 1], -sigma_01)
# h per vertex, do the division by 2 we didn't do before
h = np.sqrt((rh**2).sum(axis=-1))/2
h[(rh*tnh).sum(axis=-1) < 0] *= -1
hmean = h.sum() / (2 * total_area)
####################################
# get force per vertex
nactive = active.sum()
if nactive == 0:
force_mag = 0
else:
force_mag = np.sqrt((force[active]**2).sum(axis=1)).sum()
force_mag /= nactive
#################################################
# cluster size distribution:
if not bonding.any() and active.any():
CC, labeled_vtx = connected_components(nvtx, blist, active)
n_clusters = len(CC)
labeled_vtx[~active] = n_clusters
else:
CC, labeled_vtx = connected_components(nvtx, blist, bonding)
n_clusters = len(CC)
labeled_vtx[~bonding] = n_clusters
if n_clusters == 0:
mean_cluster_size = 0
mean_cluster_size_per_vertex = 0
std_cluster_size = np.nan
std_cluster_size_per_vertex = np.nan
perim = 0
else:
mean_cluster_size = 0.
mean_cluster_size_per_vertex = 0
std_cluster_size = 0.
std_cluster_size_per_vertex = 0.
clustered = 0
for clst in CC:
mean_cluster_size_per_vertex += len(clst)*len(clst)
mean_cluster_size += len(clst)
clustered += len(clst)
mean_cluster_size_per_vertex /= clustered
mean_cluster_size /= n_clusters
for clst in CC:
std_cluster_size_per_vertex += ((len(clst)
- mean_cluster_size_per_vertex)**2
) * len(clst)
std_cluster_size += ((len(clst) - mean_cluster_size)**2)
std_cluster_size_per_vertex /= clustered-1
std_cluster_size_per_vertex = np.sqrt(std_cluster_size_per_vertex)
std_cluster_size /= n_clusters-1
std_cluster_size = np.sqrt(std_cluster_size)
# associate every vertex with perimeter
sig_12 = np.sqrt((sigma_12**2).sum(axis=-1))/2
sig_20 = np.sqrt((sigma_20**2).sum(axis=-1))/2
sig_01 = np.sqrt((sigma_01**2).sum(axis=-1))/2
vtx_perim = np.zeros(nvtx, dtype=np.float64)
add_to_1_2 = labeled_vtx[tlist[:, 1]] != labeled_vtx[tlist[:, 2]]
numpy_sum_extend(vtx_perim, tlist[add_to_1_2, 1],
sig_12[add_to_1_2])
numpy_sum_extend(vtx_perim, tlist[add_to_1_2, 2],
sig_12[add_to_1_2])
add_to_2_0 = labeled_vtx[tlist[:, 2]] != labeled_vtx[tlist[:, 0]]
numpy_sum_extend(vtx_perim, tlist[add_to_2_0, 2],
sig_20[add_to_2_0])
numpy_sum_extend(vtx_perim, tlist[add_to_2_0, 0],
sig_20[add_to_2_0])
add_to_0_1 = labeled_vtx[tlist[:, 0]] != labeled_vtx[tlist[:, 1]]
numpy_sum_extend(vtx_perim, tlist[add_to_0_1, 0],
sig_01[add_to_0_1])
numpy_sum_extend(vtx_perim, tlist[add_to_0_1, 1],
sig_01[add_to_0_1])
perim = vtx_perim[bonding].sum()
#################################################
# get statistics of individual clusters
# stats: size, perim, l, E
clst_size = np.zeros(n_clusters, dtype=np.int64)
clst_perim = np.zeros(n_clusters, dtype=np.float64)
lam = np.zeros((n_clusters, 3), dtype=np.float64)
clst_E = np.zeros(n_clusters, dtype=np.float64)
clst_force = np.zeros((n_clusters, 3), dtype=np.float64)
clst_sample_id = np.zeros(n_clusters, dtype=np.int64)
for i, clst_list in enumerate(CC):