Docsity
Docsity

Prepare-se para as provas
Prepare-se para as provas

Estude fácil! Tem muito documento disponível na Docsity


Ganhe pontos para baixar
Ganhe pontos para baixar

Ganhe pontos ajudando outros esrudantes ou compre um plano Premium


Guias e Dicas
Guias e Dicas


Fisica Computacional, Manuais, Projetos, Pesquisas de Física

Livro muito bom de programaçao em física para C++

Tipologia: Manuais, Projetos, Pesquisas

Antes de 2010

Compartilhado em 17/10/2010

afonso-junior-12
afonso-junior-12 🇧🇷

1 documento

1 / 345

Toggle sidebar

Esta página não é visível na pré-visualização

Não perca as partes importantes!

bg1
COMPUTATIONAL PHYSICS
M. Hjorth-Jensen
Department of Physics, University of Oslo, 2003
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Pré-visualização parcial do texto

Baixe Fisica Computacional e outras Manuais, Projetos, Pesquisas em PDF para Física, somente na Docsity!

COMPUTATIONAL PHYSICS

M. Hjorth-Jensen

Department of Physics, University of Oslo, 2003

iv

 We would like to give you an opportunity to gain a deeper understanding of the physics

you have learned in other courses. In most courses one is normally confronted with simple systems which provide exact solutions and mimic to a certain extent the realistic cases. Many are however the comments like ’why can’t we do something else than the box po- tential?’. In several of the projects we hope to present some more ’realistic’ cases to solve by various numerical methods. This also means that we wish to give examples of how physics can be applied in a much broader context than it is discussed in the traditional physics undergraduate curriculum.

 To encourage you to "discover" physics in a way similar to how researchers learn in the

context of research.

 Hopefully also to introduce numerical methods and new areas of physics that can be stud-

ied with the methods discussed.

 To teach structured programming in the context of doing science.

 The projects we propose are meant to mimic to a certain extent the situation encountered

during a thesis or project work. You will tipically have at your disposal 1-2 weeks to solve numerically a given project. In so doing you may need to do a literature study as well. Finally, we would like you to write a report for every project.

 The exam reflects this project-like philosophy. The exam itself is a project which lasts one

month. You have to hand in a report on a specific problem, and your report forms the basis for an oral examination with a final grading.

Our overall goal is to encourage you to learn about science through experience and by asking questions. Our objective is always understanding, not the generation of numbers. The purpose of computing is further insight, not mere numbers! Moreover, and this is our personal bias, 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 of the problem. Most of you are by now familiar, through various undergraduate courses in physics and math- ematics, with interpreted languages such as Maple, Mathlab and Mathematica. In addition, the interest in scripting languages such as Python or Perl has increased considerably in recent years. The modern programmer would typically combine several tools, computing environments and programming languages. A typical example is the following. Suppose you are working on a project which demands extensive visualizations of the results. To obtain these results you need however a programme which is fairly fast when computational speed matters. In this case you would most likely write a high-performance computing programme in languages which are tay- lored for that. These are represented by programming languages like Fortran 90/95 and C/C++. However, to visualize the results you would find interpreted languages like e.g., Matlab or script- ing languages like Python extremely suitable for your tasks. You will therefore end up writing e.g., a script in Matlab which calls a Fortran 90/95 ot C/C++ programme where the number crunching is done and then visualize the results of say a wave equation solver via Matlab’s large

v

library of visualization tools. Alternatively, you could organize everything into a Python or Perl script which does everything for you, calls the Fortran 90/95 or C/C++ programs and performs the visualization in Matlab as well. Being multilingual is thus a feature which not only applies to modern society but to comput- ing environments as well. However, there is more to the picture than meets the eye. This course emphasizes the use of programming languages like Fortran 90/95 and C/C++ instead of interpreted ones like Matlab or Maple. Computational speed is not the only reason for this choice of programming languages. The main reason is that we feel at a certain stage one needs to have some insights into the algo- rithm used, its stability conditions, possible pitfalls like loss of precision, ranges of applicability etc. Although we will at various stages recommend the use of library routines for say linear algebra^1 , our belief is that one should understand what the given function does, at least to have a mere idea. From such a starting point we do further believe that it can be easier to develope more complicated programs, on your own. We do therefore devote some space to the algorithms behind various functions presented in the text. Especially, insight into how errors propagate and how to avoid them is a topic we’d 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 Needless to say, these lecture notes are upgraded continuously, from typos to new input. And we do always benifit from your comments, suggestions and ideas for making these notes better. It’s through the scientific discourse and critics we advance.

(^1) Such library functions are often taylored to a given machine’s architecture and should accordingly run faster than user provided ones.

Contents

Part I

Introduction to Computational Physics

4 CHAPTER 1. INTRODUCTION

11 continues this discussion by extending to studies of phase transitions in statistical physics. Chapter 12 deals with Monte-Carlo studies of quantal systems, with an emphasis on variational Monte Carlo methods and diffusion Monte Carlo methods. In chapter 13 we deal with eigen- systems and applications to e.g., the Schrödinger equation rewritten as a matrix diagonalization problem. Problems from scattering theory are also discussed, together with the most used solu- tion methods for systems of linear equations. Finally, we discuss various methods for solving differential equations and partial differential equations in chapters 14-16 with examples ranging from harmonic oscillations, equations for heat conduction and the time dependent Schrödinger equation. The emphasis is on various finite difference methods. We assume that you have taken an introductory course in programming and have some famil- iarity with high-level and modern languages such as Java, C/C++, Fortran 77/90/95, etc. Fortran^1 and C/C++ are examples of compiled high-level languages, in contrast to interpreted ones like Maple or Matlab. In such compiled languages the computer translates an entire subprogram into basic machine instructions all at one time. In an interpreted language the translation is done one statement at a time. This clearly increases the computational time expenditure. More detailed aspects of the above two programming languages will be discussed in the lab classes and various chapters of this text. There are several texts on computational physics on the market, see for example Refs. [8, 4, ? ,? , 6, 9, 7, 10], ranging from introductory ones to more advanced ones. Most of these texts treat however in a rather cavalier way the mathematics behind the various numerical methods. We’ve also succumbed to this approach, mainly due to the following reasons: several of the methods discussed are rather involved, and would thus require at least a two-semester course for an intro- duction. In so doing, little time would be left for problems and computation. This course is a compromise between three disciplines, numerical methods, problems from the physical sciences and computation. To achieve such a synthesis, we will have to relax our presentation in order to avoid lengthy and gory mathematical expositions. You should also keep in mind that Computa- tional Physics and Science in more general terms consist of the combination of several fields and crafts with the aim of finding solution strategies for complicated problems. However, where we do indulge in presenting more formalism, we have borrowed heavily from the text of Stoer and Bulirsch [? ], a text we really recommend if you’d like to have more math to chew on.

1.1 Choice of programming language

As programming language we have ended up with preferring C/C++, but every chapter, except for the next, contains also in an appendix the corresponding Fortran 90/95 programs. Fortran (FORmula TRANslation) was introduced in 1957 and remains in many scientific computing environments the language of choice. The latest standard, Fortran 95 [? , 11,? ], includes ex- tensions that are familiar to users of C/C++. Some of the most important features of Fortran 90/95 include recursive subroutines, dynamic storage allocation and pointers, user defined data structures, modules, and the ability to manipulate entire arrays. However, there are several good

(^1) With Fortran we will consistently mean Fortran 90/95. There are no programming examples in Fortran 77 in this text.

1.2. DESIGNING PROGRAMS 5

reasons for choosing C/C++ as programming language for scientific and engineering problems. Here are some:

 C/C++ is now the dominating language in Unix and Windows environments. It is widely

available and is the language of choice for system programmers.

 The C/C++ syntax has inspired lots of popular languages, such as Perl, Python and Java.

 It is an extremely portable language, all Linux and Unix operated machines have a C/C++

compiler.

 In the last years there has been an enormous effort towards developing numerical libraries

for C/C++. Numerous tools (numerical libraries such as MPI[? ]) are written in C/C++ and interfacing them requires knowledge of C/C++. Most C/C++ and Fortran 90/95 compilers compare fairly well when it comes to speed and numerical efficiency. Although Fortran 77 and C are regarded as slightly faster than C++ or Fortran 90/95, compiler improvements during the last few years have diminshed such differences. The Java numerics project has lost some of its steam recently, and Java is therefore normally slower than C/C++ or F90/95, see however the article by Jung et al. for a discussion on numerical aspects of Java [? ].

 Complex variables, one of Fortran 77 and 90/95 strongholds, can also be defined in the

new ANSI C/C++ standard.

 C/C++ is a language which catches most of the errors as early as possible, typically at

compilation time. Fortran 90/95 has some of these features if one omits implicit variable declarations.

 C++ is also an object-oriented language, to be contrasted with C and Fortran 90/95. This

means that it supports three fundamental ideas, namely objects, class hierarchies and poly-

morphism. Fortran 90/95 has, through the MODULE declaration the capability of defining

classes, but lacks inheritance, although polymorphism is possible. Fortran 90/95 is then considered as an object-based programming language, to be contrasted with C/C++ which has the capability of relating classes to each other in a hierarchical way.

C/C++ is however a difficult language to learn. Grasping the basics is rather straightforward, but takes time to master. A specific problem which often causes unwanted or odd error is dynamic memory management.

1.2 Designing programs

Before we proceed with a discussion of numerical methods, we would like to remind you of some aspects of program writing. In writing a program for a specific algorithm (a set of rules for doing mathematics or a precise description of how to solve a problem), it is obvious that different programmers will apply

1.2. DESIGNING PROGRAMS 7

 Do not use GOTO structures in Fortran. Although all varieties of spaghetti are great culi-

naric temptations, spaghetti-like Fortran with many GOTO statements is to be avoided.

Extensive amounts of time may be wasted on decoding other authors programs.

 When you name variables, use easily understandable names. Avoid v1 when you can

use speed_of_light. Associatives names make it easier to understand what a specific

subprogram does.

 Use compiler options to test program details and if possible also different compilers. They

make errors too. Also, the use of debuggers like gdb is something we highly recommend during the development of a program.