LCOV - code coverage report
Current view: top level - svd/impls/cross - cross.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 385 391 98.5 %
Date: 2024-12-18 00:51:33 Functions: 22 22 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       3             :    SLEPc - Scalable Library for Eigenvalue Problem Computations
       4             :    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
       5             : 
       6             :    This file is part of SLEPc.
       7             :    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
       8             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       9             : */
      10             : /*
      11             :    SLEPc singular value solver: "cross"
      12             : 
      13             :    Method: Uses a Hermitian eigensolver for A^T*A
      14             : */
      15             : 
      16             : #include <slepc/private/svdimpl.h>                /*I "slepcsvd.h" I*/
      17             : 
      18             : typedef struct {
      19             :   PetscBool explicitmatrix;
      20             :   EPS       eps;
      21             :   PetscBool usereps;
      22             :   Mat       C,D;
      23             : } SVD_CROSS;
      24             : 
      25             : typedef struct {
      26             :   Mat       A,AT;
      27             :   Vec       w,diag,omega;
      28             :   PetscBool swapped;
      29             : } SVD_CROSS_SHELL;
      30             : 
      31        9278 : static PetscErrorCode MatMult_Cross(Mat B,Vec x,Vec y)
      32             : {
      33        9278 :   SVD_CROSS_SHELL *ctx;
      34             : 
      35        9278 :   PetscFunctionBegin;
      36        9278 :   PetscCall(MatShellGetContext(B,&ctx));
      37        9278 :   PetscCall(MatMult(ctx->A,x,ctx->w));
      38        9278 :   if (ctx->omega && !ctx->swapped) PetscCall(VecPointwiseMult(ctx->w,ctx->w,ctx->omega));
      39        9278 :   PetscCall(MatMult(ctx->AT,ctx->w,y));
      40        9278 :   PetscFunctionReturn(PETSC_SUCCESS);
      41             : }
      42             : 
      43           8 : static PetscErrorCode MatGetDiagonal_Cross(Mat B,Vec d)
      44             : {
      45           8 :   SVD_CROSS_SHELL   *ctx;
      46           8 :   PetscMPIInt       len;
      47           8 :   PetscInt          N,n,i,j,start,end,ncols;
      48           8 :   PetscScalar       *work1,*work2,*diag;
      49           8 :   const PetscInt    *cols;
      50           8 :   const PetscScalar *vals;
      51             : 
      52           8 :   PetscFunctionBegin;
      53           8 :   PetscCall(MatShellGetContext(B,&ctx));
      54           8 :   if (!ctx->diag) {
      55             :     /* compute diagonal from rows and store in ctx->diag */
      56           8 :     PetscCall(VecDuplicate(d,&ctx->diag));
      57           8 :     PetscCall(MatGetSize(ctx->A,NULL,&N));
      58           8 :     PetscCall(MatGetLocalSize(ctx->A,NULL,&n));
      59           8 :     PetscCall(PetscCalloc2(N,&work1,N,&work2));
      60           8 :     if (ctx->swapped) {
      61           2 :       PetscCall(MatGetOwnershipRange(ctx->AT,&start,&end));
      62          42 :       for (i=start;i<end;i++) {
      63          40 :         PetscCall(MatGetRow(ctx->AT,i,&ncols,NULL,&vals));
      64         120 :         for (j=0;j<ncols;j++) work1[i] += vals[j]*vals[j];
      65          40 :         PetscCall(MatRestoreRow(ctx->AT,i,&ncols,NULL,&vals));
      66             :       }
      67             :     } else {
      68           6 :       PetscCall(MatGetOwnershipRange(ctx->A,&start,&end));
      69         137 :       for (i=start;i<end;i++) {
      70         131 :         PetscCall(MatGetRow(ctx->A,i,&ncols,&cols,&vals));
      71         894 :         for (j=0;j<ncols;j++) work1[cols[j]] += vals[j]*vals[j];
      72         131 :         PetscCall(MatRestoreRow(ctx->A,i,&ncols,&cols,&vals));
      73             :       }
      74             :     }
      75           8 :     PetscCall(PetscMPIIntCast(N,&len));
      76          24 :     PetscCallMPI(MPIU_Allreduce(work1,work2,len,MPIU_SCALAR,MPIU_SUM,PetscObjectComm((PetscObject)B)));
      77           8 :     PetscCall(VecGetOwnershipRange(ctx->diag,&start,&end));
      78           8 :     PetscCall(VecGetArrayWrite(ctx->diag,&diag));
      79         163 :     for (i=start;i<end;i++) diag[i-start] = work2[i];
      80           8 :     PetscCall(VecRestoreArrayWrite(ctx->diag,&diag));
      81           8 :     PetscCall(PetscFree2(work1,work2));
      82             :   }
      83           8 :   PetscCall(VecCopy(ctx->diag,d));
      84           8 :   PetscFunctionReturn(PETSC_SUCCESS);
      85             : }
      86             : 
      87          43 : static PetscErrorCode MatDestroy_Cross(Mat B)
      88             : {
      89          43 :   SVD_CROSS_SHELL *ctx;
      90             : 
      91          43 :   PetscFunctionBegin;
      92          43 :   PetscCall(MatShellGetContext(B,&ctx));
      93          43 :   PetscCall(VecDestroy(&ctx->w));
      94          43 :   PetscCall(VecDestroy(&ctx->diag));
      95          43 :   PetscCall(PetscFree(ctx));
      96          43 :   PetscFunctionReturn(PETSC_SUCCESS);
      97             : }
      98             : 
      99          65 : static PetscErrorCode SVDCrossGetProductMat(SVD svd,Mat A,Mat AT,Mat *C)
     100             : {
     101          65 :   SVD_CROSS       *cross = (SVD_CROSS*)svd->data;
     102          65 :   SVD_CROSS_SHELL *ctx;
     103          65 :   PetscInt        n;
     104          65 :   VecType         vtype;
     105          65 :   Mat             B;
     106             : 
     107          65 :   PetscFunctionBegin;
     108          65 :   if (cross->explicitmatrix) {
     109          43 :     if (!svd->ishyperbolic || svd->swapped) B = (!svd->expltrans && svd->swapped)? AT: A;
     110             :     else {  /* duplicate A and scale by signature */
     111           1 :       PetscCall(MatDuplicate(A,MAT_COPY_VALUES,&B));
     112           1 :       PetscCall(MatDiagonalScale(B,svd->omega,NULL));
     113             :     }
     114          22 :     if (svd->expltrans) {  /* explicit transpose */
     115          22 :       PetscCall(MatProductCreate(AT,B,NULL,C));
     116          22 :       PetscCall(MatProductSetType(*C,MATPRODUCT_AB));
     117             :     } else {  /* implicit transpose */
     118             : #if defined(PETSC_USE_COMPLEX)
     119           0 :       SETERRQ(PetscObjectComm((PetscObject)svd),PETSC_ERR_SUP,"Must use explicit transpose with complex scalars");
     120             : #else
     121             :       if (!svd->swapped) {
     122             :         PetscCall(MatProductCreate(A,B,NULL,C));
     123             :         PetscCall(MatProductSetType(*C,MATPRODUCT_AtB));
     124             :       } else {
     125             :         PetscCall(MatProductCreate(B,AT,NULL,C));
     126             :         PetscCall(MatProductSetType(*C,MATPRODUCT_ABt));
     127             :       }
     128             : #endif
     129             :     }
     130          22 :     PetscCall(MatProductSetFromOptions(*C));
     131          22 :     PetscCall(MatProductSymbolic(*C));
     132          22 :     PetscCall(MatProductNumeric(*C));
     133          22 :     if (svd->ishyperbolic && !svd->swapped) PetscCall(MatDestroy(&B));
     134             :   } else {
     135          43 :     PetscCall(PetscNew(&ctx));
     136          43 :     ctx->A       = A;
     137          43 :     ctx->AT      = AT;
     138          43 :     ctx->omega   = svd->omega;
     139          43 :     ctx->swapped = svd->swapped;
     140          43 :     PetscCall(MatCreateVecs(A,NULL,&ctx->w));
     141          43 :     PetscCall(MatGetLocalSize(A,NULL,&n));
     142          43 :     PetscCall(MatCreateShell(PetscObjectComm((PetscObject)svd),n,n,PETSC_DETERMINE,PETSC_DETERMINE,(void*)ctx,C));
     143          43 :     PetscCall(MatShellSetOperation(*C,MATOP_MULT,(void(*)(void))MatMult_Cross));
     144          43 :     if (!svd->ishyperbolic || svd->swapped) PetscCall(MatShellSetOperation(*C,MATOP_GET_DIAGONAL,(void(*)(void))MatGetDiagonal_Cross));
     145          43 :     PetscCall(MatShellSetOperation(*C,MATOP_DESTROY,(void(*)(void))MatDestroy_Cross));
     146          43 :     PetscCall(MatGetVecType(A,&vtype));
     147          43 :     PetscCall(MatSetVecType(*C,vtype));
     148             :   }
     149          65 :   PetscFunctionReturn(PETSC_SUCCESS);
     150             : }
     151             : 
     152             : /* Convergence test relative to the norm of R (used in GSVD only) */
     153         130 : static PetscErrorCode EPSConv_Cross(EPS eps,PetscScalar eigr,PetscScalar eigi,PetscReal res,PetscReal *errest,void *ctx)
     154             : {
     155         130 :   SVD svd = (SVD)ctx;
     156             : 
     157         130 :   PetscFunctionBegin;
     158         130 :   *errest = res/PetscMax(svd->nrma,svd->nrmb);
     159         130 :   PetscFunctionReturn(PETSC_SUCCESS);
     160             : }
     161             : 
     162          52 : static PetscErrorCode SVDSetUp_Cross(SVD svd)
     163             : {
     164          52 :   SVD_CROSS      *cross = (SVD_CROSS*)svd->data;
     165          52 :   ST             st;
     166          52 :   PetscBool      trackall,issinv,isks;
     167          52 :   EPSProblemType ptype;
     168          52 :   EPSWhich       which;
     169          52 :   Mat            Omega;
     170          52 :   MatType        Atype;
     171          52 :   PetscInt       n,N;
     172             : 
     173          52 :   PetscFunctionBegin;
     174          52 :   if (svd->nsv==0 && svd->stop!=SVD_STOP_THRESHOLD) svd->nsv = 1;
     175          52 :   if (!cross->eps) PetscCall(SVDCrossGetEPS(svd,&cross->eps));
     176          52 :   PetscCall(MatDestroy(&cross->C));
     177          52 :   PetscCall(MatDestroy(&cross->D));
     178          52 :   PetscCall(SVDCrossGetProductMat(svd,svd->A,svd->AT,&cross->C));
     179          52 :   if (svd->isgeneralized) {
     180          13 :     PetscCall(SVDCrossGetProductMat(svd,svd->B,svd->BT,&cross->D));
     181          13 :     PetscCall(EPSSetOperators(cross->eps,cross->C,cross->D));
     182          13 :     PetscCall(EPSGetProblemType(cross->eps,&ptype));
     183          13 :     if (!ptype) PetscCall(EPSSetProblemType(cross->eps,EPS_GHEP));
     184          39 :   } else if (svd->ishyperbolic && svd->swapped) {
     185           2 :     PetscCall(MatGetType(svd->OP,&Atype));
     186           2 :     PetscCall(MatGetSize(svd->A,NULL,&N));
     187           2 :     PetscCall(MatGetLocalSize(svd->A,NULL,&n));
     188           2 :     PetscCall(MatCreate(PetscObjectComm((PetscObject)svd),&Omega));
     189           2 :     PetscCall(MatSetSizes(Omega,n,n,N,N));
     190           2 :     PetscCall(MatSetType(Omega,Atype));
     191           2 :     PetscCall(MatDiagonalSet(Omega,svd->omega,INSERT_VALUES));
     192           2 :     PetscCall(EPSSetOperators(cross->eps,cross->C,Omega));
     193           2 :     PetscCall(EPSSetProblemType(cross->eps,EPS_GHIEP));
     194           2 :     PetscCall(MatDestroy(&Omega));
     195             :   } else {
     196          37 :     PetscCall(EPSSetOperators(cross->eps,cross->C,NULL));
     197          37 :     PetscCall(EPSSetProblemType(cross->eps,EPS_HEP));
     198             :   }
     199          52 :   if (!cross->usereps) {
     200          50 :     PetscCall(EPSGetST(cross->eps,&st));
     201          50 :     PetscCall(PetscObjectTypeCompare((PetscObject)st,STSINVERT,&issinv));
     202          50 :     PetscCall(PetscObjectTypeCompare((PetscObject)cross->eps,EPSKRYLOVSCHUR,&isks));
     203          50 :     if (svd->isgeneralized && svd->which==SVD_SMALLEST) {
     204           2 :       if (cross->explicitmatrix && isks && !issinv) {  /* default to shift-and-invert */
     205           1 :         PetscCall(STSetType(st,STSINVERT));
     206           1 :         PetscCall(EPSSetTarget(cross->eps,0.0));
     207             :         which = EPS_TARGET_REAL;
     208           1 :       } else which = issinv?EPS_TARGET_REAL:EPS_SMALLEST_REAL;
     209             :     } else {
     210          48 :       if (issinv) which = EPS_TARGET_MAGNITUDE;
     211          48 :       else if (svd->ishyperbolic) which = svd->which==SVD_LARGEST?EPS_LARGEST_MAGNITUDE:EPS_SMALLEST_MAGNITUDE;
     212          40 :       else which = svd->which==SVD_LARGEST?EPS_LARGEST_MAGNITUDE:EPS_SMALLEST_MAGNITUDE;
     213             :     }
     214          50 :     PetscCall(EPSSetWhichEigenpairs(cross->eps,which));
     215          96 :     PetscCall(EPSSetDimensions(cross->eps,svd->nsv?svd->nsv:PETSC_CURRENT,svd->ncv,svd->mpd));
     216          50 :     if (svd->stop==SVD_STOP_THRESHOLD) PetscCall(EPSSetThreshold(cross->eps,svd->thres*svd->thres,svd->threlative));
     217          68 :     PetscCall(EPSSetTolerances(cross->eps,svd->tol==(PetscReal)PETSC_DETERMINE?SLEPC_DEFAULT_TOL/10.0:svd->tol,svd->max_it));
     218          50 :     switch (svd->conv) {
     219           2 :     case SVD_CONV_ABS:
     220           2 :       PetscCall(EPSSetConvergenceTest(cross->eps,EPS_CONV_ABS));break;
     221          35 :     case SVD_CONV_REL:
     222          35 :       PetscCall(EPSSetConvergenceTest(cross->eps,EPS_CONV_REL));break;
     223          13 :     case SVD_CONV_NORM:
     224          13 :       if (svd->isgeneralized) {
     225          13 :         if (!svd->nrma) PetscCall(MatNorm(svd->OP,NORM_INFINITY,&svd->nrma));
     226          13 :         if (!svd->nrmb) PetscCall(MatNorm(svd->OPb,NORM_INFINITY,&svd->nrmb));
     227          13 :         PetscCall(EPSSetConvergenceTestFunction(cross->eps,EPSConv_Cross,svd,NULL));
     228             :       } else {
     229           0 :         PetscCall(EPSSetConvergenceTest(cross->eps,EPS_CONV_NORM));break;
     230             :       }
     231             :       break;
     232           0 :     case SVD_CONV_MAXIT:
     233           0 :       SETERRQ(PetscObjectComm((PetscObject)svd),PETSC_ERR_SUP,"Maxit convergence test not supported in this solver");
     234           0 :     case SVD_CONV_USER:
     235           0 :       SETERRQ(PetscObjectComm((PetscObject)svd),PETSC_ERR_SUP,"User-defined convergence test not supported in this solver");
     236             :     }
     237           2 :   }
     238             :   /* Transfer the trackall option from svd to eps */
     239          52 :   PetscCall(SVDGetTrackAll(svd,&trackall));
     240          52 :   PetscCall(EPSSetTrackAll(cross->eps,trackall));
     241             :   /* Transfer the initial space from svd to eps */
     242          52 :   if (svd->nini<0) {
     243           4 :     PetscCall(EPSSetInitialSpace(cross->eps,-svd->nini,svd->IS));
     244           4 :     PetscCall(SlepcBasisDestroy_Private(&svd->nini,&svd->IS));
     245             :   }
     246          52 :   PetscCall(EPSSetUp(cross->eps));
     247          52 :   PetscCall(EPSGetDimensions(cross->eps,NULL,&svd->ncv,&svd->mpd));
     248          52 :   PetscCall(EPSGetTolerances(cross->eps,NULL,&svd->max_it));
     249          52 :   if (svd->tol==(PetscReal)PETSC_DETERMINE) svd->tol = SLEPC_DEFAULT_TOL;
     250             : 
     251          52 :   svd->leftbasis = PETSC_FALSE;
     252          52 :   PetscCall(SVDAllocateSolution(svd,0));
     253          52 :   PetscFunctionReturn(PETSC_SUCCESS);
     254             : }
     255             : 
     256          52 : static PetscErrorCode SVDSolve_Cross(SVD svd)
     257             : {
     258          52 :   SVD_CROSS      *cross = (SVD_CROSS*)svd->data;
     259          52 :   PetscInt       i;
     260          52 :   PetscScalar    lambda;
     261          52 :   PetscReal      sigma;
     262             : 
     263          52 :   PetscFunctionBegin;
     264          52 :   PetscCall(EPSSolve(cross->eps));
     265          52 :   PetscCall(EPSGetConverged(cross->eps,&svd->nconv));
     266          52 :   PetscCall(EPSGetIterationNumber(cross->eps,&svd->its));
     267          52 :   PetscCall(EPSGetConvergedReason(cross->eps,(EPSConvergedReason*)&svd->reason));
     268         631 :   for (i=0;i<svd->nconv;i++) {
     269         579 :     PetscCall(EPSGetEigenvalue(cross->eps,i,&lambda,NULL));
     270         579 :     sigma = PetscRealPart(lambda);
     271         579 :     if (svd->ishyperbolic) svd->sigma[i] = PetscSqrtReal(PetscAbsReal(sigma));
     272             :     else {
     273         334 :       PetscCheck(sigma>-10*PETSC_MACHINE_EPSILON,PetscObjectComm((PetscObject)svd),PETSC_ERR_FP,"Negative eigenvalue computed by EPS: %g",(double)sigma);
     274         334 :       if (sigma<0.0) {
     275           3 :         PetscCall(PetscInfo(svd,"Negative eigenvalue computed by EPS: %g, resetting to 0\n",(double)sigma));
     276             :         sigma = 0.0;
     277             :       }
     278         334 :       svd->sigma[i] = PetscSqrtReal(sigma);
     279             :     }
     280             :   }
     281          52 :   PetscFunctionReturn(PETSC_SUCCESS);
     282             : }
     283             : 
     284          46 : static PetscErrorCode SVDComputeVectors_Cross(SVD svd)
     285             : {
     286          46 :   SVD_CROSS         *cross = (SVD_CROSS*)svd->data;
     287          46 :   PetscInt          i,mloc,ploc;
     288          46 :   Vec               u,v,x,uv,w,omega2=NULL;
     289          46 :   Mat               Omega;
     290          46 :   PetscScalar       *dst,alpha,lambda,*varray;
     291          46 :   const PetscScalar *src;
     292          46 :   PetscReal         nrm;
     293             : 
     294          46 :   PetscFunctionBegin;
     295          46 :   if (svd->isgeneralized) {
     296          13 :     PetscCall(MatCreateVecs(svd->A,NULL,&u));
     297          13 :     PetscCall(VecGetLocalSize(u,&mloc));
     298          13 :     PetscCall(MatCreateVecs(svd->B,NULL,&v));
     299          13 :     PetscCall(VecGetLocalSize(v,&ploc));
     300         119 :     for (i=0;i<svd->nconv;i++) {
     301         106 :       PetscCall(BVGetColumn(svd->V,i,&x));
     302         106 :       PetscCall(EPSGetEigenpair(cross->eps,i,&lambda,NULL,x,NULL));
     303         106 :       PetscCall(MatMult(svd->A,x,u));     /* u_i*c_i/alpha = A*x_i */
     304         106 :       PetscCall(VecNormalize(u,NULL));
     305         106 :       PetscCall(MatMult(svd->B,x,v));     /* v_i*s_i/alpha = B*x_i */
     306         106 :       PetscCall(VecNormalize(v,&nrm));    /* ||v||_2 = s_i/alpha   */
     307         106 :       alpha = 1.0/(PetscSqrtReal(1.0+PetscRealPart(lambda))*nrm);    /* alpha=s_i/||v||_2 */
     308         106 :       PetscCall(VecScale(x,alpha));
     309         106 :       PetscCall(BVRestoreColumn(svd->V,i,&x));
     310             :       /* copy [u;v] to U[i] */
     311         106 :       PetscCall(BVGetColumn(svd->U,i,&uv));
     312         106 :       PetscCall(VecGetArrayWrite(uv,&dst));
     313         106 :       PetscCall(VecGetArrayRead(u,&src));
     314         106 :       PetscCall(PetscArraycpy(dst,src,mloc));
     315         106 :       PetscCall(VecRestoreArrayRead(u,&src));
     316         106 :       PetscCall(VecGetArrayRead(v,&src));
     317         106 :       PetscCall(PetscArraycpy(dst+mloc,src,ploc));
     318         106 :       PetscCall(VecRestoreArrayRead(v,&src));
     319         106 :       PetscCall(VecRestoreArrayWrite(uv,&dst));
     320         106 :       PetscCall(BVRestoreColumn(svd->U,i,&uv));
     321             :     }
     322          13 :     PetscCall(VecDestroy(&v));
     323          13 :     PetscCall(VecDestroy(&u));
     324          33 :   } else if (svd->ishyperbolic && svd->swapped) {  /* was solved as GHIEP, set u=Omega*u and normalize */
     325           2 :     PetscCall(EPSGetOperators(cross->eps,NULL,&Omega));
     326           2 :     PetscCall(MatCreateVecs(Omega,&w,NULL));
     327           2 :     PetscCall(VecCreateSeq(PETSC_COMM_SELF,svd->ncv,&omega2));
     328           2 :     PetscCall(VecGetArrayWrite(omega2,&varray));
     329          58 :     for (i=0;i<svd->nconv;i++) {
     330          56 :       PetscCall(BVGetColumn(svd->V,i,&v));
     331          56 :       PetscCall(EPSGetEigenvector(cross->eps,i,v,NULL));
     332          56 :       PetscCall(MatMult(Omega,v,w));
     333          56 :       PetscCall(VecDot(v,w,&alpha));
     334          56 :       svd->sign[i] = PetscSign(PetscRealPart(alpha));
     335          56 :       varray[i] = svd->sign[i];
     336          56 :       alpha = 1.0/PetscSqrtScalar(PetscAbsScalar(alpha));
     337          56 :       PetscCall(VecScale(w,alpha));
     338          56 :       PetscCall(VecCopy(w,v));
     339          56 :       PetscCall(BVRestoreColumn(svd->V,i,&v));
     340             :     }
     341           2 :     PetscCall(BVSetSignature(svd->V,omega2));
     342           2 :     PetscCall(VecRestoreArrayWrite(omega2,&varray));
     343           2 :     PetscCall(VecDestroy(&omega2));
     344           2 :     PetscCall(VecDestroy(&w));
     345           2 :     PetscCall(SVDComputeVectors_Left(svd));
     346             :   } else {
     347         432 :     for (i=0;i<svd->nconv;i++) {
     348         401 :       PetscCall(BVGetColumn(svd->V,i,&v));
     349         401 :       PetscCall(EPSGetEigenvector(cross->eps,i,v,NULL));
     350         401 :       PetscCall(BVRestoreColumn(svd->V,i,&v));
     351             :     }
     352          31 :     PetscCall(SVDComputeVectors_Left(svd));
     353             :   }
     354          46 :   PetscFunctionReturn(PETSC_SUCCESS);
     355             : }
     356             : 
     357         213 : static PetscErrorCode EPSMonitor_Cross(EPS eps,PetscInt its,PetscInt nconv,PetscScalar *eigr,PetscScalar *eigi,PetscReal *errest,PetscInt nest,void *ctx)
     358             : {
     359         213 :   PetscInt       i,ncv;
     360         213 :   SVD            svd = (SVD)ctx;
     361         213 :   SVD_CROSS      *cross;
     362         213 :   PetscScalar    er,ei;
     363         213 :   ST             st;
     364             : 
     365         213 :   PetscFunctionBegin;
     366         213 :   if (svd->stop==SVD_STOP_THRESHOLD) {
     367          19 :     cross = (SVD_CROSS*)svd->data;
     368          19 :     PetscCall(EPSGetDimensions(cross->eps,NULL,&ncv,NULL));
     369          19 :     if (ncv!=svd->ncv) {  /* reallocate */
     370           1 :       PetscCall(SVDReallocateSolution(svd,ncv));
     371           7 :       for (i=svd->ncv;i<ncv;i++) svd->perm[i] = i;
     372           1 :       svd->ncv = ncv;
     373             :     }
     374             :   }
     375         213 :   PetscCall(EPSGetST(eps,&st));
     376        3228 :   for (i=0;i<PetscMin(nest,svd->ncv);i++) {
     377        3015 :     er = eigr[i]; ei = eigi[i];
     378        3015 :     PetscCall(STBackTransform(st,1,&er,&ei));
     379        3015 :     svd->sigma[i] = PetscSqrtReal(PetscAbsReal(PetscRealPart(er)));
     380        3015 :     svd->errest[i] = errest[i];
     381             :   }
     382         213 :   PetscCall(SVDMonitor(svd,its,nconv,svd->sigma,svd->errest,nest));
     383         213 :   PetscFunctionReturn(PETSC_SUCCESS);
     384             : }
     385             : 
     386          39 : static PetscErrorCode SVDSetFromOptions_Cross(SVD svd,PetscOptionItems *PetscOptionsObject)
     387             : {
     388          39 :   PetscBool      set,val;
     389          39 :   SVD_CROSS      *cross = (SVD_CROSS*)svd->data;
     390          39 :   ST             st;
     391             : 
     392          39 :   PetscFunctionBegin;
     393          39 :   PetscOptionsHeadBegin(PetscOptionsObject,"SVD Cross Options");
     394             : 
     395          39 :     PetscCall(PetscOptionsBool("-svd_cross_explicitmatrix","Use cross explicit matrix","SVDCrossSetExplicitMatrix",cross->explicitmatrix,&val,&set));
     396          39 :     if (set) PetscCall(SVDCrossSetExplicitMatrix(svd,val));
     397             : 
     398          39 :   PetscOptionsHeadEnd();
     399             : 
     400          39 :   if (!cross->eps) PetscCall(SVDCrossGetEPS(svd,&cross->eps));
     401          39 :   if (!cross->explicitmatrix && !cross->usereps) {
     402             :     /* use as default an ST with shell matrix and Jacobi */
     403          25 :     PetscCall(EPSGetST(cross->eps,&st));
     404          25 :     PetscCall(STSetMatMode(st,ST_MATMODE_SHELL));
     405             :   }
     406          39 :   PetscCall(EPSSetFromOptions(cross->eps));
     407          39 :   PetscFunctionReturn(PETSC_SUCCESS);
     408             : }
     409             : 
     410          19 : static PetscErrorCode SVDCrossSetExplicitMatrix_Cross(SVD svd,PetscBool explicitmatrix)
     411             : {
     412          19 :   SVD_CROSS *cross = (SVD_CROSS*)svd->data;
     413             : 
     414          19 :   PetscFunctionBegin;
     415          19 :   if (cross->explicitmatrix != explicitmatrix) {
     416          12 :     cross->explicitmatrix = explicitmatrix;
     417          12 :     svd->state = SVD_STATE_INITIAL;
     418             :   }
     419          19 :   PetscFunctionReturn(PETSC_SUCCESS);
     420             : }
     421             : 
     422             : /*@
     423             :    SVDCrossSetExplicitMatrix - Indicate if the eigensolver operator A^T*A must
     424             :    be computed explicitly.
     425             : 
     426             :    Logically Collective
     427             : 
     428             :    Input Parameters:
     429             : +  svd         - singular value solver
     430             : -  explicitmat - boolean flag indicating if A^T*A is built explicitly
     431             : 
     432             :    Options Database Key:
     433             : .  -svd_cross_explicitmatrix <boolean> - Indicates the boolean flag
     434             : 
     435             :    Level: advanced
     436             : 
     437             : .seealso: SVDCrossGetExplicitMatrix()
     438             : @*/
     439          19 : PetscErrorCode SVDCrossSetExplicitMatrix(SVD svd,PetscBool explicitmat)
     440             : {
     441          19 :   PetscFunctionBegin;
     442          19 :   PetscValidHeaderSpecific(svd,SVD_CLASSID,1);
     443          57 :   PetscValidLogicalCollectiveBool(svd,explicitmat,2);
     444          19 :   PetscTryMethod(svd,"SVDCrossSetExplicitMatrix_C",(SVD,PetscBool),(svd,explicitmat));
     445          19 :   PetscFunctionReturn(PETSC_SUCCESS);
     446             : }
     447             : 
     448           2 : static PetscErrorCode SVDCrossGetExplicitMatrix_Cross(SVD svd,PetscBool *explicitmat)
     449             : {
     450           2 :   SVD_CROSS *cross = (SVD_CROSS*)svd->data;
     451             : 
     452           2 :   PetscFunctionBegin;
     453           2 :   *explicitmat = cross->explicitmatrix;
     454           2 :   PetscFunctionReturn(PETSC_SUCCESS);
     455             : }
     456             : 
     457             : /*@
     458             :    SVDCrossGetExplicitMatrix - Returns the flag indicating if A^T*A is built explicitly.
     459             : 
     460             :    Not Collective
     461             : 
     462             :    Input Parameter:
     463             : .  svd  - singular value solver
     464             : 
     465             :    Output Parameter:
     466             : .  explicitmat - the mode flag
     467             : 
     468             :    Level: advanced
     469             : 
     470             : .seealso: SVDCrossSetExplicitMatrix()
     471             : @*/
     472           2 : PetscErrorCode SVDCrossGetExplicitMatrix(SVD svd,PetscBool *explicitmat)
     473             : {
     474           2 :   PetscFunctionBegin;
     475           2 :   PetscValidHeaderSpecific(svd,SVD_CLASSID,1);
     476           2 :   PetscAssertPointer(explicitmat,2);
     477           2 :   PetscUseMethod(svd,"SVDCrossGetExplicitMatrix_C",(SVD,PetscBool*),(svd,explicitmat));
     478           2 :   PetscFunctionReturn(PETSC_SUCCESS);
     479             : }
     480             : 
     481           2 : static PetscErrorCode SVDCrossSetEPS_Cross(SVD svd,EPS eps)
     482             : {
     483           2 :   SVD_CROSS      *cross = (SVD_CROSS*)svd->data;
     484             : 
     485           2 :   PetscFunctionBegin;
     486           2 :   PetscCall(PetscObjectReference((PetscObject)eps));
     487           2 :   PetscCall(EPSDestroy(&cross->eps));
     488           2 :   cross->eps     = eps;
     489           2 :   cross->usereps = PETSC_TRUE;
     490           2 :   svd->state     = SVD_STATE_INITIAL;
     491           2 :   PetscFunctionReturn(PETSC_SUCCESS);
     492             : }
     493             : 
     494             : /*@
     495             :    SVDCrossSetEPS - Associate an eigensolver object (EPS) to the
     496             :    singular value solver.
     497             : 
     498             :    Collective
     499             : 
     500             :    Input Parameters:
     501             : +  svd - singular value solver
     502             : -  eps - the eigensolver object
     503             : 
     504             :    Level: advanced
     505             : 
     506             : .seealso: SVDCrossGetEPS()
     507             : @*/
     508           2 : PetscErrorCode SVDCrossSetEPS(SVD svd,EPS eps)
     509             : {
     510           2 :   PetscFunctionBegin;
     511           2 :   PetscValidHeaderSpecific(svd,SVD_CLASSID,1);
     512           2 :   PetscValidHeaderSpecific(eps,EPS_CLASSID,2);
     513           2 :   PetscCheckSameComm(svd,1,eps,2);
     514           2 :   PetscTryMethod(svd,"SVDCrossSetEPS_C",(SVD,EPS),(svd,eps));
     515           2 :   PetscFunctionReturn(PETSC_SUCCESS);
     516             : }
     517             : 
     518          39 : static PetscErrorCode SVDCrossGetEPS_Cross(SVD svd,EPS *eps)
     519             : {
     520          39 :   SVD_CROSS      *cross = (SVD_CROSS*)svd->data;
     521             : 
     522          39 :   PetscFunctionBegin;
     523          39 :   if (!cross->eps) {
     524          39 :     PetscCall(EPSCreate(PetscObjectComm((PetscObject)svd),&cross->eps));
     525          39 :     PetscCall(PetscObjectIncrementTabLevel((PetscObject)cross->eps,(PetscObject)svd,1));
     526          39 :     PetscCall(EPSSetOptionsPrefix(cross->eps,((PetscObject)svd)->prefix));
     527          39 :     PetscCall(EPSAppendOptionsPrefix(cross->eps,"svd_cross_"));
     528          39 :     PetscCall(PetscObjectSetOptions((PetscObject)cross->eps,((PetscObject)svd)->options));
     529          39 :     PetscCall(EPSSetWhichEigenpairs(cross->eps,EPS_LARGEST_MAGNITUDE));
     530          39 :     PetscCall(EPSMonitorSet(cross->eps,EPSMonitor_Cross,svd,NULL));
     531             :   }
     532          39 :   *eps = cross->eps;
     533          39 :   PetscFunctionReturn(PETSC_SUCCESS);
     534             : }
     535             : 
     536             : /*@
     537             :    SVDCrossGetEPS - Retrieve the eigensolver object (EPS) associated
     538             :    to the singular value solver.
     539             : 
     540             :    Collective
     541             : 
     542             :    Input Parameter:
     543             : .  svd - singular value solver
     544             : 
     545             :    Output Parameter:
     546             : .  eps - the eigensolver object
     547             : 
     548             :    Level: advanced
     549             : 
     550             : .seealso: SVDCrossSetEPS()
     551             : @*/
     552          39 : PetscErrorCode SVDCrossGetEPS(SVD svd,EPS *eps)
     553             : {
     554          39 :   PetscFunctionBegin;
     555          39 :   PetscValidHeaderSpecific(svd,SVD_CLASSID,1);
     556          39 :   PetscAssertPointer(eps,2);
     557          39 :   PetscUseMethod(svd,"SVDCrossGetEPS_C",(SVD,EPS*),(svd,eps));
     558          39 :   PetscFunctionReturn(PETSC_SUCCESS);
     559             : }
     560             : 
     561           1 : static PetscErrorCode SVDView_Cross(SVD svd,PetscViewer viewer)
     562             : {
     563           1 :   SVD_CROSS      *cross = (SVD_CROSS*)svd->data;
     564           1 :   PetscBool      isascii;
     565             : 
     566           1 :   PetscFunctionBegin;
     567           1 :   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii));
     568           1 :   if (isascii) {
     569           1 :     if (!cross->eps) PetscCall(SVDCrossGetEPS(svd,&cross->eps));
     570           2 :     PetscCall(PetscViewerASCIIPrintf(viewer,"  %s matrix\n",cross->explicitmatrix?"explicit":"implicit"));
     571           1 :     PetscCall(PetscViewerASCIIPushTab(viewer));
     572           1 :     PetscCall(EPSView(cross->eps,viewer));
     573           1 :     PetscCall(PetscViewerASCIIPopTab(viewer));
     574             :   }
     575           1 :   PetscFunctionReturn(PETSC_SUCCESS);
     576             : }
     577             : 
     578          42 : static PetscErrorCode SVDReset_Cross(SVD svd)
     579             : {
     580          42 :   SVD_CROSS      *cross = (SVD_CROSS*)svd->data;
     581             : 
     582          42 :   PetscFunctionBegin;
     583          42 :   PetscCall(EPSReset(cross->eps));
     584          42 :   PetscCall(MatDestroy(&cross->C));
     585          42 :   PetscCall(MatDestroy(&cross->D));
     586          42 :   PetscFunctionReturn(PETSC_SUCCESS);
     587             : }
     588             : 
     589          41 : static PetscErrorCode SVDDestroy_Cross(SVD svd)
     590             : {
     591          41 :   SVD_CROSS      *cross = (SVD_CROSS*)svd->data;
     592             : 
     593          41 :   PetscFunctionBegin;
     594          41 :   PetscCall(EPSDestroy(&cross->eps));
     595          41 :   PetscCall(PetscFree(svd->data));
     596          41 :   PetscCall(PetscObjectComposeFunction((PetscObject)svd,"SVDCrossSetEPS_C",NULL));
     597          41 :   PetscCall(PetscObjectComposeFunction((PetscObject)svd,"SVDCrossGetEPS_C",NULL));
     598          41 :   PetscCall(PetscObjectComposeFunction((PetscObject)svd,"SVDCrossSetExplicitMatrix_C",NULL));
     599          41 :   PetscCall(PetscObjectComposeFunction((PetscObject)svd,"SVDCrossGetExplicitMatrix_C",NULL));
     600          41 :   PetscFunctionReturn(PETSC_SUCCESS);
     601             : }
     602             : 
     603          41 : SLEPC_EXTERN PetscErrorCode SVDCreate_Cross(SVD svd)
     604             : {
     605          41 :   SVD_CROSS      *cross;
     606             : 
     607          41 :   PetscFunctionBegin;
     608          41 :   PetscCall(PetscNew(&cross));
     609          41 :   svd->data = (void*)cross;
     610             : 
     611          41 :   svd->ops->solve          = SVDSolve_Cross;
     612          41 :   svd->ops->solveg         = SVDSolve_Cross;
     613          41 :   svd->ops->solveh         = SVDSolve_Cross;
     614          41 :   svd->ops->setup          = SVDSetUp_Cross;
     615          41 :   svd->ops->setfromoptions = SVDSetFromOptions_Cross;
     616          41 :   svd->ops->destroy        = SVDDestroy_Cross;
     617          41 :   svd->ops->reset          = SVDReset_Cross;
     618          41 :   svd->ops->view           = SVDView_Cross;
     619          41 :   svd->ops->computevectors = SVDComputeVectors_Cross;
     620          41 :   PetscCall(PetscObjectComposeFunction((PetscObject)svd,"SVDCrossSetEPS_C",SVDCrossSetEPS_Cross));
     621          41 :   PetscCall(PetscObjectComposeFunction((PetscObject)svd,"SVDCrossGetEPS_C",SVDCrossGetEPS_Cross));
     622          41 :   PetscCall(PetscObjectComposeFunction((PetscObject)svd,"SVDCrossSetExplicitMatrix_C",SVDCrossSetExplicitMatrix_Cross));
     623          41 :   PetscCall(PetscObjectComposeFunction((PetscObject)svd,"SVDCrossGetExplicitMatrix_C",SVDCrossGetExplicitMatrix_Cross));
     624          41 :   PetscFunctionReturn(PETSC_SUCCESS);
     625             : }

Generated by: LCOV version 1.14