137 lines
3.4 KiB
Markdown
137 lines
3.4 KiB
Markdown
# rtx-hemodynamics-server
|
|
|
|
Server layer for the RustyTorch++ hemodynamics demo.
|
|
|
|
## Overview
|
|
|
|
This crate provides the API layer between the Tauri frontend and the hemodynamics PINN backend. It manages simulation state, handles inference requests, and provides a clean interface for IPC commands.
|
|
|
|
## Architecture
|
|
|
|
```
|
|
Tauri IPC
|
|
↓
|
|
HemodynamicsService ← ServiceConfig
|
|
↓
|
|
SimulationHandle ← manages state
|
|
↓
|
|
rtx-hemodynamics ← PINN backend
|
|
```
|
|
|
|
## Modules
|
|
|
|
| Module | Description |
|
|
|--------|-------------|
|
|
| `service` | Main API: `HemodynamicsService` |
|
|
| `state` | Simulation state: `SimulationHandle`, `SimulationStatus` |
|
|
| `error` | Error types: `ServerError`, `ServerResult` |
|
|
|
|
## Usage
|
|
|
|
```rust
|
|
use rtx_hemodynamics_server::{HemodynamicsService, ServiceConfig};
|
|
use rtx_hemodynamics_shared::params::VesselParams;
|
|
|
|
// Create service
|
|
let service = HemodynamicsService::new(ServiceConfig::default()).unwrap();
|
|
|
|
// Initialize simulation
|
|
service.initialize(VesselParams::default()).await.unwrap();
|
|
|
|
// Query fields at points
|
|
let fields = service.query_fields(&points, 0.0).await.unwrap();
|
|
|
|
// Query on a grid
|
|
let grid = service.query_grid(100, 40, 0.0).await.unwrap();
|
|
|
|
// Modify geometry
|
|
service.add_stenosis(stenosis_params).await.unwrap();
|
|
|
|
// Get status
|
|
let status = service.get_status().await.unwrap();
|
|
|
|
// Get metrics
|
|
let metrics = service.get_metrics().await.unwrap();
|
|
```
|
|
|
|
## API Methods
|
|
|
|
### Initialization
|
|
|
|
| Method | Description |
|
|
|--------|-------------|
|
|
| `initialize(params)` | Initialize simulation with vessel parameters |
|
|
| `reset()` | Reset simulation to initial state |
|
|
|
|
### Inference
|
|
|
|
| Method | Description |
|
|
|--------|-------------|
|
|
| `query_fields(points, time)` | Query (u, v, p) at specific points |
|
|
| `query_grid(nx, ny, time)` | Query fields on a regular grid |
|
|
|
|
### Geometry
|
|
|
|
| Method | Description |
|
|
|--------|-------------|
|
|
| `add_stenosis(params)` | Add stenosis to vessel |
|
|
| `add_aneurysm(params)` | Add aneurysm to vessel |
|
|
| `place_stent(params)` | Place virtual stent |
|
|
| `reset_geometry()` | Reset to original geometry |
|
|
|
|
### Status
|
|
|
|
| Method | Description |
|
|
|--------|-------------|
|
|
| `get_status()` | Get simulation status |
|
|
| `get_metrics()` | Get performance metrics |
|
|
|
|
## State Management
|
|
|
|
The service maintains:
|
|
- **SimulationHandle**: Wraps the PINN model and manages GPU resources
|
|
- **SimulationStatus**: Tracks initialization, training progress, inference count
|
|
- **Metrics**: FPS, inference latency, GPU utilization
|
|
|
|
## Error Handling
|
|
|
|
```rust
|
|
pub enum ServerError {
|
|
NotInitialized,
|
|
InvalidParameters(String),
|
|
InferenceError(String),
|
|
GeometryError(String),
|
|
InternalError(String),
|
|
}
|
|
```
|
|
|
|
All public methods return `ServerResult<T>`.
|
|
|
|
## Thread Safety
|
|
|
|
`HemodynamicsService` is `Send + Sync` and can be shared across async tasks using `Arc`. Internal state is protected by appropriate synchronization primitives.
|
|
|
|
## Configuration
|
|
|
|
```rust
|
|
pub struct ServiceConfig {
|
|
pub max_query_points: usize, // Default: 50000
|
|
pub default_grid_nx: usize, // Default: 100
|
|
pub default_grid_ny: usize, // Default: 40
|
|
pub enable_cuda_graphs: bool, // Default: true
|
|
pub cache_inference: bool, // Default: true
|
|
}
|
|
```
|
|
|
|
## Tests
|
|
|
|
```bash
|
|
cargo test
|
|
```
|
|
|
|
Integration tests verify:
|
|
- Service lifecycle (init → query → reset)
|
|
- Geometry modification flow
|
|
- Error handling for invalid states
|
|
- Concurrent access safety
|