Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions periodictable/xsf.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ def xray_sld(compound, *, density=None, natural_density=None,
compound = formulas.formula(compound, density=density,
natural_density=natural_density)
assert compound.density is not None, "scattering calculation needs density"
if not np.isfinite(compound.density) or compound.density < 0.0:
raise ValueError("density must be finite and non-negative")

if wavelength is not None:
energy = xray_energy(wavelength)
Expand Down
8 changes: 8 additions & 0 deletions test/test_xsf.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,11 @@ def main():
test_xsf()
test_refl()
if __name__ == "__main__": main()


def test_xray_sld_rejects_invalid_density():
with pytest.raises(ValueError):
xray_sld("H2O", density=-1.0, wavelength=1.54)
with pytest.raises(ValueError):
xray_sld("H2O", density=float("nan"), wavelength=1.54)
assert xray_sld("", density=0.0, wavelength=1.54) == (0, 0)