-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathexample.py
More file actions
executable file
·163 lines (154 loc) · 6.34 KB
/
example.py
File metadata and controls
executable file
·163 lines (154 loc) · 6.34 KB
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
#!/usr/bin/env python
import mkipp
import kipp_data
import mesa_data
import matplotlib.pyplot as plt
from matplotlib.patches import PathPatch
import numpy as np
#simple example (saves to default filename Kippenhahn.png)
mkipp.kipp_plot(mkipp.Kipp_Args())
#specify xrange and filename
mkipp.kipp_plot(mkipp.Kipp_Args(save_filename = "Kippenhahn2.png", xlims = [300,600]))
#plot of Helium abundance against time, independent decoration
fig = plt.figure()
axis = plt.gca()
#mkipp.kipp_plot returns an object containing
# kipp_plot.contour_plot : the return value of matplotlibs contourf. Can be
# used to create a colorbar with plt.colorbar()
# kipp_plot.histories : list of history files read. data can be accesed from this
# using the get("column_name") function
# kipp_plot.xlims : limits of data in x coordinate
kipp_plot = mkipp.kipp_plot(mkipp.Kipp_Args(
xaxis = "star_age",
time_units = "Myr",
identifier = "y",
log10_on_data = False,
levels = np.arange(0.0,1.001,0.01),
decorate_plot = False,
save_file = False), axis = axis)
bar = plt.colorbar(kipp_plot.contour_plot,pad=0.05)
bar.set_label("Helium abundance")
axis.set_xlabel("Time (Myr)")
axis.set_ylabel("Mass (solar masses)")
axis.set_xlim(kipp_plot.xlims)
plt.savefig("Kippenhahn3.png")
#read out max age of star first, then create a log(tf-t) plot
fig = plt.figure()
axis = plt.gca()
#only need to read star_age column first
history = mesa_data.mesa_data("LOGS/history.data", read_data_cols = ["star_age"])
max_age = max(history.get("star_age"))
kipp_plot = mkipp.kipp_plot(mkipp.Kipp_Args(
xaxis = "star_age",
time_units = "yr",
function_on_xaxis = lambda x: np.log10(max_age+0.01 - x),
decorate_plot = False,
save_file = False), axis = axis)
bar = plt.colorbar(kipp_plot.contour_plot,pad=0.05)
bar.set_label("log |eps_nuc|")
axis.set_xlabel("log (tf-t) [yrs]")
axis.set_ylabel("Mass (solar masses)")
plt.savefig("Kippenhahn4.png")
#add custom extractor to compute g
fig = plt.figure()
axis = plt.gca()
msun = 1.99e33
rsun = 6.96e10
cgrav = 6.67e-8
def g_extractor(identifier, log10_on_data, prof, return_data_columns = False):
if return_data_columns:
return ["radius", "mass"]
data = cgrav*prof.get("mass")*msun/(prof.get("radius")*rsun)**2
if log10_on_data:
return np.log10(data)
else:
return data
kipp_plot = mkipp.kipp_plot(mkipp.Kipp_Args(
extractor = g_extractor,
decorate_plot = False,
save_file = False), axis = axis)
bar = plt.colorbar(kipp_plot.contour_plot,pad=0.05)
bar.set_label("log g")
axis.set_xlabel("model_number")
axis.set_ylabel("Mass (solar masses)")
plt.savefig("Kippenhahn5.png")
#Reading out mixing regions and data, and plotting independently
kipp_args = mkipp.Kipp_Args()
fig = plt.figure()
axis = plt.gca()
profile_paths = mesa_data.get_profile_paths(["LOGS"])
#if data is distributed among several history.data files, you can provide them
history_paths = ["LOGS/history.data"]
#read profile data
#kipp_data.get_xyz_data returns an object containing
# xyz_data.xlims : limits of data in x coordinate
# xyz_data.X : 2D array of xaxis values of profile data
# xyz_data.Y : 2D array of xaxis values of profile data
# xyz_data.Z : 2D array of xaxis values of profile data
# the last three can be used as inputs for matplotlib contour or contourf
xyz_data = kipp_data.get_xyz_data(profile_paths, kipp_args)
#read mixing regions
#kipp_data.get_mixing_zones returns an object containing
# mixing_zones.zones : matplotlib Path objects for each mixing zone.
# These can be plotted using add_patch(...)
# mixing_zones.mix_types : Integer array containing the type of each zone
# mixing_zones.x_coords : x coordinates for points at the surface
# mixing_zones.y_coords : y coordinates for points at the surface
# mixing_zones.histories : mesa_data history files to access additional data
# the last three can be used as inputs for matplotlib contour or contourf
mixing_zones = kipp_data.get_mixing_zones(history_paths, kipp_args, xlims = xyz_data.xlims)
# just plot convection, overshooting and semiconvection
for i,zone in enumerate(mixing_zones.zones):
color = ""
#Convective mixing
if mixing_zones.mix_types[i] == 1: #convection
color = '#332288'
#Overshooting
elif mixing_zones.mix_types[i] == 3: #overshooting
color = '#117733'
#Semiconvective mixing
elif mixing_zones.mix_types[i] == 4: #semiconvection
color = '#CC6677'
else:
continue
axis.add_patch(PathPatch(zone, color=color, alpha = 0.5, lw = 0))
if (xyz_data.Z.size > 0):
CS = plt.contour(xyz_data.X, xyz_data.Y, xyz_data.Z, [0,4,8], colors='k')
plt.clabel(CS, inline=1, fontsize=10)
axis.plot(mixing_zones.x_coords, mixing_zones.y_coords, 'k', lw=4)
axis.set_xlabel("model_number")
axis.set_ylabel("Mass (solar masses)")
axis.set_xlim(0,max(mixing_zones.x_coords))
axis.set_ylim(0,max(mixing_zones.y_coords))
plt.savefig("Kippenhahn6.png")
# create a multi-panel plot
fig = plt.figure()
spec = fig.add_gridspec(ncols=4, nrows=1, wspace=0.2, width_ratios = [5,5,5,1])
ax1 = fig.add_subplot(spec[0, 0])
ax2 = fig.add_subplot(spec[0, 1])
ax3 = fig.add_subplot(spec[0, 2])
ax4 = fig.add_subplot(spec[0, 3])
kipp_args = mkipp.Kipp_Args(save_file = False, xaxis="star_age", decorate_plot = False)
mkipp.kipp_plot(kipp_args, axis=ax1)
mkipp.kipp_plot(kipp_args, axis=ax2)
kipp_plot = mkipp.kipp_plot(kipp_args, axis=ax3)
bar = fig.colorbar(kipp_plot.contour_plot, cax=ax4)
#bar.ax.set_yticklabels(['0', '','2','','4','','6','','8',''])
bar.set_label("$\\log_{10}|\\epsilon_{\\rm nuc}|$")
history = mesa_data.mesa_data("LOGS/history.data", read_data_cols = ["star_age", "center_h1", "center_he4"])
for i, center_h1 in enumerate(history.get("center_h1")):
if center_h1 < 1e-3:
age_tams = history.get("star_age")[i]/1e6
break
for i, center_he4 in enumerate(history.get("center_he4")):
if center_he4 < 1e-3:
age_hedep = history.get("star_age")[i]/1e6
break
ax1.set_xlim([0,age_tams])
ax2.set_xlim([age_tams,age_hedep])
ax3.set_xlim([age_hedep,history.get("star_age")[-1]/1e6])
ax2.set_xlabel("star age (Myr)")
ax1.set_ylabel("Mass (solar masses)")
ax2.yaxis.set_ticklabels([])
ax3.yaxis.set_ticklabels([])
plt.savefig("Kippenhahn7.png")