PhiFlow Documentation

repository·master·Indexed 24 days ago

https://github.com/tum-pbs/phiflow

An 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.

Tokens
11.2K
Snippets
36
Records
74
Agent score
83%

What's inside PhiFlow

  1. Overview of PhiFlow

    master
    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.
  2. Overview of PhiFlow features

    master

    PhiFlow 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.
  3. Understand the relationship between PhiFlow and PhiML

    master
    PhiFlow 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.
  4. Navigate the Web Interface tabs and features

    master

    The 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 stride value 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's stride value.

  5. Compare PhiFlow and MantaFlow fluid solvers

    master

    When 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 Vec3 struct 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.
  6. Configure Frame Rate and Refresh Rate

    master

    The web interface distinguishes between the execution speed of your code and the visual update speed in the browser:

    1. Execution Framerate: Defined by the view() method; it controls how quickly your user code is executed.
    2. 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.
  7. Work with Batches of Scenes

    master

    To 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.

    1. Create a batch: Use Scene.create with the count argument set to the batch size and specify a batch_dim (e.g., 'batch').
    2. Writing: When calling scenes.write(), the fields are automatically unstacked along the batch_dim and distributed to the individual scenes.
    3. Reading: When calling scenes.read(), the loaded fields are automatically stacked along the batch_dim into a single batched tensor.
  8. Control loop execution using Viewer.range()

    master

    You 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=False in the view() 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)
  9. Install PhiFlow from source

    master

    Installing from source is required if you want to use PhiFlow CUDA operations with TensorFlow. The source version includes demo scripts and tests.

    1. Clone the repository: git clone https://github.com/tum-pbs/PhiFlow.git <target directory>
    2. 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>/
  10. Generate PhiFlow API documentation manually

    master

    The API documentation is generated using pdoc. To generate it manually, ensure that phi is in your Python path and that PyTorch, TensorFlow, and Jax are installed. Run the following command:

    pdoc --html --output-dir docs --force phi
  11. Write a custom physical simulation in PhiFlow

    master

    In PhiFlow, custom simulations should not rely on abstract State or Physics classes. Instead, define your simulation logic as standard Python functions and call them within a loop. The phi.physics module provides high-level operations that act on Field objects.

    Commonly used functions like advect.semi_lagrangian and fluid.make_incompressible are available via the standard from 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)