-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTP_from_helios.py
216 lines (197 loc) · 9.3 KB
/
TP_from_helios.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
#%%
import numpy as np
import os
import sys
import subprocess
from scipy import interpolate
import parallel_functions as pf
import pandas as pd
scratch = '/scratch/s2555875'
helios_folder = os.path.join(scratch, 'HELIOS')
og_param = os.path.join(helios_folder, 'param.dat')
output = os.path.join(helios_folder, 'output')
star_df = pd.read_csv(os.path.join(scratch, 'stellar_flux', 'stellar_params.csv'))
nsim_dist = 15
#%%
def change_line(line, bit_id, new_val):
bits = line.split()
bits[bit_id] = new_val
return ' '.join(bits) + '\n'
def create_param_file(sim_name, dist = None, sname = None, manual = False, r_star = None, T_star = None, subfolder = ''):
''' Function to loop through the origianl HELIOS parameter file and change
needed lines. For now these lines are: name and orbital distance.
Future lines might contain: stellar spectrum file, stellar spectrum dataset,
vertical mixing ratio'''
new_str = ''
with open(og_param, 'r') as og:
for line in og.readlines():
if line.strip() and line.split()[0] == 'name':
new_str += change_line(line, 2, sim_name)
elif 'realtime plotting' in line:
new_str += change_line(line, 3, 'no')
elif dist != None and 'orbital distance' in line:
new_str += change_line(line, 6, str(dist))
elif sname != None and 'path to stellar spectrum file' in line: # by default it asks for dile in param.dat
new_str += change_line(line, 8, './star_tool/output/{}.h5'.format(sname.lower()))
elif sname != None and sname not in ['EARLY_SUN', 'SUN'] and 'dataset in stellar spectrum file' in line:
new_str += change_line(line, 8, '/r50_kdistr/muscles/{}'.format(sname.lower()))
elif sname != None and sname in ['EARLY_SUN', 'SUN'] and 'dataset in stellar spectrum file' in line:
new_str += change_line(line, 8, '/r50_kdistr/ascii/{}'.format(sname.lower()))
elif sname != None and 'planet =' in line:
if manual:
new_str += change_line(line, 2, 'manual')
else:
new_str += change_line(line, 2, sname.upper() + '_own') # all info on planets are in HELIOS/source/planet_database.py
elif r_star != None and 'radius star [R_Sun]' in line:
new_str += change_line(line, 6, str(r_star))
elif T_star != None and 'temperature star [K]' in line:
new_str += change_line(line, 6, str(T_star))
else:
new_str += line
new_param_file = os.path.join(subfolder,'param_{}.dat'.format(sim_name))
with open(new_param_file, 'w') as f:
f.write(new_str)
return new_param_file
def read_helios_tp(sim_name):
tp_helios_file = os.path.join(helios_folder, 'output', sim_name, '{}_tp.dat'.format(sim_name))
tp_helios_val = np.genfromtxt(tp_helios_file, dtype = None, skip_header=1, names = True, usecols = (1,2))
T_helios = tp_helios_val['tempK']
P_helios = tp_helios_val['press106bar']
return T_helios, P_helios
def create_new_vulcan_pt(sim_name, subfolder = ''):
T_h, P_h = read_helios_tp(sim_name)
#T_P_interp = interpolate.interp1d(P_h, T_h, bounds_error = False, fill_value = "extrapolate")
vulcan_atm = np.genfromtxt('/home/s2555875/VULCAN-2/atm/atm_Earth_Jan_Kzz.txt', dtype = None, comments = '#', skip_header = 1, names = True)
P_vulcan = vulcan_atm['Pressure']
Kzz_vulcan = vulcan_atm['Kzz']
P_Kzz = interpolate.interp1d(P_vulcan, Kzz_vulcan, bounds_error = False, fill_value = "extrapolate")
Kzz_h = P_Kzz(P_h)
with open(os.path.join(scratch, 'TP_files', subfolder, '{}.txt'.format(sim_name)), 'w') as f:
f.write('# (dyne/cm2) (K) (cm2/s)\nPressure\tTemp\tKzz\n')
for p,t,z in zip(P_h,T_h,Kzz_h):
f.write('{:.3e}\t{:.1f}\t{:.3e}\n'.format(p,t,z))
#%%
#create_new_vulcan_pt('archean')
#%%
a_list = np.linspace(0.85, 1.35, nsim_dist, endpoint = True) # tested endpoints before running this cell to make sure durface temperature is habitable
def run_many_dist(dist_list):
for i,a in enumerate(dist_list):
sim = ''
if i < 10:
sim = 'sim_0{}_dist'.format(str(i))
else:
sim = 'sim_{}_dist'.format(str(i))
wd = os.getcwd()
os.chdir(helios_folder)
new_p = create_param_file(sim, dist = a)
subprocess.check_call(['python', 'helios.py', '-parameter_file', new_p])#, cwd=helios_folder)
os.chdir(wd)
create_new_vulcan_pt(sim)
#run_many_dist(a_list)
#%%
def run_many_planets(star_table):
name_list = star_table.Name[:]
for i,name in enumerate(name_list):
sim = ''
if i < 10:
sim = 'sim_0{}_star'.format(str(i))
else:
sim = 'sim_{}_star'.format(str(i))
wd = os.getcwd()
os.chdir(helios_folder)
new_p = create_param_file(sim, sname = name)
subprocess.check_call(['python', 'helios.py', '-parameter_file', new_p])#, cwd=helios_folder)
os.chdir(wd)
create_new_vulcan_pt(sim)
#run_many_planets(star_df)
#%%
def run_star_dist(star_table, factor = 1.1):
param_matrix = []
for star,a_min,a_max in zip(star_table.Name, star_table.a_min, star_table.a_max):
#dist = pf.semi_major_list_from_Seff(star_df, star, nsim_dist, factor = factor)
# for a few starts the original method got too cold surface temperatures so decided to test
# and make sure surface temperature are between 0and 100 Celsiusfor all
dist = np.linspace(a_min, a_max, nsim_dist, endpoint = True)
for d in dist:
param_matrix.append([star, d])
for i,sim_i in enumerate(param_matrix):
i_dist = i%nsim_dist
#if i_dist not in [0,14]:
# continue # testing extremities
#if sim_i[0] in ['EARLY_SUN', 'SUN', 'GJ1214', 'GJ674', 'GJ15A', 'HD40307', 'TRAPPIST-1', 'GJ676A', 'HD97658', 'HD149026', 'WASP17','GJ729']:
# continue
#if sim_i[0] in ['HD85512'] and i_dist == 14:
# continue
T = star_table.loc[star_table.Name == sim_i[0]].T_eff.iloc[0]
R = star_table.loc[star_table.Name == sim_i[0]].R.iloc[0]
sim = 'star_{}_'.format(sim_i[0]) # param matrix first goes through the distances
if i_dist < 10:
sim += 'dist_0{}'.format(i_dist)
else:
sim += 'dist_{}'.format(i_dist)
wd = os.getcwd()
os.chdir(helios_folder)
new_p = create_param_file(sim, sname = sim_i[0], dist = sim_i[1], manual = True, T_star = T, r_star = R)
subprocess.check_call(['python', 'helios.py', '-parameter_file', new_p])#, cwd=helios_folder)
os.chdir(wd)
create_new_vulcan_pt(sim, subfolder = 'star_dist')
run_star_dist(star_df)
#%%
# creating fastchem styled mixing ratio file for HELIOS
location_early = '/scratch/s2555875/HELIOS/input/chemistry/early_earth/chem.dat'
location_current = '/scratch/s2555875/HELIOS/input/chemistry/earth/chem.dat'
mixing_file = 'atm/mixing_Pearce_B.txt'
def change_spec_name(sp_name):
characters = []
characters[:] = sp_name
for i in range(len(characters)):
if i < len(characters)-1:
if characters[i+1].isnumeric() == False: # if another letter comes, it means there's only one of that element in the molecule
characters[i] += '1'
elif i == len(characters)-1:
if characters[i].isnumeric() == False: # if another letter comes, it means there's only one of that element in the molecule
characters[i] += '1'
new_name = ''.join(characters)
return new_name
def create_fastchem_like_vmr_from_vulcan(vul_vmr_file, vul_tp_file, filename):
vul_vmr = np.genfromtxt(vul_vmr_file, dtype = None, comments = '#', skip_header = 1, names = True)
vul_tp = np.genfromtxt(vul_tp_file, dtype = None, comments = '#', skip_header = 1, names = True)
vmr_dict = {'P(bar)':vul_vmr['Pressure']/1e6, 'T(K)':vul_tp['Temp']}
# keep ntot, ng and m as dummy now...
vmr_dict['ntot'] = np.ones_like(vmr_dict['P(bar)'])
vmr_dict['ng'] = np.ones_like(vmr_dict['P(bar)'])
vmr_dict['m'] = np.ones_like(vmr_dict['P(bar)'])
new_str = ''
for name in vul_vmr.dtype.names:
if name == 'Pressure':
new_str += '#P(bar)\tT(k)\tn_<tot>(cm-3)\tn_g(cm-3)\tm(u)\t'
else:
new_str += '{}\t'.format(change_spec_name(name))
vmr_dict[name] = vul_vmr[name]
new_str += '\n'
for i in range(len(vmr_dict['P(bar)'])):
for key in vmr_dict.keys():
new_str += '{:.4e}'.format(vmr_dict[key][i]) + '\t'
new_str += '\n'
with open(filename, 'w') as f:
f.write(new_str)
# %%
import pickle
def create_helios_vmr_from_vulcan(vul_file, filename, species):
with open(vul_file, 'rb') as handle:
data = pickle.load(handle)
spec_list = data['variable']['species']
pressure = data['atm']['pco'] # in cgs
new_str = '# (dyne/cm2)\nPressure\t'
vmrs = {'Pressure': pressure}
for sp in species:
vmrs[sp] = data['variable']['ymix'][:, spec_list.index(sp)]
new_str += '{}\t'.format(sp)
new_str += '\n'
for i in range(len(pressure)):
for k in vmrs.keys():
new_str += '{:.3e}\t'.format(vmrs[k][i])
new_str += '\n'
with open(filename, 'w') as f:
f.write(new_str)
# %%