-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenergy_profile.py
executable file
·211 lines (185 loc) · 4.73 KB
/
energy_profile.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
#!/usr/bin/env python
import sys
import os
import argparse
import numpy as np
import matplotlib.pyplot as plt
from cycler import cycler
import pandas as pd
import glob
from loadmodules import *
import gadget_snap
from const import rsol, msol
import loaders
def energy_profile(
energyfile,
energy=None,
save=None,
filetype="png",
dpi=600,
maxtime=None,
mintime=None,
scale="linear",
):
if maxtime is not None and mintime is not None:
assert maxtime > mintime, "maxtime is less than mintime"
fig, ax = plt.subplots(1, 1, figsize=[6.4, 4.8])
time = energyfile.time
ein = energyfile.ein
ekin = energyfile.ekin
epot = energyfile.epot
etot = energyfile.etot
if maxtime is None:
maxtime = max(time)
if mintime is None:
mintime = min(time)
mask = np.logical_and(time >= mintime, time <= maxtime)
time = time[mask]
if scale == "log":
ax.semilogy(
time,
ein[mask],
color="tab:blue",
label=r"E$_\mathrm{in}$",
)
ax.semilogy(
time,
ekin[mask],
color="tab:orange",
label=r"E$_\mathrm{kin}$",
)
ax.semilogy(
time,
epot[mask],
color="tab:green",
label=r"E$_\mathrm{pot}$",
)
ax.semilogy(
time,
etot[mask],
color="tab:red",
label=r"E$_\mathrm{tot}$",
)
elif scale == "linear":
ax.plot(
time,
ein[mask],
color="tab:blue",
label=r"E$_\mathrm{in}$",
)
ax.plot(
time,
ekin[mask],
color="tab:orange",
label=r"E$_\mathrm{kin}$",
)
ax.plot(
time,
epot[mask],
color="tab:green",
label=r"E$_\mathrm{pot}$",
)
ax.plot(
time,
etot[mask],
color="tab:red",
label=r"E$_\mathrm{tot}$",
)
else:
raise ValueError("Invalid scale")
ax.axhline(
y=0,
ls="-.",
color="tab:gray",
)
if energy is not None:
ax.set_title("%.2e erg added externally" % energy)
ax.set_xlabel("Time (s)")
ax.set_ylabel("Energy (erg)")
ax.grid()
fig.tight_layout()
handles, labels = ax.get_legend_handles_labels()
lgd = ax.legend(handles, labels, loc="upper left", bbox_to_anchor=(1.05, 1.05))
if not os.path.exists(save):
print("Creating save directory...")
os.mkdir(save)
savefile = os.path.join(save, "energy_evolution.%s" % filetype)
saved = False
tryed = 0
while not saved:
if os.path.exists(savefile):
tryed += 1
savefile = os.path.join(
save,
"energy_evolution-(%d).%s" % (tryed, filetype),
)
else:
fig.savefig(
savefile,
bbox_inches="tight",
bbox_extra_artists=(lgd,),
dpi=dpi,
)
saved = True
plt.close()
return
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"snappath",
help="Path to directory containing energy.txt file. Default: output",
default="output",
)
parser.add_argument(
"-e",
"--energy",
help="Energy added externally (e.g. during creation of initial conditions)",
type=float,
)
parser.add_argument(
"-s",
"--save",
help="Path to directory where plots are saved to. Default: plots",
default="plots",
)
parser.add_argument(
"-t",
"--filetype",
help="Fileformat of saved figure. Default: png",
default="png",
)
parser.add_argument(
"-d",
"--dpi",
help="DPI of saved figure. Default: 600",
type=int,
default=600,
)
parser.add_argument(
"--maxtime",
help="Upper timelimit for composition plot in s.",
type=float,
)
parser.add_argument(
"--mintime",
help="Lower timelimit for composition plot in s.",
type=float,
)
parser.add_argument(
"--scale",
help="Scale of plot. Either linear or log. Default: linear",
default="linear",
choices=["linear", "log"],
)
args = parser.parse_args()
energyfile = gadget.gadget_energyfile(snappath=args.snappath)
energy_profile(
energyfile,
energy=args.energy,
save=args.save,
filetype=args.filetype,
dpi=args.dpi,
maxtime=args.maxtime,
mintime=args.mintime,
scale=args.scale,
)