forked from IhmeGroup/pyIDT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_idt.py
104 lines (83 loc) · 2.75 KB
/
test_idt.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
from options import options
from eval_idt import eval_idt
from mesh_generate_box import mesh_generate_box
from support import check_override
import global_var
import multiprocessing
import itertools
import cantera as ct
import numpy as np
import os
import sys
import yaml
# Refer to pyCantera tutorials for multiprocessing example
# for further details regarding parallelizing. Note that
# only one machine can be used using multiprocessing
# Global storage for Cantera Solution objects
global_var.init()
def init_process(mech):
global_var.gases[mech] = ct.Solution(mech)
def main():
# Read config file from command line
config_file = sys.argv[1]
yaml_file = open(config_file)
config = yaml.safe_load(yaml_file)
# Set options structure
opt = options()
opt.palette = config["test_palette"]
opt.test_comp = config["test_composition"]
opt.outer_center = config["outer_center"]
opt.outer_intervals = config["outer_intervals"]
opt.cti_file = config["cti_file"]
opt.mixture = config["mixture_name"]
opt.nx = config["palette_resolution"]
opt.pres = config["pressure"]
opt.temp = config["temperature"]
opt.phi = config["equivalence_ratio"]
opt.t_fin = config["final_time"]
opt.target_mw = config["target_mw"]
opt.target_hc = config["target_hc"]
opt.output_file = config["output_file"]
opt.override_targets = config["override_targets"]
opt.write_output = config["write_output"]
# Close YAML file
yaml_file.close()
# Set gas
gas = ct.Solution(opt.cti_file)
# Check override
check_override(gas, opt)
# Dump options
opt.dump()
# Generate mesh
mesh_data = mesh_generate_box(gas,opt)
print "Mesh generation complete!"
print "Number of points = ", (np.array(mesh_data)).shape[0]
# Estimate number of processes
nProcs = multiprocessing.cpu_count()
print "Number of cores = ", nProcs
# Create gas objects for all processes
pool = multiprocessing.Pool(processes=nProcs,
initializer=init_process,
initargs=(opt.cti_file,))
try:
os.remove(opt.output_file)
except OSError:
pass
f = open(opt.output_file, 'a')
res = pool.map(eval_idt,
zip(itertools.repeat(opt.cti_file),
itertools.repeat(opt),
mesh_data))
# End parallelism
pool.close()
pool.join()
print "Computation finished!"
dump_mat = []
if (opt.write_output):
for idx,mesh_point in enumerate(mesh_data):
dump_point = np.hstack((mesh_point, res[idx]))
dump_mat.append(dump_point)
np.savetxt(f,dump_mat,fmt='%.3e')
f.close()
if __name__ == '__main__':
main()