Blog: Update 11/20: adiabatic_profile.py

File adiabatic_profile.py, 3.7 KB (added by adebrech, 7 years ago)
Line 
1#!/usr/bin/env python
2
3#Need to add n_p, the neutral density at planet surface to problem.data
4
5import math
6
7Gamma, M_p, T_p, Lambda_p, rho_p, Lx, Nx, max_L = (None for i in range(8))
8
9# read these from astrobear constant file
10G = 6.67e-8
11k_B = 1.381e-16
12m_H = 1.673e-24
13sigma = 6.3e-18
14M_J = 1.898e30
15R_J = 7.1492e9
16
17# read in dx, Gamma, M_p, Cs_p, R_p
18with open('physics.data') as infile:
19 for line in infile:
20 sline = line.strip().split('!', 1)[0]
21 if sline is "":
22 continue
23 if sline.split()[0] == "gamma":
24 Gamma = float(sline.split()[2].replace("d", "e"))
25with open('problem.data') as infile:
26 for line in infile:
27 sline = line.strip().split('!', 1)[0]
28 if sline is "":
29 continue
30 sline = sline.split()
31 if sline[0] == "planetMass":
32 M_p = float(sline[2].replace("d", "e"))*M_J
33 if sline[0] == "planetTemp":
34 T_p = float(sline[2].replace("d", "e"))
35 if sline[0] == "planetRadius":
36 R_p = float(sline[2].replace("d", "e"))*R_J
37 if sline[0] == "planetDensity":
38 rho_p = float(sline[2].replace("d", "e"))
39with open('global.data') as infile:
40 for line in infile:
41 sline = line.strip().split('!', 1)[0]
42 if sline == "":
43 continue
44 sline = sline.split()
45 if sline[0] == "GmX":
46 Nx = int(sline[2].split(',')[0].replace("d", "e"))
47 if sline[0] == "GxBounds":
48 Lx = float(sline[2].split(',')[3].replace("d", "e")) \
49 -float(sline[2].split(',')[0].replace("d", "e"))
50 if sline[0] == "MaxLevel":
51 max_L = int(float(sline[2].replace("d", "e")))
52
53if Gamma < 1.01:
54 print ("Warning: default gamma = %f, changing to 5/3 for calculation" % Gamma)
55 Gamma = 5./3.
56
57Cs_p2 = (k_B*T_p)/m_H
58n_p = rho_p/m_H
59Nx=Nx*(2**max_L)
60dx = Lx/Nx
61print Lx, Nx, dx
62Gamma_1 = Gamma - 1.0
63r_crit = (Gamma_1*G*M_p)/(Gamma*Cs_p2*R_p)
64r_zero = (r_crit)/(r_crit-1.0)
65n_rho = 1.0/Gamma_1
66n_prs = Gamma/Gamma_1
67rScale = rho_p
68lScale = R_p
69pScale = rho_p*Cs_p2
70
71#atmopshere profile
72def atm_profile(r):
73 return (r_crit*(1./r - 1./r_zero))**(1./Gamma_1)
74
75#radiative transfer
76h = 1.0e-6/3.
77tau = 0.0
78i=0
79if r_zero < 0:
80 print "Warning: infinitely extended atmosphere"
81 r_start = 3.
82else:
83 r_start = r_zero
84
85while (tau < 1):
86 rad = r_start - i*3.*h
87 tau += 3.*h/8.*(sigma*n_p*R_p)* \
88 ( atm_profile(rad) + 3.*atm_profile(rad-h) + \
89 3.*atm_profile(rad-2.*h) + atm_profile(rad-3.*h) )
90 i += 1
91
92r_tau1 = 1
93r_ib = r_zero/(1+(r_zero-r_tau1)/r_tau1*math.exp(3*Gamma_1))
94r_mask = r_ib - 5*Lx/Nx
95r_ob = r_zero/(1+(r_zero-r_tau1)/r_tau1*.1**Gamma_1)
96
97with open('adiabat.data', 'w') as outfile:
98 nEntries = int(math.ceil((r_zero-r_mask)/dx))
99 outfile.write("%d\n" % (nEntries+1))
100 outfile.write("r_mask, r_ib, r_tau1, r_ob, r_crit, r_zero\n")
101 outfile.write(str("%e %e %e %e %e %e\n" % (r_mask, r_ib, r_tau1, r_ob, r_crit, r_zero)).replace("e",
102"d"))
103 outfile.write("r/r_p, rho/rho_p, P/P_p\n")
104 for i in range(0, nEntries):
105 rad = r_mask + i*dx
106 f_rad = r_crit*(1.0/rad-1.0/r_zero)
107 rho = f_rad**n_rho
108 P = f_rad**n_prs
109 outfile.write(str("%e" % rad).replace("e", "d")) #r/r_p
110 outfile.write(str(" %e" % rho).replace("e", "d")) #rho/rho_p
111 outfile.write(str(" %e" % P).replace("e", "d")) #E/E_p
112 outfile.write("\n")
113 rad = r_zero
114 f_rad = r_crit*(1.0/rad-1.0/r_zero)
115 rho = f_rad**n_rho
116 P = f_rad**n_prs
117 outfile.write(str("%e" % rad).replace("e", "d")) #r/r_p
118 outfile.write(str(" %e" % rho).replace("e", "d")) #rho/rho_p
119 outfile.write(str(" %e" % P).replace("e", "d")) #E/E_p
120
121
122print "For local problem suggest setting:"
123print "rScale = %s" % (str("%e" % (rScale)).replace("e", "d"))
124print "pScale = %s" % (str("%e" % (pScale)).replace("e", "d"))
125print "lScale = %s" % (str("%e" % (lScale)).replace("e", "d"))