# Class StringSim

Simulation of a string under tension which can have waves in 2D. This is an unusual simulation from others in myPhysicsLab in that it uses a partial differential equation (PDE) model.

## Algorithm

Based on Algorithm 12.4, 'Wave Equation Finite-Difference' from Numerical Analysis, 6th Ed. by Burden & Faires.

## Three Data Arrays

The three data arrays, `w1, w2, w3` represent the current, past, and next string state. Each entry in an array is the displacement of the string at that point.

The role of representing the current, past, or next state rotates among the three arrays, changing with step forward in time.

Suppose that `w1` = past, `w2` = current, and `w3` = next. We can get space derivatives by looking at neighboring points within `w2`. We can get time derivatives by looking at the difference between a point in `w1` and `w2`. The PDE for the string then gives us the change based on those derivatives, and we can figure out `w3`.

## Stability Condition

The stability condition is:

``````sqrt(tension / density) delta_t / delta_x < 1
``````

TO DO Provide a VarsList for graphing. We could provide variables corresponding to one or more discrete points on the string, giving the position (displacement), velocity, accel at that point.

## Constructors

• #### Parameters

• ##### shape: StringShape

starting wave shape

• ##### `Optional`opt_simList: SimList

SimList to use (optional)

## Properties

avgLen_: number = 10

number of samples for averaging stability

block_: PointMass = ...

moveable block, connected to left side of string

deltaT_: number = 0.0025

time step size

deltaX_: number = 0.1

spatial grid size

density_: number = 1

density of string per unit length

lastTime_: number = -100

last time we printed delta(t)

nowTime_: number = 0

current simulation time

potentialOffset_: number = 0

potential energy offset

sequence_: number = 0

sequence number indicates when the data array has changed.

stab_: number[] = ...

for averaging stability value

startPosition_: Vector = ...

starting position for the block

timeIdx_: number = 0

index into times_ array

times_: number[] = ...

for checking on delta(t) = avg time between updates

w1_: number[] = []

data array

w2_: number[] = []

data array

w3_: number[] = []

data array

wIdx_: number = 1

tells which array of w1, w2, w3 is most recent

w_: number[] = ...

current data array

## Methods

• Adds the given Observer to this Subject's list of Observers, so that the Observer will be notified of changes in this Subject. An Observer may call `Subject.addObserver` during its `observe` method.

#### Returns void

• Adds the Parameter to the list of this Subject's available Parameters.

#### Throws

if a Parameter with the same name already exists.

• Notifies all Observers that the Parameter with the given name has changed by calling observe on each Observer.

#### Parameters

• ##### name: string

the language-independent or English name of the Parameter that has changed

#### Throws

if there is no Parameter with the given name

• Called at the end of a mouse drag operation, performs whatever action is appropriate. Only called if startDrag returned `true`.

#### Parameters

• ##### _simObject: null | SimObject

the SimObject being dragged, or `null` if no SimObject was found

• ##### _location: Vector

the location of the mouse in simulation coordinates of the SimView where `simObject` was found, or in the focus view if `simObject` is `null`.

• ##### _offset: Vector

distance from the initial object position to the mouse location at start of drag.

#### Returns void

• Return density of string (mass per unit length)

#### Returns number

density of string

• Returns the length of the string.

#### Returns number

length of the string

• Returns number of calculation points on the string

#### Returns number

number of calculation points on the string

• Returns the Parameter with the given name.

#### Parameters

• ##### name: string

the language-independent or English name of the Parameter

#### Returns Parameter

the Parameter with the given name

#### Throws

if there is no Parameter with the given name

• Sets the given MutableVector to the position of a point on the string

#### Parameters

• ##### idx: number

index of point on the string, from 0 to getNumPoints

• ##### point: MutableVector

the MutableVector which will be set to the position

#### Returns void

• Returns a sequence number which changes when the data array changes.

#### Returns number

sequence number which indicates when data array changes

• Returns the stability condition number, which must be less than one for the simulation to be stable.

#### Returns number

the stability condition number

• Returns the current Simulation time.

#### Returns number

the current Simulation time.

#### Throws

if there is no time variable for the simulation

• Returns time step used when advancing the simulation

#### Returns number

time step used when advancing the simulation

• Called when a key is pressed or released, performs whatever action is appropriate for that event.

#### Parameters

• ##### _evt: KeyboardEvent

the KeyboardEvent that happened

• ##### _pressed: boolean

`true` means this is a key-down event; `false` means a key-up event

• ##### _modifiers: ModifierKeys

the modifier keys down during event

#### Returns void

• Called at each movement during a mouse drag, performs whatever action is appropriate. Only called if startDrag returned `true`. The SimObject being moved is passed in, along with the current mouse position, in simulation coordinates, and an offset calculated at the start of the drag.

Setting the SimObject position to `(x - offsetX, y - offsetY)` will move the SimObject smoothly along with the mouse movement.

#### Parameters

• ##### simObject: null | SimObject

the SimObject being dragged, or `null` if no SimObject was found

• ##### location: Vector

the location of the mouse in simulation coordinates of the SimView where `simObject` was found, or in the focus view if `simObject` is `null`.

• ##### offset: Vector

distance from the initial object position (from DisplayObject.getPosition) to the mouse location at start of drag.

#### Returns void

• Removes the Observer from this Subject's list of Observers. An Observer may call `removeObserver` during its `observe` method.

#### Parameters

• ##### observer: Observer

the Observer to detach from list of Observers

#### Returns void

• Removes the Parameter from the list of this Subject's available Parameters.

#### Parameters

• ##### parameter: Parameter

the Parameter to remove

#### Returns void

• Saves the current variables and time as the initial state, so that this initial state can be restored with reset. Broadcasts event named 'INITIAL_STATE_SAVED'.

#### Returns void

• Sets whether this Subject will broadcast events, typically used to temporarily disable broadcasting. Intended to be used in situations where a subclass overrides a method that broadcasts an event. This allows the subclass to prevent the superclass broadcasting that event, so that the subclass can broadcast the event when the method is completed.

#### Parameters

• ##### value: boolean

whether this Subject should broadcast events

#### Returns boolean

the previous value

• Set damping

damping

#### Returns void

• Set density of string (mass per unit length)

#### Parameters

• ##### value: number

density of string

#### Returns void

• Set number of calculation points on the string.

#### Parameters

• ##### value: number

number of calculation points on the string

#### Returns void

• Sets the potential energy offset.

#### Parameters

• ##### value: number

the potential energy offset

#### Returns void

• Set initial shape of string

#### Parameters

• ##### shape: StringShape

initial shape of string

• Set tension.

tension

#### Returns void

• Set time step used when advancing the simulation

#### Parameters

• ##### value: number

time step used when advancing the simulation

#### Returns void

• Called at the start of a mouse drag. The nearest dragable SimObject is passed in, along with mouse position in simulation coordinates. If no dragable SimObject was found, `null` is passed for the first argument. If the EventHandler does not recognize the SimObject then it should return `false`.

#### Parameters

• ##### simObject: null | SimObject

the SimObject that is nearest to the mouse drag coordinates, or `null` if no SimObject was found

• ##### _location: Vector

the location of the mouse in simulation coordinates of the SimView where `simObject` was found, or in the focus view if `simObject` is `null`.

• ##### _offset: Vector

distance from the initial object position (from DisplayObject.getPosition) to the mouse location at start of drag

• ##### _dragBody: null | Vector

location of 'drag point' on the SimObject in body coordinates of the SimObject; this is where for example a spring will be attached on the SimObject when dragging; or `null` when no SimObject was found

• ##### _modifiers: ModifierKeys

the modifier keys down during event

#### Returns boolean

`true` if the EventHandler will handle dragging the SimObject

• Returns a minimal string representation of this object, usually giving just identity information like the class name and name of the object.

For an object whose main purpose is to represent another Printable object, it is recommended to include the result of calling `toStringShort` on that other object. For example, calling `toStringShort()` on a DisplayShape might return something like this:

``````DisplayShape{polygon:Polygon{'chain3'}}
``````

#### Returns string

a minimal string representation of this object.

Generated using TypeDoc