Skip to content

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

@rozyczko

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    [priority] mediumNormal/default priority[scope] maintenanceCode/tooling cleanup, no feature or bugfix (major.minor.PATCH)
    No fields configured for issues without a type.

    Projects

    Status
    Bugs

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions