Transcription of Introductory Fortran Programming - Delaware Physics
1 MotivationIntroductoryFortranProgramming Gunnar WollanDepartmentofGeosciencesUniversity ofOslo,N-0315 Oslo,NorwaySpring2005 Gunnar WollanIntroductoryFortranProgrammingMoti vationMotivation1 MotivationGunnar WollanIntroductoryFortranProgrammingMoti vationAboutFortran77and95 ContentsGentleintroductiontoFortran77and 95programmingFileI/OArraysandloopsDetail edexplanationofmodulesComputationale ciencyaspectsUsingmodulesasobjectsThepro miseofFortran2003 Gunnar WollanIntroductoryFortranProgrammingMoti vationAboutFortran77and95 RequiredbackgroundProgrammingexperiencew itheitherC++,Javaor MatlabInterestinnumericalcomputingusingF ortranInterestinwritinge cientprogramsutilizinglow-leveldetailsof thecomputerGunnar WollanIntroductoryFortranProgrammingMoti vationAboutFortran77and95 AboutlearningFortranFortranisa lesscomplicatedlanguagethanC++andJavaEve nsoit takestimetomastertheadvanceddetailsofFor tran95At least6 monthstoa year workingwithFortran95beforeyouarefamiliar withthedetailsFourdayscanonlygetyoustart edYouneedtouseFortran95inyourownprojects tomasterthelanguageFortran77codeisnotthe maintopichere.
2 ButyouneedtohavesomeknowledgeofitGunnar WollanIntroductoryFortranProgrammingMoti vationAboutFortran77and95 TeachingphilosophyIntensivecourseLecture s9 - 12 Hands-ontraining13- 16 LearnformdissectingexamplesGetintouchwit hthedirty workGetsomeoverviewofadvancestopicsFocus onprinciplesandgenericstrategiesContinue dlearningonindividualbasisThiscoursejust getyoustarted- usetextbooks,referencemanualsandsoftware examplesfromtheinternetfor furtherworkwithprojectsGunnar WollanIntroductoryFortranProgrammingMoti vationAboutFortran77and95recommendedatti dudeDiveintoexecutableexamplesDon'ttryto understandeverythingTrytoadaptexamplesto newproblemsLookuptechnicaldetailsinmanua ls/textbooksLearnondemandKeepa coolheadMake yourprogramsmallandfast- thenyoursoftwarelongwilllastGunnar WollanIntroductoryFortranProgrammingMoti vationAboutFortran77and95 AboutFortran77and952 AboutFortran77and95 Gunnar WollanIntroductoryFortranProgrammingAbou tFortran77and95 IntrotoFortran77programmingFortran77
3 Intotheearly/middleoftheninetiesFortran7 7wasthedominatinglanguagefor numbercrunchingIt'spredecessor FortranIVwasreplacedby manyprocedurallanguagesFortranhasa failrysimplesyntaxFortranisgood for onlyonething,NUMBERCRUNCHINGG unnar WollanIntroductoryFortranProgrammingAbou tFortran77and95 IntrotoFortran77programmingFortran95 Fortran95extendsFortran77withNicersyntax ,freeformatinsteadof xedformatUserde neddatatypesusingtheTYPE declaraionModulescontainingdatade nitionsandproceduredeclarationsNoimplici tvariabledeclaretions,avoidingtypingerro rsFortran77is a subsetoffortran95 Gunnar WollanIntroductoryFortranProgrammingAbou tFortran77and95 IntrotoFortran77programmingFortranversus otherlanguagesCis lowlevelandclosetothemachine.
4 Butcanbe error proneC++isa supersetofCandmorereliableJavais simplerandmorereliablethanC++Pythonis morehigh-levelthanJavaGunnar WollanIntroductoryFortranProgrammingAbou tFortran77and95 IntrotoFortran77programmingSpeedofFortra nversusotherlanguagesFortran77isregarded asveryfastCyieldslightlyslowercodeC++and fortran95areslowerthanFortran77 Javais muchslowerGunnar WollanIntroductoryFortranProgrammingAbou tFortran77and95 IntrotoFortran77programmingSomeguideline sFortran77givesveryfastprograms,buttheso urcecodeislessreadableandmoreerror proneduetoimplicitdeclarationsUseFortran 95for yourmainprogramandFortran77functionswher espeediscriticalSometimesthebestsolution is a combinationoflanguages, ,PythonandC++Usethelanguagebestsuitedfor yourproblemGunnar WollanIntroductoryFortranProgrammingAbou tFortran77and95 IntrotoFortran77programmingIntrotoFortra n77programming3 IntrotoFortran77programmingGunnar WollanIntroductoryFortranProgrammingIntr otoFortran77programmingIntrotoFortran95p rogrammingOur rstFortran77programGoal.
5 Make a programwritingthetext\HelloWorld"Impleme ntationWithoutdeclaringa stringWithstringdeclarationGunnar WollanIntroductoryFortranProgrammingIntr otoFortran77programmingIntrotoFortran95p rogrammingWithoutdeclaringa stringvariableC234567 PROGRAMhw1 WRITE(*,*)'HelloWorld'END PROGRAMhw1 Gunnar WollanIntroductoryFortranProgrammingIntr otoFortran77programmingIntrotoFortran95p rogrammingWithdeclaringa stringvariableC234567 PROGRAMhw1 CHARACTER*11strWRITE(*,*)strEND PROGRAMhw1 Gunnar WollanIntroductoryFortranProgrammingIntr otoFortran77programmingIntrotoFortran95p rogrammingSomecommentstothe\HelloWorld"p rogramFortran77uses xedformatThesourcecodeisdividedintoposit ionsonthelineThisis a heritagefromtheolddayswhencommunicationw iththecomputerwasby punchedcardsAcharacterinthe rstcolumnidenti estothecompilerthattherestofthelineisa commentThecoumns2 to5 isfor jumplabelsandformatspeci ersColumn6 is for continuationofthepreviouslineThecolumn7 to72isfor thesourcecodeColumn73to80isfor commentsGunnar
6 WollanIntroductoryFortranProgrammingIntr otoFortran77programmingIntrotoFortran95p rogrammingIntrotoFortran95programming4 IntrotoFortran95programmingGunnar WollanIntroductoryFortranProgrammingIntr otoFortran95programmingCompilingandlinki ngFortranprogramsScienti cHelloWorldinFortran95 Usage:. ,World!sin( )= rstcommand-lineargumentina oating-pointvariable2 Callthesinefunction3 Writea combinationoftextandnumberstothescreenGu nnar WollanIntroductoryFortranProgrammingIntr otoFortran95programmingCompilingandlinki ngFortranprogramsThecodePROGRAMhw1 IMPLICITNONEDOUBLEPRECISION:: r, sCHARACTER(LEN=80):: argv! InputargumentCALL getarg(1,argv)!
7 A C-functionr = a2d(argv)! Our own asciito! doubles = SIN(r)! The intrinsicSINE! functionPRINT*, 'HelloWordsin(',r,')=',sEND PROGRAMhw1 Gunnar WollanIntroductoryFortranProgrammingIntr otoFortran95programmingCompilingandlinki ngFortranprogramsDissection(1)Contraryto C++thecompilerdoesnotneedtoseadeclaratio nofsubroutinesandintrinsicfunctionsOnlye xternalfunctionsmustbe declaredCommentsinFortran95arethe! ona lineThecodeisfreeformatunlike Fortran77 Gunnar WollanIntroductoryFortranProgrammingIntr otoFortran95programmingCompilingandlinki ngFortranprogramsDissection(2)Allprogram swritteninFortranbeginswiththestatementP ROGRAM programnameandendswiththestatementENDwit htheoptionalPROGRAM programnameUnlike C++andotherprogramminglanguageFortranhas nobuiltintransferofcommandlineargumentsA calltoa C-functiongetarg(n,argv)transfersthen'th argumenttothecharacter-stringvariablearg vGunnar WollanIntroductoryFortranProgrammingIntr otoFortran95programmingCompilingandlinki ngFortranprogramsDissection(2)
8 FloatingpointvariablesinFortran1 REAL:singleprecision2 DOUBLEPRECISION:doubleprecisiona2d:youro wnasciistringtodoublefunction,Fortranhas nointrinsicfunctionsofthiskindincontrast toC/C++soyouhavetowritethisoneyourselfAu tomatictype conversion:DOUBLEPRECISION=REALTheSIN()f unctionisanintrinsicfunctionanddoesnotne eda speci cdeclarationGunnar WollanIntroductoryFortranProgrammingIntr otoFortran95programmingCompilingandlinki ngFortranprogramsAninteractiveversionLet usasktheuserfor therealnumberinsteadofreadingitfromtheco mmandlineWRITE(*.FMT='(A)',ADVANCE='NO') 'Givea realnumber:'READ(*,*)rs = SIN(r)! WollanIntroductoryFortranProgrammingIntr otoFortran95programmingCompilingandlinki ngFortranprogramsScienti cHelloWorldinFortran77C234567 PROGRAMhw1 REAL*8r,sCHARACTER*80argvCALL getarg(1,argv)r = a2d(argv)s = SIN(r)WRITE(*,*)'HelloWorld!
9 Sin(',r')=',sEND PROGRAMhw1 Gunnar WollanIntroductoryFortranProgrammingIntr otoFortran95programmingCompilingandlinki ngFortranprogramsDi erencesfromtheFortran95versionFortran77u sesREAL*8insteadofDOUBLEPRECISIONF ortran77lacksIMPLICITNONE directiveAdoubleprecisionvariablehastobe declaredinFortran77sincedefaultrealnumbe rsaresingleprecisionGunnar WollanIntroductoryFortranProgrammingIntr otoFortran95programmingCompilingandlinki ngFortranprogramsCompilingandlinkingFort ranprograms5 CompilingandlinkingFortran95programsGunn ar WollanIntroductoryFortranProgrammingComp ilingandlinkingFortran95programsManipula tedata
10 LesHowtocompileandlink(Fortran95)Onestep (compilingandliking):unix> steps:unix> #Compile, > #LinkAlinuxsystemwithIntelFortranCompile r:linux> WollanIntroductoryFortranProgrammingComp ilingandlinkingFortran95programsManipula tedata lesUsingthemake utility tocompilea programWhatisthemake utility?themake utility readsa lecontainingthename(s)ofthe le(s)tobe compiledtogehterwiththenameoftheexecutab leprogramThemake leis eithercalled\make le"or \Make le"asdefaultInvokingthemake utiltity:unix-linux>makeGunnar WollanIntroductoryFortranProgrammingComp ilingandlinkingFortran95programsManipula tedata lesAshortexampleofa make leFC= f90$(shellls *.)