Skip to content

Commit 2a56227

Browse files
authored
Merge pull request #2277 from SciML/myb/init
Add `u0_constructor` kwargs
2 parents 05525b5 + 40c828a commit 2a56227

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

docs/src/basics/AbstractSystem.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ The values which are common to all `AbstractSystem`s are:
5656
Optionally, a system could have:
5757

5858
- `observed(sys)`: All observed equations of the system and its subsystems.
59+
- `independent_variables(sys)`: The independent variables of a system.
60+
- `defaults(sys)`: A `Dict` that maps variables/parameters into their default values for the system and its subsystems.
5961
- `get_observed(sys)`: Observed equations of the current-level system.
6062
- `get_continuous_events(sys)`: `SymbolicContinuousCallback`s of the current-level system.
61-
- `get_defaults(sys)`: A `Dict` that maps variables into their default values.
62-
- `independent_variables(sys)`: The independent variables of a system.
63+
- `get_defaults(sys)`: A `Dict` that maps variables into their default values
64+
for the current-level system.
6365
- `get_noiseeqs(sys)`: Noise equations of the current-level system.
6466
- `get_metadata(sys)`: Any metadata about the system or its origin to be used by downstream packages.
6567

docs/src/basics/FAQ.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,19 @@ julia> ModelingToolkit.missing_variable_defaults(sys, [1,2,3])
129129
x2ˍtt(t) => 2
130130
x3ˍtt(t) => 3
131131
```
132+
133+
## Change the state vector type
134+
135+
Use the `u0_constructor` keyword argument to map an array to the desired
136+
container type. For example:
137+
138+
```
139+
using ModelingToolkit, StaticArrays
140+
@variables t
141+
sts = @variables x1(t)=0.0
142+
D = Differential(t)
143+
eqs = [D(x1) ~ 1.1 * x1]
144+
@named sys = ODESystem(eqs, t)
145+
sys = structural_simplify(sys)
146+
prob = ODEProblem{false}(sys, [], (0,1); u0_constructor = x->SVector(x...))
147+
```

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,7 @@ function process_DEProblem(constructor, sys::AbstractODESystem, u0map, parammap;
797797
use_union = true,
798798
tofloat = true,
799799
symbolic_u0 = false,
800+
u0_constructor = identity,
800801
kwargs...)
801802
eqs = equations(sys)
802803
dvs = states(sys)
@@ -809,6 +810,9 @@ function process_DEProblem(constructor, sys::AbstractODESystem, u0map, parammap;
809810
tofloat,
810811
use_union,
811812
symbolic_u0)
813+
if u0 !== nothing
814+
u0 = u0_constructor(u0)
815+
end
812816

813817
p, split_idxs = split_parameters_by_type(p)
814818
if p isa Tuple

test/odesystem.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ prob = ODEProblem(sys)
438438
sol = solve(prob, Tsit5())
439439
@test sol.t[end] == tspan[end]
440440
@test sum(abs, sol[end]) < 1
441+
prob = ODEProblem{false}(sys; u0_constructor = x -> SVector(x...))
442+
@test prob.u0 isa SVector
441443

442444
# check_eqs_u0 kwarg test
443445
@parameters t

0 commit comments

Comments
 (0)