Capstone: 1D heat equation

Contents
The physics in five lines
Explicit scheme
Time stepping
Write CSV for plotting

The physics in five lines

A hot rod cools by spreading heat to neighbors. Temperature changes with time as heat flows from warm cells to cool cells.

Ends stay fixed at zero for this capstone. The middle starts warm and flattens step by step.

program heat_spec implicit none integer, parameter :: n = 11 real, parameter :: r = 0.4 print *, "n =", n, "r =", r end program heat_spec

Explicit scheme

The explicit update builds the next value from the current neighbors only. No equation system is solved.

Each interior point becomes the old value plus r times the neighbors minus twice the center. Keep r below one half for stability.

program scheme_demo implicit none real :: u(5) real :: v(5) real, parameter :: r = 0.4 integer :: i u = [0.0, 1.0, 1.0, 1.0, 0.0] do i = 2, 4 v(i) = u(i) + r * (u(i - 1) - 2.0 * u(i) + u(i + 1)) end do print *, v(2), v(3), v(4) end program scheme_demo

Time stepping

Time stepping repeats the explicit update and swaps old and new arrays after each pass.

Boundaries stay fixed while interior points relax. A few steps already show the peak sinking.

program step_demo implicit none integer, parameter :: n = 7 real :: u(n) real :: v(n) integer :: i integer :: k u = 0.0 u(3:5) = 1.0 do k = 1, 5 v = u do i = 2, n - 1 v(i) = u(i) + 0.4 * (u(i - 1) - 2.0 * u(i) + u(i + 1)) end do u = v end do print *, u end program step_demo

Write CSV for plotting

Numbers on screen are hard to read, so write position and temperature rows to a CSV file.

Any spreadsheet or plotting tool can open the file and draw the cooling curve.

program write_csv implicit none real :: u(5) integer :: i integer :: ufile u = [0.0, 0.8, 1.0, 0.8, 0.0] open(newunit=ufile, file="heat.csv", status="replace") do i = 1, 5 write(ufile, *) i, ",", u(i) end do close(ufile) end program write_csv

Next: Style guide

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: