Tải bản đầy đủ (.ppt) (31 trang)

introduction to fortran programming

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (373.36 KB, 31 trang )

BIL106E
Introduction to
Scientific & Engineering Computing
Hüseyin TOROS, Ph.D.
istanbul Technical University Faculty of Aeronautics and Astronautics Dept.
of Meteorological Engineering
Voice: 285 31 27
E-mail:
/>An important fraction of our interaction will be via e-mail
Useful Pages:

/> />
(Free OnLine Dictionary of Computing)
F Compiler: Read this first, Full installer (3.4Mb): win95nt.exe
For more information syllabus_F
07/26/09
Introduction to Scientific & Engineering Computing
2
2
07/26/09
Introduction to Scientific & Engineering Computing

Fortran was originally created by a team lead by John
Backus at IBM in 1957. Originally, the name was in all
capital letters, but current usage is only requiring that the
first letter be capitalized.

The name Fortran stands for FORmula TRANslator. It
was originally aimed at scientific calculation and had
limited support for working with characters.


Until the C language became popular, it was one of the
few high level languages with a high level of portability
between different computer systems.

Several websites indicate that the work on Fortran was
started in 1954 and released commercially in 1957.

20 September 1954 may have been the day that a
small Fortran program was first successfully compiled.
History & Background
There have been several versions of Fortran. Fortran I,
II and III are considered obsolete.
The oldest Fortran versions which are considered of
much use today were Fortran IV, and Fortran 66,
which, as the name implies, was released in 1966.
All later versions of Fortran are numbered after the year
the standard was released.
The versions of Fortran most commonly remaining in
use are Fortran 77, Fortran 90, and Fortran 95.

It was the first High Level or Third Generation
programming language.

Before it, all Programs were written in Assembler,
where one Program Instruction corresponded to a
single machine operation, and each model of computer
featured its own Instruction Set.
History & Background
Why Fortran?


Among many computer scientists Fortran is the
most widely used language in scientific computing,
especially when high performance is required.

Concise language

Good compilers producing efficient machine code

Legacy: high-quality mathematical libraries
available

New version have features helpful for
parallelization
07/26/09
Introduction to Scientific & Engineering Computing
4
The F language
F
Fortran 77
Fortran 90

Easy to

learn

implement

understand

Powerful enough for use in large programs


Download F_World compiler, Installed
F_World
F is a subset of FORTRAN.
The F compiler was written
by Walt Brainerd
07/26/09
Introduction to Scientific & Engineering Computing
5
GNU Fortran and GCC
GNU Fortran is a part of GCC, the GNU Compiler Collection. GCC
consists of a collection of front ends for various languages, which
translate the source code into a languageindependent form called
GENERiC. This is then processed by a common middle end which
provides optimization, and then passed to one of a collection of back
ends which generate code for different computer architectures and
operating systems.
Functionally, this is implemented with a driver program (gcc) which
provides the command-line interface for the compiler. it calls the relevant
compiler front-end program (e.g., f951 for Fortran) for each file in the
source code, and then calls the assembler and linker as appropriate to
produce the compiled output. in a copy of GCC which has been compiled
with Fortran language support enabled, gcc will recognize files with ‘.f’,
‘.for’, ‘.ftn’, ‘.f90’, ‘.f95’, ‘.f03’ and ‘.f08’ extensions as Fortran source code,
and compile it accordingly.
07/26/09
Introduction to Scientific & Engineering Computing
7
GNU Fortran and GCC

A gfortran driver program is also provided, which is identical to gcc
except that it automatically links the Fortran runtime libraries into the
compiled program.
Source files with ‘.f’, ‘.for’, ‘.fpp’, ‘.ftn’, ‘.F’, ‘.FOR’, ‘.FPP’, and ‘.FTN’
extensions are treated as fixed form. Source files with ‘.f90’, ‘.f95’, ‘.f03’,
‘.f08’, ‘.F90’, ‘.F95’, ‘.F03’ and ‘.F08’ extensions are treated as free form.
The capitalized versions of either form are run through preprocessing.
Source files with the lower case ‘.fpp’ extension are also run through
preprocessing.
More information about gfortran
07/26/09
Introduction to Scientific & Engineering Computing
8
07/26/09
Introduction to Scientific & Engineering Computing
9
10
10
Basic statements

A program is just a sequence of li nes of text.

Execution of the program is a separate process that goes on
inside the computer when the program is executed.

The program statements are s t at i c, or fixed, while the
execution process is dynamic , or changing.

The statements exists in space, and the execution occurs in a
time dimension.

First instruction
Last instruction
Execution of first instruction
Execution of last instruction
Correspondence between the program and its execution process
07/26/09
Introduction to Scientific & Engineering Computing
07/26/09
Introduction to Scientific & Engineering Computing
General Fortran Program Structure :
Fortran constitutes an Imperative High Level
programming language, in that the Source Code in
Programs is not directly understood, let alone executed
by the hardware. Instead, the Code is submitted to a
Compiler which writes out a Binary or Executable Module,
containing (Machine Code) Instructions appropriate (and
often peculiar to) the Hardware being used.
All Fortran Programs begin with a "Non Executable" Part,
where Variables, Arrays, and Constants may be declared
and initialized.
The "Executable" Part follows, in which all computations,
logic, and File handling take place.
Diagrammatic form of a Fortran Program :
PROGRAM Statement : Optional to Name Program
Non Executable Part : Non Executable Statements
Executable Part : Executable Statements
END : Completes Executable Part
!Our First Program
program hello
implicit none

!This is my first program
write (*,*) “Hello World!”
end program hello
! The bold keywords tell the compiler where the program begins and
ends.
! A First Program Comments
program hello
implicit none
!This is my first program
write (*,*) “Hello World!“
end program hello

!Comments are preceded by a “!”

!All characters following the exclamation mark on that
line are ignored by the compiler

!The “!” inside the Hello World ! string is not part of a
comment
07/26/09
Introduction to Scientific & Engineering Computing

How Do I Run The Program?

First, prepare the program using an editor to enter the
program text.

A plain text editor such as Notepad, vi, nano,

Save the program text with the suffix .f90 (e.g.

Hello.f90)

Run the FORTRAN compiler taking its input from this file
and producing an executable program

If you used a plain text editor, run the following from the
command window.

gfortran –fimplicit-none –W hello.f90 -o hello.exe

Run the executable program (in the .exe file)

gfortran hello.f90  ./a.out

From F_world menu run program commands
07/26/09
Introduction to Scientific & Engineering Computing
Comments

Comments are used to signal the intent of the programmer

Improve readability and understanding

An important aid to debugging and maintaining code

Comments can appear anywhere in the program

When the compiler encounters a “!” (that is not contained inside a
string) it ignores the rest of the line


Comments are only there for someone reading the program, not for
the compiler to use.

Make Useful Comments
Useful Comments

Not Useful:
! Add 1 to a
a = a + 1

More Useful:
! Increment to account for new user login
a = a + 1

Sometimes, Not Necessary:
NumUsersLoggedIn = NumUsersLoggedIn + 1
!READ iN THREE iNTEGERS FROM THE KEYBOARD AND
!PRINT THEM TO
!THE SCREEN IN A DIFFERENT ORDER

program number

integer:: num1, num2, num3
print *, 'Enter 3 integers: '
read *, num1, num2, num3
print *, num2, num3, num1
stop
end program number
! Write a program that reads in a temperature in Celsius and converts it
! to Fahrenheit, then prints result to the screen

! Read in the celsius temperature and print out the fahrenheit temp
program centigrade_to_fahrenheit
implicit none
! Variable declarations;
real :: temp_c, temp_f
! Ask for Centigrate temperature;
print *, "What is the Centigrade temperature? "
read *, temp_c
! Convert to Fahrenheit;
temp_f = 9.0 * temp_c / 5.0 + 32.0
! Print both temperatures;
print *, temp_c, "C=", temp_f,"F"
end program centigrade_to_fahrenheit
En iyi Buğday
Her yil yapilan "en iyi buğday" yarişmasini yine ayni çiftçi kazanmişti.
Çiftçiye bu işin sirri soruldu. Çiftçi:
-Benim sirrimin cevabi, kendi buğday tohumlarimi komşularimla
paylaşmakta yatiyor, dedi.
-Elinizdeki kaliteli tohumlari rakiplerinizle mi paylaşiyorsunuz? Ama
neden böyle bir şeye ihtiyaç duyuyorsunuz? diye sorulduğunda,
-Neden olmasin, dedi çiftçi.
-Bilmediğiniz bir şey var; rüzgâr olgunlaşmakta olan buğdaydan poleni
alir ve tarladan tarlaya taşir. Bu nedenle, komşularimin kötü buğday
yetiştirmesi demek, benim ürünümün kalitesinin de düşük olmasi
demektir. Eğer en iyi buğdayi yetiştirmek istiyorsam, komşularimin
da iyi buğdaylar yetiştirmesine yardimci olmam gerekiyor.
Kalkınmada süreklilik çevremizle beraber olduğunda
mümkündür.
Sevgi ve paylaşmak en yakininizdan başlar. Sonra yayilarak
devam eder.

Kin, cimrilik, nefret kimsenin hoşlanacaği davranişlar değildir.
07/26/09
Introduction to Scientific & Engineering Computing
07/26/09
Introduction to Scientific & Engineering
Computing
22
22
! Write a program to calculates average of two number
program average
real::x,y,ave ! Type declarations
read*,x,y ! Read the values
Ave=(x+y)/2.0 !Calculation
print*,”x= “,x, ”y= “,y
print*,”average is= “,ave
endprogram average
07/26/09
Introduction to Scientific & Engineering Computing
23
23
Basic statements
Basic statements
Type declarations
The principal data types for F numerical data are:
1) real,
2) integer,
3) complex,
4) logical,
5) character.
07/26/09

Introduction to Scientific & Engineering Computing
Operators and assignment
Operators and assignment
The following operators are supported. Notice that the
relational operators of the form .op. (like .EQ.) have not
been included. The Fortran 90/95 feature that permits new
operator definition is included and allows the .op. form.
There is also a defined assignment capability. Arithmetic
operators +, -, * ,/,** (Addition, Subtraction, Multiplication, Division, Exponentiation)

Relational operators <, <=, ==, /=, >, >=

Logical operators .not., .and., .or., .eqv., .neqv. Character
concatenation //

Defined operator .letters.
24
24
07/26/09
Introduction to Scientific & Engineering Computing
25
25
program message
! variable declaration part
character (len=25):: name , surname
character(len=200):: my_message
!initial values
!read name
print *, “ Please input your name, then press enter”
read *, name

!read surname
print *, " Please input your surname, then press enter"
read *, surname
! read * name surname
!read message
print *, " Please input your message, then press enter"
read *, my_message
!printing part
print*
print*, " Hello !"
print*
print *, " My name is :", name ," ", surname
print*
print *,my_message
pause
end program message
07/26/09
Introduction to Scientific & Engineering Computing

×