The convection diffusion equation


In [8]:
import numpy as np
import pylab as pl
import triflow as trf
from scipy.signal import gaussian

%matplotlib inline

The convection–diffusion equation is a combination of the diffusion and convection (advection) equations, and describes physical phenomena where particles, energy, or other physical quantities are transferred inside a physical system due to two processes: diffusion and convection. (Wikipedia)

The equation reads

$$\partial_{t}U = k \partial_{xx} U - c \partial_{x} U$$

with

  • $U$ the physical quantities transferred (it could be a chemical species concentration, the temperature of a fluid...)
  • $k$ a diffusion convection
  • $c$ a velocity, which will be constant in our example.

In [9]:
model = trf.Model("k * dxxU - c * dxU", "U", ["k", "c"])

We discretize our spatial domain. We want periodic condition, so endpoint=True exclude the final node (which will be redondant with the first node, $x=0$ and $x=100$ are merged)


In [10]:
x = np.linspace(0, 100, 500, endpoint=False)

We initialize with three gaussian pulses for the initial condition


In [11]:
U = (np.roll(gaussian(x.size, 10), x.size // 5) +
     np.roll(gaussian(x.size, 10), -x.size // 5) -
     gaussian(x.size, 20))

fields = model.fields_template(x=x, U=U)

pl.figure(figsize=(15, 4))
pl.plot(fields.x, fields.U)
pl.xlim(0, fields.x.max())
pl.show()


We precise our parameters. The default scheme provide an automatic time_stepping. We set the periodic flag to True.


In [12]:
parameters = dict(k=.2, c=10, periodic=True)

We initialize the simulation.


In [13]:
%%opts Curve [show_grid=True, width=800] {-framewise}
simulation = trf.Simulation(model, fields, parameters, dt=.1, tmax=30)
container = simulation.attach_container()
trf.display_fields(simulation)


Out[13]:

We iterate on the simulation until the end.


In [14]:
result = simulation.run()




In [15]:
container.data.U.plot()


Out[15]:
<matplotlib.collections.QuadMesh at 0x7f349d36ac88>