fortran 95

Download Fortran 95

Post on 22-Oct-2014

639 views

Category:

Documents

5 download

Embed Size (px)

TRANSCRIPT

Fortran 95

Fortran 95Including Fortran 90, details of High Performance Fortran (HPF), and the Fortran module for variablelength character strings

Martin Counihan University of Southampton

Martin Counihan 1996 This book is copyright under the Berne Convention. No reproduction without permission. All rights reserved. First published in 1996 by UCL Press UCL Press Limited University College London Gower Street London WC1E 6BT and 1900 Frost Road, Suite 101 Bristol Pennsylvania 190071598 This edition published in the Taylor & Francis e-Library, 2005. To purchase your own copy of this or any of Taylor & Francis or Routledges collection of thousands of eBooks please go to www.eBookstore.tandf.co.uk. The name of University College London (UCL) is a registered trade mark used by UCL Press with the consent of the owner. British Library Cataloguing in Publication Data A catalogue record for this book is available from the British Library. Library of Congress Cataloging-in-Publication Data are available ISBN 0-203-97846-3 Master e-book ISBN

ISBN: 1-85728-367-8 (Print Edition)

To Elizabeth

Contents

Preface 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 3 3.1 3.2 3.3 What is Fortran 95? The Fortran language family Programs and programming Program structure Intrinsic procedures Statements Data Writing Fortran Coding conventions and typography Fortran 90 features deleted from Fortran 95 Getting started The character set A simple program Arithmetic operators Exercises 2.A Names and other lexical tokens Fortran source form Program structure Exercises 2.B Types of data Integers Real numbers The IF statement and the IF construct

xii 1 1 2 4 4 5 6 7 8 9 10 10 12 17 18 19 20 23 25 26 26 29 31

vi

3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 4 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10 4.11 5 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8

Intrinsic procedures Exercises 3.A The type declaration statement Kinds of real numbers Complex numbers Logical data Characters and strings The ASCII character set Exercises 3.B Introducing arrays Declaring arrays Using arrays Array constructors Array sections Exercises 4.A Subscript triplets Character substrings and arrays of character strings Masks WHERE Arrays and intrinsic functions Exercises 4.B Intrinsic procedures An intrinsic subroutine: DATE_AND_TIME Mathematical functions Numeric functions Numeric inquiry functions Exercises 5.A More intrinsic subroutines: SYSTEM_CLOCK, CPU_TIME, RANDOM and RANDOMS EED Functions for arrays Floating-point manipulation functions

33 34 34 35 37 39 42 44 46 48 48 50 52 54 57 57 59 61 62 64 65 68 68 71 72 74 77 78 80 86

vii

5.9 6 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 7 7.1 7.2 7.3 7.4 8 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 8.10 9 9.1 9.2 9.3

Exercises 5.B Execution control CASE IF: review Exercises 6.A DO FORALL (Fortran 95 feature not in Fortran 90) WHERE: review STOP Exercises 6.B Introducing external procedures Functions Exercises 7.A Subroutines Exercises 7.B More about data; the type declaration statement More about characters Operators and intrinsic functions for character data Exercises 8.A The general form of the type declaration statement Attributes Array specifications The entity declaration list Summary Defined operators; introducing interfaces Exercises 8.B Arrays and data manipulation Array element order; array constructors revisited; RESHAPE Vector subscripts Bits

88 89 89 94 96 97 105 108 108 109 110 110 115 116 122 124 124 125 128 129 135 139 144 146 147 150 152 152 156 158

viii

9.4 9.5 9.6 10 10.1 10.2 10.3 10.4 10.5 10.6 10.7 11 11.1 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.10 12 12.1 12.2 12.3 12.4 12.5 12.6 12.7

Exercises 9.A Internal files; TRANSFER Exercises 9.B Modules Data modules Exercises 10.A Module procedures Data association by argument, host and USE USE in detail Modules and interfaces Exercises 10.B More about procedures Interfaces and procedures PURE procedures (Fortran 95 feature not in Fortran 90) ELEMENTAL procedures (Fortran 95 feature not in Fortran 90) RECURSIVE procedures Exercises 11 .A Argument keywords Optional arguments Generic procedures Arrays and strings in procedures Exercises 11.B Advanced array features and derived types Allocatable arrays Intrinsic procedures for arrays Summary: classes of arrays Exercises 12.A Derived types Structure constructors Structure components

159 159 164 165 165 170 170 173 175 177 179 180 180 184 186 187 189 190 192 195 198 213 216 216 219 220 221 221 224 226

ix

12.8 12.9 12.10 12.11 13 13.1 13.2 13.3 13.4 13.5 13.6 14 14.1 14.2 14.3 14.4 14.5 14.6 14.7 14.8 15 15.1 15.2 15.3 1 5.4 15.5 1 5.6 15.7 Appendix A A.1

Structures of structures Procedures and operators with derived types Defined assignment Exercises 12.B Pointers Simple pointers Pointers and derived types Pointers and procedures Exercises 13.A Array pointers Exercises 13.B Overview Procedure names as procedure arguments Varying-length character strings INCLUDE GO TO: the pariah of Fortran Coding conventions and Core Fortran 95 Structure plans; a case study in program design Program architecture Exercises 14.A High Performance Fortran Parallel processing Pure procedures, FORALL and INDEPENDENT HPF procedures Data distribution in parallel-processor systems Extrinsic procedures HPF local procedures Final summary Input and output Data transfer

227 228 233 237 240 240 245 254 256 256 259 260 260 265 266 267 269 270 278 283 286 286 287 290 297 307 308 310 311 311

x

A.2 A.3 Appendix B B.1 B.2 B.3 Appendix C C.1 C.2 C.3 C.4 C.5 C.6 C.7 Appendix D D.1 D.2 D.3 D.4 D.5 D.6 D.7 D.8 D.9 D.10 Appendix E E.1 E.2 Appendix F F.1

Data editing and formatting File handling Bits Inside integers Bit logic A warning ISO_VARYING_STRINGS Rationale The VARYING_STRING data type Operators Extensions of intrinsic procedures New procedures for VARYING_STRING data Programs using ISO_VARYING_STRINGS Deficiencies of ISO_VARYING_STRINGS Fortran 95s new features: summary FORALL (Section 6.5) Enhancements to WHERE (Sections 4.9 & 6.6) Initialization of pointers (Sections 8.7 & 13.1) Default initialization of derived types (Section 12.5) Pure procedures (Section 11.2) Elemental procedures (Section 11.3) Automatic deallocation of allocatable arrays (Section 12.1) New and enhanced intrinsic procedures (Appendix F) Newly obsolescent features Deletions from Fortran 90 Fortran 95 statements Recommended statements Non-recommended statements Fortran 95 intrinsic procedures Recommended intrinsic procedures

322 331 344 344 345 346 348 348 349 350 351 352 355 365 367 367 367 367 368 368 368 368 368 369 369 370 370 373 375 375

xi

F.2 Appendix G Index

Non-recommended procedures Answers to selected exercises

378 387 41 6

Preface

This is a revised, expanded, and updated version of the original book Fortran 90 which appeared when Fortran 90 was first launched as an international standard programming language some five years ago. This book conforms to the proposed new Fortran 95 standard, but the new features are clearly marked as such so that the book can be used without difficulty by those who are continuing to use Fortran 90. It is designed to be useful both as a textbook for those new to programming and also as a fully up-to-date reference book for more experienced computer users. Fortran 95 includes a chapter on High Performance Fortran, the major extension to the language enabling programs to be written to exploit multiple-processor computers to the full. In addition there is an appendix giving details of the module ISO_VARYING_STRINGS, the now-standard extension of the Fortran language to handle variable-length character strings. As well as dealing with the vocabulary and syntax of Fortran, a number of general programming concepts are explained here. The production of annotated, comprehensible, bug-free, and maintainable code is encouraged. Fortran is a large language and it provides the programmer with plenty of latitude for bad as well as good programming practice, so attention is paid to disciplined methods of programming and to consistent coding conventions. The language has evolved over a long period of time, and it has always been considered important that new versions of Fortran should be backward-compatible with earlier versions so that old programs can continue to be used alongside new. So Fortran 95 contains within it virtually all of Fortran 90, which itself contained all of FORTRAN 77. Just a few features of Fortran 90 have been deleted in Fortran 95: they are listed in the first chapter, and where they arise in the course of the book they are indicated as being non-Fortran-95 features. As a result of its history Fortran 95 has many redundant features which would nowadays be regarded as obsolete. At the same time there are also elegant modern features, such as derived data structures and modules, which are very powerful and encourage up-to-date programming practice. In the main part of this book we concentrate on the modern core of Fortran 95, but all parts of the language, old and new, are included within this book and are indexed for easy reference.

CHAPTER 1 What is Fortran 95?

This introductory chapter describes the evolution of the Fortran programming language and its new extensions. The nature and structure of a Fortran program, its division into separate program units of different types, the existence of intrinsic procedures, the distinction between declarative and executable statements, and the types of data that can be represented in the language, are introduced. Some comments are made about coding conventions. 1.1 The Fortran language family Fortran is a world-standard computer programming language approved under the auspices of the International St

Recommended

View more >