Skip to content

More unitful compatibility #98

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

Merged
merged 4 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,14 @@ julia> x = 0.3u"km/s"

julia> y = 42 * u"kg"
42.0 kg
```

or by importing explicitly:

```julia
julia> using DynamicQuantities: kPa

julia> room_temp = 100u"kPa"
julia> room_temp = 100kPa
100000.0 m⁻¹ kg s⁻²
```

Expand Down
11 changes: 7 additions & 4 deletions docs/src/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ using DynamicQuantities
Set up initial conditions as quantities:

```julia
y0 = 10u"km"
v0 = 250u"m/s"
# Can explicitly import units:
using DynamicQuantities: km, m, s, min

y0 = 10km
v0 = 250m/s
θ = deg2rad(60)
g = 9.81u"m/s^2"
g = 9.81m/s^2
```

Next, we use trig functions to calculate x and y components of initial velocity.
Expand All @@ -67,7 +70,7 @@ Note that these are the same dimension (time), so it's fine to treat
them as dimensionally equivalent!

```julia
t = range(0u"s", 1.3u"min", length=100)
t = range(0s, 1.3min, length=100)
```

Next, use kinematic equations to calculate x and y as a function of time.
Expand Down
12 changes: 12 additions & 0 deletions src/DynamicQuantities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ import .Units
import .Constants
import .UnitsParse: uparse, @u_str

using .Units: UNIT_SYMBOLS

# Copy all units to top level:
let _units_import_expr = :(using .Units: m, g)
append!(
_units_import_expr.args[1].args,
map(s -> Expr(:(.), s), filter(s -> s ∉ (:m, :g), UNIT_SYMBOLS))
)
eval(_units_import_expr)
end


function __init__()
@require_extensions
end
Expand Down
13 changes: 8 additions & 5 deletions src/units.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ end
@add_prefixes mol (m,)

@doc(
"Length in meters. Available variants: `fm`, `pm`, `nm`, `μm` (/`um`), `cm`, `dm`, `mm`, `km`, `Mm`, `Gm`.",
"Length in meters. Available variants: `fm`, `pm`, `nm`, `μm` (/`um`), `cm`, `inch`, `dm`, `mm`, `ft`, `km`, `mi`, `Mm`, `Gm`.",
m,
)
@doc(
Expand Down Expand Up @@ -100,11 +100,11 @@ end
@register_unit ohm Ω
@register_unit T N / (A * m)

@add_prefixes Hz (k, M, G)
@add_prefixes Hz (n, μ, u, m, k, M, G)
@add_prefixes N ()
@add_prefixes Pa (k,)
@add_prefixes J (k,)
@add_prefixes W (k, M, G)
@add_prefixes W (m, k, M, G)
@add_prefixes C ()
@add_prefixes V (m, k, M, G)
@add_prefixes F ()
Expand All @@ -114,7 +114,7 @@ end

# SI derived units
@doc(
"Frequency in Hertz. Available variants: `kHz`, `MHz`, `GHz`.",
"Frequency in Hertz. Available variants: `nHz`, `μHz` (/`uHz`), `mHz`, `kHz`, `MHz`, `GHz`.",
Hz,
)
@doc(
Expand All @@ -130,7 +130,7 @@ end
J,
)
@doc(
"Power in Watts. Available variants: `kW`, `MW`, `GW`.",
"Power in Watts. Available variants: `mW`, `kW`, `MW`, `GW`.",
W,
)
@doc(
Expand Down Expand Up @@ -164,6 +164,9 @@ end
@register_unit d day
@register_unit wk 7 * day
@register_unit yr 365.25 * day
@register_unit inch 2.54 * cm
@register_unit ft 12 * inch
@register_unit mi 5280 * ft

@add_prefixes min ()
@add_prefixes minute ()
Expand Down