home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / demos / xgas / doc.n < prev    next >
Encoding:
Text File  |  1991-04-17  |  10.9 KB  |  356 lines

  1. .na
  2. .fz 1 +1
  3. .po 1i
  4. .fo 'Ideal Gas Simulation''%'
  5.  
  6. .ce 3
  7. Ideal Gas Simulation
  8. Larry Medwin
  9. April 1991
  10. .\"   xgas: Copyright 1991 Larry Medwin: @(#)doc.n    1.4 91/04/15
  11. .sp 3
  12.  
  13. .sh 1 "The Physical Model"
  14.  
  15. This is physical simulation of the behavior of an Ideal Gas.
  16. The gas is represented by molecules in a chamber,
  17. which consists of two boxes separated by a wall with a hole.
  18. Gas molecules move around with velocities
  19. determined by their temperature:
  20. .(l
  21. Kinetic energy = Thermal energy, or
  22.    1  2
  23.    - m v  = K  T
  24.    2         b
  25. .)l
  26. Molecular trajectories are linear (inertial) until a collision
  27. with a wall.
  28. These collisions are inelastic;
  29. that is, energy is not conserved.
  30. For example,
  31. if the wall temperature is greater than
  32. the molecule temperature,
  33. the molecule emerges from the collision
  34. with increased velocity.
  35. In addition,
  36. the angle of reflection is only approximately
  37. equal to the angle of incidence;
  38. a random component
  39. added to the trajectories
  40. allows the molecules to approach equilibrium.
  41.  
  42. Since the gas is ideal,
  43. collisions between molecules are not considered.
  44.  
  45. .sh 1 "The Implementation"
  46.  
  47. As shown in Figure 1,
  48. the Ideal Gas Demo consists of the chamber
  49. consisting of two boxes,
  50. which contain the molecules,
  51. two temperature controls,
  52. and the temperature readings under the
  53. two separately heated portions of the chamber.
  54. There is a wall with a hole separating
  55. the two boxes of the chamber.
  56.  
  57. .sh 2 "Widget Hierarchy"
  58.  
  59. The HP Widget set was used in the original version;
  60. a port to the Athena Widget set was implemented
  61. by Dave Sternlicht, of the X Consortium.
  62. The widget hierarchy is described in Figure 1.
  63.  
  64. .(z
  65. .hl
  66. .sp
  67. toplevel (applicationShell)
  68.     frame (form)
  69.     quit (command)
  70.     run (toggle)
  71.     pause (toggle)
  72.     step (command)
  73.     help (command)
  74.         helpShell (toplevelShell)
  75.         helpFrame (form)
  76.             helpQuit (command)
  77.             helpQuick (command)
  78.             helpMan (command)
  79.             helpDoc (command)
  80.         helpText (asciiText)
  81.     chamber[].control (scrollbar)
  82.     chamber[].display (label)
  83.     clock (label)
  84.     lab (gas)
  85. .sp
  86. .ce
  87. Figure 1. Widget Hierarchy
  88. .hl
  89. .)z
  90.  
  91. The widget hierarchy includes two shell widgets accessible
  92. to the window manager.
  93. One is the toplevel widget, the parent all widgets.
  94. It contains a form widget
  95. which, in turn, contains
  96. all the controls, displays, and the simulation area.
  97. The top edge of the frame has a row of command buttons.
  98. Two of these buttons,
  99. run and pause,
  100. form a radio group,
  101. to indicate whether the simulation is
  102. in run mode or pause mode.
  103. There are two scrollbars,
  104. one on each side of the lab area.
  105. The scrollbar is used to set the temperature,
  106. which is displayed in the label below it.
  107. Also below the lab is the clock display,
  108. which reports the simulated time.
  109.  
  110. The other shell is a help popup.
  111. It contains a text widget which can scroll through
  112. the man page,
  113. quick-help (a list of the mouse button actions),
  114. and this document.
  115.  
  116. Two Graphics Contexts (GC) are used in the gas widget.
  117. One describes
  118. the walls of the chamber,
  119. and the other describes
  120. the molecules, which are filled rectangles.
  121. Since the molecules move,
  122. and must be erased before shown in their new position,
  123. they are drawn using the XOR function.
  124. This allows them to be erased by drawing them twice
  125. in the same position.
  126.  
  127. A trick is used to speed up the drawing of the molecules.
  128. The plural XFillRectangles call is passed
  129. an XRectangles array which contains two entries for each molecule.
  130. These entries contain the old position and the new position.
  131. On alternate timesteps,
  132. either the odd or even elements of this array are updated
  133. with the new molecule positions.
  134. The result is that the old positions are redrawn and erased,
  135. while the new positions are drawn for the first time
  136. and remain visible.
  137. This reduces the annoying "flicker" that results from
  138. two separate calls to XDrawRectangles
  139. using a single array,
  140. in which the no molecules are visible,
  141. while the XRectangle array is being updated.
  142. Care must be taken to properly update this array
  143. in the addMolecule and expose event callbacks.
  144.  
  145. A bitmap was created with bitmap(1),
  146. and compiled into the program to be
  147. used as an icon.
  148.  
  149. .sh 2 "The Data Structures"
  150.  
  151. There are two main data structures:
  152. the molecules
  153. and the chamber (boxes).
  154. These are defined
  155. in the "gas.h" header file.
  156.  
  157. An array of molecule data structures
  158. contains the equation of motion of the molecule,
  159. the time of its next collision,
  160. and its kinetic energy (expressed as a temperature).
  161.  
  162. The chamber is an array of two boxes.
  163. Each box consists of an array of 6 walls,
  164. one of which is an opening to the other box.
  165. The temperature is associated with each box;
  166. this temperature is updated by scrollbar callbacks.
  167.  
  168. Each wall and corner in the box has an enumerated "type."
  169. This type,
  170. TOP, BOTTOM, LEFT, or RIGHT,
  171. NW, SW, SE, NE,
  172. is used as an index into a WallParam array.
  173. The WallParam array contains the coefficients of
  174. the reflection and rotation matrices used in the
  175. computation of collisions with the walls.
  176.  
  177. .sh 2 "Physics and Algorithms"
  178.  
  179. Calculations of molecule positions are repeated
  180. every timestep.
  181. The simulation begins by calculating
  182. the new positions of each molecules.
  183. After each time step,
  184. the value of the variable "labData.time" is incremented
  185. by the value "labData.timestepSize."
  186.  
  187. At each timestep,
  188. the "dynamics" routine is called
  189. for each molecule.
  190. The time of next collision of a given molecule
  191. is contained in molecule[i].collisionTime.
  192. This is compared with the current time (labData.time)
  193. to determine if a molecule
  194. will collide with a wall during this timestep.
  195. In this case,
  196. the "collide" routine is called;
  197. otherwise the "inertia" routine is called.
  198. Note that knowing the time of the next collision
  199. allows the timestep routines to ignore the walls
  200. until the collision actually occurs.
  201.  
  202. The inertia routine just solves for
  203. the molecule's location using the equations of motion
  204. associated with the molecule.
  205.  
  206. The collision is much more complicated.
  207. First,
  208. the trajectory is reflected by the wall or corner.
  209. A molecule that hits corner has both its x and y velocity components
  210. multiplied by -1.
  211. A molecule that hits a wall has either its x or y velocity component
  212. multiplied by -1, depending on which way it would bounce
  213. in a completely elastic collision (billiard balls).
  214.  
  215. The reflection angle deviates from an exact reflection
  216. by a random component,
  217. which is determined by the X resource "randomBounce."
  218. At the same time,
  219. the temperature of the molecule approaches the wall temperature
  220. at a rate determined by the X resource "equilibrium."
  221. Now that the new "temperature" and angle of trajectory
  222. have been determined,
  223. the new equations of motion are calculated.
  224. Using these equations of motion,
  225. the next wall collision is found,
  226. including the possibility of moving to the other chamber.
  227. This process is repeated
  228. until the next collision time is after the end of this timestep,
  229. Then the inertia routine is called to compute
  230. the position of the molecule at the end of the timestep.
  231.  
  232. .sh 2 "Run/Pause/Step control"
  233.  
  234. The main computations are performed at regular intervals
  235. (every "delay" milliseconds, using the X resources)
  236. via the timeout mechanism.
  237. The intrinsics call XtAddTimeOut causes a callback to be called
  238. after a given delay.
  239. In "run" mode,
  240. XtAddTimeOut is automatically called at the end of each timestep.
  241. Switching to "pause" mode removes this callback
  242. using XtRemoveTimeOut.
  243. In "pause" mode,
  244. the "step" button will perform a timestep calculation
  245. without calling XtAddTimeOut.
  246.  
  247. .sh 2 "Performance and accuracy"
  248.  
  249. 100 molecules were simulated with a timestep of 3 microseconds.
  250. The computation of 2.0 simulated milliseconds required about 75 CPU seconds.
  251.  
  252. The physical conditions of this simulation represent very high vacuum
  253. (very low density of molecules per cm squared -- this is a two
  254. dimensional simulation).
  255. The approach to equilibrium occurs only through wall collisions.
  256. Because of the line of sight trajectories,
  257. and the large aperture between the chambers,
  258. equilibrium is never reached.
  259. However, the approach towards equilibrium
  260. is evident from the migration of molecules
  261. from the warm to the cold chamber.
  262.  
  263. .sh 2 "Bugs, enhancements, and Numerical Considerations"
  264.  
  265. Doubles are used for the coefficients of
  266. the equations of motion and the collision times.
  267. Originally floats were used for these numbers,
  268. but the equations of motion are numerically unstable,
  269. and would cause errors after some 10,000 to 15,000 timesteps.
  270. Using doubles allows some 375,000 timesteps to be computed
  271. before this instability causes an error.
  272.  
  273. A better solution was to compute the trajectories using
  274. integer arithmetic.
  275. The endpoints (collision positions) of each trajectory
  276. are integer locations on the walls.
  277. The actual coefficients of the trajectory
  278. and the time until the next collision
  279. are floating point numbers, but they are recomputed
  280. from new integer endpoints at each collision.
  281. Using this new scheme, xgas has simulated 100 molecules
  282. for more than a million timesteps.
  283.  
  284. .sh 1 "Appendix"
  285.  
  286. Figure 2
  287. is a list of X resources which were created especially
  288. for controlling some of the physical aspects of the simulation,
  289. and a typical value.
  290. Figure 3
  291. is a schematic of the chamber which identifies the
  292. array indices of the points and lines making up the walls.
  293.  
  294. .bp
  295. .(l
  296. .hl
  297. #
  298. #   timestepSize in microseconds
  299. XGas*timestepSize:               3.0
  300. #
  301. # delay in milliseconds
  302. #   Real time between timestep computations.
  303. #   This doesn't overload my XR4 server running on a Sun 3/110.
  304. XGas*delay:                      30
  305. #
  306. # randomBounce
  307. #   0.0:    Angle of reflection equals angle of incidence.
  308. #   1.0:    The two angles are unrelated.
  309. XGas*randomBounce:               0.2
  310. #
  311. #   0.0:    No kinetic energy is exchanged with the wall
  312. #             during a collision
  313. #   1.0:    The molecule emerges with a kinetic energy
  314. #             corresponding to the wall temperature.
  315. XGas*equilibrium:                0.9
  316. #
  317. #   maxMolecules
  318. #           maximum number of molecules that can be created
  319. #             with the mouse
  320. XGas*maxMolecules:               100
  321. #
  322. .sp
  323. .hl
  324. .sp
  325. .ce
  326. Figure 2. Controlling Simulation Parameters with X Resources
  327. .)l
  328. .bp
  329. .(l
  330. .hl
  331.      p[0]                 p[2]                  p[4]
  332.        +--------------------+--------------------+
  333.        |        w2          |        w2          |
  334.        |                    |                    |
  335.        |                  w1|w1                  |
  336.        |                    |                    |
  337.        |                    |                    |
  338.        |                    +p[6]                |
  339.        |                                         |
  340.        |w3                w0 w0                w3|
  341.        |                                         |
  342.        |                    +p[7]                |
  343.        |                    |                    |
  344.        |                    |                    |
  345.        |                  w5|w5                  |
  346.        |                    |                    |
  347.        |        w4          |         w4         |
  348.        +--------------------+--------------------+
  349.      p[1]                 p[3]                  p[5]
  350. .sp 2
  351. .hl
  352. .ce
  353. .sp
  354. Figure 3. Assignment of walls and points
  355. .)l
  356.