~ubuntu-branches/ubuntu/quantal/psicode/quantal

« back to all changes in this revision

Viewing changes to src/bin/ccdensity/build_Z_ROHF.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck
  • Date: 2006-09-10 14:01:33 UTC
  • Revision ID: james.westby@ubuntu.com-20060910140133-ib2j86trekykfsfv
Tags: upstream-3.2.3
ImportĀ upstreamĀ versionĀ 3.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <libdpd/dpd.h>
 
2
#include <libqt/qt.h>
 
3
#include <libciomr/libciomr.h>
 
4
#include <math.h>
 
5
#define EXTERN
 
6
#include "globals.h"
 
7
 
 
8
/* build_Z_ROHF():  Solve the orbital Z-vector equations for ROHF refs:
 
9
**
 
10
**    sum E,M A(AI,EM) D(orb)(E,M) = -X(A,I)
 
11
**
 
12
** where A(AI,EM) is the orbital Hessian computed in build_A(), X(A,I)
 
13
** is the orbital rotation gradient computed in build_X(), and
 
14
** D(orb)(E,M) is the final Z-vector we want. 
 
15
**
 
16
*/
 
17
 
 
18
void build_Z_ROHF(void)
 
19
{
 
20
  dpdbuf4 A;
 
21
  dpdfile2 X1, D;
 
22
  double **X, **T, **Y, **Z;
 
23
  int num_ai, h, nirreps, a, i, count, lastcol, rank, ai;
 
24
  int *virtpi, *occpi, *openpi;
 
25
 
 
26
  nirreps = moinfo.nirreps;
 
27
  occpi = moinfo.occpi; openpi = moinfo.openpi; virtpi = moinfo.virtpi;
 
28
 
 
29
  /*** Construct the ai transformation matrix which places all singly
 
30
       occupied orbital combinations at the end of the vector ***/
 
31
  
 
32
  /* First compute the number of ai pairs */
 
33
  num_ai = 0;
 
34
  for(h=0; h < nirreps; h++)
 
35
    num_ai += virtpi[h] * occpi[h];
 
36
 
 
37
  /*  fprintf(outfile, "num_ai = %d\n", num_ai); */
 
38
 
 
39
  /* Malloc space for the transformation matrix */
 
40
  T = block_matrix(num_ai,num_ai);
 
41
 
 
42
  /* Now compute the row/column swaps we need and the number of zero
 
43
     columns*/
 
44
  for(h=0,count=0,rank=0; h < nirreps; h++)
 
45
    for(a=0; a < virtpi[h]; a++)
 
46
      for(i=0; i < occpi[h]; i++) {
 
47
        if((a >= (virtpi[h] - openpi[h])) &&
 
48
           (i >= (occpi[h] - openpi[h])) )
 
49
          T[count][count] = 0.0;
 
50
        else {
 
51
          T[count][count] = 1.0;
 
52
          rank++;
 
53
        }
 
54
        count++;
 
55
      }
 
56
  count = 0;
 
57
  lastcol = num_ai-1;
 
58
  for(h=0; h < nirreps; h++)
 
59
    for(a=0; a < virtpi[h]; a++)
 
60
      for(i=0; i < occpi[h] && lastcol > count; i++,count++) {
 
61
        if(T[count][count] == 0.0) {
 
62
          while (T[lastcol][lastcol] == 0.0) lastcol--;
 
63
          if(lastcol > count) {
 
64
            T[count][lastcol] = T[lastcol][count] = 1.0;
 
65
            T[lastcol][lastcol] = 0.0;
 
66
          }
 
67
        }
 
68
      }
 
69
 
 
70
  /*  print_mat(T, num_ai, num_ai, outfile); */
 
71
 
 
72
  /*** Finished building the transformation matrix ***/
 
73
 
 
74
  /* Place all the elements of the orbital rotation gradient, X into a
 
75
     linear array, Z */
 
76
  dpd_file2_init(&X1, CC_MISC, 0, 1, 0, "X(A,I)");
 
77
  dpd_file2_mat_init(&X1);
 
78
  dpd_file2_mat_rd(&X1);
 
79
  num_ai = 0;
 
80
  for(h=0; h < nirreps; h++)
 
81
    num_ai += X1.params->rowtot[h]*X1.params->coltot[h];
 
82
 
 
83
  Z = block_matrix(1,num_ai);
 
84
  for(h=0,count=0; h < nirreps; h++)
 
85
    for(a=0; a < X1.params->rowtot[h]; a++)
 
86
      for(i=0; i < X1.params->coltot[h]; i++) 
 
87
        Z[0][count++] = -X1.matrix[h][a][i];
 
88
 
 
89
 
 
90
  dpd_file2_mat_close(&X1);
 
91
  dpd_file2_close(&X1);
 
92
 
 
93
  /* Push the zero elements of X to the end of the vector */
 
94
  X = block_matrix(1,num_ai);
 
95
  /*  newmm(T,0,Z,0,X,num_ai,num_ai,1,1.0,0.0); */
 
96
  if(num_ai)
 
97
    C_DGEMV('n',num_ai, num_ai, 1.0, &(T[0][0]), num_ai, &(Z[0][0]), 1, 0.0, &(X[0][0]), 1);
 
98
 
 
99
  /* Now, grab only irrep 0 of the orbital Hessian */
 
100
  dpd_buf4_init(&A, CC_MISC, 0, 11, 11, 11, 11, 0, "A(EM,AI)");
 
101
  dpd_buf4_mat_irrep_init(&A, 0);
 
102
  dpd_buf4_mat_irrep_rd(&A, 0);
 
103
 
 
104
  /* Move the zero rows and columns of the Hessian to the bottom.
 
105
     Note that as long as we won't be writing A back to disk, it's OK
 
106
     to put the product back in A.matrix[0]. */
 
107
  Y = block_matrix(num_ai,num_ai);
 
108
  /*  newmm(A.matrix[0],0,T,0,Y,num_ai,num_ai,num_ai,1.0,0.0); */
 
109
  if(num_ai) 
 
110
    C_DGEMM('n','n',num_ai,num_ai,num_ai,1.0,&(A.matrix[0][0][0]),num_ai,
 
111
            &(T[0][0]),num_ai,0.0,&(Y[0][0]),num_ai);
 
112
  /*  newmm(T,0,Y,0,A.matrix[0],num_ai,num_ai,num_ai,1.0,0.0); */
 
113
  if(num_ai)
 
114
    C_DGEMM('n','n',num_ai,num_ai,num_ai,1.0,&(T[0][0]),num_ai,&(Y[0][0]),num_ai,
 
115
            0.0,&(A.matrix[0][0][0]),num_ai);
 
116
  free_block(Y);
 
117
 
 
118
  /* Trying out Matt's Pople code --- way to go, Matt! */
 
119
  pople(A.matrix[0], X[0], rank, 1, 1e-12, outfile, 0);
 
120
 
 
121
  dpd_buf4_mat_irrep_close(&A, 0);
 
122
  dpd_buf4_close(&A);
 
123
 
 
124
  /* Now re-order the elements of X back to the DPD format */
 
125
  /*  newmm(T,0,X,0,Z,num_ai,num_ai,1,1.0,0.0); */
 
126
  if(num_ai) 
 
127
    C_DGEMV('n',num_ai, num_ai, 1.0, &(T[0][0]), num_ai, &(X[0][0]), 1, 0.0, &(Z[0][0]), 1);
 
128
  free_block(X);
 
129
 
 
130
  /* We don't need the transformation matrix anymore */
 
131
  free_block(T);
 
132
 
 
133
  /*
 
134
  for(ai=0; ai < num_ai; ai++) fprintf(outfile, "Z[%d] = %20.15f\n", ai, Z[0][ai]);
 
135
  */
 
136
 
 
137
  /* Build the orbital component of Dai --- we'll build these as separate
 
138
     spin cases for future simplicity (e.g., UHF-based codes)*/
 
139
  dpd_file2_init(&D, CC_OEI, 0, 1, 0, "D(orb)(A,I)");
 
140
  dpd_file2_mat_init(&D);
 
141
  for(h=0,count=0; h < nirreps; h++)
 
142
    for(a=0; a < D.params->rowtot[h]; a++)
 
143
      for(i=0; i < D.params->coltot[h]; i++) {
 
144
        D.matrix[h][a][i] = Z[0][count++];
 
145
 
 
146
        if(a >= (virtpi[h] - openpi[h])) D.matrix[h][a][i] = 0.0;
 
147
      }
 
148
  dpd_file2_mat_wrt(&D);
 
149
  dpd_file2_mat_close(&D);
 
150
  dpd_file2_close(&D);
 
151
 
 
152
  dpd_file2_init(&D, CC_OEI, 0, 1, 0, "D(orb)(a,i)");
 
153
  dpd_file2_mat_init(&D);
 
154
  for(h=0,count=0; h < nirreps; h++)
 
155
    for(a=0; a < D.params->rowtot[h]; a++) 
 
156
      for(i=0; i < D.params->coltot[h]; i++) {
 
157
        D.matrix[h][a][i] = Z[0][count++];
 
158
              
 
159
        if(i >= (occpi[h] - openpi[h])) D.matrix[h][a][i] = 0.0;
 
160
      }
 
161
  dpd_file2_mat_wrt(&D);
 
162
  dpd_file2_mat_close(&D);
 
163
  dpd_file2_close(&D);
 
164
 
 
165
  /* We're done with Z */
 
166
  free_block(Z);
 
167
}
 
168
 
 
169