# | Line 1 | Line 1 | |
---|---|---|
1 | < | /* |
2 | < | * Copyright (C) 2000-2004 Object Oriented Parallel Simulation Engine (OOPSE) project |
3 | < | * |
4 | < | * Contact: oopse@oopse.org |
5 | < | * |
6 | < | * This program is free software; you can redistribute it and/or |
7 | < | * modify it under the terms of the GNU Lesser General Public License |
8 | < | * as published by the Free Software Foundation; either version 2.1 |
9 | < | * of the License, or (at your option) any later version. |
10 | < | * All we ask is that proper credit is given for our work, which includes |
11 | < | * - but is not limited to - adding the above copyright notice to the beginning |
12 | < | * of your source code files, and to any copyright notice that you may distribute |
13 | < | * with programs based on this work. |
14 | < | * |
15 | < | * This program is distributed in the hope that it will be useful, |
16 | < | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | < | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | < | * GNU Lesser General Public License for more details. |
19 | < | * |
20 | < | * You should have received a copy of the GNU Lesser General Public License |
21 | < | * along with this program; if not, write to the Free Software |
22 | < | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1 | > | /* |
2 | > | * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 | * | |
4 | + | * 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 | + | * 1. Acknowledgement of the program authors must be made in any |
10 | + | * publication of scientific results based in part on use of the |
11 | + | * program. An acceptable form of acknowledgement is citation of |
12 | + | * the article in which the program was described (Matthew |
13 | + | * A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
14 | + | * J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
15 | + | * Parallel Simulation Engine for Molecular Dynamics," |
16 | + | * J. Comput. Chem. 26, pp. 252-271 (2005)) |
17 | + | * |
18 | + | * 2. Redistributions of source code must retain the above copyright |
19 | + | * notice, this list of conditions and the following disclaimer. |
20 | + | * |
21 | + | * 3. Redistributions in binary form must reproduce the above copyright |
22 | + | * notice, this list of conditions and the following disclaimer in the |
23 | + | * documentation and/or other materials provided with the |
24 | + | * distribution. |
25 | + | * |
26 | + | * This software is provided "AS IS," without a warranty of any |
27 | + | * kind. All express or implied conditions, representations and |
28 | + | * warranties, including any implied warranty of merchantability, |
29 | + | * fitness for a particular purpose or non-infringement, are hereby |
30 | + | * excluded. The University of Notre Dame and its licensors shall not |
31 | + | * be liable for any damages suffered by licensee as a result of |
32 | + | * using, modifying or distributing the software or its |
33 | + | * derivatives. In no event will the University of Notre Dame or its |
34 | + | * licensors be liable for any lost revenue, profit or data, or for |
35 | + | * direct, indirect, special, consequential, incidental or punitive |
36 | + | * damages, however caused and regardless of the theory of liability, |
37 | + | * arising out of the use of or inability to use software, even if the |
38 | + | * University of Notre Dame has been advised of the possibility of |
39 | + | * such damages. |
40 | */ | |
41 | < | |
41 | > | |
42 | /** | |
43 | * @file SquareMatrix.hpp | |
44 | * @author Teng Lin | |
# | Line 55 | Line 71 | namespace oopse { | |
71 | data_[i][j] = 0.0; | |
72 | } | |
73 | ||
74 | + | /** Constructs and initializes every element of this matrix to a scalar */ |
75 | + | SquareMatrix(Real s) : RectMatrix<Real, Dim, Dim>(s){ |
76 | + | } |
77 | + | |
78 | + | /** Constructs and initializes from an array */ |
79 | + | SquareMatrix(Real* array) : RectMatrix<Real, Dim, Dim>(array){ |
80 | + | } |
81 | + | |
82 | + | |
83 | /** copy constructor */ | |
84 | SquareMatrix(const RectMatrix<Real, Dim, Dim>& m) : RectMatrix<Real, Dim, Dim>(m) { | |
85 | } | |
# | Line 199 | Line 224 | namespace oopse { | |
224 | // normalized. | |
225 | template<typename Real, int Dim> | |
226 | int SquareMatrix<Real, Dim>::jacobi(SquareMatrix<Real, Dim>& a, Vector<Real, Dim>& w, | |
227 | < | SquareMatrix<Real, Dim>& v) { |
228 | < | const int n = Dim; |
229 | < | int i, j, k, iq, ip, numPos; |
230 | < | Real tresh, theta, tau, t, sm, s, h, g, c, tmp; |
231 | < | Real bspace[4], zspace[4]; |
232 | < | Real *b = bspace; |
233 | < | Real *z = zspace; |
227 | > | SquareMatrix<Real, Dim>& v) { |
228 | > | const int n = Dim; |
229 | > | int i, j, k, iq, ip, numPos; |
230 | > | Real tresh, theta, tau, t, sm, s, h, g, c, tmp; |
231 | > | Real bspace[4], zspace[4]; |
232 | > | Real *b = bspace; |
233 | > | Real *z = zspace; |
234 | ||
235 | < | // only allocate memory if the matrix is large |
236 | < | if (n > 4) |
237 | < | { |
238 | < | b = new Real[n]; |
214 | < | z = new Real[n]; |
235 | > | // only allocate memory if the matrix is large |
236 | > | if (n > 4) { |
237 | > | b = new Real[n]; |
238 | > | z = new Real[n]; |
239 | } | |
240 | ||
241 | < | // initialize |
242 | < | for (ip=0; ip<n; ip++) |
243 | < | { |
244 | < | for (iq=0; iq<n; iq++) |
245 | < | { |
246 | < | v(ip, iq) = 0.0; |
223 | < | } |
224 | < | v(ip, ip) = 1.0; |
241 | > | // initialize |
242 | > | for (ip=0; ip<n; ip++) { |
243 | > | for (iq=0; iq<n; iq++) { |
244 | > | v(ip, iq) = 0.0; |
245 | > | } |
246 | > | v(ip, ip) = 1.0; |
247 | } | |
248 | < | for (ip=0; ip<n; ip++) |
249 | < | { |
250 | < | b[ip] = w[ip] = a(ip, ip); |
229 | < | z[ip] = 0.0; |
248 | > | for (ip=0; ip<n; ip++) { |
249 | > | b[ip] = w[ip] = a(ip, ip); |
250 | > | z[ip] = 0.0; |
251 | } | |
252 | ||
253 | < | // begin rotation sequence |
254 | < | for (i=0; i<VTK_MAX_ROTATIONS; i++) |
255 | < | { |
256 | < | sm = 0.0; |
257 | < | for (ip=0; ip<n-1; ip++) |
258 | < | { |
259 | < | for (iq=ip+1; iq<n; iq++) |
239 | < | { |
240 | < | sm += fabs(a(ip, iq)); |
253 | > | // begin rotation sequence |
254 | > | for (i=0; i<VTK_MAX_ROTATIONS; i++) { |
255 | > | sm = 0.0; |
256 | > | for (ip=0; ip<n-1; ip++) { |
257 | > | for (iq=ip+1; iq<n; iq++) { |
258 | > | sm += fabs(a(ip, iq)); |
259 | > | } |
260 | } | |
261 | < | } |
262 | < | if (sm == 0.0) |
263 | < | { |
245 | < | break; |
246 | < | } |
261 | > | if (sm == 0.0) { |
262 | > | break; |
263 | > | } |
264 | ||
265 | < | if (i < 3) // first 3 sweeps |
266 | < | { |
267 | < | tresh = 0.2*sm/(n*n); |
268 | < | } |
269 | < | else |
253 | < | { |
254 | < | tresh = 0.0; |
255 | < | } |
265 | > | if (i < 3) { // first 3 sweeps |
266 | > | tresh = 0.2*sm/(n*n); |
267 | > | } else { |
268 | > | tresh = 0.0; |
269 | > | } |
270 | ||
271 | < | for (ip=0; ip<n-1; ip++) |
272 | < | { |
273 | < | for (iq=ip+1; iq<n; iq++) |
260 | < | { |
261 | < | g = 100.0*fabs(a(ip, iq)); |
271 | > | for (ip=0; ip<n-1; ip++) { |
272 | > | for (iq=ip+1; iq<n; iq++) { |
273 | > | g = 100.0*fabs(a(ip, iq)); |
274 | ||
275 | < | // after 4 sweeps |
276 | < | if (i > 3 && (fabs(w[ip])+g) == fabs(w[ip]) |
277 | < | && (fabs(w[iq])+g) == fabs(w[iq])) |
278 | < | { |
279 | < | a(ip, iq) = 0.0; |
280 | < | } |
281 | < | else if (fabs(a(ip, iq)) > tresh) |
282 | < | { |
283 | < | h = w[iq] - w[ip]; |
284 | < | if ( (fabs(h)+g) == fabs(h)) |
285 | < | { |
286 | < | t = (a(ip, iq)) / h; |
287 | < | } |
288 | < | else |
289 | < | { |
290 | < | theta = 0.5*h / (a(ip, iq)); |
291 | < | t = 1.0 / (fabs(theta)+sqrt(1.0+theta*theta)); |
292 | < | if (theta < 0.0) |
293 | < | { |
294 | < | t = -t; |
295 | < | } |
296 | < | } |
297 | < | c = 1.0 / sqrt(1+t*t); |
298 | < | s = t*c; |
287 | < | tau = s/(1.0+c); |
288 | < | h = t*a(ip, iq); |
289 | < | z[ip] -= h; |
290 | < | z[iq] += h; |
291 | < | w[ip] -= h; |
292 | < | w[iq] += h; |
293 | < | a(ip, iq)=0.0; |
275 | > | // after 4 sweeps |
276 | > | if (i > 3 && (fabs(w[ip])+g) == fabs(w[ip]) |
277 | > | && (fabs(w[iq])+g) == fabs(w[iq])) { |
278 | > | a(ip, iq) = 0.0; |
279 | > | } else if (fabs(a(ip, iq)) > tresh) { |
280 | > | h = w[iq] - w[ip]; |
281 | > | if ( (fabs(h)+g) == fabs(h)) { |
282 | > | t = (a(ip, iq)) / h; |
283 | > | } else { |
284 | > | theta = 0.5*h / (a(ip, iq)); |
285 | > | t = 1.0 / (fabs(theta)+sqrt(1.0+theta*theta)); |
286 | > | if (theta < 0.0) { |
287 | > | t = -t; |
288 | > | } |
289 | > | } |
290 | > | c = 1.0 / sqrt(1+t*t); |
291 | > | s = t*c; |
292 | > | tau = s/(1.0+c); |
293 | > | h = t*a(ip, iq); |
294 | > | z[ip] -= h; |
295 | > | z[iq] += h; |
296 | > | w[ip] -= h; |
297 | > | w[iq] += h; |
298 | > | a(ip, iq)=0.0; |
299 | ||
300 | < | // ip already shifted left by 1 unit |
301 | < | for (j = 0;j <= ip-1;j++) |
302 | < | { |
303 | < | VTK_ROTATE(a,j,ip,j,iq); |
300 | > | // ip already shifted left by 1 unit |
301 | > | for (j = 0;j <= ip-1;j++) { |
302 | > | VTK_ROTATE(a,j,ip,j,iq); |
303 | > | } |
304 | > | // ip and iq already shifted left by 1 unit |
305 | > | for (j = ip+1;j <= iq-1;j++) { |
306 | > | VTK_ROTATE(a,ip,j,j,iq); |
307 | > | } |
308 | > | // iq already shifted left by 1 unit |
309 | > | for (j=iq+1; j<n; j++) { |
310 | > | VTK_ROTATE(a,ip,j,iq,j); |
311 | > | } |
312 | > | for (j=0; j<n; j++) { |
313 | > | VTK_ROTATE(v,j,ip,j,iq); |
314 | > | } |
315 | > | } |
316 | } | |
300 | – | // ip and iq already shifted left by 1 unit |
301 | – | for (j = ip+1;j <= iq-1;j++) |
302 | – | { |
303 | – | VTK_ROTATE(a,ip,j,j,iq); |
304 | – | } |
305 | – | // iq already shifted left by 1 unit |
306 | – | for (j=iq+1; j<n; j++) |
307 | – | { |
308 | – | VTK_ROTATE(a,ip,j,iq,j); |
309 | – | } |
310 | – | for (j=0; j<n; j++) |
311 | – | { |
312 | – | VTK_ROTATE(v,j,ip,j,iq); |
313 | – | } |
314 | – | } |
317 | } | |
316 | – | } |
318 | ||
319 | < | for (ip=0; ip<n; ip++) |
320 | < | { |
321 | < | b[ip] += z[ip]; |
322 | < | w[ip] = b[ip]; |
323 | < | z[ip] = 0.0; |
323 | < | } |
319 | > | for (ip=0; ip<n; ip++) { |
320 | > | b[ip] += z[ip]; |
321 | > | w[ip] = b[ip]; |
322 | > | z[ip] = 0.0; |
323 | > | } |
324 | } | |
325 | ||
326 | < | //// this is NEVER called |
327 | < | if ( i >= VTK_MAX_ROTATIONS ) |
328 | < | { |
329 | < | std::cout << "vtkMath::Jacobi: Error extracting eigenfunctions" << std::endl; |
330 | < | return 0; |
326 | > | //// this is NEVER called |
327 | > | if ( i >= VTK_MAX_ROTATIONS ) { |
328 | > | std::cout << "vtkMath::Jacobi: Error extracting eigenfunctions" << std::endl; |
329 | > | return 0; |
330 | } | |
331 | ||
332 | < | // sort eigenfunctions these changes do not affect accuracy |
333 | < | for (j=0; j<n-1; j++) // boundary incorrect |
334 | < | { |
336 | < | k = j; |
337 | < | tmp = w[k]; |
338 | < | for (i=j+1; i<n; i++) // boundary incorrect, shifted already |
339 | < | { |
340 | < | if (w[i] >= tmp) // why exchage if same? |
341 | < | { |
342 | < | k = i; |
332 | > | // sort eigenfunctions these changes do not affect accuracy |
333 | > | for (j=0; j<n-1; j++) { // boundary incorrect |
334 | > | k = j; |
335 | tmp = w[k]; | |
336 | + | for (i=j+1; i<n; i++) { // boundary incorrect, shifted already |
337 | + | if (w[i] >= tmp) { // why exchage if same? |
338 | + | k = i; |
339 | + | tmp = w[k]; |
340 | + | } |
341 | } | |
342 | < | } |
343 | < | if (k != j) |
344 | < | { |
345 | < | w[k] = w[j]; |
346 | < | w[j] = tmp; |
347 | < | for (i=0; i<n; i++) |
348 | < | { |
349 | < | tmp = v(i, j); |
353 | < | v(i, j) = v(i, k); |
354 | < | v(i, k) = tmp; |
342 | > | if (k != j) { |
343 | > | w[k] = w[j]; |
344 | > | w[j] = tmp; |
345 | > | for (i=0; i<n; i++) { |
346 | > | tmp = v(i, j); |
347 | > | v(i, j) = v(i, k); |
348 | > | v(i, k) = tmp; |
349 | > | } |
350 | } | |
356 | – | } |
351 | } | |
352 | < | // insure eigenvector consistency (i.e., Jacobi can compute vectors that |
353 | < | // are negative of one another (.707,.707,0) and (-.707,-.707,0). This can |
354 | < | // reek havoc in hyperstreamline/other stuff. We will select the most |
355 | < | // positive eigenvector. |
356 | < | int ceil_half_n = (n >> 1) + (n & 1); |
357 | < | for (j=0; j<n; j++) |
358 | < | { |
359 | < | for (numPos=0, i=0; i<n; i++) |
360 | < | { |
361 | < | if ( v(i, j) >= 0.0 ) |
368 | < | { |
369 | < | numPos++; |
352 | > | // insure eigenvector consistency (i.e., Jacobi can compute vectors that |
353 | > | // are negative of one another (.707,.707,0) and (-.707,-.707,0). This can |
354 | > | // reek havoc in hyperstreamline/other stuff. We will select the most |
355 | > | // positive eigenvector. |
356 | > | int ceil_half_n = (n >> 1) + (n & 1); |
357 | > | for (j=0; j<n; j++) { |
358 | > | for (numPos=0, i=0; i<n; i++) { |
359 | > | if ( v(i, j) >= 0.0 ) { |
360 | > | numPos++; |
361 | > | } |
362 | } | |
363 | < | } |
364 | < | // if ( numPos < ceil(double(n)/double(2.0)) ) |
365 | < | if ( numPos < ceil_half_n) |
366 | < | { |
367 | < | for(i=0; i<n; i++) |
376 | < | { |
377 | < | v(i, j) *= -1.0; |
363 | > | // if ( numPos < ceil(double(n)/double(2.0)) ) |
364 | > | if ( numPos < ceil_half_n) { |
365 | > | for (i=0; i<n; i++) { |
366 | > | v(i, j) *= -1.0; |
367 | > | } |
368 | } | |
379 | – | } |
369 | } | |
370 | ||
371 | < | if (n > 4) |
372 | < | { |
373 | < | delete [] b; |
385 | < | delete [] z; |
371 | > | if (n > 4) { |
372 | > | delete [] b; |
373 | > | delete [] z; |
374 | } | |
375 | < | return 1; |
375 | > | return 1; |
376 | } | |
377 | ||
378 |
– | Removed lines |
+ | Added lines |
< | Changed lines |
> | Changed lines |