# Problem with stiff ODE solved by Julia

**URL:** https://discourse.julialang.org/t/problem-with-stiff-ode-solved-by-julia/92980
**Category:** General Usage
**Created:** [January 15, 2023, 4:27am UTC](https://discourse.julialang.org/t/problem-with-stiff-ode-solved-by-julia/92980 "2023-01-15T04:27:52Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![liao2877104858](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liao2877104858/32/45869_2.png) [@liao2877104858](https://discourse.julialang.org/u/liao2877104858)
#### Post date: [January 15, 2023, 4:27am UTC](https://discourse.julialang.org/t/problem-with-stiff-ode-solved-by-julia/92980/1 "2023-01-15T04:27:52Z")

</div>

There’s an ODE to be solved. It’s a chemical reaction system. There is 863 reactions and 230 species. The system is quite stiff and large. So I ues some algorithms advised by [Solving Large Stiff Equations · DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/tutorials/advanced_ode_example/). I tried every algorithms, but the values never change at each step. In another word, the ODE solver cannot catch the small change.

So I wanna to know how I can sovle such ODE system. I just want to make the values change, so the time efficiency can be considered later.

Thanks.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [January 15, 2023, 4:30am UTC](https://discourse.julialang.org/t/problem-with-stiff-ode-solved-by-julia/92980/2 "2023-01-15T04:30:50Z")

</div>

This almost ceratinly means you coded your function incorrectly. if I had to guess, it would be that you used the in place form of `f(du, u, p, t)` but didn’t update `du`, but it’s hard to say for sure without seeing the code.

---

<div class="post-metadata">

### Author: ![liao2877104858](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liao2877104858/32/45869_2.png) [@liao2877104858](https://discourse.julialang.org/u/liao2877104858)
#### Post date: [January 15, 2023, 4:33am UTC](https://discourse.julialang.org/t/problem-with-stiff-ode-solved-by-julia/92980/3 "2023-01-15T04:33:07Z")

</div>

Here is the code. It seems du will update. Whatever algorithm I try, the value will not change.

```julia
function crnn(du,c,k,t)
    c = reshape(c,(230,1))
    k = reshape(k,(863,1))
    log_r = w_in * @.log(clamp(c,1e-35,1e15))
    log_r = log_r + @.log(clamp(k,1e-35,1e15))
    r = @.exp(log_r);
    du = w_out' * r;
    return du;
end

using Symbolics
tspan = [0,1]
du0 = copy(conc[1,:])
jac_sparsity = Symbolics.jacobian_sparsity((du,u)-> crnn(du,u,k[1,:],0.0),du0,conc[1,:])
f = ODEFunction(crnn;jac_prototype=float.(jac_sparsity))
prob = ODEProblem(f, conc[1,:], tspan, k[1,:])
sol = solve(prob,KenCarp47(linsolve=KrylovJL_GMRES()))
for i in 1:230
    println(sol.u[2][i] - sol.u[1][i])
end
```

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [January 15, 2023, 4:42am UTC](https://discourse.julialang.org/t/problem-with-stiff-ode-solved-by-julia/92980/4 "2023-01-15T04:42:22Z")

</div>

Yeah. My guess was correct. You need `du .= w_out' * r;` Julia uses “pass by sharing” so when you say `du = ...` you aren’t changing `du` you’re just making a new `du`. Also the `return `du` isn’t doing anything here since you’re using the in-place formulation.

---

<div class="post-metadata">

### Author: ![dmjalal90](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmjalal90/32/45867_2.png) [@dmjalal90](https://discourse.julialang.org/u/dmjalal90)
#### Post date: [January 16, 2023, 2:10am UTC](https://discourse.julialang.org/t/problem-with-stiff-ode-solved-by-julia/92980/5 "2023-01-16T02:10:14Z")

</div>

Could you please help me to solve my problem?

Please see the post: [Forecasting time series data with neural ordinary differential equations in Julia](https://discourse.julialang.org/t/forecasting-time-series-data-with-neural-ordinary-differential-equations-in-julia/92977)

---

<div class="post-metadata">

### Author: ![dmjalal90](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmjalal90/32/45867_2.png) [@dmjalal90](https://discourse.julialang.org/u/dmjalal90)
#### Post date: [January 16, 2023, 2:12am UTC](https://discourse.julialang.org/t/problem-with-stiff-ode-solved-by-julia/92980/6 "2023-01-16T02:12:06Z")

</div>

Could you please help?

> [@Forecasting time series data with neural ordinary differential equations in Julia](https://discourse.julialang.org/t/forecasting-time-series-data-with-neural-ordinary-differential-equations-in-julia/92977):
>
> Dear all, I am a new user of Julia and following a tutorial on NeuralODE (please see the tutorial: [Experiments with Neural ODEs in Julia](https://computationalmindset.com/en/neural-networks/experiments-with-neural-odes-in-julia.html#exp2)). How can I forecast ENSO data (time series data in a CSV file) using the above tutorial? In fact, I would like to know how to use real data for NODEs in Julia. Note: I could not reproduce a tutorial, so called Forecasting the weather with neural ODEs ([Forecasting the weather with neural ODEs | Sebastian Callh personal blog](https://sebastiancallh.github.io/post/neural-ode-weather-forecast/)). Regards, Jalal

---

<div class="post-metadata">

### Author: ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)
#### Post date: [January 16, 2023, 3:29am UTC](https://discourse.julialang.org/t/problem-with-stiff-ode-solved-by-julia/92980/7 "2023-01-16T03:29:26Z")

</div>

It’s also unclear what is intended with w\_out. Is it intentionally global?

Might help to do a MWE with a small, stiff, toy system. That can help separate syntax issues from the question of how to solve stiff systems.
