-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcs.py
1154 lines (917 loc) · 46.7 KB
/
cs.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
# coding: utf-8
# In[17]:
# timer:
from time import time
time0=time()
def pr(s,gn,message = ''):
print(gn,' :'+message+': ',time()-s)
return time(),gn+1
s,gn=pr(time0,0)
Spectrum={
1:'Compton',
2:'Burst',
3:'Thomson',
0:'FromFile'
}[0]
oblateness='AlGendy'
AtmName='res/B/CR12' # the prefix for all result files related to the set of parameters
PulsName=AtmName+'P2'
computePulse= False
plotAtm=True
plotPulse=False
# In[18]:exit()
#import:
from numpy import linspace, logspace, empty, zeros, ones, array, fromfile
# m=fromfile(AtmName+'m.bin')
# print(m)
# exit()
from numpy import pi, exp, log, sqrt, sin, cos, arccos, arctan2
from numpy import absolute, sign, floor, ceil, argmin
from numpy.polynomial.laguerre import laggauss
from numpy.polynomial.legendre import leggauss
from scipy.interpolate import interp1d,CubicSpline
from scipy.special import kn
from matplotlib.pyplot import *
from bisect import bisect
from time import time
#import numpy as np
#import matplotlib.pyplot as plt
s,gn=pr(s,gn, 'importing')
# In[19]:
colors=['xkcd:brownish red',
'xkcd:red',
'xkcd:orange',
'xkcd:dark yellow',
'xkcd:dark yellow green',
'xkcd:deep green',
'xkcd:dark cyan',
'xkcd:blue',
'xkcd:purple'
] # 'rygcbm' # Rainbow
NColors=len(colors)
#physical constants:
evere=.5109989e6 # electron volts in elecron rest energy
G=13275412528e1 # G*M_sol in km^3/s^2
c=299792458e-3 # speed of light in km/s
# Atmosphere parameters:
tau_T= 1.5 # Thomson optical depth of thermalization
x_l, x_u = -3.7 , .3 # lower and upper bounds of the log_10 energy span
Theta = 0.3 # dimensionless electron gas temperature (Theta = k T_e / m_e c^2) # it's about 0.1
T = 0.002 # 10/evere # dimensionless photon black body temperature T = k T_bb / m_e c^2
#precomputations :
ScatterNum = 15 # total number of scatterings
NGamma= 7# number of Lorenz factor points (\gamma)
NAzimuth= 10 # 12 # numbers of azimuth angles (\phi) [0,pi]
NEnergy = 50 # 50# 101 # number of energy points (x)
NDepth = 100# 101 # number of optical depth levels (\tau)
NMu = 10 # 20# 15 # number of propagation zenith angle cosines (\mu) [0,1]
NZenith = 2*NMu # number of propagation zenith angles (z) [0,pi]
IntGamma = laggauss(NGamma) # sample points and weights for computing thermal matrix
IntAzimuth = leggauss(NAzimuth*2) # sample points and weights for computing azimuth-averaged matrix
IntEnergy = logspace(x_l,x_u,NEnergy), log(1e1)*(x_u-x_l)/(NEnergy-1.) # sample points and weights for integrations over the spectrum computing sorce function
IntDepth = linspace(0,tau_T,num=NDepth,retstep=True) # sample points and weights for integrations over the optical depth computing intencity
IntZenith = leggauss(NZenith) # sample points and weights for integrations over zenith angle in positive and negative directions together
# IntZenith = linspace(1e-6-1,1-1e-6,num=NZenith), array([2.]+[2.]*(NZenith-2)+[2.])/(NZenith)
# IntZenith = linspace(1/NZenith - 1,1 - 1/NZenith,num=NZenith), array([2.]*(NZenith))/(NZenith)
# mu,mu_weight=IntZenith
# mu=array( ([1-1e-7]+list(mu)+[1-1e-7]) )
# mu_weight=array([1e-5]+list(mu_weight)+[1e-5])
# IntZenith=(mu, mu_weight)
# NZenith=NZenith+2
# NMu=NMu+1
K2Y = kn(2,1./Theta) # second modified Bessel function of reversed dimensionless temperature
mu,mu_weight=IntZenith
x,x_weight=IntEnergy
tau,tau_weight=IntDepth
outParams = open(AtmName+'.dat','w')
outParams.write(Spectrum+'\n')
outParams.write(str(tau_T)+' = total depth tau_T\n')
outParams.write(str(x_l)+' '+str(x_u)+' = log_10 energy span from to\n')
outParams.write(str(Theta)+' = dimensionless electron gas temperature in m_ec^2\n')
outParams.write(str(T)+' = dimensionless initial bb photons temperature in m_ec^2\n')
outParams.write(str(ScatterNum)+' = Number of scatterings computed\n')
outParams.write(str(NMu)+' = NMu, NZenith=2 NMu, number of points in mu grid\n')
outParams.write(str(NEnergy)+' = NEnergy, number of points in x grid\n')
outParams.write(str(NGamma)+' '+str(NAzimuth)+' '+str(NDepth)+' = NGamma NAzimuth NDepth parameters\n')
s,gn=pr(s,gn,'precomps')
# In[20]:
def Planck(x):
""" Planck function for Intensity of black body radiation
The only argument x is the energy of a photon in units of electron rest energy ( h \\nu / m_e c^2 )
The photon temperature is given by T also in units of electron rest mass
Planck returns the intensity of BB radiation
"""
e=x/T
C=1. # some dimension constant.
R=C*e*e # Rayleigh Jeans law'
I=.0 if e>5e2 else R*e/(exp(e)-1.) if e > 1e-5 else R
return I
def Delta(x):
C=2e4
I=C*exp(-1e2*(x-T)**2/T/T)
return I
def sigma_cs(x): # not averaged on electron distribution
""" This function compute the Compton scattering cross-section in electron rest frame
x is the energy of the scattering photon in units of electron rest energy
this function approaches the mean compton cross-section when electron gas temperature is small
"""
if x<.1:
a,n,s=3./8.,0.,0.
while(abs(a)*(n+2)**2>1e-11): # Taylor series sum of the formula below
s=s+a*(n+2+2/(n+1)+8/(n+2)-16/(n+3))
n=n+1
a=-2*x*a
return s
else: return 3*(2-(1/x+1-x/2)*log(1+2*x))/4/x/x + 3*(1+x)/4/(1+2*x)**2
def Compton_redistribution_m(x1,x2,mu,gamma):
""" Compton redistribution matrix for monoenergetic electron gas
The arguements are:
x1 and x2 - photon energies in units of electron rest energy ( h \\nu / m_e c^2 )
mu - cosine of a scattering angle
gamma - energy of each electron in the gas in units of the electron rest mass
This fuctions returns (R,RI,RQ,RU)
which are scalar redistribution functions for isotropic monoenergetic gas
and also the are non-zero elements of the matrix: R11,R12=R21,R22,R33 respectively
R44 or RV is also not equal to zero but we never need it
"""
#the next variables' names are adopted from J. Poutanen & O. Vilhu 1993
r = (1.+mu)/(1.-mu)
a1 = sqrt( (gamma-x1)**2 + r )
a2 = sqrt( (gamma+x2)**2 + r )
v = a1*a2
u = a2-a1 #(x1+x2)*(2.*gamma+x2-x1)/(a1+a2) # the formulas probably give always the same numbers
q = x1*x2*(1.-mu)
Q = sqrt( x1*x1 + x2*x2 - 2.*x1*x2*mu ) # sqrt( (x1-x2)**2 +2.*q ) # the formula probably gives the same also
gammaStar = (x1-x2+Q*sqrt( 1. + 2./q ) )/2.
# print( gamma-gammaStar, gammaStar,gamma,Q,u,(u-Q)/(Q+u))
if gamma < gammaStar :
print ('w') # I belive that in the case fucntion just won't be called
return (0.,0.,0.,0.)
else:
Ra = u*( u*u - Q*Q )*( u*u + 5.*v )/2./q/q/v/v/v + u*Q*Q/q/q/v/v
Rb = 2./Q + u/v*( 1. - 2./q )
Rc = u/q/v*( ( u*u - Q*Q )/r/q - 2.)
Rd = 2./Q + 2*(u-Q)/r/q*((u-Q)/r/q*(2.*Q+u) - 4.) + 2.*u/v/q
R = Ra + Rb
RI = Ra + Rc
RU = Rd + 2.*Rc
RQ = RU + Ra
#print(r,v,u,q,Q,gammaStar,Ra,Rb,Rc,R)
#print(x1,x2,mu,gamma,R,RI,RQ,RU)
return (R,RI,RQ,RU)
#the return values of this functon seem to be all right
def Maxwell_r(gamma):
"""The normalized relativistic Maxwellian distribution
the density of particles in the dimensionless momentum volume (4 \pi z^2 dz) is nomalized to unity
Theta is the dimensionless electron gas temperature (Theta = k * T_e / m_e c^2)
gamma is electron energy in units of the electron rest mass
The fuction returns the momentum dencity value ( f(\gamma) )
"""
r = .25/pi/Theta*exp(-gamma/Theta)/K2Y
return r
def Compton_redistribution(x1,x2,mu): # if distribution is not Maxwellian the function must be modified.
""" Thermal Compton redistribution matrix (integrated with electron distribution function)
And the distribution is maxwellian (if it's not the function must be modified)
The arguments are:
x1 and x2 - photon energies in units of electron rest energy ( h \\nu / m_e c^2 )
mu - cosine of a scattering angle
This fuctions returns (R,RI,RQ,RU)
which are scalar redistribution functions for Maxwellian relativistic gas
and also the are non-zero elements of the matrix: R11,R12=R21,R22,R33 respectively
R44 or RV is also not equal to zero but we never need it
"""
q = x1*x2*(1.-mu)
Q = sqrt( x1*x1 + x2*x2 - 2.*x1*x2*mu )
gammaStar = (x1-x2+Q*sqrt( 1. + 2./q ) )/2. # lower bound of integration
C=3./8.*Theta*Maxwell_r(gammaStar)
gamma, gamma_weight = IntGamma
R=[0.,0.,0.,0.]
for i in range(NGamma):
T=Compton_redistribution_m(x1,x2,mu,Theta*gamma[i]+gammaStar)
for j in range(4):
R[j]+=C*gamma_weight[i]*T[j]
return tuple(R)
def Compton_redistribution_aa(x1,x2,mu1,mu2):
""" Azimuth-avereged Compton redistribution matrix
for computing of electron scattering source function
The arguements are:
x1 and x2 are photon energies in units of electron rest energy ( h \\nu / m_e c^2 )
mu1 and mu2 are cosines of angles between photon propagation directions and fixed direction
This function returns R11 R12 R21 R22 matrix elements
We need only 2x2 matrix in the upper left corner of the general matrix,
becouse U and V components on the Stokes vector are zero in this case.
"""
eta1 = 1. - mu1*mu1 # squared sinuses of the angles
eta2 = 1. - mu2*mu2
phi, phi_weight = IntAzimuth
az_c=cos(pi*phi) # array of azimuth cosines
az_s=sin(pi*phi)**2 # array of azimuth square sinuses
sc_c=mu1*mu2-sqrt(eta1*eta2)*az_c # array of scattering angles' cosines
sc_s=1. - sc_c**2 # array of scattering angles' squared sinuses
cos2chi1 = 2.*(mu1*sc_c-mu2)*(mu1*sc_c-mu2)/eta1/sc_s-1. # array[ cos( 2 \chi_1 ) ]
cos2chi2 = 2.*(mu1-mu2*sc_c)*(mu1-mu2*sc_c)/eta2/sc_s-1. # array[ cos( 2 \chi_2 ) ]
sin2chiP = 4.*(mu1-mu2*sc_c)*(mu1*sc_c-mu2)*az_s/sc_s**2 # array[ sin( 2 \chi_1 )*sin( 2 \chi_2 ) ]
R=zeros( (2,2,), )
for i in range(NAzimuth*2):
(C,I,Q,U)=Compton_redistribution(x1,x2,sc_c[i])
R[0,0]+=C*pi*phi_weight[i]
R[0,1]+=I*pi*cos2chi2[i]*phi_weight[i]
R[1,0]+=I*pi*cos2chi1[i]*phi_weight[i]
R[1,1]+=pi*(Q*cos2chi1[i]*cos2chi2[i]+U*sin2chiP[i])*phi_weight[i]
# print(x1,x2,mu1,mu2,R)
return R*x1*x1/x2
# In[21]:
if Spectrum=='Compton' : # checking symmetries #unnecessary
from check import *
print('Angular symmetry: ',CheckAngularSymmetry(Compton_redistribution_aa,0.02,0.02,-0.4,0.5))
s,gn=pr(s,gn,'ang-check')
print('Energy symmetry: ',CheckFrequencySymmetry(Compton_redistribution_aa,0.01,0.02,0.5,0.5,Theta))
s,gn=pr(s,gn,'freq-check')
# exit()
# In[22]:
if Spectrum=='Compton' : # Computing redistribution matrices for all energies and angles
# import FRM
# print(FRM.__doc__)
# print(FRM.fill.__doc__)
# def cr(x1,x2,m1,m2):
# R=Compton_redistribution_aa(x1,x2,m1,m2)
# return 1.0,1.0,2.0,1.0
# # return R[0][0],R[0][1],R[1][0],R[1][1]
# # FRM.cr=Compton_redistribution_aa
# RedistributionMatrix , sigma = FRM.fill( mu_weight,x_weight,x,mu,Theta,cr)
# print('7')
# # print(sigma)
# # exit()
sigma=zeros(NEnergy)
RedistributionMatrix = ones( (NEnergy,NEnergy,NZenith,NZenith,2,2) )
percent=0.0
for e in range(NEnergy): # x [-\infty,\infty]
for e1 in range(e,NEnergy): # x1 [x,\infty]
percent+=200/NEnergy/(NEnergy+1)
npc=int(percent*0.6)
print('||'+'%'*npc+' '*(59-npc)+'|| {:5.3f}%'.format(percent))
for d in range(NMu): # mu [-1,0]
for d1 in range(d,NMu): # mu1 [-1,mu]
md=NZenith-d-1 # -mu
md1=NZenith-d1-1 # -mu1
w=mu_weight[d1]*x_weight*mu_weight[d]
t=d1>d
f=e1>e
r=Compton_redistribution_aa(x[e],x[e1],mu[d],mu[d1])
rm=Compton_redistribution_aa(x[e],x[e1],mu[d],mu[md1])
sigma[e1]+=(r[0,0]+rm[0,0])*w
RedistributionMatrix[e,e1,d,d1]=r
RedistributionMatrix[e,e1,md,md1]=r
RedistributionMatrix[e,e1,d,md1]=rm
RedistributionMatrix[e,e1,md,d1]=rm
if f: # frequency symmetry
m=exp((x[e]-x[e1])/Theta)*x[e1]**3/x[e]**3
rf=r*m # when Maxwellian
rmf=rm*m # or Wein distributions
sigma[e]+=(rf[0,0]+rmf[0,0])*w
RedistributionMatrix[e1,e,d,d1]=rf
RedistributionMatrix[e1,e,md,md1]=rf
RedistributionMatrix[e1,e,d,md1]=rmf
RedistributionMatrix[e1,e,md,d1]=rmf
if t: # angular symmetry
r[0,1],r[1,0]=r[1,0],r[0,1]
rm[0,1],rm[1,0]=rm[1,0],rm[0,1]
sigma[e1]+=(r[0,0]+rm[0,0])*w
RedistributionMatrix[e,e1,d1,d]=r
RedistributionMatrix[e,e1,md1,md]=r
RedistributionMatrix[e,e1,md1,d]=rm
RedistributionMatrix[e,e1,d1,md]=rm
if f: # both symmeties
rf=r*m
rmf=rm*m
sigma[e]+=(rf[0,0]+rmf[0,0])*w
RedistributionMatrix[e1,e,d1,d]=rf
RedistributionMatrix[e1,e,md1,md]=rf
RedistributionMatrix[e1,e,md1,d]=rmf
RedistributionMatrix[e1,e,d1,md]=rmf
s,gn=pr(s,gn,'RMtable')
# In[26]:
if Spectrum=='Compton' : #check cross section
# cro = open('cros.dat','r')
figure(1,figsize=(10,11))
xscale('log')
# fx=[0.0]
# cs=[1.0]
# sgm=[1.0]
# de=lambda X : float (X[:X.find('D')]+'e'+X[X.find('D')+1:])
# for n in range(85):
# line = cro.readline().split()
# fx.append(10**de(line[3]))
# cs.append(de(line[5]))
# sgm.append(sigma_cs(fx[-1]))
# # print x, cs
# cs.append(0)
# fx.append(x[-1])
# sgm.append(sigma_cs(x[-1]))
# fcs=interp1d(fx,cs)
# fcsx=fcs(x)
# plot(fx,cs)
sigam=array(list(map(sigma_cs,x)))
plot(x,sigam,'b')
plot(x,sigma,'r')
# plot(x,fcsx,'k')
# plot(fx,sgm)
# plot(x,sigma2-fcsx)
print(sigma)
print(sigam)
print(sigam-sigma)
print(x)
# savefig('compsigma.png')
# show()
clf()
s,gn=pr(s,gn,'sigmaplot')
# In[27]:
if Spectrum=='Compton' : # Initializing Stokes vectors arrays, computiong scatterings
Iin=Planck # Delta # initial photon distribution
Source=zeros((ScatterNum,NDepth,NEnergy,NZenith,2)) # source function
Stokes=zeros((ScatterNum,NDepth,NEnergy,NZenith,2)) # intensity Stokes vector
Stokes_out=zeros((ScatterNum+1,NEnergy,NZenith,2)) # outgoing Stokes vector of each scattering
Stokes_in=zeros((NDepth,NEnergy,NZenith,2)) # Stokes vector of the initial raiation (0th scattering)
Intensity=zeros((NEnergy,NZenith,2)) # total intensity of all scattering orders from the slab suface
for e in range(NEnergy):
for d in range(NZenith):
for t in range(NDepth):
Stokes_in[t,e,d,0]=Iin(x[e])*exp(-tau[t]*sigma[e]/mu[d]) if mu[d]>0 else 0
Stokes_in[t,e,d,1]=0
else:
Stokes_out[0,e,d,0]=Iin(x[e])*exp(-tau_T*sigma[e]/mu[d]) if mu[d]>0 else 0
Stokes_out[0,e,d,1]=0
s,gn=pr(s,gn,'I0')
# if mod:
try: # Fortran
import FIF
# print(FIF.fill.__doc__)
Stokes=FIF.fill(ScatterNum,Stokes_in,RedistributionMatrix,x_weight,sigma,mu,mu_weight,tau,tau_weight)
s,gn=pr(s,gn,'I')
# else:
except: # python
for k in range(ScatterNum): # do ScatterNum scattering iterations
for t in range(NDepth): # S_k= R I_{k-1}
for e in range(NEnergy):
for d in range(NZenith):
S=zeros(2)
for e1 in range(NEnergy):
for d1 in range(NZenith):
w = mu_weight[d1]*x_weight # total weight
r = RedistributionMatrix[e,e1,d,d1] #
I = Stokes[k-1,t,e1,d1] if k>0 else Stokes_in[t,e1,d1]
S[0]+= w*( I[0]*r[0,0] + I[1]*r[0,1] ) #
S[1]+= w*( I[0]*r[1,0] + I[1]*r[1,1] ) #
Source[k,t,e,d]+=S #
for t in range(NDepth):# I_k= integral S_k
for e in range(NEnergy):
for d in range(NZenith):
I=tau_weight/2*Source[k,t,e,d]
if mu[d]>0:
for t1 in range(t) : #
S=Source[k,t1,e,d] #
I+=tau_weight*S*exp(sigma[e]*(tau[t1]-tau[t])/mu[d])
S=Source[k,0,e,d] #
I-=tau_weight*S*exp(sigma[e]*(-tau[t])/mu[d])
else:
for t1 in range (t+1,NDepth):
S=Source[k,t1,e,d] #
I+=tau_weight*S*exp(sigma[e]*(tau[t1]-tau[t])/mu[d])
S=Source[k,NDepth-1,e,d] #
I-=tau_weight*S*exp(sigma[e]*(tau_T-tau[t])/mu[d])
Stokes[k,t,e,d]+=I/abs(mu[d]) #abs
s,gn=pr(s,gn,'I'+str(1+k))
Intensity += Stokes_out[0]
for k in range(ScatterNum):
Stokes_out[k+1]+=Stokes[k,-1]
Intensity += Stokes[k,-1]
# In[ ]:
if Spectrum=='Thomson':
Iin=Planck # Delta # initial photon distribution
Stokes=zeros((ScatterNum,NDepth,NEnergy,NZenith,2)) # intensity Stokes vector
Stokes_out=zeros((ScatterNum+1,NEnergy,NZenith,2)) # outgoing Stokes vector of each scattering
Intensity=zeros((NEnergy,NZenith,2)) # total intensity of all scattering orders from the slab suface
I_l = zeros((NDepth,NZenith))
I_r = zeros((NDepth,NZenith))
A=zeros(NDepth)
B=zeros(NDepth)
C=zeros(NDepth)
# x_factor=ones(NEnergy)
x_nought=x.copy()
print('Thart')
for d in range(NZenith):
for t in range(NDepth):
I_l[t,d]=exp(-tau[t]/mu[d]) if mu[d]>0 else 0
I_r[t,d]=I_l[t,d]
for e in range(NEnergy):
Stokes_out[0,e,d,0]=Iin(x[e])*(I_l[t,d] + I_r[t,d])
Stokes_out[0,e,d,1]=0
for k in range(ScatterNum): # do ScatterNum scattering iterations
# x_factor *= 1 + 4*Theta #- x
x_nought = (1 + 4*Theta - sqrt(( (1 + 4*Theta)**2-4*x_nought) ))/2
A[:]=0.
B[:]=0.
C[:]=0.
for t in range(NDepth): #
for d in range(NMu):
md=NZenith-d-1
A[t]+=(1. - mu[d]**2)*(I_l[t,d] + I_l[t,md])*mu_weight[d]*3./4./3
B[t]+=(mu[md]**2)*(I_l[t,d] + I_r[t,md])*mu_weight[d]*3./8./3
C[t]+=(I_r[t,md] + I_r[t,d])*mu_weight[d]*3./8./3
I_l[:]=0.
I_r[:]=0.
for t in range(NDepth): #
for d in range(NZenith):
w=tau_weight/abs(mu[d])
I_l[t,d]+=(A[t]*(1 - mu[d]**2) + (B[t] + C[t])*mu[d]**2)*w/2
I_r[t,d]+=(B[t] + C[t])*w/2
if mu[d]>0:
trange=range(t)
t1=0
else:
trange=range(t+1,NDepth)
t1=-1
I_l[t,d]-=exp((tau[t1]-tau[t])/mu[d])*(A[t1]*(1. - mu[d]**2) + (B[t1] + C[t1])*mu[d]**2)*w/2
I_r[t,d]-=exp((tau[t1]-tau[t])/mu[d])*(B[t1] + C[t1])*w/2
for t1 in trange:
I_l[t,d]+=exp((tau[t1] - tau[t])/mu[d])*(A[t1]*(1. - mu[d]**2) + (B[t1] + C[t1])*mu[d]**2)*w
I_r[t,d]+=exp((tau[t1] - tau[t])/mu[d])*(B[t1]+C[t1])*w
for e in range(NEnergy):
Stokes[k,t,e,d,0]=Iin(x_nought[e])*(I_l[t,d] + I_r[t,d])*x[e]/x_nought[e]
Stokes[k,t,e,d,1]=Iin(x_nought[e])*(I_l[t,d] - I_r[t,d])*x[e]/x_nought[e]
# Stokes[k,t,e,d,0]=Iin(x[e]/x_factor[e])*(I_l[t,d] + I_r[t,d])*x_factor[e]
# Stokes[k,t,e,d,1]=Iin(x[e]/x_factor[e])*(I_l[t,d] - I_r[t,d])*x_factor[e]
s,gn=pr(s,gn,'I'+str(k+1))
Intensity += Stokes_out[0]
for k in range(ScatterNum):
Stokes_out[k+1]+=Stokes[k,-1]
Intensity += Stokes[k,-1]
s,gn=pr(s,gn,'I0')
# In[ ]:
if Spectrum=='Burst' : # Initializing Stokes vectors arrays, computing zeroth scattering
Intensity=zeros((NEnergy,NZenith,2)) # total intensity of all scattering orders from the slab suface
for e in range(NEnergy):
# print(e,log(Planck(x[e-1])/Planck(x[e]))/log(x[e]/x[e-1]),' ',log(x[e])/log(10),' ')
for d in range(NZenith):
Intensity[e,d,0]=Planck(x[e])*(1 + 2.06*mu[d])
Intensity[e,d,1]=Intensity[e,d,0]*0.1171*(mu[d] - 1.)/(1. + 3.582*abs(mu[d]))
s,gn=pr(s,gn,'I0')
# exit()
# In[ ]:
if Spectrum=='FromFile' :
inI = open(AtmName+'I.bin')
inx = open(AtmName+'x.bin')
inm = open(AtmName+'m.bin')
x=fromfile(inx)
mu=fromfile(inm)
NEnergy=len(x)
NZenith=len(mu)
NMu=NZenith//2
Intensity=fromfile(inI).reshape((NEnergy,NZenith,2))
s,gn=pr(s,gn,'I is read')
else:
outI = open(AtmName+'I.bin','w')
outx = open(AtmName+'x.bin','w')
outm = open(AtmName+'m.bin','w')
Intensity.tofile(outI,format="%e")
x.tofile(outx,format="%e")
mu.tofile(outm,format="%e")
# In[28]:
if plotAtm: # plot Everything and save All pics and tabs
Iin=Planck # Delta # initial photon distribution
Stokes_defined=Spectrum=='Compton' or Spectrum=='Thomson'
Sangles= range(NMu) if Stokes_defined else [] # list of the angle indeces to be plot a detailed figure
Senergy=[int(0.5+n*log(1+4*Theta)/x_weight) for n in range(NColors) ] if Stokes_defined else []
print(Senergy)
outF = open(AtmName+'Fx.dat','w')
outp = open(AtmName+'Pd.dat','w')
frmt=lambda val, list: '{:>8}'.format(val)+': '+' '.join('{:15.5e}'.format(v) for v in list) +'\n'
outp.write(frmt('Energies',x) )
outF.write(frmt('Energies',x) )
labelsize=20
fontsize=25
figA=figure(1,figsize=(10,11))
figA.clear()
figA.suptitle(r'$\tau_T=$'+str(tau_T)+
r'$,\,T={:5.1f}keV$'.format(T*evere/1e3)+
r'$,\,\Theta=$'+str(Theta),fontsize=fontsize)
plotAF=figA.add_subplot(2,1,1,xscale='log',yscale='log')
plotAp=figA.add_subplot(2,1,2,xscale='log')
xIinx=[(Iin(x[e])*x[e]) for e in range(NEnergy)]
# plotAF.set_xlim([x[0],x[-1]])
plotAF.set_xlim([1e-3,1.])
plotAF.set_ylim([1e-3,1e-2])
plotAF.set_ylabel(r'$xI_x(\tau_T,x)$',fontsize=fontsize)
plotAF.tick_params(axis='both', which='major', labelsize=labelsize)
plotAF.plot(x,xIinx,'k-.')
plotAp.set_xlim([x[0],x[-1]])
plotAp.tick_params(axis='both', which='major', labelsize=labelsize)
plotAp.set_xlabel(r'$x\,[m_ec^2]$',fontsize=fontsize)
plotAp.set_ylabel(r'$p\,[ \% ]$',fontsize=fontsize)
plotAp.plot(x,[.0]*NEnergy,'-.',color='xkcd:brown')
for d in range(NMu):
d1=d+NMu
z=str(int(arccos(mu[d1])*180/pi))
xFx=(Intensity[:,d1,0]*x[:])
plotAF.plot(x,xFx,colors[(d*NColors)//NMu])
outF.write( frmt(z+'deg',xFx) )
p=(Intensity[:,d1,1]/Intensity[:,d1,0]*1e2)
plotAp.plot(x,p,colors[(d*NColors)//NMu])
outp.write( frmt(z+'deg',p) )
if d in Sangles: # Specific angle
figS=figure(2,figsize=(10,13))
figS.suptitle(r'$\tau_T=$'+str(tau_T)+
r'$,\,T={:5.1f}keV$'.format(T*evere*1e-3)+
r'$,\,\Theta=$'+str(Theta)+
r'$,\,\mu={:5.3f}$'.format(mu[d1])+
r'$\,(z\approx$'+z+
r'$^{\circ})$',fontsize=fontsize)
plotSF=figS.add_subplot(3,1,1,xscale='log',yscale='log')
plotSc=figS.add_subplot(3,1,2,xscale='log')
plotSp=figS.add_subplot(3,1,3,xscale='log')
plotSF.set_ylabel(r'$xI_x(\tau_T,x)$',fontsize=fontsize)
plotSF.tick_params(axis='both', which='major', labelsize=labelsize)
plotSF.set_xlim([x[0],x[-1]])
plotSF.set_ylim([1e-6,1e-1])
plotSF.plot(x,xFx,color='k',linewidth=2.1)
plotSF.plot(x,xIinx,'-.',color='xkcd:brown')
outF.write(frmt('Sc.N.0',xFx) )
plotSc.set_xlim([x[0],x[-1]])
plotSc.tick_params(axis='both', which='major', labelsize=labelsize)
plotSc.set_ylabel(r'$c\,[ \% ]$',fontsize=fontsize)
plotSp.set_xlim([x[0],x[-1]])
plotSp.tick_params(axis='both', which='major', labelsize=labelsize)
plotSp.set_xlabel(r'$x\,[m_ec^2]$',fontsize=fontsize)
plotSp.set_ylabel(r'$p\,[ \% ]$',fontsize=fontsize)
plotSp.plot(x,p,color='k',linewidth=2.1)
for k in range(ScatterNum+1):
xFx=(Stokes_out[k,:,d1,0]*x[:])
contribution=(Stokes_out[k,:,d1,0]/Intensity[:,d1,0]*1e2)
p=[.0]*NEnergy if k==0 else (Stokes_out[k,:,d1,1]/Stokes_out[k,:,d1,0]*1e2)
outF.write( frmt('Sc.N.'+str(k),xFx) )
outp.write( frmt('Sc.N.'+str(k),p) )
plotSF.plot(x,xFx,'--',color=colors[(k*NColors)//(ScatterNum+1)])
plotSc.plot(x,contribution,'--',color=colors[(k*NColors)//(ScatterNum+1)])
plotSp.plot(x,p,'--',color=colors[(k*NColors)//(ScatterNum+1)])
figS.savefig(AtmName+'z'+z+'.pdf')
figS.clf()
figA.savefig(AtmName+'zAll.pdf')
figA.clf()
figA.suptitle(r'$\tau_T=$'+str(tau_T)+
r'$,\,T={:5.1f}keV$'.format(T*evere/1e3)+
r'$,\,\Theta=$'+str(Theta),fontsize=fontsize)
plotAF=figA.add_subplot(2,1,1,xscale='linear',yscale='linear')
plotAp=figA.add_subplot(2,1,2,xscale='linear')
plotAF.set_xlim(0,1)
# plotAF.set_ylim()
plotAF.set_ylabel(r'$I_x(\tau_T,\mu)/I_x(\tau_T,1)$',fontsize=fontsize)
plotAF.tick_params(axis='both', which='major', labelsize=labelsize)
plotAp.set_xlim(0,1)
plotAp.tick_params(axis='both', which='major', labelsize=labelsize)
plotAp.set_xlabel(r'$\mu$',fontsize=fontsize)
plotAp.set_ylabel(r'$p\,[ \% ]$',fontsize=fontsize)
plotAp.plot(mu[NMu:NZenith],[.0]*NMu,'-.',color='xkcd:brown')
# show()
outp.write('\n\n' )
outF.write('\n\n' )
outp.write(frmt('Cosines',mu[NMu:]) )
outF.write(frmt('Cosines',mu[NMu:]) )
outp.write(frmt('Angles',arccos(mu[NMu:])*180/pi) )
outF.write(frmt('Angles',arccos(mu[NMu:])*180/pi) )
k=-1
for e in range(NEnergy):
Esp="{:<8.2e}".format(x[e])
Inorm=(Intensity[e,NMu:,0]/Intensity[e,-1,0])
p=(Intensity[e,NMu:,1]/Intensity[e,NMu:,0]*1e2)
outF.write( frmt(Esp,Inorm) )
outp.write( frmt(Esp,p) )
if e in Senergy:
k=k+1
# plotAp.plot(mu[NMu:NZenith],p,'-.',color='xkcd:black')
# plotAF.plot(mu[NMu:NZenith],Inorm,'-.',color='xkcd:black')
Inorm=(Stokes_out[k,e,NMu:,0]/Stokes_out[k,e,-1,0])
p=[.0]*NMu if k==0 else (Stokes_out[k,e,NMu:,1]/Stokes_out[k,e,NMu:,0]*1e2)
plotAp.plot(mu[NMu:],p,'-',color=colors[k])
plotAF.plot(mu[NMu:],Inorm,'-',color=colors[k])
figA.savefig(AtmName+'In.pdf')
outF.close()
outp.close()
s,gn=pr(s,gn,'plap')
# In[30]:
if computePulse:
NPhi = 120 # Number of equidistant phase points
NPhase = 150 # Number of observation phases
NBend= 20 # Number of knots in light bending integrations
NAlpha= 1000 # 10000 # Number of psi/aplha grid points
IntBend = leggauss(NBend)
NZenithBig=100
NRho=4#2#8
NVarphi=6#4
# IntVarphi = linspace(0,2*pi,num=NVarphi,endpoint=False,retstep=True)
IntVarphi = leggauss(NVarphi)
IntRho = leggauss(NRho)
phi=linspace(0,2*pi,num=NPhi,endpoint=False,retstep=False)
phase =linspace(0,1,num=NPhase,endpoint=True,retstep=False)
phase_obs=zeros(NPhi)
nu=600 # star rotation frequency in Hz
M=1.4 # star mass in solar masses
R_g=M*2.95 # gravitational Schwarzschild radius
R_e=12.0 # equatorial radius of the star in kilometers
if oblateness=='AlGendy': # from AlGendy et. al. (2014)
Omega_bar=2*pi*nu*sqrt(2*R_e**3/R_g)/c
print('_O_^2',Omega_bar**2,'_O_',Omega_bar)
flattening=(0.788-0.515*R_g/R_e)*Omega_bar**2
print(R_e*(1-flattening))
elif oblateness=='Sphere':
flattening=0.0
else:
flattening=oblateness
# exit()
print(flattening)
outParams = open(PulsName+'.dat','w')
outParams.write(AtmName+'.dat is the name of file with some corresponding atmosphere model\n')
outParams.write(str(R_e)+' = equatorial radius R_e\n')
outParams.write(str(M)+' = star mass M in solar masses\n')
outParams.write(str(nu)+' = star rotation frequency nu in Hz\n')
outParams.write(str(NPhi)+' '+str(NBend)+' '+str(NAlpha)+' = NPhi NBend NAlpha parameters\n')
def Beloborodov(cos_psi):
"""Beloborodov's approximation for cos_alpha(cos_psi) light bending function
takes the cos psi
returns the cos alpha and its derivative
"""
return 1. + (cos_psi - 1.)/redshift**2 ,1./redshift**2
def Schwarzschild(R,alpha):
"""Schwarzschild exact relation between the \psi and \\alpha angles, where
\\alpha is the angle between radius vector of the spot and the direction of the outgoing photon near the surface
and \psi is the angle between normal and light propagation at the limit of infinite distance.
For given distance from the mass center and the emission angle \\alpha
this function returns two numbers:
the corresponding angle \psi
and the time lag over against the fotons emited with zero impact parameter at the radius.
"""
kx,wx=IntBend
eps=(1+kx[0])/4e2
u=R_g/R
b=sin(alpha)/sqrt(1-u)*R # impact parameter
if 2*alpha>pi+eps:
cos_3eta=sqrt(27)*R_g/2/b
if cos_3eta > 1:
return pi+2*eps,0 # the timelag
closest_approach=-2*b/sqrt(3)*cos(arccos(cos_3eta)/3 + 2*pi/3)
psi_max, lag_max= Schwarzschild(closest_approach,pi/2.)
psi_min, lag_min= Schwarzschild(R,pi-alpha)
psi=2*psi_max - psi_min
lag=2*lag_max - lag_min # + 2*(R - closest_approach + R_g*log((R - R_g)/(closest_approach - R_g)))/c
if psi>pi:
return pi+eps,lag
else:
psi=0
lag=(R_e - R + R_g*log( (R_e - R_g)/(R - R_g) ) )/c
for i in range(NBend):
ex=(kx[i]+1)/2
q=(2. - ex*ex - u*(1 - ex*ex)**2/(1 - u))*sin(alpha)**2
sr=sqrt(cos(alpha)**2+ex*ex*q)
if 2*alpha>pi-eps:
dpsi=b/R/sqrt(q)*wx[i] #*2/2
else:
dpsi=ex*b/R/sr*wx[i] #*2/2
dlag=dpsi*b/c/(1+sr) #*2/2
psi+= dpsi
lag+= dlag
return psi,lag
# flattening=0
NRadius=2 + int(flattening*R_e/1e-1)
print(NRadius)
r, dr = linspace(R_e*(1 - flattening),R_e,num=NRadius,retstep=True)
alpha, dalpha = linspace(0,arccos(-1/sqrt(2*r[0]/R_g/3)),NAlpha,retstep=True)
psi=zeros((NRadius,NAlpha))
dt=zeros((NRadius,NAlpha))
for d in range(NRadius):
print(d)
for a in range(NAlpha):
psi[d,a],dt[d,a]=Schwarzschild(r[d],alpha[a])
s,gn=pr(s,gn,'psidt')
Flux=zeros((NPhase,NEnergy,3))
Flux_obs=zeros((NPhi,NEnergy,3))
i=pi*7/18 # line of sight colatitude
rho_total=0.15 # pi/5 #pi*5/180 # radius of the spot
theta_center=pi/4.1
antipodal = True
NSpots=0
varphi,dvarphi=IntVarphi[0]*pi,IntVarphi[1] *pi
rho,drho=(IntRho[0]+1)*rho_total/2,(IntRho[1])*rho_total/2
# print(IntVarphi)
# print(rho,drho)
l=[]
theta=[]
dS=[]
for v in range(NVarphi):
for rh in range(NRho):
NSpots+=1
cos_theta=cos(theta_center)*cos(rho[rh])+sin(theta_center)*sin(rho[rh])*cos(varphi[v])
sin_l=sin(rho[rh])*sin(varphi[v])/sqrt(1- cos_theta**2)
cos_l=sqrt(1- sin_l**2)
if cos_theta*cos(theta_center)> cos(rho[rh]) :
cos_l=-cos_l
l.append(arctan2(-sin_l,-cos_l) + pi)
theta.append(arccos(cos_theta))
dS.append(drho[rh]*dvarphi[v]*sin(rho[rh]))
print(v,rh,cos_theta,cos_l,sin_l,l[-1],theta[-1],dS[-1])
if antipodal :
NSpots+=1
l.append(arctan2(sin_l,cos_l) + pi)
theta.append(pi- theta[-1])
dS.append(dS[-1])
print(v,rh,cos_theta,cos_l,sin_l,l[-1],theta[-1],dS[-1])
print(NSpots)
outParams.write(str(i)+' = sight colatitude i\n')
outParams.write(str(theta_center)+' = spot colatitudes theta\n')
# outParams.write(str(l)+' = spot longitudes l\n')
outParams.write(str(rho_total)+' = angular radius of the spot rho\n')
sin_i=sin(i)
cos_i=cos(i)
BoloFlux=zeros((NPhase,3))
z=cos(linspace(-pi/2,pi/2,num=NZenithBig))
logIntensity=zeros((NEnergy,NZenithBig,3))
for e in range(NEnergy):
IntInt=CubicSpline(mu,Intensity[e,:,0],extrapolate=True) # interpolate intensity
IQ=CubicSpline(mu,Intensity[e,:,1],extrapolate=True) #
for d in range(NZenithBig):
logIntensity[e,d] = log(max(0,IntInt(z[d]))),log(absolute(IQ(z[d]))),sign(IQ(z[d]))
mu=z.copy()
s,gn=pr(s,gn,'second precomp')
for p in range(NSpots):
sin_theta=sin(theta[p])
cos_theta=cos(theta[p])
R=R_e*(1 - flattening*cos_theta**2)
dR=2*R_e*flattening*cos_theta*sin_theta # dR / d\theta
r1=bisect(r[1:-1],R)
r2=r1 + 1
dr1=(R - r[r1])/dr
dr2=(r[r2] - R)/dr
redshift=1.0/sqrt(1.0 - R_g/R) # 1/sqrt(1-R_g/R) = 1+ z = redshift
f=redshift/R*dR
sin_gamma=f/sqrt(1 + f**2) # angle gamma is positive towards the north pole
cos_gamma=1.0/sqrt(1 + f**2)
beta=2*pi*nu*R*redshift*sin_theta/c
Gamma=1.0/sqrt(1.0 - beta**2)
Gamma1= (1.0-sqrt(1.0 - beta**2) )/ beta
# print(Gamma,beta,Gamma1,'\t\t\t',p/NSpots)
for t in range(NPhi):
if True: # find mu
phi0=phi[t]+l[p]
sin_phi=sin(phi0)
cos_phi=cos(phi0)
cos_psi=cos_i*cos_theta + sin_i*sin_theta*cos_phi
sin_psi=sqrt(1. - cos_psi**2)
psi0=arccos(cos_psi)
a1=bisect(psi[r1], psi0)
a2=bisect(psi[r2], psi0)
psi1=psi[r1, a1]
psi2=psi[r2, a2]
dpsi1=psi1 - psi[r1, a1 - 1]
dpsi2=psi2 - psi[r2, a2 - 1]
dpsi=dpsi1*dr2 + dpsi2*dr1
dalpha1 = dalpha*(psi1 - psi0)/dpsi1
dalpha2 = dalpha*(psi2 - psi0)/dpsi2
alpha1=alpha[a1] - dalpha1
alpha2=alpha[a2] - dalpha2
cos_alpha = cos(alpha2*dr1 + alpha1*dr2) # linear interpolation of alpha(psi)
sin_alpha = sqrt(1. - cos_alpha**2)
sin_alpha_over_sin_psi= sin_alpha/sin_psi if sin_psi > 1e-4 else 1./redshift
dcos_alpha=sin_alpha_over_sin_psi *dalpha/dpsi # d cos\alpha \over d \cos \psi
# cos_alpha, dcos_alpha=Beloborodov(cos_psi) # insert exact formula here
# sin_alpha = sqrt(1. - cos_alpha**2)
# sin_alpha_over_sin_psi= sin_alpha/sin_psi if sin_psi > 1e-4 else 1./redshift
dt1=dt[r1,a1 - 1]*dalpha1/dalpha + dt[r1,a1]*(1. - dalpha1/dalpha)
dt2=dt[r2,a2 - 1]*dalpha2/dalpha + dt[r2,a2]*(1. - dalpha2/dalpha)
dphase=(dt1*dr2 + dt2*dr1)*nu # \delta\phi = \phi_{obs} - \phi
# dphase = 0
phase_obs[t]=( phi[t]/2/pi+dphase)%1.
cos_xi = - sin_alpha_over_sin_psi*sin_i*sin_phi
delta = 1./Gamma/(1.-beta*cos_xi)
cos_sigma = cos_gamma*cos_alpha + sin_alpha_over_sin_psi*sin_gamma*(cos_i*sin_theta - sin_i*cos_theta*cos_phi)
sin_sigma = sqrt(1. - cos_sigma**2)
mu0=delta*cos_sigma # cos(sigma')
Omega=dS[p]*mu0*redshift**2*dcos_alpha #*Gamma*R*R/cos_gamma #
# Omegaarray[t]=max(Omega,0)
# print(t,' : \t',mu0,' \t ',dcos_alpha,'\t',dphi,cos_alpha,cos_psi,Omega)
if mu0<0: # this only for speeding up. the backwards intensity is usually zero
Flux_obs[t]=0
continue