Programming and Modelling - Universiteit berg/progmod1/ Iterative solution of a linear system ... FORTRAN 95/2003 explained, Oxford University Press, 2008. S.J. Chapman, Fortran 90/95 for Scientists and Engineers,

Download Programming and Modelling - Universiteit  berg/progmod1/  Iterative solution of a linear system ... FORTRAN 95/2003 explained, Oxford University Press, 2008. S.J. Chapman, Fortran 90/95 for Scientists and Engineers,

Post on 29-Apr-2018

213 views

Category:

Documents

1 download

Embed Size (px)

TRANSCRIPT

<ul><li><p>Programming and Modelling</p><p>Module 1</p><p>Introduction to Fortran Programming</p><p>A.P. van den Berg, C. Thieulot</p><p>September 2012</p><p>Institute of Earth SciencesUtrecht University</p><p>1</p></li><li><p>12/09 2</p><p>Contents</p><p>1 Introduction 4</p><p>2 Fortran programming exercises 5</p><p>2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5</p><p>2.2 Tutorial exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5</p><p>2.2.1 Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5</p><p>2.2.2 A 22 equation solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.2.3 Tables of multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5</p><p>2.2.4 computepi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7</p><p>2.2.5 Golden Ratio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7</p><p>2.2.6 Computing and writing function tables to file . . . . . . . . . . . . . . . . . . 7</p><p>2.2.7 A modelling experiment using input and output files . . . . . . . . . . . . . . 8</p><p>2.3 Extended exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9</p><p>2.3.1 Modelling stochastic processes using random number generators . . . . . . . 9</p><p>2.3.2 Matrix vector calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13</p><p>2.3.3 Iterative solution of a linear system of equations . . . . . . . . . . . . . . . . 14</p><p>2.3.4 Fourier synthesis of a periodic boxcar function . . . . . . . . . . . . . . . . . 16</p><p>A A Fortran Tutorial 19</p><p>A.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19</p><p>A.2 Installing and executing a Fortran program . . . . . . . . . . . . . . . . . . . . . . . 19</p><p>A.2.1 Input/output redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20</p><p>A.3 Fortran source text formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20</p><p>A.4 Program variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21</p><p>A.4.1 Variables of type character . . . . . . . . . . . . . . . . . . . . . . . . . . . 22</p><p>A.5 Indexed variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22</p><p>A.6 Program Control Structures (constructs) . . . . . . . . . . . . . . . . . . . . . . . . . 23</p><p>A.6.1 Program loop statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23</p><p>A.6.2 Conditional Statements and logical expressions . . . . . . . . . . . . . . . . . 24</p><p>A.7 Program Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25</p><p>A.8 Procedures, subroutines and functions . . . . . . . . . . . . . . . . . . . . . . . . . . 25</p><p>A.8.1 A subroutine for writing table data to a 2-column text file . . . . . . . . . . . 27</p><p>A.8.2 A subroutine for making a printplot frame . . . . . . . . . . . . . . . . . . . . 27</p><p>B Supplementary Fortran programming exercises 29</p><p>B.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29</p><p>B.1.1 Computation of arithmetic, geometric and harmonic mean values . . . . . . . 29</p><p>B.1.2 Computation of the p-norm of a vector . . . . . . . . . . . . . . . . . . . . . 29</p><p>B.1.3 The bubble-sort procedure for sorting numbers . . . . . . . . . . . . . . . . . 29</p><p>B.1.4 A function to determine the length of a 2-column data file . . . . . . . . . . . 30</p><p>B.1.5 A function to read data from a 2-column data file . . . . . . . . . . . . . . . 30</p><p>B.1.6 Printplot of a 2-column data table . . . . . . . . . . . . . . . . . . . . . . . . 31</p><p>B.1.7 Removal of the mean value of a time series . . . . . . . . . . . . . . . . . . . 32</p></li><li><p>12/09 3</p><p>C A Scilab Tutorial 33C.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33C.2 Program variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34C.3 Indexed variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35C.4 Program Control Structures (constructs) . . . . . . . . . . . . . . . . . . . . . . . . . 36</p><p>C.4.1 Program loop statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36C.4.2 Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37</p><p>C.5 Program Input/Output Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38C.6 Procedures and functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39C.7 Example functions for 2-column table data file read/write I/O . . . . . . . . . . . . . 40C.8 Scilab graphics functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41</p><p>C.8.1 Plotting function tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41C.8.2 Plotting histograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42</p><p>C.9 Example script using the Scilab ode solver . . . . . . . . . . . . . . . . . . . . . . . . 43C.9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43C.9.2 Using the Scilab ODE solver . . . . . . . . . . . . . . . . . . . . . . . . . . . 43C.9.3 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44C.9.4 Example Script bullit.sci . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45</p><p>D Scilab exercises 47D.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47D.2 Tables of multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48D.3 Plotting and writing tables of functions . . . . . . . . . . . . . . . . . . . . . . . . . 48D.4 Histogram analysis of random data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49</p><p>D.4.1 Writing random generator data to file . . . . . . . . . . . . . . . . . . . . . . 49D.4.2 Analyzing arbitrary random data from file . . . . . . . . . . . . . . . . . . . . 50</p><p>E Some useful Linux commands 51</p><p>F Using plot program xmgrace to plot 2-D x,y data sets 53</p></li><li><p>12/09 4</p><p>1 Introduction</p><p>The course in programming and modelling starts with a two week module on basic computerprogramming. This module will focus on program development and applications using the Fortranprogramming language. This first two week module consists of several instruction sessions andcomputer lab sessions, for which a total of four half days have been scheduled. The instructionsessions introduce the programming tools and the computer lab experiments to be done by thestudents. The experiments of the computer labs are done in the computer workstation rooms of theBuys Ballot Building, where (limited) assistence will be available, during the sessions scheduled forthe course.</p><p>Students work together in groups of two and each group produces a Lab report that has to besubmitted on the first monday after this module ends.</p><p>Concerning the course software: the course will be taught using the Linux operating system theGNU Fortran compiler.</p><p>The Scilab programming environment that was originally also applied in this course module isno longer used in the current set up. Information on Scilab is still available in the appendix of thissyllabus as a tutorial and set of programming problems.</p><p>Useful references on Fortran programming are,</p><p> A.P. van den Berg, J. van Hunen and J.H. de Smet, Cursus Programmeren, (Dutch) CourseNotes, Institute of Earth Sciences, Utrecht University, 1999.(available on http://www.geo.uu.nl/~berg/progmod2)</p><p> M. Metcalf, J. Reid and M. Cohen, FORTRAN 95/2003 explained, Oxford University Press,2008.</p><p> S.J. Chapman, Fortran 90/95 for Scientists and Engineers, McGraw-Hill, 1998.</p><p> http://www.dmoz.org/Computers/Programming/Languages/Fortran</p><p> http://gcc.gnu.org/wiki/GFortran</p><p> http://gcc.gnu.org/onlinedocs/gfortran</p></li><li><p>12/09 5</p><p>2 Fortran programming exercises</p><p>2.1 Introduction</p><p>A tutorial overview of the main elements of the Fortran programming language is given in AppendixA. To get acquainted with the basics of applying Fortran, study the examples in the Appendix andwork through the exercises in the following sections. Include a short description of each exercise,together with the program source text and input plus output in your lab report for this coursemodule.</p><p>To get started with Fortran program development perform the exercises stated below. Useinput/output redirection such that you can save your input and output files for your lab report (seeAppendix A.2).</p><p>2.2 Tutorial exercises</p><p>2.2.1 Hello World</p><p>To test the different steps in the procedure of program development and excution write a rudimentaryfortran program hello_world (see Appendix A.2) that prints the text hello world in the terminalwindow.</p><p>2.2.2 A 22 equation solver</p><p>Write a program which prompts for the values a, b, c, d, f, g and returns the solution x, y of thefollowing linear system:{</p><p>ax+ by = fcx+ dy = g</p><p>1 Test the proper working of your program in the following way: 1) for a given test vector pairx = x0, y = y0 compute the corresponding vector pair f = f0, g = g0 by matrix-vector multiplicationand 2) solve the system of equations with the pair f0, g0 as the righthand side vector and 3) comparethe computed solution vector with the pair x0, y0.</p><p>2.2.3 Tables of multiplication</p><p>In all Fortran program units, (program, subroutine, function) you must put an implicit nonedeclaration after the first line of source code. This will enforce explicit typing of all variables usedin the program unit and removes an important source of programming errors.</p><p>You will use Fortran arrays and do-loop constructs to fill arrays with tables of multiplication(see A.6.1). Necessary input will be read from stdin and output will be written to stdout (seeA.2.1).</p><p>1Use the Cramer formula for the inverse A1 of a matrix A with,</p><p>A =</p><p>(a bc d</p><p>), A1 =</p><p>1</p><p>det(A)</p><p>(d bc a</p><p>)where det(A) = ad bc is the determinant of matrix A.</p></li><li><p>12/09 6</p><p>1. Write a Fortran program that can produce a single multiplication table. Use a one-dimensionalarray table1, which must be declared as a fixed length (10) array in the declaration block ofyour program.</p><p>Read the table number from stdin, denoted by logical unit number 5, using list directedinput (free format input), denoted by the asterisk character *.</p><p>read(5,*) numtable</p><p>Fill the table in a do-loop,</p><p>do i=1, 10</p><p>...</p><p>end do</p><p>The table array can be written using a list directed output statement,</p><p>write(6,*) (table1(i),i=1,10)</p><p>Use output redirection to write the table data to a text file table1.dat.</p><p>2. Compute multiplication tables for n = 1, ...,m in a two-dimensional array table2.</p><p>Use an array declaration with fixed array lengths in both dimensions and make sure to declaresufficient array space. Fill the two-dimensional array table2 in a program control structure(construct) with two nested do-loops, see Appendix A.6 for an example.</p><p>do i=1,m</p><p>do j=1, 10</p><p>...</p><p>end do</p><p>end do</p><p>Check what happens if the array has been declared with insufficient arraysize. Print the 2-Dtable when it has been filled in a loop over table rows,</p><p>do i=1,m</p><p>write(6,*) (table2(i,j),j=1,10)</p><p>end do</p><p>Produce an output text file containing the 2-D table data.</p></li><li><p>12/09 7</p><p>2.2.4 computepi</p><p>One can show that the number can be computed as follows:</p><p> = 4n=0</p><p>(1)n 12n+ 1</p><p>In practice is a mathematical abstraction which computers cannot represent nor process. Thisis why we shall use</p><p> = 4mn=0</p><p>(1)n 12n+ 1</p><p>with m &gt;&gt; 1</p><p>Write a Fortran program that evaluates the series expression and prints the computed (approxi-mate) value of . Compare the outcome of your calculation with the analytic value = 4 arctan(1)that can be computed within machine precision using the Fortran intrinsic function atan. 2</p><p>To investigate the effect of different number representations (single or double precision realvariables - see Apppendix A.4) do the following tests and comment on the differences in the outcomefor different parameters and declarations of the real variables:</p><p> declare the variable pi r as a (single precision) real</p><p> compute and print pi r by means of the above formula with m=103, 106, 109.</p><p> declare now the variable pi dp as a double precision real</p><p> compute and print pi dp by means of the above formula with m=103, 106, 109.</p><p>2.2.5 Golden Ratio</p><p>Write a program which computes the Golden ratio through the Fibonacci numbers:</p><p>u0 = 1 u1 = 1 . . . un+1 = un + un1</p><p>where the ratio un+1/un converges towards .</p><p>Compare your outcome with the formula = 1+5</p><p>2 and determine how many Fibonacci numbersare necessary to get a correspondence in 5 decimals. 3</p><p>2.2.6 Computing and writing function tables to file</p><p>As in section D.3, compute a sine and cosine table for a single period and write the tables ofxi, sin(xi) and xi, cos(xi), i = 1, . . . , n to separate two-column output files. Apply the fortran proce-dure writdat listed in Appendix A.8.1 to write the different function tables to file. Inspect the datain these files by plotting them in a single frame using the graphics program xmgrace (see AppendixF). Produce a single graph showing both curves. The graph should include axis labelling and alegend to distinguish both curves. Export your completed plot to a graphics output file in a suitableformat. Include this plot in your lab report.</p><p>2http://gcc.gnu.org/onlinedocs/gfortran/ATAN.html3http://en.wikipedia.org/wiki/Golden ratio</p></li><li><p>12/09 8</p><p>2.2.7 A modelling experiment using input and output files</p><p>The program for writing function tables, developed in the previous section, can be modified to obtaina simple modelling program for the computation of the trajectory of a point mass (bullet) launchedwith a given initial velocity in a uniform gravity field that falls back and impacts at the surface.</p><p>Consider a point mass launched vertically in a gravity field with given uniform gravitationalaccelleration g and initial velocity v0. Write a Fortran program that will read the values of g and v0from the standard input file. The program computes trajectory data of the point mass in separatetables, stored as 1-D Fortran arrays, containing the vertical coordinate and its corresponding verticalvelocity. The table entries correspond to discrete time values, ti = (i 1) t, i = 1, . . . , n, for afixed time step t. The tables are filled in a Fortran do-loop over n relevant time values betweenlaunch and impact. The time window covered in the computation should start with the launch andend with the final impact when the point mass returns at the point of departure. Use a subroutine tocompute the coordinate and velocity of the point mass for a given single time value ti. Use sufficienttable points to be able to produce smooth plot curves of the vertical coordinate and the velocity asfunctions of time in separate frames and label the axes of both graphs properly.</p><p>Write tables of your results to file for plotting with xmgrace using procedure writdat, describedAppendix A.8.1.</p><p>Necessary program input should be read from the standard input file. Use program inputredirection to read the input parameters from an input text file produced in advance and includethe input file in your lab report.</p><p>An example s...</p></li></ul>

Recommended

View more >