forked from BERTHA-4c-DKS/pycubescd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pydens_iso.py
187 lines (140 loc) · 5.5 KB
/
pydens_iso.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
import matplotlib.pyplot as plt
import numpy as np
import argparse
from scipy.interpolate import RegularGridInterpolator
from scipy.interpolate import interp1d
from scipy.optimize import newton
from scipy.optimize import fmin
import os
import sys
import os.path
sys.path.append("./modules")
import load_cube
parser = argparse.ArgumentParser()
parser.add_argument("-f1", "--filefrag1", help="cube format file of molecular fragment 1", \
type=str, required=True)
parser.add_argument("-f2", "--filefrag2", help="cube format file of molecular fragment 2", \
type=str, required=True)
parser.add_argument("-iseed", "--initialseed", help="initial seed (real numer). An estimated value of the isodensity value", \
type=str, required=True)
parser.add_argument("-o_iso","--outputiso", help="Output file name", \
type=str, default="stdout")
parser.add_argument("-axis", help="Specify the axis on which evaluating the isodensity value [x,y,z]",\
type=str, default="z")
parser.add_argument("--verbose", help="increase output verbosity", \
default=False, action="store_true")
if len(sys.argv) == 1:
parser.print_help()
exit(1)
args = parser.parse_args()
if args.verbose:
print("verbosity turned on")
if (args.axis != 'z' and args.axis != 'y' and args.axis != 'x'):
args.axis = 'z'
print('Problem with the axis definition, we set it to the default z value.')
print('Reading...' + args.filefrag1)
mycube = load_cube.cube()
if not (os.path.isfile(args.filefrag1)):
print "File ", args.filefrag1, " does not exist "
exit(1)
mycube.readfile(args.filefrag1)
x, y, z = np.array(mycube.get_grid_xyz())
data = mycube.get_data()
# This is for fixing the range of sampling point (info arises from the cube grid)
nump = 40000 # Fix number of sampling points
if (args.axis == 'z'):
xpt1 = np.zeros(nump)
xpt2 = np.zeros(nump)
xpt3 = np.linspace(np.min(z),np.max(z),nump)
xpt = xpt3
elif (args.axis == 'y'):
xpt1 = np.zeros(nump)
xpt2 = np.linspace(np.min(y),np.max(y),nump)
xpt3 = np.zeros(nump)
xpt = xpt2
elif (args.axis == 'x'):
xpt1 = np.linspace(np.min(x),np.max(x),nump)
xpt2 = np.zeros(nump)
xpt3 = np.zeros(nump)
xpt = xpt1
print('info... of sampling (nump, args.axis)', nump, args.axis)
print('Interpolating...' + args.filefrag1)
my_interpolating_function = RegularGridInterpolator((x,y,z),data, method ='linear')
#my_interpolating_function = RegularGridInterpolator((x,y,z),data, method ='nearest')
pts = np.transpose([xpt1,xpt2,xpt3])
y1 = my_interpolating_function(pts)
print('Reading...' + args.filefrag2)
mycube = load_cube.cube()
if not (os.path.isfile(args.filefrag2)):
print "File ", args.filefrag2, " does not exist "
exit(1)
mycube.readfile(args.filefrag2)
x,y,z = np.array(mycube.get_grid_xyz())
data = mycube.get_data()
print('Interpolating...' + args.filefrag2)
my_interpolating_function = RegularGridInterpolator((x,y,z),data, method ='linear')
y2 = my_interpolating_function(pts)
print('Interpol 1D..linear.')
ydiff = (y2 - y1)**4
fdiff = interp1d(xpt, ydiff, kind = 'linear')
try:
isodensity_point = fmin(fdiff,args.initialseed) # find a root Note that your stating point should be close to the final result
print('isodensity_point =',isodensity_point)
except ValueError :
print('Oops problem in newton algorithm')
if (args.axis == 'z'):
isodensity_value = my_interpolating_function([0.0,0.0,float(isodensity_point)])
print('isodensity_value=',isodensity_value)
if (args.axis == 'y'):
isodensity_value = my_interpolating_function([0.0,float(isodensity_point),0.0])
print('isodensity_value=',isodensity_value)
if (args.axis == 'x'):
isodensity_value = my_interpolating_function([float(isodensity_point),0.0,0.0])
print('isodensity_value=',isodensity_value)
#print(args.outputiso)
if os.path.exists(args.outputiso):
print "File ", args.outputiso, " exist, removing it "
os.remove(args.outputiso)
f = open(args.outputiso, 'w')
f.write(('Isodensity point at %f a.u. along axis %s Isodensity value of %f e/(a.u.)^3 \n') % (isodensity_point, args.axis, isodensity_value) )
f.close()
#plt.plot(xpt,ydiff)
plt.plot(xpt,y1)
plt.plot(xpt,y2)
text = 'isodensity point is' +str(isodensity_point)
plt.annotate(text, xy=(isodensity_point,0.005), xytext=(isodensity_point,0.005), \
arrowprops=dict(facecolor='black', shrink=0.05))
text1 ='isodensity value is' +str(isodensity_value)
plt.annotate(text1, xy=(isodensity_point,0.005), xytext=(isodensity_point,0.05))
plt.ylim((-0.1,1.0))
plt.xlabel('r (a.u)')
outfilename = args.outputiso + ".eps"
if os.path.exists(outfilename):
print "File ", outfilename, " exist, removing it "
os.remove(outfilename)
print "Dumping file ", outfilename
plt.savefig(outfilename)
#plt.show()
#zz = mycube.cdz('cdz.out')
#zz = mycube.cdy('cdy.out')
#zz = mycube.cdx('cdx.out')
#mycube.toXYZ()
#b.read_cube('drho1.cube')
#c = a
#c.print_cube('test.cub')
#x = np.transpose(np.array(zz))[0]
#y = np.transpose(np.array(zz))[1]
#plt.plot(x,y)
#plt.show()
#xpt1 = np.zeros(10000)
#xpt2 = np.zeros(10000)
#xpt1 = np.full(10000,0.142727)
#xpt2 = np.full(10000,0.142727)
#xpt3 = np.linspace(-10,10,10000)
#pts = np.transpose([xpt1,xpt2,xpt3])
#my_interpolating_function(pts)
#for i in pts.tolist():
# print(('%e %e %e %e') % (i[0], i[1], i[2], my_interpolating_function(np.array(i))))
#print(np.transpose(pts)[2],my_interpolating_function(pts))
#plt.plot(np.transpose(pts)[2],my_interpolating_function(pts))
#plt.show()