Skip to content

Commit

Permalink
Changing measurementStateProperty for a simpler measured emitter, see #…
Browse files Browse the repository at this point in the history
  • Loading branch information
AgustinVallejo committed Nov 18, 2024
1 parent 99eee3c commit 132d6ff
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 51 deletions.
41 changes: 4 additions & 37 deletions js/spin/model/MeasurementLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
* @author Agustín Vallejo
*/

import Emitter from '../../../../axon/js/Emitter.js';
import Property from '../../../../axon/js/Property.js';
import stepTimer from '../../../../axon/js/stepTimer.js';
import Vector2 from '../../../../dot/js/Vector2.js';
import Vector2Property from '../../../../dot/js/Vector2Property.js';
import Enumeration from '../../../../phet-core/js/Enumeration.js';
import EnumerationValue from '../../../../phet-core/js/EnumerationValue.js';
import { EmptySelfOptions } from '../../../../phet-core/js/optionize.js';
import BooleanIO from '../../../../tandem/js/types/BooleanIO.js';
import AbstractBlochSphere, { AbstractBlochSphereOptions } from '../../common/model/AbstractBlochSphere.js';
Expand All @@ -22,20 +20,6 @@ type SelfOptions = EmptySelfOptions;

export type MeasurementLineOptions = SelfOptions & AbstractBlochSphereOptions;

export class MeasurementState extends EnumerationValue {
public static readonly NOT_MEASURED = new MeasurementState();
public static readonly MEASURING = new MeasurementState();
public static readonly MEASURED = new MeasurementState();

public static readonly MEASURING_TIMEOUT_DURATION = 1000;

public static readonly enumeration = new Enumeration( MeasurementState );

public constructor() {
super();
}
}

export default class MeasurementLine {

// The bloch sphere representation of the spin state
Expand All @@ -47,8 +31,8 @@ export default class MeasurementLine {
// The position of the line in the model
public readonly positionProperty: Vector2Property;

// Flag that indicates if a particle has been measured
public readonly measurementStateProperty: Property<MeasurementState>;
// Emitter that informs that there has been a measurement
public readonly measurementEmitter: Emitter;

// Flag to indicate if the line is active
public readonly isActiveProperty: Property<boolean>;
Expand All @@ -59,29 +43,13 @@ export default class MeasurementLine {
tandem: providedOptions.tandem.createTandem( 'spinStateProperty' )
} );

this.measurementStateProperty = new Property<MeasurementState>( MeasurementState.NOT_MEASURED );

this.measurementStateProperty.link( measurementState => {
if ( measurementState === MeasurementState.MEASURING ) {
// TODO: How to interrupt this after a restart? https://github.com/phetsims/quantum-measurement/issues/53
const measurementStateListener = () => {
this.measurementStateProperty.value = MeasurementState.MEASURED;
};

// Count some time and reset the hasMeasured flag
stepTimer.setTimeout( measurementStateListener, MeasurementState.MEASURING_TIMEOUT_DURATION );
}
} );
this.measurementEmitter = new Emitter();

this.isActiveProperty = new Property<boolean>( originallyActive, {
tandem: providedOptions.tandem.createTandem( 'isActiveProperty' ),
phetioValueType: BooleanIO
} );

this.isActiveProperty.link( () => {
this.measurementStateProperty.reset();
} );

this.simpleBlochSphere = new SimpleBlochSphere( this.spinStateProperty, providedOptions );

this.positionProperty = new Vector2Property( position, {
Expand All @@ -95,7 +63,6 @@ export default class MeasurementLine {

public reset(): void {
this.spinStateProperty.reset();
this.measurementStateProperty.reset();
this.isActiveProperty.reset();
}
}
Expand Down
3 changes: 1 addition & 2 deletions js/spin/model/ParticleSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import dotRandom from '../../../../dot/js/dotRandom.js';
import Vector2 from '../../../../dot/js/Vector2.js';
import quantumMeasurement from '../../quantumMeasurement.js';
import { MeasurementState } from './MeasurementLine.js';
import { ParticleWithSpin } from './ParticleWithSpin.js';
import { SpinDirection } from './SpinDirection.js';
import SpinModel from './SpinModel.js';
Expand Down Expand Up @@ -123,7 +122,7 @@ export class ParticleSystem {
// If the particle crosses a measurement line, we update the line
this.model.measurementLines.forEach( ( line, index ) => {
if ( behindMeasurementLine[ index ] && !line.isParticleBehind( particle.positionProperty.value ) ) {
line.measurementStateProperty.value = MeasurementState.MEASURING;
line.measurementEmitter.emit();
line.spinStateProperty.value = particle.spinVectors[ index ];
// particle.stageCompleted[ index ] = true;
}
Expand Down
2 changes: 0 additions & 2 deletions js/spin/model/SpinModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ export default class SpinModel implements TModel {
this.measurementLines[ 1 ].isActiveProperty.value = isSingle;
this.measurementLines[ 2 ].isActiveProperty.value = isSingle && !experiment.isShortExperiment;

this.measurementLines.forEach( line => line.measurementStateProperty.reset() );

this.particleSystem.reset();

this.sternGerlachs.forEach( ( SternGerlach, index ) => {
Expand Down
33 changes: 23 additions & 10 deletions js/spin/view/MeasurementLineNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @author Agustín Vallejo
*/

import stepTimer from '../../../../axon/js/stepTimer.js';
import Vector2 from '../../../../dot/js/Vector2.js';
import { Shape } from '../../../../kite/js/imports.js';
import optionize, { EmptySelfOptions } from '../../../../phet-core/js/optionize.js';
Expand All @@ -18,17 +19,20 @@ import QuantumMeasurementColors from '../../common/QuantumMeasurementColors.js';
import quantumMeasurementConstants from '../../common/QuantumMeasurementConstants.js';
import BlochSphereNode from '../../common/view/BlochSphereNode.js';
import quantumMeasurement from '../../quantumMeasurement.js';
import MeasurementLine, { MeasurementState } from '../model/MeasurementLine.js';
import MeasurementLine from '../model/MeasurementLine.js';

type SelfOptions = EmptySelfOptions;

type MeasurementLineNodeOptions = SelfOptions & PickRequired<VBoxOptions, 'tandem'>;

export default class MeasurementLineNode extends VBox {

private readonly simpleBlochSphereNode: BlochSphereNode;

public constructor( measurementLine: MeasurementLine, modelViewTransform: ModelViewTransform2, providedOptions: MeasurementLineNodeOptions ) {

const simpleBlochSphere = new BlochSphereNode( measurementLine.simpleBlochSphere, {
tandem: providedOptions.tandem.createTandem( 'simpleBlochSphere' ),
const simpleBlochSphereNode = new BlochSphereNode( measurementLine.simpleBlochSphere, {
tandem: providedOptions.tandem.createTandem( 'simpleBlochSphereNode' ),
drawKets: false,
drawTitle: false,
scale: 0.5
Expand Down Expand Up @@ -64,18 +68,21 @@ export default class MeasurementLineNode extends VBox {
scale: 0.7
} );

simpleBlochSphere.stateVectorVisibleProperty.value = false;
simpleBlochSphereNode.stateVectorVisibleProperty.value = false;

measurementLine.measurementStateProperty.link( measurementState => {
simpleBlochSphere.stateVectorVisibleProperty.value = measurementState !== MeasurementState.NOT_MEASURED;
cameraPath.fill = measurementState === MeasurementState.MEASURING ?
QuantumMeasurementColors.particleColor :
'black';
measurementLine.measurementEmitter.addListener( () => {
simpleBlochSphereNode.stateVectorVisibleProperty.value = true;
cameraPath.fill = QuantumMeasurementColors.particleColor;

stepTimer.setTimeout( () => {
cameraPath.fill = 'black';
}, 500 );
} );


const options = optionize<MeasurementLineNodeOptions, SelfOptions, VBoxOptions>()( {
children: [
simpleBlochSphere,
simpleBlochSphereNode,
cameraNode
],
spacing: 25,
Expand All @@ -84,10 +91,16 @@ export default class MeasurementLineNode extends VBox {

super( options );

this.simpleBlochSphereNode = simpleBlochSphereNode;

measurementLine.positionProperty.link( position => {
this.center = modelViewTransform.modelToViewPosition( position );
} );
}

public reset(): void {
this.simpleBlochSphereNode.stateVectorVisibleProperty.value = false;
}
}

quantumMeasurement.register( 'MeasurementLineNode', MeasurementLineNode );
7 changes: 7 additions & 0 deletions js/spin/view/SpinMeasurementArea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export default class SpinMeasurementArea extends VBox {

private manyParticlesCanvasNode: ManyParticlesCanvasNode;

private readonly measurementLines: MeasurementLineNode[];

public constructor( model: SpinModel, parentNode: Node, layoutBounds: Bounds2, tandem: Tandem ) {

const items: ComboBoxItem<SpinExperiment>[] = SpinExperiment.enumeration.values.map( experiment => {
Expand Down Expand Up @@ -279,12 +281,17 @@ export default class SpinMeasurementArea extends VBox {
yMargin: 10
} );

this.measurementLines = measurementLines;
this.manyParticlesCanvasNode = manyParticlesCanvasNode;
}

public step( dt: number ): void {
this.manyParticlesCanvasNode.step();
}

public reset(): void {
this.measurementLines.forEach( line => line.reset() );
}
}

quantumMeasurement.register( 'SpinMeasurementArea', SpinMeasurementArea );
1 change: 1 addition & 0 deletions js/spin/view/SpinScreenView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default class SpinScreenView extends QuantumMeasurementScreenView {
public override reset(): void {
this.model.reset();
super.reset();
this.spinMeasurementArea.reset();
}

public override step( dt: number ): void {
Expand Down

0 comments on commit 132d6ff

Please sign in to comment.