You are on page 1of 12
Programming in Fortran 90 A First Course for Engineers and Scientists I. M. Smith University of Manchester, UK JOHN WILEY & SONS Chichester - New York « Brisbane - Toronto - Singapore Copyright ©) 1995 by John Wiley & Sons Ltd, Baffins Lane, Chichester, ‘West Sussex POI9 IUD, England National Chichester (01243)779777 International (+ 44)1243 779 777 Reprinted March 1995 All rights reserved, No part of this book may be reproduced by any means, or transmitted, or translated into a machine language without the written permission of the publisher. Cover illustration The enhanced Mark 1 in June 1949 at the University of Manchester (© National Computing Centre, Manchester) Other Wiley Editorial Offices John Wiley & Sons, Inc., 605 Third Avenue, New York, NY 10158-0012, USA Jacaranda Wiley Ltd, 33 Park Road, Milton, Queensland 4064, Australia John Wiley & Sons (Canada) Ltd, 22 Worcester Road, Rexdale, Ontario M9W 1L1, Canada John Wiley & Sons (SEA) Pte Ltd, 37 Jalan Pemimipin #05-04, Block B, Union Industrial Building, Singapore 2057 British Library Cataloguing in Publication Data A catalogue record for this book is available from the British Library ISBN 0 471 94185 9 Typeset in 10/12 pt times by Thomson Press (India) Ltd, New Delhi Printed and beund in Great Britain by Redwood Books, Trowbridge, Wiltshire ai CONTENTS MODULE procedures Operator overloading User-defined operators Generic assignment The PRIVATE and PUBLIC attributes POINTERS POINTERS to arrays POINTERS to derived data TYPEs (structures) Exercises 12 Libraries of External SUBROUTINES Introduetion Mathematical and statistical subroutines The NAG FORTRAN library ‘The IMSL FORTRAN libraries LAPACK—An applications orientated library Graphics libraries in FORTRAN Incorporating subprogram libraries in Fortran 90 programs 13 Case Studies Introduction Case study 1:Newton Raphson Case study 2: bisection Case study 3:solution of linear equations Case study 4:defleetions of a truss Case study 5:conjugate gradient method Case study 6: equations with complex coefficients Case study 7: eigenvalues Case study 8: Runge-Kutta Case study 9: water levels in a turbine ‘Case study 10: date of Easter Case study 11: calendar Case study 12: reverse Polish notation Case study 13: Ruritanian stamps Appendix 1 The Fortran Character Set Appendix 2 Glossary of Terms Appendix 3 Intrinsic Procedures Appendix 4 Options Lists for OPENing and CLOSEing FILEs Appendix 5 Answers to Exercises Index 101 112 113 113 4 7 117 117 17 118 119 120 120 121 122 123 124 127 1277 128 133 135 141 147 153 (CASE STUDIES 121 study 7: eigenvalues ‘ ‘der vibration of three railway wagons as shown, For a force f, applied to n, we have statie displacements given by Figure. Vibrational system with three degrees of freedom 1 1 1 aya aphid aplat apla 1 3 5 w= aghit gat gl i,,8,,0 usaagh t gle t gh Lt 3k 3k 3k a 15 & ie ) ae a oe) | 105 i Ss 3k 6k 6k =Mf where M is the “influence matrix” and its elements are “influence ‘ients”. no external forces are applied, we have f,=—mti (Where m, 3m). ra natural vibration, m,m,=2m, u, = X,Sin(wt + b) Jai, = —0x,sinlor + 6) ©. fy=myox, sin(ot + >) — ne Se D gle glue lows Slo el —~ Pz 8 eis ee 1 =|g & Sew eare ert hs rue — MOR aes v| 12 PROGRAMMING IN FORTRAN 90 or X =1GX where 4 = me*/k and 1/2 is an eigenvalue of G (or 2 is an eigenval G"'). We wish to find the lowest natural frequency of the system. In this mode, we ex} wagon 3 to have the greatest displacement. Therefore, let us try If we now compute X* = GX, and scale X* so that its third element is 1, we check whether X*, as scaled, is the same as X( or, in practice, sufficiently close). X is an eigenvector and the scaling factor is the eigenvalue 1/2. Otherwise, we again using X* in place of X. It can be shown that the process converges on eigenvalue 1/2 of largest absolute value, corresponding to the lowest value of required. Write a program to find the lowest natural frequency =(c/2n) of a syst railway wagons, given their masses (m,,m,...,m,), the stiffness of their spri (ky,kp,-..,k,),and an initial approximation to the eigenvector. Test your pr. on the three wagon problem above. Case study 8: Runge-Kutta A popular method for solving systems of (non-linear) ordinary differential equa is the “Runge-Kutta” method of the fourth order (see for example Griffiths Smith, 1991, p 226*). In general, such a system will be of the form d, de Oty aa) with n initial conditions y(xq) = 4, i=0,1,2,...4”. For example the system of two equations: dy 7 LOND V0) = Yo dz Te IM Xo) = 20 may be solved by advancing the solution of y and z to x; =X9 +h by the fo . VOC) = yl) + K 2X4) = 2X) +L + Griffiths, D.V. and Smith, LM. Numerical Methods for Engineers, Blackwell, 1991 CASE STUDIES 123 K=(K,+2K, +2K,+K;)/6 and L=(Lo+2L, + 2L) + L3)/6 and Ko =hf (Xo, ¥o.20) Lo = hglXo.Yo.Z0) x 10 + 3Ko,29 + 3Lo) Yo+3Ko.z0 + $L0) Yo FAK 1.29 + HL) Ly = hg Xo + 3h Yo +3K 1,20 + 3L1) Ky =hf(Xo + hyo + Kz 20 +L) Ly = hg(Xo + hy Yo + Ko. Zo + L3) a program to solve the pair of equations oY axe (0) = ax 3th NO)=4 dz a 20)=1 is method at x = 0.5, using steps of 0.1 study 9: water levels in a turbine rge tank” is a device for damping out the effects of suddenly starting or ig a turbine in the arrangement shown below: ee Surge tank y positive Reservoir Pipeline, engin WOcity y postive — Penstock Turbine \ normal fiow through the turbine, the level in the surge tank is below the it level by the amount of friction head loss h, in the pipeline, so that initially h = 4f Lv?/2gd. If the turbine is suddenly shut down the flow in the penstock is 124 PROGRAMMING IN FORTRAN 90 stopped and the momentum of the water in the pipeline carries it into the surge causing the water level to rise. A pressure gradient builds up against the pipeline and decelerates it. The motions of the water in the pipeline and the water level in surge tank are described by the equations: dy Agate where X, is the rate of flow through the turbine (zero after shut-down). Typically level in the surge tank will oscillate as shown below: Reservoir Time level yVe—— jnitial evel in surge tank, Given that the reservoir level remains constant, and that L=1200m d= 800mm D=25m ‘f= 0.005 X, (before shut-down) = I m?/sec use the Runge-Kutta method of case study 8 to calculate what will happen (a) when the turbine is shut down after steady running (b) when the turbine is started after long quiescence. Hence design a suitable height of surge tank Case study 10: date of Easter Easter Day in any given year is the Sunday following the first Full Moon on or March 21st. A Full Moon is deemed to occur 13 days after the preceding New M The date of the last New Moon in January may be calculated from the “Epact, the year, and from that the new moons in February, March and April mas obtained. The exact method is given in the Structure Chart below. All dates are as the number of days from the beginning of the year. ‘Ponted and beund in Great Britain by Redwood Books, Trowbridge, Wiltshire. 194 PROGRAMMING IN FORTRAN 90 END DO A(I,J) =A(1,J) ~SUM END DO END DO ! forward substitution stage ! this depends on the assumption 1(i, i) DOT=1,N X(1) =B(T) B(T41:N) =B(L44:N) -A(I+1:N, 1) X(T} END DO backward substitution stage DOI=N,1,-1 X(T) =B(T}/A(T, I) B(N-1:1:—1) =B(N-1:1:—1) -A(w-1 END Da END SUBROUTINE ELIMINATE END PROGRAM COMPLEX GAUSSIAN_ELIMINATION =1,0)«x() The solution is: PROGRAM EIGENVALUES ! compute natural frequencies by a simple iterative method IMPLICIT NONE REAL, ALLOCATABLE : t INTEGER: : ,N, ITERS, ITS; LOGICAL : : CONVERGED READ: : TOL, BIG READ+ ,N, TOL, ITS ALLOCATE (A (N,N) , XO (N},X1 (WN) ) READ», A; READs, XO TITERS DO ITERS=ITERS +1 XL =MATMUL (A, XO} BIG=MAXVAL (X1) ; IF (ABS (MINVAL (X1) ] > BIG) BIG=MINVAL (x1) xL(:) X1=X1/BIG CONVERGED= (MAXVAL (ABS (X1—XO) ) /MAXVAL (ABS (X1) )

You might also like