Published
Edited
Apr 16, 2022
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
makeStateVec = (nodes) => {
return nodes.map(n => n.x)
.concat(nodes.map(n => n.y))
.concat(nodes.map(n => n.vx))
.concat(nodes.map(n => n.vy));
}
Insert cell
gravityField = (nodes, G) => {
// nodes are the nodes exhibiting gravitational forces, generally celestial bodies

// the s input below is supposed to be the values affecting each other, but since we're just doing fields,
// s will always be the initial state.
G = G || 1;
const n = nodes.length;
// Borrowed from https://observablehq.com/@mcmcclur/gravitation-and-the-n-body-problem
let x_accel = (x1, y1, s, j) => {
let m2 = nodes[j].m;

// In the n-body simulation, all the nodes are moving, but in this modified version
// nothing is moving, we're just grabbing the point in time acceleration.
// let x1 = s[i];
// let y1 = s[i + n];
let x2 = s[j];
let y2 = s[j + n];
return (m2 * (x2 - x1)) / ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** (3 / 2);
};

let y_accel = (x1, y1, s, j) => {
let m2 = nodes[j].m;
// let x1 = s[i];
// let y1 = s[i + n];
let x2 = s[j];
let y2 = s[j + n];
return (m2 * (y2 - y1)) / ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** (3 / 2);
};

let gr = (x, y, s) => {
return {
xpp: G*d3.sum(d3.range(nodes.length).map((i) => x_accel(x, y, s, i))),
ypp: G*d3.sum(d3.range(nodes.length).map((i) => y_accel(x, y, s, i))),
};
};

return gr
}
Insert cell
Insert cell
Insert cell
import {createStandardGrid, drawVector, addArrowHead, vectorAdd, vectorScale} from "@pcarleton/range-kutta-supporting-functions@309"
Insert cell
d3 = require('d3@7.3.0')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more