University of Leicester Department of Physics and Astronomy Second Year Computing Workshop Modular Programming in Fortran 90

University of Leicester Department of Physics and Astronomy Second Year Computing Workshop Modular Programming in Fortran 90

University of Leicester Department of Physics and Astronomy Second Year Computing Workshop Modular Programming in Fortran 90 Prof. R. Willingale October 2, 2012 1 University of Leicester Document: modular f90 Department of Physics and Astronomy Issue: 3.1 First Year Computing Workshop Date: October2,2012 Modular Programming in Fortran 90 Page: 2 Contents 1 Introduction 4 2 Books and reference 4 3 Getting started 5 3.1 Loggingon.................................... 5 3.2 CreatingandrunningaF90program . 6 4 A pendulum problem 6 5 Modular Programming 8 5.1 TheF90function ................................ 8 5.2 Task1...................................... 9 5.3 TheF90module................................. 10 5.4 Task2...................................... 11 5.5 TheF90subroutine............................... 12 5.6 Task3...................................... 13 6 Integrating the equations of motion 14 6.1 Task4...................................... 14 6.2 Task5...................................... 15 6.3 Task6...................................... 15 6.4 Task7...................................... 16 University of Leicester Document: modular f90 Department of Physics and Astronomy Issue: 3.1 First Year Computing Workshop Date: October2,2012 Modular Programming in Fortran 90 Page: 3 7 Plotting the motion of the pendulum 16 7.1 Task8...................................... 16 7.2 Task9...................................... 18 8 More about arrays 18 9 The F90 case construct 19 10 Exploring the complexities of the motion 20 10.1Task10 ..................................... 21 10.2Task11 ..................................... 22 10.3Task12 ..................................... 22 11 Further investigation 22 11.1Task13 ..................................... 22 12 Further F90 22 13 Quick Fortran 90 reference 23 13.1 Character set, statements, literals and variable names . ....... 23 13.2 Declarations, dynamic allocation . 23 13.3 Formattedinputandoutput . 24 13.4 Arithmeticandassignment. 25 13.5 Decision structures, definite and indefinite loops . .... 26 13.6Intrinsicfunctions................................ 28 13.7Typesofcodemodule ............................. 28 University of Leicester Document: modular f90 Department of Physics and Astronomy Issue: 3.1 First Year Computing Workshop Date: October2,2012 Modular Programming in Fortran 90 Page: 4 1 Introduction In the first year you had your first taste of computing and programming. This workshop is designed to give you more practice and experience in using Fortran 90. Before you start please read this script carefully. It is important that you attempt the programming individually, not in pairs or as a consortium. You can only become proficient at programming by doing it yourself, not by watching someone else. The workshop is not a race and you must not worry if you seem to be falling behind others. The person who completes first will almost certainly have made mistakes or cut a few corners. You MUST keep a record of what you do in your laboratory workbook, obtain listings of all your programs and retain the program source files on your directory at the end of the 4 laboratory sessions so that your work can be accessed. You use the UoL IT Windows machines to access the SPECTRE system in the same way as described in the 1st year script. 2 Books and reference There are many good books on Fortran 90. Notes on the syntax were provided last year and are extended below but they are not intended to be a comprehensive text in competition with published material. You don’t have to buy a book but the following books are recommended. Whichever book you choose make sure it is up to date and covers the 1990 standard. Most books on Fortran 90 do contain a description of the 77 standard but be warned there are significant differences between f77 and f90. Do not purchase a book which only covers the 77 standard. • Fortran 90 Programming, T.M.R. Ellis, I.R.Philips, T.M. Lahey, Addison Wesley, 1994 • Fortran 90 Explained, M. Metcalf and J. Reid, Oxford University Press, 1990 The script for this workshop, the scripts for other workshops, useful references on Fortran 90, documentation on the subroutine library PGPLOT and a guide to using Irix and Word can all be found on the World Wide Web at URL: http://www.star.le.ac.uk/~rw/compshop/ University of Leicester Document: modular f90 Department of Physics and Astronomy Issue: 3.1 First Year Computing Workshop Date: October2,2012 Modular Programming in Fortran 90 Page: 5 3 Getting started 3.1 Logging on You must first log on to UoL IT Windows. To login into SPECTRE you must use the X-terminal client called NX. To install NX (only need to do this once) use Programs-->Program Installer-->NX Client (click) To login to SPECTRE use Programs-->NX Client (click) SPECTRE (click) Type it your UoL IT username and password. Start a Terminal command line window using Applications-->Accessories-->Terminal (click) When the terminal window starts up you should get a system prompt like [zrw@spectre02~]$ You type commands in response to this prompt. To logout use System--Log Out (click) Please don’t leave your terminal logged on and unattended. University of Leicester Document: modular f90 Department of Physics and Astronomy Issue: 3.1 First Year Computing Workshop Date: October2,2012 Modular Programming in Fortran 90 Page: 6 3.2 Creating and running a F90 program This is fully explained in the first year workshop. If you have forgotten how to do this now is the time to look it up. To summarise, use the following commands to create and run a program $ nedit <name>.f90 & $ gfortran <name>.f90 -o <name> $ ./<name> If you want to find more about the UNIX operating system you can browse through the help manual using the man command. $ man -k text $ man command This uses the command more to give pages of information. The -k option allows you to search the manual for specific text sequences. Otherwise you must search for a specific command. 4 A pendulum problem We are going to use a dynamics problem involving a magnetically perturbed pendulum as a vehicle to introduce the scope and power of F90. The problem cannot be solved analytically and exhibits complicated chaotic behaviour as well as simpler regular motion. The magnetically perturbed pendulum in question is similar to some executive toys you can buy to adorn your desk and distract you when the going gets tough. It consists of a magnetic bob suspended on a wire or string which lies along the z-axis when the wire is vertical. The bob is free to move in both x and y and we will only consider the motion of the bob in the horizontal xy plane. To make the motion more interesting the bob is perturbed by a magnet which can be placed in the xy plane just underneath the swinging pendulum. When the bob passes near to the magnet the path is deflected violently and makes the pendulum swing in a random way. The geometry of the pendulum is illustrated in figure 1. The motion is described by 2 simple coupled differential equations: University of Leicester Document: modular f90 Department of Physics and Astronomy Issue: 3.1 First Year Computing Workshop Date: October2,2012 Modular Programming in Fortran 90 Page: 7 Z bob y z x Y xm ym zm X magnet Figure 1: The magnetically perturbed pendulum 2 2 − − − 8 − d x/dt = gx m(x xm)/rm d dx/dt 2 2 − − − 8 − d y/dt = gy m(y ym)/rm d dy/dt In each case the 3 terms on the right correspond to gravity, the magnetic perturbation and air drag. The constants g, m and d depend on the mass of the bob, the strength of the magnet and the viscocity of air. The fixed magnet is at position (xm,ym, zm) 2 2 2 1/2 and rm = ((x − xm) +(y − ym) +(z − zm) ) . Actually these equations are only an approximation to the real state of affairs but as you will be able to demonstrate for yourselves this doesn’t matter. If m and d are zero then the pendulum executes simple harmonic motion in x and y and the bob follows an elliptical path in the xy plane. If d is zero then the motion is conservative (no energy loss) and we can express the force using a potential energy: 2 − 6 V = gr /2 m/6rm University of Leicester Document: modular f90 Department of Physics and Astronomy Issue: 3.1 First Year Computing Workshop Date: October2,2012 Modular Programming in Fortran 90 Page: 8 where r =(x2 + y2)1/2. The forces in x and y (not including drag) are then given by the gradients: fx = −dV/dx fy = −dV/dy The kinetic energy is given by: T = ((dx/dt)2 +(dy/dt)2)/2 The state of the bob at any time is specified by 4 numbers ( x, y, dx/dt , dy/dt). These define what is called a phase space for the system. As time progresses the bob will trace out a path in phase space. If we take a particular starting position for the bob in phase space we can calculate the path by integrating the differential equations. There we have it. The problem is to integrate the differential equations given g, m , d, and a starting position and to find the path of the bob in phase space. In the context of Fortran programming we will represent the position in phase space by an array of 4 numbers, P(4) where P(1) and P(2) are the position in xy and P(3) and P(4) are the corresponding velocities. In the following script the tasks you are expected to do are described in subsections entitled Task n. 5 Modular Programming Last year you learnt how to write a simple PROGRAM section. This year you will be exposed to the full power of F90; in particular we are going to use Modular Programming involving MODULEs, SUBROUTINEs and FUNCTIONs. Correct use of these makes the code easier to write, easier to maintain and alter and in many cases more efficient.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    30 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us