Code
Grape – A Swift library for force simulation and graph visualization

Grape – a Swift library for force simulation and graph visualization.
Features
2D simd | ND simd | Metal | |
---|---|---|---|
NdTree | ✅ | ✅ | |
Simulation | ✅ | 🚧 | 🚧 |
LinkForce | ✅ | ||
ManyBodyForce | ✅ | ||
CenterForce | ✅ | ||
CollideForce | ✅ | ||
PositionForce | ✅ | ||
RadialForce | ✅ | ||
SwiftUI View | 🚧 |
Usage
import ForceSimulation struct Node: Identifiable { ... } let nodes: [Node] = ... let links: [(Node.ID, Node.ID)] = ... let sim = Simulation(nodes: nodes, alphaDecay: 0.0005) sim.createManyBodyForce(strength: -30) sim.createLinkForce(links: links, originalLength: .constant(35)) sim.createCenterForce(center: .zero, strength: 0.1) sim.createCollideForce(radius: .constant(5))
See Example for more details.
Performance
Grape uses simd to calculate position and velocity. Currently it takes ~0.13 seconds to iterate 120 times over the example graph. (77 vertices, 254 edges, with manybody, center, collide and link forces. Release build on a M1 Max)
Credits
This library has been greatly influenced by the outstanding work done by D3.js (Data-Driven Documents).
