search

src/sims/springs/Molecule3App.js

1// Copyright 2016 Erik Neumann. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the 'License');
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an 'AS IS' BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15goog.provide('myphysicslab.sims.springs.Molecule3App');
16
17goog.require('myphysicslab.lab.controls.SliderControl');
18goog.require('myphysicslab.lab.model.CollisionAdvance');
19goog.require('myphysicslab.lab.model.PointMass');
20goog.require('myphysicslab.lab.model.Spring');
21goog.require('myphysicslab.lab.util.DoubleRect');
22goog.require('myphysicslab.lab.util.ParameterNumber');
23goog.require('myphysicslab.lab.util.UtilityCore');
24goog.require('myphysicslab.lab.view.DisplayShape');
25goog.require('myphysicslab.lab.view.DisplaySpring');
26goog.require('myphysicslab.sims.layout.AbstractApp');
27goog.require('myphysicslab.sims.layout.CommonControls');
28goog.require('myphysicslab.sims.layout.TabLayout');
29goog.require('myphysicslab.sims.springs.Molecule3Sim');
30
31goog.scope(function() {
32
33var lab = myphysicslab.lab;
34var sims = myphysicslab.sims;
35
36var AbstractApp = sims.layout.AbstractApp;
37var CollisionAdvance = lab.model.CollisionAdvance;
38var CommonControls = sims.layout.CommonControls;
39var DisplayShape = lab.view.DisplayShape;
40var DisplaySpring = myphysicslab.lab.view.DisplaySpring;
41var DoubleRect = lab.util.DoubleRect;
42var Molecule3Sim = sims.springs.Molecule3Sim;
43var ParameterNumber = lab.util.ParameterNumber;
44var PointMass = lab.model.PointMass;
45var SliderControl = lab.controls.SliderControl;
46var Spring = myphysicslab.lab.model.Spring;
47var TabLayout = sims.layout.TabLayout;
48var UtilityCore = lab.util.UtilityCore;
49
50/** Molecule3App displays the simulation
51{@link myphysicslab.sims.springs.Molecule3Sim Molecule3Sim}.
52
53* @param {!TabLayout.elementIds} elem_ids specifies the names of the HTML
54* elementId's to look for in the HTML document; these elements are where the user
55* interface of the simulation is created.
56* @param {number} numAtoms number of atoms to make, from 2 to 6
57* @constructor
58* @final
59* @extends {AbstractApp}
60* @struct
61* @export
62*/
63myphysicslab.sims.springs.Molecule3App = function(elem_ids, numAtoms) {
64 UtilityCore.setErrorHandler();
65 /** @type {number} */
66 this.numAtoms = numAtoms;
67 var simRect = new DoubleRect(-6, -6, 6, 6);
68 var sim = new Molecule3Sim(this.numAtoms);
69 var advance = new CollisionAdvance(sim);
70 AbstractApp.call(this, elem_ids, simRect, sim, advance, /*eventHandler=*/sim,
71 /*energySystem=*/sim);
72 this.layout.simCanvas.setBackground('black');
73
74 this.protoSpring = new DisplaySpring().setWidth(0.3).setColorCompressed('#0c0')
75 .setColorExpanded('green').setThickness(3);
76 this.protoSpecialSpring = new DisplaySpring().setWidth(0.3).setColorCompressed('#c00')
77 .setColorExpanded('red').setThickness(3);
78
79 this.walls = new DisplayShape(this.simList.getPointMass('walls'))
80 .setFillStyle('').setStrokeStyle('gray');
81 this.displayList.add(this.walls);
82 goog.array.forEach(this.simList.toArray(), function(simObj) {
83 if (simObj instanceof Spring) {
84 var s = /** @type {!Spring} */(simObj);
85 var proto = s.getName().match(/^SPECIAL/) ? this.protoSpecialSpring :
86 this.protoSpring;
87 this.displayList.add(new DisplaySpring(s, proto));
88 }
89 }, this);
90 /** @type {!Array<!lab.view.DisplayShape>} */
91 this.atoms = [];
92 var cm = ['red', 'blue', 'magenta', 'orange', 'gray', 'green'];
93 for (var i=0; i<this.numAtoms; i++) {
94 var atom = new DisplayShape(this.simList.getPointMass('atom'+(i+1)))
95 .setFillStyle(cm[i]);
96 this.atoms.push(atom);
97 this.displayList.add(atom);
98 }
99
100 this.addPlaybackControls();
101 /** @type {!ParameterNumber} */
102 var pn;
103 pn = sim.getParameterNumber(Molecule3Sim.en.GRAVITY);
104 this.addControl(new SliderControl(pn, 0, 20, /*multiply=*/false));
105
106 pn = sim.getParameterNumber(Molecule3Sim.en.DAMPING);
107 this.addControl(new SliderControl(pn, 0, 1, /*multiply=*/false));
108
109 pn = sim.getParameterNumber(Molecule3Sim.en.ELASTICITY);
110 this.addControl(new SliderControl(pn, 0, 1, /*multiply=*/false));
111
112 pn = sim.getParameterNumber(Molecule3Sim.en.MASS);
113 this.addControl(new SliderControl(pn, 0.2, 20.2, /*multiply=*/true));
114
115 pn = sim.getParameterNumber(Molecule3Sim.en.MASS_SPECIAL);
116 this.addControl(new SliderControl(pn, 0.2, 20.2, /*multiply=*/true));
117
118 pn = sim.getParameterNumber(Molecule3Sim.en.LENGTH);
119 this.addControl(new SliderControl(pn, 0.1, 10.1, /*multiply=*/true));
120
121 pn = sim.getParameterNumber(Molecule3Sim.en.LENGTH_SPECIAL);
122 this.addControl(new SliderControl(pn, 0.1, 10.1, /*multiply=*/true));
123
124 pn = sim.getParameterNumber(Molecule3Sim.en.STIFFNESS);
125 this.addControl(new SliderControl(pn, 0, 100, /*multiply=*/false));
126
127 pn = sim.getParameterNumber(Molecule3Sim.en.STIFFNESS_SPECIAL);
128 this.addControl(new SliderControl(pn, 0, 100, /*multiply=*/false));
129
130 this.addStandardControls();
131
132 this.makeEasyScript();
133 this.addURLScriptButton();
134};
135var Molecule3App = myphysicslab.sims.springs.Molecule3App;
136goog.inherits(Molecule3App, AbstractApp);
137
138if (!UtilityCore.ADVANCED) {
139 /** @inheritDoc */
140 Molecule3App.prototype.toString = function() {
141 return this.toStringShort().slice(0, -1)
142 +', atoms: '+this.atoms.length
143 +', walls: '+this.walls.toStringShort()
144 + Molecule3App.superClass_.toString.call(this);
145 };
146};
147
148/** @inheritDoc */
149Molecule3App.prototype.getClassName = function() {
150 return 'Molecule3App';
151};
152
153/** @inheritDoc */
154Molecule3App.prototype.defineNames = function(myName) {
155 Molecule3App.superClass_.defineNames.call(this, myName);
156 this.terminal.addRegex('walls|atoms|protoSpecialSpring|protoSpring',
157 myName);
158};
159
160}); // goog.scope