Skip to content

Commit af1f7c6

Browse files
docs: add documentation for tunable parameters (re-)ordering
1 parent f1b0036 commit af1f7c6

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

docs/src/basics/FAQ.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,62 @@ D = Differential(x)
244244
@variables y(x)
245245
@named sys = ODESystem([D(y) ~ x], x)
246246
```
247+
248+
## Ordering of tunable parameters
249+
250+
Tunable parameters are floating point parameters, not used in callbacks and not marked with `tunable = false` in their metadata. These are expected to be used with AD
251+
and optimization libraries. As such, they are stored together in one `Vector{T}`. To obtain the ordering of tunable parameters in this buffer, use:
252+
253+
```@docs
254+
tunable_parameters
255+
```
256+
257+
If you have an array in which a particular dimension is in the order of tunable parameters (e.g. the jacobian with respect to tunables) then that dimension of the
258+
array can be reordered into the required permutation using the symbolic variables:
259+
260+
```@docs
261+
reorder_dimension_by_tunables!
262+
reorder_dimension_by_tunables
263+
```
264+
265+
For example:
266+
267+
```@example reorder
268+
using ModelingToolkit
269+
270+
@parameters p q[1:3] r[1:2, 1:2]
271+
272+
@named sys = ODESystem(Equation[], ModelingToolkit.t_nounits, [], [p, q, r])
273+
sys = complete(sys)
274+
```
275+
276+
The canonicalized tunables portion of `MTKParameters` will be in the order of tunables:
277+
278+
```@example reorder
279+
using SciMLStructures: canonicalize, Tunable
280+
281+
ps = MTKParameters(sys, [p => 1.0, q => [2.0, 3.0, 4.0], r => [5.0 6.0; 7.0 8.0]])
282+
arr = canonicalize(Tunable(), ps)[1]
283+
```
284+
285+
We can reorder this to contain the value for `p`, then all values for `q`, then for `r` using:
286+
287+
```@example reorder
288+
reorder_dimension_by_tunables(sys, arr, [p, q, r])
289+
```
290+
291+
This also works with interleaved subarrays of symbolics:
292+
293+
```@example reorder
294+
reorder_dimension_by_tunables(sys, arr, [q[1], r[1, :], q[2], r[2, :], q[3], p])
295+
```
296+
297+
And arbitrary dimensions of higher dimensional arrays:
298+
299+
```@example reorder
300+
highdimarr = stack([i * arr for i in 1:5]; dims = 1)
301+
```
302+
303+
```@example reorder
304+
reorder_dimension_by_tunables(sys, highdimarr, [q[1:2], r[1, :], q[3], r[2, :], p]; dim = 2)
305+
```

0 commit comments

Comments
 (0)