Classwork 1 Introduction to programming in PASCAL A computer code for computing basic hydraulic quantities for free surface flow in a rectangular cross section
Why classwork 1 In this classwork we propose students to make acquaintance with a programming tool that will reveal itself invaluable in the calculations of some problems of great cultural and technical relevance. To this purpose it is not always possible to use a spreadsheet and in some cases it seems more appropriate to write your own code. On the other hand, this effort has a great educative relevance because by implementing an algorithm students have the unique opportunity of understanding how a computer code is structured. This will give them a considerable insight also on the limitations and problems of commercial codes that they will used during their future activities. In this sense, the use of specific languages for numerical computation (eg, Matlab) appears here not fully suitable to our purposes. On the other hand, Matlab will be here extensively used for graphics production. We are aware that this goal is ambitious because of the unfamiliarity of most students with the basics of programming. We will attempt to get through a phased approach leading step by step to the implementation of simple routines that calculate geometric quantities and hydraulic fundamentals. It is believed that this effort will enrich the spirit of criticism of the future practitioner and will gratifies the future researcher with a tool that will prove an invaluable tool for your reserch and professional activities. The programming language that will be used is PASCAL. The choice has been done because of its extreme semplicity and clarity, because many students already had previous experiences with it and, finally, because it is suitable also for scientific computations without significative limitations. In addition, a freeware versione of Borland Pascal 7 is available in the web. This software, altough suitable only for non-graphical application, perfectly suits our needs. In order to import the lines of code contained in this practical work into the compiler, save this file as a text file and then read it into the compiler, following your instructor s indications. Final targets of Classwork 1 Learning to write an algorithm in a programming language Understanding how is possible to extend greatly our capabilities by joining the depth of theory to the potentials of numerical computation. Start writing a preliminary part of a larger code for free surface flow computations, so understanding that the latin motto divide et impera is a true also in the programming acticvity.
The content of classwork 1: First part Program: list of instructions given to a computer using a programming language. The instructions are written using and Editor. The Editor is also a Compiler that translates the programming language to a binary language that is understandable from the computer Each programming language has Reserved Words and a sintax; PROGRAM, USES, CONST, VAR, FUNCTION, PROCEDURE,, END are some examples of Reserved Words; in the following of this Classwork they will be written in blue. Sintax, Semantic and Run Time errors; The Compiler checks Sintax Errors only, In the following you find the basic structure of a program in PASCAL: in this example, for clarity s sake, reserved words ar written in upper cases whilst variables are written in lower cases. Mind: this is not a rule of this language that in itself is unsensitive to the type of cases! the text after // is a comment, and it is not seen by the compiler! also a word within braces, such as {commento} is a comment Name and libraries Declarations Functions and Procedures PROGRAM prova; {$APPTYPE CONSOLE} USES SysUtils; VAR // here we declare the variables used throughout the program x : REAL; FUNCTION f(x:real):real; // this is a function that works on x, that is a real number PROCEDURE do_something; // this is an example of a procedure Main // this is the main, that is the core of the code WRITELN('Hello'); READLN; END. This is the end of the main and of the code! Note the. at the end of END Now let us the Editor to insert our first code
run the editor, select FILE, NEW, OTHER CONSOLE APPLICATION Copy and paste this PROGRAM prova; {$APPTYPE CONSOLE} USES SysUtils; VAR // here we declare the variables used throughout the program x : REAL; FUNCTION f(x:real):real; // this is a function that works on x, that is a real number PROCEDURE do_something; // this is an example of a procedure // this is the main, that is the core of the code WRITELN('Hello'); READLN; END. create a directory on your computer such as D:\env_hydraulics\programs\ SAVE the program within it with the name First_program From now on you can retrieve this program using OPEN PROJECT First_program
Select: PROJECT COMPILE to test the correctness of the sintax then RUN it! Not a very useful program, but it works! Now let us grow in complexity. Let us add an integer variable I. We redefine the Function so that it computes the cube of x and the Procedure so that it writes hello ; Then we add in the main three different types of loops: FOR is used for a countable loop: to repeat a set of statements a pre-defined number of times; The WHILE statement is used for a loop that must be repeated as far as the condition (here i>0) is true. You go out from the loop when it is not anymore true. Finally,the REPEAT UNTIL statement is used for a loop that must be repeated as far as the condition (here i<0) is false. You go out from the loop as soon as the condition becomes true Name and libraries PROGRAM prova; {$APPTYPE CONSOLE}
USES SysUtils; Declarations Functions and Procedures VAR // here we declare the variables used throughout the program x : REAL; i : INTEGER; FUNCTION f(x:real):real; f := x*x*x; PROCEDURE do_something; WRITELN('Hello: now I compute the cube of i'); Main WRITELN('ciclo FOR'); FOR i := 1 TO 10 DO do_something; WRITELN(f(i)); WRITELN('ciclo WHILE'); WHILE i > 0 DO WRITELN('Hello again'); i := i -1; WRITELN('ciclo REPEAT UNTIL'); i := 5; REPEAT WRITELN('and again... '); i := i -1; UNTIL i < 0; READLN; END. Now let us make a step further. Now we ll write the outputs also on a file, that will be saved on disk. Three steps are needed when operating with files: a file variable must be declared; a specific name must be assigned to it; it must be opened; it must be closed Name and libraries PROGRAM prova; {$APPTYPE CONSOLE} USES
SysUtils; Declarations Functions and Procedures the file is created the file is closed CONST // here we declare the contants used throughout the program. g = 9.8065; namefile = 'D:\env_hydraulics\programs\getta.via'; sayhello = FALSE; VAR // here we declare the variables used throughout the program x : REAL; i : INTEGER; arch : TEXT; FUNCTION f(x:real):real; VAR y : REAL; y := 2*x; f := y*y*y; PROCEDURE do_something; IF sayhello THEN WRITELN('Hello: now I compute the cube of i') ELSE WRITELN('Goodbye: now I compute the cube of i') PROCEDURE aprifile_in_scrittura; ASSIGN(arch,namefile); REWRITE(arch); PROCEDURE chiudifile; CLOSE(arch); Main here I write on the file aprifile_in_scrittura; WRITELN('ciclo FOR'); FOR i := 1 TO 10 DO do_something; WRITELN (arch,i:4, ' ',f(i):7:3); WRITELN (i:4, ' ',f(i):7:3); chiudifile; READLN;
END. As a final observation, Function and Procedure are sub units of the code that are introduced only for convenience and clarity s sake. A function returns a value (e.g, cubo receives a real value x and returns a real value) whilst a Procedure does not return a value; it simply accomplishes a task that is better to keep outside of the main. All the variables and constants declared within a Function or Procedure are seen only from that a Function or Procedure. The content of classwork 1: Second part We now wil write a code to compute and write onto disk the specific Energy, specific Force and discharge E(h), Σ(Y) for a rectagular cross-section, for a given value of Q. Then we plot the stage-discharge curve Q(Y) in uniform motion. To this purpose we write a code like the following one. In order to complete the code, the student is asked to write the functions to compute Area, wetted perimeter and hydraulic radius as a function of water depth Y: A(Y), P(Y), R(Y); Specific energy as a function of Y: E(Y) and specific discharge for constant E; Specific Force as a function of water depth Y: Σ(Y).; Chezy s equation: compute Q given h, ks, j, and the cross section base. Finally, by using the file written by the code, the student is asked to plot the E(Y), Σ(Y) and Q(Y) curves. Mind: the value of base in the const declaration is obtained as 8*(1+S/21), where S is the number corresponding to the initial of the student surname (e.g., Pilotti, S=14) By using the attached table, the code could be easily generalised to different geometries. Name and libraries Declarations PROGRAM prova; {$APPTYPE CONSOLE} USES SysUtils; CONST dest = 'D:\env_hydraulics\programs\; nameout = Stage_discharge.out'; base = alfa = 1.; beta = 1.; row = 1000; ks = 50; pendenza = 0.001; VAR arch i y,deltay,m,p,energia,spinta_totale,q : TEXT; : INTEGER; : REAL; Functions and Procedures FUNCTION pt(a,potenza:real):real; {calcola a elevato ad una potenza} IF a = 0 THEN pt := 0 ELSE
IF a > 0 THEN pt := exp(potenza*ln(a)) ELSE writeln('base negativa ed esponente reale'); FUNCTION Area(h,Base: REAL):REAL; {area per sezione rettangolare} FUNCTION Perimetro(h,Base: REAL):REAL; {perimetro idraulico per sezione rettangolare} FUNCTION R_idraulico(h,Base: REAL):REAL; {raggio idraulico per sezione rettangolare} FUNCTION E(h,Q,base:real):REAL; {calcolo di E(h) per sezioni rettangolare} FUNCTION SpintaM(h,Q,base:real):REAL; {calcolo di M(h) flusso di quantità di moto per sezioni rettangolare} FUNCTION SpintaP(h,Q,base:real):REAL; {calcolo di Π(h),risultante della forza di pressione, per sezioni rettangolare } FUNCTION SpintaTot(h,Q,base:real):REAL; FUNCTION Q_Chezy(h,ks,j,base:real):REAL; {calcolo della portata data Ks di Gauckler-Strickler, la pendenza j,la base e h}
Main ASSIGN (arch,dest+nameout); REWRITE (arch); WRITELN (arch,' [m] E [m] M[N] S_totale[N] Q[mc/s]'); WRITELN ('Scrittura dei dati su file :',dest+nameout); // first the E, M, P functions are plotted for this value of Q y := 0.0; deltay := 0.05; Q := 20; FOR i := 1 TO 100 DO y := y+deltay; energia := E(y,Q,base); M := SpintaM(y,Q,base); P := SpintaP(y,Q,base); spinta_totale := SpintaTot(y,Q,base): WRITELN(arch,y:5:2,' ',energia:7:2,' ',M:7:2,' ',P:7:2,' ',spinta_totale :7:2); // now we plot the stage-discharge curve WRITELN (arch,' [m] Q[mc/s]'); y := 0.0; FOR i := 1 TO 100 DO y := y+deltay; Q := Q_Chezy(y,ks,pendenza,base); WRITELN(arch,y:5:2, ' ',Q :7:2); CLOSE(arch); WRITELN('programma terminato: premere un tasto per uscire'); READLN; END.
(from Open Channel Hydraulics, T.Sturm. McGraw Hill)