PhiFlow Documentation
repository·master·Indexed 24 days ago
https://github.com/tum-pbs/phiflowAn open-source simulation toolkit for optimization and machine learning applications. PhiFlow enables the creation of end-to-end differentiable functions by integrating physics simulations with backends such as PyTorch, Jax, and TensorFlow. It features built-in PDE operations for fluid phenomena, a flexible web interface for live visualizations, and a backend-agnostic design supporting both 2D and 3D dimensionalities.
What's inside PhiFlow
- PhiFlow is an open-source simulation toolkit designed for optimization and machine learning applications. It is primarily written in Python and supports multiple backends, including NumPy, TensorFlow, Jax, and PyTorch. Because it integrates closely with these machine learning frameworks, it can leverage their automatic differentiation capabilities, enabling the creation of end-to-end differentiable functions that combine physics simulations with learning models.
Overview of PhiFlow features
masterPhiFlow is an open-source simulation toolkit designed for optimization and machine learning. Key features include:
- ML Integration: Tight integration with PyTorch, Jax, and TensorFlow for differentiable simulations that can run on the GPU.
- PDE Operations: Built-in operations focused on fluid phenomena for concise simulation formulation.
- Web Interface: A flexible web UI with live visualizations and interactive controls.
- Backend Agnostic: Reusable simulation code that works across different backends (NumPy, PyTorch, Jax, TensorFlow) and dimensionalities (2D or 3D) without modification.
- Linear Equation Solver: High-level solver with automated sparse matrix generation.
- Design: Object-oriented and vectorized design for expressiveness and extensibility.
Understand the relationship between PhiFlow and PhiML
masterPhiFlow is built on top of the tensor functionality provided by PhiML. To effectively use PhiFlow, it is recommended to first understand the concepts of named and typed dimensions used in PhiML, as these form the foundation of PhiFlow's tensor operations.Navigate the Web Interface tabs and features
masterThe web interface is organized into several tabs accessible from the upper left corner:
- Home: Displays the app title and description. Allows selecting a single field to view, starting/pausing the app, or stepping a single frame. App-specific controls are located at the bottom.
- Side-by-Side: Similar to Home, but allows viewing two fields simultaneously.
- Info: Shows session details like file paths and runtime. You can find the app's
stridevalue here. - Log: Displays the complete application log.
- Φ Board: Provides benchmarking functionality. For TensorFlow applications, it allows launching TensorBoard and running the TensorFlow profiler.
- Help: Documentation for the interface.
Tip: To run a specific number of frames, enter the number in the text box next to the 'Step' button. Prefixing the number with a
*(e.g.,*5) multiplies that number by the app'sstridevalue.Compare PhiFlow and MantaFlow fluid solvers
masterWhen comparing PhiFlow to MantaFlow, note these key architectural and data differences:
- Differentiability: PhiFlow supports differentiable operators, whereas MantaFlow focuses on fast CPU-based simulations.
- Grid Sizing: In PhiFlow, staggered velocity grids are larger by one layer on the positive domain sides compared to the scalar grids. In MantaFlow, all grids have the same size.
- Vector Dimensionality: MantaFlow uses a fixed 3-component
Vec3struct for all solvers. PhiFlow uses vectors that match the solver's dimensionality (e.g., 2-component arrays for 2D solvers). - Gravity Direction: In PhiFlow, gravity acts along the last or first dimension (e.g., Z in 3D, Y in 2D). In MantaFlow, gravity always acts along the Y direction.
Configure Frame Rate and Refresh Rate
masterThe web interface distinguishes between the execution speed of your code and the visual update speed in the browser:
- Execution Framerate: Defined by the
view()method; it controls how quickly your user code is executed. - Refresh Rate: Controlled via a setting above the field viewer in the web interface; it defines how often the diagrams in the browser are updated.
- Execution Framerate: Defined by the
Work with Batches of Scenes
masterTo handle data-parallel simulations efficiently, ΦFlow supports batch modes. This allows you to treat multiple scenes as a single batched object, which is ideal for stacking tensors of the same resolution.
- Create a batch: Use
Scene.createwith thecountargument set to the batch size and specify abatch_dim(e.g.,'batch'). - Writing: When calling
scenes.write(), the fields are automatically unstacked along thebatch_dimand distributed to the individual scenes. - Reading: When calling
scenes.read(), the loaded fields are automatically stacked along thebatch_diminto a single batched tensor.
- Create a batch: Use
Control loop execution using Viewer.range()
masterYou can use the GUI to pause, run single iterations, or break a loop by iterating over
Viewer.range().To prevent the loop from running immediately upon launch, use
play=Falsein theview()call. This stops execution as soon as the loop is encountered, allowing you to use GUI controls to manage the flow.data = Domain(x=32, y=32).scalar_grid(Noise()) # play=False stops execution immediately when the loop is hit for _ in view(data, play=False).range(10): data = physics(data)data = Domain(x=32, y=32).scalar_grid(Noise()) for _ in view(data, play=False).range(10): data = physics(data)Install PhiFlow from source
masterInstalling from source is required if you want to use PhiFlow CUDA operations with TensorFlow. The source version includes demo scripts and tests.
- Clone the repository:
git clone https://github.com/tum-pbs/PhiFlow.git <target directory> - Add the directory to your Python path (e.g., by running
pip install <target directory>/or configuring your IDE).
Note: If you use
pip install <target directory>/, you must rerun this command after making changes to the source code.$ git clone https://github.com/tum-pbs/PhiFlow.git <target directory> $ pip install <target directory>/- Clone the repository:
Generate PhiFlow API documentation manually
masterThe API documentation is generated using
pdoc. To generate it manually, ensure thatphiis in your Python path and that PyTorch, TensorFlow, and Jax are installed. Run the following command:pdoc --html --output-dir docs --force phiWrite a custom physical simulation in PhiFlow
masterIn PhiFlow, custom simulations should not rely on abstract
StateorPhysicsclasses. Instead, define your simulation logic as standard Python functions and call them within a loop. Thephi.physicsmodule provides high-level operations that act onFieldobjects.Commonly used functions like
advect.semi_lagrangianandfluid.make_incompressibleare available via the standardfrom phi.flow import *import.from phi.flow import * DOMAIN = Domain(x=64, y=80, boundaries=CLOSED, bounds=Box(x=100, y=100)) velocity = DOMAIN.staggered_grid(Noise()) pressure = DOMAIN.scalar_grid(0) for _ in range(100): velocity = advect.semi_lagrangian(velocity, velocity, dt=1) velocity, pressure, iterations, _ = fluid.make_incompressible(velocity, DOMAIN, pressure_guess=pressure)Verify PhiFlow installation
masterUse the following methods to ensure PhiFlow and its dependencies are correctly installed. A successful verification will display the PhiFlow version and component information.