xref: /petsc/src/ksp/pc/impls/svd/svd.c (revision a3c4e3ec3c23131fe52f9bf5f24f7affb73f344d)
127c67122SBarry Smith #define PETSCKSP_DLL
227c67122SBarry Smith 
327c67122SBarry Smith #include "private/pcimpl.h"   /*I "petscpc.h" I*/
427c67122SBarry Smith #include "petscblaslapack.h"
527c67122SBarry Smith 
627c67122SBarry Smith /*
727c67122SBarry Smith    Private context (data structure) for the SVD preconditioner.
827c67122SBarry Smith */
927c67122SBarry Smith typedef struct {
1027c67122SBarry Smith   Vec        diag,work;
1127c67122SBarry Smith   Mat        A,U,V;
1227c67122SBarry Smith   PetscInt   nzero;
1327c67122SBarry Smith } PC_SVD;
1427c67122SBarry Smith 
1527c67122SBarry Smith 
1627c67122SBarry Smith /* -------------------------------------------------------------------------- */
1727c67122SBarry Smith /*
1827c67122SBarry Smith    PCSetUp_SVD - Prepares for the use of the SVD preconditioner
1927c67122SBarry Smith                     by setting data structures and options.
2027c67122SBarry Smith 
2127c67122SBarry Smith    Input Parameter:
2227c67122SBarry Smith .  pc - the preconditioner context
2327c67122SBarry Smith 
2427c67122SBarry Smith    Application Interface Routine: PCSetUp()
2527c67122SBarry Smith 
2627c67122SBarry Smith    Notes:
2727c67122SBarry Smith    The interface routine PCSetUp() is not usually called directly by
2827c67122SBarry Smith    the user, but instead is called by PCApply() if necessary.
2927c67122SBarry Smith */
3027c67122SBarry Smith #undef __FUNCT__
3127c67122SBarry Smith #define __FUNCT__ "PCSetUp_SVD"
3227c67122SBarry Smith static PetscErrorCode PCSetUp_SVD(PC pc)
3327c67122SBarry Smith {
34*a3c4e3ecSJed Brown #if defined(PETSC_MISSING_LAPACK_GESVD)
35*a3c4e3ecSJed Brown   SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_SUP,"GESVD - Lapack routine is unavailable\nNot able to provide singular value estimates.");
36*a3c4e3ecSJed Brown #else
3727c67122SBarry Smith   PC_SVD         *jac = (PC_SVD*)pc->data;
3827c67122SBarry Smith   PetscErrorCode ierr;
3927c67122SBarry Smith   PetscScalar    *a,*u,*v,*d,*work;
4027c67122SBarry Smith   PetscBLASInt   nb,lwork,lierr;
4127c67122SBarry Smith   PetscInt       i,n;
4227c67122SBarry Smith 
4327c67122SBarry Smith   PetscFunctionBegin;
4427c67122SBarry Smith   if (!jac->diag) {
4527c67122SBarry Smith     /* assume square matrices */
4627c67122SBarry Smith     ierr = MatGetVecs(pc->mat,&jac->diag,&jac->work);CHKERRQ(ierr);
4727c67122SBarry Smith   }
4827c67122SBarry Smith   if (jac->A) {
4927c67122SBarry Smith     ierr = MatDestroy(jac->A);CHKERRQ(ierr);
5027c67122SBarry Smith   }
5127c67122SBarry Smith   ierr = MatConvert(pc->mat,MATSEQDENSE,MAT_INITIAL_MATRIX,&jac->A);CHKERRQ(ierr);
5227c67122SBarry Smith   if (!jac->U) {
5327c67122SBarry Smith     ierr = MatDuplicate(jac->A,MAT_DO_NOT_COPY_VALUES,&jac->U);CHKERRQ(ierr);
5427c67122SBarry Smith     ierr = MatDuplicate(jac->A,MAT_DO_NOT_COPY_VALUES,&jac->V);CHKERRQ(ierr);
5527c67122SBarry Smith   }
5627c67122SBarry Smith   ierr = MatGetSize(pc->mat,&n,PETSC_NULL);CHKERRQ(ierr);
5727c67122SBarry Smith   nb    = PetscBLASIntCast(n);
5827c67122SBarry Smith   lwork = 5*nb;
5927c67122SBarry Smith   ierr  = PetscMalloc(lwork*sizeof(PetscScalar),&work);CHKERRQ(ierr);
6027c67122SBarry Smith   ierr  = MatGetArray(jac->A,&a);CHKERRQ(ierr);
6127c67122SBarry Smith   ierr  = MatGetArray(jac->U,&u);CHKERRQ(ierr);
6227c67122SBarry Smith   ierr  = MatGetArray(jac->V,&v);CHKERRQ(ierr);
6327c67122SBarry Smith   ierr  = VecGetArray(jac->diag,&d);CHKERRQ(ierr);
6427c67122SBarry Smith #if !defined(PETSC_USE_COMPLEX)
6527c67122SBarry Smith   LAPACKgesvd_("A","A",&nb,&nb,a,&nb,d,u,&nb,v,&nb,work,&lwork,&lierr);
6627c67122SBarry Smith #else
6727c67122SBarry Smith   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not coded for complex");
6827c67122SBarry Smith #endif
6927c67122SBarry Smith   if (lierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"gesv() error %d",lierr);
7027c67122SBarry Smith   ierr  = MatRestoreArray(jac->A,&a);CHKERRQ(ierr);
7127c67122SBarry Smith   ierr  = MatRestoreArray(jac->U,&u);CHKERRQ(ierr);
7227c67122SBarry Smith   ierr  = MatRestoreArray(jac->V,&v);CHKERRQ(ierr);
7327c67122SBarry Smith   jac->nzero = 0;
7427c67122SBarry Smith   for (i=0; i<n; i++) {
7527c67122SBarry Smith     if (PetscRealPart(d[i]) < 1.e-12) {jac->nzero = n - i;break;}
7627c67122SBarry Smith     d[i] = 1.0/d[i];
7727c67122SBarry Smith   }
7827c67122SBarry Smith   ierr = PetscInfo1(pc,"Number of zero or nearly singular values %D\n",jac->nzero);
7927c67122SBarry Smith   ierr = VecRestoreArray(jac->diag,&d);CHKERRQ(ierr);
8027c67122SBarry Smith #if defined(foo)
8127c67122SBarry Smith {
8227c67122SBarry Smith   PetscViewer viewer;
8327c67122SBarry Smith   ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,"joe",FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
8427c67122SBarry Smith   ierr = MatView(jac->A,viewer);CHKERRQ(ierr);
8527c67122SBarry Smith   ierr = MatView(jac->U,viewer);CHKERRQ(ierr);
8627c67122SBarry Smith   ierr = MatView(jac->V,viewer);CHKERRQ(ierr);
8727c67122SBarry Smith   ierr = VecView(jac->diag,viewer);CHKERRQ(ierr);
8827c67122SBarry Smith   ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
8927c67122SBarry Smith  }
9027c67122SBarry Smith #endif
9127c67122SBarry Smith   ierr = PetscFree(work);
9227c67122SBarry Smith   PetscFunctionReturn(0);
93*a3c4e3ecSJed Brown #endif
9427c67122SBarry Smith }
9527c67122SBarry Smith 
9627c67122SBarry Smith /* -------------------------------------------------------------------------- */
9727c67122SBarry Smith /*
9827c67122SBarry Smith    PCApply_SVD - Applies the SVD preconditioner to a vector.
9927c67122SBarry Smith 
10027c67122SBarry Smith    Input Parameters:
10127c67122SBarry Smith .  pc - the preconditioner context
10227c67122SBarry Smith .  x - input vector
10327c67122SBarry Smith 
10427c67122SBarry Smith    Output Parameter:
10527c67122SBarry Smith .  y - output vector
10627c67122SBarry Smith 
10727c67122SBarry Smith    Application Interface Routine: PCApply()
10827c67122SBarry Smith  */
10927c67122SBarry Smith #undef __FUNCT__
11027c67122SBarry Smith #define __FUNCT__ "PCApply_SVD"
11127c67122SBarry Smith static PetscErrorCode PCApply_SVD(PC pc,Vec x,Vec y)
11227c67122SBarry Smith {
11327c67122SBarry Smith   PC_SVD         *jac = (PC_SVD*)pc->data;
11427c67122SBarry Smith   Vec            work = jac->work;
11527c67122SBarry Smith   PetscErrorCode ierr;
11627c67122SBarry Smith 
11727c67122SBarry Smith   PetscFunctionBegin;
11827c67122SBarry Smith   ierr = MatMultTranspose(jac->U,x,work);CHKERRQ(ierr);
11927c67122SBarry Smith   ierr = VecPointwiseMult(work,work,jac->diag);CHKERRQ(ierr);
12027c67122SBarry Smith   ierr = MatMultTranspose(jac->V,work,y);CHKERRQ(ierr);
12127c67122SBarry Smith   PetscFunctionReturn(0);
12227c67122SBarry Smith }
12327c67122SBarry Smith 
12427c67122SBarry Smith /* -------------------------------------------------------------------------- */
12527c67122SBarry Smith /*
12627c67122SBarry Smith    PCDestroy_SVD - Destroys the private context for the SVD preconditioner
12727c67122SBarry Smith    that was created with PCCreate_SVD().
12827c67122SBarry Smith 
12927c67122SBarry Smith    Input Parameter:
13027c67122SBarry Smith .  pc - the preconditioner context
13127c67122SBarry Smith 
13227c67122SBarry Smith    Application Interface Routine: PCDestroy()
13327c67122SBarry Smith */
13427c67122SBarry Smith #undef __FUNCT__
13527c67122SBarry Smith #define __FUNCT__ "PCDestroy_SVD"
13627c67122SBarry Smith static PetscErrorCode PCDestroy_SVD(PC pc)
13727c67122SBarry Smith {
13827c67122SBarry Smith   PC_SVD         *jac = (PC_SVD*)pc->data;
13927c67122SBarry Smith   PetscErrorCode ierr;
14027c67122SBarry Smith 
14127c67122SBarry Smith   PetscFunctionBegin;
14227c67122SBarry Smith   if (jac->A) {
14327c67122SBarry Smith     ierr = MatDestroy(jac->A);CHKERRQ(ierr);
14427c67122SBarry Smith   }
14527c67122SBarry Smith   if (jac->U) {
14627c67122SBarry Smith     ierr = MatDestroy(jac->U);CHKERRQ(ierr);
14727c67122SBarry Smith   }
14827c67122SBarry Smith   if (jac->V) {
14927c67122SBarry Smith     ierr = MatDestroy(jac->V);CHKERRQ(ierr);
15027c67122SBarry Smith   }
15127c67122SBarry Smith   if (jac->diag) {
15227c67122SBarry Smith     ierr = VecDestroy(jac->diag);CHKERRQ(ierr);
15327c67122SBarry Smith   }
15427c67122SBarry Smith   ierr = PetscFree(jac);CHKERRQ(ierr);
15527c67122SBarry Smith   PetscFunctionReturn(0);
15627c67122SBarry Smith }
15727c67122SBarry Smith 
15827c67122SBarry Smith #undef __FUNCT__
15927c67122SBarry Smith #define __FUNCT__ "PCSetFromOptions_SVD"
16027c67122SBarry Smith static PetscErrorCode PCSetFromOptions_SVD(PC pc)
16127c67122SBarry Smith {
16227c67122SBarry Smith   PetscErrorCode ierr;
16327c67122SBarry Smith 
16427c67122SBarry Smith   PetscFunctionBegin;
16527c67122SBarry Smith   ierr = PetscOptionsHead("SVD options");CHKERRQ(ierr);
16627c67122SBarry Smith   ierr = PetscOptionsTail();CHKERRQ(ierr);
16727c67122SBarry Smith   PetscFunctionReturn(0);
16827c67122SBarry Smith }
16927c67122SBarry Smith 
17027c67122SBarry Smith /* -------------------------------------------------------------------------- */
17127c67122SBarry Smith /*
17227c67122SBarry Smith    PCCreate_SVD - Creates a SVD preconditioner context, PC_SVD,
17327c67122SBarry Smith    and sets this as the private data within the generic preconditioning
17427c67122SBarry Smith    context, PC, that was created within PCCreate().
17527c67122SBarry Smith 
17627c67122SBarry Smith    Input Parameter:
17727c67122SBarry Smith .  pc - the preconditioner context
17827c67122SBarry Smith 
17927c67122SBarry Smith    Application Interface Routine: PCCreate()
18027c67122SBarry Smith */
18127c67122SBarry Smith 
18227c67122SBarry Smith /*MC
18327c67122SBarry Smith      PCSVD - Use pseudo inverse defined by SVD of operator
18427c67122SBarry Smith 
18527c67122SBarry Smith    Level: advanced
18627c67122SBarry Smith 
18727c67122SBarry Smith   Concepts: SVD
18827c67122SBarry Smith 
18927c67122SBarry Smith          Zero entries along the diagonal are replaced with the value 0.0
19027c67122SBarry Smith 
19127c67122SBarry Smith .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC
19227c67122SBarry Smith M*/
19327c67122SBarry Smith 
19427c67122SBarry Smith EXTERN_C_BEGIN
19527c67122SBarry Smith #undef __FUNCT__
19627c67122SBarry Smith #define __FUNCT__ "PCCreate_SVD"
1977087cfbeSBarry Smith PetscErrorCode PCCreate_SVD(PC pc)
19827c67122SBarry Smith {
19927c67122SBarry Smith   PC_SVD         *jac;
20027c67122SBarry Smith   PetscErrorCode ierr;
20127c67122SBarry Smith 
20227c67122SBarry Smith   PetscFunctionBegin;
20327c67122SBarry Smith   /*
20427c67122SBarry Smith      Creates the private data structure for this preconditioner and
20527c67122SBarry Smith      attach it to the PC object.
20627c67122SBarry Smith   */
20727c67122SBarry Smith   ierr      = PetscNewLog(pc,PC_SVD,&jac);CHKERRQ(ierr);
20827c67122SBarry Smith   pc->data  = (void*)jac;
20927c67122SBarry Smith 
21027c67122SBarry Smith   /*
21127c67122SBarry Smith       Set the pointers for the functions that are provided above.
21227c67122SBarry Smith       Now when the user-level routines (such as PCApply(), PCDestroy(), etc.)
21327c67122SBarry Smith       are called, they will automatically call these functions.  Note we
21427c67122SBarry Smith       choose not to provide a couple of these functions since they are
21527c67122SBarry Smith       not needed.
21627c67122SBarry Smith   */
21727c67122SBarry Smith   pc->ops->apply               = PCApply_SVD;
21827c67122SBarry Smith   pc->ops->applytranspose      = PCApply_SVD;
21927c67122SBarry Smith   pc->ops->setup               = PCSetUp_SVD;
22027c67122SBarry Smith   pc->ops->destroy             = PCDestroy_SVD;
22127c67122SBarry Smith   pc->ops->setfromoptions      = PCSetFromOptions_SVD;
22227c67122SBarry Smith   pc->ops->view                = 0;
22327c67122SBarry Smith   pc->ops->applyrichardson     = 0;
22427c67122SBarry Smith   PetscFunctionReturn(0);
22527c67122SBarry Smith }
22627c67122SBarry Smith EXTERN_C_END
22727c67122SBarry Smith 
228