File: rho-mat.cc

package info (click to toggle)
ergo 3.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • 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 (369 lines) | stat: -rw-r--r-- 13,877 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
/* 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 rho-mat.cc Functions for density and gradient evaluation.
    The density can be evaluated at entire batches of grid points.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <stdio.h>

#include "realtype.h"
typedef ergo_real real;

#include "mat_gblas.h"
#include "rho-mat.h"

#if !defined(restrict)
#define restrict
#endif

/** helper function for zeroing only used blocks of orbitals. Selected
    values for the first index are looped over, and all allowed values
    of the second index.

    @param tmp the matrix[nbast][nvclen]

    @param nblocks pointer to an integer containing number of nonzero
    orbital blocks.
    @param iblocks a set of nblocks integer pairs [a,b) defining the
    range of first index to be zeroed.
    @param ldaib not used
    @param nvclen batch length - and the second dimension of tmp.
 */
static void
zeroorbs(real *tmp, const int *nblocks, const int (*iblocks)[2],
         int ldaib, int nvclen)
{
    /* DIMENSION TMP(NVCLEN,NBAST),NBLOCKS(NSYM),IBLOCKS(2,LDAIB,NSYM) */
    int ibl, idx, k; 
    for(ibl=0; ibl<nblocks[0]; ibl++)
        for(idx=iblocks[ibl][0]; idx<iblocks[ibl][1]; idx++) {
            real * tmpi = tmp + idx*nvclen;
            for(k=0; k<nvclen; k++) tmpi[k] = 0.0;
        }
}

/** Computes the expectation value <o|dmat|o'> for a symmetric matrix
  and given set of precomputed orbital values gao. Sparsity of gao as
  determined with help of nblocks and iblocks is used to reduce the
  computational effort.
  @param dmat full square symmetric matrix
  @param nbast size of dmat
  @param gao orbital matrix[nbast][nvclen]. Set values are determied
  by nblocks and iblocks, other values shall not be accessed.
  @param nblocks number of nonzero row blocks in gao
  @param iblocks ranges [a,b) of nonzero blocks in gao.
  @param ldaib not used
  @param tmp temporary matrix [nbast][nvclen]
  @param nvclen batch length - number of columns in gao.
  @param rho the vector[nvclen] where the computed expectation values
   will be stored.
*/
void
getrho_blocked_lda(int nbast, const real * dmat, const real * restrict gao,
                   const int* nblocks, const int (*iblocks)[2],
                   int ldaib, real *tmp, int nvclen, real *rho)
{
/*
      DIMENSION DMAT(NBAST,NBAST), GAO(NVCLEN,NBAST,*)
      DIMENSION NBLOCKS(NSYM), IBLOCKS(2,LDAIB,NSYM), RHO(NVCLEN)
      DIMENSION TMP(NVCLEN,NBAST)
*/
    int ibl, idx, jbl, k;

    /* dzero(TMP,NVCLEN*NBAST) */
    zeroorbs(tmp, nblocks, iblocks, ldaib, nvclen);

    /* only first symmetry */
#if USE_BLAS_IN_XC
    for(ibl=0; ibl<nblocks[0]; ibl++) {
      static const ergo_real ONER = 1.0;
      static const ergo_real HALF = 0.5;
      int cColumns, sumCols; 
      for(jbl=0; jbl<ibl; jbl++) {
	cColumns = iblocks[jbl][1]-iblocks[jbl][0];
	sumCols = iblocks[ibl][1]-iblocks[ibl][0];
	mat::gemm("N", "N", &nvclen, &cColumns, &sumCols, &ONER,
		  gao+iblocks[ibl][0]*nvclen, &nvclen,
		  dmat+iblocks[jbl][0]*nbast + iblocks[ibl][0], &nbast, &ONER,
		  tmp + iblocks[jbl][0]*nvclen, &nvclen);
      }	
      cColumns = iblocks[ibl][1]-iblocks[ibl][0];
      sumCols = iblocks[ibl][1]-iblocks[ibl][0];
      mat::symm("R", "U", &nvclen, &cColumns, &HALF,
		dmat+iblocks[ibl][0]*nbast + iblocks[ibl][0], &nbast,
		gao+iblocks[ibl][0]*nvclen, &nvclen, &ONER,
		tmp + iblocks[ibl][0]*nvclen, &nvclen);
    }
#else /* USE_BLAS_IN_XC */
    int jdx;
    real d;
    for(ibl=0; ibl<nblocks[0]; ibl++)
        /* print *,"block ", IBL,IBLOCKS(1,IBL,ISYM),IBLOCKS(2,IBL,ISYM) */
        for(idx=iblocks[ibl][0]; idx<iblocks[ibl][1]; idx++) {
            real * restrict tmpj;
            const real * restrict gaoi = gao + idx*nvclen;
            for(jbl=0; jbl<nblocks[0]; jbl++) {
                int jtop = iblocks[jbl][1] > idx ? idx : iblocks[jbl][1];
                for(jdx=iblocks[jbl][0]; jdx<jtop; jdx++) {
                  d = dmat[idx +jdx*nbast];
                    tmpj = tmp + jdx*nvclen;
                    for(k=0; k<nvclen; k++)
                        tmpj[k] += gaoi[k]*d;
                }
            }
            tmpj = tmp + idx*nvclen;
            d = dmat[idx +idx*nbast]*0.5;
            for(k=0; k<nvclen; k++)
                tmpj[k] += gaoi[k]*d;
        }
#endif /* USE_BLAS_IN_XC */
    memset(rho, 0, nvclen*sizeof(real));
    for(ibl=0; ibl<nblocks[0]; ibl++)
        for(idx=iblocks[ibl][0]; idx<iblocks[ibl][1]; idx++) {
            const real * restrict gaoi = gao + idx*nvclen;
            const real * restrict tmpi = tmp + idx*nvclen;
            for(k=0; k<nvclen; k++)
                rho[k] += gaoi[k]*tmpi[k]*2.0;
        }
}

/** Computes the expectation value <o|dmat|o'> and its derivatives for
  a symmetric matrix and given set of precomputed orbital values and
  their cartesian derivatives gao. Sparsity of gao as determined with
  help of nblocks and iblocks is used to reduce the computational
  effort.

  @param dmat full square symmetric matrix
  @param nbast size of dmat

  @param gao orbital matrix[4][nbast][nvclen]. First block [0][][]
  contains orbital values. Subsequent blocks - orbital derivatives wrt
  x,y, and z coordinates. Set values are determied by nblocks and
  iblocks, other values shall not be accessed.

  @param nblocks number of nonzero row blocks in gao
  @param iblocks ranges [a,b) of nonzero blocks in gao.
  @param ldaib not used
  @param tmp temporary matrix [nbast][nvclen]
  @param nvclen batch length - number of columns in gao.
  @param rho the vector[nvclen] where the computed expectation values
  will be stored.
  @param grad a vector of triples where the computed gradient values
   will be stored.  
*/
void
getrho_blocked_gga(int nbast, const real * dmat, const real * restrict gao,
                   const int* nblocks, const int (*iblocks)[2],
                   int ldaib, real *tmp, int nvclen,
                   real *rho, real (*grad)[3])
{
/*
      DIMENSION DMAT(NBAST,NBAST), GAO(NVCLEN,NBAST,*)
      DIMENSION NBLOCKS(NSYM), IBLOCKS(2,LDAIB,NSYM), RHO(NVCLEN)
      DIMENSION TMP(NVCLEN,NBAST)
*/
    int ibl, idx, jbl, k;

    /* dzero(TMP,NVCLEN*NBAST) */
    zeroorbs(tmp, nblocks, iblocks, ldaib, nvclen);
    /* only first symmetry */
#if USE_BLAS_IN_XC == 1
    for(ibl=0; ibl<nblocks[0]; ibl++)
      for(jbl=0; jbl<nblocks[0]; jbl++) {
	static const ergo_real ONER = 1.0;
	int cColumns = iblocks[jbl][1]-iblocks[jbl][0];
	int sumCols = iblocks[ibl][1]-iblocks[ibl][0];
	mat::gemm("N", "N", &nvclen, &cColumns, &sumCols, &ONER,
		  gao+iblocks[ibl][0]*nvclen, &nvclen,
		  dmat+iblocks[jbl][0]*nbast + iblocks[ibl][0],
		  &nbast, &ONER,
		  tmp + iblocks[jbl][0]*nvclen, &nvclen);
      }
#else /* USE_BLAS_IN_XC */
    int jdx;
    real d;
    for(ibl=0; ibl<nblocks[0]; ibl++)
        /* print *,"block ", IBL,IBLOCKS(1,IBL,ISYM),IBLOCKS(2,IBL,ISYM) */
        for(idx=iblocks[ibl][0]; idx<iblocks[ibl][1]; idx++) {
            const real * restrict gaoi = gao + idx*nvclen;
            for(jbl=0; jbl<nblocks[0]; jbl++) {
                for(jdx=iblocks[jbl][0]; jdx<iblocks[jbl][1]; jdx++) {
                    real * restrict tmpj = tmp + jdx*nvclen;
                    d = dmat[idx +jdx*nbast];
                    for(k=0; k<nvclen; k++)
                        tmpj[k] += gaoi[k]*d;
                }
            }
        }
#endif /* USE_BLAS_IN_XC */

    memset(rho,  0,   nvclen*sizeof(real));
    memset(grad, 0, 3*nvclen*sizeof(real));
    for(ibl=0; ibl<nblocks[0]; ibl++)
        for(idx=iblocks[ibl][0]; idx<iblocks[ibl][1]; idx++) {
            const real * restrict gaoi = gao + idx*nvclen;
            const real * restrict tmpi = tmp + idx*nvclen;
            for(k=0; k<nvclen; k++) {
                rho[k]     += gaoi[k]*tmpi[k];
                grad[k][0] += gaoi[k + nvclen*nbast]*tmpi[k]*2;
                grad[k][1] += gaoi[k + nvclen*nbast*2]*tmpi[k]*2;
                grad[k][2] += gaoi[k + nvclen*nbast*3]*tmpi[k]*2;
            }
        }
}

/** Computes the expectation value <o|dmat|o'> for a nonsymmetric matrix
  and given set of precomputed orbital values gao. Sparsity of gao as
  determined with help of nblocks and iblocks is used to reduce the
  computational effort.
  @param dmat full square symmetric matrix
  @param nbast size of dmat
  @param gao orbital matrix[nbast][nvclen]. Set values are determied
  by nblocks and iblocks, other values shall not be accessed.
  @param nblocks number of nonzero row blocks in gao
  @param iblocks ranges [a,b) of nonzero blocks in gao.
  @param ldaib not used
  @param tmp temporary matrix [nbast][nvclen]
  @param nvclen batch length - number of columns in gao.
  @param rho the vector[nvclen] where the computed expectation values
   will be stored.
*/
void
getexp_blocked_lda(int nbast, const real * dmat, const real * gao,
                   const int* nblocks, const int (*iblocks)[2],
                   int ldaib, real *tmp, int nvclen, real *rho)
{
  /*
    DIMENSION DMAT(NBAST,NBAST), GAO(NVCLEN,NBAST,*)
    DIMENSION NBLOCKS(NSYM), IBLOCKS(2,LDAIB,NSYM), RHO(NVCLEN)
    DIMENSION TMP(NVCLEN,NBAST)
  */
  int ibl, idx, jbl, jdx, k;
  real d;

  /* dzero(TMP,NVCLEN*NBAST) */
  zeroorbs(tmp, nblocks, iblocks, ldaib, nvclen);

  /* only first symmetry */
  for(ibl=0; ibl<nblocks[0]; ibl++)
    /* print *,"block ", IBL,IBLOCKS(1,IBL,ISYM),IBLOCKS(2,IBL,ISYM) */
    for(idx=iblocks[ibl][0]; idx<iblocks[ibl][1]; idx++) {
      const real * gaoi = gao + idx*nvclen;
      for(jbl=0; jbl<nblocks[0]; jbl++) {
        for(jdx=iblocks[jbl][0]; jdx<iblocks[jbl][1]; jdx++) {
          real *tmpj = tmp + jdx*nvclen;
          d = dmat[idx +jdx*nbast];
          for(k=0; k<nvclen; k++)
            tmpj[k] += gaoi[k]*d;
        }
      }
    }

  memset(rho, 0, nvclen*sizeof(real));
  for(ibl=0; ibl<nblocks[0]; ibl++)
    for(idx=iblocks[ibl][0]; idx<iblocks[ibl][1]; idx++) {
      const real * gaoi = gao + idx*nvclen;
      const real * tmpi = tmp + idx*nvclen;
      for(k=0; k<nvclen; k++)
        rho[k] += gaoi[k]*tmpi[k];
    }
}

/** Computes the expectation value <o|dmat|o'> and its derivatives for
  a nonsymmetric matrix and given set of precomputed orbital values
  and their cartesian derivatives gao. Sparsity of gao as determined
  with help of nblocks and iblocks is used to reduce the computational
  effort.

  @param dmat full square symmetric matrix
  @param nbast size of dmat

  @param gao orbital matrix[4][nbast][nvclen]. First block [0][][]
  contains orbital values. Subsequent blocks - orbital derivatives wrt
  x,y, and z coordinates. Set values are determied by nblocks and
  iblocks, other values shall not be accessed.

  @param nblocks number of nonzero row blocks in gao
  @param iblocks ranges [a,b) of nonzero blocks in gao.
  @param ldaib not used
  @param tmp temporary matrix [nbast][nvclen]
  @param nvclen batch length - number of columns in gao.
  @param rgrad a vector of quartets where the computed expectation
  values and gradient values will be stored.  */
void
getexp_blocked_gga(int nbast, const real * dmat, const real * gao,
                   const int* nblocks, const int (*iblocks)[2],
                   int ldaib, real *tmp, int nvclen,
                   real (*rgrad)[4])
{
/*
      DIMENSION DMAT(NBAST,NBAST), GAO(NVCLEN,NBAST,*)
      DIMENSION NBLOCKS(NSYM), IBLOCKS(2,LDAIB,NSYM), RHO(NVCLEN)
      DIMENSION TMP(NVCLEN,NBAST)
*/
    int ibl, idx, jbl, jdx, k;
    real d;

    /* dzero(TMP,NVCLEN*NBAST) */
    zeroorbs(tmp, nblocks, iblocks, ldaib, nvclen);

    /* only first symmetry */
    for(ibl=0; ibl<nblocks[0]; ibl++)
        /* print *,"block ", IBL,IBLOCKS(1,IBL,ISYM),IBLOCKS(2,IBL,ISYM) */
        for(idx=iblocks[ibl][0]; idx<iblocks[ibl][1]; idx++) {
            const real * gaoi = gao + idx*nvclen;
            for(jbl=0; jbl<nblocks[0]; jbl++) {
                for(jdx=iblocks[jbl][0]; jdx<iblocks[jbl][1]; jdx++) {
                    real *tmpj = tmp + jdx*nvclen;
                    d = dmat[idx +jdx*nbast] + dmat[jdx +idx*nbast];
                    for(k=0; k<nvclen; k++)
                        tmpj[k] += gaoi[k]*d;
                }
            }
        }

    memset(rgrad, 0, 4*nvclen*sizeof(real));
    for(ibl=0; ibl<nblocks[0]; ibl++)
      for(idx=iblocks[ibl][0]; idx<iblocks[ibl][1]; idx++) {
        const real * gaoi = gao + idx*nvclen;
        const real * tmpi = tmp + idx*nvclen;
        for(k=0; k<nvclen; k++) {
          rgrad[k][0] += gaoi[k                 ]*tmpi[k]*0.5;
          rgrad[k][1] += gaoi[k + nvclen*nbast  ]*tmpi[k];
          rgrad[k][2] += gaoi[k + nvclen*nbast*2]*tmpi[k];
          rgrad[k][3] += gaoi[k + nvclen*nbast*3]*tmpi[k];
        }
      }
}