Presentation is loading. Please wait.

Presentation is loading. Please wait.

I.Prochazka, 2014 Ivan Prochazka Summer Semester 1 + 1 Czech Technical University in Prague Faculty of Nuclear Science and Physical Engineering Department.

Similar presentations


Presentation on theme: "I.Prochazka, 2014 Ivan Prochazka Summer Semester 1 + 1 Czech Technical University in Prague Faculty of Nuclear Science and Physical Engineering Department."— Presentation transcript:

1 I.Prochazka, 2014 Ivan Prochazka Summer Semester Czech Technical University in Prague Faculty of Nuclear Science and Physical Engineering Department of Physical Electronics SCIENTIFIC AND TECHNICAL COMPUTATIONS

2 I.Prochazka, general lessons language lessons mini project 6 lessons open concept : questions, comments, interrupts welcome The lectures present knowledge not available in textbooks and manuals : - difference to other languages - problem areas - “good habits” in programming Course Concept

3 I.Prochazka, 2014 No one PC touched by a student within a lecture Gentlemen aggreement

4 I.Prochazka, active presence on 80 % of lessons - completion of all the programming examples in a “real time” - completion of a mini project in time before the end of “zapoctovy tyden” Requirements

5 I.Prochazka, 2014 FORTRAN language review, history principle basic programming problem areas Programming guidelines Programming team work Project COURSE GOALS

6 I.Prochazka, Vogel, Programování ve FORTRANu, Praha 1967,… - compilers manuals / documentation - www based (GNU, Salford,..) - Microsoft Fortran 77 - Lahey Fortran (90) - Compaq Fortran (90, 95) - and many others Literature

7 I.Prochazka, 2014 numerical computations, formulas “long term problems” ( > 50 years !) large teams space / time platform independent memory and CPU efficient high CPU power requirements (rel.) low number of I/O data volumes(rel.) problem libraries SUBJECT CHARACTERISTICS

8 I.Prochazka, 2014 FORmula TRANslation Fortran history FORTRAN, BASIC FORTRAN, 4, 4X, 77, 90,.. Why to use Fortran now ? History Libraries Back-compatibility World-wide accepted Examples (Apollo Project Simulator, LOWTRAN,, ILRS, GLAD,…) FORTRAN

9 I.Prochazka, 2014 Simplicity of the basic version Close to the machine code = > SPEED = > EFFICIENCY = > SIZE Back compatible World wide accepted All platforms Main characteristics of Fortran prograns

10 I.Prochazka, 2014 Fixed line format Historical origin - punch card, last 8 positions reserved for card No. not required since “90” - however, still used for clarity - mixing of formats within one source not possible ! ……………………………………………….72 [label] digits only # continue line 6 command

11 I.Prochazka, 2014 Characters Characters A-Z, a-z, _ Upper / Lower Casesnot distinguished Spaces ignored Good practiceuse upper or lower case, only within one program unit

12 I.Prochazka, 2014 Names Name Length(5), 6, (32), unlimited depending on version Good practicekeep names short, especially for frequently used variables Not allowedconflict with built in functions, subroutines,... Warningthe name conflict may be not reported !

13 I.Prochazka, 2014 VARIABLES Variables single x array type of variable (integer, complex…) Declaration first lines of program unit Array description DIMENSION A(10)1 D vector, 10 / 11 elem. DIMENSION B(10,20,30)3 D array DIMENSION C(-10:10)1 D vector, 21 elem. Comment # 1Array origin 0/1 defined in compiler setup, affects speed !!! Comment # 2 “variable” array size since “90”

14 I.Prochazka, 2014 TYPE OF VARIABLES Descriptorlength (bytes) !!! IMPORTANT !!! Real* 4~7 digits Double Precision* 8~15 digits Integer* 2+/- 32oooo Integer*4 * 4 (Integer*8)* 8 logical* 1.TRUE. /.FALSE. Complex* 8~ 7 digits complex Double Complex* 16~ 15 digits complex character* nstring of n ASCII

15 I.Prochazka, 2014 Variable type definition Descriptor + list of var.REAL A,BLB, ahoj ImplicitI…N always INTEGER Others REAL Descriptor IMPLICITIMPLICIT INTEGER (A-H, O-Z) CombinedCOMPLEX AMPLITUDE (10) REAL TEST(30,30)

16 I.Prochazka, 2014 Command lines 1 Comment linesC in the first coll. comment line ! to the end of line comment AssignmentBLB = I = 5 A(3, J)= 1.23E-11 WARNING type of constant !!!!

17 I.Prochazka, 2014 Command lines 2 Type conversionby assignment I = A A = I integer part of a real “the nearest smaller integer value” Warning DOUBLE PRECISION A A = 1.5E-3 !!! WRONG !!! A = 1E-3 !! WRONG !!! A = 1D-3 !! WRONG !! sometimes doesn’t work, not reported ! A = 1.5D-3 !! the ONLY ONE correct !

18 I.Prochazka, 2014 Command lines 3 Arithmetic+, -, *, /, ** Allowed mixing of types, see manual !! Sequenceas in math. Bracketsas in math., (right might be missing ) only ( )

19 I.Prochazka, 2014 Program units Program unitshead …. ….. END independent program units independent variables, names,…. Exception – peripheral numbers main, subroutines, functions, BLOCK DATA

20 I.Prochazka, 2014 Main program PROGRAM name …… ……. (STOP xxxxxxx ) END (name) One and only one per program Name coinciding with the file name(s), (not obligatory) Function … see later for conventions Preferably on the top of the source file

21 I.Prochazka, 2014 INPUT OUTPUT OPERATIONS Historical review (peripherals..) Formatted / un-formatted I/O operations READ x WRITE FORMATFixed format I/O READ (lu, format, (optional parameters)) A,B,C WRITE (lu, format, (optional parameters)) A,B,C Wherelu… peripheral number (1….100) shared automatically by all program units Format… label of the FORMAT description Each command reads/writes one record

22 I.Prochazka, 2014 INPUT OUTPUT OPERATIONS 2 Peripherals numbering convention : lu = *consoleI / O 1consoleI / O 2card punchO 3card read I 4tape punchO 5tape read I 6printerO 7 8mag.tape.I / O

23 I.Prochazka, 2014 INPUT OUTPUT OPERATIONS 3 PERIPHERALS ASSIGNMENT OPEN(lu, peripheral,…..parameters..) lu peripheral file specification error handling CLOSE(lu,……parameters…) optional REWIND(lu,……parameters....) go to record #1 BACKSPACE(lu,……parameters....) one record back

24 I.Prochazka, 2014 INPUT OUTPUT OPERATIONS #4 FORMAT TYPES F j.kfloating (real, double(?)) E j.kexponential (real) D j.kexponential (double) I jinteger [ Ij.j ] A jASCII X jskip / space (I/O) ‘…..’text out /new record jnumber of positions used knum.of decimal points Preceding integer is repeating the format

25 I.Prochazka, 2014 INPUT OUTPUT OPERATIONS #4a FREE FORMAT *free format input REAL (Double prec. ?) values separator : space, comma (others ?) *output EMERGENCY ONLY !!! user un-friendly format SIMPLIFIED FORMAT READ (*, label )read from console (PC) WRITE ( *, label)write to console (PC) READ ( lu, ‘(…formatting sequence..)’ ) a

26 I.Prochazka, 2014 INPUT OUTPUT OPERATIONS #5 FORMAT EXAMPLES codingA= I = 56 WRITE(5,69) A,I 69FORMAT(‘ Ahoj ‘,//,F8.4,5x,I5) outputAhoj |blank __1.2346______56 ROUNDING !!! Comment E,F,D formats are rounding

27 I.Prochazka, 2014 INPUT OUTPUT OPERATIONS #5 FORMAT EXAMPLE codingI = 56 WRITE(5,69) I,I,I,I 69FORMAT(‘ Integer test’,I5,I2,I5,I5.5) outputInteger test ___ FORMAT EXAMPLE #2 codingI = 1 69FORMAT(3I5) J=2 K= 3 WRITE(5,69) I,J,K output____1____2____3

28 I.Prochazka, 2014 INPUT OUTPUT OPERATIONS #6 FORMAT EXAMPLE #3 codingI = 1 J=2 K= 3 WRITE(5,69) I,J,K 69FORMAT(I5)repeats itself output____1____2____3

29 I.Prochazka, 2014 Exercise general instructions 0. Get familiar with the Fortran compiler (both in the classroom and elsewhere) 1. Writing the code, create a separate file for each the exercise 2. The deadline is the next lesson 3. Obey the instructions listed on the end of this course strictly

30 I.Prochazka, 2014 Exercise # 1 0. Get familiar with some Fortran compiler 1. Create an interactive calculator 4 basic operations, real comment the code and I/O well ! 2. see # 1, output to the file 3. see # 1, input from the file 4. see # 1, double precision 5. see # 1, complex

31 I.Prochazka, 2014 Example of source code #1 C SAMPLE OF FORTRAN CODE I.PROCHAZKA APRIL 2013 PROGRAM SAMPLE WRITE(*,1) 1 FORMAT(/' Example of FORTRAN programming'// # ' input two numerical variables ',\) READ(*,*) A,B C=A+B WRITE(*,3) A,B,C 3 FORMAT(' Input ', 2F12.6, ' result',F12.6) END

32 I.Prochazka, 2014 SUBROUTINES SubroutineSUBROUTINE BLB(a,b,c,..) …………. …………. RETURN END calling CALL BLB(X,Y,Z) WARNING All parameters a,b,c. are input and output parameters! = > side effects of the function and subroutine the type of the parameters a,b,c.. must be obeyed the a,b,c.. length maybe not checked !!!

33 I.Prochazka, 2014 FUNCTIONS 1 Functionsingle line x multiple line Internal x external FunctionFUNCTION BLB(a,b,c,d….) mult.line……….. ……….. BLB=…. RETURN END Callinga = 1. / BLB(C,D) WARNING BLB has a nature of a “variable” type, define type !!...

34 I.Prochazka, 2014 FUNCTIONS 2 Single line function First after descriptions One line form Works in one (sub)program only ! Compiling process… ……... SOUCET(A,B) = A + B ISOUCIN (K,L) = K*L + K-L …………. X = SOUCET(Y,Z)-ISOUCIN(m,n)

35 I.Prochazka, 2014 FUNCTIONS 3 Internal functions see “Functions” see manual for list of internal functions sin(x), cos(x),sign(x), amax(a,b), sqrt(x).. WARNING conflict of names WARNING function and argument types

36 I.Prochazka, 2014 IF commands ArithmeticIF(variable) label1, label2, label3 0 LogicalIF(….)one line command IF(….)THEN command line(s) (ELSE) command line(s) ENDIF where …. is a logical expression or variable

37 I.Prochazka, 2014 IF commands #2 Relation operators (optional for F90 and later).LT.<.EQ.=.LE.=<.NE.#.GT.>.GE.>= Logical operators.AND..OR. …. see manual compare equal variable types ! do not use IF(A.EQ.B) possible rounding problems ! standard nesting rules

38 I.Prochazka, 2014 IF commands #3 Examples #1IF(A.GT.0..AND.A.LE.10.) WRITE(1,23) A #2IF(B(3,1)) 23,12,JUMP1 #3…. LOGICAL JUMP JUMP=.TRUE. …. IF(JUMP) THEN …. ELSE … ENDIF

39 I.Prochazka, 2014 Example of source code #2 C SAMPLE OF FORTRAN CODE I.PROCHAZKA APRIL 2013 PROGRAM SAMPLE SOUCET(X,Y)=X+Y WRITE(*,1) 1 FORMAT(/' Example of FORTRAN programming'// # ' input two numerical variables ',\) READ(*,*) A,B C=SOUCET(A,B) D=A-B E=SOUCIN(A,B) IF(ABS(B).LT. 2.E-20) STOP ‘ DIVISION BY ZERO ‘ F=A/B WRITE(*,3) A,B,C,D,E,F 3 FORMAT(' Inputs ', 2F12.6, ' results',4F12.6) END C FUNCTION SOUCIN(X,Y) C COMPUTES PRODUCT OF INPUT VALUES IP SOUCIN=X*Y RETURN END

40 I.Prochazka, 2014 Exercise # 2 0. Create the interactive calculator 4 basic operations and power, square root 1. Check all the values (division by zero…) 2. repeat the calculations until...

41 I.Prochazka, 2014 JUMPs Obsolete ?? GOTO label GOTO (label1, label2, label3….), IVAR =1=2=3 WarningIVAR out of bounds ! Example ….. JUMP=2 ….. GO TO (12,14,16,1000), JUMP …-> jump to label 14 will occur

42 I.Prochazka, 2014 Empty line Obsolete ? “ empty commend” good for label ! good for program readability CONTINUE

43 I.Prochazka, 2014 Exercise # 3 0. Create the interactive calculator 4 basic operations, real all the comments 1. separate subroutine for each operation 2. separate function for each operation 3. single line function...

44 I.Prochazka, 2014 Loops #1 “Classical loop” DO label J=k,l,m ….. …. label……. J=k execute lines till label increment J for m, check if J>l if not, repeat the loop if yes, continue on the next line

45 I.Prochazka, 2014 Loops #2 (since F90) “Block DO” DO K=1,N …. END DO no label “named block DO “ C1 : DO J=1,20 …… END DO C1 “ cycle with EXITt” DO …. …… IF(…) EXIT …. END DO

46 I.Prochazka, 2014 Loops #3 (since F90) “cycle while” DO WHILE (A>0.) …. END DO orDO 10 WHILE (A >0.),…. 10CONTINUE not allowed last commandsGOTO, IF, RETURN, STOP EXIT, END nesting rules the last command line of the cycle may be common for more cycles DO 20 I=1,K DO 20 J=10,15 20B(I,J)= B(2,3)**2

47 I.Prochazka, 2014 DATA definition of starting values of variables general : the value of variable is not defined on the first program run, depends on compiler & settings syntaxDATA a /1.23/, blb /2.35e-5/, jump / 2/ WARNINGthe variable type must be obeyed !!

48 I.Prochazka, 2014 COMMON purposedefines a list of variables, which are shared as common within more than one program unit. The variables are not moved but they do share physically common place in a memory advantages - fast - access to variables in a deep structure syntaxCOMMON [name] a, b, c,… several /different name/ common blocks may be used WARNING- the variable type is not checked !!! - the physical length must be maximal in a main program

49 I.Prochazka, 2014 EQUIVALENCE purposedefines a list of variables, which are shared as common within one program unit. advantages - type conversion,.. syntaxEQUIVALENCE (a, b), (i,j)

50 I.Prochazka, 2014 Exercise # 4 0. Create the interactive calculator 4 basic operations, double complex variables all the comments 1. separate subroutine for 2 operations 2. separate function for 2 operations 3. parameters transfer via COMMON block 4. use meaningfully EQUIVELENCE

51 I.Prochazka, 2014 Programming & Coding Guidelines #1 Every procedure has one and only one function No procedure design larger than single sheet of paper Do only the required processing at each level, do not try to do too much in any routine. Include comments in each routine : who, why, when, how, in, out Include standard header to each routine Avoid “tricky” code, comment it well when it must be used Variables in an explicit type statement whenever possible Avoid “entry” statements Every routine contains only one “return” statement immediately before the “end” statement on the end D. W. Morrison, Proceedings of 3rd WLRI, 1975,73-75

52 I.Prochazka, 2014 Programming & Coding Guidelines #2 KEEP THE CODE SELF-EXPLANATORY Main program on the top of the source code Do not mix upper / lower case letters Obey the rules of the oldest language versions whenever possible / fixed format lines, omit DO-WHILE,…./ Open / close and comment all I/O units in the main program Include and update the author and revision to each routine Coding an algorithm not created by yourself always include the author and reference


Download ppt "I.Prochazka, 2014 Ivan Prochazka Summer Semester 1 + 1 Czech Technical University in Prague Faculty of Nuclear Science and Physical Engineering Department."

Similar presentations


Ads by Google