-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_energies.py
129 lines (94 loc) · 3.86 KB
/
plot_energies.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
# Script to properly order energy files and plot a given thermodynamic output
import subprocess
from glob import glob
from natsort import natsorted
import gromacs as gro
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
import argparse
def run(commands):
'''Run commands with subprocess'''
if not isinstance(commands, list):
commands = [commands]
for cmd in commands:
subprocess.run(cmd, shell=True)
if __name__ == '__main__':
# parse arguments
parser = argparse.ArgumentParser()
parser.add_argument('-e', '--energy', nargs='+', help='Quantity from the energy file to plot (numerical selection)')
parser.add_argument('-nvt', '--nvt', action='store_true', help='Whether this quantity is calculated during NVT')
parser.add_argument('-npt', '--npt', action='store_true', help='Whether this quantity is calculated during NPT')
parser.add_argument('-o', '--output', default='energies.png', help='Name for the output figure')
parser.add_argument('-d', '--dir', help='Directory with edr energy files')
args = parser.parse_args()
# collect all edr files
nvt_edr = glob(args.dir + '/nvt*.edr')
npt_edr = glob(args.dir + '/npt*.edr')
nvt_edr = natsorted(nvt_edr)
npt_edr = natsorted(npt_edr)
if len(nvt_edr) != len(npt_edr):
nvt_edr = nvt_edr[:-1]
# loop through and generate xvg files
energies = {}
for e in args.energy:
energies[e] = []
if args.nvt and args.npt:
for nvt, npt in tqdm(zip(nvt_edr, npt_edr)):
# add all data for NVT
cmd = 'echo '
for e in energies:
cmd += f'{e} '
cmd += f'| gmx energy -f {nvt} -o tmp.xvg'
run(cmd)
xvg = gro.fileformats.XVG('tmp.xvg')
for i,e in enumerate(energies):
[energies[e].append(a) for a in xvg.array[i+1,:]]
run('rm tmp.xvg')
# add all data for NPT
cmd = 'echo '
for e in energies:
cmd += f'{e} '
cmd += f'| gmx energy -f {npt} -o tmp.xvg'
run(cmd)
xvg = gro.fileformats.XVG('tmp.xvg')
for i,e in enumerate(energies):
[energies[e].append(a) for a in xvg.array[i+1,:]]
run('rm tmp.xvg')
elif args.nvt:
for nvt in nvt_edr:
# add all data for NVT
cmd = 'echo '
for e in energies:
cmd += f'{e} '
cmd += f'| gmx energy -f {nvt} -o tmp.xvg'
run(cmd)
xvg = gro.fileformats.XVG('tmp.xvg')
for i,e in enumerate(energies):
[energies[e].append(a) for a in xvg.array[i+1,:]]
run('rm tmp.xvg')
elif args.npt:
for npt in npt_edr:
# add all data for NPT
cmd = 'echo '
for e in energies:
cmd += f'{e} '
cmd += f'| gmx energy -f {npt} -o tmp.xvg'
run(cmd)
xvg = gro.fileformats.XVG('tmp.xvg')
for i,e in enumerate(energies):
[energies[e].append(a) for a in xvg.array[i+1,:]]
run('rm tmp.xvg')
else:
raise TypeError('Please specify whether to use NVT, NPT, or all simulations with the -nvt, -npt arguments')
# plot full time series
time = np.arange(0, len(energies[args.energy[0]]))/1000*2
fig, ax = plt.subplots(1,1, figsize=(6,6))
data = time.copy()
for e in energies:
data = np.vstack((data, np.array(energies[e])))
plt.plot(time, energies[e])
np.savetxt(args.output.split('.png')[0] + '.csv', data, delimiter=',', fmt='%.8f')
plt.xlabel('time (ns)')
plt.savefig(args.output)
plt.show()