You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/examples.md
+351Lines changed: 351 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@ On your chemistry homework, you are faced with the following problem on the phot
10
10
> The energy of the incident UV light is ``7.2 \cdot 10^{-19} \mathrm{J}`` per photon. Calculate the wavelength of the ejected electrons, in nanometers.
11
11
12
12
Let's solve this problem with `DynamicQuantities.jl`!
13
+
13
14
```jldoctest examples
14
15
julia> using DynamicQuantities
15
16
@@ -30,6 +31,356 @@ julia> λ = h / p # wavelength of ejected electrons
30
31
julia> uconvert(us"nm", λ) # return answer in nanometers
31
32
3.0294912478780556 nm
32
33
```
34
+
33
35
Since units are automatically propagated, we can verify the dimension of our answer and all intermediates.
34
36
Also, using `DynamicQuantities.Constants`, we were able to obtain the (dimensionful!) values of all necessary constants without typing them ourselves.
35
37
38
+
## 2. Projectile motion
39
+
40
+
Let's solve a simple projectile motion problem.
41
+
First load the `DynamicQuantities` module:
42
+
43
+
```julia
44
+
using DynamicQuantities
45
+
```
46
+
47
+
Set up initial conditions as quantities:
48
+
49
+
```julia
50
+
y0 =10u"km"
51
+
v0 =250u"m/s"
52
+
θ =deg2rad(60)
53
+
g =9.81u"m/s^2"
54
+
```
55
+
56
+
Next, we use trig functions to calculate x and y components of initial velocity.
57
+
`vx0` is the x component and
58
+
`vy0` is the y component:
59
+
60
+
```julia
61
+
vx0 = v0 *cos(θ)
62
+
vy0 = v0 *sin(θ)
63
+
```
64
+
65
+
Next, let's create a time vector from 0 seconds to 1.3 minutes.
66
+
Note that these are the same dimension (time), so it's fine to treat
67
+
them as dimensionally equivalent!
68
+
69
+
```julia
70
+
t =range(0u"s", 1.3u"min", length=100)
71
+
```
72
+
73
+
Next, use kinematic equations to calculate x and y as a function of time.
throw(ArgumentError("Conversion of a `DynamicQuantities.Quantity` to a `Unitful.Quantity` is not defined with dimensions of type `SymbolicDimensions`. Instead, you can first use the `uexpand` function to convert the dimensions to their base SI form of type `Dimensions`, then convert this quantity to a `Unitful.Quantity`."))
Base.:(==)(::AbstractQuantity, ::WeakRef) =error("Cannot compare a quantity to a weakref")
9
+
Base.:(==)(::WeakRef, ::AbstractQuantity) =error("Cannot compare a weakref to a quantity")
10
+
11
+
Base.:*(l::AbstractDimensions, r::Number) =error("Please use an `UnionAbstractQuantity` for multiplication. You used multiplication on types: $(typeof(l)) and $(typeof(r)).")
12
+
Base.:*(l::Number, r::AbstractDimensions) =error("Please use an `UnionAbstractQuantity` for multiplication. You used multiplication on types: $(typeof(l)) and $(typeof(r)).")
13
+
Base.:/(l::AbstractDimensions, r::Number) =error("Please use an `UnionAbstractQuantity` for division. You used division on types: $(typeof(l)) and $(typeof(r)).")
14
+
Base.:/(l::Number, r::AbstractDimensions) =error("Please use an `UnionAbstractQuantity` for division. You used division on types: $(typeof(l)) and $(typeof(r)).")
Base.:*(l, r::AbstractQuantity) =new_quantity(typeof(r), l *ustrip(r), dimension(r))
7
-
Base.:*(l::AbstractDimensions, r) =error("Please use an `AbstractQuantity` for multiplication. You used multiplication on types: $(typeof(l)) and $(typeof(r)).")
8
-
Base.:*(l, r::AbstractDimensions) =error("Please use an `AbstractQuantity` for multiplication. You used multiplication on types: $(typeof(l)) and $(typeof(r)).")
1
+
for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES
@@ -56,7 +87,7 @@ which is by default a rational number.
56
87
57
88
# Constructors
58
89
59
-
- `Dimensions(args...)`: Pass all the dimensions as arguments. `R` is set to `DEFAULT_DIM_BASE_TYPE`.
90
+
- `Dimensions(args...)`: Pass all the dimensions as arguments.
60
91
- `Dimensions(; kws...)`: Pass a subset of dimensions as keyword arguments. `R` is set to `DEFAULT_DIM_BASE_TYPE`.
61
92
- `Dimensions(::Type{R}; kws...)` or `Dimensions{R}(; kws...)`: Pass a subset of dimensions as keyword arguments, with the output type set to `Dimensions{R}`.
62
93
- `Dimensions{R}()`: Create a dimensionless object typed as `Dimensions{R}`.
(::Type{D})(::Type{R}; kws...) where {R,D<:AbstractDimensions} =constructor_of(D){R}((tryrationalize(R, get(kws, k, zero(R))) for k instatic_fieldnames(D))...)
76
-
(::Type{D})(; kws...) where {R,D<:AbstractDimensions{R}} =constructor_of(D)(R; kws...)
106
+
(::Type{D})(::Type{R}; kws...) where {R,D<:AbstractDimensions} =with_type_parameters(D, R)((tryrationalize(R, get(kws, k, zero(R))) for k indimension_names(D))...)
107
+
(::Type{D})(; kws...) where {R,D<:AbstractDimensions{R}} =constructorof(D)(R; kws...)
77
108
(::Type{D})(; kws...) where {D<:AbstractDimensions} =D(DEFAULT_DIM_BASE_TYPE; kws...)
78
-
(::Type{D})(d::AbstractDimensions) where {R,D<:AbstractDimensions{R}} =D((getproperty(d, k) for k instatic_fieldnames(D))...)
109
+
function (::Type{D})(d::D2) where {R,D<:AbstractDimensions{R},D2<:AbstractDimensions}
110
+
dimension_names_equal(D, D2) ||
111
+
error("Cannot create a dimensions of `$(D)` from `$(D2)`. Please write a custom method for construction.")
112
+
D((getproperty(d, k) for k indimension_names(D))...)
0 commit comments