| 1 |
############################################################################## |
| 2 |
# Copyright (c) 2009 Trustees of the Columbia University |
| 3 |
# in the City of New York. All rights reserved. |
| 4 |
# |
| 5 |
# DANSE Diffraction group, Simon J. L. Billinge |
| 6 |
# |
| 7 |
# File coded by: Pavol Juhas |
| 8 |
# |
| 9 |
# Redistribution and use in source and binary forms, with or without |
| 10 |
# modification, are permitted provided that the following conditions |
| 11 |
# are met: |
| 12 |
# |
| 13 |
# * Redistributions of source code must retain the above copyright |
| 14 |
# notice, this list of conditions and the following disclaimer. |
| 15 |
# |
| 16 |
# * Redistributions in binary form must reproduce the above copyright |
| 17 |
# notice, this list of conditions and the following disclaimer in the |
| 18 |
# documentation and/or other materials provided with the distribution. |
| 19 |
# |
| 20 |
# * Neither the name of Columbia University nor the names of its |
| 21 |
# contributors may be used to endorse or promote products derived from |
| 22 |
# this software without specific prior written permission. |
| 23 |
# |
| 24 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 25 |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 26 |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 27 |
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 28 |
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 29 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 30 |
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 31 |
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 32 |
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 33 |
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 34 |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 |
############################################################################## |
| 36 |
|
| 37 |
|
| 38 |
""" |
| 39 |
Cromer-Mann formula for calculating x-ray scattering factors. |
| 40 |
""" |
| 41 |
|
| 42 |
# module version |
| 43 |
__id__ = "$Id: cromermann.py 1051 2010-01-30 01:01:43Z juhas $" |
| 44 |
|
| 45 |
import os |
| 46 |
|
| 47 |
import numpy as np |
| 48 |
from numpy.typing import NDArray |
| 49 |
|
| 50 |
from . import core |
| 51 |
|
| 52 |
|
| 53 |
def getCMformula(symbol: str) -> "CromerMannFormula": |
| 54 |
""" |
| 55 |
Obtain Cromer-Mann formula and coefficients for a specified element. |
| 56 |
|
| 57 |
*symbol* : string |
| 58 |
symbol of an element |
| 59 |
|
| 60 |
Return instance of CromerMannFormula. |
| 61 |
""" |
| 62 |
if not _cmformulas: |
| 63 |
_update_cmformulas() |
| 64 |
return _cmformulas[symbol] |
| 65 |
|
| 66 |
|
| 67 |
def fxrayatq(symbol: str, Q: float|NDArray, charge: int|None=None) -> NDArray: |
| 68 |
""" |
| 69 |
Return x-ray scattering factors of an element at a given Q. |
| 70 |
|
| 71 |
*symbol* : string |
| 72 |
symbol of an element or ion, e.g., "Ca", "Ca2+" |
| 73 |
*Q* : float or [float] | |1/Ang| |
| 74 |
Q value |
| 75 |
*charge* : int |
| 76 |
ion charge, overrides any valence suffixes such as "-", "+", "3+". |
| 77 |
|
| 78 |
Return float or numpy array. |
| 79 |
""" |
| 80 |
stol = np.asarray(Q) / (4 * np.pi) |
| 81 |
rv = fxrayatstol(symbol, stol, charge) |
| 82 |
return rv |
| 83 |
|
| 84 |
|
| 85 |
def fxrayatstol(symbol: str, stol: float|NDArray, charge: int|None=None) -> NDArray: |
| 86 |
""" |
| 87 |
Calculate x-ray scattering factors at specified sin(theta)/lambda |
| 88 |
|
| 89 |
*symbol* : string |
| 90 |
symbol of an element or ion, e.g., "Ca", "Ca2+" |
| 91 |
*stol* : float or [float] | |1/Ang| |
| 92 |
sin(theta)/lambda |
| 93 |
*charge* : int |
| 94 |
ion charge, overrides any valence suffixes such as "-", "+", "3+". |
| 95 |
|
| 96 |
Return NDArray. |
| 97 |
""" |
| 98 |
# resolve lookup symbol smbl, by default symbol |
| 99 |
smbl = symbol |
| 100 |
# build standard element or ion symbol |
| 101 |
if charge is not None: |
| 102 |
smbl = symbol.rstrip('012345678+-') |
| 103 |
if charge: |
| 104 |
smbl += ("%+i" % charge)[::-1] |
| 105 |
# convert Na+ or Cl- to Na1+, Cl1- |
| 106 |
elif symbol[-1:] in '+-' and not symbol[-2:-1].isdigit(): |
| 107 |
smbl = (symbol[:-1] + "1" + symbol[-1:]) |
| 108 |
# smbl is resolved here |
| 109 |
cmf = getCMformula(smbl) |
| 110 |
rv = cmf.atstol(stol) |
| 111 |
return rv |
| 112 |
|
| 113 |
|
| 114 |
class CromerMannFormula: |
| 115 |
""" |
| 116 |
Cromer-Mann formula for x-ray scattering factors. |
| 117 |
Coefficient storage and evaluation. |
| 118 |
|
| 119 |
Class data: |
| 120 |
|
| 121 |
*stollimit* : float | |1/Ang| |
| 122 |
maximum sin(theta)/lambda for which the formula works |
| 123 |
|
| 124 |
Attributes: |
| 125 |
|
| 126 |
*symbol* : string |
| 127 |
symbol of an element |
| 128 |
*a* : [float] |
| 129 |
a-coefficients |
| 130 |
*b* : [float] |
| 131 |
b-coefficients |
| 132 |
*c* : float |
| 133 |
c-coefficient |
| 134 |
""" |
| 135 |
|
| 136 |
# obtained from tables/f0_WaasKirf.dat and the associated reference |
| 137 |
# D. Waasmaier, A. Kirfel, Acta Cryst. (1995). A51, 416-413 |
| 138 |
# http://dx.doi.org/10.1107/S0108767394013292 |
| 139 |
stollimit: float = 6 |
| 140 |
a: NDArray |
| 141 |
b: NDArray |
| 142 |
c: float |
| 143 |
symbol: str |
| 144 |
|
| 145 |
def __init__(self, symbol, a, b, c): |
| 146 |
""" |
| 147 |
Create a new instance of CromerMannFormula for specified element. |
| 148 |
|
| 149 |
No return value |
| 150 |
""" |
| 151 |
self.symbol = symbol |
| 152 |
self.a = np.asarray(a, dtype=float) |
| 153 |
self.b = np.asarray(b, dtype=float) |
| 154 |
self.c = float(c) |
| 155 |
|
| 156 |
def atstol(self, stol: float|NDArray) -> NDArray: |
| 157 |
""" |
| 158 |
Calculate x-ray scattering factors at specified sin(theta)/lambda |
| 159 |
|
| 160 |
*stol* : float or [float] | |1/Ang| |
| 161 |
sin(theta)/lambda |
| 162 |
|
| 163 |
Return NDArray. |
| 164 |
""" |
| 165 |
stolflat = np.asarray(stol).flatten() |
| 166 |
n = len(stolflat) |
| 167 |
stol2row = np.reshape(stolflat ** 2, (1, n)) |
| 168 |
bcol = self.b.reshape((len(self.a), 1)) |
| 169 |
bstol2 = np.dot(bcol, stol2row) |
| 170 |
adiag = np.diag(self.a) |
| 171 |
rvrows = np.dot(adiag, np.exp(-bstol2)) |
| 172 |
rvflat = rvrows.sum(axis=0) + self.c |
| 173 |
rvflat[stolflat > self.stollimit] = np.nan |
| 174 |
# when stol is scalar, addition of zero converts the rv array to float |
| 175 |
rv = rvflat.reshape(np.shape(stol)) + 0.0 |
| 176 |
return rv |
| 177 |
|
| 178 |
# class CromerMannFormula |
| 179 |
|
| 180 |
|
| 181 |
def _update_cmformulas() -> None: |
| 182 |
""" |
| 183 |
Update the static dictionary of CromerMannFormula instances. |
| 184 |
""" |
| 185 |
path = os.path.join(core.get_data_path('.'), 'f0_WaasKirf.dat') |
| 186 |
# Data file contains: |
| 187 |
# #{C,D,F,UD,UO,UT,UIDL,UF0TYPE} header lines |
| 188 |
# |
| 189 |
# #S <Z> <El>(#[+-])? data blocks |
| 190 |
# #N 11 |
| 191 |
# #L a1 a2 a3 a4 a5 c b1 b2 b3 b4 b5 |
| 192 |
# <a1 a2 a3 a4 a5> <c> <b1 b2 b3 b4 b5> |
| 193 |
symbol = None |
| 194 |
with open(path) as fp: |
| 195 |
for line in fp: |
| 196 |
if line.startswith("#S"): |
| 197 |
symbol = line.split()[2] |
| 198 |
elif line.startswith(" "): |
| 199 |
if symbol is None: |
| 200 |
raise RuntimeError(f"Should not be here; {path} has been corrupted.") |
| 201 |
w = line.split() |
| 202 |
a = list(map(float, w[0:5])) |
| 203 |
b = list(map(float, w[6:11])) |
| 204 |
c = float(w[5]) |
| 205 |
cmf = CromerMannFormula(symbol, a, b, c) |
| 206 |
_cmformulas[cmf.symbol] = cmf |
| 207 |
symbol = None |
| 208 |
|
| 209 |
_cmformulas: dict[str, CromerMannFormula] = {} |
| 210 |
|
| 211 |
# End of file |