Computational Astrophysics AS 3013 Lecture 2: 1) F90 variable types 2) variable declaration 3) good programming style AS3013: F90 lecture2 1
Fortran 90 variable types integer whole numbers: 3, 244, -10, default is integer*4 (compiler-dependent!) 4 bytes = 32 bits => ±(2 31-1) = -2147483647 +2147483647 otherwise overflow you can also use integer*2, or integer*8 real floating point numbers: 0.0333, 0.0, -5.421, 3.56e+8, default is real*4 = single precision which gives about 7 significant digits, and exponents -38 +38 otherwise overflow or underflow AS3013: F90 lecture2 2
Fortran 90 variable types double and quadrupole precision you can switch to real*8 = double precision e.g. 0.d0, 1.2d+0, 3.756793451752398d-156 about 15 significant digits, and exponents -308 +308 real*8 constants are specified by d or D or _8 otherwise overflow or underflow somewhat slower (on 32-bit architecture) you can switch to real*16 = quadrupole precision real*16 constants are specified by q or Q or _16 real :: pi1 real*8 :: pi2 real*16 :: pi3 pi1 = ACOS(-1.0) pi2 = ACOS(-1.d0) pi3 = ACOS(-1.q0) print*,pi1 print*,pi2 print*,pi3 3.141593 3.14159265358979 AS3013: F90 lecture2 3.14159265358979323846264338327950 3
Fortran 90 variable types complex two single precision floating point numbers (Re,Im), c=re+i*im very convenient, intrinsic numerical functions for complex numbers you can switch to complex*8, or complex*16 complex :: const const = (-1.0,0.0) print *,const, print *,SQRT(const) ( -1.0000000, 0.0000000 ) ( 0.0000000, 1.0000000 ) AS3013: F90 lecture2 4
character Fortran 90 variable types a string of characters with maximum length N: character(len=n) all ASCII characters allowed, e.g. 'A', '...', 'Peter', 'signal/noise' logical character(len=20) :: YourName print *, "What's your name?" read *, YourName print *, "Hello "//trim(yourname)//", how are you today?" What's your name? Peter Hello Peter, how are you today? a logical value, can only be.true. or.false. often the result of a logical expression logical :: larger real :: a,b larger = (a>b) if (larger) then... AS3013: F90 lecture2 5
Be careful when combining variables of different types expressions containing different types of variables are a frequent source of errors and inaccuracies depending on the Compiler, automatic type conversions will be performed, sometimes with quite unexpected results use your own type conversions to say explicitly what you want real() real*4 dble() real*8 int() integer integer powers are OK though, and actually recommended program TEST implicit none real*8 :: pi=acos(-1.d0) real*16 :: pi16=acos(-1.q0) character(len=4) :: name='peter' print *, '1: ',name print *, '2: ',2/3*pi print *, '3: ',2*pi/3 print *, '4: ',2.0/3.0*pi print *, '5: ',2.d0/3.d0*pi print *, '6: ',2.q0/3.q0*pi print *, '7: ',2.q0/3.q0*pi16 print *, '8: ',int(pi) end program TEST AS3013: F90 lecture2 6
program F90 units the main program, execution starts here there may only be one program-unit subroutine a general block of code can be called from anywhere may have an arbitrary number of input and output variables function a general block of code may have any number of input, but only one output variable module a list of variables in a block of memory (selected) access can be granted in any program / subroutine / function by the use command AS3013: F90 lecture2 7
general structure of F90 units module NATURE real*8,parameter :: cl=2.9979245800d+08! [m/s] real*8,parameter :: bk=1.3806581200d-23! [J/K] real*8,parameter :: hplanck = 6.62607554d-34! [J s] real*8,parameter :: nm=1.d-9! 1nm [m] end module NATURE program MYPROG use NATURE,ONLY: cl,nm implicit none real*8 :: Temp,freq,lambda,Bnu,BPLANCK read *,Temp,lambda freq = cl/(lambda*nm) Bnu = BPLANCK(Temp,freq) end program MYPROG real*8 function BPLANCK(T,nu) use NATURE,ONLY: hplanck,cl,bk implicit none real*8,intent(in) :: T,nu real*8 :: x x = hplanck*nu/(bk*t) BPLANCK = 2.d0*hplanck*nu**3/cl**2 / (EXP(x)-1.d0) end function BPLANCK unit starts... } variable declaration unit ends unit starts... variable declaration executable statements unit ends unit starts... variable declaration executable statements unit ends AS3013: F90 lecture2 8
type variable declaration type, modifier1, modifier2,... :: name1, name2,... basic type of a variable, for example integer, real, double precision, logical, character modifier parameter unchangable constant dimension(3) three of them, e.g. for vectors external function to be passed in argument list allocatable decide about dimensions later intent(in), intent(out), intent(inout) kind of variable in argument list there are more modifiers name up to 31 characters allowed: A-Z, a-z, 0-9, and '_' first character must be a letter, for example speed_0 Fortran does not distinguish between upper and lower case letters AS3013: F90 lecture2 9
variable declaration all variables used in a unit must be declared declarations take effect only in the local unit for example, variable names declared in the main program are unknown / undefined in a subroutine implicit none switch off old-fashioned and dangerous implicit declaration use this command at the start of each Fortran unit! use MyModule,ONLY: name1, name2,... grant access to selected variables in a module very convenient AS3013: F90 lecture2 10
Programming today's astronomy is unthinkable without computers developing your programming skills will be crucial for your career in astronomy skills required: 1. critical reading what's the requested functionality? 2. analytical thinking (take it apart into simple tasks...) 3. innovative solutions for the simple tasks 4. creative synthesis (... and put it back together!) 5. attention to detail AS3013: F90 lecture2 11
Programming Tips keep it simple: simple is beautiful...... and more likely to be correct - complexity leads to errors clear code structure: meaningful variable names, well-defined short sub-units (this will be assessed in the exercises!) include comments & headers... while writing the program: comments, headers, units (this will be assessed in the exercises!) often better than external manual think before programming will minimise your debugging time base code on what is required to reach goal not on what makes an elegant code AS3013: F90 lecture2 12
How to write a program? 1. analyse the problem what are the basic tasks, functionalities, goals? which physical assumptions, approximations? 2. decide on overall code structure break up the problem into smaller and smaller sub-units decide on algorithms, draw a flow chart! names and structure of variables 3. ask for advice, early! anyone solved similar problems before? are there alternatives? (ready-to-use codes? better algorithms?) 4. code it, and test it!!! debug small units first most important parts of code writing! most time spent here! AS3013: F90 lecture2 13