1. Trang chủ
  2. » Công Nghệ Thông Tin

introduction to fortran programming

31 241 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 31
Dung lượng 479 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

07/26/09 Introduction to Scientific & Engineering Computing • Fortran was originally created by a team lead by John Backus at IBM in 1957.. Why Fortran?• Among many computer scientists

Trang 1

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: toros@itu.edu.tr

Trang 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.

was originally aimed at scientific calculation and had

limited support for working with characters

few high level languages with a high level of portability between different computer systems.

started in 1954 and released commercially in 1957

small Fortran program was first successfully compiled.

History & Background

Trang 3

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.

programming language

where one Program Instruction corresponded to a

History & Background

Trang 4

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

Trang 5

 Powerful enough for use in large programs

 Download F_World compiler, Installed

F is a subset of FORTRAN.

The F compiler was written

by Walt Brainerd

Trang 7

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

Trang 8

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

Trang 10

Basic statements

Execution of the program is a separate process that goes on

inside the computer when the program is executed

execution process is dynamic, or changing

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

Trang 11

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.

Trang 12

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

Trang 13

!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

Trang 14

! A First Program Comments

program hello

implicit none

!This is my first program

write (*,*) “Hello World!“

end program hello

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

Trang 15

• How Do I Run The Program?

program text.

Hello.f90)

and producing an executable program

command window.

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

gfortran hello.f90  /a.out

Trang 16

• 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

Trang 18

!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

Trang 19

! 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

Trang 20

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

Trang 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

Trang 23

Basic statements Type declarations

The principal data types for F numerical data are:

Trang 24

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)

Trang 25

program message

! variable declaration part

character (len=25):: name , surname

Trang 26

! Write a program to calculate sum, multiplication and

division of three numbers

program numbers

real::real_1, real_2, real_3, sum1, multi, div1

!initial values

!reading numbers

print *, " Please input three numbers"

read *, real_1, real_2, real_3

Trang 27

! This program adds two numbers and displays the

print*,"The sum of the numbers you entered is", c

end program add

Trang 30

program Radioactive_Decay

! This program calculates the amount of a radioactive substance that

! remains after a specified time, given an initial amount and its half-life

! Variables used are:

! InitalAmount : initial amount of substance (mg)

! HalfLife : half-life of substance (days)

! Time : time at which the amount remaining is calculated (days)

! AmountRemaining : amount of substance remaining (mg)

! Input: InitialAmount, HalfLife, Time

! Output: AmountRemaining

implicit none

real :: InitialAmount, HalfLife, Time, AmountRemaining

! Get values for InitialAmount, HalfLife, and Time.

print *, "Enter initial amount (mg) of substance, its half-life (days)"

print *, "and time (days) at which to find amount remaining:"

read *, InitialAmount, HalfLife, Time

! Compute the amount remaining at the specified time.

AmountRemaining = InitialAmount * 0.5 ** (Time / HalfLife)

! Display AmountRemaining.

print *, "Amount remaining =", AmountRemaining, "mg"

end program Radioactive_Decay

07/26/09

Introduction to Scientific & Engineering Computing

Trang 31

print*," * o o + + t r r A A NN N"

print*," * o o + + t r r A A N N N"

print*," ****** o o + + t r r A A N N N"

print*," * o o +++++ t rrrrr AAAAAAAA N N N" print*," * o o + + t r r A A N N N"

print*," * o o + + t r r A A N NN"

print*," * oooo + + t r r A A N N“

endprogram fortran

Ngày đăng: 24/10/2014, 21:27

TỪ KHÓA LIÊN QUAN