Skip to content

Commit 5923fa2

Browse files
authored
Merge pull request #74 from Tieqiong/doc
provide info in doc and write an example page
2 parents 50617fa + 3bb7a1e commit 5923fa2

7 files changed

Lines changed: 226 additions & 48 deletions

File tree

doc/examples/Ni_calculation.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

doc/source/examples.rst

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
.. _examples:
2+
3+
Examples
4+
########
5+
6+
Welcome! This guide offers several examples to help you effectively utilize this package.
7+
8+
Files needed:
9+
10+
1. :download:`Ni-xray.gr <examples/Ni-xray.gr>` - experimental X-ray PDF data
11+
2. :download:`Ni.stru <examples/Ni.stru>` - Ni f.c.c. structure in PDFfit format
12+
13+
======================================
14+
Example 1: Calculate PDF of FCC nickel
15+
======================================
16+
17+
The first example shows how to calculates the PDF for FCC nickel and saves the resulting data to a file and plot it using matplotlib.
18+
19+
1. Imports the PdfFit class from the diffpy.pdffit2 module::
20+
21+
from diffpy.pdffit2 import PdfFit
22+
23+
2. Create a PDF calculator object and assigned to the variable ``P``. Make sure the ``Ni.stru`` file is in the same directory as the script and you've cd to the directory, load structure file. Then allocate and configure PDF calculation and run the calculation::
24+
25+
# create new PDF calculator object
26+
P = PdfFit()
27+
28+
# load structure file in PDFFIT or DISCUS format
29+
P.read_struct("Ni.stru")
30+
31+
radiation_type = "X" # x-rays
32+
qmax = 30.0 # Q-cutoff used in PDF calculation in 1/A
33+
qdamp = 0.01 # instrument Q-resolution factor, responsible for PDF decay
34+
rmin = 0.01 # minimum r-value
35+
rmax = 30.0 # maximum r-value
36+
npts = 3000 # number of points in the r-grid
37+
38+
# allocate and configure PDF calculation
39+
P.alloc(radiation_type, qmax, qdamp, rmin, rmax, npts)
40+
P.calc()
41+
42+
3. Save the refined result::
43+
44+
P.save_pdf(1, "Ni_calculation.cgr")
45+
46+
4. We can also plot it using matplotlib::
47+
48+
import matplotlib.pyplot as plt
49+
50+
# obtain list of r-points and corresponding G values
51+
r = P.getR()
52+
G = P.getpdf_fit()
53+
54+
plt.plot(r, G)
55+
plt.xlabel("r (Å)")
56+
plt.ylabel("G (Å$^{-2}$)")
57+
plt.title("x-ray PDF of nickel simulated at Qmax = %g" % qmax)
58+
59+
# display plot window, this must be the last command in the script
60+
plt.show()
61+
62+
The scripts can be downloaded :download:`here <examples/Ni_calculation.py>`.
63+
64+
=======================================
65+
Example 2: Performing simple refinement
66+
=======================================
67+
68+
The second example shows how to perform simple refinement of Ni structure to the experimental x-ray PDF. The example uses the same data files as the first example.
69+
70+
1. Imports the PdfFit class from the diffpy.pdffit2 module::
71+
72+
from diffpy.pdffit2 import PdfFit
73+
74+
2. Create a PDF calculator object and assigned to the variable ``pf``. Load experimental x-ray PDF data and nickel structure file::
75+
76+
# Create new PDF calculator object.
77+
pf = PdfFit()
78+
79+
# Load experimental x-ray PDF data
80+
qmax = 30.0 # Q-cutoff used in PDF calculation in 1/A
81+
qdamp = 0.01 # instrument Q-resolution factor, responsible for PDF decay
82+
pf.read_data("Ni-xray.gr", "X", qmax, qdamp)
83+
84+
# Load nickel structure, must be in PDFFIT or DISCUS format
85+
pf.read_struct("Ni.stru")
86+
87+
3. Configure refinement and refine::
88+
89+
# Refine lattice parameters a, b, c.
90+
# Make them all equal to parameter @1.
91+
pf.constrain(pf.lat(1), "@1")
92+
pf.constrain(pf.lat(2), "@1")
93+
pf.constrain(pf.lat(3), "@1")
94+
# set initial value of parameter @1
95+
pf.setpar(1, pf.lat(1))
96+
97+
# Refine phase scale factor. Right side can have formulas.
98+
pf.constrain("pscale", "@20 * 2")
99+
pf.setpar(20, pf.getvar(pf.pscale) / 2.0)
100+
101+
# Refine PDF damping due to instrument Q-resolution.
102+
# Left side can be also passed as a reference to PdfFit object
103+
pf.constrain(pf.qdamp, "@21")
104+
pf.setpar(21, 0.03)
105+
106+
# Refine sharpening factor for correlated motion of close atoms.
107+
pf.constrain(pf.delta2, 22)
108+
pf.setpar(22, 0.0003)
109+
110+
# Set all temperature factors isotropic and equal to @4
111+
for idx in range(1, 5):
112+
pf.constrain(pf.u11(idx), "@4")
113+
pf.constrain(pf.u22(idx), "@4")
114+
pf.constrain(pf.u33(idx), "@4")
115+
pf.setpar(4, pf.u11(1))
116+
117+
# Refine all parameters
118+
pf.pdfrange(1, 1.5, 19.99)
119+
pf.refine()
120+
121+
4. Save the refined result::
122+
123+
pf.save_pdf(1, "Ni_refinement.fgr")
124+
pf.save_struct(1, "Ni_refinement.rstr")
125+
pf.save_res("Ni_refinement.res")
126+
127+
5. We can also plot it using matplotlib::
128+
129+
import matplotlib.pyplot as plt
130+
import numpy
131+
132+
# obtain data from PdfFit calculator object
133+
r = pf.getR()
134+
Gobs = pf.getpdf_obs()
135+
Gfit = pf.getpdf_fit()
136+
137+
# calculate difference curve
138+
Gdiff = numpy.array(Gobs) - numpy.array(Gfit)
139+
Gdiff_baseline = -10
140+
141+
plt.plot(r, Gobs, "ko")
142+
plt.plot(r, Gfit, "b-")
143+
plt.plot(r, Gdiff + Gdiff_baseline, "r-")
144+
145+
plt.xlabel("r (Å)")
146+
plt.ylabel("G (Å$^{-2}$)")
147+
plt.title("Fit of nickel to x-ray experimental PDF")
148+
149+
# display plot window, this must be the last command in the script
150+
plt.show()
151+
152+
The scripts can be downloaded :download:`here <examples/Ni_refinement.py>`.
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
"""Calculate PDF of FCC nickel and plot it using matplotlib.
4+
"""Calculate PDF of FCC nickel. Save data to Ni_calculation.cgr and plot it using matplotlib.
55
"""
66

7-
import pylab
7+
import matplotlib.pyplot as plt
88

99
from diffpy.pdffit2 import PdfFit
1010

1111
# create new PDF calculator object
1212
P = PdfFit()
1313

14+
# Load data ------------------------------------------------------------------
15+
1416
# load structure file in PDFFIT or DISCUS format
1517
P.read_struct("Ni.stru")
1618

19+
# Configure calculation ------------------------------------------------------
20+
1721
radiation_type = "X" # x-rays
1822
qmax = 30.0 # Q-cutoff used in PDF calculation in 1/A
1923
qdamp = 0.01 # instrument Q-resolution factor, responsible for PDF decay
@@ -23,17 +27,25 @@
2327

2428
# allocate and configure PDF calculation
2529
P.alloc(radiation_type, qmax, qdamp, rmin, rmax, npts)
30+
31+
# Calculate -------------------------------------------------------------------
32+
2633
P.calc()
2734

35+
# Save results ---------------------------------------------------------------
36+
37+
P.save_pdf(1, "Ni_calculation.cgr")
38+
39+
# Plot results ---------------------------------------------------------------
40+
2841
# obtain list of r-points and corresponding G values
2942
r = P.getR()
3043
G = P.getpdf_fit()
3144

32-
# pylab is matplotlib interface with MATLAB-like plotting commands
33-
pylab.plot(r, G)
34-
pylab.xlabel("r (Å)")
35-
pylab.ylabel("G (Å$^{-2}$)")
36-
pylab.title("x-ray PDF of nickel simulated at Qmax = %g" % qmax)
45+
plt.plot(r, G)
46+
plt.xlabel("r (Å)")
47+
plt.ylabel("G (Å$^{-2}$)")
48+
plt.title("x-ray PDF of nickel simulated at Qmax = %g" % qmax)
3749

3850
# display plot window, this must be the last command in the script
39-
pylab.show()
51+
plt.show()
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
Save fitted curve, refined structure and results summary.
66
"""
77

8-
import pylab
8+
import matplotlib.pyplot as plt
9+
import numpy
910

1011
from diffpy.pdffit2 import PdfFit
1112

@@ -65,24 +66,22 @@
6566

6667
# Plot results ---------------------------------------------------------------
6768

68-
# pylab is matplotlib interface with MATLAB-like plotting commands
6969
# obtain data from PdfFit calculator object
7070
r = pf.getR()
7171
Gobs = pf.getpdf_obs()
7272
Gfit = pf.getpdf_fit()
7373

74-
# calculate difference curve, with pylab arrays it can be done
75-
# without for loop
76-
Gdiff = pylab.array(Gobs) - pylab.array(Gfit)
74+
# calculate difference curve
75+
Gdiff = numpy.array(Gobs) - numpy.array(Gfit)
7776
Gdiff_baseline = -10
7877

79-
pylab.plot(r, Gobs, "ko")
80-
pylab.plot(r, Gfit, "b-")
81-
pylab.plot(r, Gdiff + Gdiff_baseline, "r-")
78+
plt.plot(r, Gobs, "ko")
79+
plt.plot(r, Gfit, "b-")
80+
plt.plot(r, Gdiff + Gdiff_baseline, "r-")
8281

83-
pylab.xlabel("r (Å)")
84-
pylab.ylabel("G (Å$^{-2}$)")
85-
pylab.title("Fit of nickel to x-ray experimental PDF")
82+
plt.xlabel("r (Å)")
83+
plt.ylabel("G (Å$^{-2}$)")
84+
plt.title("Fit of nickel to x-ray experimental PDF")
8685

8786
# display plot window, this must be the last command in the script
88-
pylab.show()
87+
plt.show()

doc/source/index.rst

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,60 @@
44

55
.. |title| replace:: diffpy.pdffit2 documentation
66

7-
diffpy.pdffit2 - PDFfit2 - real space structure refinement program..
7+
diffpy.pdffit2 - PDFfit2 - real space structure refinement program.
88

99
| Software version |release|.
1010
| Last updated |today|.
1111
12+
The diffpy.pdffit2 package provides functions for calculation and
13+
refinement of atomic Pair Distribution Function (PDF) from crystal
14+
structure model. It is used as a computational engine by PDFgui. All
15+
refinements possible in PDFgui can be done with diffpy.pdffit2,
16+
although less conveniently and with a fair knowledge of Python.
17+
The package includes an extension for the interactive `IPython
18+
<http://ipython.org>`_ shell, which tries to mimic the old PDFFIT
19+
program. To start IPython with this extension and also with plotting
20+
functions enabled, use ::
21+
22+
ipython --ext=diffpy.pdffit2.ipy_ext --pylab
23+
24+
The IPython extension is suitable for interactive use, however
25+
refinement scripts should be preferably written as a standard
26+
Python code. This is more reliable and needs only a few extra
27+
statements.
28+
1229
=======
1330
Authors
1431
=======
1532

16-
diffpy.pdffit2 is developed by Billinge Group
17-
and its community contributors.
33+
This code was derived from the first PDFFIT program by Thomas Proffen.
34+
The sources were converted to C++ by Jacques Bloch and then extensively hacked,
35+
extended and purged from most glaring bugs by Chris Farrow and Pavol Juhas.
36+
This code is currently maintained as part of the DiffPy project to create
37+
python modules for structure investigations from diffraction data.
38+
39+
The DiffPy team is located in the Billinge-group at the Applied Physics
40+
and Applied Mathematics Department of the Columbia University in New York.
41+
Previous significant contributors to this code were
42+
43+
Pavol Juhas, Chris Farrow, Jacques Bloch, Wenduo Zhou
1844

1945
For a detailed list of contributors see
2046
https://github.com/diffpy/diffpy.pdffit2/graphs/contributors.
2147

48+
49+
=========
50+
Reference
51+
=========
52+
53+
If you use this program for a scientific research that leads to publication,
54+
we ask that you acknowledge use of the program by citing the following paper
55+
in your publication:
56+
57+
C. L. Farrow, P. Juhás, J. W. Liu, D. Bryndin, E. S. Božin, J. Bloch, Th. Proffen
58+
and S. J. L. Billinge, PDFfit2 and PDFgui: computer programs for studying nanostructure
59+
in crystals (https://stacks.iop.org/0953-8984/19/335219), *J. Phys.: Condens. Matter*, 19, 335219 (2007)
60+
2261
============
2362
Installation
2463
============
@@ -34,6 +73,7 @@ Table of contents
3473

3574
license
3675
release
76+
examples
3777
Package API <api/diffpy.pdffit2>
3878

3979
=======

0 commit comments

Comments
 (0)