# Discretization of semi-coupled wave and diffusion equations fails for step size of pi/(25n) using MethodOfLines.jl

**URL:** <https://discourse.julialang.org/t/discretization-of-semi-coupled-wave-and-diffusion-equations-fails-for-step-size-of-pi-25n-using-methodoflines-jl/106091>\
**Category:** Modelling & Simulations\
**Tags:** physics, simulations, methodoflines\
**Created:** [November 11, 2023, 8:57pm UTC](https://discourse.julialang.org/t/discretization-of-semi-coupled-wave-and-diffusion-equations-fails-for-step-size-of-pi-25n-using-methodoflines-jl/106091 "2023-11-11T20:57:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![NonDairyNeutrino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nondairyneutrino/32/221496_2.png) [@NonDairyNeutrino](https://discourse.julialang.org/u/NonDairyNeutrino)\
**Post date:** [November 11, 2023, 8:57pm UTC](https://discourse.julialang.org/t/discretization-of-semi-coupled-wave-and-diffusion-equations-fails-for-step-size-of-pi-25n-using-methodoflines-jl/106091/1 "2023-11-11T20:57:38Z")

</div>

# Intro

I’m using a semi-coupled system of wave and diffusion equations to simulate a string whose mass/speed of sound diffuses (as a toy to familiarize myself with `MethodOfLines.jl` for my master’s thesis in high-performance computational physics). The initial profiles are both normal distributions, and as such I choose the spatial domain to be of length 4\*pi. But I encounter an error in the discretization.

Given a step size `dx = pi / n`, a warning is issued

> Warning: dx for x does not divide domain exactly, adding grid point at x = 6.283185307179586).

followed by a `BoundsError` such as

> ERROR: LoadError: BoundsError: attempt to access 401-element Vector{SymbolicUtils.BasicSymbolic{Real}} at index  
> [CartesianIndex{1}[CartesianIndex(0,), CartesianIndex(1,)]]

any time `n` is **a multiple of 25** , as far as I have tested. Additionally, no flags are raised when `n = 5`. I tested for multiple different dependencies on `n`, e.g. multiples of 5 not equal to multiples of 25, ±1 around multiples of 25, and all have worked just fine.

I’ve tried using the VSCode debugger, but in this particular instance, it quickly gets over my head.

# Code (does not work)

It appears the `BoundsError` is raised from within `discretize`. As such I have omitted the later code to actually get and visualize the solution.

Note: `dx = pi / 25.`

```julia-auto
using OrdinaryDiffEq, ModelingToolkit, MethodOfLines, DomainSets

# independent and dependent variables
@parameters t x # t - time, x - space
@variables u(..) c(..) # u - wave amplitude, c - speed of sound

# differential operators
Dt = Differential(t) # first order time derivative operator
Dtt = Differential(t)^2 # second order time derivative operator
Dx = Differential(x) # first order spatial derivative operator
Dxx = Differential(x)^2 # second order spatial derivative operator

# governing equations
eqwave = Dtt(u(t, x)) ~ c(t, x)^2 * Dxx(u(t, x)) # wave equation with variable speed
eqspeed = Dt(c(t, x)) ~ Dxx(c(t, x)) # diffusion equation for mass/speed

# boundary conditions
bcs = [
    # initial conditions
       u(0, x) ~ exp(-x^2), # initial spatial wave profile
    Dt(u(0, x)) ~ 0., # initial wave velocity
       c(0, x) ~ exp(-x^2), # initial spatial speed profile
    # boundary conditions (optional)
    Dx(u(t, -2pi)) ~ 0., # left side is rigid
       u(t, 2pi) ~ 0. # right side held at zero
]

# Space and time domains
domains = [
    t ∈ Interval(0.0, 20.0), # arbitrary time domain
    x ∈ Interval(-2 * pi, 2 * pi) # arbitrary spatial domain
]

# PDE system
@named pdesys = PDESystem([equ, eqc], bcs, domains, [t, x], [u(t, x), c(t, x)])

# Method of lines discretization
dx = pi / 25. # doesn't work when denominator is a multiple of 25
discretization = MOLFiniteDifference([x => dx], t)

# Convert the PDE problem into an ODE problem
prob = discretize(pdesys, discretization)

```

# Versions

## Packages

`DomainSets` has been held back from updating due to compatibility constraints.

> [0c46a032] DifferentialEquations v7.11.0  
> ⌅ [5b8099bc] DomainSets v0.6.7  
> [7a1cc6ca] FFTW v1.7.1  
> [94925ecb] MethodOfLines v0.10.1  
> [961ee093] ModelingToolkit v8.72.2  
> [1dea7af3] OrdinaryDiffEq v6.59.0

## Julia

> 1.9.3+0.x64.w64.mingw32

---

<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:** [November 11, 2023, 9:03pm UTC](https://discourse.julialang.org/t/discretization-of-semi-coupled-wave-and-diffusion-equations-fails-for-step-size-of-pi-25n-using-methodoflines-jl/106091/2 "2023-11-11T21:03:07Z")

</div>

ahh this is likely a floating point error thing. Can you open an issue?

---

<div class="post-metadata">

**Author:** ![NonDairyNeutrino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nondairyneutrino/32/221496_2.png) [@NonDairyNeutrino](https://discourse.julialang.org/u/NonDairyNeutrino)\
**Post date:** [November 11, 2023, 9:09pm UTC](https://discourse.julialang.org/t/discretization-of-semi-coupled-wave-and-diffusion-equations-fails-for-step-size-of-pi-25n-using-methodoflines-jl/106091/3 "2023-11-11T21:09:24Z")

</div>

Of course! I’ll do that now.

---

<div class="post-metadata">

**Author:** ![xtalax](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xtalax/32/35293_2.png) [@xtalax](https://discourse.julialang.org/u/xtalax)\
**Post date:** [November 17, 2023, 12:59am UTC](https://discourse.julialang.org/t/discretization-of-semi-coupled-wave-and-diffusion-equations-fails-for-step-size-of-pi-25n-using-methodoflines-jl/106091/4 "2023-11-17T00:59:43Z")

</div>

Yep, floating point, this often happens where pi is involved. I haven’t seen the bounds error though, I will investigate

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 22, 2025, 7:35am UTC](https://discourse.julialang.org/t/discretization-of-semi-coupled-wave-and-diffusion-equations-fails-for-step-size-of-pi-25n-using-methodoflines-jl/106091/5 "2025-09-22T07:35:49Z")

</div>

Can you share the link to the issue?

---

<div class="post-metadata">

**Author:** ![NonDairyNeutrino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nondairyneutrino/32/221496_2.png) [@NonDairyNeutrino](https://discourse.julialang.org/u/NonDairyNeutrino)\
**Post date:** [September 22, 2025, 3:16pm UTC](https://discourse.julialang.org/t/discretization-of-semi-coupled-wave-and-diffusion-equations-fails-for-step-size-of-pi-25n-using-methodoflines-jl/106091/6 "2025-09-22T15:16:06Z")

</div>

Should have done this originally. Thanks! [Link to issue on GitHub](https://github.com/SciML/MethodOfLines.jl/issues/337).

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [September 22, 2025, 5:53pm UTC](https://discourse.julialang.org/t/discretization-of-semi-coupled-wave-and-diffusion-equations-fails-for-step-size-of-pi-25n-using-methodoflines-jl/106091/7 "2025-09-22T17:53:04Z")

</div>

@NonDairyNeutrino Thx! Much appreciated.
