Debian Codegraph @

periodictable/xsf.py

raw
1 # This program is public domain
2 # Author: Paul Kienzle
3 r"""
4 This module has one class and nine fuctions.
5
6 :class:`Xray`
7 X-ray scattering properties for the elements.
8
9 The following attributes are added to each element:
10
11 :func:`Xray.sftable`
12 Three column table of energy vs. scattering factors f1, f2.
13
14 :func:`Xray.scattering_factors`
15 Returns f1, f2, the X-ray scattering factors for the given wavelengths
16 interpolated from sftable.
17
18 :func:`Xray.f0`
19 Returns f0 for the given vector Q, with Q[i] in $[0, 24\pi]$ |1/Ang|.
20
21 :func:`Xray.sld`
22 Returns scattering length density (*real*, *imaginary*) for the
23 given wavelengths or energies.
24
25 The following functions are available for X-ray scatting information processing:
26
27 :func:`xray_wavelength`
28 Finds X-ray wavelength in angstroms given energy in keV.
29
30 :func:`xray_energy`
31 Finds X-ray energy in keV given wavelength in angstroms.
32
33 :func:`init`
34 Initializes a periodic table with the Lawrence Berkeley Laboratory
35 Center for X-Ray Optics xray scattering factors.
36
37 :func:`init_spectral_lines`
38 Sets the K_alpha and K_beta1 wavelengths for select elements.
39
40 :func:`sld_table`
41 Prints the xray SLD table for the given wavelength.
42
43 :func:`xray_sld`
44 Computes xray scattering length densities for molecules.
45
46 :func:`index_of_refraction`
47 Express xray scattering length density as an index of refraction
48
49 :func:`mirror_reflectivity`
50 X-ray reflectivity from a mirror made of a single compound.
51
52 :func:`xray_sld_from_atoms`
53 The underlying scattering length density calculator. This works with
54 a dictionary of atoms and quantities directly.
55
56 :func:`emission_table`
57 Prints a table of emission lines.
58
59 K_alpha, K_beta1 (|Ang|):
60 X-ray emission lines for elements beyond neon, with
61 $K_\alpha = (2 K_{\alpha 1} + K_{\alpha 2})/3$.
62
63 X-ray scattering factors:
64 Low-Energy X-ray Interaction Coefficients: Photoabsorption, scattering
65 and reflection for E in 30 to 30,000 eV, and Z in 1 to 92.
66
67 .. Note::
68
69 For :ref:`custom tables <custom-table>`, use :func:`init` and
70 :func:`init_spectral_lines` to set the data.
71
72
73 Emission line tables
74 ====================
75
76 Data for the $K_\alpha$ and $K_\beta$ lines comes from
77 [#Deslattes2003]_, with the full tables available at
78 `<http://www.nist.gov/pml/data/xraytrans/index.cfm>`_.
79 Experimental Values are used, truncated to 4 digits
80 of precision to correspond to the values for the subset
81 of elements previously defined in the periodictable package.
82
83 X-ray f1 and f2 tables
84 ======================
85 The data for the tables is stored in the ``periodictable/xsf``.
86 directory. The following information is from ``periodictable/xsf/read.me``,
87 with minor formatting changes:
88
89 These ``[*.nff]`` files were used to generate the tables published in
90 reference [#Henke1993]_. The files contain three columns of data:
91
92 Energy(eV), *f_1*, *f_2*,
93
94 where *f_1* and *f_2* are the atomic (forward) scattering factors.
95 There are 500+ points on a uniform logarithmic mesh with points
96 added 0.1 eV above and below "sharp" absorption edges. The
97 tabulated values of *f_1* contain a relativistic, energy
98 independent, correction given by:
99
100 .. math::
101
102 Z^* = Z - (Z/82.5)^{2.37}
103
104 .. Note::
105 Below 29 eV *f_1* is set equal to -9999.
106
107 The atomic photoabsorption cross section, $\mu_a$, may be readily
108 obtained from the values of $f_2$ using the relation:
109
110 .. math::
111
112 \mu_a = 2 r_e \lambda f_2
113
114 where $r_e$ is the classical electron radius, and $\lambda$ is
115 the wavelength. The index of refraction for a material with *N* atoms per
116 unit volume is calculated by:
117
118 .. math::
119
120 n = 1 - N r_e \lambda^2 (f_1 + i f_2)/(2 \pi).
121
122 These (semi-empirical) atomic scattering factors are based upon
123 photoabsorption measurements of elements in their elemental state.
124 The basic assumption is that condensed matter may be modeled as a
125 collection of non-interacting atoms. This assumption is in general
126 a good one for energies sufficiently far from absorption thresholds.
127 In the threshold regions, the specific chemical state is important
128 and direct experimental measurements must be made.
129
130 These tables are based on a compilation of the available experimental
131 measurements and theoretical calculations. For many elements there is
132 little or no published data and in such cases it was necessary to
133 rely on theoretical calculations and interpolations across Z.
134 In order to improve the accuracy in the future considerably more
135 experimental measurements are needed.
136
137 Note that the following elements have been updated since the
138 publication of Ref. [#Henke1993]_ in July 1993. Check
139 `<http://henke.lbl.gov/optical_constants/update.html>`_ for more
140 recent updates.
141
142 .. table::
143
144 ======== ========== =================
145 Element Updated Energy Range (eV)
146 ======== ========== =================
147 Pt Aug 2023 0.1-800
148 Cr Aug 2023 0.4-813
149 Nb Aug 2023 10-1000
150 Y Aug 2023 50-1300
151 Er Aug 2023 3-1580
152 Mg Jan 2011 10-1300
153 Zr Apr 2010 20-1000
154 La Jun 2007 14-440
155 Gd Jun 2007 12-450
156 Sc Apr 2006 50-1300
157 Ti Aug 2004 20-150
158 Ru Aug 2004 40-1300
159 W Aug 2004 35-250
160 Mo Aug 2004 25-60
161 Be Aug 2004 40-250
162 Mo Nov 1997 10-930
163 Fe Oct 1995 600-800
164 Si Jun 1995 30-500
165 Au Jul 1994 2000-6500
166 Mg,Al,Si Jan 1994 30-200
167 Li Nov 1994 2000-30000
168 ======== ========== =================
169
170
171 Data available at:
172 #. http://henke.lbl.gov/optical_constants/asf.html
173
174 .. [#Henke1993] B. L. Henke, E. M. Gullikson, and J. C. Davis. "X-ray interactions:
175 photoabsorption, scattering, transmission, and reflection at E=50-30000 eV,
176 Z=1-92", Atomic Data and Nuclear Data Tables 54 no.2, 181-342 (July 1993).
177
178 .. [#Deslattes2003] R. D. Deslattes, E. G. Kessler, Jr., P. Indelicato, L. de Billy,
179 E. Lindroth, and J. Anton. Rev. Mod. Phys. 75, 35-99 (2003).
180 """
181 __all__ = ['Xray', 'init', 'init_spectral_lines',
182 'xray_energy', 'xray_wavelength',
183 'xray_sld', 'xray_sld_from_atoms',
184 'emission_table', 'sld_table', 'plot_xsf',
185 'index_of_refraction', 'mirror_reflectivity',
186 ]
187 import os.path
188
189 import numpy as np
190 from numpy import nan, pi, exp, sin, cos, sqrt, radians
191
192 from .core import Element, Ion, default_table, get_data_path
193 from .constants import (
194 avogadro_number, planck_constant, speed_of_light, electron_volt,
195 electron_radius)
196
197 def xray_wavelength(energy):
198 r"""
199 Convert X-ray energy to wavelength.
200
201 :Parameters:
202 *energy* : float or vector | keV
203
204 :Returns:
205 *wavelength* : float | |Ang|
206
207 Energy can be converted to wavelength using
208
209 .. math::
210
211 \lambda = h c / E
212
213 where:
214
215 $h$ = Planck constant in J\ |cdot|\ s
216
217 $c$ = speed of light in m/s
218 """
219 return planck_constant/electron_volt*speed_of_light/np.asarray(energy)*1e7
220
221 def xray_energy(wavelength):
222 r"""
223 Convert X-ray wavelength to energy.
224
225 :Parameters:
226 *wavelength* : float or vector | |Ang|
227
228 :Returns:
229 *energy* : float or vector | keV
230
231 Wavelength can be converted to energy using
232
233 .. math::
234
235 E = h c / \lambda
236
237 where:
238
239 $h$ = Planck constant in J\ |cdot|\ s
240
241 $c$ = speed of light in m/s
242 """
243 return planck_constant/electron_volt*speed_of_light/np.asarray(wavelength)*1e7
244
245 class Xray:
246 """
247 X-ray scattering properties for the elements. Refer help(periodictable.xsf)
248 from command prompt for details.
249 """
250 sftable_units: tuple[str, str, str] = ("eV", "", "")
251 scattering_factors_units: tuple[str, str] = ("", "")
252 sld_units: tuple[str, str] = ("1e-6/Ang^2", "1e-6/Ang^2")
253 element: Element
254 _nff_path = get_data_path('xsf')
255 _table = None
256 def __init__(self, element):
257 self.element = element
258
259 def _gettable(self):
260 if self._table is None:
261 # Load table when necessary; note there is no table for
262 # neutrons (n), and lowercase nitrogen=> n.nff, so it must
263 # be checked for explicitly.
264 filename = os.path.join(self._nff_path,
265 self.element.symbol.lower()+".nff")
266 if self.element.symbol != 'n' and os.path.exists(filename):
267 xsf = np.loadtxt(filename, skiprows=1).T
268 xsf[1, xsf[1] == -9999.] = np.nan
269 xsf[0] *= 0.001 # Use keV in table rather than eV
270 self._table = xsf
271 return self._table
272 sftable = property(_gettable, doc="X-ray scattering factor table (E,f1,f2)")
273
274 def scattering_factors(self, *, energy=None, wavelength=None):
275 """
276 X-ray scattering factors f', f''.
277
278 :Parameters:
279 *energy* : float or vector | keV
280 X-ray energy.
281
282 :Returns:
283 *scattering_factors* : (float, float)
284 Values outside the range return NaN.
285
286 Values are found from linear interpolation within the Henke Xray
287 scattering factors database at the Lawrence Berkeley Laboratory
288 Center for X-ray Optics.
289 """
290 xsf = self.sftable
291 if xsf is None:
292 return None, None
293
294 if wavelength is not None:
295 energy = xray_energy(wavelength)
296 if energy is None:
297 raise TypeError('X-ray scattering factors need wavelength or energy')
298
299 scalar = np.isscalar(energy)
300 if scalar:
301 energy = np.array([energy])
302 f1 = np.interp(energy, xsf[0], xsf[1], left=nan, right=nan)
303 f2 = np.exp(np.interp(
304 np.log(energy), np.log(xsf[0]), np.log(xsf[2]),
305 left=nan, right=nan))
306 if scalar:
307 f1, f2 = f1[0], f2[0]
308 return f1, f2
309
310 def f0(self, Q):
311 r"""
312 Isotropic X-ray scattering factors *f0* for the input Q.
313
314 :Parameters:
315 *Q* : float or vector in $[0, 24\pi]$ | |1/Ang|
316 X-ray scattering properties for the elements.
317
318 :Returns:
319 *f0* : float
320 Values outside the valid range return NaN.
321
322
323 .. Note::
324
325 *f0* is often given as a function of $\sin(\theta)/\lambda$
326 whereas we are using $Q = 4 \pi \sin(\theta)/\lambda$, or
327 in terms of energy $Q = 4 \pi \sin(\theta) E/(h c)$.
328
329 Reference:
330 D. Wassmaier, A. Kerfel, Acta Crystallogr. A51 (1995) 416.
331 http://dx.doi.org/10.1107/S0108767394013292
332 """
333 from . import cromermann
334 f = cromermann.fxrayatq(Q=Q,
335 symbol=self.element.symbol,
336 charge=self.element.charge)
337 return f
338
339 def sld(self, *, wavelength=None, energy=None):
340 r"""
341 X ray scattering length density.
342
343 :Parameters:
344 *wavelength* : float or vector | |Ang|
345 Wavelength of the X-ray.
346
347 *energy* : float or vector | keV
348 Energy of the X-ray (if *wavelength* not specified).
349
350 .. note:
351 Only one of *wavelength* and *energy* is needed.
352
353 :Returns:
354 *sld* : (float, float) | |1/Ang^2|
355 (*real*, *imaginary*) X-ray scattering length density.
356
357 :Raises:
358 *TypeError* : neither *wavelength* nor *energy* was specified.
359
360 The scattering length density is $r_e N (f_1 + i f_2)$.
361 where $r_e$ is the electron radius and $N$ is the
362 number density. The number density is $N = \rho_m/m N_A$,
363 with mass density $\rho_m$ molar mass $m$ and
364 Avogadro's number $N_A$.
365
366 The constants are available directly:
367
368 $r_e$ = periodictable.xsf.electron_radius
369
370 $N_A$ = periodictable.constants.avogadro_number
371
372 Data comes from the Henke Xray scattering factors database at the
373 Lawrence Berkeley Laboratory Center for X-ray Optics.
374 """
375 f1, f2 = self.scattering_factors(wavelength=wavelength, energy=energy)
376 if f1 is None or self.element.number_density is None:
377 return None, None
378 rho = f1*electron_radius*self.element.number_density*1e-8
379 irho = f2*electron_radius*self.element.number_density*1e-8
380 return rho, irho
381
382 # Note: docs and function prototype are reproduced in __init__
383 def xray_sld(compound, *, density=None, natural_density=None,
384 wavelength=None, energy=None):
385 """
386 Compute xray scattering length densities for molecules.
387
388 :Parameters:
389 *compound* : Formula initializer
390 Chemical formula.
391 *density* : float | |g/cm^3|
392 Mass density of the compound, or None for default.
393 *natural_density* : float | |g/cm^3|
394 Mass density of the compound at naturally occurring isotope abundance.
395 *wavelength* : float or vector | |Ang|
396 Wavelength of the X-ray.
397 *energy* : float or vector | keV
398 Energy of the X-ray, if *wavelength* is not specified.
399
400 :Returns:
401 *sld* : (float, float) | |1e-6/Ang^2|
402 (*real*, *imaginary*) scattering length density.
403
404 :Raises:
405 *AssertionError* : *density* or *wavelength*/*energy* is missing.
406 """
407 from . import formulas
408 compound = formulas.formula(compound, density=density,
409 natural_density=natural_density)
410 assert compound.density is not None, "scattering calculation needs density"
411
412 if wavelength is not None:
413 energy = xray_energy(wavelength)
414 assert energy is not None, "scattering calculation needs energy or wavelength"
415
416 mass, sum_f1, sum_f2 = 0, 0, 0
417 for element, quantity in compound.atoms.items():
418 mass += element.mass*quantity
419 f1, f2 = element.xray.scattering_factors(energy=energy)
420 if f1 is None:
421 raise ValueError(f"X-ray scattering factors not available for {element}")
422 #print element, f1, f2, wavelength
423 sum_f1 += f1*quantity
424 sum_f2 += f2*quantity
425
426 if mass == 0: # because the formula is empty
427 return 0, 0
428
429 N = (compound.density/mass*avogadro_number*1e-8)
430 rho = N*sum_f1*electron_radius
431 irho = N*sum_f2*electron_radius
432 return rho, irho
433
434
435 def index_of_refraction(compound, *, density=None, natural_density=None,
436 energy=None, wavelength=None):
437 """
438 Calculates the index of refraction for a given compound
439
440 :Parameters:
441 *compound* : Formula initializer
442 Chemical formula.
443 *density* : float | |g/cm^3|
444 Mass density of the compound, or None for default.
445 *natural_density* : float | |g/cm^3|
446 Mass density of the compound at naturally occurring isotope abundance.
447 *wavelength* : float or vector | |Ang|
448 Wavelength of the X-ray.
449 *energy* : float or vector | keV
450 Energy of the X-ray, if *wavelength* is not specified.
451
452 :Returns:
453 *n* : float or vector | unitless
454 index of refraction of the material at the given energy
455
456 :Notes:
457
458 Formula taken from http://xdb.lbl.gov (section 1.7) and checked
459 against http://henke.lbl.gov/optical_constants/getdb2.html
460 """
461 if energy is not None:
462 wavelength = xray_wavelength(energy)
463 assert wavelength is not None, "scattering calculation needs energy or wavelength"
464 f1, f2 = xray_sld(compound,
465 density=density, natural_density=natural_density,
466 wavelength=wavelength)
467 return 1 - wavelength**2/(2*pi)*(f1 + f2*1j)*1e-6
468
469 def delta(compound, *, density=None, natural_density=None,
470 energy=None, wavelength=None):
471 """
472 Calculates the δ component of the index of refraction for a given compound
473
474 :Parameters:
475 *compound* : Formula initializer
476 Chemical formula.
477 *density* : float | |g/cm^3|
478 Mass density of the compound, or None for default.
479 *natural_density* : float | |g/cm^3|
480 Mass density of the compound at naturally occurring isotope abundance.
481 *wavelength* : float or vector | |Ang|
482 Wavelength of the X-ray.
483 *energy* : float or vector | keV
484 Energy of the X-ray, if *wavelength* is not specified.
485
486 :Returns:
487 *delta* : float or vector | unitless
488 δ component of the index of refraction of the material at the given energy
489
490 :Notes:
491
492 Formula taken from http://xdb.lbl.gov (section 1.7) and checked
493 against http://henke.lbl.gov/optical_constants/getdb2.html.
494 """
495 if energy is not None:
496 wavelength = xray_wavelength(energy)
497 assert wavelength is not None, "scattering calculation needs energy or wavelength"
498 f1, f2 = xray_sld(compound,
499 density=density, natural_density=natural_density,
500 wavelength=wavelength)
501 return wavelength**2/(2*pi)*(f1)*1e-6
502
503 def beta(compound, *, density=None, natural_density=None,
504 energy=None, wavelength=None):
505 r"""
506 Calculates the β component of the index of refraction for a given compound
507
508 :Parameters:
509 *compound* : Formula initializer
510 Chemical formula.
511 *density* : float | |g/cm^3|
512 Mass density of the compound, or None for default.
513 *natural_density* : float | |g/cm^3|
514 Mass density of the compound at naturally occurring isotope abundance.
515 *wavelength* : float or vector | |Ang|
516 Wavelength of the X-ray.
517 *energy* : float or vector | keV
518 Energy of the X-ray, if *wavelength* is not specified.
519
520 :Returns:
521 *beta* : float or vector | unitless
522 β component of the index of refraction of the material at the given energy
523
524 :Notes:
525
526 Formula taken from the equation
527
528 .. math::
529
530 n = 1 - \delta + i \beta
531
532 derivative of the formula http://xdb.lbl.gov (section 1.7) using a more common
533 sign convention for the imaginary part of the index of refraction. Checked
534 against http://henke.lbl.gov/optical_constants/getdb2.html.
535 """
536 if energy is not None:
537 wavelength = xray_wavelength(energy)
538 assert wavelength is not None, "scattering calculation needs energy or wavelength"
539 f1, f2 = xray_sld(compound,
540 density=density, natural_density=natural_density,
541 wavelength=wavelength)
542 return wavelength**2/(2*pi)*(f2)*1e-6
543
544 def mirror_reflectivity(compound, *, density=None, natural_density=None,
545 energy=None, wavelength=None,
546 angle=None, roughness=0):
547 """
548 Calculates reflectivity of a thick mirror as function of energy and angle
549
550 :Parameters:
551 *compound* : Formula initializer
552 Chemical formula.
553 *density* : float | |g/cm^3|
554 Mass density of the compound, or None for default.
555 *natural_density* : float | |g/cm^3|
556 Mass density of the compound at naturally occurring isotope abundance.
557 *roughness* : float | |Ang|
558 High-spatial-frequency surface roughness.
559 *wavelength* : float or vector | |Ang|
560 Wavelength of the X-ray.
561 *energy* : float or vector | keV
562 Energy of the X-ray, if *wavelength* is not specified.
563 *angle* : vector | |deg|
564 Incident beam angles.
565
566 :Returns:
567 *reflectivity* : matrix
568 matrix of reflectivity as function of (angle, energy)
569
570 :Notes:
571
572 Formula taken from http://xdb.lbl.gov (section 4.2) and checked
573 against http://henke.lbl.gov/optical_constants/mirror2.html
574 """
575 if energy is not None:
576 wavelength = xray_wavelength(energy)
577 assert wavelength is not None, "scattering calculation needs energy or wavelength"
578 assert angle is not None, "scattering calculation needs incident angle"
579 angle = radians(angle)
580 if np.isscalar(wavelength):
581 wavelength = np.array([wavelength])
582 if np.isscalar(angle):
583 angle = np.array([angle])
584 nv = index_of_refraction(compound=compound,
585 density=density, natural_density=natural_density,
586 wavelength=wavelength)
587 ki = 2*pi/wavelength[None, :] * sin(angle[:, None])
588 kf = 2*pi/wavelength[None, :] * sqrt(nv[None, :]**2 - cos(angle[:, None])**2)
589 r = (ki-kf)/(ki+kf)*exp(-2*ki*kf*roughness**2)
590 return abs(r)**2
591
592
593 def xray_sld_from_atoms(*args, **kw):
594 """
595 .. deprecated:: 0.91
596
597 :func:`xray_sld` now accepts a dictionary of *{atom: count}* directly.
598 """
599 return xray_sld(*args, **kw)
600
601
602 spectral_lines_data = """\
603 Ac 0.1380 0.1205
604 Ag 0.5608 0.4970
605 Al 8.3402 7.9601
606 Am 0.1181 0.1030
607 Ar 4.1929 3.8860
608 As 1.1772 1.0573
609 At 0.1537 0.1343
610 Au 0.1818 0.1589
611 Ba 0.3866 0.3408
612 Bi 0.1624 0.1419
613 Bk 0.1122 0.0979
614 Br 1.0411 0.9328
615 Ca 3.3595 3.0897
616 Cd 0.5364 0.4751
617 Ce 0.3586 0.3158
618 Cf 0.1094 0.0954
619 Cl 4.7287 4.4034
620 Cm 0.1151 0.1004
621 Co 1.7902 1.6208
622 Cr 2.2910 2.0848
623 Cs 0.4018 0.3543
624 Cu 1.5418 1.3922
625 Dy 0.2711 0.2378
626 Er 0.2539 0.2226
627 Es 0.1067 0.0930
628 Eu 0.3000 0.2635
629 Fe 1.9373 1.7566
630 Fm 0.1040 0.0907
631 Fr 0.1456 0.1271
632 Ga 1.3414 1.2079
633 Gd 0.2899 0.2546
634 Ge 1.2553 1.1289
635 Hf 0.2238 0.1960
636 Hg 0.1767 0.1544
637 Ho 0.2623 0.2301
638 I 0.4348 0.3839
639 In 0.5136 0.4545
640 Ir 0.1926 0.1685
641 K 3.7423 3.4539
642 Kr 0.9816 0.8785
643 La 0.3722 0.3279
644 Lu 0.2308 0.2023
645 Mg 9.8902 9.5211
646 Mn 2.1031 1.9102
647 Mo 0.7107 0.6323
648 Na 11.9103 11.5752
649 Nb 0.7476 0.6657
650 Nd 0.3333 0.2933
651 Ne 14.6102 14.4522
652 Ni 1.6592 1.5001
653 Np 0.1243 0.1085
654 Os 0.1984 0.1736
655 P 6.1581 5.7961
656 Pa 0.1310 0.1143
657 Pb 0.1670 0.1459
658 Pd 0.5869 0.5205
659 Pm 0.3217 0.2829
660 Po 0.1580 0.1380
661 Pr 0.3456 0.3042
662 Pt 0.1871 0.1636
663 Pu 0.1212 0.1057
664 Ra 0.1417 0.1238
665 Rb 0.9269 0.8286
666 Re 0.2043 0.1788
667 Rh 0.6147 0.5456
668 Rn 0.1496 0.1306
669 Ru 0.6445 0.5724
670 S 5.3731 5.0316
671 Sb 0.4718 0.4170
672 Sc 3.0320 2.7796
673 Se 1.1061 0.9921
674 Si 7.1263 6.7531
675 Sm 0.3105 0.2730
676 Sn 0.4920 0.4352
677 Sr 0.8766 0.7829
678 Ta 0.2171 0.1900
679 Tb 0.2802 0.2460
680 Tc 0.6764 0.6013
681 Te 0.4527 0.4000
682 Th 0.1344 0.1174
683 Ti 2.7497 2.5139
684 Tl 0.1717 0.1501
685 Tm 0.2459 0.2155
686 U 0.1276 0.1114
687 V 2.5048 2.2844
688 W 0.2106 0.1843
689 Xe 0.4178 0.3687
690 Y 0.8302 0.7407
691 Yb 0.2382 0.2088
692 Zn 1.4364 1.2952
693 Zr 0.7873 0.7018\
694 """
695
696 def init_spectral_lines(table):
697 """
698 Sets the K_alpha and K_beta1 wavelengths for select elements
699 """
700 Element.K_alpha_units = "angstrom"
701 Element.K_beta1_units = "angstrom"
702 for row in spectral_lines_data.split('\n'):
703 el, K_alpha, K_beta1 = row.split()
704 el = table.symbol(el)
705 el.K_alpha = float(K_alpha)
706 el.K_beta1 = float(K_beta1)
707
708 def init(table, reload=False):
709
710 if 'xray' in table.properties and not reload:
711 return
712 table.properties.append('xray')
713
714 # Create an xray object for the particular element/ion. Note that
715 # we must not use normal attribute tests such as "hasattr(el, 'attr')"
716 # or "try: el.attr; except:" since the delegation methods on Ion will
717 # just return the attribute from the base element. Instead we check
718 # for an instance specific xray object for the particular ion prior
719 # to delegating.
720 # TODO: is there a better way to set up delegation on a field by
721 # field basis?
722 def _cache_xray(el):
723 if '_xray' not in el.__dict__ and isinstance(el, (Element, Ion)):
724 el._xray = Xray(el)
725 return el._xray
726 Element.xray = property(_cache_xray)
727 Ion.xray = property(_cache_xray)
728
729 ## Note: the simple approach below fails for e.g., Ni[58].ion[3].xray
730 #for el in table:
731 # for charge in el.ions:
732 # el.ion[charge].xray = Xray(el.ion[charge])
733 # el.xray = Xray(el)
734
735 def plot_xsf(el):
736 """
737 Plots the xray scattering factors for the given element.
738
739 :Parameters:
740 *el* : Element
741
742 :Returns: None
743 """
744 import matplotlib.pyplot as plt
745
746 xsf = el.xray.sftable
747 plt.title('X-ray scattering factors for '+el.name)
748 plt.plot(xsf[0], xsf[1])
749 plt.plot(xsf[0], xsf[2])
750 plt.xlabel('Energy (keV)')
751 plt.ylabel('Scattering factor')
752 plt.legend(['f1', 'f2'])
753 plt.show()
754
755 def sld_table(wavelength=None, table=None):
756 """
757 Prints the xray SLD table for the given wavelength.
758
759 :Parameters:
760 *wavelength* = Cu K-alpha : float | |Ang|
761 X-ray wavelength.
762 *table* : PeriodicTable
763 The default periodictable unless a specific table has been requested.
764
765 :Returns: None
766
767 Example
768
769 >>> sld_table() # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
770 X-ray scattering length density for 1.5418 Ang
771 El rho irho
772 H 1.19 0.00
773 He 1.03 0.00
774 Li 3.92 0.00
775 Be 13.93 0.01
776 B 18.40 0.01
777 C 18.71 0.03
778 N 6.88 0.02
779 O 9.74 0.04
780 F 12.16 0.07
781 Ne 10.26 0.09
782 Na 7.98 0.09
783 Mg 14.78 0.22
784 ...
785 """
786 table = default_table(table)
787 if wavelength is None:
788 wavelength = table.Cu.K_alpha
789
790 # NBCU spreadsheet format
791 print("X-ray scattering length density for %g Ang"%wavelength)
792 print("%3s %6s %6s"%('El', 'rho', 'irho'))
793 for el in table:
794 rho, irho = el.xray.sld(wavelength=table.Cu.K_alpha)
795 if rho is not None:
796 print("%3s %6.2f %6.2f"%(el.symbol, rho, irho))
797
798 def emission_table(table=None):
799 """
800 Prints a table of emission lines.
801
802 :Parameters:
803 *table* : PeriodicTable
804 The default periodictable unless a specific table has been requested.
805
806 :Returns: None
807
808 Example
809
810 >>> emission_table() # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
811 El Kalpha Kbeta1
812 Ne 14.6102 14.4522
813 Na 11.9103 11.5752
814 Mg 9.8902 9.5211
815 Al 8.3402 7.9601
816 Si 7.1263 6.7531
817 ...
818 """
819 table = default_table(table)
820 print("%3s %7s %7s"%('El', 'Kalpha', 'Kbeta1'))
821 for el in table:
822 if hasattr(el, 'K_alpha'):
823 print("%3s %7.4f %7.4f"%(el.symbol, el.K_alpha, el.K_beta1))