$ show tum-pbs/PhiFlow
PhiFlow is a Python library for differentiable physics simulation, primarily targeting fluid dynamics, particle systems, and PDE-based optimization. It provides a unified field abstraction (CenteredGrid, StaggeredGrid, PointCloud, Mesh) with automatic resampling and boundary handling, enabling seamless integration with machine learning workflows. The library supports multiple backends (NumPy, PyTorch, TensorFlow, JAX) and features a rich geometry module, physics solvers (advection, diffusion, incompressible flow, SPH, waves), and visualization tools. While its core abstractions are clean and well-tested, the codebase exhibits some technical drift due to deprecated API surface, large monolithic Dash-based web UI, and heavy reliance on the external phiml tensor library. Nonetheless, it is actively maintained with extensive test coverage and a growing research community.
The LLM identified 4 files worth highlighting for reuse:
Core Field abstraction representing physical quantities with geometry, values, and boundary conditions. Implements frozen dataclass with variable_attrs for autodiff, Pythonic slicing, operator overloads (+, -, *, /) that automatically resample operands to common geometry, and methods for boundary conversion. The design is clean and could be extracted as a standalone data structure for any field-based simulation or interpolation problem.
Mesh geometry class with support for loading .su2, .msh, .stl files, face/vertex connectivity computation, and element-based face construction (triangles, quadrilaterals, tetrahedra, hexahedra). Provides caching of topological properties and decimation via pyfqmr. The mesh building logic is self-contained and reusable for any mesh-based simulation or rendering pipeline.
Base classes for a pluggable visualization system: PlottingLibrary singleton and Recipe classes with can_plot/plot methods. Each backend (matplotlib, plotly, console) registers recipes for different field types. This pattern allows adding new plotting backends or plot types without modifying existing code, and could be extracted for any project needing a flexible visualization layer.
Implements incompressible fluid solvers: make_incompressible for divergence-free velocity projection (pressure solve) and incompressible_rk4 for time integration. Uses a conjugate gradient solver with custom linear operators. The code demonstrates a clean differentiable PDE solve pattern applicable to other projection-based fluid solvers.