Metadata-Version: 2.5
Name: mrsimtracks
Version: 0.3.0
Summary: Generate CFD-derived particle trajectories for MR flow simulation.
Project-URL: Homepage, https://github.com/mcgrathcm/MRSimTracks
Project-URL: Repository, https://github.com/mcgrathcm/MRSimTracks
Project-URL: Issues, https://github.com/mcgrathcm/MRSimTracks/issues
Project-URL: Changelog, https://github.com/mcgrathcm/MRSimTracks/blob/main/CHANGELOG.md
Author-email: Charles McGrath <cmmcg@stanford.edu>
License-Expression: MIT
License-File: LICENSE
Keywords: cfd,flow-simulation,mr-simulation,mri,particle-tracking,pyvista,streamlines,vtk
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Classifier: Topic :: Scientific/Engineering :: Physics
Requires-Python: >=3.12
Requires-Dist: h5py>=3.16.0
Requires-Dist: joblib>=1.5.3
Requires-Dist: matplotlib>=3.11.0
Requires-Dist: numba>=0.65.1
Requires-Dist: numpy>=2.4.6
Requires-Dist: pyvista>=0.48.4
Requires-Dist: scipy>=1.17.1
Requires-Dist: tqdm>=4.68.2
Requires-Dist: vtk>=9.6.2
Description-Content-Type: text/markdown

# MRSimTracks

Generate CFD-derived particle trajectories and ground-truth velocity images for
MR flow simulation.

MRSimTracks performs Lagrangian particle tracking in time-resolved (pulsatile)
CFD meshes and samples their velocity fields onto Cartesian reference images in
the mesh coordinate system. It seeds particles in a tetrahedral or native-cell
flow domain, advects them through a steady or time-periodic velocity field
(RK4), and recycles out-of-bounds particles back to the inflow boundaries with
optional **backflow-aware** reseeding.

<p align="center">
  <img src="docs/assets/ubend_density_slice.webp" alt="Greyscale center-slice particle animation through the U-bend" width="30%">
  <img src="docs/assets/ubend_tracks.png" alt="Selected speed-colored particle trajectories through the U-bend" width="30%">
  <img src="docs/assets/ubend_particles.webp" alt="Speed-colored particles tracked through the full pulsatile U-bend example" width="30%">
</p>

## Install

MRSimTracks is published on PyPI:

```bash
uv add "mrsimtracks==0.3.0"
```

or with pip:

```bash
python -m pip install "mrsimtracks==0.3.0"
```

To install the latest source from GitHub instead:

```bash
uv add "mrsimtracks @ git+https://github.com/mcgrathcm/MRSimTracks.git"
```

For development from a clone:

```bash
uv sync          # installs the package (editable) and dependencies
```

The distribution name and Python import package are both `mrsimtracks`.

## Quick start

```python
import numpy as np

import mrsimtracks as mt
from mrsimtracks.seeding import seed_mesh

# 1. Load a time-resolved flow field (.vtu, .pvd, directory, or VTU list)
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. (optional) Near-wall no-penetration projection
wall_slip = mt.WallSlip(flow, caps=["Inlet.vtp", "Outlet.vtp"])

# 4. 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,
    wall_slip=wall_slip,
)

# 5. Use / save
result.positions      # (n_states, n_particles, 3), starting with seeds at t=0
result.reset          # (n_states, n_particles), false at t=0
result.times          # (n_states,), starting at 0
result.save("tracks.h5")
```

For N integration steps, default output contains N+1 states. This changes the
0.2.x output convention, where the first stored frame followed one integration
step. `result.n_steps` counts stored states; use `result.times` for their times.

### Native static cells and steady flow

Preserve hexes, wedges, or mixed cells on static geometry with
`conform_mesh=False`:

```python
flow = mt.load_flow("hex_case.vtu", active_key="Velocity", conform_mesh=False)
reseeder = mt.BoundaryReseeder(["Inlet.vtp", "Outlet.vtp"], flow, dt=0.001)
seeds = seed_mesh(flow.active_mesh, 10_000, rng=np.random.default_rng(0))
result = mt.track(flow, seeds=seeds, dt=0.001, tmax=0.02, reseeder=reseeder)
```

One VTK locator is reused for all sampling and reseeding queries, preserving
native cell interpolation. The geometry must be static for this cached native
path; velocity can vary with time. A single velocity frame (`Velocity` or
`Velocity_00000`) is steady and requires an explicit finite `tmax` in `track`
or `track_parallel`. `WallSlip` requires all-tetrahedral geometry; omit it for
native non-tet cells. Parallel tracking accepts `conform_mesh=False`, with a
separate locator in each worker.

### Material motion and streaming

For particles attached to a deforming fixed-topology mesh, load nodal motion
instead of integrating a velocity field:

```python
motion = mt.load_mesh_motion(
    "ventricle_mesh.pvd",
    displacement_key="displacement_base_fixed",
    dt=0.001,
)
particles = motion.seed(100_000, rng=np.random.default_rng(0))
cloud = motion.point_cloud(particles, time=0.29)
result = motion.trajectory(particles, output_path="material_motion.h5")
result.is_file_backed  # True until result.positions is requested
result.shape           # (n_frames, n_particles, 3)
result.times           # exact stored/evaluation times
```

The mesh is tetrahedralized once without adding or removing nodes. Particles
are seeded uniformly by reference volume and retain fixed tetrahedron-local
barycentric weights throughout the deformation.

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

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

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

`time_subsample=10` stores the initial state and every tenth integration state.
Each stored reset flag is true if that particle reset at least once since the
previous stored state, and the file's `dt` is the resulting stored-state interval.

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`:

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

## Periodic nearest-neighbor mapping

Generate the 1-based destination-to-source map expected by KomaMRI's periodic
`FlowPath`. The first stored tracking frame contains the original seeds at
`t=0` in the result's particle order:

```python
cycle_map = mt.periodic_mapping(result.positions[0], result.positions[-1])
```

The mapping is not one-to-one: multiple initial locations can select the same
final particle, and some final particles may not be selected.

## Key functions

| Function | Purpose |
|---|---|
| `load_flow(path, active_key=..., mesh_mode="auto")` | Load one `.vtu` with time-labeled fields, a `.pvd`, a directory, or a VTU list. Static geometry is stored once; moving coordinates and changed topologies are retained only when needed. Declare `mesh_mode="static"`, `"moving"`, or `"changing_topology"` to skip automatic classification. `subsamp=N` keeps every Nth frame; `center_mesh=True` applies one frame-0-based translation to all mesh frames before sampling; `reverse_velocity=True` negates all velocity components before use. |
| `load_ale_flow(path, velocity_scale=...)` | Load physical velocity, mesh velocity, and displacement on a verified static reference mesh for tracking on its deformed states. `center_mesh=True` applies one frame-0-based translation to all deformed mesh states before sampling. `reverse_velocity=True` negates physical velocity while leaving mesh velocity and displacement unchanged. |
| `load_mesh_motion(path, displacement_key=None)` | Load fixed-topology nodal motion from moved-node VTUs or a three-component displacement field, then seed and evaluate attached material particles without velocity integration. `center_mesh=True` applies one frame-0-based translation to all mesh frames before seeding. `trajectory(..., output_path=..., pbar=True)` streams positions and exact times to HDF5 with frame progress reporting. |
| `periodic_mapping(initial_positions, final_positions)` | Build a fast nearest-neighbor destination-to-source map with 1-based indices for KomaMRI periodic `FlowPath` motion. |
| `sample_velocity_image(flow, ...)` | Sample a dense native-coordinate `(time,x,y,z,component)` velocity image with exact temporal-window and subvoxel averaging and sparse HDF5 save support. Set `reorder_by_extent=True` for largest-to-smallest axes. |
| `track(flow, seeds=..., dt=..., reseeder=...)` | Single-process tracking → `TrackingResult`. Use `output_path=...` for streamed HDF5 output, optionally with `time_subsample=N`, and `return_metrics=True` for timing metrics. |
| `track_parallel(path, ..., caps=..., n_workers=...)` | Multi-process tracking → `TrackingResult`. Accepts `reverse_velocity=True` for the same load-time velocity reversal in every worker. |
| `BoundaryReseeder(caps, flow, dt=...)` | Flux-weighted, time-resolved inflow reseeder. `caps` = cap surface path(s) or a surface with a `region_id` cell array. |
| `ALEBoundaryReseeder(caps, flow, dt=...)` | ALE reseeder weighted by current deformed face area and inward `Velocity - Mesh_velocity`. |
| `WallSlip(flow, caps=..., band_frac=0.02)` | Optional near-wall no-penetration projection for `track(..., wall_slip=...)`. Excludes cap faces so inlet/outlet flux remains open. |

## 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).

## Wall-slip notes

`WallSlip` removes only the into-wall velocity component for particles within a
thin wall band. Use it when interpolated near-wall velocities deposit particles
against no-slip walls. Pass the same cap surfaces used for reseeding so open
boundaries are excluded from the wall set:

```python
wall_slip = mt.WallSlip(flow, caps=["Inlet.vtp", "Outlet.vtp"], band_frac=0.02)
result = mt.track(
    flow,
    seeds=seeds,
    dt=0.002,
    reseeder=reseeder,
    wall_slip=wall_slip,
)
```

For `track_parallel`, set `wall_slip=True`; each worker builds its own
projection from the worker-local flow and `caps`.
