# Reducing Numerical Diffusion in advection model using ModelingToolkit.jl and MethodOfLines.jl

**URL:** https://discourse.julialang.org/t/reducing-numerical-diffusion-in-advection-model-using-modelingtoolkit-jl-and-methodoflines-jl/117135
**Category:** Modelling & Simulations
**Tags:** modelingtoolkit, methodoflines
**Created:** [July 17, 2024, 11:36am UTC](https://discourse.julialang.org/t/reducing-numerical-diffusion-in-advection-model-using-modelingtoolkit-jl-and-methodoflines-jl/117135 "2024-07-17T11:36:39Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![el\_desesperado](https://avatars.discourse-cdn.com/v4/letter/e/9fc348/32.png) [@el\_desesperado](https://discourse.julialang.org/u/el_desesperado)
#### Post date: [July 17, 2024, 11:36am UTC](https://discourse.julialang.org/t/reducing-numerical-diffusion-in-advection-model-using-modelingtoolkit-jl-and-methodoflines-jl/117135/1 "2024-07-17T11:36:39Z")

</div>

Hello, I have been trying to model the advection equation( du(x,t)/dt + du(x,t)/dx = 0) using ModelingToolkit.jl and MethodOfLines.jl.

```julia

import OrdinaryDiffEq, ModelingToolkit, MethodOfLines, DomainSets

ModelingToolkit.@parameters t x
ModelingToolkit.@constants a = -1
ModelingToolkit.@variables u(..)

Dt = ModelingToolkit.Differential(t)
Dx = ModelingToolkit.Differential(x)

eq = Dt(u(t, x)) ~ a*Dx(u(t, x))

bcs = [u(0, x) ~ 0
    u(t, 0) ~ 10*(t>=0.2)*(t<=0.4)
    ]
    
domains = [t ∈ DomainSets.Interval(0.0, 1.0)
    x ∈ DomainSets.Interval(0.0, 1.0)]

parameters, variables)
ModelingToolkit.@named pdysys = ModelingToolkit.PDESystem(eq, bcs, domains, [t, x], [u(t, x)])

dx = 0.01

order = 2

discretization = MethodOfLines.MOLFiniteDifference([x => dx], t, approx_order = order)

prob = MethodOfLines.discretize(pdysys, discretization)

sol = OrdinaryDiffEq.solve(prob, OrdinaryDiffEq.Tsit5(), saveat=0.05)

discrete_x = sol[x]
discrete_t = sol[t]
sol_u = sol[u(t, x)]

import Plots
anim = Plots.@animate for k in eachindex(discrete_t)
    Plots.plot(discrete_x, sol_u[k, :], ylims=(0,10), label="t= $(discrete_t[k])")
end
Plots.gif(anim,"advection.gif", fps = 8)

```

I have been tryding different input signals and notices that for a rectangular wave there is a lot of numerical dissipation.  
 ![advection_tsit5_kink](https://global.discourse-cdn.com/julialang/original/3X/5/6/562baecc67fdb9a0a6b42df7fa0c4f5d8aea6012.gif)

I have tryed increasing the approximate order of the discretization, different solvers, such as DP8() and Vern9() and reducing the spatial step. Only using a smaller step removed the loss in amplitude but the shape was still rounded. Also tryed using the WENOScheme() but could not manage to implement it.

Any suggestions on how to reduce the dissipation and preserve the wave shape?

---

<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: [July 17, 2024, 12:07pm UTC](https://discourse.julialang.org/t/reducing-numerical-diffusion-in-advection-model-using-modelingtoolkit-jl-and-methodoflines-jl/117135/2 "2024-07-17T12:07:20Z")

</div>

> [@el\_desesperado](#):
>
> approximate order of the discretization

That wouldn’t do anything. Did you try decreasing the time stepping abstol and reltol?

---

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [July 17, 2024, 12:43pm UTC](https://discourse.julialang.org/t/reducing-numerical-diffusion-in-advection-model-using-modelingtoolkit-jl-and-methodoflines-jl/117135/3 "2024-07-17T12:43:44Z")

</div>

This is very classical. A FVM scheme is well suited for tjid with SSPRK intzgrator

---

<div class="post-metadata">

### Author: ![j-fu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j-fu/32/11373_2.png) [@j-fu](https://discourse.julialang.org/u/j-fu)
#### Post date: [July 17, 2024, 3:16pm UTC](https://discourse.julialang.org/t/reducing-numerical-diffusion-in-advection-model-using-modelingtoolkit-jl-and-methodoflines-jl/117135/4 "2024-07-17T15:16:52Z")

</div>

You may have a look at Trixi.jl which is devoted to hyperbolic systems and uses some sophisticated discretization approaches.  
They also have an intro example for the advection equation: [3 Introduction to DG methods · Trixi.jl](https://docs.sciml.ai/Trixi/stable/tutorials/scalar_linear_advection_1d/)

---

<div class="post-metadata">

### Author: ![el\_desesperado](https://avatars.discourse-cdn.com/v4/letter/e/9fc348/32.png) [@el\_desesperado](https://discourse.julialang.org/u/el_desesperado)
#### Post date: [July 18, 2024, 7:28am UTC](https://discourse.julialang.org/t/reducing-numerical-diffusion-in-advection-model-using-modelingtoolkit-jl-and-methodoflines-jl/117135/5 "2024-07-18T07:28:34Z")

</div>

Also tryed changing those to higher values but it did not help

---

<div class="post-metadata">

### Author: ![el\_desesperado](https://avatars.discourse-cdn.com/v4/letter/e/9fc348/32.png) [@el\_desesperado](https://discourse.julialang.org/u/el_desesperado)
#### Post date: [July 18, 2024, 7:44am UTC](https://discourse.julialang.org/t/reducing-numerical-diffusion-in-advection-model-using-modelingtoolkit-jl-and-methodoflines-jl/117135/6 "2024-07-18T07:44:11Z")

</div>

I am doing this for a university project and am somewhat required to do it using ModelingToolkit.jl and MethodOfLines.jl. From a short look at Trixi.jl it does not seem to combine with the two.

---

<div class="post-metadata">

### Author: ![el\_desesperado](https://avatars.discourse-cdn.com/v4/letter/e/9fc348/32.png) [@el\_desesperado](https://discourse.julialang.org/u/el_desesperado)
#### Post date: [July 18, 2024, 7:46am UTC](https://discourse.julialang.org/t/reducing-numerical-diffusion-in-advection-model-using-modelingtoolkit-jl-and-methodoflines-jl/117135/7 "2024-07-18T07:46:22Z")

</div>

How can that be done using the mentioned packages?

---

<div class="post-metadata">

### Author: ![Jommy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jommy/32/30917_2.png) [@Jommy](https://discourse.julialang.org/u/Jommy)
#### Post date: [July 18, 2024, 8:21am UTC](https://discourse.julialang.org/t/reducing-numerical-diffusion-in-advection-model-using-modelingtoolkit-jl-and-methodoflines-jl/117135/8 "2024-07-18T08:21:04Z")

</div>

You might want to read [Approximate Riemann Solvers](https://www.clawpack.org/riemann_book/html/Approximate_solvers.html) 🙂
