# Errors in Discretization in ModelingToolkit.jl

**URL:** https://discourse.julialang.org/t/errors-in-discretization-in-modelingtoolkit-jl/124403
**Category:** Modelling & Simulations
**Created:** [January 3, 2025, 8:26am UTC](https://discourse.julialang.org/t/errors-in-discretization-in-modelingtoolkit-jl/124403 "2025-01-03T08:26:08Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![JustPickedThisUp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/justpickedthisup/32/214435_2.png) [@JustPickedThisUp](https://discourse.julialang.org/u/JustPickedThisUp)
#### Post date: [January 3, 2025, 8:26am UTC](https://discourse.julialang.org/t/errors-in-discretization-in-modelingtoolkit-jl/124403/1 "2025-01-03T08:26:08Z")

</div>

Hello,  
I’ve been trying to solve this time dependent Schrodingers equation with these conditions:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/8/08f642ce17583869382aff730efda8ad20e19982.png)

It keeps throwing up errors like `AssertionError: There must be the same number of equations and unknowns, got 2 equations and 1 unknowns`

```julia
## Loading packages

using OrdinaryDiffEq, ModelingToolkit, MethodOfLines, DomainSets

@parameters t x 
@variables u(..)

Dt = Differential(t) # Dt will now assign as the dt of something i.e. Dt can be assigned to u as du/dt
Dxx = Differential(x)^2 #Dxx will now assign as the d^2x of something i.e. Dxx can assign to u as d^2u/dx^2 basically
V(x) = x^2
eq = im*Dt(u(t,x)) ~ Dxx(u(t,x)) + V(x)*u(t,x)

domains = [x ∈ Interval(0, 1),
           t ∈ Interval(0,1)]

bcs = [u(0,x) ~ sin(2*pi*x),
       u(t,0) ~ 0,
       u(t,1) ~ 0]

@named pdesys = PDESystem(eq,bcs,domains,[t,x],[u(t,x)])

dx = 0.1 # When dx very small, it gets more exact but time taking
order = 2 # Based on some theory; dig on this
discretization = MOLFiniteDifference([x=>dx], t, approx_order=order)
# discretization makes the PDE into ODE using Method of lines; read pdf

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

# Solve ODE problem
using OrdinaryDiffEq
sol = solve(prob, Tsit5(), saveat=0.2)

```

Or `Sym doesn't have a operation or arguments!` if I keep initial conditions separate as:

```julia
## Loading packages

using OrdinaryDiffEq, ModelingToolkit, MethodOfLines, DomainSets

@parameters t x 
@variables u(..)

Dt = Differential(t) # Dt will now assign as the dt of something i.e. Dt can be assigned to u as du/dt
Dxx = Differential(x)^2 #Dxx will now assign as the d^2x of something i.e. Dxx can assign to u as d^2u/dx^2 basically
V(x) = x^2
eq = im*Dt(u(t,x)) ~ Dxx(u(t,x)) + V(x)*u(t,x)

domains = [x ∈ Interval(0, 1),
           t ∈ Interval(0,1)]

ics = [u(0,x) ~ sin(2*pi*x)]
bcs = [u(t,0) ~ 0,
       u(t,1) ~ 0]

@named pdesys = PDESystem(eq,bcs,domains,[t,x],[u(t,x)])

dx = 0.1 # When dx very small, it gets more exact but time taking
order = 2 # Based on some theory; dig on this
discretization = MOLFiniteDifference([x=>dx], t, approx_order=order)
# discretization makes the PDE into ODE using Method of lines; read pdf

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

# Solve ODE problem
using OrdinaryDiffEq
sol = solve(prob, Tsit5(), saveat=0.2)

discrete_x = sol[x]
discrete_y = sol[y]
discrete_t = sol[t]

solu = sol[u(x, y, t)]
solv = sol[v(x, y, t)]

using Plots
anim = @animate for k in 1:length(discrete_t)
    heatmap(solu[2:end, 2:end, k], title="$(discrete_t[k])") # 2:end since end = 1, periodic condition
end
gif(anim, "Brusselator2Dsol_u.gif", fps = 8)

anim = @animate for k in 1:length(discrete_t)
    heatmap(solv[2:end, 2:end, k], title="$(discrete_t[k])")
end
gif(anim, "Brusselator2Dsol_v.gif", fps = 8)

```

I would also like to have V(x) as something other than 0.0. How do I include it? As a constant using `@constants`?

---

<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 4, 2025, 3:34am UTC](https://discourse.julialang.org/t/errors-in-discretization-in-modelingtoolkit-jl/124403/2 "2025-01-04T03:34:57Z")

</div>

Did you try using complex variables? I’m not sure the tracing on with Julia-level complex `im` would produce the expected equation at this time.

---

<div class="post-metadata">

### Author: ![JustPickedThisUp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/justpickedthisup/32/214435_2.png) [@JustPickedThisUp](https://discourse.julialang.org/u/JustPickedThisUp)
#### Post date: [January 4, 2025, 8:45pm UTC](https://discourse.julialang.org/t/errors-in-discretization-in-modelingtoolkit-jl/124403/3 "2025-01-04T20:45:00Z")

</div>

Sorry, what do you mean by complex variables? Any documentation on introducing complex numbers such as i seems to only mention the `im`.

---

<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 5, 2025, 6:18am UTC](https://discourse.julialang.org/t/errors-in-discretization-in-modelingtoolkit-jl/124403/4 "2025-01-05T06:18:29Z")

</div>

`@variables x::Complex`

---

<div class="post-metadata">

### Author: ![JustPickedThisUp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/justpickedthisup/32/214435_2.png) [@JustPickedThisUp](https://discourse.julialang.org/u/JustPickedThisUp)
#### Post date: [January 19, 2025, 9:08pm UTC](https://discourse.julialang.org/t/errors-in-discretization-in-modelingtoolkit-jl/124403/5 "2025-01-19T21:08:42Z")

</div>

Still getting the `AssertionError: There must be the same number of equations and unknowns, got 2 equations and 1 unknowns`

```julia

using OrdinaryDiffEq, ModelingToolkit, MethodOfLines, DomainSets

@parameters t x 
@variables u(..)
@variables i::Complex

Dt = Differential(t) 
Dxx = Differential(x)^2 
eq = i*Dt(u(t,x)) ~ Dxx(u(t,x))

bcs = [u(0,x) ~ sin(2*pi*x),
    u(t,0) ~ 0,
    u(t,1) ~ 0]

domains = [x ∈ Interval(0, 1),
           t ∈ Interval(0,1)]

@named pdesys = PDESystem(eq,bcs,domains,[t,x],[u(t,x)])

dx = 0.1 
order = 2 
discretization = MOLFiniteDifference([x=>dx], t)

prob = discretize(pdesys,discretization)

```

I assume the issue is now with an equation, but I can’t find it at all.

---

<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 21, 2025, 10:25am UTC](https://discourse.julialang.org/t/errors-in-discretization-in-modelingtoolkit-jl/124403/6 "2025-01-21T10:25:17Z")

</div>

Open an issue on MethodOfLines.jl for this.
