Fortran syntax Chapter 1 Fortran Basics

Posted by C_Calav on Thu, 06 Jan 2022 02:34:30 +0100


Fortran naming habits: Fortran reserves uppercase for word and constant names, lowercase for variable names and procedure names, and underline separation between multiple words.

(1) Note:!
(2) Power:**
(3) Continuation:&
(4) Fortran is a case insensitive language.
(5)“;” Can be used to separate different statements in a row of statements.
(8) For convenience, the contents in [] will be specified as optional contents in the future.
(6) Free format: in F90 is the program file extension.
(7) Fixed format: in F or FOR is the program file extension.
(8) Add a number from 1 to 99999 before the statement to indicate the statement label of the statement.

The first asterisk of write(*, *) represents the output position. The default value is the screen. The unit position of the screen is 6, so * can also be written as 6. The second asterisk indicates that the output format is not specially set, so the statement in line 3 can be written in the following forms:

write(unit=*,fmt=*) "Hello,Fortran!"
write(6,*) "Hello,Fortran!"
write(unit=6,fmt=*) "Hello,Fortran!"

By the way, the difference between print and write is that print lacks the first asterisk, that is, the ability of output position. It can only output on the screen, so write is usually used. In addition, the write statement will wrap automatically after execution. The following is the general structure of Fortran program:

[program Program name]
    [Declaration part]
    [Executive part]
end [program [Program name]]

(1.2) data type

(1) Operation between numeric data and non numeric data is not allowed.
(2) The result of dividing two integers is still an integer. For example, the result of 1 / 2 is 0 instead of 0.5.
(3) The automatic type conversion rule of the system is to convert low-precision types to high-precision types.
(4) The conversion of data type is carried out from left to right, and the operation is carried out while converting.
(5) Integer data is faster than real data.
(6) Since type conversion takes time, mixed operations should be avoided as much as possible.

integer( [ kind = ] n ) i !integer
nTable number rangenTable number range
1 [ − 2 7 , 2 7 − 1 ] [-2^7,2^7-1] [−27,27−1]2 [ − 2 15 , 2 15 − 1 ] [-2^{15},2^{15}-1] [−215,215−1]
4 [ − 2 31 , 2 31 − 1 ] [-2^{31},2^{31}-1] [−231,231−1]8 [ − 2 63 , 2 63 − 1 ] [-2^{63},2^{63}-1] [−263,263−1]
real( [ kind = ] n ) r !Real type

(1) n takes 4 or 8. The default value is 4. When taking 8, it can be written as double precision.
(2) Decimals without integer or decimal parts are allowed, such as 314 02.
(3) e and d cannot be non integers. e can be preceded by integer constants or basic real constants.
(4) e+02 does not mean 100, but should be written as 1e+02.

complex( [ kind = ] n ) c !Plural form

(1) n can be 4 or 8, and the default value is 8.
(2) The representation format of complex constants is: (r,i)
(3) The type parameter of complex constant takes the maximum value of the type parameter of real number (excluding integer) of real part and imaginary part.

character [ ( [len = ] l) ] c !character(strand)type

(1) Character (string) constants use English single quotation marks or double quotation marks as delimiters.
(2) An English letter takes up one character and a Chinese character takes up two characters.
(3) If the string contains single / double quotation marks, it should be represented by two consecutive single / double quotation marks.

char(num) !Returns an integer num Character represented by
ichar(char) !Return character char Represents an integer number
len(string) !Return string string Declaration length of
len_trim(string) !Return string string The actual length after removing trailing spaces
program main
    implicit none
    character(10) :: string1 = "Good "
    character(10) :: string2 = "morning!"
    character(20) :: string
    string = string1//string2 ! Connect two strings
    write(*,*) string !Good□□□□□□morning!
    string(6:) = "evening!" !Resets the string after the 6th character
    write(*,*) string !Good□evening!
    string(1:2) = "go" !Change the first two characters of the string to go
    write(*,*) string !good□evening!
    string(13:13) = "." !Change the 13th and characters to“."
    write(*,*) string !good□evening.
end program main
logical( [ kind = ] n ) l !Logical type

(1) n can take 1, 2, 4 and 8, and the default value is 4.
(2) Logical constants can only be true. (logical truth) or false. (logical false).
(3) When displaying a logical variable, t represents logical true and f represents logical false.

Derived data type refers to a new data type created by users using the combination of internal types in Fortran system, such as integer, real, complex, logical, character, etc. they are actually a structure formed by internal data types.

!Creation of derived data types
type [,Access attribute description :: ] Derived data type name
    Member type description
end type [Derived data type name]
!Declaration of derived data types
type(Derived data type name) [::] Variable name

(1) Type block can only have type declaration statements, not executable statements.
(2) The access attribute description keyword can be public or private. The default is public.
(3) The private attribute cannot be used if it is not a derived type defined within the module.
(4) Derived data types can also be nested.
(5) Members of derived data types use% or quote.

!Member initialization of derived data types
type worker
    character(20) name
    integer age
end type
!First member initialization method
type(worker) :: w = "Aiden"
w.age = 21
!The second member initialization method
type(worker) :: w = worker("Aiden", 21)
!The third member initialization method
type(worker) :: w
w = worker("Aiden",21)
!The fourth member initialization method
type(worker) w
data w /worker("Aiden",21)/
!The fifth member initialization method
type(worker) w
data,w.age /"Aiden",21/

(1.3) data input

integer i
read(*,*) i

To enter multiple data at the same time, separate the data with commas, spaces and enter. The first asterisk of the read command represents the default device, that is, the keyboard. The position is 5, and the second asterisk represents the input format. Therefore, the second line of statements can be written as follows:

read(unit=*,fmt=*) i
read(5,*) i
read(unit=5,fmt=*) i

(1.4) data output

!The first way to write
write(*,"('result=',I3)") 10
!The second way to write
write(*,100) 10
100 format("result=",I3)
!The third way to write
write(*,100) 10
100 format(7Hresult=,I3)
!The fourth way to write
character(16) :: string = "('result=',I3)"
write(*,string) 10
!Integer output
!Iw[.m]: with w Character width to output an integer, at least m Number
write(*,"(I5)") 100 !□□100
write(*,"(I3)") 10000 !***
write(*,"(I5.3)") 10 !□□010
!Real output
!Fw.d: with w A character text border is used to output floating-point numbers, with the decimal part accounting for d Characters wide
write(*,"(F9.3)") 123.45 !□□123.450
!String output
!Aw: with w Characters wide to output a string
write(*,"(A10)") "Hello" !□□□□□Hello
write(*,"(A3)") "Hello" !Hel
!Logical output
!Lw: with w Characters wide to output T or F
write(*,"(L3)") .true. !□□T
!Scientific counting output
!Ew.d[Ee],Dw.d: Using scientific counting method to w Characters wide to output floating-point numbers, with the decimal part accounting for d Characters wide, exponential part output at least e Characters wide
write(*,"(E15.7)") 123.45 !□□0.1234500E+03
write(*,"(E9.2E3)") 12.34 !0.12E+002
!Move output position left and right
write(*,"(2X,I3)") 100 !First move two units to the right and then output,□□100
write(*,"(TR3,I3)") 100 !First move three units to the right and then output,□□□100
write(*,"(A10,TL3,I3)") "Call 119",110 !□□Call□110
!Specify the move output location
!Tc: Move the output position to the second line of the line c Bytes
write(*,"(T3,I3)") 100 !□□100
!Wrap output
write(*,"(I3/I3)") 10,10
!Output plus sign
!SP,SS: Yes SP After that, when outputting numbers, if the value is regular plus a positive sign, SS Is used to cancel SP function
write(*,"(SP,I2,I2,SS,I2)") 1,2,3 !+1+2□3
!Special binary output
Bw[.m]: Binary output
Ow[.m]: Octal output
Zw[.m]: Hexadecimal output
!3(1X,F5.2)Represents the same output format "1" repeated 3 times X,F5.2"
real :: a = 1.0,b = 2.0,c = 3.0
write(*,"( 3(1X,F5.2) )") a,b,c

If you want to read Hello, □ world, you must set the reading format to A12, because there are commas and spaces in this string.

(1.5) declaration and assignment of parameters

data type [ [ , attribute ] :: ] parameter list

(1) Fortran's declaration statement must be placed before the execution statement, which is different from C and other languages.
(2) When a property exists or is initialized while a variable is declared, the double colon cannot be missing.

There is a bad function in Fortran standard. Its variables do not have to be declared by the program before they can be used. The compiler will automatically determine the type of the variable according to the first letter of the variable name: variables with the first letter of I, J, K, L, M and N will be regarded as integer types, and other variables will be used as floating-point numbers.

implicit integer(A,B,C) !with A,B,C Variables that begin with are treated as integers
implicit integer(A-F,I,K) !with A reach F,I,K Variables that begin with are treated as integers
implicit none !Turn off the default type function. All variables must be declared in advance and must be written in use After statement
real,parameter :: pi = 3.14159 !declare constant  pi = 3.14159
!Batch assignment
integer a
real b
complex c
character(10) string
!data The statement is part of the declaration and can only be placed before the statement is executed
data a,b,c,string /1,2.0,(1.0,2.0),"Hello"/

Equivalence declaration can take more than two variables and declare that they use the same memory address. If you use variables in the same memory location, as long as you change one of them, you will change the values of other variables at the same time. Equivalence declarations can save memory and streamline code.

program main
    implicit none
    integer :: a = 1,b
    write(*,*) a,b !a and b All values are 1
    b = 2
    write(*,*) a,b !a and b All values change to 2
end program main

ginseng Test Endowment material come source Reference sources Reference sources

  1. Engineering analysis program design Chen Bin, Zhou qulan Xi'an Jiaotong University Press
  2. FORTRAN 95 programming Peng Guolun China Electric Power Press
  3. Fortran programming (Fourth Edition) Stephen J.Chapman. China Electric Power Press

Bo passenger create do : A i d e n   L e e Blog creation: Aiden\ Lee Blog creation: Aiden Lee
Special statement: the article is for learning reference only. Please indicate the source for reprint. Embezzlement is strictly prohibited!

Topics: fortran