Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dt is sometimes zero #171

Closed
samreid opened this issue Oct 14, 2014 · 29 comments
Closed

dt is sometimes zero #171

samreid opened this issue Oct 14, 2014 · 29 comments
Assignees

Comments

@samreid
Copy link
Member

samreid commented Oct 14, 2014

@jbphet noticed in Balancing Act that joist sometimes sends out dt=0 in the step function. It generally appears to be correlated with when the cursor changes to a hand/arrow. I have reproduced the problem on my machine (though it appears less frequently than on @jbphet machine). Perhaps related to phetsims/scenery#275

@jonathanolson
Copy link
Contributor

Does this happen on all browsers?

@samreid
Copy link
Member Author

samreid commented Oct 14, 2014

I noticed this issue in Win8/Chrome Version 38.0.2125.101 m (64-bit)

@samreid
Copy link
Member Author

samreid commented Oct 14, 2014

I updated and also saw this problem once in Chrome Version 38.0.2125.104 m (64-bit)

@pixelzoom
Copy link
Contributor

This sounds like it's either (a) vestigial or (b) potentially serious. Assigning to @jonathanolson.

@jonathanolson
Copy link
Contributor

Will try to reproduce.

@jonathanolson
Copy link
Contributor

I've spent 10 minutes banging on the sim and I'm unable to reproduce (Win7/Chrome).

Added debugger in Sim.js step if dt was zero, I'd recommend @jbphet add the debugger statement and see if you can still trigger it.

@jbphet
Copy link
Contributor

jbphet commented May 29, 2015

I was able to duplicate this, but not during normal sim usage. I was able to make it happen when I had the developer tools open and was changing the size of the tools window, sliding the divider either right or left. The attached screen shots show the output. I also tried this with a debugger statement, and the statement was indeed hit, demonstrating that this isn't just a rounding issue when printing the value.

baa-dt-zero-1

baa-dt-zero-2

The code that was added in the BalanceModel.step function looked like this (including the debug statement):

      if ( dt < 0.0083333 || dt > 0.025 ){
        console.log( 'dt off by more than 50% of nominal, value = ' + dt );
        if ( dt === 0 ){
          debugger;
        }
      }

@jbphet
Copy link
Contributor

jbphet commented May 29, 2015

Assigning back to @jonathanolson to decide if this is indeed "potentially serious" and should be addressed. The sim handles it just fine, and I assume most sims would, so it may be a non-issue. It's just a little odd that it happens at all.

By the way, the test environment where this was reproduced was Win7+Chrome43.

@jbphet jbphet assigned jonathanolson and unassigned jbphet May 29, 2015
@jonathanolson
Copy link
Contributor

Will check to make sure resizing doesn't cause sim steps.

@jonathanolson
Copy link
Contributor

Reproduced with Chrome/Mac, had two animation frames with the exact same timestamp (1432926900170) that were naturally generated. Looks like resizing causes more animation frames to be rendered (probably on each resize), so if the sim JS executes quickly enough, there can be many low-DT frames.

Because there is a resize, the ScreenView transform will change, and the view step may be expected to be called after that, so although I'd like to prevent 0-DT frames from even being stepped, I think we need to anyways. Thus in general, sims should be prepared to handle frame DTs that are zero.

@jonathanolson
Copy link
Contributor

Anything else to do here @pixelzoom, @samreid or @jbphet?

@pixelzoom
Copy link
Contributor

Nothing that I know of, but I haven't experienced this or really been paying attention. @samreid created this issue, so perhaps get him or @jbphet to close.

@jbphet
Copy link
Contributor

jbphet commented May 29, 2015

I'll go ahead and close. I'm fine with the implementation requirement that sims need to be able to handle zero values for dt.

@jbphet jbphet closed this as completed May 29, 2015
@samreid
Copy link
Member Author

samreid commented May 29, 2015

Sure, sims should handle small dt, but shouldn't we just add a line of logic in joist:

if (dt>0){
step();
}

? Then we can avoid any required corner-case handling in sim code?

@samreid samreid reopened this May 29, 2015
@pixelzoom
Copy link
Contributor

@jonathanolson said:

sims should be prepared to handle frame DTs that are zero.

Searching for "/ dt", I find 2 sims that will fail with divide-by-zero errors: balloons-and-static-electricity, faradays-law. (Don't know if those sims are testing for "dt > 0" somewhere.)

@pixelzoom
Copy link
Contributor

+1 for @samreid suggestion to address this in joist. Unless someone can explain the value of calling step when dt is zero?

@jonathanolson
Copy link
Contributor

As noted re: skipping step() if dt === 0:

Because there is a resize, the ScreenView transform will change, and the view step may be expected to be called after that, so although I'd like to prevent 0-DT frames from even being stepped, I think we need to anyways.

So we would skip model/view steps, do we also skip updateDisplay() and have the sim NOT update on window resize? Seems sketchy to me.

If we don't skip updateDisplay(), then because the ScreenView transform was changed, other view bits may have changed and would expect model/view updates (view.step() in particular). I assumed every time we redraw the sim, we'll have a view.step().

@pixelzoom
Copy link
Contributor

OK, makes sense. Then we need to take a look at balloons-and-static-electricity and faradays-law (and possibly other sims?)

@jonathanolson
Copy link
Contributor

Yes, probably for most sims. Molecule Shapes might also not handle dt=0 gracefully.

@jonathanolson
Copy link
Contributor

I'd like to update my proposal to consider skipping the model.step() if it's 0, but still doing the view step. Usually view steps don't need the DT as much, and generally models shouldn't change if there's a 0-time DT.

Thoughts?

@samreid
Copy link
Member Author

samreid commented Jun 10, 2015

@jonathanolson's suggestion #171 (comment) seems like a good solution.

@pixelzoom
Copy link
Contributor

Sounds reasonable to me. Regardless, my approach will be to program defensively :)

@jonathanolson
Copy link
Contributor

Committed the proposed change.

Sims that use DT in the view step (ignoring empty implementations):
balancing-chemical-equations (for RewardNode step)
bending-light (timer)
color-vision (beams get stepped)
energy-forms-and-changes (updates appearance)
fluid-pressure-and-flow (steps layers)
gravity-and-orbits (step() called without DT on paths!!?)

I'll check through all usages to ensure that we're fine before closing the issue.

There are also a ton of copy-pasted empty step functions, which aren't needed (if the function doesn't exist, it won't be called). Should we remove those?

@jonathanolson
Copy link
Contributor

All usages look safe, although there are a lot of step functions that are empty or that aren't part of a view and ignore their DT parameter. Will note in meeting agenda about possible cleanup.

@pixelzoom
Copy link
Contributor

Those empty step functions are probably being propagated from simula-rasa. I'll add a comment in simula-rasa that say something like "delete if not used".

pixelzoom added a commit to phetsims/simula-rasa that referenced this issue Jun 10, 2015
@pixelzoom
Copy link
Contributor

"Called by the animation loop. Optional" is the comment in simula-rasa, and it appears 39 times in sim code. So yes, this is where the empty step functions are coming from.

@samreid
Copy link
Member Author

samreid commented Jun 11, 2015

At today's meeting, we decided to remove the empty step functions in existing code, and that @samreid would do it.

@samreid samreid assigned samreid and unassigned jonathanolson Jun 11, 2015
samreid added a commit to phetsims/protein-synthesis that referenced this issue Jun 11, 2015
samreid added a commit to phetsims/isotopes-and-atomic-mass that referenced this issue Jun 11, 2015
samreid added a commit to phetsims/sugar-and-salt-solutions that referenced this issue Jun 11, 2015
samreid added a commit to phetsims/calculus-grapher that referenced this issue Jun 11, 2015
samreid added a commit to phetsims/energy-forms-and-changes that referenced this issue Jun 11, 2015
samreid added a commit to samreid/an-unconventional-weapon that referenced this issue Jun 11, 2015
samreid added a commit to phetsims/gene-expression-essentials that referenced this issue Jun 11, 2015
@samreid
Copy link
Member Author

samreid commented Jun 11, 2015

OK I removed the empty blocks above. @pixelzoom anything else to do here?

@pixelzoom
Copy link
Contributor

I think the covers it. Closing.

samreid added a commit to phetsims/capacitor-lab-basics that referenced this issue Jun 15, 2015
pixelzoom added a commit that referenced this issue Dec 19, 2022
jbphet pushed a commit that referenced this issue Dec 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants