# How to simulate 3D diffusion in python?

I want to simulate a simple 3D diffusion (e.g., an ink released from one side of a vessel) using SciPy. There are some tutorials for one-dimensional diffusion. Although the ink goes in one direction, it will not be straight. I appreciate if you can help me to consider the spread of the ink along with other dimensions too.

Solving the Fick's second law in one-dimension gives a straightforward equation to draw the concentration profiles against x at different times. How to compute the profile of c(x,y,z,t) at different times?

If introducing the code in any other programming language, I can transform it into python. I just want to understand the algorithm structure.

• Welcome to Scicomp.SE! It's not quite clear what you're asking here. Are you confused about the mathematical model (en.wikipedia.org/wiki/Convection-diffusion_equation), numerical solution of that PDE, or how to implement the numerical methods in Python? If you can update your question with what specifically you're having trouble with and what you have tried, then we can give you a more helpful answer. This page may be helpful for editing your question into one that is on-topic for this site: scicomp.stackexchange.com/help – Tyler Olsen Aug 14 '17 at 19:40
• @TylerOlsen Thanks for the welcome. I wish to find the mathematical equation for the 3D concentration profile at different times and how to implement it into an algorithm (it can be python or any other language). To be honest, my weak point is the math skills here. – Kama Aug 14 '17 at 20:11
• As you say, there are many tutorials describing the 1D case. The reason for this is that if you have a good understanding of the 1D case, the extension to two or three dimensions is straightforward. So I strongly suggest starting there. – Bill Greene Aug 14 '17 at 21:28

You just add the diffusion along the other dimensions. This superposition from orthogonal directions makes some sense, as they are independent.

So, going by wikipedia for Fick's second law of diffusion in 1D:

$$\frac{\partial \psi}{\partial t} = D \frac{\partial^2 \psi}{\partial x^2}$$

We extend it to 2d as:

$$\frac{\partial \psi}{\partial t} = D \frac{\partial^2 \psi}{\partial x^2} + D \frac{\partial^2 \psi}{\partial y^2}$$

The second derivative is called the "Laplacian operator", and for vector calculus (more than 1D) you may see it notated as $\nabla^2$. When applied to a scalar value, as here, it represents the sum of the partial differentials with respect to each dimension.

You need to "discretize" this. (I might add details here later.)

Math, discretization and Python code for 1D diffusion (step 3) and for 2D diffusion (step 7)

I think once you've seen the 2D case, extending it to 3D will be easy.

HOWEVER This diffusion won't be very interesting, just a circle (or sphere in 3d) with higher concentration ("density") in the center spreading out over time - like heat diffusing through uniform metal. Although I've addressed your specific question about multi-dimensional diffusion here, from "ink released from one side of a vessel" and the #advection-diffusion tag it may be that you're wanting to simulate diffusion through a fluid, which requires a fluid simulation, which is of course more complex.

In that case, I'd suggest looking at the full 12 steps of the above, as a start.