devito
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDevito - Symbolic PDE Solver
Devito - 符号型偏微分方程求解器
Quick Reference
快速参考
python
from devito import Grid, Function, TimeFunction, Eq, solve, Operatorpython
from devito import Grid, Function, TimeFunction, Eq, solve, OperatorCreate grid
Create grid
grid = Grid(shape=(101, 101), extent=(1000., 1000.))
grid = Grid(shape=(101, 101), extent=(1000., 1000.))
Velocity model
Velocity model
v = Function(name='v', grid=grid, space_order=4)
v.data[:] = 1500.
v = Function(name='v', grid=grid, space_order=4)
v.data[:] = 1500.
Wavefield
Wavefield
p = TimeFunction(name='p', grid=grid, time_order=2, space_order=4)
p = TimeFunction(name='p', grid=grid, time_order=2, space_order=4)
Wave equation: d2p/dt2 = v^2 * laplacian(p)
Wave equation: d2p/dt2 = v^2 * laplacian(p)
stencil = Eq(p.forward, solve(p.dt2 - v**2 * p.laplace, p.forward))
stencil = Eq(p.forward, solve(p.dt2 - v**2 * p.laplace, p.forward))
Compile and run
Compile and run
op = Operator([stencil])
op(time_M=100, dt=0.5)
undefinedop = Operator([stencil])
op(time_M=100, dt=0.5)
undefinedKey Classes
核心类
| Class | Purpose |
|---|---|
| Computational domain definition |
| Spatial field on grid |
| Time-dependent field |
| Point sources/receivers |
| Compiled computation kernel |
| 类 | 用途 |
|---|---|
| 计算域定义 |
| 网格上的空间场 |
| 随时间变化的场 |
| 点源/接收器 |
| 编译后的计算内核 |
Essential Operations
基础操作
Grid and Fields
网格与场
python
from devito import Grid, Function, TimeFunctionpython
from devito import Grid, Function, TimeFunction2D/3D Grid
2D/3D Grid
grid = Grid(shape=(nx, nz), extent=(x_size, z_size))
grid = Grid(shape=(nx, nz), extent=(x_size, z_size))
Velocity model (spatial field)
Velocity model (spatial field)
v = Function(name='v', grid=grid, space_order=4)
v.data[:] = 1500.
v = Function(name='v', grid=grid, space_order=4)
v.data[:] = 1500.
Wavefield (time-dependent)
Wavefield (time-dependent)
p = TimeFunction(name='p', grid=grid, time_order=2, space_order=4)
undefinedp = TimeFunction(name='p', grid=grid, time_order=2, space_order=4)
undefinedSource and Receivers
源与接收器
python
from examples.seismic import RickerSource, Receiver, TimeAxis
time_range = TimeAxis(start=0., stop=1000., step=dt)python
from examples.seismic import RickerSource, Receiver, TimeAxis
time_range = TimeAxis(start=0., stop=1000., step=dt)Source
Source
src = RickerSource(name='src', grid=grid, f0=10., npoint=1, time_range=time_range)
src.coordinates.data[0, :] = [500., 20.]
src = RickerSource(name='src', grid=grid, f0=10., npoint=1, time_range=time_range)
src.coordinates.data[0, :] = [500., 20.]
Receivers
Receivers
rec = Receiver(name='rec', grid=grid, npoint=101, time_range=time_range)
rec.coordinates.data[:, 0] = np.linspace(0., 1000., 101)
rec.coordinates.data[:, 1] = 20.
undefinedrec = Receiver(name='rec', grid=grid, npoint=101, time_range=time_range)
rec.coordinates.data[:, 0] = np.linspace(0., 1000., 101)
rec.coordinates.data[:, 1] = 20.
undefinedBuild and Run
构建与运行
python
undefinedpython
undefinedWave equation
Wave equation
stencil = Eq(p.forward, solve(p.dt2 - v2 * p.laplace, p.forward))
src_term = src.inject(field=p.forward, expr=src * dt2 * v**2)
rec_term = rec.interpolate(expr=p)
stencil = Eq(p.forward, solve(p.dt2 - v2 * p.laplace, p.forward))
src_term = src.inject(field=p.forward, expr=src * dt2 * v**2)
rec_term = rec.interpolate(expr=p)
Compile and execute
Compile and execute
op = Operator([stencil] + src_term + rec_term)
op(time_M=nt-1, dt=dt)
op = Operator([stencil] + src_term + rec_term)
op(time_M=nt-1, dt=dt)
Results
Results
shot_record = rec.data # (nt, nrec)
snapshot = p.data[0] # Current wavefield
undefinedshot_record = rec.data # (nt, nrec)
snapshot = p.data[0] # Current wavefield
undefinedSymbolic Derivatives
符号导数
| Syntax | Description |
|---|---|
| First/second time derivative |
| Spatial derivatives |
| Laplacian (auto-adapts to dims) |
| p at t+dt (time stepping) |
| p at t-dt (adjoint) |
| 语法 | 描述 |
|---|---|
| 一阶/二阶时间导数 |
| 空间导数 |
| 拉普拉斯算子(自动适配维度) |
| t+dt时刻的p(时间步进) |
| t-dt时刻的p(伴随运算) |
Stability and Accuracy
稳定性与精度
CFL Condition:
dt < dx / (v_max * sqrt(ndim))| Dims | Max dt |
|---|---|
| 2D | dx / (v_max * 1.414) |
| 3D | dx / (v_max * 1.732) |
| Space Order | Stencil Points | Error |
|---|---|---|
| 2 | 3 | O(h^2) |
| 4 | 5 | O(h^4) |
| 8 | 9 | O(h^8) |
Higher order = more accurate but slower. Use 4-8 for production.
CFL条件:
dt < dx / (v_max * sqrt(ndim))| 维度 | 最大dt |
|---|---|
| 2D | dx / (v_max * 1.414) |
| 3D | dx / (v_max * 1.732) |
| 空间阶数 | 模板点数 | 误差 |
|---|---|---|
| 2 | 3 | O(h^2) |
| 4 | 5 | O(h^4) |
| 8 | 9 | O(h^8) |
阶数越高,精度越高但速度越慢。生产环境建议使用4-8阶。
When to Use vs Alternatives
适用场景与替代方案
| Scenario | Recommendation |
|---|---|
| Seismic wave propagation (acoustic/elastic) | Devito - symbolic PDE, auto-optimized code |
| Full Waveform Inversion (FWI) or RTM | Devito - adjoint support, GPU-ready |
| Legacy seismic processing pipelines | Madagascar - established, large script library |
| Simple 1D/2D wave demos | Custom NumPy - no dependencies, easier to debug |
| General-purpose PDE solving (non-wave) | FEniCS - FEM-based, broader PDE support |
| Production seismic imaging at scale | Devito - generates optimized C code, MPI support |
Choose Devito when: You need high-performance finite-difference wave propagation
with symbolic equation specification. It auto-generates optimized C/OpenMP/GPU code
from Python-level math, making it ideal for FWI, RTM, and research prototyping.
Avoid Devito when: You need finite-element methods (use FEniCS), or simple
pedagogical examples where NumPy suffices.
| 场景 | 推荐方案 |
|---|---|
| 地震波传播(声波/弹性波) | Devito - 符号型PDE,自动优化代码 |
| 全波形反演(FWI)或逆时偏移(RTM) | Devito - 支持伴随运算,适配GPU |
| 传统地震处理流水线 | Madagascar - 成熟稳定,拥有大量脚本库 |
| 简单1D/2D波演示 | 自定义NumPy实现 - 无依赖,调试更简单 |
| 通用PDE求解(非波类) | FEniCS - 基于有限元法,支持更广泛的PDE |
| 大规模生产级地震成像 | Devito - 生成优化的C代码,支持MPI |
选择Devito的场景:你需要高性能的有限差分波传播模拟,且希望通过符号化方式定义方程。它能从Python层面的数学表达式自动生成优化的C/OpenMP/GPU代码,非常适合FWI、RTM以及研究原型开发。
避免使用Devito的场景:你需要有限元法(请使用FEniCS),或者简单的教学示例(NumPy即可满足需求)。
Common Workflows
常见工作流
Acoustic wave forward modelling with sources and receivers
带源与接收器的声波正演模拟
- Define with shape and physical extent matching the velocity model
Grid - Create velocity and populate with model values
Function - Create for the wavefield (time_order=2, space_order=4+)
TimeFunction - Verify CFL condition:
dt < dx / (v_max * sqrt(ndim)) - Build wave equation stencil:
Eq(p.forward, solve(p.dt2 - v**2 * p.laplace, p.forward)) - Create source () and receivers, set coordinates
RickerSource - Add source injection and receiver interpolation terms
- Compile with stencil + source + receiver terms
Operator - Run operator:
op(time_M=nt-1, dt=dt) - Extract shot record from and plot
rec.data
- 定义与速度模型匹配的(包含形状和物理范围)
Grid - 创建速度并填充模型数值
Function - 为波场创建(time_order=2,space_order≥4)
TimeFunction - 验证CFL条件:
dt < dx / (v_max * sqrt(ndim)) - 构建波方程模板:
Eq(p.forward, solve(p.dt2 - v**2 * p.laplace, p.forward)) - 创建源()和接收器,设置坐标
RickerSource - 添加源注入和接收器插值项
- 用模板+源+接收器项编译
Operator - 运行算子:
op(time_M=nt-1, dt=dt) - 从提取炮集记录并绘图
rec.data
References
参考资料
- Operators and Stencils - Detailed operator construction
- Performance Optimization - GPU execution and tuning
- 算子与模板 - 算子构建详细说明
- 性能优化 - GPU执行与调优
Scripts
脚本
- scripts/acoustic_wave.py - Basic acoustic wave modeling
- scripts/acoustic_wave.py - 基础声波模拟脚本