-
Notifications
You must be signed in to change notification settings - Fork 0
/
formfactors.py
2183 lines (1756 loc) · 62 KB
/
formfactors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
from scipy.special import jv,gamma
from numpy import exp,sin,cos,sqrt,pi
####################################################################
# basic functions
####################################################################
def bessj0(x):
"""
Bessel function of the first kind of zeroth order
"""
return jv(0,x)
def bessj1(x):
""""
Bessel function of the first kind of first order
"""
return jv(1,x)
def bessj1c(x):
"""
bessj1(x)/x
take care of zeros
"""
try:
y = np.ones(len(x))*0.5
idx = np.where(x != 0)
y[idx] = bessj1(x[idx])/x[idx]
except:
y = bessj1(x)/x
return y
def sinc(x):
"""
function for calculating sinc = sin(x)/x
numpy.sinc is defined as sinc(x) = sin(pi*x)/(pi*x)
"""
return np.sinc(x/pi)
####################################################################
# sphere
####################################################################
def psi_sphere(q,r):
"""
Form factor amplitude of a sphere
"""
x = q*r
return 3*(sin(x)-x*cos(x))/x**3
def P_sphere(q,r):
"""
Form factor of a sphere
"""
return psi_sphere(q,r)**2
def V_sphere(r):
"""
Volume of a sphere
"""
return 4*r**3*pi/3
def sphere2(q,r,s):
"""
Model: Sphere - with scaling
"""
return s*P_sphere(q,r)
def sphere3(q,r,s,b):
"""
Model: Sphere - with scaling and background
"""
return s*P_sphere(q,r)+b
####################################################################
# ellipsoid
####################################################################
def psi_ellipsoid(q,R,eps,alpha):
"""
Form factor amplitude: ellipsoid of revolution
"""
r_effective = R*sqrt(sin(alpha)**2 + eps**2*cos(alpha)**2)
psi = psi_sphere(q,r_effective)
return psi
def P_ellipsoid(q,R,eps):
"""
Form factor amplitude: ellipsoid of revolution
"""
N_alpha,alpha_max = 20,pi/2
da = alpha_max/N_alpha
alpha = np.linspace(da/2,pi/2-da/2,N_alpha)
P_sum = np.zeros(len(q))
for a in alpha:
P_sum += psi_ellipsoid(q,R,eps,a)**2*sin(a)
return P_sum*da
def V_ellipsoid(R,eps):
"""
Volume: ellipsoid of revolution
"""
return V_sphere(R)*eps
def ellipsoid(q,a,b,scale,background):
"""
Model: ellipsoid of revolution
"""
R = a
eps = b/a
I = scale * P_ellipsoid(q,R,eps) + background
return I
def ellipsoid_eps(q,R,eps,scale,background):
"""
Model: ellipsoid of revolution
parametrized with ellipticity (eps) and minor axis (R)
"""
I = scale * P_ellipsoid(q,R,eps) + background
return I
def psi_ellipsoid_rotate(q,R,eps,alpha):
"""
Form factor amplitude: ellipsoid of revolution - rotated
"""
r_effective = R*sqrt(eps**2*sin(alpha)**2 + cos(alpha)**2)
psi = psi_sphere(q,r_effective)
return psi
####################################################################
# cylinder
####################################################################
def V_cyl(R,L):
"""
Volume of a cylinder with radius R and length L
"""
return pi*R**2*L
def psi_cyl(q,R,L,a):
"""
form factor amplitude: cylinder with radius R and length L
depends on view angle a (for alpha)
reference: Pedersen1997
"""
x = q*R*sin(a)
z = q*L*cos(a)/2.0
psi=2.0*bessj1c(x)*sinc(z)
return psi
def psi_elliptical_cyl(q,r,L,eps,a,b):
"""
form factor amplitude: elliptical cylinder
eps: ellipticity (epsilon)
R: effective radius
"""
re = r*eps
R = sqrt(r**2*sin(b)**2+re**2*cos(b)**2)
psi = psi_cyl(q,R,L,a)
#x = q*R
#z = q*L*cos(a)/2.0
#psi = 2.0 * bessj1c(x)*sinc(z)
return psi
def P_elliptical_cyl(q,r,L,eps):
"""
form factor: elliptical cylinder
"""
N_alpha,alpha_max = 160,pi/2
N_beta,beta_max = 160,pi/2
alpha = np.linspace(alpha_max/N_alpha,alpha_max,N_alpha)
beta = np.linspace(beta_max/N_beta,beta_max,N_beta)
# inner loop vectorized
b = beta.reshape(-1,1)
P_a = np.zeros(len(q))
for a in alpha:
P_b = np.sum(psi_elliptical_cyl(q,r,L,eps,a,b)**2,axis=0)
P_a += P_b*sin(a)
P_a *= 2/pi
# normalization
P = P_a/P_a[0]
print('.',end='')
return P
def V_elliptical_cyl(R,L,eps):
""""
Volume elliptical cylinder with radius R, lenght L and ellipticity eps
"""
return V_cyl(R,L)*eps
def psi_elliptical_cyl_coreshell(q,R,L,Lc,eps,pc,ps,a,b):
"""
form factor amplitude: elliptical cylinder core-shell
R: minor axis
L: total length
Lc: length core
eps: ellipticity
pc: delta SLD core
ps: delta SLD shell
a,b: orientational angles
"""
A_core = pc * V_elliptical_cyl(R,Lc,eps) * psi_elliptical_cyl(q,R,Lc,eps,a,b)
norm_core = pc * V_elliptical_cyl(R,Lc,eps)
A_shell = ps * ( V_elliptical_cyl(R,L,eps)*psi_elliptical_cyl(q,R,L,eps,a,b) - V_elliptical_cyl(R,Lc,eps) * psi_elliptical_cyl(q,R,Lc,eps,a,b) )
norm_shell = ps * ( V_elliptical_cyl(R,L,eps) - V_elliptical_cyl(R,Lc,eps) )
A_coreshell = A_core + A_shell
norm_coreshell = norm_core + norm_shell
psi = A_coreshell/norm_coreshell
return psi
def P_elliptical_cyl_coreshell(q,R,L,Lc,eps,pc,ps):
"""
form factor: elliptical cylinder core-shell
R: minor axis
L: total length
Lc: length core
eps: ellipticity
pc: delta SLD core
ps: delta SLD shell
"""
N_alpha,alpha_max = 80,pi/2
N_beta,beta_max = 80,pi/2
alpha = np.linspace(alpha_max/N_alpha,alpha_max,N_alpha)
beta = np.linspace(beta_max/N_beta,beta_max,N_beta)
# inner loop vectorized
b = beta.reshape(-1,1)
P_a = np.zeros(len(q))
for a in alpha:
P_b = np.sum(psi_elliptical_cyl_coreshell(q,R,L,Lc,eps,pc,ps,a,b)**2,axis=0)
P_a += P_b*sin(a)
P_a *= 2/pi
# normalization
P = P_a/P_a[0]
return P
def elliptical_cylinder_coreshell(q,R,L,Lc,eps,psc,scale,background):
"""
model: elliptical cylinder, core shell
R: minor axis
L: total length
L: length of core
eps: ellipticity
psc: ratio of delta SLD for shell with respect to core
scaled and background subtracted
"""
return scale * P_elliptical_cyl_coreshell(q,R,L,Lc,eps,1,psc) + background
def elliptical_cylinder(q,R,L,eps,scale,background):
"""
model: elliptical cylinder
with background and scaling
"""
return scale * P_elliptical_cyl(q,R,L,eps) + background
def P_cyl(q,R,L):
"""
form factor: cylinder with radius R and length L
"""
N_alpha = 30
alpha = np.linspace(0,pi/2,N_alpha)
P_sum = 0
for a in alpha:
P_sum += psi_cyl(q,R,L,a)**2*sin(a)
P = P_sum/P_sum[0]
return P
def cylinder(q,R,L,s,b):
"""
Model: cylinder with radius R and lenght L
scale and background
"""
return s*P_cyl(q,R,L)+b
####################################################################
# stacked cylinder (cylinders stacked along length axis)
####################################################################
def psi_stacked_2cyl(q,R1,R2,L1,L2,p1,p2,a):
"""
Form factor amplitude: 2 stacked cylinders, stacked along the length axis
Ri,Li,pi: radius and length and contrast of ith cylinder
"""
# distance com to com
d = L1/2.0 + L2/2.0
qr = q*d*cos(a)
# phase factors
pf1 = 1.0
pf2 = np.exp(1j*qr)
# volumes
V1,V2 = V_cyl(R1,L1),V_cyl(R2,L2)
# form factor amplitudes
psi1,psi2 = psi_cyl(q,R1,L1,a),psi_cyl(q,R2,L2,a)
# collective form factor amplitude
pV1,pV2 = p1*V1,p2*V2
psi_dim = pV1*psi1*pf1 + pV2*psi2*pf2
return psi_dim/(pV1+pV2)
def P_stacked_2cyl(q,R1,R2,L1,L2,p1,p2):
"""
Form factor: 2 stacked cylinders, stacked along the length axis
R1,L1: radius and length of first cyl
R2,L2: radius and lenght of second cyl
"""
N_alpha,alpha_max = 50,pi/2
alpha = np.linspace(alpha_max/N_alpha,alpha_max,N_alpha)
# this is slower than a simple for loop
#a = alpha.reshape(-1,1)
#P_a = np.sum(abs(psi_stacked_2cyl(q,R1,R2,L1,L2,p1,p2,a))**2*sin(a),axis=0)
P_a = np.zeros(len(q))
for a in alpha:
P_a += abs(psi_stacked_2cyl(q,R1,R2,L1,L2,p1,p2,a))**2 * sin(a)
# normalization
P = P_a/P_a[0]
return P
def stacked_2cyl_sameR_ratio(q,R,L1,L2,p21,s,b):
"""
Model: stacked cylinders (2 cylinders), stacked along the lenght direction
they have the same radius: R
L1,L2: length of cylinder 1 and 2
p21 = p2/p1 (ratio of excess scattering length densities)
scaled and background subtracted
"""
return s*P_stacked_2cyl(q,R,R,L1,L2,1,p21)+b
def psi_stacked_3cyl(q,R1,R2,R3,L1,L2,L3,p1,p2,p3,a):
"""
Form factor amplitude: 3 stacked cylinders, stacked along the length axis
Ri,Li,pi: radius and length and contrast of ith cylinder
"""
# distance com to com
d2 = L1/2.0 + L2/2.0
d3 = L1/2.0 + L2 + L3/2.0
qr2 = q*d2*cos(a)
qr3 = q*d3*cos(a)
# phase factors
pf1 = 1.0
pf2 = np.exp(1j*qr2)
pf3 = np.exp(1j*qr3)
# volumes
V1,V2,V3 = V_cyl(R1,L1),V_cyl(R2,L2),V_cyl(R3,L3)
# form factor amplitudes
psi1,psi2,psi3 = psi_cyl(q,R1,L1,a),psi_cyl(q,R2,L2,a),psi_cyl(q,R3,L3,a)
# collective form factor amplitude
pV1,pV2,pV3 = p1*V1,p2*V2,p3*V3
psi_tri = pV1*psi1*pf1 + pV2*psi2*pf2 + pV3*psi3*pf3
return psi_tri/(pV1+pV2+pV3)
def P_stacked_3cyl(q,R1,R2,R3,L1,L2,L3,p1,p2,p3):
"""
Form factor: 3 stacked cylinders, stacked along the length axis
Ri,Li: radius and length of ith cyl
"""
N_alpha,alpha_max = 50,pi/2
alpha = np.linspace(alpha_max/N_alpha,alpha_max,N_alpha)
P_a = np.zeros(len(q))
for a in alpha:
P_a += abs(psi_stacked_3cyl(q,R1,R2,R3,L1,L2,L3,p1,p2,p3,a))**2 * sin(a)
# normalization
P = P_a/P_a[0]
return P
def stacked_3cyl_sameR_ratio(q,R,L1,L2,L3,p21,p31,s,b):
"""
Model: stacked cylinders (3 cylinders), stacked along the lenght direction
they have the same radius: R
Li: length of cylinder i
pi1 = pi/p1 (ratio of excess scattering length densities)
scaled and background subtracted
"""
return s*P_stacked_3cyl(q,R,R,R,L1,L2,L3,1,p21,p31)+b
def stacked_3cyl_sameR_ratio_2(qX,qN,R,L1,L2,L3,pX21,pX31,pN21,pN31,sX,bX,sN,bN):
"""
Model: stacked cylinders (3 cylinders), stacked along the lenght direction
they have the same radius: R
Li: length of cylinder i
pi1 = pi/p1 (ratio of excess scattering length densities)
scaled and background subtracted
fitted to two datasets: X and N
"""
yX = sX*P_stacked_3cyl(qX,R,R,R,L1,L2,L3,1,pX21,pX31)+bX
yN = sN*P_stacked_3cyl(qN,R,R,R,L1,L2,L3,1,pN21,pN31)+bN
return [yX,yN]
def stacked_3cyl_sameR_ratio_smooth(q,R,L1,L2,L3,p21,p31,sigmaR,s,b):
"""
Model: stacked cylinders (3 cylinders), stacked along the lenght direction
they have the same radius: R
Li: length of cylinder i
pi1 = pi/p1 (ratio of excess scattering length densities)
smoothness
scaled and background subtracteid
"""
return s*P_stacked_3cyl(q,R,R,R,L1,L2,L3,1,p21,p31)*exp(-q**2*sigmaR**2)+b
def psi_cylinders_spaced(q,R,L,d,a):
"""
Form factor amplitude: cylinders spaced by void
"""
A = V_cyl(R,L)*psi_cyl(q,R,L,a) - V_cyl(R,d)*psi_cyl(q,R,d,a)
B = V_cyl(R,L) - V_cyl(R,d)
psi = A/B
return psi
def P_cylinders_spaced(q,R,L,d):
"""
Form factor: 2 cylinders spaced by void
"""
alpha = np.linspace(0,pi/2,60)
P_sum = np.zeros(len(q))
for a in alpha:
P_sum += psi_cylinders_spaced(q,R,L,d,a)**2 * sin(a)
P = P_sum/P_sum[0]
return P
def cylinders_spaced(q,R,L,d,scale,background):
"""
Model: 2 cylinders separated by void
"""
return scale * P_cylinders_spaced(q,R,L,d) + background
####################################################################
# closely packed parallel cylinder
####################################################################
def psi_cylinder_dimer(q,R,L1,L2,p1,p2,a,b):
"""
Form factor amplitude: cylinder dimer (closely packed parallel cylinders)
"""
V1,V2 = V_cyl(R,L1),V_cyl(R,L2)
psi1,psi2 = psi_cyl(q,R,L1,a),psi_cyl(q,R,L2,a)
# phase factors
d = 2*R
pf1 = 1.0
pf2 = np.exp(1j*q*d*sin(a)*cos(b))
# form factor amplitude
pV1,pV2 = p1*V1,p2*V2
psi_cyl_dim = p1*psi1*pf1 + pV2*psi2*pf2
return psi_cyl_dim/(pV1 + pV2)
def psi_cylinder_trimer(q,R,L1,L2,L3,p1,p2,p3,a,b):
"""
Form factor amplitude: cylinder trimer (closely packed parallel cylinders)
"""
V1,V2,V3 = V_cyl(R,L1),V_cyl(R,L2),V_cyl(R,L3)
psi1,psi2,psi3 = psi_cyl(q,R,L1,a),psi_cyl(q,R,L2,a),psi_cyl(q,R,L3,a)
# phase factors
d = 2*R
pf1 = 1.0
pf2 = np.exp(1j*q*d*sin(a)*cos(b))
pos3 = cos(b)*cos(pi/3.0)+sin(b)*sin(pi/3.0)
pf3 = np.exp(1j*q*d*sin(a)*pos3)
# form factor amplitude
pV1,pV2,pV3 = p1*V1,p2*V2,p3*V3
psi_cyl_tri = pV1*psi1*pf1 + pV2*psi2*pf2 + pV3*psi3*pf3
return psi_cyl_tri/(pV1 + pV2 + pV3)
def P_cylinder_dimer(q,R,L1,L2,p1,p2):
"""
Form factor: cylinder dimer
"""
N_alpha,alpha_max = 100,pi/2
N_beta,beta_max = 100,pi
alpha = np.linspace(alpha_max/N_alpha,alpha_max,N_alpha)
beta = np.linspace(beta_max/N_beta,beta_max,N_beta)
# inner loop vectorized
b = beta.reshape(-1,1)
P_a = np.zeros(len(q))
for a in alpha:
P_b = np.sum(abs(psi_cylinder_dimer(q,R,L1,L2,p1,p2,a,b))**2,axis=0)
P_a += P_b*sin(a)
# normalization
P = P_a/P_a[0]
return P
def P_cylinder_trimer(q,R,L1,L2,L3,p1,p2,p3):
"""
Form factor: cylinder trimer
"""
N_alpha,alpha_max = 50,pi
N_beta,beta_max = 50,2*pi
alpha = np.linspace(alpha_max/N_alpha,alpha_max,N_alpha)
beta = np.linspace(beta_max/N_beta,beta_max,N_beta)
M = len(q)
b = beta.reshape(-1,1)
P_a = np.zeros(M)
for a in alpha:
P_b = np.sum(abs(psi_cylinder_trimer(q,R,L1,L2,L3,p1,p2,p3,a,b))**2,axis=0)
P_a += P_b*sin(a)
# normalization
P = P_a/P_a[0]
return P
def cylinder_dimer(q,R,L,s,b):
"""
Model: dimer of cylinders, closely packed in parallel
same radius: R
same length: L
scaled and background subtracted
"""
return s*P_cylinder_dimer(q,R,L,L,1,1)+b
def cylinder_dimer_ratio(q,R,L,p,s,b):
"""
Model: dimer of cylinders, closely packed in parallel
same radius: R
same length: L
deltaSLD differ by factor p (ratio)
scaled and background subtracted
"""
return s*P_cylinder_dimer(q,R,L,L,1,p)+b
def cylinder_trimer_ratio(q,R,L,p1,p2,s,b):
"""
Model: trimer of cylinders, closely packed in parallel
same radius: R
same length: L
deltaSLD differ by factors p1 and p2 (ratios)
scaled and background subtracted
"""
return s*P_cylinder_trimer(q,R,L,L,L,1,p1,p2)+b
def cylinder_trimer_ratio_L(q,R,L1,L2,L3,p1,p2,s,b):
"""
Model: trimer of cylinders, closely packed in parallel
same radius: R
lengths: L1,L2,L3
deltaSLD differ by factors p1 and p2 (ratios)
scaled and background subtracted
"""
return s*P_cylinder_trimer(q,R,L1,L2,L3,1,p1,p2)+b
####################################################################
# nanodisc
####################################################################
def psi_nanodisc(q,R,Hc,H,Hb,T,pc,ps,pb,a):
"""
form factor amplitude: nanodisc
R: radius core
Hc: height core (2xlipid tail group)
H: total height (core and shells)
Hb: height belt (protein or polymer)
T: thickness belt (protein or polymer)
pc: deltaSLD core
ps: deltaSLD shell
pb: deltaSLD belt
a: alpha (orientation parameter)
"""
A_core = pc * V_cyl(R,Hc) * psi_cyl(q,R,Hc,a)
norm_core = pc * V_cyl(R,Hc)
A_shell = ps * (V_cyl(R,H) * psi_cyl(q,R,H,a) - V_cyl(R,Hc) * psi_cyl(q,R,Hc,a) )
norm_shell = ps * (V_cyl(R,H) - V_cyl(R,Hc))
A_belt = pb * (V_cyl(R+T,Hb) * psi_cyl(q,R+T,Hb,a) - V_cyl(R,Hb) * psi_cyl(q,R,Hb,a))
norm_belt = pb * (V_cyl(R+T,Hb) - V_cyl(R,Hb))
A_nanodisc = A_core + A_shell + A_belt
norm_nanodisc = nomr_core + norm_shell + norm_belt
psi_nanodisc = A_nanodisc/norm_nanodisc
return psi_nanodisc
def P_nanodisc(q,R,Hc,H,Hb,T,pc,ps,pb):
"""
form factor: nanodisc
"""
N_alpha,alpha_max = 50,pi/2
alpha = np.linspace(alpha_max/N_alpha,alpha_max,N_alpha)
P_a = np.zeros(len(q))
for a in alpha:
P_a += abs(psi_nanodisc(q,R,Hc,H,Hb,T,pc,ps,pb,a))**2 * sin(a)
# normalization
P = P_a/P_a[0]
def P_nanodisc_Nlip(q,Nlip,Vtail,Vhead,T,pc,ps,pb):
"""
reparametrization of the nanodisc form factor in terms of Nlip
N: total number of lips
Ac: area of core
R : radius of core
Vlip: volume of 1 lipid
Vc: volume of core
Hc: height core
Hb: height belt
V: total volume of core and shells
H: total height of core and shells
"""
Ac = Alip*Nlip/2
R = sqrt(Ac/pi**2)
Vc = Vtail*Nlip
Hc = Vc/Ac
Hb = Hc
V = (Vhead+Vtail)*Nlip
H = Vcs/Ac
return P_nanodisc(q,R,Hc,H,Hb,T,pc,ps,pb)
def psi_elliptical_nanodisc(q,R,H,Hc,Hb,T,eps,pc,ps,pb,a,b):
"""
form factor amplitude: elliptical nanodiscs
"""
A_core = pc * V_elliptical_cyl(R,Hc,eps) * psi_elliptical_cyl(q,R,Hc,eps,a,b)
norm_core = pc * V_elliptical_cyl(R,Hc,eps)
A_shell = ps * ( V_elliptical_cyl(R,H,eps)*psi_elliptical_cyl(q,R,H,eps,a,b) - V_elliptical_cyl(R,Hc,eps) * psi_elliptical_cyl(q,R,Hc,eps,a,b) )
norm_shell = ps * ( V_elliptical_cyl(R,H,eps) - V_elliptical_cyl(R,Hc,eps) )
A_belt = pb * ( V_elliptical_cyl(R+T,Hb,eps) * psi_elliptical_cyl(q,R+T,Hb,eps,a,b) - V_elliptical_cyl(R,Hb,eps) * psi_elliptical_cyl(q,R,Hb,eps,a,b) )
norm_belt = pb * ( V_elliptical_cyl(R+T,Hb,eps) - V_elliptical_cyl(R,Hb,eps) )
A_elliptical_nanodisc = A_core + A_shell + A_belt
norm_elliptical_nanodisc = norm_core + norm_shell + norm_belt
psi = A_elliptical_nanodisc/norm_elliptical_nanodisc
return psi
def P_elliptical_nanodisc(q,R,Hc,H,Hb,T,eps,pc,ps,pb):
"""
form factor: elliptical nanodiscs
"""
N_alpha,alpha_max = 30,pi/2
N_beta,beta_max = 30,pi/2
alpha = np.linspace(alpha_max/N_alpha,alpha_max,N_alpha)
beta = np.linspace(beta_max/N_beta,beta_max,N_beta)
# inner loop vectorized
b = beta.reshape(-1,1)
P_a = np.zeros(len(q))
for a in alpha:
P_b = np.sum(psi_elliptical_nanodisc(q,R,H,Hc,Hb,T,eps,pc,ps,pb,a,b)**2,axis=0)
P_a += P_b*sin(a)
P_a *= 2/pi
# normalization
P = P_a/P_a[0]
return P
def elliptical_nanodisc(q,R,Hc,H,Hb,T,eps,psc,pbc,scale,background):
"""
model: elliptical nanodisc
psc: delta SLD of shell / delta SLD of core
pbc: delta SLD of belt / delta SLD of core
"""
pc = 1
ps = psc
pb = pbc
return scale * P_elliptical_nanodisc(q,R,Hc,H,Hb,T,eps,pc,ps,pb) + background
def elliptical_nanodisc2(q,R,Hc,H,Hb,T,eps,psc1,pbc1,psc2,pbc2,scale1,background1,scale2,background2):
"""
model: elliptical nanodisc, 2 datasets
psc1: delta SLD of shell / delta SLD of core, for dataset 1
pbc1: delta SLD of belt / delta SLD of core, for dataset 1
"""
y1 = elliptical_nanodisc(q,R,Hc,H,Hb,T,eps,psc1,pbc1,scale1,background1)
y2 = elliptical_nanodisc(q,R,Hc,H,Hb,T,eps,psc2,pbc2,scale2,background2)
return [y1,y2]
####################################################################
# nanodisc (specific parametrization)
####################################################################
def nanodisc(q,Bg,c,V_l,V_t,CV_p,Nlip,T,sigmaR,Ar,eps,n_w,Rg):
"""
Model: elliptical nanodisc
"""
# Volumes (V) and scattering lengths (b)
# specific for the DLPC/MSP1D1 nanodisc with tags
V_p = 54293.0
V_c = 3143.0
V_s = 30.0
b_p = 23473.0
b_h = 164.0723
b_t = 178.0440
b_c = 1.4250e+03
b_s = 10.0
# constants(avogadros number,electron scattering length)
N_A = 6.022e23
b_e = 2.82e-13
# derived params
V_h = V_l - V_t
V_p = CV_p*V_p
V_c = CV_p*V_c
# add 7.9 water molecules per lipid headgroup (Kucerka et al., 2005)
b_h = b_h + n_w * b_s
V_h = V_h + n_w * V_s
# reparametrization (from vol to scattering contrasts)
p_s = b_s/V_s # scattering length density of solvent
dp_p = b_p/V_p - p_s
dp_c = b_c/V_c - p_s
dp_t = b_t/V_t - p_s
dp_h = b_h/V_h - p_s
xx=(q*Rg)**2
P_c=(exp(-xx)+xx-1)/(xx/2.)
P_tot=0
F_tot=0
b=sqrt(abs(Nlip*Ar/(2*pi*eps)))
a=eps*b
jmax,kmax=20,20
dalf=pi/(2.*jmax)
dfi=pi/(2.*kmax)
for j in range(1,jmax+1):
alf=j*dalf
for k in range(1,kmax+1):
fi=k*dfi
r_t=sqrt((a*sin(fi))**2+(b*cos(fi))**2)
R=sqrt( ((a+T)*sin(fi))**2 +((b+T)*cos(fi))**2)
h_p=V_p/(pi*((a+T)*(b+T)-a*b))
h_t=2.0*V_t/Ar
h_h=V_h/Ar
H=h_t+2.0*h_h
Reff=R+abs(Rg)
yy=q*Reff*sin(alf)
ya=q*h_p*cos(alf)/2.
psi_cc=(1-exp(-xx))/xx*bessj0(yy)*sin(ya)/ya
tail=psi_cyl(q,r_t,h_t,alf)
pro=V_cyl(R,h_p)*psi_cyl(q,R,h_p,alf)-V_cyl(r_t,h_p)*psi_cyl(q,r_t,h_p,alf)
pro=pro/(V_cyl(R,h_p)-V_cyl(r_t,h_p))
head=(H*psi_cyl(q,r_t,H,alf)-h_t*psi_cyl(q,r_t,h_t,alf))/(2*h_h)
V_nd=Nlip*(V_t+V_h)+V_p
dp_nd=(dp_t*Nlip*V_t+dp_h*Nlip*V_h+dp_p*V_p)/V_nd
psi_nd=dp_t*(Nlip*V_t)*tail+dp_h*(Nlip*V_h)*head+dp_p*V_p*pro
psi_nd=psi_nd/(dp_nd*V_nd)
S_nd=psi_nd**2
S_nd_c=psi_cc*psi_nd
S_cc=psi_cc**2
F=(dp_nd*V_nd)**2*S_nd+4*dp_c*V_c*dp_nd*V_nd*S_nd_c+2*(dp_c*V_c)**2*(S_cc+P_c)
F_tot=F_tot+F*sin(alf)
F_tot=F_tot*dalf*dfi*(2./pi)
V_tot=V_nd+2*V_c
dp_tot=(V_nd*dp_nd+2*V_c*dp_c)/V_tot
P_tot=F_tot/(dp_tot*V_tot)**2
y=c*1.e-9*N_A*F_tot*exp(-q**2*sigmaR**2)*b_e**2+Bg
return y
####################################################################
# torus
####################################################################
# Gummel et al, Soft Matter, 2011, 7: 5731-5738, Concentration dependent pathways in spontaneous self-assembly of unilamellar vesicles
def psi_elliptical_torus(q,a,b,c,alpha):
"""
Form factor amplitude of torus with elliptical cross section
a: major radii of elliptical cross section
b: minor radii of elliptical cross section
c: distance from center of torus to middle of cross section
alpha: parameter for orientational averaging
"""
radius = np.linspace(c-0.999*a,c+0.999*a,20)
gamma = (b/a)*sqrt(a**2-(radius-c)**2)
psi_sum = np.zeros(len(q))
for (r,g) in zip(radius,gamma):
psi_sum += 2*pi*bessj0(q*r*sin(alpha))*sin(q*g*cos(alpha))*2*cos(alpha)/q
psi = psi_sum/psi_sum[0]
return psi
def psi_torus(q,r,R,alpha):
return psi_elliptical_torus(q,r,r,R,alpha)
def P_elliptical_torus(q,a,b,c):
"""
Form factor of torus with elliptical cross section
a: major radii of elliptical cross section
b: minor radii of elliptical cross section
c: distance from center of torus to middle of cross section
"""
N_alpha = 30
alpha = np.linspace(0,pi/2,N_alpha)
P_sum = np.zeros(len(q))
for aa in alpha:
P_sum += psi_elliptical_torus(q,a,b,c,aa)**2*sin(aa)
P = P_sum/P_sum[0]
return P
def P_torus(q,r,R):
return P_elliptical_torus(q,r,r,R)
def elliptical_torus(q,a,b,c,scale,background):
"""
Model: torus with elliptical cross section
a: major radii of elliptical cross section
b: minor radii of elliptical cross section
c: distance from center of torus to middle of cross section
"""
return scale * P_elliptical_torus(q,a,b,c) + background
def torus(q,r,R,scale,background):
"""
Model: torus
r: radius of cross section
R: distance from center of torus to middle of cross section
"""
return scale * P_torus(q,r,R) + background
def V_torus(r,R):
"""
Volume, torus
r: radius of cross section
R: distance from center of torus to middle of cross section
"""
#A = pi*r**2 # cross sectional area
#C = 2*pi*R # circumference
#return A*C
return V_elliptical_torus(r,r,R)
def V_elliptical_torus(a,b,R):
"""
Volume: elliptical torus
a,b: semiaxes of cross section
R: distance from center of torus to middle of cross section
"""
A = pi*a*b
C = 2*pi*R
return A*C
####################################################################
# supercylinder (superegg/superellipsoid)
####################################################################
# DOI: 10.1021/acsnano.6b08089
def beta(x,y):
return gamma(x)*gamma(y)/gamma(x+y)
def V_supercylinder(R,t,eps):
return 4*pi/(3*t)*beta(2/t,1/t)*R**3*eps
def psi_supercylinder(q,R,t,eps,a):
"""
Form factor amplitude: supercylinder
"""
N_z,z_max = 20,R*eps
dz = z_max/N_z
zz = np.linspace(dz/2,z_max-dz/2,N_z)
psi_sum = np.zeros(len(q))
for z in zz:
r = (R**t - abs(z/eps)**t)**(1.0/t)
x = q*r*sin(a)
y = q*z*cos(a)
psi_sum += 2*pi*r**2*2*bessj1c(x)*cos(y)
psi = psi_sum*dz/V_supercylinder(R,t,eps)
return psi
def P_supercylinder(q,R,t,eps):
"""
Form factor: supercylinder
"""
N_a,alpha_max = 20,pi/2
da = alpha_max/N_a
alpha = np.linspace(da/2,alpha_max-da/2,N_a)
P_sum = np.zeros(len(q))
for a in alpha:
P_sum += psi_supercylinder(q,R,t,eps,a)**2*sin(a)
P = P_sum*da
return P
def supercylinder(q,R,t,eps,scale,background):
return scale * P_supercylinder(q,R,t,eps) + background
def supercylinder_smooth(q,R,t,eps,sigmaR,scale,background):
return scale * P_supercylinder(q,R,t,eps)*exp(-q**2*sigmaR**2) + background
####################################################################
# hollow supercylinder (hollow superegg/superellipsoid)
####################################################################
def psi_hollow_supercylinder(q,R,t,eps,r,a):
"""
Form factor amplitude: hollow supercylinder
"""
L = 2*R*eps
A = V_supercylinder(R,t,eps)*psi_supercylinder(q,R,t,eps,a) - V_cyl(r,L) * psi_cyl(q,r,L,a)
B = V_supercylinder(R,t,eps) - V_cyl(r,L)
psi = A/B
return psi
def P_hollow_supercylinder(q,R,t,eps,r):
"""
Form factor: hollow supercylinder
"""
N_a,alpha_max = 20,pi/2
da = alpha_max/N_a
alpha = np.linspace(da/2,alpha_max-da/2,N_a)
P_sum = np.zeros(len(q))
for a in alpha:
P_sum += psi_hollow_supercylinder(q,R,t,eps,r,a)**2*sin(a)
P = P_sum*da
return P