Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
Qubic spline interpolation
Slides from FYS4410 Lectures
Morten Hjorth-Jensen
1Department of Physics and Center of Mathematics for ApplicationsUniversity of Oslo N-0316 Oslo Norway
Spring 2008
Computational Physics II FYS4410
Qubic spline interpolation
Topics for Week 3 January 14-18
Introduction Parallelization MPI and Hartree-Fock theory
Thursday
Introduction to Message Passing Interface (MPI) andparallelization
How to use the local cluster and the Titan cluster
Presentation of topics to be covered in part I (MHJ)
Friday
Hartree-Fock theory and presentation of project 1
Computational Physics II FYS4410
Qubic spline interpolation
Part I 17 January - 14 March
Course overview Part I Computational aspects
Parallelization (MPI) high-performance computing topicsand object orientation Choose between F95 andor C++as programming languages Python also possible asprogramming language
Algorithms for Monte Carlo Simulations (multidimensionalintegrals) Metropolis-Hastings and importance samplingalgorithms Improved Monte Carlo methods
Statistical analysis of data from Monte Carlo calculationsblocking and Jack-Knife methods
Solutions of coupled differential equations for Hartree-Fockand density functional calculations
Parameterizations of statistical data via the Singular-valuedecomposition algorithm
Project 1 deadline April 2Computational Physics II FYS4410
Qubic spline interpolation
Part I 17 January -14 March
Quantum Mechanical Methods and Systems
1 Hartree-Fock and density functional theories relevant foratomic molecularsolid state materials sciencenanotechnology quantum chemistry and nuclear physics
2 Variational Monte Carlo for rsquoab initiorsquo studies of quantummechanical many-body systems
3 Simulation of atoms like Helium Beryllium and Neon withextensions to solids
4 Aim of project 1 understand how to simulate qauntummechanical systems with many interacting particles Thespecific relevance of project 1 is taylored to the fields ofquantum chemistry materials science nanotechnologysolid state physics and atomic physics The methods wediscuss can however easily be extended to other fields
Computational Physics II FYS4410
Qubic spline interpolation
Lectures and ComputerLab
Lectures Thursday (1415-1600 room FV329) and fridayday (1415-16 room FV311)
Detailed lecture notes all programs presented andprojects can be found at the homepage of the course
Computerlab 16-19 thursday room FV329
Weekly plans and all other information are on the officialwebpage
The thursday lectures will be used to demonstratealgorithms etc while the friday lectures will be more likeclassical blackboard sessions
For the first part (MHJ) chapters 8 9 11 and 16 and 17 ofthe FYS31504150 lecture notes give a good starting pointWe recommend also J M Thijssen text ComputationalPhysics For MPI we recommend Gropp Lusk andSjellumrsquos text and UiO lecture notes on the web
Computational Physics II FYS4410
Qubic spline interpolation
Thijssenrsquos text
J M Thijssenrsquos text
ComputationalPhysics
Chapters 3-6 and 12
see httpwwwtntudelftnltnPeopleStaffThijssencomphybookhtml
Computational Physics II FYS4410
Qubic spline interpolation
MPI text
Gropp Lusk and Sjellum
Using MPI
Chapters 1-5
seehttpmitpressmiteducatalogitemdefaultaspttype=2amptid=10761
Computational Physics II FYS4410
Qubic spline interpolation
Lattice Gas and Cellular Automata text Eirikrsquos part
Rothman and Zaleski
Lattice Gas andCellular Automata
see httpwwwcambridgeorgcataloguecatalogueaspisbn=9780521607605
Computational Physics II FYS4410
Qubic spline interpolation
Selected Texts and lectures on CC++
J J Barton and L R NackmanScientific and Engineering C++ Addison Wesley3rd edition 2000
B Stoustrup The C++ programming language Pearson 1997
H P Langtangen INF-VERK3830httpheimifiuiono˜hplINF-VERK4830
D Yang C++ and Object-oriented Numeric Computing for Scientists andEngineers Springer 2000
More books reviewed at httpwwwaccuorg andhttpwwwcomeaucomputingcombooklist
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo Motivation
Most quantum mechanical problems of interest in eg atomic molecular nuclear andsolid state physics consist of a large number of interacting electrons and ions ornucleons The total number of particles N is usually sufficiently large that an exactsolution cannot be found Typically the expectation value for a chosen hamiltonian for asystem of N particles is
〈H〉 =RdR1dR2 dRNΨlowast(R1R2 RN)H(R1R2 RN)Ψ(R1R2 RN)R
dR1dR2 dRNΨlowast(R1R2 RN)Ψ(R1R2 RN)
an in general intractable problem an in general intractable problem
This integral is actually the starting point in a Variational Monte Carlo calculation
Gaussian quadrature Forget it given 10 particles and 10 mesh points for each
degree of freedom and an ideal 1 Tflops machine (all operations take the same time)
how long will it ta ke to compute the above integral Lifetime of the universe
T asymp 47times 1017s
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo
As an example from the nuclear many-body problem we have Schrodingerrsquos equationas a differential equation
HΨ(r1 rA α1 αA) = EΨ(r1 rA α1 αA)
wherer1 rA
are the coordinates andα1 αA
are sets of relevant quantum numbers such as spin and isospin for a system of A
nucleons (A = N + Z N being the number of neutrons and Z the number of protons)
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo
There are
2A timesbdquo
AZ
laquocoupled second-order differential equations in 3A dimensionsFor a nucleus like 10Be this number is 215040 This is a truely challenging many-bodyproblem
Methods like partial differential equations can at most be used for 2-3 particles
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Many-particle(body) Methods
1 Monte-Carlo methods
2 Renormalization group (RG) methods in particular density matrix RG
3 Large-scale diagonalization (Iterative methods Lanczorsquos methoddimensionalities 1010 states)
4 Coupled cluster theory favoured method in quantum chemistry molecular andatomic physics Applications to ab initio calculations in nuclear physics as well forlarge nuclei
5 Perturbative many-body methods
6 Greenrsquos function methods
7 Density functional theoryMean-field theory and Hartree-Fock theory
The physics of the system hints at which many-body methods to use For systems with
strong correlations among the constituents item 5 and 7 are ruled out
Computational Physics II FYS4410
Qubic spline interpolation
Pros and Cons of Monte Carlo
Is physically intuitive
Allows one to study systems with many degrees of freedom Diffusion MonteCarlo (DMC) and Greenrsquos function Monte Carlo (GFMC) yield in principle theexact solution to Schrodingerrsquos equation
Variational Monte Carlo (VMC) is easy to implement but needs a reliable trialwave function can be difficult to obtain This is where we will use Hartree-Focktheory to construct an optimal basis
DMCGFMC for fermions (spin with half-integer values electrons baryonsneutrinos quarks) has a sign problem Nature prefers an anti-symmetric wavefunction PDF in this case given distribution of random walkers (p ge 0)
The solution has a statistical error which can be large
There is a limit for how large systems one can study DMC needs a huge numberof random walkers in order to achieve stable results
Obtain only the lowest-lying states with a given symmetry Difficult to get excitedstates
Computational Physics II FYS4410
Qubic spline interpolation
Where and why do we use Monte Carlo Methods inQuantum Physics
Quantum systems with many particles at finite temperature Path Integral MonteCarlo with applications to dense matter and quantum liquids (phase transitionsfrom normal fluid to superfluid) Strong correlations
Bose-Einstein condensation of dilute gases method transition from non-linearPDE to Diffusion Monte Carlo as density increases
Light atoms molecules solids and nuclei
Lattice Quantum-Chromo Dynamics Impossible to solve without MCcalculations
Simulations of systems in solid state physics from semiconductors to spinsystems Many electrons active and possibly strong correlations
Computational Physics II FYS4410
Qubic spline interpolation
Bose-Einstein Condensation of atoms thousands ofAtoms in one State Project 2 in 2007
Computational Physics II FYS4410
Qubic spline interpolation
Going Parallel with MPI
Task parallelism the work of a global problem can be dividedinto a number of independent tasks which rarely need tosynchronize Monte Carlo simulation or integrations areexamples of this It is almost embarrassingly trivial to parallelizeMonte Carlo codesMPI is a message-passing library where all the routines havecorresponding CC++-binding
MPI_Command_name
and Fortran-binding (routine names are in uppercase but canalso be in lower case)
MPI_COMMAND_NAME
Computational Physics II FYS4410
Qubic spline interpolation
What is Message Passing Interface (MPI) Yetanother library
MPI is a library not a language It specifies the names callingsequences and results of functions or subroutines to be calledfrom C or Fortran programs and the classes and methods thatmake up the MPI C++ library The programs that users write inFortran C or C++ are compiled with ordinary compilers andlinked with the MPI libraryMPI is a specification not a particular implementation MPIprograms should be able to run on all possible machines andrun all MPI implementetations without changeAn MPI computation is a collection of processescommunicating with messagesSee chapter 75 of lecture notes for more details
Computational Physics II FYS4410
Qubic spline interpolation
MPI
MPI is a library specification for the message passing interfaceproposed as a standard
independent of hardware
not a language or compiler specification
not a specific implementation or product
A message passing standard for portability and ease-of-useDesigned for high performanceInsert communication and synchronization functions wherenecessary
Computational Physics II FYS4410
Qubic spline interpolation
Demands from the HPC community
In the field of scientific computing there is an ever-lasting wishto do larger simulations using shorter computer timeDevelopment of the capacity for single-processor computerscan hardly keep up with the pace of scientific computing
processor speed
memory sizespeed
Solution parallel computing
Computational Physics II FYS4410
Qubic spline interpolation
The basic ideas of parallel computing
Pursuit of shorter computation time and larger simulationsize gives rise to parallel computing
Multiple processors are involved to solve a global problem
The essence is to divide the entire computation evenlyamong collaborative processors Divide and conquer
Computational Physics II FYS4410
Qubic spline interpolation
A rough classification of hardware models
Conventional single-processor computers can be calledSISD (single-instruction-single-data) machines
SIMD (single-instruction-multiple-data) machinesincorporate the idea of parallel processing which use alarge number of process- ing units to execute the sameinstruction on different data
Modern parallel computers are so-called MIMD(multiple-instruction- multiple-data) machines and canexecute different instruction streams in parallel on differentdata
Computational Physics II FYS4410
Qubic spline interpolation
Shared memory and distributed memory
One way of categorizing modern parallel computers is tolook at the memory configuration
In shared memory systems the CPUs share the sameaddress space Any CPU can access any data in theglobal memory
In distributed memory systems each CPU has its ownmemory The CPUs are connected by some network andmay exchange messages
A recent trend is ccNUMA(cache-coherent-non-uniform-memory- access) systemswhich are clusters of SMP (symmetric multi-processing)machines and have a virtual shared memory
Computational Physics II FYS4410
Qubic spline interpolation
Different parallel programming paradigms
Task parallelism the work of a global problem can bedivided into a number of independent tasks which rarelyneed to synchronize Monte Carlo simulation is oneexample Integration is another However this paradigm isof limited use
Data parallelism use of multiple threads (eg one threadper processor) to dissect loops over arrays etc Thisparadigm requires a single memory address spaceCommunication and synchronization between processorsare often hidden thus easy to program However the usersurrenders much control to a specialized compilerExamples of data parallelism are compiler-basedparallelization and OpenMP directives
Computational Physics II FYS4410
Qubic spline interpolation
Different parallel programming paradigms
Message-passing all involved processors have anindependent memory address space The user isresponsible for partition- ing the datawork of a globalproblem and distributing the subproblems to theprocessors Collaboration between processors is achievedby explicit message passing which is used for datatransfer plus synchronization
This paradigm is the most general one where the user hasfull control Better parallel efficiency is usually achieved byexplicit message passing However message-passingprogramming is more difficult
Computational Physics II FYS4410
Qubic spline interpolation
SPMD
Although message-passing programming supports MIMD itsuffices with an SPMD (single-program-multiple-data) modelwhich is flexible enough for practical cases
Same executable for all the processors
Each processor works primarily with its assigned localdata
Progression of code is allowed to differ betweensynchronization points
Possible to have a masterslave model The standardoption in Monte Carlo calculations and numericalintegration
Computational Physics II FYS4410
Qubic spline interpolation
Todayrsquos situation of parallel computing
Distributed memory is the dominant hardwareconfiguration There is a large diversity in these machinesfrom MPP (massively parallel pro cessing) systems toclusters of off-the-shelf PCs which are very cost-effective
Message-passing is a mature programming paradigm andwidely accepted It often provides an efficient match to thehardware It is primarily for the distributed memorysystems but can also be used on shared memory systems
In these lectures we consider only message-passing for writingparallel programs
Computational Physics II FYS4410
Qubic spline interpolation
Some useful concepts
Speed-up
S(P) =T (1)
T (P)
T (1) is the time for one processor while T (P) is the timefor P processors
Efficiency
η(P) =S(P)
PLatency and bandwidth ndash the cost model of sending amessage of length L between two processors
tC(L) = τ + βL
Computational Physics II FYS4410
Qubic spline interpolation
Overhead present in parallel computing
Uneven load balance not all the processors can performuseful work at all time
Overhead of synchronization
Overhead of communication
Extra computation due to parallelization
Due to the above overhead and that certain part of a sequentialalgorithm cannot be parallelized
T (P) ge T (1)
Prarr S(P) le P
However superlinear speed-up (S(P) gt P) may sometimesoccur due to for example cache effects
Computational Physics II FYS4410
Qubic spline interpolation
Parallelizing a sequential algorithm
Identify the part(s) of a sequential algorithm that can beexecuted in parallel This is the difficult part
Distribute the global work and data among P processors
Computational Physics II FYS4410
Qubic spline interpolation
Process and processor
We refer to process as a logical unit which executes itsown code in an MIMD style
The processor is a physical device on which one or severalprocesses are executed
The MPI standard uses the concept process consistentlythroughout its documentation
However we only consider situations where one processoris responsible for one process and therefore use the twoterms interchangeably
Computational Physics II FYS4410
Qubic spline interpolation
Bindings to MPI routines
MPI is a message-passing library where all the routines havecorresponding CC++-binding
MPI_Command_name
and Fortran-binding (routine names are in uppercase but canalso be in lower case)
MPI_COMMAND_NAME
The discussion in these slides has its focus on the C++ binding
Computational Physics II FYS4410
Qubic spline interpolation
Communicator
A group of MPI processes with a name (context)
Any process is identified by its rank The rank is onlymeaningful within a particular communicator
By default communicator MPI COMM WORLD contains allthe MPI processes
Mechanism to identify subset of processes
Promotes modular design of parallel libraries
Computational Physics II FYS4410
Qubic spline interpolation
The 6 most important MPI routines
MPI Init - initiate an MPI computation
MPI Finalize - terminate the MPI computation and clean up
MPI Comm size - how many processes participate in agiven MPI communicator
MPI Comm rank - which one am I (A number between 0and size-1)
MPI Send - send a message to a particular pro cess withinan MPI communicator
MPI Recv - receive a message from a particular pro cesswithin an MPI communicator
Computational Physics II FYS4410
Qubic spline interpolation
The first MPI CC++ program
Let every process write rdquoHello worldrdquo on the standard output This is example2cpp
using namespace stdinclude ltmpihgtinclude ltiostreamgtint main (int nargs char args[])int numprocs my_rank MPI initializationsMPI_Init (ampnargs ampargs)MPI_Comm_size (MPI_COMM_WORLD ampnumprocs)MPI_Comm_rank (MPI_COMM_WORLD ampmy_rank)cout ltlt Hello world I have rank ltlt my_rank ltlt out of
ltlt numprocs ltlt endl End MPIMPI_Finalize ()
Computational Physics II FYS4410
Qubic spline interpolation
The Fortran program
PROGRAM helloINCLUDE mpifhINTEGER size my_rank ierr
CALL MPI_INIT(ierr)CALL MPI_COMM_SIZE(MPI_COMM_WORLD size ierr)CALL MPI_COMM_RANK(MPI_COMM_WORLD my_rank ierr)WRITE()Hello world Irsquove rank my_rank out of sizeCALL MPI_FINALIZE(ierr)
END PROGRAM hello
Computational Physics II FYS4410
Qubic spline interpolation
Note 1
The output to screen is not ordered since all processes aretrying to write to screen simultaneously It is then the operatingsystem which opts for an ordering If we wish to have anorganized output starting from the first process we may rewriteour program as in the next example (example3cpp) see againchapter 75 of lecture notes
Computational Physics II FYS4410
Qubic spline interpolation
Ordered output with MPI Barrier
int main (int nargs char args[])
int numprocs my_rank iMPI_Init (ampnargs ampargs)MPI_Comm_size (MPI_COMM_WORLD ampnumprocs)MPI_Comm_rank (MPI_COMM_WORLD ampmy_rank)for (i = 0 i lt numprocs i++) MPI_Barrier (MPI_COMM_WORLD)if (i == my_rank) cout ltlt Hello world I have rank ltlt my_rank ltlt
out of ltlt numprocs ltlt endlMPI_Finalize ()
Computational Physics II FYS4410
Qubic spline interpolation
Note 2
Here we have used the MPI Barrier function to ensure that thatevery process has completed its set of instructions in aparticular order A barrier is a special collective operation thatdoes not allow the processes to continue until all processes inthe communicator (here MPI COMM WORLD have calledMPI Barrier The barriers make sure that all processes havereached the same point in the code Many of the collectiveoperations like MPI ALLREDUCE to be discussed later havethe same property viz no process can exit the operation untilall processes have started However this is slightly moretime-consuming since the processes synchronize betweenthemselves as many times as there are processes In the nextHello world example we use the send and receive functions inorder to a have a synchronized action
Computational Physics II FYS4410
Qubic spline interpolation
Ordered output with MPI Recv and MPI Send
int numprocs my_rank flagMPI_Status statusMPI_Init (ampnargs ampargs)MPI_Comm_size (MPI_COMM_WORLD ampnumprocs)MPI_Comm_rank (MPI_COMM_WORLD ampmy_rank)if (my_rank gt 0)MPI_Recv (ampflag 1 MPI_INT my_rank-1 100
MPI_COMM_WORLD ampstatus)cout ltlt Hello world I have rank ltlt my_rank ltlt out of ltlt numprocs ltlt endlif (my_rank lt numprocs-1)MPI_Send (ampmy_rank 1 MPI_INT my_rank+1
100 MPI_COMM_WORLD)MPI_Finalize ()
Computational Physics II FYS4410
Qubic spline interpolation
Note 3
The basic sending of messages is given by the function MPI SEND which in CC++ isdefined as
int MPI_Send(void buf int count MPI_Datatype datatypeint dest int tag MPI_Comm comm)
This single command allows the passing of any kind of variable even a large array to
any group of tasks The variable buf is the variable we wish to send while count is the
number of variables we are passing If we are passing only a single value this should
be 1 If we transfer an array it is the overall size of the array For example if we want to
send a 10 by 10 array count would be 10times 10 = 100 since we are actually passing
100 values
Computational Physics II FYS4410
Qubic spline interpolation
Note 4
Once you have sent a message you must receive it on another task The functionMPI RECV is similar to the send call
int MPI_Recv( void buf int count MPI_Datatype datatype int sourceint tag MPI_Comm comm MPI_Status status )
The arguments that are different from those in MPI SEND are buf which is the name ofthe variable where you will be storing the received data source which replaces thedestination in the send command This is the return ID of the senderFinally we have used MPI Status status where one can check if the receive wascompletedThe output of this code is the same as the previous example but now process 0 sendsa message to process 1 which forwards it further to process 2 and so forth
Armed with this wisdom performed all hello world greetings we are now ready for
serious work
Computational Physics II FYS4410
Qubic spline interpolation
Integrating π
Examples
Go to the webpageand click on theprograms link
Go to MPI examples
Load downexample5ccp andexample6cpp(Fortran version alsoavailable)
These codescompute π using therectangular andtrapezoidal rules
Computational Physics II FYS4410
Qubic spline interpolation
Integration algos
The trapezoidal rule (example6cpp)
I =
Z b
af (x)dx = h (f (a)2 + f (a + h) + f (a + 2h) + middot middot middot+ f (b minus h) + fb2)
Another very simple approach is the so-called midpoint or rectangle method In thiscase the integration area is split in a given number of rectangles with length h andheigth given by the mid-point value of the function This gives the following simple rulefor approximating an integral
I =
Z b
af (x)dx asymp h
NXi=1
f (ximinus12)
where f (ximinus12) is the midpoint value of f for a given rectangle This is used in
example5cpp
Computational Physics II FYS4410
Qubic spline interpolation
Message Passing Interface (MPI) and the local cluster
Basic info for CC++ users
The basic instructions for using the local cluster are athttpfolkuionojonknigvdworkdocmpich2-fysmpich2-fyshtml Follow these instructionsin detail The file rsquomachinesrsquo can be pulled down from MHJrsquospart under the webpage of the course see programs and MPIprograms
Use the example code example5cppcompile and load withmpicxx -O2 -o example5x example5cpprun with 4 nodesmpiexec -np 4 example5x
Computational Physics II FYS4410
Qubic spline interpolation
Message Passing Interface (MPI) and the local cluster
Basic info for Fortran95 usersUse the example code calc_pif90compile and load withmpif90 -O2 -o calcpiexe calc_pif90run with 4 nodesmpirun -np 4 calpiexe
Computational Physics II FYS4410
Qubic spline interpolation
Dissection of example 5
1 Reactangle rule and numerical integration2 using namespace std3 include ltmpihgt4 include ltiostreamgt
5 int main (int nargs char args[])6 7 int numprocs my_rank i n = 10008 double local_sum rectangle_sum x h9 MPI initializations10 MPI_Init (ampnargs ampargs)11 MPI_Comm_size (MPI_COMM_WORLD ampnumprocs)12 MPI_Comm_rank (MPI_COMM_WORLD ampmy_rank)
Computational Physics II FYS4410
Qubic spline interpolation
Dissection of example 5
After the standard initializations with MPI such as MPI Init MPI Comm size andMPI Comm rank MPI COMM WORLD contains now the number of processes definedby using for example
mpirun -np 10 progx
In line 4 we check if we have read in from screen the number of mesh points n Notethat in line 7 we fix n = 1000 however we have the possibility to run the code with adifferent number of mesh points as well If my rank equals zero which correponds tothe master node then we read a new value of n if the number of arguments is largerthan two This can be done as follows when we run the code
mpiexec -np 10 progx 10000
Computational Physics II FYS4410
Qubic spline interpolation
Dissection of example 5
13 Read from screen a possible new vaue of n14 if (my_rank == 0 ampamp nargs gt 1) 15 n = atoi(args[1])16 17 h = 10n18 Broadcast n and h to all processes19 MPI_Bcast (ampn 1 MPI_INT 0 MPI_COMM_WORLD)20 MPI_Bcast (amph 1 MPI_DOUBLE 0 MPI_COMM_WORLD)21 Every process sets up its contribution to the integral22 local_sum = 023 for (i = my_rank i lt n i += numprocs) 24 x = (i+05)h25 local_sum += 40(10+xx)26 27 local_sum = h
In line 17 we define also the step length h In lines 19 and 20 we use the broadcast
function MPI Bcast We use this particular function because we want data on one
processor (our master node) to be shared with all other processors The broadcast
function sends data to a group of processes
Computational Physics II FYS4410
Qubic spline interpolation
Dissection of example 5
The MPI routine MPI Bcast transfers data from one task to a group of others Theformat for the call is in C++ given by the parameters of
MPI_Bcast (ampn 1 MPI_INT 0 MPI_COMM_WORLD)MPI_Bcast (amph 1 MPI_DOUBLE 0 MPI_COMM_WORLD)
in a case of a double The general structure of this function is
MPI_Bcast( void buf int count MPI_Datatype datatype int root MPI_Comm comm)
All processes call this function both the process sending the data (with rank zero) andall the other processes in MPI COMM WORLD Every process has now copies of nand h the number of mesh points and the step length respectively
We transfer the addresses of n and h The second argument represents the number of
data sent In case of a one-dimensional array one needs to transfer the number of
array elements If you have an n timesm matrix you must transfer n timesm We need also to
specify whether the variable type we transfer is a non-numerical such as a logical or
character variable or numerical of the integer real or complex type
Computational Physics II FYS4410
Qubic spline interpolation
Dissection of example 5
28 if (my_rank == 0) 29 MPI_Status status30 rectangle_sum = local_sum31 for (i=1 i lt numprocs i++) 32 MPI_Recv(amplocal_sum1MPI_DOUBLEMPI_ANY_SOURCE500
MPI_COMM_WORLDampstatus)33 rectangle_sum += local_sum34 35 cout ltlt Result ltlt rectangle_sum ltlt endl36 else37 MPI_Send(amplocal_sum1MPI_DOUBLE0500MPI_COMM_WORLD)38 End MPI39 MPI_Finalize ()40 return 041
Computational Physics II FYS4410
Qubic spline interpolation
Dissection of example 5
In lines 23-27 every process sums its own part of the final sum used by the rectanglerule The receive statement collects the sums from all other processes in casemy rank == 0 else an MPI send is performed If we are not the master node wesend the results else they are received and the local results are added to final sumThe above can be rewritten using the MPI allreduce as discussed in the next example
The above function is not very elegant Furthermore the MPI instructions can be
simplified by using the functions MPI Reduce or MPI Allreduce The first function takes
information from all processes and sends the result of the MPI operation to one process
only typically the master node If we use MPI Allreduce the result is sent back to all
processes a feature which is useful when all nodes need the value of a joint operation
We limit ourselves to MPI Reduce since it is only one process which will print out the
final number of our calculation The arguments to MPI Allreduce are the same
Computational Physics II FYS4410
Qubic spline interpolation
MPI reduce
Call as
MPI_reduce( void senddata void resultdata int countMPI_Datatype datatype MPI_Op int root MPI_Comm comm)
The two variables senddata and resultdata are obvious besides the fact that onesends the address of the variable or the first element of an array If they are arrays theyneed to have the same size The variable count represents the total dimensionality 1in case of just one variable while MPI Datatype defines the type of variable which issent and receivedThe new feature is MPI Op It defines the type of operation we want to do In our casesince we are summing the rectangle contributions from every process we defineMPI Op = MPI SUM If we have an array or matrix we can search for the largest ogsmallest element by sending either MPI MAX or MPI MIN If we want the location aswell (which array element) we simply transfer MPI MAXLOC or MPI MINOC If we wantthe product we write MPI PRODMPI Allreduce is defined as
MPI_Alreduce( void senddata void resultdata int countMPI_Datatype datatype MPI_Op MPI_Comm comm)
Computational Physics II FYS4410
Qubic spline interpolation
Dissection of example 6
Trapezoidal rule and numerical integration usign MPI example 6using namespace stdinclude ltmpihgtinclude ltiostreamgt
Here we define various functions called by the main program
double int_function(double )double trapezoidal_rule(double double int double ()(double))
Main function begins hereint main (int nargs char args[])
int n local_n numprocs my_rankdouble a b h local_a local_b total_sum local_sumdouble time_start time_end total_time
Computational Physics II FYS4410
Qubic spline interpolation
Dissection of example 6
MPI initializationsMPI_Init (ampnargs ampargs)MPI_Comm_size (MPI_COMM_WORLD ampnumprocs)MPI_Comm_rank (MPI_COMM_WORLD ampmy_rank)time_start = MPI_Wtime() Fixed values for a b and na = 00 b = 10 n = 1000h = (b-a)n h is the same for all processeslocal_n = nnumprocs make sure n gt numprocs else integer division gives zero Length of each processrsquo interval of integration = local_nhlocal_a = a + my_ranklocal_nhlocal_b = local_a + local_nh
Computational Physics II FYS4410
Qubic spline interpolation
Dissection of example 6
total_sum = 00local_sum = trapezoidal_rule(local_a local_b local_n
ampint_function)MPI_Reduce(amplocal_sum amptotal_sum 1 MPI_DOUBLE
MPI_SUM 0 MPI_COMM_WORLD)time_end = MPI_Wtime()total_time = time_end-time_startif ( my_rank == 0)
cout ltlt Trapezoidal rule = ltlt total_sum ltlt endlcout ltlt Time = ltlt total_time
ltlt on number of processors ltlt numprocs ltlt endl End MPIMPI_Finalize ()return 0
end of main program
We use MPI reduce to collect data from each process Note also the use of the
function MPI Wtime
Computational Physics II FYS4410
Qubic spline interpolation
Dissection of example 6
this function defines the function to integratedouble int_function(double x)
double value = 4(1+xx)return value
end of function to evaluate
Computational Physics II FYS4410
Qubic spline interpolation
Dissection of example 6
Implementation of the trapezoidal rule
this function defines the trapezoidal ruledouble trapezoidal_rule(double a double b int n
double (func)(double))
double trapez_sumdouble fa fb x stepint jstep=(b-a)((double) n)fa=(func)(a)2 fb=(func)(b)2 trapez_sum=0for (j=1 j lt= n-1 j++)
x=jstep+atrapez_sum+=(func)(x)
trapez_sum=(trapez_sum+fb+fa)stepreturn trapez_sum
end trapezoidal_rule
Computational Physics II FYS4410
Qubic spline interpolation
Strategies
Develop codes locally run with some few processes andtest your codes Do benchmarking timing and so forth onlocal nodes for example your laptop You can installMPICH2 on your laptop (most new laptos come with dualcores) or run locally on the clusters here
When you are convinced that your codes run correctly youstart your production runs on available supercomputers inour case titanuiono see Jon Nilsenrsquos introduction
Computational Physics II FYS4410
Qubic spline interpolation
Topics for Week 4 January 21-25
Hartree-Fock theory and computing topics
Thursday
Algorithm for the Hartree-Fock equations
computation of the single-particle equation homogeneousand non-homogeneous part
Numerovrsquos algorithm and Greenrsquos function
More on parallelization in particular how to write out MonteCarlo data in parallel This is very useful for the statisticalanalysis of the data to be performed later (presented byJon Nilsen)
Friday
Hartree-Fock theory derivation of the Hartree-Fockequations New lecture hall store fys aud
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock equations
The single-particle equation
minus1
2
d2
dr2unl (r) +
bdquol(l + 1)
2r2minus
Z
r+ Φ(r)
laquounl (r) + Fnl = enl unl (r)
is the equation we will discuss today using Numerovrsquos method The function unl is thesolution of the radial part of the Schrodinger equation The total one-particle wavefunction is
ψnlml ms = φnlml(r)ξms (s)
with s the spin (12 for electrons) ms is the spin projection ms = plusmn12 and the spatialpart is
φnlml(r) = Rnl (r)Ylml
(r)
with Y the spherical harmonics and unl = rRnl All results are in atomic units meaningthat the energy is given by enl = minusZ 22n2
The boundary values are unl (0) = unl (infin) = 0 This is a tricky boundary value
problem
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock equations
Lets specialize to the homogeneous case that is we skip Fnl and absorb Φ(r) inV (r) = minusZr + Φ(r)
minus1
2
d2
dr2unl (r) +
bdquol(l + 1)
2r2
laquounl (r) = enl unl (r)
and rewrite as
minus1
2
d2
dr2unl (r) + k2(r)unl (r) = 0
with
k2(r) = V (r) +l(l + 1)
2r2minus enl
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock equations
We can rewrite the equation as and rewrite as the general inhomogeneous equation
d2
dr2unl (r) + k2(r)unl (r) = Snl (r)
with
k2(r) = minus2(V (r) +l(l + 1)
2r2minus enl )
Note that there are restrictions on k2 If k is real then k2 gt 0 in order to have asolution If k2 lt 0 there is no solution unless k is imaginaryThe term Snl is called the source term and will in general cause problems for us Wewill insert this later (next week) Today we specialize on the homogeneous equationThe codes with a simple matching recipe are under programs for project 1 at thewebpage of the course see numerovcpp or numerovf90
Your task today is to modify these codes in order to prepare for the introduction of the
source term which in our case is the Fock term that is Snl (r) = minusFnl
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock equations
The other terms in the Hartree-Fock equation (for particle i note this)
V (ri ) = minusZri
Φ(ri ) =Xnprime lprime
2(2l prime + 1)
Z infin
0|unprime lprime (rj )|2
1
rgtdrj
with rgt = max(ri rj ) This equation with a given guess for unprime lprime can be integrated
numerically with for example gaussian quadrature or Simpsonrsquos method see chapter 7
of lecture notes and the corresponding codes in either libcpp or f90libf90
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock equations
The tricky part is the Fock term Fnl (r)
Fnl (ri ) =Xnprime lprime
l+lprimeXλ=|lminuslprime|
2l prime + 1
2λ+ 1lt ll prime00|λ0 gt2
raquoZ infin
0ulowastnprime lprime (rj )
(rlt)λ
(rgt)λ+1unl (rj )drj
ndashunprime lprime (ri )
with rgt = max(ri rj ) and with rlt = min(ri rj ) This terms is our source term The
object lt ll prime00|λ0 gt is a vector recoupling coefficient called Clebsch-Gordan You will
find the code for C++ users as clebsch-gordancpp with calling examples For f95 users
it is baked into the f90libf90 library all codes are under the Project 1 directory of the
programs catalogue
Computational Physics II FYS4410
Qubic spline interpolation
How do I solve the Hartree-Fock equations
Here follows the algorithm for doing the Hartree-Fock calculations
Set up the system how many electrons there are in the 1s 2s and 2p orbitalsChoose a charge Z and use hydrogenic wave functions as a guess for unl It canbe convenient to make a class or module which identifies the single-particlequantum numbers and the corresponding single-particle energies
Fix a numerical precision for when you wish to stop the iterative process and amaximum number of iterations For every iteration you compute new energies enland compare these with those from the previous iteration and check with yourchosen numerical precision
Computational Physics II FYS4410
Qubic spline interpolation
How do I solve the Hartree-Fock equations
At every iteration you have an input value for enl and unl Compute first Φ(ri ) andFnl (ri ) Solve then the differential equation for unl
Compute then new single-particle energies using the expression
enl =
Z infin
0
raquo1
2(
dunl (r)
dr)2 +
bdquominus
Z
r+ Φ(r) +
l(l + 1)
2r2
laquou2
nl (r)ndash
dr+Z infin
0unl (r)Fnl (r)dr
Use these energies for the next iteration You will also need to compute the totalenergy and compare this at every iteration
Computational Physics II FYS4410
Qubic spline interpolation
How do I solve the Hartree-Fock equations
The expression for the total energy E is
E =X
nl
Nnl
Z infin
0
raquo1
2(
dunl (r)
dr)2 +
bdquominus
Z
r+
1
2Φ(r) +
l(l + 1)
2r2
laquou2
nl (r)ndash
dr + EFock
with
EFock =1
2
Xnl
Nnl
Z infin
0unl (r)Fnl (r)dr
and Nnl are the number of particles in a given orbit with quantum numbers nl
Computational Physics II FYS4410
Qubic spline interpolation
How do I solve the Hartree-Fock equations
The easy part is to integrate the above expressions although it may take some timeThe difficulty resides in solving the general inhomogeneous equation Let us first startwith the homogenous equation
d2
dr2unl (r) + k2(r)unl (r) = 0
with
k2(r) = minus2(V (r) +l(l + 1)
2r2minus enl )
Fnl is the Fock term term and will in general cause problems for us However we donot need to find the energies They are given by our guess This avoids the searchfor both energies and wave functionsHowever we have still a two-point boudary value problem see chapter 14 of lecturenotes
We will first look at the homogeneous case and introduce Numerovrsquos algorithm
Computational Physics II FYS4410
Qubic spline interpolation
Numerovrsquos algo chap 14 in lecture notes
Let us rewrite the inhomogeneous equation as
d2
dx2y(x) + k2(x)y(x) = F (x)
Our discretised second derivative reads at a step xi = a + ih with i = 0 1
y primeprimei =yi+1 + yiminus1 minus 2yi
h2+ O(h2)
leading to a discretised differential equation
yi+1 + yiminus1 minus 2yi
h2+ O(h2) + k2
i yi = Fi
Recall that the fourth-order Runge-Kutta method has a local error of O(h4)Since we want to integrate our equation from x0 = a to xN = b we rewrite it as
yi+1 asymp minusyiminus1 +ldquo
2minus h2k2i
rdquoyi + h2Fi
Computational Physics II FYS4410
Qubic spline interpolation
Numerovrsquos algo
The local truncation error is O(h2) We can obtain O(h6) without too many additionalfunction evaluationsOur equation is a second order differential equation without any first order derivativesLet us also for the sake of simplicity assume that F (x) = 0 Numerovrsquos method isdesigned to solve such an equation numerically achieving a local truncation errorO(h6)We start with the Taylor expansion of the desired solution
y(x + h) = y(x) + hy (1)(x) +h2
2y (2)(x) +
h3
3y (3)(x) +
h4
4y (4)(x) + middot middot middot
Computational Physics II FYS4410
Qubic spline interpolation
Numerovrsquos algo
We have that y (n)(x) is a shorthand notation for the nth derivative dnydxn Becausethe corresponding Taylor expansion of y(x minus h) has odd powers of h appearing withnegative signs all odd powers cancel when we add y(x + h) and y(x minus h)
y(x + h) + y(x minus h) = 2y(x) + h2y (2)(x) +h4
12y (4)(x) + O(h6)
We obtain
y (2)(x) =y(x + h) + y(x minus h)minus 2y(x)
h2minus
h2
12y (4)(x) + O(h4)
Computational Physics II FYS4410
Qubic spline interpolation
Numerovrsquos algo
To eliminate the fourth-derivative term we apply the operator (1 + h2
12d2
dx2 ) on thedifferential equation
y (2)(x) +h2
12y (4)(x) + k2(x)y(x) +
h2
12
d2
dx2
ldquok2(x)y(x)
rdquoasymp 0
Computational Physics II FYS4410
Qubic spline interpolation
Numerovrsquos algo
In this expression the y (4) terms cancel To treat the general x dependence of k2(x)we approximate the second derivative of (k2(x)y(x) by
d2(k2y(x))
dx2asymp`k2(x + h)y(x + h) + k2(x)y(x)
acute+`k2(x minus h)y(x minus h) + k2(x)y(x)
acuteh2
Computational Physics II FYS4410
Qubic spline interpolation
Numerovrsquos algo
We replace then y(x + h) with the shorthand yi+1 etc and obtain a final discretisedalgorithm for obtaining yi+1
yi+1 =2ldquo
1minus 512 h2k2
i
rdquoyi minus
ldquo1 + 1
12 h2k2iminus1
rdquoyiminus1
1 + h2
12 k2i+1
+ O(h6)
where xi = ih ki = k(xi = ih) and yi = y(xi = ih) etc
Computational Physics II FYS4410
Qubic spline interpolation
Numerovrsquos algo
It is easy to add the term Fi since we need only to take the second derivative The finalalgorithm reads then
yi+1 =2ldquo
1minus 512 h2k2
i
rdquoyi minus
ldquo1 + 1
12 h2k2iminus1
rdquoyiminus1
1 + h2
12 k2i+1
+h2
12(Fi+1 + Fiminus1 minus 2Fi ) + O(h6)
This equation is implemented in the codes numerovcpp and numerovf90 Thisequation carries a local truncation error proportional to h6
Computational Physics II FYS4410
Qubic spline interpolation
Boundary Value Problems
Let us return to Schrodingerrsquos equation In the notation we use the shorthand u(r)instead of unl (r)
minus1
2
d2
dr2u(r) +
bdquoV (r) +
l(l + 1)
r2
1
2
laquou(r) = eu(r)
In our case we are interested in attractive potentials
V (r) = minusV0f (r)
where V0 gt 0 and analyze bound states where e lt 0
Computational Physics II FYS4410
Qubic spline interpolation
Boundary Value Problems
The final equation can be written as
d2
dr2u(r) + k2(r)u(r) = 0
where
k2(r) = γ
bdquof (r)minus
1
γ
l(l + 1)
r2minus ε
laquoγ = 2V0
ε =|E |V0
Computational Physics II FYS4410
Qubic spline interpolation
Boundary Value Problems
We specialize to a box potential but the arguments here apply also to a potential whichgoes like minusZr
f (r) =
10
forr le ar gt a
and choose α = a Then
k2(r) = γ
(1minus εminus 1
γl(l+1)
r2
minusεminusminus 1γ
l(l+1)
r2
forr le ar gt a
Computational Physics II FYS4410
Qubic spline interpolation
Boundary Value Problems
For small r we getd2
dr2u(r)minus
l(l + 1)
r2u(r) = 0
with solutions u(r) = r l+1 or u(r) = rminusl Since the final solution must be finiteeverywhere we get the condition for our numerical solution
u(r) = r l+1 for small r
Computational Physics II FYS4410
Qubic spline interpolation
Boundary Value Problems
For large r we getd2
dr2u(r)minus γεu(r) = 0 γ gt 0
with solutions u(r) = exp(plusmnγεr) and the condition for large r means that our numericalsolution must satisfy
u(r) = eminusγεr for large r
Computational Physics II FYS4410
Qubic spline interpolation
Boundary Value Problems
In order to find a bound state we start integrating with a trial negative value for the
energy from small values of the variable r usually zero and up to some large value of
r As long as the potential is significantly different from zero the function oscillates
Outside the range of the potential the function will approach an exponential form If we
have chosen a correct eigenvalue the function decreases exponetially as
u(r) = eminusγεr However due to numerical inaccuracy the solution will contain small
admixtures of the undesireable exponential growing function u(r) = e+γεr
Computational Physics II FYS4410
Qubic spline interpolation
Boundary Value Problems
The final solution will then become unstable Therefore it is better to generate two
solutions with one starting from small values of r and integrate outwards to some
matching point r = rm We call that function ult(r) The next solution ugt(r) is then
obtained by integrating from some large value r where the potential is of no
importance and inwards to the same matching point rm Due to the quantum
mechanical requirements the logarithmic derivative at the matching point rm should be
well defined
Computational Physics II FYS4410
Qubic spline interpolation
Boundary Value Problems
We obtain the following condition
ddr ult(r)
ult(r)=
ddr ugt(r)
ugt(r)at r = rm
We can modify this expression by normalizing the function ultult(rm) = Cugtult(rm)
Computational Physics II FYS4410
Qubic spline interpolation
d
drult(r) =
d
drugt(r) at r = rm
We can calculate the first order derivatives by
d
drult(rm) asymp
ult(rm)minus ult(rm minus h)
hd
drugt(rm) asymp
ugt(rm + h)minus ugt(rm)
h
Thus the criterium for a proper eigenfunction will be
f = ult(rm minus h)minus ugt(rm + h)
which should be smaller than a fixed number
Computational Physics II FYS4410
Qubic spline interpolation
Numerovcpp main part
quick and dirty set up of parametersr_max = 300 orb_l = 0 n = 1 max_step = 1000 charge = 1 step length energy and orbital momentum factorstep = (r_max - r_min) max_steporb_factor = orb_l (orb_l + 1)energy = -10(20nn)
allocate memory for rthe wave function and k
r = new double[max_step+1]k = new double[max_step+1]wf = new double[max_step+1]
Computational Physics II FYS4410
Qubic spline interpolation
Numerovcpp main part
set up r and add the chosen energy to the potential and set up the homogeneous partfor(i = 0 i lt= max_step i++)
r[i] = r_min + i step note stepstep in the definition of k
k[i] = -(orb_factor(r[i]r[i])-2energy-2charger[i])stepstep
calculate the wave function using Numerovrsquos method
wfn(step max_step k wf)
print out wave functionoutput(max_step r wf) delete [] r delete [] wf delete [] kofileclose() close output filereturn 0
Computational Physics II FYS4410
Qubic spline interpolation
Numerovcpp
void wfn(double step int max_step double k double wf)
int loop loop_1matchdouble fac wwf norm
Boundary conditions and value of the wave function at max stepwf[max_step] = 00wf[max_step - 1] = 10E-10 start from right and move towards rminfor(loop = max_step - 2 loop gt 0 loop--)
wf[loop] = (20(10-50k[loop+1]120) wf[loop+1]- (10+k[loop+2]120) wf[loop+2])(10+k[loop]120)
find point were the outer part starts turningin order to determine matching pointif(wf[loop] lt= wf[loop + 1]) break
matching pointmatch = loop + 1wwf = wf[match]
Computational Physics II FYS4410
Qubic spline interpolation
Numerovcpp
start from rmin and move towards the matching point boundary condition and first stepwf[0] = 00wf[1] = 10e-10for(loop = 2 loop lt= match loop++)
wf[loop] = (2(10-50k[loop-1]120) wf[loop-1]- (10+k[loop-2]120) wf[loop-2])(10+k[loop]120)
if(fabs(wf[loop]) gt INFINITY) for(loop_1 = 0 loop_1 lt= loop loop_1++)
wf[loop_1] = INFINITY
Computational Physics II FYS4410
Qubic spline interpolation
Numerovcpp
scale the wave function below the matching pointfac = wwfwf[match]for(loop = 0 loop lt= match loop++) wf[loop] = fac compute the normfor(loop = 0 norm = 00 loop lt= max_step loop++)
norm += wf[loop] wf[loop] stepif(fabs(norm) lt 10e-15)
printf(nnError in norm in function wfn() )exit(1)
norm = 1 sqrt(norm)for(loop = 0 loop lt= max_step loop++)
wf[loop] = norm
Computational Physics II FYS4410
Qubic spline interpolation
Todayrsquos tasks
Get aquainted with numerovcpp try the 2p wave or the 2s wave Try differentnumber of mesh points and length of mesh
Use it so solve the differential equation
d2
dr2u(r)minus a2u(r) = minus
1
2reminusr
with analytic solution
u(r) =
bdquo1
1minus a2
laquo2 bdquoeminusar minus eminusr
raquo1 +
1
2(1minus a2)r
ndashlaquo
with boundary conditions u(0) = u(infin) = 0 for various values of a Test yourcode when you integrate only outward and only inward
Choose different intermediate matching points and see if your results changedramatically
Before you start solving this equation you should clarify which values of a are possible
Recall that it is only the case where minusa2 gt 0 which yields a solution
Computational Physics II FYS4410
Qubic spline interpolation
Topics for Week 5 January 28 - February 1
Hartree-Fock theory and computing topics
Thursday
Continuation from last week Algorithm for theHartree-Fock equations
computation of the single-particle equation homogeneousand non-homogeneous part
Numerovrsquos algorithm and Greenrsquos function Finalization ofcode for solving differential equations with boundaryconditions
Friday
Hartree-Fock theory derivation of the Hartree-Fockequations and begin of discussion of density functionaltheory
Computational Physics II FYS4410
Qubic spline interpolation
Reminder on Greenrsquos functions
Consider the differential equation
minusu(x)primeprime = f (x) x isin (0 1) u(0) = u(1) = 0
and using the fundamental theorem of calculus there is a constant c1 such that
u(x) = c1 +
Z x
0uprime(y)dy
and a constant c2
uprime(y) = c2 +
Z y
0uprimeprime(z)dz
This is true for any twice continuously differentiable function u
Computational Physics II FYS4410
Qubic spline interpolation
Reminder on Greenrsquos functions
If u satisfies the above differential equation we have then
uprime(y) = c2 minusZ y
0f (z)dz
which inserted into the equation for u gives
u(x) = c1 + c2x minusZ x
0
bdquoZ y
0f (z)dz
laquody
and defining
F (y) =
Z y
0f (z)dz
and performing an integration by parts we obtainZ x
0F (y)dy =
Z x
0
bdquoZ y
0f (z)dz
laquody =
Z x
0(x minus y)f (y)dy
Computational Physics II FYS4410
Qubic spline interpolation
Reminder on Greenrsquos functions
This gives us
u(x) = c1 + c2x minusZ x
0(x minus y)f (y)dy
The boundary condition u(0) = 0 yields c1 = 0 and u(1) = 0 results in
c2 =
Z 1
0(1minus y)f (y)dy
meaning that we can write the solution as
u(x) = xZ 1
0(1minus y)f (y)dy minus
Z x
0(x minus y)f (y)dy
Computational Physics II FYS4410
Qubic spline interpolation
Reminder on Greenrsquos functions
If we then define the Greenrsquos function as
G(x y) =
y(1minus x) if 0 le y le xx(1minus y) if x le y le 1
we can write the solution as
u(x) =
Z 1
0G(x y)f (y)dy
Computational Physics II FYS4410
Qubic spline interpolation
Reminder on Greenrsquos functions
The Greenrsquos function is
1 continuous
2 Is symmetric in the sense that G(x y) = G(y x)
3 G(0 y) = G(1 y) = G(x 0) = G(x 1) = 0
4 Piecewise linear function of x for fixed y and vice versa Gprime is discontinuos aty = x
5 G(x y) ge 0 for all x y isin [0 1]
6 It is the solution of the differential equation
d2
dx2G(x y) = minusδ(x minus y)
For more details see for example Arfken Mathematical methods for Physicists
chapter 165
Computational Physics II FYS4410
Qubic spline interpolation
Reminder on Greenrsquos functions
The Greenrsquos function can now be used to define the solution before and after a specificpoint in the domainThe Greenrsquos function satisfies the homogeneous equation for y 6= x and its derivativeis discontinuous at x = y We can see this if we integrate the differential equation
d2
dx2G(x y) = minusδ(x minus y)
from x = y minus ε to x = y + ε with ε as an infinitesmally small number We obtain then
dG
dx|x=y+ε minus
dG
dx|x=yminusε = 1
The problem is obvioulsy to find G
Computational Physics II FYS4410
Qubic spline interpolation
Reminder on Greenrsquos functions
We can obtain this by considering two solutions of the homogenous equation Choosea general domain x isin [a b] with a boundary condition on the general solutionu(a) = u(b) = 0One solution is obtained by integrating from a to b (called ult) and one by integratinginward from b to a labelled ugtUsing the continuity requirement on the function and its derivative we can compute theWronskian
W =dugt
dxult minus
dult
dxugt
and usingdG
dx|x=y+ε minus
dG
dx|x=yminusε = 1
and one can then show that Greenrsquos function is
G(x y) = ult(xlt)ugt(xgt)
where xlt is for x = y minus ε and xgt = y + ε
Computational Physics II FYS4410
Qubic spline interpolation
Simple example
Consider the differential equation
uprimeprime(x) + λu(x) = 0 x isin (0 1) u(0) = u(1) = 0
The solution of the homogenous equation uprimeprime = 0 from x = 0 is
ult(x) = x
and starting from x = 1ugt(x) = 1minus x
The Wronskian isultuprimegt minus ugtuprimelt = minus1
we obtain the Greenrsquos function
G(x y) =
x(1minus y) if 0 le x lt yy(1minus x) if y lt x le 1
and
u(x) = λ
Z 1
0G(x y)u(t)dy
Computational Physics II FYS4410
Qubic spline interpolation
The Greenrsquos function
For our particular case discussed last week the Greenrsquos function is also a solution of
d2
dr2G(r r prime)minus a2G(r r prime) = δ(r minus r prime)
and has the property that the solution can be written as
u(r) =
Z infin
0G(r r prime)F (r prime)dr prime
and can be expressed via the matching condition (logarithmic derivative of the wavefunction)
ddr ult(r)
ult(r)=
ddr ugt(r)
ugt(r)at r = rm
asG(r r prime) = ult(rlt)ugt(rgt)
with rlt below the matching point and rgt above
Computational Physics II FYS4410
Qubic spline interpolation
Todayrsquos tasks the Greenrsquos function
The Greenrsquos function satisfies the homogenous equation and can be used to find u(r)using
u(r) =
Z infin
0G(r r prime)F (r prime)dr prime
andG(r r prime) = ult(rlt)ugt(rgt)
resulting in
u(r) = ugt(r)Z r
0ult(r prime)F (r prime)dr prime + ult(r)
Z infin
rugt(r prime)F (r prime)dr prime
Your task is to choose a matching point say the midpoint and then compute the
Greensrsquo function after you have used Numerovrsquos algo to find u (inward and outward
integration for all points) Find u integrating with the Greenrsquos function
Computational Physics II FYS4410
Qubic spline interpolation
Algorithm
Compute ugt using Numerovrsquos algorithm inward integration
Boundary conditions and value of the wave function at max stepuin[max_step] = 00uin[max_step - 1] = 10E-10 start from right and move towards rminfor(loop = max_step - 2 loop gt 0 loop--)
uin[loop] = (20(10-50k[loop+1]120) uin[loop+1]- (10+k[loop+2]120) uin[loop+2])(10+k[loop]120)
Computational Physics II FYS4410
Qubic spline interpolation
Algorithm
Compute ult using Numerovrsquos algorithm outward integration
boundary condition and first stepuout[0] = 00uout[1] = 10e-10for(loop = 2 loop lt= max_step loop++)
uout[loop] = (2(10-50k[loop-1]120) uout[loop-1]- (10+k[loop-2]120) uout[loop-2])(10+k[loop]120)
Computational Physics II FYS4410
Qubic spline interpolation
Algorithm
Compute Wronskian at matching point using
du
dxasymp
u(x + h)minus u(x + h)
2h
for the first derivative and choose matching point as the midpoint (try the stabilityof the solution afterwards)
midpoint = max_step2 first part of Wronskianwronskian = (uin[midpoin+1]-uin[midpoint-1])uout[midpoint](2step) second partwronskian -= (uout[midpoin+1]-uout[midpoint-1])uin[midpoint](2step)
Computational Physics II FYS4410
Qubic spline interpolation
Algorithm
Compute outward integral of Greenrsquos function approach note the source termsource[loop] in code Use the Trapezoidal rule
sum = 00for(loop = 0 loop lt= max_step loop++)
term = uout[loop]source[loop]sum += termwf[loop] = uin[loop]sumstep
Computational Physics II FYS4410
Qubic spline interpolation
Algorithm
Compute inward integral of Greenrsquos function approach Use the Trapezoidal rule
sum = 00for(loop = max_step loop gt= 0 loop--)
term = uin[loop]source[loop]sum += termwf[loop] = (wf[loop]+uout[loop]sumstep)wronskian
Computational Physics II FYS4410
Qubic spline interpolation
Algorithm
Compare with analytic solution
Computational Physics II FYS4410
Qubic spline interpolation
Topics for Week 6 February 4-8
Hartree-Fock density functional theory and computing topics
Thursday
Reminder on variational calculus
Final derivation of Hartree-Fock equations
Continuation from last week seeting up code for theHartree-Fock equations
Friday
Density functional theory and begin of Variational MonteCarlo
Computational Physics II FYS4410
Qubic spline interpolation
Variational Calculus and Lagrangian Multiplier
The calculus of variations involves problems where the quantity to be minimized ormaximized is an integralIn the general case we have an integral of the type
E [Φ] =
Z b
af (Φ(x)
partΦ
partx x)dx
where E is the quantity which is sought minimized or maximized The problem is that
although f is a function of the variables Φ partΦpartx and x the exact dependence of Φ
on x is not known This means again that even though the integral has fixed limits a
and b the path of integration is not known In our case the unknown quantities are the
single-particle wave functions and we wish to choose an integration path which makes
the functional E [Φ] stationary This means that we want to find minima or maxima or
saddle points In physics we search normally for minima Our task is therefore to find
the minimum of E [Φ] so that its variation δE is zero subject to specific constraints In
our case the constraints appear as the integral which expresses the orthogonality of
the single-particle wave functions The constraints can be treated via the technique of
Lagrangian multipliers
Computational Physics II FYS4410
Qubic spline interpolation
Euler-Lagrange equations
We assume the existence of an optimum path that is a path for which E [Φ] isstationary There are infinitely many such paths The difference between two paths δΦis called the variation of ΦWe call the variation η(x) and it is scaled by a factor α The function η(x) is arbitraryexcept for
η(a) = η(b) = 0
and we assume that we can model the change in Φ as
Φ(x α) = Φ(x 0) + αη(x)
andδΦ = Φ(x α)minus Φ(x 0) = αη(x)
Computational Physics II FYS4410
Qubic spline interpolation
Euler-Lagrange equations
We choose Φ(x α = 0) as the unkonwn path that will minimize E The valueΦ(x α 6= 0) describes a neighbouring pathWe have
E [Φ(α)] =
Z b
af (Φ(x α)
partΦ(x α)
partx x)dx
In the slides I will use the shorthand
Φx (x α) =partΦ(x α)
partx
In our case a = 0 and b = infin and we know the value of the wave function
Computational Physics II FYS4410
Qubic spline interpolation
Euler-Lagrange equations
The condition for an extreme of
E [Φ(α)] =
Z b
af (Φ(x α)Φx (x α) x)dx
is raquopartE [Φ(α)]
partx
ndashα=0
= 0
The α dependence is contained in Φ(x α) and Φx (x α) meaning thatraquopartE [Φ(α)]
partα
ndash=
Z b
a
bdquopartf
partΦ
partΦ
partα+
partf
partΦx
partΦx
partα
laquodx
We have definedpartΦ(x α)
partα= η(x)
and therebypartΦx (x α)
partα=
d(η(x))
dx
Computational Physics II FYS4410
Qubic spline interpolation
Euler-Lagrange equations
UsingpartΦ(x α)
partα= η(x)
andpartΦx (x α)
partα=
d(η(x))
dx
in the integral givesraquopartE [Φ(α)]
partα
ndash=
Z b
a
bdquopartf
partΦη(x) +
partf
partΦx
d(η(x))
dx
laquodx
Integrate the second term by partsZ b
a
partf
partΦx
d(η(x))
dxdx = η(x)
partf
partΦx|ba minus
Z b
aη(x)
d
dx
partf
partΦxdx
and since the first term dissappears due to η(a) = η(b) = 0 we obtainraquopartE [Φ(α)]
partα
ndash=
Z b
a
bdquopartf
partΦminus
d
dx
partf
partΦx
laquoη(x)dx = 0
Computational Physics II FYS4410
Qubic spline interpolation
Euler-Lagrange equations
raquopartE [Φ(α)]
partα
ndash=
Z b
a
bdquopartf
partΦminus
d
dx
partf
partΦx
laquoη(x)dx = 0
can also be written as
α
raquopartE [Φ(α)]
partα
ndashα=0
=
Z b
a
bdquopartf
partΦminus
d
dx
partf
partΦx
laquoδΦ(x)dx = δE = 0
The condition for a stationary value is thus a partial differential equation
partf
partΦminus
d
dx
partf
partΦx= 0
known as Eulerrsquos equation Can easily be generalized to more variables
Computational Physics II FYS4410
Qubic spline interpolation
Lagrangian Multipliers
Consider a function of three independent variables f (x y z) For the function f to bean extreme we have
df = 0
A necessary and sufficient condition is
partf
partx=partf
party=partf
partz= 0
due to
df =partf
partxdx +
partf
partydy +
partf
partzdz
In physical problems the variables x y z are often subject to constraints (in our case Φ
and the orthogonality constraint) so that they are no longer all independent It is
possible at least in principle to use each constraint to eliminate one variable and to
proceed with a new and smaller set of independent varables
Computational Physics II FYS4410
Qubic spline interpolation
Lagrangian Multipliers
The use of so-called Lagrangian multipliers is an alternative technique when theelimination of of variables is incovenient or undesirable Assume that we have anequation of constraint on the variables x y z
φ(x y z) = 0
resulting in
dφ =partφ
partxdx +
partφ
partydy +
partφ
partzdz = 0
Now we cannot set anymore
partf
partx=partf
party=partf
partz= 0
if df = 0 is wanted because there are now only two independent variables Assume x
and y are the independent variables Then dz is no longer arbitrary
Computational Physics II FYS4410
Qubic spline interpolation
Lagrangian Multipliers
However we can add to
df =partf
partxdx +
partf
partydy +
partf
partzdz
a multiplum of dφ viz λdφ resulting in
df + λdφ = (partf
partz+ λ
partφ
partx)dx + (
partf
party+ λ
partφ
party)dy + (
partf
partz+ λ
partφ
partz)dz = 0
Our multiplier is chosen so that
partf
partz+ λ
partφ
partz= 0
Computational Physics II FYS4410
Qubic spline interpolation
Lagrangian Multipliers
However we took dx and dy as to be arbitrary and thus we must have
partf
partx+ λ
partφ
partx= 0
andpartf
party+ λ
partφ
party= 0
When all these equations are satisfied df = 0 We have four unknowns x y z and λActually we want only x y z λ need not to be determined it is therefore often calledLagrangersquos undetermined multiplier If we have a set of constraints φk we have theequations
partf
partxi+X
k
λkpartφk
partxi= 0
Computational Physics II FYS4410
Qubic spline interpolation
Variational Calculus and Lagrangian Multipliers
Let us specialize to the expectation value of the energy for one particle inthree-dimensions This expectation value reads
E =
Zdxdydzψlowast(x y z)Hψ(x y z)
with the constraint Zdxdydzψlowast(x y z)ψ(x y z) = 1
and a Hamiltonian
H = minus1
2nabla2 + V (x y z)
I will skip the variables x y z below and write for example V (x y z) = V
Computational Physics II FYS4410
Qubic spline interpolation
Variational Calculus and Lagrangian Multiplier
The integral involving the kinetic energy can be written as if we assume periodicboundary conditions or that the function ψ vanishes strongly for large values of x y zZ
dxdydzψlowastbdquominus
1
2nabla2laquoψdxdydz = ψlowastnablaψ|+
Zdxdydz
1
2nablaψlowastnablaψ
Inserting this expression into the expectation value for the energy and taking thevariational minimum we obtain
δE = δ
Zdxdydz
bdquo1
2nablaψlowastnablaψ + Vψlowastψ
laquoff= 0
Computational Physics II FYS4410
Qubic spline interpolation
Variational Calculus and Lagrangian Multiplier
The constraint appears in integral form asZdxdydzψlowastψ = constant
and multiplying with a Lagrangian multiplier λ and taking the variational minimum weobtain the final variational equation
δ
Zdxdydz
bdquo1
2nablaψlowastnablaψ + Vψlowastψ minus λψlowastψ
laquoff= 0
Introducing the function f
f =1
2nablaψlowastnablaψ + Vψlowastψ minus λψlowastψ =
1
2(ψlowastxψx + ψlowastyψy + ψlowastzψz) + Vψlowastψ minus λψlowastψ
where we have skipped the dependence on x y z and introduced the shorthand ψx
ψy and ψz for the various derivatives
Computational Physics II FYS4410
Qubic spline interpolation
Variational Calculus and Lagrangian Multiplier
For ψlowast the Euler equation results in
partf
partψlowastminus
part
partx
partf
partψlowastxminus
part
party
partf
partψlowastyminus
part
partz
partf
partψlowastz= 0
which yields
minus1
2(ψxx + ψyy + ψzz) + Vψ = λψ
We can then identify the Lagrangian multiplier as the energy of the system Then thelast equation is nothing but the standard Schrodinger equation and the variationalapproach discussed here provides a powerful method for obtaining approximatesolutions of the wave function
Computational Physics II FYS4410
Qubic spline interpolation
Finding the Hartree-Fock functional E [Φ]
We rewrite our Hamiltonian
H = minusNX
i=1
1
2nabla2
i minusNX
i=1
Z
ri+
NXiltj
1
rij
as
H = H1 + H2 =NX
i=1
hi +NX
iltj=1
1
rij
hi = minus1
2nabla2
i minusZ
ri
Computational Physics II FYS4410
Qubic spline interpolation
Finding the Hartree-Fock functional E [Φ]
Let us denote the ground state energy by E0 According to the variational principle wehave
E0 le E [Φ] =
ZΦlowastHΦdτ
where Φ is a trial function which we assume to be normalizedZΦlowastΦdτ = 1
where we have used the shorthand dτ = dr1dr2 drN
Computational Physics II FYS4410
Qubic spline interpolation
Finding the Hartree-Fock functional E [Φ]
In the Hartree-Fock method the trial function is the Slater determinant which can berewritten as
Ψ(r1 r2 rN α β ν) =1
radicN
XP
(minus)PPψα(r1)ψβ(r2) ψν(rN) =radic
NAΦH
where we have introduced the anti-symmetrization operator A defined by thesummation over all possible permutations of two eletrons It is defined as
A =1
N
XP
(minus)PP
with the the Hartree-function given by the simple product of all possible single-particlefunction (two for helium four for beryllium and ten for neon)
ΦH(r1 r2 rN α β ν) = ψα(r1)ψβ(r2) ψν(rN)
Computational Physics II FYS4410
Qubic spline interpolation
Finding the Hartree-Fock functional E [Φ]
Both H1 and H2 are invariant under electron permutations and hence commute with A
[H1A] = [H2A] = 0
Furthermore A satisfiesA2 = A
since every permutation of the Slater determinant reproduces it
Computational Physics II FYS4410
Qubic spline interpolation
Finding the Hartree-Fock functional E [Φ]
The expectation value of H1ZΦlowastH1Φdτ = N
ZΦlowastHAH1AΦHdτ
is readily reduced to ZΦlowastH1Φdτ = N
ZΦlowastH H1AΦHdτ
which can be rewritten as
ZΦlowastH1Φdτ =
NXi=1
XP
(minus)PZ
ΦlowastH hi PΦHdτ
Computational Physics II FYS4410
Qubic spline interpolation
Finding the Hartree-Fock functional E [Φ]
The integral vanishes if two or more electrons are permuted in only one of theHartree-functions ΦH because the individual orbitals are orthogonal We obtain then
ZΦlowastH1Φdτ =
NXi=1
ZΦlowastH hiΦHdτ
Orthogonality allows us to further simplify the integral and we arrive at the followingexpression for the expectation values of the sum of one-body Hamiltonians
ZΦlowastH1Φdτ =
NXmicro=1
Zψlowastmicro(r i )hiψmicro(r i )dr i
or just as ZΦlowastH1Φdτ =
NXmicro=1
〈micro|h|micro〉
Computational Physics II FYS4410
Qubic spline interpolation
Finding the Hartree-Fock functional E [Φ]
The expectation value of the two-body Hamiltonian is obtained in a similar manner Wehave Z
ΦlowastH2Φdτ = N
ZΦlowastHAH2AΦHdτ
which reduces to
ZΦlowastH2Φdτ =
NXilej=1
XP
(minus)PZ
ΦlowastH1
rijPΦHdτ
by following the same arguments as for the one-body Hamiltonian Because of thedependence on the inter-electronic distance 1rij permutations of two electrons nolonger vanish and we get
ZΦlowastH2Φdτ =
NXiltj=1
ZΦlowastH
1
rij(1minus Pij )ΦHdτ
where Pij is the permutation operator that interchanges electrons i and j
Computational Physics II FYS4410
Qubic spline interpolation
Finding the Hartree-Fock functional E [Φ]
We use the assumption that the orbitals are orthogonal and obtain
ZΦlowastH2Φdτ =
1
2
NXmicro=1
NXν=1
Zψlowastmicro(r i )ψ
lowastν(r j )
1
rijψmicro(r i )ψν(r j )dr i r j
minusZψlowastmicro(r i )ψ
lowastν(r j )
1
rijψν(r i )ψmicro(r i )dx ix j
The first term is the so-called direct term or Hartree term while the second is due tothe Pauli principle and is called exchange term or Fock term The factor 12 isintroduced because we now run over all pairs twiceThe compact notation is
1
2
NXmicro=1
NXν=1
〈microν|
1
rij|microν〉 minus 〈microν|
1
rij|νmicro〉
Computational Physics II FYS4410
Qubic spline interpolation
Variational Calculus and Lagrangian Multiplier back toHartree-Fock
Our functional is written as
E [Φ] =NX
micro=1
Zψlowastmicro(r i )hiψmicro(r i )dr i +
1
2
NXmicro=1
NXν=1
Zψlowastmicro(r i )ψ
lowastν(r j )
1
rijψmicro(r i )ψν(r j )dr i r j
minusZψlowastmicro(r i )ψ
lowastν(r j )
1
rijψν(r i )ψmicro(r i )dr i r j
The more compact version is
E [Φ] =NX
micro=1
〈micro|h|micro〉+1
2
NXmicro=1
NXν=1
〈microν|
1
rij|microν〉 minus 〈microν|
1
rij|νmicro〉
Computational Physics II FYS4410
Qubic spline interpolation
Variational Strategies
With the given functional we can perform at least two types of variational strategis
Vary the Slater determinat by changing the spatial part of the single-particlewave functions themselves This is what we will do
Expand the single-particle functions in a known basis and vary the coefficientsthat is the new function single-particle wave function |a〉 is written as a linearexpansion in terms of a fixed basis (harmonic oscillator Laguerre polynomialsetc)
ψa =Xλ
Caλψλ
Both cases lead to a new Slater determinant which is related to the previous via aunitary transformation
We look at the first case first The other one willbe presented tomorrow during the
lectures
Computational Physics II FYS4410
Qubic spline interpolation
Exercise
1 Consider a Slater determinant built up of single-particle orbitals ψλ withλ = 1 2 N
The unitary transformationψa =
Xλ
Caλψλ
brings us into the new basis Show that the new basis is orthonormal
2 Show that the new Slater determinant constructed from the new single-particlewave functions can be written as the determinant based on the previous basisand the determinant of the matrix C
3 Show that the old and the new Slater determinants are equal up to a complexconstant with absolute value unity (Hint C is a unitary matrix)
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Variational Calculus and LagrangianMultiplier
If we generalize the Euler-Lagrange equations to more variables and introduce N2
Lagrange multipliers which we denote by εmicroν we can write the variational equation forthe functional of E
δE minusNX
micro=1
NXν=1
εmicroνδ
Zψlowastmicroψν = 0
For the orthogonal wave functions ψmicro this reduces to
δE minusNX
micro=1
εmicroδ
Zψlowastmicroψmicro = 0
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Variational Calculus and LagrangianMultiplier
Variation with respect to the single-particle wave functions ψmicro yields then
NXmicro=1
Zδψlowastmicrohiψmicrodr i +
1
2
NXmicro=1
NXν=1
Zδψlowastmicroψ
lowastν
1
rijψmicroψνdr i dr j minus
Zδψlowastmicroψ
lowastν
1
rijψνψmicrodr i dr j
+NX
micro=1
Zψlowastmicrohiδψmicrodr i +
1
2
NXmicro=1
NXν=1
Zψlowastmicroψ
lowastν
1
rijδψmicroψνdr i dr j minus
Zψlowastmicroψ
lowastν
1
rijψνδψmicrodr i dr j
minusNX
micro=1
Emicro
Zδψlowastmicroψmicrodr i minus
NXmicro=1
Emicro
Zψlowastmicroδψmicrodr i = 0
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Variational Calculus and LagrangianMultiplier
Although the variations δψ and δψlowast are not independent they may in fact be treated assuch so that the terms dependent on either δψ and δψlowast individually may be set equalto zero To see this simply replace the arbitrary variation δψ by iδψ so that δψlowast isreplaced by minusiδψlowast and combine the two equations We thus arrive at theHartree-Fock equations24minus1
2nabla2
i minusZ
ri+
NXν=1
Zψlowastν(r j )
1
rijψν(r j )dr j
35ψmicro(r i )
minus
24 NXν=1
Zψlowastν(r j )
1
rijψmicro(r j )dr j
35ψν(r i ) = εmicroψmicro(r i )
Notice that the integrationR
dr j implies an integration over the spatial coordinates r j
and a summation over the spin-coordinate of electron j
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Variational Calculus and LagrangianMultiplier
The two first terms are the one-body kinetic energy and the electron-nucleus potential
The third or direct term is the averaged electronic repulsion of the other electrons This
term is identical to the Coulomb integral introduced in the simple perturbative approach
to the helium atom As written the term includes the rsquoself-interactionrsquo of electrons when
i = j The self-interaction is cancelled in the fourth term or the exchange term The
exchange term results from our inclusion of the Pauli principle and the assumed
determinantal form of the wave-function The effect of exchange is for electrons of
like-spin to avoid each other
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Variational Calculus and LagrangianMultiplier
A theoretically convenient form of the Hartree-Fock equation is to regard the direct andexchange operator defined through
V dmicro(r i ) =
Zψlowastmicro(r j )
1
rijψmicro(r j )dr j
and
V exmicro (r i )g(r i ) =
Zψlowastmicro(r j )
1
rijg(r j )dr j
ψmicro(r i )
respectively
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Variational Calculus and LagrangianMultiplier
The function g(r i ) is an arbitrary function and by the substitution g(r i ) = ψν(r i ) we get
V exmicro (r i )ψν(r i ) =
Zψlowastmicro(r j )
1
rijψν(r j )dr j
ψmicro(r i )
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Variational Calculus and LagrangianMultiplier
We may then rewrite the Hartree-Fock equations as
HHFi ψν(r i ) = ενψν(r i )
with
HHFi = hi +
NXmicro=1
V dmicro(r i )minus
NXmicro=1
V exmicro (r i )
and where hi is the one-body part
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expression for Direct Term
We want to show first that
V dmicro(r i ) =
Zψlowastmicro(r j )
1
rijψmicro(r j )dr j
is
V dmicro(r i ) = V d
nl (ri ) =Xnprime lprime
2(2l prime + 1)
Z infin
0|unprime lprime (rj )|2
1
rgtdrj
with rgt = max(ri rj )
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expression for Direct TermWe need to break down the single-particle wave functions in radial angular and spinparts Note that direct term is diagonal in the spin quantum numbers Thesingle-particle wave functions are
ψα(r) = ψnlml sms (r) = φnlml(r)ξms (s)
withφnlml
(r) = Rnl (r)Ylml(r)
and we defined unl (r) = rRnl (r) The direct term is with a factor two from the spindegrees of freedom gives for a single shell
V dnprime lprime (ri ) = 2
lXmprimel =minuslprime
Z infin
0|φnprime lprimemprimel
(rj )|21
rijdr j
which reads
V dnprime lprime (ri ) = 2
Z infin
0|unprime lprime (rj )|2
1
rijdrj
lXmprimel =minuslprime
|Ylmprimel(θj φj )|2dΩj
with dΩj the angular part d(cosθj )dφj
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expression for Direct Term
The addition theorem ofthe spherical harmonics yields
lXmprimel =minuslprime
|Ylmprimel(θj φj )|2 =
2l prime + 1
4π
resulting in
V dnprime lprime (ri ) =
2(2l prime + 1)
4π
Z infin
0|unprime lprime (rj )|2
1
rijdrj dΩj
The quantity rij depends on the angles of particle i and j
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expression for Direct Term
The integral over the angles can be performed by expanding 1rij in terms of sphericalharmonics and using the fact that the functions unprime lprime do not depend on the angles Wehave
1
rij=
1
ri
infinXl=0
bdquorj
ri
laquol
Pl cos(θ))
if ri gt rj or
1
rij=
1
rj
infinXl=0
ri
rj
l
Pl cos(θ))
if rj gt ri In a compact form it reads
1
rij=
infinXl=0
(rlt)l
(rgt)l+1
Pl cos(θ))
with rgt = max(ri rj ) and with rlt = min(ri rj ) Pl is the Legendre polynomial and
cos(θ) = cos(θi )cos(θj ) + sin(θi )sin(θj )cos(φi minus φj )
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expression for Direct Term
We can use the expression for spherical harmonics to express the interaction as
1
rij=
infinXl=0
lXmprimel =minuslprime
(rlt)l
(rgt)l+1
Ylowastlmprimel
(θi φi )Ylmprimel(θj φj )
and inserting it we obtain the final expression as
Φ(ri ) =Xnprime lprime
2(2l prime + 1)
Z infin
0|unprime lprime (rj )|2
1
rgtdrj
with rgt = max(ri rj )
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expression for Exchange Term
Exercise show that the exchange part can be written as
Fnl (ri ) =Xnprime lprime
l+lprimeXλ=|lminuslprime|
2l prime + 1
2λ+ 1lt ll prime00|λ0 gt2
raquoZ infin
0ulowastnprime lprime (rj )
(rlt)λ
(rgt)λ+1unl (rj )drj
ndashunprime lprime (ri )
with rgt = max(ri rj ) and with rlt = min(ri rj )
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expression for Exchange Term
The Clebsch-Gordan coefficient can be written as
〈ll prime00|λ0〉 =1
2
p2λ+ 1(1 + (minus1)2g)(minus1)g
s(2g minus 2l)(2g minus 2l prime)(2g minus 2l prime)
(2g + 1)
timesg
(g minus l)(g minus l prime)(g minus λ)
with 2g = 2l + λ You can write a small function for this expression or use the enclosed
programs
Computational Physics II FYS4410
Qubic spline interpolation
Topics for Week 7 February 11-15
Hartree-Fock theory DFT and Monte Carlo
Thursday
Discussion of the Hartree-Fock program
Friday
End of DFT discussion and begin of Variational MonteCarlo
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expressions for various Atoms
We need to discuss how the Hartree-Fock equations look like for helium beryllium andneon (Exercise from last week)The general equations take the form24minus1
2nabla2
i minusZ
ri+
NXν=1
Zψlowastν(r j )
1
rijψν(r j )dr j
35ψmicro(r i )
minus
24 NXν=1
Zψlowastν(r j )
1
rijψmicro(r j )dr j
35ψν(r i ) = εmicroψmicro(r i )
Notice that the integrationR
dr j implies an integration over the spatial coordinates r j
and a summation over the spin-coordinate of electron j
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expressions for various Atoms
A theoretically convenient form of the Hartree-Fock equation is to regard the direct andexchange operator defined through
V dmicro(r i ) =
Zψlowastmicro(r j )
1
rijψmicro(r j )dr j
and
V exmicro (r i )g(r i ) =
Zψlowastmicro(r j )
1
rijg(r j )dr j
ψmicro(r i )
respectively
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Equations
We may then rewrite the Hartree-Fock equations as
HHFi ψν(r i ) = ενψν(r i )
with
HHFi = hi +
NXmicro=1
V dmicro(r i )minus
NXmicro=1
V exmicro (r i )
and where hi is the one-body part
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expressions for various Atomshelium
The Slater determinant for helium reads with two electrons in the 1s state
Φ(r1 r2 α β) =1radic
2
˛ψα(r1) ψα(r2)ψβ(r1) ψβ(r2)
˛
with α = nlml sms = 1001212 and β = nlml sms = 10012minus 12 or usingms = 12 =uarr and ms = minus12 =darr as α = nlml sms = 10012 uarr andβ = nlml sms = 10012 darr Writing out the Slater determinant we obtain
Φ(r1 r2 α β) =1radic
2
ˆψα(r1)ψβ(r2)minus ψβ(r1)ψγ(r2)
˜
and we see that the Slater determinant is antisymmetric with respect to thepermutation of two particles that is
Φ(r1 r2 α β) = minusΦ(r2 r1 α β)
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expressions for various Atomshelium
In the derivations of the direct and exchange terms we have not discussed the role ofthe electron spinLet us introduce
ψnlml sms = φnlml(r)ξms (s)
with s is the spin (12 for electrons) ms is the spin projection ms = plusmn12 and thespatial part is
φnlml(r) = Rnl (r)Ylml
(r)
with Y the spherical harmonics and unl = rRnl We have for helium
Φ(r1 r2 α β) =1radic
2φ100(r1)φ100(r2)
ˆξuarr(1)ξdarr(2)minus ξuarr(2)ξdarr(1)
˜
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expressions for various Atomshelium
The direct term acts on1radic
2φ100(r1)φ100(r2)ξuarr(1)ξdarr(2)
while the exchange term acts on
minus1radic
2φ100(r1)φ100(r2)ξuarr(2)ξdarr(1)
How do these terms get translated into the Hartree and the Fock terms
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expressions for various Atomshelium
The Hartree term
V dmicro(r i ) =
Zψlowastmicro(r j )
1
rijψmicro(r j )dr j
acts on ψλ(r i ) = φnlml(r i )ξms (si ) that is it results in
V dmicro(r i )ψλ(r i ) =
Zψlowastmicro(r j )
1
rijψmicro(r j )dr j
ψλ(r i )
and accounting for spins we have
V dnlmluarr(r i )ψλ(r i ) =
Zψlowastnlmluarr(r j )
1
rijψnlmluarr(r j )dr j
ψλ(r i )
and
V dnlmldarr(r i )ψλ(r i ) =
Zψlowastnlmldarr(r j )
1
rijψnlmldarr(r j )dr j
ψλ(r i )
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expressions for various Atomshelium
If the state we act on has spin up we obtain two terms from the Hartree part
NXmicro=1
V dmicro(r i )
and since the interaction does not depend on spin we end up with a total contributionfor helium
NXmicro=1
V dmicro(r i )ψλ(r i ) =
2Zφlowast100(r j )
1
rijφ100(r j )dr j
ψλ(r i )
one from spin up and one from spin down Since the energy for spin up or spin down isthe same we can then write the action of the Hartree term as
NXmicro=1
V dmicro(r i )ψλ(r i ) =
2Zφlowast100(r j )
1
rijφ100(r j )dr j
ψ100uarr(r i )
(the spin in ψ100uarr is irrelevant)
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expressions for various Atomshelium
What we need to code for helium is then
Φ(ri )u10 = 2V d10(ri )u10(ri ) = 2
Z infin
0|u10(rj )|2
1
rgtdrj )u10(ri )
with rgt = max(ri rj ) What about the exchange or Fock term
V exmicro (r i )ψλ(r i ) =
Zψlowastmicro(r j )
1
rijψλ(r j )dr j
ψmicro(r i )
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expressions for various Atomshelium
We must be careful here with
V exmicro (r i )ψλ(r i ) =
Zψlowastmicro(r j )
1
rijψλ(r j )dr j
ψmicro(r i )
because the spins of micro and λ have to be the same due to the constraint
〈smicrommicros |sλmλ
s 〉 = δmmicros mλ
s
This means that if mmicros =uarr then mλ
s =uarr and if mmicros =darr then mλ
s =darr That is
V exmicro (r i )ψλ(r i ) = δmmicro
s mλs
Zψlowastmicro(r j )
1
rijψλ(r j )dr j
ψmicro(r i )
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expressions for various Atomshelium
The consequence is that for the 1s uarr (and the same for 1s darr) state we get only onecontribution from the Fock term
NXmicro=1
V exmicro (r i )ψλ(r i )
NXmicro=1
V exmicro (r i )ψ100uarr(r i ) = δmmicro
s uarr
Zψlowastmicro(r j )
1
rijψ100uarr(r j )dr j
ψmicro(r i )
resulting in
NXmicro=1
V exmicro (r i )ψ100uarr(r i ) =
Zψlowast100uarr(r j )
1
rijψ100uarr(r j )dr j
ψ100uarr(r i )
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expressions for various Atomshelium
The final Fock term for helium is then
NXmicro=1
V exmicro (r i )ψ100uarr(r i ) =
Zψlowast100uarr(r j )
1
rijψ100uarr(r j )dr j
ψ100uarr(r i )
which is exactly the same as the Hartree term except for a factor of 2 Else the integralis the same We can then write the differential equationbdquo
minus1
2
d2
dr2+
l(l + 1)
2r2minus
2
r+ Φnl (r)minus Fnl (r)
laquounl (r) = enl unl (r)
as bdquominus
1
2
d2
dr2+
l(l + 1)
2r2minus
2
r+ 2V d
10(r)laquo
u10(r)minus V ex10 (r) = e10u10(r)
or bdquominus
1
2
d2
dr2minus
2
r+ V d
10(r)laquo
u10(r) = e10u10(r)
since l = 0 The shorthand V ex10 (r) contains the 1s wave function and can be
dangerous later
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expressions for various Atomsberyllium
The expression we have obtained are independent of the spin projections and we haveskipped them in the equations Last weekrsquos exercise was to derive the correspondingequations for beryllium with two electrons in 1s as in helium but now also twoelectrons in 2sThe Slater determinant takes the form
Φ(r1 r2 r3 r4 α β γ δ) =1radic
4
˛˛ ψ100uarr(r1) ψ100uarr(r2) ψ100uarr(r3) ψ100uarr(r4)ψ100darr(r1) ψ100darr(r2) ψ100darr(r3) ψ100darr(r4)ψ200uarr(r1) ψ200uarr(r2) ψ200uarr(r3) ψ200uarr(r4)ψ200darr(r1) ψ200darr(r2) ψ200darr(r3) ψ200darr(r4)
˛˛
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expressions for various Atomsberyllium
When we now spell out the Hartree-Fock equations we get two coupled differentialequations one for u10 and one for u20The 1s wave function has the same Hartree-Fock contribution as in helium for the 1sstate but the 2s state gives two times the Hartree term and one time the Fock termThat is we get
NXmicro=1
V dmicro(r i )ψ100uarr(r i ) = 2
Z infin
0dr j
φlowast100(r j )
1
rijφ100(r j ) + φlowast200(r j )
1
rijφ200(r j )
ψ100uarr(r i )
= (2V d10(r i ) + 2V d
20(r i ))ψ100uarr(r i )
for the Hartree part
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expressions for various Atomsberyllium
For the Fock term we get (we fix the spin)
NXmicro=1
V exmicro (r i )ψ100uarr(r i ) =
Z infin
0dr jφ
lowast100(r j )
1
rijφ100(r j )ψ100uarr(r i )+
Z infin
0dr jφ
lowast200(r j )
1
rijφ100(r j )ψ200uarr(r i ) = V ex
10 (r i ) + V ex20 (r i )
The first term is the same as we have for the Hartree term with 1s except the factor oftwo The final differential equation isbdquo
minus1
2
d2
dr2minus
4
r+ V d
10(r) + 2V d20(r)
laquou10(r)minus V ex
20 (r) = e10u10(r)
Note again that the V ex20 (r) contains the 1s function in the integral that is
V ex20 (r) =
Z infin
0dr jφ
lowast200(r j )
1
r minus rjφ100(r j )ψ200uarr(r)
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expressions for various Atomsberyllium
The 2s wave function obtains the following Hartree term (recall that the interaction hasno spin dependence)
NXmicro=1
V dmicro(r i )ψ200uarr(r i ) = 2
Z infin
0dr j
φlowast100(r j )
1
rijφ100(r j ) + φlowast200(r j )
1
rijφ200(r j )
ψ200uarr(r i ) =
(2V d10(r i ) + 2V d
20(r i ))ψ200uarr(r i )
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Explicit expressions for various Atomsberyllium
For the Fock term we get
NXmicro=1
V exmicro (r i )ψ200uarr(r i ) =
Z infin
0dr jφ
lowast100(r j )
1
rijφ200(r j )ψ100uarr(r i )+
Z infin
0dr jφ
lowast200(r j )
1
rijφ200(r j )ψ200uarr(r i ) = V ex
10 (r i ) + V ex20 (r i )
The second term is the same as we have for the Hartree term with 2s The finaldifferential equation isbdquo
minus1
2
d2
dr2minus
4
r+ 2V d
10(r) + V d20(r)
laquou20(r)minus V ex
10 (r) = e20u20(r)
Note again that the V ex10 (r) contains the 2s function in the integral that is
V ex10 (r) =
Z infin
0dr jφ
lowast100(r j )
1
r minus rjφ200(r j )ψ100uarr(r)
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Final expressions for beryllium
We have two coupled differential equationsbdquominus
1
2
d2
dr2minus
4
r+ V d
10(r) + 2V d20(r)
laquou10(r)minus V ex
20 (r) = e10u10(r)
and bdquominus
1
2
d2
dr2minus
4
r+ 2V d
10(r) + V d20(r)
laquou20(r)minus V ex
10 (r) = e20u20(r)
Recall again that the interaction does not depend on spin This means that thesingle-particle energies and single-particle function u do not depend on spin Also
Exercise derive the equivalent expressions for neon
Computational Physics II FYS4410
Qubic spline interpolation
Hartree-Fock Final expressions for neon
Exercise fill in the missing partsbdquominus
1
2
d2
dr2minus
10
r+ V d
10(r) + 2V d20(r)+
laquou10(r)minus V ex
20 (r)+ = e10u10(r)
and bdquominus
1
2
d2
dr2minus
10
r+ 2V d
10(r) + V d20(r)+
laquou20(r)minus V ex
10 (r)+ = e20u20(r)
andbdquominus
1
2
d2
dr2+
2
2r2minus
10
r+ 2V d
10(r) + 2V d20(r)+
laquou21(r)minusV ex
10 (r)minusV ex20 (r)+ = e21u21(r)
Computational Physics II FYS4410
Qubic spline interpolation
Useful Tests
The 1s hydrogen like wave function
R10(r) = 2bdquo
Z
a0
laquo32
exp (minusZra0) = u10r
The total energy for helium (not the Hartree or Fock terms) from the direct and theexchange term should give 5Z8The single-particle energy with no interactions should give minusZ 22n2The 2s hydrogen-like wave function is
R20(r) = 2bdquo
Z
2a0
laquo32 bdquo1minus
Zr
2a0
laquoexp (minusZr2a0) = u20r
and the 2p hydrogen -like wave function is
R21(r) =1radic
3
bdquoZ
2a0
laquo32 Zr
a0exp (minusZr2a0) = u21r
We use a0 = 1
Computational Physics II FYS4410
Qubic spline interpolation
Effective Charge and first Iteration
If we compute the total energy of the helium atom with the function
R10(r) = 2bdquo
Z
a0
laquo32
exp (minusZra0) = u10r
as a trial single-particle wave fuction we obtain a total energy (one-body and two-body)
E [Z ] = Z 2 minus 4Z +5
8Z
The minimum is not at Z = 2 Take the derivative wrt Z and we find that the minimumis at
Z = 2minus5
16= 16875
and represents an optimal effective charge When we do the Hartree-Fock calculations
and use the optimal single-particle wave function in a variational Monte Carlo
calculation we should have the wave function calculated at the optimal value How do
we do this
Computational Physics II FYS4410
Qubic spline interpolation
Effective Charge and first Iteration
Before the first iteration we compute the total energy of the system using thehydrogen wave function
Then we use the virial theorem for the Coulomb potential which states that
minus〈V 〉 = 2〈T 〉
where the expectation values are the total potential (one-body and two-body)and total kinetic energy
The effective charge is then
Zeff = minusZ 〈V 〉2〈T 〉
We use this effective charge in the remaining Hartree-Fock equations
Exercise Repeat the virial theorem(FYS3110 lectures)
Computational Physics II FYS4410
Qubic spline interpolation
Density Functional Theory (DFT)
Hohenberg and Kohn proved that the total energy of a system including that of themany-body effects of electrons (exchange and correlation) in the presence of staticexternal potential (for example the atomic nuclei) is a unique functional of the chargedensity The minimum value of the total energy functional is the ground state energy ofthe system The electronic charge density which yields this minimum is then the exactsingle particle ground state energyIn Hartree-Fock theory one works with large basis sets This poses a problem for largesystems An alternative to the HF methods is DFT DFT takes into account electroncorrelations but is less demanding computationally than full scale diagonalization orMonte Carlo methods
Computational Physics II FYS4410
Qubic spline interpolation
Density Functional Theory
The electronic energy E is said to be a functional of the electronic density E [ρ] in thesense that for a given function ρ(r) there is a single corresponding energy TheHohenberg-Kohn theorem confirms that such a functional exists but does not tell usthe form of the functional As shown by Kohn and Sham the exact ground-state energyE of an N-electron system can be written as
E [ρ] = minus1
2
NXi=1
ZΨlowasti (r1)nabla2
1Ψi (r1)dr1minusZ
Z
r1ρ(r1)dr1+
1
2
Zρ(r1)ρ(r2)
r12dr1dr2+EEXC [ρ]
with Ψi the Kohn-Sham (KS) orbitals
Computational Physics II FYS4410
Qubic spline interpolation
Density Functional Theory
The ground-state charge density is given by
ρ(r) =NX
i=1
|Ψi (r)|2
where the sum is over the occupied Kohn-Sham orbitals The last term EEXC [ρ] is the
exchange-correlation energy which in theory takes into account all non-classical
electron-electron interaction However we do not know how to obtain this term exactly
and are forced to approximate it The KS orbitals are found by solving the Kohn-Sham
equations which can be found by applying a variational principle to the electronic
energy E [ρ] This approach is similar to the one used for obtaining the HF equation
Computational Physics II FYS4410
Qubic spline interpolation
Density Functional Theory
The KS equations readsminus
1
2nabla2
1 minusZ
r1+
Zρ(r2)
r12dr2 + VEXC(r1)
ffΨi (r1) = εiΨi (r1)
where εi are the KS orbital energies and where the exchange-correlation potential isgiven by
VEXC [ρ] =δEEXC [ρ]
δρ
Computational Physics II FYS4410
Qubic spline interpolation
Density Functional Theory
The KS equations are solved in a self-consistent fashion A variety of basis setfunctions can be used and the experience gained in HF calculations are often usefulThe computational time needed for a DFT calculation formally scales as the thirdpower of the number of basis functionsThe main source of error in DFT usually arises from the approximate nature of EEXC Inthe local density approximation (LDA) it is approximated as
EEXC =
Zρ(r)εEXC [ρ(r)]dr
where εEXC [ρ(r)] is the exchange-correlation energy per electron in a homogeneous
electron gas of constant density The LDA approach is clearly an approximation as the
charge is not continuously distributed To account for the inhomogeneity of the electron
density a nonlocal correction involving the gradient of ρ is often added to the
exchange-correlation energy
Computational Physics II FYS4410
Qubic spline interpolation
Topics for Week 8 February 18-22
Hartree-Fock theory DFT and Monte Carlo
Thursday
Variational Monte Carlo and the Metropolis algorithm
Discussion of the Blocking method and repeat onparallelization
Discussion of variational Monte Carlo and the codesvmc paracpp and vmc blockingcpp
Friday
Variational Monte Carlo statistical data analysis andimportance sampling
Background chapters 8-11 in Lecture notes Emphasis onchapter 11
Computational Physics II FYS4410
Qubic spline interpolation
Brownian Motion and Markov Processes
A Markov process is a random walk with a selected probability for making a move Thenew move is independent of the previous history of the system The Markov process isused repeatedly in Monte Carlo simulations in order to generate new random statesThe reason for choosing a Markov process is that when it is run for a long enough timestarting with a random state we will eventually reach the most likely state of thesystem In thermodynamics this means that after a certain number of Markovprocesses we reach an equilibrium distribution This mimicks the way a real systemreaches its most likely state at a given temperature of the surroundingsTo reach this distribution the Markov process needs to obey two important conditionsthat of ergodicity and detailed balance These conditions impose then constraints onour algorithms for accepting or rejecting new random states The Metropolis algorithmdiscussed here abides to both these constraints The Metropolis algorithm is widelyused in Monte Carlo simulations and the understanding of it rests within theinterpretation of random walks and Markov processes
Computational Physics II FYS4410
Qubic spline interpolation
Brownian Motion and Markov Processes
In a random walk one defines a mathematical entity called a walker whose attributescompletely define the state of the system in question The state of the system can referto any physical quantities from the vibrational state of a molecule specified by a set ofquantum numbers to the brands of coffee in your favourite supermarketThe walker moves in an appropriate state space by a combination of deterministic andrandom displacements from its previous position
This sequence of steps forms a chain
Computational Physics II FYS4410
Qubic spline interpolation
A simple Example
The obvious case is that of a random walker on a one- or two- or three-dimensionallattice (dubbed coordinate space hereafter)Consider a system whose energy is defined by the orientation of single spins Considerthe state i with given energy Ei represented by the following N spins
uarr uarr uarr uarr darr uarr uarr darr1 2 3 k minus 1 k k + 1 N minus 1 N
We may be interested in the transition with one single spinflip to a new state j withenergy Ej
uarr uarr uarr uarr uarr uarr uarr darr1 2 3 k minus 1 k k + 1 N minus 1 N
This change from one microstate i (or spin configuration) to another microstate j is the
configuration space analogue to a random walk on a lattice Instead of jumping from
one place to another in space we rsquojumprsquo from one microstate to another
Computational Physics II FYS4410
Qubic spline interpolation
Brownian Motion and Markov Processes
We wish to study the time-development of a PDF after a given number of time stepsWe define our PDF by the function w(t) In addition we define a transition probabilityW The time development of our PDF w(t) after one time-step from t = 0 is given by
wi (t = ε) = W (j rarr i)wj (t = 0)
This equation represents the discretized time-development of an original PDF We canrewrite this as a
wi (t = ε) = Wij wj (t = 0)
with the transition matrix W for a random walk left or right (cannot stay in the sameposition) given by
Wij (ε) = W (il minus jl ε) =
12 |i minus j| = 10 else
We call Wij for the transition probability and we represent it as a matrix
Computational Physics II FYS4410
Qubic spline interpolation
Brownian Motion and Markov Processes
Both W and w represent probabilities and they have to be normalized meaning thatthat at each time step we have X
i
wi (t) = 1
and Xj
W (j rarr i) = 1
Further constraints are 0 le Wij le 1 and 0 le wj le 1 We can thus write the action of Was
wi (t + 1) =X
j
Wij wj (t)
or as vector-matrix relationw(t + 1) = Ww(t)
and if we have that ||w(t + 1)minus w(t)|| rarr 0 we say that we have reached the mostlikely state of the system the so-called steady state or equilibrium state Another wayof phrasing this is
w(t = infin) = Ww(t = infin)
Computational Physics II FYS4410
Qubic spline interpolation
Brownian Motion and Markov Processes a simpleExample
Consider the simple 3times 3 matrix W
W =
0 14 18 2334 58 00 14 13
1A
and we choose our initial state as
w(t = 0) =
0 100
1A
The first iteration iswi (t = ε) = W (j rarr i)wj (t = 0)
resulting in
w(t = ε) =
0 14340
1A
Computational Physics II FYS4410
Qubic spline interpolation
Brownian Motion and Markov Processes a simpleExample
The next iteration results in
wi (t = 2ε) = W (j rarr i)wj (t = ε)
resulting in
w(t = 2ε) =
0 5232132632
1A
Note that the vector w is always normalized to 1 We find the steady state of thesystem by solving the linear set of equations
w(t = infin) = Ww(t = infin)
Computational Physics II FYS4410
Qubic spline interpolation
Brownian Motion and Markov Processes a simpleExample
This linear set of equations reads
W11w1(t = infin) + W12w2(t = infin) + W13w3(t = infin) = w1(t = infin)
W21w1(t = infin) + W22w2(t = infin) + W23w3(t = infin) = w2(t = infin)
W31w1(t = infin) + W32w2(t = infin) + W33w3(t = infin) = w3(t = infin)
(1)
with the constraint that Xi
wi (t = infin) = 1
yielding as solution
w(t = infin) =
0 415815315
1A
Computational Physics II FYS4410
Qubic spline interpolation
Brownian Motion and Markov Processes a simpleExample
Convergence of the simple example
Iteration w1 w2 w30 100000 000000 0000001 025000 075000 0000002 015625 062625 0187503 024609 052734 0226564 027848 051416 0207365 027213 053021 0197666 026608 053548 0198447 026575 053424 0200028 026656 053321 0200239 026678 053318 020005
10 026671 053332 01999811 026666 053335 02000012 026666 053334 02000013 026667 053333 020000
w(t = infin) 026667 053333 020000
Exercise make a small program where you perform these iterationsbut change the
initial vector and study the convergence
Computational Physics II FYS4410
Qubic spline interpolation
Brownian Motion and Markov Processes what ishappening
We have after t-stepsw(t) = Wtw(0)
with w(0) the distribution at t = 0 and W representing the transition probability matrixWe can always expand w(0) in terms of the right eigenvectors v of W as
w(0) =X
i
αi v i
resulting inw(t) = Wt w(0) = Wt
Xi
αi v i =X
i
λtiαi v i
with λi the i th eigenvalue corresponding to the eigenvector v i
Computational Physics II FYS4410
Qubic spline interpolation
Brownian Motion and Markov Processes what ishappening
If we assume that λ0 is the largest eigenvector we see that in the limit t rarrinfin w(t)becomes proportional to the corresponding eigenvector v0 This is our steady state orfinal distributionIn our discussion below in connection with the entropy of a system and tomorrowrsquoslecture on physics applications we will relate these properties to correlation functionssuch as the time-correlation function
That will allow us to define the so-called equilibration timeviz the time needed for the
system to reach its most likely state Form that state and on we can can compute
contributions to various statistical variables
Computational Physics II FYS4410
Qubic spline interpolation
Brownian Motion and Markov Processes what ishappening
We can relate this property to an observable like the mean magnetization of say amagnetic material With the probabilty w(t) we can write the mean magnetization as
〈M(t)〉 =Xmicro
w(t)microMmicro
or as the scalar of a vector product
〈M(t)〉 = w(t)m
with m being the vector whose elements are the values of Mmicro in its variousmicrostates microRecall our definition of an expectation value with a discrete PDF p(xi )
E [xk ] = 〈xk 〉 =1
N
NXi=1
xki p(xi )
provided that the sums (or integrals)PN
i=1 p(xi ) converge absolutely (viz PN
i=1 |p(xi )|converges)
Computational Physics II FYS4410
Qubic spline interpolation
Brownian Motion and Markov Processes what ishappening
We rewrite the last relation as
〈M(t)〉 = w(t)m =X
i
λtiαi v i m i
If we define mi = v i m i as the expectation value of M in the i th eigenstate we canrewrite the last equation as
〈M(t)〉 =X
i
λtiαi mi
Since we have that in the limit t rarrinfin the mean magnetization is dominated by thelargest eigenvalue λ0 we can rewrite the last equation as
〈M(t)〉 = 〈M(infin)〉+Xi 6=0
λtiαi mi
Computational Physics II FYS4410
Qubic spline interpolation
Brownian Motion and Markov Processes what ishappening
We define the quantity
τi = minus1
logλi
and rewrite the last expectation value as
〈M(t)〉 = 〈M(infin)〉+Xi 6=0
αi mi eminustτi
The quantities τi are the correlation times for the system They control also thetime-correlation functions to be discussed later
The longest correlation time is obviously given by the second largest eigenvalue τ1
which normally defines the correlation time discussed above For large times this is
the only correlation time that survives If higher eigenvalues of the transition matrix are
well separated from λ1 and we simulate long enough τ1 may well define the
correlation time In other cases we may not be able to extract a reliable result for τ1
Computational Physics II FYS4410
Qubic spline interpolation
Detailed BalanceAn important condition we require is that our Markov chain should satisfy is that ofdetailed balance In statistical physics this condition ensures that it is eg theBoltzmann distribution which is generated when equilibrium is reached The definitionfor being in equilibrium is that the rates at which a system makes a transition to or froma given state i have to be equal that isX
i
W (j rarr i)wj =X
i
W (i rarr j)wi
However the condition that the rates should equal each other is in general not sufficientto guarantee that we after many simulations generate the correct distribution Wetherefore introduce an additional condition namely that of detailed balance
W (j rarr i)wj = W (i rarr j)wi
At equilibrium detailed balance gives thus
W (j rarr i)
W (i rarr j)=
wi
wj
Proof Xi
W (i rarr j)wi =X
i
W (j rarr i)wj = wj
Xi
W (j rarr i) = wj
In a Markov process w is known while W is the unknown
Computational Physics II FYS4410
Qubic spline interpolation
Ergodicity
It should be possible for any Markov process to reach everypossible state of the system from any starting point if thesimulations is carried out for a long enough time If any state ina distribution which has a probability different from zero andthis state cannot be reached from any given starting point if wesimulate long enough then the system is not ergodic
Computational Physics II FYS4410
Qubic spline interpolation
The Boltzmann Distribution as Example
We introduce the Boltzmann distribution
wi =exp (minusβ(Ei ))
Z
which states that probability of finding the system in a state i with energy Ei at aninverse temperature β = 1kBT is wi prop exp (minusβ(Ei )) The denominator Z is anormalization constant which ensures that the sum of all probabilities is normalized toone It is defined as the sum of probabilities over all microstates j of the system
Z =X
j
exp (minusβ(Ei ))
From the partition function we can in principle generate all interesting quantities for a
given system in equilibrium with its surroundings at a temperature T
Computational Physics II FYS4410
Qubic spline interpolation
Boltzmann Distribution as Example
With the probability distribution given by the Boltzmann distribution we are now in theposition where we can generate expectation values for a given variable A through thedefinition
〈A〉 =X
j
Aj wj =
Pj Aj exp (minusβ(Ej )
Z
In general most systems have an infinity of microstates making thereby thecomputation of Z practically impossible and a brute force Monte Carlo calculation overa given number of randomly selected microstates may therefore not yield thosemicrostates which are important at equilibrium To select the most importantcontributions we need to use the condition for detailed balance Since this is just givenby the ratios of probabilities we never need to evaluate the partition function Z For theBoltzmann distribution detailed balance results in
wi
wj= exp (minusβ(Ei minus Ej ))
Computational Physics II FYS4410
Qubic spline interpolation
Boltzmann Distribution as Example
Let us now specialize to a system whose energy is defined by the orientation of singlespins Consider the state i with given energy Ei represented by the following N spins
uarr uarr uarr uarr darr uarr uarr darr1 2 3 k minus 1 k k + 1 N minus 1 N
We are interested in the transition with one single spinflip to a new state j with energyEj
uarr uarr uarr uarr uarr uarr uarr darr1 2 3 k minus 1 k k + 1 N minus 1 N
We saw previously that this change from one microstate i (or spin configuration) to
another microstate j is the configuration space analogue to a random walk on a lattice
Computational Physics II FYS4410
Qubic spline interpolation
Boltzmann Distribution as Example
However the selection of states has to generate a final distribution which is theBoltzmann distribution This is again the same we saw for a random walker for thediscrete case we had always a binomial distribution whereas for the continuous casewe had a normal distribution The way we sample configurations should result in whenequilibrium is established in the Boltzmann distribution Else our algorithm forselecting microstates has to be wrongSince we do not know the analytic form of the transition rate we are free to model it as
W (i rarr j) = g(i rarr j)A(i rarr j)
where g is a selection probability while A is the probability for accepting a move It is
also called the acceptance ratio
Computational Physics II FYS4410
Qubic spline interpolation
Boltzmann Distribution as Example
The selection probability should be same for all possible spin orientations namely
g(i rarr j) =1
N
With detailed balance this gives
g(j rarr i)A(j rarr i)
g(i rarr j)A(i rarr j)= exp (minusβ(Ei minus Ej ))
but since the selection ratio is the same for both transitions we have
A(j rarr i)
A(i rarr j)= exp (minusβ(Ei minus Ej ))
In general we are looking for those spin orientations which correspond to the average
energy at equilibrium
Computational Physics II FYS4410
Qubic spline interpolation
Boltzmann Distribution as Example
We are in this case interested in a new state Ej whose energy is lower than Ei viz∆E = Ej minus Ei le 0 A simple test would then be to accept only those microstates whichlower the energy Suppose we have ten microstates with energyE0 le E1 le E2 le E3 le middot middot middot le E9 Our desired energy is E0 At a given temperature Twe start our simulation by randomly choosing state E9 Flipping spins we may then finda path from E9 rarr E8 rarr E7 middot middot middot rarr E1 rarr E0 This would however lead to biasedstatistical averages since it would violate the ergodic hypothesis discussed above Thisprinciple states that it should be possible for any Markov process to reach everypossible state of the system from any starting point if the simulations is carried out for along enough time
Computational Physics II FYS4410
Qubic spline interpolation
Boltzmann Distribution as Example
Any state in a Boltzmann distribution has a probability different from zero and if such astate cannot be reached from a given starting point then the system is not ergodicThis means that another possible path to E0 could beE9 rarr E7 rarr E8 middot middot middot rarr E9 rarr E5 rarr E0 and so forth Even though such a path couldhave a negligible probability it is still a possibility and if we simulate long enough itshould be included in our computation of an expectation value
Thus we require that our algorithm should satisfy the principle of detailed balance and
be ergodic One possible way is the Metropolis algorithm
Computational Physics II FYS4410
Qubic spline interpolation
Metropolis Algorithm
The equation for detailed balance
A(microrarr ν)
A(ν rarr micro)= exp (minusβ(Eν minus Emicro))
is general and we could replace the Boltzmann distribution with other distributions aswell It specifies the ratio of pairs of acceptance probabilities which leaves us withquite some room to manouvre
We give the largest of the two acceptance ratios the value 1 and adjust the otherto satisfy the constraint
If Emicro lt Eν then the larger of the two acceptance ratios is A(ν rarr micro) and we setto 1
Then we must have
A(microrarr ν) = exp (minusβ(Eν minus Emicro))
if Eν minus Emicro gt 0 and 1 otherwise And that is the Metropolis algorithm
Computational Physics II FYS4410
Qubic spline interpolation
More general Definition
A Markov chain Monte Carlo method for the simulation of a distribution p is any methodproducing an ergodic Markov chain of events x whose stationary distribution is p
Generate an initial value x (i)
Generate a trial value yt with probability f (yt |x (i))
Take a new value
x (i+1) =
yt with probability= ρ(x (i) yt )
x (i) with probability= 1minus ρ(x (i) yt )
We have defined
ρ(x y) = min
p(y)f (x |y)
p(x)f (y |x) 1ff
The distribution f is often called the instrumental (we will relate it to the jumpingof a walker) or proposal distribution while ρ is the Metropolis-Hastingsacceptance probability When f (y |x) is symmetric it is just called the Metropolisalgo
Computational Physics II FYS4410
Qubic spline interpolation
Implementation
Establish an initial state with some selected features to test
Do a random change of this initial state
Compute the Metropolis-Hastings acceptance probability ρ
Compare ρ with a random number r If r le ρ accept else keepthe old configuration
Compute the terms needed to obtain expectations values
Repeat the above steps in order to have as good statistics aspossible
For a given number of MC cycles compute then the finalexpectation values
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo
Most quantum mechanical problems of interest in eg atomic molecular nuclear andsolid state physics consist of a large number of interacting electrons and ions ornucleons The total number of particles N is usually sufficiently large that an exactsolution cannot be found Typically the expectation value for a chosen hamiltonian for asystem of N particles is
〈H〉 =RdR1dR2 dRNΨlowast(R1R2 RN)H(R1R2 RN)Ψ(R1R2 RN)R
dR1dR2 dRNΨlowast(R1R2 RN)Ψ(R1R2 RN)
an in general intractable problem an in general intractable problem
This integral is actually the starting point in a Variational Monte Carlo calculation
Gaussian quadrature Forget it given 10 particles and 10 mesh points for each
degree of freedom and an ideal 1 Tflops machine (all operations take the same time)
how long will it ta ke to compute the above integral Lifetime of the universe
T asymp 47times 1017s
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo
Given a hamiltonian H and a trial wave function ΨT the variational principle states thatthe expectation value of 〈H〉 defined through
E [H] = 〈H〉 =
RdRΨlowastT (R)H(R)ΨT (R)R
dRΨlowastT (R)ΨT (R)
is an upper bound to the ground state energy E0 of the hamiltonian H that is
E0 le 〈H〉
In general the integrals involved in the calculation of various expectation values are
multi-dimensional ones Traditional integration methods such as the Gauss-Legendre
will not be adequate for say the computation of the energy of a many-body system
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo
The trial wave function can be expanded in the eigenstates of the hamiltonian sincethey form a complete set viz
ΨT (R) =X
i
aiΨi (R)
and assuming the set of eigenfunctions to be normalized one obtainsPnm alowastman
RdRΨlowastm(R)H(R)Ψn(R)P
nm alowastmanR
dRΨlowastm(R)Ψn(R)=
Pn a2
nEnPn a2
nge E0
where we used that H(R)Ψn(R) = EnΨn(R) In general the integrals involved in the
calculation of various expectation values are multi-dimensional ones The variational
principle yields the lowest state of a given symmetry
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo
In most cases a wave function has only small values in large parts of configurationspace and a straightforward procedure which uses homogenously distributed randompoints in configuration space will most likely lead to poor results This may suggest thatsome kind of importance sampling combined with eg the Metropolis algorithm maybe a more efficient way of obtaining the ground state energy The hope is then thatthose regions of configurations space where the wave function assumes appreciablevalues are sampled more efficiently
The tedious part in a VMC calculation is the search for the variational minimum A
good knowledge of the system is required in order to carry out reasonable VMC
calculations This is not always the case and often VMC calculations serve rather as
the starting point for so-called diffusion Monte Carlo calculations (DMC) DMC is a way
of solving exactly the many-body Schrodinger equation by means of a stochastic
procedure A good guess on the binding energy and its wave function is however
necessary A carefully performed VMC calculation can aid in this context
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo
Construct first a trial wave function ψαT (R) for a many-body system consisting of
N particles located at positions R = (R1 RN) The trial wave functiondepends on α variational parameters α = (α1 αN)
Then we evaluate the expectation value of the hamiltonian H
E [H] = 〈H〉 =
RdRΨlowastTα
(R)H(R)ΨTα(R)R
dRΨlowastTα(R)ΨTα
(R)
Thereafter we vary α according to some minimization algorithm and return to thefirst step
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo
Choose a trial wave function ψT (R)
P(R) =|ψT (R)|2R|ψT (R)|2 dR
This is our new probability distribution function (PDF) The approximation to theexpectation value of the Hamiltonian is now
E [H] asympR
dRΨlowastT (R)H(R)ΨT (R)RdRΨlowastT (R)ΨT (R)
Define a new quantity
EL(R) =1
ψT (R)HψT (R)
called the local energy which together with our trial PDF yields
E [H] = 〈H〉 asympZ
P(R)EL(R)dR asymp1
N
NXi=1
P(Ri)EL(Ri)
with N being the number of Monte Carlo samples
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo
Algo
Initialisation Fix the number of Monte Carlo steps Choose an initial R andvariational parameters α and calculate
˛ψα
T (R)˛2
Initialise the energy and the variance and start the Monte Carlo calculation(thermalize)
1 Calculate a trial position Rp = R + r lowast step where r is a random variabler isin [0 1]
2 Metropolis algorithm to accept or reject this move
w = P(Rp)P(R)
3 If the step is accepted then we set R = Rp Update averages
Finish and compute final averages
Observe that the jumping in space is governed by the variable step Called brute-force
sampling Need importance sampling
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo
The radial Schrodinger equation for the hydrogen atom can be written as
minus~2
2m
part2u(r)
partr2minusbdquo
ke2
rminus
~2l(l + 1)
2mr2
laquou(r) = Eu(r)
or with dimensionless variables
minus1
2
part2u(ρ)
partρ2minus
u(ρ)
ρ+
l(l + 1)
2ρ2u(ρ)minus λu(ρ) = 0
with the hamiltonian
H = minus1
2
part2
partρ2minus
1
ρ+
l(l + 1)
2ρ2
Use variational parameter α in the trial wave function
uαT (ρ) = αρeminusαρ
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo
Inserting this wave function into the expression for the local energy EL gives
EL(ρ) = minus1
ρminusα
2
bdquoαminus
2
ρ
laquo
α 〈H〉 σ2 σradic
N700000E-01 -457759E-01 451201E-02 671715E-04800000E-01 -481461E-01 305736E-02 552934E-04900000E-01 -495899E-01 820497E-03 286443E-04100000E-00 -500000E-01 000000E+00 000000E+00110000E+00 -493738E-01 116989E-02 342036E-04120000E+00 -475563E-01 885899E-02 941222E-04130000E+00 -454341E-01 145171E-01 120487E-03
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo
We note that at α = 1 we obtain the exact result and the variance is zero as it shouldThe reason is that we then have the exact wave function and the action of thehamiltionan on the wave function
Hψ = constanttimes ψ
yields just a constant The integral which defines various expectation values involvingmoments of the hamiltonian becomes then
〈Hn〉 =
RdRΨlowastT (R)Hn(R)ΨT (R)R
dRΨlowastT (R)ΨT (R)= constanttimes
RdRΨlowastT (R)ΨT (R)RdRΨlowastT (R)ΨT (R)
= constant
This gives an important information the exact wave function leads to zero
variance Variation is then performed by minimizing both the energy and the variance
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo
The helium atom consists of two electrons and a nucleus with charge Z = 2 Thecontribution to the potential energy due to the attraction from the nucleus is
minus2ke2
r1minus
2ke2
r2
and if we add the repulsion arising from the two interacting electrons we obtain thepotential energy
V (r1 r2) = minus2ke2
r1minus
2ke2
r2+
ke2
r12
with the electrons separated at a distance r12 = |r1 minus r2|
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo
The hamiltonian becomes then
bH = minus~2nabla2
1
2mminus
~2nabla22
2mminus
2ke2
r1minus
2ke2
r2+
ke2
r12
and Schrodingers equation reads bHψ = Eψ
All observables are evaluated with respect to the probability distribution
P(R) =|ψT (R)|2R|ψT (R)|2 dR
generated by the trial wave function The trial wave function must approximate an exact
eigenstate in order that accurate results are to be obtained Improved trial wave
functions also improve the importance sampling reducing the cost of obtaining a
certain statistical accuracy
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo
Choice of trial wave function for Helium Assume r1 rarr 0
EL(R) =1
ψT (R)HψT (R) =
1
ψT (R)
bdquominus
1
2nabla2
1 minusZ
r1
laquoψT (R) + finite terms
EL(R) =1
RT (r1)
minus
1
2
d2
dr21
minus1
r1
d
dr1minus
Z
r1
RT (r1) + finite terms
For small values of r1 the terms which dominate are
limr1rarr0
EL(R) =1
RT (r1)
bdquominus
1
r1
d
dr1minus
Z
r1
laquoRT (r1)
since the second derivative does not diverge due to the finiteness of Ψ at the origin
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo
This results in1
RT (r1)
dRT (r1)
dr1= minusZ
andRT (r1) prop eminusZr1
A similar condition applies to electron 2 as well For orbital momenta l gt 0 we have
1
RT (r)
dRT (r)
dr= minus
Z
l + 1
Similalry studying the case r12 rarr 0 we can write a possible trial wave function as
ψT (R) = eminusα(r1+r2)eβr12
The last equation can be generalized to
ψT (R) = φ(r1)φ(r2) φ(rN)Yiltj
f (rij )
for a system with N electrons or particles
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo and Hartree-Fock
What do we want to do and where do we stand
In Hartree-Fock for helium we have reduced the six dimensional integral to aone-dimensional one We solve an integro-differential equation
The variational HF procedure finds a minimum for the energy by varying thewave function (in our case the parameter α in eminusαri of the single-particle wavefunction) This parameter has the physical interpretation of an effective charge
In variational Monte Carlo with no correlation part we should get the sameresults as the Hartree-Fock code if we start with the same guess for the Slaterdeterminant
But in VMC we compute by stochastic methods the full multi-dimensionalintegral
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo and Hartree-Fock
What do we want to do and where do we stand
Our first step is to perform a VMC calculation with the same Slater determinantas we did for helium beryllium and neon for the Hartree-Fock calculations Thatis the wave function is
ψT (R) = φ(r1)φ(r2) φ(rN)
for a system with N electrons where the single-particle functions are thehydrogen-like functions we start the Hartree-Fock equations with
We discuss the parallel VMC code vmc paracpp which implements this
In addition we discuss the statistical analysis of the Monte Carlo dato by theso-called Blocking technique and the code vmc blockingcpp
We do a brute force Metropolis sampling meaning that the points we select forthe integral are given by the uniform distribution
Computational Physics II FYS4410
Qubic spline interpolation
Quantum Monte Carlo and Hartree-Fock
What do we want to do and where do we stand
We will then replace the hydrogen-like wave functions with the optimalHartree-Fock equations You should get the same result as the Hartree-Fockcalculation (test of having implemented the new wave functions correctly)
We will need to interpolate in an array (next thursday) by using splines seechapter 6 of lecture notes The spline code is available in the c++ or F95 libraries
We will add the correlation part to the trial wave function
ψT (R) = φ(r1)φ(r2) φ(rN)Yiltj
f (rij )
and perform the variational Monte Carlo calculation on the correlation part onlyWe will use only one variational parameter The results with its standarddeviation willbe parametrized using the singular value decomposition method
We will need to improve the brute force technique with importance sampling
Finally we need to code a Slater determinant for the Monte Carlo calculation
Computational Physics II FYS4410
Qubic spline interpolation
Status of Hartree-Fock
What do we want to do and where do we stand
Many of you have working Hartree-Fock codes
If your code works jump to the VMC part
Else use today to finish the Hartree-Fock part
Computational Physics II FYS4410
Qubic spline interpolation
VMC code for helium vmc paracpp
Here we define global variables used in various functions These can be changed by reading from file the different parametersint dimension = 3 three-dimensional systemint charge = 2 we fix the charge to be that of the helium atomint my_rank numprocs these are the parameters used by MPI to
define which node and how manydouble step_length = 10 we fix the brute force jump to 1 Bohr radiusint number_particles = 2 we fix also the number of electrons to be 2
Computational Physics II FYS4410
Qubic spline interpolation
VMC code for helium vmc paracpp main part
MPI initializationsMPI_Init (ampargc ampargv)MPI_Comm_size (MPI_COMM_WORLD ampnumprocs)MPI_Comm_rank (MPI_COMM_WORLD ampmy_rank)time_start = MPI_Wtime()
if (my_rank == 0 ampamp argc lt= 2) cout ltlt Bad Usage ltlt argv[0] ltlt
read also output file on same line ltlt endlexit(1)
if (my_rank == 0 ampamp argc gt 2)
outfilename=argv[1]ofileopen(outfilename)
Computational Physics II FYS4410
Qubic spline interpolation
VMC code for helium vmc paracpp main part
Setting output file name for this rankostringstream ostost ltlt blocks_rank ltlt my_rank ltlt dat Open file for writingblockofileopen(oststr()c_str() iosout | iosbinary)
total_cumulative_e = new double[max_variations+1]total_cumulative_e2 = new double[max_variations+1]cumulative_e = new double[max_variations+1]cumulative_e2 = new double[max_variations+1]
initialize the arrays by zeroing themfor( i=1 i lt= max_variations i++)
cumulative_e[i] = cumulative_e2[i] = 00total_cumulative_e[i] = total_cumulative_e2[i] = 00
Computational Physics II FYS4410
Qubic spline interpolation
VMC code for helium vmc paracpp main part
broadcast the total number of variationsMPI_Bcast (ampmax_variations 1 MPI_INT 0 MPI_COMM_WORLD)MPI_Bcast (ampnumber_cycles 1 MPI_INT 0 MPI_COMM_WORLD)total_number_cycles = number_cyclesnumprocs array to store all energies for last variation of alphaall_energies = new double[number_cycles+1] Do the mc sampling and accumulate data with MPI_Reducemc_sampling(max_variations number_cycles cumulative_e
cumulative_e2 all_energies) Collect data in total averagesfor( i=1 i lt= max_variations i++)
MPI_Reduce(ampcumulative_e[i] amptotal_cumulative_e[i] 1 MPI_DOUBLEMPI_SUM 0 MPI_COMM_WORLD)
MPI_Reduce(ampcumulative_e2[i] amptotal_cumulative_e2[i] 1 MPI_DOUBLEMPI_SUM 0 MPI_COMM_WORLD)
Computational Physics II FYS4410
Qubic spline interpolation
VMC code for helium vmc paracpp main part
blockofilewrite((char)(all_energies+1)number_cyclessizeof(double))
blockofileclose()delete [] total_cumulative_e delete [] total_cumulative_e2delete [] cumulative_e delete [] cumulative_e2 delete [] all_energies End MPIMPI_Finalize ()return 0 end of main function
Computational Physics II FYS4410
Qubic spline interpolation
VMC code for helium vmc paracpp sampling
alpha = 05charge every node has its own seed for the random numbersidum = -1-my_rank allocate matrices which contain the position of the particlesr_old =(double )matrix(number_particlesdimensionsizeof(double))r_new =(double )matrix(number_particlesdimensionsizeof(double))for (i = 0 i lt number_particles i++)
for ( j=0 j lt dimension j++) r_old[i][j] = r_new[i][j] = 0
loop over variational parameters
Computational Physics II FYS4410
Qubic spline interpolation
VMC code for helium vmc paracpp sampling
for (variate=1 variate lt= max_variations variate++) initialisations of variational parameters and energiesalpha += 01energy = energy2 = 0 accept =0 delta_e=0 initial trial position note calling with alphafor (i = 0 i lt number_particles i++)
for ( j=0 j lt dimension j++) r_old[i][j] = step_length(ran2(ampidum)-05)
wfold = wave_function(r_old alpha)
Computational Physics II FYS4410
Qubic spline interpolation
VMC code for helium vmc paracpp sampling
loop over monte carlo cyclesfor (cycles = 1 cycles lt= number_cycles cycles++)
new positionfor (i = 0 i lt number_particles i++)
for ( j=0 j lt dimension j++) r_new[i][j] = r_old[i][j]+step_length(ran2(ampidum)-05)
for the other particles we need to set the position to the old position since we move only one particle at the time
for (k = 0 k lt number_particles k++) if ( k = i)
for ( j=0 j lt dimension j++) r_new[k][j] = r_old[k][j]
wfnew = wave_function(r_new alpha)
The Metropolis test is performed by moving one particle at the timeif(ran2(ampidum) lt= wfnewwfnewwfoldwfold )
for ( j=0 j lt dimension j++) r_old[i][j]=r_new[i][j]
wfold = wfnew
end of loop over particles
Computational Physics II FYS4410
Qubic spline interpolation
VMC code for helium vmc paracpp sampling
compute local energydelta_e = local_energy(r_old alpha wfold) save all energies on last variateif(variate==max_variations)
all_energies[cycles] = delta_e update energiesenergy += delta_eenergy2 += delta_edelta_e
end of loop over MC trials update the energy average and its squaredcumulative_e[variate] = energycumulative_e2[variate] = energy2
end of loop over variational steps
Computational Physics II FYS4410
Qubic spline interpolation
VMC code for helium vmc paracpp wave function
Function to compute the squared wave function simplest form
double wave_function(double r double alpha)
int i j kdouble wf argument r_single_particle r_12
argument = wf = 0for (i = 0 i lt number_particles i++)
r_single_particle = 0for (j = 0 j lt dimension j++)
r_single_particle += r[i][j]r[i][j]argument += sqrt(r_single_particle)
wf = exp(-argumentalpha) return wf
Computational Physics II FYS4410
Qubic spline interpolation
VMC code for helium vmc paracpp local energy
Function to calculate the local energy with num derivative
double local_energy(double r double alpha double wfold)
int i j kdouble e_local wfminus wfplus e_kinetic e_potential r_12
r_single_particledouble r_plus r_minus
allocate matrices which contain the position of the particles the function matrix is defined in the progam libraryr_plus =(double )matrix(number_particlesdimensionsizeof(double))r_minus =(double )matrix(number_particlesdimensionsizeof(double))for (i = 0 i lt number_particles i++)
for ( j=0 j lt dimension j++) r_plus[i][j] = r_minus[i][j] = r[i][j]
Computational Physics II FYS4410
Qubic spline interpolation
VMC code for helium vmc paracpp local energy
compute the kinetic energye_kinetic = 0for (i = 0 i lt number_particles i++)
for (j = 0 j lt dimension j++) r_plus[i][j] = r[i][j]+hr_minus[i][j] = r[i][j]-hwfminus = wave_function(r_minus alpha)wfplus = wave_function(r_plus alpha)e_kinetic -= (wfminus+wfplus-2wfold)r_plus[i][j] = r[i][j]r_minus[i][j] = r[i][j]
include electron mass and hbar squared and divide by wave functione_kinetic = 05h2e_kineticwfold
Computational Physics II FYS4410
Qubic spline interpolation
VMC code for helium vmc paracpp local energy
compute the potential energye_potential = 0 contribution from electron-proton potentialfor (i = 0 i lt number_particles i++)
r_single_particle = 0for (j = 0 j lt dimension j++)
r_single_particle += r[i][j]r[i][j]e_potential -= chargesqrt(r_single_particle)
contribution from electron-electron potentialfor (i = 0 i lt number_particles-1 i++)
for (j = i+1 j lt number_particles j++) r_12 = 0for (k = 0 k lt dimension k++)
r_12 += (r[i][k]-r[j][k])(r[i][k]-r[j][k])e_potential += 1sqrt(r_12)
Computational Physics II FYS4410
Qubic spline interpolation
Topics for Week 9 February 25-29
Monte Carlo Blocking and Importance Sampling
Thursday
Discussion of the spline method to interpolate data
Importance sampling
Lab Repeat of parallelization
Lab Discussion of Jon Nilsenrsquos blocking code seeadditional slides
Lab Discussion of variational Monte Carlo and the codesvmc paracpp and vmc blockingcpp
Friday
Variational Monte Carlo theoretical background forimportance sampling
Background chapters 8-11 in Lecture notes Emphasis onchapter 11
Computational Physics II FYS4410
Qubic spline interpolation
Merging Hartree-Fock with the Monte Carlo code
Things to do
Helium first write out the final HF wave functions to filewith mesh points and values of the wave function
Read this in from file in the VMC code (or merge the twocodes Every node must have access to the full wf)
Implement the spline method to interpolate in your dataset the spline functions are declared in libh and defined inlibcpp
Run just a brute force Monte Carlo with the Slaterdeterminant as your trial function See that you get thesame energy as the HF code gives
Computational Physics II FYS4410
Qubic spline interpolation
Merging Hartree-Fock with the Monte Carlo code
Things to do
Now change the trial wave function to include thecorrelation part as discussed in the project Run thecalculation for various values of β and try to find avariational minimum Use some few millions of cycles
Implement then blocking and perform a statistical analysisof the data
Implement importance sampling
If everything ok submit your production run
Repeat for Beryllium (next week HF= -1452 au)Remember to orthogonalize 1s and 2s in HF
Slater det and VMC (Neon HF= -12828 au) and finalsmoothing of data with data fitting procedures Beware ofcomplex spherical harmonics
Computational Physics II FYS4410
Qubic spline interpolation
Orthogonalize 1s and 2s States (why not 2p)
Simple way to orthogonalize (pseudo code)
if ( state == 2s) thensum = 0 overlap between 1s and 2sfor i = 1 meshpoints
sum = sum + psi1s(i)psi2s(i)end forsum = sumstep project out unwanted solutionfor i = 1 meshpoints
psi2s(i) = psi2s(i) - sumpsi1s(i)end for
Computational Physics II FYS4410
Qubic spline interpolation
Qubic Splines Chapter 6
Qubic spline interpolation is among one of the mostly used methods for interpolatingbetween data points where the arguments are organized as ascending series In thelibrary program we supply such a function based on the so-called qubic spline methodto be described belowA spline function consists of polynomial pieces defined on subintervals The differentsubintervals are connected via various continuity relationsAssume we have at our disposal n + 1 points x0 x1 xn arranged so thatx0 lt x1 lt x2 lt xnminus1 lt xn (such points are called knots) A spline function s ofdegree k with n + 1 knots is defined as follows
On every subinterval [ximinus1 xi ) s is a polynomial of degree le k
s has k minus 1 continuous derivatives in the whole interval [x0 xn]
Computational Physics II FYS4410
Qubic spline interpolation
Splines
As an example consider a spline function of degree k = 1 defined as follows
s(x) =
8gtgtltgtgts0(x) = a0x + b0 x isin [x0 x1)s1(x) = a1x + b1 x isin [x1 x2)
snminus1(x) = anminus1x + bnminus1 x isin [xnminus1 xn]
In this case the polynomial consists of series of straight lines connected to each other
at every endpoint The number of continuous derivatives is then k minus 1 = 0 as
expected when we deal with straight lines Such a polynomial is quite easy to construct
given n + 1 points x0 x1 xn and their corresponding function values
Computational Physics II FYS4410
Qubic spline interpolation
Splines
The most commonly used spline function is the one with k = 3 the so-called qubicspline function Assume that we have in adddition to the n + 1 knots a series offunctions values y0 = f (x0) y1 = f (x1) yn = f (xn) By definition the polynomialssiminus1 and si are thence supposed to interpolate the same point i ie
siminus1(xi ) = yi = si (xi )
with 1 le i le n minus 1 In total we have n polynomials of the type
si (x) = ai0 + ai1x + ai2x2 + ai2x3
yielding 4n coefficients to determine
Computational Physics II FYS4410
Qubic spline interpolation
Splines
Every subinterval provides in addition the 2n conditions
yi = s(xi )
ands(xi+1) = yi+1
to be fulfilled If we also assume that sprime and sprimeprime are continuous then
sprimeiminus1(xi ) = sprimei (xi )
yields n minus 1 conditions Similarly
sprimeprimeiminus1(xi ) = sprimeprimei (xi )
results in additional n minus 1 conditions In total we have 4n coefficients and 4n minus 2
equations to determine them leaving us with 2 degrees of freedom to be determined
Computational Physics II FYS4410
Qubic spline interpolation
Splines
Using the last equation we define two values for the second derivative namely
sprimeprimei (xi ) = fi
andsprimeprimei (xi+1) = fi+1
and setting up a straight line between fi and fi+1 we have
sprimeprimei (x) =fi
xi+1 minus xi(xi+1 minus x) +
fi+1
xi+1 minus xi(x minus xi )
and integrating twice one obtains
si (x) =fi
6(xi+1 minus xi )(xi+1 minus x)3 +
fi+1
6(xi+1 minus xi )(x minus xi )
3 + c(x minus xi ) + d(xi+1 minus x)
Computational Physics II FYS4410
Qubic spline interpolation
Splines
Using the conditions si (xi ) = yi and si (xi+1) = yi+1 we can in turn determine theconstants c and d resulting in
si (x) =fi
6(xi+1minusxi )(xi+1 minus x)3 +
fi+16(xi+1minusxi )
(x minus xi )3
+ (yi+1
xi+1minusximinus fi+1(xi+1minusxi )
6 )(x minus xi ) + (yi
xi+1minusximinus fi (xi+1minusxi )
6 )(xi+1 minus x) (2)
Computational Physics II FYS4410
Qubic spline interpolation
Splines
How to determine the values of the second derivatives fi and fi+1 We use thecontinuity assumption of the first derivatives
sprimeiminus1(xi ) = sprimei (xi )
and set x = xi Defining hi = xi+1 minus xi we obtain finally the following expression
himinus1fiminus1 + 2(hi + himinus1)fi + hi fi+1 =6
hi(yi+1 minus yi )minus
6
himinus1(yi minus yiminus1)
and introducing the shorthands ui = 2(hi + himinus1) vi = 6hi
(yi+1 minus yi )minus 6himinus1
(yi minus yiminus1)
we can reformulate the problem as a set of linear equations to be solved through eg
Gaussian elemination
Computational Physics II FYS4410
Qubic spline interpolation
Splines
Gaussian elimination2666664u1 h1 0 h1 u2 h2 0 0 h2 u3 h3 0
0 hnminus3 unminus2 hnminus20 hnminus2 unminus1
3777775
2666664f1f2f3
fnminus2fnminus1
3777775 =
2666664v1v2v3
vnminus2vnminus1
3777775
Note that this is a set of tridiagonal equations and can be solved through only O(n)
operations
Computational Physics II FYS4410
Qubic spline interpolation
Splines
The functions supplied in the program library are spline and splint In order to usequbic spline interpolation you need first to call
spline(double x[] double y[] int n double yp1double yp2 double y2[])
This function takes as input x [0 n minus 1] and y [0 n minus 1] containing a tabulation
yi = f (xi ) with x0 lt x1 lt lt xnminus1 together with the first derivatives of f (x) at x0 and
xnminus1 respectively Then the function returns y2[0 n minus 1] which contains the second
derivatives of f (xi ) at each point xi n is the number of points This function provides
the qubic spline interpolation for all subintervals and is called only once
Computational Physics II FYS4410
Qubic spline interpolation
Splines
Thereafter if you wish to make various interpolations you need to call the function
splint(double x[] double y[] double y2a[] int ndouble x double y)
which takes as input the tabulated values x [0 n minus 1] and y [0 n minus 1] and the
output y2a[0n - 1] from spline It returns the value y corresponding to the point x
Computational Physics II FYS4410