-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow External Unit Registration #107
Conversation
Benchmark Results
Benchmark PlotsA plot of the benchmark results have been uploaded as an artifact to the workflow run for this PR. |
Great idea! Can you rebase to #106? As there are changes which might affect the behavior. |
c08be49
to
220d923
Compare
Thanks! Quick initial comment is that this will need to update |
One other quick thought is that we will probably need to change |
Let me know when you're ready and I can give more comments or help out |
@ven-k ? |
192288c
to
4658a81
Compare
Now works: julia> using ModelingToolkit, DynamicQuantities
julia> @register_unit Wb u"kg*m^2*s^−2*A^−1"
julia> @variables k [unit = u"Wb"]
1-element Vector{Num}:
k |
- By interpolating `Units.UNIT_SYMBOLS` while registering units and updating vectors-and-maps-of-units, new units can be registered even from outside the `Units` module - Internally, units are registered lazily with `_lazy_register_unit` aka `UNIT_MAPPING` is updated after all units are defined.
4658a81
to
2c869e3
Compare
_UNIT_SYMBOLS
while registering new unitsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good. I added a few suggestions.
One other comment: trying to register the same unit twice should have no effect. That way you could register units within a method scope without fear of creating duplicates. Also it would probably make things safer for Revise.jl |
Just comparing |
Thanks for implementing all of those changes. Sorry to go back on what @devmotion said but do you think we could avoid the Basically I think those constants should just be write once read many arrays. When you (Also, the fewer deps the better) I think the struct could just be something like struct WriteOnceReadManyVector{T}
_raw_data::Vector{T}
end and just define |
Now that I think about it, trying to register a unit should trigger an error if the symbol is already used AND the value assigned to that symbol is different than the one being registered. But if the symbol is used already and the value is the same, it could simply be ignored and the unit registration should return early (before pushing anything). |
54ee0f8
to
6a23eb5
Compare
I've added tests for precompilation of register_unit in a closed module. Footnotes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good. Quick comment is I wonder if you could move the type definition of WriteOnceReadMany
as well as its methods into a separate file, rather than mix them with Quantity
? e.g., you will see that FixedRational
, another internal type, gets its own file.
`UNIT_SYMBOLS`, `UNIT_VALUES`, `UNIT_MAPPING`, `ALL_VALUES`,`ALL_SYMBOLS`, ALL_MAPPING`, `SYMBOLIC_UNIT_VALUES` are instances of this collection type. With this data type only a certain set of operations are permitted on these collections.
6a23eb5
to
cbd071c
Compare
I expected |
Thanks! (Currently types.jl is supposed to be “exported types” while internal types live in separate files. However I haven’t been completely consistent on this and a future refactor would certainly improve readability.) |
Cool. We're back at 100% test coverage. I did a deep review + some edits and everything is looking good now! Making some final tweaks and then will merge. Thanks so much for the great contribution!! |
One thing I'm confused about is that the Super weird: julia> ExternalUnitRegistration.ALL_SYMBOLS
DynamicQuantities.WriteOnceReadMany{Vector{Symbol}}([:m, :g, :s, :A, :K, :cd, :mol, :fm, :pm, :nm … :L_sun, :L_bol0, :sigma_T, :au, :pc, :ly, :atm, :kpc, :Mpc, :Gpc]) Where is the Is this some weird thing about precompilation or maybe |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything looks good to me. I'm just super confused about the following behaviour:
julia> push!(LOAD_PATH, joinpath(@__DIR__, "test", "precompile_test"))
4-element Vector{String}:
"@"
"@v#.#"
"@stdlib"
"/Users/mcranmer/PermaDocuments/DynamicQuantities.jl/test/precompile_test"
julia> using ExternalUnitRegistration
julia> ExternalUnitRegistration.ALL_SYMBOLS
DynamicQuantities.WriteOnceReadMany{Vector{Symbol}}([:m, :g, :s, :A, :K, :cd, :mol, :fm, :pm, :nm … :L_sun, :L_bol0, :sigma_T, :au, :pc, :ly, :atm, :kpc, :Mpc, :Gpc])
Where is :Wb
? Did the symbol list somehow get reset?
Maybe this is because I'm using Revise.jl?
That's an expected behavior (even without Revise). So currently the externally defined units can be used with Ex: Footnotes |
Got it! I like that better actually, feels safer. |
Units.UNIT_SYMBOLS
while registering units and updating vectors-and-maps-of-units, new units can be registered even from outside theUnits
module_lazy_register_unit
akaUNIT_MAPPING
is updated after all units are defined.Fixes: