forked from SLACKHA/pyJac-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
npy_convert.py
58 lines (51 loc) · 2.25 KB
/
npy_convert.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
"""A simple utility script that converts saved numpy files
from a Temperature, Pressure, Mass Fraction format to
Temperature, Pressure, Concentrations"""
import numpy as np
import cantera as ct
import os
import argparse
def main(input_dir='', output_dir='', mech=''):
assert input_dir != output_dir, 'Cannot convert in same folder'
gas = ct.Solution(mech)
npy_files = [f for f in os.listdir(input_dir)]
npy_files = [f for f in npy_files if f.endswith('.npy')
and os.path.isfile(os.path.join(input_dir, f))]
for npy in sorted(npy_files):
state_data = np.load(os.path.join(input_dir, npy))
state_data = state_data.reshape(state_data.shape[0] *
state_data.shape[1],
state_data.shape[2]
)
out_data = np.zeros((state_data.shape[0], gas.n_species + 2))
for i in range(state_data.shape[0]):
# convert to T, P, C
gas.TPY = state_data[i, 1], state_data[i, 2], state_data[i, 3:]
out_data[i, 0] = gas.T
out_data[i, 1] = gas.P
out_data[i, 2:] = gas.concentrations[:]
np.save(os.path.join(output_dir,
npy), out_data)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=(
'A simple utility script that converts saved numpy files'
'from a Temperature, Pressure, Mass Fraction format to'
'Temperature, Pressure, Concentrations'))
parser.add_argument('-i', '--input_dir',
type=str,
required=True,
help='The directory to scan for .npy files.'
)
parser.add_argument('-o', '--output_dir',
type=str,
required=True,
help='The directory to place the converted .npy files.'
)
parser.add_argument('-m', '--mech',
type=str,
required=True,
help='The Cantera format mechanism to use.')
args = parser.parse_args()
main(input_dir=args.input_dir,
output_dir=args.output_dir,
mech=args.mech)