-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRefactored_Droplet_Class.py
More file actions
1508 lines (1120 loc) · 71.9 KB
/
Refactored_Droplet_Class.py
File metadata and controls
1508 lines (1120 loc) · 71.9 KB
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
#! This will create a Droplet_Analysis Object for a given (SINGLE) input, with all the functions we need:
import numpy as np
import os, sys
import scipy.io as sp_io
from scipy.integrate import tplquad as sp_int_tpq
from scipy.special import sph_harm
import scipy.stats as sp_stats
import scipy.sparse as sp_sprs
# For vtp plots: #!!!! REFACTOR PLOTTING !!!!#
import read_write
write_vtp_atz = read_write.write_vtp;
read_vtp_atz = read_write.read_vtp;
MY_DIR = os.path.realpath(os.path.dirname(__file__))
Output_Plots_Dir = os.path.join(MY_DIR, 'Outputs') # where we put output data
#PARENT_DIR = os.path.abspath(os.path.join(MY_DIR, os.pardir))
#CORE_DIR = os.path.join(PARENT_DIR, 'SPB_CoreCodes')
# BJG: added this here to allow this to get modules below in super-folder:
#sys.path.append("../SPB_CoreCodes")
import numpy as np
import lbdv_info_SPB as lbdv_i
import sph_func_SPB as sph_f
import manifold_SPB as mnfd
import euc_k_form_SPB as euc_kf
import plots_SPB as plts
import charts_SPB as chrts
import gdist as gd # for graph distances
# Explanation: http://www.juddzone.com/ALGORITHMS/least_squares_3D_ellipsoid.html
# Code (below): https://stackoverflow.com/questions/58501545/python-fit-3d-ellipsoid-oblate-prolate-to-3d-points
def ls_ellipsoid(xx,yy,zz):
#finds best fit ellipsoid. Found at http://www.juddzone.com/ALGORITHMS/least_squares_3D_ellipsoid.html
#least squares fit to a 3D-ellipsoid
# Ax^2 + By^2 + Cz^2 + Dxy + Exz + Fyz + Gx + Hy + Iz = 1
#
# Note that sometimes it is expressed as a solution to
# Ax^2 + By^2 + Cz^2 + 2Dxy + 2Exz + 2Fyz + 2Gx + 2Hy + 2Iz = 1
# where the last six terms have a factor of 2 in them
# This is in anticipation of forming a matrix with the polynomial coefficients.
# Those terms with factors of 2 are all off diagonal elements. These contribute
# two terms when multiplied out (symmetric) so would need to be divided by two
# change xx from vector of length N to Nx1 matrix so we can use hstack
x = xx[:,np.newaxis]
y = yy[:,np.newaxis]
z = zz[:,np.newaxis]
# Ax^2 + By^2 + Cz^2 + Dxy + Exz + Fyz + Gx + Hy + Iz = 1
J = np.hstack((x*x,y*y,z*z,x*y,x*z,y*z, x, y, z))
K = np.ones_like(x) #column of ones
#np.hstack performs a loop over all samples and creates
#a row in J for each x,y,z sample:
# J[ix,0] = x[ix]*x[ix]
# J[ix,1] = y[ix]*y[ix]
# etc.
JT=J.transpose()
JTJ = np.dot(JT,J)
InvJTJ=np.linalg.inv(JTJ);
ABC= np.dot(InvJTJ, np.dot(JT,K)) #!!!! LOOK AT RESIDUALS TO GET ELLIPSOID ERRORS !!!!#
# Rearrange, move the 1 to the other side
# Ax^2 + By^2 + Cz^2 + Dxy + Exz + Fyz + Gx + Hy + Iz - 1 = 0
# or
# Ax^2 + By^2 + Cz^2 + Dxy + Exz + Fyz + Gx + Hy + Iz + J = 0
# where J = -1
eansa=np.append(ABC,-1)
return (eansa)
# For above Ellipsoid Code:
def polyToParams3D(vec,printMe):
#gets 3D parameters of an ellipsoid. Found at http://www.juddzone.com/ALGORITHMS/least_squares_3D_ellipsoid.html
# convert the polynomial form of the 3D-ellipsoid to parameters
# center, axes, and transformation matrix
# vec is the vector whose elements are the polynomial
# coefficients A..J
# returns (center, axes, rotation matrix)
#Algebraic form: X.T * Amat * X --> polynomial form
if printMe: print('\npolynomial\n',vec)
Amat=np.array(
[
[ vec[0], vec[3]/2.0, vec[4]/2.0, vec[6]/2.0 ],
[ vec[3]/2.0, vec[1], vec[5]/2.0, vec[7]/2.0 ],
[ vec[4]/2.0, vec[5]/2.0, vec[2], vec[8]/2.0 ],
[ vec[6]/2.0, vec[7]/2.0, vec[8]/2.0, vec[9] ]
])
if printMe: print('\nAlgebraic form of polynomial\n',Amat)
#See B.Bartoni, Preprint SMU-HEP-10-14 Multi-dimensional Ellipsoidal Fitting
# equation 20 for the following method for finding the center
A3=Amat[0:3,0:3]
A3inv=np.linalg.inv(A3)
ofs=vec[6:9]/2.0
center=-np.dot(A3inv,ofs)
if printMe: print('\nCenter at:',center)
# Center the ellipsoid at the origin
Tofs=np.eye(4)
Tofs[3,0:3]=center
R = np.dot(Tofs,np.dot(Amat,Tofs.T))
if printMe: print('\nAlgebraic form translated to center\n',R,'\n')
R3=R[0:3,0:3]
R3test=R3/R3[0,0]
# print('normed \n',R3test)
s1=-R[3, 3]
R3S=R3/s1
(el,ec)=np.linalg.eig(R3S)
recip=1.0/np.abs(el)
axes=np.sqrt(recip)
if printMe: print('\nAxes are\n',axes ,'\n')
inve=np.linalg.inv(ec) #inverse is actually the transpose here
if printMe: print('\nRotation matrix\n',inve)
return (center,axes,inve, ec)
# Use level set calculated above to get normals to Ellipsoid, for computing errors (instead of PyCompadre Normals):
def Ellipsoid_Level_Set_Normals(xx,yy,zz, EllipsCoef):
#least squares fit to a 3D-ellipsoid:
# F(X) = Ax^2 + By^2 + Cz^2 + Dxy + Exz + Fyz + Gx + Hy + Iz - 1 = 0 (LEVEL SET)
# Ellipsoid Coef = [A,B,C,...]
# normals given by \grad F(X)/ \| \grad F(X) \|
A = EllipsCoef.flatten()[0]
B = EllipsCoef.flatten()[1]
C = EllipsCoef.flatten()[2]
D = EllipsCoef.flatten()[3]
E = EllipsCoef.flatten()[4]
F = EllipsCoef.flatten()[5]
G = EllipsCoef.flatten()[6]
H = EllipsCoef.flatten()[7]
I = EllipsCoef.flatten()[8]
grad_F_x = 2.*A*xx + D*yy + E*zz + G
grad_F_y = 2.*B*yy + D*xx + F*zz + H
grad_F_z = 2.*C*zz + E*xx + F*yy + I
grad_F_X = np.hstack(( grad_F_x, grad_F_y, grad_F_z ))
Vec_Norms = np.sqrt(np.sum(np.multiply(grad_F_X, grad_F_X), axis = 1)).reshape(len(xx), 1)
grad_F_X_normed = np.divide(grad_F_X, Vec_Norms)
return grad_F_X_normed
# Convert input R^3 points into Ellipsoidal coors:
def Conv_3D_pts_to_Elliptical_Coors(a0, a1, a2, point_cloud_input, LS_Ellps_Inv_Rot_Mat, LS_Ellps_center):
num_pts_used = len(point_cloud_input)
U_coors_calc = np.zeros(( num_pts_used, 1 ))
V_coors_calc = np.zeros(( num_pts_used, 1 ))
for pt_numb in range(num_pts_used):
y_tilde_pt = np.linalg.solve(LS_Ellps_Inv_Rot_Mat, point_cloud_input[pt_numb, :].reshape(3,1) - LS_Ellps_center.reshape(3, 1) )
yt_0 = y_tilde_pt[0,0]
yt_1 = y_tilde_pt[1,0]
yt_2 = y_tilde_pt[2,0]
U_pt = np.arctan2( yt_1*a0, yt_0*a1 )
if(U_pt < 0):
U_pt = U_pt + 2.*np.pi
U_coors_calc[pt_numb] = U_pt
cylinder_r = np.sqrt(yt_0**2 + yt_1**2) # r in cylinderical coors for y_tilde
cyl_r_exp = np.sqrt( (a0*np.cos(U_pt))**2 + (a1*np.sin(U_pt))**2 )
V_pt = np.arctan2( cylinder_r*a2, yt_2*cyl_r_exp )
if(V_pt < 0):
V_pt = V_pt + 2.*np.pi
V_coors_calc[pt_numb] = V_pt
return U_coors_calc, V_coors_calc
# Convert Ellipsoidal Coordinates to R^3 points:
def Conv_Elliptical_Coors_to_3D_pts(U_pts_cloud, V_pts_cloud, LS_Ellps_axes, LS_Ellps_Inv_Rot_Mat, LS_Ellps_center):
num_pts_used = len(U_pts_cloud)
X_LS_Ellps_calc_pts = np.zeros(( num_pts_used, 1 ))
Y_LS_Ellps_calc_pts = np.zeros(( num_pts_used, 1 ))
Z_LS_Ellps_calc_pts = np.zeros(( num_pts_used, 1 ))
for pt_test in range(num_pts_used): #pt_test_theta in range(num_test_pts_per_dim):
theta_tp = U_pts_cloud[pt_test, 0] #theta_pts[pt_test_theta]
phi_tp = V_pts_cloud[pt_test, 0] #phi_pts[pt_test_phi]
Test_pt = np.dot(LS_Ellps_Inv_Rot_Mat, np.multiply( np.array([ [np.sin(phi_tp)*np.cos(theta_tp)], [np.sin(phi_tp)*np.sin(theta_tp)], [np.cos(phi_tp)] ]), LS_Ellps_axes.reshape(3, 1) ) ) + LS_Ellps_center.reshape(3, 1)
X_LS_Ellps_calc_pts[pt_test, 0] = Test_pt[0, 0]
Y_LS_Ellps_calc_pts[pt_test, 0] = Test_pt[1, 0]
Z_LS_Ellps_calc_pts[pt_test, 0] = Test_pt[2, 0]
return X_LS_Ellps_calc_pts, Y_LS_Ellps_calc_pts, Z_LS_Ellps_calc_pts
# Compute H on Ellipsoid, using formula from worlfram alpha: https://mathworld.wolfram.com/Ellipsoid.html
def Mean_Curvs_on_Ellipsoid(a0, a1, a2, U_pts_calc, V_pts_calc):
'''
x = a0*sin(V)*cos(U)
y = a1*sin(V)*sin(U)
z = a2*cos(V)
'''
num_H_ellps = a0*a1*a2* ( 3.*(a0**2 + a1**2) + 2.*(a2**2) + (a0**2 + a1**2 -2.*a2**2)*np.cos(2.*V_pts_calc) - 2.*(a0**2 - a1**2)*np.cos(2.*U_pts_calc)*np.sin(V_pts_calc)**2 )
den_H_ellps = 8.*( (a0*a1*np.cos(V_pts_calc))**2 + ( a2*np.sin(V_pts_calc) )**2 * ( (a1*np.cos(U_pts_calc))**2 + (a0*np.sin(U_pts_calc))**2 ) )**1.5
H_ellps_pts = num_H_ellps/den_H_ellps
return H_ellps_pts
# return least squares harmonic fit to point cloud, given choice of basis and degree:
def Least_Squares_Harmonic_Fit(deg_fit_used, U_fit_coors, V_fit_coors, X_input_fit, Y_input_fit, Z_input_fit, Use_True_Harmonics):
All_Y_mn_pt_in = []
for n in range(deg_fit_used+1):
for m in range(-1*n, n+1):
Y_mn_coors_in = []
# we can use actual harmonics or our basis:
if(Use_True_Harmonics == True):
Y_mn_coors_in = sph_harm(m, n, U_fit_coors, V_fit_coors)
else:
Y_mn_coors_in = lbdv_i.Eval_SPH_Basis(m, n, U_fit_coors, V_fit_coors)
All_Y_mn_pt_in.append(Y_mn_coors_in)
All_Y_mn_pt_in_mat = np.hstack(( All_Y_mn_pt_in ))
X_sph_coef_vec = np.linalg.lstsq(All_Y_mn_pt_in_mat, X_input_fit)[0]
Y_sph_coef_vec = np.linalg.lstsq(All_Y_mn_pt_in_mat, Y_input_fit)[0]
Z_sph_coef_vec = np.linalg.lstsq(All_Y_mn_pt_in_mat, Z_input_fit)[0]
return X_sph_coef_vec, Y_sph_coef_vec, Z_sph_coef_vec
#!!!! BJG: REFACTOR WITH ABOVE FN !!!!#
def Least_Squares_Harmonic_Fit_SINGLE(deg_fit_used, U_fit_coors, V_fit_coors, Fn_pts_to_fit, Use_True_Harmonics):
All_Y_mn_pt_in = [] # Fit SINGLE FN INPUT
for n in range(deg_fit_used+1):
for m in range(-1*n, n+1):
Y_mn_pts_in = []
# we can use actual harmonics or our basis:
if(Use_True_Harmonics == True):
Y_mn_pts_in = sph_harm(m, n, U_fit_coors, V_fit_coors)
else:
Y_mn_pts_in = lbdv_i.Eval_SPH_Basis(m, n, U_fit_coors, V_fit_coors)
All_Y_mn_pt_in.append(Y_mn_pts_in)
All_Y_mn_pt_in_mat = np.hstack(( All_Y_mn_pt_in ))
SPH_coef_vec = np.linalg.lstsq(All_Y_mn_pt_in_mat, Fn_pts_to_fit)[0]
return SPH_coef_vec
# bump function weights on pts centered on dist_x_c:
def avg_around_pt(dist_x_c, dists_pts, vals_at_pts, max_dist_used):
dist_max = max_dist_used/20. #10. #5. #10.
pts_within_1 = np.where(abs(dists_pts - dist_x_c)<=dist_max, 1., 0.) # only look at points within 1
#sum_pts_within_1 = np.sum(pts_within_1*vals_at_pts) # we can average over all pts within 1
num_pts_within_1 = np.sum(pts_within_1) # number of points in bin
pts_vals_within_1 = np.where(abs(dists_pts - dist_x_c)<=dist_max, vals_at_pts, 0.) # only look at points within 1
#dists_within_1 = np.where(abs(dists_pts - dist_x_c)<=1., dists_pts, 0.) # only look at points within 1
weights = np.where( abs(dists_pts - dist_x_c)<=1., np.exp(1.- 1./(dist_max**2 - (dists_pts - dist_x_c)**2 )), 0. )
sum_weights = np.sum(weights.flatten())
sum_pts_within_1 = np.sum( np.multiply(pts_vals_within_1, weights).flatten() )
'''
#weights = np.exp(-1.*(pts - x_c)**2/np.log(10.))
weights = np.exp(1.- 1./(1. - (pts_within_1 - x_c)**2 ))
sum_weights = np.sum(weights.flatten())
sum_pts_weights = np.sum( np.multiply(vals_at_pts, weights).flatten() )
print("pts = "+str(pts))
print("weights = "+str(weights))
'''
return sum_pts_within_1, sum_weights #num_pts_within_1
# USE TRIANGULATION TO GET LOCAL MAX/MIN:
def local_min_max_and_dists(field_on_lbdv, dists_lbdv, tris_lbdv):
quad_fit = len(field_on_lbdv)
local_max_and_min = np.zeros_like(field_on_lbdv) # should be -1 at local min, +1 at local max, 0 otherwise, 2 if both
nearest_min_max_dists = np.zeros_like(field_on_lbdv) # 0 if not local max or min; but for local max is distance to nearest local min, vice versa
nearest_min_max_anisotropic_stress = np.zeros_like(field_on_lbdv) # 2*(H_in_max - H_in_min), from extrema to nearest partner
for pt in range(quad_fit):
H_pt = field_on_lbdv[pt, 0]
# set to True, change if False:
pt_is_local_max = True
pt_is_local_min = True
tris_containing_pt = np.where( tris_lbdv == pt )
#print("tris_containing_pt = "+str(tris_containing_pt))
rows_pt = tris_containing_pt[0]
#print("rows_pt = "+str(rows_pt))
num_occ_pt = np.count_nonzero(tris_lbdv == pt)
#print("num_occ_pt = "+str(num_occ_pt))
for row_pt in range(len(rows_pt)):
row_num_pt = rows_pt[row_pt]
#print(" row_num_pt = "+str(row_num_pt))
#print("tris_lbdv[row_num_pt, :] = "+str( tris_lbdv[row_num_pt, :] ))
# compare to other pts in triangle
for other_tri_pt_num in range(3):
other_tri_pt = int(tris_lbdv[row_num_pt, other_tri_pt_num])
#print("other_tri_pt = "+str(other_tri_pt))
H_other_tri_pt = field_on_lbdv[other_tri_pt, 0]
if(H_other_tri_pt < H_pt):
pt_is_local_min = False
if(H_other_tri_pt > H_pt):
pt_is_local_max = False
if(pt_is_local_max == True):
if(pt_is_local_min == True):
local_max_and_min[pt] = 2 # local max AND min
else:
local_max_and_min[pt] = 1 # local max (only)
elif(pt_is_local_min == True):
local_max_and_min[pt] = -1 # local min (only)
else:
local_max_and_min[pt] = 0
num_local_max = np.count_nonzero(local_max_and_min == 1)
num_local_min = np.count_nonzero(local_max_and_min == -1)
num_local_both = np.count_nonzero(local_max_and_min == 2)
#print(str(num_local_max)+" local max, "+str(num_local_min)+" local min, "+str(num_local_both)+" that are both")
local_max_inds = np.where( local_max_and_min == 1 )[0]
local_min_inds = np.where( local_max_and_min == -1 )[0]
#print("local_max_inds = "+str(local_max_inds))
# list of ALL local min/max pairs' distances and difference in input fields:
All_local_min_max_pairs_anisotropies = []
All_local_min_max_pairs_distances = []
for pt_max in range(num_local_max):
pt_max_num = local_max_inds[pt_max]
local_max_field = field_on_lbdv[pt_max_num, 0]
for pt_min in range(num_local_min):
pt_min_num = local_min_inds[pt_min]
local_min_field = field_on_lbdv[pt_min_num, 0]
dist_max_min_pt = dists_lbdv[pt_min_num, pt_max_num]
Anisotropy_max_min_pts = local_max_field - local_min_field
All_local_min_max_pairs_anisotropies.append(Anisotropy_max_min_pts)
All_local_min_max_pairs_distances.append(dist_max_min_pt)
All_local_min_max_pairs_anisotropies = np.array(All_local_min_max_pairs_anisotropies, dtype=np.dtype('d'))
All_local_min_max_pairs_distances = np.array(All_local_min_max_pairs_distances, dtype=np.dtype('d'))
for pt_max in range(num_local_max):
pt_max_num = local_max_inds[pt_max]
#print("pt_max_num = "+str(pt_max_num))
local_min_dists_to_pt = dists_lbdv[pt_max_num, local_min_inds]
#print("local_min_dists_to_pt = "+str(local_min_dists_to_pt))
min_dist_to_local_min = min(local_min_dists_to_pt)
nearest_min_max_dists[pt_max_num] = min_dist_to_local_min
#print("min_dist_to_local_min = "+str(min_dist_to_local_min))
# Calculate 2*(H_max - H_nearest_min):
ind_in_list_of_nearest_min = np.argwhere(local_min_dists_to_pt == min_dist_to_local_min)
pt_num_of_nearest_min = local_min_inds[ind_in_list_of_nearest_min][0,0]
#print("ind_in_list_of_nearest_min = "+str(ind_in_list_of_nearest_min)+", pt_num_of_nearest_min = "+str(pt_num_of_nearest_min))
nearest_min_max_anisotropic_stress[pt_max_num] = ( field_on_lbdv[pt_max_num, 0] - field_on_lbdv[pt_num_of_nearest_min, 0] )
#print("nearest_min_max_anisotropic_stress[pt_max_num, 0] = "+str( nearest_min_max_anisotropic_stress[pt_max_num, 0] ))
for pt_min in range(num_local_min):
pt_min_num = local_min_inds[pt_min]
#print("pt_mib_num = "+str(pt_min_num))
local_max_dists_to_pt = dists_lbdv[pt_min_num, local_max_inds]
#print("local_max_dists_to_pt = "+str(local_max_dists_to_pt))
max_dist_to_local_min = min(local_max_dists_to_pt)
nearest_min_max_dists[pt_min_num] = max_dist_to_local_min
#print("max_dist_to_local_min = "+str(max_dist_to_local_min))
# Calculate 2*(H_min - H_nearest_max):
ind_in_list_of_nearest_max = np.argwhere(local_max_dists_to_pt == max_dist_to_local_min)
pt_num_of_nearest_max = local_max_inds[ind_in_list_of_nearest_max][0,0]
#print("ind_in_list_of_nearest_max = "+str(ind_in_list_of_nearest_max)+", pt_num_of_nearest_max = "+str(pt_num_of_nearest_max))
nearest_min_max_anisotropic_stress[pt_min_num] = ( field_on_lbdv[pt_num_of_nearest_max, 0] - field_on_lbdv[pt_min_num, 0] )
#print("nearest_min_max_anisotropic_stress[pt_min_num, 0] = "+str( nearest_min_max_anisotropic_stress[pt_min_num, 0] ))
return local_max_and_min, nearest_min_max_dists, nearest_min_max_anisotropic_stress, All_local_min_max_pairs_distances, All_local_min_max_pairs_anisotropies
# CDF Analysis for distribution:
def CDF_Analysis_of_Data(Input_Data_Field, delta_prob_extrema_exlc):
sorted_Data_Field = np.sort(Input_Data_Field.flatten())
# from: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.rv_histogram.html
hist_Data_Field = np.histogram(sorted_Data_Field, bins='auto', density=True)
hist_dist = sp_stats.rv_histogram(hist_Data_Field)
min_val_excl_Data_Field = hist_dist.ppf(delta_prob_extrema_exlc)
max_val_excl_Data_Field = hist_dist.ppf(1. - delta_prob_extrema_exlc)
curv_pts_excluded_Data_Field = np.zeros_like(sorted_Data_Field) # 0: where within .5- \delta of median, -1, where CDF<\delta, 1 where CDF>1-\delta
curv_pts_excluded_Data_Field = np.where(sorted_Data_Field < min_val_excl_Data_Field, -1, curv_pts_excluded_Data_Field)
curv_pts_excluded_Data_Field = np.where(sorted_Data_Field > max_val_excl_Data_Field, 1, curv_pts_excluded_Data_Field)
return min_val_excl_Data_Field, max_val_excl_Data_Field, curv_pts_excluded_Data_Field, hist_dist
# calculate correlations from input vecs (at same pts), and dists mats:
def Corrs_From_Input_Tris_and_Dists(Input_Vec1, Input_Vec2, dist_mat_input, num_spatial_pts, corr_start_dist_input):
'''
Upper_dist_lbdv = np.triu(dist_mat_input, 0).flatten() # we want main diagonal, and below zero'd
dist_0_args_lbdv = np.sort(np.where(Upper_dist_lbdv <= 0.))[::-1] # we want to find where distance is 0, to delete this
dists_lbdv_non0 = np.delete(Upper_dist_lbdv, dist_0_args_lbdv)
Input_vec1_pts = Input_Vec1.reshape(num_spatial_pts, 1)
Input_vec2_pts = Input_Vec2.reshape(num_spatial_pts, 1)
Corr_outer_prod_mat_pts = np.dot( Input_vec1_pts, Input_vec2_pts.T )
Upper_Corr_outer_prod_pts = np.triu(Corr_outer_prod_mat_pts, 0).flatten()
Corr_non0_pts = np.delete(Upper_Corr_outer_prod_pts, dist_0_args_lbdv)
inds_dists_lbdv_sorted = dists_lbdv_non0.argsort()
Corr_non0_pts_sorted = Corr_non0_pts[inds_dists_lbdv_sorted]
dists_lbdv_non0_sorted = np.sort(dists_lbdv_non0)
max_dist_lbdv = int(np.floor(dists_lbdv_non0_sorted.flatten()[-1]))
'''
dists_lbdv_non0 = dist_mat_input[np.triu_indices(num_spatial_pts)]
max_dist_lbdv = int(np.floor(max(dists_lbdv_non0)))
Corr_outer_prod_mat_pts = np.dot(Input_Vec1.reshape(num_spatial_pts, 1), Input_Vec2.reshape(num_spatial_pts, 1).T )
Corr_non0_pts = Corr_outer_prod_mat_pts[np.triu_indices(num_spatial_pts)]
Self_corrs_diag = np.diag(Corr_outer_prod_mat_pts)
auto_corr_norm = np.average(Self_corrs_diag.flatten(), axis=0) # norm, so auto-corrs are 1 at \delta (= |x - x'|) = 0
'''
print("dist_mat_input = "+str(dist_mat_input))
print("dists_lbdv_non0 = "+str(dists_lbdv_non0))
print("Corr_outer_prod_mat_pts = "+str(Corr_outer_prod_mat_pts))
print("Corr_non0_pts = "+str(Corr_non0_pts))
print("Self_corrs_diag = "+str(Self_corrs_diag))
sys.exit()
'''
avg_mean_curv_auto_corrs = []
dists_used = []
corr_start_dist = int(corr_start_dist_input) # where we START LOOKING AT AUTO-CORRS (Since there is a huge spike at 0)
#print("dists_lbdv_non0_sorted = "+str(dists_lbdv_non0_sorted))
#print("Corr_non0_pts_sorted = "+str(Corr_non0_pts_sorted))
for dist_i in range(corr_start_dist,max_dist_lbdv+1):
#mean_curv_corr_d_i = bump_fn_around_x_avg(dist_i, dists_lbdv_non0_sorted, Corr_non0_pts_sorted)
sum_mean_curv_corr_d_i, num_mean_curv_corr_d_i = avg_around_pt(dist_i, dists_lbdv_non0, Corr_non0_pts, max_dist_lbdv)
if(num_mean_curv_corr_d_i > 0):
mean_curv_corr_d_i = sum_mean_curv_corr_d_i/num_mean_curv_corr_d_i # average in bin
if(abs(mean_curv_corr_d_i)/auto_corr_norm <= 1.): # include if corr <= 1
avg_mean_curv_auto_corrs.append(mean_curv_corr_d_i)
dists_used.append(dist_i)
'''
else: #if(abs(mean_curv_corr_d_i/auto_corr_norm) >= 1.):
print("for dist = "+str(dist_i)+", mean_curv_corr_d_i = "+str(mean_curv_corr_d_i)+", num_mean_curv_corr_d_i = "+str(num_mean_curv_corr_d_i)+", normed corr_i = "+str( mean_curv_corr_d_i/auto_corr_norm ))
'''
'''
if(dist_i == 1):
print("\n"+"dist_i = "+str(dist_i))
print("mean_curv_corr_d_i = "+str(mean_curv_corr_d_i))
print("avg_mean_curv_auto_corrs = "+str(avg_mean_curv_auto_corrs))
print("dists_used = "+str(dists_used))
print("\n")
sys.exit()
'''
num_dists_used = len(dists_used)
num_corrs_used = len(avg_mean_curv_auto_corrs)
if(num_dists_used != num_corrs_used):
print("Error: num_corrs_used = "+str(num_corrs_used)+", num_dists_used = "+str(num_dists_used))
sys.exit()
# We get (geodesic) distances we calculate correlations on, using bump fn averages around these distances:
auto_corrs_microns_dists = np.array( dists_used, dtype=np.dtype('d')).reshape(num_dists_used, 1)
auto_corrs_avg = np.array( avg_mean_curv_auto_corrs, dtype=np.dtype('d')).reshape(num_dists_used, 1)
auto_corrs_avg_normed = auto_corrs_avg / auto_corr_norm
'''
print("auto_corrs_microns_dists = "+str(auto_corrs_microns_dists))
print("auto_corrs_avg = "+str(auto_corrs_avg))
print("auto_corr_norm = "+str(auto_corr_norm))
print("auto_corrs_avg_normed = "+str(auto_corrs_avg_normed))
sys.exit()
'''
return corr_start_dist, auto_corrs_microns_dists, auto_corrs_avg, auto_corr_norm, auto_corrs_avg_normed
# geodesic (Great Circle) distance on unit sphere, from haversine formula:
# From: https://en.wikipedia.org/wiki/Haversine_formula
def Haversine_dists_S2_lbdv(deg_lbdv, num_lbdv_pts):
#num_lbdv_pts = LBDV_Input.lbdv_quad_pts
LBDV_Input = lbdv_i.lbdv_info(deg_lbdv, num_lbdv_pts)
dists = np.zeros(( num_lbdv_pts, num_lbdv_pts ))
for pt_1 in range(num_lbdv_pts):
theta_1 = LBDV_Input.theta_pts[pt_1, 0]
phi_1 = LBDV_Input.phi_pts[pt_1, 0]
lat_1 = np.pi - phi_1 # latitude
for pt_2 in range(pt_1+1, num_lbdv_pts): # dist with self is 0
theta_2 = LBDV_Input.theta_pts[pt_2, 0]
phi_2 = LBDV_Input.phi_pts[pt_2, 0]
lat_2 = np.pi - phi_2 # latitude
lat_diff = lat_2 - lat_1
long_diff = theta_2 - theta_1
h = np.sin(lat_diff/2.)**2 + np.cos(lat_1)*np.cos(lat_2)*(np.sin(long_diff**2/2.)**2)
d_12 = np.arctan2(np.sqrt(h),np.sqrt(1. -h))
dists[pt_1, pt_2] = d_12
dists[pt_2, pt_1] = d_12
return dists
# Create class for SINGLE analyzed Droplet Point Cloud:
class Droplet_Analysis(object):
def __init__(self, Droplet_Input_Dict):
self.Drop_Info_Dict = {} # create dictionary to centralize info storage
# Load specified .mat file, read in point cloud and Elijah's calculation of mean curvature:
self.MatFile_filepath = Droplet_Input_Dict['MatFile_filepath'] # relative path to folder with .mat files
self.MatFile_name = Droplet_Input_Dict['MatFile_name'] # name of .mat file (WITH extension)
mat_file_path = os.path.join(self.MatFile_filepath, self.MatFile_name) #os.path.join(MAT_Files_Dir, mat_file_name)
mat_contents = sp_io.loadmat(mat_file_path)
self.Neha_Analysis = False # default
# If we have input .mat file of type 'XYZ_H_and_pixelSize'
if 'data' in mat_contents.keys(): # 'px_sz_um'
self.pixel_size_microns = mat_contents['data']['px_sz_um'][0, 0][0, 0] # INPUT HERE in this case, ignores input from batch script
self.mean_curvs_input = mat_contents['data']['H_inv_um'][0, 0]
self.point_cloud_input = mat_contents['data']['XYZ_um'][0, 0]
#!!!! For NEHA ANALYSIS !!!!#
if(Droplet_Input_Dict['Neha_Radial_analysis']):
#print("mat_contents['data']['drop_center_XYZ_orig_px'][0, 0] = "+str( mat_contents['data']['drop_center_XYZ_orig_px'][0, 0] ))
#print("mat_contents['data']['EK_center_XYZ_orig_px'][0, 0] = "+str( mat_contents['data']['EK_center_XYZ_orig_px'][0, 0] ))
#print("mat_contents['data']['drop_center_XYZ_um'][0, 0] = "+str( mat_contents['data']['drop_center_XYZ_um'][0, 0] ))
#print("mat_contents['data']['EK_center_XYZ_um'][0, 0] = "+str( mat_contents['data']['EK_center_XYZ_um'][0, 0] ))
self.Neha_Rad_Vec = mat_contents['data']['drop_center_XYZ_um'][0, 0] - mat_contents['data']['EK_center_XYZ_um'][0, 0]
self.len_Neha_Rad_Vec = np.linalg.norm(self.Neha_Rad_Vec, 2)
# examine and plot point closest to EK:
#neha_vec_on_coords = np.sum(np.multiply(self.point_cloud_input, -1.*self.Neha_Rad_Vec.reshape(1, 3) ), axis=1)
#index_closest_EK = np.argmax(neha_vec_on_coords)
self.Closest_EK_pt_vec = np.zeros_like( self.point_cloud_input )
#self.Closest_EK_pt_vec[index_closest_EK, :] = -1.*self.Neha_Rad_Vec.flatten()
#print("self.Closest_EK_pt_vec[index_closest_EK, :] = "+str( self.Closest_EK_pt_vec[index_closest_EK, :] ))
self.Closest_EK_pt_vec[:, 0] = -1.*self.Neha_Rad_Vec.flatten()[0]
self.Closest_EK_pt_vec[:, 1] = -1.*self.Neha_Rad_Vec.flatten()[1]
self.Closest_EK_pt_vec[:, 2] = -1.*self.Neha_Rad_Vec.flatten()[2]
self.Neha_Analysis = True
# If we have input .mat file of type 'justCoordinatesAndCurvatures00...'
else:
self.mean_curvs_input = mat_contents['meanCurvatureArray']
self.point_cloud_input = mat_contents['pointCloudArray']
# We input physical values from experiments, so we can put units on outputs:
self.pixel_size_microns = Droplet_Input_Dict['Pixel_Size_Microns'] # 1., by default, number of microns per pixel length, so we can multiply by input {X, Y, Z}
# ADJUST Inputs, to Microns:
self.point_cloud_input = self.point_cloud_input*self.pixel_size_microns # lengths are in microns
self.mean_curvs_input = self.mean_curvs_input/self.pixel_size_microns # curvatures are now in 1/microns
if(Droplet_Input_Dict['Neha_Radial_analysis']):
self.Neha_Analysis = True
# Tension: \gamma =.5, by default, so we multiply by this to get anistropic tension for curvature
self.TwoGammaVal = 2.*Droplet_Input_Dict['Tension_gamma_val']
# Plotting File_Path, create sub-dir for this if it doesnt exist:
self.Output_Plot_Subfolder_Name = Droplet_Input_Dict['Output_Plot_Subfolder_Name'] # subfolder we put this plot it (to group sequences of inputs)
self.Output_Plot_Subfolder_path = os.path.join(Output_Plots_Dir, self.Output_Plot_Subfolder_Name)
if not os.path.exists(self.Output_Plot_Subfolder_path):
os.makedirs(self.Output_Plot_Subfolder_path)
# for 1st vtu plot:
Input_Plot_name = "Plot_Inputs_"+str(self.MatFile_name).replace("mat", "vtp") # output plotting
self.Input_Plot_file_path = os.path.join(self.Output_Plot_Subfolder_path, Input_Plot_name)
# for 2nd vtu plot:
Least_Square_Ellps_Plot_name = "Least_Sq_Ellpsoid_"+str(self.MatFile_name).replace("mat", "vtp") # output plotting
self.Least_Square_Ellps_Plot_file_path = os.path.join(self.Output_Plot_Subfolder_path, Least_Square_Ellps_Plot_name)
# for 3rd vtu plot:
SPH_fit_UV_Plot_name = "SPH_fit_UV_"+str(self.MatFile_name).replace("mat", "vtp") # output plotting
self.SPH_UV_fit_Plot_file_path = os.path.join(self.Output_Plot_Subfolder_path, SPH_fit_UV_Plot_name)
# for 4th vtu plot:
SPH_fit_lbdv_Plot_name = "SPH_fit_LBDV_"+str(self.MatFile_name).replace("mat", "vtp") # output plotting
self.SPH_lbdv_fit_Plot_file_path = os.path.join(self.Output_Plot_Subfolder_path, SPH_fit_lbdv_Plot_name)
# for 5th vtu plot:
SPH_fit_lbdv_ellps_Plot_name = "SPH_fit_ELLPS_LBDV_"+str(self.MatFile_name).replace("mat", "vtp") # output plotting
self.SPH_lbdv_ellps_fit_Plot_file_path = os.path.join(self.Output_Plot_Subfolder_path, SPH_fit_lbdv_ellps_Plot_name)
# for plotting EK and drop center:
EK_DC_Plot_name = "Plot_EK_DC_"+str(self.MatFile_name).replace("mat", "vtp") # output plotting
self.EK_DC_Plot_file_path = os.path.join(self.Output_Plot_Subfolder_path, EK_DC_Plot_name)
# Get info from point cloud input for calculations we want to do:
self.H0_avg_input_curvs = np.average(self.mean_curvs_input, axis=0) #1st way we estimate H0: mean of input curvatures Elijah calculated
self.num_pts = len(self.mean_curvs_input)
self.avg_position_in = np.average(self.point_cloud_input, axis = 0)
self.X_orig_in, self.Y_orig_in, self.Z_orig_in = np.hsplit(self.point_cloud_input, 3)
self.point_cloud_adj = self.point_cloud_input - self.avg_position_in # CENTER point cloud (for orientation and volume calculations)
self.X_adj_in, self.Y_adj_in, self.Z_adj_in = np.hsplit(self.point_cloud_adj, 3)
# We can use PyCompadre to analyze droplet point cloud directly:
self.Use_PyCompadre = Droplet_Input_Dict['Use_PyCompadre']
if(self.Use_PyCompadre == True):
import CalcMeanCurvOfInput as CMI # for esimating curvatures from point cloud (NEEDS PYTHON 3.7 for PyCompadre)
self.pycompadre_p_order = Droplet_Input_Dict['PyCompadre_p_order'] # It's best to use degree 2
self.PyComp_Mean_Curvs, self.PyCompadre_Normals_used = CMI.calc_grad_point_cloud(self.point_cloud_adj, self.pycompadre_p_order)
#2nd way we estimate H0: mean of input curvatures PyCompadre calculated:
self.H0_avg_PyCompadre_curvs = np.average(self.PyComp_Mean_Curvs, axis=0)
# get LS Ellipsoid estimate:
self.LS_Ellps_fit_Coef = ls_ellipsoid(self.X_orig_in.flatten(), self.Y_orig_in.flatten(), self.Z_orig_in.flatten() )
self.LS_Ellps_center, self.LS_Ellps_axes, self.LS_Ellps_Rot_Mat, self.LS_Ellps_Inv_Rot_Mat = polyToParams3D(self.LS_Ellps_fit_Coef, False) #get ellipsoid 3D parameters
# Put our point cloud in LS Ellipsoid coordinates:
self.U_coors_pts_in = np.zeros_like(self.mean_curvs_input)
self.V_coors_pts_in = np.zeros_like(self.mean_curvs_input)
a0 = self.LS_Ellps_axes[0] #semi-axis lengths
a1 = self.LS_Ellps_axes[1]
a2 = self.LS_Ellps_axes[2]
self.U_coors_pts_in, self.V_coors_pts_in = Conv_3D_pts_to_Elliptical_Coors(a0, a1, a2, self.point_cloud_input, self.LS_Ellps_Inv_Rot_Mat, self.LS_Ellps_center)
self.LS_Ellps_Mean_Curvs = Mean_Curvs_on_Ellipsoid(a0, a1, a2, self.U_coors_pts_in, self.V_coors_pts_in)
# Compute Anis Stress/ Cell Stress from INPUT Curvatures:
self.Anis_Stress_pts_UV_input = self.TwoGammaVal*self.mean_curvs_input
H0_ellps_avg_ellps_UV_curvs = np.average(self.LS_Ellps_Mean_Curvs, axis=0) # 1st method of H0 computation, for Ellipsoid in UV points
self.Anis_Cell_Stress_pts_UV_input = self.TwoGammaVal*(self.mean_curvs_input - self.LS_Ellps_Mean_Curvs - (self.H0_avg_input_curvs - H0_ellps_avg_ellps_UV_curvs))
# See LS ellipsoid in R^3 coordinates, compare to input points:
self.X_LS_Ellps_pts, self.Y_LS_Ellps_pts, self.Z_LS_Ellps_pts = Conv_Elliptical_Coors_to_3D_pts(self.U_coors_pts_in, self.V_coors_pts_in, self.LS_Ellps_axes, self.LS_Ellps_Inv_Rot_Mat, self.LS_Ellps_center)
# OUTWARD Normal vectors of Ellipsoid:
self.LS_Ellps_normals = Ellipsoid_Level_Set_Normals(self.X_LS_Ellps_pts, self.Y_LS_Ellps_pts, self.Z_LS_Ellps_pts, self.LS_Ellps_fit_Coef)
LS_Ellps_Err_X = self.X_LS_Ellps_pts - self.X_orig_in
LS_Ellps_Err_Y = self.Y_LS_Ellps_pts - self.Y_orig_in
LS_Ellps_Err_Z = self.Z_LS_Ellps_pts - self.Z_orig_in
self.LS_Ellps_Err_vecs = np.hstack(( LS_Ellps_Err_X, LS_Ellps_Err_Y, LS_Ellps_Err_Z ))
# We want to find largest Ellipsoid Axis to compare with final Droplet orientation:
axis_1 = np.vstack(( Conv_Elliptical_Coors_to_3D_pts( np.array([[0.]]), np.array([[np.pi/2.]]), self.LS_Ellps_axes, self.LS_Ellps_Inv_Rot_Mat, np.zeros_like(self.LS_Ellps_center)) )) # x-axis
len_axis_1 = np.linalg.norm(axis_1)
axis_2 = np.vstack(( Conv_Elliptical_Coors_to_3D_pts( np.array([[np.pi/2.]]), np.array([[np.pi/2.]]), self.LS_Ellps_axes, self.LS_Ellps_Inv_Rot_Mat, np.zeros_like(self.LS_Ellps_center)) )) # y_axis
len_axis_2 = np.linalg.norm(axis_2)
axis_3 = np.vstack(( Conv_Elliptical_Coors_to_3D_pts( np.array([[0.]]), np.array([[0.]]), self.LS_Ellps_axes, self.LS_Ellps_Inv_Rot_Mat, np.zeros_like(self.LS_Ellps_center)) )) # z_axis
len_axis_3 = np.linalg.norm(axis_3)
self.Maj_Min_Axis = np.zeros((3)) # 1 for major axis, -1 for minor axis, for {X,Y,Z}
if(len_axis_1 >= len_axis_2 and len_axis_1 >= len_axis_3):
self.major_orientation_axis = axis_1
self.Maj_Min_Axis[0] = 1.
self.ellps_semi_axis_a = len_axis_1
if(len_axis_2 <= len_axis_3):
self.Maj_Min_Axis[1] = -1.
self.ellps_semi_axis_c = len_axis_2
self.minor_orientation_axis = axis_2
self.ellps_semi_axis_b = len_axis_3
self.medial_orientation_axis = axis_3
else:
self.Maj_Min_Axis[2] = -1.
self.ellps_semi_axis_c = len_axis_3
self.minor_orientation_axis = axis_3
self.ellps_semi_axis_b = len_axis_2
self.medial_orientation_axis = axis_2
elif(len_axis_2 >= len_axis_1 and len_axis_2 >= len_axis_3):
self.major_orientation_axis = axis_2
self.Maj_Min_Axis[1] = 1.
self.ellps_semi_axis_a = len_axis_2
if(len_axis_1 <= len_axis_3):
self.Maj_Min_Axis[0] = -1.
self.ellps_semi_axis_c = len_axis_1
self.minor_orientation_axis = axis_1
self.ellps_semi_axis_b = len_axis_3
self.medial_orientation_axis = axis_3
else:
self.Maj_Min_Axis[2] = -1.
self.ellps_semi_axis_c = len_axis_3
self.minor_orientation_axis = axis_3
self.ellps_semi_axis_b = len_axis_1
self.medial_orientation_axis = axis_1
elif(len_axis_3 >= len_axis_2 and len_axis_3 >= len_axis_1):
self.major_orientation_axis = axis_3
self.Maj_Min_Axis[2] = 1.
self.ellps_semi_axis_a = len_axis_3
if(len_axis_2 <= len_axis_1):
self.Maj_Min_Axis[1] = -1.
self.ellps_semi_axis_c = len_axis_2
self.minor_orientation_axis = axis_2
self.ellps_semi_axis_b = len_axis_1
self.medial_orientation_axis = axis_1
else:
self.Maj_Min_Axis[0] = -1.
self.ellps_semi_axis_c = len_axis_1
self.minor_orientation_axis = axis_1
self.ellps_semi_axis_b = len_axis_2
self.medial_orientation_axis = axis_2
else:
print("\n"+"Error, major axis not discerned"+"\n")
# Get matrix to change basis from cartesian to ellipsoidal coors:
self.A_basis_mat = np.zeros((3, 3))
self.A_basis_mat[0,0] = self.major_orientation_axis[0, 0] /self.ellps_semi_axis_a
self.A_basis_mat[0,1] = self.major_orientation_axis[1, 0] /self.ellps_semi_axis_a
self.A_basis_mat[0,2] = self.major_orientation_axis[2, 0] /self.ellps_semi_axis_a
self.A_basis_mat[1,0] = self.medial_orientation_axis[0, 0] /self.ellps_semi_axis_b
self.A_basis_mat[1,1] = self.medial_orientation_axis[1, 0] /self.ellps_semi_axis_b
self.A_basis_mat[1,2] = self.medial_orientation_axis[2, 0] /self.ellps_semi_axis_b
self.A_basis_mat[2,0] = self.minor_orientation_axis[0, 0] /self.ellps_semi_axis_c
self.A_basis_mat[2,1] = self.minor_orientation_axis[1, 0] /self.ellps_semi_axis_c
self.A_basis_mat[2,2] = self.minor_orientation_axis[2, 0] /self.ellps_semi_axis_c
self.H_ellps_e_1 = self.ellps_semi_axis_a/(2.*self.ellps_semi_axis_b**2) + self.ellps_semi_axis_a/(2.*self.ellps_semi_axis_c**2)
self.H_ellps_e_2 = self.ellps_semi_axis_b/(2.*self.ellps_semi_axis_a**2) + self.ellps_semi_axis_b/(2.*self.ellps_semi_axis_c**2)
self.H_ellps_e_3 = self.ellps_semi_axis_c/(2.*self.ellps_semi_axis_a**2) + self.ellps_semi_axis_c/(2.*self.ellps_semi_axis_b**2)
#!!!! ASSUMES WE USE PYCOMPADRE, could redo with signed dists to center !!!!#
#self.LS_Ellps_Err_signed = -1.* np.sum( np.multiply(self.PyCompadre_Normals_used, self.LS_Ellps_Err_vecs), axis = 1 )
self.LS_Ellps_Err_signed = -1.*np.sum( np.multiply(self.LS_Ellps_normals, self.LS_Ellps_Err_vecs), axis = 1 )
# Least-Squares Fit to Harmonics in {X,Y,Z}, using ellipsoidal coordinates:
self.deg_fit_lbdv = Droplet_Input_Dict['deg_lbdv_fit'] # degree of harmonics we use:
if(Droplet_Input_Dict['MAX_lbdv_fit_PTS'] == True): # If we use 5810 points, ...
self.num_lbdv_pts_fit = 5810
else: # ... or hyper-interpolation quad pts
self.num_lbdv_pts_fit = lbdv_i.look_up_lbdv_pts(self.deg_fit_lbdv+1)
LBDV_Fit = lbdv_i.lbdv_info(self.deg_fit_lbdv, self.num_lbdv_pts_fit)
#min_num_quad_pts_deg = lbdv_i.look_up_lbdv_pts(self.deg_fit_lbdv+1) # use the min number of quad_pts possible
#LBDV_Ellps = lbdv_i.lbdv_info(self.deg_fit_lbdv, min_num_quad_pts_deg)
# get info we need from Ellipsoid data fit in lbdv:
self.H0_Ellpsoid, self.Gauss_Bonnet_Rel_Err_Ellps, self.H_Ellps_Manny_lbdv_pts, self.X_lbdv_ellps_pts, self.Y_lbdv_ellps_pts, self.Z_lbdv_ellps_pts = self.Ellipsoid_LBDV(LBDV_Fit)
# use H0_Ellpsoid to calculate tissue stress projections:
self.sigma_11_e = self.TwoGammaVal*(self.H_ellps_e_1 - self.H0_Ellpsoid)
self.sigma_22_e = self.TwoGammaVal*(self.H_ellps_e_2 - self.H0_Ellpsoid)
self.sigma_33_e = self.TwoGammaVal*(self.H_ellps_e_3 - self.H0_Ellpsoid)
'''
print("self.H_ellps_e_1 = "+str(self.H_ellps_e_1))
print("self.H_ellps_e_2 = "+str(self.H_ellps_e_2))
print("self.H_ellps_e_3 = "+str(self.H_ellps_e_3))
print("self.H0_Ellpsoid = "+str(self.H0_Ellpsoid))
print("self.sigma_11_e = "+str(self.sigma_11_e))
print("self.sigma_22_e = "+str(self.sigma_22_e))
print("self.sigma_33_e = "+str(self.sigma_33_e))
'''
# tissue stress tensor, elliptical coors:
self.Tissue_Stress_Tens_Ellp_Coors = np.zeros((3,3))
self.Tissue_Stress_Tens_Ellp_Coors[0,0] = self.sigma_11_e
self.Tissue_Stress_Tens_Ellp_Coors[1,1] = self.sigma_22_e
self.Tissue_Stress_Tens_Ellp_Coors[2,2] = self.sigma_33_e
tr_sigma_ellps = self.sigma_11_e + self.sigma_22_e + self.sigma_33_e
# cartesian tissue stress tensor:
self.Tissue_Stress_Tens_Cart_Coors = np.dot( np.dot(self.A_basis_mat.T ,self.Tissue_Stress_Tens_Ellp_Coors), self.A_basis_mat)
self.sigma_11_tissue_x = self.Tissue_Stress_Tens_Cart_Coors[0,0]
self.sigma_22_tissue_y = self.Tissue_Stress_Tens_Cart_Coors[1,1]
self.sigma_33_tissue_z = self.Tissue_Stress_Tens_Cart_Coors[2,2]
'''
print("self.Tissue_Stress_Tens_Cart_Coors = "+str(self.Tissue_Stress_Tens_Cart_Coors))
tr_sigma_cart = self.sigma_11_tissue_x + self.sigma_22_tissue_y + self.sigma_33_tissue_z
print("tr_sigma_ellps = "+str(tr_sigma_ellps)+", tr_sigma_cart = "+str(tr_sigma_cart) )
sys.exit()
'''
X_fit_sph_coef_vec, Y_fit_sph_coef_vec, Z_fit_sph_coef_vec = Least_Squares_Harmonic_Fit(self.deg_fit_lbdv, self.U_coors_pts_in, self.V_coors_pts_in, self.X_orig_in, self.Y_orig_in, self.Z_orig_in, False)
X_fit_sph_coef_mat = sph_f.Un_Flatten_Coef_Vec(X_fit_sph_coef_vec, self.deg_fit_lbdv)
Y_fit_sph_coef_mat = sph_f.Un_Flatten_Coef_Vec(Y_fit_sph_coef_vec, self.deg_fit_lbdv)
Z_fit_sph_coef_mat = sph_f.Un_Flatten_Coef_Vec(Z_fit_sph_coef_vec, self.deg_fit_lbdv)
# Create SPH_func to represent X, Y, Z:
X_fit_sph = sph_f.sph_func(X_fit_sph_coef_mat, self.deg_fit_lbdv)
Y_fit_sph = sph_f.sph_func(Y_fit_sph_coef_mat, self.deg_fit_lbdv)
Z_fit_sph = sph_f.sph_func(Z_fit_sph_coef_mat, self.deg_fit_lbdv)
self.X_fit_sph_UV_pts = X_fit_sph.Eval_SPH(self.U_coors_pts_in, self.V_coors_pts_in)
self.Y_fit_sph_UV_pts = Y_fit_sph.Eval_SPH(self.U_coors_pts_in, self.V_coors_pts_in)
self.Z_fit_sph_UV_pts = Z_fit_sph.Eval_SPH(self.U_coors_pts_in, self.V_coors_pts_in)
self.XYZ_sph_fit_UV_pts = np.hstack(( self.X_fit_sph_UV_pts, self.Y_fit_sph_UV_pts, self.Z_fit_sph_UV_pts ))
# Plot SPH fit errors to point cloud:
self.XYZ_fit_UV_err_vecs = np.hstack(( self.X_fit_sph_UV_pts - self.X_orig_in, self.Y_fit_sph_UV_pts - self.Y_orig_in, self.Z_fit_sph_UV_pts - self.Z_orig_in ))
# Get {X,Y,Z} Coordinates at lebedev points, so we can leverage our code more efficiently (and uniformly) on surface:
self.X_fit_lbdv_pts = euc_kf.Extract_Quad_Pt_Vals_From_SPH_Fn(X_fit_sph, LBDV_Fit, 'A')
self.Y_fit_lbdv_pts = euc_kf.Extract_Quad_Pt_Vals_From_SPH_Fn(Y_fit_sph, LBDV_Fit, 'A')
self.Z_fit_lbdv_pts = euc_kf.Extract_Quad_Pt_Vals_From_SPH_Fn(Z_fit_sph, LBDV_Fit, 'A')
# create manifold to calculate H, average H:
Manny_Dict = {}
Manny_Name_Dict = {} # sph point cloud at lbdv
Manny_Name_Dict['X_lbdv_pts'] = self.X_fit_lbdv_pts
Manny_Name_Dict['Y_lbdv_pts'] = self.Y_fit_lbdv_pts
Manny_Name_Dict['Z_lbdv_pts'] = self.Z_fit_lbdv_pts
Manny_Dict['Pickle_Manny_Data'] = False # BJG: until it works don't pickle
Manny_Dict['Maniold_lbdv'] = LBDV_Fit
Manny_Dict['Manifold_SPH_deg'] = self.deg_fit_lbdv
Manny_Dict['use_manifold_name'] = False # we are NOT using named shapes in these tests
Manny_Dict['Maniold_Name_Dict'] = Manny_Name_Dict # sph point cloud at lbdv
Manny = mnfd.manifold(Manny_Dict)
# Test orientation:
self.Manny_lbdv_XYZ_pts = np.hstack(( self.X_fit_lbdv_pts, self.Y_fit_lbdv_pts, self.Z_fit_lbdv_pts ))
centered_lbdv_XYZ_pts = self.Manny_lbdv_XYZ_pts - self.avg_position_in #!!!! BJG: MAY WANT TO RECALCULATE CENTER USING LBDV !!!!#
self.Normal_X_lbdv_pts = euc_kf.Combine_Chart_Quad_Vals(Manny.Normal_Vec_X_A_Pts, Manny.Normal_Vec_X_B_Pts, LBDV_Fit)
self.Normal_Y_lbdv_pts = euc_kf.Combine_Chart_Quad_Vals(Manny.Normal_Vec_Y_A_Pts, Manny.Normal_Vec_Y_B_Pts, LBDV_Fit)
self.Normal_Z_lbdv_pts = euc_kf.Combine_Chart_Quad_Vals(Manny.Normal_Vec_Z_A_Pts, Manny.Normal_Vec_Z_B_Pts, LBDV_Fit)
self.Normals_XYZ_lbdv_pts = np.hstack(( self.Normal_X_lbdv_pts, self.Normal_Y_lbdv_pts, self.Normal_Z_lbdv_pts ))
# Makre sure orientation is inward, so H is positive (for Ellipsoid, and small deviations):
Orientations = np.sum( np.multiply(centered_lbdv_XYZ_pts, self.Normals_XYZ_lbdv_pts), axis = 1)
num_pos_orr = np.sum(Orientations.flatten() > 0)
Orientation = 1. # unchanged (we want INWARD)
if(num_pos_orr > .5*self.num_lbdv_pts_fit):
Orientation = -1.
# Use Gauss-Bonnet to test our resolution of the manifold:
self.K_lbdv_pts = euc_kf.Combine_Chart_Quad_Vals(Manny.K_A_pts, Manny.K_B_pts, LBDV_Fit)
Gauss_Bonnet_Err = euc_kf.Integral_on_Manny(self.K_lbdv_pts, Manny, LBDV_Fit) - 4*np.pi
self.Gauss_Bonnet_Rel_Err = abs(Gauss_Bonnet_Err)/(4*np.pi)
self.good_frame = True #whether or not the frame is good
# cutoff for bad frame:
if(self.Gauss_Bonnet_Rel_Err > 1.e-2):
self.good_frame = False
return
self.Mean_Curv_lbdv_pts = Orientation*euc_kf.Combine_Chart_Quad_Vals(Manny.H_A_pts, Manny.H_B_pts, LBDV_Fit)
# 3rd way we estimate H0: arithmetic average of H at lbdv points:
self.H0_avg_lbdv_curvs = np.sum( self.Mean_Curv_lbdv_pts.flatten() )/ self.num_lbdv_pts_fit
# 4th way we estimate H0: Integratal of H (on surface) divided by surface area. This is the ONE WE USE in our analysis:
Ones_pts = 1.*(np.ones_like(self.Mean_Curv_lbdv_pts))
self.H0_Int_of_H_over_Area = euc_kf.Integral_on_Manny(self.Mean_Curv_lbdv_pts, Manny, LBDV_Fit) / euc_kf.Integral_on_Manny(Ones_pts, Manny, LBDV_Fit)
# This is the 2\gamma*(H-H0 we USE FOR OUR anisotropic stress analysis:
self.Anis_Stress_pts_lbdv = self.TwoGammaVal*(self.Mean_Curv_lbdv_pts - self.H0_Int_of_H_over_Area)
# ellipsoid mean curvature, and deviation from surface mean curvature:
self.H_ellps_lbdv_pts = Mean_Curvs_on_Ellipsoid(a0, a1, a2, LBDV_Fit.theta_pts, LBDV_Fit.phi_pts)
self.Anis_Cell_Stress_pts_lbdv = self.TwoGammaVal*(self.Mean_Curv_lbdv_pts - self.H_ellps_lbdv_pts - (self.H0_Int_of_H_over_Area- self.H0_Ellpsoid))
self.Anis_Tissue_Stress_pts_lbdv = self.TwoGammaVal*(self.H_ellps_lbdv_pts - self.H0_Ellpsoid)
#!!! SHOULD INCLUDE H_0 - H_0_ellps
#### CALCULATE NORMALS, need manny for B-coors #####
# take ders of basis, compute cross product, normalize
X_fit_sph_dTheta = X_fit_sph.Quick_Theta_Der()
Y_fit_sph_dTheta = Y_fit_sph.Quick_Theta_Der()
Z_fit_sph_dTheta = Z_fit_sph.Quick_Theta_Der()
X_SPH_theta_UV_pts = X_fit_sph_dTheta.Eval_SPH(self.U_coors_pts_in, self.V_coors_pts_in)
Y_SPH_theta_UV_pts = Y_fit_sph_dTheta.Eval_SPH(self.U_coors_pts_in, self.V_coors_pts_in)
Z_SPH_theta_UV_pts = Z_fit_sph_dTheta.Eval_SPH(self.U_coors_pts_in, self.V_coors_pts_in)
XYZ_theta_UV_pts = np.hstack(( X_SPH_theta_UV_pts, Y_SPH_theta_UV_pts, Z_SPH_theta_UV_pts ))
X_SPH_phi_UV_pts= X_fit_sph.Eval_SPH_Der_Phi(self.U_coors_pts_in, self.V_coors_pts_in)
Y_SPH_phi_UV_pts= Y_fit_sph.Eval_SPH_Der_Phi(self.U_coors_pts_in, self.V_coors_pts_in)
Z_SPH_phi_UV_pts= Z_fit_sph.Eval_SPH_Der_Phi(self.U_coors_pts_in, self.V_coors_pts_in)
XYZ_phi_UV_pts = np.hstack(( X_SPH_phi_UV_pts, Y_SPH_phi_UV_pts, Z_SPH_phi_UV_pts ))
dTheta_cross_dPhi_UV_pts = np.cross(XYZ_theta_UV_pts, XYZ_phi_UV_pts)
cross_prod_X, cross_prod_Y, cross_prod_Z = np.hsplit(dTheta_cross_dPhi_UV_pts, 3)
Cross_Prod_Norms = np.sqrt(np.sum(np.multiply(dTheta_cross_dPhi_UV_pts, dTheta_cross_dPhi_UV_pts), axis = 1)).reshape(self.num_pts, 1)
Chart_Used = np.where(Cross_Prod_Norms > 1.e-2, 1., -1.)
# Use Manny to get Chart B info for Normals at UV pts:
U_B_pts_eval, V_B_pts_eval = chrts.Coor_A_To_B(self.U_coors_pts_in, self.V_coors_pts_in) # rotate to get B-coors on sphere pullback
X_SPH_theta_B_UV_pts = Manny.X_Bar_theta.Eval_SPH(U_B_pts_eval, V_B_pts_eval)
X_SPH_phi_B_UV_pts =Manny.X_Bar.Eval_SPH_Der_Phi(U_B_pts_eval, V_B_pts_eval)
Y_SPH_theta_B_UV_pts = Manny.Y_Bar_theta.Eval_SPH(U_B_pts_eval, V_B_pts_eval)
Y_SPH_phi_B_UV_pts =Manny.Y_Bar.Eval_SPH_Der_Phi(U_B_pts_eval, V_B_pts_eval)
Z_SPH_theta_B_UV_pts = Manny.Z_Bar_theta.Eval_SPH(U_B_pts_eval, V_B_pts_eval)
Z_SPH_phi_B_UV_pts =Manny.Z_Bar.Eval_SPH_Der_Phi(U_B_pts_eval, V_B_pts_eval)
XYZ_theta_B_UV_pts = np.hstack(( X_SPH_theta_B_UV_pts, Y_SPH_theta_B_UV_pts, Z_SPH_theta_B_UV_pts ))
XYZ_phi_B_UV_pts = np.hstack(( X_SPH_phi_B_UV_pts, Y_SPH_phi_B_UV_pts, Z_SPH_phi_B_UV_pts ))
dTheta_cross_dPhi_B_UV_pts = np.cross(XYZ_theta_B_UV_pts, XYZ_phi_B_UV_pts)
cross_prod_X_B, cross_prod_Y_B, cross_prod_Z_B = np.hsplit(dTheta_cross_dPhi_B_UV_pts, 3)
nor_dir_X = np.where(Cross_Prod_Norms > 1.e-2, cross_prod_X, cross_prod_X_B)
nor_dir_Y = np.where(Cross_Prod_Norms > 1.e-2, cross_prod_Y, cross_prod_Y_B)
nor_dir_Z = np.where(Cross_Prod_Norms > 1.e-2, cross_prod_Z, cross_prod_Z_B)