Metcalf Modern Fortran Explained

Download Metcalf Modern Fortran Explained

Post on 22-Jan-2016

84 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

<ul><li><p>NUMERICAL MATHEMATICS AND SCIENTIFIC COMPUTATION</p><p>Series Editors</p><p>A. M. STUART E. SULI</p></li><li><p>NUMERICAL MATHEMATICS AND SCIENTIFIC COMPUTATION</p><p>Books in the seriesMonographs marked with an asterisk () appeared in the series Monographs in Numerical Analysiswhich is continued by the current series.</p><p>For a full list of titles please visit</p><p>http://www.oup.co.uk/academic/science/maths/series/nmsc</p><p> J. H. Wilkinson: The algebraic eigenvalue problem I. Du, A. Erisman, and J. Reid: Direct methods for sparse matrices M. J. Baines: Moving nite elements J. D. Pryce: Numerical solution of Sturm-Liouville problems</p><p>C. Schwab: p- and hp- nite element methods: theory and applications in solid and uid mechanics</p><p>J. W. Jerome: Modelling and computation for applications in mathematics, science, and engineering</p><p>A. Quarteroni and A. Valli: Domain decomposition methods for partial dierential equations</p><p>G. Em Karniadakis and S. J. Sherwin: Spectral/hp element methods for CFD</p><p>I. Babuska and T. Strouboulis: The nite element method and its reliability</p><p>B. Mohammadi and O. Pironneau: Applied shape optimization for uids</p><p>S. Succi: The lattice Boltzmann equation: for uid dynamics and beyond</p><p>P. Monk: Finite element methods for Maxwells equations</p><p>A. Bellen and M. Zennaro: Numerical methods for delay dierential equations</p><p>J. Modersitzki: Numerical methods for image registration</p><p>M. Feistauer, J. Felcman, and I. Straskraba: Mathematical and computational methods for</p><p>compressible ow</p><p>W. Gautschi: Orthogonal polynomials: computation and approximation</p><p>M. K. Ng: Iterative methods for Toeplitz systems</p><p>M. Metcalf, J. Reid, and M. Cohen: Fortran 95/2003 explained</p><p>G. Em Karniadakis and S. Sherwin: Spectral/hp element methods for computational uid dynamics,</p><p>second edition</p><p>D. A. Bini, G. Latouche, and B. Meini: Numerical methods for structured Markov chains</p><p>H. Elman, D. Silvester, and A. Wathen: Finite elements and fast iterative solvers: with applications</p><p>in incompressible uid dynamics</p><p>M. Chu and G. Golub: Inverse eigenvalue problems: theory, algorithms, and applications</p><p>J.-F. Gerbeau, C. Le Bris, and T. Lelie`vre: Mathematical methods for the magnetohydrodynamics of</p><p>liquid metals</p><p>G. Allaire and A. Craig: Numerical analysis and optimization: an introduction to mathematical</p><p>modelling and numerical simulation</p><p>K. Urban: Wavelet methods for elliptic partial dierential equations</p><p>B. Mohammadi and O. Pironneau: Applied shape optimization for uids, second edition</p><p>K. Bohmer: Numerical methods for nonlinear elliptic dierential equations: a synopsis</p><p>M. Metcalf, J. Reid, and M. Cohen: Modern Fortran Explained</p></li><li><p>Modern Fortran Explained</p><p>Michael MetcalfFormerly of CERN, Geneva, Switzerland</p><p>John ReidJKR Associates, Oxfordshire</p><p>and</p><p>Malcolm CohenThe Numerical Algorithms Group, Oxfordshire</p><p>1</p></li><li><p>3Great Clarendon Street, Oxford ox2 6dp</p><p>Oxford University Press is a department of the University of Oxford.It furthers the Universitys objective of excellence in research, scholarship,</p><p>and education by publishing worldwide in</p><p>Oxford New York</p><p>Auckland Cape Town Dar es Salaam Hong Kong KarachiKuala Lumpur Madrid Melbourne Mexico City Nairobi</p><p>New Delhi Shanghai Taipei Toronto</p><p>With oces in</p><p>Argentina Austria Brazil Chile Czech Republic France GreeceGuatemala Hungary Italy Japan Poland Portugal SingaporeSouth Korea Switzerland Thailand Turkey Ukraine Vietnam</p><p>Oxford is a registered trade mark of Oxford University Pressin the UK and in certain other countries</p><p>Published in the United Statesby Oxford University Press Inc., New York</p><p>c Michael Metcalf, John Reid, and Malcolm Cohen 2011The moral rights of the authors have been assertedDatabase right Oxford University Press (maker)</p><p>First edition published 1987 as Fortran 8x ExplainedSecond edition published 1989</p><p>Third edition published 1990 as Fortran 90 ExplainedFourth edition published 1996 as Fortran 90/95 Explained</p><p>Fifth edition published 1999Sixth edition published 2004 as Fortran 95/2003 Explained</p><p>This edition published 2011</p><p>All rights reserved. No part of this publication may be reproduced,stored in a retrieval system, or transmitted, in any form or by any means,</p><p>without the prior permission in writing of Oxford University Press,or as expressly permitted by law, or under terms agreed with the appropriate</p><p>reprographics rights organization. Enquiries concerning reproductionoutside the scope of the above should be sent to the Rights Department,</p><p>Oxford University Press, at the address above</p><p>You must not circulate this book in any other binding or coverand you must impose the same condition on any acquirer</p><p>British Library Cataloguing in Publication Data</p><p>Data available</p><p>Library of Congress Cataloging in Publication Data</p><p>Library of Congress Control Number: 2010941705</p><p>Printed in Great Britainon acid-free paper by</p><p>CPI Antony Rowe, Chippenham, Wiltshire</p><p>ISBN 9780199601417 (Hbk)9780199601424 (Pbk)</p><p>1 3 5 7 9 10 8 6 4 2</p></li><li><p>Preface</p><p>Fortran remains one of the principal languages used in the elds of scientic, numerical,and engineering programming, and a series of revisions to the standard dening successiveversions of the language has progressively enhanced its power and kept it competitive withseveral generations of rivals.</p><p>Beginning in 1978, the technical committee responsible for the development of Fortranstandards, X3J3 (now PL22.3 but still informally called J3), laboured to produce a new, much-needed modern version of the language, Fortran 90. Its purpose was to promote portability,reliability, maintainability, and efcient execution. . . on a variety of computing systems.That standard was published in 1991, and work began in 1993 on a minor revision, known asFortran 95. Subsequently, and with the same purpose, a further major upgrade to the languagewas prepared by J3 and the international committee, WG5. This revision, which includedobject-oriented programming features, is now known as Fortran 2003. This has now beenfollowed by a further revision, Fortran 2008, and, once again, it seems appropriate to preparea denitive informal description of the language that it denes. This continues the series ofeditions of this book the two editions of Fortran 8x Explained that described the two draftsof the standard (1987 and 1989), Fortran 90 Explained that described the Fortran 90 standard(1990), two editions of Fortran 90/95 Explained that included Fortran 95 too (1996 and 1999)and Fortran 95/2003 (2004), with its added chapters on Fortran 2003. In that nal endeavour,a third co-author was welcomed.</p><p>In this book, an initial chapter sets out the background to the work on new standards,and the nine following chapters describe Fortran 95 (less its obsolescent features and theredundant Fortran 77 features whose use we deprecate) in a manner suitable both for graspingthe implications of its features, and for writing programs. We include the allocatable arrayextensions that were originally published as an ISO Technical Report and are now part ofFortran 2003, since they have been implemented in Fortran 95 compilers for many years.Some knowledge of programming concepts is assumed. In order to reduce the number offorward references and also to enable, as quickly as possible, useful programs to be writtenbased on material already absorbed, the order of presentation does not always follow that ofthe standard. In particular, we have chosen to defer to appendices the description of featuresthat are ofcially labelled as redundant (some of which were deleted from the Fortran 95standard) and other features whose use we deprecate. They may be encountered in oldprograms, but are not needed in new ones.</p><p>Chapter 11 describes another part of Fortran 2003 that was originally dened by an ISOTechnical Report. This is followed, in Chapters 12 to 17, by descriptions of the other features</p></li><li><p>vi Preface</p><p>dened by the Fortran 2003 standard. Chapter 18 describes a part of Fortran 2008 thatwas originally dened by an ISO Technical Report and two further chapters describe theother new features of Fortran 2008. The structure of the book thus allows the reader todistinguish clearly between Fortran 95 (plus allocatable array extensions), Fortran 2003, andthe new Fortran 2008 features. Note that, apart from a small number of deletions, each of thelanguages Fortran 77, Fortran 90, Fortran 95, Fortran 2003, and Fortran 2008 is a subset ofits successor.</p><p>In order to make the book a complete reference work, it concludes with seven appendices.They contain, successively, a list of the intrinsic procedures, a description of various featureswhose use we deprecate and do not describe in the body of the book, a description ofobsolescent and deleted features, advice on avoiding compilation cascades, an extendedexample illustrating the use of object orientation, a glossary of Fortran terms, and solutionsto most of the exercises.</p><p>It is our hope that this book, by providing complete descriptions of Fortran 95, Fortran 2003and Fortran 2008, will continue the helpful role that earlier editions played for thecorresponding versions of the standard, and that it will serve as a long-term reference workfor the modern Fortran programming language.</p><p>Malcolm Cohen wishes to thank the Numerical Algorithms Group (NAG) for its encour-</p><p>agement during the writing of this book.</p></li><li><p>Conventions used in this book</p><p>Fortran displayed text is set in typewriter font:</p><p>integer :: i, j</p><p>and a line consisting of a colon indicates omitted lines:</p><p>subroutine sort:</p><p>end subroutine sort</p><p>Informal BNF terms are in italics:</p><p>if (scalar-logical-expr) action-stmt</p><p>Square brackets in italics indicate optional items:</p><p>end if [name]</p><p>and an ellipsis represents an arbitrary number of repeated items:</p><p>[ case selector [name]]block] ...</p><p>The italic letter b signies a blank character.Corrections to any signicant errors detected in this book will be made available in the</p><p>les edits.ps and edits.pdf at ftp://ftp.numerical.rl.ac.uk/pub/MRandC.</p></li><li><p>This page intentionally left blank </p></li><li><p>Contents</p><p>1 Whence Fortran? 11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Fortrans early history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.3 The drive for the Fortran 90 standard . . . . . . . . . . . . . . . . . . . . . . 31.4 Language evolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.5 Fortran 95 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.6 Extensions to Fortran 95 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.7 Fortran 2003 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61.8 Fortran 2008 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71.9 Conformance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7</p><p>2 Language elements 92.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92.2 Fortran character set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92.3 Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.4 Source form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.5 Concept of type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132.6 Literal constants of intrinsic type . . . . . . . . . . . . . . . . . . . . . . . . 14</p><p>2.6.1 Integer literal constants . . . . . . . . . . . . . . . . . . . . . . . . . 142.6.2 Real literal constants . . . . . . . . . . . . . . . . . . . . . . . . . . 152.6.3 Complex literal constants . . . . . . . . . . . . . . . . . . . . . . . . 172.6.4 Character literal constants . . . . . . . . . . . . . . . . . . . . . . . 172.6.5 Logical literal constants . . . . . . . . . . . . . . . . . . . . . . . . 19</p><p>2.7 Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202.8 Scalar variables of intrinsic type . . . . . . . . . . . . . . . . . . . . . . . . 202.9 Derived data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212.10 Arrays of intrinsic type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232.11 Character substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262.12 Objects and subobjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272.13 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272.14 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29</p><p>3 Expressions and assignments 333.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33</p></li><li><p>x Contents</p><p>3.2 Scalar numeric expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 343.3 Dened and undened variables . . . . . . . . . . . . . . . . . . . . . . . . 373.4 Scalar numeric assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . 383.5 Scalar relational operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 383.6 Scalar logical expressions and assignments . . . . . . . . . . . . . . . . . . 393.7 Scalar character expressions and assignments . . . . . . . . . . . . . . . . . 413.8 Structure constructors and scalar dened operators . . . . . . . . . . . . . . 423.9 Scalar dened assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . 453.10 Array expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463.11 Array assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483.12 Pointers in expressions and assignments . . . . . . . . . . . . . . . . . . . . 483.13 The nullify statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513.14 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51</p><p>4 Control constructs 554.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554.2 The if construct and statement . . . . . . . . . . . . . . . . . . . . . . . . . 554.3 The case construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574.4 The do construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594.5 The go to statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63</p><p>5 Program units and procedures 675.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675.2 Main program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685.3 The stop statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695.4 External subprograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695.5 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705.6 Internal subprograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735.7 Arguments of procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73</p><p>5.7.1 Pointer arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 755.7.2 Restrictions on actual arguments . . . . . . . . . . . . . . . . . . . . 765.7.3 Arguments with...</p></li></ul>