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