Whether or not you use a Keurig, an R2-D2 french press, or robot barista with great pour-over skills, the end goal is the same: to make the perfect cup of joe. The ultimate way to make damn good coffee, however, is to perfect the system with math.

In a paper published Tuesday in the SIAM Journal of Applied Mathematics, a team of researchers from the University of Limerick explain that their mathematical model can maximize the flow and extraction process of making coffee. They focused on perfecting the use of a classic drip filter machine, one of the most common ways to brew.

Drip filter machines make coffee by pouring hot water over coffee grounds in a filter. The brown liquid we call coffee is extracted when gravity pulls water through the filter and through the grounds. The taste of coffee, the researchers point out, ultimately depends on how each component in the brewing process relates to each other. More specifically, it depends on the size of the coffee grounds.

A diagram of the transfers of water and coffee in the extraction model.
A diagram of the transfers of water and coffee in the extraction model.

The researchers describe their mathematical model as a “double porosity model” in which “extraction is controlled by a rapid dissolution of coffee from the surface of the grain, in conjunction with a slower diffusion of coffee through the intragranular pore network [the filter] to the grain surface.” In other words, coffee brewing happens in two steps: First, when the hot water leaches out coffee flavor from the surface of a ground-up bit of bean, and then some time later, when the remaining hot water dripping through the machine sucks out flavor from the core of those surface-stripped coffee grounds. Broken down like this, it’s a little clearer why the size of those coffee grounds is so important.

Ultimately, the essential part of making a great cup of coffee is the timing of the transfers between each component, from the water hitting the beans to the coffee coming out of the filter — all of which, again, depend on the size of the grounds. The math here is tricky, but basically the researchers looked at the ratio of the “advection timescale” (the time it takes heat to move through grounds via hot water) to the “grain diffusion timescale” (the time it takes for flavor to be leached out of coffee) and used it as a key parameter in their optimization scheme.

Figure A shows how espresso coffee is made.
Figure B demonstrates how drip filter brewing works.

“Initially, the concentration of coffee in the bed is determined by the balance between a rapid extraction from the surfaces of coffee grains and the rate at which coffee is removed from the coffee bed by the extracting water,” lead author Kevin Moroney, Ph.D., said in a statement. “Later in the process, the extraction is dominated by slow diffusion of coffee from the kernels of larger grains, which is initially negligible.”

And there you have it: Paying attention to the size of your coffee grounds is the mathematical hack for making coffee that puts Starbucks to shame. Attention baristas: Maybe instead of working on your floral foam art, brush up on your applied math instead.

Photos via SIAM/Kevin Moroney (1, 2), Giphy (1, 2)