import subprocess
import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize
%matplotlib inline
inp_default = '''!HF RHF 6-31G
* int 0 1
C 0 0 0 0 0 0
C 1 0 0 1.52986 0 0
H 1 2 0 1.08439 111.200 0
H 1 2 3 1.08439 111.200 120
H 1 2 3 1.08439 111.200 -120
H 2 1 3 1.08439 111.200 180
H 2 1 5 1.08439 111.200 120
H 2 1 5 1.08439 111.200 -120
*
'''
inp = '''!HF RHF 6-31G
* int 0 1
C 0 0 0 0 0 0
C 1 0 0 {length} 0 0
H 1 2 0 1.08439 {angle} 0
H 1 2 3 1.08439 {angle} 120
H 1 2 3 1.08439 {angle} -120
H 2 1 3 1.08439 {angle} {torsion1}
H 2 1 5 1.08439 {angle} {torsion2}
H 2 1 5 1.08439 {angle} {torsion3}
*
'''
default_length=1.52986
default_angle=111.2
default_torsion1=180
default_torsion2=120
default_torsion3=-120
difference=0.02 #шаг 0.02 ангстрема
dif_count=20 #количество разных длин
min_torsion=-180
max_torsion=180
steps=12
def run_orca(inp):
with open('orca.inp', 'w') as outfile:
outfile.write(inp)
p = subprocess.Popen("/home/shad/progs/bin/orca orca.inp",
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out=p.communicate()[0]
energy_line = filter(lambda x: 'FINAL SINGLE POINT ENERGY' in x, out.splitlines())[0]
return float(energy_line.split()[-1])
def append_length(lengths,default_angle,default_torsion1,default_torsion2,default_torsion3):
length_energies = []
for length in lengths:
length_energies.append(run_orca(inp.format(
length = length,
angle = default_angle,
torsion1 = default_torsion1,
torsion2 = default_torsion2,
torsion3 = default_torsion3,
)))
return length_energies
def append_angle(angles,default_length,default_torsion1,default_torsion2,default_torsion3):
angle_energies = []
for angle in angles:
angle_energies.append(run_orca(inp.format(
length = default_length,
angle = angle,
torsion1 = default_torsion1,
torsion2 = default_torsion2,
torsion3 = default_torsion3,
)))
return angle_energies
def append_torsion(torsions,default_length,default_angle):
torsion_energies = []
for torsion in torsions:
torsion_energies.append(run_orca(inp.format(
length = default_length,
angle = default_angle,
torsion1 = torsion,
torsion2 = torsion - 60,
torsion3 = torsion + 60,
)))
return torsion_energies
lengths = np.arange(-dif_count/2, dif_count/2+1)*difference+default_length
length_energies=append_length(lengths,default_angle,default_torsion1,default_torsion2,default_torsion3)
print(length_energies)
angles = np.arange(-dif_count/2, dif_count/2+1)*difference*10 + default_angle
angle_energies=append_angle(angles,default_length,default_torsion1,default_torsion2,default_torsion3)
print(angle_energies)
torsions = np.arange(min_torsion, max_torsion+1, steps)
torsion_energies=append_torsion(torsions,default_length,default_angle)
print(torsion_energies)
x_o=lengths
y_o=length_energies
#function is f(x)=k(b-x)^2 + a
fitfunc = lambda p, x: p[0]*pow(p[1]-x,2) + p[2] # Target function
errfunc = lambda p, x, y: fitfunc(p, x) - y # Error function
p0 = [1,1, -79] # Initial guess for the parameters
p1, success = optimize.leastsq(errfunc, p0[:], args=(x_o, y_o))
print "Optimized params:", p1
plt.plot(x_o, y_o, "ro", x_o,fitfunc(p1,x_o),"r-",c='blue',alpha=0.5)
plt.title('length_energies(lengths)')
plt.xlabel('lengths')
plt.ylabel('length_energies')
#plt.xlim(1,2)
plt.show()
x_o=angles
y_o=angle_energies
#function is f(x)=k(b-x)^2 + a
fitfunc = lambda p, x: p[0]*pow(p[1]-x,2) + p[2] # Target function
errfunc = lambda p, x, y: fitfunc(p, x) - y # Error function
p0 = [1,1, -79] # Initial guess for the parameters
p1, success = optimize.leastsq(errfunc, p0[:], args=(x_o, y_o))
print "Optimized params:", p1
plt.plot(x_o, y_o, "ro", x_o,fitfunc(p1,x_o),"r-",c='blue',alpha=0.5)
plt.title('angles_energies(angles)')
plt.xlabel('angles')
plt.ylabel('angles_energies')
#plt.xlim(1,2)
plt.show()
x_o=torsions
y_o=torsion_energies
#function is f(x)=k(b-x)^2 + a
fitfunc = lambda p, x: p[0]*pow(p[1]-x,2) + p[2] # Target function
errfunc = lambda p, x, y: fitfunc(p, x) - y # Error function
p0 = [1,1, -79] # Initial guess for the parameters
p1, success = optimize.leastsq(errfunc, p0[:], args=(x_o, y_o))
print "Optimized params:", p1
plt.plot(x_o, y_o, "ro", x_o,fitfunc(p1,x_o),"r-",c='blue',alpha=0.5)
plt.title('torsion_energies(torsions)')
plt.xlabel('torsions')
plt.ylabel('torsion_energies')
#plt.xlim(1,2)
plt.show()
x_o=torsions
y_o=torsion_energies
fitfunc = lambda p, x: p[0]*np.sin(p[1]*x)+p[3]*np.cos(p[4]*x) + p[2] # Target function
errfunc = lambda p, x, y: fitfunc(p, x) - y # Error function
p0 = [1,1,1,1, -79] # Initial guess for the parameters
p1, success = optimize.leastsq(errfunc, p0[:], args=(x_o, y_o))
print "Optimized params:", p1
# Plot
plt.plot(x_o, y_o, "ro", x_o,fitfunc(p1,x_o),"r-",c='blue',alpha=0.5)
plt.title('torsion_energies(torsions)')
plt.xlabel('torsions')
plt.ylabel('torsion_energies')
#plt.xlim(1,2)
plt.show()
plt.show()
difference=0.1
lengths_higher_difference = np.arange(-dif_count/2, dif_count/2+1)*difference+default_length
length_energies_higher_difference=append_length(lengths_higher_difference,default_angle,default_torsion1,default_torsion2,default_torsion3)
print (length_energies_higher_difference)
x_o=lengths_higher_difference
y_o=length_energies_higher_difference
fitfunc = lambda p, x: pow(x,p[1])+p[0] # Target function
errfunc = lambda p, x, y: fitfunc(p, x) - y # Error function
p0 = [1, -79] # Initial guess for the parameters
p1, success = optimize.leastsq(errfunc, p0[:], args=(x_o, y_o))
print "Optimized params:", p1
# Plot
plt.plot(x_o, y_o, "ro", x_o,fitfunc(p1,x_o),"r-",c='blue',alpha=0.5)
plt.title('length_energies(lengths)')
plt.xlabel('lengths')
plt.ylabel('length_energies')
#plt.xlim(1,2)
plt.show()