forked from diffpy/diffpy.srreal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtunePeakPrecision.py
More file actions
executable file
·195 lines (160 loc) · 5.31 KB
/
tunePeakPrecision.py
File metadata and controls
executable file
·195 lines (160 loc) · 5.31 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
#!/usr/bin/env python
"""Tune the peak precision parameter so that PDFCalculator gives equivalent
results to diffpy.pdffit2.
Usage: tunePeakPrecision.py [qmax] [peakprecision] [createplot]
"""
# global imports
import sys
import time
import numpy
import diffpy.pdffit2
from diffpy.srreal.pdf_ext import PDFCalculator
from diffpy.structure import Structure
# Results:
# Qmax peakprecision CPU Notes
# 15 3.2e-6 clear minimum
# 15 3.2e-6 clear minimum
# 20 3.2e-6 clear minimum
# 25 0.45e-6 flat shape
# PDF calculation parameters
qmax = 0.0
rmin = 0.01
rmax = 50.0
rstep = 0.01
peakprecision = None
createplot = False
# make PdfFit silent
diffpy.pdffit2.redirect_stdout(open("/dev/null", "w"))
# define nickel structure data
nickel_discus_data = """
title Ni
spcgr P1
cell 3.523870, 3.523870, 3.523870, 90.000000, 90.000000, 90.000000
ncell 1, 1, 1, 4
atoms
NI 0.00000000 0.00000000 0.00000000 0.1000
NI 0.00000000 0.50000000 0.50000000 0.1000
NI 0.50000000 0.00000000 0.50000000 0.1000
NI 0.50000000 0.50000000 0.00000000 0.1000
"""
nickel = Structure()
nickel.readStr(nickel_discus_data, format="discus")
def Gpdffit2(qmax):
"""Calculate reference nickel PDF using diffpy.pdffit2.
qmax -- vawevector cutoff value in 1/A
Return numpy array of (r, g).
"""
# calculate reference data using pdffit2
pf2 = diffpy.pdffit2.PdfFit()
pf2.alloc("X", qmax, 0.0, rmin, rmax, int((rmax - rmin) / rstep + 1))
pf2.add_structure(nickel)
pf2.calc()
rg = numpy.array((pf2.getR(), pf2.getpdf_fit()))
return rg
def Gsrreal(qmax, peakprecision=None):
"""Calculate nickel PDF using PDFCalculator from diffpy.srreal.
qmax -- vawevector cutoff value in 1/A
peakprecision -- precision factor affecting peak cutoff,
keep at default value when None.
Return numpy array of (r, g).
"""
pdfcalc = PDFCalculator()
pdfcalc._setDoubleAttr("qmax", qmax)
pdfcalc._setDoubleAttr("rmin", rmin)
pdfcalc._setDoubleAttr("rmax", rmax + rstep * 1e-4)
pdfcalc._setDoubleAttr("rstep", rstep)
if peakprecision is not None:
pdfcalc._setDoubleAttr("peakprecision", peakprecision)
pdfcalc.eval(nickel)
rg = numpy.array([pdfcalc.rgrid, pdfcalc.pdf])
return rg
def comparePDFCalculators(qmax, peakprecision=None):
"""Compare Ni PDF calculations with pdffit2 and PDFCalculator.
qmax -- vawevector cutoff value in 1/A
peakprecision -- precision factor affecting peak cutoff,
keep at default value when None.
Return a dictionary of benchmark results with the following keys:
qmax -- vawevector cutoff value
peakprecision -- actual peak precision used in PDFCalculator
r -- common r-grid for the PDF arrays
g0, g1 -- calculated PDF curves from pdffit2 and PDFCalculator
gdiff -- PDF difference equal (g0 - g1)
grmsd -- root mean square value of PDF curves difference
t0, t1 -- CPU times used by pdffit2 and PDFCalculator calls
"""
rv = {}
rv["qmax"] = qmax
rv["peakprecision"] = (
peakprecision is None
and PDFCalculator()._getDoubleAttr("peakprecision")
or peakprecision
)
ttic = time.clock()
rg0 = Gpdffit2(qmax)
ttoc = time.clock()
rv["r"] = rg0[0]
rv["g0"] = rg0[1]
rv["t0"] = ttoc - ttic
ttic = time.clock()
rg1 = Gsrreal(qmax, peakprecision)
ttoc = time.clock()
assert numpy.all(rv["r"] == rg1[0])
rv["g1"] = rg1[1]
rv["t1"] = ttoc - ttic
rv["gdiff"] = rv["g0"] - rv["g1"]
rv["grmsd"] = numpy.sqrt(numpy.mean(rv["gdiff"] ** 2))
return rv
def processCommandLineArguments():
global qmax, peakprecision, createplot
argc = len(sys.argv)
if set(["-h", "--help"]).intersection(sys.argv):
print(__doc__)
sys.exit()
if argc > 1:
qmax = float(sys.argv[1])
if argc > 2:
peakprecision = float(sys.argv[2])
if argc > 3:
createplot = sys.argv[3].lower() in ("y", "yes", "1", "true")
return
def plotComparison(cmpdata):
"""Plot comparison of PDF curves.
cmpdata -- dictionary returned from comparePDFCalculators
No return value.
"""
import pylab
pylab.clf()
pylab.subplot(211)
pylab.title("qmax=%(qmax)g peakprecision=%(peakprecision)g" % cmpdata)
pylab.ylabel("G")
r = cmpdata["r"]
g0 = cmpdata["g0"]
g1 = cmpdata["g1"]
gdiff = cmpdata["gdiff"]
pylab.plot(r, g0, r, g1)
pylab.subplot(212)
pylab.plot(r, gdiff, "r")
slope = numpy.sum(r * gdiff) / numpy.sum(r**2)
pylab.plot(r, slope * r, "--")
pylab.xlabel("r")
pylab.ylabel("Gdiff")
pylab.title("slope = %g" % slope)
pylab.draw()
return
def main():
processCommandLineArguments()
cmpdata = comparePDFCalculators(qmax, peakprecision)
print(
(
"qmax = %(qmax)g pkprec = %(peakprecision)g "
+ "grmsd = %(grmsd)g t0 = %(t0).3f t1 = %(t1).3f"
)
% cmpdata
)
if createplot:
plotComparison(cmpdata)
import pylab
pylab.show()
return
if __name__ == "__main__":
main()