Latest / Civil Engineering Made Simple / Fortran for Scientists and Engineers
Transcript
- 0:00Speaker 1: Welcome back to the deep dive. Today, we are looking
- 0:02Speaker 1: at a tool that sits at the very bedrock of
- 0:07Speaker 1: modern engineering, yet somehow it manages to have a reputation
- 0:10Speaker 1: for being well ancient.
- 0:12Speaker 2: Oh yeah, we're talking about Fortran. We are, and I
- 0:15Speaker 2: know half the audience just you know, they pictured a
- 0:17Speaker 2: stack of punch cards and maybe a room full of
- 0:20Speaker 2: people in shirt fleeved white shirts and skinny ties at NASA, which, to.
- 0:24Speaker 1: Be fair, is a strong look. Right. But the assumption
- 0:26Speaker 1: is always isn't this a dead language? Why on earth
- 0:29Speaker 1: are we talking about it now?
- 0:31Speaker 2: Well, we've been going through the fourth edition of Fortran
- 0:33Speaker 2: for Scientists and Engineers by Stephen J. Chapman, and it
- 0:36Speaker 2: turns out that assumption is it's.
- 0:39Speaker 1: Dangerous, dangerous.
- 0:40Speaker 2: How it's not just dangerous, it's expensive. I mean, if
- 0:43Speaker 2: you look at the landscape of high performance computing, weather prediction,
- 0:46Speaker 2: heavy civil engineering simulations, computational fluid dynamics, Fortran is not
- 0:52Speaker 2: a dinosaur. It's a shark, a shark, an apex predator.
- 0:55Speaker 2: It hasn't really evolved much in its basic shape because
- 0:58Speaker 2: it was perfectly designed for its environment from the get go,
- 1:01Speaker 2: and that environment is crunching massive arrays of numbers faster
- 1:05Speaker 2: than pretty much anything else.
- 1:07Speaker 1: So our mission today is to unpack Chapman's book, but
- 1:11Speaker 1: not as a history lesson right, We're looking at it
- 1:13Speaker 1: through the lens of civil engineering reliability exactly.
- 1:17Speaker 2: We aren't hacking a script together. We're building simulations for bridges,
- 1:21Speaker 2: for dams, for things where a coding error I means
- 1:25Speaker 2: something could actually fall down. And Chapman frames this entire
- 1:29Speaker 2: book around what he calls good programming practice. He's not
- 1:32Speaker 2: teaching you syntax. He's teaching a whole philosophy of how
- 1:35Speaker 2: to write code that doesn't collapse under its own weight.
- 1:38Speaker 1: So let's start there with the philosophy. Because he hits
- 1:41Speaker 1: us with a statistic from the mythical Man month that
- 1:43Speaker 1: feels really counterintuitive to anyone who's ever faced a deadline.
- 1:48Speaker 2: Oh, this is the part that makes students just grown.
- 1:50Speaker 2: Chapman argues that in a big engineering project, you should
- 1:54Speaker 2: spend one third of your time planning, just planning.
- 1:57Speaker 1: And the actual coding. Wow, that seems incredibly low. So
- 2:01Speaker 1: you're spending twice as much time drawing blueprints as you
- 2:04Speaker 1: are actually you know, typing code, and.
- 2:06Speaker 2: The other half of your time testing. The whole logic
- 2:10Speaker 2: is what he calls top down design. You don't just
- 2:12Speaker 2: open a file and start writing loops.
- 2:15Speaker 1: You state the problem first.
- 2:16Speaker 2: You state the problem, you'd find your inputs and outputs.
- 2:19Speaker 2: Then you design the algorithm with pseudocode or flow charts.
- 2:22Speaker 2: It's like if you start pouring concrete before you have
- 2:24Speaker 2: the blueprints, you just get.
- 2:26Speaker 1: A mess, and in code you get bugs that are
- 2:28Speaker 1: almost impossible to find later.
- 2:30Speaker 2: Exactly, and speaking of bugs, Chapman throws down a gonlet
- 2:33Speaker 2: really early on. He insists on one specific command implicit none.
- 2:39Speaker 1: Okay, is this just a purest thing or is it
- 2:41Speaker 1: actually practical?
- 2:42Speaker 2: Oh, it's practical. It's the difference between a working bridge
- 2:44Speaker 2: and a lawsuit.
- 2:45Speaker 1: That's a high bar for one single line of code.
- 2:48Speaker 2: Well, here's why. In old school fourtran, if you didn't
- 2:51Speaker 2: declare a variable, the compiler would just guess what it was.
- 2:54Speaker 2: If it's name started with ijk, L, M, or N,
- 3:00Speaker 2: it decided it was an integer.
- 3:01Speaker 1: And anything else was a floating point number.
- 3:03Speaker 2: Right, So imagine you make a typo. You're calculating velocity,
- 3:07Speaker 2: but you accidentally type velocit you miss the why. Okay,
- 3:11Speaker 2: Without implicit none, the compiler just says, oh, blossit never
- 3:15Speaker 2: seen that before. Must be a new variable. I'll just
- 3:17Speaker 2: create that for you and set it to zero.
- 3:20Speaker 1: And just like that, my simulation thinks the velocity is
- 3:22Speaker 1: zero when it's supposed to be. I don't know, mock
- 3:25Speaker 1: three precisely.
- 3:27Speaker 2: Chapman calls these typos the Godzella of bugs. Implicit none
- 3:31Speaker 2: makes that impossible. It forces you to declare every single
- 3:35Speaker 2: variable you use. So if you make a typo, the
- 3:37Speaker 2: compiler stops dead and screams at you. It catches the
- 3:40Speaker 2: error at compilation time, which is cheap, instead of at runtime,
- 3:43Speaker 2: which is very very expensive.
- 3:44Speaker 1: Okay, so let's move from the philosophy to the math itself,
- 3:48Speaker 1: because in engineering, precision is everything. But Chapman points out
- 3:52Speaker 1: this trap that I think catches even experienced people mixed
- 3:55Speaker 1: mode arithmetic.
- 3:56Speaker 2: This is a classic silent killer. Everyone knows that an
- 3:59Speaker 2: integer man three divided by four is zero. It trunk caates, right, It.
- 4:03Speaker 1: Just chops off the decimal. It doesn't round.
- 4:05Speaker 2: But the real danger is when you mix types in
- 4:08Speaker 2: a formula. Let's say you're writing a basic physics equation
- 4:11Speaker 2: for displacement. Why equals twelve grand t two.
- 4:14Speaker 1: Standard stuff one half gravity times times squared.
- 4:17Speaker 2: Right, But if you type twelve in your code four
- 4:20Speaker 2: trans season integer divided by an integer, the result of
- 4:23Speaker 2: that is zero.
- 4:24Speaker 1: So the computer calculates zero gravity.
- 4:26Speaker 2: Time, and your entire simulation result is zero. The bridge
- 4:29Speaker 2: just floats away into space. You have to write point
- 4:32Speaker 2: five or one point zero two point zero to force
- 4:34Speaker 2: it to use floating point math, and.
- 4:37Speaker 1: The compiler won't warn you because twelve is perfectly valid syntax.
- 4:41Speaker 2: It just gives you the wrong answer silently. He also
- 4:44Speaker 2: wants about checking if two floating point numbers are equal,
- 4:47Speaker 2: like asking the computer does a equal b.
- 4:49Speaker 1: You should never do that, right.
- 4:50Speaker 2: Never, Because of tiny round off errors. Two numbers that
- 4:54Speaker 2: should be identical might differ at like the fifteenth decimal place,
- 4:57Speaker 2: so the computer will say false. You have to check
- 5:00Speaker 2: if the difference between them is smaller than some tiny tolerance.
- 5:03Speaker 1: Okay, so let's say we've done our maths safely. Crunching
- 5:06Speaker 1: the numbers is one thing, but you have to report them.
- 5:09Speaker 1: And Chapman makes a really clear distinction between just dumping
- 5:12Speaker 1: data and actually formatting it for an engineering report.
- 5:15Speaker 2: This is all about the right statement, and specifically the
- 5:18Speaker 2: difference between the E descriptor and the end.
- 5:20Speaker 1: Descriptor right scientific notation versus engineering notation.
- 5:24Speaker 2: Exactly standard scientific notation. The E descriptor gives you a
- 5:27Speaker 2: number like say one point four or five times ten
- 5:30Speaker 2: to the negative four, which is you know it's correct,
- 5:34Speaker 2: but it's hard to visualize quickly.
- 5:36Speaker 1: Because engineers think in multiples of three mill is micros kilos.
- 5:40Speaker 2: Megas precisely, So the E end descriptor forces the exponent
- 5:43Speaker 2: to be a multiple of three. So instead of times
- 5:46Speaker 2: ten to the negative four, it'll give you one hundred
- 5:48Speaker 2: and forty five times ten to the negative six.
- 5:51Speaker 1: One hundred and forty five micro strains.
- 5:53Speaker 2: Boom, instant comprehension. It sounds like such a small thing,
- 5:56Speaker 2: but when you are scanning five hundred pages of seismic
- 5:59Speaker 2: data output, that kind of readability prevents critical interpretation errors.
- 6:03Speaker 1: All right, let's get to the heavy lifting, the real
- 6:06Speaker 1: reason you called Fortran.
- 6:07Speaker 2: The shark arrays, yes, this is where other languages they
- 6:13Speaker 2: really struggle. If you want to add two lists of
- 6:15Speaker 2: a million numbers in something like C or Python, you
- 6:18Speaker 2: usually have to write a loop for I equals one
- 6:21Speaker 2: to a million.
- 6:22Speaker 1: Do this, which is tedious to write, and more importantly,
- 6:26Speaker 1: it's slower to execute.
- 6:27Speaker 2: In Fortran, you just write C equals A plus B.
- 6:30Speaker 2: That's it. It's called whole array operations. The language understands
- 6:34Speaker 2: this math natively. It optimizes that single line to run
- 6:38Speaker 2: on the hardware as fast as is physically possible.
- 6:40Speaker 1: But there's a big shift from old to modern Fortran
- 6:43Speaker 1: here with memory. Right, yeah, Chapman talks about allocatable arrays.
- 6:47Speaker 1: This solves that worst case scenario problem, doesn't it.
- 6:50Speaker 2: It absolutely does. In the old days, you had to
- 6:52Speaker 2: hardcode your erase sizes at the beginning of the program.
- 6:55Speaker 2: So if you are writing a simulation for a dam,
- 6:57Speaker 2: you'd have to allocate enough memory for the biggest possible
- 7:00Speaker 2: you could ever imagine, even if you were just testing
- 7:02Speaker 2: a tiny, little weird.
- 7:03Speaker 1: So you're locking up gigabytes of RAM just in.
- 7:06Speaker 2: Case exactly, and if you gets wrong and the real
- 7:08Speaker 2: problem was bigger than your limit, the whole program would
- 7:11Speaker 2: just crash.
- 7:12Speaker 1: So what's a modern way.
- 7:13Speaker 2: Modern Fortran uses the allocatable attribute. You tell the program
- 7:17Speaker 2: the array exists, but you don't give it a size.
- 7:19Speaker 2: Then you read the input file see how big the
- 7:21Speaker 2: problem actually is, and you allocate exactly that much memory.
- 7:26Speaker 1: It adapts to the physics of the.
- 7:27Speaker 2: Problems, adapts, and when you're done, you deallocate it to
- 7:30Speaker 2: free up the resources for large scale civil systems, that
- 7:33Speaker 2: kind of flexibility is just non negotiable.
- 7:36Speaker 1: So we have our philosophy, we have our data. Now
- 7:38Speaker 1: let's talk about structure. Chapman talks a lot about decomposition,
- 7:42Speaker 1: which is really.
- 7:42Speaker 2: Just the art of breaking a massive problem into small, manageable,
- 7:47Speaker 2: testable chunks. And in Fortran we split these chunks into
- 7:50Speaker 2: functions and subroutines.
- 7:52Speaker 1: Now, most people know what a function is, but Chatman
- 7:55Speaker 1: makes a very strict distinction based on its purpose.
- 7:58Speaker 2: He does. A function is for pure calculation. It takes
- 8:01Speaker 2: some inputs, does some math, and returns a single value,
- 8:04Speaker 2: no side effects, like you know, calculating the stress on
- 8:07Speaker 2: one specific.
- 8:08Speaker 1: Drink orre is a subroutine.
- 8:09Speaker 2: A subroutine is the workhorse. It performs an action, it
- 8:12Speaker 2: reads a file, it's sorts of database, It updates the
- 8:16Speaker 2: state of the entire simulation, and critically, it handles data
- 8:21Speaker 2: using something called pass by reference.
- 8:23Speaker 1: This is the warehouse keys analogy, isn't it.
- 8:25Speaker 2: That's it exactly when you pass a massive array. Let's
- 8:29Speaker 2: say it's a terrain map with ten million data points
- 8:32Speaker 2: to a subroutine forourtran doesn't waste time copying all ten
- 8:36Speaker 2: million points. That would be way too slow.
- 8:38Speaker 1: It just hands over the memory.
- 8:39Speaker 2: It just hands the subroutine the keys. It says, the
- 8:41Speaker 2: data you need is an aisle four.
- 8:43Speaker 1: That's incredibly fast, but it sounds risky. If the subroutine
- 8:48Speaker 1: messes something up, it's changing the original data, not a copy,
- 8:52Speaker 1: and that's.
- 8:52Speaker 2: The trade off. It's efficient, but it can be dangerous,
- 8:55Speaker 2: which is why Chapman so heavily emphasizes modules is.
- 8:59Speaker 1: The modern replacement for common blocks.
- 9:01Speaker 2: Right oh, common blocks were a nightmare, a total mess
- 9:04Speaker 2: of memory linement issues. Modules are the clean, modern container,
- 9:08Speaker 2: and they provide something called explicit interfaces. This is crucial.
- 9:11Speaker 1: It was I mean explicit interfaces.
- 9:13Speaker 2: It means if you put your subroutine inside a module,
- 9:16Speaker 2: the compiler now knows exactly what kind of data that
- 9:19Speaker 2: subroutine expects to receive. So if you try to pass
- 9:22Speaker 2: it a single integer when it was expecting an array
- 9:25Speaker 2: of real numbers, the compiler stops you.
- 9:27Speaker 1: So it prevents you from handing the warehouse keys to
- 9:30Speaker 1: the wrong person.
- 9:31Speaker 2: Essentially, Yes, it catches all those interface errors before you
- 9:34Speaker 2: even get a chance to run the code.
- 9:36Speaker 1: Now we have to talk about how Fortran has modernized.
- 9:39Speaker 1: I think this will surprise people who haven't looked at
- 9:41Speaker 1: it since the nineties. Chapman covers object oriented programming.
- 9:45Speaker 2: He does, yeah, and this is huge for making code maintainable.
- 9:49Speaker 2: He introduces derived data types, So instead of having one
- 9:52Speaker 2: array for beam length and another for beam material, you
- 9:55Speaker 2: can create a single type called beam that holds both.
- 9:58Speaker 1: You're treating the engineering component as a single object in
- 10:01Speaker 1: the code exactly.
- 10:03Speaker 2: And with Forourtran two thousand and three. In two thousand
- 10:05Speaker 2: and eight, you get things like inheritance and polymorphism. You
- 10:08Speaker 2: can define a generic class let's call it shape, and
- 10:11Speaker 2: then extend it to more specific types like triangle or rectangle.
- 10:14Speaker 1: And you can have a generic calculatory routine that figures
- 10:17Speaker 1: out which formula to use.
- 10:18Speaker 2: Right, the program figures it out based on the object's type.
- 10:21Speaker 2: At runtime. It sounds like C plus plus or Java,
- 10:25Speaker 2: but it's all optimized for scientific throughput. You can build
- 10:28Speaker 2: these amazing libraries of reusable engineering parts.
- 10:31Speaker 1: But the real modern power move and he devotes chapter
- 10:34Speaker 1: seventeen to this is parallel processing cores.
- 10:37Speaker 2: This is the frontier. We live in a multi coore world.
- 10:41Speaker 2: Your laptop has multiple processors. Standard code just uses one.
- 10:45Speaker 2: But if you're modeling, say floodwaters moving through an entire city,
- 10:49Speaker 2: using one core is way too slow.
- 10:51Speaker 1: So how to corase handle that?
- 10:53Speaker 2: It uses a model called SPMD Single program multiple data.
- 10:57Speaker 2: When you launch a simulation, it creates a one hundred
- 10:59Speaker 2: copies of itself.
- 11:00Speaker 1: These are called images, and each image works on a
- 11:03Speaker 1: different piece of the puzzle precisely.
- 11:05Speaker 2: Image one takes the north side of the city, Image
- 11:07Speaker 2: two takes the south side, and quarries are the magic
- 11:10Speaker 2: that lets them swap data instantly. If water flows from
- 11:13Speaker 2: the north to the south, Image one passes that boundary
- 11:16Speaker 2: data directly to image too.
- 11:17Speaker 1: Turns a week long simulation into.
- 11:19Speaker 2: An afternoon job. It truly reinforces that shark idea. It's
- 11:22Speaker 2: designed to eat massive data sets.
- 11:24Speaker 1: But we do have to look at the skeletons in
- 11:28Speaker 1: the closet. Chapter eight.
- 11:30Speaker 2: Yeah, the legacy features.
- 11:31Speaker 1: Because the reality is a lot of our listeners might
- 11:34Speaker 1: get hired to work on code that was written before
- 11:36Speaker 1: they were born. Chapman lists these features that are now
- 11:40Speaker 1: considered obsolescent.
- 11:42Speaker 2: Obsolescent is a very polite way of saying kill it
- 11:44Speaker 2: with fire.
- 11:45Speaker 1: So what are the big offenders we should look out for?
- 11:48Speaker 2: The blow too statement is number one. This is the
- 11:51Speaker 2: root of all spaghetti code. You're reading along trying to
- 11:54Speaker 2: follow the logic, and suddenly the code says jogo too
- 11:58Speaker 2: one hundred and you're jump back five pages. It's unreadable.
- 12:02Speaker 1: What about the arithmetic? If I've seen that in old manuals,
- 12:05Speaker 1: it looks like a logic puzzle designed just to confuse you.
- 12:07Speaker 2: It is. It splits the code three different ways based
- 12:10Speaker 2: on whether a number is negative, zero, or positive. Trying
- 12:14Speaker 2: to debug that logic thirty years after it was written
- 12:16Speaker 2: is a complete nightmare.
- 12:17Speaker 1: So the main takeaway here is if you inherit code
- 12:20Speaker 1: with this stuff in it, you need to understand it,
- 12:23Speaker 1: but never ever write it in new code.
- 12:25Speaker 2: Please don't. You have to remember that code is read
- 12:28Speaker 2: by humans far more often than it is written by them.
- 12:31Speaker 2: You're always coding for the next engineer.
- 12:33Speaker 1: So bringing this all together, then we've gone from a
- 12:36Speaker 1: single line of code implicit none all the way to
- 12:41Speaker 1: massive parallel processing with coraise. What's the big picture for
- 12:45Speaker 1: someone listening today, The summary.
- 12:47Speaker 2: Is that modern fortran is a highly specialized tool. You're
- 12:50Speaker 2: not going to build an iPhone app with it. It's
- 12:53Speaker 2: for heavy math, heavy engineering. It is structured, its modular,
- 12:57Speaker 2: and it is incredibly incredibly fast at what it does.
- 13:00Speaker 1: It's kind of a backstage pass to the world's most
- 13:02Speaker 1: powerful supercomputing applications.
- 13:04Speaker 2: It really is. If you want to solve the world's
- 13:06Speaker 2: biggest physics problems, this is the native language that those
- 13:10Speaker 2: problems are solved in.
- 13:11Speaker 1: Before we wrap up, we want to leave you with
- 13:13Speaker 1: a little challenge, a scenario to test if you've really
- 13:15Speaker 1: absorbed Chapman's philosophy.
- 13:17Speaker 2: Okay, here it is. Imagine you are designing a simulation
- 13:20Speaker 2: for water flow in a complex pipe network.
- 13:22Speaker 1: We want you to think about and explain three things. First,
- 13:26Speaker 1: why would you use a module to define your physical
- 13:28Speaker 1: constants like gravity and water viscosity?
- 13:32Speaker 2: Second, why would you use alloctable arrays for the different
- 13:35Speaker 2: pipe segments instead of just hard coding them to a
- 13:38Speaker 2: maximum size.
- 13:39Speaker 1: And finally, how does that one little line implicit nune
- 13:43Speaker 1: protect your entire simulation from that Godzilla bug we talked
- 13:47Speaker 1: about earlier.
- 13:48Speaker 2: Think about the difference between the compiler catching a typo
- 13:51Speaker 2: versus your simulation just silently failing.
- 13:55Speaker 1: Mull that over and remember, good code isn't just about
- 13:58Speaker 1: getting the right answer today. It's about making sure the
- 14:00Speaker 1: engineer who has to read your code in ten years
- 14:03Speaker 1: can understand how.
- 14:04Speaker 2: You got there, because that engineer might just be you.
- 14:07Speaker 1: Thanks for listening to the deep dive. We'll catch you
- 14:09Speaker 1: on the next one.