Skip to main content

Generate bounded factional Brownian motion in up to 3 dimensions using Pyvista models for shapes.

Project description

Multi-Dimensional Bounded FBM Simulator - with Time-Varying Diffusivity and Hurst Parameters

Overview

This package provides tools for simulating bounded Fractional Brownian Motion (FBM) within 3D shapes created with PyVista. The simulator supports dynamic diffusion coefficients and Hurst exponents modeled as Markov chains, allowing for complex particle behavior simulation.

Key features:

  • Creation of various 3D cell shapes (spherical, rod, rectangular, ovoid) using PyVista
  • FBM simulation with time-varying diffusion coefficients
  • Time-varying Hurst exponents for different motion regimes
  • Markov chain modeling for parameter transitions
  • Boundary handling for accurate confinement within cells

Installation

pip install boundedfbm

Quick Start

import numpy as np
from boundedfbm import create_cell, CellType, FBM_BP

# 1. Create a cell to define boundaries
sphere_params = {
    "center": [0, 0, 0],
    "radius": 10.0
}
cell = create_cell(CellType.SPHERICAL, sphere_params)

# 2. Set up FBM parameters
n_steps = 1000  # Number of time steps
dt = 0.01       # Time step in seconds

# Single diffusion coefficient and Hurst exponent (simple case)
diffusion_params = np.array([1.0]) # length units of the pyvista models, time units of dt
hurst_params = np.array([0.7])  # Superdiffusive

# For state transitions, set up identity matrices (no transitions)
# probabilities are represented for a dt time event. 
diff_transition = np.array([[1.0]])
hurst_transition = np.array([[1.0]])

# Initial state probabilities (single state)
diff_prob = np.array([1.0])
hurst_prob = np.array([1.0])

# Initial position, represents the initial position of motion in the pyvista model
initial_position = np.array([0.0, 0.0, 0.0])

# 3. Create the FBM simulator
fbm_simulator = FBM_BP(
    n=n_steps,
    dt=dt,
    diffusion_parameters=diffusion_params,
    hurst_parameters=hurst_params,
    diffusion_parameter_transition_matrix=diff_transition,
    hurst_parameter_transition_matrix=hurst_transition,
    state_probability_diffusion=diff_prob,
    state_probability_hurst=hurst_prob,
    cell=cell,
    initial_position=initial_position
)

# 4. Run the simulation
trajectory = fbm_simulator.fbm(dims=3)

# 5. Visualize or analyze the results
# trajectory shape: (n_steps, 3) for 3D positions

Creating Cells

The package supports several cell types that define boundaries for the FBM:

Spherical Cell

params = {
    "center": [0, 0, 0],   # 3D center coordinates
    "radius": 10.0         # Radius of sphere
}
cell = create_cell(CellType.SPHERICAL, params)

Rod Cell

params = {
    "center": [0, 0, 0],       # 3D center coordinates
    "direction": [0, 0, 1],    # Direction vector (will be normalized)
    "height": 20.0,            # Length of the rod
    "radius": 5.0              # Radius of the rod
}
cell = create_cell(CellType.ROD, params)

Rectangular Cell

params = {
    "bounds": [-10, 10, -10, 10, -10, 10]  # [xmin, xmax, ymin, ymax, zmin, zmax]
}
cell = create_cell(CellType.RECTANGULAR, params)

Ovoid Cell

params = {
    "center": [0, 0, 0],       # 3D center coordinates
    "direction": [0, 0, 1],    # Direction vector (will be normalized)
    "xradius": 10.0,           # Radius in x-direction
    "yradius": 15.0,           # Radius in y-direction
    "zradius": 20.0            # Radius in z-direction
}
cell = create_cell(CellType.OVOID, params)

Advanced FBM Configuration

Multi-State Diffusion and Hurst Parameters

You can model particles that switch between different motion regimes:

# Two diffusion states: slow and fast
diffusion_params = np.array([0.5, 2.0])  

# Three Hurst states: subdiffusive, Brownian, and superdiffusive
hurst_params = np.array([0.3, 0.5, 0.7])  

# Transition matrices (probability of switching between states)
diff_transition = np.array([
    [0.95, 0.05],  # From state 0: 95% stay, 5% switch to state 1
    [0.10, 0.90]   # From state 1: 10% switch to state 0, 90% stay
])

hurst_transition = np.array([
    [0.90, 0.05, 0.05],  # From state 0
    [0.10, 0.80, 0.10],  # From state 1
    [0.05, 0.15, 0.80]   # From state 2
])

# Initial state probabilities
diff_prob = np.array([0.7, 0.3])  # 70% chance to start in state 0
hurst_prob = np.array([0.2, 0.5, 0.3])  # Initial distribution across 3 states

Full Example with Multiple States and Visualization

import numpy as np
import pyvista as pv
from boundedfbm import create_cell, CellType, FBM_BP

# Create a cell
cell_params = {
    "center": [0, 0, 0],
    "radius": 10.0
}
cell = create_cell(CellType.SPHERICAL, cell_params)

# Multi-state configuration
n_steps = 5000
dt = 0.01

# Two diffusion states
diffusion_params = np.array([0.5, 2.0])
diff_transition = np.array([
    [0.95, 0.05],
    [0.10, 0.90]
])
diff_prob = np.array([0.5, 0.5])

# Three Hurst exponent states
hurst_params = np.array([0.3, 0.5, 0.7])
hurst_transition = np.array([
    [0.90, 0.05, 0.05],
    [0.10, 0.80, 0.10],
    [0.05, 0.15, 0.80]
])
hurst_prob = np.array([0.33, 0.34, 0.33])

# Initial position
initial_position = np.array([0.0, 0.0, 0.0])

# Create simulator
fbm_simulator = FBM_BP(
    n=n_steps,
    dt=dt,
    diffusion_parameters=diffusion_params,
    hurst_parameters=hurst_params,
    diffusion_parameter_transition_matrix=diff_transition,
    hurst_parameter_transition_matrix=hurst_transition,
    state_probability_diffusion=diff_prob,
    state_probability_hurst=hurst_prob,
    cell=cell,
    initial_position=initial_position
)

# Run simulation
trajectory = fbm_simulator.fbm(dims=3)

# Visualize with PyVista
plotter = pv.Plotter()

# Add the cell geometry
cell_geometry = cell.get_shape()
plotter.add_mesh(cell_geometry, opacity=0.3, color='lightblue')

# Add the trajectory
points = np.array(trajectory) + initial_position  # Adjust by initial position
polyline = pv.PolyData(points)
polyline.lines = pv.lines_from_points(points)
plotter.add_mesh(polyline, line_width=2, color='red')

# Show the plot
plotter.show()

Understanding FBM Parameters

Diffusion Coefficient

The diffusion coefficient controls the magnitude of particle displacement. Higher values result in larger steps.

Hurst Exponent

The Hurst exponent (H) controls the type of diffusion:

  • H < 0.5: Subdiffusive motion (anti-persistent, tends to reverse direction)
  • H = 0.5: Brownian motion (uncorrelated, classic random walk)
  • H > 0.5: Superdiffusive motion (persistent, tends to continue in the same direction)

API Reference

Cell Creation

from boundedfbm import create_cell, CellType

# Available cell types
CellType.SPHERICAL
CellType.ROD
CellType.RECTANGULAR
CellType.OVOID

# Create a cell
cell = create_cell(CellType.SPHERICAL, params)

Parameter Validation

from boundedfbm import validate_cell_parameters

# Check if parameters are valid
is_valid, errors = validate_cell_parameters(CellType.SPHERICAL, params)

FBM Simulation

from boundedfbm import FBM_BP

# Create simulator
simulator = FBM_BP(
    n=n_steps,                                   # Number of time steps
    dt=dt,                                       # Time step duration
    diffusion_parameters=diff_params,            # Array of diffusion coefficients
    hurst_parameters=hurst_params,               # Array of Hurst exponents
    diffusion_parameter_transition_matrix=dtm,   # Transition matrix for diffusion
    hurst_parameter_transition_matrix=htm,       # Transition matrix for Hurst
    state_probability_diffusion=diff_prob,       # Initial diffusion state probabilities
    state_probability_hurst=hurst_prob,          # Initial Hurst state probabilities
    cell=cell,                                   # Boundary cell
    initial_position=init_pos                    # Starting position
)

# Run the simulation
trajectory = simulator.fbm(dims=3)  # 3D simulation

Advanced Topics

Boundary Conditions

The simulator implements reflecting boundary conditions when a particle would cross the cell boundary.

Optimizing Performance

For large simulations, consider:

  • Pre-computing cell geometries when running multiple simulations

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

boundedfbm-0.2.0.tar.gz (46.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

boundedfbm-0.2.0-py3-none-any.whl (20.7 kB view details)

Uploaded Python 3

File details

Details for the file boundedfbm-0.2.0.tar.gz.

File metadata

  • Download URL: boundedfbm-0.2.0.tar.gz
  • Upload date:
  • Size: 46.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for boundedfbm-0.2.0.tar.gz
Algorithm Hash digest
SHA256 11d5252d2263fa1d2895b3c3a50f60ffb40c95c6e3728287fda4bc792c4e640e
MD5 ec8791208f7ee6a0dbd8659f8674b3fa
BLAKE2b-256 4e7170b054865babc599a8478005717734be0846b80fc1294f74467475d55d5a

See more details on using hashes here.

Provenance

The following attestation bundles were made for boundedfbm-0.2.0.tar.gz:

Publisher: pypi_publish.yml on joemans3/boundedfbm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file boundedfbm-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: boundedfbm-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 20.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for boundedfbm-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 172f3d29bf724651dc9bf4aa8e38436594cbbf089582a7b5ef52c1c677362873
MD5 6cde7d4fb06081f5be2d51b76055b632
BLAKE2b-256 87f176849ff27af5a14f3c878515688744eb184b81d27f8414107db70366c174

See more details on using hashes here.

Provenance

The following attestation bundles were made for boundedfbm-0.2.0-py3-none-any.whl:

Publisher: pypi_publish.yml on joemans3/boundedfbm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page