File: molecule.cc

package info (click to toggle)
ergo 3.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • 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 (430 lines) | stat: -rw-r--r-- 12,243 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* 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 molecule.cc

    @brief Class representing a molecule as a set of atoms with
    assiciated coordinates and charges of the atomic nuclei.

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

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <math.h>
#include <string.h>
#include <cassert>
#include "molecule.h"
#include "xyz_file_parser.h"
#include "output.h"
#include "memorymanag.h"
#include "units.h"
#include "utilities.h"


static ergo_real
get_distance_between_atoms(const Atom& atomA, const Atom& atomB)
{
  int i;
  ergo_real sum = 0;
  for(i = 0; i < 3; i++)
    {
      ergo_real dx = atomB.coords[i] - atomA.coords[i];
      sum += dx*dx;
    }
  return template_blas_sqrt(sum);
}

void
Molecule::getExtremeInternuclearDistancesQuadratic(ergo_real & minDist, ergo_real & maxDist) const
{
  minDist = maxDist = 0; // This matters if there is only one atom.
  bool firstTime = true;
  for(int A = 0; A < noOfAtoms; A++)
    for(int B = A+1; B < noOfAtoms; B++) {
      const Atom& atomA = atoms[A];
      const Atom& atomB = atoms[B];
      ergo_real RAB = get_distance_between_atoms(atomA, atomB);
      if(firstTime) {
	minDist = maxDist = RAB;
	firstTime = false;
      }
      else {
	minDist = RAB < minDist ? RAB : minDist;
	maxDist = RAB > maxDist ? RAB : maxDist;
      }
    }
}

ergo_real
Molecule::getNuclearRepulsionEnergyQuadratic() const
{
  int A, B;
  ergo_real sum;
  int N = noOfAtoms;
  do_output(LOG_CAT_INFO, LOG_AREA_MAIN, 
	    "get_nuclear_repulsion_energy, N = %i", N);
  sum = 0;
  for(A = 0; A < N; A++)
    for(B = A+1; B < N; B++)
      {
	const Atom& atomA = atoms[A];
	const Atom& atomB = atoms[B];
	ergo_real ZA = atomA.charge;
	ergo_real ZB = atomB.charge;
	ergo_real RAB = get_distance_between_atoms(atomA, atomB);
	sum += ZA * ZB / RAB;
      }
  do_output(LOG_CAT_INFO, LOG_AREA_MAIN, 
	    "get_nuclear_repulsion_energy returning energy %33.22f", (double)sum);
  return sum;
}

void
Molecule::getNuclearRepulsionEnergyGradientContribQuadratic(ergo_real* resultGradient) const 
{
  int N = noOfAtoms;
  int A, B;
  for(A = 0; A < N; A++)
    for(B = A+1; B < N; B++) {
      const Atom& atomA = atoms[A];
      const Atom& atomB = atoms[B];
      ergo_real ZA = atomA.charge;
      ergo_real ZB = atomB.charge;
      ergo_real dx = atomB.coords[0] - atomA.coords[0];
      ergo_real dy = atomB.coords[1] - atomA.coords[1];
      ergo_real dz = atomB.coords[2] - atomA.coords[2];
      ergo_real r = template_blas_sqrt(dx*dx + dy*dy + dz*dz);
      ergo_real derivative_x = ZA * ZB * (-0.5) * 2.0 * dx / (r*r*r);
      ergo_real derivative_y = ZA * ZB * (-0.5) * 2.0 * dy / (r*r*r);
      ergo_real derivative_z = ZA * ZB * (-0.5) * 2.0 * dz / (r*r*r);
      resultGradient[A*3+0] += -1 * derivative_x;
      resultGradient[A*3+1] += -1 * derivative_y;
      resultGradient[A*3+2] += -1 * derivative_z;
      resultGradient[B*3+0] +=  1 * derivative_x;
      resultGradient[B*3+1] +=  1 * derivative_y;
      resultGradient[B*3+2] +=  1 * derivative_z;
    }    
}

ergo_real
Molecule::getNuclearElectricFieldEnergy(const Vector3D& electricField) const
{
  int N = noOfAtoms;
  do_output(LOG_CAT_INFO, LOG_AREA_MAIN, 
	    "get_nuclear_electric_field_energy, N = %i", N);
  ergo_real sum = 0;
  int A;
  for(A = 0; A < N; A++)
    {
      const Atom* atom = &atoms[A];
      sum += atom->charge * atom->coords[0] * electricField.v[0];
      sum += atom->charge * atom->coords[1] * electricField.v[1];
      sum += atom->charge * atom->coords[2] * electricField.v[2];
    } // END FOR A
  do_output(LOG_CAT_INFO, LOG_AREA_MAIN, 
	    "get_nuclear_electric_field_energy returning energy %33.22f", (double)sum);
  return sum;
}

int 
Molecule::getNumberOfElectrons() const
{
  int i;
  ergo_real noOfElectrons;
  ergo_real sum = 0;
  for(i = 0; i < noOfAtoms; i++)
    sum += atoms[i].charge;
  noOfElectrons = sum - netCharge;
  return (int)noOfElectrons;
}


static int 
readMoleculeFileInMolFormat(Molecule* result, const char* fileName, 
                            int netCharge, char **basisfilename)
{
  int nAtoms, nAtomsCurrType;
  int i, j, ii, lineLen, noOfAtomTypes, atomTypeNo, can_read_basset;
  char* p;
  ergo_real unitFactor, atomCharge;
  long int  fileSize = get_file_size(fileName);
  if(fileSize < 0) {
    do_output(LOG_CAT_ERROR, LOG_AREA_MAIN, "error getting file size for file '%s'", fileName);
    return -1;    
  }
  FILE* f = fopen(fileName, "rt");
  if(f == NULL)
    {
      do_output(LOG_CAT_ERROR, LOG_AREA_MAIN, "error opening file '%s'", fileName);
      return -1;
    }
  size_t bufSize = fileSize + 10000;
  std::vector<char> buf(bufSize);
  memset(&buf[0], 0, bufSize);
  
  int nBytes = (int)fread(&buf[0], sizeof(char), bufSize, f);
  fclose(f);
  if(nBytes <= 0) {
    do_output(LOG_CAT_ERROR, LOG_AREA_MAIN, "error reading file '%s'", fileName);
    return -1;
  }

  do_output(LOG_CAT_INFO, LOG_AREA_MAIN, "File '%s' read OK, nBytes = %i", fileName, nBytes);

  nAtoms = 0;

  p = &buf[0];

  // skip 3 or 4 lines
  if(strncmp(p, "BASIS", 5) == 0) { i = 3; can_read_basset = 1; }
  else if(strncmp(p, "ATOMDF", 6) == 0 ||
          strncmp(p, "ATOMBASIS", 9) == 0) { i = 2; can_read_basset = 0; }
  else 
    {
      do_output(LOG_CAT_ERROR, LOG_AREA_MAIN, "Unsupported molecule input file type");
      return -1;
    }
  while((*p != '\n') && (*p != '\0')) p++; /* skip first line */
  p++;
  // if basisfilename is NULL we skip getting basis set string.
  if(basisfilename)
    {
      if(can_read_basset && !*basisfilename) {
	char *eol = strchr(p, '\n');
	if(eol) {
	  while (--eol>p && *eol == ' ')
	    ;
	  if (eol>p) {
	    size_t len = eol-p+1;
	    *basisfilename = (char*)ergo_malloc(len+1);
	    strncpy(*basisfilename, p, len);
	    (*basisfilename)[len] = '\0';
	  }
	}
      }
    }
  while(i--) {
    while((*p != '\n') && (*p != '\0')) p++;
    p++;
  }
  if(*p == '\0') return -2;
  
  // the molecule input can follow in a fixed or a free format.
  unitFactor = 1.0;
  if( sscanf(p, "%d", &noOfAtomTypes) == 1)
    { 
      // check if Angstrom flag is set
      lineLen = 0;
      for(ii = 0; ii <= 19; ii++)
        {
          if((p[ii] == '\n') || (p[ii] == '\0'))
            break;
          lineLen++;
        }
      if(lineLen > 19)
        {
          if(p[19] != ' ')
            unitFactor = UNIT_one_Angstrom;
        }
      // skip till end of line
      while(*p && *p != '\n') p++;
    }
  else
    { /* "free" format */
      do {
        if(strncasecmp(p, "Atomtypes=", 10) == 0)
          sscanf(p+10, "%d", &noOfAtomTypes);
        else if(strncmp(p, "Angstrom", 8) == 0)
          unitFactor = UNIT_one_Angstrom;
        while(*p && *p != '\n' && *p != ' ') p++;
        if(*p != ' ') break;
        p++;
      } while(1);
    }
  if(*p == '\n') p++; 
  do_output(LOG_CAT_INFO, LOG_AREA_MAIN,
	    "noOfAtomTypes = %i dist. unit=%f au", noOfAtomTypes, unitFactor);
  if(noOfAtomTypes <= 0)
    {
      do_output(LOG_CAT_ERROR, LOG_AREA_MAIN, "Error: (noOfAtomTypes <= 0)");
      return -3;
    }
  
  for(atomTypeNo = 0; atomTypeNo < noOfAtomTypes; atomTypeNo++)
    {
      // now p should point to beginning of new atom type
      float charge;
      if(*p == '\0')
	{
	  do_output(LOG_CAT_ERROR, LOG_AREA_MAIN, "error in readMoleculeFile for atom type %i", 
		    atomTypeNo+1);
	  return -4;
	}

      do_output(LOG_CAT_INFO, LOG_AREA_MAIN, "atomTypeNo = %i", atomTypeNo);

      // skip blanks
      while(*p == ' ') p++;
      if(*p == '\0') return -5;
      // if the first character on the line is a digit, we assume fixed format
      if(*p >= '0' && *p <= '9')
	{
	  charge = (float)atof(p);
	  while(*p != ' ' && *p != '\n' && *p != '\0') p++;
	  // skip blanks
	  while(*p == ' ') p++;
	  // now we expect another digit
	  if(*p < '0' || *p > '9')
	    return -6;
	  nAtomsCurrType = atoi(p);
	  while(*p != ' ' && *p != '\n' && *p != '\0') p++;
	  // skip blanks
	  while(*p == ' ') p++;
	  // now we expect end of line
	  if(*p != '\n')
	    return -7;
	  p++;
	}
      else
        { /* "free" format */
	  charge = -1;
	  nAtomsCurrType = -1;
          do {
            if(strncmp(p, "Charge=", 7) == 0)
              sscanf(p+7, "%f", &charge);
            else if(strncmp(p, "Atoms=", 6) == 0)
              sscanf(p+6, "%d", &nAtomsCurrType);
            while(*p && *p != '\n' && *p != ' ') p++;
            if(*p != ' ') break;
            p++;
          } while(1);
	  if(charge < 0) {
	    do_output(LOG_CAT_ERROR, LOG_AREA_MAIN, 
		      "Invalid charge = %5.2f", (double)charge);
            return -8;
	  }
	  if(nAtomsCurrType < 0) {
	    do_output(LOG_CAT_ERROR, LOG_AREA_MAIN, 
		      "Invalid number of atoms = %i", nAtomsCurrType);
		       return -8;
	  }
        }

      do_output(LOG_CAT_INFO, LOG_AREA_MAIN, 
		"charge = %5.2f, nAtomsCurrType = %i", (double)charge, nAtomsCurrType);
      
      atomCharge = charge;
      if(atomCharge <= 0)
        return -9;
          
      for(i = 0; i < nAtomsCurrType; i++)
	{
	  // skip atom label
	  while(*p == ' ') p++;
	  while((*p != ' ') && (*p != '\0')) p++;
	  if(*p == '\0') return -10;
	  while(*p == ' ') p++;
	  // read coordinates
	  ergo_real coords[3];
	  for(j = 0; j < 3; j++)
	    {
	      // skip blanks
	      while(*p == ' ') p++;
	      if(*p == '\0') return -11;
	      coords[j] = atof(p) * unitFactor;
	      while((*p != ' ') && (*p != '\n') && (*p != '\0')) p++;
	      if(*p == '\0') return -12;
	    }

	  // skip rest of line
	  while((*p != '\n') && (*p != '\0'))
	    p++;
	  p++;

	  result->addAtom(atomCharge, coords[0], coords[1], coords[2]);
	  nAtoms++;
	} // END FOR i
    } // END FOR atomTypeNo

  // done. now there should be nothing more in the file.
  while(*p != '\0')
    {
      if(*p != ' ' && *p != '\n')
	{
	  do_output(LOG_CAT_ERROR, LOG_AREA_MAIN, "error in readMoleculeFile: "
		    "non-blank character found after last atom.");
	  return -14;
	}
      p++;
    }

#if 0
  for(i = 0; i < nAtoms; i++)
    {
      printf("%5.2f %22.11f %22.11f %22.11f\n", 
	     result->atoms[i].charge,
	     result->atoms[i].coords[0],
	     result->atoms[i].coords[1],
	     result->atoms[i].coords[2]);
    }
#endif

  assert(result->getNoOfAtoms() == nAtoms);
  result->setNetCharge(netCharge);
  
  return 0;
}



int 
Molecule::setFromMoleculeFile(const char* fileName, 
                              int netCharge, char **basisfilename)
{
  // Check filename extension
  int len = strlen(fileName);
  if(len < 5)
    {
      // Too short to have a 3-letter extension after dot.
      // Assume mol format.
      return readMoleculeFileInMolFormat(this, fileName, netCharge,
                                         basisfilename);
    }
  const char* extensionPtr = &fileName[len-4];
  if(strcasecmp(extensionPtr, ".xyz") == 0)
    return readMoleculeFileInXyzFormat(*this, fileName, netCharge, false);

  // Not xyz, assume mol format.
  return readMoleculeFileInMolFormat(this, fileName, netCharge,
                                     basisfilename);
}