Skip to content

Commit

Permalink
Linking nodes to phetio model elements, see #63
Browse files Browse the repository at this point in the history
  • Loading branch information
AgustinVallejo committed Dec 20, 2024
1 parent d2ab63b commit 0eea1a8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
15 changes: 11 additions & 4 deletions js/photons/model/Laser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import TReadOnlyProperty from '../../../../axon/js/TReadOnlyProperty.js';
import dotRandom from '../../../../dot/js/dotRandom.js';
import Range from '../../../../dot/js/Range.js';
import Vector2 from '../../../../dot/js/Vector2.js';
import PickRequired from '../../../../phet-core/js/types/PickRequired.js';
import { PhetioObjectOptions } from '../../../../tandem/js/PhetioObject.js';
import optionize from '../../../../phet-core/js/optionize.js';
import WithRequired from '../../../../phet-core/js/types/WithRequired.js';
import PhetioObject, { PhetioObjectOptions } from '../../../../tandem/js/PhetioObject.js';
import Tandem from '../../../../tandem/js/Tandem.js';
import StringUnionIO from '../../../../tandem/js/types/StringUnionIO.js';
import quantumMeasurement from '../../quantumMeasurement.js';
Expand All @@ -27,7 +28,7 @@ export type PhotonEmissionMode = 'singlePhoton' | 'manyPhotons';
type SelfOptions = {
emissionMode: PhotonEmissionMode;
};
type LaserOptions = SelfOptions & PickRequired<PhetioObjectOptions, 'tandem'>;
type LaserOptions = SelfOptions & WithRequired<PhetioObjectOptions, 'tandem'>;

const PolarizationPresetValues = [ 'vertical', 'horizontal', 'fortyFiveDegrees', 'unpolarized', 'custom' ] as const;
export type PolarizationPresets = ( typeof PolarizationPresetValues )[number];
Expand All @@ -46,7 +47,7 @@ const MAP_OF_PRESET_POLARIZATION_ANGLES = new Map<PolarizationPresets, number>(
]
);

class Laser {
class Laser extends PhetioObject {

// The position of the detector in two-dimensional space. Units are in meters.
public readonly position: Vector2;
Expand Down Expand Up @@ -84,6 +85,12 @@ class Laser {

public constructor( position: Vector2, photonCollection: PhotonCollection, providedOptions: LaserOptions ) {

const options = optionize<LaserOptions, SelfOptions, PhetioObjectOptions>()( {
phetioState: false
}, providedOptions );

super( options );

this.position = position;
this.photonCollection = photonCollection;
this.emissionMode = providedOptions.emissionMode;
Expand Down
17 changes: 10 additions & 7 deletions js/photons/model/PhotonDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import NumberProperty from '../../../../axon/js/NumberProperty.js';
import Range from '../../../../dot/js/Range.js';
import Vector2 from '../../../../dot/js/Vector2.js';
import { Line } from '../../../../kite/js/imports.js';
import { combineOptions } from '../../../../phet-core/js/optionize.js';
import PickRequired from '../../../../phet-core/js/types/PickRequired.js';
import { PhetioObjectOptions } from '../../../../tandem/js/PhetioObject.js';
import optionize from '../../../../phet-core/js/optionize.js';
import WithRequired from '../../../../phet-core/js/types/WithRequired.js';
import PhetioObject, { PhetioObjectOptions } from '../../../../tandem/js/PhetioObject.js';
import Tandem from '../../../../tandem/js/Tandem.js';
import AveragingCounterNumberProperty from '../../common/model/AveragingCounterNumberProperty.js';
import quantumMeasurement from '../../quantumMeasurement.js';
Expand All @@ -26,7 +26,7 @@ import { TPhotonInteraction } from './TPhotonInteraction.js';
type SelfOptions = {
displayMode?: DisplayMode;
};
type PhotonDetectorOptions = SelfOptions & PickRequired<PhetioObjectOptions, 'tandem'>;
type PhotonDetectorOptions = SelfOptions & WithRequired<PhetioObjectOptions, 'tandem'>;

// Define a type for the direction in which the detector is looking for photons.
export type DetectionDirection = ( [ 'up', 'down' ] )[number];
Expand All @@ -38,7 +38,7 @@ export type DisplayMode = ( [ 'count', 'rate' ] )[number];
export const COUNT_RANGE = new Range( 0, 999 );
export const RATE_RANGE = new Range( 0, 999 ); // in events per second

class PhotonDetector implements TPhotonInteraction {
class PhotonDetector extends PhetioObject implements TPhotonInteraction {

// The position of the detector in two-dimensional space. Units are in meters.
public readonly position: Vector2;
Expand Down Expand Up @@ -71,10 +71,13 @@ class PhotonDetector implements TPhotonInteraction {

public constructor( position: Vector2, detectionDirection: DetectionDirection, providedOptions: PhotonDetectorOptions ) {

const options = combineOptions<PhotonDetectorOptions>( {
displayMode: 'count'
const options = optionize<PhotonDetectorOptions, SelfOptions, PhetioObjectOptions>()( {
displayMode: 'count',
phetioState: false
}, providedOptions );

super( options );

this.position = position;
this.detectionDirection = detectionDirection;
this.displayMode = options.displayMode!;
Expand Down
2 changes: 2 additions & 0 deletions js/photons/view/LaserNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ export default class LaserNode extends Node {
}, providedOptions );

super( options );

this.addLinkedElement( model );
}
}

Expand Down
4 changes: 3 additions & 1 deletion js/photons/view/PhotonDetectorNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class PhotonDetectorNode extends Node {

public constructor( model: PhotonDetector,
modelViewTransform: ModelViewTransform2,
providedOptions?: PhotonDetectorNodeOptions ) {
providedOptions: PhotonDetectorNodeOptions ) {

// Create the detection aperture. This is essentially the anchor point for reset of the layout, meaning that the
// other nodes are positioned relative to this.
Expand Down Expand Up @@ -148,6 +148,8 @@ export default class PhotonDetectorNode extends Node {
);

super( options );

this.addLinkedElement( model );
}
}

Expand Down
8 changes: 6 additions & 2 deletions js/photons/view/PhotonTestingArea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,15 @@ class PhotonTestingArea extends Node {

const verticalPolarizationDetector = new PhotonDetectorNode(
model.verticalPolarizationDetector,
photonTestingAreaModelViewTransform
photonTestingAreaModelViewTransform, {
tandem: providedOptions.tandem.createTandem( 'verticalPolarizationDetector' )
}
);
const horizontalPolarizationDetector = new PhotonDetectorNode(
model.horizontalPolarizationDetector,
photonTestingAreaModelViewTransform
photonTestingAreaModelViewTransform, {
tandem: providedOptions.tandem.createTandem( 'horizontalPolarizationDetector' )
}
);

const polarizingBeamSplitterNode = new PolarizingBeamSplitterNode(
Expand Down

0 comments on commit 0eea1a8

Please sign in to comment.