Virtual Chemistry

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.

SimHeart 3.0

Some months ago, I wrote a series of posts about the behavior of my NetLogo heart simulation (namely, this one and this other one). SimHeart has been on the backburner since then, partly because I was making some effort (some) to pay attention to my classes, but mostly because I didn’t have any new ideas to implement. Then, a couple of weeks ago, I was messing around in NetLogo, trying to figure out how to get the cells of the grid to obey a switching-on rule similar to the kind of rules you find in neural networks. That, as it turned out, was a huge bust, but I re-used some of the code to build SimHeart 3.0.

This is a pretty radical revision, but, behaviorally, it’s pretty much the same as the old simulator. Still, there are some changes:

  • The cells, not the agents, do most of the work. Whether they fire or not is determined by a stochastic (random) sigmoid function. If a lot of a particular cell’s neighbors have high potential (the primary variable; actually, three varaibles, one for each system, but I’ll get to that later) and the cell’s potential is low enough, then the cell has a very high probability of switching its potential to 1. If not, the potential steadily drops.
  • The AV node is now fully simulated. I mentioned in my previous posts that I wanted to do this, and since the atria and the ventricles are simulated as two different kinds of potential, all I had to do to simulate the AV node was to introduce a third variant of the potential variable. Now, a heartbeat starts at the SA node (meaning a particular cell’s potential is set to 1), the wave travels until it reaches the entry cell for the AV node, triggers another wave (one that’s morphologically different, which I’ll talk about in the next bullet point) in the AV node, which travels to the exit cell, triggering the ventricular beat. This solution has the nice feature of taking care of the AV node’s natural delay for me, as well as realistically limiting the heart rate that the AV node can transmit from the atria to the ventricles. This is, by far, the most important addition.
  • The model now runs faster. Not much faster, but every bit counts, and given how much simpler the code is now, the model is now much more compact and elegant.
  • The model is more realistic. The stochastic potential-based cells are a lot more like how a real heart works than was the old model.
  • The model now takes into account the different sizes of the heart’s components. Because of the way the model works, the longer a cell’s potential takes to decay, the smaller of the particular component being simulated. Thus, the potential in the AV node takes a very long time to decay, making nodal arrhythmias essentially impossible (and making the AV node behave as though it’s very small); the potential in the atria takes a moderate amount of time to decay, making atrial arrhythmias much less common (and making the atria behave as though they’re larger than the AV node, but smaller than the ventricles. I think you see the pattern here.), and the potential in the ventricles decays quickly, making them fairly arrhythmia-prone.
  • The arrhythmias have changed. This isn’t necessarily a good thing, but it’s not crippling, either. The model now produces ventricular tachycardia much more readily than it did before, but the incidence of ventricular fibrillation has been reduced proportionately, and can be hard to differentiate between tachycardia. Unless the parameters are adjusted, atrial arrhythmais don’t occur at all. Still, the model can now incorporate junctional tachycardia (where a spiralling, self-sustaining wave in the AV node stimulates the ventricles to beat too quickly) can now be simulated easily.

And, not mentioned on the list above, the model is now prettier, too, since I was apparently experiencing some aesthetic inspiration when I was writing it. So, in SimHeart tradition (can I call it a tradition after three posts?), I present: the screenshots (note: ignore the weird screwed-up areas. They’re just explanatory text that apparently didn’t transfer into the screenshot. Don’t worry, you’re not missing anything, it’s all explained in the text below):

Periodically (with the period determined by the “s-rate” slider next to the view), the cell labeled “SA” sets its potential to 1, generating the atrial pulse (the red wave). If, for some reason (for example, if the atria are in fibrillation and there’s already a wave passing through the SA node), no wave will be generated.

When the pulse passing through the atria hits the cell labeled “AV1,” if there’s not a wave passing through it at the moment, it triggers a pulse in the AV node (the green wave).

If all goes well, the AV pulse triggers the cell labeled “AV2” which activates the ventricular pulse (the blue wave).

Of course, all does not always go well:

In this case, something has gone wrong with the electrical wave as it moves through the ventricles, causing a deadly spiral wave to form. This is simulated ventricular tachycardia, which will (probably) eventually decay into ventricular fibrillation. As you can see on the ECG, the atrial pulses are still trying to get through, but the AV node can’t activate the ventricles. Although it’s not pictured here, a quick press of the “Defibrillate” button took care of the arrhythmia.

Since the model’s now so much simpler, it’s easier to simulate diseases and treatments. Note the row of blue buttons on the right side of the images. There is, of course, the defibrillate button, which is remarkably effective at terminating arrhythmias. Below that are the “Increase Adrenalin” and “Increase Antiarrhythmics” buttons, the first of which makes the heart depolarize faster, and the second of which makes it depolarize more slowly. The first, as you might expect, makes the heart more arrhythmia-prone, and the second, obviously, makes it less arrhythmia-prone.

The set of buttons below that simulate various cardiac illnesses. “AV Block” makes the AV node’s cells depolarize very slowly, meaning that, when the sinus rate is set to a very high value, not all of the beats are able to get through. This, if I’m not mistaken, represents 2nd-degree AV block, type 1. The button below that, “Sinus Tachycardia”, sets the sinus rate to a very high value, simulating either a disease process or the effect of very strenuous exercise or other stress on the heart. “Long QT Syndrome” is a sort of bastardized version of the real disease, but has the same effect, making the ventricles dangerously more prone to arrhythmia.

All in all, I’m far happier with the new version than I was with either of the old versions. There are still some improvements to be made, however, and I’ll post updates as needed. Soon, I hope to have the model up on the NetLogo website, so that everybody can fiddle around with it.

Unexpected Consequences

One of the things that’s always fascinated me the most about simulated evolution is the way in which simulated organisms have a tendency to exploit any loophole or weakness in your code for an evolutionary advantage. Although I’ve designed and attempted to design a handful of evolutionary simulations, I’d never until today seen an example of this Darwinian cleverness.

A few days ago, I threw together a simple little evolution simulator in NetLogo. Each of the agents in the simulation had three genes: xcor (position along the x-axis), ycor (position along the y-axis), breednum (the number of daughter agents it spawns when it reproduces), and killpropensity (how likely the agent is to kill a random nearby agent). The simulation produced some interesting — although not terribly fascinating — behavior: those agents that produced the most offspring had a tendency to dominate. There was an interesting dynamic between the “killer” agents and the “peaceful” agents. The killers tended to form low-density groups (since if any of them were too close together, they’d usually kill each other), while the “pacifists” formed dense blooms. For a while, the killers would hold back the pacifists, but eventually, the pacifists would encroach and squeeze out the killers altogether. A typical run looked like this:

The agents inherit the color of their parents, so the coloration isn’t exactly by “species,” but it’s pretty close. As you can see, the green agents are fast-breeding pacifists, rapidly encroaching on the slower-breeding killers toward the center.

Then — and this is where the unexpected behavior and exploitation of loopholes I was talking about comes in — I introduced a new variable: mutationrate. It controls, obviously enough, how rapidly the agents mutate. Very quickly, every run started to look like this:

As you can see, this blue species has very rapidly come to dominate. You can’t see it, but this species has a rather high mutation rate. It took me a while to figure out why the fast-mutators were at such an enormous advantage. Then, I remembered that, in this simulation, the agents were competing for space, and in such a competition, the fittest organisms would be the ones that can maximize the space filled by their offspring. Since x-position and y-position were treated as genes, they were being mutated right along with the other variables, and since a rapidly-mutating position allowed the agents to jump farther from their parents and fill space more rapidly, fast mutation was an enormous advantage. It was such an enormous advantage that, even though the extremely large mutations the fast-mutators experienced prevented the evolution of any other behavior (because those genes tended to get so randomized that they effectively didn’t get passed on), they were still far more successful than any of the other species.

After I corrected for this ludicrous advantage (by setting it so that mutation rate couldn’t work on the position genes), this is what I got:

For a moment, I thought I’d solved the problem, until I inspected some of the agents and discovered that they had stopped mutating altogether. The sneaky intelligence of the genetic algorithm strikes again! I suppose that mutating would become something of a maladaptive behavior once the organism had optimized all of its other behaviors, since, after optimization was reached, any organism that mutated could only be at a disadvantage.

I realized that the only fix for this would be to force the mutation rate to stay above 2 (it’s a peculiarity of the random-number-generation code I cobbled together for this simulation that, at a mutation rate less than 2, no mutations occur). I thought that all I’d get would be the simulation I started with, but I was pleasantly surprised to discover that there was actually quite a diversity of mutation rates, and that none of these rates was at a particularly huge advantage over any of the others. This is what a run of the fixed simulator produced:

Those numbers you see hovering over every agent are the mutation rate. It appears that there’s not really an advantage to having a mutation rate above the usual 2, but it does seem that there’s not a disadvantage, either. So I can finally call this simulation fixed.

This experience reminded me that there’s a reason genetic algorithms are so popular in AI research, and that brings us to the moral of this little story: Darwinian evolution is a lot smarter than us. When writing evolutionary simulations, if there’s a loophole or a workaround or an exploit to be found in your code, then evolution will find it. Plan accordingly.

NOTE: Someone requested an image with the organisms color-coded by “kill propensity.” Since you asked nicely, and since I agree that that would be a good image to have up here, here you go. The organisms that are the darkest have the lowest probability of killing their neighbors, and the ones that are closer to white are very likely to kill:

As you can see, the situation is as I described in the body of the post: the killers have too great a tendency to limit their own growth, and are easily out-competed by their more peaceful counterparts.

SimHeart Update

The folks at the NetLogo website have been gracious enough to include SimHeart in their “community models” page, and the result is that there is now a place where you can run the program in your web browser (assuming you have a recent enough version of Java installed). Now, you don’t have to download or install anything in order to run it.

You can find the SimHeart applet here.

Once again, many, many thanks to the creators of NetLogo.

SimHeart — Now Available for Download

All right, as promised, I’ve finally figured out a way that people can download SimHeart to play with it themselves. Many thanks to the folks at NetLogo for automating so much of the process, and thanks to MediaFire.com for the free file hosting.

The file is kind of large because, in order for it to work, I had to put a bunch of Java modules into the folder with it, but it shouldn’t take too long to download, even over a slow-ish Internet connection. When you’ve downloaded it, you’ll need to extract the file to your desktop. I recommend an unzipping program like WinZip or WinAce. The program should (major, major emphasis on should) work on Macs and PCs, but I make no guarantees.

To run the simulation, go into the folder into which you’ve extracted SimHeart, and double click on the HTML file there. It should open up in a new window, and you should see the simulation screen. If you don’t, either you don’t have an up-to-date version of Java, or something went wrong in the download process, or I made a mistake zipping the files. If you checked the previous two things, please leave a comment and describe the problem, and I’ll try to help, although I make no claims to be very good at this kind of thing.

Also, I must provide the obligatory legal disclaimer: I take no responsibility if this file somehow damages your system. To my knowledge, there is absolutely nothing in the file that should do so, but you never know, something might have gotten corrupted or damaged along the way. Also, this software is for entertainment purposes only, and should not be taken as any form of medical advice. I’m not sure why anybody would, but you never know.

Download SimHeart 2.0 here.

If you already have the latest version of NetLogo installed on your computer, you can download the muchhere. If you’re interested in this kind of thing, you should go ahead and download NetLogo (you can do that here). Not only will it allow you to download a much smaller file, but NetLogo comes with a whole cornucopia of fascinating little simulations, and there are more you can download from the Internet. smaller .nlogo file

Okay, apparently, that site decided to get rid of the file, so if you want to have a look at SimHeart, you can find it here, on the NetLogo community models page.

If you have trouble with either of these files, please let me know by commenting on this post. If you don’t want to do that for some reason, send an e-mail to asymptote [døt] inverse [át] gmail [døt] com (Sorry about all the weird characters in there, but that account gets enough spam as it is, without ever having broadcast the address on the Internet, so I figured I’d better obfuscate as much as possible).

I’ll try to update the files as I revise SimHeart, but I seem to be at a point where there’s not much more I can do with it, at least not without rewriting most of the code. I’ll be sure to post updates as they come.

SimHeart 2.0

It seems that every time I sit down to work on my heart-simulation project, I get a lot more done than I was expecting. In my last post on the subject, I talked about how I wanted to integrate a more realistic model of the atrioventricular (AV) node, the little bundle of nerve fibers that carries the contraction impulse from the atria at the top of the heart to the ventricles on the bottom. Apparently, I’d entirely misjudged the difficulty of this effort, since, once the solution occurred to me, I was able to implement it in about five minutes.

Here’s what I did. As I said before, each cell in the simulation has two variables assigned to it: ARefrac, which determines whether or not an atrial impulse can pass through the cell; and VRefrac, which determines whether a ventricular impulse can pass through. I solved the AV-realism problem by simply introducing a global variable called AVRefrac that determines whether or not the AV node can accept an impulse. Basically, every time a simulated electrical “spark” strikes the simulated node, as long as AVRefrac is equal to or less than zero, it sets AVRefrac’s value to a user-specified constant I call AV-delay. So, basically, now the ventricles can only respond as fast as the AV node will allow, just like a real heart! When I saw how beautifully my little fix had worked, I was thrilled!

So, my simulated heart is now more realistic than ever. For example, I did a few runs with the refract-length value (the value that determines how quickly cells recover their ability to fire after each firing) set very short so that arrhythmias would occur frequently, so that I could study their effects. Before long, my simulated heart went into atrial flutter/fibrillation (a condition where the small pumping chambers at the top of the heart expand and contract quickly and chaotically, often leading to a dangerously fast ventricular rate. I was amazed to see something very similar to the many atrial-fibrillation EKG’s I’ve looked at:

(Note: in the simulated EKG, I’ve separated the atrial and ventricular signals, since whenever the ventricular rate got very fast, it obscured all the atrial activity, and I wanted to be able to study the atrial activity as well)

Given my tendency towards oversimplified simulations that produce peculiar behavior, the resemblance this bears to real supraventricular tachycardia (fast heart rate caused by the atria, which is often seen in atrial flutter or fibrillation) was frankly, surprising. After about half a second of atrial flutter, the atria begin to fibrillate, producing that classic irregular ventricular response.

Note the extremely high ventricular rate that shows up towards the end of the ECG. That’s a rather unrealistic product of my simulation, since whenever one of the waves of excitation collided with the back of a previous wave, it had a tendency to collapse into a tachycardic or fibrillatory spiral.

There are some forms of supraventricular tachycardia that terminate on their own. They’re called “paroxysmal” supraventricular tachycardia, and my simple little simulation actually managed to produce a run of it!

Some forms of atrial fibrillation occur in the presence of heat block (which, in its most common form, is basically a very slow AV node that doesn’t conduct every impulse that passes to it). In those cases, the fibrillation is frequently asymptomatic or minimally symptomatic, since the heart doesn’t end up racing. When I set the AV-delay parameter higher than usual, I observed this very same phenomenon.

Eventually, the aforementioned wave-collision problem had become annoying enough that I decided to re-write part of the simulation so that there was a small probability that an electrical spark could actually cross a cell that had not entirely recovered. That solved a lot of my problems.

In the re-written simulation, atrial fibrillation still produces that classic irregular ventricular heartbeat, and this time, since the waves are more collision-tolerant, the behavior doesn’t immediately degenerate into ventricular fibrillation, which gives me a chance to actually study it properly.

More updates as they’re warranted. And for those reader(s?) who are wondering what the hell has been wrong with me lately, don’t worry, I’ll be turning the blog over to my old cynical, sarcastic self very shortly.

UPDATE:

I was sitting around without much to do, so I opened up SimHeart and let it run in the background. When I checked in on it again a few minutes later, I’d discovered some very interesting behavior:

Apparently, some of the standard sort of atrial fibrillation had started, then, spontaneously self-organized into a coordinated wave spiraling cyclically through the atria. You can see the wave in the screenshot.

This really grabbed my attention, so I watched it for a while, and discovered that, strangely enough, the wave was quite stable.

Not even the normal sinus beats, which occasionally inserted themselves in the path of the wave, were very good at disrupting it. Not long after this screenshot, it degenerated rather suddenly into normal atrial fibrillation.

Then, while having a look at the pictures a few minutes later, I realized something: my simulation had produced true atrial flutter. What I saw before and called atrial flutter was really just organized fibrillation. This, though, exhibits all the classic features of atrial flutter: rapid atrial waves with a sawtooth shape. In this case, since I had the ventricular response set to be fairly quick, it turned into quite realistic atrial tachycardia.

I tried to save the state of the simulation so that I could study it later, but as there are some features of NetLogo with which I’m not entirely familiar, I wasn’t able to do it. So, for now, I guess I’ll just keep running HeartSim in the background until I see that rhythm again.