File: gradient_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 (346 lines) | stat: -rw-r--r-- 11,919 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* 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 gradient_test.cc Tests the gradient computation.  */

#include "integrals_1el_potential.h"
#include "integrals_1el_single.h"
#include "integrals_general.h"
#include "matrix_typedefs.h"
#include "integral_matrix_wrappers.h"
#include "matrix_utilities.h"
#include "output.h"

static void
preparePermutations(const BasisInfoStruct& basisInfo,
		    mat::SizesAndBlocks& sizeBlockInfo, 
		    std::vector<int>& permutation,
		    std::vector<int>& inversePermutation)
{
  static const int sparseMatrixBlockSize = 16, sparseMatrixBlockFactor = 4;
  sizeBlockInfo =
    prepareMatrixSizesAndBlocks(basisInfo.noOfBasisFuncs,
				sparseMatrixBlockSize,
				sparseMatrixBlockFactor,
				sparseMatrixBlockFactor,
				sparseMatrixBlockFactor);
  getMatrixPermutation(basisInfo,
		       sparseMatrixBlockSize,
		       sparseMatrixBlockFactor,
		       sparseMatrixBlockFactor,
		       sparseMatrixBlockFactor,
		       permutation,
		       inversePermutation);
}

static ergo_real get_nucl_energy_for_given_mol_and_dens(const IntegralInfo& integralInfo,
							const Molecule& molecule,
							const BasisInfoStruct& basisInfo,
							const symmMatrix & D,
							ergo_real threshold_integrals_1el,
							mat::SizesAndBlocks const & matrix_size_block_info,
							std::vector<int> const & permutationHML) {
  ergo_real nuclearRepulsionEnergy = molecule.getNuclearRepulsionEnergy();
  ergo_real elecNuclEnergy = get_electron_nuclear_attraction_energy(integralInfo,
								    molecule,
								    basisInfo,
								    D,
								    threshold_integrals_1el,
								    matrix_size_block_info,
								    permutationHML);
  return nuclearRepulsionEnergy + elecNuclEnergy;
}

static int get_gradient_using_finite_differences(const IntegralInfo& integralInfo,
						 const BasisInfoStruct& basisInfo,
						 const Molecule & molecule, 
						 const symmMatrix & densityMatrix,
						 ergo_real threshold_integrals_1el,
						 const mat::SizesAndBlocks & matrix_size_block_info,
						 const std::vector<int> & permutationHML,
						 ergo_real* resultGradient) {
  int nAtoms = molecule.getNoOfAtoms();
  for(int i = 0; i < nAtoms; i++) {
    for(int coordIdx = 0; coordIdx < 3; coordIdx++) {
      const ergo_real h = 1e-3;
      Molecule moleculeTmp = molecule;
      Atom atomTmp = molecule.getAtom(i);
      atomTmp.coords[coordIdx] += h;
      moleculeTmp.replaceAtom(i, atomTmp);
      ergo_real E1 = get_nucl_energy_for_given_mol_and_dens(integralInfo,
							    moleculeTmp,
							    basisInfo,
							    densityMatrix,
							    threshold_integrals_1el,
							    matrix_size_block_info,
							    permutationHML);
      moleculeTmp = molecule;
      atomTmp = molecule.getAtom(i);
      atomTmp.coords[coordIdx] -= h;
      moleculeTmp.replaceAtom(i, atomTmp);
      ergo_real E2 = get_nucl_energy_for_given_mol_and_dens(integralInfo,
							    moleculeTmp,
							    basisInfo,
							    densityMatrix,
							    threshold_integrals_1el,
							    matrix_size_block_info,
							    permutationHML);
      ergo_real gradientComponent = (E1 - E2) / (2 * h);
      resultGradient[i*3+coordIdx] = gradientComponent;
    } // END FOR coordIdx
  } // END FOR i
  return 0;
}

static int get_gradient_using_explicit_integrals(const IntegralInfo& integralInfo,
						 const BasisInfoStruct& basisInfo,
						 const Molecule & molecule, 
						 const symmMatrix & densityMatrix,
						 ergo_real threshold_integrals_1el,
						 const mat::SizesAndBlocks & matrix_size_block_info,
						 const std::vector<int> & permutationHML,
						 const std::vector<int> & inversePermutationHML,
						 ergo_real* resultGradient) {  
  int nAtoms = molecule.getNoOfAtoms();
  memset(resultGradient, 0, nAtoms*3*sizeof(ergo_real));
  int n = basisInfo.noOfBasisFuncs;
  std::vector<ergo_real> densityMatrix_full(n*n);
  densityMatrix.fullMatrix(densityMatrix_full, inversePermutationHML, inversePermutationHML);
  for(int i = 0; i < n; i++)
    for(int j = 0; j < n; j++) {
      // Consider gradient contributions related to V matrix element (i,j)
      // Compute list of simpleprimitives for product of basis funcs i and j.
      const int maxCount = 888;
      DistributionSpecStruct list[maxCount];
      int nPrims = get_product_simple_primitives(basisInfo, i, basisInfo, j, list, maxCount, 0);
      if(nPrims < 0)
	return -1;
      for(int m = 0; m < nPrims; m++)
	for(int k = 0; k < nAtoms; k++) {
	  std::vector<ergo_real> integralValues = do_1e_repulsion_integral_derivatives_using_symb_info(&list[m], 
												       molecule.getAtom(k).charge,
												       molecule.getAtom(k).coords,
												       integralInfo);
	  for(int coordIdx = 0; coordIdx < 3; coordIdx++) {
	    ergo_real integralValue = integralValues[coordIdx];
	    resultGradient[k*3+coordIdx] += -1 * integralValue * densityMatrix_full[i*n+j];
	  }
	}
    }
  molecule.getNuclearRepulsionEnergyGradientContrib(resultGradient);
  return 0;
}

static int
test_gradient_by_explicit_comparison(const IntegralInfo & integralInfo)
{
  Molecule molecule;
  
  // Put some atoms far away to make sure multipoles are used, but
  // still some of them close together so that significant overlaps
  // exist.
  molecule.addAtom(5.0, 0.2, 1.5, 0.5);
  molecule.addAtom(9.0, 3.6, 0.1, 0.2);
  molecule.addAtom(4.0, 1.6, 2.1, 0.9);
  molecule.addAtom(6.0, 10.1, 21.4, 85.3);
  molecule.addAtom(7.0, 13.6, 20.2, 85.1);
  molecule.addAtom(3.0, 11.2, 22.4, 85.6);
  molecule.addAtom(6.0, 10.4, 31.3, 74.7);
  molecule.addAtom(9.0, 13.5, 30.2, 74.4);
#if 0
  molecule.addAtom(7.0, 16.6, 15.2, 85.1);
  molecule.addAtom(3.0, 15.2, 18.4, 85.6);
  molecule.addAtom(6.0, 14.4, 26.3, 74.7);
  molecule.addAtom(9.0, 13.5, 28.2, 74.1);
  molecule.addAtom(6.0, 4.4, 26.3, 34.7);
  molecule.addAtom(9.0, 3.5, 28.2, 34.1);
  molecule.addAtom(6.0, 4.4, 26.3, 36.9);
  molecule.addAtom(9.0, 3.5, 27.2, 36.5);
#endif

  BasisInfoStruct basisInfo;
  if(basisInfo.addBasisfuncsForMolecule(molecule,
					 "6-31Gss",
					 0,
					 NULL,
					 integralInfo,
					 0,
					 1,
					 0) != 0)
    {
      puts("error in basisInfo.addBasisfuncsGorMolecule");
      return -1;
    }
  
  std::vector<int> permutationHML, inversePermutationHML;
  mat::SizesAndBlocks sizeBlockInfo;
  preparePermutations(basisInfo, sizeBlockInfo,
		      permutationHML, inversePermutationHML);
  ergo_real threshold = 1e-12;
  ergo_real boxSize = 3.3; // Use small box size to provoke errors.
  symmMatrix V;
  V.resetSizesAndBlocks(sizeBlockInfo, sizeBlockInfo);

  symmMatrix D;
  D.resetSizesAndBlocks(sizeBlockInfo, sizeBlockInfo);
  {
    /* Add values to density matrix diagonal and one step 
       next to diagonal. */
    const int nvalues1 = basisInfo.noOfBasisFuncs;
    std::vector<int> idxrow(nvalues1);
    std::vector<int> idxcol(nvalues1);
    std::vector<ergo_real> values(nvalues1);
    for(int i=0; i<nvalues1; i++) {
      idxrow[i] = i;
      idxcol[i] = i;
      values[i] = 1.0;
    }
    D.add_values(idxrow, idxcol, values, permutationHML, permutationHML);
    const int nvalues2 = basisInfo.noOfBasisFuncs-1;
    for(int i=0; i<nvalues2; i++) {
      idxrow[i] = i;
      idxcol[i] = i+1;
      values[i] = 0.3;
    }
    idxrow.resize(nvalues2); 
    idxcol.resize(nvalues2); 
    values.resize(nvalues2); 
    D.add_values(idxrow, idxcol, values, permutationHML, permutationHML);
  }  

  int nAtoms = molecule.getNoOfAtoms();

  // Compute gradient in 3 ways:
  // - 1: Using finite differences
  // - 2: Using explicit integrals
  // - 3: By calling the real linear-scaling gradient computation routine
  std::vector<ergo_real> gradient1(nAtoms*3);
  std::vector<ergo_real> gradient2(nAtoms*3);
  std::vector<ergo_real> gradient3(nAtoms*3);

  // - 1: Compute gradient using finite differences
  if(get_gradient_using_finite_differences(integralInfo,
					   basisInfo,
					   molecule, 
					   D,
					   threshold,
					   sizeBlockInfo,
					   permutationHML,
					   &gradient1[0]) != 0) {
    printf("Error in get_gradient_using_finite_differences.\n");
    throw "Error in get_gradient_using_finite_differences";
  }

  // - 2: Compute gradient using explicit integrals
  if(get_gradient_using_explicit_integrals(integralInfo,
					   basisInfo,
					   molecule, 
					   D,
					   threshold,
					   sizeBlockInfo,
					   permutationHML,
					   inversePermutationHML,
					   &gradient2[0]) != 0) {
    printf("Error in get_gradient_using_explicit_integrals.\n");
    throw "Error in get_gradient_using_explicit_integrals";
  }

  // - 3: Compute gradient using by calling the real linear-scaling gradient computation routine
  if(compute_gradient_of_nucl_and_trDV(basisInfo,
				       integralInfo, 
				       molecule,
				       threshold,
				       boxSize,
				       D,
				       permutationHML,
				       &gradient3[0]) != 0) {
    printf("Error in compute_gradient_of_nucl_and_trDV.\n");
    throw "Error in compute_gradient_of_nucl_and_trDV";
  }

  ergo_real maxabsdiff_1_2 = 0;
  ergo_real maxabsdiff_1_3 = 0;
  ergo_real maxabsdiff_2_3 = 0;
  for(int i = 0; i < nAtoms; i++) 
    for(int coordIdx = 0; coordIdx < 3; coordIdx++) {
      int idx = i*3+coordIdx;
      ergo_real absdiff_1_2 = std::fabs(gradient1[idx] - gradient2[idx]);
      ergo_real absdiff_1_3 = std::fabs(gradient1[idx] - gradient3[idx]);
      ergo_real absdiff_2_3 = std::fabs(gradient2[idx] - gradient3[idx]);
      if(absdiff_1_2 > maxabsdiff_1_2)
	maxabsdiff_1_2 = absdiff_1_2;
      if(absdiff_1_3 > maxabsdiff_1_3)
	maxabsdiff_1_3 = absdiff_1_3;
      if(absdiff_2_3 > maxabsdiff_2_3)
	maxabsdiff_2_3 = absdiff_2_3;
    }  
  printf("maxabsdiff_1_2 = %22.11f\n", (double)maxabsdiff_1_2);
  printf("maxabsdiff_1_3 = %22.11f\n", (double)maxabsdiff_1_3);
  printf("maxabsdiff_2_3 = %22.11f\n", (double)maxabsdiff_2_3);
  if(maxabsdiff_1_2 > 2e-5) {
    printf("Error: maxabsdiff_1_2 too large.\n");
    return -1;
  }
  if(maxabsdiff_2_3 > 5e-9) {
    printf("Error: maxabsdiff_2_3 too large.\n");
    return -1;
  }
  printf("OK!\n");
  puts("test_gradient_by_explicit_comparison OK.");
  return 0;
}



int main(int argc, char *argv[])
{
#ifdef PRECISION_SINGLE
  printf("gradient_test: skipping this test because PRECISION_SINGLE is defined.\n");
  return 0;
#endif

  IntegralInfo integralInfo(true);
  int errorCount = 0;

  // enable_output(); // Do this if you want the ergoscf.out file, to see timings etc.

  if(test_gradient_by_explicit_comparison(integralInfo) != 0)
    {
      puts("error in test_gradient_by_explicit_comparison.");
      errorCount++;
    }

  if(errorCount != 0)
    {
      puts("ERROR in gradient tests.");
      return -1;
    }

  puts("Gradient tests OK.");
  return 0;
}