phetsims / faradays-electromagnetic-lab

"Faraday's Electromagnetic Lab" is an educational simulation in HTML5, by PhET Interactive Simulations.
GNU General Public License v3.0
1 stars 0 forks source link

Light bulb sometimes does not light when magnet is flipped while inside the pickup coil #9

Closed pixelzoom closed 9 months ago

pixelzoom commented 1 year ago

For #8... Reported by Mike Dubson in https://phet.unfuddle.com/a#/projects/9404/tickets/by_number/3552 on 6/14/2013:

In the pickcoil tab, with the magnet inside the pick-up coil, when the magnet is flipped, sometimes the light bulb does not light at all. I believe the problem is due to the fact that there is no damping in the current model, so that the bulb bightness is proportional to the instantaneous change in flux, which in this case occurs over a single time step. And sometime this single time step is missed by the code.

I believe the best long-term solution is to alter the model (slightly) to include damping, as is done in the flash sim Faraday's law, so that single-time-step changes are smoothed out over several time-steps.

Rather than make significant changes to the model to incorporate damping (as described above), it might be preferrable to add a workaround for this specific case. We'll see...

pixelzoom commented 10 months ago

Reproduced in the HTML5 version:

  1. Go to the Pickup Coil screen.
  2. Make sure the light bulb is selected as "Indicator" in the Pickup Coil panel.
  3. Put the bar magnet inside the pickup coil.
  4. Press the "Flip Polarity" button, possibly many times. Eventually there will be a time when the bulb does not light.

When this occurs, the "Flip Polarity" button also looks like it's stuck in the "down" position for a moment after pressing it.

pixelzoom commented 10 months ago

I verified that coil.currentAmplitudeProperty and lightBulb.brightnessProperty are indeed changing as expected. But that occassionally results in no change to the view -- i.e., no visible rays.

pixelzoom commented 10 months ago

In the Unfuddle ticket (see above), Mike Dubson said:

I believe the best long-term solution is to alter the model (slightly) to include damping, as is done in the flash sim Faraday's law, so that single-time-step changes are smoothed out over several time-steps.

It's not clear whether that damping was ported to the HTML5 version of faradays-law. But poking around, I suspect that it is the step method in Voltmeter.js, because BulbNode.js uses voltmeter.voltageProperty.

Since there's already (broken? see #73) damping in FEL's Voltmeter.ts, I might be better off using that, than trying to extract something from faradays-law. And we may need to turn damping off when using the AC power supply.

pixelzoom commented 9 months ago

Looking for a quick-&-dirty workaround, I tried changing the bulb's brightness in the next animaton frame, but that did not resolve the problem.

Here's the diff for LightBulb.ts. I tried this with both stepTimer and animatonFrameTimer.

-      this._brightnessProperty.value = brightness;
+      stepTimer.runOnNextTick( () => {
+        this._brightnessProperty.value = brightness;
+      } );
pixelzoom commented 9 months ago

Discussed with @jonathanolson on Zoom.

The problem is likely with the use of ConstantDtClock. It looks like LightBulb is occassionally getting stepped multiple times per animation frame, so currentAmplitude goes from 0 => peak => 0, and we therefore don't see the peak.

@jonathanolson pointed me to phet-core's EventTimer, and we discussed various strategies for interpolating what happens between animation frames.

pixelzoom commented 9 months ago

In https://github.com/phetsims/faradays-electromagnetic-lab/commit/812e73ca614ce47c5c5639d1db09745419d14f07, I reimplemented ConstantDtClock as a subclass of EventTimer. So that I didn't need to change the API of ConstantDtClock, I added the ability to add listeners.

The problem with the light bulb is still present, because I haven't changed anything to address when the case where calling EventTimer step results in multiple calls to its eventCallback.

pixelzoom commented 9 months ago

This problem seems to have gone away after reimplementing the electrons view using scenery Sprites, see https://github.com/phetsims/faradays-electromagnetic-lab/issues/49#issuecomment-1965394557. The "Flip Polarity" button no longer stays stuck down, and I don't see any missing light rays. My hypothesis is that there was an occassional GC happening, possibly due to the addChild/removeChild approach of the previous implementation of electrons (which was also used in the Java version, where this bug was first reported), and we were occassionally hitting that GC.

I'll leave this open, and bang on it a bit more to be sure.

pixelzoom commented 9 months ago

I pressed the "Flip Polarity" button 300 times and never encountered this problem. I used to consistently hit the problem in 15-20 presses. So I think it's safe to consider this resolved and close this issue.

phet-dev commented 9 months ago

Reopening because there is a TODO marked for this issue.

pixelzoom commented 5 months ago

This problem is still occurring, was reported for https://github.com/phetsims/qa/issues/1091, and is now being tracked in https://github.com/phetsims/faradays-electromagnetic-lab/issues/180.