Skip to content

Commit e774ea2

Browse files
docs: add documentation for parameter initialization
1 parent 2802ba9 commit e774ea2

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

docs/src/tutorials/initialization.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,87 @@ long enough you will see that `λ = 0` is required for this equation, but since
201201
problem constructor. Additionally, any warning about not being fully determined can
202202
be suppressed via passing `warn_initialize_determined = false`.
203203

204+
## Initialization of parameters
205+
206+
Parameters may also be treated as unknowns in the initialization system. Doing so works
207+
almost identically to the standard case. For a parameter to be an initialization unknown
208+
(henceforth referred to as "solved parameter") it must represent a floating point number
209+
(have a `symtype` of `Real` or `<:AbstractFloat`) or an array of such numbers. Additionally,
210+
it must have a guess and one of the following conditions must be satisfied:
211+
212+
1. The value of the parameter as passed to `ODEProblem` is an expression involving other
213+
variables/parameters. For example, if `[p => 2q + x]` is passed to `ODEProblem`. In
214+
this case, `p ~ 2q + x` is used as an equation during initialization.
215+
2. The parameter has a default (and no value for it is given to `ODEProblem`, since
216+
that is condition 1). The default will be used as an equation during initialization.
217+
3. The parameter has a default of `missing`. If `ODEProblem` is given a value for this
218+
parameter, it is used as an equation during initialization (whether the value is an
219+
expression or not).
220+
4. `ODEProblem` is given a value of `missing` for the parameter. If the parameter has a
221+
default, it will be used as an equation during initialization.
222+
223+
All parameter dependencies (where the dependent parameter is a floating point number or
224+
array thereof) also become equations during initialization, and the dependent parameters
225+
become unknowns.
226+
227+
`remake` will reconstruct the initialization system and problem, given the new
228+
constraints provided to it. The new values will be combined with the original
229+
variable-value mapping provided to `ODEProblem` and used to construct the initialization
230+
problem.
231+
232+
### Parameter initialization by example
233+
234+
Consider the following system, where the sum of two unknowns is a constant parameter
235+
`total`.
236+
237+
```@example paraminit
238+
using ModelingToolkit, OrdinaryDiffEq # hidden
239+
using ModelingToolkit: t_nounits as t, D_nounits as D # hidden
240+
241+
@variables x(t) y(t)
242+
@parameters total
243+
@mtkbuild sys = ODESystem([D(x) ~ -x, total ~ x + y], t;
244+
defaults = [total => missing], guesses = [total => 1.0])
245+
```
246+
247+
Given any two of `x`, `y` and `total` we can determine the remaining variable.
248+
249+
```@example paraminit
250+
prob = ODEProblem(sys, [x => 1.0, y => 2.0], (0.0, 1.0))
251+
integ = init(prob, Tsit5())
252+
@assert integ.ps[total] ≈ 3.0 # hide
253+
integ.ps[total]
254+
```
255+
256+
Suppose we want to re-create this problem, but now solve for `x` given `total` and `y`:
257+
258+
```@example paraminit
259+
prob2 = remake(prob; u0 = [y => 1.0], p = [total => 4.0])
260+
initsys = prob2.f.initializeprob.f.sys
261+
```
262+
263+
The system is now overdetermined. In fact:
264+
265+
```@example paraminit
266+
[equations(initsys); observed(initsys)]
267+
```
268+
269+
The system can never be satisfied and will always lead to an `InitialFailure`. This is
270+
due to the aforementioned behavior of retaining the original variable-value mapping
271+
provided to `ODEProblem`. To fix this, we pass `x => nothing` to `remake` to remove its
272+
retained value.
273+
274+
```@example paraminit
275+
prob2 = remake(prob; u0 = [y => 1.0, x => nothing], p = [total => 4.0])
276+
initsys = prob2.f.initializeprob.f.sys
277+
```
278+
279+
The system is fully determined, and the equations are solvable.
280+
281+
```@example
282+
[equations(initsys); observed(initsys)]
283+
```
284+
204285
## Diving Deeper: Constructing the Initialization System
205286

206287
To get a better sense of the initialization system and to help debug it, you can construct

0 commit comments

Comments
 (0)