3
* $Date: 2007-01-04 18:46:10 +0100 (Thu, 04 Jan 2007) $
6
* Copyright (C) 2001-2007 The Chemistry Development Kit (CDK) project
8
* Contact: cdk-devel@lists.sf.net
10
* This program is free software; you can redistribute it and/or
11
* modify it under the terms of the GNU Lesser General Public License
12
* as published by the Free Software Foundation; either version 2.1
13
* of the License, or (at your option) any later version.
14
* All we ask is that proper credit is given for our work, which includes
15
* - but is not limited to - adding the above copyright notice to the beginning
16
* of your source code files, and to any copyright notice that you may distribute
17
* with programs based on this work.
19
* This program is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU Lesser General Public License for more details.
24
* You should have received a copy of the GNU Lesser General Public License
25
* along with this program; if not, write to the Free Software
26
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29
package org.openscience.cdk.math.qm;
31
import org.openscience.cdk.math.Matrix;
32
import org.openscience.cdk.math.Vector;
33
import org.openscience.cdk.tools.LoggingTool;
36
* Calculates the orbitals and orbital energies of electron systems
39
* @author Stephan Michels <stephan@vern.chem.tu-berlin.de>
40
* @cdk.created 2001-06-14
42
public class ClosedShellJob
44
private Orbitals orbitals;
47
private LoggingTool log = new LoggingTool(ClosedShellJob.class);
49
private int iterations = 0;
51
public ClosedShellJob(Orbitals orbitals)
53
this.orbitals = orbitals;
56
public Vector getEnergies()
62
* Sorts the orbitals by their energies
64
private void sort(Matrix C, Vector E)
72
for(i=1; i<E.size; i++)
73
if (E.vector[i-1]>E.vector[i])
76
E.vector[i] = E.vector[i-1];
77
E.vector[i-1] = value;
79
for(j=0; j<C.rows; j++)
81
value = C.matrix[j][i];
82
C.matrix[j][i] = C.matrix[j][i-1];
83
C.matrix[j][i-1] = value;
90
private Matrix calculateS(IBasis basis)
92
int size = basis.getSize();
93
Matrix S = new Matrix(size,size);
97
S.matrix[i][j] = basis.calcS(i,j);
103
* Calculates the matrix for the kinetic energy
105
* T_i,j = (1/2) * -<d^2/dx^2 chi_i | chi_j>
107
private Matrix calculateT(IBasis basis)
109
int size = basis.getSize();
110
Matrix J = new Matrix(size,size);
112
for(i=0; i<size; i++)
113
for(j=0; j<size; j++)
114
// (1/2) * -<d^2/dx^2 chi_i | chi_j>
115
J.matrix[i][j] = basis.calcJ(j,i)/2; // Vorsicht indizies sind vertauscht
121
* Calculates the matrix for the potential matrix
123
* V_i,j = <chi_i | 1/r | chi_j>
125
private Matrix calculateV(IBasis basis)
127
int size = basis.getSize();
128
Matrix V = new Matrix(size,size);
130
for(i=0; i<size; i++)
131
for(j=0; j<size; j++)
132
V.matrix[i][j] = basis.calcV(i,j);
138
* Calculates thes values for the 2 electron interactions
140
private double[][][][] calculateI(IBasis basis)
143
int size = basis.getSize();
145
double[][][][] result = new double[size][][][];
146
for(i=0; i<size; i++)
148
result[i] = new double[i+1][][];
151
result[i][j] = new double[size][];
152
for(k=0; k<size; k++)
154
result[i][j][k] = new double[k+1];
157
result[i][j][k][l] = basis.calcI(i,j,k,l);
158
//log.println("("+(i+1)+" "+(j+1)+"|"+(k+1)+" "+(l+1)+")="+result[i][j][k][l]);
167
* Calculates the density matrix
169
private Matrix calculateD(IBasis basis, Matrix C, int count_electrons)
172
int size = basis.getSize();
173
int orbitals = C.getColumns();
174
int occ = count_electrons/2;
175
int locc = count_electrons%2;
176
Matrix D = new Matrix(size,size);
177
log.debug("D:occ="+occ+" locc="+locc);
180
// logger.debug("This class work only correct for closed shells");
182
for(i=0; i<size; i++)
183
for(j=0; j<size; j++)
186
for(k=0; (k<orbitals) && (k<occ); k++)
187
D.matrix[i][j] += 2d*C.matrix[i][k]*C.matrix[j][k];
189
if ((locc==1) && (k+1<orbitals))
190
D.matrix[i][j] += C.matrix[i][k+1]*C.matrix[j][k+1];
195
private Matrix calculateJ(IBasis basis, double[][][][] I, Matrix D)
198
int size = basis.getSize();
199
Matrix J = new Matrix(size,size);
200
for(i=0; i<size; i++)
201
for(j=0; j<size; j++)
204
for(k=0; k<size; k++)
205
for(l=0; l<size; l++)
210
J.matrix[i][j] += D.matrix[k][l]*I[i][j][k][l];
212
J.matrix[i][j] += D.matrix[k][l]*I[i][j][l][k];
217
J.matrix[i][j] += D.matrix[k][l]*I[j][i][k][l];
219
J.matrix[i][j] += D.matrix[k][l]*I[j][i][l][k];
222
J.matrix[i][j] *= 2d;
227
private Matrix calculateK(IBasis basis, double[][][][] I, Matrix D)
230
int size = basis.getSize();
231
Matrix K = new Matrix(size,size);
232
for(i=0; i<size; i++)
233
for(j=0; j<size; j++)
236
for(k=0; k<size; k++)
237
for(l=0; l<size; l++)
242
K.matrix[i][j] += D.matrix[k][l]*I[i][j][k][l];
244
K.matrix[i][j] += D.matrix[k][l]*I[i][j][l][k];
249
K.matrix[i][j] += D.matrix[k][l]*I[j][i][k][l];
251
K.matrix[i][j] += D.matrix[k][l]*I[j][i][l][k];
258
private double contraction(Matrix A, Matrix B)
262
for(i=0; i<A.rows; i++)
263
for(j=0; j<A.columns; j++)
264
result += A.matrix[i][j]*B.matrix[i][j];
268
public Orbitals calculate()
270
long time = System.currentTimeMillis();
272
Matrix C,S,T,V,HAO,H,D,J,K,F,U;
275
IBasis basis = orbitals.getBasis();
277
int count_electrons = orbitals.getCountElectrons();
279
C = orbitals.getCoefficients().duplicate();
281
S = calculateS(basis);
283
log.debug("S = \n"+S+"\n");
285
log.debug("C = \n"+C+"\n");
287
C = C.orthonormalize(S);
288
log.debug("C' = \n"+C+"\n");
289
log.debug("C't * S * C' = \n"+S.similar(C)+"\n");
291
T = calculateT(basis);
292
log.debug("T = \n"+T+"\n");
294
V = calculateV(basis);
295
log.debug("V = \n"+V+"\n");
298
log.debug("HAO = \n"+HAO+"\n");
301
log.debug("H = C't * HAO * C' = \n"+H.similar(C)+"\n");
303
U = H.diagonalize(50);
304
E = H.similar(U).getVectorFromDiagonal();
307
log.debug("C(neu) = \n"+C+"\n");
308
log.debug("E = \n"+E+"\n");
310
for(int j=0; j<E.size; j++)
311
log.debug("E("+(j+1)+".Orbital)="+(E.vector[j]*27.211)+" eV");
313
time = System.currentTimeMillis()-time;
314
log.debug("Time = "+time+" ms");
315
time = System.currentTimeMillis();
318
I = calculateI(basis);
322
for(int i=0; i<iterations; i++)
324
log.debug((i+1)+".Durchlauf\n");
326
time = System.currentTimeMillis();
328
log.debug("C't * S * C' = \n"+S.similar(C)+"\n");
330
log.debug("count of electrons = "+count_electrons+"\n");
332
D = calculateD(basis, C, count_electrons);
333
log.debug("D = \n"+D+"\n");
335
//log.println("2*contraction(D*S) = "+(D.mul(S)).contraction()*2+"\n");
336
log.debug("2*contraction(D*S) = "+contraction(D,S)*2+"\n");
338
//J = calculateJ(basis, D);
339
J = calculateJ(basis, I, D);
340
log.debug("J = \n"+J+"\n");
342
//K = calculateK(basis, D);
343
K = calculateK(basis, I, D);
344
log.debug("K = \n"+K+"\n");
346
F = HAO.add(J).sub(K);
347
log.debug("F = H+J-K = \n"+F+"\n");
350
log.debug("H = C't * F * C' = \n"+H+"\n");
352
U = H.diagonalize(50);
353
E = H.similar(U).getVectorFromDiagonal();
356
log.debug("C(neu) = \n"+C+"\n");
357
log.debug("E = \n"+E+"\n");
359
for(int j=0; j<E.size; j++)
360
log.debug("E("+(j+1)+".Orbital)="+(E.vector[j]*27.211)+" eV");
362
energy = contraction(D,HAO.add(F));
363
log.debug("Gesamtenergie = "+energy+" ("+energy*27.211+" eV)\n");
365
time = System.currentTimeMillis()-time;
366
log.debug("Time = "+time+" ms");
371
return new Orbitals(basis, C);