Linker IT Software
Google
Web www.oraxcel.com
menubar-top-links menubar-top-rechts
Home Help Search Login
Welcome, Guest. Please Login.
SQL*XL: Database to Excel bridge litLIB: Excel power functions pack ExcelLock: Locking and securing your valuable Excel spreadsheets encOffice: Protect your Excel file easy and safe encOffice: Protect your Excel file easy and safe
Pages: 1
Fortran95 (Read 19437 times)
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
Fortran95
18.04.10 at 22:14:49
 
Fortran95
 
A hello world program in fortran95:
 
Create the file hello.f95 and add the following text:
Code:
program hello
print *,"Hello world"
end program hello 


 
Compile the code with the command:
f95 hello.f95 -o hello
Back to top
 
 

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
Fortran95 show file contents
Reply #1 - 18.04.10 at 22:52:09
 
Fortran95 show file contents
 
Code:
program print_file
  CHARACTER*100 str
  OPEN(UNIT=1, FILE="inp",FORM="FORMATTED",STATUS="OLD",ACTION="READ")
  DO
    READ(UNIT=1,FMT="(A)",END=100) str
    PRINT *,str
  END DO
  100 CLOSE(UNIT=1)
end program print_file 


 
Define buffer str as 100 characters (assuming a line size smaller than 100 characters)
Open the text file inp for formatted read
Loop until the read statement fails. If it does jump to statement 100
Print the read string.
Statement 100 is to close the file.
Back to top
 
 

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
Fortran95: copy file
Reply #2 - 19.04.10 at 21:33:39
 
Fortran95: copy file
 
The following code will copy a file. It reads from the file inp and writes to the file new.
 
Code:
program copy_file
  CHARACTER*100 str
  OPEN(UNIT=1, FILE="inp",FORM="FORMATTED",STATUS="OLD",ACTION="READ")
  OPEN(UNIT=2, FILE="new",FORM="FORMATTED",STATUS="NEW",ACTION="WRITE")
  DO
    READ(UNIT=1,FMT="(A)",END=100) str
    WRITE(UNIT=2,FMT="(A)") str
  END DO
  100 CLOSE(UNIT=1)
  CLOSE(UNIT=2)
end program copy_file
 

Back to top
 
 

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
Fortran95: Comments, program arguments
Reply #3 - 19.04.10 at 21:43:57
 
Fortran95: Comments, program arguments
 
Comments in Fortran start with an exclamation mark.
Program arguments can be retrieved using the getarg function.
 
Example. The copy program was changed to include comments and to read the input and output file from the command line:
 
Code:
! Usage:
! copy_file source target
program copy_file
  ! Variable declaration
  CHARACTER*100 str	! String buffer for reading and writing
  CHARACTER*100 infile   ! Input file
  CHARACTER*100 outfile  ! Output file
  ! Get the program parameters
  CALL getarg(1,infile)
  CALL getarg(2,outfile)
  ! Open the files
  OPEN(UNIT=1, FILE=infile ,FORM="FORMATTED",STATUS="OLD",ACTION="READ")
  OPEN(UNIT=2, FILE=outfile,FORM="FORMATTED",STATUS="NEW",ACTION="WRITE")
  ! Loop reading the input file line by line
  DO
    READ(UNIT=1,FMT="(A)",END=100) str
    WRITE(UNIT=2,FMT="(A)") str
  END DO
  ! All done, now close the files
  100 CLOSE(UNIT=1)
  CLOSE(UNIT=2)
end program copy_file
 


 
Columns 1 to 5 can also be used for labels. Comments are indicated by a c or a * character.
Back to top
 
« Last Edit: 11.10.12 at 13:34:35 by Gerrit-Jan Linker »  

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
Fortran95: If statements and substrings
Reply #4 - 19.04.10 at 22:05:39
 
Fortran95: If statements and substrings
 
If statements in Fortran have the following syntax:
 
if (condition) then
  ...
endif
 
A substring can be retrieved using the following syntax:
CHARACTER*100 str
str(1:5)   ! the first 5 characters in str
 
Note that str(2:5) will return the substring from character 2 to character 5. The indices are absolute indices in the character 'array'.
 
Example:
Code:
    READ(UNIT=1,FMT="(A)",END=100) str
    WRITE(UNIT=2,FMT="(A)") str
    if (str(1:5) .EQ. "O  20") then
	WRITE(UNIT=2,FMT="(A)") "SUBSTR"
    endif
 

Back to top
 
« Last Edit: 24.04.10 at 08:44:45 by Gerrit-Jan Linker »  

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
Fortran95: Convert integer to string
Reply #5 - 24.04.10 at 08:47:16
 
Fortran95: Convert integer to string
 
To convert an integer number to a string you can use the write statement to 'write' the formatted data into the string.
 
Example:
INTEGER i
CHARACTER*100 str
 
write(str(17:20),'(i4)') i

 
The number i is written with a format mask of 4 positions into the string str at positions 17 until 20.
Back to top
 
 

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
Fortran95: for loop
Reply #6 - 12.02.11 at 15:56:19
 
Fortran95: for loop
 
Loop from 1 to 10 and print the numbers.
 
Code:
program x
   do 10 i= 1, 10
     print *, i
10 continue
end program x 


 
To loop incrementing by more than 1 add , n. Example loop from 0 to 90 in steps of 5:
   do 10 i= 0, 90, 5
Back to top
 
« Last Edit: 12.02.11 at 16:13:13 by Gerrit-Jan Linker »  

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
Fortran95: Constants
Reply #7 - 12.02.11 at 16:23:33
 
Fortran95: Constants
 
Constants are defined using the parameter keyword.
 
The following code defines pi:
 
Code:
  double precision pi
  parameter ( pi = 3.14159265358979d0 ) 


 
integer: e.g. -1, 0 , 1
real: e.g. 1.2, 1.2E+2, 1.2D+2
complex: e.g. (1.2, 3.4),   (1.2E+3, 4.5E+6)
logical: .TRUE. , .FALSE.
character chain: e.g. "The fox jumps over the box."
Back to top
 
« Last Edit: 11.10.12 at 13:38:46 by Gerrit-Jan Linker »  

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
Fortran95: Formatted output
Reply #8 - 12.02.11 at 16:28:23
 
Fortran95: Formatted output
 
Formatted output can be made using the format statement. In the following example the cosine of an angle is printed. The angle in degrees is printed as an integer of length 5 and the cosine is printed as a floating point number of size 20 with 10 decimals.
 
Code:
program h
   double precision dblAngle, pi
   integer i

   parameter ( pi = 3.14159265358979d0 )

   do 10 i= 0, 90, 5
     dblAngle = pi*i/180
     write (*,5) i, cos(dblAngle)
5  format(i5,f20.10)
10 continue
end program h
 

Back to top
 
« Last Edit: 12.02.11 at 16:30:11 by Gerrit-Jan Linker »  

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
Fortran95: String concatenation
Reply #9 - 12.02.11 at 20:44:05
 
Fortran95: String concatenation
 
The string concatenation operator is //
 
print *, "Hello" // " world"
Back to top
 
 

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
Fortran77: structure of a line
Reply #10 - 11.10.12 at 13:36:30
 
Fortran77: structure of a line
 
The structure of a code line in Fortran 77 is:
 
Column 1 to 5: only for labels (e.g. c or * for comments)
Column 6: put any character to indicate line continuation
Column 7 to 72: instructions
Columns 73 to 80: ignored
Back to top
 
 

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
Fortran77: operators
Reply #11 - 11.10.12 at 13:42:09
 
Fortran77: operators
 
Arithmatic:
+ : addition
- : subtraction
* : multiplication
/ : division
** : exponential
various numerical functions: sin, cos, alog (ln), exp, sqrt, abs, ....
 
Relational operators:
.gt. : greater than
.lt. : lower than
.ge. : greater or equal than
.le. : lower or equal than
.ne. : not equal
 
Logical operators:
.not.  
.and.
.or.
Back to top
 
 

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
Fortran 77: reading and writing
Reply #12 - 11.10.12 at 13:44:38
 
Fortran 77: reading and writing
 
read (unit, format) 'list of variables'
write (unit, format) 'list of variables'
 
Unit:
0 : standard error
5 : keyboard
6 : screen
 
Format:
* : default
 
E.g.:
write (6,*) 'Hello'
Back to top
 
 

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
Fortran95: Reading in numbers
Reply #13 - 18.06.14 at 21:36:09
 
Fortran95: Reading in numbers
 
The following program can be an example for reading in a formatted file with numbers.
Each line contains 4 numbers. A x,y,z coordinate and a charge C.
The program computes the center of charge.
 
program pcs
  DOUBLE PRECISION X, Y, Z, TX, TY, TZ, C
 
  TX=0.0
  TY=0.0
  TZ=0.0
 
  OPEN(UNIT=1, FILE="inp",FORM="FORMATTED",STATUS="OLD",ACTION="READ")
  DO
    READ(1,*,END=100) X,Y,Z,C
 
    TX=TY + X*C
    TY=TY + Y*C
    TZ=TZ + Z*C
  END DO
  100 CLOSE(UNIT=1)
  
  PRINT *,TX,TY,TZ
end program pcs
Back to top
 
« Last Edit: 18.06.14 at 21:36:39 by Gerrit-Jan Linker »  

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Pages: 1