File: integrals_1el_potential_prep.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 (262 lines) | stat: -rw-r--r-- 11,207 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
/* 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 integrals_1el_potential_prep.cc

    @brief Code for 1-electron integrals, preparatory work for
    computation of electron-nuclear potential energy matrix V.

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

#include "integrals_1el_potential_prep.h"
#include "multipole.h"
#include <stdexcept>

SetOfDistrsForV::SetOfDistrsForV() { }

SetOfDistrsForV::SetOfDistrsForV(const SetOfDistrsForV & other) {
  distrList = other.distrList;
  multipoleList = other.multipoleList;
  groupList = other.groupList;
  maxMomentVectorNormList = other.maxMomentVectorNormList;
  info = other.info;
}

static void copy_data_and_advance_dest_ptr(char** destPtr, const char* srcPtr, size_t nBytes) {
  char* p = *destPtr;
  memcpy(p, srcPtr, nBytes);
  p += nBytes;
  *destPtr = p;
}

/** Function needed for Chunks and Tasks usage. */
void SetOfDistrsForV::write_to_buffer ( char * dataBuffer, size_t const bufferSize ) const {
  char* p = dataBuffer;
  if(bufferSize < get_size())
    throw std::runtime_error("Error: bufferSize too small in SetOfDistrsForV::write_to_buffer.");
  int nDistrs = distrList.size();
  int nGroups = groupList.size();
  // nDistrs
  copy_data_and_advance_dest_ptr(&p, (const char*)&nDistrs, sizeof(int));
  // nGroups
  copy_data_and_advance_dest_ptr(&p, (const char*)&nGroups, sizeof(int));
  // info
  copy_data_and_advance_dest_ptr(&p, (const char*)&info, sizeof(SetOfDistrsForVInfo));
  // distrList
  copy_data_and_advance_dest_ptr(&p, (const char*)&distrList[0], nDistrs*sizeof(DistributionSpecStructWithIndexes2));
  // multipoleList
  copy_data_and_advance_dest_ptr(&p, (const char*)&multipoleList[0], nDistrs*sizeof(multipole_struct_small));
  // groupList
  copy_data_and_advance_dest_ptr(&p, (const char*)&groupList[0], nGroups*sizeof(group_struct));
  // maxMomentVectorNormList
  copy_data_and_advance_dest_ptr(&p, (const char*) &maxMomentVectorNormList[0], nGroups*sizeof(maxMomentVectorNormStruct));
  // DONE!
}

/*
  std::vector<DistributionSpecStructWithIndexes2> distrList;
  std::vector<multipole_struct_small> multipoleList; // same size as distrList
  std::vector<group_struct> groupList;
  std::vector<maxMomentVectorNormStruct> maxMomentVectorNormList; // size same as groupList
  SetOfDistrsForVInfo info;
*/

/** Function needed for Chunks and Tasks usage. */
size_t SetOfDistrsForV::get_size() const {
  int nDistrs = distrList.size();
  int nGroups = groupList.size();
  return
    2 * sizeof(int) +
    sizeof(SetOfDistrsForVInfo) +
    nDistrs*sizeof(DistributionSpecStructWithIndexes2) +
    nDistrs*sizeof(multipole_struct_small) +
    nGroups*sizeof(group_struct) +
    nGroups*sizeof(maxMomentVectorNormStruct);
}

static void copy_data_and_advance_src_ptr(char* destPtr, const char** srcPtr, size_t nBytes) {
  const char* p = *srcPtr;
  memcpy(destPtr, p, nBytes);
  p += nBytes;
  *srcPtr = p;
}

/** Function needed for Chunks and Tasks usage. */
void SetOfDistrsForV::assign_from_buffer ( char const * dataBuffer, size_t const bufferSize) {
  if(bufferSize < 2*sizeof(int))
    throw std::runtime_error("Error: bufferSize too small in SetOfDistrsForV::assign_from_buffer.");
  const char* p = dataBuffer;
  // nDistrs
  int nDistrs;
  copy_data_and_advance_src_ptr((char*)&nDistrs, &p, sizeof(int));
  // nGroups
  int nGroups;
  copy_data_and_advance_src_ptr((char*)&nGroups, &p, sizeof(int));
  distrList.resize(nDistrs);
  multipoleList.resize(nDistrs);
  groupList.resize(nGroups);
  maxMomentVectorNormList.resize(nGroups);
  assert(bufferSize >= get_size());
  // info
  copy_data_and_advance_src_ptr((char*)&info, &p, sizeof(SetOfDistrsForVInfo));
  // distrList
  copy_data_and_advance_src_ptr((char*)&distrList[0], &p, nDistrs*sizeof(DistributionSpecStructWithIndexes2));
  // multipoleList
  copy_data_and_advance_src_ptr((char*)&multipoleList[0], &p, nDistrs*sizeof(multipole_struct_small));
  // groupList
  copy_data_and_advance_src_ptr((char*)&groupList[0], &p, nGroups*sizeof(group_struct));
  // maxMomentVectorNormList
  copy_data_and_advance_src_ptr((char*)&maxMomentVectorNormList[0], &p, nGroups*sizeof(maxMomentVectorNormStruct));
  // DONE!
}

void
organize_distrs_for_V(const IntegralInfo & integralInfo,
		      SetOfDistrsForV & setOfDistrsForV,
		      const std::vector<DistributionSpecStructWithIndexes2> & inputList,
		      ergo_real threshold,
		      ergo_real maxCharge) {
  int nDistrs = inputList.size();
  setOfDistrsForV.distrList.resize(nDistrs);
  for(int i = 0; i < nDistrs; i++)
    setOfDistrsForV.distrList[i] = inputList[i];

  // Sort list of distrs by x, y, z, exponent.
  // The point of this is to group together distrs that have same center and same exponent.
  sort_distr_list(&setOfDistrsForV.distrList[0], nDistrs);

  // identify groups of distrs that have same center and same exponent.
  // Allocate according to worst case, each distr being a separate group.
  setOfDistrsForV.groupList.resize(nDistrs);
  int ind = 0;
  int currGroupInd = 0;
  int groupCount = 0;
  int maxNDistrsPerGroup = 0;
  while(ind < nDistrs) {
    ind++;
    if(ind < nDistrs) {
      if(compare_distrs<DistributionSpecStructWithIndexes2>(&setOfDistrsForV.distrList[ind], &setOfDistrsForV.distrList[currGroupInd]) == 0)
	continue;
    }
    // define new group
    setOfDistrsForV.groupList[groupCount].startIndex = currGroupInd;
    setOfDistrsForV.groupList[groupCount].count = ind - currGroupInd;
    if (setOfDistrsForV.groupList[groupCount].count > maxNDistrsPerGroup)
      maxNDistrsPerGroup = setOfDistrsForV.groupList[groupCount].count;
    groupCount++;
    // start next group
    currGroupInd = ind;
  }
  setOfDistrsForV.groupList.resize(groupCount);
  setOfDistrsForV.maxMomentVectorNormList.resize(groupCount);

  // Create multipoles for all distrs.
  setOfDistrsForV.multipoleList.resize(nDistrs);
  memset(&setOfDistrsForV.multipoleList[0], 0, nDistrs*sizeof(multipole_struct_small));
  for(int i = 0; i < nDistrs; i++)
    compute_multipole_moments(integralInfo, &setOfDistrsForV.distrList[i].distr, &setOfDistrsForV.multipoleList[i]);

  // Determine min and max coords for all distrs, to set boundingCubeCenterCoords and boundingCubeWidth.
  ergo_real minCoords[3];
  ergo_real maxCoords[3];
  for(int i = 0; i < nDistrs; i++) {
    for(int coordIdx = 0; coordIdx < 3; coordIdx++) {
      ergo_real coord = setOfDistrsForV.distrList[i].distr.centerCoords[coordIdx];
      if(i == 0 || coord < minCoords[coordIdx])
	minCoords[coordIdx] = coord;
      if(i == 0 || coord > maxCoords[coordIdx])
	maxCoords[coordIdx] = coord;
    }
  }
  ergo_real maxWidth = 0;
  for(int coordIdx = 0; coordIdx < 3; coordIdx++) {
    ergo_real centerCoord = (minCoords[coordIdx] + maxCoords[coordIdx]) / 2;
    setOfDistrsForV.info.boundingCubeCenterCoords[coordIdx] = centerCoord;
    ergo_real width = maxCoords[coordIdx] - minCoords[coordIdx];
    if(width > maxWidth)
      maxWidth = width;
  }
  setOfDistrsForV.info.boundingCubeWidth = maxWidth;

  setOfDistrsForV.info.maxExtentForAll = 0;
  // Get maxMomentVectorNorm info for each group
  for(int l = 0; l <= MAX_MULTIPOLE_DEGREE_BASIC; l++)
    setOfDistrsForV.info.maxMomentVectorNormForAll.maxMomentVectorNormList[l] = 0;
  for(int groupIndex = 0; groupIndex < groupCount; groupIndex++) {
    int groupStartIdx = setOfDistrsForV.groupList[groupIndex].startIndex;
    multipole_struct_small* currMultipoleList = &setOfDistrsForV.multipoleList[groupStartIdx];
    int nDistrsCurrGroup = setOfDistrsForV.groupList[groupIndex].count;
    int maxNoOfMoments = 0;
    int maxDegree = 0;
    ergo_real maxExtentForGroup = 0;
    for(int l = 0; l <= MAX_MULTIPOLE_DEGREE_BASIC; l++)
      setOfDistrsForV.maxMomentVectorNormList[groupIndex].maxMomentVectorNormList[l] = 0;
    for(int i = 0; i < nDistrsCurrGroup; i++) {
      if(currMultipoleList[i].noOfMoments > maxNoOfMoments)
	maxNoOfMoments = currMultipoleList[i].noOfMoments;
      if(currMultipoleList[i].degree > maxDegree)
	maxDegree = currMultipoleList[i].degree;
      const multipole_struct_small* distrMultipole = &currMultipoleList[i];
      for(int l = 0; l <= distrMultipole->degree; l++) {
	int startIndex = l*l;
	int endIndex = (l+1)*(l+1);
	ergo_real sum = 0;
	for(int A = startIndex; A < endIndex; A++)
	  sum += distrMultipole->momentList[A]*distrMultipole->momentList[A];
	ergo_real subNorm = template_blas_sqrt(sum);
	if(subNorm > setOfDistrsForV.maxMomentVectorNormList[groupIndex].maxMomentVectorNormList[l])
	  setOfDistrsForV.maxMomentVectorNormList[groupIndex].maxMomentVectorNormList[l] = subNorm;
      }
      // Get extent
      // Here we use an extent such that beyond the extent the abs
      // value of any distr is smaller than threshold/maxCharge.
      ergo_real abscoeff = template_blas_fabs(setOfDistrsForV.distrList[groupStartIdx+i].distr.coeff);
      ergo_real exponent = setOfDistrsForV.distrList[groupStartIdx+i].distr.exponent;
      ergo_real R2 = -1 * (1/exponent) * template_blas_log(threshold/(abscoeff*maxCharge));
      ergo_real extent = 0;
      if(R2 > 0) // R2 can become negative, e.g. if abscoeff is very small, in such cases we let extent be zero.
	extent = template_blas_sqrt(R2);
      if(extent > maxExtentForGroup)
	maxExtentForGroup = extent;
    } // end for i
    setOfDistrsForV.groupList[groupIndex].maxExtent = maxExtentForGroup;
    setOfDistrsForV.groupList[groupIndex].maxNoOfMoments = maxNoOfMoments;
    setOfDistrsForV.groupList[groupIndex].maxDegree = maxDegree;
    if(maxExtentForGroup > setOfDistrsForV.info.maxExtentForAll)
      setOfDistrsForV.info.maxExtentForAll = maxExtentForGroup;
    // Update maxMomentVectorNormForAll
    for(int l = 0; l <= MAX_MULTIPOLE_DEGREE_BASIC; l++) {
      ergo_real currValue = setOfDistrsForV.maxMomentVectorNormList[groupIndex].maxMomentVectorNormList[l];
      if(currValue > setOfDistrsForV.info.maxMomentVectorNormForAll.maxMomentVectorNormList[l])
	setOfDistrsForV.info.maxMomentVectorNormForAll.maxMomentVectorNormList[l] = currValue;
    }
  } // end for groupIndex
}