ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/math/SquareMatrix.hpp
Revision: 1753
Committed: Tue Jun 12 13:20:28 2012 UTC (12 years, 10 months ago) by gezelter
File size: 11957 byte(s)
Log Message:
Added a double dot tensor contraction.

File Contents

# User Rev Content
1 gezelter 507 /*
2 gezelter 246 * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3 tim 70 *
4 gezelter 246 * The University of Notre Dame grants you ("Licensee") a
5     * non-exclusive, royalty free, license to use, modify and
6     * redistribute this software in source and binary code form, provided
7     * that the following conditions are met:
8     *
9 gezelter 1390 * 1. Redistributions of source code must retain the above copyright
10 gezelter 246 * notice, this list of conditions and the following disclaimer.
11     *
12 gezelter 1390 * 2. Redistributions in binary form must reproduce the above copyright
13 gezelter 246 * notice, this list of conditions and the following disclaimer in the
14     * documentation and/or other materials provided with the
15     * distribution.
16     *
17     * This software is provided "AS IS," without a warranty of any
18     * kind. All express or implied conditions, representations and
19     * warranties, including any implied warranty of merchantability,
20     * fitness for a particular purpose or non-infringement, are hereby
21     * excluded. The University of Notre Dame and its licensors shall not
22     * be liable for any damages suffered by licensee as a result of
23     * using, modifying or distributing the software or its
24     * derivatives. In no event will the University of Notre Dame or its
25     * licensors be liable for any lost revenue, profit or data, or for
26     * direct, indirect, special, consequential, incidental or punitive
27     * damages, however caused and regardless of the theory of liability,
28     * arising out of the use of or inability to use software, even if the
29     * University of Notre Dame has been advised of the possibility of
30     * such damages.
31 gezelter 1390 *
32     * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
33     * research, please cite the appropriate papers when you publish your
34     * work. Good starting points are:
35     *
36     * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
37     * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
38     * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008).
39 gezelter 1665 * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010).
40     * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41 tim 70 */
42 gezelter 246
43 tim 70 /**
44     * @file SquareMatrix.hpp
45     * @author Teng Lin
46     * @date 10/11/2004
47     * @version 1.0
48     */
49 gezelter 507 #ifndef MATH_SQUAREMATRIX_HPP
50 tim 70 #define MATH_SQUAREMATRIX_HPP
51    
52 tim 74 #include "math/RectMatrix.hpp"
53 gezelter 956 #include "utils/NumericConstant.hpp"
54 tim 70
55 gezelter 1390 namespace OpenMD {
56 tim 70
57 gezelter 507 /**
58     * @class SquareMatrix SquareMatrix.hpp "math/SquareMatrix.hpp"
59     * @brief A square matrix class
60     * @template Real the element type
61     * @template Dim the dimension of the square matrix
62     */
63     template<typename Real, int Dim>
64     class SquareMatrix : public RectMatrix<Real, Dim, Dim> {
65     public:
66     typedef Real ElemType;
67     typedef Real* ElemPoinerType;
68 tim 70
69 gezelter 507 /** default constructor */
70     SquareMatrix() {
71     for (unsigned int i = 0; i < Dim; i++)
72     for (unsigned int j = 0; j < Dim; j++)
73     this->data_[i][j] = 0.0;
74     }
75 tim 70
76 gezelter 507 /** Constructs and initializes every element of this matrix to a scalar */
77     SquareMatrix(Real s) : RectMatrix<Real, Dim, Dim>(s){
78     }
79 tim 151
80 gezelter 507 /** Constructs and initializes from an array */
81     SquareMatrix(Real* array) : RectMatrix<Real, Dim, Dim>(array){
82     }
83 tim 151
84    
85 gezelter 507 /** copy constructor */
86     SquareMatrix(const RectMatrix<Real, Dim, Dim>& m) : RectMatrix<Real, Dim, Dim>(m) {
87     }
88 tim 70
89 gezelter 507 /** copy assignment operator */
90     SquareMatrix<Real, Dim>& operator =(const RectMatrix<Real, Dim, Dim>& m) {
91     RectMatrix<Real, Dim, Dim>::operator=(m);
92     return *this;
93     }
94 tim 137
95 gezelter 507 /** Retunrs an identity matrix*/
96 tim 74
97 gezelter 507 static SquareMatrix<Real, Dim> identity() {
98     SquareMatrix<Real, Dim> m;
99 tim 137
100 gezelter 507 for (unsigned int i = 0; i < Dim; i++)
101     for (unsigned int j = 0; j < Dim; j++)
102     if (i == j)
103     m(i, j) = 1.0;
104     else
105     m(i, j) = 0.0;
106 tim 70
107 gezelter 507 return m;
108     }
109 tim 74
110 gezelter 507 /**
111     * Retunrs the inversion of this matrix.
112     * @todo need implementation
113     */
114     SquareMatrix<Real, Dim> inverse() {
115     SquareMatrix<Real, Dim> result;
116 tim 70
117 gezelter 507 return result;
118     }
119 tim 70
120 gezelter 507 /**
121     * Returns the determinant of this matrix.
122     * @todo need implementation
123     */
124     Real determinant() const {
125     Real det;
126     return det;
127     }
128 tim 70
129 gezelter 507 /** Returns the trace of this matrix. */
130     Real trace() const {
131     Real tmp = 0;
132 tim 137
133 gezelter 507 for (unsigned int i = 0; i < Dim ; i++)
134     tmp += this->data_[i][i];
135 tim 70
136 gezelter 507 return tmp;
137     }
138 gezelter 1753
139     /**
140     * Returns the tensor contraction (double dot product) of two rank 2
141     * tensors (or Matrices)
142     * @param t1 first tensor
143     * @param t2 second tensor
144     * @return the tensor contraction (double dot product) of t1 and t2
145     */
146     Real doubleDot( const SquareMatrix<Real, Dim>& t1, const SquareMatrix<Real, Dim>& t2 ) {
147     Real tmp;
148     tmp = 0;
149    
150     for (unsigned int i = 0; i < Dim; i++)
151     for (unsigned int j =0; j < Dim; j++)
152     tmp += t1[i][j] * t2[i][j];
153    
154     return tmp;
155     }
156 tim 70
157 gezelter 1753
158 gezelter 507 /** Tests if this matrix is symmetrix. */
159     bool isSymmetric() const {
160     for (unsigned int i = 0; i < Dim - 1; i++)
161     for (unsigned int j = i; j < Dim; j++)
162 gezelter 956 if (fabs(this->data_[i][j] - this->data_[j][i]) > epsilon)
163 gezelter 507 return false;
164 tim 137
165 gezelter 507 return true;
166     }
167 tim 70
168 gezelter 507 /** Tests if this matrix is orthogonal. */
169     bool isOrthogonal() {
170     SquareMatrix<Real, Dim> tmp;
171 tim 70
172 gezelter 507 tmp = *this * transpose();
173 tim 70
174 gezelter 507 return tmp.isDiagonal();
175     }
176 tim 70
177 gezelter 507 /** Tests if this matrix is diagonal. */
178     bool isDiagonal() const {
179     for (unsigned int i = 0; i < Dim ; i++)
180     for (unsigned int j = 0; j < Dim; j++)
181 gezelter 956 if (i !=j && fabs(this->data_[i][j]) > epsilon)
182 gezelter 507 return false;
183 tim 137
184 gezelter 507 return true;
185     }
186 tim 137
187 gezelter 507 /** Tests if this matrix is the unit matrix. */
188     bool isUnitMatrix() const {
189     if (!isDiagonal())
190     return false;
191 tim 70
192 gezelter 507 for (unsigned int i = 0; i < Dim ; i++)
193 gezelter 956 if (fabs(this->data_[i][i] - 1) > epsilon)
194 gezelter 507 return false;
195 tim 137
196 gezelter 507 return true;
197     }
198 tim 70
199 gezelter 507 /** Return the transpose of this matrix */
200     SquareMatrix<Real, Dim> transpose() const{
201     SquareMatrix<Real, Dim> result;
202 tim 273
203 gezelter 507 for (unsigned int i = 0; i < Dim; i++)
204     for (unsigned int j = 0; j < Dim; j++)
205     result(j, i) = this->data_[i][j];
206 tim 273
207 gezelter 507 return result;
208     }
209 tim 273
210 gezelter 507 /** @todo need implementation */
211     void diagonalize() {
212     //jacobi(m, eigenValues, ortMat);
213     }
214 tim 76
215 gezelter 507 /**
216     * Jacobi iteration routines for computing eigenvalues/eigenvectors of
217     * real symmetric matrix
218     *
219     * @return true if success, otherwise return false
220     * @param a symmetric matrix whose eigenvectors are to be computed. On return, the matrix is
221     * overwritten
222     * @param w will contain the eigenvalues of the matrix On return of this function
223     * @param v the columns of this matrix will contain the eigenvectors. The eigenvectors are
224     * normalized and mutually orthogonal.
225     */
226 tim 137
227 gezelter 507 static int jacobi(SquareMatrix<Real, Dim>& a, Vector<Real, Dim>& d,
228     SquareMatrix<Real, Dim>& v);
229     };//end SquareMatrix
230 tim 70
231 tim 76
232 gezelter 507 /*=========================================================================
233 tim 76
234 tim 123 Program: Visualization Toolkit
235     Module: $RCSfile: SquareMatrix.hpp,v $
236 tim 76
237 tim 123 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
238     All rights reserved.
239     See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
240    
241 gezelter 507 This software is distributed WITHOUT ANY WARRANTY; without even
242     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
243     PURPOSE. See the above copyright notice for more information.
244 tim 123
245 gezelter 507 =========================================================================*/
246 tim 123
247 gezelter 507 #define VTK_ROTATE(a,i,j,k,l) g=a(i, j);h=a(k, l);a(i, j)=g-s*(h+g*tau); \
248     a(k, l)=h+s*(g-h*tau)
249 tim 123
250     #define VTK_MAX_ROTATIONS 20
251    
252 gezelter 507 // Jacobi iteration for the solution of eigenvectors/eigenvalues of a nxn
253     // real symmetric matrix. Square nxn matrix a; size of matrix in n;
254     // output eigenvalues in w; and output eigenvectors in v. Resulting
255     // eigenvalues/vectors are sorted in decreasing order; eigenvectors are
256     // normalized.
257     template<typename Real, int Dim>
258     int SquareMatrix<Real, Dim>::jacobi(SquareMatrix<Real, Dim>& a, Vector<Real, Dim>& w,
259     SquareMatrix<Real, Dim>& v) {
260     const int n = Dim;
261     int i, j, k, iq, ip, numPos;
262     Real tresh, theta, tau, t, sm, s, h, g, c, tmp;
263     Real bspace[4], zspace[4];
264     Real *b = bspace;
265     Real *z = zspace;
266 tim 123
267 gezelter 507 // only allocate memory if the matrix is large
268     if (n > 4) {
269     b = new Real[n];
270     z = new Real[n];
271     }
272 tim 123
273 gezelter 507 // initialize
274     for (ip=0; ip<n; ip++) {
275     for (iq=0; iq<n; iq++) {
276     v(ip, iq) = 0.0;
277     }
278     v(ip, ip) = 1.0;
279     }
280     for (ip=0; ip<n; ip++) {
281     b[ip] = w[ip] = a(ip, ip);
282     z[ip] = 0.0;
283     }
284 tim 76
285 gezelter 507 // begin rotation sequence
286     for (i=0; i<VTK_MAX_ROTATIONS; i++) {
287     sm = 0.0;
288     for (ip=0; ip<n-1; ip++) {
289     for (iq=ip+1; iq<n; iq++) {
290     sm += fabs(a(ip, iq));
291     }
292     }
293     if (sm == 0.0) {
294     break;
295     }
296 tim 76
297 gezelter 507 if (i < 3) { // first 3 sweeps
298     tresh = 0.2*sm/(n*n);
299     } else {
300     tresh = 0.0;
301     }
302 tim 76
303 gezelter 507 for (ip=0; ip<n-1; ip++) {
304     for (iq=ip+1; iq<n; iq++) {
305     g = 100.0*fabs(a(ip, iq));
306 tim 76
307 gezelter 507 // after 4 sweeps
308     if (i > 3 && (fabs(w[ip])+g) == fabs(w[ip])
309     && (fabs(w[iq])+g) == fabs(w[iq])) {
310     a(ip, iq) = 0.0;
311     } else if (fabs(a(ip, iq)) > tresh) {
312     h = w[iq] - w[ip];
313     if ( (fabs(h)+g) == fabs(h)) {
314     t = (a(ip, iq)) / h;
315     } else {
316     theta = 0.5*h / (a(ip, iq));
317     t = 1.0 / (fabs(theta)+sqrt(1.0+theta*theta));
318     if (theta < 0.0) {
319     t = -t;
320     }
321     }
322     c = 1.0 / sqrt(1+t*t);
323     s = t*c;
324     tau = s/(1.0+c);
325     h = t*a(ip, iq);
326     z[ip] -= h;
327     z[iq] += h;
328     w[ip] -= h;
329     w[iq] += h;
330     a(ip, iq)=0.0;
331 tim 76
332 gezelter 507 // ip already shifted left by 1 unit
333     for (j = 0;j <= ip-1;j++) {
334     VTK_ROTATE(a,j,ip,j,iq);
335     }
336     // ip and iq already shifted left by 1 unit
337     for (j = ip+1;j <= iq-1;j++) {
338     VTK_ROTATE(a,ip,j,j,iq);
339     }
340     // iq already shifted left by 1 unit
341     for (j=iq+1; j<n; j++) {
342     VTK_ROTATE(a,ip,j,iq,j);
343     }
344     for (j=0; j<n; j++) {
345     VTK_ROTATE(v,j,ip,j,iq);
346     }
347     }
348     }
349     }
350 tim 93
351 gezelter 507 for (ip=0; ip<n; ip++) {
352     b[ip] += z[ip];
353     w[ip] = b[ip];
354     z[ip] = 0.0;
355     }
356     }
357 tim 93
358 gezelter 507 //// this is NEVER called
359     if ( i >= VTK_MAX_ROTATIONS ) {
360     std::cout << "vtkMath::Jacobi: Error extracting eigenfunctions" << std::endl;
361     return 0;
362     }
363 tim 76
364 gezelter 507 // sort eigenfunctions these changes do not affect accuracy
365     for (j=0; j<n-1; j++) { // boundary incorrect
366     k = j;
367     tmp = w[k];
368     for (i=j+1; i<n; i++) { // boundary incorrect, shifted already
369     if (w[i] >= tmp) { // why exchage if same?
370     k = i;
371     tmp = w[k];
372     }
373     }
374     if (k != j) {
375     w[k] = w[j];
376     w[j] = tmp;
377     for (i=0; i<n; i++) {
378     tmp = v(i, j);
379     v(i, j) = v(i, k);
380     v(i, k) = tmp;
381     }
382     }
383     }
384     // insure eigenvector consistency (i.e., Jacobi can compute vectors that
385     // are negative of one another (.707,.707,0) and (-.707,-.707,0). This can
386     // reek havoc in hyperstreamline/other stuff. We will select the most
387     // positive eigenvector.
388     int ceil_half_n = (n >> 1) + (n & 1);
389     for (j=0; j<n; j++) {
390     for (numPos=0, i=0; i<n; i++) {
391     if ( v(i, j) >= 0.0 ) {
392     numPos++;
393     }
394     }
395 tim 963 // if ( numPos < ceil(RealType(n)/RealType(2.0)) )
396 gezelter 507 if ( numPos < ceil_half_n) {
397     for (i=0; i<n; i++) {
398     v(i, j) *= -1.0;
399     }
400     }
401     }
402 tim 76
403 gezelter 507 if (n > 4) {
404     delete [] b;
405     delete [] z;
406 tim 76 }
407 gezelter 507 return 1;
408     }
409 tim 76
410    
411 tim 963 typedef SquareMatrix<RealType, 6> Mat6x6d;
412 tim 76 }
413 tim 123 #endif //MATH_SQUAREMATRIX_HPP
414 tim 76

Properties

Name Value
svn:keywords Author Id Revision Date