AUTHORED BY KONSTANTINOS N. ANAGNOSTOPOULOS
Physics Department, National Technical University of Athens, Zografou Campus, 15780 Zografou,
Greece
konstant@mail.ntua.gr, www.physics.ntua.gr/~konstant/
PUBLISHED BY KONSTANTINOS N. ANAGNOSTOPOULOS
and the
NATIONAL TECHNICAL UNIVERSITY OF ATHENS
Book Website:
www.physics.ntua.gr/~konstant/ComputationalPhysics
©Konstantinos N. Anagnostopoulos 2014, 2016
First Published 2014
Version1
1.1.20161207100600
Cover: Design by K.N. Anagnostopoulos. The front cover picture is a snapshot taken during Monte Carlo simulations of hexatic membranes. Work done with Mark J. Bowick. Relevant video at youtu.be/Erc7Q6YXfLk
This book and its cover(s) are subject to copyright. They are licensed under the Creative Commons
Attribution-ShareAlike 4.0 International License. To view a copy of this license, visit
creativecommons.org/licenses/by-sa/4.0/
The book is accompanied by software available at the book’s website. All the software, unless the copyright does not belong to the author, is open source, covered by the GNU public license, see www.gnu.org/licenses/. This is explicitly mentioned at the end of the respective source files.
ISBN 978-1-312-46441-4 (lulu.com, vol. I)
ISBN 978-1-312-46488-9 (lulu.com, vol. II)
The book’s website is at
http://www.physics.ntua.gr/~konstant/ComputationalPhysics/
From there, you can can download the accompanying software, which
contains, among other things, all the programs presented in the book.
Some conventions: Text using the font shown below refers to commands given using a shell (the “command line”), input and output of programs, code written in Fortran (or any other programming language), as well as to names of files and programs:
When a line starts with the prompt
then the text that follows is a command, which can be given from the command line of a terminal. The second line, Hello World, is the output of the command.
The contents of a file with Fortran code is listed below:
What you need in order to work on your PC:
If you have installed a GNU/Linux distribution on your computer, all of the above can be installed easily. For example, in a Debian like distribution (Ubuntu, ...) the commands
install all the necessary tools.
If you don’t wish to install GNU/Linux on your computer, you can try the following:
This book is the culmination of my ten years’ experience in teaching three introductory, undergraduate level, scientific computing/computational physics classes at the National Technical University of Athens. It is suitable mostly for junior or senior level science courses, but I am currently teaching its first chapters to sophomores without a problem. A two semester course can easily cover all the material in the book, including lab sessions for practicing.
Why another book in computational physics? Well, when I started teaching those classes there was no bibliography available in Greek, so I was compelled to write lecture notes for my students. Soon, I realized that my students, majoring in physics or applied mathematics, were having a hard time with the technical details of programming and computing, rather than with the physics concepts. I had to take them slowly by the hand through the “howto” of computing, something that is reflected in the philosophy of this book. Hoping that this could be useful to a wider audience, I decided to translate these notes in English and put them in an order and structure that would turn them into “a book”.
I also decided to make the book freely available on the web. I was partly motivated by my anger caused by the increase of academic (e)book prices to ridiculous levels during times of plummeting publishing costs. Publishers play a diminishing role in academic publishing. They get an almost ready-made manuscript in electronic form by the author. They need to take no serious investment risk on an edition, thanks to print-on-demand capabilities. They have virtually zero cost ebook publishing. Moreover, online bookstores have decreased costs quite a lot. Academic books need no advertisement budget, their success is due to their academic reputation. I don’t see all of these reflected on reduced book prices, quite the contrary, I’m afraid.
My main motivation, however, is the freedom that independent publishing would give me in improving, expanding and changing the book in the future. It is great to have no length restrictions for the presentation of the material, as well as not having to report to a publisher. The reader/instructor that finds the book long, can read/print the portion of the book that she finds useful for her.
This is not a reference book. It uses some interesting, I hope, physics problems in order to introduce the student to the fundamentals of solving a scientific problem numerically. At the same time, it keeps an eye in the direction of advanced and high performance scientific computing. The reader should follow the instructions given in each chapter, since the book teaches by example. Several skills are taught through the solution of a particular problem. My lectures take place in a (large) computer lab, where the students are simultaneously doing what I am doing (and more). The program that I am editing and the commands that I am executing are shown on a large screen, displaying my computer monitor and actions live. The book provides no systematic teaching of a programming language or a particular tool. A very basic introduction is given in the first chapter and then the reader learns whatever is necessary for the solution of her problem. There is more than one way to do it8 and the problems can be solved by following a basic or a fancy way, depending on the student’s computational literacy. The book provides the necessary tools for both. A bibliography is provided at the end of the book, so that the missing pieces of a puzzle can be sought in the literature.
This is also not a computational physics playground. Of course I hope that the reader will have fun doing what is in the book, but my goal is to provide an experience that will set the solid foundation for her becoming a high performance computing, number crunching, heavy duty data analysis expert in the future. This is why the programming language of the core numerical algorithms has been chosen to be Fortran, a highly optimized, scientifically oriented, programming language. The computer environment is set in a Unix family operating system, enriched by all the powerful GNU tools provided by the FSF9 . These tools are indispensable in the complicated data manipulation needed in scientific research, which requires flexibility and imagination. Of course, Fortran is not the best choice for heavy duty object oriented programming, and is not optimal for interacting with the operating system. The philosophy10 is to let Fortran do what is best for, number crunching, and leave data manipulation and file administration to external, powerful tools. Tools, like awk, shell scripting, gnuplot, Perl and others, are quite powerful and complement all the weaknesses of Fortran mentioned before. The plotting program is chosen to be gnuplot, which provides very powerful tools to manipulate the data and create massive and complicated plots. It can also create publication quality plots and contribute to the “fun part” of the learning experience by creating animations, interactive 3d plots etc. All the tools used in the book are open source software and they are accessible to everyone for free. They can be used in a Linux environment, but they can also be installed and used in Microsoft Windows and Mac OS X.
The other hard part in teaching computational physics to scientists and engineers is to explain that the approach of solving a problem numerically is quite different from solving it analytically. Usually, students of this level are coming with a background in analysis and fundamental physics. It is hard to put them into the mode of thinking about solving a problem using only additions, multiplications and some logical operations. The hardest part is to explain the discretization of a model defined analytically, which can be done in many ways, depending on the accuracy of the approximation. Then, one has to extrapolate the numerical solution, in order to obtain a good approximation of the analytic one. This is done step by step in the book, starting with problems in simple motion and ending with discussing finite size scaling in statistical physics models in the vicinity of a continuous phase transition.
The book comes together with additional material which can be found at the web page of the book11 . The accompanying software contains all the computer programs presented in the book, together with useful tools and programs solving some of the exercises of each chapter. Each chapter has problems complementing the material covered in the text. The student needs to solve them in order to obtain hands on experience in scientific computing. I hope that I have already stressed enough that, in order for this book to be useful, it is not enough to be read in a café or in a living room, but one needs to do what it says.
Hoping that this book will be useful to you as a student or as an instructor, I would like to ask you to take some time to send me feedback for improving and/or correcting it. I would also appreciate fan mail or, if you are an expert, a review of the book. If you use the book in a class, as a main textbook or as supplementary material, I would also be thrilled to know about it. Send me email at konstantmail.ntua.gr and let me know if I can publish, anonymously or not, (part of) what you say on the web page (otherwise I will only use it privately for my personal ego-boost). Well, nothing is given for free: As one of my friends says, some people are payed in dollars and some others in ego-dollars!
Have fun computing scientifically!
Athens, 2014.
The aim of this chapter is to lay the grounds for the development of the computational skills which are necessary in the following chapters. It is not an in depth exposition but a practical training by example. For a more systematic study of the topics discussed, we refer to the bibliography. Many of the references are freely available in the web.
The are many choices that one has to make when designing a computer project. These depend on the needs for numerical efficiency, on available programming hours, on the needs for extensibility and upgradability and so on. In this book we will get the flavor of a project that is mostly scientifically and number crunching oriented. One has to make the best of the available computing resources and have powerful tools available for a productive analysis of the data. Such an environment, found in most of today’s supercomputers, that offers flexibility, dependability, simplicity, powerful tools for data analysis and effective compilers is provided by the family of the Unix operating systems. The GNU/Linux operating system is a Unix variant that is freely available and most of its utilities are open source software. The voluntary work of millions of excellent programmers worldwide has built the most stable, fastest and highest quality software available for scientific computing today. Thanks to the idea of the open source software pioneered by Richard Stallman1 this giant collaboration has been made possible.
Another choice that we have to make is the programming language, and this is going to be Fortran. Fortran has been built mainly for numerical applications and it has been used by many scientists and engineers because of its efficiency in high performance computing. The language is simple and compilers are able to optimize, parallelize and vectorize the code very efficiently. There is a lot of scientific and engineering software available in libraries written in Fortran, which has been used and tested extensively for many years. This is a crucial factor for scientific software, so that it can be trusted to be efficient and free of errors. Fortran is not the best choice for interacting with the operating system or for text processing. This shortcoming can be easily overcome by the use of external tools and Fortran can be left to do what she has been designed for: number crunching. Its structure is simple and can be used both for procedural and object oriented programming, in such a way that, it will not make the life of an inexperienced programmer difficult, and at the same time provide high level, abstract and powerful tools for high performance, modular, object oriented, programming needed in a large and complicated project.
Fortran, as well as other languages like C, C++ and Java, is a language that needs to be compiled by a compiler. Other languages, like perl, awk, shell scripting, Macsyma, Mathematica, Octave, Matlab, , are interpreted line by line. These languages can be simple in their use, but they can be prohibitively slow when it comes to a numerically demanding program. A compiler is a tool that analyzes the whole program and optimizes the computer instructions executed by the computer. But if programming time is more valuable, then a simple, interpreted language can lead to faster results.
Another choice that we make in this book, and we mention it because it is not the default in most Linux distributions, is the choice of shell. The shell is a program that “connects” the user to the operating system. In this book, we will teach how to use a shell2 to “send” commands to the operating system, which is the most effective way to perform complicated tasks. We will use the shell tcsh, although most of the commands can be interpreted by most popular shells. Shell scripting is simpler in this shell, although shells like bash provide more powerful tools, mostly needed for complicated system administration tasks. That may cause a small inconvenience to some readers, since tcsh is not preinstalled in Linux distributions3 .
The Unix family of operating systems offer an environment where complicated tasks can be accomplished by combining many different tools, each of which performs a distinct task. This way, one can use the power of each tool, so that trivial but complicated parts of a calculation don’t have to be programmed. This makes the life of a researcher much easier and much more productive, since research requires from us to try many things before we understand how to compute what we are looking for.
In the Unix operating system everything is a file, and files are organized in a unique and unified filesystem. Documents, pictures, music, movies, executable programs are files. But also directories or devices, like hard disks, monitors, mice, sound cards etc, are, from the point of view of the operating system, files. In order for a music file to be played by your computer, the music data needs to be written to a device file, connected by the operating system to the sound card. The characters you type in a terminal are read from a file “the keyboard”, and written to a file “the monitor” in order to be displayed. Therefore, the first thing that we need to understand is the structure of the Unix filesystem.
There is at least one path in the filesystem associated with each file. There are two types of paths, relative paths and absolute paths. These are two examples:
The paths shown above may refer to the same or a different file. This depends on “where we are”. If “we are” in the directory /home/george, then both paths refer to the same file. If on the other way “we are” in a directory /home/john or /home/george/CompPhys, then the paths refer4 to two different files. In the last two cases, the paths refer to the files
respectively. How can we tell the difference? An absolute path always begins with the / character, whereas a relative path does not. When we say that “we are in a directory”, we refer to a position in the filesystem called the current directory, or working directory. Every process in the operating system has a unique current directory associated with it.
The filesystem is built on its root and looks like a tree positioned upside down. The symbol of the root is the character / The root is a directory. Every directory is a file that contains a list of files, and it is connected to a unique directory, its parent directory . Its list of files contains other directories, called its subdirectories, which all have it as their parent directory. All these files are the contents of the directory. Therefore, the filesystem is a tree of directories with the root directory at its top which branch to its subdirectories, which in their turn branch into other subdirectories and so on. There is practically no limit to how large this tree can become, even for a quite demanding environment5 .
A path consists of a string of characters, with the characters / separating its components, and refers to a unique location in the filesystem. Every component refers to a file. All, but the last one, must be directories in a hierarchy, from parent directory to subdirectory. The only exception is a possible / in the beginning, which refers to the root directory. Such an example can be seen in figure 1.1.
In a Unix filesystem there is complete freedom in the choice of the location of the files6 . Fortunately, there are some universally accepted conventions respected by almost everyone. One expects to find home directories in the directory /home, configuration files in the directory /etc, application executables in directories with names such as /bin, /usr/bin, /usr/local/bin, software libraries in directories with names such as /lib, /usr/lib etc.
There are some important conventions in the naming of the paths. A single dot “.” refers to the current directory and a double dot “..” to the parent directory. Similarly, a tilde “~” refers to the home directory of the user. Assume, e.g., that we are the user george running a process with a current directory /home/george/Music/Rock (see figure 1.1). Then, the following paths refer to the same file /home/george/Doc/lyrics.doc:
Notice that ~ and ~george refer to the home directory of the user george (ourselves), whereas ~mary refer to the home directory of another user, mary.
We are now going to introduce the basic commands for filesystem navigation and manipulation7 . The command cd (=change directory) changes the current directory, whereas the command pwd (=print working directory) prints the current directory:
The argument of the command cd is an absolute or a relative path. If the path is correct and we have the necessary permissions, the command changes the current directory to this path. If no path is given, then the current directory changes to the home directory of the user. If the character - is given instead of a path, then the command changes the current directory to the previous current directory.
The command mkdir creates new directories, whereas the command rmdir removes empty directories. Try:
Note that the command mkdir cannot create directories more than one level down the filesystem, whereas the command mkdir -p can. The “switch” -p makes the behavior of the command different than the default one.
In order to list the contents of a directory, we use the command ls (=list):
The first command is given without an argument and it lists the contents of the current directory. The second one, lists the contents of the subdirectory of the current directory Programs. If the argument is a list of paths pointing to regular files, then the command prints the names of the paths. Another way of giving the command is
The switch -l makes ls to list the contents of the current directory together with useful information on the files in 9 columns. The first column lists the permissions of the files (see below). The second one lists the number of links of the files8 . The third one lists the user who is the owner of each file. The fourth one lists the group that is assigned to the files. The fifth one lists the size of the file in bytes (=8 bits). The next three ones list the modification time of the file and the last one the paths of the files.
File permissions9 are separated in three classes: owner permissions, group permissions and other permissions. Each class is given three specific permissions, r=read, w=write and x=execute. For regular files, read permission effectively means access to the file for reading/copying, write permission means permission to modify the contents of the file and execute permission means permission to execute the file as a command10 . For directories, read permission means that one is able to read the names of the files in the directory (but not make it as current directory with the cd command), write permission means to be able to modify its contents (i.e. create, delete, and rename files) and execute permission grants permission to access/modify the contents of the files (but not list the names of the files, this is granted by the read permission).
The command ls -l lists permissions in three groups. The owner (positions 2-4), the group (positions 5-7) and the rest of the world (others - positions 8-10). For example
In the first case, the owner has read and write but not execute permissions and the group+others have only read permissions. In the second case, the user has read, write and execute permissions, the group has read permissions and others have no permissions at all. In the last case, the user has read, write and execute permissions, whereas the group and the world have only execute permissions. The first character d indicates a special file, which in this case is a directory. All special files have this position set to a character, while regular files have it set to -.
File permissions can be modified by using the command chmod:
Using the first command, the owner (u user) obtains (+) permission to execute (x) the file named file. Using the second one, the rest of the world (o others) and the group (ggroup) loose (-) the write (w) permission to the files named file1 and file2. Using the third one, everyone (aall) obtain read (r) permission on the file named file.
We will close this section by discussing some commands which are used for administering files in the filesystem. The command cp (copy) copies the contents of files into other files:
If the file file2.f90 does not exist, the first command copies the contents of file1.f90 to a new file file2.f90. If it already exists, it replaces its contents by the contents of the file file2.f90. In order for the second command to be executed, Programs needs to be a directory. Then, the contents of the files file1.f90, file2.f90, file3.f90 are copied to indentical files in the directory Programs. Of course, we assume that the user has the appropriate privileges for the command to be executed successfully.
The command mv “moves”, or renames, files:
The first command renames the file file1.f90 to file2.f90. The second one moves files file1.f90, file2.f90, file3.f90 into the directory Programs.
The command rm (remove) deletes files11 . Beware, the command is unforgiving: after deletion, a file cannot be restored into the filesystem12 . Therefore, after executing successfully the following commands
the files file1.f90, file2.f90, file3.f90 do not exist in the filesystem anymore. A more prudent use of the command demands the flag -i. Then, before deletion we are asked for confirmation:
When we type y, the file is deleted, when we type n, the file is not deleted.
We cannot remove directories the same way. It is possible to use the command rmdir in order to remove empty directories. In order to delete directories together with their contents (including subdirectories and their contents) use the command13 rm -r. For example, assume that the contents of the directories dir1 and dir1/dir2 are the files:
Then the results of the following commands are:
The last command removes all files (assuming that we have write permissions for all directories and subdirectories). Alternatively, we can empty the contents of all directories first, and then remove them with the command rmdir:
Note that by using a semicolon, we can execute two or more commands on the same line.
Commands in a Unix operating system are files with execute permission. When we write a sentence on the command line, like
the shell reads its and interprets it. The shell is a program that creates a interface between a user and the operating system. The first word (ls) of the sentence is interpreted as a command. The rest of the words are the arguments of the command and the program can use them (or not) at the discretion of its programmer. There is a special convention for arguments that begin with a - (e.g. -l, --help, --version, -O3). They are called options or switches, and they act as virtual switches that make the program act in a particular way. We have already seen that the program ls gives a different output with the switch -l.
In order for a command to be executed, the shell looks for a file that has the same name as the command (here a file named ls). In order to understand where the shell looks for such a file, we should digress a little bit and explain the use of shell variables and environment variables. These have a name, which is a string of permissible characters, and their values are obtained by preceding their name with the $ character. For example the variable PATH has value $PATH. The values of the environment variables can be set with the command14 setenv and of the shell variables with the command set:
Two special variables are the variables PATH and path:
The first one is an environment variable and the second one is a shell variable. Their values are set by the shell, and we don’t need to worry about them, unless we want to change them. Their value is a string of characters whose components should be valid paths to directories. In the first case, the components are separated by a :, while in the second case, by one or more spaces. In the example shown above, the shell searches each component of the path or PATH variables (in this order) until it finds a file ls in their contents. If it succeeds and the file has execute permissions, then the program in this file is executed. If it fails, then it prints an error message. Try the commands:
We see that the program that the ls command executes the program in the file /bin/ls.
The arguments of a command are passed on to the program that the command executes for possible interpretation. For example:
The argument -l is the switch that results in a long listing of the files. The arguments test.f90 and test.dat are interpreted by the program ls as paths that it will look up for file information.
You can use the * (wildcard) character as a shorthand notation for a group of files. For example, in the command shown below
the shell will expand *.f90 and *.dat to a list of all files whose names end with .f90 or .dat. Therefore, if the current directory contains the files test.f90, test1.f90, myprog.f90, test.dat, hello.dat, the arguments that will be passed on to the command ls are
For each command there are three special files associated with it. The first one is the standard input (stdin), the second one is the standard output (stdout) and the third one the standard error (stderr). These are files where the program can print or read data from. By default, these files are the terminal that the user uses to execute the command. In this case, when the program reads data from the stdin, then it reads the data that we type to the terminal using the keyboard. When the program writes data to the stdout or to the stderr, then the data is written to the terminal.
The advantage of using these special files in order to read/write data is that the user can redirect the input/output to these files to any file she wants. Using the character > at the end of a command redirects the stdout to the file whose name is written after >. For example:
The first of the above commands, prints the contents of the current working directory to the terminal. The second command redirects data written to the stdout to the file results. After executing the command, the file results is created and its contents are the names of the files file1.f90 file2.f90 file3.f90 file4.csh. If the file results does not exist (as in the above example), the file is created. If it already exists, it is truncated and its contents replaced by the data written to the stdout of the command. If we want to append data without erasing the existing contents, then we should use the string of characters >>. Therefore, if we give the command
after executing the previous commands, then the contents of the file results will be
The redirection of the stdin is accomplished by the use of the character < while that of the stderr by the use of the string of characters15 >&. We will see more examples in section 1.2.
It is possible to redirect the stdout of a command to be the stdin of another command. This is very useful for creating filters. A filter is a command that creates a flow of data between two or more programs. This process is called piping. Pipes are creating by using the character |
Using the syntax shown above, the stdout of the command cmd1 is redirected to the stdin of the command cmd2, the stdout of the command cmd2 is redirected to the stdin of the command cmd3 etc. More examples will be presented in section 1.2.
Unix got itself a reputation for not being user friendly. This is far from the truth. Although there is a steep learning curve, detailed documentation for almost everything is available online.
The key for a comfortable ride is to learn how to use the help system available on your computer and on the internet. Most of the commands are self documented. A simple test, like the one shown below, will help you with the basic usage of most of the commands:
For example, try the command ls --help. For a window application, start from the menu “Help”. You should not be afraid and/or lazy and you should proceed with careful searching and reading.
For example, let’s assume that you have heard about a command that sounds like printf, or something like that. The first level of online help is the man (=manual) command that searches the “man pages”. Read the output of the command
The command info usually provides more detailed and user friendly documentation. It has basic browsing capabilities like the browsers you use to read pages from the internet. Try the command
will inform you that there are other, possibly related, commands with names like fprintf, fwprintf, wprintf, sprintf...:
The second column printed by the whatis command is the “section” of the man pages. In order to gain access to the information in a particular section, you have to give it as an argument to the man command:
Section 1 of the man pages contains information of ordinary command line commands, section 3 contains information on functions in libraries of the C language. Section 2 contains information on commands used for system administration. You may browse the directory /usr/share/man, or read the man page of the man command (use the command man man for that!).
By using the command
we obtain plenty of memory refreshing information. The command
shows us many files related to the command printf. The commands
give information on the location of the executable(s) of the command printf.
Another useful feature of the shell is the command or it filename completion. This means that we can write only the first characters of the name of a command or filename and then press simultaneously the keys [Ctrl-d]16 (i.e. press the key Ctrl and the key of the letter d at the same time). Then the shell will complete the name of the command up to the point that is is unique with the given string of characters17 :
Try to type an x on the command line and then type [Ctrl-d]. You will learn all the commands that are available and whose name begins with an x: xterm, xeyes, xclock, xcalc, ...
Finally, the internet contains a wealth of information. Google your blues... and you will be rewarded!
For doing data analysis, we will need powerful tools for manipulating data in text files. These are files that consist solely of printable characters. Some tools that can be used in order to construct complicated and powerful filters are the programs cat, less, head, tail, grep, sort and awk.
Suppose that we have data in a file named data18 which contains information on the contents of a food warehouse and their prices:
prints the contents of the file data to the stdout. In general, this command prints the contents of all files given in its arguments or the stdin if none is given. Since the stdin and the stdout can be redirected, the command
takes the contents of the file data from the stdin and prints them to the stdout, which in this case is the file data1. This command has the same result as the command:
The command
prints the contents of the file data and then the contents of the file data1 to the stdout. Since the stdout is redirected to the file data2, data2 contains the data of both files.
you can browse the data contained in the file gfortran.txt one page at a time. Press [space] in order to “turn” a page, [b] to turn back a page. Press the up and down arrows to move one line backwards/forward. Press [g] in order to jump to the beginning of the file and press [G] in order to jump to the end. Press [h] in order to get a help message and press [q] in order to quit.
print the first line, the last two lines and the second to the last line of the file data to the stdout respectively. Note that, by piping the stdout of the command tail to the stdin of the command head, we are able to construct the filter “print the line before the last one”.
The command sort sorts the contents of a file by comparing each line of its text with all others. The sorting is alphabetical, unless otherwise set by using options. For example
For reverse sorting, try sort -r data. We can also sort by comparing specific fields of each line. By default, fields are words separated by one or more spaces. For example, in order to sort w.r.t. the second column of the file data, we can use the switch -k 2 (=second field). Furthermore, we can use the switch -n for numerical sorting:
If we omit the switch -n, the comparison of the lines is performed based on character sorting of the second field and the result is
The last column contains floating point numbers (not integers). In order to sort by the values of such numbers we should use the switch -g:
The command grep processes a text file line by line, searching for a given string of characters. When this string is found anywhere in a line, this line is printed to the stdout. The command
prints each line containing the string “kilos”. If we want to search for all line not containing the string “kilos”, then we add the switch -v:
We can use a regular expression for searching a whole family of strings of characters. These monsters need a full book for discussing them in detail! But it is not hard to learn how to use some simple forms of regular expressions. Here are some examples:
The first one, prints each line whose first character is a b. The second one, prints each line that ends with a 0. The third one, prints each line contaning the strings 32 or 34.
By far, the strongest tool in our toolbox is the awk program. By default, awk analyzes a text file line by line. Each word (or field in the awk jargon) of these lines is stored in a set of variables with names $1, $2, .... The variable $0 contains the full line currently processed, whereas the variable NF counts the number of fields in the current line. The variable NR counts the number of lines of the file processed so far by awk.
An awk program can be written in the command line. A set of commands within { ... } is executed for each line of input. The constructs BEGIN{ ... } and END{ ... } contain commands executed, only once, before and after the processing of the file respectively. For example, the command
prints the name of the product (1st column = $1) and the total value stored in the warehouse (2nd column = $2) (4th column = $4). More examples are given below:
The first one calculates the total value of all products: The processing of each line results in the increment (+=) of the variable value by the product of the second and fourth fields. In the end (END{ ... }), the string Total= is printed, together with the final value of the variable value. This is an easy way for computing the sum of the values calculated for each line. The second command, calculates and prints an average. The sum is calculated in each line and stored in the variable av. In the end, we print the quotient of the sum of all values by the number of lines that have been processed (NR). The last command shows a (crazy) mathematical expression based on numerical values found in each line of the file data: It computes the square of the second field times the sine of the fourth field plus the exponential of the fourth field.
There is much more potential in the commands presented above. Reading the documentation and getting experience by using them will provide you with very strong tools in order to accomplish complicated tasks.
For a programmer that spends many hours programming every day, the environment and the tools available for editing the commands of a large and complicated program determine, to a large extent, the quality of her life! An editor edits the contents of a text file, that consists solely of printable characters. Such editors, available in most Linux environments, are the programs gedit, vim, pico, nano, zile... They provide basic functionality such as adding, removing or changing text within a file as well as more complicated functions, such as copying, pasting, searching and replacing text etc. There are many functions that are particularly useful to a programmer, such as detecting and formatting keywords of a particular programming language, pretty printing, closing scopes etc, which can be very useful for comfortable programming and for spotting errors. A very powerful and “knowledgeable” editor, offering many such functions for several programming languages, is the GNU Emacs editor19 . Emacs is open source software, it is available for free and can be used in most available operating systems. It is programmable20 and the user can automate most of her everyday repeated tasks and configure it to her liking. There is a full interaction with the operating system, in fact Emacs has been built with the ambition of becoming an operating system. For example, a programmer can edit a Fortran file, compile it, debug it and run it, everything done with Emacs commands.
Note the character & at the end of the line. This makes the particular command to run in the background. Without it, the shell waits until a command exits in order to return the prompt.
In a desktop environment, Emacs starts in its own window. For a quick and dirty editing session, or in the case that a windows environment is not available21 , we can run Emacs in a terminal mode. Then, we omit the & at the end of the line and we run the command
The switch -nw forces Emacs to run in terminal mode.
We can interact with Emacs in various ways. Newbies will prefer buttons and menus that offer a simple and intuitive interface. For advanced usage, however, we recommend that you make an effort to learn the keyboard shortcuts. There are also thousands of functions available to be used interactively. They are called from a “command line”, called the minibuffer in the Emacs jargon.
Keyboard shortcuts are usually combinations of keystrokes that consist of the simultaneous pressing of the Ctrl or Alt keys together with other keys. Our convention is that a key sequence starting with a C- means that the characters that follow are keys simultaneously pressed with the Ctrl key. A key sequance starting with a M- means that the characters that follow are keys simultaneously pressed with the Alt key22 . Some commands have shortcuts consisting of two or more composite keystrokes. For example by C-x C-c we mean that we have to press simultaneously the Ctrl key together with x and then press simultaneously the Ctrl key together with c. This sequence is a shortcut to the command that exits Emacs. Another example is C-x 2 which means to press the Ctrl key together with x and then press only the key 2. This is a shortcut to the command that splits a window horizontally to two equal parts.
The most useful shortcuts are M-x (press the Alt key siumutaneously with the x key) and C-g. The first command takes us to the minibuffer where we can give a command by typing its name. For example, type M-x and then type save-buffers-kill-emacs in the minibuffer (this will terminate Emacs). The second one is an “SOS button” that interrupts anything Emacs does and returns control to the working buffer. This can be pretty handy when a command hangs or destroys our work and we need to interrupt it.
The conventions for the mouse events are as follows: With Mouse-1, Mouse-2 and Mouse-3 we denote a simple click with the left, middle and right buttons of the mouse respectively. With Drag-Mouse-1 we mean to press the left button of the mouse and at the same time drag the mouse.
We summarize the possible ways of giving a command in Emacs with the following examples that have the same effect: Open a file and put its contents in a buffer for editing.
The number of available commands increases from the top to the bottom of the above list.
In order to edit a file, Emacs places the contents of a file in a buffer. Such a buffer is a chunk of computer memory where the contents of the file are copied and it is not the file itself. When we make changes to the contents of a buffer, the file remains intact. For our changes to take effect and be written to the file, we have to save the buffer. Then, the contents of the buffer are written back to the file. It is important to understand the following cycle of events:
Emacs may have more than one buffers open for editing simultaneously. By default, the name of the buffer is the same as the name of the file that is edited, although this is not necessary23 . The name of a buffer is written in the modeline of the window of the buffer, as can be seen in figure 1.3.
If Emacs crashes or exits before we save our edits, it is possible to recover (part of) them. There is a command M-x recover-file that will guide us through the necessary recovery steps, or we can look for a file that has the same name as the buffer we were editing surrounded by two #. For example, if we were editing the file file.f90, the automatically saved changes can be found in the file #file.f90#. Auto saving is done periodically by Emacs and its frequency can be controlled by the user.
The point where we insert text while editing is called “the point”. This is right before the blinking cursor24 . Each buffer has another position marked by “the mark”. A point and the mark define a “region” in the buffer. This is a part of the text in the buffer where the functions of Emacs can act (e.g. copy, cut, change case, spelling etc.). We can set the region by setting a point and then press C-SPC25 or give the command M-x set-mark-command. This defines the current point to be the mark. Then we can move the cursor to another point which will define a region together with the mark that we set. Alternatively we can use Drag-Mouse-1 (hold the left mouse button and drag the mouse) and mark a region. The mark can be set with Mouse-3, i.e. with a simple click of the right button of the mouse. Therefore by Mouse-1 at a point and then Mouse-3 at a different point will set the region between the two points.
We can open a file in a buffer with the command C-x C-f, and then by typing its path. If the file already exists, its contents are copied to a buffer, otherwise a new buffer is created. Then:
When we finish editing (or frequently enough so that we don’t loose our work due to an unfortunate event), we save the changes in the buffer, either by pressing the save icon on the toolbar, or by pressing the keys C-s, or by giving the command M-x save-buffer.
Use the instructions below for slightly more advanced editing:
We note that cutting and pasting can be made between different windows of the same or different buffers.
Sometimes it is very convenient to edit one or more different buffers in two or more windows. The term “windows” in Emacs refers to regions of the same Emacs desktop window. In fact, a desktop window running an Emacs session is referred to as a frame in the Emacs jargon. Emacs can split a frame in two or more windows, horizontally or/and vertically. Study figure 1.5 on page 81 for details. We can also open a new frame and edit several buffers simultaneously27 . We can manipulate windows and frames as follows:
You can have many windows in a dumb terminal. This is a blessing when a dekstop environment is not available. Of course, in that case you cannot have many frames.
Each buffer can be in different modes. Each mode may activate different commands or editing environment. For example each mode can color keywords relevant to the mode and/or bind keys to different commands. There exist major modes, and a buffer can be in only one of them. There are also minor modes, and a buffer can be in one or more of them. Emacs activates major and minor modes by default for each file. This usually depends on the filename but there are also other ways to control this. The user can change both major and minor modes at will, using appropriate commands.
Active modes are shown in a parenthesis on the mode line (see figures 1.3 and 1.5.
Some interesting minor modes are:
In a desktop environment, we can choose modes from the menu of the mode line. By clicking with Mouse-3 on the name of a mode we are offered options for (de)activating minor modes. With a Mouse-1 we can (de)activate the read-only mode with a click on :%% or :-- respectively. See figure 1.5.
Emacs’ documentation is impressive. For newbies, we recommend to follow the mini course offered by the Emacs tutorial. You can start the tutorial by typing C-h t or select Help Emacs Tutorial from the menu. Enjoy... The Emacs man page (give the man emacs command in the command line) will give you a summary of the basic options when calling Emacs from the command line.
A quite detailed manual can be found in the Emacs info pages28 . Using info needs some training, but using the Emacs interface is quite intuitive and similar to using a web browser. Type the command C-h r (or choose HelpEmacs Tutorial from the menu) and you will open the front page of the emacs manual in a new window. By using the keys SPC and Backspace we can read the documentation page by page. When you find a link (similar to web page hyperlinks), you can click on it in order to open to read the topic it refers to. Using the navigation icons on the toolbar, you can go to the previous or to the next pages, go up one level etc. There are commands that can be given by typing single characters. For example, type d in order to jump to the main info directory. There you can find all the available manuals in the info system installed on your computer. Type g (emacs) and go to the top page of the Emacs manual. Type g (info) and read the info manual.
Emacs is structured in an intuitive and user friendly way. You will learn a lot from the names of the commands: Almost all names of Emacs commands consist of whole words, separated by a hyphen “-”, which almost form a full sentence. These make them quite long sometimes, but by using auto completion of their names this does not pose a grave problem.
You can customize everything in Emacs. From key bindings to programming your own functions in the Elisp language. The most common way for a user to customize her Emacs sessions, is to put all her customization commands in the file /.emacs in her home directory. Emacs reads and executes all these commands just before starting a session. Such a .emacs file is given below:
Everything after a ; is a comment. Functions/commands are enclosed in parentheses. The first three ones bind the keys F1, C-c s and M-s to the commands save-buffer, save-some-buffers and isearch-forward respectively. The next one defines an alias of a command. This means that, when we give the command M-x is in the minibuffer, then the command isearch-forward will be executed. The last two commands are the definitions of the functions (fm) and (sign), which can be called interactively from the minibuffer.
For more complicated examples google “emacs .emacs file” and you will see
other users’ .emacs files. You may also customize Emacs from the menu
commands OptionsCustomize Emacs. For learning the Elisp language, you
can read the manual “Emacs Lisp Reference Manual” found at the address
www.gnu.org/software/emacs/manual/elisp.html
In this section, we give a very basic introduction to the Fortran programming language. This is not a systematic exposition and you are expected to learn what is needed in this book by example. So, please, if you have not already done it, get in front of a computer and do what you read. You can find many good tutorials and books introducing Fortran in a more complete way in the bibliography.
The first program that one writes when learning a new programming language is the “Hello World!” program. This is the program that prints “Hello World!” on your screen:
Commands, or statements, in Fortran are strings of characters separated by blanks (“words”) that we are allowed to write from the 1st to the 132nd column of a file. Each line starts a new command29 . We can put more than one command on each line by separating them with a semicolon (;). Everything after an exclamation mark (!) is a comment. Proliferation of comments is necessary for documenting our code. Good documentation of our code is an integral part of programming. If the code is planned to be read by others, or by us at a later time, make sure to explain in detail what each line is supposed to do. You and your collaborators will save a lot of time in the process of debugging, improving and extending your code.
The main entry to the program is defined by the command program name, where name can be any string of alphanumeric characters and an underscore. When the program runs, it starts executing commands at this point. The end of the program, as well as of any other program unit (functions, subroutines, modules), is defined by the line end program name.
The first (and only) command given in the above program is the print command. It prints the string of characters “Hello World!” to the stdout. The “*,” is part of the syntax and it is not printed, of course. Fortran does not distinguish capital from small letters, so we could have written PRINT, Print, prINt, ... A string of characters in Fortran is enclosed in single or double quotes (’Hello World!’ or "Hello World!" is equivalent).
In order to execute the commands in a program, it is necessary to compile it. This is a job done by a program called the compiler that translates the human language programming statements into binary commands that can be loaded to the computer memory for execution. There are many Fortran compilers available, and you should learn which compilers are available for use in your computing environment. Typical names for Fortran compilers are gfortran, f90, ifort, g95, .... You should find out which compiler is best suited for your program and spend time reading its documentation carefully. It is important to learn how to use a new compiler so that you can finely tune it to optimize the performance of your program.
We are going to use the open source and freely available compiler gfortran, which can be installed on most popular operating systems30 . The compilation command is:
The switch -o defines the name of the executable file, which in our case is hello. If the compilation is successful, the program runs with the command:
Now, we will try a simple calculation. Given the radius of a circle we will compute its length and area. The program can be found in the file area_01.f90:
The first two commands define the values of the variables PI and R. These variables are of type REAL, which are floating point numbers. Fortran has implicit rules that can be used to define the type of variables. By default, variables whose name starts with i, j, k, l, m and n are of INTEGER type. These are exact whole numbers. All other variables are of type REAL31 . We can override these implicit rules by explicitly declaring the type of a variable or by changing the implicit rules with the use of the implicit statement. The following two commands have two effects: Computing the length and the area of the circle and printing the results. The expressions 2.0*PI*R and PI*R**2 are evaluated before being printed by the print command. The multiplication and raising to a power operators are * and **, respectively. Note the explicit decimal points at the constants 2.0 and 4.0. If we write 2 or 4 instead, then these are going to be constants of the INTEGER type and by using them the wrong way we may obtain surprising results32 . We compile and run the program with the commands:
We will now try a process that repeats itself for many times. We will calculate the length and area of 10 circles of different radii , . We will store the values of the radii in an array R(10) of the REAL type. The code can be found in the file area_02.f90:
The command dimension R(10) defines an array of length 10. This way, the elements of the array are referred by an index that takes value from 1 to 10. For example R(4) is the fourth element of the array.
we can write commands that are repeatedly executed while the INTEGER variable i takes values from 2 to 10 with increasing step33 equal to 1. The command:
defines the i-th radius to have a value which is larger by the (i-1)-th by 1. For the loop to work correctly, we must define the initial value of R(1), otherwise the final result is undefined34 . The second loop uses the defined R-values in order to do the computation and printing of the results.
Now, we will write an interactive version of the program. Instead of hard coding the values of the radii, we will interact with the user asking her to give her own values. The program will read the 10 values of the radii from the standard input (stdin). The program can be found in the file area_03.f90:
The first statement in the above program is implicit none! This statement deactivates the implicit rules of Fortran, and the programmer is obliged to declare all variables in a program unit. It is highly recommendable that you always use this option... You might spend a little more time typing the declarations, but this effort cannot be compared to the pain looking for bugs due to typos in the names of variables35 ! We will follow this practice throughout the book.
The declarations of the variables follow this statement. The variables N and i are declared to be of the INTEGER type, whereas the variables PI, area, perimeter and R(N) are declared to be of the REAL type. The variables PI and N are specified to be parameters. Parameters are given specific values which cannot be changed during the execution of the program.
The array elements R(i) are read using the command read:
The command read reads from the stdin. The user types the values at the terminal and then presses [Enter]. We can read more than one variables with one read command.
In order to print data to a file, we have to connect it to a unit. Each unit is represented by any number between 0 and 99. Some numbers are reserved for special units36 . The connection of a unit to a file is done with the open command. When this is done, we can write to the file with the command37 write(n,*), where n is the unit number. When we are done writing to a file we should use the command close(n). Then the unit number is available to be used for a different file. The flow of commands is like
The name of the file is determined by the option FILE=’AREA.DAT’ of the open statement. Uppercase or lowercase characters in the filename make a difference. The option FILE=’path’ can use any valid path in the filesystem, provided that we have the necessary permissions.
The line
shows us how to continue a line containing a long statement to the next one. We place a & at the end of the line and then continue writing the statement to the next. This can happen up to 39 times.
The next step will be to learn how to define and use functions and subroutines. The program below shows how to define a subroutine area_of_circle, which computes the length and area of a circle of given radius. The following program can be found in the file area_04.f90:
The calculation of the length and the area of the circle is performed by the call to the subroutine:
The command call calls a subroutine and transfers the control of the program within the subroutine. The above subroutine has the arguments (R(i),perimeter,area). The argument R(i) is an input variable. It provides the necessary data to the subroutine in order to perform its computation. The arguments perimeter and area are intended for output. Upon return of the subroutine to the main program, they store the result of the computation. The user of a subroutine must learn how to use its arguments in order to be able to call it in her program. These must be documented carefully by the programmer of the subroutine.
The actual program executed by the subroutine is between the lines:
The arguments (R,L,A) must be declared in the subroutine and need not have the same names as the ones that we use when we call it. A change of their values within the subroutine will change the values of the corresponding variables in the calling program38 . Therefore, the statements L=PI2*R and A=PI*R*R change the values of the variables perimeter and area to the desired values. The command return returns the control to the calling program. The parameters PI and PI2 are “private” to the subroutine. Their names and values are invisible outside the subroutine. Similarly, the variables i, N, ..., defined in the main program, are invisible within the subroutine.
We summarize all of the above in a program trionymo.f90, which computes the roots of a second degree polynomial:
The program reads the coefficients of the polynomial . After a check whether , it computes the discriminant by calling the function Discriminant(a,b,c). The only difference between a function and a subroutine is that the first one returns a value of a given type. We don’t need to use the command call in order to run the commands of a function, this is done by computing its value in an expression. The type of the value returned must be declared both in the program that uses the function (real :: Discriminant) and at the entry point of its program unit (real function Discriminant(a,b,c)). The value returned to the calling program is the one assigned to the variable that has the same name as the function:
Notice the use of the comparison operators .gt. (strictly greater than) and .eq. (equal to)39 :
You may skip this paragraph during a first reading of the book. It is intended mainly to be a reference when reading the later chapters.
There are more types of variables built in Fortran. In the program listed below, we show how to use CHARACTER variables, floating point numbers of double precision REAL(8) and complex numbers of single and double precision, COMPLEX and COMPLEX(8) respectively:
Some interesting points of the program above are:
Another important point to discuss is how to be able to access the same variables from different program units. So far, we simply mentioned that variables have a scope within each function and subroutine. If we wish to have access to the same location of memory40 from different program units, then we use the COMMON statement which defines a common block. See the following example:
The common block has the name CONSTANTS and we can refer to it from any program unit. Each program unit that uses this common block must use the same declaration, although the names of variables are allowed to be different. The common block CONSTANTS points to the same location in the computer memory, where we expect to find the values of two real variables. These variables (k1 and k2) are used and have their values changed in the subroutines s1 and s2. The variable k3, is a different variable in each program unit. The program prints
One of the weaknesses of Fortran is that it does not have a convenient control for Input/Output (I/O). For complicated I/O and text manipulation we will use other programs that can do a better job, like awk, perl, shell scripting, or programs written in C/C++. It is important to know some details about I/O commands in Fortran, mainly the specifications that control the accuracy of printed floating point numbers. So far, I/O commands, like print, write, read, used a * in order to control the printing of numbers. But we can replace the * with explicit format directives as follows:
Note the parentheses within the single quotes: (A5,I6,F12.7) is a format directive for the print statement. The A is for printing a CHARACTER, the I for printing an INTEGER and the F for printing a floating point number. The numbers after the letter declare the number of spaces used for printing each one. Beware! If the printing space is not enough, Fortran will not print and you will find a series of * in place of the value of your result! Bummer... In order to estimate the number of spaces needed for a floating point number, you have to include the space taken by the decimal point, the sign, the exponent character, the sign of the exponent and the digits needed for the exponent. Plus a space to separate the numbers in between... So, be generous and give plenty of printing space. In the example shown above, A5 denotes a character of 5 spaces, I6 and integer of 6 spaces and F12 a floating point number of 12 spaces. The decimal point in F12.7 means that we want a floating point with the accuracy of 7 significant digits.
The format directive (F18.16,E24.17,G24.17,G24.17) shows how to print double precision variables. These provide an accuracy of 16-17 significant digits and there is no need for keeping more digits. The command E prints a number in scientific form with an exponent. The command G prints the exponent when it is needed. The numbers before the letters denote multiplicity. Therefore 3F20.16 instructs the printing of 3 floating point numbers by reserving 20 spaces and using 16 significant digits for each one of them.
The command write(6,’(200F12.6)’)(a(i), i=1,10) shows how to print a large array using an implicit loop. We used many more spaces than actually needed (200F12.16) which is OK. If the array gets larger by increasing the range of i, then we will have enough room for printing in the same line. The program prints (we have folded the long line in order to make it visible):
We can organize the format commands by using the FORMAT statement. Then, we use labeled statements in order to refer to them. Labels are numbers put in the beginning of a line which should be unique to a program unit and are within the range 1-99999. We can transfer the control of the program to such a line with a goto command or by using the label in the I/O statements print, write and read as in the example shown below:
The reader should also study the Fortran intrinsic functions shown in table 1.2, page 181.
You may skip this section during the first reading of this book. It will be useful to come back here later.
Arrays are related data of the same type which can be accessed by using one or more indices. For example, after a declaration real, dimension(10) :: A, the expressions
refer to its 10 real values. The indices can be integer expressions, for example
where in the last case we used the integer value of the intrinsic function INT(x), which returns the integer part of x. Note that, arrays and functions enclose indices and arguments between parentheses (...) which are of the same style, and the compiler must look at their declarations in order to tell the difference. Examples of array declarations are
which declare the arrays a, b, c, d, which are of the real kind, with elements a(1) ... a(10), b(1) ... b(10), c(1) ... c(20) and d(1) ... d(20). An equivalent declaration is
or
In the last form, we show how to use constant parameters for declaring the size of arrays. For the declarations shown above, the lower bound of all arrays is 1 and the upper bound for a and b is 10 and for c and d is 20. The upper and lower bound of arrays can be explicitly determined. The declarations
define the real array a with 11 values a(0) ... a(10) and the array c with 31 values c(-10) c(-9) ... c(-1) c(0) c(1) ... c(20).
The arrays shown above have dimension 1 and they are like vectors. We can declare arrays of more than one dimension. This means that we need more than one indices in order to determine an array element41 . Therefore, the declaration
defines an integer array with values a(1,1), a(1,2), a(2,1) and a(2,2). The following declarations define two three dimensional real arrays a and b:
Some important definitions used in the bibliography are:
The values of arrays can be set the same way as scalars:
Alternatively we can use the name of the array as one object:
The first line defines the values of an array by using an array constructor. The second line defines all elements of the array b to be equal to 0. This is an example of a very convenient feature of the Fortran language. If all the arrays in an expression are conformable, then we can use the intrinsic Fortran operations to act on whole arrays. Two arrays are conformable if they have the same shape or if one of them is a scalar. Therefore the program
is equivalent to
Many Fortran intrinsic functions are elemental. This means that their arguments can be arrays, in which case the function acts on each array element separately. For example, the commands
set c(i,j) = sin(d(i,j))+x*exp(-2.0*d(i,j)) for all i and j, and the elements of a(i) equal to a random number uniformly distributed in the interval . We should stress that in order for two arrays to be conformable, it is not necessary that they have the same lower and upper bounds. For example, the command b=c*d in the following program has the same effect as the do loop:
In the following, we mention some useful functions that act on arrays. Assume that
then
You can find more functions and documentation in the bibliography [11, 10]. In the following, we provide some information related to the Input/Output (I/O) of arrays. Input (“reading”) and output (“writing”) of array values can be done by reading and writing their elements in any order we want. In the example below, we read the array a and write the array b in two different ways:
Inside the do loops, input and output is done one element per line from/to standard input/output. The commands (a(i), i=1,4) and ( (b(i,j) i=1,2), j=1,2) are implied do loops and read/write from/to the same line. During input, if the number of values for a are exhausted, then the program tries to read values from the following line(s). Similarly, if the output of b exhausts the maximum number of characters per line, then the output continues in the next line42 . Try it...
We can also preform I/O of arrays without explicit reference to their elements. In this case, the arrays are read/written in a specified order. For example, the program
reads the values a(1) a(2) a(3) a(4) from the stdin. Then, it continues reading b(1,1), b(2,1), b(1,2), b(2,2) from the next line (record). Notice that the array b is read in a column major way. Printing a and b, will print a(1) a(2) a(3) a(4) and b(1,1), b(2,1), b(1,2), b(2,2) in two different records (also in column major mode).
Finally, we summarize some of the Fortran capabilities in array manipulation. More details can be found in the bibliography. Read the comments in the program for a partial explanation of each command:
The code shown above can be found in the file f90_arrays.f90 of the accompanying software.
Plotting data is an indispensable tool for their qualitative, but also quantitative, analysis. Gnuplot is a high quality, open source, plotting program that can be used for generating publication quality plots, as well as for heavy duty analysis of a large amount of scientific data. Its great advantage is the possibility to use it from the command line, as well as from shell scripts and other programs. Gnuplot is programmable and it is possible to call external programs in order manipulate data and create complicated plots. There are many mathematical functions built in gnuplot and a fit command for non linear fitting of data. There exist interactive terminals where the user can transform a plot by using the mouse and keyboard commands.
This section is brief and only the features, necessary for the following chapters, are discussed. For more information visit the official page of gnuplot http://gnuplot.info. Try the rich demo gallery at http://gnuplot.info/screenshots/, where you can find the type of graph that you want to create and obtain an easy to use recipe for it. The book [14] is an excellent place to look for many of gnuplot’s secrets43 .
You can start a gnuplot session with the gnuplot command:
There is a welcome message and then a prompt gnuplot> is issued waiting for your command. Type a command an press [Enter]. Type quit in order to quit the program. In the following, when we show a prompt gnuplot>, it is assumed that the command after the prompt is executed from within gnuplot.
Plotting a function is extremely easy. Use the command plot and x as the independent variable of the function44 . The command
plots the function which is a straight line with slope 1. In order to plot many functions simultaneously, you can write all of them in one line:
The above command plots the functions , , and . Within the square brackets [:], we set the limits of the and axes, respectively. The bracket [-5:5] sets and the bracket [-2:4] sets . You may leave the job of setting such limits to gnuplot, by omitting some, or all of them, from the respective positions in the brackets. For example, typing [1:][:5] changes the lower and upper limits of and and leaves the upper and lower limits unchanged45 .
In order to plot data points , we can read their values from files. Assume that a file data has the following numbers recorded in it:
The first line is taken by gnuplot as a comment line, since it begins with a #. In fact, gnuplot ignores everything after a #. In order to plot the second column as a function of the first, type the command:
The name of the file is within double quotes. After the keyword using, we instruct gnuplot which columns to use as the and coordinates, respectively. The keywords with points instructs gnuplot to add each pair to the plot with points.
The command
plots the third column as a function of the first, and the keywords with lines instruct gnuplot to connect each pair with a straight line segment.
We can combine several plots together in one plot:
The first line plots the 1st and 3rd columns in the file data together with the function . The second line adds the plot of the 1st and 2nd columns in the file data and the third line adds the plot of the function .
There are many powerful ways to use the keyword using. Instead of column numbers, we can put mathematical expressions enclosed inside brackets, like using (...):(...). Gnuplot evaluates each expression within the brackets and plots the result. In these expressions, the values of each column in the file data are represented as in the awk language. $i are variables that expand to the number read from columns i=1,2,3,.... Here are some examples:
The first line plots the 1st column of the file data together with the value , where , and are the numbers in the 2nd, 1st and 3rd columns respectively. The second line adds the plot of the function .
The first line plots the logarithm of the 1st column together with the logarithm of the square of the 2nd column.
We can plot the data written to the standard output of any command. Assume that there is a program called area that prints the perimeter and area of a circle to the stdout in the form shown below:
The interesting data is at the second and fourth columns. These can be plotted directly with the gnuplot command:
All we have to do is to type the full command after the < within the double quotes. We can create complicated filters using pipes as in the following example:
The filter produces data to the stdout, by combining the action of the commands area, sort and awk. The data printed by the last program is in two columns and we plot the results using 1:2.
In order to save plots in files, we have to change the terminal that gnuplot outputs the plots. Gnuplot can produce plots in several languages (e.g. PDF, postscript, SVG, LATEX, jpeg, png, gif, etc), which can be interpreted and rendered by external programs. By redirecting the output to a file, we can save the plot to the hard disk. For example:
The first line makes the plot as usual. The second one sets the output to be in the JPEG format and the third one sets the name of the file to which the plot will be saved. The fourth lines repeats all the previous plotting commands and the fifth one closes the file data.jpg. The last line chooses the interactive terminal wxt to be the output of the next plot. High quality images are usually saved in the PDF, encapsulated postcript or SVG format. Use set terminal pdf,postscript eps or svg, respectively.
And now a few words for 3-dimensional (3d) plotting. The next example uses the command splot in order to make a 3d plot of the function . After you make the plot, you can use the mouse in order to rotate it and view it from a different perspective:
If you have data in the form and you want to create a plot of , write the data in a file, like in the following example:
Note the empty line that follows the change of the value of the first column. If the name of the file is data3, then you can plot the data with the commands:
We close this section with a few words on parametric plots. A parametric plot on the plane (2-dimensions) is a curve , where is a parameter. A parametric plot in space (3-dimensions) is a surface , where are parameters. The following commands plot the circle and the sphere :
Complicated system administration tasks are not among the strengths of the Fortran programming language. But in a typical GNU/Linux environment, there exist many powerful tools that can be used very effectively for this purpose. This way, one can use Fortran for the high performance and scientific computing part of the project and leave the administration and trivial data analysis tasks to other, external, programs.
One can avoid repeating the same sequence of commands by coding them in a file. An example can be found in the file script01.csh:
This is a very simple shell script. The first line instructs the operating system that the lines that follow are to be interpreted by the program /bin/tcsh46 . This can be any program in the system, which in our case is the tcsh shell. The following lines are valid commands for the shell, one in each line. They compile the Fortran programs found in the files that we created in section 1.4 with gfortran, and then they run the executable ./area. In order to execute the commands in the file, we have to make sure that the file has the appropriate execute permissions. If not, we have to give the command:
Then we simply type the path to the file script01.csh
and the above commands are run the one after the other. Some of the versions of the programs that we wrote are asking for input from the stdin, which, normally, you have to type on the terminal. Instead of interacting directly with the program, we can write the input data to a file Input, and run the command
A more convenient solution is to use the, so called, “Here Document”. A “Here Document” is a section of the script that is treated as if it were a separate file. As such, it can be used as input to programs by sending its “contents” to the stdin of the command that runs the program47 . The “Here Document” does not appear in the filesystem and we don’t need to administer it as a regular file. An example of using a “Here Document” can be found in the file script02.csh:
The stdin of the command ./area is redirected to the contents between the lines
The string EOF marks the beginning and the end of the “Here Document”, and can be any string you like. The last EOF has to be placed exactly in the beginning of the line.
The power of shell scripting lies in its programming capabilities: Variables, arrays, loops and conditionals can be used in order to create a complicated program. Shell variables can be used as discussed in section 1.1.2: The value of a variable name is $name and it can be set with the command set name = value. An array is defined, for example, by the command
and its data can be accessed using the syntax $R[1] ... $R[10].
Lets take a look at the following script:
The first two lines of the script define the values of the arrays files (4 values) and R (10 values). The command echo echoes its argument to the stdin. $USER is the name of the user running the script. ‘date‘ is an example of command substitution: When a command is enclosed between backquotes and is part of a string, then the command is executed and its stdout is pasted back to the string. In the example shown above, ‘date‘ is replaced by the current date and time in the format produced by the date command.
The foreach loop
is executed once for each of the 4 values of the array files. Each time the value of the variable file is set equal to one of the values area_01.f90, area_02.f90, area_03.f90, area_04.f90. These values can be used by the commands in the loop. Therefore, the command gfortran $file -o area compiles a different file each time that it is executed by the loop.
The last line in the loop
is a conditional. It executes the command cat AREA.DAT if the condition -f AREA.DAT is true. In this case, -f constructs a logical expression which is true when the file AREA.DAT exists.
We close this section by presenting a more complicated and advanced script. It only serves as a demonstration of the shell scripting capabilities. For more information, the reader is referred to the bibliography [16, 17, 18, 19, 20]. Read carefully the commands, as well as the comments which follow the # mark. Then, write the commands to a file script04.csh48 , make it an executable file with the command chmod u+x script04.csh and give the command
The script will run with the words “This is my first serious tcsh script” as its arguments. Notice how these arguments are manipulated by the script. Then, the script asks for the values of the radii of ten or more circles interactively, so that it will compute their perimeter and area. Type them on the terminal and then observe the script’s output, so that you understand the function of each command. You will not regret the time investment!
awk | search for and process patterns in a file, |
cat | display, or join, files |
cd | change working directory |
chmod | change the access mode of a file |
cp | copy files |
date | display current time and date |
df | display the amount of available disk space |
diff | display the differences between two files |
du | display information on disk usage |
echo | echo a text string to output |
find | find files |
grep | search for a pattern in files |
gzip | compress files in the gzip (.gz) format (gunzip to uncompress) |
head | display the first few lines of a file |
kill | send a signal (like KILL) to a process |
locate | search for files stored on the system (faster than find) |
less | display a file one screen at a time |
ln | create a link to a file |
lpr | print files |
ls | list information about files |
man | search information about command in man pages |
mkdir | create a directory |
mv | move and/or rename a file |
ps | report information on the processes run on the system |
pwd | print the working directory |
rm | remove (delete) files |
rmdir | remove (delete) a directory |
sort | sort and/or merge files |
tail | display the last few lines of a file |
tar | store or retrieve files from an archive file |
top | dynamic real-time view of processes |
wc | counts lines, words and characters in a file |
whatis | list man page entries for a command |
where | show where a command is located in the path (alternatively: whereis) |
which | locate an executable program using ”path” |
zip | create compressed archive in the zip format (.zip) |
unzip | get/list contents of zip archive |
When a particle moves on the plane, its position can be given in Cartesian coordinates . These, as a function of time, describe the particle’s trajectory. The position vector is , where and are the unit vectors on the and axes respectively. The velocity vector is where
The acceleration is given byIn this section we study the kinematics of a particle trajectory, therefore we assume that the functions are known. By taking their derivatives, we can compute the velocity and the acceleration of the particle in motion. We will write simple programs that compute the values of these functions in a time interval , where is the initial and is the final time. The continuous functions are approximated by a discrete sequence of their values at the times such that .
We will start the design of our program by forming a generic template to be used in all of the problems of interest. Then we can study each problem of particle motion by programming only the equations of motion without worrying about the less important tasks, like input/output, user interface etc. Figure 2.2 shows a flowchart of the basic steps in the algorithm. The first part of the program declares variables and defines the values of the fixed parameters (like , , etc). The program starts by interacting with the user (“user interface”) and asks for the values of the variables , , , , . The program prints these values to the stdout so that the user can check them for correctness and store them in her data.
The main calculation is performed in a loop executed while . The values of the positions and the velocities are calculated and printed in a file together with the time . At this point we fix the format of the program output, something that is very important to do it in a consistent and convenient way for easing data analysis. We choose to print the values t, x, y, vx, vy in five columns in each line of the output file.
The specific problem that we are going to solve is the computation of the trajectory of the circular motion of a particle on a circle with center and radius with constant angular velocity . The position on the circle can be defined by the angle , as can be seen in figure 2.3. We define the initial position of the particle at time to be .
The equations giving the position of the particle at time are
Taking the derivative w.r.t. we obtain the velocity and the acceleration We note that the above equations imply that (, , tangent to the trajectory) and ( and anti-parallel, ).The data structure is quite simple. The constant angular velocity is stored in the REAL variable omega. The center of the circle , the radius of the circle and the angle are stored in the REAL variables x0, y0, R, theta. The times at which we calculate the particle’s position and velocity are defined by the parameters and are stored in the REAL variables t0, tf, dt. The current position is calculated and stored in the REAL variables x, y and the velocity in the REAL variables vx, vy. The declarations of the variables are put in the beginning of the program:
were we defined the value1 of by using the parameter specification.
The user interface of the program is the interaction of the program with the user and, in our case, it is the part of the program where the user enters the parameters omega, x0, y0, R, t0, tf, dt. The program issues a prompt with the names the variables expected to be read. This is done using simple print statements. The variables are read from the stdin by simple read statements and the values entered by the user are printed to the stdout2 :
Next, the program initializes the state of the computation. This includes checking the validity of the parameters entered by the user, so that the computation will be possible. For example, the program computes the expression 2.0*PI/omega, where it is assumed that omega has a non zero value. We will also demand that and . An if statement will make those checks and if the parameters have illegal values, the stop statement will stop the program execution3 . The program opens the file Circle.dat for writing the calculated values of the position and the velocity of the particle.
If or the corresponding stop statements are executed which end the program execution. The optional error messages are included after the stop statements which are printed to the stdout. The value of the period is also calculated and printed for reference.
The open statement uses unit 11 for writing to the file Circle.dat. The choice of the unit number is free for the programmer to choose. We recommend using the units 10 to 99 for input/output to files4 .
The main calculation is performed within the loop
The first statement sets the initial value of the time. The statements between the do while(condition) and enddo are executed as long as condition has a .TRUE. value. The statement t=t+dt increments the time and this is necessary in order not to enter into an infinite loop. he commands put in place of the dots ......... calculate the position and the velocity and print them to the file Circle.dat:
Notice the use of the intrinsic functions sin and cos that calculate the sine and cosine of an angle expressed in radians. We use the intermediate variable theta in order to store the phase . The command write(11,*) writes the variables t,x,y,vx,vy to the unit 11, which has been associated to the file Circle.dat with the open statement shown above.
The program is stored in the file Circle.f90 and can be found in the accompanied software. The extension .f90 is used by the compiler in order to denote source code written in free format Fortran language. Compilation and running can be done using the commands:
The switch -o cl forces the compiler gfortran to write the binary commands executed by the program to the file5 cl. The command ./cl loads the program instructions to the computer memory for execution. When the programs starts execution, it first asks for the parameter data and then performs the calculation. A typical session looks like:
The lines shown above that start with a # character are printed by the program and lines without # are the values of the parameters entered interactively by the user. The user types in the parameters and then presses the Enter key in order for the program to read them. Here we have , , , , and .
You can execute the above program many times for different values of the parameter by writing the parameter values in a file using an editor. For example, in the file Circle.in type the following data:
Each line has the parameters read by the program with a read statement (a record). The rest of the line is ignored by the program and the user can write anything she likes as a comment on how to use the parameters. The program can read the above values of the parameters with the command:
The command ./cl runs the commands found in the executable file ./cl. The < Circle.in redirects the contents of the file Circle.in to the standard input (stdin) of the command ./cl. This way the program reads in the values of the parameters from the contents of the file Circle.in. The > Circle.out redirects the standard output (stdout) of the command ./cl to the file Circle.out. Its contents can be inspected after the execution of the program with the command cat:
We list the full program in Circle.f90 below:
We use gnuplot for plotting the data produced by our programs. The file Circle.dat has the time t and the components x, y, vx, vy in five columns. Therefore we can plot the functions and by using the gnuplot commands:
The second line puts the second plot together with the first one. The results can be seen in figure 2.4.
Let’s see now how we can make the plot of the function . We can do that using the raw data from the file Circle.dat within gnuplot, without having to write a new program. Note that . The function atan2 is available in gnuplot6 as well as in Fortran. Use the online help system in gnuplot in order to see its usage:
Therefore, the right way to call the function is atan2(y-y0,x-x0). In our case x0=y0=1 and x, y are in the 2nd and 3rd columns of the file Circle.dat. We can construct an expression after the using command as in page 153, where $2 is the value of the second and $3 the value of the third column:
The second command is broken in two lines by using the character ∖ so that it fits conveniently in the text7 . Note how we defined the values of the variables x0, y0 and how we used them in the expression atan2($3-x0,$2-y0). We also plot the lines which graph the constant functions and which mark the limit values of . The gnuplot variable8 pi is predefined and can be used in formed expressions. The result can be seen in the left plot of figure 2.4.
The velocity components as function of time as well as the trajectory can be plotted with the commands:
We close this section by showing how to do a simple animation of the particle trajectory using gnuplot. There is a file animate2D.gnu in the accompanied software which you can copy in the directory where you have the data file Circle.dat. We are not going to explain how it works9 but how to use it in order to make your own animations. The final result is shown in figure 2.5. All that you need to do is to define the data file10 , the initial time t0, the final time tf and the time step dt. These times can be different from the ones we used to create the data in Circle.dat. A full animation session can be launched using the commands:
The first line defines the data file that animate2D.gnu reads data from. The second line sets the range of the plots and the third line defines the time parameters used in the animation. The final line launches the animation. If you want to rerun the animation, you can repeat the last two commands as many times as you want using the same or different parameters. E.g. if you wish to run the animation at “half the speed” you should simply redefine dt=0.05 and set the initial time to t0=0:
We are now going to apply the steps described in the previous section to other examples of motion on the plane. The first problem that we are going to discuss is that of the small oscillations of a simple pendulum. Figure 2.6 shows the single oscillating degree of freedom , which is the small angle that the pendulum forms with the vertical direction.
The motion is periodic with angular frequency and period . The angular velocity is computed from which gives
We have chosen the initial conditions and . In order to write the equations of motion in the Cartesian coordinate system shown in figure 2.6 we use the relations These are similar to the equations (2.3) and (2.4) that we used in the case of the circular motion of the previous section. Therefore the structure of the program is quite similar. Its final form, which can be found in the file SimplePendulum.f90, is:We note that the acceleration of gravity is hard coded in the program and that the user can only set the length of the pendulum. The data file SimplePendulum.dat produced by the program, contains two extra columns with the current values of and the angular velocity . The statement write(11,100) writes to the unit 11 according to the format set by the FORMAT statement, found in the line labeled by the label 100. This is done so that we can be sure that the data is printed in one line for each value of (see the discussion on page 119).
A simple session for the study of the above problem is shown below11 :
The next example is the study of the trajectory of a particle shot near the earth’s surface12 when we consider the effect of air resistance to be negligible. Then, the equations describing the trajectory of the particle and its velocity are given by the parametric equations
where is the parameter. The initial conditions are , and , as shown in figure 2.7.The structure of the program is similar to the previous ones. The user enters the magnitude of the particle’s initial velocity and the shooting angle in degrees. The initial time is taken to be . The program calculates and and prints them to the stdout. The data is written to the file Projectile.dat. The full program is listed below and it can be found in the file Projectile.f90 in the accompanied software:
A typical session for the study of this problem is shown below:
Next, we will study the effect of air resistance of the form . The solutions to the equations of motion
Programming the above equations is as easy as before, the only difference being that the user needs to provide the value of the constant . The full program can be found in the file ProjectileAirResistance.f90 and it is listed below:
We also list the commands of a typical session of the study of the problem:
Long commands have been continued to the next line as before. We defined the gnuplot variables v0x, v0y, g and k to have the values that we used when running the program. We can use them in order to construct the asymptotes of the plotted functions of time. The results are shown in figures 2.9 and 2.10.
The last example of this section will be that of the anisotropic harmonic oscillator. The force on the particle is
| (2.11) |
where the “spring constants” and are different in the directions of the axes and . The solutions of the dynamical equations of motion for , , and are
If the angular frequencies and satisfy certain relations, the trajectories of the particle are closed and self intersect at a given number of points. The proof of these relations, as well as their numerical confirmation, is left as an exercise for the reader. The program listed below is in the file Lissajoux.f90:We have set in the program above. The user must enter the two angular frequencies and and the corresponding times. A typical session for the study of the problem is shown below:
The results for and are shown in figure 2.11.
By slightly generalizing the methods described in the previous section, we will study the motion of a particle in three dimensional space. All we have to do is to add an extra equation for the coordinate and the component of the velocity . The structure of the programs will be exactly the same as before.
The first example is the conical pendulum, which can be seen in figure 2.12. The particle moves on the plane with constant angular velocity . The equations of motion are derived from the relations
| (2.13) |
where . Their solution14 is
where we have to substitute the values For the velocity components we obtain Therefore we must have
| (2.17) |
and when , .
In the program that we will write, the user must enter the parameters , , the final time and the time step . We take . The convention that we follow for the output of the results is that they should be written in a file where the first 7 columns are the values of , , , , , and . Each line in this file is long and, in order to prevent Fortran from breaking it into two separate lines, we have to give an explicit format specification. See the discussion on page 119. The full program is listed below:
In order to compile and run the program we can use the commands shown below:
The results are recorded in the file ConicalPendulum.dat. In order to plot the functions , , , , , we give the following gnuplot commands:
The results are shown in figure 2.13.
In order to make a three dimensional plot of the trajectory, we should use the gnuplot command splot:
The result is shown in figure 2.14. We can click on the trajectory and rotate it and view it from a different angle. We can change the plot limits with the command:
We can animate the trajectory of the particle by using the file animate3D.gnu from the accompanying software. The commands are similar to the ones we had to give in the two dimensional case for the planar trajectories when we used the file animate2D.gnu:
The result can be seen in figure 2.15.
The program animate3D.gnu can be used on the data file of any program that prints t x y z as the first words on each of its lines. All we have to do is to change the value of the file variable in gnuplot.
Next, we will study the trajectory of a charged particle in a homogeneous magnetic field . At time , the particle is at and its velocity is , see figure 2.16.
The magnetic force on the particle is and the equations of motion are
By integrating the above equations with the given initial conditions we obtain Integrating once more, we obtain the position of the particle as a function of time where we have chosen . This choice places the center of the circle, which is the projection of the trajectory on the plane, to be at the origin of the coordinate system. The trajectory is a helix with radius and pitch .We are now ready to write a program that calculates the trajectory given by (2.20) . The user enters the parameters and , shown in figure 2.16, as well as the angular frequency (Larmor frequency). The components of the initial velocity are and . The initial position is calculated from the equation . The program can be found in the file ChargeInB.f90:
A typical session in which we calculate the trajectories shown in figures 2.17 and 2.18 is shown below:
In this section we will study the motion of a particle that is free, except when bouncing elastically on a wall or on certain obstacles. This motion is calculated by approximate algorithms that introduce systematic errors. These types of errors15 are also encountered in the study of more complicated dynamics, but the simplicity of the problem will allow us to control them in a systematic and easy to understand way.
The simplest example of such a motion is that of a particle in a “one dimensional box”. The particle moves freely on the axis for , as can be seen in figure 2.19. When it reaches the boundaries and it bounces and its velocity instantly reversed. Its potential energy is
| (2.21) |
which has the shape of an infinitely deep well. The force within the box and at the position of the walls.
Initially we have to know the position of the particle as well as its velocity (the sign of depends on the direction of the particle’s motion) at time . As long as the particle moves within the box, its motion is free and
For a small enough change in time , so that there is no bouncing on the wall in the time interval , we have that Therefore we could use the above relations in our program and when the particle bounces off a wall we could simple reverse its velocity . The devil is hiding in the word “when”. Since the time interval is finite in our program, there is no way to know the instant of the collision with accuracy better than . However, our algorithm will change the direction of the velocity at time , when the particle will have already crossed the wall. This will introduce a systematic error, which is expected to decrease with decreasing . One way to implement the above idea is by constructing the loopwhere the last line gives the testing condition for the wall collision and the subsequent change of the velocity.
The full program that realizes the proposed algorithm is listed below and can be found in the file box1D_1.f90. The user can set the size of the box L, the initial conditions x0 and v0 at time t0, the final time tf and the time step dt:
The computed data is recorded in the file box1D_1.dat in three columns. Compiling, running and plotting the trajectory using gnuplot can be done as follows:
The trajectory is shown in figure 2.20. The effects of the systematic errors can be easily seen by noting that the expected collisions occur every units of time. Therefore, on the plot to the right of figure 2.20, the reversal of the particle’s motion should have occurred at , .
The reader should have already realized that the above mentioned error can be made to vanish by taking arbitrarily small . Therefore, we naively expect that as long as we have the necessary computer power to take as small as possible and the corresponding time intervals as many as possible, we can achieve any precision that we want. Well, that is true only up to a point. The problem is that the next position is determined by the addition operation x+v*dt and the next moment in time by t+dt. Floating point numbers of the REAL type have a maximum accuracy of approximately 7 significant decimal digits. Therefore, if the operands x and v*dt are real numbers differing by more than 7 orders of magnitude (v*dt x), the effect of the addition x+v*dt=x, which is null! The reason is that the floating point unit of the processor has to convert both numbers x and v*dt into a representation having the same exponent and in doing so, the corresponding significant digits of the smaller number v*dt are lost. The result is less catastrophic when v*dt x with , but some degree of accuracy is also lost at each addition operation. And since we have accumulation of such errors over many intervals tt+dt, the error can become significant and destroy our calculation for large enough times. A similar error accumulates in the determination of the next instant of time t+dt, but we will discuss below how to make this contribution to the total error negligible. The above mentioned errors can become less detrimental by using floating point numbers of greater accuracy than the REAL type. For example REAL(8) numbers have approximately 16 significant decimal digits. But again, the precision is finite and the same type of errors are there only to be revealed by a more demanding and complicated calculation.
The remedy to such a problem can only be a change in the algorithm. This is not always possible, but in the case at hand this is easy to do. For example, consider the equation that gives the position of a particle in free motion
| (2.24) |
Let’s use the above relation for the parts of the motion between two collisions. Then, all we have to do is to reverse the direction of the motion and reset the initial position and time to be the position and time of the collision. This can be done by using the loop:
In the above algorithm, the error in the time of the collision is not vanishing but we don’t have the “instability” problem of the dt limit16 . Therefore we can isolate and study the effect of each type of error. The full program that implements the above algorithm is given below and can be found in the file box1D_2.f90:
Compiling and running the above program is done as before and the results are stored in the file box1D_2.dat. The algorithm can be improved in order to compute the exact solution. We leave that as an exercise for the reader17 .
In this section we will study the effect of the systematic errors that we encountered in the previous section in more detail. We considered two types of errors: First, the systematic error of determining the instant of the collision of the particle with the wall. This error is reduced by taking a smaller time step . Then, the systematic error that accumulates with each addition of two numbers with increasing difference in their orders of magnitude. This error is increased with decreasing . The competition of the two effects makes the optimal choice of the result of a careful analysis. Such a situation is found in many interesting problems, therefore it is quite instructive to study it in more detail.
When the exact solution of the problem is not known, the systematic errors are controlled by studying the behavior of the solution as a function of . If the solutions are converging in a region of values of , one gains confidence that the true solution has been determined up to the accuracy of the convergence.
In the previous sections, we studied two different algorithms, programmed in the files box1D_1.f90 and box1D_2.f90. We will refer to them as “method 1” and “method 2” respectively. We will study the convergence of the results as by fixing all the parameters except and then study the dependence of the results on . We will take , , , , , so that the particle will collide with the wall every 10 units of time. We will measure the position of the particle 18 as a function of and study its convergence to a limit19 as .
The analysis requires a lot of repetitive work: Compiling, setting the parameter values, running the program and calculating the value of for many values of . We write the values of the parameters read by the program in a file box1D_anal.in:
Then we compile the program
and run it with the command:
By using the pipe |, we send the contents of box1D_anal.in to the stdin of the command ./box by using the command cat. The result can be found in the last line of the file box1D_1.dat:
The third number in the above line is the value of the velocity. In a file box1D_anal.dat we write and the first two numbers coming out from the command tail. Then we decrease the value in the file box1D_anal.in and run again. We repeat for 12 more times until reaches the value20 . We do the same21 using method 2 and we place the results for in two new columns in the file box1D_anal.dat. The result is
Convergence is studied in figure 2.21. The 1st method maximizes its accuracy for , whereas for the error becomes % and the method becomes useless. The 2nd method has much better behavior that the 1st one.
We observe that as decreases, the final value of approaches the expected . Why don’t we obtain , especially when is an integer? How many steps does it really take to reach , when the expected number of those is ? Each time you take a measurement, issue the command
which measures the number of lines in the file box1D_1.dat and compare this number with the expected one. The result is interesting:
where the second column has the number of steps computed by the program and the third one has the expected number of steps. We observe that the accuracy decreases with decreasing and in the end the difference is about 5%! Notice that the last line should have given , an error comparable to the period of the particle’s motion.
We conclude that one important source of accumulation of systematic errors is the calculation of time. This type of errors become more significant with decreasing . We can improve the accuracy of the calculation significantly if we use the multiplication t=t0+i*dt instead of the addition t=t+dt, where i is a step counter:
The main loop in the program box1D_1.f90 becomes:
The full program can be found in the file box1D_4.f90 of the accompanying software. We call this “method 3”. We perform the same change in the file box1D_2.f90, which we store in the file box1D_5.f90. We call this “method 4”. We repeat the same analysis using methods 3 and 4 and we find that the problem of calculating time accurately practically vanishes. The result of the analysis can be found on the right plot of figure 2.21. Methods 2 and 4 have no significant difference in their results, whereas methods 1 and 3 do have a dramatic difference, with method 3 decreasing the error more than tenfold. The problem of the increase of systematic errors with decreasing does not vanish completely due to the operation x=x+v*dt. This type of error is harder to deal with and one has to invent more elaborate algorithms in order to reduce it significantly. This will be discussed further in chapter 4.
A particle is confined to move on the plane in the area and . When it reaches the boundaries of this two dimensional box, it bounces elastically off its walls. The particle is found in an infinite depth orthogonal potential well. The particle starts moving at time from and our program will calculate its trajectory until time with time step . Such a trajectory can be seen in figure 2.23.
If the particle’s position and velocity are known at time , then at time they will be given by the relations
The collision of the particle off the walls is modeled by reflection of the normal component of the velocity when the respective coordinate of the particle crosses the wall. This is a source of the systematic errors that we discussed in the previous section. The central loop of the program is:The full program can be found in the file box2D_1.f90. Notice that we introduced two counters nx and ny of the particle’s collisions with the walls:
A typical session for the study of a particle’s trajectory could be:
Notice the last line of output from the program: The particle bounces off the vertical walls 6 times (nx=6) and from the horizontal ones 13 (ny=13). The gnuplot commands construct the diagrams displayed in figures 2.22 and 2.23.
In order to animate the particle’s trajectory, we can copy the file box2D_animate.gnu of the accompanying software to the current directory and give the gnuplot commands:
The last line repeats the same animation at half speed. You can also use the file animate2D.gnu discussed in section 2.1.1. We add new commands in the file box2D_animate.gnu so that the plot limits are calculated automatically and the box is drawn on the plot. The arrow drawn is not the position vector with respect to the origin of the coordinate axes, but the one connecting the initial with the current position of the particle.
The next step should be to test the accuracy of your results. This can be done by generalizing the discussion of the previous section and is left as an exercise for the reader.
In this section we will study simple examples of motion in a box with different types of obstacles. We will start with a game of ... mini golf. The player shoots a (point) “ball” which moves in an orthogonal box of linear dimensions and and which is open on the side. In the box there is a circular “hole” with center at and radius . If the “ball” falls in the “hole”, the player wins. If the ball leaves out of the box through its open side, the player loses. In order to check if the ball is in the hole when it is at position , all we have to do is to check whether .
Initially we place the ball at the position at time . The player hits the ball which leaves with initial velocity of magnitude at an angle degrees with the axis. The program is found in the file MiniGolf.f90 and is listed below:
In order to run it, we can use the commands:
You should construct the plots of the position and the velocity of the particle. You can also use the animation program found in the file MiniGolf_animate.gnu for fun. Copy it from the accompanying software to the current directory and give the gnuplot commands:
The results are shown in figure 2.24.
The next example with be three dimensional. We will study the motion of a particle confined within a cylinder of radius and height . The collisions of the particle with the cylinder are elastic. We take the axis of the cylinder to be the axis and the two bases of the cylinder to be located at and . This is shown in figure 2.26.
The collisions of the particle with the bases of the cylinder are easy to program: we follow the same steps as in the case of the simple box. For the collision with the cylinder’s side, we consider the projection of the motion on the plane. The projection of the particle moves within a circle of radius and center at the intersection of the axis with the plane. This is shown in figure 2.25. At the collision, the component of the velocity is reflected , whereas remains the same. The velocity of the particle before the collision is
and after the collision is From the relations and , , we have that The inverse relations are With the transformation , the new velocity in Cartesian coordinates will beThe transformation , will be performed in the subroutine reflectVonCircle(vx,vy,x,y,xc,yc,R). Upon entry to the subroutine, we provide the initial velocity (vx,vy), the collision point (x,y), the center of the circle (xc,yc) and the radius of the circle22 R. Upon exit from the subroutine, (vx,vy) have been replaced with the new values23 .
The program can be found in the file Cylinder3D.f90 and is listed below:
Notice that the function atan2 is used for computing the angle theta. This function, when called with two arguments atan2(y,x), returns the angle in radians. The correct quadrant of the circle where lies is chosen. The angle that we want to compute is given by atan2(y-yc,x-xc). Then we apply equations (2.29) and (2.31) and in the last two lines we enforce the particle to be at the point , exactly on the circle.
A typical session is shown below:
In order to plot the position and the velocity as a function of time, we use the following gnuplot commands:
We can also compute the distance of the particle from the cylinder’s axis as a function of time using the command:
In order to plot the trajectory, together with the cylinder, we give the commands:
The command set parametric is necessary if one wants to make a parametric plot of a surface . The cylinder (without the bases) is given by the parametric equations with , .
We can also animate the trajectory with the help of the gnuplot script file Cylinder3D_animate.gnu. Copy the file from the accompanying software to the current directory and give the gnuplot commands:
The result is shown in figure 2.26.
The last example will be that of a simple model of a spacetime wormhole. This is a simple spacetime geometry which, in the framework of the theory of general relativity, describes the connection of two distant areas in space which are asymptotically flat. This means, that far enough from the wormhole’s mouths, space is almost flat - free of gravity. Such a geometry is depicted in figure 2.27. The distance traveled by someone through the mouths could be much smaller than the distance traveled outside the wormhole and, at least theoretically, traversable wormholes could be used for interstellar/intergalactic traveling and/or communications between otherwise distant areas in the universe. Of course we should note that such macroscopic and stable wormholes are not known to be possible to exist in the framework of general relativity. One needs an exotic type of matter with negative energy density which has never been observed. Such exotic geometries may realize microscopically as quantum fluctuations of spacetime and make the small scale structure of the geometry24 a “spacetime foam”.
We will study a very simple model of the above geometry on the plane with a particle moving freely in it25 .
We take the two dimensional plane and cut two equal disks of radius with centers at distance like in figure 2.28. We identify the points on the two circles such that the point 1 of the left circle is the same as the point 1 on the right circle, the point 2 on the left with the point 2 on the right etc. The two circles are given by the parametric equations , , for the right circle and , , for the left. Points on the two circles with the same are identified. A particle entering the wormhole from the left circle with velocity is immediately exiting from the right with velocity as shown in figure 2.28.
Then we will do the following:
Define the right circle by the parametric equations
| (2.32) |
and the left circle by the parametric equations
| (2.33) |
The particle’s position changes at time by
for for given , and as long as . If the point is outside the boundaries , , we redefine , in each case respectively. Points defined by the same value of are identified, i.e. they represent the same points of space. If the point crosses either one of the circles or , then we take the particle out from the other circle.Crossing the circle is determined by the relation
| (2.35) |
The angle is calculated from the equation
| (2.36) |
and the point is mapped to the point where
| (2.37) |
as can be seen in figure 2.29. For mapping , we first calculate the vectors
| (2.38) |
so that the velocity
| (2.39) |
where the radial components are and . Therefore, the relations that give the “emerging” velocity are:
| (2.40) |
Similarly we calculate the case of entering from and emerging from . The condition now is:
| (2.41) |
The angle is given by
| (2.42) |
and the point is mapped to the point where
| (2.43) |
For mapping , we calculate the vectors
| (2.44) |
so that the velocity
| (2.45) |
The emerging velocity is:
| (2.46) |
Systematic errors are now coming from crossing the two mouths of the wormhole. There are no systematic errors from crossing the boundaries , (why?). Try to think of ways to control those errors and study them.
The closed trajectories that we are looking for come from the initial conditions
| (2.47) |
and they connect points 1 of figure 2.28. They are unstable, as can be seen by taking .
The closed trajectories that cross the wormhole and “wind” through space can come from the initial conditions
and cross the points and respectively. They are also unstable, as can be easily verified by using the program that you will write. The full program is listed below:
It is easy to compile and run the program. See also the files Wormhole.csh and Wormhole_animate.gnu of the accompanying software and run the gnuplot commands:
You are now ready to answer the rest of the questions that we asked in our list.
Nonlinear differential equations model interesting dynamical systems in physics, biology and other branches of science. In this chapter we perform a numerical study of the discrete logistic map as a “simple mathematical model with complex dynamical properties” [21] similar to the ones encountered in more complicated and interesting dynamical systems. For certain values of the parameter of the map, one finds chaotic behavior giving us an opportunity to touch on this very interesting topic with important consequences in physical phenomena. Chaotic evolution restricts out ability for useful predictions in an otherwise fully deterministic dynamical system: measurements using slightly different initial conditions result in a distribution which is indistinguishable from the distribution coming from sampling a random process. This scientific field is huge and active and we refer the reader to the bibliography for a more complete introduction [21, 22, 23, 24, 25, 26, 27, 38].
The most celebrated application of the logistic map comes from the study of population growth in biology. One considers populations which reproduce at fixed time intervals and whose generations do not overlap.
The simplest (and most naive) model is the one that makes the reasonable assumption that the rate of population growth of a population is proportional to the current population:
| (3.1) |
The general solution of the above equation is showing an exponential population growth for an decline for . It is obvious that this model is reasonable as long as the population is small enough so that the interaction with its environment (adequate food, diseases, predators etc) can be neglected. The simplest model that takes into account some of the factors of the interaction with the environment (e.g. starvation) is obtained by the introduction of a simple non linear term in the equation so that
| (3.2) |
The parameter gives the maximum growth rate of the population and controls the ability of the species to maintain a certain population level. The equation (3.2) can be discretized in time by assuming that each generation reproduces every and that the n-th generation has population where . Then and equation (3.1) becomes
| (3.3) |
where . The solutions of the above equation are well approximated by so that we have population growth when and decline when . Equation (3.2) can be discretized as follows:
| (3.4) |
Defining we obtain the logistic map
| (3.5) |
We define the functions
| (3.6) |
(their only difference is that, in the first one, is considered as a given parameter), so that
| (3.7) |
where we use the notation , , , for function composition. In what follows, the derivative of will be useful:
| (3.8) |
Since we interpret to be the fraction of the population with respect to its maximum value, we should have for each1 . The function has one global maximum for which is equal to . Therefore, if , then , which for an appropriate choice of will lead to for some value of . Therefore, the interval of values of which is of interest for our model is
| (3.9) |
The logistic map (3.5) may be viewed as a finite difference equation and it is a one step inductive relation. Given an initial value , a sequence of values is produced. This will be referred2 to as the trajectory of . In the following sections we will study the properties of these trajectories as a function of the parameter .
The solutions of the logistic map are not known except in special cases. For we have
| (3.10) |
and for3
| (3.11) |
For , whereas for we have periodic trajectories resulting in rational and non periodic resulting in irrational . For other values of we have to resort to a numerical computation of the trajectories of the logistic map.
It is obvious that if the point is a solution of the equation , then for every . For the function we have two solutions
| (3.12) |
We will see that for appropriate values of , these solutions are attractors of most of the trajectories. This means that for a range of values for the initial point , the sequence approaches asymptotically one of these points as . Obviously the (measure zero) sets of initial values and result in trajectories attracted by and respectively. In order to determine which one of the two values is preferred, we need to study the stability of the fixed points and . For this, assume that for some value of , is infinitesimally close to the fixed point so that
Since
| (3.14) |
where we used the Taylor expansion of the analytic function about and the relation , we have that . Then we obtain
| (3.15) |
Therefore, if we obtain and the fixed point is stable: the sequence approaches asymptotically. If then the sequence deviates away from and the fixed point is unstable. The limiting case should be studied separately and it indicates a change in the stability properties of the fixed point. In the following discussion, these points will be shown to be bifurcation points.
For the function with we have that and . Therefore, if the point is an attractor, whereas the point is irrelevant. When , the point results in , therefore is unstable. Any initial value near deviates from it. Since for we have that , the point is an attractor. Any initial value approaches . When we have the limiting case and we say that at the critical value the fixed point bifurcates to the two fixed points and .
As increases, the fixed points continue to bifurcate. Indeed, when we have that and for the point becomes unstable. Consider the solution of the equation . If is one of its solutions and for some we have that , then and (therefore is also a solution). If are two such different solutions with , , then the trajectory is periodic with period 2. The points , are such that they are real solutions of the equation
| (3.16) |
and at the same time they are not the solutions of the equation4 , the polynomial above can be written in the form (see [22] for more details)
| (3.17) |
By expanding the polynomials (3.16) , (3.17) and comparing their coefficients we conclude that , and . The roots of the trinomial in (3.17) are determined by the discriminant . For the values of of interest (), the discriminant becomes positive when and we have two different solutions
| (3.18) |
When we have one double root, therefore a unique fixed point.
The study of the stability of the solutions of requires the same steps that led to the equation (3.15) and we determine if the absolute value of is greater, less or equal to one. By noting that5 , we see that for , and for , . For the intermediate values the derivatives for . Therefore, these points are stable solutions of and the points bifurcate to , for . Almost all trajectories with initial points in the interval are attracted by the periodic trajectory with period 2, the “2-cycle” .
Using similar arguments we find that the fixed points , bifurcate to the eight fixed points , when . These are real solutions of the equation that gives the 4-cycle . For , the points , are a stable 4-cycle which is an attractor of almost all trajectories of the logistic map6 . Similarly, for the 16 fixed points of the equation give a stable 8-cycle, for a stable 16-cycle etc7 . This is the phenomenon which is called period doubling which continues ad infinitum. The points are getting closer to each other as increases so that . As we will see, marks the onset of the non-periodic, chaotic behavior of the trajectories of the logistic map.
Computing the bifurcation points becomes quickly intractable and we have to resort to a numerical computation of their values. Initially we will write a program that computes trajectories of the logistic map for chosen values of and . The program can be found in the file logistic.f90 and is listed below:
The program is compiled and run using the commands:
The command echo prints to the stdout the values of the parameters NSTEPS=100, r=0.5 and x0=0.1. Its stdout is redirected to the stdin of the command ./l by using a pipe via the symbol |, from which the program reads their value and uses them in the calculation. The results can be found in two columns in the file log.dat and can be plotted using gnuplot. The plots are put in figure 3.1 and we can see the first two bifurcations when goes past the values and . Similarly, we can study trajectories which are -cycles when crosses the values .
Another way to depict the 2-cycles is by constructing the cobweb plots: We start from the point and we calculate the point , where . This point belongs on the curve . The point is then projected on the diagonal and we obtain the point . We repeat times obtaining the points and on and respectively. The fixed points are at the intersections of these curves and, if they are attractors, the trajectories will converge on them. If we have a -cycle, we will observe a periodic trajectory going through points which are solutions to the equation . This exercise can be done by using the following program, which can be found in the file logistic1.f90:
Compiling and running this program is done exactly as in the case of the program in logistic.f90. We can plot the results using gnuplot. The plot in figure 3.2 can be constructed using the commands:
The plot command shown above, runs the program exactly as it is done on the command line. This is accomplished by using the symbol <, which reads the plot from the stdout of the command "echo 50 3.3 0.2|./l;cat trj.dat". Only the second command "echo trj.dat" writes to the stdout, therefore the plot is constructed from the contents of the file trj.dat. The following line adds the plots of the functions , and of the diagonal . Figures 3.2 and 3.3 show examples of attractors which are fixed points, 2-cycles and 4-cycles. An example of a non periodic trajectory is also shown, which exhibits chaotic behavior which can happen when .
The bifurcations of the fixed points of the logistic map discussed in the previous section can be conveniently shown on the “bifurcation diagram”. We remind to the reader that the first bifurcations happen at the critical values of r
| (3.19) |
where , , and . For we have fixed points , of . By plotting these points as a function of we construct the bifurcation diagram. These can be calculated numerically by using the program bifurcate.f90. In this program, the user selects the values of that she needs to study and for each one of them the program records the point of the -cycles8 , . This is easily done by computing the logistic map several times until we are sure that the trajectories reach the stable state. The parameter NTRANS in the program determines the number of points that we throw away, which should contain all the transient behavior. After NTRANS steps, the program records NSTEPS points, where NSTEPS should be large enough to cover all the points of the -cycles or depict a dense enough set of values of the non periodic orbits. The program is listed below:
The program can be compiled and run using the commands:
The left plot of figure 3.4 can be constructed by the gnuplot commands:
We observe the fixed points and the -cycles for . When goes past , the trajectories become non-periodic and exhibit chaotic behavior. Chaotic behavior will be discussed more extensively in the next section. For the time being, we note that if we measure the distance between the points , we find that it decreases constantly with so that
| (3.20) |
where is the Feigenbaum constant. An additional constant , defined by the quotient of the separation of adjacent elements of period doubled attractors from one double to the next , is
| (3.21) |
It is also interesting to note the appearance of a 3-cycle right after ! By using the theorem of Sharkovskii, Li and Yorke9 showed that any one dimensional system has 3-cycles, therefore it will have cycles of any length and chaotic trajectories. The stability of the 3-cycle can be studied from the solutions of in exactly the same way that we did in equations (3.16) and (3.17) (see [22] for details). Figure 3.5 magnifies a branch of the 3-cycle. By magnifying different regions in the bifurcation plot, as shown in the right plot of figure 3.4, we find similar shapes to the branching of the 3-cycle.
Figure 3.4 shows that between intervals of chaotic behavior we obtain “windows” of periodic trajectories. These are infinite but countable. It is also quite interesting to note that if we magnify a branch withing these windows, we obtain a diagram that is similar to the whole diagram! We say that the bifurcation diagram exhibits self similarity. There are more interesting properties of the bifurcation diagram and we refer the reader to the bibliography for a more complete exposition.
We close this section by mentioning that the qualitative properties of the bifurcation diagram are the same for a whole class of functions. Feigenbaum discovered that if one takes any function that is concave and has a unique global maximum, its bifurcation diagram behaves qualitatively the same way as that of the logistic map. Examples of such functions10 studied in the literature are , and . The constants and of equations (3.20) and (3.21) are the same of all these mappings. The functions that result in chaotic behavior are studied extensively in the literature and you can find a list of those in [28].
In order to determine the bifurcation points, one has to solve the nonlinear, polynomial, algebraic equations and . For this reason, one has to use an approximate numerical calculation of the roots, and the simple Newton-Raphson method will prove to be a good choice.
Newton-Raphson’s method uses an initial guess for the solution of the equation and computes a sequence of points that presumably converges to one of the roots of the equation. The computation stops at a finite , when we decide that the desired level of accuracy has been achieved. In order to understand how it works, we assume that is an analytic function for all the values of used in the computation. Then, by Taylor expanding around we obtain
| (3.22) |
If we wish to have , we choose
| (3.23) |
The equation above gives the Newton-Raphson method for one equation of one variable . Different choices for will possibly lead to different roots. When , are non zero at the root and is bounded, the convergence of the method is quadratic with the number of iterations. This means that there is a neighborhood of the root such that the distance is . If the root has multiplicity larger than 1, convergence is slower. The proofs of these statements are simple and can be found in [29].
The Newton-Raphson method is simple to program and, most of the times, sufficient for the solution of many problems. In the general case it works well only close enough to a root. We should also keep in mind that there are simple reasons for the method to fail. For example, when for some , the method stops. For functions that tend to as , it is easy to make a bad choice for that does not lead to convergence to a root. Sometimes it is a good idea to combine the Newton-Raphson method with the bisection method. When the derivative diverges at the root we might get into trouble. For example, the equation with , does not lead to a convergent sequence. In some cases, we might enter into non-convergent cycles [8]. For some functions the basin of attraction of a root (the values of that will converge to the root) can be tiny. See problem 13.
As a test case of our program, consider the equation
| (3.24) |
which results from the solution of Schrödinger’s equation for the energy spectrum of a quantum mechanical particle of mass in a one dimensional potential well of depth and width . The parameters and . Given , we solve for which gives the energy . The function and its derivative are
The program of the Newton-Raphson method for solving the equation can be found in the file nr.f90:In the program listed above, the user is asked to set the initial point . We fix rho . It is instructive to make the plot of the left and right hand sides of (3.24) and make a graphical determination of the roots from their intersections. Then we can make appropriate choices of the initial point . Using gnuplot, the plots are made with the commands:
The compilation and running of the program can be done as follows:
We conclude that one of the roots of the equation is . The reader can compute more of these roots by following these steps by herself.
The method discussed above can be easily generalized to the case of two equations. Suppose that we need to solve simultaneously two algebraic equations and . In order to compute a sequence , , , , , that may converge to a root of the above system of equations, we Taylor expand the two functions around
Defining and and setting , , we obtain This is a linear system of equations where and , with . Solving for we obtain The iterations stop when become small enough.As an example, consider the equations with , . We have , , , . The program can be found in the file nr2.f90:
In order to guess the region where the real roots of the systems lie, we make a 3-dimensional plot using gnuplot:
We plot the functions together with the plane . The intersection of the three surfaces determine the roots we are looking for. Compiling and running the program can be done by using the commands:
The computation above leads to the roots , , .
The Newton-Raphson method for many variables becomes hard quite soon: One needs to calculate the functions as well as their derivatives, which is prohibitively expensive for many problems. It is also hard to determine the roots, since the method converges satisfactorily only very close to the roots. We refer the reader to [8] for more information on how one can deal with these problems.
In order to determine the bifurcation points for we will solve the algebraic equations and . At these points, -cycles become unstable and -cycles appear and are stable. This happens when , where . We will look for solutions for .
We define the functions and as in equation (3.6) . We will solve the algebraic equations:
According to the discussion of the previous section, in order to calculate the roots of these equations we have to solve the linear system (3.28) , where the coefficients are The derivatives will be calculated approximately using finite differences and similarly for the second derivatives We are now ready to write the program for the Newton-Raphson method like in the previous section. The only difference is the approximate calculation of the derivatives using the relations above and the calculation of the function by a routine that will compose the function -times. The program can be found in the file bifurcationPoints.f90:Compiling and running the program can be done as follows:
The above listing shows the points of the 2-cycle and some of the points of the 4-cycle. It is also possible to compare the calculated value with the expected one . Improving the accuracy of the calculation is left as an exercise for the reader who has to control the systematic errors of the calculations and achieve better accuracy in the computation of .
We have seen that when , the trajectories of the logistic map become non periodic and exhibit chaotic behavior. Chaotic behavior mostly means sensitivity of the evolution of a dynamical system to the choice of initial conditions. More precisely, it means that two different trajectories constructed from infinitesimally close initial conditions, diverge very fast from each other. This implies that there is a set of initial conditions that densely cover subintervals of whose trajectories do not approach arbitrarily close to any cycle of finite length.
Assume that two trajectories have , as initial points and . When the points , have a distance that for small enough increases exponentially with (the “time”), i.e.
| (3.34) |
the system is most likely exhibiting chaotic behavior11 . The exponent is called a Liapunov exponent. A useful equation for the calculation of is
| (3.35) |
This relation can be easily proved by considering infinitesimal so that . Then we obtain
We can show by induction that and by taking the logarithm and the limits we can prove (3.35) .A first attempt to calculate the Liapunov exponents could be made by using the definition (3.34) . We modify the program logistic.f90 so that it calculates two trajectories whose initial distance is epsilon:
After running the program, the quantity is found at the fourth column of the file lia.dat. The curves of figure 3.7 can be constructed by using the commands:
The last line plots the stdout of the command "echo 200 3.6 0.2 1e-15 |./l;cat lia.dat", i.e. the contents of the file lia.dat produced after running our program using the parameters NSTEPS , r , x0 and epsilon . The gnuplot command set logscale y, puts the y axis in a logarithmic scale. Therefore an exponential function is shown as a straight line and this is what we see in figure 3.7: The points tend to lie on a straight line as decreases. The slopes of these lines are equal to the Liapunov exponent . Deviations from the straight line behavior indicates corrections and systematic errors, as we point out in figure 3.7. A different initial condition results in a slightly different value of , and the true value can be estimated as the average over several such choices. We estimate the error of our computation from the standard error of the mean. The reader should perform such a computation as an exercise.
One can perform a fit of the points to the exponential function in the following way: Since , we can make a fit to a straight line instead. Using gnuplot, the relevant commands are:
The command shown above fits the data to the function a*x+b by taking the 1st column and the logarithm of the 4th column (using 1:(log($4))) of the stdout of the command that we used for creating the previous plot. We choose data for which ([5:53]) and the fitting parameters are a,b (via a,b). The second line, adds the fitted function to the plot.
Now we are going to use equation (3.35) for calculating . This equation is approximately correct when (a) we have already reached the steady state and (b) in the large limit. For this reason we should study if we obtain a satisfactory convergence when we (a) “throw away” a number of NTRANS steps, (b) calculate the sum (3.35) for increasing NSTEPS= (c) calculate the sum (3.35) for many values of the initial point . This has to be carefully repeated for all values of since each factor will contribute differently to the quality of the convergence: In regions that manifest chaotic behavior (large ) convergence will be slower. The program can be found in the file liapunov2.f90:
After NTRANS steps, the program calculates NSTEPS times the sum of the terms . At each step the sum divided by the number of steps i is printed to the file lia.dat. Figure 3.6 shows the results for . This is a point where the system exhibits strong chaotic behavior and convergence is achieved after we compute a large number of steps. Using NTRANS and NSTEPS the achieved accuracy is about % with . The main contribution to the error comes from the different paths followed by each initial point chosen. The plot can be constructed with the gnuplot commands:
The plot command runs the program using the parameters NTRANS , NSTEPS , r and x0 and plots the results from the contents of the file lia.dat.
In order to determine the regions of chaotic behavior we have to study the dependence of the Liapunov exponent on the value of . Using our experience coming from the careful computation of before, we will run the program for several values of using the parameters NTRANS , NSTEPS from the initial point x0 . This calculation gives accuracy of the order of %. If we wish to measure carefully and estimate the error of the results, we have to follow the steps described in figures 3.7 and 3.8. The program can be found in the file liapunov3.f90 and it is a simple modification of the previous program so that it can perform the calculation for many values of .
The program can be compiled and run using the commands:
The character & makes the program ./l to run in the background. This is recommended for programs that run for a long time, so that the shell returns the prompt to the user and the program continues to run even after the shell is terminated.
The data are saved in the file lia.dat and we can make the plot shown in figure 3.7 using gnuplot:
Now we can compare figure 3.9 with the bifurcation diagram shown in figure 3.4. The intervals with correspond to stable -cycles. The intervals where correspond to manifestation of strong chaos. These intervals are separated by points with where the system exhibits weak chaos. This means that neighboring trajectories diverge from each other with a power law instead of an exponential, where is a positive exponent that needs to be determined. The parameter is the one usually used in the literature. Strong chaos is obtained in the limit. For larger , switching between chaotic and stable periodic trajectories is observed each time changes sign. The critical values of can be computed with relatively high accuracy by restricting the calculation to a small enough neighborhood of the critical point. You can do this using the program listed above by setting the parameters rmin and rmax.
We can also study the chaotic properties of the trajectories of the logistic map by computing the distribution of the values of in the interval . After the transitional period, the distribution for the cycles will have support only at the points of the cycles, whereas for the chaotic regimes it will have support on subintervals of . The distribution function is independent for most of the initial points of the trajectories. If one obtains a large number of points from many trajectories of the logistic map, it will be practically impossible to understand that these are produced by a deterministic rule. For this reason, chaotic systems can be used for the production of pseudorandom numbers, as we will see in chapter 11. By measuring the entropy, which is a measure of disorder in a system, we can quantify the “randomness” of the distribution. As we will see in chapter 12, it is given by the equation
| (3.37) |
where is the probability of observing the state . In our case, we can make an approximate calculation of by dividing the interval to subintervals of width . For given we obtain a large number of values of the logistic map and we compute the histogram of their distribution in the intervals . The probability density is obtained from the limit of as becomes large and small (large ). Indeed, converges to . We will define .
The program listed below calculates for chosen values of , and then the entropy is calculated using (3.37) . It is a simple modification of the program in liapunov3.f90 where we add the parameter NHIST counting the number of intervals for the histograms. The probability density is calculated in the array p(NHIST). The program can be found in the file entropy.f90:
For the calculation of the distribution functions and the entropy we have to choose the parameters which control the systematic error. The parameter NTRANS should be large enough so that the transitional behavior will not contaminate our results. Our measurements must be checked for being independent of its value. The same should be done for the initial point xstart. The parameter NHIST controls the partitioning of the interval and the width , so it should be large enough. The parameter NSTEPS is the number of “measurements” for each value of and it should be large enough in order to reduce the “noise” in . It is obvious that NSTEPS should be larger when becomes smaller. Appropriate choices lead to the plots shown in figures 3.10 and 3.11 for , and . We see that stronger chaotic behavior means a wider distribution of the values of .
The entropy is shown in figure 3.12. The stable periodic trajectories lead to small entropy, whereas the chaotic ones lead to large entropy. There is a sudden increase in the value of the entropy at the beginning of chaos at , which increases even further as the chaotic behavior becomes stronger. During the intermissions of the chaotic behavior there are sudden drops in the value of the entropy. It is quite instructive to compare the entropy diagrams with the corresponding bifurcation diagrams (see figure 3.4) and the Liapunov exponent diagrams (see figure 3.9). The entropy is increasing until reaches its maximum value 4, but this is not done smoothly. By magnifying the corresponding areas in the plot, we can see an infinite number of sudden drops in the entropy in intervals of that become more and more narrow.
Several of the programs that you need to write for solving the problems of this chapter can be found in the Problems directory of the accompanying software of this chapter.
As you can see, we set NSTEPS = 1000, r = 0.5, x0 = 0.5. By setting the limits [10:] to the fit command, the fit includes only the points , therefore avoiding the transitional period and the deviation from the exponential falloff for small .
The solutions of the equation (3.3) is . How is this related to the values that you computed in your table?
What do you observe?
The command r=1 sets the value of . Take , , , , . Determine the fixed points and the -cycles from the intersections of the plots with the diagonal .
The file rcrit contains the values of table 3.1. You should vary the parameters nmin, nmax and repeat until you obtain a stable fit.
| (3.38) |
| (3.39) |
| (3.40) |
| (3.41) |
Construct the bifurcation diagram for . Within which limits do the values of lie in? On the same graph, plot the functions , .
Magnify the diagram in the area and . At which point do the two disconnected intervals within which take their values merge into one? Magnify the areas , and , and determine the merging points of two disconnected intervals within which take their values.
| (3.42) |
Construct the bifurcation diagram for and . Make your program to take as the initial point of the new trajectory to be the last one of the previous trajectory and choose for . Repeat for . What do you observe? Note that as is increased, we obtain bifurcations and “anti-bifurcations”.
| (3.43) |
(Make sure that your program keeps the values of so that ). Construct the bifurcation diagram for and .
|
In this chapter we will study the numerical solution of classical equations of motion of one dimensional mechanical systems, e.g. a point particle moving on the line, the simple pendulum etc. We will make an introduction to the numerical integration of ordinary differential equations with initial conditions and in particular to the Euler and Runge-Kutta methods. We study in detail the examples of the damped harmonic oscillator and of the damped pendulum under the influence of an external periodic force. The latter system is nonlinear and exhibits interesting chaotic behavior.
Consider the problem of the solution of the dynamical equations of motion of one particle under the influence of a dynamical field given by Newton’s law. The equations can be written in the form
| (4.1) |
where
| (4.2) |
From the numerical analysis point of view, the problems that we will discuss are initial value problems for ordinary differential equations where the initial conditions
| (4.3) |
determine a unique solution . The equations (4.1) are of second order with respect to time and it is convenient to write them as a system of twice as many first order equations:
| (4.4) |
In particular, we will be interested in the study of the motion of a particle moving on a line (1 dimension), therefore the above equations become
When the particle moves on the plane (2 dimensions) the equations of motion become
As a first attempt to tackle the problem, we will study a simple pendulum of length in a homogeneous gravitational field (figure 4.1).
The equations of motion are given by the differential equations
which can be rewritten as a first order system of differential equations The equations above need to be written in a discrete form appropriate for a numerical solution with the aid of a computer. We split the interval of time of integration to equal intervals1 of width , where . The derivatives are approximated by the relations , so that where is the angular acceleration. This is the so-called Euler method. The error at each step is estimated to be of order . This is most easily seen by Taylor expanding around the point and neglecting all terms starting from the second derivative and beyond2 . What we are mostly interested in is in the total error of the estimate of the functions we integrate for at time ! We expect that errors accumulate in an additive way at each integration step, and since the number of steps is the total error should be . This is indeed what happens, and we say that Euler’s method is a first order method. Its range of applicability is limited and we only study it for academic reasons. Euler’s method is asymmetric because it uses information only from the beginning of the integration interval . It can be put in a more balanced form by using the velocity at the end of the interval . This way we obtain the Euler-Cromer method with a slightly improved behavior, but which is still of first orderAn improved algorithm is the Euler–Verlet method which is of second order and gives total error3 . This is given by the equations
The price that we have to pay is that we have to use a two step relation in order to advance the solution to the next step. This implies that we have to carefully determine the initial conditions of the problem which are given only at one given time . We make one Euler time step backwards in order to define the value of . If the initial conditions are , , then we define
| (4.12) |
It is important that at this step the error introduced is not larger than , otherwise it will spoil and eventually dominate the total error of the method introduced by the intermediate steps. At the last step we also have to take
| (4.13) |
Even though the method has smaller total error than the Euler method, it becomes unstable for small enough due to roundoff errors. In particular, the second equation in (4.11) gives the angular velocity as the ratio of two small numbers. The problem is that the numerator is the result of the subtraction of two almost equal numbers. For small enough , this difference has to be computed from the last digits of the finite representation of the numbers and in the computer memory. The accuracy in the determination of decreases until it eventually becomes exactly zero. For the first equation of (4.11) , the term is smaller by a factor compared to the term in Euler’s method. At some point, by decreasing , we obtain and the accuracy of the method vanishes due to the finite representation of real number in the memory of the computer. When the numbers and differ from each other by more that approximately seven orders of magnitude, adding the first one to the second is equivalent to adding zero and the contribution of the acceleration vanishes4 .
Writing programs that implement the methods discussed so far is quite simple. We will write a program that compares the results from all three methods Euler, Euler–Cromer and Euler–Verlet. The main program is mainly a user interface, and the computation is carried out by three subroutines euler, euler_cromer and euler_verlet. The user must provide the function accel(x) which gives the angular acceleration as a function of x. The variable x in our problem corresponds to the angle . For starters we take accel(x)= -10.0 * sin(x), the acceleration of the simple pendulum.
The data structure is very simple: Three real arrays REAL T(P), X(P) and V(P) store the times , the angles and the angular velocities for . The user determines the time interval for the integration from to and the number of discrete times Nt. The latter should be less than P, the size of the arrays. She also provides the initial conditions and . After this, we call the main integration routines which take as input the initial conditions, the time interval of the integration and the number of discrete times Xin,Vin,Tfi,Nt. The output of the routines is the arrays T,X,V which store the results for the time, position and velocity respectively. The results are printed to the files euler.dat, euler_cromer.dat and euler_verlet.dat.
After setting the initial conditions and computing the time step , the integration in each of the subroutines is performed in do loops which advance the solution for time . The results are stored at each step in the arrays T,X,V. For example, the central part of the program for Euler’s method is:
Some care has to be taken in the case of the Euler–Verlet method where one has to initialize the first two steps, as well as take special care for the last step for the velocity:
The full program can be found in the file euler.f90 and is listed below:
Compiling the running the program can be done with the commands:
The last command shows the first 5 lines of the file euler.dat. We see the data for the time, the position and the velocity stored in 3 columns. We can graph the results using gnuplot:
These commands result in plotting the positions and the velocities as a function of time respectively. We can add the results of all methods to the last plot with the commands:
The results can be seen in figures 4.2–4.7. Euler’s method is unstable unless we take a quite small time step. The Euler–Cromer method behaves impressively better. The results converge and remain constant for Nt. The Euler–Verlet method converges much faster, but roundoff errors kick in soon. This is more obvious in figure 4.7 where the initial angular position is large. For small angles we can compare with the solution one obtains for the harmonic pendulum ():
In figures 4.2–4.4 we observe that the results agree with the above formulas for the values of where the methods converge. This way we can check our program for bugs. The plot of the functions above can be done with the following gnuplot commands5 :The results should not be compared only graphically since subtle differences can remain unnoticed. It is more desirable to plot the differences of the theoretical values from the numerically computed ones which can be done using the commands:
The command using 1:($2-x($1)) puts the values found in the first column on the axis and the value found in the second column minus the value of the function x(t) for equal to the value found in the first column on the axis. This way, we can make the plots shown in6 figures 4.11-4.14.
Euler’s method is a one step finite difference method of first order. First order means that the total error introduced by the discretization of the integration interval by discrete times is of order , where is the time step of the integration. In this section we will discuss a generalization of this approach where the total error will be of higher order in . This is the class of Runge-Kutta methods which are one step algorithms where the total discretization error is of order . The local error introduced at each step is of order leading after steps to a maximum error of order
| (4.15) |
In such a case we say that we have a Runge-Kutta method of order. The price one has to pay for the increased accuracy is the evaluation of the derivatives of the functions in more than one points in the interval .
Let’s consider for simplicity the problem with only one unknown function which evolves in time according to the differential equation:
| (4.16) |
Consider the first order method first. The most naive approach would be to take the derivative to be given by the finite difference
| (4.17) |
By Taylor expanding, we see that the error at each step is , therefore the error after integrating from is . Indeed,
| (4.18) |
The geometry of the step is shown in figure 4.8. We start from point 1 and by linearly extrapolating in the direction of the derivative we determine the point .
We can improve the method above by introducing an intermediate point 2. This process is depicted in figure 4.9. We take the point 2 in the middle of the interval by making a linear extrapolation from in the direction of the derivative . Then we use the slope at point 2 as an estimator of the derivative within this interval, i.e. . We use to linearly extrapolate from to . Summarizing, we have that
For the procedure described above we have to evaluate twice at each step, thereby doubling the computational effort. The error at each step (4.19) becomes , however, giving a total error of . So for given computational time, (4.19) is superior to (4.17) .We can further improve the accuracy gain by using the Runge–Kutta method of 4th order. In this case we have 4 evaluations of the derivative per step, but the total error becomes now and the method is superior to that of (4.19) 7. The process followed is explained geometrically in figure 4.10. We use 3 intermediate points for evolving the solution from to . Point 2 is determined by linearly extrapolating from to the midpoint of the interval by using the direction given by the derivative , i.e. . We calculate the derivative at the point 2 and we use it in order to determine point 3, also located at the midpoint of the interval . Then we calculate the derivative at the point 3 and we use it to linearly extrapolate to the end of the interval , thereby obtaining point 4, i.e. . Then we calculate the derivative at the point 4, and we use all four derivative and as estimators of the derivative of the function in the interval . If each derivative contributes with a particular weight in this estimate, the discretization error can become . Such a choice is
We note that the second term of the last equation takes an average of the four derivatives with weights , , and respectively. A generic small change in these values will increase the discretization error to worse than .We remind to the reader the fact that by decreasing the discretization errors decrease, but that roundoff errors will start showing up for small enough . Therefore, a careful determination of that minimizes the total error should be made by studying the dependence of the results as a function of .
Consider the problem of the motion of a particle in one dimension. For this, we have to integrate a system of two differential equations (4.5) for two unknown functions of time and so that
| (4.21) |
In this case, equations (4.20) generalize to:
Programming this algorithm is quite simple. The main program is an interface between the user and the driver routine of the integration. The user enters the initial and final times Ti and Tf and the number of discrete time points Nt. The initial conditions are X10, X20. The main data structure consists of three real arrays T(P), X1(P), X2(P) which store the times and the corresponding values of the functions and , . The main program calls the driver routine RK(T,X1,X2,Ti,Tf,X10,X20,Nt) which “drives” the heart of the program, the subroutine RKSTEP(t,x1,x2,dt) which performs one integration step using equations (4.22) . RKSTEP evolves the functions x1, x2 at time t by one step dt. The routine RK stores the calculated values in the arrays T, X1 and X2 at each step. When RK returns the control to the main program, all the results are stored in T, X1 and X2, which are subsequently printed in the file rk.dat. The full program is listed below and can be found in the file rk.f90:
In this section we will check our programs for correctness and accuracy w.r.t. discretization and roundoff errors. The simplest test is to check the results against a known analytic solution of a simple model. This will be done for the simple harmonic oscillator. Our programs will need small changes which are summarized below. First, we will need to use higher accuracy variables and we will change all variables of type REAL to REAL(8). For this we need to change the corresponding declarations in the beginning of each (sub)program. For each numerical constant in the program we need to put an explicit exponent with the letter D instead of an E. For example 0.5 0.5D0, 1.2E-3 1.2D-3 etc. Then we need to alter the functions that compute the acceleration of the particle to give . We will take (). Therefore the relevant part of the program in euler.f90 becomes
and that of the program in rk.f90 becomes
The programs are run for a given time interval to with the initial conditions , . The time step is varied by varying the number of steps Nt-1. The computed numerical solution is compared to the well known solution for the simple harmonic oscillator
We study the deviation and as a function of the time step . The results are shown in figures 4.11–4.14. We note that for the Euler method and the Euler–Cromer method, the errors are of order as expected. However, the latter has smaller errors compared to the first one. For the Euler–Verlet method, the error turns out to be of order whereas for the 4th order Runge–Kutta is of order8 .Another way for checking the numerical results is by looking at a conserved quantity, like the energy, momentum or angular momentum, and study its deviation from its original value. In our case we study the mechanical energy
| (4.24) |
which is computed at each step. The deviation is shown in figures 4.15–4.18.
In this section we will study a simple harmonic oscillator subject to a damping force proportional to its velocity and an external periodic driving force, which for simplicity will be taken to have a sinusoidal dependence in time,
| (4.25) |
where and is the angular frequency of the driving force.
Consider initially the system without the influence of the driving force, i.e. with . The real solutions of the differential equation9 which are finite for are given by
| (4.26) |
| (4.27) |
In the last case, the solution oscillates with an amplitude decreasing exponentially with time.
In the case, the general solution is obtained from the sum of a special solution and the solution of the homogeneous equation . A special solution can be obtained from the ansatz , which when substituted in (4.25) and solved for and we find that
| (4.29) |
and
| (4.30) |
The solution decreases exponentially with time and eventually only remains. The only case where this is not true, is when we have resonance without damping for , . In that case the solution is
| (4.31) |
The first two terms are the same as that of the simple harmonic oscillator. The last one increases the amplitude linearly with time, which is a result of the influx of energy from the external force to the oscillator.
Our program will be a simple modification of the program in rk.f90. The main routines RK(T,X1,X2,T0,TF,X10,X20,Nt) and RKSTEP(t,x1,x2,dt) remain as they are. We only change the user interface. The basic parameters , , , are entered interactively by the user from the standard input stdin. These parameters should be accessible also by the function f2(t,x1,x2), and one way to be able to do this, is to store them in variables which are placed in a common block. Such variables are accessible to all subprograms that declare a common block with the same name using a COMMON declaration. Such a declaration is shown in the following lines
which when written in a (sub)program, the (sub)program gains access to the “memory position” params where the values of the variables are stored. Another point that needs our attention is the function f2(t,x1,x2) which now takes the velocity x2 in its arguments:
The main program found in the file dlo.f90 is listed below. The subroutines RK, RKSTEP are the same as in rk.f90 and should also be included in the same file.
The results are shown in figures 4.19–4.22. Figure 4.19 shows the transition from a damped motion for to an oscillating motion with damping amplitude for . The exponential decrease of the amplitude is shown in figure 4.21, whereas the dependence of the period from the damping coefficient is shown in figure 4.22. Motivated by equation (4.28) , written in the form
| (4.32) |
we construct the plot in figure 4.22. The right hand side of the equation is put on the horizontal axis, whereas the left hand side on the vertical. Equation (4.32) predicts that both quantities are equal and all measurements should lie on a particular line, the diagonal . The period can be estimated from the time between two consecutive extrema of or two consecutive zeros of the velocity (see figure 4.19).
Finally it is important to study the trajectory of the system in phase space. This can be seen10 in figure 4.20. A point in this space is a state of the system and a trajectory describes the evolution of the system’s states in time. We see that all such trajectories end up as to the point , independently of the initial conditions. Such a point is an example of a system’s attractor.
Next, we add the external force and study the response of the system to it. The system exhibits a transient behavior that depends on the initial conditions. For large enough times it approaches a steady state that does not depend on (almost all of) the initial conditions. This can be seen in figure 4.23. This is easily understood for our system by looking at equations (4.26) – (4.28) . We see that the steady state becomes dominant when the exponentials have damped away. can be written in the form
These equations are verified in figure 4.24 where we study the dependence of the amplitude on the angular frequency of the driving force. Finally we study the trajectory of the system in phase space. As we can see in figure 4.20, this time the attractor is an ellipse, which is a one dimensional curve instead of a zero dimensional point. For large enough times, all trajectories approach their attractor asymptotically.In this section we will study a non-linear dynamical system which exhibits interesting chaotic behavior. This is a simple model which, despite its deterministic nature, the prediction of its future behavior becomes intractable after a short period of time. Consider a simple pendulum in a constant gravitational field whose motion is damped by a force proportional to its velocity and it is under the influence of a vertical, harmonic external driving force:
| (4.34) |
In the equation above, is the angle of the pendulum with the vertical axis, is the damping coefficient, is the pendulum’s natural angular frequency, is the angular frequency of the driving force and is the amplitude of the external angular acceleration caused by the driving force.
In the absence of the driving force, the damping coefficient drives the system to the point , which is an attractor for the system. This continues to happen for small enough , but for the behavior of the system becomes more complicated.
The program that integrates the equations of motion of the system can be obtained by making trivial changes to the program in the file dlo.f90. This changes are listed in detail below, but we note that X1 , X2 , a_0 . The final program can be found in the file fdp.f90. It is listed below, with the understanding that the commands in between the dots are the same as in the programs found in the files dlo.f90, rk.f90.
The final lines in the program are added so that the angle is kept within the interval .
In order to study the system’s properties we will set , , and unless we explicitly state otherwise. The natural period of the pendulum is whereas that of the driving force is . For , with , the point is an attractor, which means that the pendulum eventually stops at its stable equilibrium point. For the attractor is a closed curve, which means that the pendulum at its steady state oscillates indefinitely without circling through its unstable equilibrium point at . The period of motion is found to be twice that of the driving force. For the attractor is an open curve, because at its steady state the pendulum crosses the point. The period of the motion becomes equal to that of the driving force. For we have period doubling for critical values of , but the trajectory is still periodic. For even larger values of the system enters into a chaotic regime where the trajectories are non periodic. For we find the system in a periodic steady state again, whereas for – we have period doubling. For we enter into a chaotic regime again etc. These results can be seen in figures 4.27–4.29. The reader should construct the bifurcation diagram of the system by solving problem 20 of this chapter.
We can also use the so called Poincaré diagrams in order to study the chaotic behavior of a system. These are obtained by placing a point in phase space when the time is an integer multiple of the period of the driving force. Then, if for example the period of the motion is equal to that of the period of the driving force, the Poincaré diagram consists of only one point. If the period of the motion is an –multiple of the period of the driving force then the Poincaré diagram consists of only points. Therefore, in the period doubling regime, the points of the Poincaré diagram double at each period doubling point. In the chaotic regime, the Poincaré diagram consists of an infinite number of points which belong to sets that have interesting fractal structure. One way to construct the Poincaré diagram numerically, is to process the data of the output file fdp.dat using awk11 :
where $omega, $Nt, $TF are the values of the angular frequency , the number of points of time and the final time . We calculate the period T and the time step dt in the program. Then we print those lines of the file where the time is an integer multiple of the period12 . This is accomplished by the modulo operation $1 % T. The value of the expression $1 % T < dt is true when the remainder of the division of the first column ($1) of the file fdp.dat with the period T is smaller than dt. The results in the chaotic regime are displayed in figure 4.30.
We close this section by discussing another concept that helps us in the analysis of the dynamical properties of the pendulum. This is the concept of the basin of attraction which is the set of initial conditions in phase space that lead the system to a specific attractor. Take for example the case for in the regime where the pendulum at its steady state has a circular trajectory with a positive or negative direction. By taking a large sample of initial conditions and recording the direction of the resulting motion after the transient behavior, we obtain figure 4.31.
Equations (4.11) can be obtained from the Taylor expansion
By adding and subtracting the above equations we obtain which give equations (4.11) From the first equation and equations (4.9) we obtain:
| (4.37) |
When we perform a numerical integration, we are interested in the total error accumulated after integration steps. In this method, these errors must be studied carefully:
| (4.38) |
Therefore the total error is .
We also mention the Velocity Verlet method or the Leapfrog method. In this case we use the velocity explicitly:
The last step uses the acceleration which should depend only on the position and not on the velocity.The Verlet methods are popular in molecular dynamics simulations of many body systems. One of their advantages is that the constraints of the system of particles are easily encoded in the algorithm.
In this appendix we will show how the choice of the intermediate point 2 in equation (4.17) reduces the error by a power of . This choice is special, since by choosing another point (e.g. ) the result would have not been the same. Indeed, from the relation
| (4.40) |
By Taylor expanding around the point we obtain
| (4.41) |
Therefore
Note that for the vanishing of the term it is necessary to place the intermediate point at time .This is not a unique choice. This can be most easily seen by a different analysis of the Taylor expansion. Expanding around the point we obtain
where we have set , etc. We define and we will determine the conditions so that the terms of the last equation in the error are identical with those of equation (4.43) . By expanding we obtain Substituting in (4.44) we obtain All we need is to choose The choice , , leads to equation (4.19) . Some other choices in the bibliography are and .
| (4.48) |
Take , and calculate its mechanical energy as a function of time. Is it monotonic? Why? (show that ). Repeat for . When is the system oscillating and when it’s not? Calculate numerically the critical value of for which the system passes from a non oscillating to an oscillating regime. Compare your results with the theoretical expectations.
In this chapter we will study the motion of a particle moving on the plane under the influence of a dynamical field. Special emphasis will be given to the study of the motion in a central field, like in the problem of planetary motion and scattering. We also study the motion of two or more interacting particles moving on the plane, which requires the solution of a larger number of dynamical equations. These problems can be solved numerically by using Runge–Kutta integration methods, therefore this chapter extends and applies the numerical methods studied in the previous chapter.
In two dimensions, the initial value problem that we are interested in, is solving the system of equations (4.6)
The 4th order Runge-Kutta method can be programmed by making small modifications of the program in the file rk.f90. In order to facilitate the study of many different dynamical fields, for each field we put the code of the respective acceleration in a different file. The code which is common for all the forces, namely the user interface and the implementation of the Runge–Kutta method, will be put in the file rk2.f90. The program that computes the acceleration will be put in a file named rk_XXX.f90, where XXX is a string of characters that identifies the force. For example, the file rk2_hoc.f90 contains the program computing the acceleration of the simple harmonic oscillator, the file rk2_g.f90 the acceleration of a constant gravitational field etc.
Different force fields will require the use of one or more coupling constants which need to be accessible to the code in the main program and some subroutines. For this reason, we will provide two variables k1, k2 in a common block:
This common block will be accessed by the acceleration functions f3 and f4, the function energy and the main program where the user will enter the values of k1 and k2. The initial conditions are stored in the variables X10 , X20 , V10 , V20 , and the values of the functions of time will be stored in the arrays X1(P) , X2(P) , V1(P) , V2(P) . The integration is performed by a call to the subroutine
The results are written to the file rk2.dat. Each line in this file contains the time, position, velocity and the total mechanical energy, where the energy is calculated by the function energy(t,x1,x2,v1,v2):
The code for the function energy, which is different for each force field, is written in the same file with the acceleration. The code for the subroutine RKSTEP(t,x1,x2,x3,x4,dt) should be extended in order to integrate four instead of two functions. The full code is listed below:
Consider a particle in the constant gravitational field near the surface of the earth which moves with constant acceleration so that
| (5.2) |
The particle moves on a parabolic trajectory that depends on the initial conditions
where is the direction of the initial velocity and is the maximum height of the trajectory.The acceleration ( f3 , f4) and the mechanical energy is coded in the file rk2_g.f90:
In order to calculate a projectile’s trajectory you may use the following commands:
The analysis of the results contained in the file rk2.dat can be done using gnuplot:
The results can be seen in figures 5.1 and 5.2. We note a small increase in the mechanical energy which is due to the accumulation of numerical errors.
We can animate the trajectory by writing a script of gnuplot commands in a file rk2_animate.gpl
Before calling the script, the user must set the values of the variables icount, skip and nlines. Each time gnuplot reads the script, it plots icount number of lines from rk2.dat. Then the script is read again and a new plot is made with skip lines more than the previous one, unless icount < nlines. The plotted “file” "<cat -n rk2.dat" is the standard output (stdout) of the command cat -n rk2.dat which prints to the stdout the contents of the file rk2.dat line by line, together with the line number. Therefore the plot command reads data which are the line number, the time, the coordinate , the coordinate etc. The keyword using in
instructs the plot command to use the 3rd column on the horizontal axis and if the first column is less than icount ($1<= icount) put on the vertical axis the value of the 4th column if the first column is less than icount. Otherwise ($1 > icount) it prints an undefined number (1/0) which makes gnuplot print nothing at all. You may also uncomment the command pause if you want to make the animation slower. In order to run the script from gnuplot, issue the commands
The scripts shown above can be found in the accompanying software. More scripts can be found there that automate many of the boring procedures. The usage of two of these is explained below. The first one is in the file rk2_animate.csh:
The last line is a command that animates a trajectory read from the file rk2.dat. Each animation frame contains 500 more points than the previous one. The option -r calculates the plot range automatically. The option -h prints a short help message.
A more useful script is in the file rk2.csh.
The option -h prints operating instructions. A menu of forces is available, and a choice can be made using the option -f. The rest of the command line consists of the parameters read by the program in rk2.f90, i.e. the coupling constants k1, k2, the initial conditions x10, x20, v10, v20 and the integration parameters STEPS, t0 and tf. For example, the commands
compute the trajectory of a particle in the constant gravitational field discussed above, the trajectory of an anisotropic harmonic oscillator (k1 = , k2 = ) and the scattering of a particle in a Coulomb field – try them! I hope that you will have enough curiosity to look “under the hood” of the scripts and try to modify them or create new ones. Some advise to the lazy guys: If you need to program your own force field follow the recipe: Write the code of your acceleration field in a file named e.g. rk2_myforce.f90 as we did with rk2_g.f90. Edit the file rk2.csh and modify the line
to
(the variable $forcecode may have more entries than the ones shown above). Count the order of the string myforce, which is 6 in our case. In order to access this force field from the command line, use the option -f 6:
Now, we will study the effect of the air resistance on the motion of the projectile. For small velocities this is a force proportional to the velocity , therefore
By taking we obtain the motion of a particle with terminal velocity ( const., ).The acceleration caused by the air resistance is programmed in the file (k1 , k2 ) rk2_vg.f90:
The results are shown in figure 5.3 where we see the effect of an increasing air resistance on the particle trajectory. The effect of a resistance force of the form is shown in figure 5.4.
Consider the simple planetary model of a “sun” of mass and a planet “earth” at distance from the sun and mass such that . According to Newton’s law of gravity, the earth’s acceleration is
| (5.6) |
where , , . When the hypothesis is not valid, the two body problem is reduced to that of the one body problem with the mass replaced by the reduced mass
| (5.7) |
The force of gravity is conservative and the mechanical energy
| (5.8) |
is conserved. If we choose the origin of the coordinate axes to be the center of the force, the equations of motion (5.6) become
where . This is a system of two coupled differential equations for the functions , . The trajectories are conic sections which are either an ellipse (bound states - “planet”), a parabola (e.g. escape to infinity when the particle starts moving with speed equal to the escape velocity) or a hyperbola (e.g. scattering).Kepler’s third law of planetary motion states that the orbital period of a planet satisfies the equation
| (5.10) |
where is the semi-major axis of the elliptical trajectory. The eccentricity is a measure of the deviation of the trajectory from being circular
| (5.11) |
where is the semi-minor axis. The eccentricity is 0 for the circle and tends to 1 as the ellipse becomes more and more elongated. The foci and are located at a distance from the center of the ellipse. They have the property that for every point on the ellipse
| (5.12) |
The acceleration given to the particle by Newton’s force of gravity is programmed in the file rk2_cb.f90:
We set k1= and take special care to avoid hitting the center of the force, the singular point at . The same code can be used for the electrostatic Coulomb field with k1= .
At first we study trajectories which are bounded. We set , , , and vary . We measure the period and the length of the semi axes of the resulting ellipse. The results can be found in table 5.1.
3.2 | 1.030 | 2.049 |
3.4 | 1.281 | 2.370 |
3.6 | 1.682 | 2.841 |
3.8 | 2.396 | 3.597 |
4.0 | 3.927 | 5.000 |
4.1 | 5.514 | 6.270 |
4.2 | 8.665 | 8.475 |
4.3 | 16.931 | 13.245 |
4.3 | 28.088 | 18.561 |
4.38 | 42.652 | 24.522 |
4.40 | 61.359 | 31.250 |
4.42 | 99.526 | 43.141 |
Some of the trajectories are shown in figure 5.5. There we can see the dependence of the size of the ellipse on the period. Figure 5.6 confirms Kepler’s third law of planetary motion given by equation (5.10) .
In order to confirm Kepler’s third law of planetary motion numerically, we take the logarithm of both sides of equation (5.10)
| (5.13) |
Therefore, the points lie on a straight line. Using a linear least squares fit we calculate the slope and the intercept which should be equal to and respectively. This is left as an exercise.
In the case where the initial velocity of the particle becomes larger than the escape velocity , the particle escapes from the influence of the gravitational field to infinity. The escape velocity corresponds to zero mechanical energy, which gives
| (5.14) |
When , , , we obtain . The numerical calculation of is left as an exercise.
In this section we consider scattering of particles from a central potential1 . We assume particles that follow unbounded trajectories that start from infinity and move almost free from the influence of the force field towards its center. When they approach the region of interaction they get deflected and get off to infinity in a new direction. We say that the particles have been scattered and that the angle between their original and final direction is the scattering angle . Scattering problems are interesting because we can infer to the properties of the scattering potential from the distribution of the scattering angle. This approach is heavily used in today’s particle accelerators for the study of fundamental interactions between elementary particles.
First we will discuss scattering of small hard spheres of radius by other hard spheres or radius . The interaction potential2 is given by
| (5.15) |
where is the distance between the center of from the center of .
Assume that the particles in the beam do not interact with each other and that there is only one collision per scattering. Let be the intensity of the beam3 and its cross sectional area. Assume that the target has particles per unit area. The cross sectional area of the interaction is where and are the radii of the scattered particles and targets respectively (see figure (5.8) ): All the spheres of the beam which lie outside this area are not scattered by the particular target. The total interaction cross section is
| (5.16) |
where is the total number of target spheres which lie within the beam. On the average, the scattering rate is
| (5.17) |
The above equation is the definition of the total scattering cross section of the interaction. The differential cross section is defined by the relation
| (5.18) |
where is the number of particles per unit time scattered within the solid angle .
| (5.19) |
In the last relation we used the cylindrical symmetry of the interaction with respect to the axis of the collision. Therefore
| (5.20) |
This relation can be used in experiments for the measurement of the differential cross section by measuring the rate of detection of particles within the space contained in between two cones defined by the angles and . This is the relation that we will use in the numerical calculation of .
Generally, in order to calculate the differential cross section we shoot a particle at a target as shown in figure 5.9. The scattering angle depends on the impact parameter . The part of the beam crossing the ring of radius , thickness and area is scattered in angles between and . Since there is only one particle at the target we have that . The number of particles per unit time crossing the ring is , therefore
| (5.21) |
(the sign is because as increases, decreases). From the potential we can calculate and from we can calculate . Conversely, if we measure , we can calculate .
The scattering of a charged particle with charge (“electron”) in a Coulomb potential of a much heavier charge (“nucleus”) is called Rutherford scattering. In this case, the interaction potential is given by
| (5.22) |
which accelerates the particle with acceleration
| (5.23) |
The energy of the particle is and the magnitude of its angular momentum is , where . The dependence of the impact parameter on the scattering angle is [38]
| (5.24) |
Using equation (5.21) we obtain
| (5.25) |
Consider the scattering trajectories. The results for same charges are shown in figure 5.10. A similar figure is obtained in the case of opposite charges. In the latter case we have to take special care for small impact parameters where the scattering angle is . A large number of integration steps is needed in order to obtain the desired accuracy. A useful monitor of the accuracy of the calculation is the measurement of the energy of the particle which should be conserved. The results are shown in table 5.2.
We will now describe a method for calculating the cross section by using equation (5.20) . Alternatively we could have used equation (5.21) and perform a numerical calculation of the derivatives. This is left as an exercise for the reader. Our calculation is more like an experiment. We place a “detector” that “detects” particles scattered within angles and . For this reason we split the interval in bins so that . We perform “scattering experiments” by varying with step . Due to the symmetry of the problem we fix to be a constant, therefore a given corresponds to a cone with an opening angle and an apex at the center of scattering. For given we measure the scattering angle and record the number of particles per unit time . The latter is proportional to the area of the ring of radius . All we need now is the beam intensity which is the total number of particles per unit time (note than in the ratio the proportionality constant and cancel) and the solid angle . Finally we can easily use equation (5.19) in order to calculate the total cross section . The program that performs this calculation is in the file scatter.f90 and it is a simple modification of the program in rk2.f90:
The results are recorded in the file scatter.dat. An example session that reproduces figures 5.11 and 5.12 is
The results can be plotted with the gnuplot commands:
The results are in a very good agreement with the theoretical ones given by (5.25) . The next step will be to study other central potentials whose solution is not known analytically.
Consider scattering from a force field
| (5.26) |
This is a very simple classical model of the scattering of a positron by the hydrogen atom. The positron has positive charge and the hydrogen atom consists of a positively charged proton with charge in an electron cloud of opposite charge . We set the scales so that and . We will perform a numerical calculation of , and .
The potential energy is given by
| (5.27) |
where for . The program containing the calculation of the acceleration caused by this force can be found in the file rk_hy.f90:
The results are shown in figures 5.13–5.14. We find that (see problem 5.10).
Another interesting dynamical field is given by the Yukawa potential. This is a phenomenological model of nuclear interactions:
| (5.28) |
This field can also be used as a model of the effective interaction of electrons in metals (Thomas–Fermi) or as the Debye potential in a classic plasma. The resulting force is
| (5.29) |
The program of the resulting acceleration can be found in the file rk2_yu.f90. The results are shown in figures 5.15–5.16.
In this section we will generalize the discussion of the previous paragraphs in the case of a dynamical system with more degrees of freedom. The number of dynamical equations that need to be solved depends on the number of degrees of freedom and we have to write a program that implements the 4th order Runge–Kutta method for an arbitrary number of equations NEQ. We will explain how to allocate memory dynamically, in which case the necessary memory storage space, which depends on NEQ, is allocated at the time of running the program and not at compilation time.
Until now, memory has been allocated statically. This means that arrays have sizes which are known at compile time. For example, in the program rk2.f90 the integer parameter P had a given value which determined the size of all arrays using the declarations:
Changing P after compilation is impossible and if this becomes necessary we have to edit the file, change the value of P and recompile. Dynamical memory allocation allows us to read in Nt and NEQ at execution time and then ask from the operating system to allocate the necessary memory. All we have to do is to declare the shape of the arrays (i.e. how many indices they take) and give them the allocatable attribute. The needed memory can be asked for at execution time by calling the function ALLOCATE. Here is an example:
In this program the arrays have the allocatable attribute and for each : they have an extra index. Therefore the arrays T,X0 are rank-1 arrays and have only one index, whereas the array X is a rank-2 array and has two indices. The user enters the value of Nt and the subroutine finit sets the value of NEQ. The calls to the function ALLOCATE allocate the necessary memory4 . If memory allocation is successful, then the arrays can be used in the same way as the statically allocated ones. When allocatable arrays are not necessary anymore we should make a call to the function DEALLOCATE which returns the unused memory back to the system. Otherwise our program might suffer from “memory leaks” if e.g. the memory is repeatedly asked in a loop that calls a function that allocates memory without deallocating it in the end. Dynamical memory allocation is very convenient but for high performance computing static allocation might be preferable so that the compiler performs a more efficient optimization.
The main program will be written in the file rkA.f90, whereas the force-dependent part of the code will be written in files with names of the form rkA_XXX.f90. In the latter, the user must program a subroutine f(t,X,dXdt) which takes as input the time t and the values of the functions X(NEQ) and outputs the values of their derivatives dXdt(NEQ) at time t. The function finit(NEQ) sets the number of functions in f and it is called once during the initialization phase of the program.
The program in the file rkA.f905 is listed below:
Note the use of array sections:
The expression X (1,:) refers to the first row of the array X. The arrays X0 and X (1,:) are conformable and we can assign the entries in X (1,:) equal to the entries in X0, i.e. X(1,1)=X0(1), X(1,2)=X0(2), ... , X(1,NEQ)=X0(NEQ) in only one statement X(1,:)= X0. Similarly the statement write(...) X(i,:) prints the whole i-th row of the array X whereas the statement X(i,:)= XS assigns X(i,1)=XS(1), X(i,2)=XS(2), ... , X(i,NEQ)=XS(NEQ). Note the vector operations:
which are equivalent to the following do loops
A few words in order to explain what is an INTERFACE block. Up to now we declared only the type of the functions in the calling program. When the arguments of the function are arrays for which we only know their shape and not their size (assumed-shape arrays), the compiler needs more information. We need to declare the arguments, their types and, in case they are arrays, their shapes as well. Each program that calls these functions should include an INTERFACE block which provides this information. For the functions f and energy, the corresponding INTERFACE block is
You may create files like e.g. interfaces.inc with groups of INTERFACE blocks and include them in all subprograms that use them with the statement include "interfaces.inc".
Consider three particles of equal mass exerting a force of gravitational attraction on each other6 like the ones shown in figure 5.17. The forces exerting on each other are given by
| (5.30) |
where and the equations of motion become ()
where . The total energy of the system is
| (5.32) |
The relations shown above are programmed in the file rkA_3pcb.f90 listed below:
In order to run the program and see the results look at the commands in the shell script in the file rkA_3pcb.csh. In order to run the script use command
which will run the program setting , , , , , , , Nt and .
| (5.33) |
where . Compute the solutions of the problem numerically in one dimension and compare them to the known analytic solutions when :
| (5.34) |
where the integration constant as a function of the initial position and energy is given by
| (5.35) |
We obtain a periodic motion with an energy dependent period . For we obtain
| (5.36) |
whereas for
| (5.37) |
In these equations, the integration constant is given by a different relation and not by equation (5.35) . Compute the motion in phase space and study the transition from open to closed trajectories.
Calculate the solutions , , , on the plane for , , and numerically. In the case consider the scattering problem and calculate the functions , and the total cross section .
Consider the problem of scattering and calculate , and numerically. How much do your results depend on the minimum scattering angle?
The technical skill that you will exercise in this chapter is looking for solutions to your numerical problems provided by software written by others. It is important to be able to locate the optimal solution to your problem, find the relevant functions, read the software’s documentation carefully and filter out the necessary information in order to call and link the functions to your program.
The three dimensional equation of motion of a particle is an initial value problem given by the equations (4.6)
For its numerical solution we will use an adaptive stepsize Runge–Kutta algorithm for increased performance and accuracy. Adaptive stepsize is used in cases where one needs to minimize computational effort for given accuracy goal. The method frequently changes the time step during the integration process, so that it is set to be large through smooth intervals and small when there are abrupt changes in the values of the functions. This is achieved by exercising error control either by monitoring a conserved quantity or by computing the same solution using two different methods. In our case, two Runge-Kutta methods are used, one of order and one of order , and the difference of the results is used as an estimate of the truncation error. If the error needs to be reduced, the step size is reduced and if it is satisfactorily small the step size is increased. For the details we refer the reader to [31]. Our goal is not to analyze and understand the details of the algorithm, but to learn how to find and use appropriate and high quality code written by others. The link http://www.netlib.org/ode/ reads
There, we learn that the package provides code for Runge–Kutta methods, whose source is open and written in the Fortran language. We also learn that the code is written for double precision variables, which is suitable for our problem. Last, but not least, we are also happy to learn that it is written by highly reputable people! We download the files rksuite.f, rksuite.doc, details.doc, templates, readme.
In order to link the subroutines provided by the suite to our program we need to read the documentation carefully. In the general case, documentation is available on the web (html, pdf, ...), bundled files with names like README, INSTALL, in whole directories with names like doc/, online help in man or info pages and finally in good old fashioned printed manuals. Good quality software is also well documented inside the source code files, something that is true for the software at hand.
In order to link the suite’s subroutines to our program we need the following basic information:
It is easy to install the software. All the necessary code is in one file rksuite.f. The file rksuite.doc3 contains the documentation. There we read that we need to inform the program about the hardware dependent accuracy of floating point numbers. We need to set the values of three variables:
The variables OUTCH, MCHEPS, DWARF are defined in the subroutine ENVIRN. They are given generic default values but the programmer is free to change them by editing ENVIRN. We should identify the routine in the file rksuite.f and read the comments in it4 :
All we need to do is to comment out the WRITE and STOP commands since we will keep the default values of the OUTCH, MCHEPS, DWARF variables:
In order to check whether the default values are satisfactory, we can use the Fortran intrinsic functions EPSILON() and TINY(). In the file test_envirn.f90, we write a small test program
We compile and run the above program as follows:
We conclude that our choices are satisfactory.
Next we need to learn how to use the subroutines in the suite. By carefully reading rksuite.doc we learn the following: The interface to the adaptive stepsize Runge–Kutta algorithm is the routine UT (UT = “Usual Task”). The routine can use a 2nd-3rd (RK23) order Runge-Kutta pair for error control (METHOD=1), a 4th-5th (RK45) order pair (METHOD=2) or a 7th-8th (RK78) order pair (METHOD=3). We will set METHOD=2 (RK45). The routine SETUP must be called before UT for initialization. The user should provide a function F that calculates the derivatives of the functions we integrate for, i.e. the right hand side of 6.1.
The fastest way to learn how to use the above routines is “by example”. The suite include a templates package which can be unpacked by executing the commands in the file templates using the sh shell:
The file tmpl1a.f contains the solution of the simple harmonic oscillator and has many explanatory comments in it. We encourage the reader to study it carefully, run it and test its results.
After we become wise enough, we write the driver for the integration routine UT, which can be found in the file rk3.f90:
All common parameters and variables are declared in an include file rk3.inc. This is necessary in order for them to be accessible by the function F which calculates the derivatives. The contents of this file are substituted in each line containing the command include ’rk3.inc’.
The number of differential equations is set equal to NEQ=6. The integration method is set by the choice METHOD=2. The variable LENWRK sets the size of the workspace needed by the suite for the intermediate calculations.
The main program starts with the user interface. The initial state of the particle is stored in the array YSTART in the positions . The first three positions are the coordinates of the initial position and the last three the components of the initial velocity. Then we set some variables that determine the behavior of the integration program (see the file rksuite.doc for details) and call the subroutine SETUP. The main integration loop is:
The function F is the subroutine that calculates the derivatives and it will be programmed by us later. The variable t stores the desired moment of time at which we want to calculate the functions. Because of the adaptive stepsize, it can be different than the one returned by the subroutine UT. The actual value of time that the next step lands5 on is tstep. The array Y stores the values of the functions. We choose the data structure to be such that = Y(1), = Y(2), = Y(3) and = Y(4), = Y(5), = Y(6) (the same sequence as in the array YSTART). The function energy(t,Y) returns the value of the mechanical energy of the particle and its code will be written in the same file as that of F. Finally, the variable UFLAG indicates the error status of the calculation by UT and if UFLAG we end the calculation.
Our test code will be on the study of the motion of a projectile in a constant gravitational field, subject also to the influence of a dissipative force . The program is in the file rk3_g.f90. We choose the parameters k1 and k2 so that -k1 and k2.
For convenience we “translated” the values in the array Y(NEQ) into user-friendly variable names6 . If the file rksuite.f is in the directory rksuite/, then the compilation, running and visualization of the results can be done with the commands:
All the above commands can be executed together using the shell script in the file rk3.csh. The script uses the animation script rk3_animate.csh. The following command executes all the commands shown above:
In this section we study the non-relativistic motion of a charged particle in an electromagnetic (EM) field. The particle is under the influence of the Lorentz force:
| (6.2) |
Consider the constant EM field of the form , . The components of the acceleration of the particle are:
This field is programmed in the file rk3_B.f90. We set k1 , k2 , k3 and k4 :
We can also study space-dependent fields in the same way. The fields must satisfy Maxwell’s equations. We can study the confinement of a particle in a region of space by a magnetic field by taking with , and , . Note that . You may also want to calculate the current density from the equation .
The results are shown in figures 6.1–6.4.
Consider a particle of non zero rest mass moving with speed comparable to the speed of light. In this case, it is necessary to study its motion using the equations of motion given by special relativity7 . In the equations below we set . The particle’s rest mass is , its mass is (where ), its momentum is and its energy is . Then the equations of motion in a dynamic field are given by:
| (6.4) |
In order to write a system of first order equations, we use the relations
| (6.5) |
Using we obtain
which is a system of first order differential equations for the functions . Given the initial conditions their solution is unique and it can be computed numerically using the 4th-5th order Runge–Kutta method according to the discussion of the previous section. By using the relations we can use the initial conditions , , , , , instead. Similarly, from the solutions , , , , , we can calculate , , , , , . We always have to check that
| (6.8) |
Since half of the functions that we integrate for are the momentum instead of the velocity components, we need to make some modifications to the program in the file rk3.f90. The main program can be found in the file sr.f90:
The subroutines momentum and velocity compute the transformations (6.7) . In the subroutine momentum we check whether the condition (6.8) is satisfied. These functions are also used in the subroutine F that computes the derivatives of the functions.
The test drive of the above program is the well known relativistic motion of a charged particle in a constant EM field. The acceleration of the particle is given by equations (6.3) . The relativistic kinetic energy of the particle is
| (6.9) |
These relations are programmed in the file sr_B.f90. The contents of the file sr_B.f90 are:
The results are shown in figures 6.5–6.6.
Now we can study a more interesting problem. Consider a simple model of the Van Allen radiation belt. Assume that the electrons are moving within the Earth’s magnetic field which is modeled after a magnetic dipole field of the form:
| (6.10) |
where is the magnetic dipole moment of the Earth’s magnetic field and . The parameter values are approximately equal to , , where is the radius of the Earth. The typical energy of the moving particles is MeV which corresponds to velocities of magnitude . We choose the coordinate axes so that and we measure distance in units8 . Then we obtain:
The magnetic dipole field is programmed in the file sr_Bd.f90:
The results are shown in figure 6.8. The parameters have been exaggerated in order to achieve an aesthetically pleasant result. In reality, the electrons are moving in very thin spirals and the reader is encouraged to use more realistic values for the parameters , , . The problem of why the effect is not seen near the equator is left as an exercise.
Consider point charges which are located at fixed positions on the plane given by their position vectors , . The electric field is given by Coulomb’s law
| (7.1) |
where is the unit vector in the direction of . The components of the field are
The electrostatic potential at is
| (7.3) |
and we have that
| (7.4) |
The electric field lines are the integral curves of the vector field , i.e. the curves whose tangent lines at each point are parallel to the electric field at that point. The magnitude of the electric field is proportional to the density of the field lines (the number of field lines per perpendicular area). This means that the electric flux through a surface is proportional to the number of field lines that cross the surface. Electric field lines of point charge distributions start from positive charges (sources), end in negative charges (sinks) or extend to infinity.
The equipotential surfaces are the loci of the points of space where the electrostatic potential takes fixed values. hey are closed surfaces. Equation (7.4) tells us that a strong electric field at a point is equivalent to a strong spatial variation of the electric potential at this point, i.e. to dense equipotential surfaces. The direction of the electric field is perpendicular to the equipotential surfaces at each point1 , which is the direction of the strongest spatial variation of , and it points in the direction of decreasing . The planar cross sections of the equipotential surfaces are closed curves which are called equipotential lines.
The computer cannot solve a problem in the continuum and we have to consider a finite discretization of a field line. A continuous curve is approximated by a large but finite number of small line segments. The basic idea is illustrated in figure 7.1: The small line segment is taken in the direction of the electric field and we obtain
| (7.5) |
where .
In order to calculate the equipotential lines we use the property that they are perpendicular to the electric field at each point. Therefore, if is in the tangential direction of a field line, then is in the perpendicular direction since . Therefore the equations that give the equipotential lines are
| (7.6) |
The algorithm that will allow us to perform an approximate calculation of the electric field lines and the equipotential lines is the following: Choose an initial point that belongs to the (unique) line that you want to draw. The electric field can be calculated from the known electric charge distribution and equation (7.2) . By using a small enough step we move in the direction to the new position
| (7.7) |
where we use equations (7.5) or (7.6) . The procedure is repeated until the drawing is finished. The programmer sets a criterion for that, e.g. when the field line steps out of the drawing area or approaches a charge closer than a minimum distance.
The hurried, but slightly experienced reader may skip the details of this section and go directly to section 7.4. There she can find the final form of the program and brief usage instructions.
In order to program the algorithm described in the previous section, we will separate the algorithmic procedures into four different but well defined tasks:
In the main program, the variables N, X(N), Y(N) and Q(N) must be set. These can be hard coded by the programmer or entered by the user interactively. The first choice is coded in the program listed below, which can be found in the file ELines.f90. This is version 1 of the main program:
The commands
define two opposite charges Q(1)= -Q(2)= 1.0 located at and respectively. The next lines call the subroutine eline in order to perform the calculation of 8 field lines passing through the points , , , :
These commands print the coordinates of the field lines to the stdout and the user can analyze them further.
The program for calculating the equipotential lines is quite similar. The calls to the subroutine eline are substituted by calls to epotline.
For the program to be complete, we must program the subroutines eline, efield, mdist. This will be done later, and you can find the full code in the file ELines.f90. For the moment, let’s copy the main program4 listed above into the file Elines.f90 and compile and run it with the commands:
The stdout of the program is redirected to the file el.out. We can plot the results with gnuplot:
The result is shown in figure 7.2.
Let’s modify the program so that the user can enter the charge distribution, as well as the number and position of the field lines that she wants to draw, interactively. The part of the code that we need to change is:
The first line asks the user to enter the number of charges in the distribution. It proceeds with reading it from the stdin and prints the result to the stdout. The following loop reads the positions and charges and stores them at the position i of the arrays X(i), Y(i), Q(i). The results are printed to the stdout so that the user can check the values read by the program.
The drawing of the field lines is now done by modifying the code so that:
As a test case, we run the program for one charge located at the origin and we draw one field line passing through the point .
For charge distributions with a large number of point charges, use an editor to record the charges, their positions and the points where the field lines should go through.
If the data listed above is written into a file, e.g. Input, then the command
reads the data from the file Input and redirects the data printed to the stdout to the file el.out. This way you can create a “library” of charge distributions and the field lines of their respective electric fields. The complete code (version 2) is listed below:
If you did the exercises described above, you should have already realized that in order to draw a nice representative picture of the electric field can be time consuming. For field lines one can use simple physical intuition in order to automate the procedure. For distances close enough to a point charge the electric field is approximately isotropic. The number of field lines crossing a small enough curve which contains only the charge is proportional to the charge (Gauss’s law). Therefore we can draw a small circle centered around each charge and choose initial points isotropically distributed on the circle as initial points of the field lines. The code listed below (version 3) implements the idea for charges that are equal in magnitude. For charges different in magnitude, the program is left as an exercise to the reader.
We set the number of field lines around each charge to be equal to 12 (nd=6). The initial points are taken on the circle whose center is (X(i),Y(i)) and its radius is 0.1. The 2*nd points are determined by the angle theta=(PI/nd)*j.
We record the data of a charge distribution in a file, e.g. Input. Below, we list the example of four equal charges located at the vertices of a square:
Then we give the commands:
The results are shown in figures 7.3 and 7.4. The reader should determine the charge distributions that generate those fields and reproduce the figures as an exercise.
For the computation of the equipotential lines we can work in a similar way. We will follow a quick and dirty way which will not produce an accurate picture of the electric field and choose the initial points evenly spaced on an square lattice. For a better choice see problem 5. The listed code is in the file EPotential.f90:
The first and second part of the code is identical to the previous one. In the third part we call the subroutine epotline for drawing an equipotential line for each initial point. The initial points are on a square lattice with (2*nd+1)*(2*nd+1)= 81 points (nd=4). The lattice extends within the limits set by the square , , , (L=1.0). For each point (x0,y0) we calculate the equipotential line that passes through it. We check that this point is not too close to one of the charges by calling the subroutine mdist. The call determines the minimum distance rmin of the point from all the charges which should be larger than L/(nd*10). You can run the program with the commands:
Some of the results are shown in figure 7.5.
In this section we look under the hood and give the details of the inner parts of the program: The subroutines eline and epotline that calculate the field and equipotential lines, the subroutine efield that calculates the electric field at a point and the subroutine mdist that calculates the minimum and maximum distances of a point from the point charges.
The subroutine eline is called by the command:
The input to the routine is the initial point (x0,y0), the number of charges N, the positions of the charges (X(N),Y(N)) and the charges Q(N). The routine needs some parameters in order to draw the field line. These are “hard coded”, i.e. set to fixed values by the programmer that cannot be changed by the user that calls the routine in her program. One of them is the step of equation (7.5) which sets the discretization step of the field line. It also sets the minimum distance of approaching to a charge equal to . The size of the drawing area of the curve is set by the parameter max_dist=20.0. We should also provide a check in the program that checks whether the electric field is zero, in which case the result of the calculation in equation (7.5) becomes indeterminate. By taking , the motion is in the direction of the electric field, which ends on a negative charge or outside the drawing area (why?). In order to draw the line in both directions, set and repeat the calculation.
The code is listed below:
In the first part of the code we have the variable declarations. We only note the declaration
which declares the dimension of the arrays to be N instead of their true dimension P. This is fine, as long as the programmer of the calling program has already checked that N P. The necessary memory for the arrays is allocated in the calling program and the declaration does not provide new storage space. The arrays X,Y, Q are passed to the subroutine “by reference”, i.e. the routine learns about their position in the memory to which it can refer to, and not “by value”. The parameters step and max_dist are fixed by the parameter attribute:
Their values should be the result of a careful study by the programmer since they determine the accuracy of the calculation.
The outmost loop
sets the direction of motion on the field line (i.e. the sign of ). The command do direction = -1,1,2 executes the loop twice by setting the variable direction to take values from to with step equal to .
The commands x0 = xin, y0 = yin define the initial point on the field line. (x0, y0) is the current point on the field line which is printed to the stdout with the command print. The variables (dx, dy) set the step (x0, y0) (x0+dx, y0+dy). The drawing of the field line is done in the inner loop
which is executed provided that the logical expression (rmin .gt. (2.0*step) .and. rmax .lt. max_dist) is .TRUE. This happens as long as the current point is at a distance greater than 2.0*step and the maximum distance from all charges is less than max_dist5 . The minimum and maximum distances are calculated by calling the subroutine mdist.
The electric field, needed in equation (7.5) , is calculated by a call to efield(x0+0.5*dx,y0+0.5*dy,X,Y,Q,N,Ex,Ey). The first two arguments give the point at which we want to calculate the electric field, which is chosen to be the midpoint (x0+dx/2,y0+dy/2) instead of (x0,y0). This improves the stability and the accuracy of the algorithm.
Equation (7.5) is coded in the commands
We also perform checks for the cases E=0.0 and dx=dy=0.0:
When the magnitude of the electric field becomes too small we stop the calculation by exiting the loop with the command exit. The reader can improve the code by adding more checks of singular cases.
The subroutine epotline is programmed in a similar way. The relevant code is listed below:
The differences are minor: The equipotential lines are closed curves, therefore we only need to transverse them in one direction. The criterion for ending the calculation is to approach the initial point close enough or leave the drawing area:
The values of dx, dy are calculated according to equation (7.6) :
The subroutine efield is an application of equations6 (7.2) :
Finally, the subroutine mdist calculates the minimum and maximum distance rmin and rmax of a point (x0,y0) from all the point charges in the distribution:
The initial value of rmin depends of the limits of the drawing area (why?).
In this section we list the programs discussed in the previous sections and provide short usage information for compiling, running and analyzing your results. You can jump into this section without reading the previous ones and go back to them if you need to clarify some points that you find hard to understand.
First we list the contents of the file ELines.f90:
Then we list the contents of the file EPotential.f90:
where ... are the subroutines efield and mdist which are identical to the ones in the file ELines.f90.
In order to compile the program use the commands:
Then, edit a file and name it e.g. Input and write the data that define a charge distribution. For example:
The results are obtained with the commands:
Have fun!
Consider a time independent electric field in an area of space which is empty of electric charge. Maxwell’s equations are reduced to Gauss’s law
| (7.8) |
together with the equation that defines the electrostatic potential7
Equations (7.8) and (7.9) give the Laplace equation for the function :
| (7.10) |
The solution of the equation above is a boundary value problem: We are looking for the potential in a region of space bounded by a closed surface . When the potential is known on the solution to (7.10) is unique and the potential and the electric field is determined everywhere in .
For simplicity consider the problem confined on a plane, therefore . In this case the last term in equation (7.10) vanishes, the region is a compact subset of the plane and is a closed curve.
For the numerical solution of the problem, we approximate by a discrete, square lattice. The potential is defined on the sites of the lattice. We take to be bounded by a square with sides of length . The distance between the nearest lattice points is called the lattice constant . Then , where is the number of lattice points on each side of the square. The continuous solution is approximated by the solution on the lattice, and the approximation becomes exact in the and limits, so that the length remains constant. The curve is approximated by the lattice sites that are located on the perimeter of the square and the loci in the square where the potential takes constant values. This is a simple model of a set of conducting surfaces (points where const. ) in a compact region whose boundary is grounded (points where ). An example is depicted in figure 7.6.
In order to derive a finite difference equation which approximates equation (7.10) , we Taylor expand around a point according to the equations:
By summing both sides of the above equations, taking and ignoring the terms implied by , we obtain The second term in the second line was eliminated by using equation (7.10) .We map the coordinates of the lattice points to integers such that and where . By taking so that and , equation (7.11) becomes:
| (7.12) |
The equation above states that the potential at the position is the arithmetic mean of the potential of the nearest neighbors. We will describe an algorithm which belongs to the class of “successive overrelaxation methods” (SOR) whose basic steps are:
A careful study of the above algorithm requires to test different criteria of “very small change” and test that different choices of the initial function result in the same solution.
We write a program that implements this algorithm in the case of a system which is the projection of two parallel conducting planes inside a grounded cubic box on the plane. The lattice is depicted in figure 7.6, where the black dots correspond to the conductors. All the points of the box have and the two conductors are at constant potential and respectively. The user enters the values and , the lattice size and the required accuracy interactively. The latter is determined by a small number . The convergence criterion that we set is that the maximum difference between the values of the potential between two successive sweeps should be less than .
The data structure is very simple. We use a real array V(L,L) in order to store the values of the potential at each lattice site. A logical array isConductor(L,L) flags each site as a “conductor site” (= .TRUE.) or as a “vacuum site” (=.FALSE.).
The main program reads in the data entered by the user and then calls three subroutines:
The full program is listed below:
The program in the previous section is written in the file LaplaceEq.f90. Compiling and running is done with the commands:
In the example above, the program performs 112 sweeps until the error becomes 0.00958048937 < 0.01. The results are stored in the file data. We can make a three dimensional plot of the function with the gnuplot commands:
The results are shown in figure 7.7
This section contains a short discussion of the case where the space is filled with a continuous static charge distribution given by the charge density function . In this case the Laplace equation becomes the Poisson equation:
| (7.13) |
The equation on the lattice becomes
| (7.14) |
where9 .
The program in the file PoissonEq.f90 solves equation (7.14) for a uniform charge distribution (figure 7.10), where we have set . The reader is asked to reproduce this figure together with figures 7.8 and 7.9.
In the bibliography the algorithm described above is called the Gauss–Seidel method. In this method, the right hand side of equation (7.14) uses the updated values of the potential in the calculation of and is immediately updated. In contrast, the Jacobi method uses the old values of the potential in the right hand side of (7.14) and the new value computed is stored in order to be used in the next sweep. The Gauss–Seidel method is superior to the Jacobi method as far as speed of convergence is concerned. We can generalize Jacobi’s method by defining the residual of equation (7.14)
| (7.15) |
which vanishes when is a solution of equation (7.14) . Then, using , Jacobi’s method can be formulated as
| (7.16) |
where the quantities with index refer to the values of the potential during the -th sweep. The successive overrelaxation (SOR) method is given by:
| (7.17) |
When we have “underrelaxation” and we obtain slower convergence than the Jacobi method. When we have “overrelaxation” and an appropriate choice of can lead to an improvement compared to the Jacobi method. When SOR diverges. Further study of the SOR methods is left as an exercise to the reader.
and construct the corresponding plot for .
for L=31,61,121,241,501 and construct the corresponding plot for . Vary epsilon=0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001. What is the dependence of the number of sweeps on epsilon? Make the plot of (epsilon). Put the points and curves of (epsilon) for all values of L on the same plot.
The diffusion equation is related to the study of random walks. Consider a particle moving on a line (one dimension) performing a random walk. The motion is stochastic and the kernel
| (8.1) |
is interpreted as the probability density to observe the particle at position at time if the particle is at at . The equation that determines is
| (8.2) |
which is the diffusion equation. The coefficient depends on the details of the system that is studied. For example, for the Brownian motion of a dust particle in a fluid which moves under the influence of random collisions with the fluid particles, we have that , where is the (absolute) temperature of the fluid, is the friction coefficient1 of the particle in the fluid and is the Boltzmann constant.
Usually the initial conditions are chosen so that at the particle is localized at one point , i.e.2
| (8.3) |
The interpretation of as a probability density implies that for every we should have that3
| (8.4) |
It is not obvious that this relation can be imposed for every instant of time. Even if is normalized so that (8.4) holds for , the time evolution of is governed by equation (8.2) which can spoil equation (8.4) at later times.
If we impose equation (8.4) at , then it will hold at all times if
| (8.5) |
By taking into account that and that we obtain
The above equation tells us that for functions for which the right hand side vanishes, the normalization condition will be valid for all .A careful analysis of equation (8.2) gives that the asymptotic behavior of for small times is
| (8.7) |
This relation shows that diffusion is isotropic (the same in all directions) and that the probability of detecting the particle drops exponentially with the distance squared from the initial position of the particle. This relation cannot hold for all times, since for large enough times the probability of detecting the particle will be the same everywhere4 .
The return probability of the particle to its initial position is
| (8.8) |
The above relation defines the spectral dimension of space. in our case.
The expectation value of the distance squared of the particle at time is easily calculated5
| (8.9) |
This equation is very important. It tells us that the random walk (Brownian motion) is not a classical motion but it can only be given a stochastic description: A classical particle moving with constant velocity so that results in .
In the following sections we take6 and define
| (8.10) |
Consider a thin rod of length and let be the temperature distribution within the rod at time . The two ends of the rod are kept at constant temperature . If the initial temperature distribution in the rod is , then the temperature distribution at all times is determined by the diffusion equation
| (8.11) |
where is the thermal diffusivity, is the thermal conductivity, is the density and is the specific heat of the rod.
Define
| (8.12) |
where . The function , giving the fraction of the temperature difference to the temperature at the ends of the rod, is dimensionless and
| (8.13) |
These are called Dirichlet boundary conditions7 .
Equation (8.11) becomes
| (8.14) |
Equation (8.6) becomes
| (8.15) |
The relation above cannot be equal to zero at all times due to the boundary conditions (8.13) . This can be easily understood with an example. Suppose that
| (8.16) |
then it is easy to confirm that the boundary conditions are satisfied and that the function
| (8.17) |
is the solution to the diffusion equation. It is easy to see that
The exponential drop of the magnitude of is in agreement with the expectation that the rod will have constant temperature at long times, which will be equal to the temperature at its ends ().
The numerical solution of equation (8.14) will be computed in the interval for . The problem will be defined on a two dimensional discrete lattice and the differential equation will be approximated by finite difference equations.
The lattice is defined by spatial points
| (8.18) |
where the intervals have the same width
| (8.19) |
and by the time points
| (8.20) |
where the time intervals have the same duration
| (8.21) |
We note that the ends of the intervals correspond to
| (8.22) |
The function is approximated by its values on the lattice
| (8.23) |
The derivatives are replaced by the finite differences
| (8.24) |
By equating both sides of the above relations according to (8.14) , we obtain the dynamic evolution of in time
| (8.26) |
This is a one step iterative relation in time. This is very convenient, because one does not need to store the values for all in the computer memory.
The second term (the “second derivative”) in (8.26) contains only the nearest neighbors of the lattice point at a given time slice . Therefore it can be used for all . The relations (8.26) are not needed for the points and since the values are kept constant.
The parameter
| (8.27) |
determines the time evolution in the algorithm. It is called the Courant parameter and in order to have a time evolution without instabilities it is necessary to have
| (8.28) |
This condition will be checked in our analysis empirically.
The fact that equation (8.26) is a one time step iterative relation, leads to a substantial simplification of the structure of the program. Because of this, at each time step, it is sufficient to store the values of the second term (the “second derivative”) in one array. This array will be used in order to update the values of . Therefore we will define only two arrays , and , which store the values of and at time respectively. In the program listed below, the names of these arrays are u(P) and d2udx2(P).
The data is stored in the array positions u(1) ... u(Nx) and d2udx2(1) ... d2udx2(Nx) and the parameter P is taken large enough so that Nx is always smaller than P.
The user enters the Nx, Nt and tf interactively. The values of , and courant are calculated during the initialization.
On exit, we obtain the results in the file d.dat which contains in three columns. When a time slice is printed, the program prints an empty line so that the output is easily read by the three dimensional plotting function splot of gnuplot.
The program is in the file diffusion.f90 and is listed below:
The compilation and running of the program can be done with the commands:
The input to the program ./d is read from the stdin and it is given by the stdout of the command echo through a pipe, as shown in the second line in the listing above. The lines that follow are the standard output stdout of the program.
The three dimensional plot of the function can be made with the gnuplot commands:
In order to make the plot of for a fixed value of we first note that an empty line in the file d.dat marks a change in time. The following awk program counts the empty lines of d.dat and prints only the lines when the number of empty lines that have been encountered so far is equal to 3. The counter n=0, 1, ..., Nt-1 determines the value of . We save the results in the file tj which can be plotted with gnuplot. We repeat as many times as we wish:
The above task can be completed without creating the intermediate file tj by using the awk filter within gnuplot. For example, the commands
run the program for Nx=10, Nt=800, tf= 2 and construct the plot in figure 8.2
It is instructive to compare the results with the known solution . We compute the relative error
The results can be seen in figure 8.3.
In order to study the kernel for the diffusion, or random walk, problem, we should impose the normalization condition (8.4) for all times. In the case of the function defined for the relation becomes
| (8.29) |
In order to maintain this relation at all times, it is necessary that the right hand side of equation (8.15) is equal to 0. One way to impose this condition is to study the diffusion problem on the circle. If we parametrize the circle using the variable , then the points and are identified and we obtain
| (8.30) |
The second relation in the above equations makes the right hand side of equation (8.15) to vanish. Therefore if , we obtain , .
Using the above assumptions, the discretization of the differential equation is done exactly as in the problem of heat conduction. Instead of keeping the values , we apply equation (8.26) also for the points , . In order to take into account the cyclic topology we take
| (8.31) |
and
| (8.32) |
since the neighbor to the right of the point is the point and the neighbor to the left of the point is the point . For the rest of the points equation (8.26) is applied normally.
The program that implements the problem described above can be found in the file diffusionS1.f90. The boundary conditions (8.30) are enforced in the lines
The initial conditions at are chosen so that the particle is located at . For each instant of time we perform measurements in order to verify the equations (8.4) and (8.9) and the fact that const.
The variable prob and we should check that its value is conserved and is always equal to 1.
The variable r2 is a discrete estimator of the expectation value of the distance squared from the initial position. For small enough times it should follow the law given by equation (8.9) .
These variables are written to the file e.dat together with the values , and . The latter are measured in order to check if for large enough times they obtain the same constant value according to the expectation const.
The full code is listed below:
For each moment of time, the program writes the following quantities to the file e.dat:
| (8.33) |
which is an estimator of (8.29) and we expect to obtain for all ,
| (8.34) |
which is an estimator of (8.9) for which we expect to obtain
| (8.35) |
for small times as well as the values of , , .
The values of , , , , , are found in columns 2, 3, 4, 5, 6 and 7 respectively of the file e.dat. The gnuplot commands
compile and run the program within gnuplot. They set , , , , , .
The gnuplot commands
construct the plot in figure 8.4. We observe that for large times we obtain uniform diffusion.
The relation can be easily confirmed by inspecting the values recorded in the file e.dat.
The asymptotic relation can be confirmed with the commands
which construct the plot in figure 8.5.
Finally we make a plot of the function with the commands
and the result is shown in figure 8.6.
Hint: Make your program print only the final temperature distribution .
For the calculation of the eigenvalues we will use software that is found in the well known library Lapack which contains high quality, freely available, linear algebra software. Part of the goals of this chapter is to show to the reader how to link her programs with software libraries. In order to solve the same problem using Mathematica or Matlab see [40] and [41] respectively.
The Hamiltonian of the harmonic oscillator is given by
| (9.1) |
Define the position and momentum scales , so that we can express the above equation using dimensionless terms:
| (9.2) |
If we take the units of energy, distance and momentum to be , and , then we obtain
| (9.3) |
where , and are now dimensionless. The operator can be diagonalized with the help of the creation and annihilation operators and , defined by the relations:
| (9.4) |
or
| (9.5) |
which obey the commutation relation
| (9.6) |
which leads to
| (9.7) |
The eigenstates , of span the Hilbert space of states and satisfy the relations
| (9.8) |
therefore
| (9.9) |
and
| (9.10) |
The position representation of the eigenstates is given by the wavefunctions:
| (9.11) |
where are the Hermite polynomials.
From equations (9.4) and (9.8) we obtain
From the above equations we can easily calculate the Hamiltonian of the anharmonic oscillator
| (9.15) |
The matrix elements of in this representation are:
where can be calculated from equation (9.12) :
| (9.18) |
This relation computes the matrix elements of the matrix from the matrix product of with itself.
The problem of the calculation of the energy spectrum has now been reduced to the problem of calculating the eigenvalues of the matrix .
We start by choosing the dimension of the subspace of the Hilbert space of states . We will restrict ourselves to states within this subspace and we will use the dimensional representation matrices of , and in . For example, when we obtain
| (9.19) |
| (9.20) |
| (9.21) |
Our goal is to write a program that calculates the eigenvalues of the matrix . Instead of reinventing the wheel, we will use ready made routines that calculate eigenvalues and eigenvectors of matrices found in the Lapack library. This library can be found in the high quality numerical software repository Netlib and more specifically at http://www.netlib.org/lapack/. Documentation can be found at http://www.netlib.org/lapack/lug/, but it is also easily accessible online by a Google search or by using the man pages1 .
As inexperienced users we will first look for driver routines that perform a diagonalization process. Since our task is to diagonalize a real symmetric matrix, we pick the subroutine DSYEV (D = double precision, SY = symmetric, EV = eigenvalues with optional eigenvectors). If the documentation of the library is installed in our system, we may use the Linux man pages for accessing it:2
From this page we learn how to use this subroutine:
These originally cryptic pages contain all the necessary information and the reader should familiarize herself with its format. For a quick and dirty use of the routine, all we need to know is the types and usage of its arguments. These are classified as “input”, “output” and “working space” variables (some are in more than one classes). Input is the necessary data that the routine needs in order to perform the computation. Output is where the results of the computation are stored. And working space is the memory provided by the user to the routine in order to store intermediate results.
From the information above we learn that the matrix to be diagonalized is A which is a rectangular matrix with the number of its rows and columns . The number of rows LDA (LDA= “leading dimension of A”) can be larger than N is which case DSYEV will diagonalize the upper left NN part of the matrix3 . In our program we define a large matrix A(LDA,LDA) and diagonalize a smaller submatrix A(N,N). This way we can study many values of using the same matrix. The subroutine can be used in two ways:
The reader should also familiarize herself with the use of the workspace array WORK. This is memory space given to the routine for all its intermediate calculations. Determining the size of this array needs some care. This is given by LWORK and if performance is an issue the reader should read the documentation carefully for its optimal determination. We will make the simple choice LWORK=3*LDA-1. The variable INFO is used as a flag which informs the user whether the calculation was successful, in which case its value is set to 0. In our case, if INFO takes a non zero value, the program will abort the calculation.
Before using the program in a complicated calculation, it is necessary to test its use in a simple, easily controlled problem. We will familiarize ourselves with the use of DSYEV by writing the following program:
The next step is to compile and link the program. In order to link the program to Lapack we have to instruct the linker ld where to find the libraries Lapack and BLAS4 and link them to our program. A library contains compiled software in archives of object files. The convention for their names in a Unix environment is to start with the string “lib” followed by the name of the library and a .a or .so extension. For example, in our case the files we are interested in have the names liblapack.so and libblas.so which can be searched in the file system by the commands:
In order to see the files that they contain we give the commands5 :
In the commands shown above you may have to substitute /usr/lib with the path appropriate for your system. If the program is in the file test.f90, the compilation/linking command is:
The option -L/usr/lib instructs the linker to look for libraries in the /usr/lib directory6 , whereas the options -llapack -lblas instructs the linker to look for any unresolved symbols (i.e. names of external functions and subroutines not coded in our program) first in the library liblapack.a and then in the library libblas.a.
The command shown above produces the executable file test which, when run, produces the result:
We are now ready to tackle the problem of computing the energy spectrum of the anharmonic oscillator. The main program contains the user interface where the basic parameters for the calculation are read from the stdin. The user can specify the dimension DIM of and the coupling constant . Then the program computes the eigenvalues of the matrix , which represents the action of the operator in the representation in . The tasks are allocated to the subroutines calculate_X4, calculate_evs and calculate_H. The subroutine calculate_X4 calculates the matrix . First, the matrix is calculated and then is obtained by computing its fourth power. The matrix can also be calculated analytically and this is left as an exercise to the reader. The subroutine calculate_H calculates the matrix using the result for given by calculate_X4. Finally the eigenvalues are calculated in the subroutine calculate_evs by a call to DSYEV, which are returned to the main program for printing to the stdout. The program is listed below and can be found in the file anharmonic.f90:
Compiling and running the program can be done with the commands:
In the above program we used and . The choice leads us to the simple harmonic oscillator and we obtain the expected solutions: , and the eigenstates (eigenvectors of ) . Similar results can be obtained for larger .
For non zero values of , the finite calculation contains systematic errors from neglecting all the matrix elements for or . Our program calculates the eigenvalues of the finite matrix , and one expects that
| (9.22) |
where
| (9.23) |
is the true -th level eigenvalue of the Hamiltonian . In practice the limit 9.22 for given and is calculated by computing numerically for increasing values of . If convergence to a desired level of accuracy is achieved for the accessible values of , then the approached limit is taken as an approximation to . This process is shown graphically in figures 9.1-9.3 for . Convergence is satisfactory for quite small for but larger values of are needed for . Increasing the value of for fixed makes the use of larger values of necessary. Similarly for a given energy level , increasing also makes the use of larger values of necessary. A session that computes this limit for the ground level energy is shown below7 :
Further automation of this process can be found in the shell script file anharmonic.csh in the accompanying software. We note the large convergence of and that we can take . For higher accuracy, a computation using larger will be necessary.
We can also compute the expectation values of an operator when the anharmonic oscillator is in a state :
| (9.24) |
In practice, the expectation value will be computed from the limit
| (9.25) |
where are the eigenvectors of the finite matrix computed numerically by DSYEV. These are determined by their components , where
| (9.26) |
which are stored in the columns of the array H after the call to DSYEV:
| (9.27) |
Substituting equation (9.26) to (9.24) we obtain
| (9.28) |
and we can use (9.27) for the computation of the sum.
As an application, consider the expectation values of the operators , and . Taking into account that , we obtain the uncertainties and . Their product should satisfy Heisenberg’s uncertainty relation . The results are shown in table 9.1 and in figures 9.4-9.5. The calculation is left as an exercise to the reader.
0 | 0.305814 | 0.826297 | 0.502686 | 0.21223 | 1.19801 | 0.504236 |
1 | 0.801251 | 2.83212 | 1.5064 | 0.540792 | 4.21023 | 1.50893 |
2 | 1.15544 | 5.38489 | 2.49438 | 0.761156 | 8.15146 | 2.49089 |
3 | 1.46752 | 8.28203 | 3.48627 | 0.958233 | 12.6504 | 3.48166 |
4 | 1.75094 | 11.4547 | 4.47845 | 1.13698 | 17.596 | 4.47285 |
5 | 2.01407 | 14.8603 | 5.47079 | 1.30291 | 22.9179 | 5.46443 |
6 | 2.2617 | 18.4697 | 6.4632 | 1.45905 | 28.5683 | 6.45619 |
7 | 2.49696 | 22.2616 | 7.45562 | 1.60735 | 34.5124 | 7.44805 |
8 | 2.72198 | 26.2196 | 8.44804 | 1.74919 | 40.7234 | 8.43998 |
9 | 2.93836 | 30.3306 | 9.44045 | 1.88558 | 47.1801 | 9.43194 |
The physics of the anharmonic oscillator can be better understood by studying the large limit. As shown in figure 9.5, the term dominates in this limit and the expectation value decreases. This means that states that confine the oscillator to a smaller range of are favored. This, using the uncertainty principle, implies that the typical momentum of the oscillator also increases in magnitude. This is confirmed in figure 9.5 where we observe the expectation value to increase with . In order to understand quantitatively these competing effects we will use a scaling argument due to Symanzik. We redefine , in the Hamiltonian and for large enough we obtain8 the asymptotic behavior
| (9.29) |
where is the Hamiltonian of the anharmonic “oscillator” with . Since the operator is independent of , the energy spectrum will have the asymptotic behavior
| (9.30) |
In reference [42] it is shown that for we have that
| (9.31) |
with an accuracy better than one part in . For large values of , the authors obtain the asymptotic behavior
| (9.32) |
where . This relation is tested in figure 9.6 where we observe good agreement with our calculations.
0.002 | 0.501 489 66 | 1.507 419 39 | 2.519 202 12 | 3.536 744 13 | 4.559 955 56 |
0.006 | 0.504 409 71 | 1.521 805 65 | 2.555 972 30 | 3.606 186 33 | 4.671 800 37 |
0.01 | 0.507 256 20 | 1.535 648 28 | 2.590 845 80 | 3.671 094 94 | 4.774 913 12 |
0.05 | 0.532 642 75 | 1.653 436 01 | 2.873 979 63 | 4.176 338 91 | 5.549 297 81 |
0.1 | 0.559 146 33 | 1.769 502 64 | 3.138 624 31 | 4.628 882 81 | 6.220 300 90 |
0.3 | 0.637 991 78 | 2.094 641 99 | 3.844 782 65 | 5.796 573 63 | 7.911 752 73 |
0.5 | 0.696 175 82 | 2.324 406 35 | 4.327 524 98 | 6.578 401 95 | 9.028 778 72 |
0.7 | 0.743 903 50 | 2.509 228 10 | 4.710 328 10 | 7.193 265 28 | 9.902 610 70 |
1 | 0.803 770 65 | 2.737 892 27 | 5.179 291 69 | 7.942 403 99 | 10.963 5831 |
2 | 0.951 568 47 | 3.292 867 82 | 6.303 880 57 | 9.727 323 19 | 13.481 2759 |
50 | 2.499 708 77 | 8.915 096 36 | 17.436 9921 | 27.192 6458 | 37.938 5022 |
200 | 3.930 931 34 | 14.059 2268 | 27.551 4347 | 43.005 2709 | 60.033 9933 |
1000 | 3.694 220 85 | 23.972 2061 | 47.017 3387 | 73.419 1140 | 102.516 157 |
8000 | 13.366 9076 | 47.890 7687 | 93.960 6046 | 146.745 512 | 204.922 711 |
20000 | 18.137 2291 | 64.986 6757 | 127.508 839 | 199.145 124 | 278.100 238 |
0.002 | 5.588 750 05 | 6.623 044 60 | 7.662 759 33 | 8.707 817 30 | |
0.006 | 5.752 230 87 | 6.846 948 47 | 7.955 470 29 | 9.077 353 66 | |
0.01 | 5.901 026 67 | 7.048 326 88 | 8.215 837 81 | 9.402 692 31 | |
0.05 | 6.984 963 10 | 8.477 397 34 | 10.021 9318 | 11.614 7761 | |
0.1 | 7.899 767 23 | 9.657 839 99 | 11.487 3156 | 13.378 9698 | |
0.3 | 10.166 4889 | 12.544 2587 | 15.032 7713 | 17.622 4482 | |
0.5 | 11.648 7207 | 14.417 6692 | 17.320 4242 | 20.345 1931 | |
0.7 | 12.803 9297 | 15.873 6836 | 19.094 5183 | 22.452 9996 | |
1 | 14.203 1394 | 17.634 0492 | 21.236 4362 | 24.994 9457 | |
2 | 17.514 1324 | 21.790 9564 | 26.286 1250 | 30.979 8830 | |
50 | 49.516 4187 | 61.820 3488 | 74.772 8290 | 88.314 3280 | |
200 | 78.385 6232 | 97.891 3315 | 118.427 830 | 139.900 400 | |
1000 | 133.876 891 | 167.212 258 | 202.311 200 | 239.011 580 | |
8000 | 267.628 498 | 334.284 478 | 404.468 350 | 477.855 700 | |
20000 | 363.201 843 | 453.664 875 | 548.916 140 | 648.515 330 | |
We can also use matrix methods in order to calculate the energy spectrum of a particle in a double well potential given by the Hamiltonian:
| (9.33) |
The equilibrium points of the classical motion are located at the minima:
| (9.34) |
When the well is very deep, then for the lowest energy levels the potential can be well approximated by that of a harmonic oscillator with angular frequency , therefore
| (9.35) |
In this case the tunneling effect is very weak and the energy levels are arranged in almost degenerate pairs. The corresponding eigenstates are symmetric and antisymmetric linear combinations of states localized near the left and right minima of the potential. For example, for the two lowest energy levels we expect that
| (9.36) |
where and
| (9.37) |
where the states and are localized to the left and right well of the potential respectively (see also figure 10.4 of chapter 10).
We will use equations (9.12) in order to calculate the Hamiltonian (9.33) . We need to make very small modifications to the code in the file anharmonic.f90. We will only add a routine that calculates the matrices . The resulting program can be found in the file doublewell.f90:
Where is the particle’s favorite place when it is in the states and ? The answer to this question is obtained from the study of the expectation value of the position operator in each one of them. We know that when the particle is in one of the energy eigenstates, then we have that
| (9.38) |
because the potential is even. Therefore
where in the last line we used the relation (9.38) and that the amplitudes . Also9 we have that . Therefore, if we have that and , we obtain
| (9.40) |
Given that for finite , the subroutine DSYEV returns approximations to the coefficients in the columns of the matrix H(DIM,DIM) so that H(m+1,n+1), you may compare the value of with the classical values as is increased.
| (9.41) |
Calculate for , and .
In this chapter, we will study the time independent Schrödinger equation for a non relativistic particle of mass , without spin, moving in one dimension, in a static potential . We will only study bound states. The solutions in this case yield the discrete energy spectrum as well as the corresponding eigenstates of the Hamiltonian in position representation.
From a numerical analysis point of view, the problem consists of solving for the eigensystem of a differential equation with boundary conditions. Part of the solution is the energy eigenvalue which also needs to be determined.
As an exercise, we will use two different methods, one that can be applied to a particle in an infinite well with , and one that can be applied to more general cases. The first method is introduced only for educational purposes and the reader may skip section 10.2 to go directly to section 10.3.
The wave functions , which are the position representation of the energy eigenstates, satisfy the Schrödinger equation
| (10.1) |
with the normalization condition
| (10.2) |
The Hamiltonian operator is given in position representation by
| (10.3) |
and it is Hermitian, i.e. . Equation (10.1) is an eigenvalue problem
| (10.4) |
which, for bound states, has as solutions a discrete set of real functions such that . The numbers are real and they are the (bound) energy spectrum of the particle in the potential1 . The minimum energy is called the ground state energy and the corresponding ground state is given by a non trivial function . According to the Heisenberg uncertainty principle, in this state the uncertainties in momentum and position so that .
The eigenstates form an orthonormal basis
| (10.5) |
so that any (square integrable) wave function which represents the state is given by the linear combination
| (10.6) |
The amplitudes are complex numbers that give the probability to measure energy in the state .
For any state the function
| (10.7) |
is the probability density of finding the particle at position , i.e. the probability of detecting the particle in the interval is given by
| (10.8) |
The normalization condition (10.2) reflects the conservation of probability (independent of time, respected by the time dependent Schrödinger equation) and the completeness (in this case the certainty that the particle will be observed somewhere on the axis).
The classical observables of this quantum mechanical system are functions of the position and the momentum and their quantum mechanical versions are given by operators . Their expectation values when the system in a state are given by
| (10.9) |
From a numerical point of view, the eigenvalue problem (10.1) requires the solution of an ordinary second order differential equation. There are certain differences in this problem compared to the ones studied in previous sections:
As an introduction to such classes of problems, we will present some simple methods which are special to one dimension.
For the numerical solution of the above equation we renormalize , the function and the parameters so that we deal only with dimensionless quantities. Equation (10.1) is rewritten as:
| (10.10) |
Then we choose a length scale which is defined by the parameters of the problem2 and we redefine . We define and we obtain
| (10.11) |
We define , and change notation to , . We obtain
| (10.12) |
The solutions of equation (10.1) can be obtained from those of equation (10.12) by using the following “dictionary”3 :
| (10.13) |
The dimensionless momentum is defined as and we obtain
| (10.14) |
The commutation relation becomes . The kinetic energy is given by
| (10.15) |
and the Hamiltonian
| (10.16) |
In what follows, we will omit the tilde above the symbols and write instead of .
The simplest model for studying the qualitative features of bound states is the infinite potential well of width where a particle is confined within the interval :
| (10.17) |
The length scale chosen here is and the dimensionless variable corresponds to when is measured in length units.
The solution of (10.12) can be easily computed. Due to the symmetry
| (10.18) |
of the potential, the solutions have well defined parity. This property will be crucial to the method used below. The method discussed in the next section can also be used on non even potentials.
The solutions are divided into two categories, one with even parity for and one with odd parity for .
| (10.19) |
where
| (10.20) |
and the normalization has been chosen so that4 .
The solutions can be found by using the parity of the wave functions. We note that for the positive parity solutions
| (10.21) |
whereas for the negative parity solutions
| (10.22) |
The constant depends on the normalization of the wave function. Therefore we can set originally and then renormalize the wave function so that equation (10.2) is satisfied. If the energy is known, the relations (10.21) and (10.22) can be taken as initial conditions in relation (10.12) . By using a Runge–Kutta algorithm we can evolve the solution towards . The problem is that the energy is unknown. If the energy is not allowed by the quantum theory we will find that the boundary conditions
| (10.23) |
are violated. As we approach the correct value of the energy, we obtain .
Therefore we follow the steps described below:
For the evolution of the solution from to we use the 4th order Runge-Kutta method programmed in the file rk.f90 of chapter 4. We copy the subroutine RKSTEP in a local file rk.f90. The integration of (10.12) can by done by using the function
with the initial conditions We use the notation psi, psip. The functions f1 and f2 correspond to the right hand side of (10.24) . They are the derivatives of and respectively and f1=psip, f2=(V-energy)*psi. The code of f1 and f2 is put in a different file so that we can easily reuse the code for many different potentials . The file wellInfSq.f90 contains the necessary program for the potential of equation (10.17) :We stress that the energy energy is put in a common block so that it can be accessed by the main program.
The main program is in the file well.f90. The user enters the parameters (energy, parity, Nx) and the loop
exits when psinew has an absolute value which is less than epsilon, i.e. when the condition (10.23) is satisfied to the desired accuracy. The value of the energy increases up to the point where the sign of the wave function at changes (psinew*psiold). Then the value of the energy is overestimated and we change the sign of the step de and reduce its magnitude by a half. The algorithm described on page 1064 is implemented inside the loop. After exiting the loop, the energy has been determined with the desired accuracy and the rest of the program stores the solution in the array psifinal(STEPS). The results are written to the file psi.dat. Note how the variable parity is used so that both cases parity can be studied. The full program is listed below:
The compilation and running of the program can be done with the commands
The energy is determined to be 2.467401123 which can be compared to the exact value 2.467401100. The fractional error is . The convergence can be studied graphically in figure 10.2.
The calculation of the excited states is done by changing the parity and by choosing the initial energy slightly higher than the one determined in the previous step6 . The results are in table 10.1. The agreement with the exact result is excellent.
Square | Triangular | Double Well | ||
1 | 2.467401100 | 2.467401123 | 5.248626709 | 15.294378662 |
2 | 9.869604401 | 9.869604492 | 14.760107422 | 15.350024414 |
3 | 22.2066099 | 22.2066040 | 27.0690216 | 59.1908203 |
4 | 39.47841 | 39.47839 | 44.51092 | 59.96887 |
5 | 61.6850275 | 61.6850242 | 66.6384315 | 111.3247375 |
6 | 88.82643 | 88.82661 | 93.84588 | 126.37628 |
7 | 120.902653 | 120.902664 | 125.878830 | 150.745215 |
8 | 157.91367 | 157.91382 | 162.92569 | 194.07578 |
9 | 199.859489 | 199.859490 | 204.845026 | 235.017471 |
10 | 246.74011 | 246.74060 | 251.74813 | 275.67383 |
11 | 298.555533 | 298.555554 | 303.545814 | 331.428306 |
12 | 355.3057 | 355.3064 | 360.3107 | 388.7444 |
We close this section with two more examples. First, we study a potential well with triangular shape at its bottom
| (10.26) |
and then a double well potential with
| (10.27) |
where the parameters are positive numbers. A qualitative plot of these functions is shown in figure 10.1.
For the triangular potential we take , whereas for the double well potential and . The code in wellInfSq.f90 is appropriately modified and saved in the files wellInfTr.f90 and wellInfDbl.f90 respectively. All we have to do is to change the line computing the value of the potential in the function f2. For example the file wellInfTr.f90 contains the code
whereas the file wellInfDbl.f90 contains the code
The analysis is performed in exactly the same way and the results are shown in table 10.1. Note that, for large enough , the energy levels of all the potentials that we studied above tend to have identical values. This happens because, when the particle has energy much larger than , the details of the potential at the bottom do not influence its dynamical properties very much. For the triangular potential, the energy levels have higher values than the corresponding ones of the square potential. This happens because, on the average, the potential energy is higher and the potential tends to confine the particle to a smaller region ( is decreased, therefore is increased). This can be seen in figure 10.3 where the wave functions of the particle in each of the two potentials are compared.
Similar observations can be made for the double well potential. Moreover, we note the approximately degenerate energy levels, something which is expected for potentials of this form. This can be understood in terms of the localized states given by the wave functions and . The first one represents a state where the particle is localized in the left well and the second one in the right. This is shown in figure 10.4. As the two wells decouple and the wave functions become equal to the energy eigenstate wave functions of two particles in separate infinite square wells of width with energy eigenvalues . The difference of and from these two values is due to the finite (see problem 4).
We will now discuss the limitations of this method. First, the method can be used only on potential wells that are even, i.e. . We used this assumption in equations (10.21) and (10.22) giving the initial conditions for states of well defined parity. When the potential is even, the energy eigenstates have definite parity. The other problem can be understood by solving problem 4: When , the wave function is almost zero around and the integration from to will be dominated by numerical errors. The same is true when the particle has to go through high potential barriers.
This method can also we used on potential wells that are not infinite. In that case we can add infinite walls at points that are far enough so that the wave function is practically zero there. Then the influence of this artificial wall will be negligible (see problem 3).
A serious problem with the method discussed in the previous section is that it is numerically unstable. You should have already realized that if you tried to solve problem 3. In that problem, when the walls are moved further than , the convergence of the algorithm becomes harder. You can understand this by realizing that in the integration process the solution is evolved from the classically allowed into the classically forbidden region so that an oscillating solution changes into an exponentially damped one. But as there are two solutions, one that is physically acceptable and one that is diverging which is not acceptable due to (10.2) . Therefore, in order to achieve convergence to the physically acceptable solution, the energy has to be finely tuned, especially when we integrate towards large . For this reason it is preferable to integrate from the exponentially damped region towards the oscillating region. The idea is to start integrating from these regions and try to match the solutions and their derivatives at appropriately chosen matching points. The matching is achieved at a point by trying to determine the value of the energy that sets the ratio
| (10.28) |
equal to zero, within the attainable numerical accuracy. It is desirable to choose a point within the classical region () and usually we pick a turning point . By renormalizing we can always set , therefore means that . The denominator of (10.28) sets the scale of the desired accuracy7 The idea is depicted in figure 10.5. The algorithm is the following:
When we exit the above loop, the current wave function is a good approximation to the eigenfunction corresponding to the eigenvalue . We normalize the wave function according to equation (10.2) and we calculate the expectation values according to (10.9) . It is also interesting to determine the number of nodes9 of the wave function which is related to by .
Our program needs to implement the Runge–Kutta algorithm. We use the routine RKSTEP (see page 539) which performs a 4th order Runge–Kutta step. Its code is copied to the file rk.f90.
The potential is coded in the function V(x). The boundary conditions are programmed in the subroutine boundary(xmin, xmax, psixmin, psipxmin, psixmax, psipxmax) which returns the values of psixmin = , psipxmin = , psixmax = , psipxmax = to the calling program. These routines are put in a separate file for each potential that we want to study. The name of the file is related to the form of the potential, e.g. we choose schInfSq.f90 for the infinite potential well of (10.17) . The same file contains the code for the functions f1, f2:
We note that if the potential becomes infinite for xmin and/or xmax, then this will be determined by the boundary conditions at xmin and/or xmax.
The main program is in the file sch.f90. The code is listed below and it includes the function integrate(psi, dx, Nx) used for the normalization of the wave function. It performs a numerical integration of the square of a function whose values psi(i) i=1,...,Nx are given at an odd number of Nx equally spaced points by a distance dx using Simpson’s rule.
The reproduction of the results of the previous section for the infinite potential well is left as an exercise. The compilation and running of the program can be done with the commands:
We set xmin= -1, xmax = 1, Nx= 2000 and , . The energy of the ground state is found to be . The wave function is stored in the file psi.dat and can be plotted with the gnuplot command
The functions computed during the iterations of the algorithm are stored in the file all.dat. The first column is the iteration number (here we have iter = 0, ... 31) and we can easily filter each one of them with the commands
which reproduce figure 10.6.
The action of an operator on a state can be easily calculated in the position representation by its action on the corresponding wave function . The action of the operators
| (10.29) |
yield10
| (10.30) |
Using equation (10.9) we can calculate the expectation value of the operator when the system is at the state . Interesting examples are the observables “position” , “position squared” , “momentum” , “momentum squared” , “kinetic energy” , “potential energy” , “energy” or “Hamiltonian” whose expectation values are given by the relations
We remind the reader that we used the dimensionless as well as equations (10.15) and (10.16) . Especially interesting are the “uncertainties” , that satisfy the inequality (“Heisenberg’s uncertainty relation”)
| (10.32) |
In the previous section we described how to calculate numerically the eigenfunctions of the Hamiltonian. If , we obtain that . Other operators need a numerical approximation for the calculation of their expectation values. If the values of the wave function are given at equally spaced points , then we obtain
| (10.33) |
where and
| (10.34) |
Both equations entail an error of the order of . Special care should be taken at the endpoints of the interval . As a first approach we will use the naive approximations11
and The relevant program that calculates , , , , , can be found in the file observables.f90 and is listed below:The program needs to read in the wave function at the points in the format produced by the program in sch.f90. The first line should have the energy written at the 3rd column, whereas from the 2nd line and on there should be two columns with the pairs. It is not necessary to have the wave function properly normalized, the program will take care of it. If this data is stored in a file psi.dat, then the program can be used by running the commands
The program prints the normalization constant of , the value of the energy12 , , , , , , and to the stdout.
Some details about the program: In order to read in the data from the file psi.dat we use the functions iargc(), getarg(n,string). The former returns the number of arguments of the command line and the latter stores the n-th argument to the CHARACTER variable string. Therefore, the statements
stop the program if the command line does not have exactly one argument and store the first argument to the variable file.
The command
opens a file which should already exist (status=’OLD’), otherwise an error message is issued. The option err=100 transfers the control of the program to the statement labeled ’100’. In the example shown above, the program stops and prints an error message ’Cannot open filename’ to the stdout.
The commands
read the opened file line by line. The option end=101 at the statement read(11,*,end=101) transfers the control of the program to the labeled statement with label 101 (i.e. outside the do loop) when we reach the end of file.
The rest of the commands are applications of equations (10.33) , (10.34) , (10.35) and (10.36) to the formulas (10.31) and the reader is asked to study them carefully. The program uses the function integrate in order to perform the necessary integrals.
In the previous chapter 9 we studied the quantum mechanical harmonic and anharmonic oscillator in the representation of the energy eigenstates of the harmonic oscillator . In this section we will revisit the problem by using the position representation. We will calculate the eigenfunctions that diagonalize the Hamiltonian (9.15) , which are the solutions of the Schrödinger equation. By setting in equation (10.13) , equation (10.12) becomes
| (10.37) |
where . For we obtain the harmonic oscillator with
| (10.38) |
where are the Hermite polynomials.
We start with the simple harmonic oscillator where the exact solution is known. The potential and the initial conditions are programmed in the file schHOC.f90. The changes that we need to make concern the functions V(x), boundary(xmin, xmax, psixmin, psipxmin, psixmax, psipxmax):
The code omitted at the dots is identical to the one discussed in the previous section. The initial conditions are inspired by the asymptotic behavior of the solutions to Schrödinger’s13 equation , . You are encouraged to test the influence of other choices on the results.
The results are depicted in figure 10.7 where, besides the qualitative agreement, their difference from the known values (10.38) is also shown. This difference turns out to be of the order of –. The values of the energy for are in agreement with (10.38) with relative accuracy better than .
Then we calculate the expectation values , , , , and . These are easily calculated using equations (9.4) and (9.8) . We see that , , whereas
| (10.39) |
The program observables.f90 calculates with accuracy and with accuracy . The expectation values , are shown in table 10.2.
0 | 0.500000000 | 0.4999977 | 0.4999989 |
1 | 1.500000284 | 1.4999883 | 1.4999943 |
2 | 2.499999747 | 2.4999711 | 2.4999854 |
3 | 3.499999676 | 3.4999441 | 3.4999719 |
4 | 4.499999607 | 4.4999082 | 4.4999539 |
5 | 5.499999520 | 5.4998633 | 5.4999314 |
6 | 6.499999060 | 6.4998098 | 6.4999044 |
7 | 7.499999642 | 7.4995484 | 7.4997740 |
8 | 8.499999715 | 8.4994203 | 8.4997100 |
9 | 9.499999837 | 9.4992762 | 9.4996380 |
10 | 10.500000012 | 10.4991160 | 10.4995580 |
11 | 11.499999542 | 11.4994042 | 11.4997019 |
12 | 12.499999610 | 12.4992961 | 12.4996479 |
13 | 13.499999705 | 13.4991791 | 13.4995894 |
14 | 14.499999835 | 14.4990529 | 14.4995264 |
Next, the calculation is repeated for the anharmonic oscillator for . We copy the file schHOC.f90 to schUOC.f90 and change the potential in the function V(x):
0 | 1.0000 | 1.3924 | 1.9031 |
1 | 3.0000 | 4.6488 | 6.5857 |
2 | 5.0000 | 8.6550 | 12.6078 |
3 | 7.0000 | 13.1568 | 19.4546 |
4 | 9.0000 | 18.0576 | 26.9626 |
5 | 11.0000 | 23.2974 | 35.0283 |
6 | 13.0000 | 28.8353 | 43.5819 |
7 | 15.0000 | 34.6408 | 52.5723 |
8 | 17.0000 | 40.6904 | 61.9598 |
9 | 19.0000 | 46.9650 | 71.7129 |
0 | 0.3058 | 0.8263 | 0.5027 | 0.2122 | 1.1980 | 0.5042 |
1 | 0.8013 | 2.8321 | 1.5064 | 0.5408 | 4.2102 | 1.5089 |
2 | 1.1554 | 5.3848 | 2.4944 | 0.7612 | 8.1513 | 2.4909 |
3 | 1.4675 | 8.2819 | 3.4862 | 0.9582 | 12.6501 | 3.4816 |
4 | 1.7509 | 11.4545 | 4.4784 | 1.1370 | 17.5955 | 4.4728 |
5 | 2.0141 | 14.8599 | 5.4707 | 1.3029 | 22.9169 | 5.4643 |
6 | 2.2617 | 18.4691 | 6.4631 | 1.4590 | 28.5668 | 6.4560 |
7 | 2.4970 | 22.2607 | 7.4555 | 1.6074 | 34.5103 | 7.4478 |
8 | 2.7220 | 26.2184 | 8.4478 | 1.7492 | 40.7206 | 8.4397 |
9 | 2.9384 | 30.3289 | 9.4402 | 1.8856 | 47.1762 | 9.4316 |
The wave functions are plotted in figure 10.8. We see that by increasing the particle becomes more confined in space as expected. In table 10.3 we list the values of the energy for . By increasing , is increased. Table 10.4 lists the expectation values , and for the anharmonic oscillator for the states , . By increasing , is decreased and is increased. The product of the uncertainties seems to be quite close to the corresponding values for the harmonic oscillator. The results should be compared with the ones obtained in table 9.1 of chapter 9.
The Lennard–Jones potential is a simple phenomenological model of the interaction between two neutral atoms in a diatomic molecule. This is given by
| (10.40) |
The repulsive term describes the Pauli interaction due to the overlapping of the electron orbitals, whereas the attractive term describes the Van der Waals force. The first one dominates at short distances and the latter at long distances. We choose in (10.13) and define . Equation (10.40) becomes
| (10.41) |
whereas the eigenvalues are related to the energy values by
| (10.42) |
The plot of the potential is shown in figure 10.5 for . The minimum is located at and its value is . The code for this potential is in the file schLJ.f90. The necessary changes to the code discussed in the previous sections are listed below:
0 | -173.637 | 1.186 | 1.0e-10 | 1.415 | 34.193 | 0.091 | 5.847 | 0.534 |
1 | -70.069 | 1.364 | 6.0e-11 | 1.893 | 56.832 | 0.178 | 7.539 | 1.338 |
2 | -18.191 | 1.699 | -4.5e-08 | 2.971 | 39.480 | 0.291 | 6.283 | 1.826 |
3 | -1.317 | 2.679 | -2.6e-08 | 7.586 | 9.985 | 0.638 | 3.160 | 2.016 |
For the integration we choose and xmin = 0.7, xmax . The results are plotted in figure 10.9. There are four bound states. The first two ones are quite confined within the potential well whereas the last ones begin to “spill” out of it. Table 10.5 lists the results. We observe that within the attained accuracy as expected for real, bound states14 .
In this chapter we will study the typical path followed by a ... drunk when he decides to start walking from a given position. Because of his drunkenness, his steps are in random directions and uncorrelated. These are the basic properties of the models that we are going to study. These models are related to specific physical problems like the Brownian motion, the diffusion, the motion of impurities in a lattice, the large distance properties of macromolecules etc. In the physics of elementary particles random walks describe the propagation of free scalar particles and they most clearly arise in the Feynman path integral formulation of the euclidean quantum field theory. Random walks are precursors to the theory of random surfaces which is related to the theory of two dimensional “soft matter” membranes, two dimensional quantum gravity and string theory [44].
The geometry of a typical path of a simple random walk is not classical and this can be seen from two of its non classical properties. First, the average distance traveled by the random walker is proportional to the square root of the time traveled, i.e. the classical relation does not apply. Second, the geometry of the path of the random walker has fractal dimension which is larger than one1 . Similar structures arise in the study of quantum field theories and random surfaces, where the non classical properties of a typical configuration can be described by appropriate generalizations of these concepts. For further study we refer to [7, 43, 44, 45].
In order to simulate a stochastic system on the computer, it is necessary to use random number generators. In most of the cases, these are deterministic algorithms that generate a sequence of pseudorandom numbers distributed according to a desired distribution. The heart of these algorithms generate numbers distributed uniformly from which we can generate any other complex distribution. In this chapter we will study simple random number generators and learn how to use high quality, research grade, portable, random number generators.
The production of pseudorandom2 numbers is at the heart of a Monte Carlo simulation. The algorithm used in their production is deterministic: The generator is put in an initial state and the sequence of pseudorandom numbers is produced during its “time evolution”. The next number in the sequence is determined from the current state of the generator and it is in this sense that the generator is deterministic. Same initial conditions result in exactly the same sequence of pseudorandom numbers. But the “time evolution” is chaotic and “neighboring” initial states result in very different, uncorrelated, sequences. The chaotic properties of the generators is the key to the pseudorandomness of the numbers in the sequence: the numbers in the sequence decorrelate exponentially fast with “time”. But this is also the weak point of the pseudorandom number generators. Bad generators introduce subtle correlations which produce systematic errors. Truly random numbers (useful in cryptography) can be generated by using special devices based on e.g. radioactive decay or atmospheric noise3 . Almost random numbers are produced by the special files /dev/random and /dev/urandom available on unix systems, which read bits from an entropy pool made up from several external sources (computer temperature, device noise etc).
Pseudorandom number generators, however, are the source of random numbers of choice when efficiency is important. The most popular generators are the modulo generators (D.H. Lehmer, 1951) because of their simplicity. Their state is determined by only one integer from which the next one is generated by the relation
| (11.1) |
for appropriately chosen values of , and . In the bibliography, there is a lot of discussion on the good and bad choices of , and , which depend on the programming language and whether we are on a 32–bit or 64–bit systems. For details see the chapter on random numbers in [8].
The value of the integer determines the maximum period of the sequence. It is obvious that if the sequence encounters the same number after steps, then the exact same sequence will be produced and will be the period of the sequence. Since there are at most different numbers, the period is at most equal to . For a bad choice of , and the period will be much smaller. But cannot be arbitrarily large since there is a maximum number of bits that computers use for the storage of integers. For 4-byte (32 bit) unsigned integers the maximum number is , whereas for signed integers . One can prove4 that a good choice of , and results in a sequence which is a permutation of the numbers . This is good enough for simple applications that require fast random number generation but for serious calculations one has to carefully balance efficiency with quality. Good quality random generators are more complicated algorithms and their states are determined by more than one integer. If you need the source code for such generators you may look in the bibliography, like in e.g. [4], [5], [8], [47]. If portability is an issue, we recommend the RANLUX random number generator [47] or the Marsaglia, Zaman and Tsang generator. The Fortran code for RANLUX can also be found in the accompanying software, whereas the MZT generator can be found in Berg’s book/site [5].
In order to understand the use of random number generators, but also in order to get a feeling of the problems that may arise, we list the code of the two functions naiveran() and drandom(). The first one is obviously problematic and we will use it in order to study certain type of correlations that may exist in the generated sequences of random numbers. The second one is much better and can be used in non–trivial applications, like in the random walk generation or in the Ising model simulations studied in the following chapters.
The function naiveran() is a simple application of equation (11.1) with , and :
The function drandom() is also an application of the same equation, but now we set , and . This is the choice of Lewis, Goodman and Miller (1969) and provides a generator that passes many tests and, more importantly, it has been used countless of times successfully. One technical problem is that, when we multiply by , we may obtain a number which is outside the range of 4-byte integers and this will result in an “integer overflow”. In order to have a fast and portable code, it is desirable to stay within the range of the positive, 32-bit (4 byte), signed integers. Schrage has proposed to use the relation
| (11.2) |
where , and . One can show that if and if , then , and that (11.2) is valid. The period of the generator is . The proof of the above statements is left as an exercise to the reader.
The line that checks the result produced by the generator is necessary in order to check for the number which appears once in the sequence. This adds a % overhead, depending on the compiler. If you don’t care about that, you may remove the line. Note that the number seed is put in a common block so it can be accessed by other parts of the program.
Now we will write a program in order to test the problem of correlations in the sequence of numbers produced by naiveran(). The program will produce pairs of integers , where , which are subsequently mapped on the plane. This is done by taking the integer part of the numbers with and is the random number produced by the generator:
The program can be found in the file correlations2ran.f90. In order to test naiveran() we compile with the command
whereas in order to test drandom() we uncomment the print lines as follows
and recompile:
These commands result in two executable files naiveran and drandom. In order to see the results we run the commands
which produce points used in the plots in figures 11.1 and 11.2. In the plot of figure 11.1, we see the pair correlations between the numbers produced by naiveran(). Figure 11.2 shows the points produced by drandom(), and we can see that the correlations shown in figure 11.1 have vanished. The plot in figure 11.2 is qualitative, and a detailed, quantitative, study of drandom() shows that the pairs that it produces, do not pass the test when we have more than points, which is much less than the period of the generator. In order to avoid such problems, there are many solutions that have been proposed and the simplest among them “shuffle” the results so that the low order serial correlations vanish. Such generators will be discussed in the next section.
The uniform distribution of the random numbers produced can be examined graphically by constructing a histogram of the relative frequency of their appearance. In order to construct the histograms we use the script histogram which is written in the awk language5 as shown below:
The command histogram -v f=0.01 constructs a histogram of the data so that the bin width is . The reciprocal of the number following the option -v f=0.01 defines the bin width. The histogram is saved in the file drandom.out.
The results are shown in figures 11.3 and 11.4. Next, we study the variance of the measurements, shown in figure 11.3. The variance is decreased with the size of the sample of the collected random numbers. This is seen in the histogram of figure 11.5. For a quantitative study of the dependence of the variance on the size of the sample, we calculate the standard deviation
| (11.3) |
where is the sequence of random numbers. Figure 11.6 plots this relation. By fitting
| (11.4) |
to a straight line, we see that
| (11.5) |
If we need to generate random numbers which are distributed according to the probability density we can use a sequence of uniformly distributed random numbers in the interval as follows: Consider the cumulative distribution function
| (11.6) |
which is equal to the area under the curve in the interval and it is equal to the probability . If is uniformly distributed in the interval then we have that . Therefore is such that and follows the distribution. Therefore, if form a sequence of uniformly distributed random numbers, then the numbers
| (11.7) |
form a sequence of random numbers distributed according to .
Consider for example the Cauchy distribution
| (11.8) |
Then
| (11.9) |
According to the previous discussion, the random number generator is given by the equation
| (11.10) |
or equivalently (for a more efficient generation)
| (11.11) |
The generator of Gaussian random numbers is found in many applications. The Gaussian distribution is given by the probability density
| (11.12) |
The cumulative distribution function is
| (11.13) |
where is the error function. The error function, as well as its inverse, can be calculated numerically, but this would result in a slow computation. A trick to make a more efficient calculation is to consider the probability density of two independent Gaussian random variables and
| (11.14) |
where , . Then we have that
| (11.15) |
which, upon inversion, it gives
| (11.16) |
Therefore it is sufficient to generate a sequence of uniformly distributed random numbers and take
The algorithm shown above gives a sequence of pseudorandom numbers , which follow the Gaussian distribution6 . The program for is listed below:
The result is shown in figure 11.7. Notice the SAVE attribute for the variables new and x. This means that their values are saved between calls of drandom. We do this because each time we calculate according to (11.17) , we generate two random numbers, whereas the function returns only one. The function needs to know whether it is necessary to generate a new pair (this is what the “flag” new marks) and, if not, to return the previously generated number, saved in the variable x. The analysis of the results is left as an exercise to the reader.
The function drandom() is good enough for the problems studied in this book. However, in many demanding and high accuracy calculations, it is necessary to use higher quality random numbers and/or have the need of much longer periods. In this section we will discuss how to use two high quality, efficient and portable generators which are popular among many researchers.
The first one is an intrinsic procedure in the Fortran language, the subroutine RANDOM_NUMBER. The algorithm implemented is not universal and depends on the Fortran environment7 . For the gfortran compiler RANDOM_NUMBER uses the “multiply-with-carry” algorithm of George Marsaglia in combination with a modulo and shift-register generator. The period is larger than . The state of the generator is determined by more than one numbers. In order to use it we should learn
Saving the current state of the generator is very important when we execute a job that is split in several parts (checkpointing). This is done very often on computer systems that set time limits for jobs or when our jobs are so long (more than 8-10 hours) that it will be painful to loose the resources (time and money) spent for the calculation in case of a computer crash. If we want to restart the job from exactly the same state as it was before we stopped, we also need to restart the random number generator from the same state.
Starting from a new, fresh state is called seeding. The seeding of RANDOM_NUMBER is done by an unspecified number of NSEEDS integers. In order to get this number, we should call the subroutine RANDOM_SEED(size = NSEEDS) which returns the number of seeds NSEEDS when its argument is size = NSEEDS. Then we have to define the integer values of the array seeds(NSEEDS) and call again the routine RANDOM_SEED(PUT = seeds) with the argument PUT = seeds, which will seed the generator from the seeds in seeds. In the code listed below we show how to seed the generator by using only one integer seed:
The last line8 uses the values stored in the array seeds(1) ... seeds(NSEEDS) in order to initialize RANDOM_NUMBER. It is important to note that, using this method, the same seed will generate the same sequence of pseudorandom numbers.
Sometimes we need to initialize the random number generator from as a random initial state as possible, so that each time that we run our program, a different sequence of random numbers is generated. For Unix like systems, like the GNU/Linux system, we can use the two special files /dev/random and /dev/urandom in order to generate cryptographic-grade random numbers. These generate random bits from the current state of the computer and it is practically impossible to predict the obtained sequence of bits. It is preferable to use /dev/urandom because /dev/random ceases to work when there are no new random bits in its pool and waits until they are safely generated. The code that uses9 /dev/urandom for seeding is
The special file /dev/urandom provides binary, non printable, data so it is necessary to open it with the unformatted option. For the same reason, the command read does not have a format instruction but only the unit number. It reads the number of bits necessary to fill the array seeds. If we need to work in an environment where the special file /dev/urandom is not available, it is possible to seed using the current time and the process number ID. The latter is necessary in case we start several processes in parallel and we need different seeds. Check the file seed.f90 in order to see how to do it10 .
In order to save the current state of the random number generator use the subroutine RANDOM_SEED(GET = seeds) with argument GET = seeds. The call stores the necessary information in the array seeds. We can save the values of the array seeds in order to use them to restart the random number generator from exactly the same state. The necessary code is:
In order to restart the generator from a saved state, we read the values of the array seeds and call RANDOM_SEED(PUT = seeds) with argument PUT = seeds. The following code reads seeds from a file named state:
In order to generate random numbers, we can use a scalar variable and generate them one by one or we can use an array which RANDOM_NUMBER will fill with random numbers. The first method has a small overhead and in same cases we will prefer the second one. The code that applies the first method is
and the code that applies the second is
The code in the file test_random_number.f90 implements all of the above tasks and we list it below:
Use the following commands in order to compile and see the results:
A very high quality, portable random number generator was proposed by Martin Lüscher [47], and a program that implements it is Ranlux. Besides the high quality random numbers and a period greater than , a great advantage of RANLUX is that it will run in any Fortran environment. The code, which can also be found in the accompanying software, has been written by Fred James and you can download it in its original form from the links given in the bibliography [47]. The generator uses a subtract-with-borrow algorithm by Marsaglia and Zaman [49], which has a very large period but fails some of the statistical tests. Based on the chaotic properties of the algorithm, Lüscher attributed the problems to short time autocorrelations and proposed a solution in order to eliminate them.
In order to start RANLUX using a single seed, use the subroutine RLUXGO. The necessary code is:
The value of the variable ranlux_level determines the quality of random numbers and it can take the values 1, 2, 3 or 4. Setting ranlux_level=2 is enough for the needs of this book and ranlux_level=3 is the default value. A larger value of ranlux_level requires more computational effort (see problem).
In order to save the current state of RANLUX, we need an integer array of size 25. A call to the subroutine RLUXUT saves the necessary information in this array. We can save this array in order to read it at a later time and start the sequence of random numbers from the same point. The necessary code is:
In order to start RANLUX from a previously saved state we call the subroutine RLUXIN as follows:
We can generate random numbers one by one and store them in a scalar variable
or generate many random numbers with one call and store them in a one dimensional array
where the parameter NR is set equal to the desired value. The program in the file test_ranlux.f90 implements all of the above tasks and we list it below:
Compile the file together with the file ranlux.F, which contains the RANLUX code, and run it with the commands
Consider a particle which is located at one of the sites of a two dimensional square lattice. After equilibrating at this position, it can jump randomly to one of its nearest neighbor positions. There, it might need some time to equilibrate again before jumping to a new position. During this time, the momentum that it had at its arrival is lost, therefore the next jump is made without “memory” of the previous position where it came from. This process is repeated continuously. We are not interested in the mechanism that causes the jumping11 , and we seek a simple phenomenological description of the process.
Assume that the particle jumps in each direction with equal probability and that each jump occurs after the same time . The minimum distance between the lattice sites is (lattice constant). The vector that describes the change of the position of the particle during the -th jump is a random variable and it always has the same magnitude . This means that, given the position of the particle at time , its position at time is
| (11.21) |
where
| (11.22) |
The vectors and are uncorrelated for and we have that
| (11.23) |
The possible values of are equally probable, therefore we obtain
| (11.24) |
This is because the positive and negative terms in the sum performed in the calculation of occur with the same frequency and they cancel each other. Therefore for . Since the magnitude of the vectors is constant, we obtain
| (11.25) |
The probability for a path of length to occur is12
| (11.26) |
where is the number of nearest neighbors of a lattice site. This probability depends on the length of the path and not on its geometry. This can be easily seen using the obvious relation , since there are exactly equally probable cases. The partition function is
| (11.27) |
and it counts the number of different paths of length .
After time the particle is displaced from its original position by
| (11.28) |
The average value of the displacement vanishes
| (11.29) |
The expectation value of the displacement squared is non zero
| (11.30) |
The conclusion is that the random walker has been displaced from its original position rather slowly
| (11.31) |
For a particle with a non zero average velocity we expect that .
Equation (11.31) defines the critical exponent
| (11.32) |
where means asymptotic behavior in the limit . For a classical walker , whereas for the random walker .
The Random Walker (RW) model has several variations, like the Non Reversal Random Walker (NRRW) and the Self Avoiding Walk (SAW) . The NRRW model is defined by excluding the vector pointing to the previous position of the walker and by selecting the remaining vectors with equal probability. The SAW is a NRRW with the additional requirement that, when the walker ends in a previously visited position, the ... walking ends! Some models studied in the literature include, besides the infinite repulsive force, an attractive contribution to the total energy for every pair of points of the path that are nearest neighbors. In this case, each path is weighted with the corresponding Boltzmann weight according to equation (12.4) .
For the NRRW, equation (11.32) is similar to that of the RW, i.e. . Even though the paths differ microscopically, their long distance properties are the same. They are examples of models belonging to the same universality class according to the discussion in section 13.1.
This is not the case for the SAW. For this system we have that [50]
| (11.33) |
therefore the typical paths in this model are longer than those of the RW. If we introduce a nearest neighbor attraction according to the previous discussion, then there is a critical temperature such that for we have similar behavior given by equation (11.33) , whereas for the attractive interaction dominates, the paths collapse and we obtain . For we have that . For more information we refer the reader to the book of Binder and Heermann [7].
In order to write a program that simulates the RW we apply the following algorithm:
All we need to explain is how to program the choice of “random direction”. The program is in the file rw.f90
The length of the paths is Nstep and the number of the generated paths is Nwalk. Their values are hard coded and a run using different values requires recompilation. The results are written to the files dataR and data. The square of the final displacement of the walker is written to dataR and the coordinates of the points visited by the walker in each path is written to data. In order to make the contents of the files available immediately, we empty the I/O buffers by a call to the subroutine flush(unit). The file data is truncated at the beginning of each path, therefore it contains the coordinates of the current path only.
Each path is made of Nstep steps. The random vector is chosen and it is added in the current position . The choice on is made in the line
where the variable ir because the function INT returns the integer part of a real. The values of ir correspond to the four possible directions of . We use the construct select case(ir) in order to move in the direction chosen by ir. Depending on its value, the control of the program is transferred to the command that moves the walker to the corresponding direction.
Compiling and running the program can be done with the commands
Because of the command call sleep(2), the program temporarily halts execution for 2 seconds at the end of each generated path (you should remove this line at the production stage). This allows us to monitor the generated paths graphically. During the execution of the program, use gnuplot in order to plot the random walk which is currently stored in the file data:
Repeat for as many times as you wish to see new random walks. The automation of this process is taken care in the script eternal-rw:
The last command ends the execution of all programs.
Some typical paths are shown in figure 11.8. Figure 11.9 shows the results for the expectation value for which confirm equation (11.30) .
You can reproduce this figure as follows:
Write the results in a file r2.dat in two columns with the length of the paths in the first column and with in the second. The command13 {av+=$1} in the awk program adds the first column of each line of the file dataR to the variable av. After reading the whole file, the command END{print av/NR}, prints the variable av divided by the number of lines in the file (NR = “Number of Records”). This is a simple way for computing the mean of the first column of the file dataR.
The obtained results are meaningless without their statistical errors. Since each measurement is statistically independent, the true expectation value is approached in the limit of infinite measurements with a speed proportional to , where is the number of measurements. For the same reason14 , the statistical error is given by equation (11.3) , e.g.
| (11.34) |
We can add the calculation of the error in the program in rw.f90 or we can leave this task to external utilities. For example we can use the awk script, which is written in the file average:
The contents of this file is an example of a script interpreted by the awk program. The operating system knows which program to use for the interpretation by reading the first line #!/bin/awk -f where the first two characters of the program should be exactly #!. For the commands to be interpreted and executed, one has to make the script executable using the command chmod a+x average. Then the command
executes the script using the awk interpreter. We remind to the reader that the commands between curly brackets { ... } are executed by awk for every line of the file dataR. The commands between END{ ... } are executed after the last line of the file has been read15 . Therefore the lines
add the first column of the file dataR and its square to the variables av and er respectively. The commands
are executed after the whole file dataR has been read and divide the variables av and er with the predefined variable NR which counts the total number of lines read so far. The last lines of the script compute the error according to equation (11.34) and print the final result. The shell script in the file rw1-anal.csh codes all of the above commands in a script. Read the comments in the file for usage instructions.
Upon exit of the command, the program /usr/bin/time prints the total CPU time in seconds to the stderr. Compute the time needed to generate random numbers using the function drandom() and the subroutines random_number and ranlux. For RANLUX, measure the CPU time for each value of ranlux_level from 1 to 4. How does this time depend on whether the random numbers are generated one by one or in groups of 1000 by calls to random_number and ranlux having as an argument an array of size 1000? How does the time change if the random numbers are generated in groups of 10000 instead? (Hint: see the file performance_ran.f90)
In this chapter we review the basic principles of Monte Carlo simulations in statistical mechanics. In the introduction, we review some of the fundamental concepts of statistical physics. The reader should have a basic understanding of concepts like the canonical ensemble, the partition function, the entropy, the density of states and the quantitative description of fluctuations of thermodynamic quantities. For a more in depth discussion of these concepts, see [4, 43, 51, 52, 53, 54].
For most of the interesting systems, the partition function cannot be calculated analytically, and in such a case we may resort to a numerical computation. This is what is done most effectively using Monte Carlo simulations, which consist of collecting a statistical sample of states of the system with an appropriately chosen probability distribution. It is remarkable that, by collecting a sample which is a tiny fraction of the total number of states, we can perform an accurate calculation of its thermodynamic quantities1 . But this is no surprise: it happens in our labs all the time2 !
Statistical physics describes systems with a very large number of degrees of freedom . For simple macroscopic systems – . For such systems, it is practically impossible to solve the microscopic equations that govern their dynamics. Even if we could, the solution would have had much more information than we need (and capable of analyzing!). It is enough, however, to know a small number of bulk properties of the system in order to have a useful description of it. E.g. it is enough to know the internal energy and magnetization of a magnet or the energy and density of a fluid instead of the detailed knowledge of the position, momentum, energy and angular momentum of each particle they are made of. These quantities provide a thermodynamic description of a system. Statistical physics makes an attempt to derive these quantities from the microscopic degrees of freedom and their dynamics given by the Hamiltonian of the system.
Consider a system which can be in a set of discrete states which belong to a countable set . The energy spectrum of those states is assumed to consist of discrete values3 . This system is in contact and interacts with a large heat reservoir which has temperature . The contact with the reservoir results in random transitions which change the energy of the system4 . The system is described by the weights which give the probability to find the system in a state at time . These weights are the connection between the microscopic and statistical description of the system. When this system is in thermal equilibrium with the reservoir, its statistical properties are described by the, so called, canonical ensemble.
Let be the transition rates from the state , i.e.
| (12.1) |
which depend on the interaction between the system and the thermal reservoir. The master equation for the weights is
The first of the above equations tells us that the change in is equal to the rate that the system comes into the state from any other state , minus the rate of leaving the state . The second equation is a result of the probability interpretation of the weights and states that the probability of finding the system in any state is equal to 1 at all times.The transition rates are assumed to be time independent and then the above system of equations for is linear with real parameters. This, together with the constraint , implies that5 , in the large time limit, and the system reaches equilibrium. Then, the converge to finite numbers . These are the equilibrium occupation probabilities
For a system in thermodynamic equilibrium with a reservoir in temperature , with , the probabilities follow the Boltzmann distribution (Gibbs 1902)
| (12.4) |
and define the, so called, canonical ensemble. The parameter will be frequently referred to as simply “the temperature” of the system, although, strictly speaking, it is the inverse of it. Its appearance in the exponential in equation (12.4) , defines a characteristic energy scale of the system. The Boltzmann constant is simply a conversion constant between units of energy6 .
The normalization in equation (12.4) is the so called partition function of the system. The condition implies
| (12.5) |
The measurement of a physical quantity, or observable, of a thermodynamic system has a stochastic character. For systems with very large number of degrees of freedom , one is interested only in the average value of such a quantity. This is because the probability of measuring the quantity to take a value significantly different from its average is ridiculously small. The average, or expectation value, of a physical observable whose value in a state is is equal to
| (12.6) |
As we will see later, the standard deviation for a typical thermodynamic system is such that
| (12.7) |
which is quite small for macroscopic systems7 . In such cases, the fluctuations of the values of from its expectation value can be neglected. The limit is the so called thermodynamic limit, and it is in this limit in which we are studying systems in statistical mechanics. Most systems in the lab are practically in this limit, but in the systems simulated on a computer we may be far from it. The state of the art is to invent methods which can be used to extrapolate the results from the study of the finite system to the thermodynamic limit efficiently.
Because of (12.5) , the partition function encodes all the statistical information about the system. It is not just a simple function of one or more variables, but it counts all the states of the system with the correct weight. Its knowledge is equivalent to being able to compute any thermodynamic quantity like, for example, the expectation value of the energy of the system8 :
Similarly, one can calculate the specific heat from
| (12.9) |
The entropy of a thermodynamic system is defined by
| (12.10) |
where is the free energy if the system. We will attempt to provide microscopic definitions that are consistent with the above equations.
We define the free energy from the relation
or equivalently Note that for the free energy becomes the ground state energy9 . Indeed, as only the lowest energy term in equation (12.11) survives. For this reason, equation (12.10) gives , which is the third law of thermodynamics.The definition (12.11) is consistent with (12.10) since
| (12.13) |
The relation of the entropy to the microscopic degrees of freedom can be derived from equations (12.11) and (12.10) :
| (12.14) |
But
| (12.15) |
therefore
Finally
| (12.17) |
Let’s analyze the above relation in some special cases. Consider a system10 where all possible states have the same energy. For such a system, using equation (12.17) , we obtain that
| (12.18) |
Therefore, the entropy simply counts the number of states of the system. This is also the case in the microcanonical ensemble. Indeed, equation (12.18) is also valid for the distribution
| (12.19) |
which can be considered to be equivalent to the microcanonical ensemble since it enforces Equation (12.19) can viewed as an approximation to a distribution sharply peaked at . In such a case, counts, more or less, the number of states of the system with energy close to .
In general, the function11 is defined to be equal to the number of states with energy equal to . Then the probability to measure energy in the canonical ensemble is
| (12.20) |
Since , we obtain
| (12.21) |
For a generic system we have that
| (12.22) |
where is the number of degrees of freedom of the system and is a constant.
The qualitative behavior of the distribution (12.21) is shown in figure 12.1. For such a system the most probable values of the energy are sharply peaked around a value and the deviation is a measure of the energy fluctuations. The ratio drops with as . Indeed, the function12
| (12.23) |
has a maximum when
| (12.24) |
or
| (12.25) |
As the temperature increases ( decreases), shifts to larger values. is proportional to the system size. By Taylor expanding around we obtain
where we used equation (12.24) and computed . Therefore
| (12.27) |
which is a Gaussian distribution with standard deviation
| (12.28) |
Therefore we confirm the relation (12.7)
| (12.29) |
In the analysis above we assumed analyticity (Taylor expansion, equation (12.26) ), which is not valid at a critical point of a phase transition in the thermodynamic limit.
Another important case where the above analysis becomes slightly more complicated is when the distribution has more than one equally probable maxima13 separated by a large probability barrier as shown in figure 12.2 like when the system undergoes a first order phase transition. Such a transition occurs when ice turns into water or when a ferromagnet looses its permanent magnetization due to temperature increase past its Curie point. In such a case the two states, ice – water / ferromagnet – paramagnet, are equally probable and coexist. This is qualitatively depicted in figure 12.2.
The stochastic behavior of every observable is given by a distribution function which can be derived from the Boltzmann distribution (12.4) . Such a distribution is completely determined by its expectation value and all its higher order moments, i.e. the expectation values , . The most commonly studied moment is the second moment ()
| (12.30) |
For a distribution with a single maximum, is a measure of the fluctuations of away from its expectation value . When we obtain
| (12.31) |
and using the relations
| (12.32) |
and
| (12.33) |
we obtain that
| (12.34) |
which, according to (12.9) , is the specific heat
| (12.35) |
This way we relate the specific heat of a system (a thermodynamic quantity) with the microscopic fluctuations of the energy.
This is true for every physical quantity which is linearly coupled to an external field (in the case of , this role is played by ). For a magnetic system in a constant magnetic field , such a quantity is the magnetization . If is the magnetization of the system in the sate and we assume that its direction is parallel to the direction of the magnetic field , then the Hamiltonian of the system is
| (12.36) |
and the partition function is
| (12.37) |
“Linear coupling” signifies the presence of the linear term in the Hamiltonian. The quantities and are called conjugate to each other. Other well known conjugate quantities are the pressure/volume (/) in a gas or the chemical potential/number of particles (/) in the grand canonical ensemble.
Because of this linear coupling we obtain
| (12.38) |
which is analogous to (12.8) . The equation corresponding to (12.34) is obtained from (12.30) for
| (12.39) |
From (12.37) we obtain
| (12.40) |
therefore
| (12.41) |
The magnetic susceptibility is defined by the equation
| (12.42) |
where we see its relation to the fluctuations of the magnetization. This analysis can be repeated in a similar way for every pair of conjugate quantities.
The correlation functions can be obtained is a similar manner when we consider external fields which are space dependent. For simplicity, consider a system defined on a discrete lattice, whose sites are mapped to natural numbers . Then the magnetic field is a function of the position and interacts with the spin so that
| (12.43) |
Then the magnetization per site 14 at position is
| (12.44) |
The connected two point correlation function is defined by
| (12.45) |
When the values of and are strongly correlated, i.e. they “vary together” in the random samples that we take, the function (12.45) takes on large positive values. When the values of and are not at all correlated with each other, the terms in the sum over in the expectation value cancel each other and is zero15 .
The function takes its maximum value for . Then it falls off quite fast. For a generic system
| (12.46) |
where is the distance between the points and . The correlation length , is a characteristic length scale of the system which is a measure of the distance where there is a measurable correlation between the magnetic moments of two lattice sites. It depends on the parameters that define the system . It is important to stress that it is a length scale that arises dynamically. In contrast, length scales like the size of the system or the lattice constant are parameters of the system which don’t depend on the dynamics. In most of the cases, is of the order of a few lattice constants and such a system does not exhibit correlations at macroscopic scales (i.e. of the order of ).
Interesting physics arises when . This can happen by fine tuning the parameters on which depends on to their critical values. For example, in the neighborhood of a continuous16 phase transition, the exponential falloff in (12.46) vanishes and falls off like a power (see figure 12.3)
| (12.47) |
where is the number of dimensions of space and a critical exponent. As we approach the critical point17 , correlations extend to distances . Then the system is not sensitive to the short distance details of the lattice and its dynamics are very well approximated by continuum space dynamics. Then we say that we obtain the continuum limit of a theory which is microscopically defined on a lattice. Since the microscopic details become irrelevant, a whole class of theories with different microscopic definitions18 have the same continuum limit. This phenomenon is called universality and plays a central role in statistical physics and quantum field theories.
Our main goal is to calculate the expectation value ,
| (12.48) |
of a physical quantity, or observable, of a statistical system in the canonical ensemble approximately. For this reason we construct a sample of states which are distributed according to a chosen probability distribution . We define the estimator of to be
| (12.49) |
The above equation is easily understood since, for a large enough sample, , and we expect that
| (12.50) |
Our goal is to find an appropriate so that the convergence of (12.50) is as fast as possible. Consider the following cases:
We choose , and equation (12.49) becomes
| (12.51) |
The problem with this choice is the small overlap of the sample with the states that make the most important contributions to the sum in (12.48) . As we have already mentioned in the introduction, the size of the sample in a Monte Carlo simulation is a minuscule fraction of the total number of states. Therefore, the probability of picking the ones that make important contributions to the sum in (12.48) is very small. Consider for example the case in a generic model. According to equation (12.21) we have that
| (12.52) |
where is the probability of measuring energy in the system. A qualitative plot of is shown in figure 12.1. From (12.25) and (12.28) we have that and , therefore for and the qualitative behavior of the respective distributions is shown in figure 12.4.
The distribution of the simple sampling corresponds to the case in equation (12.4) , since const. in this case19 . In order to calculate the sum (12.52) with acceptable accuracy for we have to obtain a good sample in the region where the product is relatively important. The probability of obtaining a state such that is non negligible is very small when we use the distribution. This can be seen pictorially in figure 12.4.
Even though this method has this serious shortcoming, it could still be useful in some cases. We have already applied it in the study of random walks. Note that, by applying equation (12.51) , we can use the same sample for calculating expectation values for all values of .
From the previous discussion it has become clear that, for a large system, a very small fraction of the space of states makes a significant contribution to the calculation of . If we choose a sample with probability
| (12.53) |
then we expect to sample exactly within this region. Indeed, the estimator, given by equation (12.49) , is calculated from
| (12.54) |
Sampling this way is called importance sampling, and it is the method of choice in most Monte Carlo simulations. The sample depends on the temperature and the calculation of the expectation values (12.54) requires a new sample for each20 . This extra effort, however, is much smaller than the one required in order to overcome the overlap problem discussed in the previous subsection.
Sampling according to a desired probability distribution is not possible in a direct way. For example, if we attempt to construct a sample according to by picking a state by chance and add it to the sample with probability , then we have a very small probability to accept that state in the sample. Therefore, the difficulty of constructing the sample runs into the same overlap problem as in the case of simple sampling. For this reason we construct a Markov chain instead. The members of the sequence of the chain will be our sample. A Markov process, or a Markov chain, is a stochastic process which, given the system in a state , puts the system in a new state in such a way that it has the Markov property, i.e. that it is memoryless. This means that a chain of states
| (12.55) |
is constructed in such a way that the transition probabilities from the state to a new state satisfy the following requirements:
| (12.56) |
holds. Beware, in most of the cases , i.e. the system has a nonzero probability to remain in the same state
Then our sample will be . We may imagine that this construction happens in “time” . In a Monte Carlo simulation we construct a sample from a Markov chain by appropriately choosing the transition probabilities so that the convergence 4. is fast.
Choosing the initial state can become a non trivial task. If it turns out not to be a typical state of the sample, then it could take a long “time” for the system to “equilibrate”, i.e. for the Markov process to start sampling states typical of the simulated temperature. The required time for this to happen is called the thermalization time which can become a serious part of our computational effort if we make a wrong choice of and/or .
A necessary condition for the sample to converge to the desired distribution is for the process to be ergodic. This means that for every state it is possible to reach any other state in a finite number of steps. If this criterion is not satisfied and a significant part of phase space is not sampled, then sampling will fail. Usually, given a state , the reachable states at the next step (i.e. the states for which ) are very few. Therefore the ergodicity of the algorithm considered must be checked carefully21 .
Equation (12.2) tells us that, in order to find the system in equilibrium in the distribution, the transition probabilities should be such that
| (12.57) |
This means that the rate that the system comes into the state is equal to the rate in which it leaves . From equation (12.56) we obtain
| (12.58) |
This condition is necessary but it is not sufficient (see section 2.2.3 in [4]). A sufficient, but not necessary, condition is the detailed balance condition. When the transition probabilities satisfy
| (12.59) |
then the system will equilibrate to after sufficiently long thermalization time. By summing both sides of (12.59) , we obtain the equilibrium condition (12.57) . For the canonical ensemble (12.4) the condition becomes
| (12.60) |
One can show that if the transition probabilities satisfy the above conditions then the equilibrium distribution of the system will be the Boltzmann distribution (12.4) . A program implementing a Monte Carlo simulation of a statistical system in the canonical ensemble consists of the following main steps:
Equation (12.60) has many solutions. For a given problem, we are looking for the most efficient one. Below we list some possible choices:
| (12.61) |
| (12.62) |
| (12.63) |
for appropriately chosen states and
| (12.64) |
for any other state . In order for (12.64) to be meaningful, the constant has to be chosen so that
| (12.65) |
Equation (12.65) gives much freedom in the choice of transition probabilities. In most cases, we split in two independent parts
| (12.66) |
The probability is the selection probability of the state when the system is in the state . Therefore the first step in the algorithm is to select a state with probability .
The second step is to accept the change with probability . If the answer is no, then the system remains in the state . This way equation (12.64) is satisfied. The probabilities are called the acceptance ratios.
The art in the field is to device algorithms that give the maximum possible acceptance ratios for the new states and that the states are as much as possible statistically independent from the original state . An ideal situation is to have for all for which . As we will see in a following chapter, this is what happens in the case of the Wolff cluster algorithm.
This chapter is an introduction to the basic Monte Carlo methods used in the simulations of the Ising model on a two dimensional rectangular lattice, but also in a wide spectrum of scientific applications. We will introduce the Metropolis algorithm, which is the most common algorithm used in Monte Carlo simulations. We will discuss the thermalization of the system and the effect of correlations between successive spin configurations generated during the simulation. The autocorrelation function and the time scale defined by it, the autocorrelation time, are measures of these autocorrelations and play a central role in the study of the statistical independence of our measurements. Beating autocorrelations is crucial in Monte Carlo simulations since they are the main obstacle for studying large systems, which in turn is essential for taking the thermodynamic limit without the systematic errors introduced by finite size effects. We will also introduce methods for the computation of statistical errors that take into account autocorrelations. The determination of statistical errors is of central importance in order to assess the quality of a measurement and predict the amount of resources needed for reaching a specific accuracy goal.
The Ising model (1925) [55] has played an important role in the evolution of ideas in statistical physics and quantum field theory. In particular, the two dimensional model is complicated enough in order to possess nontrivial properties but simple enough in order to be able to obtain an exact analytic solution. The zero magnetic field model has a 2nd order phase transition for a finite value of the temperature and we are able to compute critical exponents and study its continuum limit in detail. This gives us valuable information on the non analytic properties of a system undergoing a second order phase transition, the appearance of scaling, the renormalization group and universality. Using the exact solution1 of Onsager (1948) [56] and others, we obtain exact results and compare them with those obtained via approximate methods, like Monte Carlo simulations, high and low temperature expansions, mean field theory etc. The result is also interesting from a physics point of view, since it is the simplest, phenomenologically interesting, model of a ferromagnetic material. Due to universality, the model describes also the liquid/vapor phase transition at the triple point. A well known textbook for a discussion of statistical mechanical models that can be solved exactly is the book by Baxter [54].
In order to define the model, consider a two dimensional square lattice like the one shown in figure 13.1. On each site or node of the lattice we have an “atom” or a “magnet” of spin . The geometry is determined by the distance of the nearest neighbors, the lattice constant , and the number of sites . Each side consists of sites so that , where is the dimension of space. The topology is determined by the way sites are connected with each other via links. Special care is given to the sites located on the sides of the lattice. We usually take periodic boundary conditions which is equivalent to identifying the opposite sides of the square by connecting their sites with a link. This is depicted in figure (13.1) . Periodic boundary conditions endow the plane on which the lattice is defined with a toroidal topology. The system’s dynamics are determined by the spin–spin interaction. We take it to be short range and the simplest case considered here takes into account only nearest neighbor interactions.
In the Ising model, spins have two possible values, “up” or “down” which we map2 to the numerical values or . For the ferromagnetic model, each link is a “bond” whose energy is higher when the spins on each side of the link are pointing in the same direction and lower when they point in the opposite3 direction. This is depicted in figure 13.1. The system could also be immersed in a constant magnetic field whose direction is parallel to the direction of the spins.
We are now ready to write the Hamiltonian and the partition function of the system. Consider a square lattice of lattice sites (or vertices) labeled by a number . The lattice has links (or bonds) among nearest neighbors. These are labeled by , where is the pair of vertices on each side of the link. We identify the sides of the square like in figure 13.1. Then, since two vertices are connected by one link and four links intersect at one vertex, we have that4
| (13.1) |
At each vertex we place a spin . The Hamiltonian of the system is given by
| (13.2) |
The first term is the spin–spin interaction and for the system is ferromagnetic. In this book, we consider only the case. A link connecting same spins has energy , whereas a link connecting opposite spins has energy . The difference of the energy between the two states is and the spin-spin dynamics favor links connecting same spins. The minimum energy is obtained for the ground state, which is the unique5 state in which all spins point in the direction6 of . This is equal to
| (13.3) |
| (13.4) |
where is a spin configuration of the system. The number of terms is equal to the number of configurations , which is equal to , i.e. it increases exponentially with . For a humble lattice we have terms.
The two dimensional Ising model for has the interesting property that, for , where
| (13.5) |
it undergoes a phase transition between an ordered or low temperature phase where the system is magnetized () and a disordered or high temperature phase where the magnetization vanishes (). The magnetization distinguishes between the two phases and it is called the order parameter. The critical temperature is the Curie temperature. The phase transition is of second order, which is a special case of a continuous phase transition. For a continuous phase transition the order parameter is continuous at , but it is non analytic7 . For a second order phase transition, its derivative is not continuous. This is qualitatively depicted in figure 13.1.
For the correlation function (12.45) behaves like in equation (12.46) resulting in a finite correlation length . The correlation length8 diverges as we approach the critical temperature, and its asymptotic behavior in this limit is given by the scaling relation
| (13.6) |
Then the correlation function behaves according to (12.47)
| (13.7) |
Scaling behavior is also found for the specific heat , the magnetization and the magnetic susceptibility according to the relations
whereas the magnetization for and nonzero magnetic field behaves like
| (13.11) |
The exponents in the above scaling relations are called critical exponents or scaling exponents. They take universal values, i.e. they don’t depend on the details of the lattice construction or of the interaction. A whole class of such models with different microscopic definitions have the exact same long distance behavior9 ! The systems in the same universality class need to share the same symmetries and dimensionality of space and the fact that the interaction is of short range. In the particular model that we study, these exponents take the so called Onsager exponent values
| (13.12) |
Theses exponents determine the non analytic behavior of the corresponding functions in the thermodynamic limit. Non analyticity cannot arise in the finite model. The partition function (13.4) is a sum of a finite number of analytic terms, which of course result in an analytic function. The non analytic behavior manifests in the limit, where the finite analytic functions converge to a non analytic one. The loss of analyticity is related to the appearance of long distance correlations between the spins and the scaling of the correlation length according to equation (13.6) .
The two phases, separated by the phase transition, are identified by the different values of an order parameter. Each phase is characterized by the appearance or the breaking of a symmetry. In the Ising model, the order parameter is the magnetization and the symmetry is the symmetry represented by the transformation . The magnetization is zero in the disordered, high temperature phase and non zero in the ordered, low temperature phase. This implies that the magnetization is a non analytic function of the temperature10 .
Universality and scale invariance appear in the limit. In our case, this occurs by tuning only one parameter, the temperature, to its critical value. A unique, dynamical, length scale emerges from the correlation function, the correlation length . Scale invariance manifests when the correlation length becomes much larger than the microscopic length scale when . In the critical region, all quantities which are functions of the distance become functions only of the ratio . Everything depends on the long wavelength fluctuations required by the symmetry of the order parameter and all models in the same universality class have the same long distance behavior. This way one can study only the simplest model within a universality class in order to deduce the large distance/long wavelength properties of all systems in the class.
Consider a square lattice with sites on each side so that is the number of lattice sites (vertices) and is the number of links (bonds) between the sites. The relation holds because we choose helical boundary conditions as shown in figure 13.6. The choice of boundary conditions will be discussed later. On each site we have one degree of freedom, the “spin” which takes on two values . We consider the case of zero magnetic field , therefore the Hamiltonian is given by11
| (13.13) |
The sum is a sum over the links , corresponding to the pairs of sites . Then since each bond is counted twice in the second sum. The partition function is
| (13.14) |
Our goal is to collect a sample of states that is distributed according to the Boltzmann distribution (12.4) . This will be constructed via a Markov process according to the discussion in section 12.6. Sampling is made according to (12.53) and the expectation values are estimated from the sample using (12.54) . At each step the next state is chosen according to (12.60) , and for large enough sample, or “time steps”, the sample is approximately in the desired distribution.
Suppose that the system is in a state12 . According to (12.66) , the probability that in the next step the system goes into the state is
| (13.15) |
where is the selection probability of the state when the system is in the state and is the acceptance ratio, i.e. the probability that the system jumps into the new state. If the detailed balance condition (12.60)
| (13.16) |
is satisfied, then the distribution of the sample will converge to (12.4) . In order that the system changes states often enough, the probabilities should be of order one and the differences in the energy should not be too large. This means that the product of the temperature with the energy difference should be a number of order one or less. One way to accomplish this is to consider states that differ by the value of the spin on only one site . Since the energy (13.13) is a local quantity, the change in energy will be small. More specifically, if each site has nearest neighbors, the change of the spin on site results in a change of sign for terms in (13.13) . The change in the energy for each bond is . If the state is given by and the state by (i.e. all the spins are the same except the spin which changes sign), the energy difference will be
| (13.17) |
If the site is randomly chosen then
| (13.18) |
and the algorithm is ergodic. Then we have that
| (13.19) |
A simple choice for satisfying this condition is (12.61)
| (13.20) |
In order to maximize the acceptance ratios we have to take . Remember that we should have and . Therefore
| (13.21) |
Figure 13.4 depicts the dependence of on the change in energy for different values of . We observe that this probability is small even for zero energy change and we expect this method not to perform very well.
It is much more efficient to use the algorithm proposed by Nicolas Metropolis et. al. 1953 [59] which is given by (12.63)
| (13.22) |
According to this relation, when a change in the states lowers the energy, the change is always accepted. When it increases the energy, the change is accepted with a probability less than one. As we can see in figure 13.4, this process accepts new states much more frequently than the previous algorithm.
The Metropolis algorithm is very widely used. It is applicable to any system, it is simple and efficient. We note that the choice to change the spin only locally is not a restriction put by the metropolis algorithm. There exist efficient algorithms that make non local changes to the system’s configuration that (almost) conserve the Hamiltonian13 and, consequently, the acceptance ratios are satisfactorily large.
The first step in designing a code is to define the data structure. The degrees of freedom are the spins which are defined on lattice sites. The most important part in designing the data structure in a lattice simulation is to define the neighboring relations among the lattice sites in the computer memory and this includes the implementation of the boundary conditions. A bad choice of boundary conditions will make the effect of the boundary on the results to be large and increase the finite size effects. This will affect the speed of convergence of the results to the thermodynamic limit, which is our final goal. The most popular choice is the toroidal or periodic boundary conditions. A small variation of these lead to the so called helical boundary conditions, which will be our choice because of their simplicity. Both choices share the fact that each site has the same number of nearest neighbors, which give the same local geometry everywhere on the lattice and minimize finite size effects due to the boundary. In contrast, if we choose fixed or free boundary conditions on the sides of the square lattice, the boundary sites have a smaller number of nearest neighbors than the ones inside the lattice.
One choice for mapping the lattice sites into the computer memory is to use their coordinates , . Each spin is stored in an array s(L,L). For a site s(i,j) the four nearest neighbors are s(i1,j), s(i,j1). The periodic boundary conditions are easily implemented by adding to i,j each time they become less than one or greater than . This is shown in figures 13.5 and 13.30.
The elements of the array s(L,L) are stored linearly into the computer memory. The element s(i,j) is at a “distance” (j-1)*L+i array positions from s(1,1) and accessing its value involves an, invisible to the programmer, multiplication. Using helical boundary conditions this multiplication can be avoided. The positions of the lattice sites are now given by one number , as shown in figures 13.6 and 13.31. The spins are stored in memory in a one dimensional array s(N) and the calculation of the nearest neighbors of a site s(i) is easily done by taking the spins s(i1) and s(iL). The simplicity of the helical boundary conditions is based on the fact that, for the nearest neighbors of sites on the sides of the square, all we have to do is to make sure that the index i stays within the accepted range 1 i N. This is easily done by adding or subtracting when necessary. Therefore in a program that we want to calculate the four nearest neighbors nn of a site i, all we have to do is:
We will choose helical boundary conditions for their simplicity and efficiency in calculating nearest neighbors14 .
The dynamics of the Monte Carlo evolution is determined by the initial state and the Metropolis algorithm. A good choice of initial configuration can be important in some cases. It could lead to fast or slow thermalization, or even to no thermalization at all. In the model that we study it will not play an important role, but we will discuss it because of its importance in the study of other systems. We may choose a “cold” ( - all spins aligned) or a “hot” ( - all spins are equal to with equal probability ) initial configuration. For large lattices, it is desirable to start in one of these states and then lower/increase the temperature in small steps. Each time that the temperature is changed, the spin configuration is saved and used in the next simulation.
Ergodicity and thermalization must be checked by performing independent simulations15 and verify that we obtain the same results. Similarly, independent simulations starting from different initial states must also be checked that yield the same results.
Consider each step in the Markov process defined by the Metropolis algorithm. Assume that the system is in the state and consider the transition to a new state which differs only by the value of the spin (spin flip), whereas all the other spins are the same: . The energy difference between the two states is
where the second line is obtained after the cancellation of the common terms in the sums. In the third line we used the relation , which you can prove easily by examining the cases separately. The important property of this relation is that it is local since it depends only on the nearest neighbors. The calculation of the energy difference is fast and is always a number of order one16 .The Metropolis condition is easily implemented. We calculate the sum in the parenthesis of the last line of equation (13.23) and obtain the energy difference . If the energy decreases, i.e. , the new state is accepted and “we flip the spin”. If the energy increases, i.e. , then the acceptance ratio is . In order to accept the new state with this probability we pick a random number uniformly distributed in . The probability that this number is is equal to17 . Therefore if the change is accepted. If the change is rejected and the system remains in the same state .
A small technical remark is in order: The possible values of the sum and these are the only values that enter in the calculation of . Moreover, only the values that increase the energy, i.e. are of interest to us. Therefore we only need two values of , which depend only on the temperature. These can by calculated once and for all in the initialization phase of the program, stored in an array and avoid the repeated calculation of the exponential which is expensive.
In our program we also need to implement the calculation of the observables that we want to measure. These are the energy (13.13)
| (13.24) |
| (13.25) |
Beware of the absolute value in the last equation! The Hamiltonian has a symmetry because it is symmetric under reflection of all the spins. The probability of appearance of a state depends only on the value of , therefore two configurations with opposite spin are equally probable. But such configurations have opposite magnetization, therefore the average magnetization will be zero due to this cancellation18 .
We can measure the energy and the magnetization in two ways. The first one is by updating their values each time a Metropolis step is accepted. This is easy and cheap since the difference in the sum in equations (13.24) and (13.25) depends only on the value of the spin and its nearest neighbors. The energy difference is already calculated by (13.23) whereas the difference in the magnetization in (13.25) is given by
| (13.26) |
The second way is by calculating the full sums in (13.24) and (13.25) every time that we want to take a measurement. The optimal choice depends on how often one obtains a statistically independent measurement19 . If the average acceptance ratio is , then the calculation of the magnetization using the first method requires additions per Monte Carlo steps, whereas the second one requires additions per measurement.
We use the normalization
| (13.27) |
which gives the energy per link. We have that , where for the ground state in which all links have energy equal to . The magnetization per site is
| (13.28) |
We have that , where for (perfect disorder) and for the ground state at (perfect order). We call the order parameter since its value determines the phase that the system is in.
The specific heat is given by the fluctuations of the energy
| (13.29) |
and the magnetic susceptibility by the fluctuations of the magnetization
| (13.30) |
In order to estimate the amount of data necessary for an accurate measurement of these quantities, we consider the fact that for independent measurements the statistical error drops as . The problem of determining how often we have independent measurements is very important and it will be discussed in detail later in this chapter.
In this section we discuss the program20 that implements the Monte Carlo simulation of the Ising model. The code in this section can be found in the accompanying software of this chapter in the directory Ising_Introduction.
In the design of the code, we follow the philosophy of modular programming. Different independent sections of the program will be coded in different files. This makes easier the development, maintenance and correction of the code by one or a team of programmers. A header file contains the definitions which are common for the code in one or more files. Then, all the parameters and common blocks are in one place and they are easier to modify for all program units in a unified way, therefore avoiding errors. In our case we have only one such file, named include.inc, whose code will be included in the beginning of each program unit using an include statement:
The lattice size is a constant parameter, whereas the arrays and variables encoding the spins and the simulation parameters are put in common blocks. The array s(N) stores the spin of each lattice site which take values . The variable beta is the temperature and the array prob(0:4) stores the useful values of the acceptance ratios according to the discussion on page 1322. The function drandom() is the one discussed in section 11.1, which generates pseudorandom numbers uniformly distributed in the interval - and excluded. The parameters XNN and YNN are used for computing the nearest neighbors in the X and Y directions according to the discussion of section 13.3 on helical boundary conditions. For example, for an internal site i, i+XNN is the nearest neighbor in the direction and i-YNN is the nearest neighbor in the direction.
The main program is in the file main.f90 and drives the simulation:
In the beginning we set the simulation parameters. The initial configuration is determined by the value of start. If start=0, then it is a cold configuration and if start=1, then it is a hot configuration. The temperature is set by the value of beta and the number of sweeps of the lattice by the value of nsweep. One sweep of the lattice is defined by N attempted spin flips. The flow of the simulation is determined by the initial call to init, which performs all initialization tasks, and the subsequent calls to met and measure, which perform nsweep Metropolis sweeps and measurements respectively.
One level down lies the subroutine init. The value of start is passed through its argument so that the desired initial state is set:
At first the array prob(0:4) is initialized to the values of the acceptance ratios . Those probabilities are going to be used when and the possible values are obtained when this expression takes the values 2 and 4. These are the values stored in the array prob(0:4), and we remember that the index of the array is the expression , when it is positive.
The initial spin configuration is determined by the integer start. Using the select case block allows us to add more options in the future. When start=0 all spins are set equal to 1, whereas when start=1 each spin’s value is set to with equal probability. The probability that drandom()<0.5 is21 in which case we set s(i)=1, otherwise (probability ) we set s(i)=-1.
The heart of the program is the subroutine met() which attempts N Metropolis steps. It picks a random site N times and asks the question whether to perform a spin flip. This is done using the Metropolis algorithm by calculating the change in the energy of the system before and after the change of the spin value according to (13.23) :
The line
picks a site i=1,...,N with equal probability. It is important that the value i=N+1 never appears, something that happens if drandom()=1.0. This value has been excluded according to the discussion in section 11.1.
Next, we calculate the sum in (13.23) . The nearest neighbors of the site i have to be determined and this happens in the lines
The variable delta is set equal to the product (13.23) . If it turns out to be negative, then the change in energy is negative and the spin flip is accepted. If it turns out to be positive, then we apply the criterion set by (13.22) by using the array prob(delta), which has been set in the subroutine init. The probability that drandom()<prob(delta) is equal to prob(delta), in which case the spin flip is accepted. In all other cases, the spin flip is rejected and s(i) remains the same.
After each Metropolis sweep we perform a measurement. The code is minimal and simply prints the value of the energy and the magnetization to the stdout. The analysis is assumed to be performed by external programs. This way we keep the production code simple and store the raw data for a detailed and flexible analysis. The printed values of the energy and the magnetization will be used as monitors of the progress of the simulation, check thermalization and measure autocorrelation times. Plots of the measured values of an observable as a function of the Monte Carlo “time” are the so called “time histories”. Time histories of appropriately chosen observables should always be viewed and used in order to check the progress and spot possible problems in the simulation.
The subroutine measure calculates the total energy and magnetization (without the absolute value) by a call to the functions E() and M(), which apply the formulas (13.24) and (13.25) .
The compilation of the code is done with the command
which results in the executable file is:
The output of the program is two columns with the values of the total energy and magnetization (without the absolute value). In order to construct their time histories we give the gnuplot commands:
The last line calculates the absolute values of the second column. The C-like construct ($2>0)?$2:-$2 checks whether the expression ($2>0) is true. If it is, then it returns $2, otherwise it returns -$2.
In this section we will improve the code, mostly at the user interface level. This is a nice exercise on the interaction of the programming language with the shell and the operating system. The code presented can be found in the accompanying software of this chapter in the directory Ising_Metropolis.
An annoying feature of the program discussed in the previous section is that the simulation parameters are hard coded and the user needs to recompile the program each time she changes them. This is not very convenient if she has to do a large number of simulations. Another notable change that needs to be made in the code is that the final configuration of the simulation must be saved in a file, in order to be read as an initial configuration by another simulation.
One of the parameters that the user might want to set interactively at run time is the size of the lattice L. But this parameter determines the required memory for the array s(N). Therefore we have to use dynamic memory allocation for this array using the intrinsic function ALLOCATE. Another problem is that the array s(N) needs to be accessible by several parts of the program and allocatable arrays cannot be put in a common block. Another mechanism for sharing data among different functions and subroutines is the use of modules. This is the preferable method of doing it in modern Fortran programs where the use of common blocks is discouraged. The shared data needs to be put between the following statements:
In place of the ... we can put variable declarations. We use the statement SAVE so that their values are saved between function and subroutine calls. The module has a name which in our case is global_data. Each program unit that needs to have access to its data needs to start with the statement use global_data:
In the file global_data.f90 we put all the global variables as follows:
The array s(:) is allocatable and its storage space will be allocated in the subroutine init. The variables L, N, XNN and YNN are not parameters anymore and their values will also be set in init. The new variables are acceptance which computes the fraction of accepted spin flips in a simulation, ranlux_level which determines the luxury level of RANLUX and prog which stores the command line name of the program that runs the simulation.
The main program has very few changes:
Notice the line use global_data which gives access to the data in the module global_data. This is the first line of all program units. The subroutine endsim finishes off the simulation. Its most important function is to store the final configuration to a file for later use.
The subroutine init is changed quite a bit since it performs most of the functions that have to do with the user interface:
In the beginning, the simulation parameters that are to be determined by the user are given invalid default values. This way they are flagged as not been set. The subroutine22 get_the_options sets the parameters to the values that the user passes through the command line:
Upon return of get_the_options, one has to check if all the parameters have been set to acceptable values. For example, if the user has forgotten to set the lattice size L, the call to the subroutine locerr stops the program and prints the error message passed through its argument:
When the value of N is calculated from L, the program allocates memory for the array s(N):
When memory allocation is successful, the variable chk is set to 0 by ALLOCATE. Otherwise we stop the program with a call to locerr.
Using the construct SELECT CASE(start) we set the initial configuration of the simulation. A value of start=0 sets all spins equal to 1. The subroutine simmmessage(f_unit) prints important information about the simulation to the unit f_unit. The random number generator RANLUX is initialized with a call to RLUXGO according to the discussion in section 11.2, page 1202. The global variable ranlux_level is set to 3 by default, but the user can change it from the command line (see get_the_options). If start=0 the initial configuration is hot.
If start=2 we attempt to read a configuration stored in a file named conf. The format of the file is strictly set by the way we print the configuration in the subroutine endsim. If the file does not exist, the argument ERR=101 transfers the control of the program to the labeled statement with label 101. This is near the end of the program and stops the program with a call to locerr. In order to read the configuration properly we need to know the format of the data in the file conf which is, more or less, as follows:
All comments of the first line are discarded in the character variable buf. The parameters L and beta of the stored configuration are stored in temporary variables OL, obeta, so that they can be compared with the values set by the user.
If the user provides a seed, then her seed will be used for seeding. Otherwise RANLUX is initialized to the state read from the file conf. Both choices are desirable in different cases: If the user wants to split a long simulation into several short runs, then each time she wants to restart the random number generator at exactly the same state. If she wants to use the same configuration in order to produce many independent results, then RANLUX has to produce different sequences of random numbers each time23 . This feature is coded in the lines:
When reading the spins, we have to make sure that they take only the legal values and that the data is enough to fill the array s(N)24 . Reading enough data is checked by the READ argument END=102. If the READ statement attempts to read past the end of the file conf, then the control of the program is transferred to the labeled statement with label 102. This will happen, e.g. if we attempt to read from a corrupted file.
The subroutine endsim saves the last configuration in the file conf and can be found in the file end.f90:
The state of the random number generator RANLUX is saved by a call to RLUXUT which stores the necessary information in the array seeds. The call to the subroutine RENAME renames the file conf (if it exists) to the backup file conf.old. The format (A4, I5, A4, I5, A6, G28.17, A6, 25I16) has to be obeyed strictly during the output, as well as during the input of the configuration in the subroutine init.
The subroutine get_the_options() reads the parameters, passed through options from the command line. The choice to use options for passing parameters to the program has the advantage that they can be passed optionally and in any order desired. Let’s see how they work. Assume that the executable file is named is. The command
will run the program after setting L=10 (-L 10), beta=0.44 (-b 0.44), start=1 (-s 1), seed=5342 (-S 5342) and nsweep=1000 (-n 1000). The -L, -b, -s, -S, -n are options or switches and can be put in any order in the arguments of the command line. The arguments following an option are the values passed to the corresponding variables. Options can also be used without arguments, in which case a common use is to make the command function differently25 . In our case, the option -h is an option without an argument which makes the program to print a usage message and exit without running the simulation:
This is a way to provide a short documentation on the usage of a program.
Let’s see the code, which is found in the file options.f90:
The command call getarg(0,prog) stores the name of the program in the command line to the character variable prog. The function getopt is a function written by Mark Gates and its code is in the file getopt.f90. It is programmed so that its usage is similar to the corresponding C function26 . The argument "-hL:b:s:S:n:r:u" in getopt defines the allowed options ’-’, ’L’, ’b’, ’s’, ’S’, ’n’, ’r’, ’u’. When a user passes one of those through the command line (e.g. -L 100, -h) the do loop takes us to the corresponding CASE. If an option does not take an argument (e.g. -h), then a set of commands can be executed, like call usage. If an option takes an argument, this is marked by a semicolon in the argument of getopt (e.g. L:, b:, ...) and the argument can be accessed through the character variable optarg. For example, the statements
and the command line arguments -L 10 set optarg to be equal to ’10’. Be careful, ’10’ is not a number, but a string of characters! In order to convert the character ’10’ to the integer 10 we use the command READ, where instead of a unit number in its arguments we put the variable name. We do the same for the other simulation parameters.
The subroutine locerr takes a character variable in its argument which prints it to the stderr together with the name of the program in the command line. Then it stops the execution of the program:
Note the use of the intrinsic function TRIM which removes the trailing blanks of a character variable. If we hadn’t been using it, the variable character(1024) :: prog would have been printed in 1024 character spaces, something that it wouldn’t have been very pretty...
The subroutine usage is ... used very often! It is a constant reminder of the way that the program is used and helps users with weak long and/or short term memory!
The subroutine simmessage is also quite important. It “labels” our results by printing all the information that defines the simulation. It is very important to label all of our data with this information, otherwise it can be dangerously useless! Imagine a set of energy measurements without knowing the lattice size and/or the temperature... Other useful information may turn out to be crucial, even though we might not appreciate it at programming time: The name of the computer, the operating system, the user name, the date etc. By varying the unit number in the argument, we can print the same information in any file we want.
The compilation can be done with the command:
It is important to note that the files containing modules, like global_data.f90 and getopt.f90, must precede the files with the code that use the modules.
In order to run the program we pass the parameters through options in the command line, like for example:
The command time is added in order to measure the computer resources (CPU time, memory, etc) that the program uses at run time.
A useful tool for complicated compilations is the utility make. Its documentation is several hundred pages which can be accessed through the info pages27 and the interested reader is encouraged to browse through it. If in the current directory there is a file named Makefile whose contents28 are
then this instructs the program make how to “make” the executable file is. What have we gained? In order to see that, run make for the first time. Then try making a trivial change in the file main.f90 and rerun make. Then only the modified file is compiled and not the ones that have not been touched. This is accomplished by defining dependencies in Makefile which execute commands conditionally depending on the time stamps on the relevant files. Dependencies are defined in lines which are of the form keyword: word1 word2 .... For example, the line options.o: getopt.f90 defines a dependency of the file options.o from the file getopt.f90. Lines 2-4 in the above Makefile define variables which can be used in the commands that follow. There are many predefined variables29 in make which makes make programming easier. By using make in a large project, we can automatically link to libraries, pass complicated compiler options, do conditional compilation (depending, e.g., on the operating system, the compiler used etc), etc. A serious programmer needs to invest some time in order to use the full potential of make for the needs of her project30 .
The problem of thermalization can be important for some systems studied with Monte Carlo simulations. Even though it will not be so important in the simulations performed in this book, we will discuss it because of its importance in other problems. The reader should bear in mind that the thermalization problem becomes more serious with increasing system size and when autocorrelation times are large.
In a Monte Carlo simulation, the system is first put in a properly chosen initial configuration in order to start the Markov process. In section 12.2 we saw that when a system is in thermal equilibrium with a reservoir at a given temperature, then a typical state has energy that differs very little from its average value and belongs to a quite restricted region of phase space. Therefore, if we choose an initial state that is far from this region, then the system has to perform a random walk in the space of states until it finds the region of typical states. This is the thermalization process in a Monte Carlo simulation.
There are two problems that need to be addressed: The first one is the appropriate choice of the initial configuration and the second one is to find criteria that will determine when the system is thermalized. For the Ising model the initial configuration is either, (a) cold, (b) hot or (c) old state. It is obvious that choosing a hot state in order to simulate the system at a cool temperature is not the best choice, and the system will take longer to thermalize than if we choose a cold state or an old state at a nearby temperature. This is clearly seen in figure 13.7.
Thermalization depends on the temperature and the system size, but it also depends on the physical quantity that we measure. Energy is thermalized faster than magnetization. In general, a local quantity thermalizes fast and a non local one slower. For the Ising model, thermalization is easier far from the critical temperature, provided that we choose an initial configuration in the same phase. It is easier to thermalize a small system rather than a large one.
The second problem is to determine when the system becomes thermalized and discard all measurements before that. One way is to start simulations using different initial states, or by keeping the same initial state and using a different sequence of random numbers. When the times histories of the monitored quantities converge, we are confident that the system has been thermalized. Figure 13.7 shows that the thermalization time can vary quite a lot.
A more systematic way is to compute an expectation value by removing an increasing number of initial measurements. When the results converge within the statistical error, then the physical quantity that we measure has thermalized.
This process is shown in figures 13.10 and 13.11 where we progressively drop and initial measurements until the expectation value of the magnetization stabilizes within the limits of its statistical error.
In order to construct a set of independent measurements using a Markov process, the states put in the sample should be statistically uncorrelated. But for a process using the Metropolis algorithm this is not possible. The next state differs from the previous one by at most one value of their spins. We would expect that we could obtain an almost statistically independent configuration after one spin update per site, a so called sweep of the lattice. This is indeed the case for the Ising model for temperatures far from the critical region. But as one approaches , correlations between configurations obtained after a few sweeps remain strong. It is easy to understand why this is happening. As the correlation length (12.46) becomes much larger than a few lattice spacings, large clusters of same spins are formed, as can be seen in figure 13.32. For two statistically independent configurations, the size, shape and position of those clusters should be quite different. For a single flip algorithm, like the Metropolis algorithm, this process takes a lot of time31 .
For the quantitative study of autocorrelations between configurations we use the autocorrelation function. Consider a physical quantity (e.g. energy, magnetization, etc) and let be its value after Monte Carlo “time” . can be measured in sweeps or multiples of it. The autocorrelation function of is
| (13.31) |
where is the average value over the configurations in the sample for . The normalization is such that .
The above definition reminds us the correlation function of spins in space (see equation (12.45) ) and the discussion about its properties is similar to the one of section 12.4. In a few words, when the value of after time is strongly correlated to the one at , then the product in the numerator in (13.31) will be positive most of the time and the value of will be positive. When the correlation is weak, the product will be positive and negative the same number of times and will be almost zero. In the case of anti-correlations is negative. Negative values of occur, but these are artifacts of the finite size of the sample and should be rejected.
Asymptotically drops exponentially
| (13.32) |
is the time scale of decorrelation of the measurements of and it is called the autocorrelation time of . After time , has dropped to the of its initial value and then we say that we have an independent measurement of32 . Therefore, if we have measurements, the number of independent measurements of is
| (13.33) |
For expensive measurements we should measure every sweeps. If the cost of measurement is not significant, then we usually measure more often, since there is still statistical information even in slightly correlated configurations. An accurate determination of is not easy since it requires measuring for .
An example is shown in figure 13.12 for the case of the magnetization (). We calculate the function and we see that a fit to equation (13.32) is quite good for sweeps. The calculation is performed on a sample of measurements with 1 measurement/sweep. Therefore the number of independent measurements is .
Another estimator of the autocorrelation time is the so called integrated autocorrelation time . Its definition stems from equation (13.32) where we take
| (13.34) |
The values of and differ slightly due to systematic errors that come from the corrections33 to equation (13.32) . The upper limit of the integral is cut off by a maximum value
| (13.35) |
For large enough we observe a plateau in the plot of the value of which indicates convergence, and we take this as the estimator of . For even larger , finite sample effects enter in the sum that should be discarded.
This calculation is shown in figure 13.14 where we used the same measurements as the ones in figure 13.12. We find that sweeps, which is somewhat smaller than the autocorrelation time that we calculated using the exponential fit to the autocorrelation function. If we are interested in the scaling properties of the autocorrelation time with the size of the system or the temperature , then this difference is not important34 . The calculation of is quicker since it involves no fitting35 .
Autocorrelation times are not a serious problem away from the critical region. Figures 13.15 and 13.16 show that they are no longer than a few sweeps and that they are independent of the system size . As we approach the critical region, autocorrelation times increase. At the critical region we observe scaling of their values with the system size, which means that for large we have that
| (13.36) |
This is the phenomenon of critical slowing down. For the Metropolis algorithm and the autocorrelation time of the magnetization, we have that [60]. This is a large value and that makes the algorithm expensive for the study of the critical properties of the Ising model. It means that the simulation time necessary for obtaining a given number of independent configurations increases as
| (13.37) |
In the next chapter, we will discuss the scaling relation (13.36) in more detail and present new algorithms that reduce critical slowing down drastically.
The estimate of the expectation value of an observable from its average value in a sample gives no information about the quality of the measurement. The complete information is provided by the full distribution, but in practice we are usually content with the determination of the “statistical error” of the measurement. This is defined using the assumption that the distribution of the measurements is Gaussian, which is a very good approximation if the measurements are independent. The statistical error is determined by the fluctuations of the values of the observable in the sample around its average (see discussion in section 12.2 and in particular equation (12.27) ). Statistical errors can be made to vanish, because they decrease as the inverse square root of the size of the sample.
Besides statistical errors, one has systematic errors, which are harder to control. Some of them are easier to control (like e.g. poor thermalization) and others maybe hard even to realize their effect (like e.g. a subtle problem in a random number generator). In the case of a discrete, finite, lattice, approximating a continuous theory, there are systematic errors due to the discretization and the finite size of the system. These errors are reduced by simulating larger systems and by using several techniques (e.g. finite size scaling) in order to extrapolate the results to the thermodynamic limit. These will be studied in detail in the following chapter.
Using the assumption that the source of statistical errors are the thermal fluctuations around the average value of an observable, we conclude that its expectation value can be estimated by the mean of the sample and its error by the error of the mean. Therefore if we have a sample of measurements , their mean is an estimator of
| (13.38) |
The error of the mean is an estimator of the statistical error
| (13.39) |
The above equations assume that the sample is a set of statistically independent measurements. This is not true in a Monte Carlo simulation due to the presence of autocorrelations. If the autocorrelation time, measured in number of measurements, is , then according to equation 13.33 we will have independent measurements. One can show that in this case, the statistical error in the measurement of is36 [61]
| (13.40) |
If , then we obtain equation (13.39) . If
which is nothing but equation (13.39) for independent measurements (we assumed that ). The above relation is consistent with our assumption that measurements become independent after time .In some cases, the straightforward application of equations (13.41) is not convenient. This happens when, measuring the autocorrelation time according to the discussion in section 13.5, becomes laborious and time consuming. Moreover, one has to compute the errors of observables that are functions of correlated quantities, like in the case of the magnetic susceptibility (13.30) . The calculation requires the knowledge of quantities that are not defined on one spin configuration, like and (or on each configuration ). After these are calculated on the sample, the error is not a simple function of and . This is because of the correlation between the two quantities and the well known formula of error propagation cannot be applied.
The simplest solution to the problems arising in the calculation of statistical errors discussed in the previous section is to divide a sample into blocks or bins. If one has measurements, she can put them in “bins” and each bin is to be taken as an independent measurement. This will be true if the number of measurements per bin . If is average value of in the bin , then the error is given by (13.39)
| (13.42) |
This is the binning or blocking method and it is quite simple in its use. Note that quantities, like the magnetic susceptibilities, are calculated in each bin as if the bin were an independent sample. Then the error is easily calculated by equation (13.42) . If the bin is too small and the samples are not independent, then the error is underestimated by a factor of (see equation (13.40) ). The bins are statistically independent if . If is not a priori known we compute the error (13.42) by decreasing the number of bins . When the error is not increasing anymore and takes on a constant value, then the calculation converges to the true statistical error.
But the method of choice in this book is the jackknife method. It is more stable and more reliable, especially if the sample is small. The basic idea is similar to the binning method. The difference is that the bins are constructed in a different way and equation (13.42) is slightly modified. The data is split in bins which contain elements as follows: The bin contains the part of the sample obtained after we we erase the contents of the -th bin of the binning method from the full sample . The procedure is depicted in figure 13.21.
We calculate the average value of in each bin and we obtain . Then the statistical error in the measurement of is
| (13.43) |
In order to determine the error, one has to vary the number of bins and check for the convergence of (13.43) , like in the case of the binning method.
For more details and proofs of the above statements, the reader is referred to the book of Berg [5]. Appendix 13.8.1 provides examples and a program for the calculation of jackknife errors.
Another useful method for the estimation of statistical errors is the bootstrap method. Suppose that we have independent measurements. From these we create random samples as follows: We choose one of the measurements with equal probability. We repeat times using the same set of measurements - i.e. by putting the chosen measurements back to the sample. This means that on the average of the sample will consist of the same measurements. In each sample we calculate the average values and from those
| (13.44) |
and
| (13.45) |
The estimate for the error in is37
| (13.46) |
We stress that the above formula gives the error for independent measurements. If we have non negligible autocorrelation times, then we must use the correction
| (13.47) |
Appendix 13.8.2 discusses how to use the bootstrap method in order to calculate the true error without an a priori knowledge of . For more details, the reader is referred to the articles of Bradley Efron [62]. In appendix 13.8.2 you will find examples and a program that implements the bootstrap method.
This appendix discusses the technical details of the calculation of the autocorrelation function (13.31) and the autocorrelation time given by equations (13.32) and (13.34) . The programs can be found in the directory Tools in the accompanying software.
If we have a finite sample of measurements , then we can use the following estimator for the autocorrelation function, given by equation (13.31) ,
| (13.48) |
where the average values are computed from the equations38
| (13.49) |
The constant is chosen so that .
The program for the calculation of (13.48) and the autocorrelation time (13.34) is listed below. It is in the file autoc.f90 and you should read the comments embedded in the code for explanations of the most important steps.
The calculation of the autocorrelation function is put in a separate module rho_function which can be used by any of your programs. After the statement CONTAINS we can add code for functions and subroutines which can be accessed39 by any program unit that uses the module. The module makes global variables, like NMAX, tmax and prog, accessible to all program units that use the module.
The compilation is done with the commands
If our data is written in a file named data in one column, then the calculation of the autocorrelation function and the autocorrelation time is done with the command
The results are written to the file data.rho in three columns. The first one is the time , the second one is and the third one is (equation (13.35) ). The corresponding plots are constructed by the gnuplot commands:
If we wish to increase the maximum number of data NMAX or the maximum time tmax, then we use the options -n and -t respectively:
For doing all the work at once using gnuplot, we can give the command:
The above command is long and it is broken into 3 lines for better printing. You can type it in one line by removing the trailing ∖.
A script that works out many calculations together is listed below. It is in the file autoc_L and computes the data shown in figure 13.19.
Then we give the gnuplot commands:
The plots in figure 13.17 are constructed in a similar way.
For the calculation of we do the following:
where in the last line we compute . The fit command is just an example and one should try different fitting ranges. The first plot command shows graphically the approximate range of the exponential falloff of the autocorrelation function. We should vary the upper and lower limits of the fitting range until the value of stabilizes and the40 is minimized41 . The of the fit can be read off from the output of the command fit
from the line “variance of residuals”. From the next lines we read the values of the fitted parameters with their errors42 and we conclude that . We stress that this is the statistical error of the fit for the given fitting range. But usually the largest contributions to the error come from systematic errors, which, in our case, are seen by varying the fitting range43 . By trying different fitting ranges and using the criterion that the minimum doubles its minimum value, we find that .
In our case the largest systematic error comes from neglecting the effect of smaller autocorrelation times. These make non negligible contributions for small .
By fitting to
| (13.50) |
we have taken into account only the largest autocorrelation time.
One should take into account also the smaller autocorrelation times. In this case we expect that . We find that the data for the autocorrelation function fit perfectly to the function
| (13.51) |
As we can see in figures 13.22 and 13.23, the small fit is excellent and the result for the dominant autocorrelation time is . The secondary autocorrelation times are , which are considerably smaller that .
The commands for the analysis are listed below:
In this section we present a program that calculates the errors using the jackknife method discussed in section 13.6.2. Figure 13.21 shows the division of the data into bins. For each bin we calculate the average value of the quantity and then we use equation (13.43) in order to calculate the error. The program is in the file jack.f90 which you can find in the directory Tools in the accompanying software. The program calculates , , and .
For the compilation we use the command
If we assume that our data is in one column in the file data, the command that calculates the jackknife errors using 50 bins is:
The program has set a maximum of MAXDAT=1,000,000 measurements. If we need to analyze more data, we have to use the switch -d. For example, for 2,000,000 measurements, use -d 2000000. The program reads data from the stdin and we can construct filters in order to do complicated analysis tasks. For example, the analysis of the magnetization produced by the output of the Ising model program can be done with the command:
The command shown above can be written in one line by removing the backslashes (’∖’) at the end of each line. Let’s explain it in detail: The first line runs the program is for the Ising model with lattice sites (-L 20) and (-b 0.4407). It starts the simulation from a hot configuration (-s 1) and makes 2,000,000 measurements (-n 2000000). The command grep -v filters out the comments from the output of the program, which are lines starting with a #. The second line calls awk and defines the awk variable L to be equal to 20 (-v L=20). For each line in its input, it prints the absolute value of the second column ($2) divided by the number of lattice sites L*L. The third line makes the jackknife calculation of the average values of and with their errors using the program jack. The comments of the output of the command jack are removed with the command grep -v. The fourth line is needed only for the calculation of the magnetic susceptibility, using equation (13.30) . There, we need to multiply the fluctuations and their error by the factor in order to obtain .
In this subsection we present a program for the calculation of the errors using the bootstrap method according to the discussion in section 13.6.3. The program is in the file boot.f90:
For the compilation we use the command
If our data is in one column in the file data, then the command that calculates the errors using 500 samples is:
The maximum number of measurements is set to 1,000,000 as in the jack program. For more measurements we should use the -d switch, e.g. for 2,000,000 measurements use -d 2000000. For the analysis of the magnetization from the output of the program is we can use the following command:
In this subsection we will compute errors using equation (13.40) , the jackknife method (13.43) and the bootstrap method (13.47) . In order to appreciate the differences, we will use data with large autocorrelation times. We use the Metropolis algorithm on the Ising model with , and measure the magnetization per site (13.28) . We take measurements using the commands:
The file outL40b0.4407.m has the measurements of the magnetization in one column and the file outL40b0.4407.rhom has the autocorrelation function and the integrated autocorrelation time as described after page 1429. We obtain . The integrated autocorrelation time is found to be .
The expectation value is . The application of equation (13.39) , valid for independent measurements, gives the (underestimated) error . Using equation (13.40) we obtain . The error of the magnetic susceptibility cannot be calculated this way.
| (13.52) |
For the calculation of the error of the magnetic susceptibility we have to resort to the jackknife or to the bootstrap method. The latter is applied initially using a variable number of samples so that the optimal number of samples is be determined. Figure 13.24 shows the results for the magnetization. We observe a very fast convergence to for quite small number of samples. The analysis could have safely used . In the case of the magnetic susceptibility, convergence is slower, but we can still use . We obtain and . The error assumes independent measurements, something that is not true in our case. We should use the correction factor which gives . Therefore
| (13.53) |
We note that the error is quite large, which is because we have few independent measurements: . The a priori knowledge of is necessary in this calculation.
In the case of the jackknife method, the calculation can proceed without an priori knowledge of . The errors are calculated for a variable number of bins . Figure 13.26 shows the results for the magnetization. When the samples consist of all the measurements except one. Then the error is equal to the error calculated using the standard deviation formula and it is underestimated by the factor . This is shown in figure 13.26, where we observe a slow convergence to the value . The effect of the autocorrelations vanishes when we delete measurements from each bin. This happens when . Of course this an order of magnitude estimate and a careful study is necessary in order to determine the correct value for . Figure 13.26 shows that the error converges for to the value , which is quite close to the value . We note that, by using a small number , we obtain an acceptable estimate, a rule of the thumb that can be used for quick calculations.
Similar results are obtained for the magnetic susceptibility , where the error converges to the value , in accordance with the previous estimates. For the error converges to the underestimated error .
We can use the bootstrap method, in a similar way to the jackknife method, in order to determine the real error , without calculating directly. The data is split into bins, whose bin width is . Each jackknife bin contains data elements and we apply the bootstrap method on this data, by taking samples of random data. Then each jackknife bin gives a measurement on which we apply equation (13.43) in order to calculate errors.
The above calculations can be reversed and used for the calculation of the autocorrelation time. By computing the underestimated error and the true error using one of the methods described above, we can calculate using the relation . Therefore
| (13.54) |
By calculating using all the methods described here, these relations can also be used in order to check the analysis for self-consistency and see if they agree. This is not always a trivial work since a system may have many autocorrelation times which influence each observable in a different way (fast modes, slow modes).
Make the plot . What do you observe?
For systems near a first order phase transition, the order parameter can take two different values with almost equal probability. This means that the free energy has two local minima. Only one of them is the true, global minimum. This is depicted in figure 12.2 where two equally probable values of the order parameter are shown. This happens exactly at the critical point. When we move away from the critical point, one of the peaks grows and it is favored corresponding to the global minimum of the free energy. The local minimum is called a metastable state and when the system is in such a state, it takes a long time until a thermal fluctuation makes it overcome the free energy barrier and find the global minimum. In a Monte Carlo simulation such a case presents a great difficulty in sampling states correctly near the two local minima. Repeat the above simulations, this time making sweeps per point. Plot the time series of the magnetization and observe the transitions from the metastable state to the stable one and backwards. Compute the histogram of the values of the magnetization and determine which state is the metastable in each case. How is the histogram changing as is increased?
Define the staggered magnetization to be the magnetization per site of the sublattice consisting of sites with odd and coordinate. Set and compute the energy, the , the specific heat, the magnetic susceptibility and the staggered magnetic susceptibility .
has a maximum in the region . Compute its value at this temperature for . Show that does not diverge as , therefore does not show a phase transition.
Repeat the calculation for . What do you conclude? Compare the behavior of for the antiferromagnetic Ising model with of the ferromagnetic.
| (14.1) |
the correlation length , calculated in the thermodynamic limit diverges according to the relation
| (14.2) |
The behavior of such systems near the phase transition is characterized by critical exponents, such as the exponent , which are the same for all systems in the same universality class. The critical exponents describe the leading non analytic behavior of the observables in the thermodynamic limit2 , when . Systems with the same long distance behavior, but which could possibly differ microscopically, belong to the same universality class. For example, if we add a next to nearest neighbor interaction in the Hamiltonian of the Ising model or if we consider the system on a triangular instead of a square lattice, the system will still belong to the same universality class. As these details become irrelevant and all these systems have the same long distance behavior. Microscopic degrees of freedom of systems in the same universality class can be quite different, as is the case of the liquid/vapor phase transition at the triple point and the Ising model.
The critical exponents of the 2d Ising model universality class are the Onsager exponents:
| (14.3) |
| (14.4) |
| (14.5) |
This behavior is seen only in the thermodynamic limit . For a finite lattice, all observables are analytic since they are calculated from the analytic3 partition function given by equation (13.4) . When the model behaves approximately as the infinite system. As and finite size effects dominate. Then the fluctuations, e.g. and , on the finite lattice have a maximum for a pseudocritical temperature for which we have that4
| (14.6) |
For the Ising model on the square lattice, defined by (13.14) , we have that .
Because of (14.2) , when on the finite lattice we take , we have that , therefore equations (14.3) – (14.5) become
| (14.7) |
| (14.8) |
| (14.9) |
The left hand sides of the above relations are normally evaluated at , but they can also be evaluated at any temperature in the pseudocritical region. Most of the times, one calculates the observables for , but one can also use e.g. 5. In the next sections we will show how to calculate the critical exponents by using the scaling relations (14.3) – (14.5) and (14.7) – (14.9) .
The computation of critical exponents is quite involved and requires accurate measurements, as well as simulations of large systems in order to reduce finite size effects. The Metropolis algorithm suffers from severe critical slowing down, i.e. diverging autocorrelation times with large dynamic exponent according to (13.36) , near the critical region, which makes it impossible to study large systems. In this section we will discuss the cause of this effect whose understanding will lead us to new algorithms that beat critical slowing down. These are the cluster algorithms and, in particular, the Wolff algorithm. The success of these algorithms is based on the dynamics of the system and, therefore, they have a more specialized range of applications. In contrast, the Metropolis algorithm can, in principle, be applied on any system studied with the Monte Carlo method.
According to the discussion in section 13.5, the Ising model simulation using the Metropolis algorithm near the critical region exhibits an increase in autocorrelation times given by the scaling relation (13.36)
| (14.10) |
The correlation length of the finite system becomes in this region, and we obtain equation (13.36) , . When we have the effect of critical slowing down.
Critical slowing down is the main reason that prohibits the simulation of very large systems, at least as far as CPU time is concerned6 . The generation of a given number of configuration requires an effort . But the measurement of a local quantity, like , for a given number of times requires no extra cost, since each configuration yields measurements7 . In this case, measuring for the largest possible is preferable, since it reduces finite size effects. We see that, in the absence of critical slowing down, the cost of measurement of is .
Critical slowing down, however, adds to the cost of production of independent configurations and we obtain , making the large simulations prohibitively expensive. For the Metropolis algorithm on the two dimensional Ising model we have that ; and the problem is severe. Therefore, it is important to invent new algorithms that beat critical slowing down. In the case of the Ising model and similar spin systems, the solution is relatively easy. It is special to the specific dynamics of spin systems and does not have a universal application.
The reason for the appearance of critical slowing down is the divergence of the correlation length . As we approach the critical temperature from the disordered phase, the typical configurations are dominated by large clusters of same spins. The Metropolis algorithm makes at most one spin flip per step and the acceptance ratios for spins inside a cluster are small. For example, a spin with four same neighboring spins can flip with probability , which is quite small. The spins that change more often are the ones with more neighbors having opposite spins, therefore the largest activity is observed at the boundaries of the large clusters. In order to obtain a statistically independent configuration, we need to destroy and create many clusters, something that happens very slowly using the Metropolis algorithm who realizes this process mostly by moving the boundaries of the clusters.
Beating critical slowing down requires new algorithms so that at each step a spin configuration is changed at the scale of a spin cluster8 . The cluster algorithms construct such regions of same spins in a way that the proposed new configuration has all the spins of the clusters flipped. For such an algorithm to be successful, the acceptance ratios should be large. The most famous ones are the Swendsen-Wang [63] and the Wolff [64] cluster algorithms.
The process of constructing the clusters is stochastic and depends on the temperature. Small clusters should be favored for , whereas large clusters of size should dominate for .
The basic idea of the Wolff algorithm is to choose a site randomly, a so called seed of the cluster, and construct a spin cluster around it. At each step, we add new members to the cluster with probability . If is properly chosen, the detailed balance condition (12.59) is satisfied and the new configuration is always accepted. This process is depicted in figure 14.1. In the state , the cluster is enclosed by the dashed line. The new state is obtained by flipping all the spins in the cluster, leaving the rest of the spins to be the same.
The correct choice of will yield equation (12.60)
| (14.11) |
The discussion that follows proves (14.11) and can be found in the book by Newman and Barkema [4]. The crucial observation is that the change in energy in the exponent of the right hand side of (14.11) is due to the creation/destruction of bonds on the boundary of the cluster. The structure of the bonds in the interior of the cluster is identical in the two configurations and . This can be seen in the simple example of figure 14.1. By properly choosing the selection probability of the new state and the acceptance ratio , so that
| (14.12) |
we will succeed in satisfying (14.11) and maximize the acceptance ratio. In fact in our case we will find that !
The selection probability is the probability of constructing a particular cluster and can be split in three factors:
| (14.13) |
The first term is the probability to start the cluster from the particular seed. By choosing a lattice site with equal probability we obtain
| (14.14) |
Then the cluster starts growing around its seed. The second term is the probability to include all cluster members found in the interior of the cluster. This probability is complicated and depends on the size and shape of the cluster. Fortunately, it is not important to calculate it. The reason is that in the opposite transition , the corresponding term is exactly the same since the two clusters are exactly the same (the only differ by the value of the spin)!
| (14.15) |
The third term is the most interesting one. The cluster stops growing when we are on the boundary and say “no” to including all nearest neighbors with same spins, which are not already in the cluster (obviously, the opposite spins are not included). If is the probability to include a nearest neighbor of same spin to the cluster, the probability of saying “no” is . Assume that we have “bonds”9 of same spins on the boundary of the cluster in the state , and that we have such bonds in the state . In figure 14.1, for example, we have that and . Therefore, the probability to stop the cluster in the state is to say “no” times, which happens with probability :
| (14.16) |
Similarly, the cluster in the state stops at the same boundary with probability
| (14.17) |
Therefore
| (14.18) |
The right hand side of the above equation depends only on the number of bonds on the boundary of the cluster. The energy difference depends only on the creation/destruction of bonds on the boundary of the cluster and the internal bonds don’t make any contribution to it. Each bond created during the transition decreases the energy by 2 and each bond destroyed increases the energy by 2:
| (14.19) |
which yields
| (14.20) |
From the above relation we see that if we choose
| (14.21) |
then we can also choose
| (14.22) |
Therefore, we can make the condition (14.11) to hold by constructing a cluster using the given by (14.21) , flipping its spins, and always accepting the resulting configuration as the new state.
Summarizing, the algorithm for the construction of a Wolff cluster consists of the following steps:
The algorithm is ergodic, since every state can be obtained from any other state by constructing a series of clusters of size 1 (equivalent to single flips).
The probability depends on the temperature . It is quite small for and almost 1 for . Therefore, in the first case the algorithm favors very small clusters (they are of size 1 for ) and in the second case it favors large clusters. In the high temperature regime, we have almost random spin flips, like in the Metropolis algorithm. In the low temperature regime, we have large probability of flipping the dominant cluster of the lattice. This is clearly seen in figure 14.2, where the fraction of the average cluster size to the lattice size is plotted as a function of the temperature. For small , whereas for large , .
Figure 14.3 shows typical spin configurations in the high and low temperature regimes. For small , most of the time the algorithm chooses a lattice site randomly and constructs a small cluster around it and flips its spins. The Metropolis algorithm picks a lattice site randomly and flips it most of the times. In both cases, the two algorithms function almost the same way and construct the high temperature disordered spin configurations. For large , a typical spin configuration is a “frozen” one: A large cluster of same spins with a few isolated thermal fluctuations of different spins. Most of the times, the Wolff algorithm picks a seed in the dominant cluster and the new cluster is almost the same as the dominant cluster: Most of its sites are included with few ones excluded, which upon flipping of the spins, they will form the new thermal fluctuations. After the flips, the old thermal fluctuations have the same spin as the dominant cluster and they become part of the new dominant cluster. The Metropolis algorithm picks lattice sites randomly: When they belong to the dominant cluster they are seldomly flipped, whereas the thermal fluctuations are flipped most of the time. Both algorithms function similarly and have the same efficiency.
Figure 14.4 shows typical spin configurations in the critical region. These are dominated by large clusters whose size, shape and position are random. The Wolff algorithm constructs large clusters easily, therefore, large clusters are easily created and destroyed in a few steps (figure 14.2 shows that ). In contrast, the Metropolis algorithm modifies clusters by slowly moving their boundaries and large clusters are destroyed/created very slowly. Autocorrelation times are expected to reduce drastically when using the Wolff algorithm in the critical region.
The expectation value of the size of the Wolff clusters is a dynamical quantity. In order to see this, we will show that in the disordered phase () we have that
| (14.23) |
We take the discussion from Newmann and Barkema [4]: Create a bond on each link of the lattice connecting two same spins with probability . In the end, the lattice will be divided in Wolff10 clusters. Each one will consist of sites, whose spin is . Choose a lattice site randomly and flip the spins of the cluster it belongs to. Destroy the bonds and repeat the process11 . The total magnetization is:
| (14.24) |
and
| (14.25) |
The values are equally probable due to the symmetry of the model, therefore the first term vanishes. Since , we obtain
| (14.26) |
In the Wolff algorithm, the creation of a cluster is equivalent to the choice of one of the clusters we created by following the procedure described above. The probability of selecting the cluster is
| (14.27) |
therefore the average value of the size of the Wolff clusters will be
| (14.28) |
By using equation (14.26) and the fact that for we have that 12, therefore
| (14.29) |
In order to create a cluster around a seed, we need a memory buffer for storing the new members of the cluster. We draw cluster sites from this buffer, and examine whether to add their nearest neighbors to the cluster.
There are two data structures that can be used in this job. The first one is the stack (or LIFO: last in – first out) and the second one is the queue (or FIFO: first in – first out). They are both one dimensional arrays, the only difference is how we draw data from them. In the case of a stack, we draw the last element that we stored in it. In the case of the queue, we draw the first element that we stored in it.
The stack is implemented as a one dimensional array stack(0:N-1)13 in which we “push” a new value that we want to store and we “pop” one that we want to retrieve. We use an integer m as a pointer to the last value that we stored in the position stack(m-1). m is also the number of active elements in the stack. In order to push a value e into the stack we:
In order to pop a value and store it in the variable e we:
The queue implementation is different. The data topology is cyclic, as shown in figure 14.5. We use an array queue(0:N-1) and two integers m, n which point at the beginning and at the end of the buffer. The beginning of the data is the element queue(m-1) and the end of the data is the element queue(n). When the queue is empty, we have that m=n and the same is true when it is full. Therefore we need a flag that flags whether the queue is empty or full. In the beginning we set flag=0 (queue is empty)14 . The number (m-n) mod N is the number of stored elements15 . When the queue has data, we set flag=1. In order to store a value e into the queue we:
In order to pop a value and store it in the variable e we:
Summarizing, the algorithm for constructing a Wolff cluster for the Ising model is the following:
The choice between stack or queue is not important. The results are the same and the performance similar. The only difference is the way that the clusters are constructed (for the stack, the cluster increases around the seed whereas for the queue it increases first in one direction and then in another). The careful programmer will try both during the debugging phase of the development. Bad random number generators can be revealed in such a test, since the Wolff algorithm turns out to be sensitive to their shortcomings.
The heart of the algorithm is coded in the subroutine16 wolff() in the file wolff.f90. Each call to wolff() constructs a Wolff cluster, flips its spin and records its size.
The buffer stack(0:N-1) is used in order to store the new members of the cluster. We call the function ALLOCATE for dynamically allocating the necessary memory and use DEALLOCATE before returning to the calling program in order to return this memory back to the system - and avoid memory leaks.
If the requested memory is not available, then chk>0 and the subroutine locerr() stops the program.
The seed is chosen randomly by a call to ranlux:
The seed is stored in cseed which is immediately added to the cluster (stack(0)=cseed). The variable nstack records the number of elements in the stack and it is originally set equal to 1. The variable ncluster counts the number of sites in the cluster and it is originally set equal to 1. sold=s(cseed) is the old value of the spin of the cluster and snew=-sold is the new one. The value of the spin of a new member of the cluster is immediately changed (s(cseed)=snew)! This increases the efficiency of the algorithm. By checking whether the spin of a nearest neighbor is equal to sold, we check whether the spin is the same as that of the cluster and if it has already been included in the cluster during a previous check.
The loop over the new members of the cluster is summarized below:
The loop do while(nstack > 0) is executed while nstack>0, i.e. as long as the stack is not empty and there exist new members in the cluster. The variable scluster is the current site drawn from the stack in order to check its nearest neighbors. The line nn = scluster + XNN; if(nn > N) nn = nn - N chooses the nearest neighbor to the right and stores it in the variable nn. If the spin s(nn) of nn is equal to sold, then this neighbor has the same spin as that of the cluster and it has not already been included to the cluster (otherwise its spin would have been flipped). The variable padd is equal to (it has been set in init) and if r<padd (which happens with probability ), then we add nn to the cluster: We add nn to the stack, we flip its spin (s(nn)=snew) and increase the cluster size by 1. We repeat for the rest of the nearest neighbors. The full code is listed below:
In order to link the subroutine with the rest of the program so that we construct one cluster per “sweep”17 , we modify main() accordingly:
The (global) variable algorithm controls whether the Wolff18 or the Metropolis algorithm will be used for the spin updates. The (global) variable padd is defined in init(). The following lines are added: into the file global_data.f90
The following lines are added to the file init.f90
The following lines are added to the file options.f90
in order to add the option -w to the command line. This option sets algorithm=1, which makes the program run the Wolff algorithm instead of the Metropolis. Some extra info must also be added to the help message printed by usage and simmessage and ... we are ready! For the compilation we use the Makefile
The commands
do the compilation, print the usage instructions of the program and perform a test run for , , by constructing 5000 clusters, starting from a hot configuration and writing the data to the file outL20b0.44.
In order to study the Ising model on a square lattice of given size , we have to perform simulations for many values of . Then, we want to study the finite size properties and extrapolate the results to the thermodynamic limit, by repeating the process for several values of . The process is long and ... boring. Moreover, a bored researcher makes mistakes and several bugs can enter into her calculations. Laziness is a virtue in this case and it is worth the trouble and the time investment in order to learn some techniques that will make our life easier, our work more efficient, and our results more reliable. Shell scripting can be used in order to code repeated tasks of the command line. In its simplest form, it is just a series of commands written into a text file. Such an example can be found in the file run1:
The first line is a comment, since everything after a # is ignored by the shell. The second line starts a simulation from a hot configuration (-s 1), lattice size L=20 (-L 20) and temperature (-b 0.10). The seed for the random number generator is set equal to 3423 (-S 3423) and we measure on 5000 Wolff clusters (-n 5000 -w). The results, printed to the stdout, are redirected to the file outL20b0.10 (> outL20b0.10).
The next ten lines continue the simulation for – . Each simulation starts from the configuration stored in the file conf at the end of the previous simulation.
In order to run these commands, the file run1 should be given execute permissions (only once, the permissions ... stay after that) using the command chmod:
Then run1 can be executed like any other command:
Not bad... But we can do better! Instead of adding one line for each simulation, we can use the programming capabilities of the shell. Let’s see how. The file run2 contains the commands:
The first line19 calls the shell tcsh in order to interpret the script. This was not necessary in run1, since every shell can interpret the commands that it contains. But in this case we use syntax which is special to the shell tcsh.
The second line is a comment.
The third line defines a shell variable whose name is L. Its value is set after the = character equal to the string "20". This value is accessible by adding a $ in front of the name of the variable. Therefore, whenever we write $L (or ${L}), the shell substitutes the string of characters 20. For example, in place of outL${L}b the shell constructs the string outL20b.
The fourth line defines an array, whose name is betas. The different elements of the array can be accessed by using the syntax $betas[number], where “number” is the array element starting from 1. In the example shown above $betas[1]= 0.10, $betas[2]= 0.20, ..., $betas[11]= 0.70. The special variable $#betas is the number of elements in the array, which is equal to 11. When we write $betas, the shell expands it to all the values in the array20 .
The fifth line defines the variable start to be equal to the string of characters "-s 1 -S 3423". The quotes have been put because we want it to be treated as a single string of characters. If we omit them, then the shell treats -s, 1, -S and 3423 as separate words, and we obtain a syntax error. Everything after the character # is a comment.
The command foreach is a way to construct a loop in tcsh. The commands between the foreach and end repeat once for every word in the parentheses in the foreach line. Each time, the loop variable, whose name is put after the keyword foreach, is set equal to the next word in the parenthesis. In our case, these words are the values of the array betas, and the loop will execute 11 times, once for each value , , ... , , each time with $beta set equal to one of those values.
The next three lines are the commands that are repeated by the foreach loop. The command echo “echoes” its arguments to the stdout and informs us about the current value of the parameters used in the simulation (quite useful, especially when the simulations take a long time). The command ./is runs the program, each time using a different value of beta. Notice that the name of the file in which we redirect the stdout changes each time that beta changes value. Therefore our data will be stored in the files outL20b0.10, outL20b0.20, ..., outL20b0.70. The third command forces the program to read the initial configuration from the file conf. The first time that the loop is executed, the value of start is "-s 1 -S 3423" (hot configuration, seed equal to 3423), whereas for all the next simulations, start is equal to "-s 2" (old configuration).
We can also include a loop over many values of L as follows:
The array variable Ls stores as many L as we wish. Note that the definitions of start are put in a special place (why?).
Data production must be monitored by looking at the time histories of properly chosen observables. This will allow us to spot gross mistakes and it will serve as a qualitative check of whether the system has thermalized and how long are the autocorrelation times. It is easy to construct time histories using gnuplot. For example, the commands21 :
show us the time histories of the energy, of the (absolute value of the) magnetization and of the size of the clusters in a simulation with and .
The expectation values of the energy per link and the magnetization per site with their errors can be calculated by the jackknife program, which can be found in the file jack.f90 in the directory Tools (see appendix 13.8). We compile the program into an executable file jack which we copy into the current working directory. The expectation value can be calculated using the command:
We pass the value L=40 to the program awk by using the option -v, therefore making possible the calculation of the ratio of the first column $1 by . The condition NR>500 makes the printing command to be executed only after awk reads the first 500 lines22 . This way we can discard a number of thermalization sweeps. The result of the above command is printed to the stdout as follows:
The first three lines are the comments printed by the program jack, which inform the user about the important parameters of the analysis. The last line gives and its error and then the fluctuations and their error. The latter must be multiplied by , in order to obtain the specific heat and its error according to (13.29) . By adding a few more lines to the command shown above, this multiplication can be done on the fly:
Well, why all this fuzz? Notice that all the commands shown above can be given in one single line in the command line (by removing the trailing ∖ of each line). By recalling the command, it is easy to obtain the results for a different value of and/or , by editing the values of the variables L and/or b. The result is
i.e. and .
We can work in a similar way for computing the magnetization. We have to calculate the absolute value of the second column of the stdout of the command ./is, for every line that does not start with a #:
The absolute value is calculated by the expression ($2>0)?$2:-$2, and it is stored in the variable m, which in turn is printed after being divided by . The result is
which gives and .
Similarly we can calculate :
The result is
which gives .
All of the above commands can be summarized in the script in the file run3:
The script is run with the command
Then, we can plot the results using gnuplot23 :
The above commands plot the magnetization.
The above commands plot the magnetic susceptibility.
The above commands plot the energy.
The above commands plot the specific heat.
The above commands plot .
In the case of the Metropolis algorithm, the “unit of time” in the Monte Carlo simulation is one “sweep”, which is equal to attempted spin flips. In the case of the Wolff algorithm, the size of the clusters is a stochastic variable, which depends on temperature. Therefore, flipping the spins of a cluster is not a convenient unit of time, and we define:
| (14.30) |
This definition of a sweep can be compared to a Metropolis sweep defined as accepted spin flips24 . For convenience, we also use the –dependent unit of time equal to one Wolff cluster update. We use the notation when the autocorrelation time of is measured in Wolf cluster updates, and when using the definition (14.30) . Their relation is:
| (14.31) |
We simulate the Ising model for and at using the Wolff algorithm. We construct Wolff clusters. The results are written to files with names outL${L}b0.4407. We also perform simulations using the Metropolis algorithm with sweeps. The results are written to files with names outL${L}b0.4407met. The following shell script makes life easier:
We compile the file autoc.f90 from the Tools directory and the executable file is named autoc and copied to the current working directory. Then, the following shell script calculates the autocorrelation functions :
We throw away sweeps for thermalization. The results are written to files whose names have file extension .rhom. The function is fitted to (13.51) using three autocorrelation times according to the discussion in appendix 13.7. The results are shown in table 14.125
10 | 2.18(2) | 0.6124(2) | 1.33(1) | 16.1(1) |
20 | 3.48(5) | 0.5159(1) | 1.80(3) | 70.7(4) |
40 | 5.10(6) | 0.4342(2) | 2.21(3) | 330(6) |
60 | 6.12(6) | 0.3927(2) | 2.40(2) | 795(5) |
80 | 7.33(7) | 0.3653(3) | 2.68(3) | 1740(150) |
100 | 8.36(6) | 0.3457(1) | 2.89(2) | 2660(170) |
From (14.10) we expect that where is the dynamic exponent. can be calculated by the gnuplot commands:
The exponent is calculated for the Wolff algorithm in Wolff steps and Wolff sweeps. The results are
| (14.32) |
| (14.33) |
| (14.34) |
The plots are shown in figures 14.11-14.13. The values of reported in the bibliography are , and respectively [4, 60, 67]. We can obtain better results by increasing the statistics and the lattice size and this is left as an exercise for the reader.
We also mention the relation between the dynamic exponents given by equations (14.32) and (14.33) . From (14.29) , (13.10) , and (13.6) and using , valid in the critical region, we obtain
| (14.35) |
where we assumed that , and . Therefore
| (14.36) |
Using the values given in (13.12) , , , we obtain
| (14.37) |
which is in agreement, within error, with the calculated values and the values in the bibliography.
40 | 1.7598(44) | 1.730(17) |
60 | 1.7455(24) | 1.691(14) |
80 | 1.7409(21) | 1.737(12) |
100 | 1.7420(24) | 1.7226(75) |
120 | 1.7390(15) | 1.7725(69) |
140 | 1.7390(23) | 1.7354(72) |
160 | 1.7387(10) | 1.746(17) |
200 | 1.7380(11) | 1.759(15) |
500 | 1.7335(8) | 1.7485(83) |
40 | 0.1101(7) | 0.1122(29) |
60 | 0.1129(5) | 0.1102(19) |
80 | 0.1147(5) | 0.1118(21) |
100 | 0.1175(3) | 0.1170(11) |
120 | 0.1167(4) | 0.1172(16) |
140 | 0.1190(2) | 0.1187(19) |
160 | 0.1191(4) | 0.1134(20) |
200 | 0.1205(10) | 0.1138(24) |
500 | 0.1221(2) | 0.1294(50) |
In this section we will discuss the extent to which relations (14.3) – (14.5) can be used for the calculation of the critical exponents , and . The result is that, although using them it is possible to compute correct results, these relations are not the best choice26 . In order to see clear scaling and reduce finite size effects, we have to consider and large . The results depend strongly on the choice of range of the data included in the fits. The systematic errors are large and the results in some cases plain wrong27 .
We simulate the Ising model for , , , , , , , and . The temperatures chosen correspond to small enough in order to observe scaling. For the values of used in the simulations, see the shell scripts in the accompanying software.
First we compute the exponent from the relation (14.3) . For given , we fit the data for for an appropriate range of to the function , which has two fitting parameters, and . We determine the range of where gives a linear plot in a log–log scale28 . For large , we observe deviations from the linear behavior and for very small we observe finite size effects when . As increases, finite size effects decrease, and the data get closer to the asymptotic behavior for even smaller . The results are more clear for , because for the fluctuations near the pseudocritical temperature are larger and the finite size effects are larger.
Table 14.2 shows the results for the exponent for all the measured values of . The errors reported are the statistical errors of the fits, which are smaller than the systematic errors coming from the choice or range of of the data included in the fits. One has to vary this range as long as the dof of the fit remains acceptable, and the resulting variation in the values of the parameters has to be included in the estimate of the error. Sometimes, this method gives an overestimated error, and it is a matter or experience to decide which parameter values to include in the estimate. For example, figures 14.14 and 14.15 show that the acceptable range of fitting becomes more clear by studying for increasing . As increases, the points approach the asymptotic curve even closer. Even though for fixed one obtains acceptable power fits over a larger range of , by studying the large convergence, we can determine the scaling region with higher accuracy. Another point to consider is whether the parameters of the fits have reasonable values. For example, even though the value of is unknown, it is reasonable to expect that its value is of order . By taking all these remarks into consideration we obtain
| (14.38) |
| (14.39) |
Next, we compute the critical exponent using relation (14.5) . This relation is valid as we approach the critical point from the low temperature phase, or . In the thermodynamic limit, the magnetization is everywhere zero for all . For a finite lattice , and it is reasonable to expect a scaling of the form
| (14.40) |
where is defined so that
| (14.41) |
By following the same procedure, we calculate the exponents and shown in table 14.3. By taking the systematic errors described above into consideration, we find that
| (14.42) |
| (14.43) |
which should be compared to the expected values .
The calculation of the exponent needs special care. The expected value is . This does not imply that const. but that 29
| (14.44) |
In this case, we find that the data is better fitted to the above relation instead of being fitted to a power. This can be seen pictorially by making a log–log plot and comparing it to a plot. We see that the second choice leads to a better linear plot than the first one. A careful study will compute the quality of the fits and choose the better model this way. This is left as an exercise for the reader.
In this section we will calculate the critical exponents by using relations (14.7) - (14.9) , i.e. by using the asymptotic scaling of , and with increasing system size . This is called “finite size scaling”.
In order to calculate the exponent given by equation (14.7) , we calculate the magnetic susceptibility at the known for increasing values of . We fit the results to the function and calculate the fitting parameters and . Then, we compare the computed value of to the expected value of . In this procedure we have to decide which values of should be included in the fits. The most obvious criterion is to obtain reasonable and that the error in and be small. This is not enough: Table 14.4 shows small variations in the obtained values of , if we consider different fit ranges. These variations give an estimate of the systematic error which enters in the calculation. Problem 9 is about trying this calculation yourselves. Table 14.4 shows the results, and figure 14.20 shows the corresponding plot. The final result, which includes also an estimate of the systematic errors, is
| (14.45) |
For the calculation of the exponent given by equation (14.9) , we compute the magnetization at the critical temperature and repeat the same analysis. The result is
| (14.46) |
Equation (14.9) gives the exponent . But the expected value leads, in analogy with equation (14.44) , to
| (14.47) |
This relation is shown in figure 14.22. The vertical axis is not in a logarithmic scale whereas the horizontal is. The linear plot of the data shows consistency with equation (14.47) . Problem 9 asks you to show whether the logarithmic fit is better than a fit to a function of the form and appreciate the difficulties that arise in this study. By increasing the statistics, and by measuring for larger , the data in table 14.8 will improve and lead to even clearer conclusions.
We observe that, by using finite size scaling, we can compute the critical exponents more effectively, than by using temperature scaling as in section 14.7. The data follow the scaling relations (14.7) – (14.9) suffering smaller finite size effects30 .
40 | 0.4308(4) | 30.68(4) | 0.437(1) | 0.5000(20) |
60 | 0.4342(2) | 62.5(1) | 0.4382(7) | 0.5515(15) |
80 | 0.4357(2) | 103.5(1) | 0.4388(5) | 0.5865(12) |
100 | 0.4368(1) | 153.3(2) | 0.4396(2) | 0.6154(18) |
120 | 0.4375(1) | 210.9(2) | 0.4396(4) | 0.6373(20) |
140 | 0.43793(13) | 276.2(4) | 0.4397(5) | 0.6554(18) |
160 | 0.4382(1) | 349.0(5) | 0.4398(4) | 0.6718(25) |
200 | 0.43870(7) | 516.3(7) | 0.4399(2) | 0.6974(17) |
500 | 0.43988(4) | 2558(5) | 0.44038(8) | 0.7953(25) |
1000 | 0.44028(4) | 8544(10) | 0.44054(8) | 0.8542(36) |
In the previous sections we discussed scaling in and in the critical region. In the calculations we used the exact value of the critical temperature . When is not known, the analysis becomes harder and its computation contributes to the total error in the value of the critical exponents. When doing finite size scaling using the scaling relations (14.7) – (14.9) , one has to choose the values of the temperature at which to calculate the left hand sides. So far, these values were computed at . What should we do when is not a priori known? A good choice is to use the pseudocritical temperature , the temperature where the fluctuations of the order parameter are at their maximum. Otherwise, we can compute according to the discussion in this section and use the computed in the finite size scaling analysis.
Both choices yield the same results in the large limit, even though the finite size effects are different. In fact any value of in the critical region can be used for this calculation. The reason is that as we approach the critical region for given , the correlation length becomes and finite size effects become important. This is the behavior that characterizes the pseudocritical region of the finite system. The pseudocritical region becomes narrower as becomes larger. Any value of in this region will give us observables that scale at large , but the best choice is
| (14.48) |
In this case, the values on the left hand sides of (14.7) – (14.9) should be taken at .
The definition of in not unique. One could use, for example, the maximum of the specific heat
| (14.49) |
which defines a different . Of course and both choices will yield the same results for large . The speed of convergence and the errors involved in the calculation of the pseudocritical temperatures are different in each case and there is a preferred choice, which in our case is .
First we calculate . When we are in the pseudocritical region we have that , therefore (14.2) gives
| (14.50) |
The calculation is straightforward to do: First we measure the magnetic susceptibility. For each we determine the pseudocritical region and we calculate and the corresponding maximum value . In order to do that, we should take many measurements around . We have to be very careful in determining the autocorrelation time (which increases as ), so that we can control the number of independent measurements and the thermalization of the system. We use the relation (14.50) and fit the results to , and from the calculated parameters and we compute , . In cases where one of the parameters , is known independently, then it is kept constant during the fit.
The results are shown in figure 14.23 where we plot the numbers contained in table 14.5. The final result is:
This can be compared to the known values and .This process is repeated for the pseudocritical temperatures and the maximum values of the specific heat . The results are shown in figure 14.24. The final result is:
Figure 14.24 and the results reported above show that the calculation using the specific heat gives results compatible with (14.51) , but that they are less accurate. The values of the specific heat around its maximum are more spread and more noisy than the ones of the magnetic susceptibility.From the maxima of the magnetic susceptibility we can calculate the exponent . Their values are shown in table 14.5. The data are fitted to , according to the asymptotic relation (14.9) , with and being fitting parameters. We find very good scaling, therefore our data are in the asymptotic region. The result is
| (14.51) |
which is consistent with the analytically computed value .
From the maxima of the specific heat we can calculate the exponent . Since , the form of the asymptotic behavior is given by (14.47) . We find that our results are not very well fitted to the function and it is possible that the discrepancy is due to finite size effects. We add terms that are subleading in and find that the fit to the function is very good31 . If we attempt to fit the data to the function , the quality of the fit is poor and the result for is consistent with zero. The results are shown in figure 14.26.
The scaling relations (14.3) – (14.9) are due to the appearance of a unique, dynamical length scale, the correlation length32 . As we approach the critical point, diverges as , and we obtain universal behavior for all systems in the same universality class. If we consider the magnetic susceptibility , its values depend both on the temperature , the size of the system and of course on the details of the system’s degrees of freedom and their dynamics. Universality leads to the assumption that the magnetic susceptibility of the infinite system in the critical region depends only on the correlation length . For the finite system in the pseudocritical region, finite size effects suppress the fluctuations when . The length scales that determine the dominant scaling behavior are and , therefore the dimensionless variable is the only independent variable in the scaling functions. In order to obtain the scaling relation , valid for the infinite system, we only need to assume that for the finite system33
| (14.52) |
where is a function of one variable, such that
| (14.53) |
and
| (14.54) |
Indeed, when () the magnetic susceptibility takes values very close to those of the infinite system, and (14.53) gives . As , finite size effects enter and (14.54) gives . The latter is nothing but (14.7) for the maxima of the magnetic susceptibility of the finite system that we studied in figure 14.25. Therefore the function describes how the magnetic susceptibility deviates from scaling due to finite size effects.
The function can be calculated using the measurements coming from the Monte Carlo simulation. Since the correlation length is not directly calculated, but appears indirectly in the measurements, we define the dimensionless variable
| (14.55) |
where since34 . We define so that (14.52) becomes
| (14.56) |
The asymptotic properties of the scaling function are determined by the relations (14.53) and (14.54) . When , equation (14.53) is valid for and we obtain From the definition we obtain and we confirm the scaling property of the magnetic susceptibility in the thermodynamic limit . Therefore
| (14.57) |
When , (14.54) is valid and we have that . Then we obtain Therefore, we confirm that, when finite size effects are dominant (), we have that . Therefore
| (14.58) |
By inverting equation (14.56) , we can calculate the scaling function from the measurements of the magnetic susceptibility
| (14.59) |
where are measurements for temperatures in the pseudocritical region for several values of . When equation (14.59) is valid, then all the measurements fall onto the same curve independently of the size ! Of course deviations due to finite size effects are expected, especially when is small. But, as we will see, convergence is quite fast.
Using the above procedure, we can determine the critical temperature , the exponent and the ratio simultaneously! In order to check (14.59) , we have to compute the variable , for which it is necessary to know () and the exponent . For the calculation of it is necessary to know that appears on the right hand side of (14.59) . Relation (14.59) depends quite sensitively on the parameters , and and this way we obtain an accurate method for their calculation.
In order to do the calculation, we set initial values for the parameters . Using , , and , we calculate the scaling variable . Using and , we calculate and plot the points near the critical region . Then we vary until the curves for different collapse onto each other. The optimal collapse determines .
The collapse of the curves that are constructed from the points for different is the most efficient method for studying scaling in the critical region. Figure 14.27 shows the function for the known values of the parameters . Small variations of the parameters lead to a sharp change of the quality of the collapse. We can make a quick and dirty estimate of the accuracy of the method by varying one of the parameters, and look for a visible deviation from all data collapsing onto a single curve. The result is
Notice that, this crude estimate yields results whose accuracy is comparable to the previously calculated ones!A similar procedure can be followed for other scaling observables, like the specific heat and the magnetization. Equations (14.8) and (14.9) generalize to35
| (14.60) |
and
| (14.61) |
since . The results are shown in figures 14.28 and 14.29 respectively.
Below, we list a gnuplot program in order to construct plots like the ones shown in figures 14.27–14.29. If we assume that the data are in a file named all in the following format36 :
where the lines starting with the character m contain whereas the ones starting with e contain . The program can be found in the file scale_gamma.gpl:
In order to use the above program, we give the gnuplot commands
The first two lines define the parameters of the plot. The variable Ls contains all the lattice sizes that we want to study, each value of separated from another by one or more spaces. The variables bc, nu, gnu are the parameters , and that will be used in the scaling relation (14.59) . The third command calls the program that makes the plot. If we need to vary the parameters, then we redefine the corresponding variables and ... repeat.
In order to dissect the above program, look at the online help manual of gnuplot37 . We will concentrate on the construction of the awk filter that computes the points in the plot properly normalized. The value of the function cplot(i) is a string of characters which varies with (the index i corresponds to the i-th word of the variable Ls). For each i, this string is substituted in the plot command, and it is of the form "< grep ... L-gnu)*$7}’". The values of the parameters are passed using the function sprintf(), which is called each time with a different value of i. The dirty work is done by awk, which calculates each point (columns 6 and 7: $6, $7 are and its error ). For a given value of , the grep component of the filter prints the lines of the file all which contain the magnetization. The sort component sorts data in the order of increasing temperature (column 3: -k3,3g)
Can we make the above study more systematic and apply quantitative criteria for the quality of the collapse, which will help us estimate the error in the results? One crude way to estimate errors is to split the data in bins and work independently on each set of data. Each bin will give an optimal set of parameters which will be assumed to be an independent measurement. The errors can be calculated using equation (13.39) (for ).
In order to provide a quantitative measure of the quality of the collapse, we define a similar to the one used in data fitting, as discussed in appendix 13.7. When the distance between the collapsing curves increases, this should increase. Assume that our measurements consist of sets of measurements for . After setting the parameters and an interval , we calculate the data sets , for all , using our measurements. The data sets consist of points of the data for , for which the are in the interval . For each point , we calculate the scaling function , which depends on the chosen parameters and the lattice size . Then, we have to choose an interpolation method, which will define the interpolation functions 38, so that we obtain a good estimate of the scaling function between two data points. Then, each point in a data set has a well defined distance from every other data set ( and ), which is defined by the distance from the interpolating function of the other sets. This is equal to39 . We define the quantity
where is the number of terms in the sum. The normalization constant is used, because this is the number of pairs of curves in the sum. Each term is weighted by its error , so that points with small error have a larger contribution than points with large error. This is the definition used in [71], but you can see other approaches in [4], [69].The depends on the parameters and the interval . Initially, we keep fixed and perform a minimization with respect to the parameters . The minimum is given by the values and these values are the estimators that we are looking for. In order to calculate the errors we can bin our data according to the discussion on page 1663. Alternatively, we may assume a distribution of the measurements, and if the minimum , then the intervals of the parameter values that keep give an estimate of the errors in the parameters.
The results depend on the chosen interval . Usually [69], this is chosen so that its center is at the maximum of so that . If is larger than it should, then is large and we don’t have good scaling. If it is too small, then the errors will be large. By taking the limit , we calculate by studying the convergence of to stable, optimal with respect to error, values (see figure 8.7, page 238 in [4] as well as [69]).
Up to now, we have studied fluctuations of observables by computing second order cumulants40 . The calculation of the critical temperature, the order of the phase transition and the critical exponents can also be done by computing higher order cumulants and sometimes this calculation gives more accurate and clear results.The most famous one is the Binder cumulant which is a fourth order cumulant41 , and its name derives from Kurt Binder who studied it first [72, 73],
| (14.63) |
Appendix 14.12 discusses its properties in detail. For a continuous phase transition
| (14.64) |
where for the Ising model on a square lattice [73]. The value corresponds to the Gaussian distribution, whereas the value corresponds to two Gaussian distributions of small width around two symmetric values (see problems 14 and 15).
It practice, it is found that finite size corrections to are small, therefore the calculation of gives an accurate measurement of the critical temperature . The curves intersect at the point (, ) for different and this point gives a very good estimate of .
40 | 60 | 80 | 0.44069(4) | 0.6109(5) |
60 | 80 | 100 | 0.44069(4) | 0.6108(7) |
80 | 100 | 120 | 0.44068(4) | 0.6108(7) |
100 | 120 | 140 | 0.44069(4) | 0.6108(11) |
120 | 140 | 160 | 0.44069(4) | 0.6109(20) |
140 | 160 | 200 | 0.44067(3) | 0.6102(12) |
160 | 200 | 500 | 0.44067(2) | 0.6105(10) |
200 | 500 | 1000 | 0.44068(1) | 0.6106(9) |
Figure 14.30 shows our measurements for . The intersection of the curves in the figure at a single point (, ) is impressively accurate. Table 14.6 shows an attempt to calculate systematically by computing the critical temperature from the intersection of the curves for three values of . By taking into account all the measurements for – the computed result is
| (14.65) |
which is in a very good agreement with the expected values , . Notice that, in the calculation of the systematic error due to finite size effects decreases with increasing , whereas the statistical error increases due to the increase of the slope of the curves near the point . But the accuracy of the calculation of turns out to be better with increasing .
Finite size scaling can also be applied to the Binder cumulant in order to calculate and . From equation (14.90) (14.119) of appendix 14.12, we expect that scales as
| (14.66) |
This is confirmed in figure 14.31. From the value , we obtain , which is consistent with the result (14.65) .
The numerical calculation of critical exponents, and especially , can be hard in the general case. Therefore it is desirable to cross check results using several observables which have known scaling behavior. We discuss some of them below42 . They involve the correlations of the magnetization with the energy.
The derivative of the Binder cumulant is
| (14.67) |
Its scaling is given by equation (14.120)
| (14.68) |
which us plotted in figure 14.32. Notice that defines a pseudocritical region around its maximum. The scaling of the maximum as well as the scaling of its position can be used in order to compute , as we did in figures 14.23 and 14.25 for the magnetic susceptibility.
It could also turn out to be useful to study correlation functions of the form
| (14.69) |
whose scaling properties are given by equation (14.126) of appendix 14.12,
| (14.70) |
In particular we are interested in the case
| (14.71) |
and
| (14.72) |
We also mention the energy cumulant
| (14.73) |
In [76], it is shown that for a second order phase transition , whereas for a first order phase transition, we obtain a non trivial value. Therefore, this parameter can be used in order to determine whereas a system undergoes a first order phase transition. This is confirmed in figure 14.35. The minima of the curves converge to the critical temperature according to (14.50) .
In section 14.11, we studied the scaling properties of the Binder cumulant
| (14.74) |
numerically. In this appendix, we will use the general scaling properties of a system that undergoes a continuum phase transition near its critical temperature, in order to derive the scaling properties of and its derivatives. For more details, the reader is referred to [73], [6].
The values of are trivial in two cases: When the magnetization follows a Gaussian distribution, which is true in the high temperature, disordered phase, we have that . When we are in the low temperature, ordered phase, we have that . The proof is easy and it is left as an exercise (see problems 14 and 15).
According to the discussion in chapter 14, when the critical temperature of a continuum phase transition is approached, the system exhibits scaling properties due to the diverging correlation length . If we approach from the high temperature phase, then we expect that the distribution function of the magnetization per site (not its absolute value) is approximately of the form
| (14.75) |
which is a Gaussian with standard deviation . We have temporarily assumed that the system is defined on a –dimensional hypercubic lattice of edge .
When the critical temperature is approached, the distribution function scales according to the relation [73]
| (14.76) |
where , , is the correlation length in the thermodynamic limit. As we approach the critical point, , in such a way that . Equation (14.76) is a scaling hypothesis which plays a fundamental role in the study of critical phenomena.
In order to calculate the exponents in equation (14.76) , we apply the normalization condition of a probability distribution function
where we set . For the left hand side to be equal to one, we must have that ,
| (14.78) |
and . , and is a constant independent of and . Finally, we obtain
| (14.79) |
The moments of the distribution of the spins are
where the last line is the definition of the function . When we first take the thermodynamic limit and then approach the critical temperature , the correlation length diverges in such a way that . In the region () we have that , and by using the relations
| (14.81) |
we obtain
| (14.82) |
In the above equations, we introduced the universal amplitudes and , which are universal constants (i.e. they are the same within a universality class) and they are defined from equation (14.81) . In this limit, in order for (14.80) to have consistent scaling for on the right and left hand sides43 , we obtain (compare with equation (14.57) )
| (14.83) |
In order to compute the -scaling, we substitute the above equations to (14.80) for , and we obtain
| (14.84) |
Then, we obtain
| (14.85) |
where we used the known44 hyperscaling relation
| (14.86) |
Finally, we obtain the equations ,
| (14.87) |
| (14.88) |
| (14.89) |
which are valid in the disordered phase . From equation (14.74) , we find that the critical behavior of the Binder cumulant is
| (14.90) |
Finite size effects dominate in the pseudocritical region, in which case we take the thermodynamic limit keeping finite, and the fluctuations get suppressed, rendering the functions finite. Therefore, we obtain45
| (14.91) |
which shows why the value of at the critical temperature turned out to be almost independent of the system size . is found to depend on the boundary conditions and on the anisotropy of the interaction. For the Ising model on the square lattice we have that [73] (Kamieniarz+Blöte)
| (14.92) |
Consider a change of length scale on a lattice so that
| (14.93) |
where is the dimensionless correlation length in the thermodynamic limit and is the scaling factor. Then, the basic assumption for the scaling of thermodynamics quantities in the region of a continuous phase transition is that the free energy is changed according to 46
| (14.94) |
where is the reduced temperature and is the external magnetic field47 . The above relation summarizes the scaling hypothesis, and it is a relation similar to (14.76) . This relation can be understood through the renormalization group approach, and the fundamental assumption is the appearance of a unique dynamical length scale that diverges as we approach the critical point. The arguments and give the change in the coupling constants and under the change in length scale in order that the equation remains valid.
By applying the above relation times we obtain
| (14.95) |
If we take , , keeping the product fixed, we obtain
where we substituted and defined the scaling function and the critical exponent
| (14.97) |
By applying the same reasoning to (14.93) for the correlation length we obtains
| (14.98) |
By taking the limit , , keeping the product , the left hand side will give a finite value, e.g. whereas the right hand side will give
| (14.99) |
By considering the case , and by comparing to the known relation (14.4) , we obtain
| (14.100) |
By taking the derivative of (14.96) with respect to the temperature, we obtain
| (14.101) |
We use the notation whenever we neglect terms that are not related to the scaling properties of a function.
By taking the derivative once more, and by setting , we obtain the specific heat
| (14.102) |
Therefore, the critical exponent is nothing but the critical exponent of the specific heat defined in equation (14.4) .
The magnetic susceptibility can be obtained in a similar way by taking the derivative of (14.96) with respect to
| (14.103) |
By taking the derivative once more time and by setting we obtain the magnetic susceptibility
| (14.104) |
and, by comparing to (14.3) , we obtain
| (14.105) |
In the last two equations we used the hyperscaling relations
| (14.106) |
We will now extend the analysis of the previous section to the case of a system of finite size. We will assume that the system’s degrees of freedom are located on a lattice whose linear size is (the volume is , is the number of dimensions), where is the (dimensionless) number of lattice sites and is the lattice constant. We consider the limit and , so that remains constant. By changing the -scale
| (14.107) |
and
| (14.108) |
equation (14.94) generalizes to
| (14.109) |
By taking the limit , and (approach of the critical point), the above relation becomes
| (14.110) |
By differentiating and setting as in the previous section we obtain48
| (14.111) |
where we set , .
The thermodynamic limit is obtained for where , which yields the known relation .
When is comparable to , finite size effects dominate. The large fluctuations are suppressed and the magnetic susceptibility has a maximum at a crossover (pseudocritical) temperature , where . The last relation holds because by assumption. We obtain
| (14.112) |
In the region of the maximum, we obtain the functional form
| (14.113) |
which is nothing but equation (14.59) . The function is analytic in its argument , since for a finite system is an analytic function of the temperature49 . In the thermodynamic limit ( and , therefore )
| (14.114) |
so that . Near the pseudocritical point
| (14.115) |
and we expect that for we have that
| (14.116) |
The above relations lead to the following conclusions:
We conclude that depends on the boundary conditions and the geometry of the lattice.
Similarly, we obtain
| (14.117) |
and by following similar arguments leading to (14.113) , we obtain
| (14.118) |
For the Binder cumulant we obtain
| (14.119) |
where in the last equality we expanded the analytic functions for small . Then, we see that
| (14.120) |
By differentiating (14.110) with respect to the temperature we obtain
where we used the notation . The term proportional to vanishes when we set . In the pseudocritical region, where , the first and third term are of the same order in and we obtain
| (14.122) |
and by successive differentiation
| (14.123) |
The derivatives
| (14.124) |
| (14.125) |
In particular
| (14.126) |
| (14.127) |
| (14.128) |
The scaling relation
| (14.129) |
defines the exponents , . The relation
| (14.130) |
defines the exponent coming from the two point correlation function .
From the definitions and the hyperscaling relations we have that
| (14.132) |
| (14.133) |
| (14.134) |
The files all and allem in the accompanying software contain measurements that you can use for your data analysis or compare them with your own measurements.
40 | 20.50 | 0.02 | 0.6364 | 0.0001 | 0.4883 | 0.0007 |
60 | 41.78 | 0.08 | 0.6049 | 0.0002 | 0.5390 | 0.0008 |
80 | 69.15 | 0.09 | 0.5835 | 0.0001 | 0.5743 | 0.0012 |
100 | 102.21 | 0.25 | 0.5673 | 0.0002 | 0.6026 | 0.0014 |
120 | 140.18 | 0.11 | 0.5548 | 0.0001 | 0.6235 | 0.0010 |
140 | 183.95 | 0.33 | 0.5442 | 0.0002 | 0.6434 | 0.0006 |
160 | 232.93 | 0.55 | 0.5351 | 0.0001 | 0.6584 | 0.0020 |
200 | 342.13 | 0.72 | 0.5206 | 0.0001 | 0.6858 | 0.0014 |
500 | 1687.2 | 4.4 | 0.4647 | 0.0002 | 0.7794 | 0.0018 |
1000 | 6245 | 664 | 0.4228 | 0.0040 | – | – |
[1] www.physics.ntua.gr/~konstant/ComputationalPhysics/. The site of this book. The accompanying software and additional material can be found there. You may also find contact information about the author for sending corrections and/or suggestions. Fan mail accepted too!
[2] H. Gould, J. Tobochnik and H. Christian, “Computer Simulation Methods, Application to Physical Systems”, Third Edition, Addison Wesley (2007). A great introductory book in computational physics. Java is the programming language of choice and a complete computing environment is provided for using and creating interacting and visual physics applications. The software is open source and can be downloaded from opensourcephysics.org. The book has open access and can be downloaded freely.
[3] R. Landau, M. J. Pez and C. C. Bordeianu, “Computational Physics: Problem Solving with Computers”, Wiley-VCH, 2 ed. (2007).
[4] M. E. J. Newman and G. T. Barkema, “Monte Carlo Methods in Statistical Physics”, Clarendon Press, Oxford (2002). Excellent book for an introductory and intermediate level course in Monte Carlo methods in physics.
[5] B. A. Berg, “Markov Chain Monte Carlo Simulations and Their Statistical Analysis. With Web-Based Fortran Code”, World Scientific, 2004. Graduate level Monte Carlo from a great expert in the field. Covers many advanced Monte Carlo methods.
[6] D. P. Landau and K. Binder, “A Guide to Monte Carlo Simulations in Statistical Physics”, Cambridge University Press, 3rd Edition, 2009.
[7] K. Binder and D. W. Heermann, “Monte Carlo Simulation in Statistical Physics”, Fifth Edition, Springer (2010).
[8] W. H. Press, S. A. Teukolsky, W. T. Vetterling and B. P. Flanney, “Numerical Recipes, The Art of Scientific Computing”, Third Edition, Cambridge University Press (2007), www.nr.com. Well, this is the handbook of every physicist in numerical methods.
[Chapter 1]
[9] M. Metcalf, J. Reid , M. Cohen, Modern Fortran Explained, 4th Edition, OUP Oxford (2011).
[10] J. C. Adams, W. S. Brainerd, R. A. Hendrickson, R. E. Maine, J. T. Martin and B. T. Smith, “The Fortran 2003 Handbook: The Complete Syntax, Features and Procedures”, Springer (2009).
[11] T. M. R. Ellis, I. R. Philips and T. M. Lahey, “Fortran 90 Programming”, Addison-Wesley (1994).
[12] C. G. Page, Professional Programmer’s Guide to Fortran77, http://www.star.le.ac.uk/~cgp/prof77.html.
[13] Gnuplot official site http://gnuplot.info/
[14] P. K. Janert, “Gnuplot in Action: Understanding Data with Graphs”, 2nd Edition, Manning Publications (2012).
[15] L. Phillips, “gnuplot Cookbook”, Packt Publishing, 2012.
[16] tcsh homepage: http://www.tcsh.org/Home
[17] P. DuBois, “Using csh & tcsh”, O’Reilly and Associates (1995), www.kitebird.com/csh-tcsh-book/.
[18] M. J. Currie, “C-shell Cookbook”,
http://www.astro.soton.ac.uk/unixtut/sc4.pdf.
[19] Wiki book: “C Shell Scripting”,
http://en.wikibooks.org/wiki/C_Shell_Scripting.
[20] G. Anderson and P. Anderson, “The Unix C Shell Field Guide”, Prentice Hall (1986).
[Chapter 3]
[21] R. M. May, “Simple Mathematical Models with Very Complicated Dynamics”, Nature 261 (1976) 459. The first pedagogical and relatively brief introduction to the logistic map and its basic properties.
[22] C. Efthimiou, “Introduction to Functional Equations: Theory and Problem-Solving Strategies for Mathematical Competitions and Beyond”, MSRI Mathematical Circles Library (2010). Section 16.7 presents a brief and simple presentation of the mathematical properties of the logistic map.
[23] P. Cvitanović, R. Artuso, R. Mainieri, G. Tanner and G. Vattay, “Chaos: Classical and Quantum”, ChaosBook.org, Niels Bohr Institute (2012). An excellent book on the subject. Can be freely downloaded from the site of the book.
[24] L. Smith, “Chaos: A Very Short Introduction”, Oxford University Press (2007).
[25] M. Schroeder, “Fractals, Chaos, Power Laws: Minutes from an Infinite Paradise”, W.H. Freeman (1991).
[26] S. H. Strogatz, “Non Linear Dynamics and Chaos”, Addison-Wesley (1994).
[27] Wikipedia: “Chaos Theory”, “Logistic Map”, “Bifurcation Diagram”, “Liapunov Exponents”, “Fractal Dimension”, “Feigenbaum constants”.
[28] Wikipedia: “List of chaotic maps”.
[29] Wikipedia: “Newton’s method”.
[30] M. Jakobson, “Absolutely continuous invariant measures for one-parameter families of one-dimensional maps”, Commun. Math. Phys. 81 (1981) 39.
[Chapter 4]
[31] “Numerical Recipes” [8]. See chapters on the Runge–Kutta methods.
[32] E. W. Weisstein, “Runge-Kutta Method”, from MathWorld–A
Wolfram Web Resource.
http://mathworld.wolfram.com/Runge-KuttaMethod.html.
[33] J. H. E. Cartwright and O. Piro, “The dynamics of Runge-Kutta methods”, Int. J. Bifurcation and Chaos 2, (1992) 427-449.
[34] J. H. Mathews, K. Fink, “Numerical Methods Using Matlab”, Prentice Hall (2003), Chapter 9.
[35] J. H. Mathews, “Numerical Analysis - Numerical Methods Project”, http://math.fullerton.edu/mathews/numerical.html.
[36] I. Percival and D. Richards, “Introduction to Dynamics”, Cambridge University Press (1982). See also [38].
[37] J. B. McLaughlin, “Period Doubling bifurcations and chaotic motion for a parametrically forced pendulum”, J. Stat. Phys. 24 (1981) 375–388.
[Chapter 5]
[38] J. V. José and E. J. Saletan, “Classical Dynamics, a Contemporary Approach”, Cambridge University Press, 1998. A great book on Classical Mechanics. You will find a lot of information on non linear dynamical systems exhibiting chaotic behavior. See also the chapters on scattering and planetary motion.
[Chapter 6]
[39] R. W. Brankin, I. Gladwell, and L. F. Shampine, “RKSUITE: a suite of Runge-Kutta codes for the initial value problem for ODEs”, Softreport 92-S1, Department of Mathematics, Southern Methodist University, Dallas, Texas, U.S.A (1992). Available at www.netlib.org/ode/rksuite and in the accompanying software of the book.
[Chapter 9]
[40] See the Mathematica Notebooks of Peter West http://young.physics.ucsc.edu/115/.
[41] U. Wolff, B. Bunk, F. Knechtli, “Computational Physics I”,
http://www.physik.hu-berlin.de/com/
teachingandseminars/previous_CPI_CPII.
[42] F. T. Hioe and E. W. Montroll, “Quantum theory of anharmonic oscillators. I. Energy levels of oscillators with positive quartic anharmonicity”, J. Math. Phys. 16 (1975) 1945, http://dx.doi.org/10.1063/1.522747
[Chapter 11]
[43] L. Kadanoff, “Statistical Physics – Statics, Dynamics and Renormalization”, World Scientific (2000). A great book in advanced statistical physics by one of the greatest in the field!
[44] J. Ambjørn, B. Durhuus and T. Jonsson, “Quantum Geometry”, Cambridge Monographs on Mathematical Physics, Cambridge University Press (1997). More in depth discussion of random walks in field theory and statistical mechanics.
[45] C. Itzykson and J. M. Drouffe, “Statistical Field Theory”, Volume 1, Cambridge Monographs on Mathematical Physics, Cambridge University Press (1989). Random walks and Euclidean quantum field theory.
[46] D. E. Knuth, “Seminumerical Algorithms”, Vol. 2 of “The Art of Computer Programming”, Addison-Wesley (1981).
[47] M. Lüscher, Comput.
Phys. Commun. 79 (1994) 100; F. James, Comput. Phys. Commun. 79
(1994) 111; Erratum 97 (1996) 357. The code is available at the journal’s
site http://cpc.cs.qub.ac.uk/summaries/ACPR_v1_0.html as well as
from CERN at
http://wwwasd.web.cern.ch/wwwasd/cernlib/
download/2001_wnt/src/mathlib/gen/v/ranlux.F.
[48] L. Schrage, “A More Portable Fortran Random Number Generator”, ACM Transactions on Mathematical Software, 5 (1979) 132-138; P. Bratley, B. L. Fox and L. Schrage, “A Guide to Simulation”, Springer-Verlag, 1983.
[49] G. Marsaglia and A. Zaman, Ann. Appl. Prob. 1 (1991) 462.
[50] B. Li, N. Madras and A. D. Sokal, “Critical Exponents, Hyperscaling and Universal Amplitude Ratios for Two- and Three-Dimensional Self-Avoiding Walks”, J.Statist.Phys. 80 (1995) 661-754 [arXiv:hep-lat/9409003]; G. Slade, “The self-avoiding walk: A brief survey”, Surveys in Stochastic Processes, pp. 181-199, eds. J. Blath, P. Imkeller and S. Roelly, European Mathematical Society, Zurich, (2011), http://www.math.ubc.ca/~slade/spa_proceedings.pdf.
[Chapter 12]
[51] J. J. Binney, N. J. Dowrick, A. J. Fisher and M. E. J. Newman, “The Theory of Critical Phenomena”, Clarenton Press (1992). A simple introduction to critical phenomena and the renormalization group.
[52] R. K. Pathria and P. D. Beale, “Statistical Mechanics”, Third Edition, Elsevier (2011). A classic in statistical physics.
[53] F. Mandl, “Statistical Physics”, Second Edition, Wiley (1988).
[54] R. J. Baxter, “Exactly Solved Models in Statistical Mechanics”, Dover Publications (2008).
[Chapter 13]
[55] E. Ising, “Beitrag zur Theorie des Ferromagnetizmus”, Z. Phys. 31 (1925) 253–258.
[56] L. Onsager, “Crystal Statistics. I. A Two–Dimensional Model with an Order–Disorder Transition”, Phys. Rev. 65 (1944) 117–119.
[57] K. Huang, “Statistical Mechanics”, John Wiley & Sons, New York, (1987). A detailed presentation of the Onsager solution.
[58] C. N. Yang, Phys. Rev. 85 (1952) 809.
[59] N. Metropolis, A. W. Rosenbluth, M. N. Rosenbluth, A. H. Teller and E. J. Teller, Chem. Phys. 21 (1953) 1087.
[60] M. P. Nightingale and H. W. J. Blöte, Phys. Rev. Lett. 76 (1996) 4548.
[61] H. Müller-Krumbhaar and K. Binder, J. Stat. Phys. 8 (1973) 1.
[62] B. Efron SIAM Review 21 (1979) 460; Ann. Statist. 7 (1979) 1;B. Efron and R. Tibshirani, Statistical Science 1 (1986) 54. Freely available from projecteuclid.org.
[Chapter 14]
[63] R. H. Swendsen and J.-S. Wang, Phys. Rev. Lett. 58 (1987) 86.
[64] U. Wolff, Phys. Rev. Lett. 62 (1989) 361.
[65] A. Pelisseto and E. Vicari, “Critical Phenomena and Renormalization–Group Theory”, Phys. Reports 368 (2002) 549.
[66] F. Y. Wu, “The Potts Model”, Rev. Mod. Phys. 54 (1982) 235.
[67] P. D. Coddington and C. F. Baillie, Phys. Rev. Lett. 68 (1992) 962.
[68] H. Rieger, Phys. Rev. B 52 (1995) 6659.
[69] E. J. Newman and G. T. Barkema, Phys. Rev. E 53 (1996) 393.
[70] A. E. Ferdinand and M. E. Fisher, Phys. Rev. 185 (1969) 832; N. Sh. Izmailian and C. -K. Hu, Phys. Rev. E 65 (2002) 036103; J. Salas, J. Phys. A 34 (2001) 1311; W. Janke and R. Kenna, Nucl. Phys. (Proc. Suppl.) 106 (2002) 929.
[71] J. Ambjørn and K. N. Anagnostopoulos, Nucl. Phys. B 497 (1997) 445.
[72] K. Binder, Phys. Rev. Lett. 47 (1981) 693.
[73] K. Binder, Z. Phys. B 43 (1981) 119; G. Kamieniarz and H. W. J. Blöte, J. Phys. A 26 (1993) 201.
[74] J. Cardy, “Scaling and Renormalization in Statistical Physics”, 1st Edition, Cambridge University Press (1996).
[75] A. M. Ferrenberg and D. P. Landau, Phys. Rev. B44 (1991) 5081.
[76] M. S. S. Challa, D. P. Landau and K. Binder, Phys. Rev. B34 (1986) 1841.
[77] H. E. Stanley, “Introduction to Phase Transitions and Critical Phenomena”, Oxford (1971).
[78] R. Creswick and S.-Y. Kim, J. Phys. A: Math.Gen. 30 (1997) 8785.
[79] C. Holm and W. Janke, Phys. Rev. B 48 (1993) 936 [arXiv:hep-lat/9301002].
[80] M. Hasenbusch and S. Meyer, Phys. Lett. B 241 (1990) 238.
[81] M. Kolesik and M. Suzuki, Physica A 215 (1995) 138 [arXiv:cond-mat/9411109].
absolute path, 76
acceptance ratio, 77, 78
average, 79, 80
anharmonic oscillator, 81, 82
annihilation operator, 83
attractor, 84, 85
autocorrelation
critical slowing down, 86
dynamical exponent , 87
function, 88, 89
independent measurement, 90, 91
time, 92, 93
subdominant, 94
time, integrated, 95
basin of attraction, 96, 97, 98
bifurcation, 99, 100, 101
Binder cumulant, 102
Boltzmann constant, 103
Boltzmann distribution, 104
bootstrap, 105, 106, 107
boundary conditions
fixed, 108
free, 109
helical, 110, 111
periodic, 112, 113
toroidal, 114, 115
boundary value problem, 116
canonical ensemble, 117, 118
chaos, 119, 120, 121
period doubling, 122
pseudorandom numbers, 123
circle map, 124
cluster, 125, 126
cluster algorithms, 127
cluster seed, 128
cobweb plot, 129
cold state, 130, 131
collapse, 132
command completion, 133
command substitution, 134
completion
command, 135
filename, 136
conformable arrays, 137, 138
conjugate thermodynamic quantities, 139
continuum limit, 140
correlation function, 141, 142
correlation length, 143, 144, 145, 146
Coulomb’s law, 147
Courant parameter, 148
CPU time, 149
creation operator, 150
critical exponents, 151, 152, 153, 154
, 155, 156, 157, 158, 159
, 160, 161
, 162, 163
, 164, 165
, 166, 167, 168
, 169, 170, 171, 172
, 173, 174, 175
, 176, 177
, 178, 179
, 180, 181, 182
critical slowing down, 183, 184
cross section, 185
differential, 186, 187
total, 188
cumulant, 189
cumulative distribution function, 190
Curie temperature, 191
current directory, 192
density of states, 193
dependencies, 194
derivative
numerical, 195, 196
derivative, numerical, 197
detailed balance, 198
detailed balance condition, 199
diagonalization, 200
diffusion, 201
equation, 202
kernel, 203
Dirac delta function, 204
directory, 205
home, 206, 207
parent, 208
Dirichlet boundary condition, 209
disordered phase, 210
double well, 211, 212
DSYEV, 213
Duffing map, 214
dynamic exponent , 215, 216
dynamic memory allocation, 217
eccentricity, 218
eigenstate, 219, 220
eigenvalues, 221
eigenvectors, 222
electric equipotential surfaces, 223
electric field, 224, 225
electric field lines, 226
electric potential, 227, 228
Emacs, 229
abort command, 230, 231
auto completion, 232
commands, 233
Ctrl key, C-, 234
cut/paste, 235, 236
edit a buffer, 237
frames, 238
help, 239, 240
info, 241, 242
kill a buffer, 243
mark, 244
Meta key, M-, 245
minibuffer, 246
minibuffer, M-x, 247
modes, 248
auto fill, 249
C, 250
font lock (coloring), 251
Fortran, 252
overwrite, 253
point, 254
read a file, 255, 256, 257
recover a buffer, 258
recover file, 259
region, 260
replace, 261
save a buffer, 262, 263, 264
search, 265
spelling, 266
undo, 267, 268
window, split, 269, 270
windows, 271, 272
energy spectrum, 273
entropy, 274, 275
ergodicity, 276, 277
error
binning, 278
blocking, 279
bootstrap, 280, 281, 282
binning, 283
error of the mean, 284
integration, 285
jackknife, 286, 287
statistical, 288, 289
systematic, 290
estimator, 291, 292
Euler method, 293
Euler-Verlet method, 294, 295
expectation value, 296, 297, 298, 299
Feigenbaum constant, 300
FIFO, 301
file
owner, 302
permissions, 303
filename completion, 304
filesystem, 305
finite size effects, 306
finite size scaling, 307
first order phase transition, 308
fit, 309, 310
dof, 311
variance of residuals, 312
fluctuations, 313
focus,foci, 314
foreach, 315, 316
Fortran
RANDOM_SEED, 317
RANLUX, 318, 319
allocatable, 320, 321
ALLOCATE, 322, 323, 324
ALLOCATED, 325
arrays, 326
bound upper/lower, 327
conformable, 328, 329
constructor, 330
dimension, 331, 332
DOT_PRODUCT, 333
extent, 334
LBOUND, 335
MATMUL, 336
MAXVAL, 337
MINVAL, 338
PRODUCT, 339
range, 340
rank, 341
RESHAPE, 342
scalar, 343
section, 344
shape, 345, 346
size, 347
SUM, 348
TRANSPOSE, 349
UBOUND, 350
vector operations, 351
call, 352
CMPLX, 353
column limits, 354
comments, 355
common blocks, 356, 357
comparison operators, 358
compile, 359
complex, 360
CONJG, 361
CONTAINS, 362
continuation of lines, 363
DATE_AND_TIME, 364
DBLE, 365
DEALLOCATE, 366
dimension, 367
do, 368
do while, 369, 370
DOT_PRODUCT, 371
DSYEV, 372
elemental, 373
EPSILON, 374, 375
FDATE, 376
FILE, 377
FORALL, 378
format, 379, 380
function, 381
GETARG, 382, 383
GETENV, 384
GETLOG, 385
GETOPT, 386, 387, 388
Hello World!, 389
HUGE, 390, 391
IARGC, 392
IF, 393, 394, 395
implicit, 396, 397
implied do loops, 398
INT, 399
INTERFACE, 400
intrinsic functions, 401
labeled statement, 402, 403
main program, 404
MATMUL, 405
MAXVAL, 406
MINVAL, 407
MOD, 408
module, 409, 410, 411
OPEN, 412, 413
options, 414
PACK, 415
parameter, 416
PRECISION, 417
PRODUCT, 418
RANDOM_NUMBER, 419, 420
RANDOM_SEED, 421
RANGE, 422
READ, 423
END (end of file), 424
real, 425
real, accuracy, 426
RENAME, 427
RESHAPE, 428
rksuite, 429
SAVE, 430, 431, 432
scalar, 433
SELECT, 434, 435, 436
SHAPE, 437
SIZE, 438
SLEEP, 439
STOP, 440, 441
string comparison, 442
subroutine, 443
SUM, 444, 445, 446
TINY, 447, 448
TRANSPOSE, 449
TRIM, 450, 451, 452
UBOUND, 453
unit, 454, 455
WHERE, 456
WRITE, 457
free energy, 458
Gauss map, 459
Gauss’s law, 460
Gauss-Seidel overrelaxation, 461
Gaussian distribution, 462
Gibbs, 463
Gnuplot, 464
¡, 465, 466, 467, 468
1/0 (undefined number), 469
animation, 470, 471
atan2, 472
comment, 473
fit, 474, 475, 476
functions, 477
hidden3d, 478
load, 479, 480
log plots, 481
parametric plot, 482
plot, 483
plot 3d, 484, 485, 486, 487
plot command output, 488, 489, 490, 491
plot expressions, 492, 493, 494
pm3d, 495
replot, 496, 497
reread, 498
save plots, 499
splot, 500, 501, 502, 503
using, 504, 505, 506
variables, 507, 508
with, 509
ground state, 510, 511, 512
ground state energy, 513
Hénon map, 514
hard sphere, 515
harmonic oscillator, 516, 517
heat conduction, 518
heat reservoir, 519, 520
Heisenberg’s uncertainty principle, 521
Heisenberg’s uncertainty relation, 522, 523
helical boundary conditions, 524, 525
high temperature phase, 526
histogram, 527
home directory, 528, 529
hot state, 530, 531
hyperscaling, 532
hyperscaling relations, 533, 534, 535
impact parameter, 536, 537
importance sampling, 538
independent measurement, 539, 540
initial state, 541
internal energy, 542
Ising model
symmetry, 543, 544
, 545
energy, 546
ferromagnetic, 547
Hamiltonian, 548, 549
magnetization, 550
partition function, 551, 552
jackknife, 553, 554
Jacobi overrelaxation, 555
Kepler’s law, 556
Lapack, 557
Laplace equation, 558
lattice
constant, 559, 560
triangular, 561
leapfrog method, 562
Lennard-Jones potential, 563
Liapunov exponent, 564
libblas, 565
liblapack, 566
LIFO, 567
linear coupling, 568
logistic map, 569
cycles, 570
attractor, 571
bifurcation, 572, 573, 574
cobweb plot, 575
entropy, 576
fixed points, 577
stability, 578
onset of chaos, 579, 580
special solutions, 581
strong chaos, 582
transient behavior, 583
weak chaos, 584
low temperature phase, 585
magnetic susceptibility, 586, 587
scaling, 588, 589
magnetization, 590, 591
scaling, 592, 593
staggered, 594
magnetized, 595
Makefile, 596
man pages, 597
Markov chain, 598
Markov process, 599
Marsaglia and Zaman, 600
master equation, 601
memory
allocation, dynamic, 602
allocation, static, 603
leak, 604
Metropolis algorithm, 605
minibuffer, 606
Monte Carlo
cold state, 607, 608
hot state, 609, 610
initial state, 611, 612
simulation, 613, 614
sweep, 615, 616, 617, 618
mouse map, 619
Netlib, 620, 621
Blas, 622
Lapack, 623
DSYEV, 624
liblapack, 625
rksuite, 626
Newton’s law of gravity, 627
Newton-Raphson method, 628, 629
NRRW (non reversal random walker), 630
numerical
derivative, 631, 632, 633
integration, 634
observable, 635
Onsager, 636
exponents, 637
Onsager critical exponents, 638
options, 639, 640
order parameter, 641
ordered phase, 642
overflow, 643
overlap, 644
overrelaxation, 645, 646
parent directory, 647
parity, 648
partition function, 649
Ising model, 650
path
absolute, 651
command, 652
file, 653
relative, 654
period, 655, 656
period doubling, 657
periodic boundary conditions, 658, 659
phase transition
1st order, 660, 661
2nd order, 662
continuous, 663
piping, 664
Poincaré diagram, 665
Poisson equation, 666
pseudocritical region, 667, 668
pseudocritical temperature, 669
pseudorandom, 670
queue, 671
random
RANLUX, 672, 673
RLUXGO, 674
RLUXIN, 675
RLUXUT, 676
drandom(), 677
gaussran(), 678
naiveran(), 679
Cauchy distribution, 680
chaos, 681
correlations, 682
Gaussian distribution, 683
generator, 684
Marsaglia and Zaman, 685
modulo generator, 686
multiply-with-carry, 687
non uniform, 688
period, 689, 690, 691
pseudorandom, 692
Ranlux, 693
save state, 694, 695
Schrage, 696
seed, 697, 698
uniform, 699
urandom, 700, 701, 702
random walk, 703, 704
NRRW, 705
SAW, 706
Ranlux, 707
RANLUX, 708, 709
RLUXGO, 710, 711
RLUXIN, 712, 713
RLUXUT, 714, 715
ranlux_level, 716, 717
save state, 718
redirection, 719
relative path, 720
relativity
special, 721
reservoir, heat, 722
return probability, 723
rksuite, 724
root, 725
Runge-Kutta method, 726, 727, 728, 729, 730
adaptive stepsize, 731
Rutherford scattering, 732
RW (random walker), 733
sample, 734
sampling, 735
importance, 736
simple, 737
SAW (self avoiding walk), 738
scale invariance, 739
scaling, 740
collapse, 741
exponents, 742
factor, 743
hypothesis, 744, 745
scattering, 746, 747
rate, 748
Rutherford, 749
Schrödinger equation, 750
Schrage, 751
second order phase transition, 752
seed, 753, 754
seed of cluster, 755
selection probability, 756, 757
shell
argv, 758
array variable, 759
arrays, 760
command substitution, 761
foreach, 762
here document, 763, 764
if, 765
input $¡, 766
script, 767, 768
set, 769
tcsh, 770
variable, 771
shell script, 772
Simpson’s rule, 773
sine map, 774
SOR, successive overrelaxation, 775, 776
specific heat, 777, 778
scaling, 779, 780
spectral dimension, 781
spin
configuration, 782
spin cluster, 783, 784
splinter, 785
stack, 786
staggered, 787
standard deviation, 788
standard error, 789
standard input, 790
standard output, 791
statistical physics, 792
subdirectory, 793
successive overrelaxation, 794
susceptibility, magnetic, 795
sweep, 796, 797, 798, 799
symmetry breaking, 800
tcsh, 801
temperature, 802
tent map, 803
thermal conductivity, 804
thermal diffusivity, 805
thermalization, 806
discard, 807
time, 808
thermodynamic limit, 809
third law of thermodynamics, 810
timing jobs, 811
Tinkerbell map, 812
toroidal boundary conditions, 813, 814
transient behavior, 815, 816
transition probability, 817, 818, 819
transition rates, 820
tunneling, 821, 822
turning point, 823
universality, 824, 825, 826
class, 827
universality class, 828
user interface, 829
variables
environment, 830
shell, 831
variance, 832
wave function, 833
weights, statistical, 834
Wolff cluster algorithm, 835, 836
working directory, 837