/** * Initialize the link force algorithm and start simulation. */ function startLinkSimulation() { console.log("startLinkSimulation") springForce = false; alreadyRanIterations = 0; simulation.stop(); p1 = performance.now(); let links = []; // Initialize link array. nodes = simulation.nodes(); for (i = nodes.length-1; i >= 1; i--) { for (j = i-1; j >= 0; j--) { links.push({ source: nodes[i], target: nodes[j], }); } } /* Add force * Please add the distance function before feeding the force * it to the simulation or adding links. * * On setting the distance fn and being initialized by the simulation, the * force will pre-calculate high-dimensional distances of every link and * store that as cache. * Adding distance fn before links means that the first pre-calculation will * calculate noting as there was no link. * The full pre-calculation will then occur once when the force is being * initialized by the simulation. */ simulation.force(forceName, d3.forceLinkOptimized() .distance(function (n) { return distanceFunction(n.source, n.target, props, norm); }) .links(links) ).alphaDecay(0) //.velocityDecay(0.8) .alpha(1) .restart(); }