Introduction to Fortran

Fortran and CESM » Namelists

We mentioned namelists before, and now is our chance to discuss them. Fortran is a compiled programming language. This means all of your code is directly built into a machine language. This is one of the reasons that Fortran is faster than some other languages. However, once your program is compiled, if you change any code, you will have to recompile the entire program, which could take time for large code. When you don’t want to have to recompile code all the time, you can write your initial code to have choices built in to it that can be controlled by what is called a namelist.

Let’s give you an example because this might be hard to think about without one. Let’s assume we have some Fortran code that is written for BOTH Fahrenheit and Celsius, but you only want to output in one of those temperature scales.

 if (namelist_Fahrenheit .EQ. .TRUE.) then      temp = ...      expressions else       temp = ...      expressions end if  print *, temp 

After compiling this code into test_code.exe, you would have to use a namelist file to control whether your output was in Fahrenheit or Celsius. You would do that with a command run from the Unix command line like so:

 test_code.exe < namelist.in 

The contents of namelist.in would look something like:

 & namelist.in       namelist_Fahrenheit = .TRUE.       namelist_start_hour = 12       namelist_pi = 3.1415       namelist_output_filename = ‘output.nc’ / 

In this example we started all of the variable names with “namelist_”, but this is not a requirement.

Note that the namelist variables don’t need to be declared. They are declared by the syntax used to set them.

  • Logicals are set to “.TRUE.” or “.FALSE.” (without the quotes)
  • Integers are set to numeric values without decimal places
  • Reals are set to numeric values with decimal places
  • Characters are set to strings wrapped in single quotes like ‘output.nc‘

These namelist variables decide how the compiled code is then run, which saves us a lot of time compiling. In the CESM model, namelists are used to help define the input and output files, the time steps, parameterizations choices, and other key characteristics for running the model.