forked from johanmazoyer/debrisdisk_mcmc_fit_and_plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk_models.py
1082 lines (823 loc) · 36.3 KB
/
disk_models.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
########################################################
########################################################
#### MAIN AUTHOR Max Millar Blanchaer
#### from a James Graham's original code
#### Initial model by Max Millar Blanchaer from a James Graham's original code,
### If you are using this model, please cite
### Millar-Blanchaer, M. A., Graham, J. R., Pueyo, L., et al. 2015, ApJ, 811, 18
#### exctracted from anadisk_e.py sent by Max in 2017
########################################################
########################################################
import math as mt
import numpy as np
from scipy.integrate import quad
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
def hg_1g(scatt_angles, g1, Norm):
"""
take a set of scatt angles and a set of HG parameter and return a
1g HG SPF
Args:
scatt_angles: a list of angles in degrees of dimensions N_angles.
The list must contains 90 degree values
g1: first HG parameter
Norm: Normalisation (value at 90 degree of the function)
Returns:
the 1g SPF, list of dimensions N_angles.
"""
scattered_angles_rad = np.radians(scatt_angles)
cos_phi = np.cos(scattered_angles_rad)
g1_2 = g1 * g1 #First HG g squared
#Constant for HG function
k = 1. / (4 * np.pi)
#Henyey Greenstein function
hg1 = k * (1. - g1_2) / (1. + g1_2 - (2 * g1 * cos_phi))**1.5
hg = hg1
hg_norm = hg / hg[np.where(scatt_angles == 90)] * Norm
return hg_norm
def hg_2g(scatt_angles, g1, g2, alpha, Norm):
"""
take a set of scatt angles and a set of HG parameter and return a
2g HG SPF
Args:
scatt_angles: a list of angles in degrees of dimensions N_angles.
The list must contains 90 degree values
g1: first HG parameter
g2: second HG parameter
alpha: relative weight
hg = alpha * hg1 * hg2 + (1 - alpha) * hg2
Norm: Normalisation (value at 90 degree of the function)
Returns:
the 2g SPF, list of dimensions N_angles.
"""
scattered_angles_rad = np.radians(scatt_angles)
cos_phi = np.cos(scattered_angles_rad)
g1_2 = g1 * g1 #First HG g squared
g2_2 = g2 * g2 #Second HG g squared
#Constant for HG function
k = 1. / (4 * np.pi)
#Henyey Greenstein function
hg1 = k * alpha * (1. - g1_2) / (1. + g1_2 - (2 * g1 * cos_phi))**1.5
hg2 = k * (1 - alpha) * (1. - g2_2) / (1. + g2_2 - (2 * g2 * cos_phi))**1.5
hg = hg1 + hg2
hg_norm = hg / hg[np.where(np.rint(scatt_angles) == 90)] * Norm
return hg_norm
def hg_3g(scatt_angles, g1, g2, g3, alpha1, alpha2, Norm):
"""
take a set of scatt angles and a set of HG parameter and return a
3g HG SPF
Args:
scatt_angles: a list of angles in degrees of dimensions N_angles.
The list must contains 90 degree values
g1: first HG parameter
g2: second HG parameter
g3: third HG parameter
alpha1: first relative weight
alpha2: second relative weight
hg = alpha1 * hg1 + alpha2 * hg2 + (1 - alpha1 - alpha2) * hg3
Norm: Normalisation (value at 90 degree of the function)
Returns:
the 3g SPF, list of dimensions N_angles.
"""
scattered_angles_rad = np.radians(scatt_angles)
cos_phi = np.cos(scattered_angles_rad)
g1_2 = g1 * g1 #First HG g squared
g2_2 = g2 * g2 #Second HG g squared
g3_2 = g3 * g3 #Third HG g squared
#Constant for HG function
k = 1. / (4 * np.pi)
#Henyey Greenstein function
hg1 = k * (1. - g1_2) / (1. + g1_2 - (2 * g1 * cos_phi))**1.5
hg2 = k * (1. - g2_2) / (1. + g2_2 - (2 * g2 * cos_phi))**1.5
hg3 = k * (1. - g3_2) / (1. + g3_2 - (2 * g3 * cos_phi))**1.5
hg = alpha1 * hg1 + alpha2 * hg2 + (1 - alpha1 - alpha2) * hg3
hg_norm = hg / hg[np.where(scatt_angles == 90)] * Norm
return hg_norm
def log_hg_2g(scatt_angles, g1, g2, alpha, Norm):
"""
take a set of scatt angles and a set of HG parameter and return
the log of a 2g HG SPF (usefull to fit from a set of points)
Args:
scatt_angles: a list of angles in degrees of dimensions N_angles.
The list must contains 90 degree values
g1: first HG parameter
g2: second HG parameter
alpha: relative weight
hg = alpha * hg1 * hg2 + (1 - alpha) * hg2
Norm: Normalisation (value at 90 degree of the function)
Returns:
the log of the 2g SPF, list of dimensions N_angles.
"""
return np.log(hg_2g(scatt_angles, g1, g2, alpha, Norm))
def log_hg_3g(scatt_angles, g1, g2, g3, alpha1, alpha2, Norm):
"""
take a set of scatt angles and a set of HG parameter and return the
log of the 3g HG SPF (usefull to fit from a set of points)
Args:
scatt_angles: a list of angles in degrees of dimensions N_angles.
The list must contains 90 degree values
g1: first HG parameter
g2: second HG parameter
g3: third HG parameter
alpha1: first relative weight
alpha2: second relative weight
hg = alpha1 * hg1 + alpha2 * hg2 + (1 - alpha1 - alpha2) * hg3
Norm: Normalisation (value at 90 degree of the function)
Returns:
the log of the 3g SPF, list of dimensions N_angles.
"""
return np.log(hg_3g(scatt_angles, g1, g2, g3, alpha1, alpha2, Norm))
def integrand_dxdy_1g(xp, yp_dy2, yp2, zp, zp2, zpsi_dx, zpci, R1, R2, beta,
a_r, g1, g1_2, ci, si, maxe, dx, dy,
k):
# author : Max Millar Blanchaer
# compute the scattering integrand
# see analytic-disk.nb
xx = (xp * ci + zpsi_dx)
d1 = mt.sqrt((yp_dy2 + xx * xx))
if (d1 < R1 or d1 > R2):
return 0.0
d2 = xp * xp + yp2 + zp2
#The line of sight scattering angle
cos_phi = xp / mt.sqrt(d2)
# phi=np.arccos(cos_phi)
#Henyey Greenstein function
hg = k * (1. - g1_2) / (1. + g1_2 - (2 * g1 * cos_phi))**1.5
#Radial power low r propto -beta
int1 = hg * (R1 / d1)**beta
#The scale height function
zz = (zpci - xp * si)
hh = (a_r * d1)
expo = zz * zz / (hh * hh)
# if expo > 2*maxe: # cut off exponential after 28 e-foldings (~ 1E-06)
# return 0.0
int2 = np.exp(0.5 * expo)
int3 = int2 * d2
return int1 / int3
def gen_disk_dxdy_1g(dim,
param_disk,
mask=None,
sampling=1,
distance=72.8,
pixscale=0.01414):
""" author : Max Millar Blanchaer
modified by Johan Mazoyer
create a 1g SPF disk model. The disk is normalized at Norm at 90degree
(before star offset). also normalized by aspect_ratio. These
normalization avoid weird correlation in the parameters
Args:
dim: dimension of the image in pixel assuming square image
param_disk: a dict with keywords:
R1: inner radius of the disk
R2: outer radius of the disk
beta: radial power law of the disk between R1 and R2
aspect_ratio=0.1 vertical width of the disk
g1: %, 1st HG param
inc: degree, inclination
pa: degree, principal angle
dx: au, + -> NW offset disk plane Minor Axis
dy: au, + -> SW offset disk plane Major Axis
offset: vertical residue image
mask: a np.where result that give where the model should be
measured (important to save a lot of time)
sampling: increase this parameter to bin the model
and save time
distance: distance of the star
pixscale: pixel scale of the instrument
Returns:
a 2d model
"""
R1 = param_disk['r1']
R2 = param_disk['r2']
beta = param_disk['beta']
inc = param_disk['inc']
pa = param_disk['PA']
dx = param_disk['dx']
dy = param_disk['dy']
Norm = param_disk['Norm']
g1 = param_disk['g1']
aspect_ratio = param_disk['a_r']
offset = param_disk['offset']
max_fov = dim / 2. * pixscale #maximum radial distance in AU from the center to the edge
npts = int(np.floor(dim / sampling))
xsize = max_fov * distance #maximum radial distance in AU from the center to the edge
#The coordinate system here [x,y,z] is defined :
# +ve x is the line of sight
# +ve y is going right from the center
# +ve z is going up from the center
# y = np.linspace(0,xsize,num=npts/2)
y = np.linspace(-xsize, xsize, num=npts)
z = np.linspace(-xsize, xsize, num=npts)
#Only need to compute half the image
# image =np.zeros((npts,npts/2+1))
image = np.zeros((npts, npts))
#Some things we can precompute ahead of time
maxe = mt.log(np.finfo('f').max) #The log of the machine precision
#Inclination Calculations
incl = np.radians(90 - inc)
ci = mt.cos(incl) #Cosine of inclination
si = mt.sin(incl) #Sine of inclination
#Position angle calculations
pa_rad = np.radians(90 - pa) #The position angle in radians
cos_pa = mt.cos(pa_rad) #Calculate these ahead of time
sin_pa = mt.sin(pa_rad)
#HG g value squared
g1_2 = g1 * g1 # HG g squared
#Constant for HG function
k = 1. / (4 * np.pi)
#The aspect ratio
a_r = aspect_ratio
#Henyey Greenstein function at 90
hg_90 = k * (1. - g1_2) / (1. + g1_2)**1.5
#If there's no mask then calculate for the full image
if len(np.shape(mask)) < 2:
for i, yp in enumerate(y):
for j, zp in enumerate(z):
#This rotates the coordinates in the image frame
yy = yp * cos_pa - zp * sin_pa #Rotate the y coordinate by the PA
zz = yp * sin_pa + zp * cos_pa #Rotate the z coordinate by the PA
#The distance from the center (in each coordinate) squared
y2 = yy * yy
z2 = zz * zz
#This rotates the coordinates in and out of the sky
zpci = zz * ci #Rotate the z coordinate by the inclination.
zpsi = zz * si
#Subtract the offset
zpsi_dx = zpsi - dx
#The distance from the offset squared
yy_dy = yy - dy
yy_dy2 = yy_dy * yy_dy
image[j, i] = quad(integrand_dxdy_1g,
-R2,
R2,
epsrel=0.5e-3,
limit=75,
args=(yy_dy2, y2, zp, z2, zpsi_dx, zpci, R1,
R2, beta, a_r, g1, g1_2, ci, si, maxe, dx, dy, k))[0]
#If there is a mask then don't calculate disk there
else:
hmask = mask
# hmask = mask[:,140:] #Use only half the mask
for i, yp in enumerate(y):
for j, zp in enumerate(z):
# if hmask[j,npts/2+i]: #This assumes
# that the input mask has is the same size as
# the desired image (i.e. ~ size / sampling)
if hmask[j, i]:
image[j, i] = 0. #np.nan
else:
#This rotates the coordinates in the image frame
yy = yp * cos_pa - zp * sin_pa #Rotate the y coordinate by the PA
zz = yp * sin_pa + zp * cos_pa #Rotate the z coordinate by the PA
#The distance from the center (in each coordinate) squared
y2 = yy * yy
z2 = zz * zz
#This rotates the coordinates in and out of the sky
zpci = zz * ci #Rotate the z coordinate by the inclination.
zpsi = zz * si
#Subtract the offset
zpsi_dx = zpsi - dx
#The distance from the offset squared
yy_dy = yy - dy
yy_dy2 = yy_dy * yy_dy
image[j, i] = quad(integrand_dxdy_1g,
-R2,
R2,
epsrel=0.5e-3,
limit=75,
args=(yy_dy2, y2, zp, z2, zpsi_dx, zpci,
R1, R2, beta, a_r, g1, g1_2, ci, si, maxe, dx,
dy, k))[0]
# print("Running time: ", datetime.now()-starttime)
# # normalize the HG function by the width
image = image / a_r
# normalize the HG function at the PA
image = Norm * image / hg_90
# add offset
image = image + offset
return image
def integrand_dxdy_2g(xp, yp_dy2, yp2, zp, zp2, zpsi_dx, zpci, R1, R2, beta,
a_r, g1, g1_2, g2, g2_2, alpha1, ci, si, maxe, dx, dy,
k):
# author : Max Millar Blanchaer
# compute the scattering integrand
# see analytic-disk.nb
xx = (xp * ci + zpsi_dx)
d1 = mt.sqrt((yp_dy2 + xx * xx))
if (d1 < R1 or d1 > R2):
return 0.0
d2 = xp * xp + yp2 + zp2
#The line of sight scattering angle
cos_phi = xp / mt.sqrt(d2)
# phi=np.arccos(cos_phi)
#Henyey Greenstein function
hg1 = k * alpha1 * (1. - g1_2) / (1. + g1_2 - (2 * g1 * cos_phi))**1.5
hg2 = k * (1 - alpha1) * (1. - g2_2) / (1. + g2_2 -
(2 * g2 * cos_phi))**1.5
hg = hg1 + hg2
#Radial power low r propto -beta
int1 = hg * (R1 / d1)**beta
#The scale height function
zz = (zpci - xp * si)
hh = (a_r * d1)
expo = zz * zz / (hh * hh)
# if expo > 2*maxe: # cut off exponential after 28 e-foldings (~ 1E-06)
# return 0.0
int2 = np.exp(0.5 * expo)
int3 = int2 * d2
return int1 / int3
def gen_disk_dxdy_2g(dim,
param_disk,
mask=None,
sampling=1,
distance=72.8,
pixscale=0.01414):
""" author : Max Millar Blanchaer
modified by Johan Mazoyer
create a 2g SPF disk model. The disk is normalized at 1 at 90degree
(before star offset). also normalized by aspect_ratio. These
normalization avoid weird correlation in the parameters
Args:
dim: dimension of the image in pixel assuming square image
param_disk: a dict with keywords:
R1: inner radius of the disk
R2: outer radius of the disk
beta: radial power law of the disk between R1 and R2
aspect_ratio=0.1 vertical width of the disk
g1: %, 1st HG param
g2: %, 2nd HG param
Aplha: %, relative HG weight
inc: degree, inclination
pa: degree, principal angle
dx: au, + -> NW offset disk plane Minor Axis
dy: au, + -> SW offset disk plane Major Axis
offset: vertical residue image
mask: a np.where result that give where the model should be
measured (important to save a lot of time)
sampling: increase this parameter to bin the model
and save time
distance: distance of the star
pixscale: pixel scale of the instrument
Returns:
a 2d model
"""
R1 = param_disk['r1']
R2 = param_disk['r2']
beta = param_disk['beta']
inc = param_disk['inc']
pa = param_disk['PA']
dx = param_disk['dx']
dy = param_disk['dy']
Norm = param_disk['Norm']
g1 = param_disk['g1']
g2 = param_disk['g2']
alpha1 = param_disk['alpha1']
aspect_ratio = param_disk['a_r']
offset = param_disk['offset']
max_fov = dim / 2. * pixscale #maximum radial distance in AU from the center to the edge
npts = int(np.floor(dim / sampling))
xsize = max_fov * distance #maximum radial distance in AU from the center to the edge
#The coordinate system here [x,y,z] is defined :
# +ve x is the line of sight
# +ve y is going right from the center
# +ve z is going up from the center
# y = np.linspace(0,xsize,num=npts/2)
y = np.linspace(-xsize, xsize, num=npts)
z = np.linspace(-xsize, xsize, num=npts)
#Only need to compute half the image
# image =np.zeros((npts,npts/2+1))
image = np.zeros((npts, npts))
#Some things we can precompute ahead of time
maxe = mt.log(np.finfo('f').max) #The log of the machine precision
#Inclination Calculations
incl = np.radians(90 - inc)
ci = mt.cos(incl) #Cosine of inclination
si = mt.sin(incl) #Sine of inclination
#Position angle calculations
pa_rad = np.radians(90 - pa) #The position angle in radians
cos_pa = mt.cos(pa_rad) #Calculate these ahead of time
sin_pa = mt.sin(pa_rad)
#HG g value squared
g1_2 = g1 * g1 #First HG g squared
g2_2 = g2 * g2 #Second HG g squared
#Constant for HG function
k = 1. / (4 * np.pi)
#The aspect ratio
a_r = aspect_ratio
#Henyey Greenstein function at 90
hg1_90 = k * alpha1 * (1. - g1_2) / (1. + g1_2)**1.5
hg2_90 = k * (1 - alpha1) * (1. - g2_2) / (1. + g2_2)**1.5
hg_90 = hg1_90 + hg2_90
#If there's no mask then calculate for the full image
if len(np.shape(mask)) < 2:
for i, yp in enumerate(y):
for j, zp in enumerate(z):
#This rotates the coordinates in the image frame
yy = yp * cos_pa - zp * sin_pa #Rotate the y coordinate by the PA
zz = yp * sin_pa + zp * cos_pa #Rotate the z coordinate by the PA
#The distance from the center (in each coordinate) squared
y2 = yy * yy
z2 = zz * zz
#This rotates the coordinates in and out of the sky
zpci = zz * ci #Rotate the z coordinate by the inclination.
zpsi = zz * si
#Subtract the offset
zpsi_dx = zpsi - dx
#The distance from the offset squared
yy_dy = yy - dy
yy_dy2 = yy_dy * yy_dy
image[j, i] = quad(integrand_dxdy_2g,
-R2,
R2,
epsrel=0.5e-3,
limit=75,
args=(yy_dy2, y2, zp, z2, zpsi_dx, zpci, R1,
R2, beta, a_r, g1, g1_2, g2, g2_2,
alpha1, ci, si, maxe, dx, dy, k))[0]
#If there is a mask then don't calculate disk there
else:
hmask = mask
# hmask = mask[:,140:] #Use only half the mask
for i, yp in enumerate(y):
for j, zp in enumerate(z):
# if hmask[j,npts/2+i]: #This assumes
# that the input mask has is the same size as
# the desired image (i.e. ~ size / sampling)
if hmask[j, i]:
image[j, i] = 0. #np.nan
else:
#This rotates the coordinates in the image frame
yy = yp * cos_pa - zp * sin_pa #Rotate the y coordinate by the PA
zz = yp * sin_pa + zp * cos_pa #Rotate the z coordinate by the PA
#The distance from the center (in each coordinate) squared
y2 = yy * yy
z2 = zz * zz
#This rotates the coordinates in and out of the sky
zpci = zz * ci #Rotate the z coordinate by the inclination.
zpsi = zz * si
#Subtract the offset
zpsi_dx = zpsi - dx
#The distance from the offset squared
yy_dy = yy - dy
yy_dy2 = yy_dy * yy_dy
image[j, i] = quad(integrand_dxdy_2g,
-R2,
R2,
epsrel=0.5e-3,
limit=75,
args=(yy_dy2, y2, zp, z2, zpsi_dx, zpci,
R1, R2, beta, a_r, g1, g1_2, g2,
g2_2, alpha1, ci, si, maxe, dx,
dy, k))[0]
# print("Running time: ", datetime.now()-starttime)
# # normalize the HG function by the width
image = image / a_r
# normalize the HG function at the PA
image = Norm * image / hg_90
# add offset
image = image + offset
return image
def integrand_dxdy_3g(xp, yp_dy2, yp2, zp, zp2, zpsi_dx, zpci, R1, R2, beta,
a_r, g1, g1_2, g2, g2_2, g3, g3_2, alpha1, alpha2, ci,
si, maxe, dx, dy, k):
# compute the scattering integrand
# see analytic-disk.nb
xx = (xp * ci + zpsi_dx)
d1 = mt.sqrt((yp_dy2 + xx * xx))
if (d1 < R1 or d1 > R2):
return 0.0
d2 = xp * xp + yp2 + zp2
#The line of sight scattering angle
cos_phi = xp / mt.sqrt(d2)
# phi=np.arccos(cos_phi)
#Henyey Greenstein function
# hg1=k*alpha1* (1. - g1_2)/(1. + g1_2 - (2*g1*cos_phi))**1.5
# hg2=k*(1-alpha1)* (1. - g2_2)/(1. + g2_2 - (2*g2*cos_phi))**1.5
#Henyey Greenstein function
hg1 = k * (1. - g1_2) / (1. + g1_2 - (2 * g1 * cos_phi))**1.5
hg2 = k * (1. - g2_2) / (1. + g2_2 - (2 * g2 * cos_phi))**1.5
hg3 = k * (1. - g3_2) / (1. + g3_2 - (2 * g3 * cos_phi))**1.5
hg = alpha1 * hg1 + alpha2 * hg2 + (1 - alpha1 - alpha2) * hg3
#Radial power low r propto -beta
int1 = hg * (R1 / d1)**beta
#The scale height function
zz = (zpci - xp * si)
hh = (a_r * d1)
expo = zz * zz / (hh * hh)
# if expo > 2*maxe: # cut off exponential after 28 e-foldings (~ 1E-06)
# return 0.0
int2 = np.exp(0.5 * expo)
int3 = int2 * d2
return int1 / int3
def gen_disk_dxdy_3g(dim,
param_disk,
mask=None,
sampling=1,
distance=72.8,
pixscale=0.01414):
""" adapted from Max Millar Blanchaer by Johan Mazoyer
create a 3g SPF disk model. The disk is normalized at 1 at 90degree
(before star offset). also normalized by aspect_ratio
Args:
dim: dimension of the image in pixel assuming square image
param_disk: a dict with keywords:
R1: inner radius of the disk
R2: outer radius of the disk
beta: radial power law of the disk between R1 and R2
aspect_ratio vertical width of the disk
g1: %, 1st HG param
g2: %, 2nd HG param
g3: %, 3rd HG param
Aplha1: %, first relative HG weight
Aplha2: %, second relative HG weight
inc: degree, inclination
pa: degree, principal angle
dx: au, + -> NW offset disk plane Minor Axis
dy: au, + -> SW offset disk plane Major Axis
offset: vertical residue image
mask: a np.where result that give where the model should be
measured (important to save a lot of time)
sampling: increase this parameter to bin the model
and save time
distance: distance of the star
pixscale: pixel scale of the instrument
Returns:
a 2d model
"""
R1 = param_disk['r1']
R2 = param_disk['r2']
beta = param_disk['beta']
inc = param_disk['inc']
pa = param_disk['PA']
dx = param_disk['dx']
dy = param_disk['dy']
Norm = param_disk['Norm']
g1 = param_disk['g1']
g2 = param_disk['g2']
alpha1 = param_disk['alpha1']
g3 = param_disk['g3']
alpha2 = param_disk['alpha2']
aspect_ratio = param_disk['a_r']
offset = param_disk['offset']
# starttime=datetime.now()
max_fov = dim / 2. * pixscale #maximum radial distance in AU from the center to the edge
npts = int(np.floor(dim / sampling))
xsize = max_fov * distance #maximum radial distance in AU from the center to the edge
#The coordinate system here [x,y,z] is defined :
# +ve x is the line of sight
# +ve y is going right from the center
# +ve z is going up from the center
# y = np.linspace(0,xsize,num=npts/2)
y = np.linspace(-xsize, xsize, num=npts)
z = np.linspace(-xsize, xsize, num=npts)
#Only need to compute half the image
# image =np.zeros((npts,npts/2+1))
image = np.zeros((npts, npts))
#Some things we can precompute ahead of time
maxe = mt.log(np.finfo('f').max) #The log of the machine precision
#Inclination Calculations
incl = np.radians(90 - inc)
ci = mt.cos(incl) #Cosine of inclination
si = mt.sin(incl) #Sine of inclination
#Position angle calculations
pa_rad = np.radians(90 - pa) #The position angle in radians
cos_pa = mt.cos(pa_rad) #Calculate these ahead of time
sin_pa = mt.sin(pa_rad)
#HG g value squared
g1_2 = g1 * g1 #First HG g squared
g2_2 = g2 * g2 #Second HG g squared
g3_2 = g3 * g3 #Second HG g squared
#Constant for HG function
k = 1. / (4 * np.pi) * 100
### we add a 100 multiplicateur to k avoid hg values to be too small, it makes the integral fail on certains points
### Since we normalize by hg90 at the end, this has no impact on the actual model
#The aspect ratio
a_r = aspect_ratio
#Henyey Greenstein function at 90
hg1_90 = k * (1. - g1_2) / (1. + g1_2)**1.5
hg2_90 = k * (1. - g2_2) / (1. + g2_2)**1.5
hg3_90 = k * (1. - g3_2) / (1. + g3_2)**1.5
hg_90 = alpha1 * hg1_90 + alpha2 * hg2_90 + (1 - alpha1 - alpha2) * hg3_90
#If there's no mask then calculate for the full image
if len(np.shape(mask)) < 2:
for i, yp in enumerate(y):
for j, zp in enumerate(z):
#This rotates the coordinates in the image frame
yy = yp * cos_pa - zp * sin_pa #Rotate the y coordinate by the PA
zz = yp * sin_pa + zp * cos_pa #Rotate the z coordinate by the PA
#The distance from the center (in each coordinate) squared
y2 = yy * yy
z2 = zz * zz
#This rotates the coordinates in and out of the sky
zpci = zz * ci #Rotate the z coordinate by the inclination.
zpsi = zz * si
#Subtract the offset
zpsi_dx = zpsi - dx
#The distance from the offset squared
yy_dy = yy - dy
yy_dy2 = yy_dy * yy_dy
# image[j,i]= quad(integrand_dxdy_2g, -R2, R2, epsrel=0.5e-3,limit=75,args=(yy_dy2,y2,zp,z2,zpsi_dx,zpci,R1,R2,beta,a_r,g1,g1_2,g2,g2_2, alpha1,ci,si,maxe,dx,dy,k))[0]
image[j, i] = quad(integrand_dxdy_3g,
-R2,
R2,
epsrel=0.5e-12,
limit=75,
args=(yy_dy2, y2, zp, z2, zpsi_dx, zpci, R1,
R2, beta, a_r, g1, g1_2, g2, g2_2, g3,
g3_2, alpha1, alpha2, ci, si, maxe,
dx, dy, k))[0]
#If there is a mask then don't calculate disk there
else:
hmask = mask
# hmask = mask[:,140:] #Use only half the mask
for i, yp in enumerate(y):
for j, zp in enumerate(z):
# if hmask[j,npts/2+i]: #This assumes that the input mask has is the same size as the desired image (i.e. ~ size / sampling)
if hmask[j, i]:
image[j, i] = 0. #np.nan
else:
#This rotates the coordinates in the image frame
yy = yp * cos_pa - zp * sin_pa #Rotate the y coordinate by the PA
zz = yp * sin_pa + zp * cos_pa #Rotate the z coordinate by the PA
#The distance from the center (in each coordinate) squared
y2 = yy * yy
z2 = zz * zz
#This rotates the coordinates in and out of the sky
zpci = zz * ci #Rotate the z coordinate by the inclination.
zpsi = zz * si
#Subtract the offset
zpsi_dx = zpsi - dx
#The distance from the offset squared
yy_dy = yy - dy
yy_dy2 = yy_dy * yy_dy
# image[j,i]= quad(integrand_dxdy_2g, -R2, R2, epsrel=0.5e-3,limit=75,args=(yy_dy2,y2,zp,z2,zpsi_dx,zpci,R1,R2,beta,a_r,g1,g1_2,g2,g2_2, alpha1,ci,si,maxe,dx,dy,k))[0]
image[j, i] = quad(integrand_dxdy_3g,
-R2,
R2,
epsrel=0.5e-12,
limit=75,
args=(yy_dy2, y2, zp, z2, zpsi_dx, zpci,
R1, R2, beta, a_r, g1, g1_2, g2,
g2_2, g3, g3_2, alpha1, alpha2,
ci, si, maxe, dx, dy, k))[0]
# # normalize the HG function by the width
image = image / a_r
# normalize the HG function at the PA
image = Norm * image / hg_90
# add offset
image = image + offset
# print("Running time 3g: {0}".format(datetime.now()-starttime))
return image
def integrand_dxdy_flat(xp, yp_dy2, yp2, zp, zp2, zpsi_dx, zpci, R1, R2, beta,
a_r, ci, si, maxe, dx, dy):
# compute the scattering integrand
# see analytic-disk.nb
xx = (xp * ci + zpsi_dx)
d1 = mt.sqrt((yp_dy2 + xx * xx))
if (d1 < R1 or d1 > R2):
return 0.0
d2 = xp * xp + yp2 + zp2
#The line of sight scattering angle
# cos_phi = xp / mt.sqrt(d2)
# phi=np.arccos(cos_phi)
#Radial power low r propto -beta
int1 = (R1 / d1)**beta
#The scale height function
zz = (zpci - xp * si)
hh = (a_r * d1)
expo = zz * zz / (hh * hh)
# if expo > 2*maxe: # cut off exponential after 28 e-foldings (~ 1E-06)
# return 0.0
int2 = np.exp(0.5 * expo)
int3 = int2 * d2
return int1 / int3
def gen_disk_dxdy_flat(dim,
R1=74.42,
R2=82.45,
beta=1.0,
aspect_ratio=0.1,
inc=76.49,
pa=30,
dx=0,
dy=0.,
mask=None,
sampling=1,
distance=72.8,
pixscale=0.01414):
""" adapted from Max Millar Blanchaer by Johan Mazoyer
create a 3g SPF disk model. The disk is normalized at 1 at 90degree
(before star offset). also normalized by aspect_ratio
Args:
dim: dimension of the image in pixel assuming square image
R1: inner radius of the disk
R2: outer radius of the disk
beta: radial power law of the disk between R1 and R2
aspect_ratio=0.1 vertical width of the disk
g1: %, 1st HG param
g2: %, 2nd HG param
g3: %, 3rd HG param
Aplha1: %, first relative HG weight
Aplha2: %, second relative HG weight
inc: degree, inclination
pa: degree, principal angle
dx: au, + -> NW offset disk plane Minor Axis
dy: au, + -> SW offset disk plane Major Axis
mask: a np.where result that give where the model should be
measured (important to save a lot of time)
sampling: increase this parameter to bin the model
and save time
distance: distance of the star
pixscale: pixel scale of the instrument
Returns:
a 2d model
"""
# starttime=datetime.now()
max_fov = dim / 2. * pixscale #maximum radial distance in AU from the center to the edge
npts = int(np.floor(dim / sampling))
xsize = max_fov * distance #maximum radial distance in AU from the center to the edge
#The coordinate system here [x,y,z] is defined :
# +ve x is the line of sight
# +ve y is going right from the center
# +ve z is going up from the center
# y = np.linspace(0,xsize,num=npts/2)
y = np.linspace(-xsize, xsize, num=npts)
z = np.linspace(-xsize, xsize, num=npts)
#Only need to compute half the image
# image =np.zeros((npts,npts/2+1))
image = np.zeros((npts, npts))
#Some things we can precompute ahead of time
maxe = mt.log(np.finfo('f').max) #The log of the machine precision
#Inclination Calculations
incl = np.radians(90 - inc)
ci = mt.cos(incl) #Cosine of inclination
si = mt.sin(incl) #Sine of inclination
#Position angle calculations
pa_rad = np.radians(90 - pa) #The position angle in radians
cos_pa = mt.cos(pa_rad) #Calculate these ahead of time
sin_pa = mt.sin(pa_rad)
#The aspect ratio
a_r = aspect_ratio
#If there's no mask then calculate for the full image