fys4411/fys9411 computational physics ii …...fys4411/fys9411 computational physics ii university...
TRANSCRIPT
-
FYS4411/FYS9411 Computational Physics IIUniversity of Oslo, Norway
Morten Hjorth-Jensen
1Department of Physics,University of Oslo, N-0316 Oslo, Norway
National Superconducting Cyclotron Laboratory and Department of Astronomyand Physics,
Michigan State University, East Lansing, Michigan, USA
Spring 2014
1 / 234
-
Topics for Week 3, January 13-17
Introduction, discussion of project and definitions
I Presentation of topics to be covered and definitionsI Discussion of the project and first stepsI Reading assignment for next week: study chapter 4 of
Thijssen’s text and chapter 7 of lectures notes of MHJI Computational assignment: solve numerically the
one-body hydrogen-like problem (this week and nextweek).
I Form as soon as possible collaboration groups of 2-3participants.
2 / 234
-
January 13 - May 31
Course overview, Aims and Computational aspects
I The aim is to use the Hartree-Fock method to study atomsand molecules
I The results can be used to provide potentials and chargedistributions for molecular dynamics simulations
I Eigenvalue solvers, efficient computations of integralsI Iterative methods for solutions of non-linear equationsI Object orientation and efficient parallelizationI The first part of the project (ready by March 7) deals with
Hartree-Fock calculations of closed-shell atoms like He, Beand Ne using standard hydrogen-like single-particle statefunctions.
I The second part includes optimized single-particle statefunctions and computations of both atoms and molecules
3 / 234
-
January 13 - May 31
Projects, deadlines and oral exam
1. Deadline part 1: March 72. Deadline full project: 31 May3. Oral exam: To be settled
The oral exam is based on your presentation of the project.
4 / 234
-
January 13 - May 31
More on projects
1. Keep a logbook, important for keeping track of all yourchanges etc etc.
2. The project should be written as a regular scientific article,with introduction, formalism, codes which have beendeveloped and discussion of results. Conclusions andreferences should also be included. An example can befound on the webpage of the course.
The methods are relevant for atomic, molecular,solid state,materials science, lief science, nanotechnology, quantumchemistry and nuclear physics.
5 / 234
-
Lectures and ComputerLab
I Lectures: Thursday (14.15-16, room FV329)I Detailed lecture notes, all programs presented and
projects can be found at the homepage of the course.I Computerlab: 16-19 thursday, room FV329I Weekly plans and relevant information are on the official
webpage.I Chapters 7 and 16 of the FYS3150 lecture notes give a
good starting point. Chapter 16 will also be revised. Werecommend also J. M. Thijssen text ComputationalPhysics, chapters 3-6. For MPI we recommend Gropp,Lusk and Sjellum’s text.
6 / 234
-
Thijssen’s text
J. M. Thijssen’s text
I ComputationalPhysics
I Chapters 3-6I see http://www.tn.tudelft.nl/tn/People/Staff/Thijssen/comphybook.html
7 / 234
http://www.tn.tudelft.nl/tn/People/Staff/Thijssen/comphybook.htmlhttp://www.tn.tudelft.nl/tn/People/Staff/Thijssen/comphybook.htmlhttp://www.tn.tudelft.nl/tn/People/Staff/Thijssen/comphybook.htmlhttp://www.tn.tudelft.nl/tn/People/Staff/Thijssen/comphybook.htmlhttp://www.tn.tudelft.nl/tn/People/Staff/Thijssen/comphybook.html
-
MPI text
Gropp, Lusk andSjellum
I Using MPII Chapters 1-5I seehttp://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10761
8 / 234
http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10761http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10761http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10761http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10761http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10761
-
Selected Texts and lectures on C/C++
J. J. Barton and L. R. Nackman,Scientific and Engineering C++, Addison Wesley,3rd edition 2000.
B. Stoustrup, The C++ programming language, Pearson, 1997.
George Em Karniadakis and Robert M. Kirby II, Parallel Scientific Computing inC++ and MPI http://www.cambridge.org/catalogue/catalogue.asp?isbn=9780521520805
D. Yang, C++ and Object-oriented Numeric Computing for Scientists andEngineers, Springer 2000.
More books reviewed at http:://www.accu.org/ andhttp://www.comeaucomputing.com/booklist/
9 / 234
http://www.cambridge.org/catalogue/catalogue.asp?isbn=9780521520805http://www.cambridge.org/catalogue/catalogue.asp?isbn=9780521520805http:://www.accu.org/http://www.comeaucomputing.com/booklist/
-
Definitions
The quantum mechanical wave function of a given state withquantum numbers λ (encompassing all quantum numbersneeded to specify the system), ignoring time, is with N particles(electrons in our case)
Ψλ = Ψλ(x1, x2, . . . , xN),
with xi = (ri , σi) and the projection of σi takes the values{−1/2,+1/2} for fermions with spin 1/2. We will hereafteralways refer to Ψλ as the exact wave function, and if the groundstate is not degenerate we label it as
Ψ0 = Ψ0(x1, x2, . . . , xN).
10 / 234
-
Definitions
Since the solution Ψλ seldomly can be found in closed form,approximations are sought. In these slides we define anapproximative wave function or an ansatz to the exact wavefunction as
Φλ = Φλ(x1, x2, . . . , xN),
withΦ0 = Φ0(x1, x2, . . . , xN),
being the ansatz to the ground state.
11 / 234
-
Definitions
The wave function Ψλ is sought in the Hilbert space of eithersymmetric or anti-symmetric N-body functions, namely
Ψλ ∈ HN := H1 ⊕H1 ⊕ · · · ⊕ H1,
where the single-particle Hilbert space H1 is the space ofsquare integrable functions over ∈ Rd ⊕ (σ) resulting in
H1 := L2(Rd ⊕ (σ)).
12 / 234
-
DefinitionsOur Hamiltonian is invariant under the permutation(interchange) of two fermions. Since we deal with fermionshowever, the total wave function is antisymmetric. Let P̂ be anoperator which interchanges two fermions. Due to thesymmetries we have ascribed to our Hamiltonian, this operatorcommutes with the total Hamiltonian,
[Ĥ, P̂] = 0,
meaning that Ψλ(x1, x2, . . . , xN) is an eigenfunction of P̂ as well,that is
P̂ijΨλ(x1, x2, . . . , xi , . . . , xj , . . . , xN) = βΨλ(x1, x2, . . . , xj , . . . , xi , . . . , xN),
where β is the eigenvalue of P̂. We have introduced the suffix ijin order to indicate that we permute fermions i and j . The Pauliprinciple tells us that the total wave function for a system offermions has to be antisymmetric, resulting in the eigenvalueβ = −1.
13 / 234
-
Definitions and notations
The Schrödinger equation reads
Ĥ(x1, x2, . . . , xN )Ψλ(x1, x2, . . . , xN ) = EλΨλ(x1, x2, . . . , xN ), (1)
where the vector xi represents the coordinates (spatial and spin) of particle i , λ stands
for all the quantum numbers needed to classify a given N-particle state and Ψλ is the
pertaining eigenfunction. Throughout this course, Ψ refers to the exact eigenfunction,
unless otherwise stated.
14 / 234
-
Definitions and notations
We write the Hamilton operator, or Hamiltonian, in a generic way
Ĥ = T̂ + V̂
where T̂ represents the kinetic energy of the system
T̂ =N∑
i=1
p2i2mi
=N∑
i=1
(−
~2
2mi∇i2)
=N∑
i=1
t(xi )
while the operator V̂ for the potential energy is given by
V̂ =N∑
i=1
ûext(xi ) +N∑
ji=1
v(xi , xj ) +N∑
ijk=1
v(xi , xj , xk ) + . . . (2)
Hereafter we use natural units, viz. ~ = c = e = 1, with e the elementary charge and c
the speed of light. This means that momenta and masses have dimension energy.
15 / 234
-
Definitions and notations
If one does quantum chemistry or atomic physics as we do here, after havingintroduced the Born-Oppenheimer approximation which effectively freezes out thenucleonic degrees of freedom, the Hamiltonian for N = ne electrons takes the followingform
Ĥ =ne∑
i=1
t(xi )−ne∑
i=1
kZri
+
ne∑i
-
Definitions and notations
We can rewrite this as
Ĥ = Ĥ0 + ĤI =ne∑
i=1
ĥ0(xi ) +ne∑
i
-
Definitions and notations
The potential energy term due to the attraction of the nucleus defines the one-bodyfield ui = uext(xi ) of Eq. (2). We have moved this term into the Ĥ0 part of theHamiltonian, instead of keeping it in V̂ as in Eq. (2). The reason is that we willhereafter treat Ĥ0 as our non-interacting Hamiltonian. For a many-body wavefunctionΦλ defined by an appropriate single-particle basis, we may solve exactly thenon-interacting eigenvalue problem
Ĥ0Φλ = wλΦλ,
with wλ being the non-interacting energy. This energy is defined by the sum over
single-particle energies to be defined below. For atoms the single-particle energies
could be the hydrogen-like single-particle energies corrected for the charge Z . For
nuclei and quantum dots, these energies could be given by the harmonic oscillator in
three and two dimensions, respectively.
18 / 234
-
Definitions and notations
We will assume that the interacting part of the Hamiltonian can be approximated by atwo-body interaction. This means that our Hamiltonian is written as
Ĥ = Ĥ0 + ĤI =N∑
i=1
ĥ0(xi ) +N∑
i
-
Definitions and notations
Our Hamiltonian is invariant under the permutation (interchange) of two fermions.Since we deal with fermions, the total wave function is antisymmetric. Let P̂ be anoperator which interchanges two fermions. Due to the symmetries we have ascribed toour Hamiltonian, this operator commutes with the total Hamiltonian,
[Ĥ, P̂] = 0,
meaning that Ψλ(x1, x2, . . . , xN ) is an eigenfunction of P̂ as well, that is
P̂ij Ψλ(x1, x2, . . . , xi , . . . , xj , . . . , xN ) = βΨλ(x1, x2, . . . , xi , . . . , xj , . . . , xN ),
where β is the eigenvalue of P̂. We have introduced the suffix ij in order to indicate that
we permute fermions i and j . The Pauli principle tells us that the total wave function for
a system of fermions has to be antisymmetric, resulting in the eigenvalue β = −1.
20 / 234
-
Definitions and notations
In our case we assume that we can approximate the exact eigenfunction with a Slaterdeterminant
Φ(x1, x2, . . . , xN , α, β, . . . , σ) =1√
N!
∣∣∣∣∣∣∣∣∣ψα(x1) ψα(x2) . . . . . . ψα(xN )ψβ(x1) ψβ(x2) . . . . . . ψβ(xN ). . . . . . . . . . . . . . .. . . . . . . . . . . . . . .
ψσ(x1) ψσ(x2) . . . . . . ψσ(xN )
∣∣∣∣∣∣∣∣∣ ,(7)
where xi stand for the coordinates and spin values of a particle i and α, β, . . . , γ are
quantum numbers needed to describe remaining quantum numbers.
21 / 234
-
Definitions and notations
Let us denote the ground state energy by E0. According to the variational principle wehave
E0 ≤ E [Φ] =∫
Φ∗ĤΦdτ
where Φ is a trial function which we assume to be normalized∫Φ∗Φdτ = 1,
where we have used the shorthand dτ = dx1dx2 . . . dxN .
22 / 234
-
Definitions and notations
In the Hartree-Fock method the trial function is the Slater determinant of Eq. (7) whichcan be rewritten as
Φ(x1, x2, . . . , xN , α, β, . . . , ν) =1√
N!
∑P
(−)P P̂ψα(x1)ψβ(x2) . . . ψν(xN ) =√
N!AΦH ,
(8)
where we have introduced the antisymmetrization operator A defined by thesummation over all possible permutations of two fermions.
23 / 234
-
Definition and notations
Before we proceed, we need to discuss the single-particle states which enter thedefinition of the Slater determinant. We will start with hydrogen-like orbits and write asmall program which diagonalizes the hydrogen-like problem. To achieve this, we needto refresh our knowledge about the Schrödinger equation for the hydrogen atom.To solve the Schrödinger equation as a matrix diagonalization problem, let us study theradial part of the Schrödinger equation. The radial part of the wave function, R(r), is asolution to
−~2
2m
(1r2
ddr
r2ddr−
l(l + 1)r2
)R(r) + V (r)R(r) = ER(r).
In our case V (r) = −Z/r .
24 / 234
-
Definition and notations
Then we substitute R(r) = (1/r)u(r) and obtain
−~2
2md2
dr2u(r) +
(V (r) +
l(l + 1)r2
~2
2m
)u(r) = Eu(r).
We introduce a dimensionless variable ρ = (1/α)r where α is a constant withdimension length and get
−~2
2mα2d2
dρ2u(ρ) +
(V (ρ) +
l(l + 1)ρ2
~2
2mα2
)u(ρ) = Eu(ρ).
25 / 234
-
Definition and notationsWe replace ρ with x , take away the centrifugal barrier (that is setting l = 0) term andset the potential equal to
V (x) = −kZxα
,
with k = 1.44 eVnm being a constant. We multiply with mα2/~2 and since α is just aconstant, we fix it by requiring that
kZmα~2
= 1,
giving
α =~2
kZm,
which is the Bohr radius and leads to a natural length scale. This leads to the equation
−12
d2
dx2u(x)−
1x
u(x) = λu(x),
with
λ =Emα2
~2.
26 / 234
-
Definition and notations
Let us now see how we can rewrite this equation as a matrix eigenvalue problem. Firstwe need to compute the second derivative. We use here the following expression forthe second derivative of a function f
f ′′ =f (x + h)− 2f (x) + f (x − h)
h2+ O(h2),
where h is our step. Next we define minimum and maximum values for the variable x ,Rmin and Rmax, respectively. With a given number of steps, Nstep, we then define thestep h as
h =Rmax − Rmin
Nstep.
27 / 234
-
Definition and notations
We discretize x as
xi = Rmin + ih i = 1, 2, . . . ,Nstep − 1
we can rewrite the Schrödinger equation for xi as
−12
u(xk + h)− 2u(xk ) + u(xk − h)h2
−1xk
u(xk ) = λu(xk ),
or in a more compact way
−12
uk+1 − 2uk + uk−1h2
−1xk
uk = −12
uk+1 − 2uk + uk−1h2
+ Vk uk = λuk ,
where uk = u(xk ), uk±1 = u(xk ± h) and Vk = −1/xk , the given potential.
28 / 234
-
Definition and notations
Let us see how this recipe may lead to a matrix reformulation of the Schrödingerequation. Define first the diagonal matrix element
dk =1h2
+ Vk ,
and the non-diagonal matrix element
ek = −1
2h2.
In this case the non-diagonal matrix elements are given by a mere constant. Allnon-diagonal matrix elements are equal. With these definitions the Schrödingerequation takes the following form
dk uk + ek−1uk−1 + ek+1uk+1 = λuk ,
where uk is unknown.
29 / 234
-
Definition and notations
Since we have Nstep values of k we can write the latter equation as a matrix eigenvalueproblem
d1 e1 0 0 . . . 0 0e1 d2 e2 0 . . . 0 00 e2 d3 e3 0 . . . 0. . . . . . . . . . . . . . . . . . . . .0 . . . . . . . . . . . . dNstep−1 eNstep−10 . . . . . . . . . . . . eNstep−1 dNstep
u1u2. . .. . .. . .
uNstep
= λ
u1u2. . .. . .. . .
uNstep
(9)
When setting up the matrix, be careful with the endpoints. Our wave function has the
values u(0) = u(∞) = 0 and you don’t need to include these points in the equations.
30 / 234
-
Definition and notations
This is a matrix problem with a tridiagonal matrix of dimension Nstep × Nstep and willthus yield Nstep eigenvalues.The algorithm for solving this problem may take the following form
I Define values for Nstep, Rmin and Rmax. These values define in turn the step sizeh. Typical values for Rmax and Rmin could be 0 and 10 respectively for thelowest-lying states. The number of mesh points Nstep could be in the range 100to some thousands. You can check the stability of the results as functions of Nstepand Rmax and Rmin against the exact solutions.
I Construct then two one-dimensional arrays which contain all values of xk and thepotential Vk . For the latter it can be convenient to write a small function whichsets up the potential as function of xk . For the three-dimensional case you mayalso need to include the centrifugal potential.
31 / 234
-
Definition and notations
I Construct thereafter the one-dimensional vectors d and e, where d stands forthe diagonal matrix elements and e the non-diagonal ones. Be careful with theendpoints, since we know the wave function u at both ends of the chosen grid.
I We are now ready to obtain the eigenvalues by calling the function tqli whichcan be found on the web page of the course FYS3150, see fall 2013. Calling tqli,you have to transfer the vectors d and e and their dimension dimension n = Nstepand a matrix z of dimension Nstep1× Nstep which returns the eigenfunctions. Onreturn, the array d contains the eigenvalues. If z is given as the unity matrix oninput, it returns the eigenvectors. For a given eigenvalue k , the eigenvector isgiven by the column k in z, that is z[][k] in C, or z(:,k) in Fortran.
32 / 234
-
Definition and notations
I TQLI does however not return an ordered sequence of eigenvalues. You maythen need to sort them as e.g., an ascending series of numbers. The programwe provide includes a sorting function as well.
This program is discussed in chapter 7 of the FYS3150 Lecture notes.
33 / 234
-
Definition and notations
I Your task this week and next week is to set up a program which solves the aboveproblem.
I The program TQLI can be found in the lib.cpp (use also the lib.h file) at thewebpage of FYS3150, click on the program link of the Fall 2013 version of thecourse. Read also chapter 7 of the FYS3150 Lectures.
I The eigenvalue for the lowest state should be −1/2 for the hydrogen-likeproblem. You should also plot the eigenvector. Compute also the second l = 0state, both its energy and its eigenfuntion.
I Try also to include the centrifugal barrier and find the lowest eigenstate andeigenvector for l = 1 and comment your results.
I At the lab, we will also discuss how to use armadillo instead of the standardmemory allocation done in chapter 7.
34 / 234
-
Definitions and notations
It is defined asA =
1N!
∑p
(−)pP̂, (10)
with p standing for the number of permutations. We have introduced for later use theso-called Hartree-function, defined by the simple product of all possible single-particlefunctions
ΦH (x1, x2, . . . , xN , α, β, . . . , ν) = ψα(x1)ψβ(x2) . . . ψν(xN ).
35 / 234
-
Definitions and notations
Both Ĥ0 andˆ̂
IH are invariant under all possible permutations of any two fermions andhence commute with A
[H0,A] = [HI ,A] = 0. (11)
Furthermore, A satisfiesA2 = A, (12)
since every permutation of the Slater determinant reproduces it.
36 / 234
-
Definitions and notations
The expectation value of Ĥ0∫Φ∗Ĥ0Φdτ = N!
∫Φ∗HAĤ0AΦHdτ
is readily reduced to ∫Φ∗Ĥ0Φdτ = N!
∫Φ∗H Ĥ0AΦHdτ,
where we have used eqs. (11) and (12). The next step is to replace theantisymmetrization operator by its definition Eq. (8) and to replace Ĥ0 with the sum ofone-body operators
∫Φ∗Ĥ0Φdτ =
N∑i=1
∑p
(−)p∫
Φ∗H ĥ0P̂ΦHdτ.
37 / 234
-
Definitions and notations
The integral vanishes if two or more fermions are permuted in only one of theHartree-functions ΦH because the individual single-particle wave functions areorthogonal. We obtain then
∫Φ∗Ĥ0Φdτ =
N∑i=1
∫Φ∗H ĥ0ΦHdτ.
Orthogonality of the single-particle functions allows us to further simplify the integral,and we arrive at the following expression for the expectation values of the sum ofone-body Hamiltonians
∫Φ∗Ĥ0Φdτ =
N∑µ=1
∫ψ∗µ(x)ĥ0ψµ(x)dx. (13)
38 / 234
-
Definitions and notations
We introduce the following shorthand for the above integral
〈µ|ĥ0|µ〉 =∫ψ∗µ(x)ĥ0ψµ(x)dx.,
and rewrite Eq. (13) as ∫Φ∗Ĥ0Φdτ =
N∑µ=1
〈µ|ĥ0|µ〉. (14)
39 / 234
-
Definitions and notations
The expectation value of the two-body part of the Hamiltonian (assuming a two-bodyHamiltonian at most) is obtained in a similar manner. We have∫
Φ∗ĤIΦdτ = N!∫
Φ∗HAĤIAΦHdτ,
which reduces to
∫Φ∗ĤIΦdτ =
N∑i≤j=1
∑p
(−)p∫
Φ∗H v̂(xij )P̂ΦHdτ,
by following the same arguments as for the one-body Hamiltonian.
40 / 234
-
Definitions and notations
Because of the dependence on the inter-particle distance rij , permutations of any twofermions no longer vanish, and we get
∫Φ∗ĤIΦdτ =
N∑i
-
Definitions and notations
We obtain
∫Φ∗ĤIΦdτ =
12
N∑µ=1
N∑ν=1
[∫ψ∗µ(xi )ψ
∗ν(xj )v̂(xij )ψµ(xi )ψν(xj )dxi dxj
−∫ψ∗µ(xi )ψ
∗ν(xj )v̂(xij )ψν(xi )ψµ(xj )dxi dxj
].
(15)
The first term is the so-called direct term. In Hartree-Fock theory it leads to the
so-called Hartree term, while the second is due to the Pauli principle and is called the
exchange term and in Hartree-Fock theory it defines the so-called Fock term. The
factor 1/2 is introduced because we now run over all pairs twice.
42 / 234
-
Definitions and notations
The last equation allows us to introduce some further definitions. The single-particlewave functions ψµ(x), defined by the quantum numbers µ and x (recall that x alsoincludes spin degree, later we will also add isospin) are defined as the overlap
ψα(x) = 〈x |α〉.
43 / 234
-
Definitions and notations
We introduce the following shorthands for the above two integrals
〈µν|V |µν〉 =∫ψ∗µ(xi )ψ
∗ν(xj )v̂(xij )ψµ(xi )ψν(xj )dxi dxj ,
and〈µν|V |νµ〉 =
∫ψ∗µ(xi )ψ
∗ν(xj )v̂(xij )ψν(xi )ψµ(xj )dxi dxj .
44 / 234
-
Definitions and notations
The direct and exchange matrix elements can be brought together if we define theantisymmetrized matrix element
〈µν|V |µν〉AS = 〈µν|V |µν〉 − 〈µν|V |νµ〉,
or for a general matrix element
〈µν|V |στ〉AS = 〈µν|V |στ〉 − 〈µν|V |τσ〉.
It has the symmetry property
〈µν|V |στ〉AS = −〈µν|V |τσ〉AS = −〈νµ|V |στ〉AS.
45 / 234
-
Definitions and notations
The antisymmetric matrix element is also hermitian, implying
〈µν|V |στ〉AS = 〈στ |V |µν〉AS.
With these notations we rewrite Eq. (15) as
∫Φ∗ĤIΦdτ =
12
N∑µ=1
N∑ν=1
〈µν|V |µν〉AS. (16)
46 / 234
-
Definitions and notations
Combining Eqs. (14) and (24) we obtain the energy functional
E [Φ] =N∑µ=1
〈µ|ĥ0|µ〉+12
N∑µ=1
N∑ν=1
〈µν|V |µν〉AS. (17)
This equation is very useful, in particular if we only look at the unperturbed part H0.
This part can be represented by closed form expressions that can be used to check our
algorithms.
47 / 234
-
Topics for Week 4, January 20-24
Hartree-Fock theory
I Repetition from last weekI Derivation of the Hartree-Fock equations by varying the
single-particle functions or the coefficients of thesingle-particle equations (to be continued next week)
I Reading assignment for next week: chapter 3(background) and chapter 4.1-4.4 of Thijssen. The link tothe text has been sent to you by email.
See also suggested exercises this week.
48 / 234
-
Hartree-Fock: our many-body approach in this course
HF theory is an algorithm for a finding an approximative expression for the ground stateof a given Hamiltonian. The basic ingredients are
I Define a single-particle basis {ψα} so that
ĥHFψα = εαψα
withĥHF = t̂ + ûext + ûHF
I where ûHF is a single-particle potential to be determined by the HF algorithm.
I The HF algorithm means to choose ûHF in order to have
〈Ĥ〉 = EHF = 〈Φ0|Ĥ|Φ0〉
a local minimum with Φ0 being the SD ansatz for the ground state.
I The variational principle ensures that EHF ≥ Ẽ0, Ẽ0 the exact ground stateenergy.
49 / 234
-
How to solve Schrödinger’s equation for single-particlepotentials
Last week we showed that we rewrite the SE as an eigenvalue problem
d1 e1 0 0 . . . 0 0e1 d2 e2 0 . . . 0 00 e2 d3 e3 0 . . . 0. . . . . . . . . . . . . . . . . . . . .0 . . . . . . . . . . . . dNstep−1 eNstep−10 . . . . . . . . . . . . eNstep−1 dNstep
u1u2. . .. . .. . .
uNstep
= λ
u1u2. . .. . .. . .
uNstep
(18)
50 / 234
-
How to solve Schrödinger’s equation for single-particlepotentials
This is a matrix problem with a tridiagonal matrix of dimension Nstep × Nstep and willthus yield Nstep eigenvalues.The algorithm for solving this problem may take the following form
I Define values for Nstep, Rmin and Rmax. These values define in turn the step sizeh. Typical values for Rmax and Rmin could be 0 and 10 respectively for thelowest-lying states. The number of mesh points Nstep could be in the range 100to some thousands. You can check the stability of the results as functions of Nstepand Rmax and Rmin against the exact solutions.
I Construct then two one-dimensional arrays which contain all values of xk and thepotential Vk . For the latter it can be convenient to write a small function whichsets up the potential as function of xk . For the three-dimensional case you mayalso need to include the centrifugal potential.
51 / 234
-
How to solve Schrödinger’s equation for single-particlepotentials
I Construct thereafter the one-dimensional vectors d and e, where d stands forthe diagonal matrix elements and e the non-diagonal ones. Be careful with theendpoints, since we know the wave function u at both ends of the chosen grid.
I We are now ready to obtain the eigenvalues by calling the function tqli , see thecode examples at the webpage. Calling tqli, you have to transfer the vectors dand e and their dimension dimension n = Nstep and a matrix z of dimensionNstep1× Nstep which returns the eigenfunctions. On return, the array d containsthe eigenvalues. If z is given as the unity matrix on input, it returns theeigenvectors. For a given eigenvalue k , the eigenvector is given by the column kin z, that is z[][k] in C, or z(:,k) in Fortran. Alternatively, if you use armadillo, seebelow, you would call one of the armadillo functions or lapack functions.
52 / 234
-
Example of c++ code for solving Schrödinger’sequation and read and write to file
We are using armadillo here. For standard c++, see the code example on thewebpage. Idem for the Fortran code.
/∗Solves the one−p a r t i c l e Schrodinger equat ionf o r a p o t e n t i a l s p e c i f i e d i n f u n c t i o np o t e n t i a l ( ) . This example i s f o r the harmonic
o s c i l l a t o rcompile as c++ −O3 −o ho3d . x ho3d . cpp − l a r m a d i l l o
− l l apack − l b l a s∗ /#include #include #include #include #include using namespace s td ;using namespace arma ;/ / ou tput f i l e as g loba l v a r i a b l eofstream o f i l e ;
53 / 234
-
Example of c++ code for solving Schrödinger’sequation and read and write to file
/ / f u n c t i o n dec la r a t i onsdouble P o t e n t i a l ( double ) ;/ / Main program s t a r t s herei n t main ( i n t argc , char∗ argv [ ] ){
i n t i , j , MaxStep , OrbL ;double Rmax, Step , Const1 , Const2 ,
O r b i t a l F a c t o r ;char ∗ out f i lename ;
54 / 234
-
Example of c++ code for solving Schrödinger’sequation and read and write to file
/ / Read i n output f i l e , abor t i f there are toofew command− l i n e arguments
i f ( argc
-
Example of c++ code for solving Schrödinger’sequation and read and write to file
/ / i n i t i a l i s e constants assuming Rmin = 0 . 0 ;Step = Rmax / ( MaxStep+1) ;Const2 = −0.5 / ( Step ∗ Step ) ; Const1 = 1.0 / (
Step ∗ Step ) ;O r b i t a l F a c t o r = OrbL ∗ ( OrbL + 1) ;/ / se t up r , d iagonal and non−diagonal elementsvec r ( MaxStep+1) , d ( MaxStep−1) , e ( MaxStep−1) ;for ( i = 0 ; i < MaxStep−1; i ++) {
r ( i ) = ( i +1) ∗ Step ; / / avoid r = 0 and i n f t y, known values
d ( i ) = Const1+ P o t e n t i a l ( r ( i ) ) + 0.5∗O r b i t a l F a c t o r / ( r ( i ) ∗ r ( i ) ) ;
}
56 / 234
-
Example of c++ code for solving Schrödinger’sequation and read and write to file
e = Const2 ;mat A( MaxStep−1,MaxStep−1) ;/ / Set up mat r i xfor ( i = 0 ; i < MaxStep−1; i ++) {
/ / Diagonal elementsA( i , i ) = d ( i ) ;/ / Non−diagonal elementsi f ( i < MaxStep − 2) {
A( i , i + 1) = Const2 ;A( i + 1 , i ) = Const2 ;
}}
57 / 234
-
Example of c++ code for solving Schrödinger’sequation and read and write to file
vec e i g v a l ( MaxStep−1) ;mat eigvec ( MaxStep−1,MaxStep−1) ;/ / The e igenvectors are normal izedeig sym ( e igva l , eigvec , A) ;
58 / 234
-
Example of c++ code for solving Schrödinger’sequation and read and write to file
/ / p r i n t r e s u l t so f i l e
-
Example of c++ code for solving Schrödinger’sequation and read and write to file
/∗The f u n c t i o n p o t e n t i a l ( )c a l c u l a t e s and r e t u r n the value o f thep o t e n t i a l f o r a given argument x .The p o t e n t i a l here i s the harmonic o s c i l l a t o r
∗ /double P o t e n t i a l ( double x ){
return 0.5∗ x∗x ;
} / / End : f u n c t i o n p o t e n t i a l ( )
60 / 234
-
Programming exercise week 4
Hydrogen-like SEThis exercise is meant to get you started with programming andyou are to solve the above described single-particle problemnumerically. You can use the codes on the webpage asexamples or simply write you own code.
I Compute the lowest eigenvalue for hydrogen-like atoms(replace the harmonic oscillator potential with an attractiveCoulomb interaction −Z/r . Compare with the exact values
I Add also a centrifugal barrier and compute the eigenvaluesfor 2p state.
I Plot the wave functions for the 1s, 2s and 2p states andcomment your results.
61 / 234
-
Paper and pencil exercises week 4 and 5
Slater determinantsFor a two-electron system, the wave function can be written as
Ψ(x1, x2) = Φ(r1, r2) · χ(s1, s2) (19)
Because the wave function Ψ is antisymmetric under particle exchange, we may take Φsymmetric in 1 and 2 and χ antisymmetric, or vice versa.We construct the wave function Φ and χ from the orthonormal spatial orbitals φ1(r),φ2(r) and the spin-up and -down functions α(s) and β(s) respectively.
1. Write down the antisymmetric wave functions which can be constructed in thisway.
2. Write down all possible Slater determinants which can be built from theone-electron spin-orbitals consisting of a product of one of the spatial orbitals φ1and φ2 and a spin-up or -down spinor.
62 / 234
-
Paper and pencil exercises week 4 and 5
Slater determinantsConsider a Slater determinant;
ΨAS =1√
N!
∣∣∣∣∣∣∣∣∣ψ1(x1) ψ1(x2) . . . . . . ψ1(xN )ψ2(x1) ψ2(x2) . . . . . . ψ2(xN ). . . . . . . . . . . . . . .. . . . . . . . . . . . . . .
ψN (x1) ψN (x2) . . . . . . ψN (xN )
∣∣∣∣∣∣∣∣∣ , (20)where ψk (x) are orthonormal single-particle states.
1. Show that the Slater determinant is normalized.
2. Show that the density of electrons with coordinates x, is given by
n(x) = N∫
dx2 . . . dxN |ΨAS(x, x2, . . . , xN )|2 (21)
can be written in terms of the ψk as
n(x) =∑
k
|ψk (x)|2. (22)
63 / 234
-
Background material: Variational Calculus andLagrangian Multiplier
The calculus of variations involves problems where the quantity to be minimized ormaximized is an integral.In the general case we have an integral of the type
E [Φ] =∫ b
af (Φ(x),
∂Φ
∂x, 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 Φ, ∂Φ/∂x 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
64 / 234
-
Background material: 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 , α)− Φ(x , 0) = αη(x).
65 / 234
-
Background material: Euler-Lagrange equations
We choose Φ(x , α = 0) as the unkonwn path that will minimize E . The valueΦ(x , α 6= 0) describes a neighbouring path.We have
E [Φ(α)] =∫ b
af (Φ(x , α),
∂Φ(x , α)∂x
, x)dx .
In the slides I will use the shorthand
Φx (x , α) =∂Φ(x , α)∂x
.
In our case a = 0 and b =∞ and we know the value of the wave function.
66 / 234
-
Background material: Euler-Lagrange equations
The condition for an extreme of
E [Φ(α)] =∫ b
af (Φ(x , α),Φx (x , α), x)dx ,
is [∂E [Φ(α)]
∂x
]α=0
= 0.
The α dependence is contained in Φ(x , α) and Φx (x , α) meaning that[∂E [Φ(α)]
∂α
]=
∫ ba
(∂f∂Φ
∂Φ
∂α+
∂f∂Φx
∂Φx
∂α
)dx .
We have defined∂Φ(x , α)∂α
= η(x)
and thereby∂Φx (x , α)
∂α=
d(η(x))dx
.
67 / 234
-
Background material: Euler-Lagrange equations
Using∂Φ(x , α)∂α
= η(x),
and∂Φx (x , α)
∂α=
d(η(x))dx
,
in the integral gives[∂E [Φ(α)]
∂α
]=
∫ ba
(∂f∂Φ
η(x) +∂f∂Φx
d(η(x))dx
)dx .
Integrate the second term by parts∫ ba
∂f∂Φx
d(η(x))dx
dx = η(x)∂f∂Φx|ba −
∫ baη(x)
ddx
∂f∂Φx
dx ,
and since the first term dissappears due to η(a) = η(b) = 0, we obtain[∂E [Φ(α)]
∂α
]=
∫ ba
(∂f∂Φ−
ddx
∂f∂Φx
)η(x)dx = 0.
68 / 234
-
Background material: Euler-Lagrange equations
[∂E [Φ(α)]
∂α
]=
∫ ba
(∂f∂Φ−
ddx
∂f∂Φx
)η(x)dx = 0,
can also be written as
α
[∂E [Φ(α)]
∂α
]α=0
=
∫ ba
(∂f∂Φ−
ddx
∂f∂Φx
)δΦ(x)dx = δE = 0.
The condition for a stationary value is thus a partial differential equation
∂f∂Φ−
ddx
∂f∂Φx
= 0,
known as Euler’s equation. Can easily be generalized to more variables.
69 / 234
-
Background material: 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
∂f∂x
=∂f∂y
=∂f∂z
= 0,
due to
df =∂f∂x
dx +∂f∂y
dy +∂f∂z
dz.
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.
70 / 234
-
Background material: 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φ =∂φ
∂xdx +
∂φ
∂ydy +
∂φ
∂zdz = 0.
Now we cannot set anymore
∂f∂x
=∂f∂y
=∂f∂z
= 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.
71 / 234
-
Background material: Lagrangian Multipliers
However, we can add to
df =∂f∂x
dx +∂f∂y
dy +∂f∂z
dz,
a multiplum of dφ, viz. λdφ, resulting in
df + λdφ = (∂f∂z
+ λ∂φ
∂x)dx + (
∂f∂y
+ λ∂φ
∂y)dy + (
∂f∂z
+ λ∂φ
∂z)dz = 0.
Our multiplier is chosen so that
∂f∂z
+ λ∂φ
∂z= 0.
72 / 234
-
Background material: Lagrangian Multipliers
However, we took dx and dy as to be arbitrary and thus we must have
∂f∂x
+ λ∂φ
∂x= 0,
and∂f∂y
+ λ∂φ
∂y= 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 calledLagrange’s undetermined multiplier. If we have a set of constraints φk we have theequations
∂f∂xi
+∑
k
λk∂φk∂xi
= 0.
73 / 234
-
Background material: Variational Calculus andLagrangian Multipliers
Let us specialize to the expectation value of the energy for one particle inthree-dimensions. This expectation value reads
E =∫
dxdydzψ∗(x , y , z)Ĥψ(x , y , z),
with the constraint ∫dxdydzψ∗(x , y , z)ψ(x , y , z) = 1,
and a HamiltonianĤ = −
12∇2 + v̂(x , y , z).
I will skip the variables x , y , z below, and write for example v̂(x , y , z) = V .
74 / 234
-
Background material: Variational Calculus andLagrangian 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 , z,∫
dxdydzψ∗(−
12∇2)ψdxdydz = −ψ∗∇ψ|+
∫dxdydz
12∇ψ∗∇ψ.
Inserting this expression into the expectation value for the energy and taking thevariational minimum we obtain
δE = δ{∫
dxdydz(
12∇ψ∗∇ψ + Vψ∗ψ
)}= 0.
75 / 234
-
Background material: Variational Calculus andLagrangian Multiplier
The constraint appears in integral form as∫dxdydzψ∗ψ = constant,
and multiplying with a Lagrangian multiplier λ and taking the variational minimum weobtain the final variational equation
δ
{∫dxdydz
(12∇ψ∗∇ψ + Vψ∗ψ − λψ∗ψ
)}= 0.
Introducing the function f
f =12∇ψ∗∇ψ + Vψ∗ψ − λψ∗ψ =
12
(ψ∗xψx + ψ∗yψy + ψ
∗zψz ) + Vψ
∗ψ − λψ∗ψ,
where we have skipped the dependence on x , y , z and introduced the shorthand ψx ,
ψy and ψz for the various derivatives.
76 / 234
-
Background material: Variational Calculus andLagrangian Multiplier
For ψ∗ the Euler equation results in
∂f∂ψ∗
−∂
∂x∂f∂ψ∗x
−∂
∂y∂f∂ψ∗y
−∂
∂z∂f∂ψ∗z
= 0,
which yields
−12
(ψ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 Schrödinger equation and the variationalapproach discussed here provides a powerful method for obtaining approximatesolutions of the wave function.
77 / 234
-
Topics for Week 5, January 27-31
Hartree-Fock theory
I Repetition from last weekI Derivation of the Hartree-Fock equations by varying the
single-particle functions or the coefficients of thesingle-particle equations (continued from last week)
I See updated project and discussion of a Hartree-Fockcode
I Next week we will discuss how to introduce classes andobject orientation for the Hartree-Fock project.
78 / 234
-
Finding the Hartree-Fock functional E [Φ]
We have the Hamiltonian
Ĥ = Ĥ0 + ĤI =A∑
i=1
ĥ0(xi ) +A∑
i
-
Finding the Hartree-Fock functional E [Φ]
Let us denote the ground state energy by E0. According to the variational principle wehave
E0 ≤ E [Φ] =∫
Φ∗ĤΦdτ
where Φ is a trial function which we assume to be normalized∫Φ∗Φdτ = 1,
where we have used the shorthand dτ = dx1dx2 . . . dxN .
80 / 234
-
Finding the Hartree-Fock functional E [Φ]
In the Hartree-Fock method the trial function is the Slater determinant which can berewritten as
Ψ(x1, x2, . . . , xN , α, β, . . . , ν) =1√
N!
∑P
(−)PPψα(x1)ψβ(x2) . . . ψν(xN ) =√
N!AΦH ,
where we have introduced the anti-symmetrization operator A defined by thesummation over all possible permutations of two fermions. It is defined as
A =1
N!
∑P
(−)PP,
with the the Hartree-function given by the simple product of all possible single-particlefunction
ΦH (x1, x2, . . . , xN , α, β, . . . , ν) = ψα(x1)ψβ(x2) . . . ψν(xN ).
81 / 234
-
Finding the Hartree-Fock functional E [Φ]
Both Ĥ0 and ĤI are invariant under permutations of fermions, and hence commute withA
[H0,A] = [HI ,A] = 0.
Furthermore, A satisfiesA2 = A,
since every permutation of the Slater determinant reproduces it.
82 / 234
-
Variational Calculus and Lagrangian Multiplier, back toHartree-Fock
Our functional for N electrons is then
E [Φ] =N∑µ=1
∫ψ∗µ(xi )ĥ0(xi )ψµ(xi )dxi +
12
N∑µ=1
N∑ν=1
[∫ψ∗µ(xi )ψ
∗ν(xj )v̂(rij )ψµ(xi )ψν(xj )dxi dxj
−∫ψ∗µ(xi )ψ
∗ν(xj )v̂(rij )ψν(xi )ψµ(xj )dxi dxj
]The more compact version is
E [Φ] =N∑µ=1
〈µ|ĥ0|µ〉+12
N∑µ=1
N∑ν=1
[〈µν|v̂(rij )|µν〉 − 〈µν|v̂(rij )|νµ〉
].
83 / 234
-
Hartree-Fock: Variational Calculus and LagrangianMultiplier
If we generalize the Euler-Lagrange equations to more variables and introduce A2Lagrange multipliers which we denote by �µν , we can write the variational equation forthe functional of E
δE −N∑µ=1
N∑ν=1
�µνδ
∫ψ∗µψν = 0.
For the orthogonal wave functions ψµ this reduces to
δE −N∑µ=1
�µδ
∫ψ∗µψµ = 0.
84 / 234
-
Hartree-Fock: Variational Calculus and LagrangianMultiplier
Variation with respect to the single-particle wave functions ψµ yields then
N∑µ=1
∫δψ∗µĥ0(xi )ψµdxi +
N∑µ=1
N∑ν=1
[∫δψ∗µψ
∗ν v̂(rij )ψµψνdxi dxj −
∫δψ∗µψ
∗ν
1rijψνψµdxi dxj
]
+N∑µ=1
∫ψ∗µĥ0(xi )δψµdxi +
N∑µ=1
N∑ν=1
[∫ψ∗µψ
∗ν
1rijδψµψνdxi dxj −
∫ψ∗µψ
∗ν v̂(rij )ψνδψµdxi dxj
]
−N∑µ=1
Eµ∫δψ∗µψµdxi −
N∑µ=1
Eµ∫ψ∗µδψµdxi = 0.
85 / 234
-
Hartree-Fock: Variational Calculus and LagrangianMultiplier
Although the variations δψ and δψ∗ are not independent, they may in fact be treated assuch, so that the terms dependent on either δψ and δψ∗ individually may be set equalto zero. To see this, simply replace the arbitrary variation δψ by iδψ, so that δψ∗ isreplaced by −iδψ∗, and combine the two equations. We thus arrive at theHartree-Fock equations−1
2∇2i − uext(xi ) +
N∑ν=1
∫ψ∗ν(xj )v̂(rij )ψν(xj )dxj
ψµ(xi )−
N∑ν=1
∫ψ∗ν(xj )v̂(rij )ψµ(xj )dxj
ψν(xi ) = �µψµ(xi ).Notice that the integration
∫dxj implies an integration over the spatial coordinates rj
and a summation over the spin-coordinate of fermion j .
86 / 234
-
Hartree-Fock: Variational Calculus and LagrangianMultiplier
The two first terms are the expectation value of the one-body operator. The third or
direct term is the averaged field set up by all other electrons. As written, the term
includes the ’self-interaction’ 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 equal single-particle quantum numbers to avoid
each other.
87 / 234
-
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 dµ(xi ) =∫ψ∗µ(xj )ψµ(xj )v̂(rij )dxj
and
V exµ (xi )g(xi ) =(∫
ψ∗µ(xj )v̂(rij )g(xj )dxj
)ψµ(xi ),
respectively.
88 / 234
-
Hartree-Fock: Variational Calculus and LagrangianMultiplier
The function g(xi ) is an arbitrary function, and by the substitution g(xi ) = ψν(xi ) weget
V exµ (xi )ψν(xi ) =(∫
ψ∗µ(xj )v̂(rij )ψν(xj )dxj
)ψµ(xi ).
89 / 234
-
Hartree-Fock: Variational Calculus and LagrangianMultiplier
We may then rewrite the Hartree-Fock equations as
ĥHF (xi )ψν(xi ) = �νψν(xi ),
with
ĥHF (xi ) = ĥ0(xi ) +N∑µ=1
V dµ(xi )−N∑µ=1
V exµ (xi ),
and where ĥ0(i) is the one-body part. The latter is normally chosen as a part whichyields solutions in closed form. The harmonic oscilltor is a classical problem thereof.We normally rewrite the last equation as
ĥHF (xi ) = ĥ0(xi ) + ûHF (xi ).
90 / 234
-
Hartree-Fock by varying the coefficients of a wavefunction expansion
Another possibility is to expand the single-particle functions in a known basis and varythe coefficients, that is, the new single-particle wave function is written as a linearexpansion in terms of a fixed chosen orthogonal basis (for example harmonic oscillator,Laguerre polynomials etc)
ψHFp =∑λ
Cpλψλ. (23)
In this case we vary the coefficients Caλ. If the basis has infinitely many solutions, weneed to truncate the above sum. In all our equations we assume a truncation has beenmade.The single-particle wave functions ψλ(r), defined by the quantum numbers λ and r aredefined as the overlap
ψλ(r) = 〈r|λ〉.
91 / 234
-
Hartree-Fock by varying the coefficients of a wavefunction expansion
We will omit the radial dependence of the wave functions and introduce first thefollowing shorthands for the direct and exchange part
〈µν|V |µν〉 =∫ψ∗µ(ri )ψ
∗ν(rj )V (rij )ψµ(ri )ψν(rj )dri drj ,
and〈µν|V |νµ〉 =
∫ψ∗µ(ri )ψ
∗ν(rj )V (rij )ψν(ri )ψµ(ri )dri drj .
92 / 234
-
Hartree-Fock by varying the coefficients of a wavefunction expansion
Since the interaction is invariant under the interchange of two particles it means forexample that we have
〈µν|V |µν〉 = 〈νµ|V |νµ〉,
or in the more general case
〈µν|V |στ〉 = 〈νµ|V |τσ〉.
93 / 234
-
Hartree-Fock by varying the coefficients of a wavefunction expansion
The direct and exchange matrix elements can be brought together if we define theantisymmetrized matrix element
〈µν|V |µν〉AS = 〈µν|V |µν〉 − 〈µν|V |νµ〉,
or for a general matrix element
〈µν|V |στ〉AS = 〈µν|V |στ〉 − 〈µν|V |τσ〉.
It has the symmetry property
〈µν|V |στ〉AS = −〈µν|V |τσ〉AS = −〈νµ|V |στ〉AS .
The antisymmetric matrix element is also hermitian, implying
〈µν|V |στ〉AS = 〈στ |V |µν〉AS .
94 / 234
-
Hartree-Fock by varying the coefficients of a wavefunction expansion
We have for the interaction part
∫Φ∗Ĥ1Φdτ =
12
N∑µ=1
N∑ν=1
〈µν|V |µν〉AS . (24)
Combining Eqs. (14) and (24) we obtain the energy functional
E [Φ] =N∑µ=1
〈µ|ĥ0|µ〉+12
N∑µ=1
N∑ν=1
〈µν|V |µν〉AS . (25)
95 / 234
-
Hartree-Fock by varying the coefficients of a wavefunction expansion
If we vary the above energy functional with respect to the basis functions |µ〉, thiscorresponds to what was done in the previous case. We are however interested indefining a new basis defined in terms of a chosen basis as defined in Eq. (23). We canthen rewrite the energy functional as
E [ΦHF] =N∑
i=1
〈i|ĥ0|i〉+12
N∑ij=1
〈ij|V |ij〉AS , (26)
where ΦHF is the new Slater determinant defined by the new basis of Eq. (23).
96 / 234
-
Hartree-Fock by varying the coefficients of a wavefunction expansion
Using Eq. (23) we can rewrite Eq. (26) as
E [ΦHF] =N∑
i=1
∑αβ
C∗iαCiβ〈α|h|β〉+12
N∑ij=1
∑αβγδ
C∗iαC∗jβCiγCjδ〈αβ|V |γδ〉AS . (27)
97 / 234
-
Hartree-Fock by varying the coefficients of a wavefunction expansion
We wish now to minimize the above functional. We introduce again a set of Lagrangemultipliers, noting that since 〈i|j〉 = δi,j and 〈α|β〉 = δα,β , the coefficients Ciγ obey therelation
〈i|j〉 = δi,j =∑αβ
C∗iαCiβ〈α|β〉 =∑α
C∗iαCiα,
which allows us to define a functional to be minimized that reads
F [ΦHF] = E [ΦHF]−N∑
i=1
�i∑α
C∗iαCiα. (28)
98 / 234
-
Hartree-Fock by varying the coefficients of a wavefunction expansion
Minimizing with respect to C∗iα, remembering that C∗iα and Ciα are independent, we
obtaind
dC∗iα
E [ΦHF]− A∑j
�j∑α
C∗jαCjα
= 0, (29)which yields for every single-particle state i the following Hartree-Fock equations
∑γ
Ciγ〈α|ĥ0|γ〉+N∑
j=1
∑βγδ
C∗jβCjδCiγ〈αβ|V |γδ〉AS = �k Ciα. (30)
99 / 234
-
Hartree-Fock by varying the coefficients of a wavefunction expansion
We can rewrite this equation as
∑β
〈α|ĥ0|β〉+N∑j
∑γδ
C∗jγCjδ〈αγ|V |βδ〉AS
Ciβ = �HFi Ciα. (31)Note that the sums over greek indices run over the number of basis set functions (in
principle an infinite number).
100 / 234
-
Hartree-Fock by varying the coefficients of a wavefunction expansion
Defining
hHFαβ = 〈α|ĥ0|β〉+N∑
j=1
∑γδ
C∗jγCjδ〈αγ|V |βδ〉AS ,
we can rewrite the new equations as∑β
hHFαβCiβ = �HFi Ciα. (32)
Note again that the sums over greek indices run over the number of basis set functions
(in principle an infinite number).
101 / 234
-
Rewriting the energy functional
The equationĥHF (xi ) = ĥ0(xi ) + ûHF (xi ),
allows us to rewrite the ground state energy (adding and subtracting ûHF (xi ))
EHF0 = 〈Φ0|Ĥ|Φ0〉 =N∑
i≤F〈i|ĥ0 + ûHF |i〉+
12
N∑i≤F
N∑j≤F
[〈ij|v̂ |ij〉 − 〈ij|v̂ |ji〉]−N∑
i≤F〈i|ûHF |i〉,
as
EHF0 =N∑
i≤Fεi +
12
N∑i≤F
N∑j≤F
[〈ij|v̂ |ij〉 − 〈ij|v̂ |ji〉]−N∑
i≤F〈i|ûHF |i〉,
which is nothing but
EHF0 =N∑
i≤Fεi −
12
N∑i≤F
N∑j≤F
[〈ij|v̂ |ij〉 − 〈ij|v̂ |ji〉] .
102 / 234
-
Hartree-Fock by varying the coefficients of a wavefunction expansion
Our Hartree-Fock matrix is thus
ĥHFαβ = 〈α|ĥ0|β〉+N∑
j=1
∑γδ
C∗jγCjδ〈αγ|V |βδ〉AS .
The Hartree-Fock are solved in an iterative waym starting with a guess for thecoefficients Cjγ = δj,γ and solving the equations by diagonalization till the newsingle-particle energies �HFi do not change anymore by a prefixed quantity.Normally we assume that the single-particle basis |β〉 forms an eigenbasis for theoperator ĥ0, meaning that the Hartree-Fock matrix becomes
ĥHFαβ = �αδα,β +N∑
j=1
∑γδ
C∗jγCjδ〈αγ|V |βδ〉AS .
103 / 234
-
Hartree-Fock algorithmOur Hartree-Fock matrix is thus
ĥHFαβ = 〈α|ĥ0|β〉+A∑
j=1
∑γδ
C∗jγCjδ〈αγ|V |βδ〉AS .
Normally we assume that the single-particle basis |β〉 forms an eigenbasis for theoperator ĥ0, meaning that the Hartree-Fock matrix becomes
ĥHFαβ = �αδα,β +A∑
j=1
∑γδ
C∗jγCjδ〈αγ|V |βδ〉AS .
The Hartree-Fock eigenvalue problem∑β
ĥHFαβCiβ = �HFi Ciα,
can be written out in a more compact form as
ĥHF Ĉ = �HFĈ.
104 / 234
-
Hartree-Fock algorithm
The Hartree-Fock equations are, in their simplest form, solved in an iterative way,starting with a guess for the coefficients Ciα. We label the coefficients as C
(n)iα , where
the subscript n stands for iteration n. To set up the algorithm we can proceed asfollows:
1. We start with a guess C(0)iα = δi,α. Alternatively, we could have used randomstarting values as long as the vectors are normalized. Another possibility is togive states below the Fermi level a larger weight.
2. The Hartree-Fock matrix simplifies then to
ĥHFαβ = �αδα,β +N∑
j=1
∑γδ
C∗jγ(0)C(0)jδ 〈αγ|V |βδ〉AS .
Solving the Hartree-Fock eigenvalue problem yields then new eigenvectors C(1)iαand eigenvalues �HFi
(1).
105 / 234
-
Hartree-Fock algorithm
1. With the new eigenvalues we can set up a new Hartree-Fock potential
N∑j=1
∑γδ
C∗jγ(1)C(1)jδ 〈αγ|V |βδ〉AS .
The diagonalization with the new Hartree-Fock potential yields new eigenvectorsand eigenvalues. This process is continued till for example∑
p |�HFi(n) − �HFi
(n−1)|m
≤ λ,
where λ is a user prefixed quantity (λ ∼ 10−8 or smaller) and p runs over allcalculated single-particle energies and m is the number of single-particle states.
106 / 234
-
Hartree-Fock code
void hart reeFock : : run ( ) {double i n t e r a c t i o n ;/ / −−−−−−−−−−−−−−− Se t t i ng up the HF−
h a m i l t i n i a n using C = 1 −−−−−−−−−−−−−−−mat H;vec E = zeros ( nStates , 1) ;vec ePrev = zeros ( nStates , 1) ;mat C = eye ( nStates , nStates ) ;vec d i f f ;
/ / Hartree−Fock loopi n t h f I t = 0 ;while ( h f I t < HFI te ra t i ons ) {
cout
-
Hartree-Fock codefor ( i n t alpha = 0; alpha < nStates ; alpha
++) {for ( i n t gamma = 0; gamma < nStates ;
gamma++) {i n t e r a c t i o n = 0;for ( i n t p = 0; p < n P a r t i c l e s ; p
++) {for ( i n t beta = 0; beta <
nStates ; beta ++) {for ( i n t de l t a = 0 ; de l t a <
nStates ; de l t a ++) {i n t e r a c t i o n += C. a t ( p ,
beta ) ∗ C. a t ( p ,de l t a ) ∗matr ixElement ( alpha, beta , gamma,de l t a ) ;
}}
}108 / 234
-
Hartree-Fock codeH( alpha , gamma) = H(gamma, alpha ) =
h0 ( alpha , gamma) + i n t e r a c t i o n;
}}/ / Computing the HF one−body energieseig sym (E, C, H) ;C = t rans (C) ;h f I t ++;/ / Convergence t e s td i f f = E − ePrev ;i f ( abs ( d i f f .max ( ) ) < t h resho ld )
break ;ePrev = E;
}double E0 = calcEnergy (C) ;cout
-
Koopman’s theorem and interpretation of theHartree-Fock energies
We have defined
E [ΦHF(N)] =N∑
i=1
〈i|ĥ0|i〉+12
N∑ij=1
〈ij|V |ij〉AS ,
where ΦHF(A) is the new Slater determinant defined by the new basis of Eq. (23) for Nelectrons (same Z ). If we assume that the single-particle wave functions in the newbasis do not change when we remove one electron or add one electron, we can thendefine the corresponding energy for the N − 1 systems as
E [ΦHF(N − 1)] =N∑
i=1;i 6=k〈i|ĥ0|i〉+
12
N∑ij=1;i,j 6=k
〈ij|V |ij〉AS ,
where we have removed a single-particle state k ≤ F , that is a state below the Fermilevel.
110 / 234
-
Koopman’s theorem and interpretation of theHartree-Fock energies
Calculating the difference
E [ΦHF(N)]− E [ΦHF(N − 1)] = 〈k |ĥ0|k〉+12
N∑i=1;i 6=k
〈ik |V |ik〉AS12
N∑j=1;j 6=k
〈kj|V |kj〉AS ,
which becomes
E [ΦHF(N)]− E [ΦHF(N − 1)] = 〈k |ĥ0|k〉+12
N∑j=1
〈kj|V |kj〉AS
which is just our definition of the Hartree-Fock single-particle energy
E [ΦHF(N)]− E [ΦHF(N − 1)] = �HFk
111 / 234
-
Koopman’s theorem and interpretation of theHartree-Fock energies
Similarly, we can now compute the difference (we label the single-particle states abovethe Fermi level as abcd > F )
E [ΦHF(N + 1)]− E [ΦHF(N)] = �HFa .
This equation and the one on the previous slide, are linked to Koopman’s theorem. In
atomic physics it is used to define the electron ionization or affinity energies.
Koopman’s theorem states that the ionization energy of closed-shell systems is given
by the energy of the highest occupied single-particle state.
112 / 234
-
Discussion of the project
We will now set up the Hartree-Fock program by using hydrogen-like single-particle
functions for the 1s, 2s and 3s orbitals. We will apply these basis functions only to the
helium and beryllium atoms. For all other systems, we will use so-called Gaussian type
of orbitals (GTO). These will be introduced after we have set up our first Hartree-Fock
programs. For Hydrogen like-orbitals, the matrix elements involving the Coulomb
interaction, can all be evaluated in a closed form, see the table below for all integrals
involving these orbitals. Your first step is to write a function which reads in these
integrals and sets up the antisymmetrized matrix elements. (Hint: the table lists only
the integrals, there is no spin).
113 / 234
-
Discussion of the projectThe table contains the matrix elements for the radial integrals to be used for the directpart and the exchange part. You will need these integrals in solving the Hartree-Fockequations for the helium and beryllium atoms when using hydrogen-like single-particlestates. The table lists only the integrals, no spin is included!∫
ψ∗µ(ri )ψ∗ν(rj )V (rij )ψµ(ri )ψν(rj )dri drj ,
Table: Closed form expressions for the Coulomb matrix elements(see project for all elements). The nomenclature is 1 = 1s, 2 = 2sand 3 = 3s, with no spin degrees of freedom.
〈11|V |11〉 = (5Z )/8 〈11|V |12〉 = (4096√
2Z )/64827〈11|V |13〉 = (1269
√3Z )/50000 〈11|V |21〉 = (4096
√2Z )/64827
〈11|V |22〉 = (16Z )/729 〈11|V |23〉 = (110592√
6Z )/24137569〈11|V |31〉 = (1269
√3Z )/50000 〈11|V |32〉 = (110592
√6Z )/24137569
〈11|V |33〉 = (189Z )/32768 〈12|V |11〉 = (4096√
2Z )/64827. . . . . . . . . . . .
〈33|V |33〉 = (17Z )/256
114 / 234
-
Discussion of the project
Set up the Hartree-Fock equations for the ground states of helium and beryllium withthe electrons occupying the respective ’hydrogen-like’ orbitals 1s, 2s and 3s. There isno spin-orbit part in the two-body Hamiltonian. Find also the total binding ground stateenergy and compare this to the energy obtaining without performing the Hartree-Fockcalculations. The experimental values are −2.904 atomic units (a.u.) and −14.67 a.u.for the helium and beryllium atoms, respectively.In setting up the equations make sure your Hartree-Fock matrix is block diagonal in thespin quantum numbers. Since there is no spin-dependent part in the Hamiltonian,states with the same l and n will be degenerate in energy with respect to the spinprojections.
The code you develop here will serve as a starting point for the inclusion of GTOs later
in this course. We will discuss object orientation next week.
115 / 234
-
Topics for Week 8, February 17-21
Classes and object orientation in C++
I Simple classes in C++I TemplatesI How to object orient the Hartree-Fock programI Next week we will discuss new orbital functions, the
so-called Gaussian-type orbitals
116 / 234
-
Object orientation
Why object orientation?
I Three main topics: objects, class hierarchies and polymorphism
I The aim here is to be to be able to write a more general code which can easilybe tailored to new situations.
I Polymorphism is a term used in software development to describe a variety oftechniques employed by programmers to create flexible and reusable softwarecomponents. The term is Greek and it loosely translates to ”many forms”.
Strategy: try to single out the variables needed to describe a given system and those
needed to describe a given solver.
117 / 234
-
Object orientation
In programming languages, a polymorphic object is an entity, such as a variable or aprocedure, that can hold or operate on values of differing types during the program’sexecution. Because a polymorphic object can operate on a variety of values and types,it can also be used in a variety of programs, sometimes with little or no change by theprogrammer. The idea of write once, run many, also known as code reusability, is animportant characteristic to the programming paradigm known as Object-OrientedProgramming (OOP).OOP describes an approach to programming where a program is viewed as acollection of interacting, but mostly independent software components. These softwarecomponents are known as objects in OOP and they are typically implemented in aprogramming language as an entity that encapsulates both data and procedures.
118 / 234
-
Programming classes
In Fortran a vector or matrix start with 1, but it is easy to change a vector so that it
starts with zero or even a negative number. If we have a double precision Fortran
vector which starts at −10 and ends at 10, we could declare it asREAL(KIND=8):: vector(−10:10). Similarly, if we want to start at zero and endat 10 we could write REAL(KIND=8):: vector(0:10). We have also seen thatFortran allows us to write a matrix addition A = B + C as A = B + C. This meansthat we have overloaded the addition operator so that it translates this operation into
two loops and an addition of two matrix elements aij = bij + cij .
119 / 234
-
Programming classes
The way the matrix addition is written is very close to the way we express this relationmathematically. The benefit for the programmer is that our code is easier to read.Furthermore, such a way of coding makes it more likely to spot eventual errors as well.In Ansi C and C++ arrays start by default from i = 0. Moreover, if we wish to add twomatrices we need to explicitely write out the two loops as
for ( i =0 ; i < n ; i ++) {for ( j =0 ; j < n ; j ++) {
a [ i ] [ j ]=b [ i ] [ j ]+ c [ i ] [ j ]}
}
120 / 234
-
Programming classes
However, the strength of C++ is the possibility to define new data types, tailored tosome particular problem. Via new data types and overloading of operations such asaddition and subtraction, we can easily define sets of operations and data types whichallow us to write a matrix addition in exactly the same way as we would do in Fortran.We could also change the way we declare a C++ matrix elements aij , from a[i][j] to saya(i, j), as we would do in Fortran. Similarly, we could also change the default rangefrom 0 : n − 1 to 1 : n.To achieve this we need to introduce two important entities in C++ programming,
classes and templates.
121 / 234
-
Programming classes
The function and class declarations are fundamental concepts within C++. Functions
are abstractions which encapsulate an algorithm or parts of it and perform specific
tasks in a program. We have already met several examples on how to use functions.
Classes can be defined as abstractions which encapsulate data and operations on
these data. The data can be very complex data structures and the class can contain
particular functions which operate on these data. Classes allow therefore for a higher
level of abstraction in computing. The elements (or components) of the data type are
the class data members, and the procedures are the class member functions.
122 / 234
-
Programming classes
Classes are user-defined tools used to create multi-purposesoftware which can be reused by other classes or functions.These user-defined data types contain data (variables) andfunctions operating on the data.A simple example is that of a point in two dimensions. The datacould be the x and y coordinates of a given point. Thefunctions we define could be simple read and write functions orthe possibility to compute the distance between two points.
123 / 234
-
Programming classesC++ has a class complex in its standard template library (STL).The standard usage in a given function could then look like
/ / Program to c a l c u l a t e a d d i t i o n and m u l t i p l i c a t i o no f two complex numbers
using namespace s td ;#include #include #include i n t main ( ){
complex x ( 6 . 1 , 8 . 2 ) , y ( 0 . 5 , 1 . 3 ) ;/ / w r i t e out x+ycout
-
Programming classesWe proceed by splitting our task in three files.We define first a header file complex.h which contains the declarations of the class.The header file contains the class declaration (data and functions), declaration ofstand-alone functions, and all inlined functions, starting as follows
# i fndef Complex H#define Complex H/ / va r ious inc lude statements and d e f i n i t i o n s#include / / Standard ANSI−C++
inc lude f i l e s#include #include . . . .
class Complex{ . . .d e f i n i t i o n o f v a r i a b l e s and t h e i r character} ;/ / dec l a r a t i ons o f var ious f u n c t i o n s used by the
c lass. . .#endif
125 / 234
-
Programming classes
Next we provide a file complex.cpp where the code and algorithms of differentfunctions (except inlined functions) declared within the class are written. The filescomplex.h and complex.cpp are normally placed in a directory with other classes andlibraries we have defined.
Finally,we discuss here an example of a main program which uses this particular class.
An example of a program which uses our complex class is given below. In particular we
would like our class to perform tasks like declaring complex variables, writing out the
real and imaginary part and performing algebraic operations such as adding or
multiplying two complex numbers.
126 / 234
-
Programming classes
#include "Complex.h". . . o ther include and dec l a ra t i o nsi n t main ( ){
Complex a ( 0 . 1 , 1 . 3 ) ; / / we dec lare a complexv a r i a b l e a
Complex b ( 3 . 0 ) , c (5 .0 ,−2.3) ; / / we dec larecomplex v a r i a b l e s b and c
Complex d = b ; / / we dec lare a newcomplex v a r i a b l e d
cout
-
Programming classes
We include the header file complex.h and define four differentcomplex variables. These are a = 0.1 + ı1.3, b = 3.0 + ı0 (notethat if you don’t define a value for the imaginary part this is setto zero), c = 5.0− ı2.3 and d = b. Thereafter we have definedstandard algebraic operations and the member functions of theclass which allows us to print out the real and imaginary part ofa given variable.
128 / 234
-
Programming classes
class Complex{private :
double re , im ; / / r e a l and imaginary pa r tpublic :
Complex ( ) ; / /Complex c ;
Complex ( double re , double im = 0 .0 ) ; / /D e f i n i t i o n o f a complex v a r i a b l e ;
Complex ( const Complex& c ) ; / /Usage : Complex c ( a ) ; / / equate two complex
v a r i a b l e sComplex& operator= ( const Complex& c ) ; / / c = a ;
/ / equate two complex var iab les , same asprev ious
. . . .
129 / 234
-
Programming classes
˜Complex ( ) {} / /d e s t r u c t o r
double Re ( ) const ; / / double r e a l p a r t= a .Re ( ) ;
double Im ( ) const ; / / double imag par t= a . Im ( ) ;
double abs ( ) const ; / / double m = a . abs( ) ; / / modulus
f r iend Complex operator+ ( const Complex& a ,const Complex& b ) ;
f r iend Complex operator− ( const Complex& a ,const Complex& b ) ;
f r iend Complex operator∗ ( const Complex& a ,const Complex& b ) ;
f r iend Complex operator / ( const Complex& a ,const Complex& b ) ;
} ;
130 / 234
-
Programming classes
The class is defined via the statement class Complex. We mustfirst use the key word class, which in turn is followed by theuser-defined variable name Complex. The body of the class,data and functions, is encapsulated within the parentheses{...};.
131 / 234
-
Programming classes
Data and specific functions can be private, which means thatthey cannot be accessed from outside the class. This meansalso that access cannot be inherited by other functions outsidethe class. If we use protected instead of private, then data andfunctions can be inherited outside the class.
132 / 234
-
Programming classes
The key word public means that data and functions can beaccessed from outside the class. Here we have defined severalfunctions which can be accessed by functions outside the class.The declaration friend means that stand-alone functions canwork on privately declared variables of the type (re, im). Datamembers of a class should be declared as private variables.
133 / 234
-
Programming classes
The first public function we encounter is a so-called constructor,which tells how we declare a variable of type Complex and howthis variable is initialized. We have chose three possibilities inthe example above:
I A declaration like Complex c; calls the member functionComplex() which can have the following implementation
Complex : : Complex ( ) { re = im = 0 . 0 ; }
meaning that it sets the real and imaginary parts to zero.Note the way a member function is defined. Theconstructor is the first function that is called when an objectis instantiated.
134 / 234
-
Programming classes
I Another possibility is
Complex : : Complex ( ) {}
which means that there is no initialization of the real andimaginary parts. The drawback is that a given compiler canthen assign random values to a given variable.
I A call like Complex a(0.1,1.3); means that we could call themember function Complex(double, double)as
Complex : : Complex ( double re a , double im a ){ re = re a ; im = im a ; }
135 / 234
-
Programming classes
The simplest member function are those we defined to extractthe real and imaginary part of a variable. Here you have torecall that these are private data, that is they invisible for usersof the class. We obtain a copy of these variables by defining thefunctions
double Complex : : Re ( ) const { return re ; }} / /g e t t i n g the r e a l pa r t
double Complex : : Im ( ) const { return im ; } / /and the imaginary pa r t
Note that we have introduced the declaration const. What doesit mean? This declaration means that a variabale cannot bechanged within a called function.
136 / 234
-
Programming classes
If we define a variable as const double p = 3; and then try tochange its value, we will get an error when we compile ourprogram. This means that constant arguments in functionscannot be changed.
/ / const arguments ( i n f u n c t i o n s ) cannot be changed:
void myfunc ( const Complex& c ){ c . re = 0 . 2 ; /∗ ILLEGAL ! ! compi ler e r r o r . . . ∗ / }
If we declare the function and try to change the value to 0.2, thecompiler will complain by sending an error message.
137 / 234
-
Programming classes
If we define a function to compute the absolute value ofcomplex variable like
double Complex : : abs ( ) { return s q r t ( re ∗ re + im∗im ) ;}
without the constant declaration and define thereafter afunction myabs as
double myabs ( const Complex& c ){ return c . abs ( ) ; } / / Not ok because c . abs ( ) i s
not a const func .
the compiler would not allow the c.abs() call in myabs sinceComplex::abs is not a constant member function.
138 / 234
-
Programming classes
Constant functions cannot change the object’s state. To avoidthis we declare the function abs as
double Complex : : abs ( ) const { return s q r t ( re ∗ re +im∗ im ) ; }
139 / 234
-
Programming classes
C++ (and Fortran) allow for overloading of operators. Thatmeans we can define algebraic operations on for examplevectors or any arbitrary object. As an example, a vectoraddition of the type c = a + b means that we need to write asmall part of code with a for-loop over the dimension of thearray. We would rather like to write this statement as c = a+b; asthis makes the code much more readable and close to eventualequations we want to code. To achieve this we need to extendthe definition of operators.
140 / 234
-
Programming classes
Let us study the declarations in our complex class. In our mainfunction we have a statement like d = b;, which means that wecall d.operator= (b) and we have defined a so-calledassignment operator as a part of the class defined as
Complex& Complex : : operator= ( const Complex& c ){
re = c . re ;im = c . im ;return ∗ th is ;
}
141 / 234
-
Programming classes
With this function, statements like Complex d = b; orComplex d(b); make a new object d , which becomes a copy ofb. We can make simple implementations in terms of theassignment
Complex : : Complex ( const Complex& c ){ ∗ th is = c ; }
which is a pointer to ”this object”, ∗this is the present object, so∗this = c; means setting the present object equal to c, that isthis−>operator= (c);.
142 / 234
-
Programming classes
The meaning of the addition operator + for Complex objects isdefined in the function Complex operator+ (const Complex& a,const Complex& b); //a+b The compiler translates c = a + b; intoc = operator+ (a, b);. Since this implies the call to function, itbrings in an additional overhead. If speed is crucial and thisfunction call is performed inside a loop, then it is more difficultfor a given compiler to perform optimizations of a loop.
143 / 234
-
Programming classes
The solution to this is to inline functions. We discussed inliningin chapter 2 of the lecture notes. Inlining means that thefunction body is copied directly into the calling code, thusavoiding calling the function. Inlining is enabled by the inlinekeyword
i n l i n e Complex operator+ ( const Complex& a , constComplex& b )
{ return Complex ( a . re + b . re , a . im + b . im ) ; }
Inline functions, with complete bodies must be written in theheader file complex.h.
144 / 234
-
Programming classes
Consider the case c = a + b; that is,c.operator= (operator+ (a,b)); If operator+, operator= and theconstructor Complex(r,i) all are inline functions, this transformsto
c . re = a . re + b . re ;c . im = a . im + b . im ;
by the compiler, i.e., no function calls
145 / 234
-
Programming classes
The stand-alone function operator+ is a friend of the Complexclass
class Complex{
. . .f r iend Complex operator+ ( const Complex& a ,
const Complex& b ) ;. . .
} ;
so it can read (and manipulate) the private data parts re and imvia
i n l i n e Complex operator+ ( const Complex& a , constComplex& b )
{ return Complex ( a . re + b . re , a . im + b . im ) ; }
146 / 234
-
Programming classes
Since we do not need to alter the re and im variables, we canget the values by Re() and Im(), and there is no need to be afriend function
i n l i n e Complex operator+ ( const Complex& a , constComplex& b )
{ return Complex ( a .Re ( ) + b .Re ( ) , a . Im ( ) + b . Im ( ) ); }
147 / 234
-
Programming classes
The multiplication functionality can now be extended toimaginary numbers by the following code
i n l i n e Complex operator∗ ( const Complex& a , constComplex& b )
{return Complex ( a . re ∗b . re − a . im∗b . im , a . im∗b . re +
a . re ∗b . im ) ;}
It will be convenient to inline all functions used by this operator.
148 / 234
-
Programming classes
To inline the complete expression a∗b;, the constructors andoperator= must also be inlined. This can be achieved via thefollowing piece of code
i n l i n e Complex : : Complex ( ) { re = im = 0 . 0 ; }i n l i n e Complex : : Complex ( double re , double im ){ . . . }i n l i n e Complex : : Complex ( const Complex& c ){ . . . }i n l i n e Complex : : operator= ( const Complex& c ){ . . . }
149 / 234
-
Programming classes
/ / e , c , d are complexe = c∗d ;/ / f i r s t compi ler t r a n s l a t i o n :e . operator= ( operator∗ ( c , d ) ) ;/ / r e s u l t o f nested i n l i n e f u n c t i o n s/ / opera tor = , opera tor ∗ , Complex ( double , double =0) :e . re = c . re ∗d . re − c . im∗d . im ;e . im = c . im∗d . re + c . re ∗d . im ;
The definitions operator− and operator/ follow the same set up.
150 / 234
-
Programming classes
Finally, if we wish to write to file or another device a complexnumber using the simple syntax cout
-
Programming classes, templates
What if we wanted to make a class which takes integers orfloating point numbers with single precision? A simple way toachieve this is copy and paste our class and replace doublewith for example int.C++ allows us to do this automatically via the usage oftemplates, which are the C++ constructs for parameterizingparts of classes. Class templates is a template for producingclasses. The declaration consists of the keyword templatefollowed by a list of template arguments enclosed in brackets.
152 / 234
-
Programming classesWe can therefore make a more general class by rewriting ouroriginal example as
templateclass Complex{private :
T re , im ; / / r e a l and imaginary pa r tpublic :
Complex ( ) ; / /Complex c ;
Complex (T re , T im = 0) ; / / D e f i n i t i o n o f acomplex v a r i a b l e ;
Complex ( const Complex& c ) ; / /Usage : Complex c ( a ) ; / / equate two complex
v a r i a b l e sComplex& operator= ( const Complex& c ) ; / / c = a ;
/ / equate two complex var iab les , same asprev ious
153 / 234
-
Programming classesWe can therefore make a more general class by rewriting ouroriginal example as
˜Complex ( ) {} / /d e s t r u c t o r
T Re ( ) const ; / / T r e a l p a r t = a .Re ( ) ;T Im ( ) const ; / / T imag par t = a . Im ( ) ;T abs ( ) const ; / / T m = a . abs ( ) ; / /
modulusf r iend Complex operator+ ( const Complex& a ,
const Complex& b ) ;f r iend Complex operator− ( const Complex& a ,
const Complex& b ) ;f r iend Complex operator∗ ( const Complex& a ,
const Complex& b ) ;f r iend Complex operator / ( const Complex& a ,
const Complex& b ) ;} ;
154 / 234
-
Programming classes
What it says is that Complex is a parameterized type with T asa parameter and T has to be a type such as double or float.The class complex is now a class template and we woulddefine variables in a code as
Complex a ( 1 0 . 0 , 5 . 1 ) ;Complex b (1 ,0 ) ;
155 / 234
-
Programming classes
Member functions of our class are defined by preceding thename of the function with the template keyword. Consider thefunction we defined asComplex:: Complex (double re a, double im a). We would rewritethis function as
templateComplex : : Complex (T re a , T im a ){ re = re a ; im = im a ; }
The member functions are otherwise defined following ordinarymember function definitions.
156 / 234
-
Programming classes
Here follows a very simple first class
/ / Class to compute the square o f a numberclass Squared{
public :/ / De fau l t cons t ruc to r , not used hereSquared ( ) {}
/ / Overload the f u n c t i o n opera tor ( )double operator ( ) ( double x ) {
return x∗x ;}
} ;
157 / 234
-
Programming classes
and we would use it as
#include using namespace s td ;
i n t main ( ) {Squared s ;cout
-
The Hartree-Fock code
It can be useful to split the code in a system and a solver part,where the system part contains information about variousquantum numbers, the interaction elements such as theexpectation value of the one-body and two-body interactionsand other properties pertinent to a given system. The solverpart needs only to deal with the explicit solution algorithm andtakes as input for example only indices relevant to a givensingle-particle system. Here’s an example which contains
I A basis classI An interaction class, the Coulomb interaction hereI And a solver class, which here contains only the
Hartree-Fock method
159 / 234
-
The Hartree-Fock code: the main program
#include #include #include #include #include #include "coulomb.h"#include "basis.h"#include "hartreeFock.h"#include
using namespace arma ;using namespace std ;
160 / 234
-
The Hartree-Fock code: the main program/ / The main programi n t main ( i n t argc , char∗∗ argv ) {
i n t n P a r t i c l e s = 4;i n t dim = 3;i n t n = 20; / / number o f s ing le−p a r t i c l e s ta tesdouble w = 1;double t h resho ld = 1e−4;i n t HFI te ra t i ons = 30;
/ / Se t t i ng up the basisbasis harm = basis ( dim , n , w) ;harm . createBas is ( ) ;harm . genera te In te rac t ionE lements ( ) ;vector s ta tes = harm . getSPS ( ) ;map i n tE = harm . g e t I n t ( ) ;
/ / Running Hartree−Fock c a l c u l a t i o n shart reeFock HF = hart reeFock ( s ta tes , nPa r t i c l es
, i n tE ) ;HF. run ( ) ;. . . / / more statements , p r i n t e tcreturn 0;
}
161 / 234
-
The Hartree-Fock code: the basis class
# i f n d e f BASIS H#define BASIS H#include #include #include #include #include #include
using namespace std ;using namespace arma ;
162 / 234
-
The Hartree-Fock code: the basis class
class basis {public :
bas is ( ) ;bas is ( i n t dim , i n t n , double w) ;bas is ( const basis& o r i g ) ;v i r t u a l ˜ bas is ( ) ;void createBas is ( ) ;void genera te In te rac t ionE lements ( ) ;vector getSPS ( ) {
return s ta tes ;} ;vector get IE ( ) {
return s ta tes ;} ;
map g e t I n t ( ) {return i e ;
} ;
163 / 234
-
The Hartree-Fock code: the basis class
private :s t r i n g basisName ;i n t dim ;i n t n ;double w;i n t maxRange ;vector s ta tes ;vector i n te rac t i onE lemen ts ;map i e ;
} ;#endif /∗ BASIS H ∗ /
164 / 234
-
The Hartree-Fock code: the basis class and specificfunctions
. . . / / more statementsbasis : : bas is ( i n t dim , i n t n , double w) : dim ( dim ) ,
n ( n ) , w(w) {}
void basis : : c reateBas is ( ) {
/ / Generat ing a l l s i n g l e p a r t i c l e s ta tes andenergies
vec s ta te ( 4 ) ;i n t counter = 0 ;/ / s p e c i f i c casefor ( i n t k = 0; k
-
The Hartree-Fock code: the basis class and specificfunctions
. . . . .void basis : : genera te In te rac t ionE lements ( ) {
double E;i n t n1 , l1 , s1 , n2 , l2 , s2 , n3 , l3 , s3 , n4 , l4 ,