# introduction to fortran 90/95/2003 - to fortran 90/95/2003 ... first version 1954 fortran 66 ......

Post on 25-Mar-2018

236 views

Category:

## Documents

Embed Size (px)

TRANSCRIPT

• Introduction toFortran90/95/2003NGSSCCource:ProgramminginScienceandTechnology2008JonasLindemann

• History

• FortranDevelopment

Firstversion1954

FORTRAN66FORTRAN77

Fortran90 Backwardscompatible

(TheFprogramminglanguage)Fortran95

Fortran2003,(2004)

• Pascalprogram tpk(input,output);var i : integer;y : real;a : array [0..10] of real;

function f ( t : real) : real;beginf := sqrt(abs(t)) + 5*t*t*t

end;

beginfor i := 0 to 10 do read(a[i]);for i := 10 downto 0 dobeginy := f(a[i]);if y > 400 then writeln(i,' TOO LARGE')

else writeln(i,y);

end;end;

end.

• C#include #include #include

double f (double t){

double temp;temp = sqrt(fabs(t)) + 5*pow(t,3);return temp;

}

int main(){

int i;double y;double a[11];for ( i = 0; i = 0; i = i - 1 ) {

y = f(a[i]);if ( y > 400 ) {

printf(" %d",i); printf(" TOO LARGE\n");

}else {

printf(" %d",i); printf(" %lf",y); printf(" \n");

}}return 0;

}

• FORTRAN0

2 DO 3,8,11 J=1,113 I=11-J

Y=SQRT(ABS(A(I+1)))+5*A(I+1)**3IF (400>=Y) 8,4

4 PRINT I,999.GOTO 2

8 PRINT I,Y11 STOP

• FORTRANI

C FORTRAN I STYLEFUNF(T)=SQRTF(ABSF(T))+5.0*T**3DIMENSION A(11)

4 PRINT 5,I5 FORMAT(I10,10H TOO LARGE)

GOTO 108 PRINT 9,I,Y9 FORMAT(I10,F12.7)

10 CONTINUESTOP 52525

• FORTRANIVellerFortran66

C FORTRAN IV STYLEDIMENSION A(11)FUN(T) = SQRT(ABS(T)) + 5.0*T**3READ (5,1) A

1 FORMAT(5F10.2)DO 10 J = 1, 11

I = 11 - JY = FUN(A(I+1))IF (400.0-Y) 4, 8, 8

4 WRITE (6,5) I5 FORMAT(I10, 10H TOO LARGE)

GO TO 108 WRITE(6,9) I, Y

FORMAT(I10, F12.6)10 CONTINUE

STOPEND

• Fortran77PROGRAM TPK

C FORTRAN 77 STYLEREAL A(0:10)READ (5,*) ADO 10 I = 10, 0, -1

Y = FUN(A(I))IF ( Y .LT. 400) THEN

WRITE(6,9) I, Y9 FORMAT(I10, F12.6)

ELSEWRITE (6,5) I

5 FORMAT(I10,' TOO LARGE')ENDIF

10 CONTINUEEND

REAL FUNCTION FUN(T)REAL TFUN = SQRT(ABS(T)) + 5.0*T**3END

• Fortran90/95program tpk! Fortran 90 styleimplicit noneinteger :: ireal :: yreal, dimension(0:10) :: Aread (*,*) Ado i = 10, 0, -1 ! Backwards

y = fun(A(I))if ( y < 400.0 ) then

write (*,*) i, yelse

write (*,*) i, ' Too large'end if

end docontains ! Local functionfunction fun(t)

real :: funreal, intent(in) :: tfun = sqrt(abs(t)) + 5.0*t**3

end function funend program tpk

• Fprogram tpk! Fortran F styleimplicit noneinteger :: ireal :: yreal, dimension(0:10) :: Aread *, Ado i = 10, 0, -1 ! Backwards

y = fun(A(I))if ( y < 400.0 ) then

print *, i, yelse

print *, i, "Too large"end if

end docontains ! Local functionfunction fun(t) result(fun_result)

real :: fun_resultreal, intent(in) :: tfun_result = sqrt(abs(t)) + 5.0*t**3

end function funend program tpk

• Languageelements

• Sourceform

Freeform Fixed form

Source format Max 132characters Code inpositions772, linenumbers inposition25

Comments ! anywhere onaline !, Cor*atposition1

Continuation &atend of line character atposition6oncontinuation line

Multiplestatements perline

;between statements Onstatement perrow

• ExampleofFortran90/95codeprogramexample

implicitnone

integer,parameter:: ap=selected_real_kind(15,300)real(ap)::x,yREAL(ap)::K(20,20)

x=6.0_ap!Thisislinecomment

y= &0.25_ap+&0.25_ap

write(*,*)x;write(*,*)ywrite(*,*)ap

callmyproc(K)

stop

endprogramexample

• Variablenames

Max31characters

Lettersaz,AZ(not,,)Numbers 09

Underscore _

Firstcharacter mustbealetterLower case lettersequivalent withupper caseletters

• Variabletypes

integer Integer

real Floating point

complex Complex numbers

logical Boolean variables

character Characters andstrings

• Variabledeclaration

integer ::i Scalar integer

real::p Floating point scalar

integer,parameter::a=5 Integer constant

integer,dimension(10) ::b Integer array with10elements

real::C(10) Floating point array with10elements

real ::K(20,20) Floating point array with 20x20elements

• Variabletype parameters

integer(kind=4)::ainteger(4)::b!Short form

real(kind=8)::creal(8)::a!Short form

character(len=20)::textstringcharacter(20)::textstring2character(len=20,kind=1)::textstring3!english stringcharacter(len=20,kind=kanji)::textstring4!kanji string

dependent.

• Determining appropriate type

selected_int_kind(r) returns theappropriatetype parametertorepresent allinteger values,n,intherange 10r

• Appropriatetype example

integer,parameter::rk =selected_real_kind(15,300)

integer,parameter::ik =selected_real_kind(6)

real(kind=rk)::xreal(rk)::y

integer(kind=ik)::zinteger(ik)::c

• Generaltyperule

Undeclared variablesaredefined asinteger if thevariablename startswithi,j,k,l,m,n,otherwisereal.

Theimplicitnone directive forces declarationofallvariables.Recommended.

• Arraystorage

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

1 9

2 10

3 11

4 12

5 13

6 14

7 15

8 16

1 3

2 4

5 7

6 8

9 11

10 12

13 15

14 16

real::A(16)

real::A(8,2)

real::A(2,8)

• Characterandstrings

Declaration Description

Character ::ch Character

character, dimension(60)::chv array ofcharacters

character ::chv(60) array ofcharacters

character(len=80)::line character string

character ::line*80 character string

character(len=80),dimension(60)::page array ofstrings

character(len=80) ::page(60) array ofstrings

• Derived datatypes

type pointreal::xreal::yreal::size

end type point

Type definition:

type(point)::point !Structure

Declaration statement:

Accessing structure member variables:

point%x =1.0point%y =2.0point%size =4.0

• Assignments

integer::mm

integer,parameter::ap =8

real(kind=ap)::pi

mm=113

pi=3.141592653589793_ap

m = 113

pi = 3.141592653589793

Python:

datatype parameter

• Assignments

integer,parameter::ap =&selected_real_kind(15,300)

real(kind=ap)::K(20,20)

Element(5,6)isassignedavalueof5.0

AllelementsofKareassignedavalueof5.0

K(5,6)=5.0_ap

K=5.0_ap

• Assignments

integer,parameter::ap=&selected_real_kind(15,300)

real(kind=ap)::A(3,3)

Row2isassigned1,2,3

Column2isassigned4,5,6

A(2,:)=(/1.0_ap,2.0_ap,3.0_ap/)

A(:,2)=(/4.0_ap,5.0_ap,3.0_ap/)

• Assignments

programpi_testinteger,parameter::&

ap =selected_real_kind(15,300)real(ap)pi1,pi2

pi1=3.141592653589793pi2=3.141592653589793_apwrite(*,*)pi1=',pi1write(*,*)pi2=',pi2stopendprogrampi_test

pi1=3.14159274101257pi2=3.14159265358979

• Assignments

logical::flag

character(40)::first_name

flag=.false.

first_name=Jan

• Defined andundefined variables

Avariableissaid tobeundefined until ithasbeen assigned avaluereal::speed!variablespeedexists,but isundefined

Anundefined variablemustnotbereferencedWhen avariableisassigned avalue itisconsidered definedspeed=42.0!variablespeedisdefinedItcan now bereferenced byother expressions

Anarray issaid tobedefined when allelementshave been assigned values

• Arithmeticoperators

** exponentiation

* Multiplication

/ Division

+ ddition

Subbtraction

Orderofprecedence

• Unary operators

xor+x

x**(y)isok

• Expressionsa+b+c

((a)+b)+c

Evaluated fromleft toright as:

a**b**c

a**(b**c)

Evaluated as:

• Integer expressions

Results ofdivisionswill betruncated towards 0

6/3=28/3=28/3=2

Awarning!

2**3=82**(3)=1/(2**3)=0

• Mixedmode expressions

Numeric expressionswithoperands ofdifferingdatatypes

Weaker oftodatatypes will becoerced tothestronger oneResult will beofthestronger type

real::ainteger ::ireal::b

b=a*i

icoerced toreal

• Arrayexpressions

Intrinsic operationscan also apply toarrays

Sameshape arrays areassumedOneormore operands can bescalarscalar values broadcasttoarray operands

Orderofarray operationsisnotspecified inthestandardEnabling efficient execution onavector orparallelcomputer

• Arrayexpressionsexamples

real,dimension(10,20)::a,b

real,dimension(5)::v

a/b!Arrayofshape (10,20),withelementsa(i,j)/b(i,j)

v+1.!Arrayofshape (5),withelementsv(i)+1.0

5/v+a(1:5,5)!Arrayofshape (5),withelements5/v(i)+a(i,5)

a.eq.b !.true.if a(i,j)==b(i,j)and.false.otherwise

• Relationaloperators

Operator Meaning Python

< .lt. less than = .ge. greater than orequal >=

• Logiskaoperatorer

Operator Meaning Python

.and. and and

.or. or or

.not. not not

• Controlconstructs

• Ifstatements