title: Implementing code for zero flux condition in Advection-Diffusion equation
body: <p>I would like to know how to implement a zero flux condition for the avdection-diffusion equation defined by:</p>
  
  <p><img src="https://latex.codecogs.com/gif.latex?%5Cfrac%7B%5Cpartial&space;u%7D%7B%5Cpartial&space;t%7D&space;=&space;D&space;%5Cfrac%7B%5Cpartial%5E2&space;u%7D%7B%5Cpartial&space;x%5E2%7D&space;&plus;&space;c%5Cfrac%7B%5Cpartial&space;u%7D%7B%5Cpartial&space;x%7D" alt="Difussion"></p>
  
  <p>Analysing the above we can realise that zero flux condition is satisfied when:</p>
  
  <p><img src="https://latex.codecogs.com/gif.latex?D&space;%5Cfrac%7B%5Cpartial&space;u%7D%7B%5Cpartial&space;x%7D&space;&plus;&space;c&space;u=&space;0" alt="BC">.</p>
  
  <p>So, I write a code using finite difference scheme:</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  
  nx = 101
  dx = 0.01
  nt = 200
  c = -1.
  D = .1
  dt = 0.0001
  
  u = np.zeros(nx)
  u[0:50] = 2
  un = u.copy()
  
  for n in range(nt):
      for i in range(1, nx-1):
      un = u.copy()
      u[i] = un[i] + D * dt /dx**2* (un[i+1]-2*un[i]+un[i-1])-\
             c*dt/dx*(un[i] - un[i-1])
  
      u[0] =  u[1]*(c*dx+D)/D #BC zero flux at left side
  
  plt.plot(np.linspace(0, 1, 101), u)
  plt.ylim(0, 4)
  plt.xlim(0, 1.)
  plt.show()
  </code></pre>
  
  <p>Where  </p>
  
  <pre><code>u[1]*(c*dx+D)/D
  </code></pre>
  
  <p>represent the zero flux condition which result from:</p>
  
  <p><img src="https://latex.codecogs.com/gif.latex?D%5Cfrac%7Bu%5E%7Bt%7D_%7Bnx&plus;1%7D&space;-&space;u%5E%7Bt%7D_%7Bnx%7D&space;%7D%7B%5CDelta&space;t%7D&space;&plus;&space;c&space;u%5E%7Bt%7D_%7Bnx&plus;1%7D&space;=0&space;%5C&space;u%5E%7Bt%7D_%7Bnx%7D&space;=u%5Et_%7Bnx&plus;1%7D&space;%5Cfrac%7Bc*%5CDelta&space;x&space;&plus;&space;D%7D%7BD%7D" alt=""></p>
  
  <p>However, the result does not satisfy the zero flux condition, so the mass is not conservative along the time.</p>
  
  <p>Anybody can help me to detect my mistakes?</p>
  
  <p>Thanks in advance,</p>