CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-py-pde

Python package for solving partial differential equations with a focus on ease of use and performance

Pending
Overview
Eval results
Files

visualization.mddocs/

Visualization

Visualization functions create movies and interactive plots from field data with automatic scaling, customizable styling, and support for various output formats.

Capabilities

Movie Generation

Create animated movies from stored simulation data.

def movie(storage, filename=None, *, progress=True, **kwargs):
    """
    Create movie from stored field data.
    
    Parameters:
    - storage: StorageBase, stored simulation data
    - filename: str, output filename (auto-generated if None)
    - progress: bool, show progress bar during generation
    - kwargs: additional movie parameters (fps, dpi, writer, etc.)
    
    Returns:
    str: Path to generated movie file
    """

def movie_multiple(storages, filename=None, *, progress=True, **kwargs):
    """
    Create movie from multiple storage sources.
    
    Parameters:
    - storages: list of StorageBase, multiple data sources
    - filename: str, output filename
    - progress: bool, show progress bar
    - kwargs: movie parameters and layout options
    
    Returns:
    str: Path to generated movie file
    """

def movie_scalar(storage, filename=None, *, progress=True, **kwargs):
    """
    Create movie optimized for scalar field data.
    
    Parameters:
    - storage: StorageBase, scalar field data
    - filename: str, output filename
    - progress: bool, show progress bar
    - kwargs: scalar-specific visualization parameters
    
    Returns:
    str: Path to generated movie file
    """

Interactive Plotting

Create interactive plots for real-time field visualization and analysis.

def plot_interactive(field, title=None, *, fig_num=1, **kwargs):
    """
    Create interactive plot of field data.
    
    Parameters:
    - field: FieldBase, field to visualize
    - title: str, plot title
    - fig_num: int, matplotlib figure number
    - kwargs: additional plotting parameters
    
    Returns:
    matplotlib figure: Interactive plot figure
    """

Kymograph Visualization

Create kymograph plots showing field evolution over time and space.

def plot_kymograph(storage, *, field_index=0, transpose=False, **kwargs):
    """
    Plot kymograph (space-time diagram) from stored data.
    
    Parameters:
    - storage: StorageBase, time series field data
    - field_index: int, field index for FieldCollection data
    - transpose: bool, transpose space and time axes
    - kwargs: plotting parameters (colormap, scaling, etc.)
    
    Returns:
    matplotlib.axes.Axes: Kymograph plot axes
    """

def plot_kymographs(storage, *, filename=None, **kwargs):
    """
    Plot multiple kymographs for vector/tensor fields.
    
    Parameters:
    - storage: StorageBase, time series data
    - filename: str, save filename (shows if None)
    - kwargs: plotting and layout parameters
    
    Returns:
    matplotlib.figure.Figure: Figure with multiple kymographs
    """

Field Magnitude Visualization

Visualize magnitude and components of vector/tensor fields.

def plot_magnitudes(storage, *, title=None, filename=None, **kwargs):
    """
    Plot magnitude evolution from stored data.
    
    Parameters:
    - storage: StorageBase, time series data
    - title: str, plot title
    - filename: str, save filename (shows if None)  
    - kwargs: plotting parameters
    
    Returns:
    PlotReference: Reference to the created plot
    """

Interactive Visualization

Create interactive visualizations using external tools.

def plot_interactive(storage, viewer_args=None, **kwargs):
    """
    Create interactive plot using napari or similar viewer.
    
    Parameters:
    - storage: StorageBase, field data for visualization
    - viewer_args: dict, arguments for the viewer
    - kwargs: additional visualization parameters
    """

Movie and Visualization Classes

Advanced movie creation with custom rendering and effects.

class Movie:
    def __init__(self, filename, *, writer="auto", fps=10, dpi=100):
        """
        Initialize movie writer.
        
        Parameters:
        - filename: str, output movie filename
        - writer: str, movie writer backend
        - fps: int, frames per second
        - dpi: int, resolution
        """

    def add_frame(self, frame_data, **kwargs):
        """
        Add frame to movie.
        
        Parameters:
        - frame_data: FieldBase, field data for frame
        - kwargs: frame-specific parameters
        """

    def save(self):
        """Finalize and save movie file"""

    def __enter__(self):
        """Context manager entry"""
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        """Context manager exit"""
        self.save()

Usage Examples

Basic Movie Creation

import pde

# Run simulation with data collection
grid = pde.CartesianGrid([[0, 10], [0, 10]], [64, 64])
eq = pde.AllenCahnPDE()
state = eq.get_initial_condition(grid)

# Collect data for movie
storage = pde.MemoryStorage()
tracker = pde.DataTracker(storage=storage, interrupts=0.1)
result = eq.solve(state, t_range=5.0, tracker=tracker)

# Create movie
movie_file = pde.movie(storage, "phase_separation.mp4", fps=15)
print(f"Movie saved as: {movie_file}")

Custom Movie with Styling

import pde
import matplotlib.pyplot as plt

# Simulation setup
grid = pde.UnitGrid([128], periodic=True)
eq = pde.KuramotoSivashinskyPDE()
state = eq.get_initial_condition(grid)

storage = pde.MemoryStorage()
tracker = pde.DataTracker(storage=storage, interrupts=0.05)
result = eq.solve(state, t_range=20.0, tracker=tracker)

# Create styled movie
movie_params = {
    "fps": 20,
    "dpi": 150,
    "colormap": "plasma",
    "title": "Kuramoto-Sivashinsky Dynamics",
    "xlabel": "Position",
    "ylabel": "Height"
}

pde.movie_scalar(storage, "ks_evolution.mp4", **movie_params)

Interactive Visualization

import pde
import matplotlib.pyplot as plt

# 2D field for interactive exploration
grid = pde.CartesianGrid([[-5, 5], [-5, 5]], [64, 64])
field = pde.ScalarField.from_expression(grid, "exp(-(x**2 + y**2)/2)")

# Create interactive plot
fig = pde.plot_interactive(
    field, 
    title="Interactive Field Visualization",
    colorbar=True,
    aspect="equal"
)

plt.show()

Kymograph Analysis

import pde

# 1D simulation for kymograph
grid = pde.UnitGrid([128], periodic=True)
eq = pde.DiffusionPDE(diffusivity=0.01)

# Create traveling wave initial condition
def traveling_wave(x):
    return np.sin(2*np.pi*x) + 0.5*np.sin(4*np.pi*x)

state = pde.ScalarField.from_expression(grid, traveling_wave)

# Long time simulation
storage = pde.MemoryStorage()
tracker = pde.DataTracker(storage=storage, interrupts=0.1)
result = eq.solve(state, t_range=50.0, tracker=tracker)

# Create kymograph
ax = pde.plot_kymograph(
    storage,
    transpose=False,
    colormap="RdBu_r",
    title="Diffusion Evolution"
)
plt.xlabel("Position")
plt.ylabel("Time")
plt.show()

Vector Field Visualization

import pde

# Create vector field
grid = pde.CartesianGrid([[-2, 2], [-2, 2]], [32, 32])
def vortex(x, y):
    return [-y, x]

vector_field = pde.VectorField.from_expression(grid, vortex)

# Visualize components and magnitude
fig = pde.plot_magnitudes(
    vector_field,
    layout="grid",
    colormap="viridis",
    title="Vortex Field Analysis"
)
plt.show()

Custom Movie with Multiple Fields

import pde

# Simulate coupled system
grid = pde.CartesianGrid([[0, 20], [0, 20]], [64, 64]) 
eq = pde.CahnHilliardPDE()
state = eq.get_initial_condition(grid)

# Create auxiliary fields for analysis
storage = pde.MemoryStorage()
aux_storage = pde.MemoryStorage()

def analysis_callback(field, t):
    # Store gradient magnitude
    grad = field.gradient("auto_periodic_neumann")
    grad_mag = grad.magnitude()
    aux_storage.append(grad_mag, {"t": t})

trackers = [
    pde.DataTracker(storage=storage, interrupts=0.2),
    pde.CallbackTracker(analysis_callback, interrupts=0.2)
]

result = eq.solve(state, t_range=10.0, tracker=trackers)

# Create multi-panel movie
storages = [storage, aux_storage]
labels = ["Concentration", "Gradient Magnitude"]

pde.movie_multiple(
    storages,
    "cahn_hilliard_analysis.mp4",
    layout="horizontal",
    titles=labels,
    fps=10
)

High-Quality Movie Production

import pde

# High-resolution simulation
grid = pde.CartesianGrid([[0, 10], [0, 10]], [256, 256])
eq = pde.SwiftHohenbergPDE(a=0.2, b=1.0)
state = eq.get_initial_condition(grid)

# Dense data collection for smooth animation
storage = pde.MemoryStorage()
tracker = pde.DataTracker(storage=storage, interrupts=0.02) 

result = eq.solve(state, t_range=30.0, tracker=tracker)

# High-quality movie parameters
movie_config = {
    "fps": 30,
    "dpi": 300,
    "writer": "ffmpeg",
    "bitrate": 5000,
    "codec": "h264",
    "colormap": "magma",
    "interpolation": "bilinear"
}

pde.movie(storage, "high_quality_patterns.mp4", **movie_config)
print("High-quality movie generated")

Real-time Visualization

import pde
import matplotlib.pyplot as plt

# Set up real-time plotting
grid = pde.UnitGrid([64, 64], periodic=True)
eq = pde.AllenCahnPDE()
state = eq.get_initial_condition(grid)

# Custom real-time plotting tracker
class RealTimePlotter(pde.TrackerBase):
    def __init__(self, interrupts=1):
        super().__init__(interrupts=interrupts)
        self.fig, self.ax = plt.subplots(figsize=(8, 6))
        self.im = None
        
    def handle(self, field, t):
        if self.im is None:
            self.im = self.ax.imshow(field.data, animated=True)
            self.ax.set_title("Real-time Evolution")
            plt.colorbar(self.im, ax=self.ax)
        else:
            self.im.set_array(field.data)
            self.im.set_clim(field.data.min(), field.data.max())
        
        self.ax.set_title(f"t = {t:.2f}")
        plt.pause(0.01)

# Run with real-time visualization
plotter = RealTimePlotter(interrupts=0.1)
result = eq.solve(state, t_range=10.0, tracker=plotter)
plt.show()

Install with Tessl CLI

npx tessl i tessl/pypi-py-pde

docs

boundaries.md

fields.md

grids.md

index.md

pdes.md

solvers.md

storage.md

trackers.md

visualization.md

tile.json