Skip to main content

Generate CFD-derived particle trajectories for MR flow simulation.

Project description

MRSimTracks

Generate CFD-derived particle trajectories for MR flow simulation.

MRSimTracks performs Lagrangian particle tracking in time-resolved (pulsatile) CFD meshes. It operates on mesh velocity fields, not MR image data. It seeds particles in a tetrahedral flow domain, advects them through a time-periodic velocity field (RK4), and recycles out-of-bounds particles back to the inflow boundaries with optional backflow-aware reseeding.

Install

From GitHub:

pip install "mrsimtracks @ git+https://github.com/mcgrathcm/MRSimTracks.git"

For development from a clone:

uv sync          # installs the package (editable) and dependencies

The distribution name and Python import package are both mrsimtracks.

Quick start

import numpy as np

import mrsimtracks as mt
from mrsimtracks.seeding import seed_mesh

# 1. Load a time-resolved flow field (.vtu single-file series or .pvd collection)
flow = mt.load_flow("case.pvd", active_key="Velocity")

# 2. (optional) Backflow-aware inflow reseeder from labeled cap surfaces
reseeder = mt.BoundaryReseeder(["Inlet.vtp", "Outlet.vtp"], flow, dt=0.002)

# 3. Seed and track
seeds = seed_mesh(flow.active_mesh, 200_000, rng=np.random.default_rng(0))
result = mt.track(flow, seeds=seeds, dt=0.002, reseeder=reseeder)

# 4. Use / save
result.positions      # (n_steps, n_particles, 3)
result.reset          # (n_steps, n_particles) reseed flags
result.times          # (n_steps,)
result.save("tracks.h5")

For large single-process runs, write each timestep directly to HDF5 instead of keeping all positions in RAM:

result, metrics = mt.track(
    flow,
    seeds=seeds,
    dt=0.002,
    reseeder=reseeder,
    output_path="tracks.h5",
    return_metrics=True,
)

result.is_file_backed  # True until result.positions or result.reset is loaded
metrics["particle_steps_per_s"]

Run example.py for a complete version using the reduced fixture committed in tests/data/. The full-cycle example flow file is tracked with Git LFS; see example/README.md.

Large runs (multiple processes)

Each worker reloads the field, so memory scales with n_workers:

result = mt.track_parallel(
    "case.pvd", seeds=seeds, dt=0.002,
    caps=["Inlet.vtp", "Outlet.vtp"], active_key="Velocity",
    n_workers=3, subsamp=1,
)

Key functions

Function Purpose
load_flow(path, active_key=...) Load .vtu (one geometry, many time fields) or .pvd (series); auto-selects the memory-efficient reader. subsamp=N keeps every Nth frame.
track(flow, seeds=..., dt=..., reseeder=...) Single-process tracking → TrackingResult. Use output_path=... for streamed HDF5 output and return_metrics=True for timing metrics.
track_parallel(path, ..., caps=..., n_workers=...) Multi-process tracking → TrackingResult.
BoundaryReseeder(caps, flow, dt=...) Flux-weighted, time-resolved inflow reseeder. caps = cap surface path(s) or a surface with a region_id cell array.

Reseeding notes

  • The reseeder weights every cap face by max(-v·n, 0)·area at the nearest flow frame, so particles re-enter only through currently-inflow faces — handling backflow and partial inflow/outflow on a single cap.
  • BoundaryReseeder(..., dt=dt) spreads new particles over a thin inflow volume (depth ~v_n·dt) so successive reseeds overlap, keeping spatial density smooth (important for MR-style uniform-density use). Omit dt for plane seeding.
  • flux_waveform() returns per-cap net flux over the cycle — a conservation / validation diagnostic (Σ caps ≈ 0 for a well-resolved incompressible field).

Performance

Sampling reuses a cell-tree locator built once, a temporal-coherence tet walk (numba) seeded from each particle's previous cell, and fused walk+interpolation. benchmark.py measures sampling and tracking throughput.

Development

Normal CI runs against a reduced real-data fixture:

uv sync --group dev
uv run pytest -m "not large" --cov=mrsimtracks --cov-report=term-missing

Full-data validation uses the Git LFS example file and runs only for release validation:

git lfs pull --include="example/CFD_velocity.vtu"
uv run pytest -m large

See CONTRIBUTING.md for the full development workflow.

Data

The included CFD fixtures are documented separately in DATA_LICENSE.md.

Documentation

Documentation is built with MkDocs:

uv sync --group docs
uv run --group docs mkdocs build --strict

The GitHub Pages site is deployed from main.

Notes

  • Flow meshes are assumed all-tetrahedral and static in time (the field varies, the geometry does not).
  • .pvd loading stores one geometry plus one field per frame for static-mesh series, so long time series fit in a few GB instead of tens.

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

mrsimtracks-0.1.0rc1.tar.gz (119.5 kB view details)

Uploaded Source

Built Distribution

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

mrsimtracks-0.1.0rc1-py3-none-any.whl (27.0 kB view details)

Uploaded Python 3

File details

Details for the file mrsimtracks-0.1.0rc1.tar.gz.

File metadata

  • Download URL: mrsimtracks-0.1.0rc1.tar.gz
  • Upload date:
  • Size: 119.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mrsimtracks-0.1.0rc1.tar.gz
Algorithm Hash digest
SHA256 a6e1159535fd96dafff2a8c6716b1a429e2d49f2d5e4f5518d7cfbc643f10c11
MD5 10bfd0941187d99d1c0029e7f18e3f14
BLAKE2b-256 55cdd8572368af3c6be9d7a74bfe59d666a9c5fa6c9e958be224438240116f80

See more details on using hashes here.

Provenance

The following attestation bundles were made for mrsimtracks-0.1.0rc1.tar.gz:

Publisher: publish.yml on mcgrathcm/MRSimTracks

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

File details

Details for the file mrsimtracks-0.1.0rc1-py3-none-any.whl.

File metadata

  • Download URL: mrsimtracks-0.1.0rc1-py3-none-any.whl
  • Upload date:
  • Size: 27.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mrsimtracks-0.1.0rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 5e99c997665ac3d342b500a3fa2eae433159db7acbcf59e0b6f6529a42e64184
MD5 13c7682db01193beba7d809e2ad94598
BLAKE2b-256 4e779c62c9d3a4d5b806b7f32e1b799bf5bb480ae1ebc599877699e31a2cb61a

See more details on using hashes here.

Provenance

The following attestation bundles were made for mrsimtracks-0.1.0rc1-py3-none-any.whl:

Publisher: publish.yml on mcgrathcm/MRSimTracks

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