# How to override defaults with u0 in solve

**URL:** <https://discourse.julialang.org/t/how-to-override-defaults-with-u0-in-solve/121629>\
**Category:** Modelling & Simulations\
**Created:** [October 23, 2024, 9:30am UTC](https://discourse.julialang.org/t/how-to-override-defaults-with-u0-in-solve/121629 "2024-10-23T09:30:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bart\_van\_de\_Lint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bart_van_de_lint/32/212161_2.png) [@Bart\_van\_de\_Lint](https://discourse.julialang.org/u/Bart_van_de_Lint)\
**Post date:** [October 23, 2024, 9:30am UTC](https://discourse.julialang.org/t/how-to-override-defaults-with-u0-in-solve/121629/1 "2024-10-23T09:30:42Z")

</div>

I have an ODEProblem with a system with default values, and with a starting state `prob.u0`. I want to be able to `solve` this problem from an arbitrary starting state u0 to the next timestep using `solve(prob, QNDF(); save_start = true, saveat=Ts)`. The problem is that solve does not use `prob.u0`, but it rather uses `defaults(prob.f.sys)` as initial values.

```julia
julia> defaults(prob.f.sys)
Dict{Any, Any} with 82 entries:
  (pos(t))[3, 5] => 24.0023
  (vel(t))[2, 10] => 0.0
  (pos(t))[2, 3] => 0.0
  (set_values(t))[1] => 0.0
  (tether_vel(t))[2] => 0.0
  (vel(t))[2, 6] => 0.0
  (pos(t))[3, 12] => 53.5791
  ⋮ => ⋮

julia> println(prob.u0)
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]

julia> @time sol = solve(prob, solver; u0=prob.u0, save_start = true, saveat=Ts)
  0.003862 seconds (310 allocations: 127.625 KiB)
retcode: Success
Interpolation: 1st order linear
t: 2-element Vector{Float64}:
 0.0
 0.05
u: 2-element Vector{Vector{Float64}}:
 [12.49482743653575, 0.3989368894537224, 24.002340185174127, 12.49482743653575, -0.3989368894537224, 24.002340185174127, 2.1788935686914535, 0.0, 24.90486745229364, 0.17453292519943295 … 0.0, 0.0, 0.0, 0.0, 57.60663544529114, 57.60663544529114, 50.0, 0.0, 0.0, 0.0]
 [12.08517813866516, 0.423952531012562, 25.969115391426246, 12.085178138665146, -0.4239525310125608, 25.96911539142627, 2.3196742729500457, -1.440317109756495e-17, 25.018474359821806, 0.18357897074375468 … 3.246285727696867, 2.680128140006882, -9.338003043785488e-14, 2.3886546391917896, 59.53250254353384, 59.53250254353384, 51.85721596131688, 67.78422267562938, 67.78422267562944, 66.79836234747967]

```

`sol.u[1]` result should be filled with ones, but it is filled with the default values.  
I tried creating a dict with u0 values, but the solver still does not start at `u0 .= 1.0`.

```julia
julia> default
Dict{SymbolicUtils.BasicSymbolic{Real}, Float64} with 52 entries:
  (pos(t))[3, 5] => 1.0
  (vel(t))[2, 10] => 1.0
  (tether_vel(t))[2] => 1.0
  (vel(t))[2, 6] => 1.0
  (pos(t))[3, 12] => 1.0
  (pos(t))[2, 9] => 1.0
  (vel(t))[1, 4] => 1.0
  (pos(t))[3, 10] => 1.0
  ⋮ => ⋮

julia> @time sol = solve(prob, solver; u0=default, save_start = true, saveat=Ts)
  0.004908 seconds (6.26 k allocations: 1.112 MiB)
retcode: Success
Interpolation: 1st order linear
t: 2-element Vector{Float64}:
 0.0
 0.05
u: 2-element Vector{Vector{Float64}}:
 [12.49482743653575, 0.3989368894537224, 24.002340185174127, 12.49482743653575, -0.3989368894537224, 24.002340185174127, 2.1788935686914535, 0.0, 24.90486745229364, 0.17453292519943295 … 0.0, 0.0, 0.0, 0.0, 57.60663544529114, 57.60663544529114, 50.0, 0.0, 0.0, 0.0]
 [12.08517813866516, 0.423952531012562, 25.969115391426246, 12.085178138665146, -0.4239525310125608, 25.96911539142627, 2.3196742729500457, -1.440317109756495e-17, 25.018474359821806, 0.18357897074375468 … 3.246285727696867, 2.680128140006882, -9.338003043785488e-14, 2.3886546391917896, 59.53250254353384, 59.53250254353384, 51.85721596131688, 67.78422267562938, 67.78422267562944, 66.79836234747967]

```

What can I do to override the defaults and use `prob.u0`?

---

<div class="post-metadata">

**Author:** ![Bart\_van\_de\_Lint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bart_van_de_lint/32/212161_2.png) [@Bart\_van\_de\_Lint](https://discourse.julialang.org/u/Bart_van_de_Lint)\
**Post date:** [October 23, 2024, 1:21pm UTC](https://discourse.julialang.org/t/how-to-override-defaults-with-u0-in-solve/121629/2 "2024-10-23T13:21:56Z")

</div>

The solution is using the integrator interface: [Integrator Interface · DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/basics/integrator/)

But still, I don’t understand why changing `prob.u0` with `solve` does not work.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [October 24, 2024, 5:02am UTC](https://discourse.julialang.org/t/how-to-override-defaults-with-u0-in-solve/121629/3 "2024-10-24T05:02:41Z")

</div>

> [@Bart\_van\_de\_Lint](#):
>
> But still, I don’t understand why changing `prob.u0` with `solve` does not work.

There is an initialization phase in the DAE solve that uses `prob.u0` as the guesses. If you don’t change the initialization problem (i.e. you bypass the SII and directly change the pieces of the generated code) then you’re not changing the initial values, just the guesses.
