ebook img

Computational Physics - Department of Physics PDF

563 Pages·2013·4.79 MB·English
by  
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 Computational Physics - Department of Physics

Morten Hjorth-Jensen Computational Physics Lecture Notes Fall 2013 October 14 2013 University of Oslo, 2012 Preface So, ultimately, in order to understand nature it may be necessary to have a deeper understanding of mathematical relationships. But the real reason is that the subject is enjoyable, and although we humanscutnatureupindifferentways,andwehavedifferentcoursesindifferentdepartments,such compartmentalizationis really artificial, and we should take ourintellectualpleasureswhere we find them.RichardFeynman,TheLawsofThermodynamics. Why a preface you may ask? Isn’t that just a mere exposition of a raison d’eˆtre of an author’schoiceofmaterial,preferences,biases,teachingphilosophyetc.?ToalargeextentI can answer in the affirmativeto that. A preface ought to be personal. Indeed, what you will see in the various chapters of these notes represents how I perceive computational physics shouldbetaught. Thissetoflecturenotesservesthescopeofpresentingtoyouandtrainyouinanalgorith- micapproachtoproblemsinthesciences,representedherebytheunityofthreedisciplines, physics, mathematics and informatics. This trinity outlines the emerging field of computa- tionalphysics. Ourinsightinaphysicalsystem,combinedwithnumericalmathematicsgivesustherules for setting up an algorithm, viz. a set of rules for solving a particular problem. Our under- standing of the physical system under study is obviously gauged by the natural laws at play, theinitialconditions,boundaryconditionsandotherexternalconstraintswhichinfluencethe given system. Having spelled out the physics, for example in the form of a set of coupled partialdifferentialequations,weneedefficientnumericalmethodsinordertosetupthefinal algorithm.Thisalgorithmisinturncodedintoacomputerprogramandexecutedonavailable computingfacilities.Todevelopsuchanalgorithmicapproach,youwillbeexposedtoseveral physicscases,spanningfromtheclassicalpendulumtoquantummechanicalsystems.Wewill alsopresentsomeofthemostpopularalgorithmsfromnumericalmathematicsusedtosolve a plethoraofproblemsinthesciences. Finallywe willcodifythese algorithmsusing someof the most widely used programming languages, presently C, C++ and Fortran and its most recentstandardFortran20081.However,ahigh-levelandfullyobject-oriented languagelike Python is now emerging as a good alternative although C++ and Fortran still outperform Python when it comes to computational speed. In this text we offer an approach where one can write all programs in C/C++ or Fortran. We will also show you how to develop large programsinPythoninterfacingC++and/orFortranfunctionsforthosepartsoftheprogram whichareCPUintensive.Suchanapproachallowsyoutostructuretheflowofdatainahigh- level languagelikePythonwhiletasksofamererepetitiveandCPUintensivenatureareleft tolow-levellanguageslikeC++orFortran.Pythonallowsyoualsotosmoothlyinterfaceyour program with other software, such as plotting programs or operating system instructions. 1ThroughoutthistextwerefertoFortran2008asFortran,implyingthelateststandard. v vi Preface A typical Python program you may end up writing contains everything from compiling and runningyourcodestopreparingthebodyofafileforwritingupyourreport. Computersimulationsarenowadaysanintegralpartofcontemporarybasicandappliedre- search in the sciences. Computation is becoming as important as theory and experiment. In physics, computational physics, theoretical physics and experimental physics are all equally importantinourdailyresearchandstudiesofphysicalsystems.Physicsistheunityoftheory, experiment and computation2. Moreover, the ability "to compute" forms part of the essen- tial repertoire of research scientists. Several new fields within computational science have emerged and strengthened their positionsinthe lastyears, suchascomputationalmaterials science,bioinformatics,computationalmathematicsandmechanics,computationalchemistry andphysicsandsoforth,justtomentionafew.Thesefieldsunderscoretheimportanceofsim- ulations as a means to gain novel insights into physical systems, especially for those cases wherenoanalyticalsolutionscanbefoundoranexperimentistoocomplicatedorexpensive to carry out. To be able to simulate large quantal systems with many degrees of freedom suchasstronglyinteractingelectronsinaquantumdotwillbeofgreatimportanceforfuture directions in novel fields like nano-techonology. This ability often combines knowledge from many different subjects, in our case essentially from the physical sciences, numerical math- ematics,computinglanguages,topicsfromhigh-performacecomputingandsomeknowledge ofcomputers. In 1999, when I started this course at the department of physics in Oslo, computational physics and computational science in general were still perceived by the majority of physi- cists and scientists as topics dealing with just mere tools and number crunching, and not as subjectsoftheirown.Thecomputationalbackgroundofmoststudentsenlistingforthecourse on computationalphysics could spanfromdedicated hackers andcomputer freaks to people who basically had never used a PC. The majority of undergraduate and graduate students hadaveryrudimentaryknowledgeofcomputationaltechniquesandmethods.Questionslike ’doyouknowofbettermethodsfornumericalintegrationthanthetrapezoidalrule’werenot uncommon. I do happen to know of colleagues who applied for time at a supercomputing centre because they needed to invert matrices of the size of 104 104 since they were using × thetrapezoidalruletocomputeintegrals.WithGaussianquadraturethisdimensionalitywas easilyreducedtomatrixproblemsofthesizeof102 102,withmuchbetterprecision. × Morethan a decadelater most students have now been exposed to a fairlyuniform intro- duction to computers, basic programming skills and use of numerical exercises. Practically every undergraduate student in physics has now made a Matlab or Maple simulation of for example the pendulum, with or without chaotic motion. Nowadays most of you are famil- iar, through various undergraduate courses in physics and mathematics, with interpreted languages such as Maple, Matlab and/or Mathematica. In addition, the interest in scripting languagessuchasPythonorPerlhasincreasedconsiderablyinrecentyears.Themodernpro- grammer would typically combine several tools, computing environments and programming languages.Atypicalexampleisthefollowing.Supposeyouareworkingonaprojectwhichde- mandsextensivevisualizationsoftheresults.Toobtaintheseresults,thatistosolveaphysics problemslikeobtainingthedensity profileofaBose-Einstein condensate,youneed however aprogramwhichisfairlyfastwhencomputationalspeedmatters.Inthiscaseyouwouldmost 2Wementionedpreviouslythetrinityofphysics,mathematicsandinformatics.Viewingphysicsasthetrinity of theory, experiment and simulations is yet another example. It is obviously tempting to go beyond the sciences. History shows that triunes, trinities and for example triple deities permeate the Indo-European cultures(andprobablyallhumancultures),fromtheancientCeltsandHindustomoderndays.Theancient Celtsreveredmanysuchtrinues,theirworldwasdividedintoearth,seaandair,naturewasdividedinanimal, vegetableandmineralandthecardinalcolourswerered,yellowandblue,justtomentionafew.Asacurious digression, it was a Gaulish Celt, Hilary, philosopher and bishop of Poitiers (AD 315-367) in his work De TrinitatewhoformulatedtheHolyTrinityconceptofChristianity,perhapsinordertoaccomodatemilleniaof humandivinationpractice. Preface vii likelywriteahigh-performancecomputingprogramusingMonteCarlomethodsinlanguages which are tailored for that. These are represented by programming languages like Fortran andC++.However,tovisualizetheresultsyouwouldfindinterpretedlanguageslikeMatlab orscriptinglanguageslikePythonextremelysuitableforyourtasks.Youwillthereforeendup writingforexampleascriptinMatlabwhichcallsaFortranorC++programwherethenum- bercrunchingisdoneandthenvisualizetheresultsofsayawaveequationsolverviaMatlab’s largelibraryofvisualizationtools.Alternatively,youcouldorganizeeverything intoaPython or Perl script which does everything for you, calls the Fortran and/or C++ programs and performs the visualization in Matlab or Python. Used correctly, these tools, spanning from scripting languages to high-performance computing languages and vizualization programs, speedupyourcapabilitytosolvecomplicatedproblems.Beingmultilingualisthusanadvan- tagewhichnotonlyappliestoourglobalizedmodernsocietybuttocomputingenvironments aswell.ThistextshowsyouhowtouseC++andFortranasprogramminglanguages. Thereishowever moretothepicturethanmeetstheeye.Althoughinterpretedlanguages likeMatlab,MathematicaandMapleallowyounowadaystosolveverycomplicatedproblems, and high-level languages like Python can be used to solve computational problems, compu- tational speed and the capability to write an efficient code are topics which still do matter. To this end, the majority of scientists still use languages like C++ and Fortran to solve sci- entific problems. When you embark on a master or PhD thesis, you will most likely meet these high-performance computing languages. This course emphasizes thus the use of pro- gramming languages like Fortran, Python and C++ instead of interpreted ones like Matlab orMaple.Youshouldhowevernotethattherearestilllargedifferencesincomputertimebe- tweenforexamplenumericalPythonandacorrespondingC++programformanynumerical applicationsinthephysicalsciences,withacodeinC++orFortranbeingthefastest. Computationalspeedisnottheonlyreasonforthischoiceofprogramminglanguages.An- otherimportantreasonisthatwefeelthatatacertainstageoneneedstohavesomeinsights intothealgorithmused,itsstabilityconditions,possiblepitfallslikelossofprecision,ranges of applicability, the possibility to improve the algorithm and taylor it to special purposes etc etc. One of our major aims here is to present to you what we would dub ’the algorithmic approach’, a set of rules for doing mathematics or a precise description of how to solve a problem. To device an algorithm and thereafter write a code for solving physics problems is a marvelous way of gaining insight into complicated physical systems. The algorithm you end up writing reflects in essentially all cases your own understanding of the physics and themathematics(thewayyouexpressyourself)oftheproblem.Wedothereforedevotequite some space to the algorithms behind various functions presented in the text. Especially, in- sight into how errors propagate and how to avoid them is a topic we would like you to pay special attention to. Only then can you avoid problems like underflow, overflow and loss of precision. Such a control is not always achievable with interpreted languages and canned functions where the underlying algorithm and/or code is not easily accesible. Although we will at various stages recommend the use of library routines for say linear algebra3, our belief is that one should understand what the given function does, at least to have a mere idea. With such a starting point, we strongly believe that it can be easier to develope more complicatedprogramsonyourownusingFortran,C++orPython. Wehaveseveralotheraimsaswell,namely: • We would like to give you an opportunity to gain a deeper understanding of the physics youhavelearnedinothercourses.Inmostcoursesoneisnormallyconfrontedwithsimple systems which provide exact solutions and mimic to a certain extent the realistic cases. Manyarehoweverthecommentslike’whycan’twedosomethingelsethantheparticlein 3Suchlibraryfunctionsareoftentayloredtoagivenmachine’sarchitectureandshouldaccordinglyrunfaster thanuserprovidedones. viii Preface aboxpotential?’.Inseveraloftheprojectswehopetopresentsomemore’realistic’cases to solve by various numerical methods. This also means that we wish to give examples of howphysicscanbeappliedinamuchbroadercontextthanitisdiscussedinthetraditional physicsundergraduatecurriculum. • To encourage you to "discover" physics in a way similar to how researchers learn in the contextofresearch. • Hopefullyalsotointroducenumericalmethodsandnewareasofphysicsthatcanbestud- iedwiththemethodsdiscussed. • Toteachstructuredprogramminginthecontextofdoingscience. • Theprojectsweproposearemeanttomimictoacertainextentthesituationencountered during a thesis or project work. You will tipicallyhave atyour disposal 2-3 weeks to solve numerically a given project. In so doing you may need to do a literature study as well. Finally,wewouldlikeyoutowriteareportforeveryproject. Ouroverallgoalistoencourageyoutolearnaboutsciencethroughexperienceandbyasking questions. Our objective is always understanding and the purpose of computing is further insight, not mere numbers! Simulations can often be considered as experiments. Rerunning asimulationneednotbeascostlyasrerunninganexperiment. Needless to say, these lecture notes are upgraded continuously, from typos to new in- put. And wedo alwaysbenefit from your comments, suggestions and ideasfor making these notes better. It’s through the scientific discourse and critics we advance. Moreover, I have benefitted immensely from many discussions with fellow colleagues and students. In partic- ular I must mention Hans Petter Langtangen, Anders Malthe-Sørenssen, Knut Mørken and Øyvind Ryan, whose input during the last fifteen years has considerably improved these lecture notes. Furthermore, the time we have spent and keep spending together on the Computing in Science Education project at the University, is just marvelous. Thanks so much. Concerning the Computing in Science Education initiative, you can read more at http://www.mn.uio.no/english/about/collaboration/cse/. Finally, I would like to add a petit note on referencing. These notes have evolved over many years and the idea is that they should end up in the format of a web-based learning environment for doing computational science. It will be fully free and hopefully represent a muchmoreefficientwayofconveyingteachingmaterialthantraditionaltextbooks.Ihavenot yet settled on a specific format, so any input is welcome. At present however, it is very easy formetoupgradeandimprovethematerialonsayayearlybasis,fromsimpletypostoadding newmaterial.Whenaccessingthewebpageofthecourse,youwillhavenoticedthatyoucan obtainallsourcefilesfortheprogramsdiscussed inthetext. Manypeoplehavethuswritten to me about how they should properly reference this material and whether they can freely use it. My answer is rather simple. You are encouraged to use these codes, modify them, includetheminpublications,thesiswork,yourlecturesetc.Aslongasyouruseispartofthe dialectics of science you can use this material freely. However, since many weekends have elapsed in writing several of these programs, testing them, sweating over bugs, swearing in frontofa f*@?%gcodewhichdidn’tcompileproperlyten minutesbeforemondaymorning’s eight o’clock lecture etc etc, I would dearly appreciate in case you find these codes of any use,toreferencethemproperly.Thatcanbedoneinasimpleway,refertoM.Hjorth-Jensen, ComputationalPhysics, University of Oslo (2013). The weblink to the course should also be included.Hopeitisnottoomuchtoaskfor.Enjoy! Contents Part I Introduction to programming and numerical methods 1 Introduction ............................................................... 3 1.1 Choiceofprogramminglanguage ......................................... 5 1.2 Designingprograms..................................................... 6 2 Introduction to C++ and Fortran ........................................... 9 2.1 GettingStarted ......................................................... 9 2.1.1 Scientifichelloworld .............................................. 10 2.2 RepresentationofIntegerNumbers ....................................... 15 2.2.1 Fortrancodes..................................................... 18 2.3 RealNumbersandNumericalPrecision ................................... 19 2.3.1 Representationofrealnumbers..................................... 20 2.3.2 Machinenumbers ................................................. 22 2.4 ProgrammingExamplesonLossofPrecisionandRound-offErrors ........... 24 2.4.1 Algorithmsfore−x................................................. 24 2.4.2 Fortrancodes..................................................... 27 2.4.3 Furtherexamples ................................................. 30 2.5 AdditionalFeaturesofC++andFortran .................................. 32 2.5.1 OperatorsinC++ ................................................. 32 2.5.2 PointersandarraysinC++......................................... 34 2.5.3 MacrosinC++ ................................................... 36 2.5.4 StructuresinC++andTYPEinFortran ............................. 37 2.6 Exercises............................................................... 39 3 Numerical differentiation and interpolation ................................ 45 3.1 NumericalDifferentiation ................................................ 45 3.1.1 Thesecondderivativeofexp(x) ..................................... 49 3.1.2 Erroranalysis .................................................... 58 3.2 NumericalInterpolationandExtrapolation................................. 60 3.2.1 Interpolation ..................................................... 61 3.2.2 Richardson’sdeferredextrapolationmethod ......................... 64 3.3 ClassesinC++ ......................................................... 65 3.3.1 TheComplexclass ................................................ 67 3.3.2 Thevectorclass................................................... 72 3.4 ModulesinFortran...................................................... 86 3.5 HowtomakeFigureswithGnuplot........................................ 90 3.6 Exercises............................................................... 92 ix x Contents 4 Non-linear Equations ...................................................... 95 4.1 ParticleinaBoxPotential ................................................ 95 4.2 IterativeMethods ....................................................... 96 4.3 Bisection............................................................... 98 4.4 Newton-Raphson’sMethod............................................... 99 4.5 TheSecantMethod...................................................... 103 4.5.1 Broyden’sMethod................................................. 105 4.6 Exercises............................................................... 106 5 Numerical Integration ..................................................... 109 5.1 Newton-CotesQuadrature ............................................... 109 5.2 AdaptiveIntegration..................................................... 115 5.3 GaussianQuadrature .................................................... 116 5.3.1 Orthogonalpolynomials,Legendre .................................. 119 5.3.2 Integrationpointsandweightswithorthogonalpolynomials ........... 121 5.3.3 ApplicationtothecaseN=2 ....................................... 123 5.3.4 GeneralintegrationintervalsforGauss-Legendre..................... 124 5.3.5 Otherorthogonalpolynomials ...................................... 125 5.3.6 Applicationstoselectedintegrals ................................... 126 5.4 TreatmentofSingularIntegrals........................................... 128 5.5 ParallelComputing...................................................... 131 5.5.1 Briefsurveyofsupercomputingconceptsandterminologies ........... 131 5.5.2 Parallelism ....................................................... 132 5.5.3 MPIwithsimpleexamples ......................................... 134 5.5.4 NumericalintegrationwithMPI .................................... 139 5.6 AnIntegrationClass..................................................... 143 5.7 Exercises............................................................... 147 Part II Linear Algebra and Eigenvalues 6 Linear Algebra............................................................. 153 6.1 Introduction ............................................................ 153 6.2 MathematicalIntermezzo ................................................ 154 6.3 ProgrammingDetails .................................................... 157 6.3.1 Declarationoffixed-sized vectorsandmatrices....................... 158 6.3.2 RuntimeDeclarationsofVectorsandMatricesinC++................. 160 6.3.3 MatrixOperationsandC++andFortranFeaturesofMatrixhandling ... 162 6.4 LinearSystems ......................................................... 168 6.4.1 GaussianElimination .............................................. 170 6.4.2 LUDecompositionofaMatrix ...................................... 173 6.4.3 SolutionofLinearSystemsofEquations ............................. 177 6.4.4 InverseofaMatrixandtheDeterminant............................. 178 6.4.5 TridiagonalSystemsofLinearEquations............................. 185 6.5 SplineInterpolation ..................................................... 187 6.6 IterativeMethods ....................................................... 189 6.6.1 Jacobi’smethod................................................... 189 6.6.2 Gauss-Seidel...................................................... 190 6.6.3 Successiveover-relaxation ......................................... 191 6.6.4 ConjugateGradientMethod ........................................ 192 6.7 Avectorandmatrixclass ................................................ 194 6.7.1 Howtoconstructyourownmatrix-vectorclass ....................... 196 6.8 Exercises............................................................... 205 Contents xi 6.8.1 Solution.......................................................... 209 7 Eigensystems .............................................................. 213 7.1 Introduction ............................................................ 213 7.2 Eigenvalueproblems .................................................... 213 7.3 Similaritytransformations ............................................... 214 7.4 Jacobi’smethod......................................................... 215 7.5 SimilarityTransformationswithHouseholder’smethod...................... 220 7.5.1 TheHouseholder’smethodfortridiagonalization ..................... 220 7.5.2 DiagonalizationofaTridiagonalMatrixviaFrancis’Algorithm ......... 224 7.6 PowerMethods ......................................................... 225 7.7 Iterativemethods:Lanczos’algorithm..................................... 226 7.8 Schrödinger’sEquationThroughDiagonalization ........................... 227 7.8.1 NumericalsolutionoftheSchrödingerequationbydiagonalization ..... 230 7.8.2 Programexampleandresultsfortheone-dimensionalharmonicoscillator230 7.9 Exercises............................................................... 235 Part III Differential Equations 8 Differential equations...................................................... 243 8.1 Introduction ............................................................ 243 8.2 Ordinarydifferentialequations ........................................... 244 8.3 Finitedifferencemethods ................................................ 245 8.3.1 ImprovementsofEuler’salgorithm,higher-ordermethods............. 247 8.3.2 VerletandLeapfrogalgorithms ..................................... 248 8.3.3 Predictor-Correctormethods ....................................... 249 8.4 Moreonfinitedifferencemethods,Runge-Kuttamethods.................... 250 8.5 AdaptiveRunge-Kutta andmultistepmethods .............................. 253 8.6 Physicsexamples ....................................................... 255 8.6.1 Idealharmonicoscillations ......................................... 255 8.6.2 Dampingofharmonicoscillationsandexternalforces ................. 260 8.6.3 Thependulum,anonlineardifferentialequation ...................... 261 8.7 PhysicsProject:thependulum ............................................ 263 8.7.1 Analyticresultsforthependulum ................................... 263 8.7.2 Thependulumcode ............................................... 265 8.8 Exercises............................................................... 270 9 Two point boundary value problems ........................................ 283 9.1 Introduction ............................................................ 283 9.2 Shootingmethods....................................................... 284 9.2.1 Improvedapproximationtothesecondderivative,Numerov’smethod... 284 9.2.2 Waveequationwithconstantacceleration............................ 286 9.2.3 Schrödingerequationforsphericalpotentials ........................ 289 9.3 Numericalprocedure,shootingandmatching .............................. 291 9.3.1 AlgorithmforsolvingSchrödinger’sequation......................... 292 9.4 Green’sfunctionapproach ............................................... 294 9.5 Exercises............................................................... 297 xii Contents 10 Partial Differential Equations .............................................. 301 10.1Introduction ............................................................ 301 10.2Diffusionequation....................................................... 303 10.2.1ExplicitScheme................................................... 304 10.2.2ImplicitScheme................................................... 308 10.2.3Crank-Nicolsonscheme ............................................ 310 10.2.4SolutionfortheOne-dimensional DiffusionEquation .................. 313 10.2.5Explictschemeforthediffusionequationintwodimensions ........... 315 10.3Laplace’sandPoisson’sEquations ........................................ 315 10.3.1SchemeforsolvingLaplace’s(Poisson’s)equation .................... 316 10.3.2JacobiAlgorithmforsolvingLaplace’sEquation ...................... 319 10.3.3Jacobi’salgorithmextendedtothediffusionequationintwodimensions. 321 10.4WaveEquationintwoDimensions......................................... 322 10.4.1Closed-formSolution .............................................. 324 10.5Exercises............................................................... 325 Part IV Monte Carlo Methods 11 Outline of the Monte Carlo Strategy........................................ 337 11.1Introduction ............................................................ 337 11.1.1Definitions ....................................................... 339 11.1.2FirstIllustrationoftheUseofMonte-CarloMethods .................. 342 11.1.3SecondIllustration,ParticlesinaBox ............................... 346 11.1.4RadioactiveDecay ................................................ 349 11.1.5ProgramExampleforRadioactiveDecay............................. 349 11.1.6BriefSummary.................................................... 351 11.2ProbabilityDistributionFunctions......................................... 351 11.2.1MultivariableExpectationValues ................................... 354 11.2.2TheCentralLimitTheorem......................................... 356 11.2.3DefinitionofCorrelationFunctionsandStandardDeviation............ 358 11.3RandomNumbers....................................................... 359 11.3.1PropertiesofSelected RandomNumberGenerators................... 362 11.4ImprovedMonteCarloIntegration ........................................ 364 11.4.1ChangeofVariables ............................................... 365 11.4.2ImportanceSampling.............................................. 369 11.4.3Acceptance-RejectionMethod ...................................... 370 11.5MonteCarloIntegrationofMultidimensionalIntegrals ...................... 371 11.5.1BruteForceIntegration............................................ 372 11.5.2ImportanceSampling.............................................. 373 11.6ClassesforRandomNumberGenerators................................... 375 11.7Exercises............................................................... 376 12 Random walks and the Metropolis algorithm ............................... 381 12.1Motivation ............................................................. 381 12.2DiffusionEquationandRandomWalks..................................... 382 12.2.1DiffusionEquation ................................................ 382 12.2.2RandomWalks.................................................... 385 12.3MicroscopicDerivationoftheDiffusionEquation ........................... 387 12.3.1DiscretizedDiffusionEquationandMarkovChains.................... 389 12.3.2ContinuousEquations ............................................. 394 12.3.3NumericalSimulation ............................................. 395 12.4EntropyandEquilibriumFeatures ........................................ 397

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.