Skip to content

Commit e8c464f

Browse files
Merge pull request #3363 from hersle/mtkmodel_description
Add @description to @mtkmodel
2 parents 25c8001 + b107e12 commit e8c464f

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

docs/src/basics/MTKLanguage.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ equations.
2323

2424
`@mtkmodel` definition contains begin blocks of
2525

26+
- `@description`: for describing the whole system with a human-readable string
2627
- `@components`: for listing sub-components of the system
2728
- `@constants`: for declaring constants
2829
- `@defaults`: for passing `defaults` to ODESystem
@@ -42,20 +43,23 @@ using ModelingToolkit
4243
using ModelingToolkit: t
4344
4445
@mtkmodel ModelA begin
46+
@description "A component with parameters `k` and `k_array`."
4547
@parameters begin
4648
k
4749
k_array[1:2]
4850
end
4951
end
5052
5153
@mtkmodel ModelB begin
54+
@description "A component with parameters `p1` and `p2`."
5255
@parameters begin
5356
p1 = 1.0, [description = "Parameter of ModelB"]
5457
p2 = 1.0, [description = "Parameter of ModelB"]
5558
end
5659
end
5760
5861
@mtkmodel ModelC begin
62+
@description "A bigger system that contains many more things."
5963
@icon "https://github.com/SciML/SciMLDocs/blob/main/docs/src/assets/logo.png"
6064
@constants begin
6165
c::Int = 1, [description = "Example constant."]
@@ -91,6 +95,10 @@ end
9195
end
9296
```
9397

98+
#### `@description`
99+
100+
A documenting `String` that summarizes and explains what the model is.
101+
94102
#### `@icon`
95103

96104
An icon can be embedded in 3 ways:

docs/src/tutorials/acausal_components.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ end
8484
end
8585
8686
@mtkmodel RCModel begin
87+
@description "A circuit with a constant voltage source, resistor and capacitor connected in series."
8788
@components begin
8889
resistor = Resistor(R = 1.0)
8990
capacitor = Capacitor(C = 1.0)
@@ -251,6 +252,7 @@ make all of our parameter values 1.0. As `resistor`, `capacitor`, `source` lists
251252

252253
```@example acausal
253254
@mtkmodel RCModel begin
255+
@description "A circuit with a constant voltage source, resistor and capacitor connected in series."
254256
@components begin
255257
resistor = Resistor(R = 1.0)
256258
capacitor = Capacitor(C = 1.0)

src/systems/model_parsing.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,13 @@ function _model_macro(mod, name, expr, isconnector)
109109
gui_metadata = isassigned(icon) > 0 ? GUIMetadata(GlobalRef(mod, name), icon[]) :
110110
GUIMetadata(GlobalRef(mod, name))
111111

112+
description = get(dict, :description, "")
113+
112114
@inline pop_structure_dict!.(
113115
Ref(dict), [:constants, :defaults, :kwargs, :structural_parameters])
114116

115117
sys = :($ODESystem($(flatten_equations)(equations), $iv, variables, parameters;
116-
name, systems, gui_metadata = $gui_metadata, defaults))
118+
name, description = $description, systems, gui_metadata = $gui_metadata, defaults))
117119

118120
if length(ext) == 0
119121
push!(exprs.args, :(var"#___sys___" = $sys))
@@ -598,7 +600,9 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps, c_evts, d_evts,
598600
dict, mod, arg, kwargs, where_types)
599601
mname = arg.args[1]
600602
body = arg.args[end]
601-
if mname == Symbol("@components")
603+
if mname == Symbol("@description")
604+
parse_description!(body, dict)
605+
elseif mname == Symbol("@components")
602606
parse_components!(exprs, comps, dict, body, kwargs)
603607
elseif mname == Symbol("@extend")
604608
parse_extend!(exprs, ext, dict, mod, body, kwargs)
@@ -1156,6 +1160,14 @@ function parse_icon!(body::Symbol, dict, icon, mod)
11561160
parse_icon!(getfield(mod, body), dict, icon, mod)
11571161
end
11581162

1163+
function parse_description!(body, dict)
1164+
if body isa String
1165+
dict[:description] = body
1166+
else
1167+
error("Invalid description string $body")
1168+
end
1169+
end
1170+
11591171
### Parsing Components:
11601172

11611173
function component_args!(a, b, varexpr, kwargs; index_name = nothing)

test/model_parsing.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ end
127127
end
128128

129129
@mtkmodel RC begin
130+
@description "An RC circuit."
130131
@structural_parameters begin
131132
R_val = 10u"Ω"
132133
C_val = 10u"F"
@@ -139,7 +140,6 @@ end
139140
constant = Constant(; k = k_val)
140141
ground = MyMockModule.Ground()
141142
end
142-
143143
@equations begin
144144
connect(constant.output, source.V)
145145
connect(source.p, resistor.p)
@@ -157,6 +157,7 @@ sol = solve(prob)
157157
defs = ModelingToolkit.defaults(rc)
158158
@test sol[rc.capacitor.v, end] defs[rc.constant.k]
159159
resistor = getproperty(rc, :resistor; namespace = false)
160+
@test ModelingToolkit.description(rc) == "An RC circuit."
160161
@test getname(rc.resistor) === getname(resistor)
161162
@test getname(rc.resistor.R) === getname(resistor.R)
162163
@test getname(rc.resistor.v) === getname(resistor.v)

0 commit comments

Comments
 (0)