1c2fc9fa9SBarry Smith 2c2fc9fa9SBarry Smith #include <../src/snes/impls/vi/rs/virsimpl.h> /*I "petscsnes.h" I*/ 3af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 4af0996ceSBarry Smith #include <petsc/private/vecimpl.h> 5c2fc9fa9SBarry Smith 6c2fc9fa9SBarry Smith /* 7c2fc9fa9SBarry Smith SNESVIGetInactiveSet - Gets the global indices for the inactive set variables (these correspond to the degrees of freedom the linear 8c2fc9fa9SBarry Smith system is solved on) 9c2fc9fa9SBarry Smith 107a7aea1fSJed Brown Input parameter: 11c2fc9fa9SBarry Smith . snes - the SNES context 12c2fc9fa9SBarry Smith 137a7aea1fSJed Brown Output parameter: 14d5f1b7e6SEd Bueler . inact - inactive set index set 15c2fc9fa9SBarry Smith 16c2fc9fa9SBarry Smith */ 17c2fc9fa9SBarry Smith PetscErrorCode SNESVIGetInactiveSet(SNES snes,IS *inact) 18c2fc9fa9SBarry Smith { 19f450aa47SBarry Smith SNES_VINEWTONRSLS *vi = (SNES_VINEWTONRSLS*)snes->data; 206e111a19SKarl Rupp 21c2fc9fa9SBarry Smith PetscFunctionBegin; 22f009fc93SPatrick Farrell *inact = vi->IS_inact; 23c2fc9fa9SBarry Smith PetscFunctionReturn(0); 24c2fc9fa9SBarry Smith } 25c2fc9fa9SBarry Smith 26c2fc9fa9SBarry Smith /* 27c2fc9fa9SBarry 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 28c2fc9fa9SBarry Smith defined by the reduced space method. 29c2fc9fa9SBarry Smith 30c2fc9fa9SBarry Smith Simple calls the regular DM interpolation and restricts it to operation on the variables not associated with active constraints. 31c2fc9fa9SBarry Smith 32d49cd2b7SBarry Smith */ 33c2fc9fa9SBarry Smith typedef struct { 34c2fc9fa9SBarry Smith PetscInt n; /* size of vectors in the reduced DM space */ 35c2fc9fa9SBarry Smith IS inactive; 36f5af7f23SKarl Rupp 3725296bd5SBarry Smith PetscErrorCode (*createinterpolation)(DM,DM,Mat*,Vec*); /* DM's original routines */ 38c2fc9fa9SBarry Smith PetscErrorCode (*coarsen)(DM, MPI_Comm, DM*); 39c2fc9fa9SBarry Smith PetscErrorCode (*createglobalvector)(DM,Vec*); 405a84ad33SLisandro Dalcin PetscErrorCode (*createinjection)(DM,DM,Mat*); 414a7a4c06SLawrence Mitchell PetscErrorCode (*hascreateinjection)(DM,PetscBool*); 42f5af7f23SKarl Rupp 43c2fc9fa9SBarry Smith DM dm; /* when destroying this object we need to reset the above function into the base DM */ 44c2fc9fa9SBarry Smith } DM_SNESVI; 45c2fc9fa9SBarry Smith 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 PetscContainer isnes; 53c2fc9fa9SBarry Smith DM_SNESVI *dmsnesvi; 54c2fc9fa9SBarry Smith 55c2fc9fa9SBarry Smith PetscFunctionBegin; 569566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm,"VI",(PetscObject*)&isnes)); 5728b400f6SJacob Faibussowitsch PetscCheck(isnes,PetscObjectComm((PetscObject)dm),PETSC_ERR_PLIB,"Composed SNES is missing"); 589566063dSJacob Faibussowitsch PetscCall(PetscContainerGetPointer(isnes,(void**)&dmsnesvi)); 599566063dSJacob Faibussowitsch PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)dm),dmsnesvi->n,PETSC_DETERMINE,vec)); 609566063dSJacob Faibussowitsch PetscCall(VecSetDM(*vec, dm)); 61c2fc9fa9SBarry Smith PetscFunctionReturn(0); 62c2fc9fa9SBarry Smith } 63c2fc9fa9SBarry Smith 64c2fc9fa9SBarry Smith /* 65e727c939SJed Brown DMCreateInterpolation_SNESVI - Modifieds the interpolation obtained from the DM by removing all rows and columns associated with active constraints. 66c2fc9fa9SBarry Smith 67c2fc9fa9SBarry Smith */ 68e727c939SJed Brown PetscErrorCode DMCreateInterpolation_SNESVI(DM dm1,DM dm2,Mat *mat,Vec *vec) 69c2fc9fa9SBarry Smith { 70c2fc9fa9SBarry Smith PetscContainer isnes; 71c2fc9fa9SBarry Smith DM_SNESVI *dmsnesvi1,*dmsnesvi2; 72c2fc9fa9SBarry Smith Mat interp; 73c2fc9fa9SBarry Smith 74c2fc9fa9SBarry Smith PetscFunctionBegin; 759566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm1,"VI",(PetscObject*)&isnes)); 7628b400f6SJacob Faibussowitsch PetscCheck(isnes,PetscObjectComm((PetscObject)dm1),PETSC_ERR_PLIB,"Composed VI data structure is missing"); 779566063dSJacob Faibussowitsch PetscCall(PetscContainerGetPointer(isnes,(void**)&dmsnesvi1)); 789566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm2,"VI",(PetscObject*)&isnes)); 7928b400f6SJacob Faibussowitsch PetscCheck(isnes,PetscObjectComm((PetscObject)dm2),PETSC_ERR_PLIB,"Composed VI data structure is missing"); 809566063dSJacob Faibussowitsch PetscCall(PetscContainerGetPointer(isnes,(void**)&dmsnesvi2)); 81c2fc9fa9SBarry Smith 829566063dSJacob Faibussowitsch PetscCall((*dmsnesvi1->createinterpolation)(dm1,dm2,&interp,NULL)); 839566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(interp,dmsnesvi2->inactive,dmsnesvi1->inactive,MAT_INITIAL_MATRIX,mat)); 849566063dSJacob Faibussowitsch PetscCall(MatDestroy(&interp)); 859e5d0892SLisandro Dalcin *vec = NULL; 86c2fc9fa9SBarry Smith PetscFunctionReturn(0); 87c2fc9fa9SBarry Smith } 88c2fc9fa9SBarry Smith 8925acbd8eSLisandro Dalcin static PetscErrorCode DMSetVI(DM,IS); 9025acbd8eSLisandro Dalcin static PetscErrorCode DMDestroyVI(DM); 91c2fc9fa9SBarry Smith 92c2fc9fa9SBarry Smith /* 93c2fc9fa9SBarry Smith DMCoarsen_SNESVI - Computes the regular coarsened DM then computes additional information about its inactive set 94c2fc9fa9SBarry Smith 95c2fc9fa9SBarry Smith */ 96c2fc9fa9SBarry Smith PetscErrorCode DMCoarsen_SNESVI(DM dm1,MPI_Comm comm,DM *dm2) 97c2fc9fa9SBarry Smith { 98c2fc9fa9SBarry Smith PetscContainer isnes; 99c2fc9fa9SBarry Smith DM_SNESVI *dmsnesvi1; 100c2fc9fa9SBarry Smith Vec finemarked,coarsemarked; 101c2fc9fa9SBarry Smith IS inactive; 1026dbf9973SLawrence Mitchell Mat inject; 103c2fc9fa9SBarry Smith const PetscInt *index; 104c2fc9fa9SBarry Smith PetscInt n,k,cnt = 0,rstart,*coarseindex; 105c2fc9fa9SBarry Smith PetscScalar *marked; 106c2fc9fa9SBarry Smith 107c2fc9fa9SBarry Smith PetscFunctionBegin; 1089566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm1,"VI",(PetscObject*)&isnes)); 10928b400f6SJacob Faibussowitsch PetscCheck(isnes,PetscObjectComm((PetscObject)dm1),PETSC_ERR_PLIB,"Composed VI data structure is missing"); 1109566063dSJacob Faibussowitsch PetscCall(PetscContainerGetPointer(isnes,(void**)&dmsnesvi1)); 111c2fc9fa9SBarry Smith 112c2fc9fa9SBarry Smith /* get the original coarsen */ 1139566063dSJacob Faibussowitsch PetscCall((*dmsnesvi1->coarsen)(dm1,comm,dm2)); 114c2fc9fa9SBarry Smith 115c2fc9fa9SBarry Smith /* not sure why this extra reference is needed, but without the dm2 disappears too early */ 11694c98981SBarry Smith /* Updating the KSPCreateVecs() to avoid using DMGetGlobalVector() when matrix is available removes the need for this reference? */ 1179566063dSJacob Faibussowitsch /* PetscCall(PetscObjectReference((PetscObject)*dm2));*/ 118c2fc9fa9SBarry Smith 119c2fc9fa9SBarry Smith /* need to set back global vectors in order to use the original injection */ 1209566063dSJacob Faibussowitsch PetscCall(DMClearGlobalVectors(dm1)); 1211aa26658SKarl Rupp 122c2fc9fa9SBarry Smith dm1->ops->createglobalvector = dmsnesvi1->createglobalvector; 1231aa26658SKarl Rupp 1249566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(dm1,&finemarked)); 1259566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(*dm2,&coarsemarked)); 126c2fc9fa9SBarry Smith 127c2fc9fa9SBarry Smith /* 128c2fc9fa9SBarry Smith fill finemarked with locations of inactive points 129c2fc9fa9SBarry Smith */ 1309566063dSJacob Faibussowitsch PetscCall(ISGetIndices(dmsnesvi1->inactive,&index)); 1319566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(dmsnesvi1->inactive,&n)); 1329566063dSJacob Faibussowitsch PetscCall(VecSet(finemarked,0.0)); 133c2fc9fa9SBarry Smith for (k=0; k<n; k++) { 1349566063dSJacob Faibussowitsch PetscCall(VecSetValue(finemarked,index[k],1.0,INSERT_VALUES)); 135c2fc9fa9SBarry Smith } 1369566063dSJacob Faibussowitsch PetscCall(VecAssemblyBegin(finemarked)); 1379566063dSJacob Faibussowitsch PetscCall(VecAssemblyEnd(finemarked)); 138c2fc9fa9SBarry Smith 1399566063dSJacob Faibussowitsch PetscCall(DMCreateInjection(*dm2,dm1,&inject)); 1409566063dSJacob Faibussowitsch PetscCall(MatRestrict(inject,finemarked,coarsemarked)); 1419566063dSJacob Faibussowitsch PetscCall(MatDestroy(&inject)); 142c2fc9fa9SBarry Smith 143c2fc9fa9SBarry Smith /* 144c2fc9fa9SBarry Smith create index set list of coarse inactive points from coarsemarked 145c2fc9fa9SBarry Smith */ 1469566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(coarsemarked,&n)); 1479566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(coarsemarked,&rstart,NULL)); 1489566063dSJacob Faibussowitsch PetscCall(VecGetArray(coarsemarked,&marked)); 149c2fc9fa9SBarry Smith for (k=0; k<n; k++) { 150c2fc9fa9SBarry Smith if (marked[k] != 0.0) cnt++; 151c2fc9fa9SBarry Smith } 1529566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(cnt,&coarseindex)); 153c2fc9fa9SBarry Smith cnt = 0; 154c2fc9fa9SBarry Smith for (k=0; k<n; k++) { 155c2fc9fa9SBarry Smith if (marked[k] != 0.0) coarseindex[cnt++] = k + rstart; 156c2fc9fa9SBarry Smith } 1579566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(coarsemarked,&marked)); 1589566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)coarsemarked),cnt,coarseindex,PETSC_OWN_POINTER,&inactive)); 159c2fc9fa9SBarry Smith 1609566063dSJacob Faibussowitsch PetscCall(DMClearGlobalVectors(dm1)); 1611aa26658SKarl Rupp 162c2fc9fa9SBarry Smith dm1->ops->createglobalvector = DMCreateGlobalVector_SNESVI; 1631aa26658SKarl Rupp 1649566063dSJacob Faibussowitsch PetscCall(DMSetVI(*dm2,inactive)); 165c2fc9fa9SBarry Smith 1669566063dSJacob Faibussowitsch PetscCall(VecDestroy(&finemarked)); 1679566063dSJacob Faibussowitsch PetscCall(VecDestroy(&coarsemarked)); 1689566063dSJacob Faibussowitsch PetscCall(ISDestroy(&inactive)); 169c2fc9fa9SBarry Smith PetscFunctionReturn(0); 170c2fc9fa9SBarry Smith } 171c2fc9fa9SBarry Smith 172c2fc9fa9SBarry Smith PetscErrorCode DMDestroy_SNESVI(DM_SNESVI *dmsnesvi) 173c2fc9fa9SBarry Smith { 174c2fc9fa9SBarry Smith PetscFunctionBegin; 175c2fc9fa9SBarry Smith /* reset the base methods in the DM object that were changed when the DM_SNESVI was reset */ 17625296bd5SBarry Smith dmsnesvi->dm->ops->createinterpolation = dmsnesvi->createinterpolation; 177c2fc9fa9SBarry Smith dmsnesvi->dm->ops->coarsen = dmsnesvi->coarsen; 178c2fc9fa9SBarry Smith dmsnesvi->dm->ops->createglobalvector = dmsnesvi->createglobalvector; 1795a84ad33SLisandro Dalcin dmsnesvi->dm->ops->createinjection = dmsnesvi->createinjection; 1804a7a4c06SLawrence Mitchell dmsnesvi->dm->ops->hascreateinjection = dmsnesvi->hascreateinjection; 181c2fc9fa9SBarry Smith /* need to clear out this vectors because some of them may not have a reference to the DM 182c2fc9fa9SBarry Smith but they are counted as having references to the DM in DMDestroy() */ 1839566063dSJacob Faibussowitsch PetscCall(DMClearGlobalVectors(dmsnesvi->dm)); 184c2fc9fa9SBarry Smith 1859566063dSJacob Faibussowitsch PetscCall(ISDestroy(&dmsnesvi->inactive)); 1869566063dSJacob Faibussowitsch PetscCall(PetscFree(dmsnesvi)); 187c2fc9fa9SBarry Smith PetscFunctionReturn(0); 188c2fc9fa9SBarry Smith } 189c2fc9fa9SBarry Smith 190c2fc9fa9SBarry Smith /* 191c2fc9fa9SBarry Smith DMSetVI - Marks a DM as associated with a VI problem. This causes the interpolation/restriction operators to 192c2fc9fa9SBarry Smith be restricted to only those variables NOT associated with active constraints. 193c2fc9fa9SBarry Smith 194c2fc9fa9SBarry Smith */ 19525acbd8eSLisandro Dalcin static PetscErrorCode DMSetVI(DM dm,IS inactive) 196c2fc9fa9SBarry Smith { 197c2fc9fa9SBarry Smith PetscContainer isnes; 198c2fc9fa9SBarry Smith DM_SNESVI *dmsnesvi; 199c2fc9fa9SBarry Smith 200c2fc9fa9SBarry Smith PetscFunctionBegin; 201c2fc9fa9SBarry Smith if (!dm) PetscFunctionReturn(0); 202c2fc9fa9SBarry Smith 2039566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)inactive)); 204c2fc9fa9SBarry Smith 2059566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm,"VI",(PetscObject*)&isnes)); 206c2fc9fa9SBarry Smith if (!isnes) { 2079566063dSJacob Faibussowitsch PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm),&isnes)); 2089566063dSJacob Faibussowitsch PetscCall(PetscContainerSetUserDestroy(isnes,(PetscErrorCode (*)(void*))DMDestroy_SNESVI)); 2099566063dSJacob Faibussowitsch PetscCall(PetscNew(&dmsnesvi)); 2109566063dSJacob Faibussowitsch PetscCall(PetscContainerSetPointer(isnes,(void*)dmsnesvi)); 2119566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)dm,"VI",(PetscObject)isnes)); 2129566063dSJacob Faibussowitsch PetscCall(PetscContainerDestroy(&isnes)); 2131aa26658SKarl Rupp 21425296bd5SBarry Smith dmsnesvi->createinterpolation = dm->ops->createinterpolation; 21525296bd5SBarry Smith dm->ops->createinterpolation = DMCreateInterpolation_SNESVI; 216c2fc9fa9SBarry Smith dmsnesvi->coarsen = dm->ops->coarsen; 217c2fc9fa9SBarry Smith dm->ops->coarsen = DMCoarsen_SNESVI; 218c2fc9fa9SBarry Smith dmsnesvi->createglobalvector = dm->ops->createglobalvector; 219c2fc9fa9SBarry Smith dm->ops->createglobalvector = DMCreateGlobalVector_SNESVI; 2205a84ad33SLisandro Dalcin dmsnesvi->createinjection = dm->ops->createinjection; 2215a84ad33SLisandro Dalcin dm->ops->createinjection = NULL; 2224a7a4c06SLawrence Mitchell dmsnesvi->hascreateinjection = dm->ops->hascreateinjection; 2235a84ad33SLisandro Dalcin dm->ops->hascreateinjection = NULL; 224c2fc9fa9SBarry Smith } else { 2259566063dSJacob Faibussowitsch PetscCall(PetscContainerGetPointer(isnes,(void**)&dmsnesvi)); 2269566063dSJacob Faibussowitsch PetscCall(ISDestroy(&dmsnesvi->inactive)); 227c2fc9fa9SBarry Smith } 2289566063dSJacob Faibussowitsch PetscCall(DMClearGlobalVectors(dm)); 2299566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(inactive,&dmsnesvi->n)); 2301aa26658SKarl Rupp 231c2fc9fa9SBarry Smith dmsnesvi->inactive = inactive; 232c2fc9fa9SBarry Smith dmsnesvi->dm = dm; 233c2fc9fa9SBarry Smith PetscFunctionReturn(0); 234c2fc9fa9SBarry Smith } 235c2fc9fa9SBarry Smith 236c2fc9fa9SBarry Smith /* 237c2fc9fa9SBarry Smith DMDestroyVI - Frees the DM_SNESVI object contained in the DM 23825296bd5SBarry Smith - also resets the function pointers in the DM for createinterpolation() etc to use the original DM 239c2fc9fa9SBarry Smith */ 24025acbd8eSLisandro Dalcin static PetscErrorCode DMDestroyVI(DM dm) 241c2fc9fa9SBarry Smith { 242c2fc9fa9SBarry Smith PetscFunctionBegin; 243c2fc9fa9SBarry Smith if (!dm) PetscFunctionReturn(0); 2449566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)dm,"VI",(PetscObject)NULL)); 245c2fc9fa9SBarry Smith PetscFunctionReturn(0); 246c2fc9fa9SBarry Smith } 247c2fc9fa9SBarry Smith 248c2fc9fa9SBarry Smith /* --------------------------------------------------------------------------------------------------------*/ 249c2fc9fa9SBarry Smith 250f450aa47SBarry Smith PetscErrorCode SNESCreateIndexSets_VINEWTONRSLS(SNES snes,Vec X,Vec F,IS *ISact,IS *ISinact) 251c2fc9fa9SBarry Smith { 252c2fc9fa9SBarry Smith PetscFunctionBegin; 2539566063dSJacob Faibussowitsch PetscCall(SNESVIGetActiveSetIS(snes,X,F,ISact)); 2549566063dSJacob Faibussowitsch PetscCall(ISComplement(*ISact,X->map->rstart,X->map->rend,ISinact)); 255c2fc9fa9SBarry Smith PetscFunctionReturn(0); 256c2fc9fa9SBarry Smith } 257c2fc9fa9SBarry Smith 258c2fc9fa9SBarry Smith /* Create active and inactive set vectors. The local size of this vector is set and petsc computes the global size */ 259f450aa47SBarry Smith PetscErrorCode SNESCreateSubVectors_VINEWTONRSLS(SNES snes,PetscInt n,Vec *newv) 260c2fc9fa9SBarry Smith { 261c2fc9fa9SBarry Smith Vec v; 262c2fc9fa9SBarry Smith 263c2fc9fa9SBarry Smith PetscFunctionBegin; 2649566063dSJacob Faibussowitsch PetscCall(VecCreate(PetscObjectComm((PetscObject)snes),&v)); 2659566063dSJacob Faibussowitsch PetscCall(VecSetSizes(v,n,PETSC_DECIDE)); 2669566063dSJacob Faibussowitsch PetscCall(VecSetType(v,VECSTANDARD)); 267c2fc9fa9SBarry Smith *newv = v; 268c2fc9fa9SBarry Smith PetscFunctionReturn(0); 269c2fc9fa9SBarry Smith } 270c2fc9fa9SBarry Smith 271c2fc9fa9SBarry Smith /* Resets the snes PC and KSP when the active set sizes change */ 272c2fc9fa9SBarry Smith PetscErrorCode SNESVIResetPCandKSP(SNES snes,Mat Amat,Mat Pmat) 273c2fc9fa9SBarry Smith { 274c2fc9fa9SBarry Smith KSP snesksp; 275c2fc9fa9SBarry Smith 276c2fc9fa9SBarry Smith PetscFunctionBegin; 2779566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes,&snesksp)); 2789566063dSJacob Faibussowitsch PetscCall(KSPReset(snesksp)); 2799566063dSJacob Faibussowitsch PetscCall(KSPResetFromOptions(snesksp)); 280c2fc9fa9SBarry Smith 281c2fc9fa9SBarry Smith /* 282c2fc9fa9SBarry Smith KSP kspnew; 283c2fc9fa9SBarry Smith PC pcnew; 284ea799195SBarry Smith MatSolverType stype; 285c2fc9fa9SBarry Smith 2869566063dSJacob Faibussowitsch PetscCall(KSPCreate(PetscObjectComm((PetscObject)snes),&kspnew)); 287c2fc9fa9SBarry Smith kspnew->pc_side = snesksp->pc_side; 288c2fc9fa9SBarry Smith kspnew->rtol = snesksp->rtol; 289c2fc9fa9SBarry Smith kspnew->abstol = snesksp->abstol; 290c2fc9fa9SBarry Smith kspnew->max_it = snesksp->max_it; 2919566063dSJacob Faibussowitsch PetscCall(KSPSetType(kspnew,((PetscObject)snesksp)->type_name)); 2929566063dSJacob Faibussowitsch PetscCall(KSPGetPC(kspnew,&pcnew)); 2939566063dSJacob Faibussowitsch PetscCall(PCSetType(kspnew->pc,((PetscObject)snesksp->pc)->type_name)); 2949566063dSJacob Faibussowitsch PetscCall(PCSetOperators(kspnew->pc,Amat,Pmat)); 2959566063dSJacob Faibussowitsch PetscCall(PCFactorGetMatSolverType(snesksp->pc,&stype)); 2969566063dSJacob Faibussowitsch PetscCall(PCFactorSetMatSolverType(kspnew->pc,stype)); 2979566063dSJacob Faibussowitsch PetscCall(KSPDestroy(&snesksp)); 298c2fc9fa9SBarry Smith snes->ksp = kspnew; 2999566063dSJacob Faibussowitsch PetscCall(PetscLogObjectParent((PetscObject)snes,(PetscObject)kspnew)); 3009566063dSJacob Faibussowitsch PetscCall(KSPSetFromOptions(kspnew));*/ 301c2fc9fa9SBarry Smith PetscFunctionReturn(0); 302c2fc9fa9SBarry Smith } 303c2fc9fa9SBarry Smith 304c2fc9fa9SBarry Smith /* Variational Inequality solver using reduce space method. No semismooth algorithm is 305c2fc9fa9SBarry Smith implemented in this algorithm. It basically identifies the active constraints and does 306c2fc9fa9SBarry Smith a linear solve on the other variables (those not associated with the active constraints). */ 307f450aa47SBarry Smith PetscErrorCode SNESSolve_VINEWTONRSLS(SNES snes) 308c2fc9fa9SBarry Smith { 309f450aa47SBarry Smith SNES_VINEWTONRSLS *vi = (SNES_VINEWTONRSLS*)snes->data; 310c2fc9fa9SBarry Smith PetscInt maxits,i,lits; 311422a814eSBarry Smith SNESLineSearchReason lssucceed; 312c2fc9fa9SBarry Smith PetscReal fnorm,gnorm,xnorm=0,ynorm; 3139bd66eb0SPeter Brune Vec Y,X,F; 314c2fc9fa9SBarry Smith KSPConvergedReason kspreason; 31592e89061SBarry Smith KSP ksp; 31692e89061SBarry Smith PC pc; 317c2fc9fa9SBarry Smith 318c2fc9fa9SBarry Smith PetscFunctionBegin; 31992e89061SBarry Smith /* Multigrid must use Galerkin for coarse grids with active set/reduced space methods; cannot rediscretize on coarser grids*/ 3209566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes,&ksp)); 3219566063dSJacob Faibussowitsch PetscCall(KSPGetPC(ksp,&pc)); 3229566063dSJacob Faibussowitsch PetscCall(PCMGSetGalerkin(pc,PC_MG_GALERKIN_BOTH)); 32392e89061SBarry Smith 324c2fc9fa9SBarry Smith snes->numFailures = 0; 325c2fc9fa9SBarry Smith snes->numLinearSolveFailures = 0; 326c2fc9fa9SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 327c2fc9fa9SBarry Smith 328c2fc9fa9SBarry Smith maxits = snes->max_its; /* maximum number of iterations */ 329c2fc9fa9SBarry Smith X = snes->vec_sol; /* solution vector */ 330c2fc9fa9SBarry Smith F = snes->vec_func; /* residual vector */ 331c2fc9fa9SBarry Smith Y = snes->work[0]; /* work vectors */ 3329bd66eb0SPeter Brune 3339566063dSJacob Faibussowitsch PetscCall(SNESLineSearchSetVIFunctions(snes->linesearch, SNESVIProjectOntoBounds, SNESVIComputeInactiveSetFnorm)); 3349566063dSJacob Faibussowitsch PetscCall(SNESLineSearchSetVecs(snes->linesearch, X, NULL, NULL, NULL, NULL)); 3359566063dSJacob Faibussowitsch PetscCall(SNESLineSearchSetUp(snes->linesearch)); 336c2fc9fa9SBarry Smith 3379566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsTakeAccess((PetscObject)snes)); 338c2fc9fa9SBarry Smith snes->iter = 0; 339c2fc9fa9SBarry Smith snes->norm = 0.0; 3409566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsGrantAccess((PetscObject)snes)); 341c2fc9fa9SBarry Smith 3429566063dSJacob Faibussowitsch PetscCall(SNESVIProjectOntoBounds(snes,X)); 3439566063dSJacob Faibussowitsch PetscCall(SNESComputeFunction(snes,X,F)); 3449566063dSJacob Faibussowitsch PetscCall(SNESVIComputeInactiveSetFnorm(snes,F,X,&fnorm)); 3459566063dSJacob Faibussowitsch PetscCall(VecNorm(X,NORM_2,&xnorm)); /* xnorm <- ||x|| */ 346422a814eSBarry Smith SNESCheckFunctionNorm(snes,fnorm); 3479566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsTakeAccess((PetscObject)snes)); 348c2fc9fa9SBarry Smith snes->norm = fnorm; 3499566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsGrantAccess((PetscObject)snes)); 3509566063dSJacob Faibussowitsch PetscCall(SNESLogConvergenceHistory(snes,fnorm,0)); 3519566063dSJacob Faibussowitsch PetscCall(SNESMonitor(snes,0,fnorm)); 352c2fc9fa9SBarry Smith 353c2fc9fa9SBarry Smith /* test convergence */ 3549566063dSJacob Faibussowitsch PetscCall((*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP)); 355c2fc9fa9SBarry Smith if (snes->reason) PetscFunctionReturn(0); 356c2fc9fa9SBarry Smith 357c2fc9fa9SBarry Smith for (i=0; i<maxits; i++) { 358c2fc9fa9SBarry Smith 359f009fc93SPatrick Farrell IS IS_act; /* _act -> active set _inact -> inactive set */ 360c2fc9fa9SBarry Smith IS IS_redact; /* redundant active set */ 361c2fc9fa9SBarry Smith VecScatter scat_act,scat_inact; 362c2fc9fa9SBarry Smith PetscInt nis_act,nis_inact; 363c2fc9fa9SBarry Smith Vec Y_act,Y_inact,F_inact; 364c2fc9fa9SBarry Smith Mat jac_inact_inact,prejac_inact_inact; 365c2fc9fa9SBarry Smith PetscBool isequal; 366c2fc9fa9SBarry Smith 367c2fc9fa9SBarry Smith /* Call general purpose update function */ 368c2fc9fa9SBarry Smith if (snes->ops->update) { 3699566063dSJacob Faibussowitsch PetscCall((*snes->ops->update)(snes, snes->iter)); 370c2fc9fa9SBarry Smith } 3719566063dSJacob Faibussowitsch PetscCall(SNESComputeJacobian(snes,X,snes->jacobian,snes->jacobian_pre)); 37207b62357SFande Kong SNESCheckJacobianDomainerror(snes); 373c2fc9fa9SBarry Smith 374c2fc9fa9SBarry Smith /* Create active and inactive index sets */ 375c2fc9fa9SBarry Smith 376c2fc9fa9SBarry Smith /*original 3779566063dSJacob Faibussowitsch PetscCall(SNESVICreateIndexSets_RS(snes,X,F,&IS_act,&vi->IS_inact)); 378c2fc9fa9SBarry Smith */ 3799566063dSJacob Faibussowitsch PetscCall(SNESVIGetActiveSetIS(snes,X,F,&IS_act)); 380c2fc9fa9SBarry Smith 381c2fc9fa9SBarry Smith if (vi->checkredundancy) { 3829566063dSJacob Faibussowitsch PetscCall((*vi->checkredundancy)(snes,IS_act,&IS_redact,vi->ctxP)); 383c2fc9fa9SBarry Smith if (IS_redact) { 3849566063dSJacob Faibussowitsch PetscCall(ISSort(IS_redact)); 3859566063dSJacob Faibussowitsch PetscCall(ISComplement(IS_redact,X->map->rstart,X->map->rend,&vi->IS_inact)); 3869566063dSJacob Faibussowitsch PetscCall(ISDestroy(&IS_redact)); 3871aa26658SKarl Rupp } else { 3889566063dSJacob Faibussowitsch PetscCall(ISComplement(IS_act,X->map->rstart,X->map->rend,&vi->IS_inact)); 389c2fc9fa9SBarry Smith } 390c2fc9fa9SBarry Smith } else { 3919566063dSJacob Faibussowitsch PetscCall(ISComplement(IS_act,X->map->rstart,X->map->rend,&vi->IS_inact)); 392c2fc9fa9SBarry Smith } 393c2fc9fa9SBarry Smith 394c2fc9fa9SBarry Smith /* Create inactive set submatrix */ 3959566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(snes->jacobian,vi->IS_inact,vi->IS_inact,MAT_INITIAL_MATRIX,&jac_inact_inact)); 396c2fc9fa9SBarry Smith 39766bfb381SJed Brown if (0) { /* Dead code (temporary developer hack) */ 39866bfb381SJed Brown IS keptrows; 3999566063dSJacob Faibussowitsch PetscCall(MatFindNonzeroRows(jac_inact_inact,&keptrows)); 40066bfb381SJed Brown if (keptrows) { 401c2fc9fa9SBarry Smith PetscInt cnt,*nrows,k; 402c2fc9fa9SBarry Smith const PetscInt *krows,*inact; 403367daffbSBarry Smith PetscInt rstart; 404c2fc9fa9SBarry Smith 4059566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(jac_inact_inact,&rstart,NULL)); 4069566063dSJacob Faibussowitsch PetscCall(MatDestroy(&jac_inact_inact)); 4079566063dSJacob Faibussowitsch PetscCall(ISDestroy(&IS_act)); 408c2fc9fa9SBarry Smith 4099566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(keptrows,&cnt)); 4109566063dSJacob Faibussowitsch PetscCall(ISGetIndices(keptrows,&krows)); 4119566063dSJacob Faibussowitsch PetscCall(ISGetIndices(vi->IS_inact,&inact)); 4129566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(cnt,&nrows)); 4131aa26658SKarl Rupp for (k=0; k<cnt; k++) nrows[k] = inact[krows[k]-rstart]; 4149566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(keptrows,&krows)); 4159566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(vi->IS_inact,&inact)); 4169566063dSJacob Faibussowitsch PetscCall(ISDestroy(&keptrows)); 4179566063dSJacob Faibussowitsch PetscCall(ISDestroy(&vi->IS_inact)); 418c2fc9fa9SBarry Smith 4199566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)snes),cnt,nrows,PETSC_OWN_POINTER,&vi->IS_inact)); 4209566063dSJacob Faibussowitsch PetscCall(ISComplement(vi->IS_inact,F->map->rstart,F->map->rend,&IS_act)); 4219566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(snes->jacobian,vi->IS_inact,vi->IS_inact,MAT_INITIAL_MATRIX,&jac_inact_inact)); 422c2fc9fa9SBarry Smith } 42366bfb381SJed Brown } 4249566063dSJacob Faibussowitsch PetscCall(DMSetVI(snes->dm,vi->IS_inact)); 425c2fc9fa9SBarry Smith /* remove later */ 426c2fc9fa9SBarry Smith 427c2fc9fa9SBarry Smith /* 4289566063dSJacob Faibussowitsch PetscCall(VecView(vi->xu,PETSC_VIEWER_BINARY_(((PetscObject)(vi->xu))->comm))); 4299566063dSJacob Faibussowitsch PetscCall(VecView(vi->xl,PETSC_VIEWER_BINARY_(((PetscObject)(vi->xl))->comm))); 4309566063dSJacob Faibussowitsch PetscCall(VecView(X,PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)X)))); 4319566063dSJacob Faibussowitsch PetscCall(VecView(F,PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)F)))); 4329566063dSJacob Faibussowitsch PetscCall(ISView(vi->IS_inact,PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)vi->IS_inact)))); 433c2fc9fa9SBarry Smith */ 434c2fc9fa9SBarry Smith 435c2fc9fa9SBarry Smith /* Get sizes of active and inactive sets */ 4369566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(IS_act,&nis_act)); 4379566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(vi->IS_inact,&nis_inact)); 438c2fc9fa9SBarry Smith 439c2fc9fa9SBarry Smith /* Create active and inactive set vectors */ 4409566063dSJacob Faibussowitsch PetscCall(SNESCreateSubVectors_VINEWTONRSLS(snes,nis_inact,&F_inact)); 4419566063dSJacob Faibussowitsch PetscCall(SNESCreateSubVectors_VINEWTONRSLS(snes,nis_act,&Y_act)); 4429566063dSJacob Faibussowitsch PetscCall(SNESCreateSubVectors_VINEWTONRSLS(snes,nis_inact,&Y_inact)); 443c2fc9fa9SBarry Smith 444c2fc9fa9SBarry Smith /* Create scatter contexts */ 4459566063dSJacob Faibussowitsch PetscCall(VecScatterCreate(Y,IS_act,Y_act,NULL,&scat_act)); 4469566063dSJacob Faibussowitsch PetscCall(VecScatterCreate(Y,vi->IS_inact,Y_inact,NULL,&scat_inact)); 447c2fc9fa9SBarry Smith 448c2fc9fa9SBarry Smith /* Do a vec scatter to active and inactive set vectors */ 4499566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(scat_inact,F,F_inact,INSERT_VALUES,SCATTER_FORWARD)); 4509566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(scat_inact,F,F_inact,INSERT_VALUES,SCATTER_FORWARD)); 451c2fc9fa9SBarry Smith 4529566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(scat_act,Y,Y_act,INSERT_VALUES,SCATTER_FORWARD)); 4539566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(scat_act,Y,Y_act,INSERT_VALUES,SCATTER_FORWARD)); 454c2fc9fa9SBarry Smith 4559566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(scat_inact,Y,Y_inact,INSERT_VALUES,SCATTER_FORWARD)); 4569566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(scat_inact,Y,Y_inact,INSERT_VALUES,SCATTER_FORWARD)); 457c2fc9fa9SBarry Smith 458c2fc9fa9SBarry Smith /* Active set direction = 0 */ 4599566063dSJacob Faibussowitsch PetscCall(VecSet(Y_act,0)); 460c2fc9fa9SBarry Smith if (snes->jacobian != snes->jacobian_pre) { 4619566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(snes->jacobian_pre,vi->IS_inact,vi->IS_inact,MAT_INITIAL_MATRIX,&prejac_inact_inact)); 462c2fc9fa9SBarry Smith } else prejac_inact_inact = jac_inact_inact; 463c2fc9fa9SBarry Smith 4649566063dSJacob Faibussowitsch PetscCall(ISEqual(vi->IS_inact_prev,vi->IS_inact,&isequal)); 465c2fc9fa9SBarry Smith if (!isequal) { 4669566063dSJacob Faibussowitsch PetscCall(SNESVIResetPCandKSP(snes,jac_inact_inact,prejac_inact_inact)); 4679566063dSJacob Faibussowitsch PetscCall(PCFieldSplitRestrictIS(pc,vi->IS_inact)); 468c2fc9fa9SBarry Smith } 469c2fc9fa9SBarry Smith 4709566063dSJacob Faibussowitsch /* PetscCall(ISView(vi->IS_inact,0)); */ 4719566063dSJacob Faibussowitsch /* PetscCall(ISView(IS_act,0));*/ 472c2fc9fa9SBarry Smith /* ierr = MatView(snes->jacobian_pre,0); */ 473c2fc9fa9SBarry Smith 4749566063dSJacob Faibussowitsch PetscCall(KSPSetOperators(snes->ksp,jac_inact_inact,prejac_inact_inact)); 4759566063dSJacob Faibussowitsch PetscCall(KSPSetUp(snes->ksp)); 476c2fc9fa9SBarry Smith { 477c2fc9fa9SBarry Smith PC pc; 478c2fc9fa9SBarry Smith PetscBool flg; 4799566063dSJacob Faibussowitsch PetscCall(KSPGetPC(snes->ksp,&pc)); 4809566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&flg)); 481c2fc9fa9SBarry Smith if (flg) { 482c2fc9fa9SBarry Smith KSP *subksps; 4839566063dSJacob Faibussowitsch PetscCall(PCFieldSplitGetSubKSP(pc,NULL,&subksps)); 4849566063dSJacob Faibussowitsch PetscCall(KSPGetPC(subksps[0],&pc)); 4859566063dSJacob Faibussowitsch PetscCall(PetscFree(subksps)); 4869566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)pc,PCBJACOBI,&flg)); 487c2fc9fa9SBarry Smith if (flg) { 488c2fc9fa9SBarry Smith PetscInt n,N = 101*101,j,cnts[3] = {0,0,0}; 489c2fc9fa9SBarry Smith const PetscInt *ii; 490c2fc9fa9SBarry Smith 4919566063dSJacob Faibussowitsch PetscCall(ISGetSize(vi->IS_inact,&n)); 4929566063dSJacob Faibussowitsch PetscCall(ISGetIndices(vi->IS_inact,&ii)); 493c2fc9fa9SBarry Smith for (j=0; j<n; j++) { 494c2fc9fa9SBarry Smith if (ii[j] < N) cnts[0]++; 495c2fc9fa9SBarry Smith else if (ii[j] < 2*N) cnts[1]++; 496c2fc9fa9SBarry Smith else if (ii[j] < 3*N) cnts[2]++; 497c2fc9fa9SBarry Smith } 4989566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(vi->IS_inact,&ii)); 499c2fc9fa9SBarry Smith 5009566063dSJacob Faibussowitsch PetscCall(PCBJacobiSetTotalBlocks(pc,3,cnts)); 501c2fc9fa9SBarry Smith } 502c2fc9fa9SBarry Smith } 503c2fc9fa9SBarry Smith } 504c2fc9fa9SBarry Smith 5059566063dSJacob Faibussowitsch PetscCall(KSPSolve(snes->ksp,F_inact,Y_inact)); 5069566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(scat_act,Y_act,Y,INSERT_VALUES,SCATTER_REVERSE)); 5079566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(scat_act,Y_act,Y,INSERT_VALUES,SCATTER_REVERSE)); 5089566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(scat_inact,Y_inact,Y,INSERT_VALUES,SCATTER_REVERSE)); 5099566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(scat_inact,Y_inact,Y,INSERT_VALUES,SCATTER_REVERSE)); 510c2fc9fa9SBarry Smith 5119566063dSJacob Faibussowitsch PetscCall(VecDestroy(&F_inact)); 5129566063dSJacob Faibussowitsch PetscCall(VecDestroy(&Y_act)); 5139566063dSJacob Faibussowitsch PetscCall(VecDestroy(&Y_inact)); 5149566063dSJacob Faibussowitsch PetscCall(VecScatterDestroy(&scat_act)); 5159566063dSJacob Faibussowitsch PetscCall(VecScatterDestroy(&scat_inact)); 5169566063dSJacob Faibussowitsch PetscCall(ISDestroy(&IS_act)); 517c2fc9fa9SBarry Smith if (!isequal) { 5189566063dSJacob Faibussowitsch PetscCall(ISDestroy(&vi->IS_inact_prev)); 5199566063dSJacob Faibussowitsch PetscCall(ISDuplicate(vi->IS_inact,&vi->IS_inact_prev)); 520c2fc9fa9SBarry Smith } 5219566063dSJacob Faibussowitsch PetscCall(ISDestroy(&vi->IS_inact)); 5229566063dSJacob Faibussowitsch PetscCall(MatDestroy(&jac_inact_inact)); 523c2fc9fa9SBarry Smith if (snes->jacobian != snes->jacobian_pre) { 5249566063dSJacob Faibussowitsch PetscCall(MatDestroy(&prejac_inact_inact)); 525c2fc9fa9SBarry Smith } 526fa6eefd1SCian Wilson 5279566063dSJacob Faibussowitsch PetscCall(KSPGetConvergedReason(snes->ksp,&kspreason)); 528fa6eefd1SCian Wilson if (kspreason < 0) { 529fa6eefd1SCian Wilson if (++snes->numLinearSolveFailures >= snes->maxLinearSolveFailures) { 53063a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(snes,"iter=%" PetscInt_FMT ", number linear solve failures %" PetscInt_FMT " greater than current SNES allowed, stopping solve\n",snes->iter,snes->numLinearSolveFailures)); 531fa6eefd1SCian Wilson snes->reason = SNES_DIVERGED_LINEAR_SOLVE; 532fa6eefd1SCian Wilson break; 533fa6eefd1SCian Wilson } 534fa6eefd1SCian Wilson } 535fa6eefd1SCian Wilson 5369566063dSJacob Faibussowitsch PetscCall(KSPGetIterationNumber(snes->ksp,&lits)); 537c2fc9fa9SBarry Smith snes->linear_its += lits; 53863a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(snes,"iter=%" PetscInt_FMT ", linear solve iterations=%" PetscInt_FMT "\n",snes->iter,lits)); 539c2fc9fa9SBarry Smith /* 5406b2b7091SBarry Smith if (snes->ops->precheck) { 541c2fc9fa9SBarry Smith PetscBool changed_y = PETSC_FALSE; 5429566063dSJacob Faibussowitsch PetscCall((*snes->ops->precheck)(snes,X,Y,snes->precheck,&changed_y)); 543c2fc9fa9SBarry Smith } 544c2fc9fa9SBarry Smith 545c2fc9fa9SBarry Smith if (PetscLogPrintInfo) { 5469566063dSJacob Faibussowitsch PetscCall(SNESVICheckResidual_Private(snes,snes->jacobian,F,Y,G,W)); 547c2fc9fa9SBarry Smith } 548c2fc9fa9SBarry Smith */ 549c2fc9fa9SBarry Smith /* Compute a (scaled) negative update in the line search routine: 550c2fc9fa9SBarry Smith Y <- X - lambda*Y 551c2fc9fa9SBarry Smith and evaluate G = function(Y) (depends on the line search). 552c2fc9fa9SBarry Smith */ 5539566063dSJacob Faibussowitsch PetscCall(VecCopy(Y,snes->vec_sol_update)); 554c2fc9fa9SBarry Smith ynorm = 1; gnorm = fnorm; 5559566063dSJacob Faibussowitsch PetscCall(SNESLineSearchApply(snes->linesearch, X, F, &gnorm, Y)); 5569566063dSJacob Faibussowitsch PetscCall(SNESLineSearchGetReason(snes->linesearch, &lssucceed)); 5579566063dSJacob Faibussowitsch PetscCall(SNESLineSearchGetNorms(snes->linesearch, &xnorm, &gnorm, &ynorm)); 5589566063dSJacob Faibussowitsch PetscCall(PetscInfo(snes,"fnorm=%18.16e, gnorm=%18.16e, ynorm=%18.16e, lssucceed=%d\n",(double)fnorm,(double)gnorm,(double)ynorm,(int)lssucceed)); 559c2fc9fa9SBarry Smith if (snes->reason == SNES_DIVERGED_FUNCTION_COUNT) break; 560c2fc9fa9SBarry Smith if (snes->domainerror) { 561c2fc9fa9SBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 5629566063dSJacob Faibussowitsch PetscCall(DMDestroyVI(snes->dm)); 563c2fc9fa9SBarry Smith PetscFunctionReturn(0); 564c2fc9fa9SBarry Smith } 565422a814eSBarry Smith if (lssucceed) { 566c2fc9fa9SBarry Smith if (++snes->numFailures >= snes->maxFailures) { 567c2fc9fa9SBarry Smith PetscBool ismin; 568c2fc9fa9SBarry Smith snes->reason = SNES_DIVERGED_LINE_SEARCH; 5699566063dSJacob Faibussowitsch PetscCall(SNESVICheckLocalMin_Private(snes,snes->jacobian,F,X,gnorm,&ismin)); 570c2fc9fa9SBarry Smith if (ismin) snes->reason = SNES_DIVERGED_LOCAL_MIN; 571c2fc9fa9SBarry Smith break; 572c2fc9fa9SBarry Smith } 573c2fc9fa9SBarry Smith } 5749566063dSJacob Faibussowitsch PetscCall(DMDestroyVI(snes->dm)); 575c2fc9fa9SBarry Smith /* Update function and solution vectors */ 576c2fc9fa9SBarry Smith fnorm = gnorm; 577c2fc9fa9SBarry Smith /* Monitor convergence */ 5789566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsTakeAccess((PetscObject)snes)); 579c2fc9fa9SBarry Smith snes->iter = i+1; 580c2fc9fa9SBarry Smith snes->norm = fnorm; 581c1e67a49SFande Kong snes->xnorm = xnorm; 582c1e67a49SFande Kong snes->ynorm = ynorm; 5839566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsGrantAccess((PetscObject)snes)); 5849566063dSJacob Faibussowitsch PetscCall(SNESLogConvergenceHistory(snes,snes->norm,lits)); 5859566063dSJacob Faibussowitsch PetscCall(SNESMonitor(snes,snes->iter,snes->norm)); 586c2fc9fa9SBarry Smith /* Test for convergence, xnorm = || X || */ 5879566063dSJacob Faibussowitsch if (snes->ops->converged != SNESConvergedSkip) PetscCall(VecNorm(X,NORM_2,&xnorm)); 5889566063dSJacob Faibussowitsch PetscCall((*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&snes->reason,snes->cnvP)); 589c2fc9fa9SBarry Smith if (snes->reason) break; 590c2fc9fa9SBarry Smith } 59191a42fcfSBarry Smith /* make sure that the VI information attached to the DM is removed if the for loop above was broken early due to some exceptional conditional */ 5929566063dSJacob Faibussowitsch PetscCall(DMDestroyVI(snes->dm)); 593c2fc9fa9SBarry Smith if (i == maxits) { 59463a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(snes,"Maximum number of iterations has been reached: %" PetscInt_FMT "\n",maxits)); 595c2fc9fa9SBarry Smith if (!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT; 596c2fc9fa9SBarry Smith } 597c2fc9fa9SBarry Smith PetscFunctionReturn(0); 598c2fc9fa9SBarry Smith } 599c2fc9fa9SBarry Smith 600c2fc9fa9SBarry Smith PetscErrorCode SNESVISetRedundancyCheck(SNES snes,PetscErrorCode (*func)(SNES,IS,IS*,void*),void *ctx) 601c2fc9fa9SBarry Smith { 602f450aa47SBarry Smith SNES_VINEWTONRSLS *vi = (SNES_VINEWTONRSLS*)snes->data; 603c2fc9fa9SBarry Smith 604c2fc9fa9SBarry Smith PetscFunctionBegin; 605c2fc9fa9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 606c2fc9fa9SBarry Smith vi->checkredundancy = func; 607c2fc9fa9SBarry Smith vi->ctxP = ctx; 608c2fc9fa9SBarry Smith PetscFunctionReturn(0); 609c2fc9fa9SBarry Smith } 610c2fc9fa9SBarry Smith 611c2fc9fa9SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 612c2fc9fa9SBarry Smith #include <engine.h> 613c2fc9fa9SBarry Smith #include <mex.h> 614c2fc9fa9SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext; 615c2fc9fa9SBarry Smith 616c2fc9fa9SBarry Smith PetscErrorCode SNESVIRedundancyCheck_Matlab(SNES snes,IS is_act,IS *is_redact,void *ctx) 617c2fc9fa9SBarry Smith { 618c2fc9fa9SBarry Smith SNESMatlabContext *sctx = (SNESMatlabContext*)ctx; 619c2fc9fa9SBarry Smith int nlhs = 1, nrhs = 5; 620c2fc9fa9SBarry Smith mxArray *plhs[1], *prhs[5]; 621c2fc9fa9SBarry Smith long long int l1 = 0, l2 = 0, ls = 0; 6220298fd71SBarry Smith PetscInt *indices=NULL; 623c2fc9fa9SBarry Smith 624c2fc9fa9SBarry Smith PetscFunctionBegin; 625c2fc9fa9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 626c2fc9fa9SBarry Smith PetscValidHeaderSpecific(is_act,IS_CLASSID,2); 627c2fc9fa9SBarry Smith PetscValidPointer(is_redact,3); 628c2fc9fa9SBarry Smith PetscCheckSameComm(snes,1,is_act,2); 629c2fc9fa9SBarry Smith 630c2fc9fa9SBarry Smith /* Create IS for reduced active set of size 0, its size and indices will 631c2fc9fa9SBarry Smith bet set by the Matlab function */ 6329566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)snes),0,indices,PETSC_OWN_POINTER,is_redact)); 633c2fc9fa9SBarry Smith /* call Matlab function in ctx */ 6349566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(&ls,&snes,1)); 6359566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(&l1,&is_act,1)); 6369566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(&l2,is_redact,1)); 637c2fc9fa9SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 638c2fc9fa9SBarry Smith prhs[1] = mxCreateDoubleScalar((double)l1); 639c2fc9fa9SBarry Smith prhs[2] = mxCreateDoubleScalar((double)l2); 640c2fc9fa9SBarry Smith prhs[3] = mxCreateString(sctx->funcname); 641c2fc9fa9SBarry Smith prhs[4] = sctx->ctx; 6429566063dSJacob Faibussowitsch PetscCall(mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESVIRedundancyCheckInternal")); 6439566063dSJacob Faibussowitsch PetscCall(mxGetScalar(plhs[0])); 644c2fc9fa9SBarry Smith mxDestroyArray(prhs[0]); 645c2fc9fa9SBarry Smith mxDestroyArray(prhs[1]); 646c2fc9fa9SBarry Smith mxDestroyArray(prhs[2]); 647c2fc9fa9SBarry Smith mxDestroyArray(prhs[3]); 648c2fc9fa9SBarry Smith mxDestroyArray(plhs[0]); 649c2fc9fa9SBarry Smith PetscFunctionReturn(0); 650c2fc9fa9SBarry Smith } 651c2fc9fa9SBarry Smith 652c2fc9fa9SBarry Smith PetscErrorCode SNESVISetRedundancyCheckMatlab(SNES snes,const char *func,mxArray *ctx) 653c2fc9fa9SBarry Smith { 654c2fc9fa9SBarry Smith SNESMatlabContext *sctx; 655c2fc9fa9SBarry Smith 656c2fc9fa9SBarry Smith PetscFunctionBegin; 657c2fc9fa9SBarry Smith /* currently sctx is memory bleed */ 6589566063dSJacob Faibussowitsch PetscCall(PetscNew(&sctx)); 6599566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(func,&sctx->funcname)); 660c2fc9fa9SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6619566063dSJacob Faibussowitsch PetscCall(SNESVISetRedundancyCheck(snes,SNESVIRedundancyCheck_Matlab,sctx)); 662c2fc9fa9SBarry Smith PetscFunctionReturn(0); 663c2fc9fa9SBarry Smith } 664c2fc9fa9SBarry Smith 665c2fc9fa9SBarry Smith #endif 666c2fc9fa9SBarry Smith 667c2fc9fa9SBarry Smith /* -------------------------------------------------------------------------- */ 668c2fc9fa9SBarry Smith /* 669f450aa47SBarry Smith SNESSetUp_VINEWTONRSLS - Sets up the internal data structures for the later use 670c2fc9fa9SBarry Smith of the SNESVI nonlinear solver. 671c2fc9fa9SBarry Smith 672c2fc9fa9SBarry Smith Input Parameter: 673c2fc9fa9SBarry Smith . snes - the SNES context 674c2fc9fa9SBarry Smith 675c2fc9fa9SBarry Smith Application Interface Routine: SNESSetUp() 676c2fc9fa9SBarry Smith 677c2fc9fa9SBarry Smith Notes: 678c2fc9fa9SBarry Smith For basic use of the SNES solvers, the user need not explicitly call 679c2fc9fa9SBarry Smith SNESSetUp(), since these actions will automatically occur during 680c2fc9fa9SBarry Smith the call to SNESSolve(). 681c2fc9fa9SBarry Smith */ 682f450aa47SBarry Smith PetscErrorCode SNESSetUp_VINEWTONRSLS(SNES snes) 683c2fc9fa9SBarry Smith { 684f450aa47SBarry Smith SNES_VINEWTONRSLS *vi = (SNES_VINEWTONRSLS*) snes->data; 685c2fc9fa9SBarry Smith PetscInt *indices; 686c2fc9fa9SBarry Smith PetscInt i,n,rstart,rend; 687f1c6b773SPeter Brune SNESLineSearch linesearch; 688c2fc9fa9SBarry Smith 689c2fc9fa9SBarry Smith PetscFunctionBegin; 6909566063dSJacob Faibussowitsch PetscCall(SNESSetUp_VI(snes)); 691c2fc9fa9SBarry Smith 692c2fc9fa9SBarry Smith /* Set up previous active index set for the first snes solve 693c2fc9fa9SBarry Smith vi->IS_inact_prev = 0,1,2,....N */ 694c2fc9fa9SBarry Smith 6959566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(snes->vec_sol,&rstart,&rend)); 6969566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(snes->vec_sol,&n)); 6979566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n,&indices)); 698c2fc9fa9SBarry Smith for (i=0; i < n; i++) indices[i] = rstart + i; 6999566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)snes),n,indices,PETSC_OWN_POINTER,&vi->IS_inact_prev)); 7009bd66eb0SPeter Brune 7019bd66eb0SPeter Brune /* set the line search functions */ 7029bd66eb0SPeter Brune if (!snes->linesearch) { 7039566063dSJacob Faibussowitsch PetscCall(SNESGetLineSearch(snes, &linesearch)); 7049566063dSJacob Faibussowitsch PetscCall(SNESLineSearchSetType(linesearch, SNESLINESEARCHBT)); 7059bd66eb0SPeter Brune } 706c2fc9fa9SBarry Smith PetscFunctionReturn(0); 707c2fc9fa9SBarry Smith } 708c2fc9fa9SBarry Smith /* -------------------------------------------------------------------------- */ 709f450aa47SBarry Smith PetscErrorCode SNESReset_VINEWTONRSLS(SNES snes) 710c2fc9fa9SBarry Smith { 711f450aa47SBarry Smith SNES_VINEWTONRSLS *vi = (SNES_VINEWTONRSLS*) snes->data; 712c2fc9fa9SBarry Smith 713c2fc9fa9SBarry Smith PetscFunctionBegin; 7149566063dSJacob Faibussowitsch PetscCall(SNESReset_VI(snes)); 7159566063dSJacob Faibussowitsch PetscCall(ISDestroy(&vi->IS_inact_prev)); 716c2fc9fa9SBarry Smith PetscFunctionReturn(0); 717c2fc9fa9SBarry Smith } 718c2fc9fa9SBarry Smith 719c2fc9fa9SBarry Smith /* -------------------------------------------------------------------------- */ 720c2fc9fa9SBarry Smith /*MC 721f450aa47SBarry Smith SNESVINEWTONRSLS - Reduced space active set solvers for variational inequalities based on Newton's method 722c2fc9fa9SBarry Smith 72361589011SJed Brown Options Database: 724b621fa8fSRichard Tran Mills + -snes_type <vinewtonssls,vinewtonrsls> - a semi-smooth solver, a reduced space active set method 72561589011SJed Brown - -snes_vi_monitor - prints the number of active constraints at each iteration. 726c2fc9fa9SBarry Smith 727c2fc9fa9SBarry Smith Level: beginner 728c2fc9fa9SBarry Smith 729b80f3ac1SShri Abhyankar References: 730606c0280SSatish Balay . * - T. S. Munson, and S. Benson. Flexible Complementarity Solvers for Large Scale 731b80f3ac1SShri Abhyankar Applications, Optimization Methods and Software, 21 (2006). 732b80f3ac1SShri Abhyankar 733*c2e3fba1SPatrick Sanan .seealso: `SNESVISetVariableBounds()`, `SNESVISetComputeVariableBounds()`, `SNESCreate()`, `SNES`, `SNESSetType()`, `SNESVINEWTONSSLS`, `SNESNEWTONTR`, `SNESLineSearchSetType()`, `SNESLineSearchSetPostCheck()`, `SNESLineSearchSetPreCheck()` 734c2fc9fa9SBarry Smith 735c2fc9fa9SBarry Smith M*/ 7368cc058d9SJed Brown PETSC_EXTERN PetscErrorCode SNESCreate_VINEWTONRSLS(SNES snes) 737c2fc9fa9SBarry Smith { 738f450aa47SBarry Smith SNES_VINEWTONRSLS *vi; 739d8d34be6SBarry Smith SNESLineSearch linesearch; 740c2fc9fa9SBarry Smith 741c2fc9fa9SBarry Smith PetscFunctionBegin; 742f450aa47SBarry Smith snes->ops->reset = SNESReset_VINEWTONRSLS; 743f450aa47SBarry Smith snes->ops->setup = SNESSetUp_VINEWTONRSLS; 744f450aa47SBarry Smith snes->ops->solve = SNESSolve_VINEWTONRSLS; 745c2fc9fa9SBarry Smith snes->ops->destroy = SNESDestroy_VI; 746c2fc9fa9SBarry Smith snes->ops->setfromoptions = SNESSetFromOptions_VI; 7470298fd71SBarry Smith snes->ops->view = NULL; 7488d359177SBarry Smith snes->ops->converged = SNESConvergedDefault_VI; 749c2fc9fa9SBarry Smith 750c2fc9fa9SBarry Smith snes->usesksp = PETSC_TRUE; 751efd4aadfSBarry Smith snes->usesnpc = PETSC_FALSE; 752c2fc9fa9SBarry Smith 7539566063dSJacob Faibussowitsch PetscCall(SNESGetLineSearch(snes, &linesearch)); 754ec786807SJed Brown if (!((PetscObject)linesearch)->type_name) { 7559566063dSJacob Faibussowitsch PetscCall(SNESLineSearchSetType(linesearch, SNESLINESEARCHBT)); 756ec786807SJed Brown } 7579566063dSJacob Faibussowitsch PetscCall(SNESLineSearchBTSetAlpha(linesearch, 0.0)); 758d8d34be6SBarry Smith 7594fc747eaSLawrence Mitchell snes->alwayscomputesfinalresidual = PETSC_TRUE; 7604fc747eaSLawrence Mitchell 7619566063dSJacob Faibussowitsch PetscCall(PetscNewLog(snes,&vi)); 762c2fc9fa9SBarry Smith snes->data = (void*)vi; 7630298fd71SBarry Smith vi->checkredundancy = NULL; 764c2fc9fa9SBarry Smith 7659566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)snes,"SNESVISetVariableBounds_C",SNESVISetVariableBounds_VI)); 7669566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)snes,"SNESVISetComputeVariableBounds_C",SNESVISetComputeVariableBounds_VI)); 767c2fc9fa9SBarry Smith PetscFunctionReturn(0); 768c2fc9fa9SBarry Smith } 769