-
Notifications
You must be signed in to change notification settings - Fork 2
/
OPTIMIZE-setup.py
executable file
·365 lines (300 loc) · 13.6 KB
/
OPTIMIZE-setup.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env python
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
#%%% --------------------------------------------- OPTIMIZE-setup.py --------------------------------------------- %%%#
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
#
# AUTHOR:
# Rostam Golesorkhtabar
#
# DATE:
# Wed Aug 01 00:00:00 2012
#
# SYNTAX:
# python OPTIMIZE-setup.py
# OPTIMIZE-setup.py
#
# EXPLANATION:
#
#______________________________________________________________________________________________________________________
import os
import sys
import math
import shutil
import os.path
import numpy as np
from sys import stdin
from lxml import etree as ET
#%%%--- Dictionaries ---%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Def_matrix = {\
'VOL' :
'\n[ 1+eps 0 0 ]' \
'\n[ 0 1+eps 0 ]' \
'\n[ 0 0 1+eps ]',\
'BOA' :
'\n[(1+eps)^-.5 0 0 ]' \
'\n[ 0 (1+eps)^+1. 0 ]' \
'\n[ 0 0 (1+eps)^-.5 ]',\
'COA' :
'\n[(1+eps)^-.5 0 0 ]' \
'\n[ 0 (1+eps)^-.5 0 ]' \
'\n[ 0 0 (1+eps)^+1. ]',\
'ALPHA':
'\n[ 1/(1-eps^2) 0 0 ]' \
'\n[ 0 1 eps ]' \
'\n[ 0 eps 1 ]',\
'BETA' :
'\n[ 1 0 eps ]' \
'\n[ 0 1/(1-eps^2) 0 ]' \
'\n[eps 0 1 ]',\
'GAMMA':
'\n[ 1 eps 0 ]' \
'\n[eps 1 0 ]' \
'\n[ 0 0 1/(1-eps^2)]'}
LC_Dic = { \
'CI' :'Cubic I' ,\
'CII':'Cubic II' ,\
'HI' :'Hexagonal I' ,\
'HII':'Hexagonal II' ,\
'RI' :'Rhombohedral I' ,\
'RII':'Rhombohedral II',\
'TI' :'Tetragonal I' ,\
'TII':'Tetragonal II' ,\
'O' :'Orthorhombic' ,\
'M' :'Monoclinic' ,\
'N' :'Triclinic'}
#----------------------------------------------------------------------------------------------------------------------
#%%%--- Checking the input file exist ---%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
INF = raw_input('\n>>>> Please enter the exciting input file name: ')
if (os.path.exists(INF) == False):
sys.exit('\n ... Oops ERROR: There is NO '+ INF +' file !?!?!? \n')
#----------------------------------------------------------------------------------------------------------------------
#%%%--- Calculating the Space-Group Number and classifying it ---%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
os.system('xsltproc $EXCITINGSCRIPTS/exciting2sgroup.xsl '+ INF +' > sgroup.in')
os.system('sgroup sgroup.in 1>sgroup.out 2>sgroup.err; rm -f sgroup.in ')
if (os.path.getsize('sgroup.err') != 0):
fer = open('sgroup.err', 'r')
lines= fer.readlines()
print '\n ... Oops '+ lines[0]
for i in range(1, len(lines)):
print ' '+ lines[i]
print
fer.close()
sys.exit()
else:
os.system('rm -f sgroup.err')
SGf = open('sgroup.out', 'r')
SGlins= SGf.readlines()
SGf.close()
for i in range(len(SGlins)):
if (SGlins[i].find('Number and name of space group:') >= 0):
SGN = int(float(SGlins[i].split()[6]))
SGN_explanation=SGlins[i].strip()
break
if (1 <= SGN and SGN <= 2): # Triclinic
LC = 'N'
elif(3 <= SGN and SGN <= 15): # Monoclinic
LC = 'M'
elif(16 <= SGN and SGN <= 74): # Orthorhombic
LC = 'O'
elif(75 <= SGN and SGN <= 88): # Tetragonal II
LC = 'TII'
elif(89 <= SGN and SGN <= 142): # Tetragonal I
LC = 'TI'
elif(143 <= SGN and SGN <= 148): # Rhombohedral II
LC = 'RII'
elif(149 <= SGN and SGN <= 167): # Rhombohedral I
LC = 'RI'
elif(168 <= SGN and SGN <= 176): # Hexagonal II
LC = 'HII'
elif(177 <= SGN and SGN <= 194): # Hexagonal I
LC = 'HI'
elif(195 <= SGN and SGN <= 206): # Cubic II
LC = 'CII'
elif(207 <= SGN and SGN <= 230): # Cubic I
LC = 'CI'
else: sys.exit('\n ... Oops ERROR: WRONG Space-Group Number !?!?!? \n')
print '\n '+ SGN_explanation +'\
\n '+ LC_Dic[LC] +' structure in the Laue classification.'
#----------------------------------------------------------------------------------------------------------------------
#%%%--- Read optimization type ---%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (LC=='CI' or LC=='CII'):
dirn = 'VOL'
print
if (LC=='HI' or\
LC=='HII'or\
LC=='RI' or\
LC=='RII'or\
LC=='TI' or\
LC=='TII'):
print '\n Which parameter would you like to optimize?'\
'\n 1 ... volume '\
'\n 2 ... c/a ratio with constant volume '
num = input(">>>> Please choose '1' or '2': ")
if (num != 1 and num != 2):
sys.exit("\n ... Oops ERROR: Choose '1' or '2' \n")
if (num == 1 ): dirn = 'VOL'
if (num == 2 ): dirn = 'COA'
if (LC=='O'):
print '\n Which parameter would you like to optimize?'\
'\n 1 ... volume '\
'\n 2 ... b/a ratio with constant volume '\
'\n 3 ... c/a ratio with constant volume '
num = input(">>>> Please choose '1' or '2' or '3': ")
if (num != 1 and num != 2 and num != 3):
sys.exit("\n ... Oops ERROR: Choose '1' or '2' or '3'\n")
if (num == 1 ): dirn = 'VOL'
if (num == 2 ): dirn = 'BOA'
if (num == 3 ): dirn = 'COA'
if (LC=='M'):
print '\n Which parameter would you like to optimize?'\
'\n 1 ... volume '\
'\n 2 ... b/a ratio with constant volume '\
'\n 3 ... c/a ratio with constant volume '\
'\n 4 ... gamma angle with constant volume '
num = input(">>>> Please choose '1' or '2' or '3' or '4': ")
if (num != 1 and num != 2 and num != 3 and num != 4):
sys.exit("\n ... Oops ERROR: Choose '1' or '2' or '3' or '4'\n")
if (num == 1 ): dirn = 'VOL'
if (num == 2 ): dirn = 'BOA'
if (num == 3 ): dirn = 'COA'
if (num == 4 ): dirn = 'GAMMA'
if (LC=='N'):
print '\n Which parameter would you like to optimize?'\
'\n 1 ... volume '\
'\n 2 ... b/a ratio with constant volume '\
'\n 3 ... c/a ratio with constant volume '\
'\n 4 ... alpha angle with constant volume '\
'\n 5 ... beta angle with constant volume '\
'\n 6 ... gamma angle with constant volume '
num = input(">>>> Please choose '1' or '2' or '3' or '4' or '5' or '6': ")
if (num != 1 and num != 2 and num != 3 and num != 4 and num != 5 and num != 6):
sys.exit("\n ... Oops ERROR: Choose '1' or '2' or '3' or '4' or '5' or '6'\n")
if (num == 1 ): dirn = 'VOL'
if (num == 2 ): dirn = 'BOA'
if (num == 3 ): dirn = 'COA'
if (num == 4 ): dirn = 'ALPHA'
if (num == 5 ): dirn = 'BETA'
if (num == 6 ): dirn = 'GAMMA'
#----------------------------------------------------------------------------------------------------------------------
#%%%--- Reading the input file ---%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
INOBJ= open(INF, 'r')
doc = ET.parse(INOBJ)
root = doc.getroot()
#----------------------------------------------------------------------------------------------------------------------
#%%%--- Reading the scale, stretch, and base vectors from the input file and calculate the volume ---%%%%%%%%%%%%%%%%%%
scale = map(float,doc.xpath('/input/structure/crystal/@scale'))
if (scale==[]):
sys.exit('\n ... Oops ERROR: There is NO scale in '+ INF +' file !?!?!? \n')
stretchstr = doc.xpath('/input/structure/crystal/@stretch')
if (stretchstr==[]):
stretch=[1.,1.,1.]
else:
stretch=np.array(map(float,stretchstr[0].split()))
basevectsn = doc.xpath('//basevect/text()')
bv = []
for basevect in basevectsn:
bv.append(map(float,basevect.split()))
M_old= np.array(bv)
D = np.linalg.det(M_old)
V0 = abs(stretch[0]*stretch[1]*stretch[2]*scale[0]**3*D)
#----------------------------------------------------------------------------------------------------------------------
#%%%--- Directory management ---%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (os.path.exists(dirn+'_old')):
shutil.rmtree( dirn+'_old')
if (os.path.exists(dirn)):
os.rename(dirn,dirn+'_old')
os.mkdir(dirn)
os.chdir(dirn)
#----------------------------------------------------------------------------------------------------------------------
#%%%--- Reading the maximum physical strain and number of distorted structures ---%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
mdr = input('\n>>>> Please enter the maximum physical strain value '\
'\n The suggested value is between 0.001 and 0.050: ')
if (1 < mdr or mdr < 0):
sys.exit('\n ... Oops ERROR: The maximum physical strain is OUT of range !!!!!!\n')
mdr = round(mdr, 4)
print ' The maximum physical strain is '+ str(mdr)
NoP = input('\n>>>> Please enter the number of the distorted structures [odd number > 4]: ')
NoP = int(abs(NoP))
if (NoP < 5):
sys.exit('\n ... Oops ERROR: The NUMBER of the distorted structures < 5 !!!!!! \n')
if (99 < NoP):
sys.exit('\n ... Oops ERROR: The NUMBER of the distorted structures > 99 !!!!!! \n')
if (NoP%2 == 0):
NoP += 1
print ' The number of the distorted structures is '+ str(NoP) + '\n'
ptn = int((NoP-1)/2)
if (mdr/ptn <= 0.00001):
sys.exit(' ... Oops ERROR: The interval of the strain values is < 0.00001'\
'\n Choose a larger maximum physical strain '\
'\n or a less number of distorted structures. \n')
#----------------------------------------------------------------------------------------------------------------------
#%%%--- Making the INFO file ---%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
INFO = open('INFO_'+ dirn , 'w')
print >>INFO, 'Space-group number =', SGN ,\
'\nStructure type =', LC_Dic[LC],\
'\nMaximum physical strain =', mdr ,\
'\nNumber of distorted structures =', NoP
INFO.close
#----------------------------------------------------------------------------------------------------------------------
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
###------------------------------------------- Structures maker ----------------------------------------------------###
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
fdis = open(dirn.lower()+'_Parameters', 'w')
print >>fdis,dirn+', Deformation Matrix = ' + Def_matrix[dirn]
cont= 0
for s in range(-ptn, ptn+1):
r = mdr*s/ptn
if (s==0): r = 0.00001
eps = r
def_matrix={\
'VOL' :[[1.+eps , 0. , 0. ],
[0. , 1.+eps , 0. ],
[0. , 0. , 1+eps ]],\
'BOA' :[[(1+eps)**-.5, 0. , 0. ],
[ 0. , 1.+eps , 0. ],
[ 0. , 0. ,(1+eps)**-.5]],\
'COA' :[[(1+eps)**-.5, 0. , 0. ],
[ 0. , (1+eps)**-.5, 0. ],
[ 0. , 0. , 1.+eps ]],\
'ALPHA':[[1./(1-eps**2), 0. , 0. ],
[ 0. , 1. ,eps ],
[ 0. ,eps , 1. ]],\
'BETA' :[[ 1. , 0. ,eps ],
[ 0. , 1./(1-eps**2), 0. ],
[eps , 0. , 1. ]],\
'GAMMA':[[ 1. ,eps , 0. ],
[eps , 1. , 0. ],
[ 0. , 0. , 1./(1-eps**2)]]}
M_eps = np.array(def_matrix[dirn])
M_new = np.dot(M_old, M_eps)
cont += 1
if (cont < 10):
dirn_num = dirn.lower() + '_0'+str(cont)
else:
dirn_num = dirn.lower() + '_' +str(cont)
bsvct = doc.xpath('//crystal/basevect')
print>>fdis, '\n'+dirn_num+', Physical strain = '+ str(eps)
for j in range(3):
bdummy = '%22.16f'%(M_new[j,0]) + '%22.16f'%(M_new[j,1]) + '%22.16f'%(M_new[j,2])+' '
print>>fdis, 'V' + str(j+1) + ' --=> ' + bdummy
bsvct[j].text = bdummy
os.mkdir(dirn_num)
os.chdir(dirn_num)
# Writing the structure file --------------------------------------------------------------------------------------
fileName = dirn_num + '.xml'
OUTOBJ = open(fileName, 'w')
OUTOBJ.write(ET.tostring(root, method ='xml',
pretty_print =True ,
xml_declaration=True ,
encoding ='UTF-8'))
OUTOBJ.close()
os.chdir('../')
#----------------------------------------------------------------------------------------------------------------------
print>>fdis,'\n Distorted parameters: END'
fdis.close()
os.mkdir(dirn.lower()+'-xml')
os.system('cp -f */*_??.xml '+dirn.lower()+'-xml/')
os.system('cp -f ../'+INF+' '+dirn.lower()+'-xml/input.xml')
os.chdir('../')
#----------------------------------------------------------------------------------------------------------------------