-
Notifications
You must be signed in to change notification settings - Fork 62
/
h2.cc
148 lines (118 loc) · 4.91 KB
/
h2.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
This file is part of MADNESS.
Copyright (C) 2007,2010 Oak Ridge National Laboratory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For more information please contact:
Robert J. Harrison
Oak Ridge National Laboratory
One Bethel Valley Road
P.O. Box 2008, MS-6367
email: harrisonrj@ornl.gov
tel: 865-241-3937
fax: 865-572-0680
$Id$
*/
/*!
\file h2.cc
\brief Solves the Hartree-Fock equations for the hydrogen molecule
\defgroup examplesh2hf Hartree-Fock equations for the hydrogen molecule
\ingroup examples
The source is <a href=https://github.com/m-a-d-n-e-s-s/madness/blob/master/src/examples/h2.cc>h2.cc</a>.
The Hartree-Fock wave function is computed for the hydrogen molecule
in three dimensions without using symmetry.
Since all of the details except for the nuclear potential are the
same, please refer to the \ref examplehehf helium atom HF example.
*/
#include <madness/mra/mra.h>
#include <madness/mra/operator.h>
using namespace madness;
static const double R = 1.4; // bond length
static const double L = 64.0*R; // box size
static const long k = 8; // wavelet order
static const double thresh = 1e-6; // precision
static double guess(const coord_3d& r) {
const double x=r[0], y=r[1], z=r[2];
return (exp(-sqrt(x*x+y*y+(z-R/2)*(z-R/2)+1e-8))+
exp(-sqrt(x*x+y*y+(z+R/2)*(z+R/2)+1e-8)));
}
static double V(const coord_3d& r) {
const double x=r[0], y=r[1], z=r[2];
return -1.0/sqrt(x*x+y*y+(z-R/2)*(z-R/2)+1e-8)+
-1.0/sqrt(x*x+y*y+(z+R/2)*(z+R/2)+1e-8);
}
void iterate(World& world, real_function_3d& V, real_function_3d& psi, double& eps) {
real_convolution_3d op = BSHOperator3D(world, sqrt(-2*eps), 0.001, 1e-6);
real_function_3d Vpsi = (V*psi);
Vpsi.scale(-2.0).truncate();
real_function_3d tmp = op(Vpsi).truncate();
double norm = tmp.norm2();
real_function_3d r = tmp-psi;
double rnorm = r.norm2();
double eps_new = eps - 0.5*inner(Vpsi,r)/(norm*norm);
if (world.rank() == 0) {
print("norm=",norm," eps=",eps," err(psi)=",rnorm," err(eps)=",eps_new-eps);
}
psi = tmp.scale(1.0/norm);
eps = eps_new;
}
int main(int argc, char** argv) {
initialize(argc, argv);
World world(SafeMPI::COMM_WORLD);
startup(world,argc,argv);
std::cout.precision(6);
FunctionDefaults<3>::set_k(k);
FunctionDefaults<3>::set_thresh(thresh);
FunctionDefaults<3>::set_refine(true);
FunctionDefaults<3>::set_initial_level(5);
FunctionDefaults<3>::set_truncate_mode(1);
FunctionDefaults<3>::set_cubic_cell(-L/2, L/2);
// for (int i=0; i<3; i++) {
// FunctionDefaults<3>::cell(i,0) = -L/2;
// FunctionDefaults<3>::cell(i,1) = L/2;
// }
real_function_3d Vnuc = real_factory_3d(world).f(V);
real_function_3d psi = real_factory_3d(world).f(guess);
psi.truncate();
psi.scale(1.0/psi.norm2());
real_convolution_3d op = CoulombOperator(world, 0.001, 1e-6);
double eps = -0.6;
for (int iter=0; iter<10; iter++) {
real_function_3d rho = square(psi).truncate();
real_function_3d potential = Vnuc + op(rho).truncate();
iterate(world, potential, psi, eps);
}
double kinetic_energy = 0.0;
for (int axis=0; axis<3; axis++) {
real_derivative_3d D = free_space_derivative<double,3>(world, axis);
real_function_3d dpsi = D(psi);
kinetic_energy += inner(dpsi,dpsi);
}
real_function_3d rho = square(psi);
double two_electron_energy = inner(op(rho),rho);
double nuclear_attraction_energy = 2.0*inner(Vnuc,rho);
double nuclear_repulsion_energy = 1.0/R;
double total_energy = kinetic_energy + two_electron_energy +
nuclear_attraction_energy + nuclear_repulsion_energy;
double virial = (nuclear_attraction_energy + two_electron_energy + nuclear_repulsion_energy) / kinetic_energy;
if (world.rank() == 0) {
print(" Kinetic energy ", kinetic_energy);
print(" Nuclear attraction energy ", nuclear_attraction_energy);
print(" Two-electron energy ", two_electron_energy);
print(" Nuclear repulsion energy ", nuclear_repulsion_energy);
print(" Total energy ", total_energy);
print(" Virial ", virial);
}
world.gop.fence();
finalize();
return 0;
}