Skip to content

MaterialDensity.chemical_structure setter computes SLD with stale molar mass (test enshrines wrong value) #369

Description

@rozyczko

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    [priority] highShould be prioritized soon[scope] bugBug report or fix (major.minor.PATCH)

    Projects

    Status
    Bugs

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions