Classwork 1 Introduction to programming in PASCAL



Similar documents
Classwork 10. Solving Streeter-Phelps model for the Oglio River upstream of lake Iseo

WESTMORELAND COUNTY PUBLIC SCHOOLS Integrated Instructional Pacing Guide and Checklist Computer Math

LECTURE 9: Open channel flow: Uniform flow, best hydraulic sections, energy principles, Froude number

What is the most obvious difference between pipe flow and open channel flow????????????? (in terms of flow conditions and energy situation)

Appendix 4-C. Open Channel Theory

Performing a Steady Flow Analysis

CHAPTER 9 CHANNELS APPENDIX A. Hydraulic Design Equations for Open Channel Flow

Floodplain Hydraulics! Hydrology and Floodplain Analysis Dr. Philip Bedient

Hydraulics Laboratory Experiment Report

SECTION VI: FLOOD ROUTING. Consider the watershed with 6 sub-basins. Q 1 = Q A + Q B (Runoff from A & B)

Topic 8: Open Channel Flow

Experiment (13): Flow channel

OPEN-CHANNEL FLOW. Free surface. P atm

Chapter 9. Steady Flow in Open channels

Sources: On the Web: Slides will be available on:

Informatica e Sistemi in Tempo Reale

Course MS10975A Introduction to Programming. Length: 5 Days

CE 504 Computational Hydrology Computational Environments and Tools Fritz R. Fiedler

Available in Base or Survey Standard or Survey Professional series with different modules add-ons to suit your technical requirement and budget.

14:440:127 Introduction to Computers for Engineers. Notes for Lecture 06

Open channel flow Basic principle

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Math 0980 Chapter Objectives. Chapter 1: Introduction to Algebra: The Integers.

Custom Javascript In Planning

Compiler Construction

CHAPTER 5 OPEN CHANNEL HYDROLOGY

Common Core Unit Summary Grades 6 to 8

JMulTi/JStatCom - A Data Analysis Toolkit for End-users and Developers

Programming Languages & Tools

CATIA V5 Tutorials. Mechanism Design & Animation. Release 18. Nader G. Zamani. University of Windsor. Jonathan M. Weaver. University of Detroit Mercy

Chapter 10. Open- Channel Flow

Edmund Li. Where is defined as the mutual inductance between and and has the SI units of Henries (H).

Everyday Mathematics. Grade 4 Grade-Level Goals CCSS EDITION. Content Strand: Number and Numeration. Program Goal Content Thread Grade-Level Goal

Tutorial for Assignment #2 Gantry Crane Analysis By ANSYS (Mechanical APDL) V.13.0

Explain the relationship between a class and an object. Which is general and which is specific?

Exercise (4): Open Channel Flow - Gradually Varied Flow

How To Solve Factoring Problems

Everyday Mathematics. Grade 4 Grade-Level Goals. 3rd Edition. Content Strand: Number and Numeration. Program Goal Content Thread Grade-Level Goals

GRADES 7, 8, AND 9 BIG IDEAS

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

CGN Computer Methods

Modeling with Python

EXAMPLES (OPEN-CHANNEL FLOW) AUTUMN 2015

EKT150 Introduction to Computer Programming. Wk1-Introduction to Computer and Computer Program

Forecasting in STATA: Tools and Tricks

MATH 095, College Prep Mathematics: Unit Coverage Pre-algebra topics (arithmetic skills) offered through BSE (Basic Skills Education)

L r = L m /L p. L r = L p /L m

The Elective Part of the NSS ICT Curriculum D. Software Development

Data Storage. Chapter 3. Objectives. 3-1 Data Types. Data Inside the Computer. After studying this chapter, students should be able to:

Computer Networks/DV2 Lab

Hydrogeological Data Visualization

Data Storage 3.1. Foundations of Computer Science Cengage Learning

Using GIS Data With HEC-RAS

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

TML. User's Guide and Reference Manual. for the APPLE IIGS. APW Version

Visualization of Output Data from Particle Transport Codes

CHAPTER 3 STORM DRAINAGE SYSTEMS

Introduction to Python

Element Property Definition for the Space Satellite

Computational Mathematics with Python

Lecture 5 Hemodynamics. Description of fluid flow. The equation of continuity

Volumes of Revolution

Delphi Developer Certification Exam Study Guide

EasyC. Programming Tips

Backwater Rise and Drag Characteristics of Bridge Piers under Subcritical

STATE OF FLORIDA DEPARTMENT OF TRANSPORTATION DRAINAGE HANDBOOK OPEN CHANNEL. OFFICE OF DESIGN, DRAINAGE SECTION November 2009 TALLAHASSEE, FLORIDA

Animated Lighting Software Overview

Open Channel Flow. M. Siavashi. School of Mechanical Engineering Iran University of Science and Technology

M6a: Open Channel Flow (Manning s Equation, Partially Flowing Pipes, and Specific Energy)

Origins of Operating Systems OS/360. Martin Grund HPI

Chapter 13 OPEN-CHANNEL FLOW

Thnkwell s Homeschool Precalculus Course Lesson Plan: 36 weeks

Lecture 25 Design Example for a Channel Transition. I. Introduction

PGR Computing Programming Skills

T O B C A T C A S E G E O V I S A T DETECTIE E N B L U R R I N G V A N P E R S O N E N IN P A N O R A MISCHE BEELDEN

From Civil 3D, with Love

For example, estimate the population of the United States as 3 times 10⁸ and the

Physics 210 Q ( PHYSICS210BRIDGE ) My Courses Course Settings

Cross-platform event logging in Object Pascal

Developing an Inventory Management System for Second Life

FOREWORD. Executive Secretary

Automated parameter conversion from HICUM/L2 to HICUM/L0

ABAQUS/CAE Tutorial: Analysis of an Aluminum Bracket

Moving from CS 61A Scheme to CS 61B Java

Factoring Trinomials: The ac Method

Broad Crested Weirs. I. Introduction

Environmental Data Management Programs

A n. P w Figure 1: Schematic of the hydraulic radius

21. Channel flow III ( )

Assignment 2: Option Pricing and the Black-Scholes formula The University of British Columbia Science One CS Instructor: Michael Gelbart

2 SYSTEM DESCRIPTION TECHNIQUES

Activity 1: Using base ten blocks to model operations on decimals

Introduction to Quadratic Functions

Zhenping Liu *, Yao Liang * Virginia Polytechnic Institute and State University. Xu Liang ** University of California, Berkeley

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

10.1. Solving Quadratic Equations. Investigation: Rocket Science CONDENSED

Transcription:

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)