Summary
src/easyreflectometry/sample/elements/materials/material_density.py:179-191: the chemical_structure setter updates _scattering_length_real/_imag from the new formula but never updates _molecular_weight. The derived SLD (d * b * N_A / M) is then computed with the new scattering length and the old element's molar mass.
Evidence
tests/sample/elements/materials/test_material_density.py:38-48 builds Co (M = 58.93 g/mol), switches to 'B', and asserts sld ≈ 4.820. Working the numbers: with boron's b = 5.30 fm and boron's M = 10.81 the SLD at rho = 8.9 g/cm^3 would be ≈ 26.3 (in 1e-6 A^-2); with the stale cobalt M = 58.93 it is ≈ 4.82 — exactly the asserted value. The unit test enshrines the wrong physics.
(The underlying free function is correct: density_to_sld in special/calculations.py:99-117 reproduces Si = 2.074e-6 A^-2.)
Suggested fix
One line in the setter:
self._molecular_weight.value = molecular_weight(structure_string)
plus corrected expectations in test_chemical_structure_change.
Found during deep code review (DEEP_ANALYSIS.md §4.3).
Summary
src/easyreflectometry/sample/elements/materials/material_density.py:179-191: thechemical_structuresetter updates_scattering_length_real/_imagfrom the new formula but never updates_molecular_weight. The derived SLD (d * b * N_A / M) is then computed with the new scattering length and the old element's molar mass.Evidence
tests/sample/elements/materials/test_material_density.py:38-48builds Co (M = 58.93 g/mol), switches to'B', and assertssld ≈ 4.820. Working the numbers: with boron's b = 5.30 fm and boron's M = 10.81 the SLD at rho = 8.9 g/cm^3 would be ≈ 26.3 (in 1e-6 A^-2); with the stale cobalt M = 58.93 it is ≈ 4.82 — exactly the asserted value. The unit test enshrines the wrong physics.(The underlying free function is correct:
density_to_sldinspecial/calculations.py:99-117reproduces Si = 2.074e-6 A^-2.)Suggested fix
One line in the setter:
plus corrected expectations in
test_chemical_structure_change.Found during deep code review (DEEP_ANALYSIS.md §4.3).