-
Notifications
You must be signed in to change notification settings - Fork 1
/
sens_main.py
276 lines (189 loc) · 12.5 KB
/
sens_main.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
import os, sys, csv
from random import randint
import itertools
import thermo_get_properties as thermo_reader
import sampling as sam
###################################
# @author Shimaa_Gamil
# KAUST-CCRC
###################################
# STEPS:
# 1. Read Thermo file
# 2. Caclulate thermo preperties of thermo file (Hf, S, CPs)
# 3. Define the Sampling Method (# of samples, normal distribution)
# 4. Do Sampling
# 5. Generate many files with differnt properties based on sampling values
# 6. Generate Thermo files of the above different thermo properties files
###
# a function to call the module that calculates thermo properties from therm file
###
def readThermoFile (input_file):
path = os.path.dirname(input_file)
file_name_only = os.path.basename(input_file).split('.')[0]
output_file = path + '\\' + file_name_only + '_props.csv'
thermo_reader.readThermoFile (input_file, output_file)
return output_file
###
# a function to calculate thermo data for the passed properties file,
# then call a function to rewrite the source thermo file and update thermo data of the butanol species
###
def writeNewThermoFile (butanol_species_list, props_file, source_thermo_file, output_thermo_file):
butanol_species_props_list = []
with open(props_file, newline='') as props_f:
reader = csv.reader(props_f)
for row in reader:
species = row [0]
if species in butanol_species_list:
butanol_species_props_list.append ( row )
thermo_reader.reWriteThermoFile (butanol_species_list, butanol_species_props_list, source_thermo_file, output_thermo_file)
###
# a function to calculate thermo data for the passed properties file,
# then call a function to rewrite the source thermo file and update thermo data of the butanol species
###
def getbutanolSpeciesThermo (butanol_species_list, props_file, output_file):
butanol_species_props_list = []
butanol_props_file = open(output_file, "wt", newline='')
writer = csv.writer(butanol_props_file)
with open(props_file, newline='') as props_f:
reader = csv.reader(props_f)
for row in reader:
species = row [0]
if species in butanol_species_list:
writer.writerow (row)
butanol_props_file.close ()
###
# main code that runs first
###
# You should replace these file paths with your therm file, and the dic file that
# include your target species for doing sensitivity
thermo_file_path = 'C:\\Shimaa_Work\\Thermo_UQ\\PECS_therm.therm'
species_type_file_path = 'C:\\Shimaa_Work\\Thermo_UQ\\butanol_species_dict.csv'
# Read thermo file and calculate properties
properties_file = readThermoFile (thermo_file_path)
if not os.path.exists(properties_file): # handle the case if the props file is not created for any reason
print ("An error has occured while calculating Thermo properties")
sys.exit(0)
# Read the important species to do UQ on their parameters
species_type_dict = {}
butanol_species_list = []
with open(species_type_file_path, newline='') as type_f:
reader = csv.reader(type_f)
for row in reader:
butanol_species_list.append (row[0])
species_type = row[1]
if 'alcohol like' in species_type:
species_type_dict[ row[0] ] = sam.ALCOHOL_LIKE_TYPE
elif 'hydroxy alkyl radical' in species_type:
species_type_dict[ row[0] ] = sam.HYDROXY_ALKYL_RADICAL_TYPE
elif 'peroxy radical' in species_type:
species_type_dict[ row[0] ] = sam.PEROXY_RADICAL_TYPE
elif 'hydroperoxide' in species_type:
species_type_dict[ row[0] ] = sam.HYDROPEROXIDE_TYPE
elif 'alkoxy radical like propoxy' in species_type:
species_type_dict[ row[0] ] = sam.ALKOXY_RADICAL_LIKE_PROPOXY_TYPE
butanol_species_flag_list = [0] * len (butanol_species_list)
#print (species_type_dict)
#print (butanol_species_list)
output_file = os.path.dirname(properties_file) + '\\butanol_species_thermo_props.csv'
getbutanolSpeciesThermo (butanol_species_list, properties_file, output_file)
# create a directory with a unique key to write output files
unique_key = randint(100, 100000)
path = os.path.dirname(properties_file)
directory = path + '\\' + 'sampling_output_' + str(unique_key)
if not os.path.exists(directory):
os.makedirs(directory)
Hf_directory = directory + '\\Hf'
S_directory = directory + '\\S'
if not os.path.exists(Hf_directory):
os.makedirs(Hf_directory)
if not os.path.exists(S_directory):
os.makedirs(S_directory)
for species_name in butanol_species_list:
with open(properties_file, newline='') as props_f:
reader = csv.reader(props_f)
file_name_only = os.path.basename(properties_file).split('.')[0]
Hf_output_props_maxSD_file = Hf_directory + '\\' + file_name_only + '_' + species_name + '_Hf_maxSD' + '.csv'
Hf_output_props_minSD_file = Hf_directory + '\\' + file_name_only + '_' + species_name + '_Hf_minSD' + '.csv'
Hf_output_thermo_maxSD_file = Hf_directory + '\\thermo_' + species_name + '_Hf_maxSD' + '.dat'
Hf_output_thermo_minSD_file = Hf_directory + '\\thermo_' + species_name + '_Hf_minSD' + '.dat'
S_output_props_maxSD_file = S_directory + '\\' + file_name_only + '_' + species_name + '_S_maxSD' + '.csv'
S_output_props_minSD_file = S_directory + '\\' + file_name_only + '_' + species_name + '_S_minSD' + '.csv'
S_output_thermo_maxSD_file = S_directory + '\\thermo_' + species_name + '_S_maxSD' + '.dat'
S_output_thermo_minSD_file = S_directory + '\\thermo_' + species_name + '_S_minSD' + '.dat'
Hf_output_max = open(Hf_output_props_maxSD_file, "wt", newline='')
Hf_writer_max = csv.writer(Hf_output_max)
Hf_output_min = open(Hf_output_props_minSD_file, "wt", newline='')
Hf_writer_min = csv.writer(Hf_output_min)
S_output_max = open(S_output_props_maxSD_file, "wt", newline='')
S_writer_max = csv.writer(S_output_max)
S_output_min = open(S_output_props_minSD_file, "wt", newline='')
S_writer_min = csv.writer(S_output_min)
for row in reader:
species_name_file = row [0]
if species_name == species_name_file:
Hf = row [1]
S = row [2]
newHf_max = Hf
newS_max = S
newHf_min = Hf
newS_min = S
species_type = species_type_dict [species_name]
if species_type == sam.ALCOHOL_LIKE_TYPE:
newHf_max = float(Hf) + sam.ALCOHOL_LIKE_HF_SD
newS_max = float(S) + sam.ALCOHOL_LIKE_S_SD
newHf_min = float(Hf) - sam.ALCOHOL_LIKE_HF_SD
newS_min = float(S) - sam.ALCOHOL_LIKE_S_SD
elif species_type == sam.HYDROXY_ALKYL_RADICAL_TYPE:
newHf_max = float(Hf) + sam.HYDROXY_ALKYL_RADICAL_HF_SD
newS_max = float(S) + sam.HYDROXY_ALKYL_RADICAL_S_SD
newHf_min = float(Hf) - sam.HYDROXY_ALKYL_RADICAL_HF_SD
newS_min = float(S) - sam.HYDROXY_ALKYL_RADICAL_S_SD
elif species_type == sam.PEROXY_RADICAL_TYPE:
newHf_max = float(Hf) + sam.PEROXY_RADICAL_HF_SD
newS_max = float(S) + sam.PEROXY_RADICAL_S_SD
newHf_min = float(Hf) - sam.PEROXY_RADICAL_HF_SD
newS_min = float(S) - sam.PEROXY_RADICAL_S_SD
elif species_type == sam.HYDROPEROXIDE_TYPE:
newHf_max = float(Hf) + sam.HYDROPEROXIDE_HF_SD
newS_max = float(S) + sam.HYDROPEROXIDE_S_SD
newHf_min = float(Hf) - sam.HYDROPEROXIDE_HF_SD
newS_min = float(S) - sam.HYDROPEROXIDE_S_SD
elif species_type == sam.ALKOXY_RADICAL_LIKE_PROPOXY_TYPE:
newHf_max = float(Hf) + sam.ALKOXY_RADICAL_LIKE_PROPOXY_HF_SD
newS_max = float(S) + sam.ALKOXY_RADICAL_LIKE_PROPOXY_S_SD
newHf_min = float(Hf) - sam.ALKOXY_RADICAL_LIKE_PROPOXY_HF_SD
newS_min = float(S) - sam.ALKOXY_RADICAL_LIKE_PROPOXY_S_SD
row_max_Hf = row [:]
row_min_Hf = row [:]
row_max_S = row [:]
row_min_S = row [:]
row_max_Hf [1] = newHf_max
row_min_Hf [1] = newHf_min
row_max_S [2] = newS_max
row_min_S [2] = newS_min
Hf_writer_max.writerow (row_max_Hf)
Hf_writer_min.writerow (row_min_Hf)
S_writer_max.writerow (row_max_S)
S_writer_min.writerow (row_min_S)
print (row)
print (row_max_Hf)
print (row_min_Hf)
print (row_max_S)
print (row_min_S)
#print ( 'Species Name', species_name , 'Species Type = ' , species_type, 'Hf = ', Hf, 'new Hf = ', newHf)
#print ('S = ', S, 'new S = ', newS)
else:
Hf_writer_max.writerow (row)
Hf_writer_min.writerow (row)
S_writer_max.writerow (row)
S_writer_min.writerow (row)
Hf_output_max.close ()
Hf_output_min.close ()
S_output_max.close ()
S_output_min.close ()
## Recalculate and write therrmo file based on the new generated properties file
writeNewThermoFile (butanol_species_list, Hf_output_props_maxSD_file, thermo_file_path, Hf_output_thermo_maxSD_file)
writeNewThermoFile (butanol_species_list, Hf_output_props_minSD_file, thermo_file_path, Hf_output_thermo_minSD_file)
writeNewThermoFile (butanol_species_list, S_output_props_maxSD_file, thermo_file_path, S_output_thermo_maxSD_file)
writeNewThermoFile (butanol_species_list, S_output_props_minSD_file, thermo_file_path, S_output_thermo_minSD_file)
#print (len(butanol_species_list))