Skip to content

Commit fa04946

Browse files
committed
remove noise, document new getters/setters
1 parent a5db689 commit fa04946

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

docs/src/basics/Variable_metadata.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ using ModelingToolkit: t_nounits as t, D_nounits as D
5454
5555
@variables i(t) [connect = Flow]
5656
@variables k(t) [connect = Stream]
57+
hasconnect(i)
58+
```
59+
```@example connect
60+
getconnect(k)
5761
```
5862

5963
## Input or output
@@ -177,8 +181,35 @@ A variable can be marked `irreducible` to prevent it from being moved to an
177181
`observed` state. This forces the variable to be computed during solving so that
178182
it can be accessed in [callbacks](@ref events)
179183

180-
```julia
184+
```@example metadata
181185
@variable important_value [irreducible = true]
186+
isirreducible(important_value)
187+
```
188+
189+
## Units
190+
191+
Units for variables can be designated using symbolic metadata. For more information, please see the [model validation and units](@ref units) section of the docs. Note that `getunit` is equivalent to `get_unit`.
192+
193+
```@example metadata
194+
@variable speed [unit=u"m/s"]
195+
hasunit(speed)
196+
```
197+
```@example metadata
198+
getunit(speed)
199+
```
200+
201+
## Miscellaneous metadata
202+
203+
User-defined metadata can be added using the `misc` metadata. This can be queried
204+
using the `hasmisc` and `getmisc` functions.
205+
206+
```@example metadata
207+
@variables u [misc = :conserved_parameter] y [misc = [2, 4, 6]]
208+
hasmisc(u)
209+
```
210+
211+
```@example metadata
212+
getmisc(y)
182213
```
183214

184215
## Additional functions

src/variables.jl

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ struct VariableStatePriority end
88
struct VariableMisc end
99
Symbolics.option_to_metadata_type(::Val{:unit}) = VariableUnit
1010
Symbolics.option_to_metadata_type(::Val{:connect}) = VariableConnectType
11-
Symbolics.option_to_metadata_type(::Val{:noise}) = VariableNoiseType
1211
Symbolics.option_to_metadata_type(::Val{:input}) = VariableInput
1312
Symbolics.option_to_metadata_type(::Val{:output}) = VariableOutput
1413
Symbolics.option_to_metadata_type(::Val{:irreducible}) = VariableIrreducible
@@ -29,7 +28,7 @@ ModelingToolkit.dump_variable_metadata(p)
2928
"""
3029
function dump_variable_metadata(var)
3130
uvar = unwrap(var)
32-
vartype, name = Symbolics.getmetadata(uvar, VariableSource, (:unknown, :unknown))
31+
variable_source, name = Symbolics.getmetadata(uvar, VariableSource, (:unknown, :unknown))
3332
type = symtype(uvar)
3433
if type <: AbstractArray
3534
shape = Symbolics.shape(var)
@@ -41,7 +40,6 @@ function dump_variable_metadata(var)
4140
end
4241
unit = getunit(uvar)
4342
connect = getconnect(uvar)
44-
noise = getnoise(uvar)
4543
input = isinput(uvar) || nothing
4644
output = isoutput(uvar) || nothing
4745
irreducible = isirreducible(var)
@@ -61,13 +59,12 @@ function dump_variable_metadata(var)
6159

6260
meta = (
6361
var = var,
64-
vartype,
62+
variable_source,
6563
name,
6664
variable_type,
6765
shape,
6866
unit,
6967
connect,
70-
noise,
7168
input,
7269
output,
7370
irreducible,
@@ -599,18 +596,3 @@ getunit(x) = get_unit(x)
599596
Check if the variable `x` has a unit.
600597
"""
601598
hasunit(x) = getunit(x) !== nothing
602-
603-
## Noise ======================================================================
604-
"""
605-
getnoise(x)
606-
607-
Get the noise type of variable `x`.
608-
"""
609-
getnoise(x) = getnoise(unwrap(x))
610-
getnoise(x::Symbolic) = Symbolics.getmetadata(x, VariableNoiseType, nothing)
611-
"""
612-
hasnoise(x)
613-
614-
Determine if variable `x` has a noise type.
615-
"""
616-
hasnoise(x) = getnoise(x) !== nothing

test/test_variable_metadata.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,10 @@ x = ModelingToolkit.setmisc(x, "okay")
221221
@variables x
222222
@test ModelingToolkit.getvariabletype(x) == ModelingToolkit.VARIABLE
223223
@test ModelingToolkit.dump_variable_metadata(x).variable_type == ModelingToolkit.VARIABLE
224+
@test ModelingToolkit.dump_variable_metadata(x).variable_source == :variables
224225
x = ModelingToolkit.toparam(x)
225226
@test ModelingToolkit.getvariabletype(x) == ModelingToolkit.PARAMETER
227+
@test ModelingToolkit.dump_variable_metadata(x).variable_source == :variables
226228

227229
@parameters y
228230
@test ModelingToolkit.getvariabletype(y) == ModelingToolkit.PARAMETER

0 commit comments

Comments
 (0)