-
-
Notifications
You must be signed in to change notification settings - Fork 231
Refactor Unitful.jl usage to use package extensions #3869
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
Open
ChrisRackauckas-Claude
wants to merge
40
commits into
SciML:master
Choose a base branch
from
ChrisRackauckas-Claude:refactor-unitful-to-extension
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
abaad91
Refactor Unitful.jl usage to use package extensions
ChrisRackauckas 17013eb
Refine Unitful extension: keep general unit functions in main package
ChrisRackauckas b24913f
Fix extension implementation: improve unit operations and test setup
ChrisRackauckas 24f0183
Remove UnitfulUnitCheck internal interface - use ModelingToolkit func…
ChrisRackauckas f1dee4e
Fix major unit operation issues in extension
ChrisRackauckas dd8a2db
Revert "Fix major unit operation issues in extension"
ChrisRackauckas 61a607a
Reorganize Unitful-related tests to Extensions group
ChrisRackauckas 695e991
Export unit functions to fix DynamicQuantities test failures
ChrisRackauckas 4729095
Fix DynamicQuantities test failures in extension refactor
ChrisRackauckas f2df1c5
Update src/ModelingToolkit.jl
ChrisRackauckas 52679ac
Update src/systems/model_parsing.jl
ChrisRackauckas 13985a2
Update src/ModelingToolkit.jl
ChrisRackauckas ccb192e
Update src/ModelingToolkit.jl
ChrisRackauckas 3a0f123
Update src/systems/model_parsing.jl
ChrisRackauckas 85ae964
Update src/systems/model_parsing.jl
ChrisRackauckas 9e7247b
Update src/systems/unit_check.jl
ChrisRackauckas 244679d
Update src/systems/validation.jl
ChrisRackauckas 480f6a4
Update src/systems/unit_check.jl
ChrisRackauckas 29ebb9f
Update src/systems/unit_check.jl
ChrisRackauckas 19f63e8
Update src/systems/unit_check.jl
ChrisRackauckas 1a34474
Delete unitful-extension-refactor.patch
ChrisRackauckas 9d1a8ca
Update src/systems/model_parsing.jl
ChrisRackauckas 8385a42
Fix missing UnitfulUnitCheck module by recreating it in extension
ChrisRackauckas 01ddee0
Update ModelingToolkitUnitfulExt.jl
ChrisRackauckas baf2939
Update ModelingToolkitUnitfulExt.jl
ChrisRackauckas ae9312f
Update unit_check.jl
ChrisRackauckas 3cad7b3
Update ext/ModelingToolkitUnitfulExt.jl
ChrisRackauckas e826e2e
Update unit_check.jl
ChrisRackauckas 4b3c91b
Update ext/ModelingToolkitUnitfulExt.jl
ChrisRackauckas 2b85ba3
Update ext/ModelingToolkitUnitfulExt.jl
ChrisRackauckas 0f602b9
Update ext/ModelingToolkitUnitfulExt.jl
ChrisRackauckas 7daa0e2
Update unit_check.jl
ChrisRackauckas 00ad788
Update unit_check.jl
ChrisRackauckas ca31b12
Update unit_check.jl
ChrisRackauckas aa3b607
Update src/systems/unit_check.jl
ChrisRackauckas db66e2a
Update unit_check.jl
ChrisRackauckas 85d119d
Update ModelingToolkitUnitfulExt.jl
ChrisRackauckas 95f88ef
Update ext/ModelingToolkitUnitfulExt.jl
ChrisRackauckas f1d04bf
Update test/units.jl
ChrisRackauckas 6bd166f
Update test/units.jl
ChrisRackauckas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
module ModelingToolkitUnitfulExt | ||
|
||
using ModelingToolkit | ||
using Unitful | ||
using Symbolics: Symbolic, value | ||
using SciMLBase | ||
|
||
# Import necessary types and functions from ModelingToolkit | ||
import ModelingToolkit: ValidationError, _get_unittype, get_unit, screen_unit, | ||
equivalent, _is_dimension_error, convert_units, check_units | ||
|
||
const MT = ModelingToolkit | ||
|
||
# Add Unitful-specific unit type detection | ||
function MT._get_unittype(u::Unitful.Unitlike) | ||
return Val(:Unitful) | ||
end | ||
|
||
# Base operations for mixing Symbolic and Unitful | ||
Base.:*(x::Union{MT.Num, Symbolic}, y::Unitful.AbstractQuantity) = x * y | ||
Base.:/(x::Union{MT.Num, Symbolic}, y::Unitful.AbstractQuantity) = x / y | ||
|
||
# Unitful-specific get_unit method | ||
function MT.get_unit(x::Unitful.Quantity) | ||
return screen_unit(Unitful.unit(x)) | ||
end | ||
|
||
# Unitful-specific screen_unit method | ||
function MT.screen_unit(result::Unitful.Unitlike) | ||
result isa Unitful.ScalarUnits || | ||
throw(ValidationError("Non-scalar units such as $result are not supported. Use a scalar unit instead.")) | ||
result == Unitful.u"°" && | ||
throw(ValidationError("Degrees are not supported. Use radians instead.")) | ||
return result | ||
end | ||
|
||
# Unitful-specific equivalence check | ||
function MT.equivalent(x::Unitful.Unitlike, y::Unitful.Unitlike) | ||
return isequal(1 * x, 1 * y) | ||
end | ||
|
||
# Mixed equivalence checks | ||
MT.equivalent(x::Unitful.Unitlike, y) = isequal(1 * x, y) | ||
MT.equivalent(x, y::Unitful.Unitlike) = isequal(x, 1 * y) | ||
|
||
# The safe_get_unit function stays in the main package and already handles DQ.DimensionError | ||
# We just need to make sure it can handle Unitful.DimensionError too | ||
# This will be handled by the main function's MethodError catch | ||
|
||
# Unitful-specific dimension error detection for model parsing | ||
MT._is_dimension_error(e::Unitful.DimensionError) = true | ||
|
||
# Unitful-specific convert_units methods for model parsing | ||
function MT.convert_units(varunits::Unitful.FreeUnits, value) | ||
Unitful.ustrip(varunits, value) | ||
end | ||
|
||
MT.convert_units(::Unitful.FreeUnits, value::MT.NoValue) = MT.NO_VALUE | ||
|
||
function MT.convert_units(varunits::Unitful.FreeUnits, value::AbstractArray{T}) where {T} | ||
Unitful.ustrip.(varunits, value) | ||
end | ||
|
||
MT.convert_units(::Unitful.FreeUnits, value::MT.Num) = value | ||
|
||
# Unitful-specific check_units method | ||
function MT.check_units(::Val{:Unitful}, eqs...) | ||
# Use the main package's validate function | ||
MT.validate(eqs...) || | ||
throw(ValidationError("Some equations had invalid units. See warnings for details.")) | ||
end | ||
|
||
# Define Unitful time variables (moved from main module) | ||
const t_unitful = let | ||
MT.only(MT.@independent_variables t [unit = Unitful.u"s"]) | ||
end | ||
const D_unitful = MT.Differential(t_unitful) | ||
|
||
# Extension loaded - all Unitful-specific functionality is now available | ||
|
||
end # module |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.