You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unit metadata is gamed: 1e-23 fudge factor leaves SLD parameter with unit 'kmol/m^5' #377
EasyScience's make_dependent_on is unit-aware (it evaluates the expression with scipp units and overwrites the dependent parameter's unit with the result's unit). The library fights this instead of using it:
MaterialDensity creates its scattering-length parameters from the SLD defaults dict (material_density.py:88-99 passes default_dict=DEFAULTS['sld']), so a scattering length carries the unit 1/angstrom^2.
The dependency expression includes a magic 1e-23 (material_density.py:113) so that after scipp normalizes g/cm^3 * A^-2 * mol/g the numeric value lands right — while the parameter's unit becomes kmol/m^5, dimensionally meaningless for an SLD.
tests/sample/elements/materials/test_material_density.py:54 asserts the rendered string '2.074e-6 kmol/m^5', baking the nonsense into the contract.
Root cause, library-wide: the SLD unit string is '1 / angstrom^2' while values are stored in the 1e-6 A^-2 convention (material.py:17-35; _dict_repr hard-codes an e-6 suffix into display strings). This factor-of-1e6 gap between declared unit and stored value will keep generating bugs whenever someone trusts the unit metadata.
Suggested fix
Give scattering lengths the unit angstrom (as layer_area_per_molecule.py:30-47 already does correctly — its constraint a/(b*p)*1e6 yields both the right value and a sensible unit), express SLD in a properly scaled unit (scipp supports 1e-6/angstrom^2-style scales) or store true A^-2, delete the 1e-23 fudge, and fix the enshrining test.
Found during deep code review (DEEP_ANALYSIS.md §4.4).
layer_area_per_molecule.py as the correct counter-example
Confirmed: its sl/isl are declared in angstrom and the constraint a/(b*p)*1e6 yields unit 1/Å^2, so LayerAreaPerMolecule().material.material_a.sld comes out as 1.246 1/Å^2
One addition since the issue was filed: the only commit touching these files is 35c0aa1 ("Enable SLD dependence on material data", #403). It did not change the units: it duplicated the 1e-23 expression into the new _setup_sld_constraints() path, so a fix now has two call sites to keep in sync, plus the new sld_coupled = False state where sld becomes a directly settable parameter whose unit is whatever the dependency last left behind.
Activity
rozyczko commentedon Sep 15, 2026
Re-checked on
developatad7d255f: still valid.default_dict=DEFAULTS['sld']/DEFAULTS['isld']1e-23in the dependency expression'1 / angstrom^2'vs values stored in the 1e-6 conventione-6suffix hard-coded into display at material.py:111-112'2.074e-6 kmol/m^5'layer_area_per_molecule.pyas the correct counter-examplesl/islare declared inangstromand the constrainta/(b*p)*1e6yields unit1/Å^2, soLayerAreaPerMolecule().material.material_a.sldcomes out as1.246 1/Å^2Runtime check:
One addition since the issue was filed: the only commit touching these files is 35c0aa1 ("Enable SLD dependence on material data", #403). It did not change the units: it duplicated the
1e-23expression into the new_setup_sld_constraints()path, so a fix now has two call sites to keep in sync, plus the newsld_coupled = Falsestate wheresldbecomes a directly settable parameter whose unit is whatever the dependency last left behind.correct units for scattering lengths #377 (#419)
🎉 Release: merge develop into master (#442)
Release 2.0.0 (#454)