static char help[] = "Test LAPACK routine DSYEV() or DSYEVX(). \n\ Reads PETSc matrix A \n\ then computes selected eigenvalues, and optionally, eigenvectors of \n\ a real generalized symmetric-definite eigenproblem \n\ A*x = lambda*x \n\ Input parameters include\n\ -f : file to load\n\ e.g. ./ex116 -f $DATAFILESPATH/matrices/small \n\n"; #include #include extern PetscErrorCode CkEigenSolutions(PetscInt,Mat,PetscInt,PetscInt,PetscReal*,Vec*,PetscReal*); int main(int argc,char **args) { Mat A,A_dense; Vec *evecs; PetscViewer fd; /* viewer */ char file[1][PETSC_MAX_PATH_LEN]; /* input file name */ PetscBool flg,TestSYEVX=PETSC_TRUE; PetscErrorCode ierr; PetscBool isSymmetric; PetscScalar *arrayA,*evecs_array,*work,*evals; PetscMPIInt size; PetscInt m,n,i,cklvl=2; PetscBLASInt nevs,il,iu,in; PetscReal vl,vu,abstol=1.e-8; PetscBLASInt *iwork,*ifail,lwork,lierr,bn; PetscReal tols[2]; ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr; ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRMPI(ierr); if (size != 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"This is a uniprocessor example only!"); ierr = PetscOptionsHasName(NULL,NULL, "-test_syev", &flg);CHKERRQ(ierr); if (flg) { TestSYEVX = PETSC_FALSE; } /* Determine files from which we read the two matrices */ ierr = PetscOptionsGetString(NULL,NULL,"-f",file[0],sizeof(file[0]),&flg);CHKERRQ(ierr); /* Load matrix A */ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file[0],FILE_MODE_READ,&fd);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr); ierr = MatLoad(A,fd);CHKERRQ(ierr); ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr); ierr = MatGetSize(A,&m,&n);CHKERRQ(ierr); /* Check whether A is symmetric */ ierr = PetscOptionsHasName(NULL,NULL, "-check_symmetry", &flg);CHKERRQ(ierr); if (flg) { Mat Trans; ierr = MatTranspose(A,MAT_INITIAL_MATRIX, &Trans);CHKERRQ(ierr); ierr = MatEqual(A, Trans, &isSymmetric);CHKERRQ(ierr); if (!isSymmetric) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"A must be symmetric"); ierr = MatDestroy(&Trans);CHKERRQ(ierr); } /* Solve eigenvalue problem: A_dense*x = lambda*B*x */ /*==================================================*/ /* Convert aij matrix to MatSeqDense for LAPACK */ ierr = MatConvert(A,MATSEQDENSE,MAT_INITIAL_MATRIX,&A_dense);CHKERRQ(ierr); ierr = PetscBLASIntCast(8*n,&lwork);CHKERRQ(ierr); ierr = PetscBLASIntCast(n,&bn);CHKERRQ(ierr); ierr = PetscMalloc1(n,&evals);CHKERRQ(ierr); ierr = PetscMalloc1(lwork,&work);CHKERRQ(ierr); ierr = MatDenseGetArray(A_dense,&arrayA);CHKERRQ(ierr); if (!TestSYEVX) { /* test syev() */ ierr = PetscPrintf(PETSC_COMM_SELF," LAPACKsyev: compute all %D eigensolutions...\n",m);CHKERRQ(ierr); LAPACKsyev_("V","U",&bn,arrayA,&bn,evals,work,&lwork,&lierr); evecs_array = arrayA; ierr = PetscBLASIntCast(m,&nevs);CHKERRQ(ierr); il = 1; ierr = PetscBLASIntCast(m,&iu);CHKERRQ(ierr); } else { /* test syevx() */ il = 1; ierr = PetscBLASIntCast(0.2*m,&iu);CHKERRQ(ierr); ierr = PetscBLASIntCast(n,&in);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_SELF," LAPACKsyevx: compute %d to %d-th eigensolutions...\n",il,iu);CHKERRQ(ierr); ierr = PetscMalloc1(m*n+1,&evecs_array);CHKERRQ(ierr); ierr = PetscMalloc1(6*n+1,&iwork);CHKERRQ(ierr); ifail = iwork + 5*n; /* in the case "I", vl and vu are not referenced */ vl = 0.0; vu = 8.0; LAPACKsyevx_("V","I","U",&bn,arrayA,&bn,&vl,&vu,&il,&iu,&abstol,&nevs,evals,evecs_array,&in,work,&lwork,iwork,ifail,&lierr); ierr = PetscFree(iwork);CHKERRQ(ierr); } ierr = MatDenseRestoreArray(A_dense,&arrayA);CHKERRQ(ierr); if (nevs <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_CONV_FAILED, "nev=%D, no eigensolution has found", nevs); /* View eigenvalues */ ierr = PetscOptionsHasName(NULL,NULL, "-eig_view", &flg);CHKERRQ(ierr); if (flg) { ierr = PetscPrintf(PETSC_COMM_SELF," %D evals: \n",nevs);CHKERRQ(ierr); for (i=0; i dot_max) dot_max = dot; if (dot > tols[1]) { ierr = VecNorm(evec[i],NORM_INFINITY,&norm);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_SELF,"|delta(%D,%D)|: %g, norm: %g\n",i,j,(double)dot,(double)norm);CHKERRQ(ierr); } } } ierr = PetscPrintf(PETSC_COMM_SELF," max|(x_j^T*x_i) - delta_ji|: %g\n",(double)dot_max);CHKERRQ(ierr); case 1: norm_max = 0.0; for (i = il; i< iu; i++) { ierr = MatMult(A, evec[i], vt1);CHKERRQ(ierr); ierr = VecCopy(evec[i], vt2);CHKERRQ(ierr); tmp = -eval[i]; ierr = VecAXPY(vt1,tmp,vt2);CHKERRQ(ierr); ierr = VecNorm(vt1, NORM_INFINITY, &norm);CHKERRQ(ierr); norm = PetscAbsScalar(norm); if (norm > norm_max) norm_max = norm; /* sniff, and bark if necessary */ if (norm > tols[0]) { ierr = PetscPrintf(PETSC_COMM_SELF," residual violation: %D, resi: %g\n",i, norm);CHKERRQ(ierr); } } ierr = PetscPrintf(PETSC_COMM_SELF," max_resi: %g\n", (double)norm_max);CHKERRQ(ierr); break; default: ierr = PetscPrintf(PETSC_COMM_SELF,"Error: cklvl=%D is not supported \n",cklvl);CHKERRQ(ierr); } ierr = VecDestroy(&vt2);CHKERRQ(ierr); ierr = VecDestroy(&vt1);CHKERRQ(ierr); PetscFunctionReturn(0); } /*TEST build: requires: !complex test: requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES) args: -f ${DATAFILESPATH}/matrices/small output_file: output/ex116_1.out test: suffix: 2 requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES) args: -f ${DATAFILESPATH}/matrices/small -test_syev -check_symmetry TEST*/