Skip to content

Unit metadata is gamed: 1e-23 fudge factor leaves SLD parameter with unit 'kmol/m^5' #377

Closed

Description

@rozyczko

Summary

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

Activity

  1. added
    [scope] maintenance
    [priority] medium
    on Jun 12, 2026
  2. rozyczko commented on Sep 15, 2026

    @rozyczko
    MemberAuthor

    Re-checked on develop at ad7d255f: still valid.

    Claim Status
    Scattering lengths built from the SLD defaults dict Still true: material_density.py:124-135 passes default_dict=DEFAULTS['sld'] / DEFAULTS['isld']
    Magic 1e-23 in the dependency expression Still there, and now in two places: material_density.py:149 and material_density.py:178
    SLD unit '1 / angstrom^2' vs values stored in the 1e-6 convention Still true: material.py:17-35, with the e-6 suffix hard-coded into display at material.py:111-112
    Test enshrining '2.074e-6 kmol/m^5' Still asserted at test_material_density.py:57
    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

    Runtime check:

    MaterialDensity().sld       -> 2.0737423003838082  unit: kmol/m^5
    MaterialDensity().isld      -> unit: kmol/m^5
    scattering_length_real      -> 4.1507e-05  unit: 1/A^2   (a length carrying an area^-1 unit)
    _dict_repr                  -> {'sld': '2.074e-6 kmol/m^5', ...}
    

    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.

  3. added a commit that references this issue on Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [priority] medium[scope] maintenance

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions