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

PhET-iO instrumentation #83

Open
13 of 56 tasks
chrisklus opened this issue Mar 5, 2019 · 4 comments
Open
13 of 56 tasks

PhET-iO instrumentation #83

chrisklus opened this issue Mar 5, 2019 · 4 comments

Comments

@chrisklus
Copy link
Contributor

chrisklus commented Mar 5, 2019

Before instrumenting

  • Create a "PhET-iO Instrumentation" issue in the simulation repository. Copy this checklist to the issue description (top issue comment) for tracking. From https://github.com/phetsims/phet-io/commit/bf946f5fd7b377c5ba465b272f78f2bf3ef5b411.
  • Understand the goal. Launch the Faraday's Law wrapper index, read through it and visit all of the linked wrappers and docs. Test each wrapper, investigate, report bugs, ask questions!
  • Create a --brands=phet dev release before instrumentation. This creates a benchmark to reference against for memory-leaks, sim size, performance, etc. Document the dev release in the sim's phet-io github issue.
  • Schedule a PhET-iO design meeting for the simulation to identify what should be customizable/interoperable/data stream and track it in an issue. For example, see how-to-design-phet-io-features-for-a-simulation.md Think about how a researcher or 3rd party may wish to configure the simulation or collect data from it, and make sure that is supported by the instrumentation. For example, some simulations will need custom higher-level events (such as
    whether the user created a parallel circuit), for events that are useful, easy to compute in simulation code and difficult to compute in wrapper code. Or a simulation may need to be configurable in a way that is not already supported by the instrumentation you have already completed. These features should be determined in the PhET-iO design meeting.
  • Typically it is best if the responsible developer for the simulation is available to perform the PhET-iO instrumentation. They have important insight into the structure, history, trade-offs and other important details of the simulation implementation that will facilitate the instrumentation. If the responsible developer is not available for instrumentation, it would be nice if they are available for consultation or support during instrumentation.

Code Review

A high-quality Code Review will make instrumentation easier, promote long term maintainability for the
simulation, and protect the simulation from a volatile API. If the simulation is already in good shape, the review will not take too long. If the simulation is not in good shape, then it needs your help.

  • Read through the open issues and be aware of any outstanding problems, future work, etc.
  • If there is a branch with significant effort, consider merging it before instrumentation.
  • Complete any planned refactorings.
  • Address TODOs in the code
  • Bring the sim up to standards.
  • If there are sim components that can be exchanged to use newer common code ones, do so. Consulting phet design patterns may be helpful.

Instrumentation

Now that the simulation is in good shape and the PhET-iO design meeting is complete, we are ready to instrument the simulation. Follow the checklist below, and if you have questions you can review Faraday's Law and its PhET-iO instrumentation or reach out to teammates who may have come this way before.

Initial Setup

  • Add 'phet-io' as a supportedBrand in the sim's package.json. A script on Bayes will automatically add the simulation to the list of phet-io simulations. This will make it possible to use phetmarks to launch wrappers for testing. This also will add it to continuous fuzz testing with phetioValidateTandems=false.
  • Import Tandem to main.js, see faradays-law-main.js for an example
  • Pass tandems to each screen using tandem.createTandem(...)

Visit Objects that Should be Instrumented

Consult the PhET-iO design issue to see what features the sim should support. See
https://github.com/phetsims/tandem/tree/master/js/PhetioObject.js for the
supported PhetioObject options. Not every node in the hierarchy must be instrumented, but every leaf is instrumented. For example the view is rarely instrumented.

  • Recursively pass tandems and other PhetioObject options into objects that should be instrumented. Do not instrument objects that are "implementation details" and do not over-instrument.
  • Instrument user interface components such as Checkbox, HSlider, etc.
  • Instrument model components such as Axon Property that are critical to the save state or operation of the sim. This does not necessarily include "implementation details" that should be hidden from the public API; again a design meeting may be needed here. (Note that some Property sub-classes utilize options specific for use with PhET-IO, units in NumberProperty for example, and should be passed where appropriate.)
  • Instrument all of the features identified in the simulation PhET-iO design issue.

Creating and Naming Tandems

Well-designed tandem names are important. Once the PhET-iO simulation is published, the API becomes public and therefore difficult to change. Sometimes PhET-iO design meetings can also help come up tandem names. NOTE: "Tandem" is a PhET internal name, publicly to clients the full strings are known as "phetioIDs."

  • Screen tandems should end with a Screen suffix.
  • Property instances should have Property suffix.
  • The screen's model and view should be named model and view.
  • Tandems should be named as we wish clients to see them, and for long-term stability. For maintainability, local vars should be renamed to match tandem names.
  • When adding tandem args to constructors, please follow the following heuristic regarding required vs optional tandem args from Should tandem be in options or required param? joist#489 (comment):
    • Use @param tandem for constructors that don't have an options parameter. This typically includes top-level model and view types that are specific to the sim.
    • Use tandem in options object: Tandem.required for constructors that already have an options parameter. This default can be helpful for identifying cases where you have neglected to pass a tandem in (because Tandem.required will error loudly if validating tandems).
  • Use createGroupTandem for arrays or otherwise numbered tandems. See usages for examples.
  • Use static tandems where necessary. For instance, static objects that are not created from the main sequence. See BeersLawSolution.js for an example.
  • When naming any input listener that will be added to a Node, the tandem name should be 'inputListener'. This will ensure a general and maintainable tandem structure.

Feature Support

  • Where appropriate, create or instrument Property instances to make it possible to get/set a value, so value changes will appear on the data stream and so the item can be stored and restored in save/load.
  • If necessary, instrument common code components that are not yet instrumented. You can check if something is instrumented by checking whether it extends PhetioObject and whether it supplies any PhetioObject options. To instrument a new common code component, you may need to add instrumented Property or Emitter elements by composition, or subclass PhetioObject.
    Run phetmarks=>aqua=>Test Sims(Fast Build) with PhET-iO checked. This will help catch any simulations using the component you just instrumented.
  • Add tandem: Tandem.required or tandem: Tandem.optional to the options accordingly. Here are some conventions to guide this decision:
    • Most UI components are Tandem.required
    • Event if a common code component that your subtype extends is a certain option, it is safest to set this tandem constraint at the subtype level in sim specific code too.
    • The safest convention for required tandems is to add tandem: Tandem.required to default options wherever you intend to pass tandem via options.
    • It's absolutely required wherever you intend to use options.tandem.createOptions to create a tandem for a subcomponent.
    • For more information/context see What subtypes need to extend Tandem.required? graphing-quadratics#64 (comment)
  • Note Node already extends PhetioObject--its PhetioObject options should be provided to the constructor or mutate but not both.
  • Use the phetioPrintMissingTandems flag if you want to collect a list of all required, optional, and uninstrumented common code classes instead of erroring out on the first missing tandem. Each occurrence is numbered to give a better idea of how many the sim has to do.
  • Transient State should not be saved. For instance, whether a button is highlighted from mouseover, or whether the About dialog is showing should not be part of the save state of the simulation.
  • Run the simulation with ?phetioValidateTandems to see if you missed anything that should be instrumented.
  • Add the simulation to perennial/data/testable-phet-io-validated so CT will test with phetioValidateTandems=true
  • Use the following conventions regarding phetioDocumentation:
    • Documentation strings do not have to be in complete sentences, i.e. "The location of the center of the bar magnet in view coordinates" seems complete even though it is just a subject.
    • Following up on this, the above example does not need (and shouldn't have) a period because it isn't a sentence.
    • We want phetioDocumentation that is client facing to start with a capital letter.
    • We allow HTML in our documentation strings to be rendered by the wrapper.
    • There is no need to restrict any characters, if chars break the HTML, it should be manually verified in Studio before publication.
    • For now we there isn't validation, only manual inspection as you instrument.
    • see https://github.com/phetsims/phet-io/issues/1334#issuecomment-405368103 for context
  • Port vibe audio (if any) to tambo. See Rewrite Vibe to use Tambo vibe#33 and note that PhET-iO query
    parameters support tambo but not vibe.
  • Avoid instrumenting values in DEFAULT_OPTIONS or at least be very careful about how it is done, see the concerns
    mentioned in https://github.com/phetsims/phet-io/issues/1179

Create new IO types

If necessary, create new IO types to support desired feature set. Generally we don't want to be locked in to coupling IO Types to sim types. Instead, we decided that we want the PhET-iO API to be able to vary independently from the sim implementation instead of leaking sim implementation details. Still, for a well-designed simulation, IO Types will often match closely with the sim types. To ensure good IO type inheritance hierarchies follow these principles:

  • factor out duplicated code or responsibilities
  • having the sim developer involved in instrumentation
  • making sure everything is reviewed
    See sloppy TTypes beers-law-lab#213 for more context on prior problems in this area and discussion about it.

The Data Stream

  • Create Emitter instances as appropriate to augment the data stream.
  • Instrumented Emitters and Property instances naturally emit to a structured data stream and are probably what you need. If you need something more custom, you can call phetioStartEvent and phetioEndEvent directly.
  • Disabled components should not deliver events, even when clicked. Change them to be pickable=false when disabled.
    See https://github.com/phetsims/phet-io/issues/282
  • New code should use Emitter.addListener instead of Events.onStatic
  • To suppress an Emitter.emitN argument, you may specify VoidIO for its type, see PressListener.js
  • When instrumenting new types, make sure that events are marked as phetioEventType: 'user' for pointer events, keyboard events and UI events
    (like checkbox toggled, button pressed), and phetioEventType: 'model' for model actions/responses. This is easiest to test in the console: colorized wrapper. Model events will be logged in black, and user events will be logged blue. You can also go to the data-stream wrapper to see events in JSON form. If your simulation only leverages existing model types (like Property/Emitter) and UI types (like sun components), then you will not be instrumenting new types.

Post Instrumentation and Checks

  • Make sure unused PhetioObject instances are disposed, which unregisters the tandem.
  • Make sure Joist dt values are used instead of Date.now() or other Date functions. This is necessary for reproducible playback via input events. Perhaps try phet.joist.elapsedTime.
  • Are random numbers using phet.joist.random, and all doing so after modules are declared (non-statically)? For example, the following methods (and perhaps others) should not be used: Math.random, _.shuffle, _.sample, _.random.
  • Like JSON, keys for undefined values are omitted in the state--consider this when determining whether toStateObject should use null or undefined values.
  • Verify that the simulation works in all of the phet-io wrappers. Launch the "index" wrapper at
    http://localhost/phet-io-wrappers/index/?sim={{simulation-name}} and test all the links. To further understand what each wrapper exemplifies, read the description for it in the wrapper index, and launch that wrapper with a sim already completely PhET-iO instrumented like Faraday's Law.
  • Build with grunt --brands=phet-io and test the built version by launching build/wrappers/index and testing all the links.
  • Manually look through Studio to make sure that tandems work as expected and are formatted correctly.
  • Perform a full test for memory leaks. The benchmark dev release can be helpful here. This will help catch faulty tandem disposal. PhET-iO instantiates different objects and wires up listeners that are not present in the PhET-branded simulation. It needs to be tested separately for memory leaks. Use ?ea&brand=phet-io&phetioStandalone&fuzz to run with assertions, PhET-iO brand and fuzzing.
  • Run phetmarks=>aqua=>Test Sims(Fast Build) with PhET-iO checked. This will help catch any simulations using the component you just instrumented. Next you will need to pass tandems for those cases.

Support dynamic state

For simulations that have static content (such as a fixed number of objects and properties), instrumentation is complete after you have completed the preceding steps. For simulations that have a dynamic number of objects, such as Circuit Construction Kit circuits or Molecules and Light photons, the containers and elements must be instrumented. This is currently tricky with PhET-iO. Some sims may wish to avoid this entire hassle by pre-allocating all of the instrumented instances. Consider adding flags to indicate whether the objects are "alive" or "in the pool".

Details about how to support dynamic state.

Beer's Law Lab and Charges and Fields demonstrates how this may be done. A container class defines two methods:
clearChildInstances which empties a container and addChildInstance which repopulates a container one element at a
time. For example, see ShakerParticlesIO in the beers-law-lab instrumentation.

When state is set, first the container is cleared, then children are created. Child states can be obtained from toStateObject
and set back with fromStateObject, with an additional call to setValue in case additional data is supplied, or custom
code can be used.

Dispose must be implemented properly on all dynamic instances, or else it will result in stale values in the playback sim.
For example, if a simulation is sending the position of a particle as a property, if the particle position property
hasn't been disposed of, the simulation will try to create a new property with the same id and hence throw an assertion
error because that tandem is already registered.

On January 11, 2017 ControlPoints were not being disposed correctly in Energy-skate-park-basics, causing a mysterious bug
(impossible set state), make sure that children are being disposed correctly before creating them in the downstream sim!

Other tips and tricks for "impossible set state":

  • addChildInstance must return the instance, it is used as a flag to determine whether addition was successful
  • the given tandems must be reused. Do not use GroupTandem to assign a new tandem, use the specified tandem so the object
    can be addressed the same way

Dispose functions must be added to types that are instrumented. But that's only half of the memory management issue. The
other half is revisiting memory management for all instances that don't exist for the lifetime of the sim, and verifying
that tandems are properly cleaned up.

Tips, Tricks, Notes, Misc

  • When testing iframes in Chrome, you sometimes must hit refresh twice in order to test your code changes. This is one reason that testing without iframes, using the `Data: colorized" wrapper is sometimes preferable.
  • Sometimes toStateObject and fromStateObject need to manage private state, so must be declared in the type itself, see https://github.com/phetsims/phet-io/issues/107
  • When navigating to wrappers, the easiest way to get to the whole wrapper suite is through the "wrapper index." After a while of testing it can be annoying to have the extra step: phetmarks --> index --> desired wrapper. Instead you can use phetmarks to launch any individual wrapper. Note that the wrapper index in the build version is at the top level of the build dir (build/phet-io/).

Two types of serialization

Data type serialization For example, numbers, strings, Vector2 instances fall into this category. These values are instantiated by fromStateObject.

Reference type serialization For example, Nodes and Properties. For example, if a simulation has one heightProperty that exists for the lifetime of the sim then when we save the state of the sim, we save the dynamic characteristics of the heightProperty (rather than trying to serialize the entire list of listeners and phet-io metadata. Then the PhET-iO library calls setValue() to update the dynamic characteristics of the heightProperty without dealing with all of Property's many attributes. The static setValue methods on IO Types are automatically called by PhET-iO to restore dynamic characteristics of reference-type serialized instances. Search for toStateObject in *IO.js files for examples.

Review and Publication

Happy instrumenting!

@chrisklus chrisklus self-assigned this Mar 5, 2019
@chrisklus
Copy link
Contributor Author

chrisklus added a commit that referenced this issue Mar 5, 2019
chrisklus added a commit that referenced this issue Mar 6, 2019
chrisklus added a commit that referenced this issue Mar 14, 2019
chrisklus added a commit that referenced this issue Mar 14, 2019
chrisklus added a commit that referenced this issue Mar 14, 2019
chrisklus added a commit that referenced this issue Mar 14, 2019
chrisklus added a commit that referenced this issue Mar 26, 2019
chrisklus added a commit that referenced this issue Apr 22, 2019
chrisklus added a commit that referenced this issue Apr 24, 2019
@chrisklus
Copy link
Contributor Author

@arnabp the instrumentation process required a non-trivial refactor for item number 5 on the list in #36 (comment). The relevant changes are in 1dc0c91, could you please review?

@arnabp
Copy link
Contributor

arnabp commented May 8, 2019

@chrisklus reviewed BGRAndStarDisplay. I put the node's initial layout placement back in BlackbodySpectrumScreenView, everything else looks good.

@arnabp arnabp removed their assignment May 8, 2019
@chrisklus
Copy link
Contributor Author

@arnabp sounds good, thanks.

@chrisklus chrisklus removed their assignment May 1, 2023
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

2 participants