Module

Hylograph.ForceEngine.WASMEngine

Package
purescript-hylograph-simulation
Repository
afcondon/purescript-hylograph-simulation

WASM Engine Adapter

High-performance force simulation using Rust/WebAssembly. Drop-in replacement for the D3.js-based simulation.

Provides 3-4x speedup over D3.js for large graphs (10,000+ nodes) by offloading force calculations to compiled WebAssembly.

Usage:

import Hylograph.ForceEngine.WASMEngine as WASMEngine

main = do
  -- Initialize WASM module (once at app startup)
  WASMEngine.initWasm "./pkg/force_kernel.js"

  -- Create WASM simulation with nodes
  wasmSim <- WASMEngine.create nodes links config

  -- Create adapter for Scene Engine
  let adapter = WASMEngine.mkAdapter wasmSim

  -- Use with Scene Engine
  engine <- Engine.createEngine adapter

#WASMSim Source

type WASMSim :: Row Type -> Typetype WASMSim r = { linksRef :: Ref (Array { source :: Int, target :: Int }), nodesRef :: Ref (Array (SimulationNode r)), wasmSim :: WASMSimulation }

WASM Simulation handle. Encapsulates the WASM simulation and PureScript node data.

#WASMSimConfig Source

type WASMSimConfig = { center :: CenterConfig, enableCenter :: Boolean, enableLinks :: Boolean, enableManyBody :: Boolean, links :: LinkConfig, manyBody :: ManyBodyConfig }

Configuration for creating a WASM simulation

#SimulationNode Source

type SimulationNode :: Row Type -> Typetype SimulationNode r = { fx :: Number, fy :: Number, id :: Int, vx :: Number, vy :: Number, x :: Number, y :: Number | r }

Simulation node type for WASM engine. Contains both position data (synced with WASM) and user data.

#defaultConfig Source

defaultConfig :: WASMSimConfig

Default simulation configuration

#initWasm Source

initWasm :: String -> Aff Unit

Initialize the WASM module. Call once at application startup.

The URL should point to the generated JavaScript module from wasm-pack. Example: initWasm "./pkg/force_kernel.js"

#isWasmReady Source

isWasmReady :: Effect Boolean

Check if WASM is initialized and ready

#create Source

create :: forall r. Array (SimulationNode r) -> Array { source :: Int, target :: Int } -> WASMSimConfig -> Effect (WASMSim r)

Create a new WASM simulation with nodes and links.

#free Source

free :: forall r. WASMSim r -> Effect Unit

Free WASM simulation memory. Call when done with simulation.

#setNodes Source

setNodes :: forall r. Array (SimulationNode r) -> WASMSim r -> Effect Unit

Set/replace all nodes in the simulation.

#mkAdapter Source

mkAdapter :: forall r. WASMSim r -> EngineAdapter (SimulationNode r)

Create an EngineAdapter for use with the Scene Engine.

This is the key integration point - allows WASMEngine to be used as a drop-in replacement for D3-based simulation in scene transitions.

#tick Source

tick :: forall r. WASMSim r -> Effect Number

Run a single simulation tick

#tickN Source

tickN :: forall r. Int -> WASMSim r -> Effect Number

Run multiple simulation ticks

#getNodes Source

getNodes :: forall r. WASMSim r -> Effect (Array (SimulationNode r))

Get current nodes with positions

#reheat Source

reheat :: forall r. WASMSim r -> Effect Unit

Reheat simulation (set alpha to 1.0)

#isRunning Source

isRunning :: forall r. WASMSim r -> Effect Boolean

Check if simulation is still running

#configureManyBody Source

configureManyBody :: forall r. WASMSim r -> ManyBodyConfig -> Effect Unit

Configure many-body (charge) force

#configureCenter Source

configureCenter :: forall r. WASMSim r -> CenterConfig -> Effect Unit

Configure center force

#enableForces Source

enableForces :: forall r. WASMSim r -> { center :: Boolean, links :: Boolean, manyBody :: Boolean } -> Effect Unit

Enable/disable forces individually