N-body gravitational simulation using spatial hash grid. Partitions particles into cells based on bounding box; each particle interacts only with neighbors in its 3x3 cell neighborhood. Reduces pairwise evaluations for spatially clustered distributions.
| 日期 | 适应度 | 安全分 | 调用数 |
|---|---|---|---|
| 3月17日 | 1.0000 | 1.00 | 1 |
Spatial hash grid N-body simulation — a pure-computation Native Gene for the Rotifer Protocol.
Partitions space into a uniform grid of fixed-size cells. For each particle, only computes gravitational forces from particles in the same cell and 8 neighboring cells (3×3 neighborhood). Long-range forces beyond the cell neighborhood are ignored.
Cell size is adaptive: cellSize = max(2ε, 2 × √(area / n)), scaling with the average inter-particle spacing and bounded by the softening parameter.
Integration uses the symplectic leapfrog (kick-drift-kick) scheme.
This approach trades long-range gravitational accuracy for raw speed — achieving near-O(n) performance when particles are roughly uniformly distributed. It's better suited for short-range-dominated systems (collisions, dense clusters) than for orbital mechanics where long-range gravity dominates.
# Cluster preset (good fit for spatial hashing)
rotifer run particle-spatial --input '{"preset": "cluster", "count": 256}'
# Collision (where short-range forces dominate)
rotifer run particle-spatial --input '{"preset": "collision", "count": 512, "steps": 300}'
# Solar system (less accurate due to missing long-range forces)
rotifer run particle-spatial --input '{"preset": "solar"}'
| Preset | Description |
|---|---|
solar |
Central massive body + orbiting lighter bodies with circular velocities |
binary |
Two equal-mass stars in mutual orbit + debris ring |
cluster |
Random particles in a disk with small random velocities |
collision |
Two groups of particles approaching head-on |
particles — final positions and velocities of all particlessteps_computed — number of simulation steps executedtotal_energy — total system energy (kinetic + potential) for conservation validationinteractions_computed — total neighbor-cell force evaluations| Metric | Expected |
|---|---|
| Success Rate | 1.0 (deterministic, no failure mode) |
| Accuracy | Low-medium (only short-range interactions, misses distant forces) |
| Complexity | ~O(n × steps) for uniform distributions |
| Interactions | Depends on particle clustering — much fewer than n² |
| Best For | Large N with short-range dominated interactions (collisions, dense clusters) |
| Weakness | Poor for orbital/long-range gravity (solar, binary) |
{
"type": "object",
"required": [],
"properties": {
"G": {
"type": "number",
"default": 1,
"description": "Gravitational constant"
},
"dt": {
"type": "number",
"default": 0.01,
"description": "Time step size"
},
"seed": {
"type": "number",
"description": "Random seed for preset generation (deterministic output)"
},
"count": {
"type": "number",
"default": 64,
"maximum": 2048,
"minimum": 2,
"description": "Number of particles for preset generation"
},
"steps": {
"type": "number",
"default": 100,
"maximum": 10000,
"minimum": 1,
"description": "Number of simulation steps"
},
"preset": {
"enum": [
"solar",
"binary",
"cluster",
"collision"
],
"type": "string",
"description": "Preset particle configuration"
},
"particles": {
"type": "array",
"items": {
"type": "object",
"required": [
"x",
"y",
"vx",
"vy",
"mass"
],
"properties": {
"x": {
"type": "number"
},
"y": {
"type": "number"
},
"vx": {
"type": "number"
},
"vy": {
"type": "number"
},
"mass": {
"type": "number"
}
}
},
"description": "Initial particle states. Ignored if preset is provided."
},
"softening": {
"type": "number",
"default": 0.01,
"description": "Softening parameter to prevent singularity at close range"
}
}
} {
"type": "object",
"required": [
"particles",
"steps_computed",
"total_energy",
"interactions_computed"
],
"properties": {
"particles": {
"type": "array",
"items": {
"type": "object",
"properties": {
"x": {
"type": "number"
},
"y": {
"type": "number"
},
"vx": {
"type": "number"
},
"vy": {
"type": "number"
},
"mass": {
"type": "number"
}
}
},
"description": "Final particle states after simulation"
},
"total_energy": {
"type": "number",
"description": "Total system energy (kinetic + potential) at final state"
},
"steps_computed": {
"type": "number",
"description": "Actual number of simulation steps computed"
},
"interactions_computed": {
"type": "number",
"description": "Total pairwise force evaluations across all steps"
}
}
}