-
Notifications
You must be signed in to change notification settings - Fork 0
/
sa.py
159 lines (143 loc) · 4 KB
/
sa.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
import numpy as np
import random
import math
import re
import sys
import os
import subprocess
import time
atomCount = 5
def runGaussian(filename):
cmd = "g09 " + filename + ".gjf"
os.system(cmd)
#returned_value = subprocess.call(cmd, shell=True)
def loadArray(filename):
readFile = open(filename + ".txt", "r")
rline = readFile.readlines()
print("Range: " + str(len(rline)))
struct = []
for i in range(atomCount):
struct.append([])
words = rline[i].split()
struct[i].append(float(words[0]))
struct[i].append(float(words[1]))
struct[i].append(float(words[2]))
readFile.close()
return struct
def arrayToTxt(struct, filename):
writeFile = open(filename + ".txt", "w")
for i in range(atomCount):
for j in range(3):
writeFile.write(str(struct[i][j]))
writeFile.write("\t")
writeFile.write("\n")
writeFile.close()
def genGJF(filename):
readFile = open(filename + ".txt", "r")
writeFile = open(filename + ".gjf", "w")
lines_of_text = ["%nprocshared=2\n", "%mem=2GB\n", "%nosave\n", "# b3lyp/6-311+g(d,p)\n", "\n", "B5 struct\n", "\n", "0 2\n"]
writeFile.writelines(lines_of_text)
rline = readFile.readlines()
for line in rline:
writeFile.write("B\t")
writeFile.write(line)
writeFile.write("\n\n")
readFile.close()
writeFile.close()
def readEnergy(filename):
readFile = open(filename + ".log", "r")
rline = readFile.readlines()
for i in range (len(rline)):
if "E(UB3LYP)" in rline[i]:
words = rline[i].split()
readFile.close()
return float(words[4])
readFile.close()
return 9999.00
def readTimeTaken(filename):
readFile = open(filename + ".log", "r")
rline = readFile.readlines()
for i in range (len(rline)):
if "Job cpu time" in rline[i]:
words = rline[i].split()
readFile.close()
return (float(words[7]) * 60 + float(words[9]))
readFile.close()
filename = sys.argv[1]
genGJF(filename)
runGaussian(filename)
#print(readEnergy(filename))
struct = loadArray(filename)
iter = 30
trailsPerIter = 20
na = 0.0
p1 = 0.7
p50 = 0.001
# Initial temperature
t1 = -1.0/math.log(p1)
#print("t1:", t1)
# Final temperature
t50 = -1.0/math.log(p50)
#print("t50:", t50)
# Fractional reduction every cycle
frac = (t50/t1)**(1.0/(iter-1.0))
na = na + 1.0
structNew = []
for i in range(atomCount):
structNew.append([])
structNew[i].append(struct[i][0])
structNew[i].append(struct[i][1])
structNew[i].append(struct[i][2])
structBest = []
for i in range(atomCount):
structBest.append([])
structBest[i].append(struct[i][0])
structBest[i].append(struct[i][1])
structBest[i].append(struct[i][2])
initialCost = readEnergy(filename)
costBest = initialCost
newCost = initialCost
t = t1
timeTotal = 0.0
DeltaE_avg = 0.0
for i in range(iter):
print('Cycle: ' + str(i) + ' with Temperature: ' + str(t))
for j in range(trailsPerIter):
print('\tTrial number: ' + str(j))
# Generate new trial points
for k in range(atomCount):
structNew[k][0] = structNew[k][0] + random.random() - 0.5
structNew[k][1] = structNew[k][1] + random.random() - 0.5
structNew[k][2] = structNew[k][2] + random.random() - 0.5
arrayToTxt(structNew, filename + "new")
genGJF(filename + "new")
runGaussian(filename + "new")
print("\tNew Energy:\t" + str(readEnergy(filename + "new")))
timeTotal = timeTotal + readTimeTaken(filename + "new")
print("\tTime Taken:\t" + str(readTimeTaken(filename + "new")))
DeltaE = abs(readEnergy(filename + "new") - costBest)
if(readEnergy(filename + "new") > costBest):
if (i==0 and j==0):
DeltaE_avg = DeltaE
p = math.exp(-DeltaE/(DeltaE_avg * t))
if (random.random() < p):
accept = True
else:
accept = False
else:
accept = True
if(accept == True):
for k in range(atomCount):
structBest[k][0] = structNew[k][0]
structBest[k][1] = structNew[k][1]
structBest[k][2] = structNew[k][2]
costBest = readEnergy(filename + "new")
na = na + 1.0
DeltaE_avg = (DeltaE_avg * (na - 1.0) + DeltaE) / na
t = frac * t
print("Time Taken: " + str(timeTotal) + "\n")
print("Best Structure: \n")
print(structBest)
print("Best Cost: \n")
print(costBest)
#