|
| 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>`. |
0 commit comments