File: explicit_integral_test.cc

package info (click to toggle)
ergo 3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 17,044 kB
  • ctags: 6,813
  • sloc: cpp: 91,488; ansic: 15,728; sh: 6,416; makefile: 1,287; yacc: 123; lex: 108
file content (164 lines) | stat: -rw-r--r-- 5,890 bytes parent folder | download
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* Ergo, version 3.5, a program for linear scaling electronic structure
 * calculations.
 * Copyright (C) 2016 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
 * and Anastasia Kruchinina.
 * 
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 * 
 * Primary academic reference:
 * Kohn−Sham Density Functional Theory Electronic Structure Calculations 
 * with Linearly Scaling Computational Time and Memory Usage,
 * Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek,
 * J. Chem. Theory Comput. 7, 340 (2011),
 * <http://dx.doi.org/10.1021/ct100611z>
 * 
 * For further information about Ergo, see <http://www.ergoscf.org>.
 */

/** @file explicit_integral_test.cc Tests the explicit computation of
    2-electron integrals by moving basis functions by small distances
    and verifying that the computed 2-el integrals vary smoothly. */

#include <stdio.h>
#include <unistd.h>
#include <memory>
#include <limits>
#include <vector>

#include "integrals_2el_explicit.h"

int try_integral_diffs(const IntegralInfo & integralInfo, ergo_real h) {

  Molecule m;

  int nx = 2;
  int ny = 1;
  int nz = 1;

  const ergo_real space = 8.8;
  int atomCount = 0;
  for(int ix = 0; ix < nx; ix++)
    for(int iy = 0; iy < ny; iy++)
      for(int iz = 0; iz < nz; iz++)
	{
	  ergo_real x = ix*space + 0.4*std::cos((ix+iy+iz)*0.2+0.0)*space;
	  ergo_real y = iy*space + 0.4*std::cos((ix+iy+iz)*0.2+0.3)*space;
	  ergo_real z = iz*space + 0.4*std::cos((ix+iy+iz)*0.2+0.6)*space;
	  /* Use a mix of charges: H, C, Zn.
	     It is good to have some Zn there so we check also usage
	     of basis functions of f type. */
	  int charge = 1;
	  if(atomCount%3 == 0)
	    charge = 6;
	  //	  if(atomCount%9 == 0)
	  //	    charge = 30;
	  m.addAtom(charge, x, y, z);
	  atomCount++;
	}

  ergo_real moleculeBaseCoord = m.getAtom(0).coords[0];

  ergo_real machine_epsilon = std::numeric_limits<ergo_real>::epsilon();

  // OK, now we have a molecule. Now do a loop where we move one atom
  // by a small distance and save resulting 2-el integrals for each
  // case.
  const int noOfCases = 55;
  std::vector< std::vector< std::vector< std::vector< std::vector<ergo_real> > > > > integralList(noOfCases);
  int n = 0; // Will be set later.
  for(int caseIdx = 0; caseIdx < noOfCases; caseIdx++) {
    // Move first atom by small distance in x direction.
    Atom atom = m.getAtom(0);
    atom.coords[0] = moleculeBaseCoord + caseIdx * h;
    m.replaceAtom(0, atom);
    // New scope here so that the BasisInfoStruct pointer is freed each time.
    {
      BasisInfoStruct bis;
      if(bis.addBasisfuncsForMolecule(m, ERGO_SPREFIX "/basis/3-21G",
				       0, NULL, integralInfo, 0, 0, 0) != 0) {
	printf("bis.addBasisfuncsForMolecule failed.\n");
	return -1;
      }
      // OK, now we have a basis set for the current version of the
      // molecule. Now compute integrals.
      n = bis.noOfBasisFuncs;
      integralList[caseIdx].resize(n);
      for(int i = 0; i < n; i++) {
	integralList[caseIdx][i].resize(n);
	for(int j = i; j < n; j++) {
	  integralList[caseIdx][i][j].resize(n);
	  for(int k = 0; k < n; k++) {
	    integralList[caseIdx][i][j][k].resize(n);
	    for(int l = k; l < n; l++) {
	      integralList[caseIdx][i][j][k][l] = do_2e_integral(i, k, l, j, bis, integralInfo);
	    }
	  }
	}
      }
    } // End of scope for temporary BasisInfoStruct
  } // END FOR caseIdx
  // OK, now we have integrals computed for all cases. Now check if
  // each integral value varies smoothly.
  int count = 0;
  for(int i = 0; i < n; i++)
    for(int j = i; j < n; j++)
      for(int k = 0; k < n; k++)
	for(int l = k; l < n; l++) {
	  // Now we are interested in the integral (ij|kl). First
	  // check if this integral values seems to be approximately
	  // linearly dependent on the modufied atomic coordinate.
	  ergo_real totDiff  = integralList[noOfCases-1][i][j][k][l] - integralList[0][i][j][k][l];
	  ergo_real halfDiff = integralList[noOfCases/2][i][j][k][l] - integralList[0][i][j][k][l];
	  if(halfDiff != 0 && std::fabs(totDiff) > machine_epsilon) {
	    ergo_real kvot = totDiff / halfDiff;
	    // Now if kvot is approximately 2 we think this integral behaves linearly.
	    if(kvot > 1.95 && kvot < 2.05) {
	      count++;
	      for(int caseIdx = 1; caseIdx < noOfCases; caseIdx++) {
		ergo_real diff = integralList[caseIdx][i][j][k][l] - integralList[caseIdx-1][i][j][k][l];
		ergo_real expectedDiff = totDiff / (noOfCases-1);
		if(diff/expectedDiff < 0.9 || diff/expectedDiff > 1.1) {
		  printf("Error for integral i j k l : %d %d %d %d, expectedDiff = %9.5g, diff = %9.5g\n", 
			 i, j, k, l, (double)expectedDiff, (double)diff);
		  return -1;
		} // END IF
	      } // END FOR caseIdx
	    } // END IF
	  } // END IF (halfDiff != 0)
	} // END FOR i j k l

  unlink("ergoscf.out");

  printf("try_integral_diffs finished OK, count = %d.\n", count);

  if(count == 0)
    exit(0);

  return 0;
}


int main(int argc, char *argv[]) {
  IntegralInfo integralInfo(true);
  integralInfo.init();

  ergo_real machine_epsilon = std::numeric_limits<ergo_real>::epsilon();
  ergo_real h = 10000*machine_epsilon;
  printf("machine_epsilon = %9.5g, using h = %9.5g\n", (double)machine_epsilon, (double)h);

  if(try_integral_diffs(integralInfo, h) != 0)
    return -1;

  return 0;
}