xref: /petsc/src/snes/impls/vi/rs/virs.c (revision c1e67a491dcd3fce18efcb05d6f137bc2aa8e6a9)
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 
10c2fc9fa9SBarry Smith    Input parameter
11c2fc9fa9SBarry Smith .  snes - the SNES context
12c2fc9fa9SBarry Smith 
13c2fc9fa9SBarry Smith    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 
32c2fc9fa9SBarry 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*);
401d32ca44SLawrence Mitchell   PetscErrorCode (*getinjection)(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   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);
58ce94432eSBarry Smith   if (!isnes) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_PLIB,"Composed SNES is missing");
59c2fc9fa9SBarry Smith   ierr = PetscContainerGetPointer(isnes,(void**)&dmsnesvi);CHKERRQ(ierr);
60ce94432eSBarry Smith   ierr = VecCreateMPI(PetscObjectComm((PetscObject)dm),dmsnesvi->n,PETSC_DETERMINE,vec);CHKERRQ(ierr);
61c2fc9fa9SBarry Smith   PetscFunctionReturn(0);
62c2fc9fa9SBarry Smith }
63c2fc9fa9SBarry Smith 
644a7a4c06SLawrence Mitchell static PetscErrorCode DMHasCreateInjection_SNESVI(DM dm, PetscBool *flg)
654a7a4c06SLawrence Mitchell {
664a7a4c06SLawrence Mitchell   PetscFunctionBegin;
674a7a4c06SLawrence Mitchell   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
684a7a4c06SLawrence Mitchell   PetscValidPointer(flg,2);
694a7a4c06SLawrence Mitchell   *flg = PETSC_FALSE;
704a7a4c06SLawrence Mitchell   PetscFunctionReturn(0);
714a7a4c06SLawrence Mitchell }
724a7a4c06SLawrence Mitchell 
73c2fc9fa9SBarry Smith /*
74e727c939SJed Brown      DMCreateInterpolation_SNESVI - Modifieds the interpolation obtained from the DM by removing all rows and columns associated with active constraints.
75c2fc9fa9SBarry Smith 
76c2fc9fa9SBarry Smith */
77e727c939SJed Brown PetscErrorCode  DMCreateInterpolation_SNESVI(DM dm1,DM dm2,Mat *mat,Vec *vec)
78c2fc9fa9SBarry Smith {
79c2fc9fa9SBarry Smith   PetscErrorCode ierr;
80c2fc9fa9SBarry Smith   PetscContainer isnes;
81c2fc9fa9SBarry Smith   DM_SNESVI      *dmsnesvi1,*dmsnesvi2;
82c2fc9fa9SBarry Smith   Mat            interp;
83c2fc9fa9SBarry Smith 
84c2fc9fa9SBarry Smith   PetscFunctionBegin;
85c2fc9fa9SBarry Smith   ierr = PetscObjectQuery((PetscObject)dm1,"VI",(PetscObject*)&isnes);CHKERRQ(ierr);
863b4367a7SBarry Smith   if (!isnes) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_PLIB,"Composed VI data structure is missing");
87c2fc9fa9SBarry Smith   ierr = PetscContainerGetPointer(isnes,(void**)&dmsnesvi1);CHKERRQ(ierr);
88c2fc9fa9SBarry Smith   ierr = PetscObjectQuery((PetscObject)dm2,"VI",(PetscObject*)&isnes);CHKERRQ(ierr);
893b4367a7SBarry Smith   if (!isnes) SETERRQ(PetscObjectComm((PetscObject)dm2),PETSC_ERR_PLIB,"Composed VI data structure is missing");
90c2fc9fa9SBarry Smith   ierr = PetscContainerGetPointer(isnes,(void**)&dmsnesvi2);CHKERRQ(ierr);
91c2fc9fa9SBarry Smith 
920298fd71SBarry Smith   ierr = (*dmsnesvi1->createinterpolation)(dm1,dm2,&interp,NULL);CHKERRQ(ierr);
937dae84e0SHong Zhang   ierr = MatCreateSubMatrix(interp,dmsnesvi2->inactive,dmsnesvi1->inactive,MAT_INITIAL_MATRIX,mat);CHKERRQ(ierr);
94c2fc9fa9SBarry Smith   ierr = MatDestroy(&interp);CHKERRQ(ierr);
95c2fc9fa9SBarry Smith   *vec = 0;
96c2fc9fa9SBarry Smith   PetscFunctionReturn(0);
97c2fc9fa9SBarry Smith }
98c2fc9fa9SBarry Smith 
9925acbd8eSLisandro Dalcin static PetscErrorCode DMSetVI(DM,IS);
10025acbd8eSLisandro Dalcin static PetscErrorCode DMDestroyVI(DM);
101c2fc9fa9SBarry Smith 
102c2fc9fa9SBarry Smith /*
103c2fc9fa9SBarry Smith      DMCoarsen_SNESVI - Computes the regular coarsened DM then computes additional information about its inactive set
104c2fc9fa9SBarry Smith 
105c2fc9fa9SBarry Smith */
106c2fc9fa9SBarry Smith PetscErrorCode  DMCoarsen_SNESVI(DM dm1,MPI_Comm comm,DM *dm2)
107c2fc9fa9SBarry Smith {
108c2fc9fa9SBarry Smith   PetscErrorCode ierr;
109c2fc9fa9SBarry Smith   PetscContainer isnes;
110c2fc9fa9SBarry Smith   DM_SNESVI      *dmsnesvi1;
111c2fc9fa9SBarry Smith   Vec            finemarked,coarsemarked;
112c2fc9fa9SBarry Smith   IS             inactive;
1136dbf9973SLawrence Mitchell   Mat            inject;
114c2fc9fa9SBarry Smith   const PetscInt *index;
115c2fc9fa9SBarry Smith   PetscInt       n,k,cnt = 0,rstart,*coarseindex;
116c2fc9fa9SBarry Smith   PetscScalar    *marked;
117c2fc9fa9SBarry Smith 
118c2fc9fa9SBarry Smith   PetscFunctionBegin;
119c2fc9fa9SBarry Smith   ierr = PetscObjectQuery((PetscObject)dm1,"VI",(PetscObject*)&isnes);CHKERRQ(ierr);
1203b4367a7SBarry Smith   if (!isnes) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_PLIB,"Composed VI data structure is missing");
121c2fc9fa9SBarry Smith   ierr = PetscContainerGetPointer(isnes,(void**)&dmsnesvi1);CHKERRQ(ierr);
122c2fc9fa9SBarry Smith 
123c2fc9fa9SBarry Smith   /* get the original coarsen */
124c2fc9fa9SBarry Smith   ierr = (*dmsnesvi1->coarsen)(dm1,comm,dm2);CHKERRQ(ierr);
125c2fc9fa9SBarry Smith 
126c2fc9fa9SBarry Smith   /* not sure why this extra reference is needed, but without the dm2 disappears too early */
12794c98981SBarry Smith   /* Updating the KSPCreateVecs() to avoid using DMGetGlobalVector() when matrix is available removes the need for this reference? */
12894c98981SBarry Smith   /*  ierr = PetscObjectReference((PetscObject)*dm2);CHKERRQ(ierr);*/
129c2fc9fa9SBarry Smith 
130c2fc9fa9SBarry Smith   /* need to set back global vectors in order to use the original injection */
131c2fc9fa9SBarry Smith   ierr = DMClearGlobalVectors(dm1);CHKERRQ(ierr);
1321aa26658SKarl Rupp 
133c2fc9fa9SBarry Smith   dm1->ops->createglobalvector = dmsnesvi1->createglobalvector;
1341aa26658SKarl Rupp 
135c2fc9fa9SBarry Smith   ierr = DMCreateGlobalVector(dm1,&finemarked);CHKERRQ(ierr);
136c2fc9fa9SBarry Smith   ierr = DMCreateGlobalVector(*dm2,&coarsemarked);CHKERRQ(ierr);
137c2fc9fa9SBarry Smith 
138c2fc9fa9SBarry Smith   /*
139c2fc9fa9SBarry Smith      fill finemarked with locations of inactive points
140c2fc9fa9SBarry Smith   */
141c2fc9fa9SBarry Smith   ierr = ISGetIndices(dmsnesvi1->inactive,&index);CHKERRQ(ierr);
142c2fc9fa9SBarry Smith   ierr = ISGetLocalSize(dmsnesvi1->inactive,&n);CHKERRQ(ierr);
143c2fc9fa9SBarry Smith   ierr = VecSet(finemarked,0.0);CHKERRQ(ierr);
144c2fc9fa9SBarry Smith   for (k=0; k<n; k++) {
145c2fc9fa9SBarry Smith     ierr = VecSetValue(finemarked,index[k],1.0,INSERT_VALUES);CHKERRQ(ierr);
146c2fc9fa9SBarry Smith   }
147c2fc9fa9SBarry Smith   ierr = VecAssemblyBegin(finemarked);CHKERRQ(ierr);
148c2fc9fa9SBarry Smith   ierr = VecAssemblyEnd(finemarked);CHKERRQ(ierr);
149c2fc9fa9SBarry Smith 
150e727c939SJed Brown   ierr = DMCreateInjection(*dm2,dm1,&inject);CHKERRQ(ierr);
1512adcf181SLawrence Mitchell   ierr = MatRestrict(inject,finemarked,coarsemarked);CHKERRQ(ierr);
1526dbf9973SLawrence Mitchell   ierr = MatDestroy(&inject);CHKERRQ(ierr);
153c2fc9fa9SBarry Smith 
154c2fc9fa9SBarry Smith   /*
155c2fc9fa9SBarry Smith      create index set list of coarse inactive points from coarsemarked
156c2fc9fa9SBarry Smith   */
157c2fc9fa9SBarry Smith   ierr = VecGetLocalSize(coarsemarked,&n);CHKERRQ(ierr);
1580298fd71SBarry Smith   ierr = VecGetOwnershipRange(coarsemarked,&rstart,NULL);CHKERRQ(ierr);
159c2fc9fa9SBarry Smith   ierr = VecGetArray(coarsemarked,&marked);CHKERRQ(ierr);
160c2fc9fa9SBarry Smith   for (k=0; k<n; k++) {
161c2fc9fa9SBarry Smith     if (marked[k] != 0.0) cnt++;
162c2fc9fa9SBarry Smith   }
163785e854fSJed Brown   ierr = PetscMalloc1(cnt,&coarseindex);CHKERRQ(ierr);
164c2fc9fa9SBarry Smith   cnt  = 0;
165c2fc9fa9SBarry Smith   for (k=0; k<n; k++) {
166c2fc9fa9SBarry Smith     if (marked[k] != 0.0) coarseindex[cnt++] = k + rstart;
167c2fc9fa9SBarry Smith   }
168c2fc9fa9SBarry Smith   ierr = VecRestoreArray(coarsemarked,&marked);CHKERRQ(ierr);
169ce94432eSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)coarsemarked),cnt,coarseindex,PETSC_OWN_POINTER,&inactive);CHKERRQ(ierr);
170c2fc9fa9SBarry Smith 
171c2fc9fa9SBarry Smith   ierr = DMClearGlobalVectors(dm1);CHKERRQ(ierr);
1721aa26658SKarl Rupp 
173c2fc9fa9SBarry Smith   dm1->ops->createglobalvector = DMCreateGlobalVector_SNESVI;
1741aa26658SKarl Rupp 
175c2fc9fa9SBarry Smith   ierr = DMSetVI(*dm2,inactive);CHKERRQ(ierr);
176c2fc9fa9SBarry Smith 
177c2fc9fa9SBarry Smith   ierr = VecDestroy(&finemarked);CHKERRQ(ierr);
178c2fc9fa9SBarry Smith   ierr = VecDestroy(&coarsemarked);CHKERRQ(ierr);
179c2fc9fa9SBarry Smith   ierr = ISDestroy(&inactive);CHKERRQ(ierr);
180c2fc9fa9SBarry Smith   PetscFunctionReturn(0);
181c2fc9fa9SBarry Smith }
182c2fc9fa9SBarry Smith 
183c2fc9fa9SBarry Smith PetscErrorCode DMDestroy_SNESVI(DM_SNESVI *dmsnesvi)
184c2fc9fa9SBarry Smith {
185c2fc9fa9SBarry Smith   PetscErrorCode ierr;
186c2fc9fa9SBarry Smith 
187c2fc9fa9SBarry Smith   PetscFunctionBegin;
188c2fc9fa9SBarry Smith   /* reset the base methods in the DM object that were changed when the DM_SNESVI was reset */
18925296bd5SBarry Smith   dmsnesvi->dm->ops->createinterpolation = dmsnesvi->createinterpolation;
190c2fc9fa9SBarry Smith   dmsnesvi->dm->ops->coarsen             = dmsnesvi->coarsen;
191c2fc9fa9SBarry Smith   dmsnesvi->dm->ops->createglobalvector  = dmsnesvi->createglobalvector;
1921d32ca44SLawrence Mitchell   dmsnesvi->dm->ops->getinjection        = dmsnesvi->getinjection;
1934a7a4c06SLawrence Mitchell   dmsnesvi->dm->ops->hascreateinjection  = dmsnesvi->hascreateinjection;
194c2fc9fa9SBarry Smith   /* need to clear out this vectors because some of them may not have a reference to the DM
195c2fc9fa9SBarry Smith     but they are counted as having references to the DM in DMDestroy() */
196c2fc9fa9SBarry Smith   ierr = DMClearGlobalVectors(dmsnesvi->dm);CHKERRQ(ierr);
197c2fc9fa9SBarry Smith 
198c2fc9fa9SBarry Smith   ierr = ISDestroy(&dmsnesvi->inactive);CHKERRQ(ierr);
199c2fc9fa9SBarry Smith   ierr = PetscFree(dmsnesvi);CHKERRQ(ierr);
200c2fc9fa9SBarry Smith   PetscFunctionReturn(0);
201c2fc9fa9SBarry Smith }
202c2fc9fa9SBarry Smith 
203c2fc9fa9SBarry Smith /*
204c2fc9fa9SBarry Smith      DMSetVI - Marks a DM as associated with a VI problem. This causes the interpolation/restriction operators to
205c2fc9fa9SBarry Smith                be restricted to only those variables NOT associated with active constraints.
206c2fc9fa9SBarry Smith 
207c2fc9fa9SBarry Smith */
20825acbd8eSLisandro Dalcin static PetscErrorCode DMSetVI(DM dm,IS inactive)
209c2fc9fa9SBarry Smith {
210c2fc9fa9SBarry Smith   PetscErrorCode ierr;
211c2fc9fa9SBarry Smith   PetscContainer isnes;
212c2fc9fa9SBarry Smith   DM_SNESVI      *dmsnesvi;
213c2fc9fa9SBarry Smith 
214c2fc9fa9SBarry Smith   PetscFunctionBegin;
215c2fc9fa9SBarry Smith   if (!dm) PetscFunctionReturn(0);
216c2fc9fa9SBarry Smith 
217c2fc9fa9SBarry Smith   ierr = PetscObjectReference((PetscObject)inactive);CHKERRQ(ierr);
218c2fc9fa9SBarry Smith 
219c2fc9fa9SBarry Smith   ierr = PetscObjectQuery((PetscObject)dm,"VI",(PetscObject*)&isnes);CHKERRQ(ierr);
220c2fc9fa9SBarry Smith   if (!isnes) {
221ce94432eSBarry Smith     ierr = PetscContainerCreate(PetscObjectComm((PetscObject)dm),&isnes);CHKERRQ(ierr);
222c2fc9fa9SBarry Smith     ierr = PetscContainerSetUserDestroy(isnes,(PetscErrorCode (*)(void*))DMDestroy_SNESVI);CHKERRQ(ierr);
223b00a9115SJed Brown     ierr = PetscNew(&dmsnesvi);CHKERRQ(ierr);
224c2fc9fa9SBarry Smith     ierr = PetscContainerSetPointer(isnes,(void*)dmsnesvi);CHKERRQ(ierr);
225c2fc9fa9SBarry Smith     ierr = PetscObjectCompose((PetscObject)dm,"VI",(PetscObject)isnes);CHKERRQ(ierr);
226c2fc9fa9SBarry Smith     ierr = PetscContainerDestroy(&isnes);CHKERRQ(ierr);
2271aa26658SKarl Rupp 
22825296bd5SBarry Smith     dmsnesvi->createinterpolation = dm->ops->createinterpolation;
22925296bd5SBarry Smith     dm->ops->createinterpolation  = DMCreateInterpolation_SNESVI;
230c2fc9fa9SBarry Smith     dmsnesvi->coarsen             = dm->ops->coarsen;
231c2fc9fa9SBarry Smith     dm->ops->coarsen              = DMCoarsen_SNESVI;
232c2fc9fa9SBarry Smith     dmsnesvi->createglobalvector  = dm->ops->createglobalvector;
233c2fc9fa9SBarry Smith     dm->ops->createglobalvector   = DMCreateGlobalVector_SNESVI;
2341d32ca44SLawrence Mitchell     dmsnesvi->getinjection        = dm->ops->getinjection;
2351d32ca44SLawrence Mitchell     dm->ops->getinjection         = NULL;
2364a7a4c06SLawrence Mitchell     dmsnesvi->hascreateinjection  = dm->ops->hascreateinjection;
2374a7a4c06SLawrence Mitchell     dm->ops->hascreateinjection   = DMHasCreateInjection_SNESVI;
238c2fc9fa9SBarry Smith   } else {
239c2fc9fa9SBarry Smith     ierr = PetscContainerGetPointer(isnes,(void**)&dmsnesvi);CHKERRQ(ierr);
240c2fc9fa9SBarry Smith     ierr = ISDestroy(&dmsnesvi->inactive);CHKERRQ(ierr);
241c2fc9fa9SBarry Smith   }
242c2fc9fa9SBarry Smith   ierr = DMClearGlobalVectors(dm);CHKERRQ(ierr);
243c2fc9fa9SBarry Smith   ierr = ISGetLocalSize(inactive,&dmsnesvi->n);CHKERRQ(ierr);
2441aa26658SKarl Rupp 
245c2fc9fa9SBarry Smith   dmsnesvi->inactive = inactive;
246c2fc9fa9SBarry Smith   dmsnesvi->dm       = dm;
247c2fc9fa9SBarry Smith   PetscFunctionReturn(0);
248c2fc9fa9SBarry Smith }
249c2fc9fa9SBarry Smith 
250c2fc9fa9SBarry Smith /*
251c2fc9fa9SBarry Smith      DMDestroyVI - Frees the DM_SNESVI object contained in the DM
25225296bd5SBarry Smith          - also resets the function pointers in the DM for createinterpolation() etc to use the original DM
253c2fc9fa9SBarry Smith */
25425acbd8eSLisandro Dalcin static PetscErrorCode DMDestroyVI(DM dm)
255c2fc9fa9SBarry Smith {
256c2fc9fa9SBarry Smith   PetscErrorCode ierr;
257c2fc9fa9SBarry Smith 
258c2fc9fa9SBarry Smith   PetscFunctionBegin;
259c2fc9fa9SBarry Smith   if (!dm) PetscFunctionReturn(0);
2600298fd71SBarry Smith   ierr = PetscObjectCompose((PetscObject)dm,"VI",(PetscObject)NULL);CHKERRQ(ierr);
261c2fc9fa9SBarry Smith   PetscFunctionReturn(0);
262c2fc9fa9SBarry Smith }
263c2fc9fa9SBarry Smith 
264c2fc9fa9SBarry Smith /* --------------------------------------------------------------------------------------------------------*/
265c2fc9fa9SBarry Smith 
266c2fc9fa9SBarry Smith 
267f450aa47SBarry Smith PetscErrorCode SNESCreateIndexSets_VINEWTONRSLS(SNES snes,Vec X,Vec F,IS *ISact,IS *ISinact)
268c2fc9fa9SBarry Smith {
269c2fc9fa9SBarry Smith   PetscErrorCode ierr;
270c2fc9fa9SBarry Smith 
271c2fc9fa9SBarry Smith   PetscFunctionBegin;
272c2fc9fa9SBarry Smith   ierr = SNESVIGetActiveSetIS(snes,X,F,ISact);CHKERRQ(ierr);
273c2fc9fa9SBarry Smith   ierr = ISComplement(*ISact,X->map->rstart,X->map->rend,ISinact);CHKERRQ(ierr);
274c2fc9fa9SBarry Smith   PetscFunctionReturn(0);
275c2fc9fa9SBarry Smith }
276c2fc9fa9SBarry Smith 
277c2fc9fa9SBarry Smith /* Create active and inactive set vectors. The local size of this vector is set and petsc computes the global size */
278f450aa47SBarry Smith PetscErrorCode SNESCreateSubVectors_VINEWTONRSLS(SNES snes,PetscInt n,Vec *newv)
279c2fc9fa9SBarry Smith {
280c2fc9fa9SBarry Smith   PetscErrorCode ierr;
281c2fc9fa9SBarry Smith   Vec            v;
282c2fc9fa9SBarry Smith 
283c2fc9fa9SBarry Smith   PetscFunctionBegin;
284ce94432eSBarry Smith   ierr  = VecCreate(PetscObjectComm((PetscObject)snes),&v);CHKERRQ(ierr);
285c2fc9fa9SBarry Smith   ierr  = VecSetSizes(v,n,PETSC_DECIDE);CHKERRQ(ierr);
286c0dedaeaSBarry Smith   ierr  = VecSetType(v,VECSTANDARD);CHKERRQ(ierr);
287c2fc9fa9SBarry Smith   *newv = v;
288c2fc9fa9SBarry Smith   PetscFunctionReturn(0);
289c2fc9fa9SBarry Smith }
290c2fc9fa9SBarry Smith 
291c2fc9fa9SBarry Smith /* Resets the snes PC and KSP when the active set sizes change */
292c2fc9fa9SBarry Smith PetscErrorCode SNESVIResetPCandKSP(SNES snes,Mat Amat,Mat Pmat)
293c2fc9fa9SBarry Smith {
294c2fc9fa9SBarry Smith   PetscErrorCode ierr;
295c2fc9fa9SBarry Smith   KSP            snesksp;
296c2fc9fa9SBarry Smith 
297c2fc9fa9SBarry Smith   PetscFunctionBegin;
298c2fc9fa9SBarry Smith   ierr = SNESGetKSP(snes,&snesksp);CHKERRQ(ierr);
299c2fc9fa9SBarry Smith   ierr = KSPReset(snesksp);CHKERRQ(ierr);
30084343125SMatthew G. Knepley   ierr = KSPResetFromOptions(snesksp);CHKERRQ(ierr);
301c2fc9fa9SBarry Smith 
302c2fc9fa9SBarry Smith   /*
303c2fc9fa9SBarry Smith   KSP                    kspnew;
304c2fc9fa9SBarry Smith   PC                     pcnew;
305ea799195SBarry Smith   MatSolverType          stype;
306c2fc9fa9SBarry Smith 
307c2fc9fa9SBarry Smith 
308ce94432eSBarry Smith   ierr = KSPCreate(PetscObjectComm((PetscObject)snes),&kspnew);CHKERRQ(ierr);
309c2fc9fa9SBarry Smith   kspnew->pc_side = snesksp->pc_side;
310c2fc9fa9SBarry Smith   kspnew->rtol    = snesksp->rtol;
311c2fc9fa9SBarry Smith   kspnew->abstol    = snesksp->abstol;
312c2fc9fa9SBarry Smith   kspnew->max_it  = snesksp->max_it;
313c2fc9fa9SBarry Smith   ierr = KSPSetType(kspnew,((PetscObject)snesksp)->type_name);CHKERRQ(ierr);
314c2fc9fa9SBarry Smith   ierr = KSPGetPC(kspnew,&pcnew);CHKERRQ(ierr);
315c2fc9fa9SBarry Smith   ierr = PCSetType(kspnew->pc,((PetscObject)snesksp->pc)->type_name);CHKERRQ(ierr);
31623ee1639SBarry Smith   ierr = PCSetOperators(kspnew->pc,Amat,Pmat);CHKERRQ(ierr);
3173ca39a21SBarry Smith   ierr = PCFactorGetMatSolverType(snesksp->pc,&stype);CHKERRQ(ierr);
3183ca39a21SBarry Smith   ierr = PCFactorSetMatSolverType(kspnew->pc,stype);CHKERRQ(ierr);
319c2fc9fa9SBarry Smith   ierr = KSPDestroy(&snesksp);CHKERRQ(ierr);
320c2fc9fa9SBarry Smith   snes->ksp = kspnew;
3213bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)snes,(PetscObject)kspnew);CHKERRQ(ierr);
322c2fc9fa9SBarry Smith    ierr = KSPSetFromOptions(kspnew);CHKERRQ(ierr);*/
323c2fc9fa9SBarry Smith   PetscFunctionReturn(0);
324c2fc9fa9SBarry Smith }
325c2fc9fa9SBarry Smith 
326c2fc9fa9SBarry Smith /* Variational Inequality solver using reduce space method. No semismooth algorithm is
327c2fc9fa9SBarry Smith    implemented in this algorithm. It basically identifies the active constraints and does
328c2fc9fa9SBarry Smith    a linear solve on the other variables (those not associated with the active constraints). */
329f450aa47SBarry Smith PetscErrorCode SNESSolve_VINEWTONRSLS(SNES snes)
330c2fc9fa9SBarry Smith {
331f450aa47SBarry Smith   SNES_VINEWTONRSLS    *vi = (SNES_VINEWTONRSLS*)snes->data;
332c2fc9fa9SBarry Smith   PetscErrorCode       ierr;
333c2fc9fa9SBarry Smith   PetscInt             maxits,i,lits;
334422a814eSBarry Smith   SNESLineSearchReason lssucceed;
335c2fc9fa9SBarry Smith   PetscReal            fnorm,gnorm,xnorm=0,ynorm;
3369bd66eb0SPeter Brune   Vec                  Y,X,F;
337c2fc9fa9SBarry Smith   KSPConvergedReason   kspreason;
33892e89061SBarry Smith   KSP                  ksp;
33992e89061SBarry Smith   PC                   pc;
340c2fc9fa9SBarry Smith 
341c2fc9fa9SBarry Smith   PetscFunctionBegin;
34292e89061SBarry Smith   /* Multigrid must use Galerkin for coarse grids with active set/reduced space methods; cannot rediscretize on coarser grids*/
34392e89061SBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
34492e89061SBarry Smith   ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
3452134b1e4SBarry Smith   ierr = PCMGSetGalerkin(pc,PC_MG_GALERKIN_BOTH);CHKERRQ(ierr);
34692e89061SBarry Smith 
347c2fc9fa9SBarry Smith   snes->numFailures            = 0;
348c2fc9fa9SBarry Smith   snes->numLinearSolveFailures = 0;
349c2fc9fa9SBarry Smith   snes->reason                 = SNES_CONVERGED_ITERATING;
350c2fc9fa9SBarry Smith 
351c2fc9fa9SBarry Smith   maxits = snes->max_its;               /* maximum number of iterations */
352c2fc9fa9SBarry Smith   X      = snes->vec_sol;               /* solution vector */
353c2fc9fa9SBarry Smith   F      = snes->vec_func;              /* residual vector */
354c2fc9fa9SBarry Smith   Y      = snes->work[0];               /* work vectors */
3559bd66eb0SPeter Brune 
356f1c6b773SPeter Brune   ierr = SNESLineSearchSetVIFunctions(snes->linesearch, SNESVIProjectOntoBounds, SNESVIComputeInactiveSetFnorm);CHKERRQ(ierr);
3570298fd71SBarry Smith   ierr = SNESLineSearchSetVecs(snes->linesearch, X, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
358f1c6b773SPeter Brune   ierr = SNESLineSearchSetUp(snes->linesearch);CHKERRQ(ierr);
359c2fc9fa9SBarry Smith 
360e04113cfSBarry Smith   ierr       = PetscObjectSAWsTakeAccess((PetscObject)snes);CHKERRQ(ierr);
361c2fc9fa9SBarry Smith   snes->iter = 0;
362c2fc9fa9SBarry Smith   snes->norm = 0.0;
363e04113cfSBarry Smith   ierr       = PetscObjectSAWsGrantAccess((PetscObject)snes);CHKERRQ(ierr);
364c2fc9fa9SBarry Smith 
365c2fc9fa9SBarry Smith   ierr = SNESVIProjectOntoBounds(snes,X);CHKERRQ(ierr);
366c2fc9fa9SBarry Smith   ierr = SNESComputeFunction(snes,X,F);CHKERRQ(ierr);
367c2fc9fa9SBarry Smith   ierr = SNESVIComputeInactiveSetFnorm(snes,F,X,&fnorm);CHKERRQ(ierr);
3682fbecc10SBarry Smith   ierr = VecNorm(X,NORM_2,&xnorm);CHKERRQ(ierr);        /* xnorm <- ||x||  */
369422a814eSBarry Smith   SNESCheckFunctionNorm(snes,fnorm);
370e04113cfSBarry Smith   ierr       = PetscObjectSAWsTakeAccess((PetscObject)snes);CHKERRQ(ierr);
371c2fc9fa9SBarry Smith   snes->norm = fnorm;
372e04113cfSBarry Smith   ierr       = PetscObjectSAWsGrantAccess((PetscObject)snes);CHKERRQ(ierr);
373a71f0d7dSBarry Smith   ierr       = SNESLogConvergenceHistory(snes,fnorm,0);CHKERRQ(ierr);
374c2fc9fa9SBarry Smith   ierr       = SNESMonitor(snes,0,fnorm);CHKERRQ(ierr);
375c2fc9fa9SBarry Smith 
376c2fc9fa9SBarry Smith   /* test convergence */
377c2fc9fa9SBarry Smith   ierr = (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);CHKERRQ(ierr);
378c2fc9fa9SBarry Smith   if (snes->reason) PetscFunctionReturn(0);
379c2fc9fa9SBarry Smith 
380c2fc9fa9SBarry Smith 
381c2fc9fa9SBarry Smith   for (i=0; i<maxits; i++) {
382c2fc9fa9SBarry Smith 
383f009fc93SPatrick Farrell     IS         IS_act; /* _act -> active set _inact -> inactive set */
384c2fc9fa9SBarry Smith     IS         IS_redact; /* redundant active set */
385c2fc9fa9SBarry Smith     VecScatter scat_act,scat_inact;
386c2fc9fa9SBarry Smith     PetscInt   nis_act,nis_inact;
387c2fc9fa9SBarry Smith     Vec        Y_act,Y_inact,F_inact;
388c2fc9fa9SBarry Smith     Mat        jac_inact_inact,prejac_inact_inact;
389c2fc9fa9SBarry Smith     PetscBool  isequal;
390c2fc9fa9SBarry Smith 
391c2fc9fa9SBarry Smith     /* Call general purpose update function */
392c2fc9fa9SBarry Smith     if (snes->ops->update) {
393c2fc9fa9SBarry Smith       ierr = (*snes->ops->update)(snes, snes->iter);CHKERRQ(ierr);
394c2fc9fa9SBarry Smith     }
395d1e9a80fSBarry Smith     ierr = SNESComputeJacobian(snes,X,snes->jacobian,snes->jacobian_pre);CHKERRQ(ierr);
396c2fc9fa9SBarry Smith 
397c2fc9fa9SBarry Smith 
398c2fc9fa9SBarry Smith     /* Create active and inactive index sets */
399c2fc9fa9SBarry Smith 
400c2fc9fa9SBarry Smith     /*original
401f009fc93SPatrick Farrell     ierr = SNESVICreateIndexSets_RS(snes,X,F,&IS_act,&vi->IS_inact);CHKERRQ(ierr);
402c2fc9fa9SBarry Smith      */
403c2fc9fa9SBarry Smith     ierr = SNESVIGetActiveSetIS(snes,X,F,&IS_act);CHKERRQ(ierr);
404c2fc9fa9SBarry Smith 
405c2fc9fa9SBarry Smith     if (vi->checkredundancy) {
406c2fc9fa9SBarry Smith       (*vi->checkredundancy)(snes,IS_act,&IS_redact,vi->ctxP);CHKERRQ(ierr);
407c2fc9fa9SBarry Smith       if (IS_redact) {
408c2fc9fa9SBarry Smith         ierr = ISSort(IS_redact);CHKERRQ(ierr);
409f009fc93SPatrick Farrell         ierr = ISComplement(IS_redact,X->map->rstart,X->map->rend,&vi->IS_inact);CHKERRQ(ierr);
410c2fc9fa9SBarry Smith         ierr = ISDestroy(&IS_redact);CHKERRQ(ierr);
4111aa26658SKarl Rupp       } else {
412f009fc93SPatrick Farrell         ierr = ISComplement(IS_act,X->map->rstart,X->map->rend,&vi->IS_inact);CHKERRQ(ierr);
413c2fc9fa9SBarry Smith       }
414c2fc9fa9SBarry Smith     } else {
415f009fc93SPatrick Farrell       ierr = ISComplement(IS_act,X->map->rstart,X->map->rend,&vi->IS_inact);CHKERRQ(ierr);
416c2fc9fa9SBarry Smith     }
417c2fc9fa9SBarry Smith 
418c2fc9fa9SBarry Smith 
419c2fc9fa9SBarry Smith     /* Create inactive set submatrix */
4207dae84e0SHong Zhang     ierr = MatCreateSubMatrix(snes->jacobian,vi->IS_inact,vi->IS_inact,MAT_INITIAL_MATRIX,&jac_inact_inact);CHKERRQ(ierr);
421c2fc9fa9SBarry Smith 
42266bfb381SJed Brown     if (0) {                    /* Dead code (temporary developer hack) */
42366bfb381SJed Brown       IS keptrows;
424c2fc9fa9SBarry Smith       ierr = MatFindNonzeroRows(jac_inact_inact,&keptrows);CHKERRQ(ierr);
42566bfb381SJed Brown       if (keptrows) {
426c2fc9fa9SBarry Smith         PetscInt       cnt,*nrows,k;
427c2fc9fa9SBarry Smith         const PetscInt *krows,*inact;
428367daffbSBarry Smith         PetscInt       rstart;
429c2fc9fa9SBarry Smith 
430367daffbSBarry Smith         ierr = MatGetOwnershipRange(jac_inact_inact,&rstart,NULL);CHKERRQ(ierr);
431c2fc9fa9SBarry Smith         ierr = MatDestroy(&jac_inact_inact);CHKERRQ(ierr);
432c2fc9fa9SBarry Smith         ierr = ISDestroy(&IS_act);CHKERRQ(ierr);
433c2fc9fa9SBarry Smith 
434c2fc9fa9SBarry Smith         ierr = ISGetLocalSize(keptrows,&cnt);CHKERRQ(ierr);
435c2fc9fa9SBarry Smith         ierr = ISGetIndices(keptrows,&krows);CHKERRQ(ierr);
436f009fc93SPatrick Farrell         ierr = ISGetIndices(vi->IS_inact,&inact);CHKERRQ(ierr);
437785e854fSJed Brown         ierr = PetscMalloc1(cnt,&nrows);CHKERRQ(ierr);
4381aa26658SKarl Rupp         for (k=0; k<cnt; k++) nrows[k] = inact[krows[k]-rstart];
439c2fc9fa9SBarry Smith         ierr = ISRestoreIndices(keptrows,&krows);CHKERRQ(ierr);
440f009fc93SPatrick Farrell         ierr = ISRestoreIndices(vi->IS_inact,&inact);CHKERRQ(ierr);
441c2fc9fa9SBarry Smith         ierr = ISDestroy(&keptrows);CHKERRQ(ierr);
442f009fc93SPatrick Farrell         ierr = ISDestroy(&vi->IS_inact);CHKERRQ(ierr);
443c2fc9fa9SBarry Smith 
444f009fc93SPatrick Farrell         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)snes),cnt,nrows,PETSC_OWN_POINTER,&vi->IS_inact);CHKERRQ(ierr);
445f009fc93SPatrick Farrell         ierr = ISComplement(vi->IS_inact,F->map->rstart,F->map->rend,&IS_act);CHKERRQ(ierr);
4467dae84e0SHong Zhang         ierr = MatCreateSubMatrix(snes->jacobian,vi->IS_inact,vi->IS_inact,MAT_INITIAL_MATRIX,&jac_inact_inact);CHKERRQ(ierr);
447c2fc9fa9SBarry Smith       }
44866bfb381SJed Brown     }
449f009fc93SPatrick Farrell     ierr = DMSetVI(snes->dm,vi->IS_inact);CHKERRQ(ierr);
450c2fc9fa9SBarry Smith     /* remove later */
451c2fc9fa9SBarry Smith 
452c2fc9fa9SBarry Smith     /*
4535f042095SDmitry Karpeev     ierr = VecView(vi->xu,PETSC_VIEWER_BINARY_(((PetscObject)(vi->xu))->comm));CHKERRQ(ierr);
4545f042095SDmitry Karpeev     ierr = VecView(vi->xl,PETSC_VIEWER_BINARY_(((PetscObject)(vi->xl))->comm));CHKERRQ(ierr);
455ce94432eSBarry Smith     ierr = VecView(X,PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)X)));CHKERRQ(ierr);
456ce94432eSBarry Smith     ierr = VecView(F,PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)F)));CHKERRQ(ierr);
457f009fc93SPatrick Farrell     ierr = ISView(vi->IS_inact,PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)vi->IS_inact)));CHKERRQ(ierr);
458c2fc9fa9SBarry Smith      */
459c2fc9fa9SBarry Smith 
460c2fc9fa9SBarry Smith     /* Get sizes of active and inactive sets */
461c2fc9fa9SBarry Smith     ierr = ISGetLocalSize(IS_act,&nis_act);CHKERRQ(ierr);
462f009fc93SPatrick Farrell     ierr = ISGetLocalSize(vi->IS_inact,&nis_inact);CHKERRQ(ierr);
463c2fc9fa9SBarry Smith 
464c2fc9fa9SBarry Smith     /* Create active and inactive set vectors */
465f450aa47SBarry Smith     ierr = SNESCreateSubVectors_VINEWTONRSLS(snes,nis_inact,&F_inact);CHKERRQ(ierr);
466f450aa47SBarry Smith     ierr = SNESCreateSubVectors_VINEWTONRSLS(snes,nis_act,&Y_act);CHKERRQ(ierr);
467f450aa47SBarry Smith     ierr = SNESCreateSubVectors_VINEWTONRSLS(snes,nis_inact,&Y_inact);CHKERRQ(ierr);
468c2fc9fa9SBarry Smith 
469c2fc9fa9SBarry Smith     /* Create scatter contexts */
47035928de7SBarry Smith     ierr = VecScatterCreateWithData(Y,IS_act,Y_act,NULL,&scat_act);CHKERRQ(ierr);
47135928de7SBarry Smith     ierr = VecScatterCreateWithData(Y,vi->IS_inact,Y_inact,NULL,&scat_inact);CHKERRQ(ierr);
472c2fc9fa9SBarry Smith 
473c2fc9fa9SBarry Smith     /* Do a vec scatter to active and inactive set vectors */
474c2fc9fa9SBarry Smith     ierr = VecScatterBegin(scat_inact,F,F_inact,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
475c2fc9fa9SBarry Smith     ierr = VecScatterEnd(scat_inact,F,F_inact,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
476c2fc9fa9SBarry Smith 
477c2fc9fa9SBarry Smith     ierr = VecScatterBegin(scat_act,Y,Y_act,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
478c2fc9fa9SBarry Smith     ierr = VecScatterEnd(scat_act,Y,Y_act,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
479c2fc9fa9SBarry Smith 
480c2fc9fa9SBarry Smith     ierr = VecScatterBegin(scat_inact,Y,Y_inact,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
481c2fc9fa9SBarry Smith     ierr = VecScatterEnd(scat_inact,Y,Y_inact,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
482c2fc9fa9SBarry Smith 
483c2fc9fa9SBarry Smith     /* Active set direction = 0 */
484c2fc9fa9SBarry Smith     ierr = VecSet(Y_act,0);CHKERRQ(ierr);
485c2fc9fa9SBarry Smith     if (snes->jacobian != snes->jacobian_pre) {
4867dae84e0SHong Zhang       ierr = MatCreateSubMatrix(snes->jacobian_pre,vi->IS_inact,vi->IS_inact,MAT_INITIAL_MATRIX,&prejac_inact_inact);CHKERRQ(ierr);
487c2fc9fa9SBarry Smith     } else prejac_inact_inact = jac_inact_inact;
488c2fc9fa9SBarry Smith 
489f009fc93SPatrick Farrell     ierr = ISEqual(vi->IS_inact_prev,vi->IS_inact,&isequal);CHKERRQ(ierr);
490c2fc9fa9SBarry Smith     if (!isequal) {
491c2fc9fa9SBarry Smith       ierr = SNESVIResetPCandKSP(snes,jac_inact_inact,prejac_inact_inact);CHKERRQ(ierr);
4926dbb499eSCian Wilson       ierr = PCFieldSplitRestrictIS(pc,vi->IS_inact);CHKERRQ(ierr);
493c2fc9fa9SBarry Smith     }
494c2fc9fa9SBarry Smith 
495f009fc93SPatrick Farrell     /*      ierr = ISView(vi->IS_inact,0);CHKERRQ(ierr); */
496c2fc9fa9SBarry Smith     /*      ierr = ISView(IS_act,0);CHKERRQ(ierr);*/
497c2fc9fa9SBarry Smith     /*      ierr = MatView(snes->jacobian_pre,0); */
498c2fc9fa9SBarry Smith 
499c2fc9fa9SBarry Smith 
500c2fc9fa9SBarry Smith 
50123ee1639SBarry Smith     ierr = KSPSetOperators(snes->ksp,jac_inact_inact,prejac_inact_inact);CHKERRQ(ierr);
502c2fc9fa9SBarry Smith     ierr = KSPSetUp(snes->ksp);CHKERRQ(ierr);
503c2fc9fa9SBarry Smith     {
504c2fc9fa9SBarry Smith       PC        pc;
505c2fc9fa9SBarry Smith       PetscBool flg;
506c2fc9fa9SBarry Smith       ierr = KSPGetPC(snes->ksp,&pc);CHKERRQ(ierr);
507251f4c67SDmitry Karpeev       ierr = PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&flg);CHKERRQ(ierr);
508c2fc9fa9SBarry Smith       if (flg) {
509c2fc9fa9SBarry Smith         KSP *subksps;
5100298fd71SBarry Smith         ierr = PCFieldSplitGetSubKSP(pc,NULL,&subksps);CHKERRQ(ierr);
511c2fc9fa9SBarry Smith         ierr = KSPGetPC(subksps[0],&pc);CHKERRQ(ierr);
512c2fc9fa9SBarry Smith         ierr = PetscFree(subksps);CHKERRQ(ierr);
513251f4c67SDmitry Karpeev         ierr = PetscObjectTypeCompare((PetscObject)pc,PCBJACOBI,&flg);CHKERRQ(ierr);
514c2fc9fa9SBarry Smith         if (flg) {
515c2fc9fa9SBarry Smith           PetscInt       n,N = 101*101,j,cnts[3] = {0,0,0};
516c2fc9fa9SBarry Smith           const PetscInt *ii;
517c2fc9fa9SBarry Smith 
518f009fc93SPatrick Farrell           ierr = ISGetSize(vi->IS_inact,&n);CHKERRQ(ierr);
519f009fc93SPatrick Farrell           ierr = ISGetIndices(vi->IS_inact,&ii);CHKERRQ(ierr);
520c2fc9fa9SBarry Smith           for (j=0; j<n; j++) {
521c2fc9fa9SBarry Smith             if (ii[j] < N) cnts[0]++;
522c2fc9fa9SBarry Smith             else if (ii[j] < 2*N) cnts[1]++;
523c2fc9fa9SBarry Smith             else if (ii[j] < 3*N) cnts[2]++;
524c2fc9fa9SBarry Smith           }
525f009fc93SPatrick Farrell           ierr = ISRestoreIndices(vi->IS_inact,&ii);CHKERRQ(ierr);
526c2fc9fa9SBarry Smith 
527c2fc9fa9SBarry Smith           ierr = PCBJacobiSetTotalBlocks(pc,3,cnts);CHKERRQ(ierr);
528c2fc9fa9SBarry Smith         }
529c2fc9fa9SBarry Smith       }
530c2fc9fa9SBarry Smith     }
531c2fc9fa9SBarry Smith 
532d4211eb9SBarry Smith     ierr = KSPSolve(snes->ksp,F_inact,Y_inact);CHKERRQ(ierr);
533c2fc9fa9SBarry Smith     ierr = VecScatterBegin(scat_act,Y_act,Y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
534c2fc9fa9SBarry Smith     ierr = VecScatterEnd(scat_act,Y_act,Y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
535c2fc9fa9SBarry Smith     ierr = VecScatterBegin(scat_inact,Y_inact,Y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
536c2fc9fa9SBarry Smith     ierr = VecScatterEnd(scat_inact,Y_inact,Y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
537c2fc9fa9SBarry Smith 
538c2fc9fa9SBarry Smith     ierr = VecDestroy(&F_inact);CHKERRQ(ierr);
539c2fc9fa9SBarry Smith     ierr = VecDestroy(&Y_act);CHKERRQ(ierr);
540c2fc9fa9SBarry Smith     ierr = VecDestroy(&Y_inact);CHKERRQ(ierr);
541c2fc9fa9SBarry Smith     ierr = VecScatterDestroy(&scat_act);CHKERRQ(ierr);
542c2fc9fa9SBarry Smith     ierr = VecScatterDestroy(&scat_inact);CHKERRQ(ierr);
543c2fc9fa9SBarry Smith     ierr = ISDestroy(&IS_act);CHKERRQ(ierr);
544c2fc9fa9SBarry Smith     if (!isequal) {
545c2fc9fa9SBarry Smith       ierr = ISDestroy(&vi->IS_inact_prev);CHKERRQ(ierr);
546f009fc93SPatrick Farrell       ierr = ISDuplicate(vi->IS_inact,&vi->IS_inact_prev);CHKERRQ(ierr);
547c2fc9fa9SBarry Smith     }
548f009fc93SPatrick Farrell     ierr = ISDestroy(&vi->IS_inact);CHKERRQ(ierr);
549c2fc9fa9SBarry Smith     ierr = MatDestroy(&jac_inact_inact);CHKERRQ(ierr);
550c2fc9fa9SBarry Smith     if (snes->jacobian != snes->jacobian_pre) {
551c2fc9fa9SBarry Smith       ierr = MatDestroy(&prejac_inact_inact);CHKERRQ(ierr);
552c2fc9fa9SBarry Smith     }
553fa6eefd1SCian Wilson 
554fa6eefd1SCian Wilson     ierr = KSPGetConvergedReason(snes->ksp,&kspreason);CHKERRQ(ierr);
555fa6eefd1SCian Wilson     if (kspreason < 0) {
556fa6eefd1SCian Wilson       if (++snes->numLinearSolveFailures >= snes->maxLinearSolveFailures) {
557fa6eefd1SCian Wilson         ierr         = PetscInfo2(snes,"iter=%D, number linear solve failures %D greater than current SNES allowed, stopping solve\n",snes->iter,snes->numLinearSolveFailures);CHKERRQ(ierr);
558fa6eefd1SCian Wilson         snes->reason = SNES_DIVERGED_LINEAR_SOLVE;
559fa6eefd1SCian Wilson         break;
560fa6eefd1SCian Wilson       }
561fa6eefd1SCian Wilson     }
562fa6eefd1SCian Wilson 
563c2fc9fa9SBarry Smith     ierr              = KSPGetIterationNumber(snes->ksp,&lits);CHKERRQ(ierr);
564c2fc9fa9SBarry Smith     snes->linear_its += lits;
565c2fc9fa9SBarry Smith     ierr              = PetscInfo2(snes,"iter=%D, linear solve iterations=%D\n",snes->iter,lits);CHKERRQ(ierr);
566c2fc9fa9SBarry Smith     /*
5676b2b7091SBarry Smith     if (snes->ops->precheck) {
568c2fc9fa9SBarry Smith       PetscBool changed_y = PETSC_FALSE;
5696b2b7091SBarry Smith       ierr = (*snes->ops->precheck)(snes,X,Y,snes->precheck,&changed_y);CHKERRQ(ierr);
570c2fc9fa9SBarry Smith     }
571c2fc9fa9SBarry Smith 
572c2fc9fa9SBarry Smith     if (PetscLogPrintInfo) {
573c2fc9fa9SBarry Smith       ierr = SNESVICheckResidual_Private(snes,snes->jacobian,F,Y,G,W);CHKERRQ(ierr);
574c2fc9fa9SBarry Smith     }
575c2fc9fa9SBarry Smith     */
576c2fc9fa9SBarry Smith     /* Compute a (scaled) negative update in the line search routine:
577c2fc9fa9SBarry Smith          Y <- X - lambda*Y
578c2fc9fa9SBarry Smith        and evaluate G = function(Y) (depends on the line search).
579c2fc9fa9SBarry Smith     */
580c2fc9fa9SBarry Smith     ierr  = VecCopy(Y,snes->vec_sol_update);CHKERRQ(ierr);
581c2fc9fa9SBarry Smith     ynorm = 1; gnorm = fnorm;
582f1c6b773SPeter Brune     ierr  = SNESLineSearchApply(snes->linesearch, X, F, &gnorm, Y);CHKERRQ(ierr);
583422a814eSBarry Smith     ierr  = SNESLineSearchGetReason(snes->linesearch, &lssucceed);CHKERRQ(ierr);
584f1c6b773SPeter Brune     ierr  = SNESLineSearchGetNorms(snes->linesearch, &xnorm, &gnorm, &ynorm);CHKERRQ(ierr);
585c2fc9fa9SBarry 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);
586c2fc9fa9SBarry Smith     if (snes->reason == SNES_DIVERGED_FUNCTION_COUNT) break;
587c2fc9fa9SBarry Smith     if (snes->domainerror) {
588c2fc9fa9SBarry Smith       snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
589c2fc9fa9SBarry Smith       ierr         = DMDestroyVI(snes->dm);CHKERRQ(ierr);
590c2fc9fa9SBarry Smith       PetscFunctionReturn(0);
591c2fc9fa9SBarry Smith     }
592422a814eSBarry Smith     if (lssucceed) {
593c2fc9fa9SBarry Smith       if (++snes->numFailures >= snes->maxFailures) {
594c2fc9fa9SBarry Smith         PetscBool ismin;
595c2fc9fa9SBarry Smith         snes->reason = SNES_DIVERGED_LINE_SEARCH;
5969bd66eb0SPeter Brune         ierr         = SNESVICheckLocalMin_Private(snes,snes->jacobian,F,X,gnorm,&ismin);CHKERRQ(ierr);
597c2fc9fa9SBarry Smith         if (ismin) snes->reason = SNES_DIVERGED_LOCAL_MIN;
598c2fc9fa9SBarry Smith         break;
599c2fc9fa9SBarry Smith       }
600c2fc9fa9SBarry Smith    }
60187e98922SBarry Smith    ierr = DMDestroyVI(snes->dm);CHKERRQ(ierr);
602c2fc9fa9SBarry Smith     /* Update function and solution vectors */
603c2fc9fa9SBarry Smith     fnorm = gnorm;
604c2fc9fa9SBarry Smith     /* Monitor convergence */
605e04113cfSBarry Smith     ierr       = PetscObjectSAWsTakeAccess((PetscObject)snes);CHKERRQ(ierr);
606c2fc9fa9SBarry Smith     snes->iter = i+1;
607c2fc9fa9SBarry Smith     snes->norm = fnorm;
608*c1e67a49SFande Kong     snes->xnorm = xnorm;
609*c1e67a49SFande Kong     snes->ynorm = ynorm;
610e04113cfSBarry Smith     ierr       = PetscObjectSAWsGrantAccess((PetscObject)snes);CHKERRQ(ierr);
611a71f0d7dSBarry Smith     ierr       = SNESLogConvergenceHistory(snes,snes->norm,lits);CHKERRQ(ierr);
612c2fc9fa9SBarry Smith     ierr       = SNESMonitor(snes,snes->iter,snes->norm);CHKERRQ(ierr);
613c2fc9fa9SBarry Smith     /* Test for convergence, xnorm = || X || */
614e2a6519dSDmitry Karpeev     if (snes->ops->converged != SNESConvergedSkip) { ierr = VecNorm(X,NORM_2,&xnorm);CHKERRQ(ierr); }
615c2fc9fa9SBarry Smith     ierr = (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&snes->reason,snes->cnvP);CHKERRQ(ierr);
616c2fc9fa9SBarry Smith     if (snes->reason) break;
617c2fc9fa9SBarry Smith   }
61891a42fcfSBarry 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 */
61991a42fcfSBarry Smith   ierr = DMDestroyVI(snes->dm);CHKERRQ(ierr);
620c2fc9fa9SBarry Smith   if (i == maxits) {
621c2fc9fa9SBarry Smith     ierr = PetscInfo1(snes,"Maximum number of iterations has been reached: %D\n",maxits);CHKERRQ(ierr);
622c2fc9fa9SBarry Smith     if (!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT;
623c2fc9fa9SBarry Smith   }
624c2fc9fa9SBarry Smith   PetscFunctionReturn(0);
625c2fc9fa9SBarry Smith }
626c2fc9fa9SBarry Smith 
627c2fc9fa9SBarry Smith PetscErrorCode SNESVISetRedundancyCheck(SNES snes,PetscErrorCode (*func)(SNES,IS,IS*,void*),void *ctx)
628c2fc9fa9SBarry Smith {
629f450aa47SBarry Smith   SNES_VINEWTONRSLS *vi = (SNES_VINEWTONRSLS*)snes->data;
630c2fc9fa9SBarry Smith 
631c2fc9fa9SBarry Smith   PetscFunctionBegin;
632c2fc9fa9SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
633c2fc9fa9SBarry Smith   vi->checkredundancy = func;
634c2fc9fa9SBarry Smith   vi->ctxP            = ctx;
635c2fc9fa9SBarry Smith   PetscFunctionReturn(0);
636c2fc9fa9SBarry Smith }
637c2fc9fa9SBarry Smith 
638c2fc9fa9SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
639c2fc9fa9SBarry Smith #include <engine.h>
640c2fc9fa9SBarry Smith #include <mex.h>
641c2fc9fa9SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext;
642c2fc9fa9SBarry Smith 
643c2fc9fa9SBarry Smith PetscErrorCode SNESVIRedundancyCheck_Matlab(SNES snes,IS is_act,IS *is_redact,void *ctx)
644c2fc9fa9SBarry Smith {
645c2fc9fa9SBarry Smith   PetscErrorCode    ierr;
646c2fc9fa9SBarry Smith   SNESMatlabContext *sctx = (SNESMatlabContext*)ctx;
647c2fc9fa9SBarry Smith   int               nlhs  = 1, nrhs = 5;
648c2fc9fa9SBarry Smith   mxArray           *plhs[1], *prhs[5];
649c2fc9fa9SBarry Smith   long long int     l1      = 0, l2 = 0, ls = 0;
6500298fd71SBarry Smith   PetscInt          *indices=NULL;
651c2fc9fa9SBarry Smith 
652c2fc9fa9SBarry Smith   PetscFunctionBegin;
653c2fc9fa9SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
654c2fc9fa9SBarry Smith   PetscValidHeaderSpecific(is_act,IS_CLASSID,2);
655c2fc9fa9SBarry Smith   PetscValidPointer(is_redact,3);
656c2fc9fa9SBarry Smith   PetscCheckSameComm(snes,1,is_act,2);
657c2fc9fa9SBarry Smith 
658c2fc9fa9SBarry Smith   /* Create IS for reduced active set of size 0, its size and indices will
659c2fc9fa9SBarry Smith    bet set by the Matlab function */
660ce94432eSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)snes),0,indices,PETSC_OWN_POINTER,is_redact);CHKERRQ(ierr);
661c2fc9fa9SBarry Smith   /* call Matlab function in ctx */
662c2fc9fa9SBarry Smith   ierr    = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
663c2fc9fa9SBarry Smith   ierr    = PetscMemcpy(&l1,&is_act,sizeof(is_act));CHKERRQ(ierr);
664c2fc9fa9SBarry Smith   ierr    = PetscMemcpy(&l2,is_redact,sizeof(is_act));CHKERRQ(ierr);
665c2fc9fa9SBarry Smith   prhs[0] = mxCreateDoubleScalar((double)ls);
666c2fc9fa9SBarry Smith   prhs[1] = mxCreateDoubleScalar((double)l1);
667c2fc9fa9SBarry Smith   prhs[2] = mxCreateDoubleScalar((double)l2);
668c2fc9fa9SBarry Smith   prhs[3] = mxCreateString(sctx->funcname);
669c2fc9fa9SBarry Smith   prhs[4] = sctx->ctx;
670c2fc9fa9SBarry Smith   ierr    = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESVIRedundancyCheckInternal");CHKERRQ(ierr);
671c2fc9fa9SBarry Smith   ierr    = mxGetScalar(plhs[0]);CHKERRQ(ierr);
672c2fc9fa9SBarry Smith   mxDestroyArray(prhs[0]);
673c2fc9fa9SBarry Smith   mxDestroyArray(prhs[1]);
674c2fc9fa9SBarry Smith   mxDestroyArray(prhs[2]);
675c2fc9fa9SBarry Smith   mxDestroyArray(prhs[3]);
676c2fc9fa9SBarry Smith   mxDestroyArray(plhs[0]);
677c2fc9fa9SBarry Smith   PetscFunctionReturn(0);
678c2fc9fa9SBarry Smith }
679c2fc9fa9SBarry Smith 
680c2fc9fa9SBarry Smith PetscErrorCode SNESVISetRedundancyCheckMatlab(SNES snes,const char *func,mxArray *ctx)
681c2fc9fa9SBarry Smith {
682c2fc9fa9SBarry Smith   PetscErrorCode    ierr;
683c2fc9fa9SBarry Smith   SNESMatlabContext *sctx;
684c2fc9fa9SBarry Smith 
685c2fc9fa9SBarry Smith   PetscFunctionBegin;
686c2fc9fa9SBarry Smith   /* currently sctx is memory bleed */
687854ce69bSBarry Smith   ierr      = PetscNew(&sctx);CHKERRQ(ierr);
688c2fc9fa9SBarry Smith   ierr      = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
689c2fc9fa9SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
690c2fc9fa9SBarry Smith   ierr      = SNESVISetRedundancyCheck(snes,SNESVIRedundancyCheck_Matlab,sctx);CHKERRQ(ierr);
691c2fc9fa9SBarry Smith   PetscFunctionReturn(0);
692c2fc9fa9SBarry Smith }
693c2fc9fa9SBarry Smith 
694c2fc9fa9SBarry Smith #endif
695c2fc9fa9SBarry Smith 
696c2fc9fa9SBarry Smith /* -------------------------------------------------------------------------- */
697c2fc9fa9SBarry Smith /*
698f450aa47SBarry Smith    SNESSetUp_VINEWTONRSLS - Sets up the internal data structures for the later use
699c2fc9fa9SBarry Smith    of the SNESVI nonlinear solver.
700c2fc9fa9SBarry Smith 
701c2fc9fa9SBarry Smith    Input Parameter:
702c2fc9fa9SBarry Smith .  snes - the SNES context
703c2fc9fa9SBarry Smith 
704c2fc9fa9SBarry Smith    Application Interface Routine: SNESSetUp()
705c2fc9fa9SBarry Smith 
706c2fc9fa9SBarry Smith    Notes:
707c2fc9fa9SBarry Smith    For basic use of the SNES solvers, the user need not explicitly call
708c2fc9fa9SBarry Smith    SNESSetUp(), since these actions will automatically occur during
709c2fc9fa9SBarry Smith    the call to SNESSolve().
710c2fc9fa9SBarry Smith  */
711f450aa47SBarry Smith PetscErrorCode SNESSetUp_VINEWTONRSLS(SNES snes)
712c2fc9fa9SBarry Smith {
713c2fc9fa9SBarry Smith   PetscErrorCode    ierr;
714f450aa47SBarry Smith   SNES_VINEWTONRSLS *vi = (SNES_VINEWTONRSLS*) snes->data;
715c2fc9fa9SBarry Smith   PetscInt          *indices;
716c2fc9fa9SBarry Smith   PetscInt          i,n,rstart,rend;
717f1c6b773SPeter Brune   SNESLineSearch    linesearch;
718c2fc9fa9SBarry Smith 
719c2fc9fa9SBarry Smith   PetscFunctionBegin;
720c2fc9fa9SBarry Smith   ierr = SNESSetUp_VI(snes);CHKERRQ(ierr);
721c2fc9fa9SBarry Smith 
722c2fc9fa9SBarry Smith   /* Set up previous active index set for the first snes solve
723c2fc9fa9SBarry Smith    vi->IS_inact_prev = 0,1,2,....N */
724c2fc9fa9SBarry Smith 
725c2fc9fa9SBarry Smith   ierr = VecGetOwnershipRange(snes->vec_sol,&rstart,&rend);CHKERRQ(ierr);
726c2fc9fa9SBarry Smith   ierr = VecGetLocalSize(snes->vec_sol,&n);CHKERRQ(ierr);
727785e854fSJed Brown   ierr = PetscMalloc1(n,&indices);CHKERRQ(ierr);
728c2fc9fa9SBarry Smith   for (i=0; i < n; i++) indices[i] = rstart + i;
729ce94432eSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)snes),n,indices,PETSC_OWN_POINTER,&vi->IS_inact_prev);CHKERRQ(ierr);
7309bd66eb0SPeter Brune 
7319bd66eb0SPeter Brune   /* set the line search functions */
7329bd66eb0SPeter Brune   if (!snes->linesearch) {
7337601faf0SJed Brown     ierr = SNESGetLineSearch(snes, &linesearch);CHKERRQ(ierr);
7341a4f838cSPeter Brune     ierr = SNESLineSearchSetType(linesearch, SNESLINESEARCHBT);CHKERRQ(ierr);
7359bd66eb0SPeter Brune   }
736c2fc9fa9SBarry Smith   PetscFunctionReturn(0);
737c2fc9fa9SBarry Smith }
738c2fc9fa9SBarry Smith /* -------------------------------------------------------------------------- */
739f450aa47SBarry Smith PetscErrorCode SNESReset_VINEWTONRSLS(SNES snes)
740c2fc9fa9SBarry Smith {
741f450aa47SBarry Smith   SNES_VINEWTONRSLS *vi = (SNES_VINEWTONRSLS*) snes->data;
742c2fc9fa9SBarry Smith   PetscErrorCode    ierr;
743c2fc9fa9SBarry Smith 
744c2fc9fa9SBarry Smith   PetscFunctionBegin;
745c2fc9fa9SBarry Smith   ierr = SNESReset_VI(snes);CHKERRQ(ierr);
746c2fc9fa9SBarry Smith   ierr = ISDestroy(&vi->IS_inact_prev);CHKERRQ(ierr);
747c2fc9fa9SBarry Smith   PetscFunctionReturn(0);
748c2fc9fa9SBarry Smith }
749c2fc9fa9SBarry Smith 
750c2fc9fa9SBarry Smith /* -------------------------------------------------------------------------- */
751c2fc9fa9SBarry Smith /*MC
752f450aa47SBarry Smith       SNESVINEWTONRSLS - Reduced space active set solvers for variational inequalities based on Newton's method
753c2fc9fa9SBarry Smith 
75461589011SJed Brown    Options Database:
755b621fa8fSRichard Tran Mills +   -snes_type <vinewtonssls,vinewtonrsls> - a semi-smooth solver, a reduced space active set method
75661589011SJed Brown -   -snes_vi_monitor - prints the number of active constraints at each iteration.
757c2fc9fa9SBarry Smith 
758c2fc9fa9SBarry Smith    Level: beginner
759c2fc9fa9SBarry Smith 
760b80f3ac1SShri Abhyankar    References:
76196a0c994SBarry Smith .  1. - T. S. Munson, and S. Benson. Flexible Complementarity Solvers for Large Scale
762b80f3ac1SShri Abhyankar      Applications, Optimization Methods and Software, 21 (2006).
763b80f3ac1SShri Abhyankar 
764f4091ad2SBarry Smith .seealso:  SNESVISetVariableBounds(), SNESVISetComputeVariableBounds(), SNESCreate(), SNES, SNESSetType(), SNESVINEWTONSSLS, SNESNEWTONTR, SNESLineSearchSetType(),SNESLineSearchSetPostCheck(), SNESLineSearchSetPreCheck()
765c2fc9fa9SBarry Smith 
766c2fc9fa9SBarry Smith M*/
7678cc058d9SJed Brown PETSC_EXTERN PetscErrorCode SNESCreate_VINEWTONRSLS(SNES snes)
768c2fc9fa9SBarry Smith {
769c2fc9fa9SBarry Smith   PetscErrorCode    ierr;
770f450aa47SBarry Smith   SNES_VINEWTONRSLS *vi;
771c2fc9fa9SBarry Smith 
772c2fc9fa9SBarry Smith   PetscFunctionBegin;
773f450aa47SBarry Smith   snes->ops->reset          = SNESReset_VINEWTONRSLS;
774f450aa47SBarry Smith   snes->ops->setup          = SNESSetUp_VINEWTONRSLS;
775f450aa47SBarry Smith   snes->ops->solve          = SNESSolve_VINEWTONRSLS;
776c2fc9fa9SBarry Smith   snes->ops->destroy        = SNESDestroy_VI;
777c2fc9fa9SBarry Smith   snes->ops->setfromoptions = SNESSetFromOptions_VI;
7780298fd71SBarry Smith   snes->ops->view           = NULL;
7798d359177SBarry Smith   snes->ops->converged      = SNESConvergedDefault_VI;
780c2fc9fa9SBarry Smith 
781c2fc9fa9SBarry Smith   snes->usesksp = PETSC_TRUE;
782efd4aadfSBarry Smith   snes->usesnpc = PETSC_FALSE;
783c2fc9fa9SBarry Smith 
7844fc747eaSLawrence Mitchell   snes->alwayscomputesfinalresidual = PETSC_TRUE;
7854fc747eaSLawrence Mitchell 
786b00a9115SJed Brown   ierr                = PetscNewLog(snes,&vi);CHKERRQ(ierr);
787c2fc9fa9SBarry Smith   snes->data          = (void*)vi;
7880298fd71SBarry Smith   vi->checkredundancy = NULL;
789c2fc9fa9SBarry Smith 
790bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)snes,"SNESVISetVariableBounds_C",SNESVISetVariableBounds_VI);CHKERRQ(ierr);
791bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)snes,"SNESVISetComputeVariableBounds_C",SNESVISetComputeVariableBounds_VI);CHKERRQ(ierr);
792c2fc9fa9SBarry Smith   PetscFunctionReturn(0);
793c2fc9fa9SBarry Smith }
794c2fc9fa9SBarry Smith 
795