Pages

Saturday, August 26, 2023

Head's Up

Recently, I saw my father-in-law Scott pour a bottle of beer into a glass, and I was fascinated by the relationship between the rising beer at the bottom, and the foam moving on top. I was curious if I could model the dynamics involved, so I decided to check if anyone else has tackled the problem. I found an article from a brewer discussing some of the steps involved, and I decided to split the process into 4 parts:

  1. For each bubble in the foam, apply forces from neighboring bubbles, and gravity pulling down.
  2. Drain liquid from higher to lower bubbles, based on the content of each. Bubbles at the bottom drain into the liquid beer.
  3. If neighboring bubbles each have low liquid content, merge them into a single larger bubble.
  4. Bubbles with low liquid and/or large size pop, adding their liquid to the beer at the bottom.
To represent these bubbles, we need to use sphere-packing to figure out how they fit in the glass. I've talked about the concept before, but since we're making a simulation this time, I found the package spack for Python. This handles keeping track of where the bubbles are, and can draw them for us. It also calculates the forces between the bubbles, based on their separation and radii.

To apply these forces, we need the mass of the bubbles – They're made of gas and liquid, but the liquid mass will far outweigh the gas. We're already keeping track of liquid content for the other steps, so we can use that for the mass and then displace each bubble based on the total force divided by its mass.

The spack package keeps track of which bubbles are adjacent – For each pair, we can drain liquid from the upper to the lower. The proportion I settled on was that at each step the lower bubble would get 51% of the total moisture, and the top one would be left with 49%. For bubbles with nothing below them, they drain the full amount to the liquid at the bottom. Similarly, we can use the adjacent list to pick bubbles to merge – Based on the article I linked above, I decided their circumferences would add, rather than areas. We select which bubbles merge based on their liquid content – Dryer bubbles merge more easily. Big, dry bubbles can also pop, giving up their liquid to the beer at the bottom.

We start off the simulation by filling the glass with a bunch of small bubbles, then let the rules outlined take over. I ended up using 1000 bubbles to start with, which takes a bit of time to get through, but I'm impressed with the results:

We can also measure some averages over the course of the simulation. First, we can simply count the number of bubbles:

The rate is fairly constant, despite the various dynamics going into determining the merging/popping. We can also look at the average size of bubbles:

I was a bit surprised by the sudden rise in the average size at the end, but I think it may be related to the merging of the bubbles: They get larger and fewer, resulting in a compounded effect on the average, and near the end, we have far fewer bubbles, making the average more sensitive to change. The average liquid content has a similar knee at the later times:

I have no idea how accurate this model is, but it does seem to follow the events outlined in the article: Bubbles merge, dry out, and pop, resulting in a shrinking head of foam, and growing reservoir of liquid at the bottom. Clearly I need to gather more data – Cheers!

Saturday, August 12, 2023

Maximized Magnets

Recently another post from Hack-a-Day caught my eye, discussing a technique for stacking magnets to get a stronger field, called a Halbach Array. You might assume that if you have a bunch of small magnets, the best way to combine them would be stack them with all their poles pointing in the same direction, but it turns out you can get a stronger field by stacking them in an unintuitive pattern:

Wikipedia

The arrows point from south to north pole of each magnet. Using the (wonderfully named) Python package Magpylib we can look at the magnetic field produced by this combination:

The field strength is represented by a higher density of lines, but that's a little hard to read off this plot, so we can also plot the overall magnitude of the field on a line just above and just below the magnets:

Not only is the field stronger than a single magnet's, it's stronger on one side than the other! This gave me pause, since it sounded like a monopole magnet, which is forbidden by Maxwell's Equations, specifically Gauss's Law for Magnets:

This says that for any closed surface S, all the field lines going out of the surface need to be matched with lines coming in, so that the sum cancels. It seems like our setup could have more lines going out the bottom than coming in the top, but the key is that even though the magnitude of the field is stronger on one side of the array, it includes both north and south poles. You can see this in the field plot above: there are lines going in and out on both sides. We can double check by integrating around a box as S:

When the loop closes, we're back to zero, and no laws are violated!

That last section may have been a little technical, so I'd like to end on something (I find) beautiful. In the Wikipedia article for Halbach arrays, they mention a version using magnetic rods, which can be rotated to switch the field from one side to the other. I was curious how the field strength varied during this, so I made an animation with the output from Magpylib:

The dots mark the north pole of each rod. Each 90° rotation swaps the strong side, but I think the movement of the 3 low-field nodes is really cool. Thanks, Hack-a-Day, for introducing me to this nifty structure!

Sunday, August 6, 2023

Nega Millions

The Mega Millions lottery has been in the news a bunch lately, due to the growing jackpot, and I wondered how big the jackpot would need to be for it to be worthwhile to play. Specifically, I was curious about the relationship between the jackpot and the number of players, and exactly how unlikely winning is.

In many articles I found, like the one above, I saw the probability quoted as 1 in about 300 million, but I wanted to go through it myself: As of 2017, the drawing involves 5 numbers chosen from a set of 70, and 1 chosen from a set of 25. The order doesn't matter, so we can write this as

where the exclamation mark is the factorial operator, defined as n! = n*(n-1)*(n-2)*...*2*1. If you do this calculation, you'll find the number given elsewhere – Initially, I thought the order did matter, in which case the 5! would be left out, and there would be significantly more possibilities.

The total jackpot starts at $20 million, and then grows based on the number of tickets sold. We can look at how the jackpot has changed over the past year:

Each time the jackpot is won, it drops back to the baseline $20 million. The slope of the climb depends on the number of players – Note that as the jackpot increases, the slope of the curve also increases. When discussing outcomes with different probabilities, mathematicians often use the expectation value, a weighted average using the probabilities as weights. Since multiple winners split the jackpot, we can write the expectation value for the payout as

The chance of winning is 1/C; the chance of not winning then is (1 - 1/C). If k people out of N players win, we can raise the respective probabilities to the power of k and N-k to compound them. Now we can look at the relationship between the jackpot, number of players, and expected payout:

A couple interesting features of this plot: $500 million appears to be a threshold for a lot of people – The slope of the points increases here, suggesting people outside the set of regular players have joined in. Due to the low chances of winning, the expected value only barely passes $4, although that does exceed the purchase price of $2, so buying a ticket is "worth it" (if you ignore the time cost of buying the ticket and checking the numbers). The low win probability also means that the multiple winner aspect is pretty insignificant. For the numbers I used here, it didn't seem to change anything from the base winning number.

Th other thing I was interested in was how many drawings passed between wins. We can split the data up based on when the jackpot resets to $20 million, and then plot the curves on top of each other:

There are several that last only a couple days, but I was really surprised that the longer-lasting ones have almost identical changes in the number of players over time. That suggests the threshold behavior I mentioned earlier also applies on a smaller scale: As the jackpot grows, there's a continuous increase in the number of people willing to take the chance.

Going into this, I was already confident that playing the lottery was a terrible idea, but to me the expected value really drove that idea home: Even a record-setting jackpot will only barely push you over the break-even point.