Skip to content

Commit bb7f520

Browse files
committed
add docs on low level acces to state ordering
1 parent f19966b commit bb7f520

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

docs/src/symbolic_indexing.md

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Symbolic Indexing
22

3-
Using SciML's [`SymblicIndexingInterface.jl`](https://github.com/SciML/SymbolicIndexingInterface.jl), `ND.jl` provides lots of methods to access and change variables and Parameters.
3+
Using SciML's [`SymbolicIndexingInterface.jl`](https://github.com/SciML/SymbolicIndexingInterface.jl), `ND.jl` provides numerous methods to access and change variables and parameters.
44

55
!!! details "Setup code to make following examples work"
66
```@example si
@@ -11,7 +11,7 @@ Using SciML's [`SymblicIndexingInterface.jl`](https://github.com/SciML/SymbolicI
1111
```
1212

1313
## Provide Symbol Names
14-
When construction component models, you can pass symbolic names using the `sym` and `psym` keywords.
14+
When constructing component models, you can pass symbolic names using the `sym` and `psym` keywords.
1515
```@example si
1616
function _edgef!(e, v_s, v_d, (K,), t)
1717
e .= K * (v_s[1] .- v_d[1])
@@ -28,9 +28,9 @@ vertexf = VertexModel(f=_vertexf!, g=1, sym=[:storage])
2828
```
2929

3030

31-
## Fundamental Symblic Indices
31+
## Fundamental Symbolic Indices
3232
The default types for this access are the types [`VIndex`](@ref), [`EIndex`](@ref), [`VPIndex`](@ref) and [`EPIndex`](@ref).
33-
Each of those symbolic indices consists of 2 elements: a reference to the network componen and a reference to the symbol within that component.
33+
Each of those symbolic indices consists of 2 elements: a reference to the network component and a reference to the symbol within that component.
3434
As such, `VIndex(2, :x)` refers to variable with symbolic name `:x` in vertex number 2.
3535
`EPIndex(4, 2)` would refer to the *second* parameter of the edge component for the 4th edge.
3636

@@ -56,7 +56,7 @@ Often, you need many individual symbolic indices. For that there are the helper
5656
With the help of those methods you can generate arrays of symbolic indices:
5757

5858
```@example si
59-
vidxs(nw, :, :storage) # get variable "storage" for all nodes
59+
vidxs(nw, :, :storage) # get variable "storage" for all vertices
6060
```
6161
```@example si
6262
plot(sol; idxs=vidxs(nw, :, :storage))
@@ -70,7 +70,7 @@ p = NWParameter(nw)
7070
```
7171
creates a `NWParameter` object for the network `nw`.
7272
It essentially creates a new flat parameter array and fills it with the default parameter values define in the component.
73-
The parameters in the `NWParameter` object can be accessed using the symbolic indices.
73+
The parameters in the `NWParameter` object can be accessed using symbolic indices.
7474
```@example si
7575
p[EPIndex(5, :K)] = 2.0 # change the parameter K of the 5th edge
7676
nothing #hide
@@ -79,20 +79,20 @@ Similarly, you can create a `NWState` object for the network `nw` using
7979
```@example si
8080
s = NWState(nw)
8181
```
82-
No default values were provided in the network components, so the state array is filled with `NaN`s.
82+
No default values were provided in the network components, so the state array is filled with `NaN` values.
8383
```@example si
84-
s[VIndex(:, :storage)] .= randn(5) # set the (initial) storage for alle nodes
84+
s[VIndex(:, :storage)] .= randn(5) # set the (initial) storage for all vertices
8585
s #hide
8686
```
87-
For both `NWState` and `NWParameter` objects, the there is a more convenient way to access the variables and parameters.
87+
For both `NWState` and `NWParameter` objects, there is a more convenient way to access the variables and parameters.
8888
```@example si
8989
@assert s.v[1, :storage] == s[VIndex(1, :storage)] # s.v -> access vertex states
9090
@assert s.e[1, :flow] == s[EIndex(1, :flow)] # s.e -> access edge states
9191
@assert s.p.e[1,:K] == p[EPIndex(1, :K)] # s.p -> access parameters
9292
```
9393

9494
The `NWState` and `NWParameter` objects are mutable, thus changing them will also change the underlying wrapped flat arrays.
95-
You can allways access the flat representations by calling [`uflat`](@ref) and [`pflat`](@ref).
95+
You can always access the flat representations by calling [`uflat`](@ref) and [`pflat`](@ref).
9696

9797
!!! note
9898
The `NWState` and `NWParameter` wrappers can be constructed from various objects.
@@ -101,8 +101,8 @@ You can allways access the flat representations by calling [`uflat`](@ref) and [
101101

102102
## Observables
103103
Sometimes, the "states" you're interested in aren't really states in the DAE sense but rather
104-
algebraic derivations from DAE states, parameters and time -- in accordance with the naming in
105-
the `SciML`-ecosystem those states are called Observables.
104+
algebraic derivations from DAE states, parameters, and time -- in accordance with the naming in
105+
the `SciML` ecosystem, these values are called Observables.
106106

107107
A prime example of Observables are edge/vertex-outputs, such as the `flow` in the edge model defined above.
108108
It is also possible to define additional Observables manually by using the `obssym` and `obsf` keyword
@@ -117,17 +117,43 @@ plot(sol; idxs=eidxs(nw, :, :flow))
117117

118118
## Derived `ObservableExpressions` using `@obsex`
119119

120-
Sometimes it is usefull to plot or observe some simple derived quantity.For that,
121-
one can used the [`@obsex`](@ref) macro, to define simple derived quantities.
120+
Sometimes it is useful to plot or observe simple derived quantities. For that,
121+
one can use the [`@obsex`](@ref) macro to define simple derived quantities.
122122

123123
For example, we can directly plot the storage difference with respect to storage of node 1.
124124

125125
```@example si
126126
plot(sol; idxs=@obsex(vidxs(nw,:,:storage) .- VIndex(1,:storage)))
127127
```
128128

129-
Other examples are the calculation of magnitude and argument of complex values which are modeld in real and imaginary part.
129+
Other examples include calculating the magnitude and argument of complex values that are modeled using real and imaginary parts.
130130
```
131131
@obsex mag = sqrt(VIndex(1, :u_r)^2 + VIndex(2, :u_i)^2)
132132
```
133133

134+
## Low-level accessors for flat array indices
135+
Sometimes, you want to know the indices of your states in the flat arrays.
136+
For that, you can use the low-level methods defined in `SymbolicIndexingInterface.jl`:
137+
138+
```@example si
139+
using NetworkDynamics: SII # SII = SymbolicIndexingInterface
140+
idxs = SII.variable_index(nw, vidxs(1:2, :storage))
141+
```
142+
```@example si
143+
uflat(s)[idxs] == s.v[1:2, :storage]
144+
```
145+
Analogous with parmeters:
146+
```@example si
147+
idxs = SII.parameter_index(nw, eidxs(1:2, :K))
148+
pflat(s)[idxs] == s.p.e[1:2, :K]
149+
```
150+
151+
If you need the symbols of all the states/parameters in order, you can use
152+
```@example si
153+
SII.variable_symbols(nw)
154+
```
155+
and
156+
```@example si
157+
SII.parameter_symbols(nw)
158+
```
159+
All above examples also work on other "symbolic containers", e.g. `SII.variable_symbols(::NWState)`.

src/symbolicindexing.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ end
239239
function SII.variable_index(nw::Network, sni)
240240
if _hascolon(sni)
241241
SII.variable_index(nw, _resolve_colon(nw,sni))
242-
elseif SII.symbolic_type(sni) === SII.ArraySymbolic()
242+
elseif sni isa AbstractVector || SII.symbolic_type(sni) === SII.ArraySymbolic()
243243
SII.variable_index.(nw, sni)
244244
else
245245
_variable_index(nw, sni)
@@ -289,7 +289,7 @@ end
289289
function SII.parameter_index(nw::Network, sni)
290290
if _hascolon(sni)
291291
SII.parameter_index(nw, _resolve_colon(nw,sni))
292-
elseif SII.symbolic_type(sni) === SII.ArraySymbolic()
292+
elseif sni isa AbstractVector || SII.symbolic_type(sni) === SII.ArraySymbolic()
293293
SII.parameter_index.(nw, sni)
294294
else
295295
_parameter_index(nw, sni)

0 commit comments

Comments
 (0)