Skip to content

Commit 4c220c6

Browse files
author
Lucas Morton
committed
Update docs.
1 parent e4ed6e2 commit 4c220c6

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

docs/src/basics/Validation.md

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,24 @@ Units may assigned with the following syntax.
99
```julia
1010
using ModelingToolkit, Unitful
1111
@variables t [unit = u"s"] x(t) [unit = u"m"] g(t) w(t) [unit = "Hz"]
12-
#Or,
12+
1313
@variables(t, [unit = u"s"], x(t), [unit = u"m"], g(t), w(t), [unit = "Hz"])
14-
#Or,
14+
1515
@variables(begin
1616
t, [unit = u"s"],
1717
x(t), [unit = u"m"],
1818
g(t),
1919
w(t), [unit = "Hz"]
2020
end)
21+
22+
# Simultaneously set default value (use plain numbers, not quantities)
23+
@variable x=10 [unit = u"m"]
24+
25+
# Symbolic array: unit applies to all elements
26+
@variable x[1:3] [unit = u"m"]
2127
```
2228

23-
Do not use `quantities` such as `1u"s"` or `1/u"s"` or `u"1/s"` as these will result in errors; instead use `u"s"` or `u"s^1"`.
29+
Do not use `quantities` such as `1u"s"`, `1/u"s"` or `u"1/s"` as these will result in errors; instead use `u"s"`, `u"s^-1"`, or `u"s"^-1`.
2430

2531
## Unit Validation & Inspection
2632

@@ -62,8 +68,38 @@ eqs = eqs = [D(E) ~ P - E/τ,
6268
0 ~ P ]
6369
ModelingToolkit.validate(eqs) #Returns false while displaying a warning message
6470
```
71+
## User-Defined Registered Functions and Types
72+
73+
In order to validate user-defined types and `register`ed functions, specialize `get_unit`. Single-parameter calls to `get_unit`
74+
expect an object type, while two-parameter calls expect a function type as the first argument, and a vector of arguments as the
75+
second argument.
76+
77+
```julia
78+
using ModelingToolkit
79+
# Composite type parameter in registered function
80+
@parameters t
81+
D = Differential(t)
82+
struct NewType
83+
f
84+
end
85+
@register dummycomplex(complex::Num, scalar)
86+
dummycomplex(complex, scalar) = complex.f - scalar
87+
88+
c = NewType(1)
89+
MT.get_unit(x::NewType) = MT.get_unit(x.f)
90+
function MT.get_unit(op::typeof(dummycomplex),args)
91+
argunits = MT.get_unit.(args)
92+
MT.get_unit(-,args)
93+
end
94+
95+
sts = @variables a(t)=0 [unit = u"cm"]
96+
ps = @parameters s=-1 [unit = u"cm"] c=c [unit = u"cm"]
97+
eqs = [D(a) ~ dummycomplex(c, s);]
98+
sys = ODESystem(eqs, t, [sts...;], [ps...;], name=:sys)
99+
sys_simple = structural_simplify(sys)
100+
```
65101

66-
## `Unitful` Literals & User-Defined Functions
102+
## `Unitful` Literals
67103

68104
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.
69105

0 commit comments

Comments
 (0)