File: mm_limit_table.cc

package info (click to toggle)
ergo 3.8.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: sid, 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 (265 lines) | stat: -rw-r--r-- 8,977 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
/* 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 mm_limit_table.cc

    @brief MMLimitTable class used to predict the magnitude of
    contributions when using truncated multipole expansions.

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

#include <stdio.h>
#include <vector>
#include <limits>
#include <stdexcept>

#include "mm_limit_table.h"

#include "matrix_norm.h"
#include "template_blas_basicmath.h"
#include "multipole.h"


const ergo_real HUGE_REAL_NUMBER = template_blas_sqrt(template_blas_get_num_limit_max<ergo_real>()) / 100;
const ergo_real INITIAL_STEP = 0.5;
const ergo_real RANGE_STEP_DIFF_FACTOR = 0.25;
const ergo_real MAX_ALLOWED_DISTANCE = 1e5;


MMLimitTable::MMLimitTable()
{
  noOfRangesUsed = 0;
}

MMLimitTable::~MMLimitTable()
{
}

void MMLimitTable::inittt(const MultipolePrepManager & multipolePrep)
{
  ergo_real maxDistance_input = MAX_ALLOWED_DISTANCE;
  ergo_real maxDistance = maxDistance_input + 0.1; // ELIAS NOTE 2013-11-29: add small value here to avoid problems at exactly the maxDistance_input distance.
  ergo_real r = 0;
  ergo_real currStep = INITIAL_STEP;
  int rangeCount = 0;

  const int NO_OF_SAMPLE_POINTS = 7;
  ergo_real dxlist[NO_OF_SAMPLE_POINTS][3];
  dxlist[0][0] = 1;
  dxlist[0][1] = 0;
  dxlist[0][2] = 0;
  dxlist[1][0] = 0;
  dxlist[1][1] = 1;
  dxlist[1][2] = 0;
  dxlist[2][0] = 0;
  dxlist[2][1] = 0;
  dxlist[2][2] = 1;
  dxlist[3][0] = 1;
  dxlist[3][1] = 1;
  dxlist[3][2] = 0;
  dxlist[4][0] = 1;
  dxlist[4][1] = 0;
  dxlist[4][2] = 1;
  dxlist[5][0] = 0;
  dxlist[5][1] = 1;
  dxlist[5][2] = 1;
  dxlist[6][0] = 1;
  dxlist[6][1] = 1;
  dxlist[6][2] = 1;
  MMInteractor interactor(multipolePrep);

  while(r < maxDistance)
    {
      interaction_matrix_limit_range_struct & range = rangeList[rangeCount];
      range.startDistance = r;
      range.step = currStep;
      for(int i = 0; i < NO_OF_STEPS_PER_RANGE; i++)
	{
	  r = range.startDistance + i*range.step;
	  range.maxDistance = r + range.step;

	  for(int l_large = 0; l_large <= MAX_MULTIPOLE_DEGREE; l_large++)
	    for(int l_small = 0; l_small <= MAX_MULTIPOLE_DEGREE_BASIC; l_small++)
	      range.list[i].x[l_large][l_small] = 0;
      
	  for(int randloop = 0; randloop < NO_OF_SAMPLE_POINTS; randloop++)
	    {
	      ergo_real dx = dxlist[randloop][0];
	      ergo_real dy = dxlist[randloop][1];
	      ergo_real dz = dxlist[randloop][2];
	      ergo_real norm = template_blas_sqrt(dx*dx+dy*dy+dz*dz);
	      dx *= r / norm;
	      dy *= r / norm;
	      dz *= r / norm;
	      
	      ergo_real T[MAX_NO_OF_MOMENTS_PER_MULTIPOLE_BASIC*MAX_NO_OF_MOMENTS_PER_MULTIPOLE];
	      if(r > 0) {
		interactor.getInteractionMatrix(dx, dy, dz,
						MAX_MULTIPOLE_DEGREE_BASIC,
						MAX_MULTIPOLE_DEGREE, T);
	      }
	      else
		{
		  // For r=0 use huge values for all elements in T.
		  for(int k = 0; k < MAX_NO_OF_MOMENTS_PER_MULTIPOLE_BASIC*MAX_NO_OF_MOMENTS_PER_MULTIPOLE; k++)
		    T[k] = HUGE_REAL_NUMBER;
		}
	  
	      // Compute norms for submatrices of T
	      for(int l_large = 0; l_large <= MAX_MULTIPOLE_DEGREE; l_large++)
		{
		  int startIndex_large = l_large*l_large;
		  int endIndex_large = (l_large+1)*(l_large+1);
		  int n_large = endIndex_large - startIndex_large;
		  for(int l_small = 0; l_small <= MAX_MULTIPOLE_DEGREE_BASIC; l_small++)
		    {
		      int startIndex_small = l_small*l_small;
		      int endIndex_small = (l_small+1)*(l_small+1);
		      int n_small = endIndex_small - startIndex_small;

		      ergo_real T_sub[n_small*n_large];
		      for(int ii = 0; ii < n_large; ii++)
			for(int jj = 0; jj < n_small; jj++)
			  T_sub[ii*n_small+jj] = T[(startIndex_small+jj)*MAX_NO_OF_MOMENTS_PER_MULTIPOLE+startIndex_large+ii];

		      ergo_real matrixNorm = get_euclidean_norm(n_large, n_small, T_sub);

		      if(matrixNorm > range.list[i].x[l_large][l_small])
			range.list[i].x[l_large][l_small] = matrixNorm;
		  
		    } // END FOR l_small
		} // END FOR l_large
	    } // END FOR randloop
	  
	} // END FOR i
      currStep = r * RANGE_STEP_DIFF_FACTOR;
      rangeCount++;
      if(rangeCount >= NO_OF_RANGES)
	throw "error in MMLimitTable::Init: (rangeCount >= NO_OF_RANGES)";
    } // END WHILE
  noOfRangesUsed = rangeCount;
}


const MMLimitTable::interaction_matrix_limit_struct & MMLimitTable::get_x_from_distance(ergo_real distance) const
{
  if(distance < 0)
    throw "Error in MMLimitTable::get_x_from_distance(): (distance < 0).";
  int rangeIndex = 0;
  while(rangeList[rangeIndex].maxDistance <= distance) // ELIAS NOTE 2013-11-29: changed from "<" to "<=" here to fix problem found by Anastasia, for case when (distanceLeft / range.step) divides evenly.
    {
      rangeIndex++;
      if(rangeIndex >= noOfRangesUsed)
	throw "error in MMLimitTable::get_x_from_distance: (rangeIndex >= noOfRangesUsed)";
    }
  const interaction_matrix_limit_range_struct & range = rangeList[rangeIndex];
  ergo_real distanceLeft = distance - range.startDistance;
  int i = (int)(distanceLeft / range.step);
  if(i < 0 || i >= NO_OF_STEPS_PER_RANGE)
    {
      if(i < 0)
	throw "error in MMLimitTable::get_x_from_distance: i < 0";
      throw "error in MMLimitTable::get_x_from_distance: i >= NO_OF_STEPS_PER_RANGE";
    }
  const interaction_matrix_limit_struct & x = range.list[i];
  return x;
}


ergo_real MMLimitTable::get_max_abs_mm_contrib(int degree1,
					       const ergo_real* maxMomentVectorNormList1,
					       int degree2,
					       const ergo_real* maxMomentVectorNormList2,
					       ergo_real distance) const
{
  ergo_real maxAbsContributionFromMultipole = 0;
  // Get worst-case interaction matrix limits
  const interaction_matrix_limit_struct & x = get_x_from_distance(distance);
  for(int l_large = degree1; l_large >= 0; l_large--)
    {
      ergo_real contribThisDegree = 0;
      for(int l_small = 0; l_small <= degree2; l_small++)
	{
	  contribThisDegree += 
	    maxMomentVectorNormList1[l_small] *
	    maxMomentVectorNormList2[l_large] *
	    x.x[l_large][l_small];
	} // END FOR l_small
      maxAbsContributionFromMultipole += contribThisDegree;
    } // END FOR l_large
  return maxAbsContributionFromMultipole;
}

int MMLimitTable::get_minimum_multipole_degree_needed(ergo_real distance,
						      const multipole_struct_large* boxMultipole, 
						      int maxDegreeForDistrs, 
						      const ergo_real* maxMomentVectorNormForDistrsList, 
						      ergo_real threshold) const
{
  // Get worst-case interaction matrix limits
  const interaction_matrix_limit_struct & x = get_x_from_distance(distance);
  ergo_real maxAbsContribution = 0;
  int degreeNeeded = boxMultipole->degree;
  for(int l_large = boxMultipole->degree; l_large >= 0; l_large--)
    {
      degreeNeeded = l_large;
      ergo_real contribThisDegree = 0;
      for(int l_small = 0; l_small <= maxDegreeForDistrs; l_small++)
	{
	  contribThisDegree += 
	    maxMomentVectorNormForDistrsList[l_small] * 
	    boxMultipole->euclideanNormList[l_large] * 
	    x.x[l_large][l_small];
	} // END FOR l_small
      maxAbsContribution += contribThisDegree;
      if(maxAbsContribution > threshold)
	break;
    } // END FOR l_large
  return degreeNeeded;
}

void MMLimitTable::write_to_buffer ( char * dataBuffer, size_t const bufferSize ) const {
  char* p = dataBuffer;
  if(bufferSize < get_size())
    throw std::runtime_error("Error in MMLimitTable::write_to_buffer: bufferSize too small.");
  memcpy(p, this, sizeof(MMLimitTable));
}

size_t MMLimitTable::get_size() const {
  return sizeof(MMLimitTable);
}

void MMLimitTable::assign_from_buffer ( char const * dataBuffer, size_t const bufferSize) {
  const char* p = dataBuffer;
  if(bufferSize < sizeof(MMLimitTable))
    throw std::runtime_error("Error in MMLimitTable::assign_from_buffer: bufferSize too small.");
  memcpy(this, p, sizeof(MMLimitTable));
}