-
Notifications
You must be signed in to change notification settings - Fork 1
/
limb_darkening.py
executable file
·92 lines (62 loc) · 2.17 KB
/
limb_darkening.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
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
class mc_packet(object):
def __init__(self, tau_low):
self.tau_low = tau_low
self.tau = tau_low
self.mu = np.sqrt(np.random.rand(1)[0])
self.draw_new_tau()
def draw_new_tau(self):
self.tau_int = -np.log(np.random.rand(1)[0])
def scatter_packet(self):
self.mu = 2. * np.random.rand(1)[0] - 1.
def advance_packet(self):
self.tau -= self.mu * self.tau_int
def check_for_backscatter(self):
if self.tau > self.tau_low:
return True
else:
return False
def check_for_escape(self):
if self.tau < 0:
return True
else:
return False
def propagate_packet_until_escape(self):
while True:
self.advance_packet()
if self.check_for_backscatter():
return False
if self.check_for_escape():
return True
self.scatter_packet()
## if self.check_for_escape():
## return True
## if self.check_for_backscatter():
## self.tau = self.tau_low
## self.mu = -self.mu
## else:
## self.scatter_packet()
self.draw_new_tau()
def run_simulation(Npackets, tau_min = 10):
mu_esc = []
mu_bins = np.linspace(0, 1, 21)
dmu = mu_bins[1] - mu_bins[0]
for i in xrange(Npackets):
print(i)
packet = mc_packet(tau_min)
if packet.propagate_packet_until_escape():
mu_esc.append(packet.mu)
mu_esc = np.array(mu_esc)
mu_mid = (np.floor(mu_esc / dmu) + 0.5 ) * dmu
plt.hist(mu_esc, bins = mu_bins, normed = True, label = "Monte Carlo results")
plt.figure()
plt.hist(mu_esc, bins = mu_bins, weights = 1. / mu_mid, normed = True, label = "Monte Carlo results")
plt.plot(mu_bins, 1. / 0.7 * (0.4 + 0.6 * mu_bins), label = "expectation", color = "red", lw = 2)
plt.xlabel(r"$\mu$")
plt.ylabel(r"$I(\mu)$ [arbitrary units]")
plt.legend(loc = "upper left")
plt.show()
if __name__ == "__main__":
run_simulation(100000)