BIL106 E Introduction to Scientific & Engineering Computing
description
Transcript of BIL106 E Introduction to Scientific & Engineering Computing
BIL106EIntroduction to
Scientific & Engineering ComputingHüseyin TOROS, Ph.D.
istanbul Technical University Faculty of Aeronautics and Astronautics Dept. of Meteorological Engineering
Voice: 285 31 27E-mail: [email protected]
http://atlas.cc.itu.edu.tr/~torosAn important fraction of our interaction will be via e-mail
Useful Pages:http://www.be.itu.edu.tr/ http://atlas.cc.itu.edu.tr/~toros/bil106e.htmhttp://atlas.cc.itu.edu.tr/~F90/mainindex.htmlhttp://www.fortran.com/ http://www.foldoc.org (Free OnLine Dictionary of Computing)F Compiler: Read this first, Full installer (3.4Mb): win95nt.exeFor more information syllabus_F
07/26/09Introduction to Scientific & Engineering Computing
2207/26/09Introduction 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/09Introduction to Scientific & Engineering Computing 4
The F language
FFortran 77
Fortran 90
Easy to• learn• implement• understand
Powerful enough for use in large programsDownload F_World compiler, Installed
F_World
F is a subset of FORTRAN.The F compiler was written by Walt Brainerd
07/26/09Introduction to Scientific & Engineering Computing 5
GNU Fortran and GCCGNU 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 whichprovides 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/09Introduction to Scientific & Engineering Computing 7
GNU Fortran and GCCA 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/09Introduction to Scientific & Engineering Computing 8
07/26/09Introduction to Scientific & Engineering Computing 9
1010
Basic statements A program is just a sequence of lines 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 static, 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 process07/26/09Introduction to Scientific & Engineering Computing
07/26/09Introduction 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 -- Commentsprogram helloimplicit none!This is my first programwrite (*,*) “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/09Introduction 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/09Introduction 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 aa = a + 1• More Useful:! Increment to account for new user logina = 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/09Introduction to Scientific & Engineering Computing
07
/26
/09
Intro
du
ction
to S
cien
tific & E
ng
ine
erin
g
Co
mp
utin
g
2222
! 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/09Introduction to Scientific & Engineering Computing
2323
Basic statementsBasic statementsType declarations
The principal data types for F numerical data are:1) real, 2) integer, 3) complex, 4) logical, 5) character.
07/26/09Introduction to Scientific & Engineering Computing
Operators and assignmentOperators 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.
242407/26/09Introduction to Scientific & Engineering Computing
2525
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/09Introduction to Scientific & Engineering Computing
2626
! 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!summation sum1= real_1 + real_2 + real_3 print*, sum1! multiplication part multi= real_1 * real_2 * real_3 print *, multi! division part div1= real_1 / real_2 / real_3 print *, multi!print new values print* end program numbers 07/26/09Introduction to Scientific & Engineering Computing
2727
! This program adds two numbers and displays the result
program add
real:: a,b,c
print*,"Input two numbers (include a decimal point)"
read*,a,b
c=a+b
print*,"The sum of the numbers you entered is", c
end program add
program sumofnumbers
07/26/09Introduction to Scientific & Engineering Computing
2828
! Write a program to calculates area of a rectangle
program area_of_rectangle
real::x,y,area ! x and y are the edges of rectangle
read*,x,y
area=x*y
print*,”x=“,x, ”y=“,y
print*,”area of rectangle=“,area
endprogram area_of_rectangle
07/26/09Introduction to Scientific & Engineering Computing
07/26/09Introduction to Scientific & Engineering Computing
3030
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: AmountRemainingimplicit 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/09Introduction to Scientific & Engineering Computing
3131
program kalpprint*," -- --"print*," -- - - --"print*,"-- - - --"print*,"- - -"print*,"- -"print*," - '' ----"print*," - '' -"print*," - -"print*," - -"print*," - -"print*," - -"print*," -"endprogram kalp
Program fortranprint*," ****** oooo ++++ ttttttttt rrrr AA N N"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
07/26/09Introduction to Scientific & Engineering Computing