If you’re interested, Virtual Chemistry can now be played with or downloaded here.
A little while ago, I was, once again, idly messing around in NetLogo, having fun with the layout-spring method, which basically treats agents like the nodes of a network of springs, and the links between them as the springs themselves, then computes the dynamics and changes the positions of hte nodes accordingly. Eventually, inspiration struck, and I wondered if it would be possible to build a simulation of chemical bonding. I spent about an hour writing and revising the code, and thus, Virtual Chemistry was born.
It works like this: at the beginning of each simulation run, a random number is generated. This is the number of unique elements that will appear. Arrays are created that store the properties of these elements, namely: complements (which tells the element which other elements it can form bonds with), and maxbonds (which, obviously, tells the element the maximum number of bonds that it can form). Every step, an atom checks for other atoms within the radius “interact-radius” (which the user can set using a slider). If any of the nearby atoms are in the atom’s complement list, there is a ten percent chance that it will form a bond, as long as it hasn’t reached the maximum number of bonds. Atoms also wiggle around randomly to simulate temperature effects.
It took me a while to get this to work and to get rid of a few irritating bugs, but eventually, I got it working, and, not to be immodest, but I was impressed with the kinds of behavior it could produce.
As I watched the simulations unfold, I observed behavior that I hadn’t even considered when I was building the simulation. For example, when the simulated space was packed to a sufficient density with atoms, it began to experience real pressure effects. The way the physics works, bonds hold atoms together, but atoms repel each other. At the start, with the atoms at a low density (and a low enough temperature), certain “molecules” formed. As I added atoms (effectively increasing the pressure), some structures that had been unstable became stable, and some of the structures that had been stable destabilized. I was fairly happy, because I realized that what I was seeing were actual phase changes in my virtual material. Similar phase changes occurred when I changed the temperature, too.
One experimental run in particular is very illustrative. I call it Triad World. In this world, there is only one element (“Element 0”), and each atom of Element 0 can have at most three bonds. Even in a simple world like this, I observed some interesting things.
Here, you can see the atoms, and the three-atom triangles that give Triad World its name. As you can see, these dominate, but there are also other “molecules” which occur quite frequently. The scene above arose at a low temperature and a fairly low pressure.
After adding a few atoms (and thus increasing the pressure), new stable structures emerged. Note the large chain near the center of the image. Only the restraining force exerted by the surrounding atoms prevents the heat from tearing this chain apart. The same force, though, also prevents the two-atom dimers from forming triad,s which they normally would have done.
There are a lot of potential applications for a program like this (more so, perhaps, than my zombie infection simulator), and it’s certainly a lot of fun to play with. The problem is, running the simulation with more than about two hundred atoms slows it down pretty badly. But in order to simulate anything as complicated as, say, a rudimentary biomolecule, thousands of atoms or more would be required. With that in mind, here’s my to-do list of improvements:
- Optimize the code, if possible, to make it run faster.
- Write a better bond-forming routine. I’m not happy with the one that’s currently implemented.
- Tune the attraction and repulsion strengths. Right now, I get the vague feeling that the weird interatomic forces that exist in the simulation are preventing some interesting structures from forming. I know for a fact that they’re preventing any but the most rudimentary chemical reactions.
- Implement some kind of energy system. Right now, the only energy comes in the form of the random motions induced by the heat. I’d like to make it so that forming bonds consumes energy and breaking bonds releases it.
That’s all for now. I’m hoping to have the complete program up on the NetLogo website soon, and when it’s up, I’ll publish the link.