File: Perturbation.h

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 (338 lines) | stat: -rw-r--r-- 11,718 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
/* 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 Perturbation.h Perturbation theory class
 *
 * Copyright(c) Emanuel Rubensson 2008
 *
 * @author Emanuel Rubensson
 * @date June 2008
 *
 */
#ifndef MAT_PERTURBATION
#define MAT_PERTURBATION
namespace per {
  template<typename Treal, typename Tmatrix, typename Tvector>
    class Perturbation {
  public: 
    Perturbation(std::vector<Tmatrix *> const & F, 
		 /**< Vector with matrices (input). */
		 std::vector<Tmatrix *> & D, 
		 /**< Vector with matrices (output). */
		 mat::Interval<Treal> const & gap, /**< Band gap. */
		 mat::Interval<Treal> const & allEigs, 
		 /**< Interval containing all eigenvalues of 
		  *   X0 + delta*X1 + delta^2*X2 + ... 
		  *   for all delta in [0, deltaMax]
		  *   for initial X.
		  */
		 Treal const deltaMax, /**< Largest allowed delta. */
		 Treal const errorTol, /**< Error tolerance. */
		 mat::normType const norm, /**< Norm for truncation etc. */
		 Tvector & vect /**< Vector. */
		 );
    void perturb() {
      dryRun();
      run();
    }

    void checkIdempotencies(std::vector<Treal> & idemErrors);
    template<typename TmatNoSymm>
      void checkCommutators(std::vector<Treal> & commErrors, 
			    TmatNoSymm const & dummyMat);
    void checkMaxSubspaceError(Treal & subsError);
    
  protected:
    /* This is input from the beginning */
    std::vector<Tmatrix *> const & F;
    std::vector<Tmatrix *> & X;
    mat::Interval<Treal> gap;
    mat::Interval<Treal> const & allEigs;
    Treal deltaMax;
    Treal errorTol;
    mat::normType const norm;
    Tvector & vect;

    /* These variables are set in the dry run. */
    int nIter;
    std::vector<Treal> threshVal;
    std::vector<Treal> sigma;
    
    /** Dry run to obtain some needed numbers.
     *
     *  After call to this function we know:
     *   - number of iterations (nIter),
     *   - threshold values (threshVal), and 
     *   - polyunomials to choose (sigma = -1 | = 1)
     *
     *  If requested accuracy is too high or gap too small, an
     *  exception is thrown.
     */
    void dryRun();
    void run();
  private:
    
  };
  
  template<typename Treal, typename Tmatrix, typename Tvector>
    Perturbation<Treal, Tmatrix, Tvector>::
    Perturbation(std::vector<Tmatrix *> const & F_in, 
		 std::vector<Tmatrix *> & X_in, 
		 mat::Interval<Treal> const & gap_in,
		 mat::Interval<Treal> const & allEigs_in,
		 Treal const deltaMax_in,
		 Treal const errorTol_in,
		 mat::normType const norm_in,
		 Tvector & vect_in) 
    : F(F_in), X(X_in), gap(gap_in), allEigs(allEigs_in),
    deltaMax(deltaMax_in), errorTol(errorTol_in), norm(norm_in),
    vect(vect_in) {
      if (!X.empty())
	throw "Perturbation constructor: D vector is expected to be empty (size==0)";
      for (unsigned int iMat = 0; iMat < F.size(); ++iMat)
	X.push_back(new Tmatrix(*F[iMat]));
      
      Treal lmin = allEigs.low();
      Treal lmax = allEigs.upp();
      
      /***** Initial linear transformation of matrix sequence. */
      typename std::vector<Tmatrix *>::iterator matIt = X.begin();
      /* Scale to [0, 1] interval and negate */
      (*matIt)->add_identity(-lmax); 
      *(*matIt) *= ((Treal)1.0 / (lmin - lmax));
      matIt++;
      /* ...and derivatives: */
      for ( ; matIt != X.end(); matIt++ ) 
	*(*matIt) *= ((Treal)-1.0 / (lmin - lmax));
      /* Compute transformed gap */
      gap = (gap - lmax) / (lmin - lmax);
    }
  
  template<typename Treal, typename Tmatrix, typename Tvector>
    void Perturbation<Treal, Tmatrix, Tvector>::dryRun() {
    Treal errorTolPerIter;
    int nIterGuess = 0;
    nIter = 1;
    Treal lumo;
    Treal homo;
    Treal m;
    Treal g;
    while (nIterGuess < nIter) {
      nIterGuess++;
      errorTolPerIter = 0.5 * errorTol /nIterGuess;
      nIter = 0;
      mat::Interval<Treal> gapTmp(gap);
      sigma.resize(0);
      threshVal.resize(0);
      while (gapTmp.low() > 0.5 * errorTol || gapTmp.upp() < 0.5 * errorTol) {
	lumo = gapTmp.low();
	homo = gapTmp.upp();
	m = gapTmp.midPoint();
	g = gapTmp.length();
	if (m > 0.5) {
	  lumo = lumo*lumo;
	  homo = homo*homo;
	  sigma.push_back(-1);
	}
	else {
	  lumo = 2*lumo - lumo*lumo;
	  homo = 2*homo - homo*homo;
	  sigma.push_back(1);
	}
	/* Compute threshold value necessary to converge. */
	Treal forceConvThresh = template_blas_fabs(1-2*m) * g / 10;
	/* We divide by 10 > 2 so that this loop converges at some point. */
	/* Compute threshold value necessary to maintain accuracy in subspace.*/
	Treal subspaceThresh = errorTolPerIter * (homo-lumo) / (1+errorTolPerIter);
	/* Choose the most restrictive threshold of the two. */
	threshVal.push_back(forceConvThresh < subspaceThresh ?
			    forceConvThresh : subspaceThresh);
	homo -= threshVal.back();
	lumo += threshVal.back();
	gapTmp = mat::Interval<Treal>(lumo, homo);
	if (gapTmp.empty())
	  throw "Perturbation<Treal, Tmatrix, Tvector>::dryRun() : Perturbation iterations will fail to converge; Gap is too small or desired accuracy too high.";
	nIter++;
      } /* end 2nd while */
    } /* end 1st while */
    /* Now, we have nIter, threshVal, and sigma. */ 
  }
  
  template<typename Treal, typename Tmatrix, typename Tvector>
    void Perturbation<Treal, Tmatrix, Tvector>::run() {
    Treal const ONE = 1.0;
    mat::SizesAndBlocks rowsCopy;
    X.front()->getRows(rowsCopy);
    mat::SizesAndBlocks colsCopy;
    X.front()->getCols(colsCopy);
    Tmatrix tmpMat;
    //    tmpMat.resetSizesAndBlocks(rowsCopy, colsCopy);
    int nMatrices;
    Treal threshValPerOrder;
    Treal chosenThresh;
    for (int iter = 0; iter < nIter; iter++) {
      std::cout<<"\n\nInside outer loop iter = "<<iter
	       <<"  nIter = "<<nIter
	       <<"  sigma = "<<sigma[iter]<<std::endl;
      /* Number of matrices increases by 1 in each iteration: */
      X.push_back(new Tmatrix);
      nMatrices = X.size();
      X[nMatrices-1]->resetSizesAndBlocks(rowsCopy, colsCopy);
      /* Compute threshold value for each order. */
      threshValPerOrder = threshVal[iter] / nMatrices;
      /* Loop through all nonzero orders. */
      std::cout<<"Entering inner loop nMatrices = "<<nMatrices<<std::endl;
      for (int j = nMatrices-1 ; j >= 0 ; --j) {
      std::cout<<"Inside inner loop j = "<<j<<std::endl;
      std::cout<<"X[j]->eucl() (before compute) = "<<X[j]->eucl(vect,1e-7)<<std::endl;
      std::cout<<"X[j]->frob() (before compute) = "<<X[j]->frob()<<std::endl;
      tmpMat = Treal(Treal(1.0)+sigma[iter]) * (*X[j]);
	std::cout<<"tmpMat.eucl() (before for) = "<<tmpMat.eucl(vect,1e-7)<<std::endl;
      std::cout<<"tmpMat.frob() (before for) = "<<tmpMat.frob()<<std::endl;
	for (int k = 0; k <= j; k++) {
	  /* X[j] = X[j] - sigma * X[k] * X[j-k]      */
	  Tmatrix::ssmmUpperTriangleOnly(-sigma[iter], *X[k], *X[j-k],
					 ONE, tmpMat);
	} /* End 3rd for */
	std::cout<<"tmpMat.eucl() (after for) = "<<tmpMat.eucl(vect,1e-7)<<std::endl;
	*X[j] = tmpMat;
	
	/* Truncate tmpMat, remove if it becomes zero. */
	chosenThresh = threshValPerOrder / pow(deltaMax, Treal(j));
	std::cout<<"X[j]->eucl() (before thresh) = "<<X[j]->eucl(vect,1e-7)<<std::endl;
	std::cout<<"Chosen thresh: "<<chosenThresh<<std::endl;
	Treal actualThresh = X[j]->thresh(chosenThresh, vect, norm);
	std::cout<<"X[j]->eucl() (after thresh) = "<<X[j]->eucl(vect,1e-7)<<std::endl;
#if 1
	/*  If the current matrix is zero AND 
	 *  it is the last matrix
	 */
	if (*X[j] == 0 && int(X.size()) == j+1) {
	  std::cout<<"DELETION: j = "<<j<<"  X.size() = "<<X.size()
		   <<"  X[j] = "<<X[j]<< "  X[j]->frob() = "<<X[j]->frob()
		   <<std::endl;
	  delete X[j];
	  X.pop_back();
	}
	else 
	  std::cout<<"NO DELETION: j = "<<j<<"  X.size() = "<<X.size()
		   <<"  X[j] = "<<X[j]<< "  X[j]->frob() = "<<X[j]->frob()
		   <<std::endl;
#endif
	
      } /* End 2nd for (Loop through orders)     */
    }   /* End 1st for (Loop through iterations) */
  }  /* End run() */
  

  template<typename Treal, typename Tmatrix, typename Tvector>
    void Perturbation<Treal, Tmatrix, Tvector>::
    checkIdempotencies(std::vector<Treal> & idemErrors) {
    Tmatrix tmpMat;
    Treal const ONE = 1.0;
    unsigned int j;
    for (unsigned int m = 0; m < X.size(); ++m) {
      tmpMat = (-ONE) * (*X[m]);
      for (unsigned int i = 0; i <= m; ++i) {
	j = m - i;
	/* TMP = TMP + X[i] * X[j]      */
	Tmatrix::ssmmUpperTriangleOnly(ONE, *X[i], *X[j], ONE, tmpMat);
      }
      /* TMP is symmetric! */
      idemErrors.push_back(tmpMat.eucl(vect,1e-10));
    }
  }

  template<typename Treal, typename Tmatrix, typename Tvector>
    template<typename TmatNoSymm>
    void Perturbation<Treal, Tmatrix, Tvector>::
    checkCommutators(std::vector<Treal> & commErrors, 
		     TmatNoSymm const & dummyMat) {
    mat::SizesAndBlocks rowsCopy;
    X.front()->getRows(rowsCopy);
    mat::SizesAndBlocks colsCopy;
    X.front()->getCols(colsCopy);
    TmatNoSymm tmpMat;
    tmpMat.resetSizesAndBlocks(rowsCopy, colsCopy);
    Treal const ONE = 1.0;
    unsigned int j;
    for (unsigned int m = 0; m < X.size(); ++m) {
      tmpMat = 0;
      std::cout<<"New loop\n";
      for (unsigned int i = 0; i <= m && i < F.size(); ++i) {
	j = m - i;
	std::cout<<i<<", "<<j<<std::endl;
	/* TMP = TMP + F[i] * X[j] - X[j] * F[i]    */
	tmpMat += ONE * (*F[i]) * (*X[j]);
	tmpMat += -ONE * (*X[j]) * (*F[i]);
      }
      /* TMP is not symmetric! */
      commErrors.push_back(tmpMat.frob());
    }   
  }
  
  
  template<typename Treal, typename Tmatrix, typename Tvector>
    void Perturbation<Treal, Tmatrix, Tvector>::
    checkMaxSubspaceError(Treal & subsError) {
    Treal const ONE = 1.0;
    Tmatrix XdeltaMax(*F.front());
    for (unsigned int ind = 1; ind < F.size(); ++ind)
      XdeltaMax += pow(deltaMax, Treal(ind)) * (*F[ind]);
    /***** Initial linear transformation of matrix. */
    Treal lmin = allEigs.low();
    Treal lmax = allEigs.upp();
    /* Scale to [0, 1] interval and negate */
    XdeltaMax.add_identity(-lmax); 
    XdeltaMax *= ((Treal)1.0 / (lmin - lmax));
    
    Tmatrix X2;
    for (int iter = 0; iter < nIter; iter++) {
      X2 = ONE * XdeltaMax * XdeltaMax;
      if (sigma[iter] == Treal(1.0)) {
	XdeltaMax *= 2.0; 
	XdeltaMax -= X2;
      }
      else {
	XdeltaMax = X2; 
      }
    }   /* End of for (Loop through iterations) */
    
    Tmatrix DdeltaMax(*X.front());
    for (unsigned int ind = 1; ind < X.size(); ++ind)
      DdeltaMax += pow(deltaMax, Treal(ind)) * (*X[ind]);
    subsError = Tmatrix::eucl_diff(XdeltaMax,DdeltaMax,
				   vect, errorTol *1e-2); 
  }
  


} /* end namespace mat */
#endif