




























































































Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Prepare-se para as provas
Estude fácil! Tem muito documento disponível na Docsity
Prepare-se para as provas com trabalhos de outros alunos como você, aqui na Docsity
Encontra documentos específicos para os exames da tua universidade
Prepare-se com as videoaulas e exercícios resolvidos criados a partir da grade da sua Universidade
Responda perguntas de provas passadas e avalie sua preparação.
Ganhe pontos para baixar
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Livro muito bom de programaçao em física para C++
Tipologia: Manuais, Projetos, Pesquisas
1 / 345
Esta página não é visível na pré-visualização
Não perca as partes importantes!





























































































iv
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.
context of research.
ied with the methods discussed.
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.
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.
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.
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.
reasons for choosing C/C++ as programming language for scientific and engineering problems. Here are some:
available and is the language of choice for system programmers.
compiler.
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 [? ].
new ANSI C/C++ standard.
compilation time. Fortran 90/95 has some of these features if one omits implicit variable declarations.
means that it supports three fundamental ideas, namely objects, class hierarchies and poly-
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.
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
Extensive amounts of time may be wasted on decoding other authors programs.
subprogram does.
make errors too. Also, the use of debuggers like gdb is something we highly recommend during the development of a program.