8533ec6ab4a497e267cf8e88a594d6bc32e0b468
[jalviewjs.git] / site / j2s / org / uwi / Boltzmann.js
1 Clazz.declarePackage ("org.uwi");
2 Clazz.load (["java.awt.event.ActionListener", "$.MouseAdapter", "javax.swing.JApplet", "$.JTextArea", "java.awt.Color", "JU.SB", "javax.swing.BorderFactory", "$.JButton", "$.JLabel", "$.JPanel", "$.JScrollPane", "$.JTextField", "org.uwi.BoltzCanvas", "$.EntropyCanvas"], "org.uwi.Boltzmann", ["java.awt.Dimension", "$.FlowLayout", "$.GridBagConstraints", "$.GridBagLayout", "$.Insets", "java.text.DecimalFormat", "org.uwi.SimThread"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.BoltzSimGraph = null;
5 this.DispBoltz = null;
6 this.EntropyGraph = null;
7 this.DispEntropy = null;
8 this.lineBorder1 = null;
9 this.lineBorder2 = null;
10 this.UserInput = null;
11 this.lQuanta = null;
12 this.lParticles = null;
13 this.lCollisions = null;
14 this.tEnergy = null;
15 this.tParticles = null;
16 this.tCollisions = null;
17 this.bStartSim = null;
18 this.DispResults = null;
19 this.ShowText = null;
20 this.initialEnergy = 0;
21 this.curMaxEnergy = 0;
22 this.maxParticles = 0;
23 this.maxCollisions = 0;
24 this.particleEnergy = null;
25 this.EntropyCalcs = 0;
26 this.start_pressed = false;
27 if (!Clazz.isClassDefined ("org.uwi.Boltzmann.SymAction")) {
28 org.uwi.Boltzmann.$Boltzmann$SymAction$ ();
29 }
30 if (!Clazz.isClassDefined ("org.uwi.Boltzmann.LevelInfoArea")) {
31 org.uwi.Boltzmann.$Boltzmann$LevelInfoArea$ ();
32 }
33 this.simThread = null;
34 if (!Clazz.isClassDefined ("org.uwi.Boltzmann.SymMouse")) {
35 org.uwi.Boltzmann.$Boltzmann$SymMouse$ ();
36 }
37 this.particle1 = 0;
38 this.particle2 = 0;
39 this.e1 = 0;
40 this.e2 = 0;
41 this.collisionEnergy = 0;
42 this.numOfCollisions = 0;
43 this.displayFactor = 0;
44 this.entropyFactor = 0;
45 Clazz.instantialize (this, arguments);
46 }, org.uwi, "Boltzmann", javax.swing.JApplet);
47 Clazz.prepareFields (c$, function () {
48 this.BoltzSimGraph =  new javax.swing.JPanel ();
49 this.DispBoltz =  new org.uwi.BoltzCanvas (this);
50 this.EntropyGraph =  new javax.swing.JPanel ();
51 this.DispEntropy =  new org.uwi.EntropyCanvas ();
52 this.lineBorder1 = javax.swing.BorderFactory.createLineBorder (java.awt.Color.black);
53 this.lineBorder2 = javax.swing.BorderFactory.createLineBorder (java.awt.Color.black);
54 this.UserInput =  new javax.swing.JPanel ();
55 this.lQuanta =  new javax.swing.JLabel ();
56 this.lParticles =  new javax.swing.JLabel ();
57 this.lCollisions =  new javax.swing.JLabel ();
58 this.tEnergy =  new javax.swing.JTextField ();
59 this.tParticles =  new javax.swing.JTextField ();
60 this.tCollisions =  new javax.swing.JTextField ();
61 this.bStartSim =  new javax.swing.JButton ();
62 this.DispResults =  new javax.swing.JScrollPane ();
63 this.ShowText = Clazz.innerTypeInstance (org.uwi.Boltzmann.LevelInfoArea, this, null);
64 });
65 Clazz.makeConstructor (c$, 
66 function () {
67 Clazz.superConstructor (this, org.uwi.Boltzmann, []);
68 this.setName ("Boltzmann");
69 });
70 Clazz.overrideMethod (c$, "init", 
71 function () {
72 this.getContentPane ().setLayout (null);
73 this.setSize (562, 391);
74 this.BoltzSimGraph.setBorder (this.lineBorder2);
75 this.BoltzSimGraph.setLayout ( new java.awt.FlowLayout (1, 5, 5));
76 this.getContentPane ().add (this.BoltzSimGraph);
77 this.BoltzSimGraph.setBackground (java.awt.Color.white);
78 this.BoltzSimGraph.setBounds (0, 0, 384, 300);
79 this.BoltzSimGraph.add (this.DispBoltz);
80 this.DispBoltz.setPreferredSize ( new java.awt.Dimension (380, 294));
81 this.DispBoltz.setBounds (2, 6, 380, 294);
82 this.EntropyGraph.setBorder (this.lineBorder1);
83 this.EntropyGraph.setLayout ( new java.awt.FlowLayout (1, 5, 5));
84 this.getContentPane ().add (this.EntropyGraph);
85 this.EntropyGraph.setBackground (java.awt.Color.white);
86 this.EntropyGraph.setBounds (384, 0, 180, 156);
87 this.EntropyGraph.add (this.DispEntropy);
88 this.DispEntropy.setPreferredSize ( new java.awt.Dimension (178, 150));
89 this.DispEntropy.setBounds (1, 6, 178, 150);
90 this.UserInput.setLayout ( new java.awt.GridBagLayout ());
91 this.getContentPane ().add (this.UserInput);
92 this.UserInput.setBackground (java.awt.Color.yellow);
93 this.UserInput.setBounds (0, 300, 384, 96);
94 this.lQuanta.setText ("Initial Energy");
95 this.UserInput.add (this.lQuanta,  new java.awt.GridBagConstraints (0, 0, 1, 1, 1.0, 1.0, 10, 0,  new java.awt.Insets (0, 0, 0, 0), 0, 0));
96 this.lQuanta.setBounds (15, 6, 97, 15);
97 this.lParticles.setText ("No. of Particles");
98 this.UserInput.add (this.lParticles,  new java.awt.GridBagConstraints (1, 0, 1, 1, 1.0, 0.0, 10, 0,  new java.awt.Insets (0, 0, 0, 0), 0, 0));
99 this.lParticles.setBounds (149, 6, 86, 15);
100 this.lCollisions.setText ("No. of Collisions");
101 this.UserInput.add (this.lCollisions,  new java.awt.GridBagConstraints (2, 0, 1, 1, 1.0, 0.0, 10, 0,  new java.awt.Insets (0, 0, 0, 0), 0, 0));
102 this.lCollisions.setBounds (275, 6, 90, 15);
103 this.tEnergy.setAutoscrolls (false);
104 this.tEnergy.setColumns (10);
105 this.tEnergy.setNextFocusableComponent (this.tParticles);
106 this.UserInput.add (this.tEnergy,  new java.awt.GridBagConstraints (0, 1, 1, 1, 1.0, 1.0, 10, 0,  new java.awt.Insets (0, 0, 0, 0), 0, 0));
107 this.tEnergy.setBounds (9, 33, 110, 19);
108 this.tParticles.setAutoscrolls (false);
109 this.tParticles.setColumns (10);
110 this.tParticles.setNextFocusableComponent (this.tCollisions);
111 this.UserInput.add (this.tParticles,  new java.awt.GridBagConstraints (1, 1, 1, 1, 1.0, 1.0, 10, 0,  new java.awt.Insets (0, 0, 0, 0), 0, 0));
112 this.tParticles.setBounds (137, 33, 110, 19);
113 this.tCollisions.setAutoscrolls (false);
114 this.tCollisions.setColumns (10);
115 this.tCollisions.setNextFocusableComponent (this.bStartSim);
116 this.UserInput.add (this.tCollisions,  new java.awt.GridBagConstraints (2, 1, 1, 1, 1.0, 1.0, 10, 0,  new java.awt.Insets (0, 0, 0, 0), 0, 0));
117 this.tCollisions.setBounds (265, 33, 110, 19);
118 this.bStartSim.setText ("Start");
119 this.bStartSim.setActionCommand ("Start");
120 this.bStartSim.setNextFocusableComponent (this.tEnergy);
121 this.UserInput.add (this.bStartSim,  new java.awt.GridBagConstraints (1, 2, 1, 1, 1.0, 1.0, 10, 0,  new java.awt.Insets (0, 0, 0, 0), 0, 0));
122 this.bStartSim.setBackground (java.awt.Color.green);
123 this.bStartSim.setBounds (160, 64, 63, 25);
124 this.DispResults.setHorizontalScrollBarPolicy (31);
125 this.DispResults.setVerticalScrollBarPolicy (22);
126 this.DispResults.setOpaque (true);
127 this.getContentPane ().add (this.DispResults);
128 this.DispResults.setBounds (384, 156, 180, 240);
129 this.ShowText.setRows (10000);
130 this.ShowText.setDisabledTextColor ( new java.awt.Color (153, 153, 153));
131 this.DispResults.getViewport ().add (this.ShowText);
132 this.ShowText.setBounds (0, 0, 162, 150000);
133 var lSymAction = Clazz.innerTypeInstance (org.uwi.Boltzmann.SymAction, this, null);
134 this.bStartSim.addActionListener (lSymAction);
135 this.tEnergy.addActionListener (lSymAction);
136 this.tParticles.addActionListener (lSymAction);
137 this.tCollisions.addActionListener (lSymAction);
138 var aSymMouse = Clazz.innerTypeInstance (org.uwi.Boltzmann.SymMouse, this, null);
139 this.ShowText.addMouseListener (aSymMouse);
140 this.setBackground (java.awt.Color.white);
141 this.maxParticles = 10000;
142 this.maxCollisions = 60000;
143 this.initialEnergy = 250;
144 this.EntropyCalcs = 50;
145 this.tEnergy.setText (Integer.toString (this.initialEnergy));
146 this.tParticles.setText (Integer.toString (this.maxParticles));
147 this.tCollisions.setText (Integer.toString (this.maxCollisions));
148 this.initEnvironment ();
149 });
150 Clazz.defineMethod (c$, "bStartSim_actionPerformed", 
151 function (event) {
152 if (!this.initEnvironment ()) return;
153 if (this.simThread != null) this.simThread.interrupt ();
154 this.simThread =  new org.uwi.SimThread (this);
155 this.simThread.start ();
156 }, "java.awt.event.ActionEvent");
157 Clazz.defineMethod (c$, "initEnvironment", 
158 function () {
159 try {
160 var tmp = (Integer.parseInt (this.tCollisions.getText ().trim ()));
161 if (tmp >= 200) this.maxCollisions = tmp;
162 tmp = (Integer.parseInt (this.tEnergy.getText ().trim ()));
163 if (tmp > 0) this.initialEnergy = tmp;
164 tmp = (Integer.parseInt (this.tParticles.getText ().trim ()));
165 if (tmp >= 50) this.maxParticles = tmp;
166 } catch (e) {
167 if (Clazz.exceptionOf (e, NumberFormatException)) {
168 return false;
169 } else {
170 throw e;
171 }
172 }
173 this.DispBoltz.maxEnergy = 20 * this.initialEnergy;
174 this.particleEnergy =  Clazz.newIntArray (this.maxParticles, 0);
175 this.DispBoltz.energyLevels =  Clazz.newIntArray (this.DispBoltz.maxEnergy + 1, 0);
176 this.curMaxEnergy = this.initialEnergy;
177 this.DispEntropy.Entropy =  Clazz.newDoubleArray (this.EntropyCalcs + 1, 0);
178 this.DispEntropy.EntropyCalc = this.EntropyCalcs;
179 this.DispEntropy.entCounter = 0;
180 for (var i = 0; i < this.maxParticles; i++) {
181 this.particleEnergy[i] = this.initialEnergy;
182 }
183 for (var i = 0; i <= this.DispBoltz.maxEnergy; i++) {
184 this.DispBoltz.energyLevels[i] = 0;
185 }
186 for (var i = 0; i < this.EntropyCalcs; i++) {
187 this.DispEntropy.Entropy[i] = 0;
188 }
189 this.DispBoltz.energyLevels[this.initialEnergy] = this.maxParticles;
190 return true;
191 });
192 Clazz.defineMethod (c$, "calcEntropy", 
193 function (x) {
194 var i;
195 this.DispEntropy.Entropy[x] = this.maxParticles * Math.log (this.maxParticles);
196 for (i = 0; i <= this.curMaxEnergy; i++) if (this.DispBoltz.energyLevels[i] > 0) this.DispEntropy.Entropy[x] = this.DispEntropy.Entropy[x] - (this.DispBoltz.energyLevels[i] * Math.log (this.DispBoltz.energyLevels[i]));
197
198 }, "~N");
199 Clazz.defineMethod (c$, "ShowText_mouseReleased", 
200 function (event) {
201 }, "java.awt.event.MouseEvent");
202 Clazz.defineMethod (c$, "sjs_initSimulation", 
203 function () {
204 this.numOfCollisions = this.maxCollisions;
205 this.entropyFactor = Clazz.doubleToInt (Math.ceil (Clazz.doubleToInt (this.maxCollisions / this.EntropyCalcs)));
206 {
207 this.displayFactor = 100;
208 }});
209 Clazz.defineMethod (c$, "sjs_loopSimulation", 
210 function () {
211 this.particle1 = Clazz.doubleToInt ((this.maxParticles - 1) * Math.random ());
212 this.particle2 = Clazz.doubleToInt ((this.maxParticles - 1) * Math.random ());
213 while (this.particle1 == this.particle2) {
214 this.particle2 = Clazz.doubleToInt ((this.maxParticles - 1) * Math.random ());
215 }
216 if (this.particleEnergy[this.particle1] < this.particleEnergy[this.particle2]) {
217 var temp = this.particleEnergy[this.particle1];
218 this.particleEnergy[this.particle1] = this.particleEnergy[this.particle2];
219 this.particleEnergy[this.particle2] = temp;
220 }this.e1 = this.particleEnergy[this.particle1];
221 this.e2 = this.particleEnergy[this.particle2];
222 this.collisionEnergy = Clazz.doubleToInt (Math.ceil (this.e1 * Math.random ()));
223 this.particleEnergy[this.particle1] = this.e1 - this.collisionEnergy;
224 this.particleEnergy[this.particle2] = this.e2 + this.collisionEnergy;
225 this.DispBoltz.energyLevels[this.e1] = this.DispBoltz.energyLevels[this.e1] - 1;
226 this.DispBoltz.energyLevels[this.e2] = this.DispBoltz.energyLevels[this.e2] - 1;
227 this.e1 = this.particleEnergy[this.particle1];
228 this.e2 = this.particleEnergy[this.particle2];
229 this.DispBoltz.energyLevels[this.e1] = this.DispBoltz.energyLevels[this.e1] + 1;
230 this.DispBoltz.energyLevels[this.e2] = this.DispBoltz.energyLevels[this.e2] + 1;
231 if (this.e1 < this.e2) this.e1 = this.e2;
232 if (this.e1 > this.curMaxEnergy) this.curMaxEnergy = this.e1;
233 this.DispBoltz.maxEnergy = this.curMaxEnergy;
234 return (--this.numOfCollisions > 0);
235 });
236 Clazz.defineMethod (c$, "sjs_finalizeGraph", 
237 function () {
238 for (var i = this.curMaxEnergy; i > 0; i--) {
239 if (this.DispBoltz.energyLevels[i] < 1) this.curMaxEnergy--;
240  else break;
241 }
242 this.showTheText ();
243 });
244 Clazz.defineMethod (c$, "showTheText", 
245  function () {
246 this.ShowText.levelInfo.setLength (0);
247 this.ShowText.appendLine ("Init. Energy   = " + this.initialEnergy);
248 this.ShowText.appendLine ("No. particles  = " + this.maxParticles);
249 this.ShowText.appendLine ("No. collisions = " + this.maxCollisions);
250 this.ShowText.appendLine ("-------------");
251 for (var i = 1; i <= this.curMaxEnergy; i++) this.ShowText.appendLine ("EL " + i + "= " + this.DispBoltz.energyLevels[i]);
252
253 this.ShowText.appendLine ("-------------");
254 var df =  new java.text.DecimalFormat ("0.00");
255 for (var i = 0; i < this.EntropyCalcs; i++) this.ShowText.appendLine ("WL " + i + "= " + df.format (this.DispEntropy.Entropy[i]));
256
257 this.ShowText.setRows (this.curMaxEnergy + this.EntropyCalcs + 2);
258 this.ShowText.setText (this.ShowText.levelInfo.toString ());
259 this.repaint ();
260 });
261 Clazz.defineMethod (c$, "sjs_checkRepaint", 
262 function () {
263 if ((this.numOfCollisions % this.entropyFactor) == 0) {
264 this.calcEntropy (this.DispEntropy.entCounter++);
265 this.DispEntropy.invalidate ();
266 }if ((this.numOfCollisions % this.displayFactor) != 1) return false;
267 this.repaint ();
268 return true;
269 });
270 c$.$Boltzmann$SymAction$ = function () {
271 Clazz.pu$h(self.c$);
272 c$ = Clazz.decorateAsClass (function () {
273 Clazz.prepareCallback (this, arguments);
274 Clazz.instantialize (this, arguments);
275 }, org.uwi.Boltzmann, "SymAction", null, java.awt.event.ActionListener);
276 Clazz.overrideMethod (c$, "actionPerformed", 
277 function (a) {
278 var b = a.getSource ();
279 if (b === this.b$["org.uwi.Boltzmann"].bStartSim) this.b$["org.uwi.Boltzmann"].bStartSim_actionPerformed (a);
280 this.b$["org.uwi.Boltzmann"].ShowText.levelInfo.setLength (0);
281 }, "java.awt.event.ActionEvent");
282 c$ = Clazz.p0p ();
283 };
284 c$.$Boltzmann$LevelInfoArea$ = function () {
285 Clazz.pu$h(self.c$);
286 c$ = Clazz.decorateAsClass (function () {
287 Clazz.prepareCallback (this, arguments);
288 this.levelInfo = null;
289 Clazz.instantialize (this, arguments);
290 }, org.uwi.Boltzmann, "LevelInfoArea", javax.swing.JTextArea);
291 Clazz.prepareFields (c$, function () {
292 this.levelInfo =  new JU.SB ();
293 });
294 Clazz.makeConstructor (c$, 
295 function () {
296 Clazz.superConstructor (this, org.uwi.Boltzmann.LevelInfoArea);
297 this.levelInfo.setLength (0);
298 });
299 Clazz.defineMethod (c$, "appendLine", 
300 function (a) {
301 this.levelInfo.append (a).append ("\n");
302 }, "~S");
303 c$ = Clazz.p0p ();
304 };
305 c$.$Boltzmann$SymMouse$ = function () {
306 Clazz.pu$h(self.c$);
307 c$ = Clazz.decorateAsClass (function () {
308 Clazz.prepareCallback (this, arguments);
309 Clazz.instantialize (this, arguments);
310 }, org.uwi.Boltzmann, "SymMouse", java.awt.event.MouseAdapter);
311 Clazz.overrideMethod (c$, "mouseReleased", 
312 function (a) {
313 var b = a.getSource ();
314 if (b === this.b$["org.uwi.Boltzmann"].ShowText) this.b$["org.uwi.Boltzmann"].ShowText_mouseReleased (a);
315 }, "java.awt.event.MouseEvent");
316 c$ = Clazz.p0p ();
317 };
318 });