File: grid_test.cc

package info (click to toggle)
ergo 3.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 17,396 kB
  • sloc: cpp: 94,740; ansic: 17,015; sh: 7,559; makefile: 1,402; yacc: 127; lex: 110; awk: 23
file content (284 lines) | stat: -rw-r--r-- 8,255 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
/* Ergo, version 3.8, a program for linear scaling electronic structure
 * calculations.
 * Copyright (C) 2019 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:
 * Ergo: An open-source program for linear-scaling electronic structure
 * calculations,
 * Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
 * Kruchinina,
 * SoftwareX 7, 107 (2018),
 * <http://dx.doi.org/10.1016/j.softx.2018.03.005>
 * 
 * For further information about Ergo, see <http://www.ergoscf.org>.
 */

/** @file grid_test.cc Tests the DFT grid generation.  This
    test generates the grid, possibly several times to detect problems
    with eg. thread synchronisation.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <memory>

#include "dft_common.h"
#include "grid_reader.h"
#include "grid_stream.h"

#define NATOMS      2
#define N_BF_SHELLS 2
#define N_BFS       4

static const struct {
  ergo_real position[3];
  int charge;
} Atoms[] = {
  { { 0, 0, 0 }, 1 },
  { { 0, 0, 1 }, 2 }
};

static ergo_real ShellRadii[] = { 1, 2 };

class MyMolInfo : public  GridGenMolInfo {
public:
  MyMolInfo() : GridGenMolInfo(NATOMS, N_BFS, N_BF_SHELLS) {}
  virtual void getAtom(int icent, int *cnt, real (*coor)[3],
                       int *charge, int *mult) const
  {
    *cnt = 1;
    coor[0][0] = Atoms[icent].position[0];
    coor[0][1] = Atoms[icent].position[1];
    coor[0][2] = Atoms[icent].position[2];
    *charge    = Atoms[icent].charge;
    *mult = 1;
  }

  virtual void setShellRadii(real *shellRadii) const
  {
    memcpy(shellRadii, ShellRadii, N_BF_SHELLS*sizeof(ergo_real));
  }

  virtual void getBlocks(const real *center, real cellsz, const real *rshell,
                         int *nblcnt, int (*iblcks)[2]) const
  {
    *nblcnt = 1;
    if (center[0]*center[0] + center[1]*center[1] + center[2]*center[2]
        < 3) {
      iblcks[0][0] = 0;
      iblcks[0][1] = N_BF_SHELLS;
    } else {
      iblcks[0][0] = 1;
      iblcks[0][1] = 2;
    }
  }

  virtual void getExps(int *maxl, int **bascnt, real (**aa)[2]) const
  {
    static const int lda = 2;
    maxl[0] = 2;
    *bascnt = (int*)calloc(lda*NATOMS, sizeof(int));
    *aa     = (real(*)[2])calloc(2*lda*NATOMS, sizeof(real));
    (*bascnt)[0 + 0*lda] = 1; /* s functions on first atom */
    (*bascnt)[1 + 0*lda] = 0; /* p functions on the first atom */
    (*bascnt)[0 + 1*lda] = 0; /* s functions on first atom */
    (*bascnt)[1 + 1*lda] = 1; /* p functions on the first atom */
  
    /* Range of exponents on the first atom */
    (*aa)[0][0] = 0.5;
    (*aa)[0][1] = 0.5;
    (*aa)[1][0] = 0.0; /* no p functions */
    (*aa)[1][1] = 0.0; /* no p functions */

    /* Range of exponents on the second atom */
    (*aa)[2][0] = 0.0;
    (*aa)[2][1] = 0.0;
    (*aa)[3][0] = 0.2;
    (*aa)[3][1] = 0.2;
  }
};

static const MyMolInfo MolInfo;

static bool
pattern_to_ps(Dft::SparsePattern& p, const char* fName)
{
//  const char * PS_PREFIX = "";
#if 0
    "%%!PS-Adobe-2.0 EPSF-2.0\n"
    "%%%%Title: Test\n"
    "%%%%Creator: Pawel Salek\n"
    "%%%%Magnification: 1.00\n"
    "%%%%Orientation: Portrait\n"
    "%%%%EndComments\n\n"
    "/inch { 72 mul } def\n"
    "/b { 0 rmoveto currentpoint currentpoint newpath moveto\n"
    "1 0 rlineto 0 1 rlineto -1 0 rlineto closepath fill moveto } def\n"
    "%% now run it!\n"
    "1 inch 1 inch moveto\n";
#endif

//  const char * PS_SUFFIX =
//    "\nshowpage\n"
//    "%%EOF\n";

  FILE *f=fopen(fName, "wt");
  if (!f) {
    perror("fopen");
    return false;
  }
//  fprintf(f, PS_PREFIX);
  fprintf(f, "%f %f scale %% size = %d\n", 72*6.0/p.size(), 72*6.0/p.size(),
	  p.size());
  for(int column=0; column<p.size(); ++column) {
    int lastbox = 0;
    fprintf(f,"currentpoint\n");
    for (Dft::SparsePattern::Column::Iterator i = p[column].begin();
	 i != p[column].end();
	 ++i) {
      fprintf(f,"%d b\n", *i-lastbox);
      //for(int x=lastbox; x<(*i)-1; ++x) putchar(' '); putchar('x');
      lastbox = *i;

    }
    fprintf(f,"moveto 0 1 rmoveto\n");
    //puts("");
  }
//  fprintf(f, PS_SUFFIX);
  fclose(f);
  return true;
}
/** This routine tests the sparsity pattern generation scalability
    properties.  At some point in time, water boxes of increasing
    sizes had irregular sparse pattern. This test helps to debug such
    cases.
 */ 
static bool
grid_test_scaling(const char *fName)
{
  time_t tm; time(&tm);

  Molecule mol;
  int res = mol.setFromMoleculeFile(fName, 0, NULL);

  if(res != 0) {
    fprintf(stderr, "Loading molecule from %s failed.\n", fName);
    return false;
  }

  IntegralInfo ii(true);
  BasisInfoStruct bisOrig;
  if(bisOrig.addBasisfuncsForMolecule(mol, ERGO_SPREFIX "/basis/STO-1G",
				       0, NULL, ii, 0, 0, 0) != 0) {
    printf("bis->addBasisfuncsForMolecule failed.\n");
    throw "addBasisfuncs failed";
  }
  Dft::GridParams gridParams(1e-7, 6, 25);
  gridParams.radialGridScheme = Dft::GridParams::LMG;
  
  int *shellMap = new int[bisOrig.noOfShells];
  int *aoMap = new int[bisOrig.noOfBasisFuncs];

  Dft::setupShellMap(bisOrig, shellMap, aoMap);
  BasisInfoStruct * bisPermuted = bisOrig.permuteShells(shellMap, ii);
  delete []shellMap;
  delete []aoMap;

  ErgoMolInfo molInfo(*bisPermuted, mol);
  /* The important structure of this test */
  Dft::SparsePattern pattern(*bisPermuted); 

  char grid_file_name[] = "scaling_test_XXXXXX";
  close(mkstemp(grid_file_name));
  ErgoGridStream *egStream = grid_stream_new(gridParams, molInfo);
  grid_stream_set_sparse_pattern(egStream, &pattern);
  grid_stream_generate(egStream, grid_file_name, dft_get_num_threads());
  grid_stream_free(egStream);

  printf("Stop %lu s wall time %d nonzero elements\n",
	 ((unsigned long)time(NULL))-tm, pattern.sizeTotal());
  unlink(grid_file_name);

  pattern_to_ps(pattern, "pattern.ps");
  return true;
}

static void
grid_test_synchronisation()
{
  static const int NREPEAT = 500;
  static const int BATCH_LENGTH = 50000;

  // Elias note: earlier, coor and weight were placed directly on the stack like this:
  //    real coor[BATCH_LENGTH][3];
  //    real weight[BATCH_LENGTH];
  // but that gave stack overflow on some systems, so it was changed
  // to the dynamic allocation below.

  real (*coor)[3];
  std::vector<real> coorData(3*BATCH_LENGTH);
  coor = (real (*)[3])(&coorData[0]);
  std::vector<real> weight(BATCH_LENGTH);

  Dft::GridParams gridParams(1e-5, 6, 7);

  Molecule mol;
  for(int i = 0; i < NATOMS; i++)
    mol.addAtom(Atoms[i].charge, Atoms[i].position[0], Atoms[i].position[1], Atoms[i].position[2]);

  IntegralInfo ii(true);
  BasisInfoStruct bis;
  if(bis.addBasisfuncsForMolecule(mol, ERGO_SPREFIX "/basis/STO-1G",
				   0, NULL, ii, 0, 0, 0) != 0) {
    printf("bis.addBasisfuncsForMolecule failed.\n");
    throw "addBasisfuncs failed";
  }
  
  for(int i = 0; i < NREPEAT; i++) {
    DftGridReader* g = grid_open_full(&MolInfo, gridParams, NULL, NULL, bis);
    int np;
    unsigned cnt = 0;
    while ( (np = grid_getchunk_blocked(g, BATCH_LENGTH,
                                        NULL, NULL, 
                                        coor, &weight[0])) >= 0){
      cnt += np;
    }
    grid_close(g);
    grid_free_files();
  }
  printf("Grid generation %i times succeeded.\n", NREPEAT); 
}

int
main(int argc, char *argv[])
{  
  const char *tmpdir = getenv("TMPDIR");
  tmpdir = tmpdir ? tmpdir : "/tmp";
  grid_set_tmpdir(tmpdir);

  switch (argc) {
  case 2:
    grid_test_scaling(argv[1]);
    break;
  default:
    grid_test_synchronisation();
  }

  return 0;
}