Repository navigation
Expand file tree
/
Copy pathpy1_binary_exp2ZPF_v2.py
More file actions
306 lines (288 loc) · 13.7 KB
/
Copy pathpy1_binary_exp2ZPF_v2.py
File metadata and controls
306 lines (288 loc) · 13.7 KB
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
import re
import numpy as np
import json
import glob
###################################################################################
first_elem ='CR' # can be upper or lower letters for elements, will adjust them to upper later
weightTL = 1 # Weight of tie lines
weightINV = 50 # Weight of Inv equilibria
nTL_step = 3 # for tie lines, the step: 1 for all; 2 half TL, 3 for 1/3 TL, ...
text1 = 'Comment from TDB'
text2 = 'Reference from TDB'
####################################################################################
####################################################################################
################ Begin: def some funcitons #########################################
def find_TL_in_PLOT(blockTL_plot):
dd=dict()
Comp1=[]; nComp1=[]; Temp2=[]; nTemp2=[]; phase=[]
index_start = blockTL_plot.find(" M") # seperate by " M"
tieline=blockTL_plot[index_start - 36:]
tt_lines = tieline.split('\n'); # print('tt_lines =', tt_lines, "and lines = ", len(tt_lines))
for line in tt_lines:
if line == '': continue
filtered_line = [val for val in line.split(' ') if val != '']
Comp1.append(filtered_line[0]) # yields a list of composition
value1 = float(filtered_line[0])
if value1 < 1e-4:
print('find TL value < 1e-4 with its value = ', value1)
value1 = 1e-4
if value1 > 0.9999:
print('find TL value > 0.9999 with its value = ', value1)
value1 = 0.9999
nComp1.append(value1) # yields a list of composition, number
Temp2.append(filtered_line[1]) # yields a list of composition
nTemp2.append(float(filtered_line[1])) # yields a list of composition, number
phase_start = blockTL_plot.find("(")
phase_end = blockTL_plot.find(")")
phelem = blockTL_plot[phase_start + 1:phase_end]; # print(phelem)
asdf = re.split(",", phelem)
phase = asdf[0]; # print(phase)
if '#' in phase:
aaa = re.split("#", phase)
phase=aaa[0]
dd['TLphase']=phase
dd['TLcomp1']=nComp1
dd['TLtemp2']=nTemp2
return dd
#---------------
def find_phase_in_plot(line_value, whole_blockTL):
blockTL = re.split("\$ PLOTTED COLUMNS ARE", whole_blockTL)
phase_find=[]
for ii in range(1, len(blockTL)):
if line_value in blockTL[ii]:
#print('In func, for block=',ii, 'values are=', blockTL[ii])
asdf=blockTL[ii]
phase_start = asdf.find("(")
phase_end = asdf.find(")")
phelem = asdf[phase_start + 1:phase_end]
aa = re.split(",", phelem)[0]
if '#' in aa:
bb = re.split("#", aa)
phase = bb[0]
phase_find.append(phase)
else:
phase_find.append(aa)
return phase_find
##----------------
def two_values_in_one_line(line_value):
filtered_line = [val for val in line_value.split(' ') if val != '']
value1=float(filtered_line[0]) # yields the first value (for composition)
if value1 < 1e-4 :
print('find INV value < 1e-4 with its value = ', value1)
value1 = 1e-4
if value1 > 0.9999:
print('find INV value > 0.9999 with its value = ', value1)
value1 = 0.9999
value2=float(filtered_line[1]) # yields the second value (for Temp)
dd=[value1, value2]
return dd
##----------------
def To_write_json_file(in_dict, out_json_file):
with open(out_json_file, 'w') as f:
f.write('{\n')
f.write(' "comment": ')
f.write(json.dumps(in_dict.get('comment')))
f.write(",")
f.write('\n "reference": ')
f.write(json.dumps(in_dict.get('reference')))
f.write(",")
f.write('\n "weight": ')
f.write(json.dumps(in_dict.get('weight')))
f.write(",")
f.write('\n "phases": ')
f.write(json.dumps(in_dict.get('phases')))
f.write(",")
f.write('\n "values": ')
f.write(json.dumps(in_dict.get('values')))
f.write(",")
f.write('\n "components": ')
f.write(json.dumps(in_dict.get('components')))
f.write(",")
f.write('\n "output": ')
f.write(json.dumps(in_dict.get('output')))
f.write(",")
f.write('\n "broadcast_conditions": false,')
f.write('\n "conditions": ')
f.write(json.dumps(in_dict.get('conditions')))
f.write('\n}')
f.write('\n')
f.close()
def find_two_elems(textp):
aa_lines = textp.split('\n')
elem_tot=[]
for line in aa_lines:
if " XTEXT " in line:
filtered_line = [val for val in line.split(' ') if val != '']
elem1=filtered_line[2]
if " YTEXT " in line:
filtered_line = [val for val in line.split(' ') if val != '']
elem2=filtered_line[1] # for binary case only, is not useful
elem_tot=[elem1, elem2]
return elem_tot
#---------------
def To_build_TLdict(ALLTL_PH, ALLTL_comp1, ALLTL_temp2, all_elements, second_elem, nTL_step, text1, text2, weightTL):
# for tie-line case in a small block, NOT for all tie-lines
phase_list = np.unique(ALLTL_PH)
phase_list = [x for x in phase_list]
val_temp = []; val_TL = []
for nnn in range(0, len(ALLTL_comp1[0]), nTL_step):
qqq = [[ALLTL_PH[0], [second_elem], [ALLTL_comp1[0][nnn]]],
[ALLTL_PH[1], [second_elem], [ALLTL_comp1[1][nnn]]]]
val_TL.append(qqq)
val_temp.append([ALLTL_temp2[0][nnn]])
val_temp = [x[0] for x in val_temp]
condits = dict(P=101325, T=val_temp)
TL_dict = dict(comment=text1, phases=phase_list, reference=text2, values=val_TL, components=all_elements,
output='ZPF', broadcast_conditions='false', conditions=condits, weight=weightTL)
return TL_dict
################## End: def some funcitons #########################################
####################################################################################
####################################################################################
#---------------------
#my_file=open("correctedPD_CrHf.exp",'r') # open Thermo-Calc EXP file: binary system
current_directory_exp = glob.glob('*.exp')
exp_tc_name =current_directory_exp[0]
print('Used EXP file for this job is: ', current_directory_exp[0])
my_file=open(exp_tc_name,'r') # open Thermo-Calc EXP file: binary system
### ---------------------
with open('temp1.txt', 'w') as f:
f.write(str(2)) # 3 for ternary and 2 for binary
f.close()
### ---------------------
textp = my_file.read() # read Thermo-Calc EXP file
elemtwo=find_two_elems(textp)
first_elem =first_elem.upper()
#second_elem=second_elem.upper()
second_elem =elemtwo[0]
all_elements=np.sort([first_elem, second_elem]) # alpha-beta list of all upper elements
all_elements=[x for x in all_elements]
#---------------------------------------
#
# next to split the blocks and process both Tie-Line and Inv Eqs separately
#
blocksp = re.split("BLOCKEND", textp) # splits txt file by BLOCKEND
blocksp = blocksp[0:len(blocksp)-1] # remove the last block
index_TL=[]; index_INV=[]
for index in range(0,len(blocksp)): #for each block
if "INVARIANT EQUILIBRIUM" in blocksp[index]: ## case 1 of 2 for Inv blocks
#print("The block of", index, " is for invariant Eqs")
num_M=blocksp[index].count(" M")
if num_M == 3: index_INV.append(index)
else: #-------- case 2 of 2: for Tie Line block/case below
#print("The block of", index, " is for tie lines")
index_TL.append(index)
print('The blocks for index_INV = ', index_INV)
print('The blocks for index_TL = ', index_TL)
print()
#################################################################################
ALLINV_comp1 =[]; ALLINV_temp2 =[]; ALLINV_PH=[]
for index in index_INV: #for each block of INV
print("The block of", index, " is for invariant Eqs")
blockINV = re.split("\$ INVARIANT EQUILIBRIUM", blocksp[index])
ph3_comp1 = []; ph3_temp2 = []; ph3_phases = []; nph3_comp1 = []; nph3_temp2 = []
aa_lines = blockINV[0].split('\n'); #print('blockINV0=', blockINV[0], 'aa_lines Inv = ', len(aa_lines))
for line in aa_lines:
if " BLOCK " in line: continue
if line == '': continue
filtered_line = [val for val in line.split(' ') if val != '']
phph=filtered_line[-1]
if '#' in phph:
bb = re.split("#", phph)
ph3_phases.append(bb[0])
else:
ph3_phases.append(phph)
#print('3-phases_inv_block = ', ph3_phases)
aa_lines = blockINV[1].split('\n'); #print('blockINV1=', blockINV[1], 'aa_lines Inv = ', len(aa_lines))
list_del = ("BLOCK ", "COLOR ", " M")
my_phase=[]
for line in aa_lines:
if any(s in line for s in list_del): continue
if line == '': continue
check_line = line; #print('Here the line value =', line)
asdf=two_values_in_one_line(check_line)
ph3_comp1.append(asdf[0])
ph3_temp2.append(asdf[1])
for jj in index_TL:
aa=find_phase_in_plot(check_line, blocksp[jj]); #print('The phase find is = ', aa)
if len(aa) == 1: my_phase.append(aa); break
print('Here are the 3 Phases for INV =', my_phase)
print('Here are the 3 Comp1 for INV =', ph3_comp1)
print('Here are the 3 Temp2 for INV =', ph3_temp2)
ALLINV_comp1.append(ph3_comp1)
ALLINV_temp2.append(ph3_temp2)
my_phase=[x[0] for x in my_phase]
ALLINV_PH.append(my_phase)
print()
#print('ALL Inv comp1 =', ALLINV_comp1); print('ALL Inv comp2 =', ALLINV_temp2); print('ALL Inv Phases=', ALLINV_PH)
#--------------------------
phase_list3=np.unique(ALLINV_PH)
phase_list3=[x for x in phase_list3]
print('List All Unique Phases in Inv =', phase_list3)
val_temp3 = []; val_INV = []
for iINV in range(0,len(ALLINV_PH)):
qqq = [[ALLINV_PH[iINV][0], [second_elem], [ALLINV_comp1[iINV][0]]],
[ALLINV_PH[iINV][1], [second_elem], [ALLINV_comp1[iINV][1]]],
[ALLINV_PH[iINV][2], [second_elem], [ALLINV_comp1[iINV][2]]]]
val_INV.append(qqq)
val_temp3.append(ALLINV_temp2[iINV][0])
condit3=dict(P=101325, T=val_temp3)
#print('T and P = ', condit3); print('val_TL3= ', val_TL3)
INV_dict=dict(comment=text1, phases=phase_list3, reference=text2, values=val_INV, components=all_elements,
output='ZPF', broadcast_conditions='false', conditions=condit3, weight=weightINV)
To_write_json_file(INV_dict, 'INV_Alls.json')
print('########################## END of the INV cases #########################')
print()
####################################################################################
####################################################################################
###########################################: next for TL: one by one and all_together
ALLTL_comp1 = []; ALLTL_temp2 = []; ALLTL_PH = []
for index in index_TL: #[0, 2]: #for each block of Tie-Lines
print("The block of", index, " is for tie lines")
blockTL = re.split("\$ PLOTTED COLUMNS ARE :", blocksp[index])
index_0 = blockTL[0].find(" $ BLOCK #"); #print('index_0=',index_0)
newcontent = blockTL[0][index_0 + 10 : ]; #print('newcontent=',newcontent)
block_number = re.split(" ", newcontent)[0]
print('block_number=',block_number)
phaseTL=[]; compTL_1=[]; tempTL_2=[]
for ii in [1,2]:
#print('for case in block', ii) # print('blockTL= \n', blockTL[ii])
TLdata=find_TL_in_PLOT(blockTL[ii])
#print('blockTL of phase= ',TLdata.get('TLphase'))
phaseTL.append(TLdata.get('TLphase'))
compTL_1.append(TLdata.get('TLcomp1'))
tempTL_2.append(TLdata.get('TLtemp2'))
print('These two phases in TL =', phaseTL);
#print('comp1 in TL for two phases len= ',len(compTL_1), ' values=', compTL_1); print('temp2 in TL for two phases=', tempTL_2)
TLname=['TL+',block_number,'+',phaseTL[0],'+',phaseTL[1],'+step', str(nTL_step),'.json']
TLname=''.join(TLname); #print('TL_NAME=', TLname)
TL_dict=To_build_TLdict(phaseTL, compTL_1, tempTL_2, all_elements, second_elem, nTL_step, text1, text2, weightTL)
To_write_json_file(TL_dict, TLname)
#-----------
ALLTL_comp1.append(compTL_1)
ALLTL_temp2.append(tempTL_2)
ALLTL_PH.append(phaseTL)
print()
#-----------------------------------------------------------------
#print('ALLTL_PH len and values=', len(ALLTL_PH), ALLTL_PH)
phase_list=np.unique(ALLTL_PH)
phase_list=[x for x in phase_list]
print('List ALL Unique Phases of TLs: ', phase_list)
#print('ALLTL_comp1 len and values=', len(ALLTL_comp1), ALLTL_comp1)
val_temp = []; val_TL = []
for iTL in range(0,len(ALLTL_PH)):
for nnn in range(0, len(ALLTL_comp1[iTL][0]), nTL_step):
qqq=[[ALLTL_PH[iTL][0], [second_elem], [ALLTL_comp1[iTL][0][nnn]]],
[ALLTL_PH[iTL][1], [second_elem], [ALLTL_comp1[iTL][1][nnn]]]]
val_TL.append(qqq)
val_temp.append(ALLTL_temp2[iTL][0][nnn])
condit=dict(P=101325, T=val_temp)
INV_TL=dict(comment=text1, phases=phase_list3, reference=text2, values=val_TL, components=all_elements,
output='ZPF', broadcast_conditions='false', conditions=condit, weight=weightTL)
name2=['TL_ALLs_step',str(nTL_step),'.json']
ALLname=''.join(name2)
To_write_json_file(INV_TL, ALLname)
##########
print()
print('========================================================')
print('Updated on 2019-07-19: read EXP first one, and second_elem auto')
print('Updated on 2019-06-26: adjust TL/INV compositions close to 0 1')