📄 arpack.c
字号:
for reverse communication. The user should not use WORKD as temporary workspace during the iteration !!!!!!!!!! See Data Distribution Note below. */ lworkl = 3*ncv*ncv + 5*ncv; /* Dimension of the "workl" vector (see below). On must have: lworkl >= 3*ncv*ncv + 5*ncv */ workl = (complex_16*)Malloc(lworkl * sizeof(complex_16)); /* Private (replicated) array on each PE or array allocated on the front end. See Data Distribution Note below. */ rwork = (double*)Malloc(ncv * sizeof(double)); /* Used as workspace */ info = 0; /* If INFO .EQ. 0, a randomly initial residual vector is used. If INFO .NE. 0, RESID contains the initial residual vector, possibly from a previous run. Error flag on output. = 0: Normal exit. = 1: Maximum number of iterations taken. All possible eigenvalues of OP has been found. IPARAM(5) returns the number of wanted converged Ritz values. = 2: No longer an informational error. Deprecated starting with release 2 of ARPACK. = 3: No shifts could be applied during a cycle of the Implicitly restarted Arnoldi iteration. One possibility is to increase the size of NCV relative to NEV. See remark 4 below. = -1: N must be positive. = -2: NEV must be positive. = -3: NCV-NEV >= 1 and less than or equal to N. = -4: The maximum number of Arnoldi update iteration must be greater than zero. = -5: WHICH must be one of 'LM', 'SM', 'LR', 'SR', 'LI', 'SI' = -6: BMAT must be one of 'I' or 'G'. = -7: Length of private work array is not sufficient. = -8: Error return from LAPACK eigenvalue calculation; = -9: Starting vector is zero. = -10: IPARAM(7) must be 1,2,3. = -11: IPARAM(7) = 1 and BMAT = 'G' are incompatible. = -12: IPARAM(1) must be equal to 0 or 1. = -9999: Could not build an Arnoldi factorization. User input error highly likely. Please check actual array dimensions and layout. IPARAM(5) returns the size of the current Arnoldi factorization. */ rvec = 1; /* .true. */ /* If we want Ritz vectors to be computed as well. */ howmny = 'A'; /* What do we want: Ritz or Schur vectors? For Schur, choose: howmny = 'P' */ select = (unsigned int*)Malloc(ncv * sizeof(unsigned int)); /* Internal workspace */ d = (complex_16*)Malloc(nev * sizeof(complex_16)); /* Vector containing the "nev" eigenvalues computed. VERY IMPORTANT: on line 69 of zneupd.f they say it should be nev+1; this is wrong, for see line 283 where it is declared as d(nev) */ z = (complex_16*)Malloc(n * nev * sizeof(complex_16)); /* On exit, if RVEC = .TRUE. and HOWMNY = 'A', then the columns of Z represents approximate eigenvectors (Ritz vectors) corresponding to the NCONV=IPARAM(5) Ritz values for eigensystem A*z = lambda*B*z. If RVEC = .FALSE. or HOWMNY = 'P', then Z is NOT REFERENCED. NOTE: If if RVEC = .TRUE. and a Schur basis is not required, the array Z may be set equal to first NEV+1 columns of the Arnoldi basis array V computed by ZNAUPD. In this case the Arnoldi basis will be destroyed and overwritten with the eigenvector basis. */ ldz = n; /* Leading dimension of "z". In our case, the number of lines of "z". */ sigma.re = 0.; sigma.im = 0.; /* The shift. Not used in this case: we deal with the shift "by hand". */ workev = (complex_16*)Malloc(2 * ncv * sizeof(complex_16)); /* Workspace */ if(bmat != 'I' || iparam[6] != 1) Msg(GERROR, "General and/or shift-invert mode should not be used"); /* Create temp vectors and matrices and apply shift. Warning: with PETSc, the shifting can be very slow if the masks are very different, for example if we are in real arithmetic and have one real matrix and one complex "simulated-real" matrix */ if(!quad_evp){ LinAlg_CreateVector(&v1, &DofData_P->Solver, DofData_P->NbrDof, DofData_P->NbrPart, DofData_P->Part); LinAlg_CreateVector(&v2, &DofData_P->Solver, DofData_P->NbrDof, DofData_P->NbrPart, DofData_P->Part); /* K = K - shift * M */ LinAlg_AddMatrixProdMatrixDouble(K, M, -shift_r, K) ; } else{ /* This is an explanation of our approach to a quadratic eigenvalue problem i.e. - w^2 M x + i w L x + K x = 0. This system is equivalent to (y = i w x) and (i w M y + i w L x + K x = 0), or, in matrix form: | L M | |x| |-K 0 | |x| | I 0 | |y| iw = | 0 I | |y| , or |x| |x| A |y| iw = B |y|. To apply Arpack with a shift 's' (but not in shift inverted mode to avoid the Hermitian constraint!), we build the following operator: (B- sA)^-1 A. To do this, the following computation is performed: (x,y) is transformed to (Solve(D,Lx+sMx+My),Solve(D,-Kx+sMy)) where Solve(D,v) means the solution of the Dx=v linear system and where D=-(s^2M+sL+K). Note that if the number of degrees of freedom is N, the matrix computations are still performed on NxN matrices but the Arpack vector is of size n=2*N. Only the x part of the (x,y) eigenvectors are retained as physical solutions. */ LinAlg_CreateVector(&x, &DofData_P->Solver, DofData_P->NbrDof, DofData_P->NbrPart, DofData_P->Part); LinAlg_CreateVector(&y, &DofData_P->Solver, DofData_P->NbrDof, DofData_P->NbrPart, DofData_P->Part); LinAlg_CreateVector(&v1, &DofData_P->Solver, DofData_P->NbrDof, DofData_P->NbrPart, DofData_P->Part); LinAlg_CreateVector(&w1, &DofData_P->Solver, DofData_P->NbrDof, DofData_P->NbrPart, DofData_P->Part); LinAlg_CreateVector(&w2, &DofData_P->Solver, DofData_P->NbrDof, DofData_P->NbrPart, DofData_P->Part); LinAlg_CreateMatrix(&D, &DofData_P->Solver, DofData_P->NbrDof, DofData_P->NbrDof, DofData_P->NbrPart, DofData_P->Part, NULL); /* D = -(shift^2 * M + shift * L + K) */ LinAlg_CopyMatrix(M, &D); LinAlg_AddMatrixProdMatrixDouble(L, &D, shift_r, &D); LinAlg_AddMatrixProdMatrixDouble(K, &D, shift_r, &D); LinAlg_ProdMatrixDouble(&D, -1., &D); } /* Keep calling znaupd again and again until ido == 99 */ k = 0; do { znaupd_(&ido, &bmat, &n, which, &nev, &tol, resid, &ncv, v, &ldv, iparam, ipntr, workd, workl, &lworkl, rwork, &info); if(ido == 1 || ido == -1){ Msg(INFO, "Arpack iteration %d", k+1); if(!quad_evp){ Arpack2GetDP(n, &workd[ipntr[0]-1], &v1); LinAlg_ProdMatrixVector(M, &v1, &v2); if(!k) LinAlg_Solve(K, &v2, &DofData_P->Solver, &v1); else LinAlg_SolveAgain(K, &v2, &DofData_P->Solver, &v1); GetDP2Arpack(&v1, &workd[ipntr[1]-1]); } else{ Arpack2GetDPSplit(n, &workd[ipntr[0]-1], &x, &y); LinAlg_ProdMatrixVector(M, &y, &w2); LinAlg_ProdMatrixVector(L, &x, &v1); LinAlg_AddVectorVector(&v1, &w2, &v1); LinAlg_ProdMatrixVector(M, &x, &w1); LinAlg_AddVectorProdVectorDouble(&v1, &w1, shift_r, &v1); if(!k) LinAlg_Solve(&D, &v1, &DofData_P->Solver, &w1); else LinAlg_SolveAgain(&D, &v1, &DofData_P->Solver, &w1); LinAlg_ProdMatrixVector(K, &x, &v1); LinAlg_ProdVectorDouble(&v1, -1., &v1); LinAlg_AddVectorProdVectorDouble(&v1, &w2, shift_r, &v1); LinAlg_SolveAgain(&D, &v1, &DofData_P->Solver, &w2); GetDP2ArpackMerge(&w1, &w2, &workd[ipntr[1]-1]); } k++; } else if(ido == 99){ /* We're done! */ break; } else{ Msg(INFO, "Arpack code = %d (ignored)", info); } } while (1); Msg(BIGINFO, "Arpack required %d iterations", k); /* Testing for errors */ if(info == 0){ /* OK */ } else if(info == 1){ Msg(WARNING, "Maxmimum number of iteration reached in EigenSolve"); } else if(info == 2){ Msg(WARNING, "No shifts could be applied during a cycle of the"); Msg(WARNING, "Implicitly restarted Arnoldi iteration. One possibility"); Msg(WARNING, "is to increase the size of NCV relative to NEV."); } else if(info < 0){ Msg(GERROR, "Arpack code = %d", info); } else{ Msg(WARNING, "Arpack code = %d (unknown)", info); } /* Call to zneupd for post-processing */ zneupd_(&rvec, &howmny, select, d, z, &ldz, &sigma, workev, &bmat, &n, which, &nev, &tol, resid, &ncv, v, &ldv, iparam, ipntr, workd, workl, &lworkl, rwork, &info); /* Test for errors */ if(info != 0) Msg(GERROR, "Arpack code = %d (eigenvector post-processing)", info); /* Compute the unshifted eigenvalues and print them, and store the associated eigenvectors */ newsol = 0; for (k = 0; k < nev; k++){ /* Unshift the eigenvalues */ tmp = SQU(d[k].re) + SQU(d[k].im); d[k].re = shift_r + d[k].re/tmp; d[k].im = shift_i - d[k].im/tmp; if(!quad_evp){ /* Eigenvalue = omega^2 */ omega2.re = d[k].re; omega2.im = d[k].im; abs = sqrt(SQU(omega2.re) + SQU(omega2.im)); arg = atan2(omega2.im, omega2.re); omega.re = sqrt(abs) * cos(0.5*arg); omega.im = sqrt(abs) * sin(0.5*arg); f.re = omega.re / TWO_PI; f.im = omega.im / TWO_PI; } else{ /* Eigenvalue = i*omega */ omega.re = d[k].im; omega.im = -d[k].re; omega2.re = SQU(omega.re) - SQU(omega.im); omega2.im = 2. * omega.re * omega.im; f.re = omega.re / TWO_PI; f.im = omega.im / TWO_PI; } Msg(BIGINFO, "Eigenvalue %03d: w^2 = %.12e %s %.12e * i", k+1, omega2.re, (omega2.im > 0) ? "+" : "-", (omega2.im > 0) ? omega2.im : -omega2.im); Msg(BIGINFO, " w = %.12e %s %.12e * i", omega.re, (omega.im > 0) ? "+" : "-", (omega.im > 0) ? omega.im : -omega.im); Msg(BIGINFO, " f = %.12e %s %.12e * i", f.re, (f.im > 0) ? "+" : "-", (f.im > 0) ? f.im : -f.im); if(newsol) { /* Create new solution */ LinAlg_CreateVector(&Solution_S.x, &DofData_P->Solver, DofData_P->NbrDof, DofData_P->NbrPart, DofData_P->Part); List_Add(DofData_P->Solutions, &Solution_S); DofData_P->CurrentSolution = (struct Solution*) List_Pointer(DofData_P->Solutions, List_Nbr(DofData_P->Solutions)-1); } newsol = 1; DofData_P->CurrentSolution->Time = omega.re; DofData_P->CurrentSolution->TimeImag = omega.im; DofData_P->CurrentSolution->TimeStep = (int)Current.TimeStep; DofData_P->CurrentSolution->TimeFunctionValues = NULL; DofData_P->CurrentSolution->SolutionExist = 1; for(l = 0; l < DofData_P->NbrDof; l+=gCOMPLEX_INCREMENT){ j = l / gCOMPLEX_INCREMENT; LinAlg_SetComplexInVector(z[k*n+j].re, z[k*n+j].im, &DofData_P->CurrentSolution->x, l, l+1); } /* Arpack returns eigenvectors normalized in L-2 norm. Renormalize them in L-infty norm so that the absolute value of the largest element is 1 */ tmp = 0.; for(l = 0; l < DofData_P->NbrDof; l+=gCOMPLEX_INCREMENT){ LinAlg_GetComplexInVector(&d1, &d2, &DofData_P->CurrentSolution->x, l, l+1); abs = sqrt(SQU(d1) + SQU(d2)); if(abs > tmp) tmp = abs; } if(tmp > 1.e-16) LinAlg_ProdVectorDouble(&DofData_P->CurrentSolution->x, 1./tmp, &DofData_P->CurrentSolution->x); /* Increment the global timestep counter so that a future GenerateSystem knows which solutions exist */ Current.TimeStep += 1.; /* Update the current value of Time and TimeImag so that $EigenvalueReal and $EigenvalueImag are up-to-date */ Current.Time = omega.re; Current.TimeImag = omega.im; } /* Deallocate */ if(!quad_evp){ LinAlg_DestroyVector(&v1); LinAlg_DestroyVector(&v2); } else{ LinAlg_DestroyVector(&x); LinAlg_DestroyVector(&y); LinAlg_DestroyVector(&v1); LinAlg_DestroyVector(&w1); LinAlg_DestroyVector(&w2); LinAlg_DestroyMatrix(&D); } Free(resid); Free(v); Free(workd); Free(workl); Free(rwork); Free(select); Free(d); Free(z); Free(workev); GetDP_End;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -