FORTRAN (Lab 2)
Goals
- To learn how to write FORTRAN programs to solve scientific problems.
Introductory Lecture. old PDF Copy of PowerPoint Lecture for Fortran week 2 .
revised 2024: pdf copy of PowerPoint Lecture for Fortran - Part 2.
Handouts (paper or electronic)
Activities in Lab
- creating and using linux aliases.
- add modules to share data .
- writing output files .
- interpret compiler error messages .
- continue learning how to debug code.
Extra Info
- Precision: Lecture slides on binary numbers, and program (double.f95) that you can compile and run to learn about single, double, and triple precision numbers in Fortran.
- Namelists: These are used extensively in large NWP models such as WRF. Here is a short demo program (testnamelist2.f95) that you can compile and run, which uses the following input files: inputfile.txt and inputfile2.txt .
- Pointers and Structures: You can compile and run this short demo program (stullist-v3.f95) which uses the following input file (snowlayerinput.txt). It creates a doubly linked list, using pointers, to keep track of layers of snow, each having their own attributes that were read in from the input file. A much more complete and sophisticated snow model using this approach was used by Dr. Rosie Howard in her model for piste conditions for the 2010 Winter Olympics (Howard & Stull, 2014). Pointers in Fortran are more intuitive than in C.
This demo also shows an example of a "defined type" in Fortran, which corresponds to structures (structs) in C.
- Finally, here is a demo fortran program that creates a Skew-T diagram from text-file sounding data such as you can get from the U. Wyoming upper air website. Because plotting packages in Fortran are not as nice/friendly as for other languages, I took the easy approach to just produce an output file in .csv format , so that we could open this csv file in Excel and then use Excel to plot the lines in the skew-T. Feel free to compile and test it, using darwin.txt as input.
In the attached folder are:
- skewt14.f95 ! the fortran code that you can compile and run
- skewt14.f95.pdf ! a pdf copy of the fortran code, with line numbers, for easy viewing
- darwin.txt ! the input file that you specify when you run the skewt program
- darwinout.csv ! a copy of the output file produced by this program
- darwinout.xlsx ! after reading the csv file into excel, and telling excel to plot
everything in the first two columns starting in row 12
- darwin-skewt-plot-excel.pdf ! a sample of the plot produced, for easy viewing.
whatever you prefer for your skew-T.
Note that excel initially makes all the lines the same colour. So you need to modify the colours in Excel to
Instructions
Homework Assignment.
Lab and Homework. You will write your fortran code in stages, a little at a
time. This is the top-down approach to programming. To
ensure that you do this, I ask that you give a different name to each
version of your code. You must get each version of the code
compiled and running, before you enhance the code to create the next
version. This way, the marker can see all the versions of your
code, can confirm that each version compiled and ran successfully, and
can mark each one separately.
Reading Assignment
- Skim chapters 6 & 8 of the following FORTRAN tutorials.
(courtesy of Stephen Brooks, Univ. of St. Andrews, Scotland. http://www-solar.mcs.st-and.ac.uk/%7Esteveb/course/course.html )
- Basics: program structure, data types, programming style, conditional statements, loops, arrays .
- Arithmetic expressions, intrinsic functions, simple input and output (I/O) .
- Characters, strings, print .
- Internal subroutines and functions .
- Modules .
- More about arrays, dynamic allocation of arrays, key words .
- More about I/O .
- Case statement .
- Read Chapter 5 on Debugging, from Kernighan and Pike "The Practice of Programming" (which is in the course material pack).
-end-
Copyright © 2012, 2024 by Roland Stull.
UBC.