Skip to content

Commit

Permalink
Cleanup in CoilModel, see #86
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Nov 5, 2017
1 parent be9a60b commit 4d30c2c
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions js/faradays-law/model/CoilModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@ define( function( require ) {
var inherit = require( 'PHET_CORE/inherit' );
var Property = require( 'AXON/Property' );

// constants
// in pixels, set size for transition from B=constant to B=power law
var NEAR_FIELD_RADIUS = 50;

/**
* @param {Vector2} position - center of the coil
* @param {number} numberOfSpirals - number of spirals
* @param {MagnetModel} magnetModel - model of the magnet
* @constructor
*/
function CoilModel( position, numberOfSpirals, magnetModel ) {
var self = this;

// @private
this.sense = 1; //sense of magnet = +1 or -1, simulates flipping of magnet. Magnetic field sign

// @public (read-only) // TODO convert args to Vector2
// @public (read-only)
this.position = position;

// @private - current value of magnetic field
Expand All @@ -38,17 +41,15 @@ define( function( require ) {
// @public - signal strength in coil = 'electromotive force'
this.emfProperty = new Property( 0 );

magnetModel.flippedProperty.link( function( flipped ) {
self.sense = flipped ? -1 : 1; //change magnetic field direction
} );

// @private
this.magnetModel = magnetModel;

//from flash simulation
this.A = 50; //near-field radius in pixels, set size for transition from B=constant to B=power law
this.N = numberOfSpirals / 2; //number of turns in coil (equal to half the number of turns in the graphic image)
// @private
this.numberOfSpirals = numberOfSpirals;

this.reset();
// set up initial conditions
this.updateMagneticField();
this.previousMagneticFieldProperty.set( this.magneticFieldProperty.get() );
}

faradaysLaw.register( 'CoilModel', CoilModel );
Expand All @@ -72,11 +73,14 @@ define( function( require ) {
* @private
*/
updateMagneticField: function() {
var rSquared = this.position.distanceSquared( this.magnetModel.positionProperty.get() ) / (this.A * this.A); // normalized squared distance from coil to magnet

var sign = this.magnetModel.flippedProperty.value ? -1 : 1;

var rSquared = this.position.distanceSquared( this.magnetModel.positionProperty.get() ) / (NEAR_FIELD_RADIUS * NEAR_FIELD_RADIUS); // normalized squared distance from coil to magnet

// if magnet is very close to coil, then B field is at max value;
if ( rSquared < 1 ) {
this.magneticFieldProperty.set( this.sense * 2 );
this.magneticFieldProperty.set( sign * 2 );
}
else {

Expand All @@ -86,8 +90,8 @@ define( function( require ) {
// r - normalized distance between magnet and coil

// normalized x-displacement from coil to magnet
var dx = (this.magnetModel.positionProperty.get().x - this.position.x) / this.A;
this.magneticFieldProperty.set( this.sense * (3 * dx * dx - rSquared) / (rSquared * rSquared) );
var dx = (this.magnetModel.positionProperty.get().x - this.position.x) / NEAR_FIELD_RADIUS;
this.magneticFieldProperty.set( sign * (3 * dx * dx - rSquared) / (rSquared * rSquared) );
}
},

Expand All @@ -99,8 +103,11 @@ define( function( require ) {
step: function( dt ) {
this.updateMagneticField();

// number of turns in coil (equal to half the number of turns in the graphic image)
var numberOfCoils = this.numberOfSpirals / 2;

// emf = (nbr coils)*(change in B)/(change in t)
this.emfProperty.set( this.N * (this.magneticFieldProperty.get() - this.previousMagneticFieldProperty.get()) / dt );
this.emfProperty.set( numberOfCoils * (this.magneticFieldProperty.get() - this.previousMagneticFieldProperty.get()) / dt );
this.previousMagneticFieldProperty.set( this.magneticFieldProperty.get() );
}
} );
Expand Down

0 comments on commit 4d30c2c

Please sign in to comment.