FiPy is a Python-based framework for solving partial differential equations (PDEs) using the finite volume method. If you’re new to FiPy, the fastest way to “get it” is to run one small example end-to-end and understand what each piece of code is responsible for.
In this tutorial, you’ll build a minimal 1D diffusion simulation (think: heat spreading through a rod, or a concentration peak smoothing out over time). By the end, you’ll know the core FiPy workflow: mesh → variable → equation → time loop → visualization, and you’ll be able to safely tweak parameters to see how the model responds.
Before You Start: What You Need
You’ll need Python installed and a working environment that can run scientific packages. FiPy typically relies on common numerical and plotting libraries.
Quick environment checklist
- Python installed (a modern version is recommended)
- A virtual environment (recommended) to keep dependencies clean
- FiPy installed along with its numerical dependencies
- Matplotlib available for plotting
If you’re following this in a notebook, that’s fine too—just be consistent about running cells in order.
What We’re Building: A Simple 1D Diffusion Model
Diffusion is one of the best “first examples” because it’s intuitive and stable when set up correctly. You start with a field (temperature or concentration) that has a sharp feature—like a step or a spike—and diffusion gradually smooths it out.
Conceptually, you should expect the curve to flatten over time. If the peak doesn’t change at all, the model isn’t stepping forward; if the curve becomes noisy or explodes, your time step or setup likely needs adjustment.
The FiPy Building Blocks (Explained)
1) Mesh: Your simulation domain
A mesh defines the geometry and resolution of your problem. In 1D, it’s a line split into small cells. More cells generally means better detail, but also more computation.
nx: number of cellsdx: cell size (spatial step)- Domain length is roughly
nx * dx
2) Variable: The field you solve for
In FiPy, the unknown field is often stored as a CellVariable (values at cell centers). You give it an initial value (your starting condition), then FiPy updates it as the equation is solved over time.
3) Boundary conditions: What happens at the edges
Boundary conditions are essential because PDEs need “rules” at the domain boundaries. Two common patterns:
- Fixed value (Dirichlet): the field is pinned to a constant at an edge.
- No-flux (Neumann): nothing flows through the boundary (common default for diffusion demos).
For a first run, it’s often easiest to use no-flux boundaries so the solution evolves without extra forcing from the edges.
4) Equation: How FiPy represents a PDE
FiPy builds PDEs using “term objects.” For diffusion over time, the structure often looks like:
TransientTerm() == DiffusionTerm(coeff=D)
That reads as: “time change equals diffusion.” The diffusion coefficient D controls how quickly the field smooths out.
5) Time stepping: marching the solution forward
You pick a time step dt, then repeatedly solve the equation. A good dt is large enough that you see progress, but not so large that results become unstable or inaccurate.
6) Visualization: seeing what happened
You can visualize with FiPy’s viewer tools or with matplotlib. For a first example, matplotlib is simple and portable, and it also makes it easy to save plots.
Your First Working FiPy Example (1D Diffusion)
Below is a complete minimal example you can run as a script. It builds a 1D grid, creates a field with a “step” initial condition, then solves diffusion over time and plots snapshots.
Try These 6 Small Changes to Learn Faster
- Increase resolution: change
nxfrom 200 to 400 and see how the curve looks smoother. - Change diffusion speed: set
Dto 0.1 (slower) or 5.0 (faster) and compare. - Change the initial shape: replace the step with a “bump” by setting a narrow region to 1.0.
- Capture more snapshots: add more step indices into
capture_atto see the transition in detail. - Run longer: increase
stepsand confirm the profile approaches a more uniform state. - Save the plot: add
plt.savefig("diffusion.png", dpi=200)beforeplt.show().
Troubleshooting: Common “First Run” Problems
The line doesn’t change
- You might be capturing snapshots before the solve step and not capturing later moments.
stepsmight be too low, ordtmight be extremely small.- If you’re in a notebook, ensure you re-ran the whole notebook from top to bottom.
The solution looks unstable or noisy
- Try decreasing
dt(e.g., from1e-4to5e-5). - Reduce
Dor increase spatial resolution (nx) to improve behavior.
Import errors
- Confirm FiPy is installed in the environment you’re running.
- Check that your plotting/numerical libraries are installed and import correctly.
Next Steps After Your First Example
Once you can run diffusion confidently, you can level up quickly by expanding one dimension at a time:
- Move to a 2D mesh and visualize a field as an image.
- Add a source term to model heating, injection, or production.
- Try convection–diffusion to simulate transport with flow.
- Experiment with parameter sweeps and basic validation checks.
Conclusion
Your first FiPy example is less about the physics and more about learning the workflow. If you understand how mesh, variables, equations, and time stepping connect, you can build many different PDE models by swapping terms and adjusting conditions.
The best next move is to make one small change—like D, nx, or the initial condition—run it again, and explain to yourself what changed and why. That loop of “edit → run → interpret” is where FiPy starts to feel natural.