JuliaPhysics / JuliaPhysics/Unitful.jl

Initialization problems with `preferunits` for physical constants on Julia 1.8

Open
#545 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
675
Forks
124
Avg merge
3h 38m
Merged PRs (30d)
1

Description

Hi, I struggle with a subtle issue concerning the definition of physical constants in unitful.jl after preferunits has been set.

I set up a small package on https://github.com/j-fu/UTest.jl which can be dev'd for testing.

In the root directory it contains utest.jl which can be called either with

julia --project=. --compile=min utest.jl`

or with

julia --project=. utest.jl`

The correct output is

Unitful.upreferred(1 * u"m") = 100 cm
ustrip(upreferred(u"ε0")) = 8.85418781762039e-18
ustrip(upreferred(Unitful.ε0)) = 8.85418781762039e-18
ustrip(upreferred(PhysicalConstants.CODATA2018.ε_0)) = 8.8541878128e-18

This is obtained on all older Julia versions, and on 1.8 (beta, rc3) with --compile=min

On 1.8 (beta, rc3) and 1.9 (f1991edd30) without --compile=min one gets

Unitful.upreferred(1 * u"m") = 100 cm
ustrip(upreferred(u"ε0")) = 8.85418781762039e-12
ustrip(upreferred(Unitful.ε0)) = 8.85418781762039e-12
ustrip(upreferred(PhysicalConstants.CODATA2018.ε_0)) = 8.8541878128e-18

One gets the correct result with optimized code if after modification, the --compile=min has been called first. The problem occurs only for physical constants, not for units. PhysicalConstants.jl behaves in the right way, though.

I see three possibilities here:

  • Problem with the architecture of Unitful
  • Problem with code loading in Julia 1.8 when optimizing
  • Evaluating upreferred into the global context in a package using Unitful is a bad idea, even if preferunits is called before loading the package. If this is the case, it needs to be documented. (in fact I struggled with this situation in more obvious situations, so I have an Idea what to write into a PR...)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducible utest.jl script in the linked UTest.jl package and compare Julia 1.8/1.9 runs with and without --compile=min. Trace preferunits, upreferred(u"ε0"), Unitful.ε0, and PhysicalConstants.CODATA2018.ε_0; done means physical constants produce the same correct result under optimized and minimal compilation.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.