|
| 1 | +--- |
| 2 | +title: 'Basics' |
| 3 | +teaching: 35 |
| 4 | +exercises: 15 |
| 5 | +--- |
| 6 | + |
| 7 | +::::::::::::::::::::::::::::::::::::: questions |
| 8 | + |
| 9 | +- How do we represent and manipulate data in Fortran? |
| 10 | + |
| 11 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
| 12 | + |
| 13 | +::::::::::::::::::::::::::::::::::::: objectives |
| 14 | + |
| 15 | +- Understand the different intrinsic data types |
| 16 | +- Understand the different intrinsic operators and functions available |
| 17 | + |
| 18 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
| 19 | + |
| 20 | +## Fortran data types |
| 21 | + |
| 22 | +Fortran provides the following intrinsic data types: |
| 23 | + |
| 24 | +* numeric types |
| 25 | + * `integer` |
| 26 | + * `real` |
| 27 | + * `complex` |
| 28 | +* non-numeric types |
| 29 | + * `logical` |
| 30 | + * `character` |
| 31 | + |
| 32 | +### Defining numeric variables |
| 33 | + |
| 34 | +The following program declares a variable with each of the three intrinsic |
| 35 | +numeric types, and provides an initial value in each case. |
| 36 | +```fortran |
| 37 | +program example1 |
| 38 | +
|
| 39 | + integer :: i = 1 ! An integer |
| 40 | + real :: a = 2.0 ! A floating point number |
| 41 | + complex :: z = (0.0, 1.0) ! A complex number as (real-part, imag-part) |
| 42 | +
|
| 43 | +end program example1 |
| 44 | +``` |
| 45 | +Initial values are optional, and these values may be changed later within the code. |
| 46 | +If a declaration does not specify an initial value, the variable is said to be _undefined_. |
| 47 | + |
| 48 | +### Variable names |
| 49 | + |
| 50 | +The valid Fortran character set for names is `a-z`, `A-Z`, `0-9` and the |
| 51 | +underscore `_`. Valid names must begin with a character. The maximum |
| 52 | +length of a name is 63 characters (introduced in the F2003 standard) with |
| 53 | +no spaces allowed. This includes names for programs, modules, subroutines, |
| 54 | +and functions, as well as names for variables. |
| 55 | + |
| 56 | +### The `implicit` statement |
| 57 | + |
| 58 | +By default, variables in Fortran do not need to be declared a specific type, they |
| 59 | +can just be used within the code. Variables with names beginning with letters `i-n` are implicitly |
| 60 | +of type `integer`, while anything else is of type `real` (unless explicitly declared otherwise). |
| 61 | + |
| 62 | +**This is very bad practice and modern Fortran should not be used in this way.** |
| 63 | + |
| 64 | +The solution to prevent errors involving undeclared variables (usually arising from |
| 65 | +typos) is to declare that no names have implicit type via the use of the |
| 66 | +```fortran |
| 67 | +implicit none |
| 68 | +``` |
| 69 | +statment at the very top of the code. |
| 70 | + |
| 71 | +With this statement inserted, all variable names must be declared explicitly before they |
| 72 | +are referenced. It is still common to see variables beginning with `i-n` as integers. |
| 73 | + |
| 74 | +::::::::::::::::::::::::::::::::::::: challenge |
| 75 | + |
| 76 | +## Use `implicit none` |
| 77 | + |
| 78 | +Edit the above example to include an `implicit none` statment, and print out the three variables |
| 79 | + |
| 80 | +:::::::::::::::: solution |
| 81 | + |
| 82 | +```fortran |
| 83 | +program solution1 |
| 84 | + implicit none |
| 85 | +
|
| 86 | + integer :: i = 1 ! An integer |
| 87 | + real :: a = 2.0 ! A floating point number |
| 88 | + complex :: z = (0.0, 1.0) ! A complex number as (real-part, imag-part) |
| 89 | +
|
| 90 | + print *, i, a, z |
| 91 | +
|
| 92 | +end program solution1 |
| 93 | +``` |
| 94 | +Compiling and running this code will give the following output |
| 95 | +``` |
| 96 | +$ ./a.out |
| 97 | + 1 2.00000000 (0.00000000,1.00000000) |
| 98 | +``` |
| 99 | + |
| 100 | +::::::::::::::::::::::::: |
| 101 | +::::::::::::::::::::::::::::::::::::::::::::::: |
| 102 | + |
| 103 | +### Parameters |
| 104 | + |
| 105 | +In the above example, it is possible to change the values of initialised variables, e.g. |
| 106 | +```fortran |
| 107 | +program example2 |
| 108 | + implicit none |
| 109 | +
|
| 110 | + real :: a = 2.0 |
| 111 | +
|
| 112 | + print *, a |
| 113 | +
|
| 114 | + a = a + 5.0 |
| 115 | +
|
| 116 | + print *, a |
| 117 | +
|
| 118 | +end program example2 |
| 119 | +``` |
| 120 | +This will then give the output: |
| 121 | +``` |
| 122 | +$ ./a.out |
| 123 | + 2.00000000 |
| 124 | + 7.00000000 |
| 125 | +``` |
| 126 | +However, you can also define constant values that do not change by defining variables using `parameter`, e.g. |
| 127 | +```fortran |
| 128 | +program example3 |
| 129 | + implicit none |
| 130 | +
|
| 131 | + real, parameter :: pi = 3.14159265 |
| 132 | +
|
| 133 | +end program example3 |
| 134 | +``` |
| 135 | + |
| 136 | +::::::::::::::::::::::::::::::::::::: challenge |
| 137 | + |
| 138 | +## Write a Fortran program to calculate the circumference of a circle |
| 139 | + |
| 140 | +Using the `parameter` statement, write a Fortran program to calculate the circumference of a circle of |
| 141 | +radius 3.0. |
| 142 | + |
| 143 | +::::::::::::::::: hint |
| 144 | + |
| 145 | +The cicumference $c$ of a circle can be calculated from the radius $r$ and constant $\pi$ by |
| 146 | + |
| 147 | +$c = 2 \pi r$ |
| 148 | + |
| 149 | +:::::::::::::::::::::: |
| 150 | + |
| 151 | +:::::::::::::::: solution |
| 152 | + |
| 153 | +```fortran |
| 154 | +program solution2 |
| 155 | + implicit none |
| 156 | +
|
| 157 | + real, parameter :: pi = 3.14159265 |
| 158 | +
|
| 159 | + real :: r=3.0 ! radius of the circle |
| 160 | + real :: c ! circumference of the circle |
| 161 | +
|
| 162 | + c = 2.0 * pi * r |
| 163 | +
|
| 164 | + print *, c |
| 165 | +
|
| 166 | +end program solution2 |
| 167 | +``` |
| 168 | +Compiling and running this code will give the following output |
| 169 | +``` |
| 170 | +$ ./a.out |
| 171 | + 18.8495560 |
| 172 | +``` |
| 173 | + |
| 174 | +::::::::::::::::::::::::: |
| 175 | +::::::::::::::::::::::::::::::::::::::::::::::: |
| 176 | + |
| 177 | +## Logical variables |
| 178 | + |
| 179 | +Fortran has a `logical` type that has two literal values, True and False, defined by |
| 180 | +```fortran |
| 181 | + logical :: switch0 = .false. |
| 182 | + logical :: switch1 = .true. |
| 183 | +``` |
| 184 | + |
| 185 | +### Logical operators and expressions |
| 186 | + |
| 187 | +Values can be tested logical operators `.or.`, `.and.` and `.not.` are available, and |
| 188 | +these can be used to set the value of logical variables. |
| 189 | + |
| 190 | +The precedence is illustrated by, e.g., |
| 191 | +```fortran |
| 192 | + q = i .or. j .and. .not. k ! evaluated as i .or. (j .and. (.not. k)) |
| 193 | +``` |
| 194 | +where q, i, j, and k are all logical variables. |
| 195 | + |
| 196 | +The use of parentheses is highly recommended to avoid ambiguity and/or to add clarity. |
| 197 | + |
| 198 | +### Relational operators |
| 199 | + |
| 200 | +To form logical expressions from numeric or other expressions, we require |
| 201 | +relational operators. The are two forms in Fortran, illustrated in the table |
| 202 | +below. It is recommended that you avoid the older form. |
| 203 | + |
| 204 | +| Relation | Operator | Older form | For | |
| 205 | +|--------------------------|----------|------------|------------------| |
| 206 | +| Less than | `< ` | `.lt.` | `integer` `real` | |
| 207 | +| Less than or equal to | `<=` | `.le.` | `integer` `real` | |
| 208 | +| Greater than | `> ` | `.gt.` | `integer` `real` | |
| 209 | +| Greater than or equal to | `>=` | `.ge.` | `integer` `real` | |
| 210 | +| Equal to | `==` | `.eq.` | `integer` `real` `complex`| |
| 211 | +| Not equal to | `/=` | `.neq.` | `integer` `real` `complex`| |
| 212 | + |
| 213 | +### Logical equivalence |
| 214 | + |
| 215 | +Equivalence between two logical expressions or variables is established |
| 216 | +via the logical operators `.eqv.` and `.neqv.`. |
| 217 | + |
| 218 | +While some some compilers may allow the use of `==`, this should be avoided. |
| 219 | + |
| 220 | +### Using logical operators |
| 221 | + |
| 222 | +These operators can be used to check and set the values of logical variables, dependent on other variables, e.g. |
| 223 | +```fortran |
| 224 | +program example4 |
| 225 | + implicit none |
| 226 | +
|
| 227 | + real, parameter :: pi = 3.14159265 |
| 228 | + logical, parameter :: switch1 = .true. |
| 229 | +
|
| 230 | + real :: a=3.0 |
| 231 | + logical :: test1, test2, test3 |
| 232 | +
|
| 233 | + test1 = a >= pi ! True if a is greater than or equal to pi |
| 234 | +
|
| 235 | + test2 = (.not. test1) ! True if test1 is False, False if test1 is True |
| 236 | +
|
| 237 | + test3 = (test2 .eqv. switch1) ! True if test2 is True, False if test2 is False |
| 238 | +
|
| 239 | + print *, test1 |
| 240 | +
|
| 241 | + print *, test2 |
| 242 | +
|
| 243 | + print *, test3 |
| 244 | +
|
| 245 | +end program example4 |
| 246 | +``` |
| 247 | +Compiling and running this code will give the following output |
| 248 | +``` |
| 249 | +$ ./a.out |
| 250 | + F |
| 251 | + T |
| 252 | + T |
| 253 | +``` |
| 254 | + |
| 255 | +## Character variables |
| 256 | + |
| 257 | +Character variables are strings that hold zero or more characters. Some examples are: |
| 258 | +```fortran |
| 259 | +program example5 |
| 260 | + implicit none |
| 261 | +
|
| 262 | + character (len = *), parameter :: string1 = "don't" ! assumed length of 5 characters |
| 263 | + character (len = 5) :: string2 = "Don""t" ! 5 characters |
| 264 | + character (len = 6) :: string3 = ' don''t' ! blank + 5 characters |
| 265 | +
|
| 266 | +end program example5 |
| 267 | +``` |
| 268 | +There should be a `len` specifier. Specifying `len = *` means that the length will be assumed |
| 269 | +from the length of the string. A space is a valid character in a string. |
| 270 | + |
| 271 | +Strings must be defined within quotation marks, either single, `'`, or double, `"`, beginning |
| 272 | +and ending with the same type of mark. To define a quotation mark within a string, either |
| 273 | +use one of the alternative type, e.g. `"'"`, or use two of the same type, e.g. `''''`, which |
| 274 | +will be intepreted as a single mark within the string. |
| 275 | + |
| 276 | +Strings may be concatenated with the string concatenation operator `//`. For example `string2` |
| 277 | +and `string3` could be concatenated into a new variable `string4` by |
| 278 | +```fortran |
| 279 | + string4 = string2//string3 |
| 280 | +``` |
| 281 | +A single character, or a subset of characters, can be extracted via |
| 282 | +use of an array-index like notation by defining the start and end characters |
| 283 | +to extract, e.g. `string1(1:1)` would extract only the first character from the string |
| 284 | +variable `string1`. |
| 285 | + |
| 286 | +::::::::::::::::::::::::::::::::::::: challenge |
| 287 | + |
| 288 | +## Working with strings |
| 289 | + |
| 290 | +Using the above `example5`, concatenate `string2` and `string3` into a new variable `string4`, |
| 291 | +and print out the values of all strings. Also, print the third character of `string4`. |
| 292 | + |
| 293 | +:::::::::::::::: solution |
| 294 | + |
| 295 | +```fortran |
| 296 | +program solution3 |
| 297 | + implicit none |
| 298 | +
|
| 299 | + character (len = *), parameter :: string1 = "don't" ! 5 characters |
| 300 | + character (len = 5) :: string2 = "Don""t" ! 5 characters |
| 301 | + character (len = 6) :: string3 = ' don''t' ! blank + 5 characters |
| 302 | + character (len = 11) :: string4 ! length of string2+string3 |
| 303 | +
|
| 304 | + string4 = string2//string3 |
| 305 | +
|
| 306 | + print *, string1 |
| 307 | +
|
| 308 | + print *, string2 |
| 309 | +
|
| 310 | + print *, string3 |
| 311 | +
|
| 312 | + print *, string4 |
| 313 | +
|
| 314 | + print *, string4(3:3) |
| 315 | +
|
| 316 | +end program solution3 |
| 317 | +``` |
| 318 | +Compiling and running this code will give the following output |
| 319 | +``` |
| 320 | +$ ./a.out |
| 321 | + don't |
| 322 | + Don"t |
| 323 | + don't |
| 324 | + Don"t don't |
| 325 | + n |
| 326 | +``` |
| 327 | + |
| 328 | +::::::::::::::::::::::::: |
| 329 | +::::::::::::::::::::::::::::::::::::::::::::::: |
| 330 | + |
0 commit comments