Force Functions (fa2util)¶
Internal force computation module. These functions exist in three backends:
- Cython (
.pyx): Compiled C extension for maximum performance - Pure Python (
.py): Fallback when Cython is not compiled - Vectorized (
fa2util_vectorized.py): NumPy broadcasting backend
Note
Some low-level force functions (linRepulsion, linGravity, strongGravity, etc.)
are internal (cdef in Cython) and not part of the public API. They are called
by the batch functions below.
Data Classes¶
Batch Operations¶
These are the main entry points used by the ForceAtlas2 class:
apply_repulsion
¶
Apply repulsion forces between all pairs of nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
list of Node
|
All graph nodes. |
required |
coefficient
|
float
|
Repulsion coefficient ( |
required |
adjustSizes
|
bool
|
Use anti-collision repulsion. |
False
|
Source code in fa2/fa2util.py
apply_gravity
¶
Apply gravity forces to all nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
list of Node
|
|
required |
gravity
|
float
|
Gravity strength. |
required |
scalingRatio
|
float
|
Used as coefficient for strong gravity. |
required |
useStrongGravity
|
bool
|
Use distance-independent strong gravity. |
False
|
Source code in fa2/fa2util.py
apply_attraction
¶
apply_attraction(nodes, edges, distributedAttraction, coefficient, edgeWeightInfluence, linLogMode=False, adjustSizes=False)
Apply attraction forces along all edges.
Selects the appropriate attraction function based on linLogMode
and adjustSizes. Optimizes for edgeWeightInfluence of 0 or 1
to avoid slow pow() calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
list of Node
|
|
required |
edges
|
list of Edge
|
|
required |
distributedAttraction
|
bool
|
Divide by source mass (dissuade hubs). |
required |
coefficient
|
float
|
Attraction coefficient. |
required |
edgeWeightInfluence
|
float
|
Exponent applied to edge weights. |
required |
linLogMode
|
bool
|
Use logarithmic attraction. |
False
|
adjustSizes
|
bool
|
Use anti-collision attraction. |
False
|
Source code in fa2/fa2util.py
Attraction Functions¶
linAttraction
¶
Apply linear attraction between connected nodes.
Force: F = -coefficient * edgeWeight * distance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n1
|
Node
|
Source and target nodes. |
required |
n2
|
Node
|
Source and target nodes. |
required |
e
|
float
|
Edge weight (after |
required |
distributedAttraction
|
bool
|
If True, divide by source node mass (dissuade hubs). |
required |
coefficient
|
float
|
Attraction coefficient. |
0
|
Source code in fa2/fa2util.py
logAttraction
¶
Apply logarithmic attraction for LinLog mode.
Force: F = -coefficient * edgeWeight * log(1 + distance).
Reference: Jacomy et al. 2014 Formula 3; Gephi ForceFactory.java.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n1
|
Node
|
Source and target nodes. |
required |
n2
|
Node
|
Source and target nodes. |
required |
e
|
float
|
Edge weight. |
required |
distributedAttraction
|
bool
|
If True, divide by source node mass. |
required |
coefficient
|
float
|
Attraction coefficient. |
0
|
Source code in fa2/fa2util.py
linAttraction_antiCollision
¶
Linear attraction with anti-collision: zero force when overlapping.
Reference: Gephi ForceFactory.java linAttraction_antiCollision.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n1
|
Node
|
Source and target nodes. |
required |
n2
|
Node
|
Source and target nodes. |
required |
e
|
float
|
Edge weight. |
required |
distributedAttraction
|
bool
|
If True, divide by source node mass. |
required |
coefficient
|
float
|
Attraction coefficient. |
0
|
Source code in fa2/fa2util.py
logAttraction_antiCollision
¶
Logarithmic attraction with anti-collision: zero force when overlapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n1
|
Node
|
Source and target nodes. |
required |
n2
|
Node
|
Source and target nodes. |
required |
e
|
float
|
Edge weight. |
required |
distributedAttraction
|
bool
|
If True, divide by source node mass. |
required |
coefficient
|
float
|
Attraction coefficient. |
0
|
Source code in fa2/fa2util.py
Speed Adjustment¶
adjustSpeedAndApplyForces
¶
Adjust simulation speed and apply accumulated forces to node positions.
Uses swing/traction measurement to adaptively control speed. High swing (oscillation) reduces speed; high traction (consistent movement) increases it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
list of Node
|
All graph nodes with accumulated forces. |
required |
speed
|
float
|
Current simulation speed. |
required |
speedEfficiency
|
float
|
Current speed efficiency factor. |
required |
jitterTolerance
|
float
|
How much swinging is tolerated. |
required |
adjustSizes
|
bool
|
Cap per-node speed based on node size. |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Source code in fa2/fa2util.py
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 | |
Barnes-Hut Spatial Tree¶
Region
¶
Barnes-Hut spatial tree node.
Generalizes the quadtree to 2^dim partitioning for N-dimensional
layouts. Each region stores aggregated mass and center of mass for
its child nodes, enabling O(n log n) repulsion approximation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
list of Node
|
Nodes contained in this region. |
required |