Logout succeed
Logout succeed. See you again!

Modern Fortran in Practice PDF
Preview Modern Fortran in Practice
Modern Fortran in Practice From its earliest days, the Fortran programming language has been designed withcomputingefficiencyinmind.Thelateststandard,Fortran2008,incor- porates a host of modern features, including object-orientation, array opera- tions,user-definedtypes,andprovisionsforparallelcomputing. This tutorial guide shows Fortran programmers how to apply these features intwenty-first-centurystyle:modular,concise,object-oriented,andresource- efficient, using multiple processors. It offers practical real-world examples of interfacingtoC,memorymanagement,graphicsandGUIs,andparallelcom- puting using MPI, OpenMP, and coarrays. The author also analyzes several numerical algorithms and their implementations and illustrates the use of severalopensourcelibraries.Fullsourcecodefortheexamplesisavailableon thebook’swebsite. ArjenMarkusisaseniorconsultantatDeltares,aninstituteforappliedresearch inthefieldofwater,subsurfaceandinfrastructureinTheNetherlands,where hedevelopsandmaintainstheirnumericalmodelingprogramsandthetools that accompany them. He is an active contributor to the ACM newsletter FortranForumandthecomp.lang.fortrannewsgroup. Modern Fortran in Practice Arjen Markus with Foreword by Michael Metcalf cambridgeuniversitypress Cambridge,NewYork,Melbourne,Madrid,CapeTown, Singapore,Sa˜oPaulo,Delhi,MexicoCity CambridgeUniversityPress 32AvenueoftheAmericas,NewYork,NY10013-2473,USA www.cambridge.org Informationonthistitle:www.cambridge.org/9781107603479 (cid:2)c ArjenMarkus2012 Thispublicationisincopyright.Subjecttostatutoryexception andtotheprovisionsofrelevantcollectivelicensingagreements, noreproductionofanypartmaytakeplacewithoutthewritten permissionofCambridgeUniversityPress. Firstpublished2012 PrintedintheUnitedStatesofAmerica AcatalogrecordforthispublicationisavailablefromtheBritishLibrary. LibraryofCongressCataloginginPublicationData Markus,Arjen. ModernFortraninpractice/ArjenMarkus,MichaelMetcalf. pagescm Includesbibliographicalreferencesandindex. ISBN978-1-107-01790-0(hardback)–ISBN978-1-107-60347-9(pbk.) 1.Fortran2008(Computerprogramlanguage) I.Metcalf,Michael. II.Title. QA76.73.F25M375 2012 005.26(cid:2)2–dc23 2012000126 ISBN978-1-107-01790-0Hardback ISBN978-1-107-60347-9Paperback Additionalresourcesforthispublicationathttp://flibs.sf.net/examples-modern-fortran.html CambridgeUniversityPresshasnoresponsibilityforthepersistenceoraccuracyofURLsfor externalorthird-partyInternetwebsitesreferredtointhispublicationanddoesnotguarantee thatanycontentonsuchwebsitesis,orwillremain,accurateorappropriate. "Eademmutataresurgo" (Loosely:thoughchanged,Ireappearasmyself) InscriptiononthegraveofJacobBernoulli, referringtothelogarithmicspiral. Inmemoryofmyfather Myparentstaughtmetobeinquisitive. Mywifeandkidsteachmestillother importantthings. Contents ForewordbyMichaelMetcalf.......................................pagexi Preface..............................................................xv 1. Introduction to Modern Fortran............................ 1 1.1 TheFlavorofModernFortran..................................1 1.2 Fortran90...................................................5 1.3 Fortran95...................................................7 1.4 Fortran2003.................................................8 1.5 Fortran2008.................................................9 1.6 WhatHasNotChanged?.....................................10 2. Array-Valued Functions.................................... 12 2.1 PassingArrays...............................................12 2.2 ElementalFunctionsandAutomaticReallocation................15 2.3 TwoMoreAdvancedExamples................................16 2.4 ConciseStyle................................................20 3. Mathematical Abstractions............................... 21 3.1 AutomaticDifferentiation ....................................21 3.2 IntegerProgramming.........................................26 3.3 EnumeratingSolutionsofDiophantineEquations................28 3.4 DelayedEvaluation..........................................31 4. Memory Management..................................... 35 4.1 FlexibleArrays.............................................. 35 4.2 MemoryLeakswithPointers..................................36 4.3 ExtendinganArray..........................................36 4.4 CharacterStringswithAdjustableLength.......................37 4.5 CombiningAutomaticandAllocatableArrays...................40 4.6 PerformanceofVariousTypesofArrays.........................41 4.7 ParametrizedDerivedTypes...................................42 4.8 AvoidingMemoryLeakswithDerivedTypes....................44 4.9 PerformanceandMemoryAccess..............................48 5. An Interface Problem...................................... 51 5.1 FillingintheParameters......................................52 5.2 UsingaPoolofData.........................................55 vii viii CONTENTS 5.3 PassingExtraArguments......................................60 5.4 ControlStructures...........................................66 5.5 DealingwithDifferentPrecisions..............................73 5.6 Overview...................................................75 6. Interfacing to C:SQLiteAs an Example ................. 76 6.1 MatchingDataTypes........................................76 6.2 PassingArgumentsBetweenCandFortranRoutines.............79 6.3 NamingandCallingConventions..............................80 6.4 DealingwithDerivedTypes...................................82 6.5 InterfacingtoSQLite.........................................85 7. Graphics, GUIs, and the Internet......................... 92 7.1 PlottingtheResults..........................................92 7.2 GraphicalUser-Interfaces.....................................98 7.3 TheInternet...............................................105 7.4 DealingwithXMLFiles.....................................109 8. Unit Testing................................................ 114 8.1 TestingFrameworks.........................................114 8.2 TridiagonalMatricesAsanExample...........................115 8.3 DesignandImplementation ................................. 118 8.4 ConcludingRemarks........................................120 9. Code Reviews............................................. 121 9.1 BeExplicit.................................................121 9.2 Don’tStray................................................125 9.3 AvoidTraps................................................128 9.4 CleanCode................................................132 10. Robust Implementation of Several Simple Algorithms 135 10.1 RelatedApproaches........................................136 10.2 LinearInterpolation.......................................137 10.3 BasicStatistics ............................................143 10.4 FindingtheRootsofanEquation............................151 11. Object-Oriented Programming.......................... 163 11.1 ExtendingTypesandType-BoundProcedures.................163 11.2 InterfacesAsContracts.....................................173 11.3 UsingaPrototypeApproach................................178 11.4 AbstractDataTypesandGenericProgramming ............... 184 11.5 ChangingtheBehaviorofaType............................187 11.6 DesignPatterns...........................................189