Skip to main content

Automating Irrigation Calculations: A Developer's Take

The hydraulics textbook gives you the formula. It doesn’t tell you the formula is the easy 10%.

I’m an engineer who writes code. Most of my day-job math is open-channel hydraulics — how much water a canal carries, how deep it runs, where the water backs up behind a check structure. These are “solved problems.” Every one has a tidy equation in a textbook that’s been around for a century. So when I started building FlowStudio, a tool to automate them, I assumed the calculations would be the trivial part.

They were the trivial part. The interesting engineering was everything the textbook leaves out.

The formula lies about which direction you’ll use it

Take Manning’s equation — the workhorse of canal design. Give it a water depth and it hands you the flow rate directly: area, wetted perimeter, hydraulic radius, a couple of exponents, done. Clean and explicit.

Except that’s backwards from how engineers actually work. In the field you know the flow rate — the canal has to deliver so many cubic meters per second — and you need to find the depth. And there’s no closed-form way to invert Manning’s equation for a trapezoidal channel. The depth is buried inside both the area and the perimeter, under a 2/3 power. You cannot solve for it algebraically.

So the “simple” calculation becomes a root-finding problem. FlowStudio brackets the answer — start with a tiny depth, grow the upper bound by 60% at a time until the computed flow overshoots the target — then bisects that interval a hundred times until the flow matches to a relative tolerance of 1e-12. The user types one number and gets a depth. They never see that under the hood the tool is hunting for it numerically, because the equation refuses to be rearranged.

That’s the recurring theme: the physics is one-directional, but real work runs it the other way.

Reality bites hardest near critical flow

Backwater curves — the water-surface profile upstream of a structure — are governed by a differential equation you integrate step by step along the channel:

dy/dx = (S0 − Sf) / (1 − Fr²)

Beautiful. Also a landmine. As the flow approaches critical (Froude number → 1), that 1 − Fr² denominator goes to zero, and dy/dx blows up toward infinity. The math has a genuine singularity, and a naive integrator marching in fixed steps will either produce garbage or divide by zero right where the engineer most needs an answer.

The textbook draws a smooth curve and moves on. The code has to survive that region. FlowStudio watches how close it is to critical and adaptively chops each step into smaller substeps when the equation gets stiff — up to a couple hundred of them near the singularity — then resumes normal strides once it’s clear. None of that is in the hydraulics book. It’s the part where numerical methods and irrigation engineering meet, and almost nobody writes about that intersection.

The unglamorous 80%: making a number an engineer will trust

Here’s what actually took the time:

  • Units and guards. Every input gets validated before it touches a formula. Width, depth, Manning’s n, slope — all must be positive; a negative side-slope is nonsense. Gravity is a fixed 9.80665, not a rounded 9.81, because these numbers end up in a signed design report.
  • Honest failure. If someone asks for a flow rate that’s physically unreachable for their channel, the tool says “discharge is below the reachable range for these inputs” — not NaN, not a silent wrong answer. A wrong number a field engineer trusts is far more dangerous than an error message.
  • Showing the work. It reports the Froude number, the hydraulic depth, the wetted perimeter — the intermediate quantities a reviewer needs to sanity-check the result, not just the final depth.

That’s the difference between a formula and a tool. The formula is 10 lines. The trust is the other 90%.

Why not just use a spreadsheet?

Most of this world runs on Excel sheets passed around by email. They work — until a circular reference silently breaks, or someone overwrites a cell, or you need to know which version produced the number in a report filed two years ago. Spreadsheets have no memory and no guardrails.

Building a real tool means versioned worksheets, validated inputs, and reproducible reports — the boring infrastructure that turns a calculation into something you can defend in a design review.

The takeaway

If you write code and you know a technical domain deeply, the domain is your moat. Plenty of developers can write a bisection solver; very few know that irrigation canal design needs one, or that the backwater equation will try to divide by zero on you near critical flow. That overlap is rare, and it’s where the genuinely useful tools get built.

The whole thing runs as plain JavaScript in the browser — no framework, no heavy install. Just the math, made trustworthy.

Comments

Popular posts from this blog

Cursor AI Review: Is the AI Code Editor Worth It?

I've been using Cursor as my main code editor for a while now, and enough people have asked whether it's worth switching to that a proper review felt overdue. Short version: for me, yes — but with caveats. What is Cursor? Cursor is an AI-first code editor built as a fork of VS Code. That means every extension, theme, and keybinding you already use in VS Code works here, but with AI woven directly into the editing experience instead of bolted on as a plugin. It's made by Anysphere and can run models from OpenAI and Anthropic under the hood. What I like Tab completion is uncanny. Cursor predicts your next edit — not just the rest of the line, but the next change across the file. Once you get used to hitting Tab, going back to a plain editor feels slow. The Composer / Agent mode. You describe a change in plain language and it edits multiple files at once, showing you a diff to accept or reject. For refactors and boilerplate, this saves real time. It unde...

MacBook Pro M5 vs M5 Pro: Which One Should You Actually Buy?

Apple's latest 14-inch MacBook Pro comes in two very different flavors: the base M5 and the step-up M5 Pro . On paper they look similar — same gorgeous Liquid Retina XDR display, same design — but under the hood the gap is bigger than the names suggest. Here's a clear, no-hype breakdown, with concrete use cases so you can match the chip to your work. Quick spec comparison Spec M5 M5 Pro CPU 10-core (4 performance + 6 efficiency) Up to 18-core (6 performance + 12 efficiency) GPU 10-core Up to 20-core Neural Engine 16-core 16-core Memory bandwidth 153 GB/s 307 GB/s (roughly double) Unified memory 16 / 24 / 32 GB 24 / 48 / 64 GB Max storage Up to 4 TB SSD Up to 8 TB SSD Battery (video playback) Up to 24 hours Up to 22 hours Media engines Single encode/ProRes engine More encode/ProRes engines (higher configs) What actually changes between them More cores — the M5 Pro nearly doubles CPU cores and adds GPU cores, so sustained, multi-threaded work finishe...

Running a Server on a Mac Mini: Apple Silicon vs the Home-Server Field

The Mac Mini has quietly become one of the most interesting home-server boxes you can buy. It’s tiny, nearly silent, sips power, and Apple Silicon punches far above its weight. But is it actually the right machine to run your services on — or are you paying an Apple tax for a job a $400 mini PC does better? Let’s put it head-to-head. Why a Mac Mini makes a surprisingly good server Three things make Apple Silicon compelling as an always-on machine: Performance per watt. This is the headline. An M4 Mini idles at just a few watts and rarely pushes past ~35W under load, while delivering multicore performance that embarrasses machines drawing twice the power. Silence. Under typical server loads the fan is inaudible. If your “server” lives in a living room or bedroom, this matters more than any benchmark. Footprint. It’s the size of a coaster and runs cool, so it tucks anywhere. The honest catch It’s not all upside: macOS isn’t...