Overview
This tutorial will walk you through your first Dedalus simulation step by step. We’ll solve the 2D Poisson equation with mixed boundary conditions - a simple but complete example that demonstrates the core workflow.This example should run in just a few seconds and requires no MPI (single process). It’s perfect for getting started!
Prerequisites
Before starting, make sure you have:- Dedalus installed (see Installation)
- Basic familiarity with Python and PDEs
- A text editor or Python IDE
The Problem
We’ll solve the 2D Poisson equation on a rectangular domain: With boundary conditions:- (Dirichlet condition at bottom)
- (Neumann condition at top)
- Periodic in
Complete Example Code
Create a file calledpoisson.py with the following code:
poisson.py
Step-by-Step Explanation
Import Dedalus
Import the Dedalus public interface and other required packages:The
d3 module contains all the main Dedalus classes and functions for version 3.Define Domain Parameters
Set the domain size and resolution:
Higher resolution gives more accurate results but takes longer to solve. Start with these values and experiment.
Set Up Coordinate System and Bases
Create the coordinate system and spectral bases:
- RealFourier: For periodic directions (efficient for real-valued fields)
- Chebyshev: For non-periodic directions with boundaries
- Distributor: Handles MPI parallelization automatically
Create Fields
Define the solution field and tau terms for boundary conditions:
For a scalar Laplacian with two boundaries, we need two tau terms. These enforce the boundary conditions using the tau method.
Set Up Forcing and Boundary Data
Create the right-hand side and boundary condition fields:The
'g' notation refers to grid space (as opposed to coefficient space).Define Operators
Set up differential operators and lifting:Lifting raises the tau terms to the appropriate basis for the PDE.
Formulate the Problem
Create the linear boundary value problem:
Notice how the equations are written symbolically, just like mathematical notation! Dedalus handles the discretization automatically.
lap(u): Laplacian operatorlift(...): Incorporates tau terms into the equationu(y=0): Boundary evaluation
Build and Solve
Build the solver and solve the linear system:This constructs the sparse linear system and solves it using efficient direct methods.
Running the Example
Run the script from the command line:Expected Output
You should see:- Console output: Minimal logging from Dedalus
- Image file:
poisson.pngshowing the solution field
The script should complete in just a few seconds. If it takes much longer, check that threading is disabled (
OMP_NUM_THREADS=1).Key Concepts Demonstrated
This example showcases several important Dedalus features:Symbolic Equations
Write PDEs naturally using
lap(), grad(), div() operatorsMultiple Bases
Combine Fourier and Chebyshev bases for different directions
Boundary Conditions
Enforce Dirichlet and Neumann conditions using tau method
MPI Ready
Same code works in serial or parallel with no changes
Next Example: Time Evolution
Now let’s try an initial value problem (IVP) that evolves in time. Here’s the 1D KdV-Burgers equation:kdv_burgers.py
Key Differences for Time Evolution
- IVP instead of LBVP: Initial Value Problem for time-dependent equations
- Timestepper: Use
d3.SBDF2(semi-implicit backward differentiation) - Main loop: Iterate with
solver.step()untilsolver.proceedis False - Dealiasing: Set
dealias=3/2to prevent aliasing errors in nonlinear terms
Common Patterns
Problem Types
- IVP
- LBVP
- EVP
Initial Value Problem - time evolution:
Useful Operators
Tips and Best Practices
Choosing resolution
Choosing resolution
Start with moderate resolution and increase until results converge:
- Monitor spectral coefficients to check if they decay to zero
- Use
field.allgather_data('c')to examine coefficients - Increase resolution by factors of 2 for testing
Handling nonlinear terms
Handling nonlinear terms
For equations with nonlinear terms like :
- Always use dealiasing:
dealias=3/2 - Write as:
u*dx(u)oru@grad(u)for vectors - Dedalus automatically handles the dealiasing
Saving data
Saving data
Use the built-in analysis system for large simulations:Data is saved to HDF5 files that can be read with h5py or xarray.
Performance optimization
Performance optimization
- Always set
OMP_NUM_THREADS=1 - Use MPI for parallelism:
mpiexec -n N - Profile with:
python3 -m cProfile -o output.prof script.py - Monitor with:
flow.add_property()for runtime diagnostics
Where to Go Next
Example Gallery
Browse complete examples including fluid dynamics, eigenvalue problems, and more
Core Concepts
Deep dive into bases, operators, boundary conditions, and tau method
API Reference
Complete documentation of all classes and functions
Community
Join the mailing list to ask questions and share your work
Troubleshooting
If you encounter issues:- Check threading:
echo $OMP_NUM_THREADSshould return 1 - Verify installation:
python3 -m dedalus test - Check MPI: Run with
-n 1first, then try parallel - Examine logs: Dedalus uses Python logging for diagnostics
- Ask for help: Post to the mailing list
Remember: Most performance issues are due to threading not being disabled. Always check
OMP_NUM_THREADS=1!