ergo
dft_common.h
Go to the documentation of this file.
1 /* Ergo, version 3.2, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2012 Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Primary academic reference:
19  * Kohn−Sham Density Functional Theory Electronic Structure Calculations
20  * with Linearly Scaling Computational Time and Memory Usage,
21  * Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek,
22  * J. Chem. Theory Comput. 7, 340 (2011),
23  * <http://dx.doi.org/10.1021/ct100611z>
24  *
25  * For further information about Ergo, see <http://www.ergoscf.org>.
26  */
27 
28 #ifndef _DFT_COMMON_H_
29 #define _DFT_COMMON_H_
30 
31 #include <stdlib.h>
32 #include <vector>
33 
34 #ifdef __cplusplus
35 #define EXTERN_C extern "C"
36 #else
37 #define EXTERN_C
38 #endif
39 
40 #include "realtype.h"
41 #include "basisinfo.h"
42 #include "matrix_typedefs.h"
43 #include "functionals.h"
44 #include "grid_atomic.h"
45 
51 typedef struct {
52  real fR; /* d/drho F */
53  real fZ; /* d/zeta F */
54 } FirstDrv;
55 
56 /* SecondDrv: matrix of second order functional derivatives with
57  * respect to two parameters: density rho and SQUARE of the
58  * density gradient zeta. The derivatives are computed for alpha-alpha
59  * or beta-beta spin-orbital block (i.e. include triplet flag).
60  */
61 typedef struct {
62  real fR; /* d/drho F */
63  real fZ; /* d/dzeta F */
64  real fRR; /* d/drho^2 F */
65  real fRZ; /* d/(drho dzeta) F */
66  real fZZ; /* d/dzeta^2 F */
67  /* additional derivatives required by */
68  /* general linear response scheme */
69  real fRG; /* d/(drho dgamma) F */
70  real fZG; /* d/(dzeta dgamma) F */
71  real fGG; /* d/dgamma^2 F */
72  real fG; /* d/dgamma F */
73 } SecondDrv;
74 
75 
76 EXTERN_C void dftpot0_(FirstDrv *ds, const real* weight, const FunDensProp* dp);
77 EXTERN_C void dftpot1_(SecondDrv *ds, const real* w, const FunDensProp* dp,
78  const int* triplet);
79 
80 EXTERN_C void dft_init(void);
81 EXTERN_C int dft_setfunc(const char *line);
82 
83 class ShellTree;
84 
86 class ErgoMolInfo : public GridGenMolInfo {
89  public:
90  ErgoMolInfo(const BasisInfoStruct& bis_, const Molecule& mol);
91  virtual ~ErgoMolInfo();
92 
93  virtual void getAtom(int icent, int *cnt, real (*coor)[3],
94  int *charge, int *mult) const;
95  virtual void setShellRadii(real *shellRadii) const;
96  virtual void getBlocks(const real *center, real cellsz,
97  const real *rshell,
98  int *nblcnt, int (*iblcks)[2]) const;
99  void getBlocks1(const real *center, real cellsz,
100  const real *rshell,
101  int *nblcnt, int (*iblcks)[2]) const;
102  virtual void getExps(int *maxl, int **nucbas, real (**aa)[2]) const;
104 };
105 
106 EXTERN_C void ergoShellsToOrbs(const int *nshlbl, const int (*shlblock)[2],
107  int *norbbl, int (*orbblock)[2],
108  const BasisInfoStruct& bis);
109 
111 EXTERN_C void dft_set_num_threads(int nThreads);
112 
113 
114 EXTERN_C void dft_init(void);
115 
116 #define dal_new(sz,tp) (tp*)dal_malloc_((sz)*sizeof(tp),__FUNCTION__, __LINE__)
117 void* dal_malloc_(size_t sz, const char *func, unsigned line);
118 /* dal_malloc: usage discouraged */
119 #define dal_malloc(sz) dal_malloc_((sz),__FUNCTION__, __LINE__)
120 
121 /* useful constants for BLAS interfacing */
122 extern int ZEROI, ONEI, THREEI, FOURI;
123 extern real ZEROR, ONER, TWOR, FOURR;
124 
128 class Box {
129 public:
130  real getDistanceTo(const real* v) const;
131  int getMaxDim() const;
132  real size(int dim) const { return hi[dim]-lo[dim]; }
133 
134  bool overlapsWith(const real *center, real radius) const {
135  real d = getDistanceTo(center);
136  return d < radius;
137  }
138 
143  bool contains(const real *p) const {
144 #if 0
145  printf("B:(%8.2f %8.2f %8.2f)-(%8.2f %8.2f %8.2f): %8.2f %8.2f %8.2f ",
146  lo[0], lo[1], lo[2], hi[0], hi[1], hi[2],
147  p[0], p[1], p[2]);
148 #endif
149  for(int i=0; i<3; i++)
150  if(p[i]<lo[i] || p[i] >= hi[i]) {
151  //puts("F");
152  return false;
153  }
154  //puts("T");
155  return true;
156  }
157 
158  real lo[3];
159  real hi[3];
160 };
161 
162 template<typename Iterator>
163  void getBoundingBox(Box& box, Iterator start, Iterator end)
164 {
165  static const ergo_real OFF = 0.1;
166  if(start == end)
167  throw "BoundingBox called for empty set";
168 
169  real r = start->radius() + OFF;
170  for(int i=0; i<3; i++) {
171  box.lo[i] = start->center[i]-r;
172  box.hi[i] = start->center[i]+r;
173  }
174 
175  for(++start; start != end; ++start) {
176  real r = start->radius() + OFF;
177  for(int i=0; i<3; i++) {
178  real l = start->center[i]-r; if (l<box.lo[i]) box.lo[i] = l;
179  real h = start->center[i]+r; if (h>box.hi[i]) box.hi[i] = h;
180  }
181  }
182 }
183 
184 
185 int sync_threads(bool release, int nThreads);
186 
187 #endif /* _DFT_COMMON_H_ */