Skip to content

Commit 3d52a72

Browse files
committed
add description metadata to variables
1 parent 0ceecb6 commit 3d52a72

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

docs/src/basics/Variable_metadata.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@
22
It is possible to add metadata to symbolic variables. The following
33
information can be added (note, it's possible to extend this to user-defined metadata as well)
44

5+
## Variable descriptions
6+
Descriptive strings can be attached to variables using the `[description = "descriptive string"]` syntax:
7+
```@example metadata
8+
using ModelingToolkit
9+
@variables u [description = "This is my input"]
10+
getdescription(u)
11+
```
12+
13+
When variables with descriptions are present in systems, they will be printed when the system is shown in the terminal:
14+
```@example metadata
15+
@parameters t
16+
@variables u(t) [description = "A short description of u"]
17+
@parameters p [description = "A description of p"]
18+
@named sys = ODESystem([u ~ p], t)
19+
show(stdout, "text/plain", sys) # hide
20+
```
21+
522
## Input or output
623
Designate a variable as either an input or an output using the following
724
```@example metadata

src/ModelingToolkit.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export NonlinearSystem, OptimizationSystem
173173
export alias_elimination, flatten
174174
export connect, @connector, Connection, Flow, Stream, instream
175175
export isinput, isoutput, getbounds, hasbounds, isdisturbance, istunable, getdist, hasdist,
176-
tunable_parameters, isirreducible
176+
tunable_parameters, isirreducible, getdescription, hasdescription
177177
export ode_order_lowering, dae_order_lowering, liouville_transform
178178
export PDESystem
179179
export Differential, expand_derivatives, @derivatives

src/systems/abstractsystem.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,10 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem)
752752
:displaysize => (1, displaysize(io)[2])), val)
753753
print(io, "]")
754754
end
755+
description = getdescription(s)
756+
if description !== nothing && description != ""
757+
print(io, ": ", description)
758+
end
755759
end
756760
end
757761
limited && print(io, "\n")
@@ -774,6 +778,10 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem)
774778
:displaysize => (1, displaysize(io)[2])), val)
775779
print(io, "]")
776780
end
781+
description = getdescription(s)
782+
if description !== nothing && description != ""
783+
print(io, ": ", description)
784+
end
777785
end
778786
end
779787
limited && print(io, "\n")

src/variables.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,24 @@ function getbounds(p::AbstractVector)
267267
ub = last.(bounds)
268268
(; lb, ub)
269269
end
270+
271+
## Description =================================================================
272+
struct VariableDescription end
273+
Symbolics.option_to_metadata_type(::Val{:description}) = VariableDescription
274+
275+
getdescription(x::Num) = getdescription(Symbolics.unwrap(x))
276+
277+
"""
278+
getdescription(x)
279+
280+
Return any description attached to variables `x`. If no description is attached, an empty string is returned.
281+
"""
282+
function getdescription(x)
283+
p = Symbolics.getparent(x, nothing)
284+
p === nothing || (x = p)
285+
Symbolics.getmetadata(x, VariableDescription, "")
286+
end
287+
288+
function hasdescription(x)
289+
getdescription(x) != ""
290+
end

test/test_variable_metadata.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,19 @@ sp = Set(p)
6666
@test T sp
6767
@test k2 sp
6868
@test length(p) == 3
69+
70+
## Descriptions
71+
@variables u [description = "This is my input"]
72+
@test getdescription(u) == "This is my input"
73+
@test hasdescription(u)
74+
75+
@variables u
76+
@test getdescription(u) == ""
77+
@test !hasdescription(u)
78+
79+
@parameters t
80+
@variables u(t) [description = "A short description of u"]
81+
@parameters p [description = "A description of p"]
82+
@named sys = ODESystem([u ~ p], t)
83+
84+
@test_nowarn show(stdout, "text/plain", sys)

0 commit comments

Comments
 (0)