# Odd behavior with order of cells and force simulations

**URL:** https://talk.observablehq.com/t/odd-behavior-with-order-of-cells-and-force-simulations/2757
**Category:** Feedback
**Created:** [January 19, 2020, 6:08pm UTC](https://talk.observablehq.com/t/odd-behavior-with-order-of-cells-and-force-simulations/2757 "2020-01-19T18:08:38Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jimmcnulty41](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/jimmcnulty41/32/2600_2.png) [@jimmcnulty41](https://talk.observablehq.com/u/jimmcnulty41)
#### Post date: [January 19, 2020, 6:08pm UTC](https://talk.observablehq.com/t/odd-behavior-with-order-of-cells-and-force-simulations/2757/1 "2020-01-19T18:08:38Z")

</div>

I’ve been having a host of odd issues as I try to play around with d3-force in observable notebooks.

I’ve tracked down how to reproduce one especially odd guy I’ve been running into, but can’t tell exactly what the underlying issue is. and would love to understand what is going on.

I have a notebook that explores the issue here:

> **[d3-force-in-observable Troubleshooting](https://observablehq.com/@jimmcnulty41/d3-force-in-observable-troubleshooting)**
>
> I've had a lot of difficulty in getting an iterative d3-force playground going, and it's hard to tell what comes from my difficulties with the d3 APIs vs usage of Observable. This notebook will hopefully help me keep track of all the various ways...

But I’ll give the tl;dr here too:

1. I set up a simulation with a faulty set of nodes (due to a syntax error)
2. I fix the error, but the simulation with the faulty data appears to continue running, throwing an error in the console with each time step  
Visually, this results in all of my nodes being stacked on top of each other
3. If I refresh after fixing the error, the simulation works as expected, but I’m unclear why the refresh fixes this, and would love to not have to refresh to fix it.

---

<div class="post-metadata">

### Author: ![bgchen](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/bgchen/32/784_2.png) [@bgchen](https://talk.observablehq.com/u/bgchen)
#### Post date: [January 19, 2020, 8:29pm UTC](https://talk.observablehq.com/t/odd-behavior-with-order-of-cells-and-force-simulations/2757/2 "2020-01-19T20:29:30Z")

</div>

Welcome @jimmcnulty41!

Your question is a fun one. Here’s an issue that I couldn’t come up with a good solution for (hoping some d3 experts will drop by with better advice): if `nodes` is bad then `d3.forceSimulation(nodes)` starts an infinite loop which spams errors, and there doesn’t seem to be any way to stop it except by refreshing the page.

See my comments in `chart` here:

> **[d3-force-in-observable Troubleshooting / Bryan Gin-ge Chen / Observable](https://observablehq.com/d/b6a765552e1372f1)**
>
> The magic notebook for visualization.

`chart2` is working fine; in my fork I added some `invalidation` logic and then commented it out since as far as I can tell it doesn’t have anything to do with what’s happening here.

---

<div class="post-metadata">

### Author: ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)
#### Post date: [January 19, 2020, 9:23pm UTC](https://talk.observablehq.com/t/odd-behavior-with-order-of-cells-and-force-simulations/2757/3 "2020-01-19T21:23:41Z")

</div>

In addition to the invalidation code that should be added in any case, one can catch the error by initializing the sim without nodes and wrapping potentially dangerous logic in a try/catch:

```auto
  const sim = d3.forceSimulation();
  try {
    sim
      .nodes(nodes)
      .force("charge", d3.forceManyBody())
      .force("center", d3.forceCenter(width / 2, height / 2));
      sim
        .on("tick", () => {
          nodeSelection.attr("cx", d => d.x).attr("cy", d => d.y);
      });
  }
  catch(e) {
    sim.stop();
    throw e;
  }

```

---

<div class="post-metadata">

### Author: ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)
#### Post date: [January 19, 2020, 9:52pm UTC](https://talk.observablehq.com/t/odd-behavior-with-order-of-cells-and-force-simulations/2757/4 "2020-01-19T21:52:15Z")

</div>

I think the timer is launched

> <https://github.com/d3/d3-force/blob/master/src/simulation.js#L23>

  
before the simulation is initialized  

> <https://github.com/d3/d3-force/blob/master/src/simulation.js#L82>

it might be worth opening an issue at [https://github.com/d3/d3-force/issues](https://github.com/d3/d3-force/issues)

---

<div class="post-metadata">

### Author: ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)
#### Post date: [January 20, 2020, 9:50am UTC](https://talk.observablehq.com/t/odd-behavior-with-order-of-cells-and-force-simulations/2757/5 "2020-01-20T09:50:21Z")

</div>

It might be similar to this issue [https://github.com/d3/d3-force/issues/45](https://github.com/d3/d3-force/issues/45)
