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

hooks to make PhET-iO API calls on 'step' #570

Closed
pixelzoom opened this issue Jul 22, 2019 · 30 comments
Closed

hooks to make PhET-iO API calls on 'step' #570

pixelzoom opened this issue Jul 22, 2019 · 30 comments

Comments

@pixelzoom
Copy link
Contributor

pixelzoom commented Jul 22, 2019

From 7/18/19 Gas Properties design meeting notes in phetsims/gas-properties#30 (comment):

  • Are there hooks to make API calls on step?

The use case is to allow the client to get an array of particle speeds each time the model is stepped.

The answer is "not currently supported". There are some ideas in the Slack discussion below, but no final decision. @kathy-phet @ariel-phet @arouinfar FYI, prioritization needed.

Slack discussion

Chris Malley 3:14 PM
Does PhET-iO provide clients with a way to optionally call a function on each call to Sim stepSimulation? The use case is in Gas Properties, where the design team would like to provide an API function that allows the client to get an array of particle speeds each time the model is stepped.

Michael Kauzmann:house_with_garden: 3:15 PM
@jessegreenberg @samreid and I were just looking at that! We noticed that stepSimulationAction is an Action, so we can't add listeners to it for PhET-iO
Not sure if @samreid or @jessegreenberg made an issue.

Mainly we noted that phetsims/axon#241 wasn't complete, because ActionIO has an addListener function which is buggy.

Chris Malley 3:17 PM
Listen to frameStartedEmitter or frameEndedEmitter?

Michael Kauzmann:house_with_garden: 3:18 PM
Yes perhaps that is the solution, though they aren't currently instrumented. That seems like adding unneeded complexity. That said I think that may be the best/easiest solution.

Chris Malley 3:18 PM
It also provides the flexibility to be notified before or after the step has occurred.

Michael Kauzmann:house_with_garden: 3:19 PM
So you think that perhaps instrumenting both is the way to go?

Chris Malley 3:19 PM
This is one of 3 “Ask PhET-iO Team” action items that I have from last week’s Gas Properties PhET-iO meeting. Perhaps I should create an issue for each of them? See phetsims/gas-properties#30 (comment)
The other alternative would be a sim-specific {Emitter} particleSystemChanged that fires each time the particle system has changed. But I’m hesitant to add any more overhead that occurs in step.

Michael Kauzmann:house_with_garden: 3:21 PM
Sounds excellent. Also it would be good to note that since phet-io listeners are inherently async, perhaps only the frame ended emitter should be instrumented, because likely the frame will complete before the thread executes anything from the wrapper frame.

@samreid
Copy link
Member

samreid commented Jul 22, 2019

There is discussion and a proposal in phetsims/axon#222 (comment) about how to deal with Action and listeners. In this case, it would mean instrumenting Sim.frameEndedEmitter. @zepumph and @pixelzoom sound OK?

@pixelzoom
Copy link
Contributor Author

Sim.frameEndedEmitter sounds fine for hooks at the end of a frame. You'll need to ask the designers if they think there's a need for hooks at the start of a frame.

@pixelzoom pixelzoom removed their assignment Jul 23, 2019
@samreid
Copy link
Member

samreid commented Jul 23, 2019

@zepumph pointed out in another thread that this method is asynchronous, and it is unnecessary to have both started and ended, since they would likely be called at the same time anyways.

@kathy-phet
Copy link

I don't know what happens on a frame well enough to know whether we would need control over the start or the end of the frame. I think what would be important is that we don't get model values that are mid-way calculated .. e.g. PV before being updated and NT from after being updates. We need them to be either all calculated or all before the calculation update.

@zepumph
Copy link
Member

zepumph commented Jul 23, 2019

My vote would be for instrumenting the frameEndedEmitter, not both.

@zepumph
Copy link
Member

zepumph commented Jul 23, 2019

We need them to be either all calculated or all before the calculation update.

Because of the asynchronous nature of phet-io when communicating from the sim frame to the wrapper frame, all calculations would be done in the sim for a given frame before the wrapper get's any messages for that frame.

@zepumph zepumph removed their assignment Jul 23, 2019
@pixelzoom
Copy link
Contributor Author

pixelzoom commented Jul 23, 2019

I agree with @kathy-phet's comment in #570 (comment), but that pertains to a specific case, Graphing Quadratics. The question about whether both pre and post callbacks are needed is a general question. If the answer is "no", that's fine. I just want to make sure we're considering this generally, not doing something specific for Graphing Quadratics.

@kathy-phet
Copy link

I think Chris means Gas Properties.

@pixelzoom
Copy link
Contributor Author

Sorry, yeah, Gas Properties (the other "G" sim :)

@samreid
Copy link
Member

samreid commented Aug 22, 2020

This is pending discussion in https://github.com/phetsims/phet-io/issues/1543, on hold until we discuss there.

@zepumph
Copy link
Member

zepumph commented Sep 2, 2020

@samreid and I think we will just make this an Emitter. It makes sense to convert the stepSimulationAction to be an Emitter so that it can be listened to. Over to @samreid for implementation.

@samreid
Copy link
Member

samreid commented Sep 3, 2020

Some questions for @zepumph before we proceed:

  1. stepSimulationAction is currently type {Action}. All 24 of our phetioPlayback: true are currently {Action} and none of them are {Emitter}. Does the playback engine support Emitter?
  2. Previously in this issue we discussed instrumenting the frameEndedEmitter but the prior comment is about stepSimulationAction, what is your recommendation?

Please reassign to me after answering.

@samreid samreid assigned zepumph and unassigned samreid Sep 3, 2020
@zepumph
Copy link
Member

zepumph commented Sep 3, 2020

stepSimulationAction is currently type {Action}. All 24 of our phetioPlayback: true are currently {Action} and none of them are {Emitter}. Does the playback engine support Emitter?

Yes I believe so. Currently there are no Emitters that are phetioPlayback:true, but all the wrapper-side cares about is the "emitted" event, which Actions and Emitters send (in handlePlaybackEvent.js:

      // handle playback Actions
      else if ( event.name === 'emitted' ) {
        const orderedArgs = event.metadata.dataKeys.map( argumentName => event.data[ argumentName ] );
        totalInvokes++;
        phetioClient.invoke( event.phetioID, 'execute', orderedArgs, completedInvoke );
      }

Previously in this issue we discussed instrumenting the frameEndedEmitter but the prior comment is about stepSimulationAction, what is your recommendation?

Yes sorry. Any one of these, no preference. frameEndedEmitter is great.

@zepumph zepumph removed their assignment Sep 3, 2020
@pixelzoom
Copy link
Contributor Author

"Each time the model is stepped" is not the same as "the end of every frame". Sim.frameEndedEmitter continues to emit when the model is not being stepped.

@samreid
Copy link
Member

samreid commented Sep 8, 2020

@pixelzoom can you please assign to the designer(s) for this sim for clarification?

@samreid samreid assigned pixelzoom and unassigned samreid Sep 8, 2020
@pixelzoom
Copy link
Contributor Author

Since this issue is a breakout from meeting notes in phetsims/gas-properties#30 (comment), assigning to @arouinfar.

@pixelzoom pixelzoom assigned arouinfar and unassigned pixelzoom Sep 8, 2020
@arouinfar
Copy link

From a design perspective, it doesn't seem particularly important to have the emitter fire while the sim is paused, but it also doesn't seem harmful. While the sim is playing, does frameEndedEmitter align with the model being stepped? If so, that would fulfill the original request. I'm not sure of the pros/cons of using frame vs. step and which is preferable in a more general case. If you want more designer feedback, please add this issue to the phet-io meeting agenda.

@arouinfar arouinfar assigned samreid and unassigned arouinfar Sep 15, 2020
@samreid
Copy link
Member

samreid commented Sep 16, 2020

There is no standard interface for pausing and running models, so something would have to be done on a sim-by-sim basis for this. I'm not sure it is worth the effort. If the client only wants updates when the model is running, they could add a check that says

client.invoke('frameEndedEmitter','addListener',[()=>{
    
// check if the model is running.  If it is, then collect the data
client.invoke('model.isRunningProperty','getValue',[],(returnValue)=>{

  // Only access the particle speeds if the model is running
  returnValue && client.invoke('model','getParticleSpeeds',[],(particleSpeeds)=>{
  // etc.

});
});

}]);

PhET-iO used to have a way to pass an API call "schema" to the core side to evaluate at a specific time synchronously, but we dropped support for that and now have to use the asynchronous method from above.

@samreid samreid assigned arouinfar and pixelzoom and unassigned samreid Sep 16, 2020
@pixelzoom
Copy link
Contributor Author

I'm still not sure what the requirements are here, so I don't know what additional feedback to give. The closest I can find to a requirement is what @kathy-phet alluded to in #570 (comment):

I think what would be important is that we don't get model values that are mid-way calculated .. e.g. PV before being updated and NT from after being updates. We need them to be either all calculated or all before the calculation update.

No idea what PV and NT mean. And I would think that view values would be as important as model values. But this sounds like a desire to have a way to get notification that all values have finished changing, and you can safely check values without getting any intermediate values. If I interpret that correctly, then frameEndedEmitter gives you that, with the inconvenience of being notified even when nothing could have possibly changed in the sim.

@pixelzoom pixelzoom removed their assignment Sep 17, 2020
@arouinfar
Copy link

I don't think an asynchronous conversation is going to be very productive here, so I've added this issue to the agenda for next Thursday's phet-io meeting.

@zepumph
Copy link
Member

zepumph commented Oct 8, 2020

We discussed this at phet-io meeting today, and designers and devs feel like the way @samreid outlined in #570 (comment) will work for this use case, and if, in the future, the asynchronous behavior is not acceptable, then we can investigate adding support for running commands inside the sim frame.

@pixelzoom how does this sound to you? Anything else here?

@pixelzoom
Copy link
Contributor Author

I've never been clear on the requirements, and this issue was opened at the request of designers. So if designers are OK with what @samreid proposed in #570 (comment), then that sounds fine to me.

Should an example be added to client-facing documentation?

@pixelzoom pixelzoom assigned zepumph and unassigned pixelzoom Oct 12, 2020
@zepumph
Copy link
Member

zepumph commented Oct 12, 2020

Maybe it would be nice to start a "FAQ" section of the wrapper index, that can expand for some use cases that devs have as they do. Part of me feels like this goes beyond the scope of our client facing doc right now, and I'm tentative to add to our current wrapper index before it gets a face lift.

@zepumph
Copy link
Member

zepumph commented Nov 13, 2020

I think we can close this issue. There is no good place to add client facing documentation at this time. If we feel like this is an important feature for a sim, then it can be added its client guide.

Closing

@zepumph zepumph closed this as completed Nov 13, 2020
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

6 participants