|
| 1 | +#!/usr/bin/env python |
| 2 | +############################################################################## |
| 3 | +# |
| 4 | +# diffpy.srreal Complex Modeling Initiative |
| 5 | +# (c) 2016 Brookhaven Science Associates, |
| 6 | +# Brookhaven National Laboratory. |
| 7 | +# All rights reserved. |
| 8 | +# |
| 9 | +# File coded by: Pavol Juhas |
| 10 | +# |
| 11 | +# See AUTHORS.txt for a list of people who contributed. |
| 12 | +# See LICENSE.txt for license information. |
| 13 | +# |
| 14 | +############################################################################## |
| 15 | + |
| 16 | + |
| 17 | +"""\ |
| 18 | +Compositional averaging of atom scattering factors. |
| 19 | +
|
| 20 | +
|
| 21 | +Examples |
| 22 | +-------- |
| 23 | +:: |
| 24 | +
|
| 25 | + import numpy as np |
| 26 | + from diffpy.srreal.scatteringfactortable import SFTXray |
| 27 | + xtb = SFTXray() |
| 28 | + qa = np.linspace(0, 25) |
| 29 | + sfavg1 = SFAverage.fromComposition(xtb, {'Na' : 1, 'Cl' : 1}, qa) |
| 30 | + sfavg2 = SFAverage.fromComposition(xtb, [('Mn', 1), ('O', 2)], qa) |
| 31 | +
|
| 32 | + from diffpy.Structure import loadStructure |
| 33 | + from diffpy.srreal.pdfcalculator import DebyePDFCalculator |
| 34 | + dpdfc = DebyePDFCalculator() |
| 35 | + dpdfc(loadStructure('rutile.cif')) |
| 36 | + sfavg3 = SFAverage.fromStructure(dpdfc.scatteringfactortable, |
| 37 | + dpdfc.getStructure(), dpdfc.qgrid) |
| 38 | +""" |
| 39 | + |
| 40 | + |
| 41 | +# class SFAverage ------------------------------------------------------------ |
| 42 | + |
| 43 | +class SFAverage(object): |
| 44 | + """\ |
| 45 | + Calculate compositional statistics of atom scattering factors. |
| 46 | +
|
| 47 | + Compositional averages can be calculated for an array of Q-values. |
| 48 | + Results are stored in the class attributes. |
| 49 | +
|
| 50 | + Attributes |
| 51 | + ---------- |
| 52 | + f1sum : |
| 53 | + Sum of scattering factors from all atoms. |
| 54 | + Float or NumPy array. |
| 55 | + f2sum : |
| 56 | + Sum of squared scattering factors from all atoms. |
| 57 | + Float or NumPy array. |
| 58 | + count : |
| 59 | + Total number of atoms. Can be non-integer in case of |
| 60 | + fractional occupancies. |
| 61 | + f1avg : |
| 62 | + Compositional average of scattering factors. |
| 63 | + Float or NumPy array. |
| 64 | + f2avg : |
| 65 | + Compositional average of squared scattering factors. |
| 66 | + Float or NumPy array. |
| 67 | + composition : |
| 68 | + Dictionary of atom symbols and their total abundancies. |
| 69 | + """ |
| 70 | + |
| 71 | + f1sum = 0 |
| 72 | + f2sum = 0 |
| 73 | + count = 0 |
| 74 | + f1avg = 0 |
| 75 | + f2avg = 0 |
| 76 | + composition = None |
| 77 | + |
| 78 | + @classmethod |
| 79 | + def fromStructure(cls, sftb, stru, q=0): |
| 80 | + """\ |
| 81 | + Calculate average scattering factors from a structure object. |
| 82 | +
|
| 83 | + Parameters |
| 84 | + ---------- |
| 85 | + sftb : ScatteringFactorTable |
| 86 | + The ScatteringFactorTable object for looking up the values. |
| 87 | + stru : diffpy Structure or pyobjcryst Crystal or StructureAdapter |
| 88 | + The structure object that stores the atom species and their |
| 89 | + occupancies. Can be any type with a registered conversion |
| 90 | + to the StructureAdapter class. |
| 91 | + q : float or NumPy array (optional) |
| 92 | + The Q value in inverse Angstroms for which to lookup |
| 93 | + the scattering factor values. |
| 94 | +
|
| 95 | + See also |
| 96 | + -------- |
| 97 | + RegisterStructureAdapter : to add support for more structure types. |
| 98 | +
|
| 99 | + Returns |
| 100 | + ------- |
| 101 | + SFAverage |
| 102 | + The calculated scattering factor averages. |
| 103 | + """ |
| 104 | + # a bit of duck-typing for faster handling of diffpy.Structure |
| 105 | + if hasattr(type(stru), 'composition'): |
| 106 | + composition = stru.composition |
| 107 | + if isinstance(composition, dict): |
| 108 | + return cls.fromComposition(sftb, composition, q) |
| 109 | + # otherwise let's convert to a known structure type |
| 110 | + from diffpy.srreal.structureadapter import createStructureAdapter |
| 111 | + adpt = createStructureAdapter(stru) |
| 112 | + composition = {} |
| 113 | + for i in range(adpt.countSites()): |
| 114 | + smbl = adpt.siteAtomType(i) |
| 115 | + cnt = adpt.siteOccupancy(i) * adpt.siteMultiplicity(i) |
| 116 | + composition[smbl] = composition.get(smbl, 0) + cnt |
| 117 | + return cls.fromComposition(sftb, composition, q) |
| 118 | + |
| 119 | + |
| 120 | + @classmethod |
| 121 | + def fromComposition(cls, sftb, composition, q=0): |
| 122 | + """\ |
| 123 | + Calculate average scattering factors from atom concentrations. |
| 124 | +
|
| 125 | + Parameters |
| 126 | + ---------- |
| 127 | + sftb : ScatteringFactorTable |
| 128 | + The ScatteringFactorTable object for looking up the values. |
| 129 | + composition : dictionary or a list of (symbol, amount) pairs. |
| 130 | + The chemical composition for evaluating the average. Atom |
| 131 | + symbols may repeat when it is a list of (symbol, amount) pairs. |
| 132 | + q : float or NumPy array (optional) |
| 133 | + The Q value in inverse Angstroms for which to lookup |
| 134 | + the scattering factor values. |
| 135 | +
|
| 136 | + Returns |
| 137 | + ------- |
| 138 | + SFAverage |
| 139 | + The calculated scattering factor averages. |
| 140 | + """ |
| 141 | + sfa = cls() |
| 142 | + sfa.composition = {} |
| 143 | + if isinstance(composition, dict): |
| 144 | + sfa.composition.update(composition) |
| 145 | + else: |
| 146 | + for smbl, cnt in composition: |
| 147 | + if not smbl in sfa.composition: |
| 148 | + sfa.composition[smbl] = 0 |
| 149 | + sfa.composition[smbl] += cnt |
| 150 | + sfa.f1sum = 0.0 * q |
| 151 | + sfa.f2sum = 0.0 * q |
| 152 | + for smbl, cnt in sfa.composition.items(): |
| 153 | + sfq = sftb.lookup(smbl, q) |
| 154 | + sfa.f1sum += cnt * sfq |
| 155 | + sfa.f2sum += cnt * sfq**2 |
| 156 | + sfa.count += cnt |
| 157 | + denom = sfa.count if sfa.count > 0 else 1 |
| 158 | + sfa.f1avg = sfa.f1sum / denom |
| 159 | + sfa.f2avg = sfa.f2sum / denom |
| 160 | + return sfa |
| 161 | + |
| 162 | +# End of class SFAverage |
0 commit comments