1 //
2 // init2e.cc
3 //
4 // Copyright (C) 1996 Limit Point Systems, Inc.
5 //
6 // Author: Curtis Janssen <cljanss@limitpt.com>
7 // Maintainer: LPS
8 //
9 // This file is part of the SC Toolkit.
10 //
11 // The SC Toolkit is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU Library General Public License as published by
13 // the Free Software Foundation; either version 2, or (at your option)
14 // any later version.
15 //
16 // The SC Toolkit is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 // GNU Library General Public License for more details.
20 //
21 // You should have received a copy of the GNU Library General Public License
22 // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
23 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 //
25 // The U.S. Government is granted a limited license as per AL 91-7.
26 //
27 
28 #include <stdlib.h>
29 #include <math.h>
30 
31 #include <util/misc/formio.h>
32 #include <chemistry/qc/intv3/flags.h>
33 #include <chemistry/qc/intv3/macros.h>
34 #include <chemistry/qc/intv3/types.h>
35 #include <chemistry/qc/intv3/int2e.h>
36 #include <chemistry/qc/intv3/utils.h>
37 
38 using namespace std;
39 using namespace sc;
40 
41 static void
fail()42 fail()
43 {
44   ExEnv::errn() << scprintf("failing module:\n%s",__FILE__) << endl;
45   abort();
46 }
47 
48 /* Initialize the 2e integral computation routines.
49  * storage = the amount of storage available in bytes
50  * order = order of derivative, must be zero or one
51  * cs1 = center structure for center 1
52  * cs2 = center structure for center 2
53  * cs3 = center structure for center 3
54  * cs4 = center structure for center 4
55  * The integrals which will be computed are (cs1 cs2|cs3 cs4).
56  * This function returns the pointer to the buffer where the
57  * integrals are stored.
58  */
59 double *
int_initialize_erep(size_t storage,int order,const Ref<GaussianBasisSet> & cs1,const Ref<GaussianBasisSet> & cs2,const Ref<GaussianBasisSet> & cs3,const Ref<GaussianBasisSet> & cs4)60 Int2eV3::int_initialize_erep(size_t storage, int order,
61                              const Ref<GaussianBasisSet> &cs1,
62                              const Ref<GaussianBasisSet> &cs2,
63                              const Ref<GaussianBasisSet> &cs3,
64                              const Ref<GaussianBasisSet> &cs4)
65 {
66   int nc1,nc2,nc3,nc4;
67   int jmax,jmax1,jmax2,jmax3,jmax4;
68 
69   redundant_ = 1;
70   permute_ = 0;
71 
72   int_unit_shell = 0;
73 
74   /* Reset the integral storage variables. */
75   int_integral_storage = 0;
76   used_storage_ = 0;
77 
78   /* Turn off exponent weighted contractions. */
79   int_expweight1 = 0;
80   int_expweight2 = 0;
81   int_expweight3 = 0;
82   int_expweight4 = 0;
83 
84   /* See if the order of derivative needed is allowed. */
85   if (order > 1) {
86     ExEnv::errn() << scprintf("int_initialize_erep cannot handle order>1, yet\n");
87     }
88 
89   if (order > 0) {
90     int_derivative_bounds = 1;
91     }
92   else {
93     int_derivative_bounds = 0;
94     }
95 
96   /* Put the center pointers into the global centers pointers. */
97   int_cs1 = cs1;
98   int_cs2 = cs2;
99   int_cs3 = cs3;
100   int_cs4 = cs4;
101 
102   /* Find the max angular momentum on each center. */
103   jmax1 = cs1->max_angular_momentum();
104   if (!int_unit2) jmax2 = cs2->max_angular_momentum();
105   else jmax2 = 0;
106   jmax3 = cs3->max_angular_momentum();
107   if (!int_unit4) jmax4 = cs4->max_angular_momentum();
108   else jmax4 = 0;
109 
110   /* Find the maximum number of contractions in a shell on each center. */
111   nc1 = cs1->max_ncontraction();
112   if (!int_unit2) nc2 = cs2->max_ncontraction();
113   else nc2 = 1;
114   nc3 = cs3->max_ncontraction();
115   if (!int_unit4) nc4 = cs4->max_ncontraction();
116   else nc4 = 1;
117 
118   /* Initialize the Fj(T) routine. */
119   jmax = jmax1+jmax2+jmax3+jmax4;
120   if (int_derivative_bounds) {
121       fjt_ = new FJT(jmax + 2*order); /* The 2 is for bounds checking */
122     }
123   else {
124       fjt_ = new FJT(jmax + order);
125     }
126 
127   /* Initialize the build and shift routines. */
128   int_init_buildgc(order,jmax1,jmax2,jmax3,jmax4,nc1,nc2,nc3,nc4);
129   int_init_shiftgc(order,jmax1,jmax2,jmax3,jmax4);
130 
131   /* Allocate storage for the integral buffer. */
132   int maxsize = cs1->max_ncartesian_in_shell()
133                 *(int_unit2?1:cs2->max_ncartesian_in_shell())
134                 *cs3->max_ncartesian_in_shell()
135                 *(int_unit4?1:cs4->max_ncartesian_in_shell());
136   if (order==0) {
137     int_buffer = (double *) malloc(sizeof(double) * maxsize);
138     int_derint_buffer = 0;
139     }
140   else if (order==1) {
141     int nderint;
142     nderint = cs1->max_ncartesian_in_shell(1)
143              *(int_unit2?1:cs2->max_ncartesian_in_shell(1))
144              *cs3->max_ncartesian_in_shell(1)
145              *(int_unit4?1:cs4->max_ncartesian_in_shell(1));
146 
147     /* Allocate the integral buffers. */
148     int_buffer = (double *) malloc(sizeof(double) * 9*maxsize);
149     int_derint_buffer = (double *) malloc(sizeof(double) * nderint);
150     if (!int_derint_buffer) {
151       ExEnv::errn() << scprintf("couldn't malloc intermed storage for derivative ints\n");
152       fail();
153       }
154     }
155 
156   if (!int_buffer) {
157     ExEnv::errn() << scprintf("couldn't allocate integrals\n");
158     fail();
159     }
160 
161   /* See if the intermediates are to be computed and set global variables
162    * accordingly. */
163 
164   // this size estimate is only accurate if all centers are the same
165   int size_inter_1 = cs1->nshell() * (sizeof(double*)+sizeof(int));
166   if (storage - used_storage_ >= size_inter_1) {
167       int_store1 = 1;
168       used_storage_ += size_inter_1;
169     }
170   else {
171     ExEnv::out0() << indent
172          << "Int2eV3: not storing O(N) intemediates due to lack of memory"
173          << endl;
174     int_store1 = 0;
175     }
176 
177   // this size estimate is only accurate if all centers are the same
178   int size_inter_2 = cs1->nprimitive() * cs1->nprimitive() * (7*sizeof(double));
179   if (storage - used_storage_ >= size_inter_2) {
180       int_store2 = 1;
181       used_storage_ += size_inter_2;
182     }
183   else {
184     ExEnv::out0() << indent
185          << "Int2eV3: not storing O(N^2) intermediates due to lack of memory"
186          << endl;
187     int_store2 = 0;
188     }
189 
190   if (used_storage_ > storage || !int_store1 || !int_store2) {
191     ExEnv::out0()
192          << indent << "Int2eV3: wanted more storage than given" << endl
193          << indent << "  given  storage = " << storage << endl
194          << indent << "  build  storage = " << used_storage_build_ << endl
195          << indent << "  shift  storage = " << used_storage_shift_ << endl
196          << indent << "  used   storage = " << used_storage_ << endl
197          << indent << "  O(N)   storage = " << size_inter_1
198          <<           (int_store1?"":" (not used)") << endl
199          << indent << "  O(N^2) storage = " << size_inter_2
200          <<           (int_store2?"":" (not used)") << endl
201          << endl;
202     }
203 
204   int prim_inter_size = bs1_prim_offset_ + cs1->nprimitive();
205   int shell_inter_size = bs1_shell_offset_ + cs1->nshell();
206   if (bs2_prim_offset_ + (int_unit2?1:cs2->nprimitive()) > prim_inter_size) {
207     prim_inter_size = bs2_prim_offset_ + (int_unit2?1:cs2->nprimitive());
208     shell_inter_size = bs2_shell_offset_ + (int_unit2?1:cs2->nshell());
209     }
210   if (bs3_prim_offset_ + cs3->nprimitive() > prim_inter_size) {
211     prim_inter_size = bs3_prim_offset_ + cs3->nprimitive();
212     shell_inter_size = bs3_shell_offset_ + cs3->nshell();
213     }
214   if (bs4_prim_offset_ + (int_unit4?1:cs4->nprimitive()) > prim_inter_size) {
215     prim_inter_size = bs4_prim_offset_ + (int_unit4?1:cs4->nprimitive());
216     shell_inter_size = bs4_shell_offset_ + (int_unit4?1:cs4->nshell());
217     }
218 
219   /* Allocate storage for the intermediates. */
220   alloc_inter(prim_inter_size, shell_inter_size);
221 
222   /* Set up the one shell intermediates, block by block. */
223   if (int_store1) {
224     compute_shell_1(cs1, bs1_shell_offset_, bs1_prim_offset_);
225     if (cs2.operator!=(cs1))
226         compute_shell_1(cs2, bs2_shell_offset_, bs2_prim_offset_);
227     if (cs3.operator!=(cs2) && cs3.operator!=(cs1))
228         compute_shell_1(cs3, bs3_shell_offset_, bs3_prim_offset_);
229     if (cs4.operator!=(cs3) && cs4.operator!=(cs2)&& cs4.operator!=(cs1))
230         compute_shell_1(cs4, bs4_shell_offset_, bs4_prim_offset_);
231     }
232 
233   /* Compute the two shell intermediates, block by block. */
234   if (int_store2) {
235     /* Compute the two primitive intermediates, block by block. */
236     // Some unnecessary pairs of intermediates are avoided, but
237     // some unnecessary pairs are still being computed.
238     compute_prim_2(cs1,bs1_shell_offset_,bs1_prim_offset_,
239                    cs1,bs1_shell_offset_,bs1_prim_offset_);
240     if (cs2.operator!=(cs1)) {
241       compute_prim_2(cs1,bs1_shell_offset_,bs1_prim_offset_,
242                      cs2,bs2_shell_offset_,bs2_prim_offset_);
243       compute_prim_2(cs2,bs2_shell_offset_,bs2_prim_offset_,
244                      cs1,bs1_shell_offset_,bs1_prim_offset_);
245       // cs2 cs2 terms are not needed since cs1 != cs2
246       //compute_prim_2(cs2,bs2_shell_offset_,bs2_prim_offset_,
247       //               cs2,bs2_shell_offset_,bs2_prim_offset_);
248       }
249     if (cs3.operator!=(cs2) && cs3.operator!=(cs1)) {
250       compute_prim_2(cs1,bs1_shell_offset_,bs1_prim_offset_,
251                      cs3,bs3_shell_offset_,bs3_prim_offset_);
252       compute_prim_2(cs3,bs3_shell_offset_,bs3_prim_offset_,
253                      cs1,bs1_shell_offset_,bs1_prim_offset_);
254       compute_prim_2(cs2,bs2_shell_offset_,bs2_prim_offset_,
255                      cs3,bs3_shell_offset_,bs3_prim_offset_);
256       compute_prim_2(cs3,bs3_shell_offset_,bs3_prim_offset_,
257                      cs2,bs2_shell_offset_,bs2_prim_offset_);
258       compute_prim_2(cs3,bs3_shell_offset_,bs3_prim_offset_,
259                      cs3,bs3_shell_offset_,bs3_prim_offset_);
260       }
261     if (cs4.operator!=(cs3) && cs4.operator!=(cs2) && cs4.operator!=(cs1)) {
262       compute_prim_2(cs1,bs1_shell_offset_,bs1_prim_offset_,
263                      cs4,bs4_shell_offset_,bs4_prim_offset_);
264       compute_prim_2(cs4,bs4_shell_offset_,bs4_prim_offset_,
265                      cs1,bs1_shell_offset_,bs1_prim_offset_);
266       compute_prim_2(cs2,bs2_shell_offset_,bs2_prim_offset_,
267                      cs4,bs4_shell_offset_,bs4_prim_offset_);
268       compute_prim_2(cs4,bs4_shell_offset_,bs4_prim_offset_,
269                      cs2,bs2_shell_offset_,bs2_prim_offset_);
270       compute_prim_2(cs3,bs3_shell_offset_,bs3_prim_offset_,
271                      cs4,bs4_shell_offset_,bs4_prim_offset_);
272       compute_prim_2(cs4,bs4_shell_offset_,bs4_prim_offset_,
273                      cs3,bs3_shell_offset_,bs3_prim_offset_);
274       // cs4 cs4 terms are never needed since cs4 != cs3
275       //compute_prim_2(cs4,bs4_shell_offset_,bs_prim_offset_,
276       //               cs4,bs4_shell_offset_,bs_prim_offset_);
277       }
278     }
279 
280   return int_buffer;
281   }
282 
283 /* This is called when no more 2 electron integrals are needed.
284  * It will free the intermediates. */
285 void
int_done_erep()286 Int2eV3::int_done_erep()
287 {
288   if (int_unit_shell) delete_int_unit_shell();
289   if (int_derint_buffer) free(int_derint_buffer);
290   free(int_buffer);
291   if (int_store1) {
292     delete[] int_shell_to_prim;
293     }
294   int_done_buildgc();
295   int_done_shiftgc();
296 }
297 
298 /* Allocates storage for the intermediates.  The arguments are the
299  * total number of unique primitive and shells. */
300 void
alloc_inter(int nprim,int nshell)301 Int2eV3::alloc_inter(int nprim,int nshell)
302 {
303   if (int_store1) {
304     int_shell_r.set_dim(nshell,3);
305     int_shell_to_prim = new int[nshell];
306     if (int_shell_to_prim == 0) {
307       ExEnv::errn() << "problem allocating O(n) integral intermediates for";
308       ExEnv::errn() << scprintf(" %d shells and %d primitives",nshell,nprim);
309       ExEnv::errn() << endl;
310       fail();
311       }
312     }
313   if (int_store2) {
314     int_prim_zeta.set_dim(nprim,nprim);
315     int_prim_oo2zeta.set_dim(nprim,nprim);
316     int_prim_k.set_dim(nprim,nprim);
317     int_prim_p.set_dim(nprim,nprim,3);
318     }
319   }
320 
321 void
compute_shell_1(Ref<GaussianBasisSet> cs,int shell_offset,int prim_offset)322 Int2eV3::compute_shell_1(Ref<GaussianBasisSet> cs,
323                          int shell_offset, int prim_offset)
324 {
325   if (cs.null()) {
326     for (int i=0; i<3; i++) {
327       int_shell_r(shell_offset,i) = 0.0;
328       }
329     int_shell_to_prim[shell_offset] = prim_offset;
330     return;
331     }
332 
333   int i,j;
334   int offset;
335   int iprim;
336 
337   offset = shell_offset;
338   iprim = prim_offset;
339   for (i=0; i<cs->ncenter(); i++) {
340     for (j=0; j<cs->nshell_on_center(i); j++) {
341 
342       /* The offset shell geometry vectors. */
343       for (int xyz=0; xyz<3; xyz++) {
344         int_shell_r(offset,xyz) = cs->molecule()->r(i,xyz);
345         }
346 
347       /* The number of the first offset primitive in a offset shell. */
348       int_shell_to_prim[offset] = iprim;
349 
350       offset++;
351       iprim += cs->shell(i,j).nprimitive();
352       }
353     }
354   }
355 
356 /* The 2 primitive intermediates. */
357 void
compute_prim_2(Ref<GaussianBasisSet> cs1,int shell_offset1,int prim_offset1,Ref<GaussianBasisSet> cs2,int shell_offset2,int prim_offset2)358 Int2eV3::compute_prim_2(Ref<GaussianBasisSet> cs1,
359                         int shell_offset1, int prim_offset1,
360                         Ref<GaussianBasisSet> cs2,
361                         int shell_offset2, int prim_offset2)
362 {
363   int offset1, offset2;
364   int i1,j1,k1,i2,j2,k2;
365   GaussianShell *shell1,*shell2;
366   int i;
367   /* This is 2^(1/2) * pi^(5/4) */
368   const double sqrt2pi54 = 5.9149671727956129;
369   double AmB,AmB2;
370 
371   if (cs2.null() && !int_unit_shell) make_int_unit_shell();
372 
373   offset1 = prim_offset1;
374   int cs1_ncenter = (cs1.null()?1:cs1->ncenter());
375   for (i1=0; i1<cs1_ncenter; i1++) {
376     int cs1_nshell_on_center = (cs1.null()?1:cs1->nshell_on_center(i1));
377     for (j1=0; j1<cs1_nshell_on_center; j1++) {
378       if (cs1.nonnull()) shell1 = &cs1->shell(i1,j1);
379       else               shell1 = int_unit_shell;
380       for (k1=0; k1<shell1->nprimitive(); k1++) {
381         offset2 = prim_offset2;
382         int cs2_ncenter = (cs2.null()?1:cs2->ncenter());
383         for (i2=0; i2<cs2_ncenter; i2++) {
384           int cs2_nshell_on_center = (cs2.null()?1:cs2->nshell_on_center(i2));
385           for (j2=0; j2<cs2_nshell_on_center; j2++) {
386             if (cs2.nonnull()) shell2 = &cs2->shell(i2,j2);
387             else               shell2 = int_unit_shell;
388             for (k2=0; k2<shell2->nprimitive(); k2++) {
389 
390               /* The zeta = alpha + beta intermediate. */
391               int_prim_zeta(offset1,offset2) =
392                 shell1->exponent(k1) + shell2->exponent(k2);
393 
394               /* The 1/(2 zeta) intermediate times 2.0. */
395               int_prim_oo2zeta(offset1,offset2) =
396                 1.0/int_prim_zeta(offset1,offset2);
397 
398               /* The p = (alpha A + beta B) / zeta */
399               for (i=0; i<3; i++) {
400                 int_prim_p(offset1,offset2,i) =
401                   (  shell1->exponent(k1) * (cs1.null()?0.0
402                                              :cs1->molecule()->r(i1,i))
403                    + shell2->exponent(k2) * (cs2.null()?0.0
404                                              :cs2->molecule()->r(i2,i)))
405                   *  int_prim_oo2zeta(offset1,offset2);
406                 }
407 
408               /* Compute AmB^2 */
409               AmB2 = 0.0;
410               for (i=0; i<3; i++) {
411                 AmB = (cs2.null()?0.0:cs2->molecule()->r(i2,i))
412                     - (cs1.null()?0.0:cs1->molecule()->r(i1,i));
413                 AmB2 += AmB*AmB;
414                 }
415 
416               /* Compute the K intermediate. */
417               int_prim_k(offset1,offset2) =
418                    sqrt2pi54
419                  * int_prim_oo2zeta(offset1,offset2)
420                  * exp( -   shell1->exponent(k1) * shell2->exponent(k2)
421                           * int_prim_oo2zeta(offset1,offset2)
422                           * AmB2 );
423 
424               /* Finish the 1/(2 zeta) intermediate. */
425               int_prim_oo2zeta(offset1,offset2) =
426                 0.5 * int_prim_oo2zeta(offset1,offset2);
427 
428               offset2++;
429               }
430             }
431           }
432         offset1++;
433         }
434       }
435     }
436   }
437 
438 /////////////////////////////////////////////////////////////////////////////
439 
440 // Local Variables:
441 // mode: c++
442 // c-file-style: "CLJ-CONDENSED"
443 // End:
444