# ReadOnlyMemoryError() in an ODEProblem

**URL:** <https://discourse.julialang.org/t/readonlymemoryerror-in-an-odeproblem/74189>\
**Category:** Performance\
**Tags:** ode\
**Created:** [January 7, 2022, 12:31pm UTC](https://discourse.julialang.org/t/readonlymemoryerror-in-an-odeproblem/74189 "2022-01-07T12:31:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frazze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frazze/32/25142_2.png) [@Frazze](https://discourse.julialang.org/u/Frazze)\
**Post date:** [January 7, 2022, 12:31pm UTC](https://discourse.julialang.org/t/readonlymemoryerror-in-an-odeproblem/74189/1 "2022-01-07T12:31:18Z")

</div>

Hi everyone

I’m studying a pde (swift-hohenberg model) converting it in the ODE form.

I’ve discretized the laplacian:

```julia
function Laplacian2D(Nx, lx)
    hx = lx/Nx

	D2x = CenteredDifference(2, 2, hx, Nx)

	Qx = PeriodicBC(hx |> typeof)

	A = kron(sparse(I, Nx, Nx), sparse(D2x * Qx)[1]) + kron(sparse(D2x * Qx)[1], sparse(I, Nx, Nx))
	return A
end

```

After that I have built the function for the ODEProblem:

```julia
 function F_sh2!(du,u,p,t)
     @unpack l , d, d2 = p
     
     du .= - u .- d*u .- d2*u .+ (l .* u .- u.^3) 
     return nothing
 end

```

Where `l` is a float, `d` is the laplacian and `d2` is laplacian^2.  
For no large discretizations (64x64 grid) it’s all ok, but if I try to increase the grid, for instance 128x128, the solver returns error: `ReadOnlyMemoryError()`

I’m wondering if it’s only due to my computational limits or if I’m missing some errors.

Every help will be welcome, thank you so much!

---

<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:** [January 7, 2022, 1:39pm UTC](https://discourse.julialang.org/t/readonlymemoryerror-in-an-odeproblem/74189/2 "2022-01-07T13:39:14Z")

</div>

Please share the whole script. I cannot necessarily even know what solver was chosen from what you showed. My guess is that it switched to a method for stiff ODEs and a full Jacobian because sparsity was not specified.

---

<div class="post-metadata">

**Author:** ![Frazze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frazze/32/25142_2.png) [@Frazze](https://discourse.julialang.org/u/Frazze)\
**Post date:** [January 7, 2022, 1:54pm UTC](https://discourse.julialang.org/t/readonlymemoryerror-in-an-odeproblem/74189/3 "2022-01-07T13:54:32Z")

</div>

> [@ChrisRackauckas](#):
>
> My guess is that it switched to a method for stiff ODEs and a full Jacobian because sparsity was not specified.

I’m sorry, here my whole code.

```julia
#Domain and grid 
Nx = 128

lx = 16*pi;

# the Laplacian operator
function Laplacian2D(Nx, lx)
    hx = lx/Nx

	D2x = CenteredDifference(2, 2, hx, Nx)

	Qx = PeriodicBC(hx |> typeof)

	A = kron(sparse(I, Nx, Nx), sparse(D2x * Qx)[1]) + kron(sparse(D2x * Qx)[1], sparse(I, Nx, Nx))
	return A
end

function F_sh2!(du,u,p,t)
    @unpack l , d, d2 = p
    
    du .= - u .- d*u .- d2*u .+ (l .* u .- u.^3) 
    return nothing
end

# definition of the parameter for the PDE
del = Laplacian2D(Nx, lx)

D=2 .*del
D2=del^2; 

p=@time (l=0.1,d=D,d2=D2 );

#initial conditions and tspan
tspan = (0.0,110)
u0= rand(MersenneTwister(1234),Nx*Nx) 

#Definition of the problem
prob =@time ODEProblem(F_sh2!,u0,tspan,p);

#Solution of the problem
sol =@time solve(prob,lsoda(),abstol=1e-5,reltol=1e-3,save_everystep=false,save_start=false,dt=0.1)

```

---

<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:** [January 7, 2022, 2:03pm UTC](https://discourse.julialang.org/t/readonlymemoryerror-in-an-odeproblem/74189/4 "2022-01-07T14:03:20Z")

</div>

Yes, LSODA will be a bad idea for this code given it uses a dense Jacobian. That will make it fail with a memory issue if the problem is big enough. I would recommend just not picking an algorithm at all and letting it use the defaults.

---

<div class="post-metadata">

**Author:** ![Frazze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frazze/32/25142_2.png) [@Frazze](https://discourse.julialang.org/u/Frazze)\
**Post date:** [January 7, 2022, 2:42pm UTC](https://discourse.julialang.org/t/readonlymemoryerror-in-an-odeproblem/74189/5 "2022-01-07T14:42:08Z")

</div>

Thank you so much! Now it works. I had only to put dt not more fixed because it gave me instabilities (also if it should satisfy the CFL condition). Do you reccomend to not fix the dt? Or maybe I have to do another choice?

---

<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:** [January 7, 2022, 2:51pm UTC](https://discourse.julialang.org/t/readonlymemoryerror-in-an-odeproblem/74189/6 "2022-01-07T14:51:45Z")

</div>

That’s just the starting `dt`. I would let the heuristics find a good starting dt.

---

<div class="post-metadata">

**Author:** ![Frazze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frazze/32/25142_2.png) [@Frazze](https://discourse.julialang.org/u/Frazze)\
**Post date:** [January 7, 2022, 2:58pm UTC](https://discourse.julialang.org/t/readonlymemoryerror-in-an-odeproblem/74189/7 "2022-01-07T14:58:23Z")

</div>

Ok.  
Thank you so much for the help!
