# Using \`\`\`set\_state\`\`\`?

**URL:** <https://discourse.julialang.org/t/using-set-state/68045>\
**Category:** Modelling & Simulations\
**Created:** [September 12, 2021, 10:12am UTC](https://discourse.julialang.org/t/using-set-state/68045 "2021-09-12T10:12:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![pafloxy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pafloxy/32/28694_2.png) [@pafloxy](https://discourse.julialang.org/u/pafloxy)\
**Post date:** [September 12, 2021, 10:12am UTC](https://discourse.julialang.org/t/using-set-state/68045/1 "2021-09-12T10:12:10Z")

</div>

Can someone elaborate on how to use `set_state` while using `parallel_integrator` .Specifically how exactly to specify the index of the state to be set.?  
I couldn’t interpret tht documentation provided on their website!

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 12, 2021, 11:20am UTC](https://discourse.julialang.org/t/using-set-state/68045/2 "2021-09-12T11:20:44Z")

</div>

Which package are you referring to?

---

<div class="post-metadata">

**Author:** ![pafloxy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pafloxy/32/28694_2.png) [@pafloxy](https://discourse.julialang.org/u/pafloxy)\
**Post date:** [September 12, 2021, 11:43am UTC](https://discourse.julialang.org/t/using-set-state/68045/3 "2021-09-12T11:43:51Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/5/2/52e7cf19b7e4b3ba5c884bc181cb2d3802b17a81.png)  
This one in specific, how to pass that `ì` thing on `parallel_integrator` object.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 12, 2021, 11:48am UTC](https://discourse.julialang.org/t/using-set-state/68045/4 "2021-09-12T11:48:00Z")

</div>

That’s just a regular `i`. The notation `set_state!(integ, u [, i::Int = 1])` means that `set_state!` has two methods, one accepting two arguments (`set_state!(integ, u)`) and one accepting three arguments (`set_state!(integ, u, i)`), where the third argument is constrained to be an `Int` and by default having a value of `1` if not set. In general, a method signature with a part of it in brackets `[]` like in the one you posted means that the part in brackets is optional (though you may have to set them manually if you require certain functionality, as seems to be the case with this parallel integrator of yours).

---

<div class="post-metadata">

**Author:** ![pafloxy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pafloxy/32/28694_2.png) [@pafloxy](https://discourse.julialang.org/u/pafloxy)\
**Post date:** [September 12, 2021, 11:53am UTC](https://discourse.julialang.org/t/using-set-state/68045/5 "2021-09-12T11:53:23Z")

</div>

Hi thanks!  
I am new to Julia yet but however this does not seem to work as expected

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/2/8226cc1aac4fb7d2af373be2ebf0241246e8c93e.png)  
Here `pintg` is the parallel\_integrator object , `rescaled` is the state that is to be passed in as the second `ì=2` state, however this error message appears!  
if nothing is passed as the third argument it works as usual on `i=1` !

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 12, 2021, 11:57am UTC](https://discourse.julialang.org/t/using-set-state/68045/6 "2021-09-12T11:57:37Z")

</div>

How did you create `pintg`? I suspect this isn’t a parallel integrator, since it says `SimpleDiffEq.SimpleATsit5Integrator`.

Also, posting code instead of screenshots of code will make it easier for us to help you:

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![pafloxy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pafloxy/32/28694_2.png) [@pafloxy](https://discourse.julialang.org/u/pafloxy)\
**Post date:** [September 12, 2021, 11:59am UTC](https://discourse.julialang.org/t/using-set-state/68045/7 "2021-09-12T11:59:09Z")

</div>

```julia
# parallel_integrator(chaotic_ds,[u0, u1])
## set random initial-config: u_0, perturbation: perturb, perturbed-config: u_p  
u_0 = randn(dim)  
perturb = 10^(-5)
u_p = u0 + perturb.*randn(dim)
## set parallel_integrator object: pintg
pintg = parallel_integrator(chaotic_ds,[u_0, u_p])

```

This is how I defined it

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 12, 2021, 12:01pm UTC](https://discourse.julialang.org/t/using-set-state/68045/8 "2021-09-12T12:01:15Z")

</div>

Please provide a complete example - what is `chaotic_ds`?

---

<div class="post-metadata">

**Author:** ![pafloxy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pafloxy/32/28694_2.png) [@pafloxy](https://discourse.julialang.org/u/pafloxy)\
**Post date:** [September 12, 2021, 12:04pm UTC](https://discourse.julialang.org/t/using-set-state/68045/9 "2021-09-12T12:04:54Z")

</div>

hi ! here is the main part of my code

```julia
dim = 25
p = (G1 = randn(dim,dim), G2 = randn(dim,dim,dim), dim=dim)
tspan = (0., 10.)
function deff(dx, x, p, t)
   for j = 1:p.dim
       dx[j] = p.G1[j,:]' * x + x' * p.G2[j,:,:] * x - x[j]^3
   end
   return nothing 
end

chaotic_ds = ContinuousDynamicalSystem(deff,randn(dim) ,p );
# x0 = [randn(dim), randn(dim), randn(dim)]
# deffeq = [ODEProblem(deff, x0[i], tspan, p) for i in 1:length(x0)]

```

```julia
# parallel_integrator(chaotic_ds,[u0, u1])
## set random initial-config: u_0, perturbation: perturb, perturbed-config: u_p  
u_0 = randn(dim)  
perturb = 10^(-5)
u_p = u0 + perturb.*randn(dim)
## set parallel_integrator object: pintg
pintg = parallel_integrator(chaotic_ds,[u_0, u_p])

```

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [September 12, 2021, 12:58pm UTC](https://discourse.julialang.org/t/using-set-state/68045/10 "2021-09-12T12:58:29Z")

</div>

Hello,  
thanks for the help @Sukera !

Here is a full MWE (which also defines `rescaled` as its mandatory to identify the problem):

```julia
using DynamicalSystems

dim = 25
p = (G1 = randn(dim,dim), G2 = randn(dim,dim,dim), dim=dim)
tspan = (0., 10.)
function deff(dx, x, p, t)
   for j = 1:p.dim
       dx[j] = p.G1[j,:]' * x + x' * p.G2[j,:,:] * x - x[j]^3
   end
   return nothing 
end
chaotic_ds = ContinuousDynamicalSystem(deff,randn(dim) ,p );

u_0 = randn(dim)  
perturb = 10^(-5)
u_p = u_0 + perturb.*randn(dim)

pintg = parallel_integrator(chaotic_ds,[u_0, u_p])

rescaled = u_p .* 2

set_state!(pintg, rescaled, 2)

```

Which indeed errors.

Thanks @pafloxy you have identified a bug. I’ve opened a PR to fix it: [https://github.com/JuliaDynamics/DynamicalSystemsBase.jl/pull/123](https://github.com/JuliaDynamics/DynamicalSystemsBase.jl/pull/123) After the PR is merged your code will work normally. Sorry about that!
