-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPHOENIXscripts.py
440 lines (406 loc) · 15.9 KB
/
PHOENIXscripts.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
from __future__ import print_function
import sys
import os
import re
import numpy as np
import subprocess
from matplotlib import pyplot as plt
class PHOENIX:
def __init__(self, saveloc, inputfile = None, omegafile = None, eigenfile = None):
## Initialisation of class attributes (just a handy place to define file locations for use later)
## Must define the directory to save plots to
self.saveloc = str(saveloc)
## Other file locations assume working in the base directory for PHOENIX, but can also define if needed)
self.inputfile = os.path.join(os.getcwd(),'INPUT/phoenix.inp')
if inputfile is not None:
self.inputfile = inputfile
self.omegafile = os.path.join(os.getcwd(),'OUTPUT/omega_csp')
if omegafile is not None:
self.omegafile = omegafile
self.eigenfile = os.path.join(os.getcwd(),'OUTPUT/eigenvector.dat')
if eigenfile is not None:
self.eigenfile = eigenfile
def run(self):
subprocess.call(["csphoenix"])
subprocess.call(["phoenix"])
def plot_freq(self, omegamin=None, omegamax=None, gammamin=None, gammamax=None, overplot=False, show=False):
## Plots frequency continuum with target frequency location
# Plot the csphoneix continuum
# 30/07/2018 by Zhisong Qu
#
# Inputs:
# csp_file : the csp spectrum file name
# omega_min : the min omega to plot (default=-2.0)
# omega_max : max (default= 2.0)
# overplot : True for component of overplot function (default=False)
# show : display plot if True, save if False (default= False)
## Define these y limits for the plot if needed
# Number of variables (don't change)
n_variable = 8
# Number of output frequencies (don't change)
n_multiplier = n_variable * 8
filename = self.omegafile
if omegamin is None:
omega_min = -2.0
else:
omega_min = omegamin
if omegamax is None:
omega_max = 2.0
else:
omega_max = omegamax
with open(filename, 'r') as f:
line = f.readline()
[m, nr] = map(int, line.split())
print('M = ', m)
print('NR = ', nr)
n_output = m * n_multiplier * nr
r = np.zeros(n_output)
q = np.zeros(n_output)
gamma = np.zeros(n_output)
omega = np.zeros(n_output)
i = 0
for line in f:
[rf, qf, omegaf, gammaf] = map(float, line.split())
#print(rf, qf, gammaf, omegaf)
r[i] = rf
q[i] = qf
gamma[i] = gammaf
omega[i] = omegaf
i = i + 1
## Dont want separate plots if it is overplotting
if overplot is False:
fig, ax = plt.subplots()
plt.scatter(r, omega, s=1, marker='x')
with open(self.inputfile, 'r') as inpfile:
filecontents = inpfile.read()
target = re.findall('\([^A]+\)',filecontents)[0]
val, power = (target[1:-1].split(',')[1]).split('D')
val, power = float(str(val).strip()), float(str(power).strip())
freq = val*pow(10.0,power)
plt.plot([np.min(r),np.max(r)],[-freq,-freq],"r--")
plt.text(0.1, 0.05, '-- Target', ha='center', va='center', transform=ax.transAxes, fontsize=8, color = 'r')
plt.xlim([np.min(r),np.max(r)])
plt.xlabel('s')
plt.ylim([omega_min,omega_max])
plt.ylabel('$\omega / \omega_{A0}$')
plt.title('Continuous Spectrum Frequency')
## Show if show is true, save if show is False and overplot is False
if show is True:
plt.show()
elif overplot is False:
plt.savefig(self.saveloc+"FREQCONTINUUM.png")
def plot_freq_notarget(self,omegamin=None, omegamax=None, gammamin=None, gammamax=None, overplot=False, show=False, name = None):
## Plots frequency continuum without target frequency location
# Plot the csphoneix continuum
# 30/07/2018 by Zhisong Qu
#
# Inputs:
# csp_file : the csp spectrum file name
# omega_min : the min omega to plot (default=-2.0)
# omega_max : max (default= 2.0)
# overplot : True for component of overplot function (default=False)
# show : display plot if True, save if False (default= False)
# Number of variables (don't change)
n_variable = 8
# Number of output frequencies (don't change)
n_multiplier = n_variable * 8
filename = self.omegafile
if omegamin is None:
omega_min = -2.0
else:
omega_min = omegamin
if omegamax is None:
omega_max = 2.0
else:
omega_max = omegamax
with open(filename, 'r') as f:
line = f.readline()
[m, nr] = map(int, line.split())
print('M = ', m)
print('NR = ', nr)
n_output = m * n_multiplier * nr
r = np.zeros(n_output)
q = np.zeros(n_output)
gamma = np.zeros(n_output)
omega = np.zeros(n_output)
i = 0
for line in f:
[rf, qf, omegaf, gammaf] = map(float, line.split())
#print(rf, qf, gammaf, omegaf)
r[i] = rf
q[i] = qf
gamma[i] = gammaf
omega[i] = omegaf
i = i + 1
if overplot is False:
fig, ax = plt.subplots()
plt.scatter(r, omega, s=1, marker='x')
elif overplot is True:
plt.scatter(r, omega, s=1, marker='x', label=name)
plt.xlim([np.min(r),np.max(r)])
plt.xlabel('s')
plt.ylim([omega_min,omega_max])
plt.ylabel('$\omega / \omega_{A0}$')
plt.title('Continuous Spectrum Frequency')
if show is True:
plt.show()
elif overplot is False:
plt.savefig(self.saveloc+"FREQCONTINUUM_notarget.png")
def plot_gr(self,omegamin=None, omegamax=None, gammamin=None, gammamax=None, show=False):
# Plot the csphoneix continuum
# 30/07/2018 by Zhisong Qu
#
# Inputs:
# csp_file : the csp spectrum file name
# gamma_min : the min gamma to plot (default=-0.1)
# gamma_max : max (default= 0.1)
# show : display plot if True, save if False (default= False)
# Number of variables (don't change)
n_variable = 8
# Number of output frequencies (don't change)
n_multiplier = n_variable * 8
filename = self.omegafile
if gammamin is None:
gamma_min = -0.1
else:
gamma_min = gammamin
if gammamax is None:
gamma_max = 0.1
else:
gamma_max = gammamax
with open(filename, 'r') as f:
line = f.readline()
[m, nr] = map(int, line.split())
print('M = ', m)
print('NR = ', nr)
n_output = m * n_multiplier * nr
r = np.zeros(n_output)
q = np.zeros(n_output)
gamma = np.zeros(n_output)
omega = np.zeros(n_output)
i = 0
for line in f:
[rf, qf, omegaf, gammaf] = map(float, line.split())
#print(rf, qf, gammaf, omegaf)
r[i] = rf
q[i] = qf
gamma[i] = gammaf
omega[i] = omegaf
i = i + 1
fig, ax = plt.subplots()
plt.scatter(r, gamma, s=1, marker='x')
plt.xlim([np.min(r),np.max(r)])
plt.xlabel('s')
plt.ylim([gamma_min,gamma_max])
plt.ylabel('$\gamma / \omega_{A0}$')
plt.title('Continuous Spectrum Growth Rate')
if show is True:
plt.show()
else:
plt.savefig(self.saveloc+"GRCONTINUUM.png")
def plot_eigen(self, value='v1', eigenvalue=None, show=False):
# Plot phoenix eigenvector components (real)
# 30/10/2018 by Emlyn Graham
#
# Inputs:
# value : chosen component to plot (choose from ['p','v1','v2','v3','T','A1','A2','A3']
# , default 'v1')
# eigenvalue : eigenvalue number (choose from 0-14, default=0)
# show : display plot if True, save if False (default= False)
if value=='v1':
dim = 'v1'
else:
dim = str(value)
if eigenvalue is None:
EV=0
else:
EV=eigenvalue
filename = self.eigenfile
with open(filename, 'rt') as f:
dim_list = ['p', 'v1', 'v2', 'v3', 'T', 'A1', 'A2', 'A3']
dataset = [[str(entry).strip() for entry in line.split()] for line in f.readlines()]
[NEV, NR, MANZ, MDIF] = [int(str(entry).strip()) for entry in dataset[0]]
dataset.pop(0);
NEV_list = [dataset[i:i+NR] for i in range(0, len(dataset), NR)]
NEV_list = [[[NRx[i:i+16] for i in range(0, len(NRx), 16)] for NRx in N] for N in NEV_list]
NEV_list = [[[[entry[i:i+2] for i in range(0, len(entry), 2)] for entry in NRx] for NRx in N] for N in NEV_list]
NEV_list = [[[[[float(x) for x in re_im] for re_im in dims] for dims in M] for M in N] for N in NEV_list]
# Now we have a dataset of the form [re,im] for each [p,v1,v2,...] for each [1,...,MANZ] for each [1,...,NR] for each [1,...,NEV]
# As a nested list
var = [[NEV_list[EV][i][M][dim_list.index(dim)][0] for i in range(0, len(NEV_list[EV]))] for M in range(0,MANZ,1)]
fig, ax = plt.subplots()
inputfile = os.path.join(os.getcwd(),'INPUT/phoenix.inp')
with open(inputfile, 'r') as inpfile:
filecontents = inpfile.read()
rfour = re.findall("(?<==)[^A]+(?=NTOR)", filecontents)[0]
rfourval, rfourpower = re.findall("[^DE]+",rfour)
rfourval, rfourpower = float(str(rfourval).strip()), float(str(rfourpower).strip())
startingharmonic = int(rfourval*10**rfourpower)
nvalue = re.findall("(?<==)[^ADE]+(?=MANZ)", filecontents)[0]
nval = float(str(nvalue).strip())
if nval >= 0:
nvalue = 1
elif nval < 0:
nvalue = -1
innerlim = re.findall("(?<==)[^A]+(?=OUTER_WALL)", filecontents)[0]
innerlimval, innerlimpower = re.findall("[^DE]+",innerlim)
innerlimval, innerlimpower = float(str(innerlimval).strip()), float(str(innerlimpower).strip())
inner = innerlimval*10**innerlimpower
outerlim = re.findall("(?<==)[^A]+(?=MESH_ACCUMULATION)", filecontents)[0]
outerlimval, outerlimpower = re.findall("[^DE]+",outerlim)
outerlimval, outerlimpower = float(str(outerlimval).strip()), float(str(outerlimpower).strip())
outer = outerlimval*10**outerlimpower
for N in range(0,len(var)):
plt.plot(np.linspace(inner, outer, num=len(var[N])), var[N], label='M='+str(N+startingharmonic-np.floor((len(var)-1)/2)), marker=".", markersize=3, ls="")
plt.xlabel('S')
plt.ylabel('{}'.format(dim))
plt.title('Re {} - EV {}'.format(dim, EV))
plt.legend(loc='best')
plt.autoscale(enable=True, axis="y", tight = False)
if show is True:
plt.show()
else:
plt.savefig(self.saveloc+dim+"_eig"+str(EV)+".png")
def plot_eigen_im(self, value='v1', eigenvalue=None, show=False):
# Plot phoenix eigenvector components (im)
# 30/10/2018 by Emlyn Graham
#
# Inputs:
# value : chosen component to plot (choose from ['p','v1','v2','v3','T','A1','A2','A3']
# , default 'v1')
# eigenvalue : eigenvalue number (choose from 0-14, default=0)
# show : display plot if True, save if False (default= False)
if value=='v1':
dim = 'v1'
else:
dim = str(value)
if eigenvalue is None:
EV=0
else:
EV=eigenvalue
filename = self.eigenfile
with open(filename, 'rt') as f:
dim_list = ['p', 'v1', 'v2', 'v3', 'T', 'A1', 'A2', 'A3']
dataset = [[str(entry).strip() for entry in line.split()] for line in f.readlines()]
[NEV, NR, MANZ, MDIF] = [int(str(entry).strip()) for entry in dataset[0]]
dataset.pop(0);
NEV_list = [dataset[i:i+NR] for i in range(0, len(dataset), NR)]
NEV_list = [[[NRx[i:i+16] for i in range(0, len(NRx), 16)] for NRx in N] for N in NEV_list]
NEV_list = [[[[entry[i:i+2] for i in range(0, len(entry), 2)] for entry in NRx] for NRx in N] for N in NEV_list]
NEV_list = [[[[[float(x) for x in re_im] for re_im in dims] for dims in M] for M in N] for N in NEV_list]
# Now we have a dataset of the form [re,im] for each [p,v1,v2,...] for each [1,...,MANZ] for each [1,...,NR] for each [1,...,NEV]
# As a nested list
var = [[NEV_list[EV][i][M][dim_list.index(dim)][1] for i in range(0, len(NEV_list[EV]))] for M in range(0,MANZ,1)]
fig, ax = plt.subplots()
inputfile = os.path.join(os.getcwd(),'INPUT/phoenix.inp')
with open(inputfile, 'r') as inpfile:
filecontents = inpfile.read()
rfour = re.findall("(?<==)[^A]+(?=NTOR)", filecontents)[0]
rfourval, rfourpower = re.findall("[^DE]+",rfour)
rfourval, rfourpower = float(str(rfourval).strip()), float(str(rfourpower).strip())
startingharmonic = int(rfourval*10**rfourpower)
nvalue = re.findall("(?<==)[^ADE]+(?=MANZ)", filecontents)[0]
nval = float(str(nvalue).strip())
if nval >= 0:
nvalue = 1
elif nval < 0:
nvalue = -1
innerlim = re.findall("(?<==)[^A]+(?=OUTER_WALL)", filecontents)[0]
innerlimval, innerlimpower = re.findall("[^DE]+",innerlim)
innerlimval, innerlimpower = float(str(innerlimval).strip()), float(str(innerlimpower).strip())
inner = innerlimval*10**innerlimpower
outerlim = re.findall("(?<==)[^A]+(?=MESH_ACCUMULATION)", filecontents)[0]
outerlimval, outerlimpower = re.findall("[^DE]+",outerlim)
outerlimval, outerlimpower = float(str(outerlimval).strip()), float(str(outerlimpower).strip())
outer = outerlimval*10**outerlimpower
for N in range(0,len(var)):
plt.plot(np.linspace(inner, outer, num=len(var[N])), var[N], label='M='+str(N+startingharmonic-np.floor((len(var)-1)/2)), marker=".", markersize=3, ls="")
plt.xlabel('S')
plt.ylabel('{}'.format(dim))
plt.title('Im {} - EV {}'.format(dim, EV))
plt.legend(loc='best')
plt.autoscale(enable=True, axis="y", tight = False)
if show is True:
plt.show()
else:
plt.savefig(self.saveloc+dim+"_eig"+str(EV)+"_im.png")
def overplot(self, equilibriumlist, plotname=None, show=False):
# Plot phoenix eigenvector components (im)
# 30/10/2018 by Emlyn Graham
#
# Inputs:
# equilibriumlist : list of equilibrium mapping files for PHOENIX
# plotname : name of plot if saving (needs to be defined if saving)
# show : display plot if True, save if False (default = False)
#
# Equilibrium mapping files need to be located in 'INPUT/' in PHOENIX directory
print("Initialising")
fig, ax = plt.subplots()
## Taking turns to plot each
for filename in equilibriumlist:
## Rename the equilibrium file before running CSPHOENIX
os.rename('INPUT/'+filename, 'INPUT/equilibrium.map')
subprocess.call(["csphoenix"])
os.rename('INPUT/equilibrium.map', 'INPUT/'+filename)
## Add plot to the main plot
self.plot_freq_notarget(overplot=True, name=filename)
print("Done overplot!")
plt.legend(loc='upper right')
if plotname is not None:
plt.savefig(self.saveloc+str(plotname)+".png")
elif show is True:
plt.show()
def scan(self, grlist, freqlist):
print("Initialising")
## Looping through frequency and growth rate options to plot results
savelocORIGINAL = self.saveloc
subprocess.call(["csphoenix"])
self.plot_freq_notarget()
self.plot_gr()
for freq1 in freqlist:
freq = round(freq1, 3)
for gr1 in grlist:
gr = round(gr1, 3)
self.saveloc = savelocORIGINAL +"GR"+str(round(gr,3))+"FREQ"+str(round(freq,3))
with open(self.inputfile, 'r+') as inpfile:
print("Reading old input file")
## Reads input file and changes target value
## PHOENIX changes the sign of the frequency input for some reason
## so this is altered and files named correctly
filecontents = inpfile.read()
##target = re.findall('\([^A]+\)',filecontents)[0]
newtarget = "("+str(gr)+"D+0, "+str(-freq)+"D+0)"
inpfile.seek(0)
inpfile.write(re.sub('\([^A]+\)', newtarget, filecontents, 1))
print("Writing new input file")
inpfile.truncate()
inpfile.close()
## Call csphoenix and phoenix to produce outputs
subprocess.call(["phoenix"])
print("Continuum plots done")
plt.close("all")
dim_list = ['p', 'v1', 'v2', 'v3', 'T', 'A1', 'A2', 'A3']
for dim in dim_list:
evnum = 1
EVs = os.path.join(os.getcwd(),'OUTPUT/eigenvalues.dat')
with open(EVs, 'r') as evdata:
for i, l in enumerate(evdata):
pass
evnum = i+1
evdata.close()
for EV in list(range(evnum)):
try:
self.plot_eigen(value=dim,eigenvalue=EV)
plt.close("all")
except IndexError:
pass
try:
self.plot_eigen_im(value=dim,eigenvalue=EV)
plt.close("all")
except IndexError:
pass
plt.close("all")
print("Eigenvector plots done")
self.saveloc = savelocORIGINAL
print("DONE!")
#############################################################################