1c2fc9fa9SBarry Smith 2c2fc9fa9SBarry Smith #include <../src/snes/impls/vi/rs/virsimpl.h> /*I "petscsnes.h" I*/ 3b45d2f2cSJed Brown #include <../include/petsc-private/kspimpl.h> 4b45d2f2cSJed Brown #include <../include/petsc-private/matimpl.h> 5b45d2f2cSJed Brown #include <../include/petsc-private/dmimpl.h> 6c2fc9fa9SBarry Smith 7c2fc9fa9SBarry Smith #undef __FUNCT__ 8c2fc9fa9SBarry Smith #define __FUNCT__ "SNESVIGetInactiveSet" 9c2fc9fa9SBarry Smith /* 10c2fc9fa9SBarry Smith SNESVIGetInactiveSet - Gets the global indices for the inactive set variables (these correspond to the degrees of freedom the linear 11c2fc9fa9SBarry Smith system is solved on) 12c2fc9fa9SBarry Smith 13c2fc9fa9SBarry Smith Input parameter 14c2fc9fa9SBarry Smith . snes - the SNES context 15c2fc9fa9SBarry Smith 16c2fc9fa9SBarry Smith Output parameter 17c2fc9fa9SBarry Smith . ISact - active set index set 18c2fc9fa9SBarry Smith 19c2fc9fa9SBarry Smith */ 20c2fc9fa9SBarry Smith PetscErrorCode SNESVIGetInactiveSet(SNES snes,IS* inact) 21c2fc9fa9SBarry Smith { 22c2fc9fa9SBarry Smith SNES_VIRS *vi = (SNES_VIRS*)snes->data; 23c2fc9fa9SBarry Smith PetscFunctionBegin; 24c2fc9fa9SBarry Smith *inact = vi->IS_inact_prev; 25c2fc9fa9SBarry Smith PetscFunctionReturn(0); 26c2fc9fa9SBarry Smith } 27c2fc9fa9SBarry Smith 28c2fc9fa9SBarry Smith /* 29c2fc9fa9SBarry Smith Provides a wrapper to a DM to allow it to be used to generated the interpolation/restriction from the DM for the smaller matrices and vectors 30c2fc9fa9SBarry Smith defined by the reduced space method. 31c2fc9fa9SBarry Smith 32c2fc9fa9SBarry Smith Simple calls the regular DM interpolation and restricts it to operation on the variables not associated with active constraints. 33c2fc9fa9SBarry Smith 34c2fc9fa9SBarry Smith <*/ 35c2fc9fa9SBarry Smith typedef struct { 36c2fc9fa9SBarry Smith PetscInt n; /* size of vectors in the reduced DM space */ 37c2fc9fa9SBarry Smith IS inactive; 3825296bd5SBarry Smith PetscErrorCode (*createinterpolation)(DM,DM,Mat*,Vec*); /* DM's original routines */ 39c2fc9fa9SBarry Smith PetscErrorCode (*coarsen)(DM, MPI_Comm, DM*); 40c2fc9fa9SBarry Smith PetscErrorCode (*createglobalvector)(DM,Vec*); 41c2fc9fa9SBarry Smith DM dm; /* when destroying this object we need to reset the above function into the base DM */ 42c2fc9fa9SBarry Smith } DM_SNESVI; 43c2fc9fa9SBarry Smith 44c2fc9fa9SBarry Smith #undef __FUNCT__ 45c2fc9fa9SBarry Smith #define __FUNCT__ "DMCreateGlobalVector_SNESVI" 46c2fc9fa9SBarry Smith /* 47c2fc9fa9SBarry Smith DMCreateGlobalVector_SNESVI - Creates global vector of the size of the reduced space 48c2fc9fa9SBarry Smith 49c2fc9fa9SBarry Smith */ 50c2fc9fa9SBarry Smith PetscErrorCode DMCreateGlobalVector_SNESVI(DM dm,Vec *vec) 51c2fc9fa9SBarry Smith { 52c2fc9fa9SBarry Smith PetscErrorCode ierr; 53c2fc9fa9SBarry Smith PetscContainer isnes; 54c2fc9fa9SBarry Smith DM_SNESVI *dmsnesvi; 55c2fc9fa9SBarry Smith 56c2fc9fa9SBarry Smith PetscFunctionBegin; 57c2fc9fa9SBarry Smith ierr = PetscObjectQuery((PetscObject)dm,"VI",(PetscObject *)&isnes);CHKERRQ(ierr); 58c2fc9fa9SBarry Smith if (!isnes) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_PLIB,"Composed SNES is missing"); 59c2fc9fa9SBarry Smith ierr = PetscContainerGetPointer(isnes,(void**)&dmsnesvi);CHKERRQ(ierr); 60c2fc9fa9SBarry Smith ierr = VecCreateMPI(((PetscObject)dm)->comm,dmsnesvi->n,PETSC_DETERMINE,vec);CHKERRQ(ierr); 61c2fc9fa9SBarry Smith PetscFunctionReturn(0); 62c2fc9fa9SBarry Smith } 63c2fc9fa9SBarry Smith 64c2fc9fa9SBarry Smith #undef __FUNCT__ 65e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation_SNESVI" 66c2fc9fa9SBarry Smith /* 67e727c939SJed Brown DMCreateInterpolation_SNESVI - Modifieds the interpolation obtained from the DM by removing all rows and columns associated with active constraints. 68c2fc9fa9SBarry Smith 69c2fc9fa9SBarry Smith */ 70e727c939SJed Brown PetscErrorCode DMCreateInterpolation_SNESVI(DM dm1,DM dm2,Mat *mat,Vec *vec) 71c2fc9fa9SBarry Smith { 72c2fc9fa9SBarry Smith PetscErrorCode ierr; 73c2fc9fa9SBarry Smith PetscContainer isnes; 74c2fc9fa9SBarry Smith DM_SNESVI *dmsnesvi1,*dmsnesvi2; 75c2fc9fa9SBarry Smith Mat interp; 76c2fc9fa9SBarry Smith 77c2fc9fa9SBarry Smith PetscFunctionBegin; 78c2fc9fa9SBarry Smith ierr = PetscObjectQuery((PetscObject)dm1,"VI",(PetscObject *)&isnes);CHKERRQ(ierr); 79c2fc9fa9SBarry Smith if (!isnes) SETERRQ(((PetscObject)dm1)->comm,PETSC_ERR_PLIB,"Composed VI data structure is missing"); 80c2fc9fa9SBarry Smith ierr = PetscContainerGetPointer(isnes,(void**)&dmsnesvi1);CHKERRQ(ierr); 81c2fc9fa9SBarry Smith ierr = PetscObjectQuery((PetscObject)dm2,"VI",(PetscObject *)&isnes);CHKERRQ(ierr); 82c2fc9fa9SBarry Smith if (!isnes) SETERRQ(((PetscObject)dm2)->comm,PETSC_ERR_PLIB,"Composed VI data structure is missing"); 83c2fc9fa9SBarry Smith ierr = PetscContainerGetPointer(isnes,(void**)&dmsnesvi2);CHKERRQ(ierr); 84c2fc9fa9SBarry Smith 8525296bd5SBarry Smith ierr = (*dmsnesvi1->createinterpolation)(dm1,dm2,&interp,PETSC_NULL);CHKERRQ(ierr); 86c2fc9fa9SBarry Smith ierr = MatGetSubMatrix(interp,dmsnesvi2->inactive,dmsnesvi1->inactive,MAT_INITIAL_MATRIX,mat);CHKERRQ(ierr); 87c2fc9fa9SBarry Smith ierr = MatDestroy(&interp);CHKERRQ(ierr); 88c2fc9fa9SBarry Smith *vec = 0; 89c2fc9fa9SBarry Smith PetscFunctionReturn(0); 90c2fc9fa9SBarry Smith } 91c2fc9fa9SBarry Smith 92c2fc9fa9SBarry Smith extern PetscErrorCode DMSetVI(DM,IS); 93c2fc9fa9SBarry Smith 94c2fc9fa9SBarry Smith #undef __FUNCT__ 95c2fc9fa9SBarry Smith #define __FUNCT__ "DMCoarsen_SNESVI" 96c2fc9fa9SBarry Smith /* 97c2fc9fa9SBarry Smith DMCoarsen_SNESVI - Computes the regular coarsened DM then computes additional information about its inactive set 98c2fc9fa9SBarry Smith 99c2fc9fa9SBarry Smith */ 100c2fc9fa9SBarry Smith PetscErrorCode DMCoarsen_SNESVI(DM dm1,MPI_Comm comm,DM *dm2) 101c2fc9fa9SBarry Smith { 102c2fc9fa9SBarry Smith PetscErrorCode ierr; 103c2fc9fa9SBarry Smith PetscContainer isnes; 104c2fc9fa9SBarry Smith DM_SNESVI *dmsnesvi1; 105c2fc9fa9SBarry Smith Vec finemarked,coarsemarked; 106c2fc9fa9SBarry Smith IS inactive; 107c2fc9fa9SBarry Smith VecScatter inject; 108c2fc9fa9SBarry Smith const PetscInt *index; 109c2fc9fa9SBarry Smith PetscInt n,k,cnt = 0,rstart,*coarseindex; 110c2fc9fa9SBarry Smith PetscScalar *marked; 111c2fc9fa9SBarry Smith 112c2fc9fa9SBarry Smith PetscFunctionBegin; 113c2fc9fa9SBarry Smith ierr = PetscObjectQuery((PetscObject)dm1,"VI",(PetscObject *)&isnes);CHKERRQ(ierr); 114c2fc9fa9SBarry Smith if (!isnes) SETERRQ(((PetscObject)dm1)->comm,PETSC_ERR_PLIB,"Composed VI data structure is missing"); 115c2fc9fa9SBarry Smith ierr = PetscContainerGetPointer(isnes,(void**)&dmsnesvi1);CHKERRQ(ierr); 116c2fc9fa9SBarry Smith 117c2fc9fa9SBarry Smith /* get the original coarsen */ 118c2fc9fa9SBarry Smith ierr = (*dmsnesvi1->coarsen)(dm1,comm,dm2);CHKERRQ(ierr); 119c2fc9fa9SBarry Smith 120c2fc9fa9SBarry Smith /* not sure why this extra reference is needed, but without the dm2 disappears too early */ 121c2fc9fa9SBarry Smith ierr = PetscObjectReference((PetscObject)*dm2);CHKERRQ(ierr); 122c2fc9fa9SBarry Smith 123c2fc9fa9SBarry Smith /* need to set back global vectors in order to use the original injection */ 124c2fc9fa9SBarry Smith ierr = DMClearGlobalVectors(dm1);CHKERRQ(ierr); 125c2fc9fa9SBarry Smith dm1->ops->createglobalvector = dmsnesvi1->createglobalvector; 126c2fc9fa9SBarry Smith ierr = DMCreateGlobalVector(dm1,&finemarked);CHKERRQ(ierr); 127c2fc9fa9SBarry Smith ierr = DMCreateGlobalVector(*dm2,&coarsemarked);CHKERRQ(ierr); 128c2fc9fa9SBarry Smith 129c2fc9fa9SBarry Smith /* 130c2fc9fa9SBarry Smith fill finemarked with locations of inactive points 131c2fc9fa9SBarry Smith */ 132c2fc9fa9SBarry Smith ierr = ISGetIndices(dmsnesvi1->inactive,&index);CHKERRQ(ierr); 133c2fc9fa9SBarry Smith ierr = ISGetLocalSize(dmsnesvi1->inactive,&n);CHKERRQ(ierr); 134c2fc9fa9SBarry Smith ierr = VecSet(finemarked,0.0);CHKERRQ(ierr); 135c2fc9fa9SBarry Smith for (k=0;k<n;k++){ 136c2fc9fa9SBarry Smith ierr = VecSetValue(finemarked,index[k],1.0,INSERT_VALUES);CHKERRQ(ierr); 137c2fc9fa9SBarry Smith } 138c2fc9fa9SBarry Smith ierr = VecAssemblyBegin(finemarked);CHKERRQ(ierr); 139c2fc9fa9SBarry Smith ierr = VecAssemblyEnd(finemarked);CHKERRQ(ierr); 140c2fc9fa9SBarry Smith 141e727c939SJed Brown ierr = DMCreateInjection(*dm2,dm1,&inject);CHKERRQ(ierr); 142c2fc9fa9SBarry Smith ierr = VecScatterBegin(inject,finemarked,coarsemarked,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 143c2fc9fa9SBarry Smith ierr = VecScatterEnd(inject,finemarked,coarsemarked,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 144c2fc9fa9SBarry Smith ierr = VecScatterDestroy(&inject);CHKERRQ(ierr); 145c2fc9fa9SBarry Smith 146c2fc9fa9SBarry Smith /* 147c2fc9fa9SBarry Smith create index set list of coarse inactive points from coarsemarked 148c2fc9fa9SBarry Smith */ 149c2fc9fa9SBarry Smith ierr = VecGetLocalSize(coarsemarked,&n);CHKERRQ(ierr); 150c2fc9fa9SBarry Smith ierr = VecGetOwnershipRange(coarsemarked,&rstart,PETSC_NULL);CHKERRQ(ierr); 151c2fc9fa9SBarry Smith ierr = VecGetArray(coarsemarked,&marked);CHKERRQ(ierr); 152c2fc9fa9SBarry Smith for (k=0; k<n; k++) { 153c2fc9fa9SBarry Smith if (marked[k] != 0.0) cnt++; 154c2fc9fa9SBarry Smith } 155c2fc9fa9SBarry Smith ierr = PetscMalloc(cnt*sizeof(PetscInt),&coarseindex);CHKERRQ(ierr); 156c2fc9fa9SBarry Smith cnt = 0; 157c2fc9fa9SBarry Smith for (k=0; k<n; k++) { 158c2fc9fa9SBarry Smith if (marked[k] != 0.0) coarseindex[cnt++] = k + rstart; 159c2fc9fa9SBarry Smith } 160c2fc9fa9SBarry Smith ierr = VecRestoreArray(coarsemarked,&marked);CHKERRQ(ierr); 161*5f042095SDmitry Karpeev ierr = ISCreateGeneral(((PetscObject)coarsemarked)->comm,cnt,coarseindex,PETSC_OWN_POINTER,&inactive);CHKERRQ(ierr); 162c2fc9fa9SBarry Smith 163c2fc9fa9SBarry Smith ierr = DMClearGlobalVectors(dm1);CHKERRQ(ierr); 164c2fc9fa9SBarry Smith dm1->ops->createglobalvector = DMCreateGlobalVector_SNESVI; 165c2fc9fa9SBarry Smith ierr = DMSetVI(*dm2,inactive);CHKERRQ(ierr); 166c2fc9fa9SBarry Smith 167c2fc9fa9SBarry Smith ierr = VecDestroy(&finemarked);CHKERRQ(ierr); 168c2fc9fa9SBarry Smith ierr = VecDestroy(&coarsemarked);CHKERRQ(ierr); 169c2fc9fa9SBarry Smith ierr = ISDestroy(&inactive);CHKERRQ(ierr); 170c2fc9fa9SBarry Smith PetscFunctionReturn(0); 171c2fc9fa9SBarry Smith } 172c2fc9fa9SBarry Smith 173c2fc9fa9SBarry Smith #undef __FUNCT__ 174c2fc9fa9SBarry Smith #define __FUNCT__ "DMDestroy_SNESVI" 175c2fc9fa9SBarry Smith PetscErrorCode DMDestroy_SNESVI(DM_SNESVI *dmsnesvi) 176c2fc9fa9SBarry Smith { 177c2fc9fa9SBarry Smith PetscErrorCode ierr; 178c2fc9fa9SBarry Smith 179c2fc9fa9SBarry Smith PetscFunctionBegin; 180c2fc9fa9SBarry Smith /* reset the base methods in the DM object that were changed when the DM_SNESVI was reset */ 18125296bd5SBarry Smith dmsnesvi->dm->ops->createinterpolation = dmsnesvi->createinterpolation; 182c2fc9fa9SBarry Smith dmsnesvi->dm->ops->coarsen = dmsnesvi->coarsen; 183c2fc9fa9SBarry Smith dmsnesvi->dm->ops->createglobalvector = dmsnesvi->createglobalvector; 184c2fc9fa9SBarry Smith /* need to clear out this vectors because some of them may not have a reference to the DM 185c2fc9fa9SBarry Smith but they are counted as having references to the DM in DMDestroy() */ 186c2fc9fa9SBarry Smith ierr = DMClearGlobalVectors(dmsnesvi->dm);CHKERRQ(ierr); 187c2fc9fa9SBarry Smith 188c2fc9fa9SBarry Smith ierr = ISDestroy(&dmsnesvi->inactive);CHKERRQ(ierr); 189c2fc9fa9SBarry Smith ierr = PetscFree(dmsnesvi);CHKERRQ(ierr); 190c2fc9fa9SBarry Smith PetscFunctionReturn(0); 191c2fc9fa9SBarry Smith } 192c2fc9fa9SBarry Smith 193c2fc9fa9SBarry Smith #undef __FUNCT__ 194c2fc9fa9SBarry Smith #define __FUNCT__ "DMSetVI" 195c2fc9fa9SBarry Smith /* 196c2fc9fa9SBarry Smith DMSetVI - Marks a DM as associated with a VI problem. This causes the interpolation/restriction operators to 197c2fc9fa9SBarry Smith be restricted to only those variables NOT associated with active constraints. 198c2fc9fa9SBarry Smith 199c2fc9fa9SBarry Smith */ 200c2fc9fa9SBarry Smith PetscErrorCode DMSetVI(DM dm,IS inactive) 201c2fc9fa9SBarry Smith { 202c2fc9fa9SBarry Smith PetscErrorCode ierr; 203c2fc9fa9SBarry Smith PetscContainer isnes; 204c2fc9fa9SBarry Smith DM_SNESVI *dmsnesvi; 205c2fc9fa9SBarry Smith 206c2fc9fa9SBarry Smith PetscFunctionBegin; 207c2fc9fa9SBarry Smith if (!dm) PetscFunctionReturn(0); 208c2fc9fa9SBarry Smith 209c2fc9fa9SBarry Smith ierr = PetscObjectReference((PetscObject)inactive);CHKERRQ(ierr); 210c2fc9fa9SBarry Smith 211c2fc9fa9SBarry Smith ierr = PetscObjectQuery((PetscObject)dm,"VI",(PetscObject *)&isnes);CHKERRQ(ierr); 212c2fc9fa9SBarry Smith if (!isnes) { 213c2fc9fa9SBarry Smith ierr = PetscContainerCreate(((PetscObject)dm)->comm,&isnes);CHKERRQ(ierr); 214c2fc9fa9SBarry Smith ierr = PetscContainerSetUserDestroy(isnes,(PetscErrorCode (*)(void*))DMDestroy_SNESVI);CHKERRQ(ierr); 215c2fc9fa9SBarry Smith ierr = PetscNew(DM_SNESVI,&dmsnesvi);CHKERRQ(ierr); 216c2fc9fa9SBarry Smith ierr = PetscContainerSetPointer(isnes,(void*)dmsnesvi);CHKERRQ(ierr); 217c2fc9fa9SBarry Smith ierr = PetscObjectCompose((PetscObject)dm,"VI",(PetscObject)isnes);CHKERRQ(ierr); 218c2fc9fa9SBarry Smith ierr = PetscContainerDestroy(&isnes);CHKERRQ(ierr); 21925296bd5SBarry Smith dmsnesvi->createinterpolation = dm->ops->createinterpolation; 22025296bd5SBarry Smith dm->ops->createinterpolation = DMCreateInterpolation_SNESVI; 221c2fc9fa9SBarry Smith dmsnesvi->coarsen = dm->ops->coarsen; 222c2fc9fa9SBarry Smith dm->ops->coarsen = DMCoarsen_SNESVI; 223c2fc9fa9SBarry Smith dmsnesvi->createglobalvector = dm->ops->createglobalvector; 224c2fc9fa9SBarry Smith dm->ops->createglobalvector = DMCreateGlobalVector_SNESVI; 225c2fc9fa9SBarry Smith } else { 226c2fc9fa9SBarry Smith ierr = PetscContainerGetPointer(isnes,(void**)&dmsnesvi);CHKERRQ(ierr); 227c2fc9fa9SBarry Smith ierr = ISDestroy(&dmsnesvi->inactive);CHKERRQ(ierr); 228c2fc9fa9SBarry Smith } 229c2fc9fa9SBarry Smith ierr = DMClearGlobalVectors(dm);CHKERRQ(ierr); 230c2fc9fa9SBarry Smith ierr = ISGetLocalSize(inactive,&dmsnesvi->n);CHKERRQ(ierr); 231c2fc9fa9SBarry Smith dmsnesvi->inactive = inactive; 232c2fc9fa9SBarry Smith dmsnesvi->dm = dm; 233c2fc9fa9SBarry Smith PetscFunctionReturn(0); 234c2fc9fa9SBarry Smith } 235c2fc9fa9SBarry Smith 236c2fc9fa9SBarry Smith #undef __FUNCT__ 237c2fc9fa9SBarry Smith #define __FUNCT__ "DMDestroyVI" 238c2fc9fa9SBarry Smith /* 239c2fc9fa9SBarry Smith DMDestroyVI - Frees the DM_SNESVI object contained in the DM 24025296bd5SBarry Smith - also resets the function pointers in the DM for createinterpolation() etc to use the original DM 241c2fc9fa9SBarry Smith */ 242c2fc9fa9SBarry Smith PetscErrorCode DMDestroyVI(DM dm) 243c2fc9fa9SBarry Smith { 244c2fc9fa9SBarry Smith PetscErrorCode ierr; 245c2fc9fa9SBarry Smith 246c2fc9fa9SBarry Smith PetscFunctionBegin; 247c2fc9fa9SBarry Smith if (!dm) PetscFunctionReturn(0); 248c2fc9fa9SBarry Smith ierr = PetscObjectCompose((PetscObject)dm,"VI",(PetscObject)PETSC_NULL);CHKERRQ(ierr); 249c2fc9fa9SBarry Smith PetscFunctionReturn(0); 250c2fc9fa9SBarry Smith } 251c2fc9fa9SBarry Smith 252c2fc9fa9SBarry Smith /* --------------------------------------------------------------------------------------------------------*/ 253c2fc9fa9SBarry Smith 254c2fc9fa9SBarry Smith 255c2fc9fa9SBarry Smith 256c2fc9fa9SBarry Smith 257c2fc9fa9SBarry Smith #undef __FUNCT__ 258c2fc9fa9SBarry Smith #define __FUNCT__ "SNESCreateIndexSets_VIRS" 259c2fc9fa9SBarry Smith PetscErrorCode SNESCreateIndexSets_VIRS(SNES snes,Vec X,Vec F,IS* ISact,IS* ISinact) 260c2fc9fa9SBarry Smith { 261c2fc9fa9SBarry Smith PetscErrorCode ierr; 262c2fc9fa9SBarry Smith 263c2fc9fa9SBarry Smith PetscFunctionBegin; 264c2fc9fa9SBarry Smith ierr = SNESVIGetActiveSetIS(snes,X,F,ISact);CHKERRQ(ierr); 265c2fc9fa9SBarry Smith ierr = ISComplement(*ISact,X->map->rstart,X->map->rend,ISinact);CHKERRQ(ierr); 266c2fc9fa9SBarry Smith PetscFunctionReturn(0); 267c2fc9fa9SBarry Smith } 268c2fc9fa9SBarry Smith 269c2fc9fa9SBarry Smith /* Create active and inactive set vectors. The local size of this vector is set and petsc computes the global size */ 270c2fc9fa9SBarry Smith #undef __FUNCT__ 271c2fc9fa9SBarry Smith #define __FUNCT__ "SNESCreateSubVectors_VIRS" 272c2fc9fa9SBarry Smith PetscErrorCode SNESCreateSubVectors_VIRS(SNES snes,PetscInt n,Vec* newv) 273c2fc9fa9SBarry Smith { 274c2fc9fa9SBarry Smith PetscErrorCode ierr; 275c2fc9fa9SBarry Smith Vec v; 276c2fc9fa9SBarry Smith 277c2fc9fa9SBarry Smith PetscFunctionBegin; 278c2fc9fa9SBarry Smith ierr = VecCreate(((PetscObject)snes)->comm,&v);CHKERRQ(ierr); 279c2fc9fa9SBarry Smith ierr = VecSetSizes(v,n,PETSC_DECIDE);CHKERRQ(ierr); 280c2fc9fa9SBarry Smith ierr = VecSetFromOptions(v);CHKERRQ(ierr); 281c2fc9fa9SBarry Smith *newv = v; 282c2fc9fa9SBarry Smith 283c2fc9fa9SBarry Smith PetscFunctionReturn(0); 284c2fc9fa9SBarry Smith } 285c2fc9fa9SBarry Smith 286c2fc9fa9SBarry Smith /* Resets the snes PC and KSP when the active set sizes change */ 287c2fc9fa9SBarry Smith #undef __FUNCT__ 288c2fc9fa9SBarry Smith #define __FUNCT__ "SNESVIResetPCandKSP" 289c2fc9fa9SBarry Smith PetscErrorCode SNESVIResetPCandKSP(SNES snes,Mat Amat,Mat Pmat) 290c2fc9fa9SBarry Smith { 291c2fc9fa9SBarry Smith PetscErrorCode ierr; 292c2fc9fa9SBarry Smith KSP snesksp; 293c2fc9fa9SBarry Smith 294c2fc9fa9SBarry Smith PetscFunctionBegin; 295c2fc9fa9SBarry Smith ierr = SNESGetKSP(snes,&snesksp);CHKERRQ(ierr); 296c2fc9fa9SBarry Smith ierr = KSPReset(snesksp);CHKERRQ(ierr); 297c2fc9fa9SBarry Smith 298c2fc9fa9SBarry Smith /* 299c2fc9fa9SBarry Smith KSP kspnew; 300c2fc9fa9SBarry Smith PC pcnew; 301c2fc9fa9SBarry Smith const MatSolverPackage stype; 302c2fc9fa9SBarry Smith 303c2fc9fa9SBarry Smith 304c2fc9fa9SBarry Smith ierr = KSPCreate(((PetscObject)snes)->comm,&kspnew);CHKERRQ(ierr); 305c2fc9fa9SBarry Smith kspnew->pc_side = snesksp->pc_side; 306c2fc9fa9SBarry Smith kspnew->rtol = snesksp->rtol; 307c2fc9fa9SBarry Smith kspnew->abstol = snesksp->abstol; 308c2fc9fa9SBarry Smith kspnew->max_it = snesksp->max_it; 309c2fc9fa9SBarry Smith ierr = KSPSetType(kspnew,((PetscObject)snesksp)->type_name);CHKERRQ(ierr); 310c2fc9fa9SBarry Smith ierr = KSPGetPC(kspnew,&pcnew);CHKERRQ(ierr); 311c2fc9fa9SBarry Smith ierr = PCSetType(kspnew->pc,((PetscObject)snesksp->pc)->type_name);CHKERRQ(ierr); 312c2fc9fa9SBarry Smith ierr = PCSetOperators(kspnew->pc,Amat,Pmat,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr); 313c2fc9fa9SBarry Smith ierr = PCFactorGetMatSolverPackage(snesksp->pc,&stype);CHKERRQ(ierr); 314c2fc9fa9SBarry Smith ierr = PCFactorSetMatSolverPackage(kspnew->pc,stype);CHKERRQ(ierr); 315c2fc9fa9SBarry Smith ierr = KSPDestroy(&snesksp);CHKERRQ(ierr); 316c2fc9fa9SBarry Smith snes->ksp = kspnew; 317c2fc9fa9SBarry Smith ierr = PetscLogObjectParent(snes,kspnew);CHKERRQ(ierr); 318c2fc9fa9SBarry Smith ierr = KSPSetFromOptions(kspnew);CHKERRQ(ierr);*/ 319c2fc9fa9SBarry Smith PetscFunctionReturn(0); 320c2fc9fa9SBarry Smith } 321c2fc9fa9SBarry Smith 3229bd66eb0SPeter Brune extern PetscErrorCode SNESLineSearchNo_VI(SNES,void *,Vec,Vec,Vec,PetscReal,PetscReal,Vec,Vec,PetscReal *,PetscReal *,PetscBool *); 323c2fc9fa9SBarry Smith 324c2fc9fa9SBarry Smith /* Variational Inequality solver using reduce space method. No semismooth algorithm is 325c2fc9fa9SBarry Smith implemented in this algorithm. It basically identifies the active constraints and does 326c2fc9fa9SBarry Smith a linear solve on the other variables (those not associated with the active constraints). */ 327c2fc9fa9SBarry Smith #undef __FUNCT__ 328c2fc9fa9SBarry Smith #define __FUNCT__ "SNESSolve_VIRS" 329c2fc9fa9SBarry Smith PetscErrorCode SNESSolve_VIRS(SNES snes) 330c2fc9fa9SBarry Smith { 331c2fc9fa9SBarry Smith SNES_VIRS *vi = (SNES_VIRS*)snes->data; 332c2fc9fa9SBarry Smith PetscErrorCode ierr; 333c2fc9fa9SBarry Smith PetscInt maxits,i,lits; 334c2fc9fa9SBarry Smith PetscBool lssucceed; 335c2fc9fa9SBarry Smith MatStructure flg = DIFFERENT_NONZERO_PATTERN; 336c2fc9fa9SBarry Smith PetscReal fnorm,gnorm,xnorm=0,ynorm; 3379bd66eb0SPeter Brune Vec Y,X,F; 338c2fc9fa9SBarry Smith KSPConvergedReason kspreason; 339c2fc9fa9SBarry Smith 340c2fc9fa9SBarry Smith PetscFunctionBegin; 3419bd66eb0SPeter Brune 342c2fc9fa9SBarry Smith snes->numFailures = 0; 343c2fc9fa9SBarry Smith snes->numLinearSolveFailures = 0; 344c2fc9fa9SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 345c2fc9fa9SBarry Smith 346c2fc9fa9SBarry Smith maxits = snes->max_its; /* maximum number of iterations */ 347c2fc9fa9SBarry Smith X = snes->vec_sol; /* solution vector */ 348c2fc9fa9SBarry Smith F = snes->vec_func; /* residual vector */ 349c2fc9fa9SBarry Smith Y = snes->work[0]; /* work vectors */ 3509bd66eb0SPeter Brune 351f1c6b773SPeter Brune ierr = SNESLineSearchSetVIFunctions(snes->linesearch, SNESVIProjectOntoBounds, SNESVIComputeInactiveSetFnorm);CHKERRQ(ierr); 352f1c6b773SPeter Brune ierr = SNESLineSearchSetVecs(snes->linesearch, X, PETSC_NULL, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr); 353f1c6b773SPeter Brune ierr = SNESLineSearchSetUp(snes->linesearch);CHKERRQ(ierr); 354c2fc9fa9SBarry Smith 355c2fc9fa9SBarry Smith ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr); 356c2fc9fa9SBarry Smith snes->iter = 0; 357c2fc9fa9SBarry Smith snes->norm = 0.0; 358c2fc9fa9SBarry Smith ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr); 359c2fc9fa9SBarry Smith 360c2fc9fa9SBarry Smith ierr = SNESVIProjectOntoBounds(snes,X);CHKERRQ(ierr); 361c2fc9fa9SBarry Smith ierr = SNESComputeFunction(snes,X,F);CHKERRQ(ierr); 362c2fc9fa9SBarry Smith if (snes->domainerror) { 363c2fc9fa9SBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 364c2fc9fa9SBarry Smith PetscFunctionReturn(0); 365c2fc9fa9SBarry Smith } 366c2fc9fa9SBarry Smith ierr = SNESVIComputeInactiveSetFnorm(snes,F,X,&fnorm);CHKERRQ(ierr); 367c2fc9fa9SBarry Smith ierr = VecNormBegin(X,NORM_2,&xnorm);CHKERRQ(ierr); /* xnorm <- ||x|| */ 368c2fc9fa9SBarry Smith ierr = VecNormEnd(X,NORM_2,&xnorm);CHKERRQ(ierr); 369*5f042095SDmitry Karpeev if (PetscIsInfOrNanReal(fnorm)) SETERRQ(((PetscObject)X)->comm,PETSC_ERR_FP,"User provided compute function generated a Not-a-Number"); 370c2fc9fa9SBarry Smith 371c2fc9fa9SBarry Smith ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr); 372c2fc9fa9SBarry Smith snes->norm = fnorm; 373c2fc9fa9SBarry Smith ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr); 374c2fc9fa9SBarry Smith SNESLogConvHistory(snes,fnorm,0); 375c2fc9fa9SBarry Smith ierr = SNESMonitor(snes,0,fnorm);CHKERRQ(ierr); 376c2fc9fa9SBarry Smith 377c2fc9fa9SBarry Smith /* set parameter for default relative tolerance convergence test */ 378c2fc9fa9SBarry Smith snes->ttol = fnorm*snes->rtol; 379c2fc9fa9SBarry Smith /* test convergence */ 380c2fc9fa9SBarry Smith ierr = (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);CHKERRQ(ierr); 381c2fc9fa9SBarry Smith if (snes->reason) PetscFunctionReturn(0); 382c2fc9fa9SBarry Smith 383c2fc9fa9SBarry Smith 384c2fc9fa9SBarry Smith for (i=0; i<maxits; i++) { 385c2fc9fa9SBarry Smith 386c2fc9fa9SBarry Smith IS IS_act,IS_inact; /* _act -> active set _inact -> inactive set */ 387c2fc9fa9SBarry Smith IS IS_redact; /* redundant active set */ 388c2fc9fa9SBarry Smith VecScatter scat_act,scat_inact; 389c2fc9fa9SBarry Smith PetscInt nis_act,nis_inact; 390c2fc9fa9SBarry Smith Vec Y_act,Y_inact,F_inact; 391c2fc9fa9SBarry Smith Mat jac_inact_inact,prejac_inact_inact; 392c2fc9fa9SBarry Smith PetscBool isequal; 393c2fc9fa9SBarry Smith 394c2fc9fa9SBarry Smith /* Call general purpose update function */ 395c2fc9fa9SBarry Smith if (snes->ops->update) { 396c2fc9fa9SBarry Smith ierr = (*snes->ops->update)(snes, snes->iter);CHKERRQ(ierr); 397c2fc9fa9SBarry Smith } 398c2fc9fa9SBarry Smith ierr = SNESComputeJacobian(snes,X,&snes->jacobian,&snes->jacobian_pre,&flg);CHKERRQ(ierr); 399c2fc9fa9SBarry Smith 400c2fc9fa9SBarry Smith 401c2fc9fa9SBarry Smith /* Create active and inactive index sets */ 402c2fc9fa9SBarry Smith 403c2fc9fa9SBarry Smith /*original 404c2fc9fa9SBarry Smith ierr = SNESVICreateIndexSets_RS(snes,X,F,&IS_act,&IS_inact);CHKERRQ(ierr); 405c2fc9fa9SBarry Smith */ 406c2fc9fa9SBarry Smith ierr = SNESVIGetActiveSetIS(snes,X,F,&IS_act);CHKERRQ(ierr); 407c2fc9fa9SBarry Smith 408c2fc9fa9SBarry Smith if (vi->checkredundancy) { 409c2fc9fa9SBarry Smith (*vi->checkredundancy)(snes,IS_act,&IS_redact,vi->ctxP);CHKERRQ(ierr); 410c2fc9fa9SBarry Smith if (IS_redact){ 411c2fc9fa9SBarry Smith ierr = ISSort(IS_redact);CHKERRQ(ierr); 412c2fc9fa9SBarry Smith ierr = ISComplement(IS_redact,X->map->rstart,X->map->rend,&IS_inact);CHKERRQ(ierr); 413c2fc9fa9SBarry Smith ierr = ISDestroy(&IS_redact);CHKERRQ(ierr); 414c2fc9fa9SBarry Smith } 415c2fc9fa9SBarry Smith else { 416c2fc9fa9SBarry Smith ierr = ISComplement(IS_act,X->map->rstart,X->map->rend,&IS_inact);CHKERRQ(ierr); 417c2fc9fa9SBarry Smith } 418c2fc9fa9SBarry Smith } else { 419c2fc9fa9SBarry Smith ierr = ISComplement(IS_act,X->map->rstart,X->map->rend,&IS_inact);CHKERRQ(ierr); 420c2fc9fa9SBarry Smith } 421c2fc9fa9SBarry Smith 422c2fc9fa9SBarry Smith 423c2fc9fa9SBarry Smith /* Create inactive set submatrix */ 424c2fc9fa9SBarry Smith ierr = MatGetSubMatrix(snes->jacobian,IS_inact,IS_inact,MAT_INITIAL_MATRIX,&jac_inact_inact);CHKERRQ(ierr); 425c2fc9fa9SBarry Smith 42666bfb381SJed Brown if (0) { /* Dead code (temporary developer hack) */ 42766bfb381SJed Brown IS keptrows; 428c2fc9fa9SBarry Smith ierr = MatFindNonzeroRows(jac_inact_inact,&keptrows);CHKERRQ(ierr); 42966bfb381SJed Brown if (keptrows) { 430c2fc9fa9SBarry Smith PetscInt cnt,*nrows,k; 431c2fc9fa9SBarry Smith const PetscInt *krows,*inact; 432c2fc9fa9SBarry Smith PetscInt rstart=jac_inact_inact->rmap->rstart; 433c2fc9fa9SBarry Smith 434c2fc9fa9SBarry Smith ierr = MatDestroy(&jac_inact_inact);CHKERRQ(ierr); 435c2fc9fa9SBarry Smith ierr = ISDestroy(&IS_act);CHKERRQ(ierr); 436c2fc9fa9SBarry Smith 437c2fc9fa9SBarry Smith ierr = ISGetLocalSize(keptrows,&cnt);CHKERRQ(ierr); 438c2fc9fa9SBarry Smith ierr = ISGetIndices(keptrows,&krows);CHKERRQ(ierr); 439c2fc9fa9SBarry Smith ierr = ISGetIndices(IS_inact,&inact);CHKERRQ(ierr); 440c2fc9fa9SBarry Smith ierr = PetscMalloc(cnt*sizeof(PetscInt),&nrows);CHKERRQ(ierr); 441c2fc9fa9SBarry Smith for (k=0; k<cnt; k++) { 442c2fc9fa9SBarry Smith nrows[k] = inact[krows[k]-rstart]; 443c2fc9fa9SBarry Smith } 444c2fc9fa9SBarry Smith ierr = ISRestoreIndices(keptrows,&krows);CHKERRQ(ierr); 445c2fc9fa9SBarry Smith ierr = ISRestoreIndices(IS_inact,&inact);CHKERRQ(ierr); 446c2fc9fa9SBarry Smith ierr = ISDestroy(&keptrows);CHKERRQ(ierr); 447c2fc9fa9SBarry Smith ierr = ISDestroy(&IS_inact);CHKERRQ(ierr); 448c2fc9fa9SBarry Smith 449*5f042095SDmitry Karpeev ierr = ISCreateGeneral(((PetscObject)snes)->comm,cnt,nrows,PETSC_OWN_POINTER,&IS_inact);CHKERRQ(ierr); 450c2fc9fa9SBarry Smith ierr = ISComplement(IS_inact,F->map->rstart,F->map->rend,&IS_act);CHKERRQ(ierr); 451c2fc9fa9SBarry Smith ierr = MatGetSubMatrix(snes->jacobian,IS_inact,IS_inact,MAT_INITIAL_MATRIX,&jac_inact_inact);CHKERRQ(ierr); 452c2fc9fa9SBarry Smith } 45366bfb381SJed Brown } 454c2fc9fa9SBarry Smith ierr = DMSetVI(snes->dm,IS_inact);CHKERRQ(ierr); 455c2fc9fa9SBarry Smith /* remove later */ 456c2fc9fa9SBarry Smith 457c2fc9fa9SBarry Smith /* 458*5f042095SDmitry Karpeev ierr = VecView(vi->xu,PETSC_VIEWER_BINARY_(((PetscObject)(vi->xu))->comm));CHKERRQ(ierr); 459*5f042095SDmitry Karpeev ierr = VecView(vi->xl,PETSC_VIEWER_BINARY_(((PetscObject)(vi->xl))->comm));CHKERRQ(ierr); 460*5f042095SDmitry Karpeev ierr = VecView(X,PETSC_VIEWER_BINARY_(((PetscObject)X)->comm));CHKERRQ(ierr); 461*5f042095SDmitry Karpeev ierr = VecView(F,PETSC_VIEWER_BINARY_(((PetscObject)F)->comm));CHKERRQ(ierr); 462*5f042095SDmitry Karpeev ierr = ISView(IS_inact,PETSC_VIEWER_BINARY_(((PetscObject)IS_inact)->comm));CHKERRQ(ierr); 463c2fc9fa9SBarry Smith */ 464c2fc9fa9SBarry Smith 465c2fc9fa9SBarry Smith /* Get sizes of active and inactive sets */ 466c2fc9fa9SBarry Smith ierr = ISGetLocalSize(IS_act,&nis_act);CHKERRQ(ierr); 467c2fc9fa9SBarry Smith ierr = ISGetLocalSize(IS_inact,&nis_inact);CHKERRQ(ierr); 468c2fc9fa9SBarry Smith 469c2fc9fa9SBarry Smith /* Create active and inactive set vectors */ 470c2fc9fa9SBarry Smith ierr = SNESCreateSubVectors_VIRS(snes,nis_inact,&F_inact);CHKERRQ(ierr); 471c2fc9fa9SBarry Smith ierr = SNESCreateSubVectors_VIRS(snes,nis_act,&Y_act);CHKERRQ(ierr); 472c2fc9fa9SBarry Smith ierr = SNESCreateSubVectors_VIRS(snes,nis_inact,&Y_inact);CHKERRQ(ierr); 473c2fc9fa9SBarry Smith 474c2fc9fa9SBarry Smith /* Create scatter contexts */ 475c2fc9fa9SBarry Smith ierr = VecScatterCreate(Y,IS_act,Y_act,PETSC_NULL,&scat_act);CHKERRQ(ierr); 476c2fc9fa9SBarry Smith ierr = VecScatterCreate(Y,IS_inact,Y_inact,PETSC_NULL,&scat_inact);CHKERRQ(ierr); 477c2fc9fa9SBarry Smith 478c2fc9fa9SBarry Smith /* Do a vec scatter to active and inactive set vectors */ 479c2fc9fa9SBarry Smith ierr = VecScatterBegin(scat_inact,F,F_inact,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 480c2fc9fa9SBarry Smith ierr = VecScatterEnd(scat_inact,F,F_inact,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 481c2fc9fa9SBarry Smith 482c2fc9fa9SBarry Smith ierr = VecScatterBegin(scat_act,Y,Y_act,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 483c2fc9fa9SBarry Smith ierr = VecScatterEnd(scat_act,Y,Y_act,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 484c2fc9fa9SBarry Smith 485c2fc9fa9SBarry Smith ierr = VecScatterBegin(scat_inact,Y,Y_inact,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 486c2fc9fa9SBarry Smith ierr = VecScatterEnd(scat_inact,Y,Y_inact,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 487c2fc9fa9SBarry Smith 488c2fc9fa9SBarry Smith /* Active set direction = 0 */ 489c2fc9fa9SBarry Smith ierr = VecSet(Y_act,0);CHKERRQ(ierr); 490c2fc9fa9SBarry Smith if (snes->jacobian != snes->jacobian_pre) { 491c2fc9fa9SBarry Smith ierr = MatGetSubMatrix(snes->jacobian_pre,IS_inact,IS_inact,MAT_INITIAL_MATRIX,&prejac_inact_inact);CHKERRQ(ierr); 492c2fc9fa9SBarry Smith } else prejac_inact_inact = jac_inact_inact; 493c2fc9fa9SBarry Smith 494c2fc9fa9SBarry Smith ierr = ISEqual(vi->IS_inact_prev,IS_inact,&isequal);CHKERRQ(ierr); 495c2fc9fa9SBarry Smith if (!isequal) { 496c2fc9fa9SBarry Smith ierr = SNESVIResetPCandKSP(snes,jac_inact_inact,prejac_inact_inact);CHKERRQ(ierr); 497c2fc9fa9SBarry Smith flg = DIFFERENT_NONZERO_PATTERN; 498c2fc9fa9SBarry Smith } 499c2fc9fa9SBarry Smith 500c2fc9fa9SBarry Smith /* ierr = ISView(IS_inact,0);CHKERRQ(ierr); */ 501c2fc9fa9SBarry Smith /* ierr = ISView(IS_act,0);CHKERRQ(ierr);*/ 502c2fc9fa9SBarry Smith /* ierr = MatView(snes->jacobian_pre,0); */ 503c2fc9fa9SBarry Smith 504c2fc9fa9SBarry Smith 505c2fc9fa9SBarry Smith 506c2fc9fa9SBarry Smith ierr = KSPSetOperators(snes->ksp,jac_inact_inact,prejac_inact_inact,flg);CHKERRQ(ierr); 507c2fc9fa9SBarry Smith ierr = KSPSetUp(snes->ksp);CHKERRQ(ierr); 508c2fc9fa9SBarry Smith { 509c2fc9fa9SBarry Smith PC pc; 510c2fc9fa9SBarry Smith PetscBool flg; 511c2fc9fa9SBarry Smith ierr = KSPGetPC(snes->ksp,&pc);CHKERRQ(ierr); 512c2fc9fa9SBarry Smith ierr = PetscTypeCompare((PetscObject)pc,PCFIELDSPLIT,&flg);CHKERRQ(ierr); 513c2fc9fa9SBarry Smith if (flg) { 514c2fc9fa9SBarry Smith KSP *subksps; 515c2fc9fa9SBarry Smith ierr = PCFieldSplitGetSubKSP(pc,PETSC_NULL,&subksps);CHKERRQ(ierr); 516c2fc9fa9SBarry Smith ierr = KSPGetPC(subksps[0],&pc);CHKERRQ(ierr); 517c2fc9fa9SBarry Smith ierr = PetscFree(subksps);CHKERRQ(ierr); 518c2fc9fa9SBarry Smith ierr = PetscTypeCompare((PetscObject)pc,PCBJACOBI,&flg);CHKERRQ(ierr); 519c2fc9fa9SBarry Smith if (flg) { 520c2fc9fa9SBarry Smith PetscInt n,N = 101*101,j,cnts[3] = {0,0,0}; 521c2fc9fa9SBarry Smith const PetscInt *ii; 522c2fc9fa9SBarry Smith 523c2fc9fa9SBarry Smith ierr = ISGetSize(IS_inact,&n);CHKERRQ(ierr); 524c2fc9fa9SBarry Smith ierr = ISGetIndices(IS_inact,&ii);CHKERRQ(ierr); 525c2fc9fa9SBarry Smith for (j=0; j<n; j++) { 526c2fc9fa9SBarry Smith if (ii[j] < N) cnts[0]++; 527c2fc9fa9SBarry Smith else if (ii[j] < 2*N) cnts[1]++; 528c2fc9fa9SBarry Smith else if (ii[j] < 3*N) cnts[2]++; 529c2fc9fa9SBarry Smith } 530c2fc9fa9SBarry Smith ierr = ISRestoreIndices(IS_inact,&ii);CHKERRQ(ierr); 531c2fc9fa9SBarry Smith 532c2fc9fa9SBarry Smith ierr = PCBJacobiSetTotalBlocks(pc,3,cnts);CHKERRQ(ierr); 533c2fc9fa9SBarry Smith } 534c2fc9fa9SBarry Smith } 535c2fc9fa9SBarry Smith } 536c2fc9fa9SBarry Smith 537c2fc9fa9SBarry Smith ierr = SNES_KSPSolve(snes,snes->ksp,F_inact,Y_inact);CHKERRQ(ierr); 538c2fc9fa9SBarry Smith ierr = KSPGetConvergedReason(snes->ksp,&kspreason);CHKERRQ(ierr); 539c2fc9fa9SBarry Smith if (kspreason < 0) { 540c2fc9fa9SBarry Smith if (++snes->numLinearSolveFailures >= snes->maxLinearSolveFailures) { 541c2fc9fa9SBarry Smith ierr = PetscInfo2(snes,"iter=%D, number linear solve failures %D greater than current SNES allowed, stopping solve\n",snes->iter,snes->numLinearSolveFailures);CHKERRQ(ierr); 542c2fc9fa9SBarry Smith snes->reason = SNES_DIVERGED_LINEAR_SOLVE; 543c2fc9fa9SBarry Smith break; 544c2fc9fa9SBarry Smith } 545c2fc9fa9SBarry Smith } 546c2fc9fa9SBarry Smith 547c2fc9fa9SBarry Smith ierr = VecScatterBegin(scat_act,Y_act,Y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 548c2fc9fa9SBarry Smith ierr = VecScatterEnd(scat_act,Y_act,Y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 549c2fc9fa9SBarry Smith ierr = VecScatterBegin(scat_inact,Y_inact,Y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 550c2fc9fa9SBarry Smith ierr = VecScatterEnd(scat_inact,Y_inact,Y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 551c2fc9fa9SBarry Smith 552c2fc9fa9SBarry Smith ierr = VecDestroy(&F_inact);CHKERRQ(ierr); 553c2fc9fa9SBarry Smith ierr = VecDestroy(&Y_act);CHKERRQ(ierr); 554c2fc9fa9SBarry Smith ierr = VecDestroy(&Y_inact);CHKERRQ(ierr); 555c2fc9fa9SBarry Smith ierr = VecScatterDestroy(&scat_act);CHKERRQ(ierr); 556c2fc9fa9SBarry Smith ierr = VecScatterDestroy(&scat_inact);CHKERRQ(ierr); 557c2fc9fa9SBarry Smith ierr = ISDestroy(&IS_act);CHKERRQ(ierr); 558c2fc9fa9SBarry Smith if (!isequal) { 559c2fc9fa9SBarry Smith ierr = ISDestroy(&vi->IS_inact_prev);CHKERRQ(ierr); 560c2fc9fa9SBarry Smith ierr = ISDuplicate(IS_inact,&vi->IS_inact_prev);CHKERRQ(ierr); 561c2fc9fa9SBarry Smith } 562c2fc9fa9SBarry Smith ierr = ISDestroy(&IS_inact);CHKERRQ(ierr); 563c2fc9fa9SBarry Smith ierr = MatDestroy(&jac_inact_inact);CHKERRQ(ierr); 564c2fc9fa9SBarry Smith if (snes->jacobian != snes->jacobian_pre) { 565c2fc9fa9SBarry Smith ierr = MatDestroy(&prejac_inact_inact);CHKERRQ(ierr); 566c2fc9fa9SBarry Smith } 567c2fc9fa9SBarry Smith ierr = KSPGetIterationNumber(snes->ksp,&lits);CHKERRQ(ierr); 568c2fc9fa9SBarry Smith snes->linear_its += lits; 569c2fc9fa9SBarry Smith ierr = PetscInfo2(snes,"iter=%D, linear solve iterations=%D\n",snes->iter,lits);CHKERRQ(ierr); 570c2fc9fa9SBarry Smith /* 571c2fc9fa9SBarry Smith if (snes->ops->precheckstep) { 572c2fc9fa9SBarry Smith PetscBool changed_y = PETSC_FALSE; 573c2fc9fa9SBarry Smith ierr = (*snes->ops->precheckstep)(snes,X,Y,snes->precheck,&changed_y);CHKERRQ(ierr); 574c2fc9fa9SBarry Smith } 575c2fc9fa9SBarry Smith 576c2fc9fa9SBarry Smith if (PetscLogPrintInfo){ 577c2fc9fa9SBarry Smith ierr = SNESVICheckResidual_Private(snes,snes->jacobian,F,Y,G,W);CHKERRQ(ierr); 578c2fc9fa9SBarry Smith } 579c2fc9fa9SBarry Smith */ 580c2fc9fa9SBarry Smith /* Compute a (scaled) negative update in the line search routine: 581c2fc9fa9SBarry Smith Y <- X - lambda*Y 582c2fc9fa9SBarry Smith and evaluate G = function(Y) (depends on the line search). 583c2fc9fa9SBarry Smith */ 584c2fc9fa9SBarry Smith ierr = VecCopy(Y,snes->vec_sol_update);CHKERRQ(ierr); 585c2fc9fa9SBarry Smith ynorm = 1; gnorm = fnorm; 586f1c6b773SPeter Brune ierr = SNESLineSearchApply(snes->linesearch, X, F, &gnorm, Y);CHKERRQ(ierr); 587f1c6b773SPeter Brune ierr = SNESLineSearchGetNorms(snes->linesearch, &xnorm, &gnorm, &ynorm);CHKERRQ(ierr); 588c2fc9fa9SBarry Smith ierr = PetscInfo4(snes,"fnorm=%18.16e, gnorm=%18.16e, ynorm=%18.16e, lssucceed=%d\n",(double)fnorm,(double)gnorm,(double)ynorm,(int)lssucceed);CHKERRQ(ierr); 589c2fc9fa9SBarry Smith if (snes->reason == SNES_DIVERGED_FUNCTION_COUNT) break; 590c2fc9fa9SBarry Smith if (snes->domainerror) { 591c2fc9fa9SBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 592c2fc9fa9SBarry Smith ierr = DMDestroyVI(snes->dm);CHKERRQ(ierr); 593c2fc9fa9SBarry Smith PetscFunctionReturn(0); 594c2fc9fa9SBarry Smith } 595f1c6b773SPeter Brune ierr = SNESLineSearchGetSuccess(snes->linesearch, &lssucceed);CHKERRQ(ierr); 5969bd66eb0SPeter Brune 597c2fc9fa9SBarry Smith if (!lssucceed) { 598c2fc9fa9SBarry Smith if (++snes->numFailures >= snes->maxFailures) { 599c2fc9fa9SBarry Smith PetscBool ismin; 600c2fc9fa9SBarry Smith snes->reason = SNES_DIVERGED_LINE_SEARCH; 6019bd66eb0SPeter Brune ierr = SNESVICheckLocalMin_Private(snes,snes->jacobian,F,X,gnorm,&ismin);CHKERRQ(ierr); 602c2fc9fa9SBarry Smith if (ismin) snes->reason = SNES_DIVERGED_LOCAL_MIN; 603c2fc9fa9SBarry Smith break; 604c2fc9fa9SBarry Smith } 605c2fc9fa9SBarry Smith } 606c2fc9fa9SBarry Smith /* Update function and solution vectors */ 607c2fc9fa9SBarry Smith fnorm = gnorm; 608c2fc9fa9SBarry Smith /* Monitor convergence */ 609c2fc9fa9SBarry Smith ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr); 610c2fc9fa9SBarry Smith snes->iter = i+1; 611c2fc9fa9SBarry Smith snes->norm = fnorm; 612c2fc9fa9SBarry Smith ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr); 613c2fc9fa9SBarry Smith SNESLogConvHistory(snes,snes->norm,lits); 614c2fc9fa9SBarry Smith ierr = SNESMonitor(snes,snes->iter,snes->norm);CHKERRQ(ierr); 615c2fc9fa9SBarry Smith /* Test for convergence, xnorm = || X || */ 616c2fc9fa9SBarry Smith if (snes->ops->converged != SNESSkipConverged) { ierr = VecNorm(X,NORM_2,&xnorm);CHKERRQ(ierr); } 617c2fc9fa9SBarry Smith ierr = (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&snes->reason,snes->cnvP);CHKERRQ(ierr); 618c2fc9fa9SBarry Smith if (snes->reason) break; 619c2fc9fa9SBarry Smith } 620c2fc9fa9SBarry Smith ierr = DMDestroyVI(snes->dm);CHKERRQ(ierr); 621c2fc9fa9SBarry Smith if (i == maxits) { 622c2fc9fa9SBarry Smith ierr = PetscInfo1(snes,"Maximum number of iterations has been reached: %D\n",maxits);CHKERRQ(ierr); 623c2fc9fa9SBarry Smith if(!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT; 624c2fc9fa9SBarry Smith } 625c2fc9fa9SBarry Smith PetscFunctionReturn(0); 626c2fc9fa9SBarry Smith } 627c2fc9fa9SBarry Smith 628c2fc9fa9SBarry Smith #undef __FUNCT__ 629c2fc9fa9SBarry Smith #define __FUNCT__ "SNESVISetRedundancyCheck" 630c2fc9fa9SBarry Smith PetscErrorCode SNESVISetRedundancyCheck(SNES snes,PetscErrorCode (*func)(SNES,IS,IS*,void*),void *ctx) 631c2fc9fa9SBarry Smith { 632c2fc9fa9SBarry Smith SNES_VIRS *vi = (SNES_VIRS*)snes->data; 633c2fc9fa9SBarry Smith 634c2fc9fa9SBarry Smith PetscFunctionBegin; 635c2fc9fa9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 636c2fc9fa9SBarry Smith vi->checkredundancy = func; 637c2fc9fa9SBarry Smith vi->ctxP = ctx; 638c2fc9fa9SBarry Smith PetscFunctionReturn(0); 639c2fc9fa9SBarry Smith } 640c2fc9fa9SBarry Smith 641c2fc9fa9SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 642c2fc9fa9SBarry Smith #include <engine.h> 643c2fc9fa9SBarry Smith #include <mex.h> 644c2fc9fa9SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext; 645c2fc9fa9SBarry Smith 646c2fc9fa9SBarry Smith #undef __FUNCT__ 647c2fc9fa9SBarry Smith #define __FUNCT__ "SNESVIRedundancyCheck_Matlab" 648c2fc9fa9SBarry Smith PetscErrorCode SNESVIRedundancyCheck_Matlab(SNES snes,IS is_act,IS* is_redact,void* ctx) 649c2fc9fa9SBarry Smith { 650c2fc9fa9SBarry Smith PetscErrorCode ierr; 651c2fc9fa9SBarry Smith SNESMatlabContext *sctx = (SNESMatlabContext*)ctx; 652c2fc9fa9SBarry Smith int nlhs = 1, nrhs = 5; 653c2fc9fa9SBarry Smith mxArray *plhs[1], *prhs[5]; 654c2fc9fa9SBarry Smith long long int l1 = 0, l2 = 0, ls = 0; 655c2fc9fa9SBarry Smith PetscInt *indices=PETSC_NULL; 656c2fc9fa9SBarry Smith 657c2fc9fa9SBarry Smith PetscFunctionBegin; 658c2fc9fa9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 659c2fc9fa9SBarry Smith PetscValidHeaderSpecific(is_act,IS_CLASSID,2); 660c2fc9fa9SBarry Smith PetscValidPointer(is_redact,3); 661c2fc9fa9SBarry Smith PetscCheckSameComm(snes,1,is_act,2); 662c2fc9fa9SBarry Smith 663c2fc9fa9SBarry Smith /* Create IS for reduced active set of size 0, its size and indices will 664c2fc9fa9SBarry Smith bet set by the Matlab function */ 665c2fc9fa9SBarry Smith ierr = ISCreateGeneral(((PetscObject)snes)->comm,0,indices,PETSC_OWN_POINTER,is_redact);CHKERRQ(ierr); 666c2fc9fa9SBarry Smith /* call Matlab function in ctx */ 667c2fc9fa9SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 668c2fc9fa9SBarry Smith ierr = PetscMemcpy(&l1,&is_act,sizeof(is_act));CHKERRQ(ierr); 669c2fc9fa9SBarry Smith ierr = PetscMemcpy(&l2,is_redact,sizeof(is_act));CHKERRQ(ierr); 670c2fc9fa9SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 671c2fc9fa9SBarry Smith prhs[1] = mxCreateDoubleScalar((double)l1); 672c2fc9fa9SBarry Smith prhs[2] = mxCreateDoubleScalar((double)l2); 673c2fc9fa9SBarry Smith prhs[3] = mxCreateString(sctx->funcname); 674c2fc9fa9SBarry Smith prhs[4] = sctx->ctx; 675c2fc9fa9SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESVIRedundancyCheckInternal");CHKERRQ(ierr); 676c2fc9fa9SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 677c2fc9fa9SBarry Smith mxDestroyArray(prhs[0]); 678c2fc9fa9SBarry Smith mxDestroyArray(prhs[1]); 679c2fc9fa9SBarry Smith mxDestroyArray(prhs[2]); 680c2fc9fa9SBarry Smith mxDestroyArray(prhs[3]); 681c2fc9fa9SBarry Smith mxDestroyArray(plhs[0]); 682c2fc9fa9SBarry Smith PetscFunctionReturn(0); 683c2fc9fa9SBarry Smith } 684c2fc9fa9SBarry Smith 685c2fc9fa9SBarry Smith #undef __FUNCT__ 686c2fc9fa9SBarry Smith #define __FUNCT__ "SNESVISetRedundancyCheckMatlab" 687c2fc9fa9SBarry Smith PetscErrorCode SNESVISetRedundancyCheckMatlab(SNES snes,const char* func,mxArray* ctx) 688c2fc9fa9SBarry Smith { 689c2fc9fa9SBarry Smith PetscErrorCode ierr; 690c2fc9fa9SBarry Smith SNESMatlabContext *sctx; 691c2fc9fa9SBarry Smith 692c2fc9fa9SBarry Smith PetscFunctionBegin; 693c2fc9fa9SBarry Smith /* currently sctx is memory bleed */ 694c2fc9fa9SBarry Smith ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 695c2fc9fa9SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 696c2fc9fa9SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 697c2fc9fa9SBarry Smith ierr = SNESVISetRedundancyCheck(snes,SNESVIRedundancyCheck_Matlab,sctx);CHKERRQ(ierr); 698c2fc9fa9SBarry Smith PetscFunctionReturn(0); 699c2fc9fa9SBarry Smith } 700c2fc9fa9SBarry Smith 701c2fc9fa9SBarry Smith #endif 702c2fc9fa9SBarry Smith 703c2fc9fa9SBarry Smith /* -------------------------------------------------------------------------- */ 704c2fc9fa9SBarry Smith /* 705c2fc9fa9SBarry Smith SNESSetUp_VIRS - Sets up the internal data structures for the later use 706c2fc9fa9SBarry Smith of the SNESVI nonlinear solver. 707c2fc9fa9SBarry Smith 708c2fc9fa9SBarry Smith Input Parameter: 709c2fc9fa9SBarry Smith . snes - the SNES context 710c2fc9fa9SBarry Smith . x - the solution vector 711c2fc9fa9SBarry Smith 712c2fc9fa9SBarry Smith Application Interface Routine: SNESSetUp() 713c2fc9fa9SBarry Smith 714c2fc9fa9SBarry Smith Notes: 715c2fc9fa9SBarry Smith For basic use of the SNES solvers, the user need not explicitly call 716c2fc9fa9SBarry Smith SNESSetUp(), since these actions will automatically occur during 717c2fc9fa9SBarry Smith the call to SNESSolve(). 718c2fc9fa9SBarry Smith */ 719c2fc9fa9SBarry Smith #undef __FUNCT__ 720c2fc9fa9SBarry Smith #define __FUNCT__ "SNESSetUp_VIRS" 721c2fc9fa9SBarry Smith PetscErrorCode SNESSetUp_VIRS(SNES snes) 722c2fc9fa9SBarry Smith { 723c2fc9fa9SBarry Smith PetscErrorCode ierr; 724c2fc9fa9SBarry Smith SNES_VIRS *vi = (SNES_VIRS*) snes->data; 725c2fc9fa9SBarry Smith PetscInt *indices; 726c2fc9fa9SBarry Smith PetscInt i,n,rstart,rend; 727f1c6b773SPeter Brune SNESLineSearch linesearch; 728c2fc9fa9SBarry Smith 729c2fc9fa9SBarry Smith PetscFunctionBegin; 730c2fc9fa9SBarry Smith ierr = SNESSetUp_VI(snes);CHKERRQ(ierr); 731c2fc9fa9SBarry Smith 732c2fc9fa9SBarry Smith /* Set up previous active index set for the first snes solve 733c2fc9fa9SBarry Smith vi->IS_inact_prev = 0,1,2,....N */ 734c2fc9fa9SBarry Smith 735c2fc9fa9SBarry Smith ierr = VecGetOwnershipRange(snes->vec_sol,&rstart,&rend);CHKERRQ(ierr); 736c2fc9fa9SBarry Smith ierr = VecGetLocalSize(snes->vec_sol,&n);CHKERRQ(ierr); 737c2fc9fa9SBarry Smith ierr = PetscMalloc(n*sizeof(PetscInt),&indices);CHKERRQ(ierr); 738c2fc9fa9SBarry Smith for(i=0;i < n; i++) indices[i] = rstart + i; 739c2fc9fa9SBarry Smith ierr = ISCreateGeneral(((PetscObject)snes)->comm,n,indices,PETSC_OWN_POINTER,&vi->IS_inact_prev); 7409bd66eb0SPeter Brune 7419bd66eb0SPeter Brune /* set the line search functions */ 7429bd66eb0SPeter Brune if (!snes->linesearch) { 743f1c6b773SPeter Brune ierr = SNESGetSNESLineSearch(snes, &linesearch);CHKERRQ(ierr); 744f1c6b773SPeter Brune ierr = SNESLineSearchSetType(linesearch, SNES_LINESEARCH_BT);CHKERRQ(ierr); 7459bd66eb0SPeter Brune } 746c2fc9fa9SBarry Smith PetscFunctionReturn(0); 747c2fc9fa9SBarry Smith } 748c2fc9fa9SBarry Smith /* -------------------------------------------------------------------------- */ 749c2fc9fa9SBarry Smith #undef __FUNCT__ 750c2fc9fa9SBarry Smith #define __FUNCT__ "SNESReset_VIRS" 751c2fc9fa9SBarry Smith PetscErrorCode SNESReset_VIRS(SNES snes) 752c2fc9fa9SBarry Smith { 753c2fc9fa9SBarry Smith SNES_VIRS *vi = (SNES_VIRS*) snes->data; 754c2fc9fa9SBarry Smith PetscErrorCode ierr; 755c2fc9fa9SBarry Smith 756c2fc9fa9SBarry Smith PetscFunctionBegin; 757c2fc9fa9SBarry Smith ierr = SNESReset_VI(snes);CHKERRQ(ierr); 758c2fc9fa9SBarry Smith ierr = ISDestroy(&vi->IS_inact_prev);CHKERRQ(ierr); 759c2fc9fa9SBarry Smith PetscFunctionReturn(0); 760c2fc9fa9SBarry Smith } 761c2fc9fa9SBarry Smith 762c2fc9fa9SBarry Smith /* -------------------------------------------------------------------------- */ 763c2fc9fa9SBarry Smith /*MC 764c2fc9fa9SBarry Smith SNESVIRS - Reduced space active set solvers for variational inequalities based on Newton's method 765c2fc9fa9SBarry Smith 76661589011SJed Brown Options Database: 76761589011SJed Brown + -snes_vi_type <ss,rs,rsaug> a semi-smooth solver, a reduced space active set method, and a reduced space active set method that does not eliminate the active constraints from the Jacobian instead augments the Jacobian with additional variables that enforce the constraints 76861589011SJed Brown - -snes_vi_monitor - prints the number of active constraints at each iteration. 769c2fc9fa9SBarry Smith 770c2fc9fa9SBarry Smith Level: beginner 771c2fc9fa9SBarry Smith 772b80f3ac1SShri Abhyankar References: 773b80f3ac1SShri Abhyankar - T. S. Munson, and S. Benson. Flexible Complementarity Solvers for Large-Scale 774b80f3ac1SShri Abhyankar Applications, Optimization Methods and Software, 21 (2006). 775b80f3ac1SShri Abhyankar 77661589011SJed Brown .seealso: SNESVISetVariableBounds(), SNESVISetComputeVariableBounds(), SNESCreate(), SNES, SNESSetType(), SNESVIRS, SNESVISS, SNESTR, SNESLineSearchSet(), 777c2fc9fa9SBarry Smith SNESLineSearchSetPostCheck(), SNESLineSearchNo(), SNESLineSearchCubic(), SNESLineSearchQuadratic(), 778c2fc9fa9SBarry Smith SNESLineSearchSet(), SNESLineSearchNoNorms(), SNESLineSearchSetPreCheck(), SNESLineSearchSetParams(), SNESLineSearchGetParams() 779c2fc9fa9SBarry Smith 780c2fc9fa9SBarry Smith M*/ 781c2fc9fa9SBarry Smith EXTERN_C_BEGIN 782c2fc9fa9SBarry Smith #undef __FUNCT__ 783c2fc9fa9SBarry Smith #define __FUNCT__ "SNESCreate_VIRS" 784c2fc9fa9SBarry Smith PetscErrorCode SNESCreate_VIRS(SNES snes) 785c2fc9fa9SBarry Smith { 786c2fc9fa9SBarry Smith PetscErrorCode ierr; 787c2fc9fa9SBarry Smith SNES_VIRS *vi; 788c2fc9fa9SBarry Smith 789c2fc9fa9SBarry Smith PetscFunctionBegin; 790c2fc9fa9SBarry Smith snes->ops->reset = SNESReset_VIRS; 791c2fc9fa9SBarry Smith snes->ops->setup = SNESSetUp_VIRS; 792c2fc9fa9SBarry Smith snes->ops->solve = SNESSolve_VIRS; 793c2fc9fa9SBarry Smith snes->ops->destroy = SNESDestroy_VI; 794c2fc9fa9SBarry Smith snes->ops->setfromoptions = SNESSetFromOptions_VI; 795c2fc9fa9SBarry Smith snes->ops->view = PETSC_NULL; 796c2fc9fa9SBarry Smith snes->ops->converged = SNESDefaultConverged_VI; 797c2fc9fa9SBarry Smith 798c2fc9fa9SBarry Smith snes->usesksp = PETSC_TRUE; 799c2fc9fa9SBarry Smith snes->usespc = PETSC_FALSE; 800c2fc9fa9SBarry Smith 801c2fc9fa9SBarry Smith ierr = PetscNewLog(snes,SNES_VIRS,&vi);CHKERRQ(ierr); 802c2fc9fa9SBarry Smith snes->data = (void*)vi; 803c2fc9fa9SBarry Smith vi->checkredundancy = PETSC_NULL; 804c2fc9fa9SBarry Smith 80561589011SJed Brown ierr = PetscObjectComposeFunctionDynamic((PetscObject)snes,"SNESVISetVariableBounds_C","SNESVISetVariableBounds_VI",SNESVISetVariableBounds_VI);CHKERRQ(ierr); 80661589011SJed Brown ierr = PetscObjectComposeFunctionDynamic((PetscObject)snes,"SNESVISetComputeVariableBounds_C","SNESVISetComputeVariableBounds_VI",SNESVISetComputeVariableBounds_VI);CHKERRQ(ierr); 807c2fc9fa9SBarry Smith PetscFunctionReturn(0); 808c2fc9fa9SBarry Smith } 809c2fc9fa9SBarry Smith EXTERN_C_END 810c2fc9fa9SBarry Smith 811