3 #include <petscviewerhdf5.h>
26 const std::vector<SPIVec> &A,
32 for(PetscInt j=0; j<n; ++j){
42 Init(rowscols,rowscols,_name);
50 Init(rowsm,colsn,_name);
63 ierr = MatCreate(PETSC_COMM_WORLD,&
mat);CHKERRXX(
ierr);
64 ierr = MatSetSizes(
mat,PETSC_DECIDE,PETSC_DECIDE,m,n);CHKERRXX(
ierr);
67 ierr = MatSetType(
mat,MATMPIAIJCUSPARSE);CHKERRXX(
ierr);
82 ierr = MatSetValue(
mat,m,n,v,INSERT_VALUES);CHKERRXX(
ierr);
90 for(PetscInt i=0; i<
rows; ++i){
91 (*this)(i,
col,v(i,PETSC_TRUE));
102 ierr = MatSetValue(
mat,m,n,v,ADD_VALUES);CHKERRXX(
ierr);
111 PetscErrorCode ierr2;
112 PetscScalar v,v_global=0.;
114 ierr2 = MatGetOwnershipRange(
mat,&low, &high);CHKERRXX(ierr2);
115 if ((low<=m) && (m<high)){
116 ierr2 = MatGetValues(
mat,1,&m, 1,&n, &v);
119 MPIU_Allreduce(&v,&v_global,1,MPIU_SCALAR,MPIU_SUM,PETSC_COMM_WORLD);
134 PetscScalar v,v_global=0.;
136 ierr = MatGetOwnershipRange(
mat,&low, &high);CHKERRXX(
ierr);
137 if ((low<=m) && (m<high)){
138 ierr = MatGetValues(
mat,1,&m, 1,&n, &v);
141 MPIU_Allreduce(&v,&v_global,1,MPIU_SCALAR,MPIU_SUM,PETSC_COMM_WORLD);
155 ierr = MatSetValue(
mat,m,n,v,INSERT_VALUES);CHKERRXX(
ierr);
166 return (*
this)(m,n,(PetscScalar)(v+0.0*PETSC_i));
175 return (*
this)(m,n,(PetscScalar)((
double)v+0.0*PETSC_i));
187 PetscInt rowoffset = m;
188 PetscInt coloffset = n;
189 PetscInt nsub = Asub.
rows;
190 PetscInt Isubstart,Isubend;
192 const PetscInt *
cols;
193 const PetscScalar *vals;
196 MatGetOwnershipRange(Asub.
mat,&Isubstart,&Isubend);
199 for (PetscInt i=Isubstart; i<Isubend && i<nsub; i++){
201 PetscInt offcols[ncols];
202 PetscScalar avals[ncols];
203 for (PetscInt j=0; j<ncols; j++) {
204 offcols[j] =
cols[j]+coloffset;
208 PetscInt rowoffseti = i+rowoffset;
209 ierr = MatSetValues(
mat,1,&rowoffseti,ncols,offcols,avals,addv);CHKERRXX(
ierr);
211 MatRestoreRow(Asub.
mat,i,&ncols,&
cols,&vals);
219 ierr = MatAssemblyBegin(
mat,MAT_FINAL_ASSEMBLY);CHKERRXX(
ierr);
220 ierr = MatAssemblyEnd(
mat,MAT_FINAL_ASSEMBLY);CHKERRXX(
ierr);
228 ierr = MatAXPY(this->
mat,1.,X.
mat,DIFFERENT_NONZERO_PATTERN);CHKERRXX(
ierr);
236 ierr = MatAXPY(this->
mat,a,X.
mat,DIFFERENT_NONZERO_PATTERN);CHKERRXX(
ierr);
246 ierr = MatAXPY(A.
mat,1.,X.
mat,DIFFERENT_NONZERO_PATTERN);CHKERRXX(
ierr);
248 ierr = MatSetType(A.
mat,MATMPIAIJCUSPARSE);CHKERRXX(
ierr);
259 ierr = MatAXPY(this->
mat,-1.,X.
mat,DIFFERENT_NONZERO_PATTERN);CHKERRXX(
ierr);
269 ierr = MatAXPY(A.
mat,-1.,X.
mat,DIFFERENT_NONZERO_PATTERN);CHKERRXX(
ierr);
271 ierr = MatSetType(A.
mat,MATMPIAIJCUSPARSE);CHKERRXX(
ierr);
316 ierr = MatScale(this->
mat,(PetscScalar)(a+0.*PETSC_i));CHKERRXX(
ierr);
337 return (1./a)*(*this);
344 for(PetscInt i=0; i<Z.
rows; ++i){
345 for(PetscInt j=0; j<Z.
cols; ++j){
346 Z(i,j,((*
this)(i,j,PETSC_TRUE))/A(i,j,PETSC_TRUE));
360 ierr = MatMatMult(
mat,A.
mat,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&C.
mat); CHKERRXX(
ierr);
377 ierr = MatConvert(A.
mat,MATSAME,MAT_INITIAL_MATRIX,&
mat);CHKERRXX(
ierr);
379 ierr = MatSetType(
mat,MATMPIAIJCUSPARSE);CHKERRXX(
ierr);
398 ierr = MatTranspose(
mat,MAT_INITIAL_MATRIX,&A.
mat);CHKERRXX(
ierr);
418 ierr = MatHermitianTranspose(
mat,MAT_INITIAL_MATRIX,&A.
mat);CHKERRXX(
ierr);
423 ierr = MatHermitianTranspose(
mat,MAT_INPLACE_MATRIX,&
mat);CHKERRXX(
ierr);
454 ierr = MatZeroRows(
mat,1,&row,0.,0,0); CHKERRXX(
ierr);
461 ierr = MatZeroRows(
mat,1,&row,1.,0,0); CHKERRXX(
ierr);
468 for(PetscInt j=0; j<
cols; j++){
476 std::vector<PetscInt> rows
483 std::vector<PetscInt> rows
500 PetscPrintf(PETSC_COMM_WORLD,(
"\n---------------- "+
name+
"---start------\n").c_str());
502 ierr = MatView(
mat,PETSC_VIEWER_STDOUT_WORLD);CHKERRXX(
ierr);
503 PetscPrintf(PETSC_COMM_WORLD,(
"---------------- "+
name+
"---done-------\n\n").c_str());
543 ierr = VecDuplicate(b.
vec,&x.
vec);CHKERRXX(ierr);
547 ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRXX(ierr);
550 ierr = KSPSetOperators(ksp,A.
mat,A.
mat);CHKERRXX(ierr);
557 ierr = KSPGetPC(ksp,&pc);CHKERRXX(ierr);
558 ierr = PCSetType(pc,PCLU);CHKERRXX(ierr);
559 ierr = KSPSetType(ksp,KSPPREONLY);CHKERRXX(ierr);
564 ierr = KSPSolve(ksp,b.
vec,x.
vec);CHKERRXX(ierr);
574 ierr = KSPDestroy(&ksp);CHKERRXX(ierr);
583 for(PetscInt i=0; i<Y.
rows; ++i){
584 for(PetscInt j=0; j<Y.
cols; ++j){
585 Y(i,j,std::pow<PetscReal>(a,A(i,j,PETSC_TRUE)));
612 Acopy.
ierr = MatCreate(PETSC_COMM_WORLD,&Amat);CHKERRXX(Acopy.
ierr);
613 Acopy.
ierr = MatSetSizes(Amat,PETSC_DECIDE,PETSC_DECIDE,A.
rows,A.
cols);CHKERRXX(Acopy.
ierr);
614 Acopy.
ierr = MatSetType(Amat,MATDENSE);CHKERRXX(Acopy.
ierr);
615 Acopy.
ierr = MatSetUp(Amat);CHKERRXX(Acopy.
ierr);
620 Acopy();CHKERRXX(Acopy.
ierr);
621 Acopy.
ierr = MatLUFactor(Acopy.
mat,NULL,NULL,NULL); CHKERRXX(Acopy.
ierr);
624 B.
ierr = MatCreate(PETSC_COMM_WORLD,&Bmat);CHKERRXX(B.
ierr);
625 B.
ierr = MatSetSizes(Bmat,PETSC_DECIDE,PETSC_DECIDE,A.
rows,A.
cols);CHKERRXX(B.
ierr);
626 B.
ierr = MatSetType(Bmat,MATDENSE);CHKERRXX(B.
ierr);
627 B.
ierr = MatSetUp(Bmat);CHKERRXX(B.
ierr);
632 Ainv.
ierr = MatCreate(PETSC_COMM_WORLD,&Ainvmat);CHKERRXX(Ainv.
ierr);
633 Ainv.
ierr = MatSetSizes(Ainvmat,PETSC_DECIDE,PETSC_DECIDE,A.
rows,A.
cols);CHKERRXX(Ainv.
ierr);
634 Ainv.
ierr = MatSetType(Ainvmat,MATDENSE);CHKERRXX(Ainv.
ierr);
635 Ainv.
ierr = MatSetUp(Ainvmat);CHKERRXX(Ainv.
ierr);
652 ierr = VecDuplicate(b.
vec,&x.
vec);CHKERRXX(ierr);
657 ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRXX(ierr);
660 ierr = KSPSetOperators(ksp,A.
mat,A.
mat);CHKERRXX(ierr);
667 ierr = KSPGetPC(ksp,&pc);CHKERRXX(ierr);
668 ierr = PCSetType(pc,PCLU);CHKERRXX(ierr);
669 ierr = KSPSetType(ksp,KSPPREONLY);CHKERRXX(ierr);
675 for(PetscInt j=0; j<A.
cols; ++j){
676 ierr = KSPSolve(ksp,B.
col(j).
vec,x.
vec);CHKERRXX(ierr);
677 for(PetscInt i=0; i<A.
cols; ++i){
678 PetscScalar value=x(i,PETSC_TRUE);
679 if(value != 0.) Ainv(i,j,value);
691 ierr = KSPDestroy(&ksp);CHKERRXX(ierr);
703 I.
ierr = MatDiagonalSet(I.
mat,one.
vec,INSERT_VALUES);CHKERRXX(I.
ierr);
734 A.
ierr = MatDiagonalSet(A.
mat,d.
vec,INSERT_VALUES);CHKERRXX(A.
ierr);
738 PetscInt r0=d.
rows, r1=k;
739 PetscInt c0=k, c1=d.
rows;
742 A01.ierr = MatDiagonalSet(A01.mat,d.
vec,INSERT_VALUES);CHKERRXX(A01.ierr);
752 PetscInt r0=-k, r1=d.
rows;
753 PetscInt c0=d.
rows, c1=-k;
762 A10.
ierr = MatDiagonalSet(A10.
mat,d.
vec,INSERT_VALUES);CHKERRXX(A10.
ierr);
788 MatGetSize(A.
mat,&m,&n);
789 MatGetSize(B.
mat,&p,&q);
792 PetscInt na=m, nb=p,nc;
800 const PetscInt *cols;
801 const PetscScalar *vals;
802 PetscInt Isubstart,Isubend;
803 ierr = MatGetOwnershipRange(A.
mat,&Isubstart,&Isubend);CHKERRXX(ierr);
804 for (PetscInt rowi=0; rowi<na; rowi++){
806 bool onprocessor=(Isubstart<=rowi) and (rowi<Isubend);
809 ierr = MatGetRow(A.
mat,rowi,&ncols,&cols,&vals);CHKERRXX(ierr);
815 MPI_Allreduce(&ncols,&ncols2,1,MPIU_INT,MPI_SUM,PETSC_COMM_WORLD);
818 PetscScalar vals2[ncols2];
819 PetscInt cols2[ncols2];
820 PetscScalar *vals_temp=
new PetscScalar[ncols2] ();
821 PetscInt *cols_temp=
new PetscInt[ncols2] ();
823 for(PetscInt i=0; i<ncols2; i++){
824 vals_temp[i]=vals[i];
825 cols_temp[i]=cols[i];
829 MPIU_Allreduce(vals_temp,vals2,ncols2,MPIU_SCALAR,MPIU_SUM,PETSC_COMM_WORLD);
830 MPI_Allreduce(cols_temp,cols2,ncols2,MPIU_INT,MPI_SUM,PETSC_COMM_WORLD);
833 for(PetscInt i=0; i<ncols2; i++){
835 C(rowi*nb,cols2[i]*nb,B*vals2[i],INSERT_VALUES);
840 ierr = MatRestoreRow(A.
mat,rowi,&ncols,&cols,&vals); CHKERRXX(ierr);
851 std::tuple<std::vector<PetscReal>, std::vector<SPIVec>, std::vector<SPIVec>>
svd(
861 std::vector<PetscReal> sigma(n);
862 std::vector<SPIVec> u(n);
863 std::vector<SPIVec> v(n);
867 SVDCreate(PETSC_COMM_WORLD, &
svd);
868 SVDSetOperator(
svd, A.
mat);
870 SVDSetFromOptions(
svd);
871 SVDSetDimensions(
svd,n,PETSC_DEFAULT,PETSC_DEFAULT);
876 SVDGetConverged(
svd, &nconv);
877 std::cout<<
"n="<<n<<
" nconv = "<<nconv<<std::endl;
878 for(PetscInt j=0; j<nconv; j++){
880 MatCreateVecs(A.
mat,&v[j].vec,&u[j].vec);
881 SVDGetSingularTriplet(
svd,j,&(sigma[j]),u[j].vec,v[j].vec);
882 u[j].flag_init=PETSC_TRUE;
886 v[j].flag_init=PETSC_TRUE;
902 return std::make_tuple(sigma,u,v);
910 std::vector<PetscReal> sigma;
911 std::vector<SPIVec> u,v;
913 std::tie(sigma,u,v) =
svd(A);
914 for(PetscInt j=0; j<n; ++j){
915 x += ((y.
dot(u[j]))/sigma[j]) * v[j];
921 const std::vector<SPIVec> &A,
928 std::tuple<PetscScalar, SPIVec, SPIVec>
eig(
931 const PetscScalar target,
933 const PetscInt max_iter
936 PetscInt rows=A.
rows;
942 PetscScalar ki,alpha;
943 SPIVec eigl_vec(rows),eigr_vec(rows);
950 ierr = EPSCreate(PETSC_COMM_WORLD,&eps);CHKERRXX(ierr);
954 ierr = EPSSetOperators(eps,A.
mat,B.
mat);CHKERRXX(ierr);
958 ierr = EPSSetFromOptions(eps);CHKERRXX(ierr);
962 EPSWhich which=EPS_TARGET_MAGNITUDE;
964 EPSSetWhichEigenpairs(eps,which);
965 EPSSetDimensions(eps,nev,PETSC_DEFAULT,PETSC_DEFAULT);
966 if ((max_iter==-1) && (tol==-1.)){
967 EPSSetTolerances(eps,PETSC_DEFAULT,PETSC_DEFAULT);
970 EPSSetTolerances(eps,PETSC_DEFAULT,max_iter);
972 else if(max_iter==-1){
973 EPSSetTolerances(eps,tol,PETSC_DEFAULT);
976 EPSSetTolerances(eps,tol,max_iter);
979 which==EPS_TARGET_REAL ||
980 which==EPS_TARGET_IMAGINARY ||
981 which==EPS_TARGET_MAGNITUDE){
983 EPSSetTarget(eps,target);
991 STSetType(st,STSINVERT);
993 EPSSetTwoSided(eps,PETSC_TRUE);
995 ierr = EPSSolve(eps);CHKERRXX(ierr);
1019 ierr = EPSGetConverged(eps,&nconv);CHKERRXX(ierr);
1051 ierr = EPSGetEigenpair(eps,0,&alpha,PETSC_NULL,eigr_vec.
vec,PETSC_NULL);CHKERRXX(ierr);
1052 ierr = EPSGetLeftEigenvector(eps,0,eigl_vec.vec,PETSC_NULL);CHKERRXX(ierr);
1062 ierr = EPSDestroy(&eps);CHKERRXX(ierr);
1068 return std::make_tuple(alpha,eigl_vec,eigr_vec);
1075 const PetscScalar target,
1076 const PetscReal tol,
1077 const PetscInt max_iter
1080 PetscInt rows=A.
rows;
1085 PetscErrorCode ierr;
1086 PetscScalar ki,alpha;
1090 PetscScalar kr_temp;
1094 ierr = EPSCreate(PETSC_COMM_WORLD,&eps);CHKERRXX(ierr);
1098 ierr = EPSSetOperators(eps,A.
mat,B.
mat);CHKERRXX(ierr);
1102 ierr = EPSSetFromOptions(eps);CHKERRXX(ierr);
1106 EPSWhich which=EPS_TARGET_MAGNITUDE;
1108 EPSSetWhichEigenpairs(eps,which);
1109 EPSSetDimensions(eps,nev,PETSC_DEFAULT,PETSC_DEFAULT);
1110 if ((max_iter==-1) && (tol==-1.)){
1111 EPSSetTolerances(eps,PETSC_DEFAULT,PETSC_DEFAULT);
1114 EPSSetTolerances(eps,PETSC_DEFAULT,max_iter);
1116 else if(max_iter==-1){
1117 EPSSetTolerances(eps,tol,PETSC_DEFAULT);
1120 EPSSetTolerances(eps,tol,max_iter);
1123 which==EPS_TARGET_REAL ||
1124 which==EPS_TARGET_IMAGINARY ||
1125 which==EPS_TARGET_MAGNITUDE){
1127 EPSSetTarget(eps,target);
1135 STSetType(st,STSINVERT);
1139 ierr = EPSSolve(eps);CHKERRXX(ierr);
1163 ierr = EPSGetConverged(eps,&nconv);CHKERRXX(ierr);
1195 ierr = EPSGetEigenpair(eps,0,&alpha,PETSC_NULL,eigr_vec.
vec,PETSC_NULL);CHKERRXX(ierr);
1207 ierr = EPSDestroy(&eps);CHKERRXX(ierr);
1213 return std::make_tuple(alpha,eigr_vec);
1220 const PetscScalar target,
1224 const PetscInt max_iter
1227 PetscInt rows=A.
rows;
1232 PetscErrorCode ierr;
1234 SPIVec eigl_vec(rows),eigr_vec(rows);
1236 PetscScalar kr_temp, ki_temp;
1240 ierr = EPSCreate(PETSC_COMM_WORLD,&eps);CHKERRXX(ierr);
1244 ierr = EPSSetOperators(eps,A.
mat,B.
mat);CHKERRXX(ierr);
1248 ierr = EPSSetFromOptions(eps);CHKERRXX(ierr);
1252 EPSWhich which=EPS_TARGET_MAGNITUDE;
1254 EPSSetWhichEigenpairs(eps,which);
1255 EPSSetDimensions(eps,nev,PETSC_DEFAULT,PETSC_DEFAULT);
1256 if ((max_iter==-1) && (tol==-1.)){
1257 EPSSetTolerances(eps,PETSC_DEFAULT,PETSC_DEFAULT);
1260 EPSSetTolerances(eps,PETSC_DEFAULT,max_iter);
1262 else if(max_iter==-1){
1263 EPSSetTolerances(eps,tol,PETSC_DEFAULT);
1266 EPSSetTolerances(eps,tol,max_iter);
1269 which==EPS_TARGET_REAL ||
1270 which==EPS_TARGET_IMAGINARY ||
1271 which==EPS_TARGET_MAGNITUDE){
1273 EPSSetTarget(eps,target);
1281 STSetType(st,STSINVERT);
1283 std::vector<Vec> qrvec(1);
1285 ierr = EPSSetInitialSpace(eps,1,qrvec.data());CHKERRXX(ierr);
1286 std::vector<Vec> qlvec(1);
1288 ierr = EPSSetLeftInitialSpace(eps,1,qlvec.data());CHKERRXX(ierr);
1291 EPSSetTwoSided(eps,PETSC_TRUE);
1292 ierr = EPSSolve(eps);CHKERRXX(ierr);
1315 ierr = EPSGetConverged(eps,&nconv);CHKERRXX(ierr);
1347 ierr = EPSGetEigenpair(eps,0,&alpha,PETSC_NULL,eigr_vec.
vec,PETSC_NULL);CHKERRXX(ierr);
1348 ierr = EPSGetLeftEigenvector(eps,0,eigl_vec.vec,PETSC_NULL);CHKERRXX(ierr);
1358 ierr = EPSDestroy(&eps);CHKERRXX(ierr);
1364 return std::make_tuple(alpha,eigl_vec,eigr_vec);
1371 const PetscScalar target,
1374 const PetscInt max_iter
1377 PetscInt rows=A.
rows;
1382 PetscErrorCode ierr;
1386 PetscScalar kr_temp, ki_temp;
1390 ierr = EPSCreate(PETSC_COMM_WORLD,&eps);CHKERRXX(ierr);
1394 ierr = EPSSetOperators(eps,A.
mat,B.
mat);CHKERRXX(ierr);
1398 ierr = EPSSetFromOptions(eps);CHKERRXX(ierr);
1402 EPSWhich which=EPS_TARGET_MAGNITUDE;
1404 EPSSetWhichEigenpairs(eps,which);
1405 EPSSetDimensions(eps,nev,PETSC_DEFAULT,PETSC_DEFAULT);
1406 if ((max_iter==-1) && (tol==-1.)){
1407 EPSSetTolerances(eps,PETSC_DEFAULT,PETSC_DEFAULT);
1410 EPSSetTolerances(eps,PETSC_DEFAULT,max_iter);
1412 else if(max_iter==-1){
1413 EPSSetTolerances(eps,tol,PETSC_DEFAULT);
1416 EPSSetTolerances(eps,tol,max_iter);
1419 which==EPS_TARGET_REAL ||
1420 which==EPS_TARGET_IMAGINARY ||
1421 which==EPS_TARGET_MAGNITUDE){
1423 EPSSetTarget(eps,target);
1431 STSetType(st,STSINVERT);
1433 std::vector<Vec> qrvec(1);
1435 ierr = EPSSetInitialSpace(eps,1,qrvec.data());CHKERRXX(ierr);
1442 ierr = EPSSolve(eps);CHKERRXX(ierr);
1467 ierr = EPSGetConverged(eps,&nconv);CHKERRXX(ierr);
1499 ierr = EPSGetEigenpair(eps,0,&alpha,PETSC_NULL,eigr_vec.
vec,PETSC_NULL);CHKERRXX(ierr);
1511 ierr = EPSDestroy(&eps);CHKERRXX(ierr);
1517 return std::make_tuple(alpha,eigr_vec);
1524 const std::vector<PetscScalar> targets,
1525 const std::vector<SPIVec> &qrs,
1527 const PetscInt max_iter
1530 PetscInt rows=A.
rows;
1535 PetscErrorCode ierr;
1536 std::vector<PetscScalar> alphas(qrs.size());
1538 std::vector<SPIVec> eigr_vecs(qrs.size());
1541 PetscScalar kr_temp, ki_temp;
1545 ierr = EPSCreate(PETSC_COMM_WORLD,&eps);CHKERRXX(ierr);
1549 ierr = EPSSetOperators(eps,A.
mat,B.
mat);CHKERRXX(ierr);
1553 ierr = EPSSetFromOptions(eps);CHKERRXX(ierr);
1557 EPSWhich which=EPS_TARGET_MAGNITUDE;
1559 EPSSetWhichEigenpairs(eps,which);
1560 EPSSetDimensions(eps,nev,PETSC_DEFAULT,PETSC_DEFAULT);
1561 if ((max_iter==-1) && (tol==-1.)){
1562 EPSSetTolerances(eps,PETSC_DEFAULT,PETSC_DEFAULT);
1565 EPSSetTolerances(eps,PETSC_DEFAULT,max_iter);
1567 else if(max_iter==-1){
1568 EPSSetTolerances(eps,tol,PETSC_DEFAULT);
1571 EPSSetTolerances(eps,tol,max_iter);
1574 which==EPS_TARGET_REAL ||
1575 which==EPS_TARGET_IMAGINARY ||
1576 which==EPS_TARGET_MAGNITUDE){
1578 EPSSetTarget(eps,targets[0]);
1586 STSetType(st,STSINVERT);
1588 PetscInt k=qrs.size();
1589 std::vector<Vec> qrvec(k);
1590 for(PetscInt i=0; i<k; ++i){
1591 qrvec[i] = qrs[i].vec;
1593 ierr = EPSSetInitialSpace(eps,qrvec.size(),qrvec.data());CHKERRXX(ierr);
1601 for(PetscInt i=0; i<k; ++i){
1602 EPSSetWhichEigenpairs(eps,EPS_LARGEST_REAL);
1603 EPSSetWhichEigenpairs(eps,which);
1604 EPSSetTarget(eps,targets[i]);
1605 ierr = EPSSolve(eps);CHKERRXX(ierr);
1629 ierr = EPSGetConverged(eps,&nconv);CHKERRXX(ierr);
1661 ierr = EPSGetEigenpair(eps,0,&alpha,PETSC_NULL,eigr_vec.
vec,PETSC_NULL);CHKERRXX(ierr);
1664 eigr_vecs[i] = eigr_vec;
1676 ierr = EPSDestroy(&eps);CHKERRXX(ierr);
1682 return std::make_tuple(alphas,eigr_vecs);
1687 const std::vector<SPIMat> &As,
1688 const PetscScalar target,
1689 const PetscReal tol,
1690 const PetscInt max_iter
1700 PetscInt rows=As[0].rows;
1702 PetscErrorCode ierr;
1703 PetscScalar ki,alpha;
1706 PetscScalar kr_temp;
1708 ierr = PEPCreate(PETSC_COMM_WORLD,&pep);CHKERRXX(ierr);
1710 std::vector<Mat> AsMat(As.size());
1711 for (
unsigned i=0; i<As.size(); ++i){ AsMat[i]=As[i].mat; }
1712 ierr = PEPSetOperators(pep,AsMat.size(),AsMat.data());CHKERRXX(ierr);
1714 ierr = PEPSetFromOptions(pep);CHKERRXX(ierr);
1716 PEPWhich which=PEP_TARGET_MAGNITUDE;
1718 PEPSetWhichEigenpairs(pep,which);
1719 PEPSetDimensions(pep,nev,PETSC_DEFAULT,PETSC_DEFAULT);
1720 if ((max_iter==-1) && (tol==-1.)){ PEPSetTolerances(pep,PETSC_DEFAULT, PETSC_DEFAULT); }
1721 else if(tol==-1.){ PEPSetTolerances(pep,PETSC_DEFAULT, max_iter); }
1722 else if(max_iter==-1){ PEPSetTolerances(pep,tol, PETSC_DEFAULT); }
1723 else{ PEPSetTolerances(pep,tol, max_iter); }
1724 PEPSetTarget(pep,target);
1728 STSetType(st,STSINVERT);
1734 ierr = PEPSolve(pep);CHKERRXX(ierr);
1738 ierr = PEPGetConverged(pep,&nconv);CHKERRXX(ierr);
1741 ierr = PEPGetEigenpair(pep,0,&alpha,PETSC_NULL,eigr_vec.
vec,PETSC_NULL);CHKERRXX(ierr);
1748 ierr = PEPDestroy(&pep);CHKERRXX(ierr);
1750 return std::make_tuple(alpha,eigr_vec);
1755 const std::vector<SPIMat> &As,
1756 const PetscScalar target,
1758 const PetscReal tol,
1759 const PetscInt max_iter
1769 PetscInt rows=As[0].rows;
1771 PetscErrorCode ierr;
1775 PetscScalar kr_temp;
1777 ierr = PEPCreate(PETSC_COMM_WORLD,&pep);CHKERRXX(ierr);
1779 std::vector<Mat> AsMat(As.size());
1780 for (
unsigned i=0; i<As.size(); ++i){ AsMat[i]=As[i].mat; }
1781 ierr = PEPSetOperators(pep,AsMat.size(),AsMat.data());CHKERRXX(ierr);
1783 ierr = PEPSetFromOptions(pep);CHKERRXX(ierr);
1785 PEPWhich which=PEP_TARGET_MAGNITUDE;
1787 PEPSetWhichEigenpairs(pep,which);
1788 PEPSetDimensions(pep,nev,PETSC_DEFAULT,PETSC_DEFAULT);
1789 if ((max_iter==-1) && (tol==-1.)){ PEPSetTolerances(pep,PETSC_DEFAULT, PETSC_DEFAULT); }
1790 else if(tol==-1.){ PEPSetTolerances(pep,PETSC_DEFAULT, max_iter); }
1791 else if(max_iter==-1){ PEPSetTolerances(pep,tol, PETSC_DEFAULT); }
1792 else{ PEPSetTolerances(pep,tol, max_iter); }
1793 PEPSetTarget(pep,target);
1797 STSetType(st,STSINVERT);
1799 std::vector<Vec> qrvec(1);
1801 ierr = PEPSetInitialSpace(pep,1,qrvec.data());CHKERRXX(ierr);
1810 ierr = PEPSolve(pep);CHKERRXX(ierr);
1813 ierr = PEPGetConverged(pep,&nconv);CHKERRXX(ierr);
1815 ierr = PEPGetEigenpair(pep,0,&alpha,PETSC_NULL,eigr_vec.
vec,PETSC_NULL);CHKERRXX(ierr);
1819 ierr = PEPDestroy(&pep);CHKERRXX(ierr);
1821 return std::make_tuple(alpha,eigr_vec);
1861 const PetscInt rows=Blocks.size();
1862 const PetscInt cols=Blocks[0].size();
1864 PetscInt msum=Blocks[0][0].rows;
1866 PetscInt nsum=Blocks[0][0].cols;
1868 for (PetscInt i=1; i<rows; ++i) m[i] = m[i-1]+Blocks[i-1][0].rows;
1869 for (PetscInt i=1; i<rows; ++i) msum += Blocks[i][0].rows;
1870 for (PetscInt j=1; j<cols; ++j) nsum += Blocks[0][j].cols;
1871 for (PetscInt j=1; j<cols; ++j) n[j] = n[j-1]+Blocks[0][j-1].cols;
1883 for(PetscInt i=0; i<rows; ++i){
1884 for (PetscInt j=0; j<cols; ++j){
1886 A(m[i],n[j],Blocks[i][j],INSERT_VALUES);
1902 for(PetscInt i=0; i<m; ++i){
1903 for(PetscInt j=0; j<n; ++j){
1904 X(i,j,x(i,PETSC_TRUE));
1905 Y(i,j,y(j,PETSC_TRUE));
1910 return std::make_tuple(X,Y);
1926 const std::string filename
1930 PetscErrorCode ierr;
1931 std::ifstream f(filename.c_str());
1933 ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename.c_str(),FILE_MODE_APPEND,&viewer);CHKERRXX(ierr);
1936 ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename.c_str(),FILE_MODE_WRITE,&viewer);CHKERRXX(ierr);
1939 ierr = MatView(A.
mat,viewer);CHKERRXX(ierr);
1940 ierr = PetscViewerDestroy(&viewer);CHKERRXX(ierr);
1945 const std::vector<SPIMat> &As,
1946 const std::string filename
1950 PetscErrorCode ierr;
1951 std::ifstream f(filename.c_str());
1953 ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename.c_str(),FILE_MODE_APPEND,&viewer);CHKERRXX(ierr);
1956 ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename.c_str(),FILE_MODE_WRITE,&viewer);CHKERRXX(ierr);
1959 for(
unsigned i=0; i<As.size(); ++i){
1960 ierr = MatView(As[i].mat,viewer);CHKERRXX(ierr);
1962 ierr = PetscViewerDestroy(&viewer);CHKERRXX(ierr);
1968 const std::string filename
1972 A.
ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD, filename.c_str(), FILE_MODE_READ, &viewer); CHKERRXX(A.
ierr);
1974 A.
ierr = PetscViewerDestroy(&viewer); CHKERRXX(A.
ierr);
1979 std::vector<SPIMat> &As,
1980 const std::string filename
1984 PetscErrorCode ierr;
1985 ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD, filename.c_str(), FILE_MODE_READ, &viewer); CHKERRXX(ierr);
1986 for(
unsigned i=0; i<As.size(); ++i){
1987 ierr = MatLoad(As[i].mat,viewer); CHKERRXX(ierr);
1989 ierr = PetscViewerDestroy(&viewer); CHKERRXX(ierr);
1998 PetscErrorCode ierr;
1999 ierr = PetscViewerDrawOpen(PETSC_COMM_WORLD,NULL,A.
name.c_str(),PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,&viewer);CHKERRXX(ierr);
2000 ierr = MatView(A.
mat,viewer);CHKERRXX(ierr);
2003 SPI::printf(
" draw(SPIMat) with title=%s, hit ENTER to continue",A.
name.c_str());
2006 ierr = PetscViewerDestroy(&viewer);CHKERRXX(ierr);
2016 for (PetscInt i=0; i<out.
rows; ++i){
2017 for (PetscInt j=0; j<out.
cols; ++j){
2018 out(i,j,(*f)(A(i,j,PETSC_TRUE)));
2035 for (PetscInt i=0; i<out.
rows; ++i){
2036 for (PetscInt j=0; j<out.
cols; ++j){
2037 out(i,j,A(i,j,PETSC_TRUE)*B(i,j,PETSC_TRUE));
2047 for (PetscInt i=0; i<out.
rows; ++i){
2048 for (PetscInt j=0; j<out.
cols; ++j){
2049 out(i,j,
std::abs(A(i,j,PETSC_TRUE)));
2058 const std::vector<SPIVec> &x
2061 PetscInt m=x[0].rows;
2062 PetscInt n=x.size();
2064 for(PetscInt i=0; i<n; ++i) xvec[i]=x[i].vec;
2069 E.
ierr = BVCreate(PETSC_COMM_WORLD,&bv); CHKERRXX(E.
ierr);
2070 E.
ierr = BVSetSizesFromVec(bv,x[0].vec,n); CHKERRXX(E.
ierr);
2071 E.
ierr = BVSetFromOptions(bv);CHKERRXX(E.
ierr);
2072 E.
ierr = BVInsertVecs(bv,0,&n,xvec,PETSC_TRUE);
2075 E.
ierr = BVOrthogonalize(bv,PETSC_NULL); CHKERRXX(E.
ierr);
2077 E.
ierr = BVCreateMat(bv,&E.
mat); CHKERRXX(E.
ierr);
2080 E.
ierr = MatConvert(E.
mat,MATMPIAIJCUSPARSE,MAT_INPLACE_MATRIX,&E.
mat); CHKERRXX(E.
ierr);
2082 E.
ierr = MatConvert(E.
mat,MATMPIAIJ,MAT_INPLACE_MATRIX,&E.
mat); CHKERRXX(E.
ierr);