File: basisset.cc

package info (click to toggle)
ergo 3.8.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,568 kB
  • sloc: cpp: 94,763; ansic: 17,785; sh: 10,701; makefile: 1,403; yacc: 127; lex: 116; awk: 23
file content (407 lines) | stat: -rw-r--r-- 14,771 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* Ergo, version 3.8.2, a program for linear scaling electronic structure
 * calculations.
 * Copyright (C) 2023 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 basisset.cc

    \brief Code for representing basis set information for Gaussian
    basis sets, and for parsing a text file specifying such a
    basisset.

    @author: Elias Rudberg <em>responsible</em>.
*/

/* -*-mode:c; indent-tabs-mode: nil -*- */
/* basisset.c: provides read_basisset_file() which creates a
   basisset_struct from a data contained in a file */

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <stdexcept>

#include "basisset.h"
#include "output.h"
#include "memorymanag.h"


basisset_info::basisset_info() {
  atoms.resize(MAX_NO_OF_ATOM_TYPES);
  clear();
}

void basisset_info::clear() {
  memset(&atoms[0], 0x00, MAX_NO_OF_ATOM_TYPES*sizeof(basisset_atom_struct));
}

void basisset_info::write_to_buffer ( char * dataBuffer, size_t const bufferSize ) const {
  if(bufferSize < get_size())
    throw std::runtime_error("Error in basisset_info::write_to_buffer: bufferSize too small.");
  memcpy(dataBuffer, &atoms[0], MAX_NO_OF_ATOM_TYPES*sizeof(basisset_atom_struct));
}

size_t basisset_info::get_size() const {
  return MAX_NO_OF_ATOM_TYPES*sizeof(basisset_atom_struct);
}

void basisset_info::assign_from_buffer ( char const * dataBuffer, size_t const bufferSize) {
  if(bufferSize != get_size())
    throw std::runtime_error("Error in basisset_info::assign_from_buffer: wrong bufferSize.");
  memcpy(&atoms[0], dataBuffer, MAX_NO_OF_ATOM_TYPES*sizeof(basisset_atom_struct));
}


static void
remove_zeros(basisset_atom_struct* currAtom,
             int shellBaseIndex, int noOfShellsCurrBatch) {
  /*  Remove zero coefficients. */
  for(int i = 0; i < noOfShellsCurrBatch; i++) {
    int count = 0;
    ergo_real *coeffList = currAtom->shells[shellBaseIndex+i].coeffList;
    ergo_real *expList   = currAtom->shells[shellBaseIndex+i].exponentList;
    for(int j = 0; j < currAtom->shells[shellBaseIndex+i].contrCount; j++) {
      ergo_real currCoeff    = coeffList[j];
      ergo_real currExponent = expList[j];
      if(currCoeff != 0) {
	coeffList[count] = currCoeff;
	expList[count] = currExponent;
	count++;
      }
    } /*  END FOR j */
    currAtom->shells[shellBaseIndex+i].contrCount = count;
  } /*  END FOR i         */
}

/* read_basisset_file: reads a basis set from fileName. The basis set
   exponents and contraction coefficients are placed in result.  The
   reading procedure is bit convoluted because the basis set file
   follows the Fortran syntax, with wrapping and skipping empty
   elements. We basically need to emulate fortran `format'
   statement. There is one "but" though: AhlrichsDenFit does not
   follow the format syntax so it will/may be misread by eg. dalton. What
   a mess...
   
   The parser is implemented as a state machine. It still cannot read
   ANO-type basis sets...
*/
int 
read_basisset_file(basisset_info & result, 
		   const char* fileName,
                   int dirc, 
		   const char *dirv[],
                   int print_raw)
{
  enum { END_PARSING, ATOM_EXPECTED, SHELL_EXPECTED,
         SHELL_OR_ATOM_EXPECTED, CONTRACTION_BLOCK_EXPECTED } state;
  int uncontracted = 0;
  char line[512];
  basisset_atom_struct* currAtom = NULL;
  int spdf = -1;
  int shellBaseIndex = -1;
  int expNo = -1;
  FILE* f = NULL;

  if(!fileName) {
    do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in read_basisset_file: fileName == NULL.");
    return -1;
  }
  if(fileName[0] == '/')
    f = fopen(fileName, "rt");
  else {
    for(int i = 0; i < dirc; i++) {
      const int ffname_sz = 256;
      char ffname[ffname_sz];
      int len = strlen(dirv[i]);
      strncpy(ffname, dirv[i], sizeof(ffname));
      if(len>0 && ffname[len-1] != '/')
        strncat(ffname, "/", sizeof(ffname)-1-len);
      strncat(ffname, fileName, sizeof(ffname)-2-len);
      do_output(LOG_CAT_WARNING, LOG_AREA_INTEGRALS, 
                "Trying basis set file '%s'...", ffname);
      if( (f=fopen(ffname, "rt")) != NULL)
        break;
      /* To make things work also if filenames contain underscore '_'
	 characters instead of parentheses: Try also with replacing
	 '(' and ')' characters with underscore '_' characters in the
	 filename. */
      for(int i = 0; i < ffname_sz; i++) {
	char c = ffname[i];
	if(c == '('  || c == ')')
	  ffname[i] = '_';
      }
      do_output(LOG_CAT_WARNING, LOG_AREA_INTEGRALS,
                "Trying basis set file '%s'...", ffname);
      if( (f=fopen(ffname, "rt")) != NULL)
        break;
    }
  }

  if(f == NULL)
    {
      do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error opening file '%s'", fileName);
      return -1;
    }
  
  /*  now create basis set by reading buf2 */
  result.clear();
  int noOfAtomTypes = 0;
  state = ATOM_EXPECTED;
  int lineNo = 0;
  int lineConsumed = 1;
  ergo_real currExponent = 0;
  /* start global parsing loop */
  do {
    int dummy;
    if(lineConsumed) {
      for(unsigned int cc = 0; cc < sizeof(line); cc++)
	line[cc] = '\0';
      if(fgets(line, sizeof(line), f) == NULL) {
        state = END_PARSING;
        break;
      }
      // This only works if sizeof(line) is large enough. Here we check that sizeof(line) is really large enough.
      if(strlen(line) >= sizeof(line)-1) {
	do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "Error in read_basisset_file: (strlen(line) >= sizeof(line)-1). "
		  "This indicates that the basis set file '%s' included a line that was too long, longer than %d characters.", fileName, (int)(sizeof(line)-1));
	return -1;
      }
      lineConsumed = 0;
      lineNo++;
    }

    for(int cc = strlen(line)-1; cc>=0 && isspace(line[cc]); cc--)
      line[cc] = '\0';
        
    if(line[0] == '$' || line[0] == '!' || line[0] == '#'||
       line[0] == '*' || line[0] == '\0'|| line[0] == '\n') {
      lineConsumed = 1; /* skip the comment and move on */
      continue;
    }
    switch(state) {
    case ATOM_EXPECTED:
      if(line[0] == 'a' || line[0] == 'A') {
        noOfAtomTypes++;
        int atomType = atoi(line+1);
        if(print_raw) 
          do_output(LOG_CAT_INFO, LOG_AREA_INTEGRALS, "Basis set for atom of Z=%d", atomType);
        state = SHELL_EXPECTED;
        if(atomType <= 0)
          {
            do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in read_basisset_file: (atomType <= 0) "
                      " in line %d %s", lineNo, fileName);
            return -1;
          }
        if(atomType >= MAX_NO_OF_ATOM_TYPES)
          {
            do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in read_basisset_file: "
                      "(atomType >= MAX_NO_OF_ATOM_TYPES) in line %d %s",
                      lineNo, fileName);
            return -1;
          }
        currAtom = &result.atoms[atomType];
        spdf = 0;
        shellBaseIndex = 0;
      }
      lineConsumed = 1;
      break;

    case SHELL_OR_ATOM_EXPECTED:
      if(line[0] == 'a' || line[0] == 'A') {
        state = ATOM_EXPECTED;
        /* fininalize current atom data */
        if(currAtom == NULL || shellBaseIndex < 0) {
	  do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in read_basisset_file: (currAtom == NULL || shellBaseIndex < 0) in line %d %s", lineNo, fileName);
          return -1;
	}
        currAtom->noOfShells = shellBaseIndex;
        break;
      } /* else fall through */

    case SHELL_EXPECTED:
      if(shellBaseIndex < 0) {
	do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in read_basisset_file: (shellBaseIndex < 0) in line %d %s", lineNo, fileName);
        return -1;
      }
      int noOfExponents, noOfShellsCurrBatch;
      if(sscanf(line, "%d %d %d",
                &noOfExponents, &noOfShellsCurrBatch, &dummy ) != 3)
        {
          do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in read_basisset_file: "
                    "Shell data expected in line %d:\n%s", lineNo, line);
          return -1;
        }
      if(noOfExponents <= 0)
        {
          do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in read_basisset_file: "
                    "(noOfExponents <= 0) in line %d %s", lineNo, fileName);
          return -1;
        }
      if(noOfExponents >= MAX_NO_OF_CONTR)
        {
          do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in read_basisset_file: "
                    "(noOfExponents >= MAX_NO_OF_CONTR) in line %d",
                    lineNo);
          return -1;
        }
      if(noOfShellsCurrBatch < 0)
        {
          do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in read_basisset_file: "
                    "(noOfShellsCurrBatch < 0) in line %d", lineNo);
          return -1;
        }
      if(noOfShellsCurrBatch == 0) {
        /*  special case: uncontracted, expect only one column */
        noOfShellsCurrBatch = noOfExponents;
        uncontracted = 1;
      } else uncontracted = 0;

      if(shellBaseIndex + noOfShellsCurrBatch >= MAX_NO_OF_SHELLS_PER_ATOM)
        {
          do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in read_basisset_file: too many shells.");
          return -1;
        }
      /* initialize shell data. Set the contraction count to its upper
         limit. remove_zeros() will later check for a better, lower
         value. */
      for(int i = 0; i < noOfShellsCurrBatch; i++) {
	if(currAtom == NULL || spdf < 0) {
	  do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in read_basisset_file: (currAtom == NULL || spdf < 0) in line %d %s", lineNo, fileName);
	  return -1;
	}
	currAtom->shells[shellBaseIndex+i].type = spdf;
	currAtom->shells[shellBaseIndex+i].contrCount = noOfExponents;
      }
      expNo = 0;
      state = CONTRACTION_BLOCK_EXPECTED;
      if(print_raw)
        do_output(LOG_CAT_INFO, LOG_AREA_INTEGRALS, 
                  "Block for L=%d primitives: %d contracted: %d",
                  spdf, noOfExponents, noOfShellsCurrBatch);

      lineConsumed = 1;
      break;

    case CONTRACTION_BLOCK_EXPECTED:
      currExponent = atof(line);
      if(currExponent <= 0) {
	do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error: (currExponent <= 0) in line %d", lineNo);
	return -1;
      }
      if(currAtom == NULL || shellBaseIndex < 0 || expNo < 0) {
	do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in read_basisset_file: (currAtom == NULL || shellBaseIndex < 0 || expNo < 0) in line %d %s", lineNo, fileName);
        return -1;
      }
      if(uncontracted) {
        for(int i = 0; i < noOfShellsCurrBatch; i++) {
          currAtom->shells[shellBaseIndex+i].exponentList[expNo] =
            currExponent;
          currAtom->shells[shellBaseIndex+i].coeffList[expNo] =
            i == expNo ? 1.0 : 0.0;
        }
      } else {
        int idx = 0;
        /* skip exponent */
        while(line[idx] && isspace(line[idx]))  idx++;
        for(int i = 0; i < noOfShellsCurrBatch; i++) {
          currAtom->shells[shellBaseIndex+i].exponentList[expNo] =
            currExponent;
          while(line[idx] && !isspace(line[idx])) idx++;
          while(line[idx] && isspace(line[idx]))  idx++;
          if( !line[idx] ) {
	    /* Second line begins when we are about to read 7th
	       contraction coefficient (i=6), third line for 14th
	       (i=13), fourth for i=20 etc. If this pattern does not
	       match, warn the user. */
	    if( i != 6 && (i+1) % 7 != 0 )
	      do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "WARN: line %d has trailing data: '%s'"
			"non-conformant basis set file for i=%d.",
			lineNo, line + idx, i);
	    if(fgets(line, sizeof(line), f) == NULL) {
	      do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "reading error when continuing shell data.");
	      return -1;
	    }
	    lineNo++;
	    idx = 0;
	    while(line[idx] && isspace(line[idx]))  idx++;
	  }
          currAtom->shells[shellBaseIndex+i].coeffList[expNo] =
            atof(line + idx);
        }  /*  END FOR i */
      }
      if(print_raw) {
        char line[256], eee[20];
        line[0] = '\0';
        for(int i = 0; i<noOfShellsCurrBatch; i++) {
          sprintf(eee, "%10.5f",
                  (double)currAtom->shells[shellBaseIndex+i].coeffList[expNo]);
          strcat(line, eee);
        }
        do_output(LOG_CAT_INFO, LOG_AREA_INTEGRALS, 
                  "%d %12.6f: %s", expNo, (double)currExponent, line);
      }
      if(++expNo == noOfExponents) {
        remove_zeros(currAtom, shellBaseIndex, noOfShellsCurrBatch);
        shellBaseIndex += noOfShellsCurrBatch;
        spdf++;
        state = SHELL_OR_ATOM_EXPECTED;
      }
      lineConsumed = 1;
      break;
    case END_PARSING:
      /*  do nothing */
      break;
    }
  } while(state != END_PARSING);
  fclose(f);

  /* fininalize current atom data */
  if(currAtom == NULL || shellBaseIndex < 0) {
    do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in read_basisset_file: (currAtom == NULL || shellBaseIndex < 0) in line %d %s", lineNo, fileName);
    return -1;
  }
  currAtom->noOfShells = shellBaseIndex;

  /*  Postprocessing... */
  /*  set shell ID for each shell in basis set */
  int currShellID = 0;
  for(int i = 0; i < MAX_NO_OF_ATOM_TYPES; i++) {
    int noOfShells = result.atoms[i].noOfShells;
    for(int j = 0; j < noOfShells; j++) {
      basisset_shell_struct* currShell = &result.atoms[i].shells[j];
      currShellID++;
      currShell->shell_ID = currShellID;
    } /*  END FOR j */
  } /*  END FOR i */
  do_output(LOG_CAT_INFO, LOG_AREA_INTEGRALS, "total number of shells in basis set: %i", currShellID);
  do_output(LOG_CAT_INFO, LOG_AREA_INTEGRALS, "Basis set file '%s' processed OK, noOfAtomTypes = %i",
            fileName, noOfAtomTypes);

  return 0;
}