ebook img

Guide to Fortran 2008 Programming PDF

412 Pages·2015·2.357 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Guide to Fortran 2008 Programming

Walter S. Brainerd Guide to Fortran 2008 Programming Guide to Fortran 2008 Programming Walter S. Brainerd Guide to Fortran 2008 Programming Walter S. Brainerd The Fortran Company Tucson, Arizona, USA ISBN 978-1-4471-6758-7 ISBN 978-1-4471-6759-4 (eBook) DOI 10.1007/978-1-4471-6759-4 Library of Congress Control Number: 2015949998 Springer London Heidelberg New York Dordrecht © Springer-Verlag London 2009, 2015 This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use. The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, express or implied, with respect to the material contained herein or for any errors or omissions that may have been made. Printed on acid-free paper Springer-Verlag London Ltd. is part of Springer Science+Business Media (www.springer.com) Preface Fortran has been the premier language for scientific computing since its introduction in 1957. Fortran originally was designed to allow programmers to evaluate formu- las—FORmula TRANslation—easily on large computers. Fortran compilers are now available on all sizes of machines, from small desktop computers to huge multiproces- sors. The Guide to Fortran 2008 Programming is an informal, tutorial introduction to the most important features of Fortran 2008 (also known as Fortran 08), the latest standard version of Fortran. Fortran has many modern features that will assist the programmer in writing efficient, portable, and maintainable programs that are useful for everything from “hard science” to text processing. Target Audience This book is intended for anyone who wants to learn Fortran 08, including those famil- iar with programming language concepts but unfamiliar with Fortran. Experienced Fortran 90/95 programmers will be able to use this volume to learn more about the im- portant features of F90/95, such as modules and arrays, and to assimilate quickly those features in Fortran 03 and Fortran 08 that are not in Fortran 90/95. This guide is not a complete reference work for the entire Fortran language; it cov- ers the basic features needed to be a good Fortran programmer and an introduction to the important features of Fortran 66, 77. 90, 95, 03, and 08. Many older error-prone fea- tures have been omitted and some of the more esoteric features that are new to Fortran also are not discussed. To understand some of the features used in old Fortran pro- grams, other sources should be consulted after learning the best basic collection of fea- tures for writing new codes or enhancing old ones. Guide to Fortran 2008 Programming is organized so that it may be read from begin- ning to end, but also particular topics may be studied by reading some chapters before previous ones are mastered. To a reasonable extent, all the material about one topic is presented together, making the book suitable as a reference work, as well as a tutorial. Examples and Case Studies Most of the important features of the Fortran programming language are covered with examples, beginning with the simplest constructs. The book concentrates to some ex- tent on the newer features of the Fortran programming language, because they often provide the best facilities to accomplish a particular programming task. Both the style of the many example programs and the selection of topics discussed in detail guide the v vi Preface reader toward acquiring programming skills to produce Fortran programs that are readable, maintainable, and efficient. Case studies are used to illustrate the practical use of features of Fortran 08 and to show how complete programs are put together. There are also simple problems to en- able the reader to exercise knowledge of the topics learned. Style of the Programming Examples To illustrate the author’s opinion of good Fortran programming style, the program- ming examples do not illustrate the wide variety of styles that might be used. There are certainly many other good programming styles, but it is important to use a style con- sistently within a programming project. The style notes also reflect opinions of the au- thor and reflect one possible good style to use. Most of the program examples have been run on either the free GCC compiler gfortran or the Intel Fortran compiler ifort. Organization of the Content An unusual feature of the book is that the first chapter contains a complete discussion of all the basic features needed to write complete Fortran programs: the form of For- tran programs, data types, simple expressions and assignment, and simple input and output. Subsequent chapters contain detailed discussions of control constructs, mod- ules, procedures, arrays, character strings, data structures and derived types, pointer variables, input/output, object-oriented programming, and coarrays. Module-oriented programming is a very important part of Fortran programming and the topic of modules is introduced early to provide the framework for organizing data and procedures for a Fortran program. From the beginning, Fortran has had extensive facilities for input and output; how- ever, this is a topic that is not explored fully in many books because it is a little more difficult than other features and perhaps just not as interesting as some features. The use of these facilities is very important in production programs, so this book contains, in Chapter 11, an extensive discussion of the excellent input/output facilities in Fortran. Appendix A lists the many intrinsic procedures. Appendix B provides a brief infor- mal syntax specification for the language. There still will be occasions when more details about the language must be learned. In these cases it will be necessary to consult the official standard, published by the International Standards Organization or the reference work The Fortran 2003 Hand- book, by Adams, Brainerd, Hendrickson, Maine, Martin, and Smith, Springer, 2009. For more information about Fortran, go to http://www.fortran.com. Many suggestions made by Brian Smith improved the book significantly. Bill Long of Cray Inc. ran some programs using the Cray compiler that used For- tran 2008 features not implemented on any compiler available to the author. Tucson, Arizona, USA Walter S. Brainerd Contents 1 Introduction to Programming in Fortran 1 1.1 Programs that Calculate and Print 1 1.2 Intrinsic Data Types 4 1.3 Variables and Input 12 1.4 The Form of a Fortran Program 19 1.5 Some Intrinsic Functions 21 1.6 Expressions and Assignment 24 1.7 Introduction to Formatting 29 1.8 Case Study: Quadratic Formula 34 1.9 Case Study: Debugging Pendulum Calculations 42 2 Control Constructs 47 2.1 Statement Blocks 47 2.2 The if Construct 49 2.3 The case Construct 62 2.4 The do Construct 65 2.5 Case Study: Numerical Integration I 71 2.6 Exercises 75 2.7 The stop Statement 77 2.8 The go to and continue Statements 78 3 Modules and Procedures 79 3.1 Modules 79 3.2 Procedures 82 vii viii Contents 3.3 Subroutines 83 3.4 Putting Procedures in a Module 84 3.5 Arguments 86 3.6 Functions 89 3.7 Pure Procedures and Side Effects 92 3.8 Argument Passing 93 3.9 Interface Blocks 97 3.10 Exercises 98 3.11 Using a Function in a Declaration Statement 98 3.12 The return Statement 99 3.13 Scope 99 3.14 Case Study: Numerical Integration II 101 3.15 Case Study: Calculating Probabilities I 103 3.16 Recursion 105 3.17 Case Study: Adaptive Numerical Integration 113 4 Arrays 119 4.1 Declaring and Using Arrays 119 4.2 Searching a List 135 4.3 Sorting 142 4.4 Selecting 148 4.5 Case Study: Solving Linear Equations 150 4.6 Case Study: Heat Transfer I 155 4.7 Case Study: Calculating Probabilities II 157 5 Character Data 161 5.1 Use of Character Data in Fortran Programs 161 5.2 Text Analysis 174 5.3 Case Study: Expression Evaluation 187 6 Structures and Derived Types 195 6.1 Structures 195 6.2 Derived Types 196 Contents ix 6.3 Declaring and Using Structures 200 7 IEEE Arithmetic and Exceptions 205 7.1 Numerical Representations 205 7.2 NaN and Inf 206 7.3 Exceptions 209 8 More about Modules and Procedures 211 8.1 Submodules 211 8.2 Date and Time Subroutines 213 8.3 Command-Line Arguments 214 8.4 Environment Variables 215 8.5 Executing a System Command 215 8.6 Generic Procedures 216 8.7 Elemental Procedures 219 8.8 More Array Intrinsic Procedures 221 8.9 Bit Intrinsic Procedures 224 8.10 Calling C Procedures 225 9 Extending Fortran 231 9.1 Extending Assignment 231 9.2 Extending Operators 233 9.3 User-Defined Operators 235 9.4 Extending Intrinsic Functions 235 9.5 Derived-Type Editing 237 9.6 Case Study: Computing with Big Integers 239 10 Pointer and Allocatable Variables 253 10.1 The Use of Pointers in Fortran 253 10.2 Moving Pointers vs. Moving Data 262 10.3 Linked Lists 263 10.4 Trees 272

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.