Skip to content

Commit c4f5aec

Browse files
docs: use DynamicQuantities instead of Unitful
1 parent 82a570f commit c4f5aec

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

docs/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665"
44
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
55
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
66
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
7+
DynamicQuantities = "06fc5a27-2a28-4c7c-a15d-362465fb6821"
78
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
89
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
910
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
@@ -24,6 +25,7 @@ DifferentialEquations = "7.6"
2425
Distributions = "0.25"
2526
Documenter = "1"
2627
ModelingToolkit = "8.33, 9"
28+
DynamicQuantities = "0.12"
2729
NonlinearSolve = "0.3, 1, 2, 3"
2830
Optim = "1.7"
2931
Optimization = "3.9"

docs/src/basics/Validation.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ModelingToolkit.jl provides extensive functionality for model validation and uni
77
Units may be assigned with the following syntax.
88

99
```@example validation
10-
using ModelingToolkit, Unitful
10+
using ModelingToolkit, DynamicQuantities
1111
@variables t [unit = u"s"] x(t) [unit = u"m"] g(t) w(t) [unit = "Hz"]
1212
1313
@variables(t, [unit = u"s"], x(t), [unit = u"m"], g(t), w(t), [unit = "Hz"])
@@ -45,7 +45,7 @@ ModelingToolkit.get_unit
4545
Example usage below. Note that `ModelingToolkit` does not force unit conversions to preferred units in the event of nonstandard combinations -- it merely checks that the equations are consistent.
4646

4747
```@example validation
48-
using ModelingToolkit, Unitful
48+
using ModelingToolkit, DynamicQuantities
4949
@parameters τ [unit = u"ms"]
5050
@variables t [unit = u"ms"] E(t) [unit = u"kJ"] P(t) [unit = u"MW"]
5151
D = Differential(t)
@@ -65,7 +65,7 @@ ModelingToolkit.get_unit(eqs[1].rhs)
6565
An example of an inconsistent system: at present, `ModelingToolkit` requires that the units of all terms in an equation or sum to be equal-valued (`ModelingToolkit.equivalent(u1,u2)`), rather than simply dimensionally consistent. In the future, the validation stage may be upgraded to support the insertion of conversion factors into the equations.
6666

6767
```@example validation
68-
using ModelingToolkit, Unitful
68+
using ModelingToolkit, DynamicQuantities
6969
@parameters τ [unit = u"ms"]
7070
@variables t [unit = u"ms"] E(t) [unit = u"J"] P(t) [unit = u"MW"]
7171
D = Differential(t)
@@ -81,10 +81,9 @@ expect an object type, while two-parameter calls expect a function type as the f
8181
second argument.
8282

8383
```@example validation2
84-
using ModelingToolkit, Unitful
84+
using ModelingToolkit, DynamicQuantities
85+
using ModelingToolkit: t_nounits as t, D_nounits as D
8586
# Composite type parameter in registered function
86-
@variables t
87-
D = Differential(t)
8887
struct NewType
8988
f::Any
9089
end
@@ -105,12 +104,12 @@ sys = ODESystem(eqs, t, [sts...;], [ps...;], name = :sys)
105104
sys_simple = structural_simplify(sys)
106105
```
107106

108-
## `Unitful` Literals
107+
## `DynamicQuantities` Literals
109108

110109
In order for a function to work correctly during both validation & execution, the function must be unit-agnostic. That is, no unitful literals may be used. Any unitful quantity must either be a `parameter` or `variable`. For example, these equations will not validate successfully.
111110

112111
```julia
113-
using ModelingToolkit, Unitful
112+
using ModelingToolkit, DynamicQuantities
114113
@variables t [unit = u"ms"] E(t) [unit = u"J"] P(t) [unit = u"MW"]
115114
D = Differential(t)
116115
eqs = [D(E) ~ P - E / 1u"ms"]
@@ -124,7 +123,7 @@ ModelingToolkit.validate(eqs) #Returns false while displaying a warning message
124123
Instead, they should be parameterized:
125124

126125
```@example validation3
127-
using ModelingToolkit, Unitful
126+
using ModelingToolkit, DynamicQuantities
128127
@parameters τ [unit = u"ms"]
129128
@variables t [unit = u"ms"] E(t) [unit = u"kJ"] P(t) [unit = u"MW"]
130129
D = Differential(t)
@@ -138,8 +137,8 @@ eqs = [D(E) ~ P - myfunc(E, τ)]
138137
ModelingToolkit.validate(eqs) #Returns true
139138
```
140139

141-
It is recommended *not* to circumvent unit validation by specializing user-defined functions on `Unitful` arguments vs. `Numbers`. This both fails to take advantage of `validate` for ensuring correctness, and may cause in errors in the
142-
future when `ModelingToolkit` is extended to support eliminating `Unitful` literals from functions.
140+
It is recommended *not* to circumvent unit validation by specializing user-defined functions on `DynamicQuantities` arguments vs. `Numbers`. This both fails to take advantage of `validate` for ensuring correctness, and may cause in errors in the
141+
future when `ModelingToolkit` is extended to support eliminating `DynamicQuantities` literals from functions.
143142

144143
## Other Restrictions
145144

@@ -149,7 +148,7 @@ future when `ModelingToolkit` is extended to support eliminating `Unitful` liter
149148

150149
If a system fails to validate due to unit issues, at least one warning message will appear, including a line number as well as the unit types and expressions that were in conflict. Some system constructors re-order equations before the unit checking can be done, in which case the equation numbers may be inaccurate. The printed expression that the problem resides in is always correctly shown.
151150

152-
Symbolic exponents for unitful variables *are* supported (ex: `P^γ` in thermodynamics). However, this means that `ModelingToolkit` cannot reduce such expressions to `Unitful.Unitlike` subtypes at validation time because the exponent value is not available. In this case `ModelingToolkit.get_unit` is type-unstable, yielding a symbolic result, which can still be checked for symbolic equality with `ModelingToolkit.equivalent`.
151+
Symbolic exponents for unitful variables *are* supported (ex: `P^γ` in thermodynamics). However, this means that `ModelingToolkit` cannot reduce such expressions to `DynamicQuantities.Quantity` subtypes at validation time because the exponent value is not available. In this case `ModelingToolkit.get_unit` is type-unstable, yielding a symbolic result, which can still be checked for symbolic equality with `ModelingToolkit.equivalent`.
153152

154153
## Parameter & Initial Condition Values
155154

0 commit comments

Comments
 (0)