|
57 | 57 | ```
|
58 | 58 |
|
59 | 59 | SciMLSensitivity does not know how to handle the parameter object, because it does not
|
60 |
| -implement the SciMLStructures interface. |
| 60 | +implement the SciMLStructures interface. The bare minimum necessary for SciMLSensitivity |
| 61 | +is the `Tunable` portion. |
61 | 62 |
|
62 | 63 | ```@example
|
63 | 64 | import SciMLStructures as SS
|
@@ -119,3 +120,57 @@ Zygote.gradient(0.1ones(length(SS.canonicalize(SS.Tunable(), p)[1]))) do tunable
|
119 | 120 | return sum(sol.u[end])
|
120 | 121 | end
|
121 | 122 | ```
|
| 123 | + |
| 124 | +We can also implement a `Constants` portion to store the rest of the values: |
| 125 | + |
| 126 | +```@example |
| 127 | +SS.hasportion(::SS.Constants, ::Parameters) = true |
| 128 | +
|
| 129 | +function SS.canonicalize(::SS.Constants, p::Parameters) |
| 130 | + buffer = mapreduce(vcat, p.subparams) do subpar |
| 131 | + [subpar.q, subpar.r] |
| 132 | + end |
| 133 | + repack = let p = p |
| 134 | + function repack(newbuffer) |
| 135 | + SS.replace(SS.Constants(), p, newbuffer) |
| 136 | + end |
| 137 | + end |
| 138 | +
|
| 139 | + return buffer, repack, false |
| 140 | +end |
| 141 | +
|
| 142 | +function SS.replace(::SS.Constants, p::Parameters, newbuffer) |
| 143 | + subpars = [SubproblemParameters(p.subparams[i].p, newbuffer[2i-1], newbuffer[2i]) for i in eachindex(p.subparams)] |
| 144 | + return Parameters(subpars, p.coeffs) |
| 145 | +end |
| 146 | +
|
| 147 | +function SS.replace!(::SS.Constants, p::Parameters, newbuffer) |
| 148 | + for i in eachindex(p.subparams) |
| 149 | + p.subparams[i].q = newbuffer[2i-1] |
| 150 | + p.subparams[i].r = newbuffer[2i] |
| 151 | + end |
| 152 | + return p |
| 153 | +end |
| 154 | +
|
| 155 | +buf, repack, alias = SS.canonicalize(SS.Constants(), p) |
| 156 | +buf |
| 157 | +``` |
| 158 | + |
| 159 | +```@example |
| 160 | +repack(ones(length(buf))) |
| 161 | +``` |
| 162 | + |
| 163 | +```@example |
| 164 | +SS.replace(SS.Constants(), p, ones(length(buf))) |
| 165 | +``` |
| 166 | + |
| 167 | +```@example |
| 168 | +SS.replace!(SS.Constants(), p, ones(length(buf))) |
| 169 | +p |
| 170 | +``` |
| 171 | + |
| 172 | +In general, all values belonging to a portion should be concatenated into an array of the |
| 173 | +appropriate length in `canonicalize`. If a higher dimensional array is part of the portion, |
| 174 | +it should be flattened. If a portion contains values of multiple types, a non-concrete |
| 175 | +array should be used to store the values. `replace` and `replace!` should assume the array |
| 176 | +they receive have the same ordering as the one returned from `canonicalize`. |
0 commit comments