From 29681e6dfdf5dc50428e6d8a9294e69489ca4780 Mon Sep 17 00:00:00 2001 From: Harlan Date: Fri, 16 Jan 2026 12:27:38 -0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20add=20functions=20to=20calculate=20?= =?UTF-8?q?=CE=B4,=20=CE=B2,=20n,=20and=20k=20components=20of=20the=20inde?= =?UTF-8?q?x=20of=20refraction=20as=20extensions=20from=20the=20`xsf.index?= =?UTF-8?q?=5Fof=5Frefraction(...)`=20function.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- periodictable/xsf.py | 149 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) diff --git a/periodictable/xsf.py b/periodictable/xsf.py index a1a5950..65d8aae 100644 --- a/periodictable/xsf.py +++ b/periodictable/xsf.py @@ -465,6 +465,155 @@ def index_of_refraction(compound, *, density=None, natural_density=None, wavelength=wavelength) return 1 - wavelength**2/(2*pi)*(f1 + f2*1j)*1e-6 +def delta(compound, *, density=None, natural_density=None, + energy=None, wavelength=None): + """ + Calculates the δ component of the index of refraction for a given compound + + :Parameters: + *compound* : Formula initializer + Chemical formula. + *density* : float | |g/cm^3| + Mass density of the compound, or None for default. + *natural_density* : float | |g/cm^3| + Mass density of the compound at naturally occurring isotope abundance. + *wavelength* : float or vector | |Ang| + Wavelength of the X-ray. + *energy* : float or vector | keV + Energy of the X-ray, if *wavelength* is not specified. + + :Returns: + *delta* : float or vector | unitless + δ component of the index of refraction of the material at the given energy + + :Notes: + + Formula taken from http://xdb.lbl.gov (section 1.7) and checked + against http://henke.lbl.gov/optical_constants/getdb2.html. + """ + if energy is not None: + wavelength = xray_wavelength(energy) + assert wavelength is not None, "scattering calculation needs energy or wavelength" + f1, f2 = xray_sld(compound, + density=density, natural_density=natural_density, + wavelength=wavelength) + return wavelength**2/(2*pi)*(f1)*1e-6 + +def beta(compound, *, density=None, natural_density=None, + energy=None, wavelength=None): + """ + Calculates the β component of the index of refraction for a given compound + + :Parameters: + *compound* : Formula initializer + Chemical formula. + *density* : float | |g/cm^3| + Mass density of the compound, or None for default. + *natural_density* : float | |g/cm^3| + Mass density of the compound at naturally occurring isotope abundance. + *wavelength* : float or vector | |Ang| + Wavelength of the X-ray. + *energy* : float or vector | keV + Energy of the X-ray, if *wavelength* is not specified. + + :Returns: + *beta* : float or vector | unitless + β component of the index of refraction of the material at the given energy + + :Notes: + + Formula taken from the equation + + .. math:: + + n = 1 - \delta + i \beta + + derivitive of the formula http://xdb.lbl.gov (section 1.7) using a more common + sign convention for the imaginary part of the index of refraction. Checked + against http://henke.lbl.gov/optical_constants/getdb2.html. + """ + if energy is not None: + wavelength = xray_wavelength(energy) + assert wavelength is not None, "scattering calculation needs energy or wavelength" + f1, f2 = xray_sld(compound, + density=density, natural_density=natural_density, + wavelength=wavelength) + return wavelength**2/(2*pi)*(f2)*1e-6 + +def n(compound, *, density=None, natural_density=None, + energy=None, wavelength=None): + """ + Calculates the real part (n) of the index of refraction for a given compound + + :Parameters: + *compound* : Formula initializer + Chemical formula. + *density* : float | |g/cm^3| + Mass density of the compound, or None for default. + *natural_density* : float | |g/cm^3| + Mass density of the compound at naturally occurring isotope abundance. + *wavelength* : float or vector | |Ang| + Wavelength of the X-ray. + *energy* : float or vector | keV + Energy of the X-ray, if *wavelength* is not specified. + + :Returns: + *n* : float or vector | unitless + n component of the index of refraction of the material at the given energy + + :Notes: + + Formula taken from the equation + + .. math:: + + n = 1 - \delta + + derivitive of the formula for δ from http://xdb.lbl.gov (section 1.7) using a more common + sign convention for the imaginary part of the index of refraction. Checked + against http://henke.lbl.gov/optical_constants/getdb2.html. + """ + return 1 - delta(compound, + density=density, natural_density=natural_density, + energy=energy, wavelength=wavelength) + +def k(compound, *, density=None, natural_density=None, + energy=None, wavelength=None): + """ + Calculates the imaginary part (k) of the index of refraction for a given compound + + :Parameters: + *compound* : Formula initializer + Chemical formula. + *density* : float | |g/cm^3| + Mass density of the compound, or None for default. + *natural_density* : float | |g/cm^3| + Mass density of the compound at naturally occurring isotope abundance. + *wavelength* : float or vector | |Ang| + Wavelength of the X-ray. + *energy* : float or vector | keV + Energy of the X-ray, if *wavelength* is not specified. + + :Returns: + *k* : float or vector | unitless + k component of the index of refraction of the material at the given energy + + :Notes: + + Formula taken from the equation + + .. math:: + + n = 1 - \delta + i \beta + + derivitive of the formula for β from http://xdb.lbl.gov (section 1.7) using a more common + sign convention for the imaginary part of the index of refraction. Checked + against http://henke.lbl.gov/optical_constants/getdb2.html. + """ + return beta(compound, + density=density, natural_density=natural_density, + energy=energy, wavelength=wavelength) + def mirror_reflectivity(compound, *, density=None, natural_density=None, energy=None, wavelength=None, angle=None, roughness=0): From aa545dbc6d2e072b96a8609740ac12b8dc1000a1 Mon Sep 17 00:00:00 2001 From: Harlan Date: Tue, 20 Jan 2026 23:45:18 -0800 Subject: [PATCH 2/3] fix: remove n and k --- periodictable/xsf.py | 88 ++++---------------------------------------- 1 file changed, 7 insertions(+), 81 deletions(-) diff --git a/periodictable/xsf.py b/periodictable/xsf.py index 65d8aae..62cfdf3 100644 --- a/periodictable/xsf.py +++ b/periodictable/xsf.py @@ -469,7 +469,7 @@ def delta(compound, *, density=None, natural_density=None, energy=None, wavelength=None): """ Calculates the δ component of the index of refraction for a given compound - + :Parameters: *compound* : Formula initializer Chemical formula. @@ -485,11 +485,11 @@ def delta(compound, *, density=None, natural_density=None, :Returns: *delta* : float or vector | unitless δ component of the index of refraction of the material at the given energy - + :Notes: Formula taken from http://xdb.lbl.gov (section 1.7) and checked - against http://henke.lbl.gov/optical_constants/getdb2.html. + against http://henke.lbl.gov/optical_constants/getdb2.html. """ if energy is not None: wavelength = xray_wavelength(energy) @@ -503,7 +503,7 @@ def beta(compound, *, density=None, natural_density=None, energy=None, wavelength=None): """ Calculates the β component of the index of refraction for a given compound - + :Parameters: *compound* : Formula initializer Chemical formula. @@ -519,7 +519,7 @@ def beta(compound, *, density=None, natural_density=None, :Returns: *beta* : float or vector | unitless β component of the index of refraction of the material at the given energy - + :Notes: Formula taken from the equation @@ -529,8 +529,8 @@ def beta(compound, *, density=None, natural_density=None, n = 1 - \delta + i \beta derivitive of the formula http://xdb.lbl.gov (section 1.7) using a more common - sign convention for the imaginary part of the index of refraction. Checked - against http://henke.lbl.gov/optical_constants/getdb2.html. + sign convention for the imaginary part of the index of refraction. Checked + against http://henke.lbl.gov/optical_constants/getdb2.html. """ if energy is not None: wavelength = xray_wavelength(energy) @@ -540,80 +540,6 @@ def beta(compound, *, density=None, natural_density=None, wavelength=wavelength) return wavelength**2/(2*pi)*(f2)*1e-6 -def n(compound, *, density=None, natural_density=None, - energy=None, wavelength=None): - """ - Calculates the real part (n) of the index of refraction for a given compound - - :Parameters: - *compound* : Formula initializer - Chemical formula. - *density* : float | |g/cm^3| - Mass density of the compound, or None for default. - *natural_density* : float | |g/cm^3| - Mass density of the compound at naturally occurring isotope abundance. - *wavelength* : float or vector | |Ang| - Wavelength of the X-ray. - *energy* : float or vector | keV - Energy of the X-ray, if *wavelength* is not specified. - - :Returns: - *n* : float or vector | unitless - n component of the index of refraction of the material at the given energy - - :Notes: - - Formula taken from the equation - - .. math:: - - n = 1 - \delta - - derivitive of the formula for δ from http://xdb.lbl.gov (section 1.7) using a more common - sign convention for the imaginary part of the index of refraction. Checked - against http://henke.lbl.gov/optical_constants/getdb2.html. - """ - return 1 - delta(compound, - density=density, natural_density=natural_density, - energy=energy, wavelength=wavelength) - -def k(compound, *, density=None, natural_density=None, - energy=None, wavelength=None): - """ - Calculates the imaginary part (k) of the index of refraction for a given compound - - :Parameters: - *compound* : Formula initializer - Chemical formula. - *density* : float | |g/cm^3| - Mass density of the compound, or None for default. - *natural_density* : float | |g/cm^3| - Mass density of the compound at naturally occurring isotope abundance. - *wavelength* : float or vector | |Ang| - Wavelength of the X-ray. - *energy* : float or vector | keV - Energy of the X-ray, if *wavelength* is not specified. - - :Returns: - *k* : float or vector | unitless - k component of the index of refraction of the material at the given energy - - :Notes: - - Formula taken from the equation - - .. math:: - - n = 1 - \delta + i \beta - - derivitive of the formula for β from http://xdb.lbl.gov (section 1.7) using a more common - sign convention for the imaginary part of the index of refraction. Checked - against http://henke.lbl.gov/optical_constants/getdb2.html. - """ - return beta(compound, - density=density, natural_density=natural_density, - energy=energy, wavelength=wavelength) - def mirror_reflectivity(compound, *, density=None, natural_density=None, energy=None, wavelength=None, angle=None, roughness=0): From ff828c3224dd16a0e1a051771b2bf3c32324c975 Mon Sep 17 00:00:00 2001 From: Paul Kienzle Date: Wed, 21 Jan 2026 11:40:29 -0500 Subject: [PATCH 3/3] Fix typo in comment in xsf.beta() --- periodictable/xsf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/periodictable/xsf.py b/periodictable/xsf.py index 62cfdf3..13e0dbb 100644 --- a/periodictable/xsf.py +++ b/periodictable/xsf.py @@ -528,7 +528,7 @@ def beta(compound, *, density=None, natural_density=None, n = 1 - \delta + i \beta - derivitive of the formula http://xdb.lbl.gov (section 1.7) using a more common + derivative of the formula http://xdb.lbl.gov (section 1.7) using a more common sign convention for the imaginary part of the index of refraction. Checked against http://henke.lbl.gov/optical_constants/getdb2.html. """