xref: /petsc/src/dm/interface/dmget.c (revision 50eeb1caef1147b508abeaab76ff107eb3036a23)
1af0996ceSBarry Smith #include <petsc/private/dmimpl.h> /*I "petscdm.h" I*/
268260fa0SJed Brown 
368260fa0SJed Brown #undef __FUNCT__
468260fa0SJed Brown #define __FUNCT__ "DMGetLocalVector"
568260fa0SJed Brown /*@
668260fa0SJed Brown    DMGetLocalVector - Gets a Seq PETSc vector that
768260fa0SJed Brown    may be used with the DMXXX routines. This vector has spaces for the ghost values.
868260fa0SJed Brown 
968260fa0SJed Brown    Not Collective
1068260fa0SJed Brown 
1168260fa0SJed Brown    Input Parameter:
1268260fa0SJed Brown .  dm - the distributed array
1368260fa0SJed Brown 
1468260fa0SJed Brown    Output Parameter:
1568260fa0SJed Brown .  g - the local vector
1668260fa0SJed Brown 
1768260fa0SJed Brown    Level: beginner
1868260fa0SJed Brown 
1968260fa0SJed Brown    Note:
2068260fa0SJed Brown    The vector values are NOT initialized and may have garbage in them, so you may need
2168260fa0SJed Brown    to zero them.
2268260fa0SJed Brown 
2368260fa0SJed Brown    The output parameter, g, is a regular PETSc vector that should be returned with
2468260fa0SJed Brown    DMRestoreLocalVector() DO NOT call VecDestroy() on it.
2568260fa0SJed Brown 
26f1978aafSBarry Smith    This is intended to be used for vectors you need for a short time, like within a single function call.
27f1978aafSBarry Smith    For vectors that you intend to keep around (for example in a C struct) or pass around large parts of your
28f1978aafSBarry Smith    code you should use DMCreateLocalVector().
29f1978aafSBarry Smith 
3068260fa0SJed Brown    VecStride*() operations can be useful when using DM with dof > 1
3168260fa0SJed Brown 
3268260fa0SJed Brown .keywords: distributed array, create, local, vector
3368260fa0SJed Brown 
3468260fa0SJed Brown .seealso: DMCreateGlobalVector(), VecDuplicate(), VecDuplicateVecs(),
3568260fa0SJed Brown           DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMGlobalToLocalBegin(),
3668260fa0SJed Brown           DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMCreateLocalVector(), DMRestoreLocalVector(),
3768260fa0SJed Brown           VecStrideMax(), VecStrideMin(), VecStrideNorm()
3868260fa0SJed Brown @*/
3968260fa0SJed Brown PetscErrorCode  DMGetLocalVector(DM dm,Vec *g)
4068260fa0SJed Brown {
4168260fa0SJed Brown   PetscErrorCode ierr,i;
4268260fa0SJed Brown 
4368260fa0SJed Brown   PetscFunctionBegin;
4468260fa0SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4568260fa0SJed Brown   PetscValidPointer(g,2);
4668260fa0SJed Brown   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4768260fa0SJed Brown     if (dm->localin[i]) {
4868260fa0SJed Brown       *g             = dm->localin[i];
490298fd71SBarry Smith       dm->localin[i] = NULL;
5068260fa0SJed Brown       goto alldone;
5168260fa0SJed Brown     }
5268260fa0SJed Brown   }
5368260fa0SJed Brown   ierr = DMCreateLocalVector(dm,g);CHKERRQ(ierr);
5468260fa0SJed Brown 
5568260fa0SJed Brown alldone:
5668260fa0SJed Brown   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
5768260fa0SJed Brown     if (!dm->localout[i]) {
5868260fa0SJed Brown       dm->localout[i] = *g;
5968260fa0SJed Brown       break;
6068260fa0SJed Brown     }
6168260fa0SJed Brown   }
6268260fa0SJed Brown   PetscFunctionReturn(0);
6368260fa0SJed Brown }
6468260fa0SJed Brown 
6568260fa0SJed Brown #undef __FUNCT__
6668260fa0SJed Brown #define __FUNCT__ "DMRestoreLocalVector"
6768260fa0SJed Brown /*@
6868260fa0SJed Brown    DMRestoreLocalVector - Returns a Seq PETSc vector that
6968260fa0SJed Brown      obtained from DMGetLocalVector(). Do not use with vector obtained via
7068260fa0SJed Brown      DMCreateLocalVector().
7168260fa0SJed Brown 
7268260fa0SJed Brown    Not Collective
7368260fa0SJed Brown 
7468260fa0SJed Brown    Input Parameter:
7568260fa0SJed Brown +  dm - the distributed array
7668260fa0SJed Brown -  g - the local vector
7768260fa0SJed Brown 
7868260fa0SJed Brown    Level: beginner
7968260fa0SJed Brown 
8068260fa0SJed Brown .keywords: distributed array, create, local, vector
8168260fa0SJed Brown 
8268260fa0SJed Brown .seealso: DMCreateGlobalVector(), VecDuplicate(), VecDuplicateVecs(),
8368260fa0SJed Brown           DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMGlobalToLocalBegin(),
8468260fa0SJed Brown           DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMCreateLocalVector(), DMGetLocalVector()
8568260fa0SJed Brown @*/
8668260fa0SJed Brown PetscErrorCode  DMRestoreLocalVector(DM dm,Vec *g)
8768260fa0SJed Brown {
8868260fa0SJed Brown   PetscErrorCode ierr;
8968260fa0SJed Brown   PetscInt       i,j;
9068260fa0SJed Brown 
9168260fa0SJed Brown   PetscFunctionBegin;
9268260fa0SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9368260fa0SJed Brown   PetscValidPointer(g,2);
9468260fa0SJed Brown   for (j=0; j<DM_MAX_WORK_VECTORS; j++) {
9568260fa0SJed Brown     if (*g == dm->localout[j]) {
960298fd71SBarry Smith       dm->localout[j] = NULL;
9768260fa0SJed Brown       for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
9868260fa0SJed Brown         if (!dm->localin[i]) {
9968260fa0SJed Brown           dm->localin[i] = *g;
10068260fa0SJed Brown           goto alldone;
10168260fa0SJed Brown         }
10268260fa0SJed Brown       }
10368260fa0SJed Brown     }
10468260fa0SJed Brown   }
10568260fa0SJed Brown   ierr = VecDestroy(g);CHKERRQ(ierr);
10668260fa0SJed Brown alldone:
107dd6887adSBarry Smith   *g = NULL;
10868260fa0SJed Brown   PetscFunctionReturn(0);
10968260fa0SJed Brown }
11068260fa0SJed Brown 
11168260fa0SJed Brown #undef __FUNCT__
11268260fa0SJed Brown #define __FUNCT__ "DMGetGlobalVector"
11368260fa0SJed Brown /*@
11468260fa0SJed Brown    DMGetGlobalVector - Gets a MPI PETSc vector that
11568260fa0SJed Brown    may be used with the DMXXX routines.
11668260fa0SJed Brown 
11768260fa0SJed Brown    Collective on DM
11868260fa0SJed Brown 
11968260fa0SJed Brown    Input Parameter:
12068260fa0SJed Brown .  dm - the distributed array
12168260fa0SJed Brown 
12268260fa0SJed Brown    Output Parameter:
12368260fa0SJed Brown .  g - the global vector
12468260fa0SJed Brown 
12568260fa0SJed Brown    Level: beginner
12668260fa0SJed Brown 
12768260fa0SJed Brown    Note:
12868260fa0SJed Brown    The vector values are NOT initialized and may have garbage in them, so you may need
12968260fa0SJed Brown    to zero them.
13068260fa0SJed Brown 
13168260fa0SJed Brown    The output parameter, g, is a regular PETSc vector that should be returned with
13268260fa0SJed Brown    DMRestoreGlobalVector() DO NOT call VecDestroy() on it.
13368260fa0SJed Brown 
134f1978aafSBarry Smith    This is intended to be used for vectors you need for a short time, like within a single function call.
135f1978aafSBarry Smith    For vectors that you intend to keep around (for example in a C struct) or pass around large parts of your
136f1978aafSBarry Smith    code you should use DMCreateGlobalVector().
137f1978aafSBarry Smith 
13868260fa0SJed Brown    VecStride*() operations can be useful when using DM with dof > 1
13968260fa0SJed Brown 
14068260fa0SJed Brown .keywords: distributed array, create, Global, vector
14168260fa0SJed Brown 
14268260fa0SJed Brown .seealso: DMCreateGlobalVector(), VecDuplicate(), VecDuplicateVecs(),
14368260fa0SJed Brown           DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMGlobalToLocalBegin(),
14468260fa0SJed Brown           DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMCreateLocalVector(), DMRestoreLocalVector()
14568260fa0SJed Brown           VecStrideMax(), VecStrideMin(), VecStrideNorm()
14668260fa0SJed Brown 
14768260fa0SJed Brown @*/
14868260fa0SJed Brown PetscErrorCode  DMGetGlobalVector(DM dm,Vec *g)
14968260fa0SJed Brown {
15068260fa0SJed Brown   PetscErrorCode ierr;
15168260fa0SJed Brown   PetscInt       i;
15268260fa0SJed Brown 
15368260fa0SJed Brown   PetscFunctionBegin;
15468260fa0SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15568260fa0SJed Brown   PetscValidPointer(g,2);
15668260fa0SJed Brown   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
15768260fa0SJed Brown     if (dm->globalin[i]) {
15868260fa0SJed Brown       *g              = dm->globalin[i];
1590298fd71SBarry Smith       dm->globalin[i] = NULL;
16068260fa0SJed Brown       goto alldone;
16168260fa0SJed Brown     }
16268260fa0SJed Brown   }
16368260fa0SJed Brown   ierr = DMCreateGlobalVector(dm,g);CHKERRQ(ierr);
16468260fa0SJed Brown 
16568260fa0SJed Brown alldone:
16668260fa0SJed Brown   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
16768260fa0SJed Brown     if (!dm->globalout[i]) {
16868260fa0SJed Brown       dm->globalout[i] = *g;
16968260fa0SJed Brown       break;
17068260fa0SJed Brown     }
17168260fa0SJed Brown   }
17268260fa0SJed Brown   PetscFunctionReturn(0);
17368260fa0SJed Brown }
17468260fa0SJed Brown 
17568260fa0SJed Brown #undef __FUNCT__
17668260fa0SJed Brown #define __FUNCT__ "DMClearGlobalVectors"
17768260fa0SJed Brown /*@
17868260fa0SJed Brown    DMClearGlobalVectors - Destroys all the global vectors that have been stashed in this DM
17968260fa0SJed Brown 
18068260fa0SJed Brown    Collective on DM
18168260fa0SJed Brown 
18268260fa0SJed Brown    Input Parameter:
18368260fa0SJed Brown .  dm - the distributed array
18468260fa0SJed Brown 
18568260fa0SJed Brown    Level: developer
18668260fa0SJed Brown 
18768260fa0SJed Brown .keywords: distributed array, create, Global, vector
18868260fa0SJed Brown 
18968260fa0SJed Brown .seealso: DMCreateGlobalVector(), VecDuplicate(), VecDuplicateVecs(),
19068260fa0SJed Brown           DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMGlobalToLocalBegin(),
19168260fa0SJed Brown           DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMCreateLocalVector(), DMRestoreLocalVector()
19268260fa0SJed Brown           VecStrideMax(), VecStrideMin(), VecStrideNorm()
19368260fa0SJed Brown 
19468260fa0SJed Brown @*/
19568260fa0SJed Brown PetscErrorCode  DMClearGlobalVectors(DM dm)
19668260fa0SJed Brown {
19768260fa0SJed Brown   PetscErrorCode ierr;
19868260fa0SJed Brown   PetscInt       i;
19968260fa0SJed Brown 
20068260fa0SJed Brown   PetscFunctionBegin;
20168260fa0SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20268260fa0SJed Brown   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
20362d839e9SJed Brown     Vec g;
20468260fa0SJed Brown     if (dm->globalout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Clearing DM of global vectors that has a global vector obtained with DMGetGlobalVector()");
20562d839e9SJed Brown     g = dm->globalin[i];
20662d839e9SJed Brown     dm->globalin[i] = NULL;
20762d839e9SJed Brown     ierr = VecDestroy(&g);CHKERRQ(ierr);
20868260fa0SJed Brown   }
20968260fa0SJed Brown   PetscFunctionReturn(0);
21068260fa0SJed Brown }
21168260fa0SJed Brown 
21268260fa0SJed Brown #undef __FUNCT__
213*50eeb1caSToby Isaac #define __FUNCT__ "DMClearLocalVectors"
214*50eeb1caSToby Isaac /*@
215*50eeb1caSToby Isaac    DMClearLocalVectors - Destroys all the local vectors that have been stashed in this DM
216*50eeb1caSToby Isaac 
217*50eeb1caSToby Isaac    Collective on DM
218*50eeb1caSToby Isaac 
219*50eeb1caSToby Isaac    Input Parameter:
220*50eeb1caSToby Isaac .  dm - the distributed array
221*50eeb1caSToby Isaac 
222*50eeb1caSToby Isaac    Level: developer
223*50eeb1caSToby Isaac 
224*50eeb1caSToby Isaac .keywords: distributed array, create, Local, vector
225*50eeb1caSToby Isaac 
226*50eeb1caSToby Isaac .seealso: DMCreateLocalVector(), VecDuplicate(), VecDuplicateVecs(),
227*50eeb1caSToby Isaac           DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMLocalToLocalBegin(),
228*50eeb1caSToby Isaac           DMLocalToLocalEnd(), DMLocalToLocalBegin(), DMCreateLocalVector(), DMRestoreLocalVector()
229*50eeb1caSToby Isaac           VecStrideMax(), VecStrideMin(), VecStrideNorm()
230*50eeb1caSToby Isaac 
231*50eeb1caSToby Isaac @*/
232*50eeb1caSToby Isaac PetscErrorCode  DMClearLocalVectors(DM dm)
233*50eeb1caSToby Isaac {
234*50eeb1caSToby Isaac   PetscErrorCode ierr;
235*50eeb1caSToby Isaac   PetscInt       i;
236*50eeb1caSToby Isaac 
237*50eeb1caSToby Isaac   PetscFunctionBegin;
238*50eeb1caSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
239*50eeb1caSToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
240*50eeb1caSToby Isaac     Vec g;
241*50eeb1caSToby Isaac     if (dm->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Clearing DM of local vectors that has a local vector obtained with DMGetLocalVector()");
242*50eeb1caSToby Isaac     g = dm->localin[i];
243*50eeb1caSToby Isaac     dm->localin[i] = NULL;
244*50eeb1caSToby Isaac     ierr = VecDestroy(&g);CHKERRQ(ierr);
245*50eeb1caSToby Isaac   }
246*50eeb1caSToby Isaac   PetscFunctionReturn(0);
247*50eeb1caSToby Isaac }
248*50eeb1caSToby Isaac 
249*50eeb1caSToby Isaac #undef __FUNCT__
25068260fa0SJed Brown #define __FUNCT__ "DMRestoreGlobalVector"
25168260fa0SJed Brown /*@
25268260fa0SJed Brown    DMRestoreGlobalVector - Returns a Seq PETSc vector that
25368260fa0SJed Brown      obtained from DMGetGlobalVector(). Do not use with vector obtained via
25468260fa0SJed Brown      DMCreateGlobalVector().
25568260fa0SJed Brown 
25668260fa0SJed Brown    Not Collective
25768260fa0SJed Brown 
25868260fa0SJed Brown    Input Parameter:
25968260fa0SJed Brown +  dm - the distributed array
26068260fa0SJed Brown -  g - the global vector
26168260fa0SJed Brown 
26268260fa0SJed Brown    Level: beginner
26368260fa0SJed Brown 
26468260fa0SJed Brown .keywords: distributed array, create, global, vector
26568260fa0SJed Brown 
26668260fa0SJed Brown .seealso: DMCreateGlobalVector(), VecDuplicate(), VecDuplicateVecs(),
26768260fa0SJed Brown           DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMGlobalToGlobalBegin(),
26868260fa0SJed Brown           DMGlobalToGlobalEnd(), DMGlobalToGlobal(), DMCreateLocalVector(), DMGetGlobalVector()
26968260fa0SJed Brown @*/
27068260fa0SJed Brown PetscErrorCode  DMRestoreGlobalVector(DM dm,Vec *g)
27168260fa0SJed Brown {
27268260fa0SJed Brown   PetscErrorCode ierr;
27368260fa0SJed Brown   PetscInt       i,j;
27468260fa0SJed Brown 
27568260fa0SJed Brown   PetscFunctionBegin;
27668260fa0SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27768260fa0SJed Brown   PetscValidPointer(g,2);
27868260fa0SJed Brown   for (j=0; j<DM_MAX_WORK_VECTORS; j++) {
27968260fa0SJed Brown     if (*g == dm->globalout[j]) {
2800298fd71SBarry Smith       dm->globalout[j] = NULL;
28168260fa0SJed Brown       for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
28268260fa0SJed Brown         if (!dm->globalin[i]) {
28368260fa0SJed Brown           dm->globalin[i] = *g;
28468260fa0SJed Brown           goto alldone;
28568260fa0SJed Brown         }
28668260fa0SJed Brown       }
28768260fa0SJed Brown     }
28868260fa0SJed Brown   }
28968260fa0SJed Brown   ierr = VecDestroy(g);CHKERRQ(ierr);
29068260fa0SJed Brown alldone:
291dd6887adSBarry Smith   *g = NULL;
29268260fa0SJed Brown   PetscFunctionReturn(0);
29368260fa0SJed Brown }
29468260fa0SJed Brown 
29568260fa0SJed Brown #undef __FUNCT__
296e77ac854SMatthew G. Knepley #define __FUNCT__ "DMHasNamedGlobalVector"
297e77ac854SMatthew G. Knepley /*@C
298e77ac854SMatthew G. Knepley    DMHasNamedGlobalVector - check for a named, persistent global vector
299e77ac854SMatthew G. Knepley 
300e77ac854SMatthew G. Knepley    Not Collective
301e77ac854SMatthew G. Knepley 
302e77ac854SMatthew G. Knepley    Input Arguments:
303e77ac854SMatthew G. Knepley +  dm - DM to hold named vectors
304e77ac854SMatthew G. Knepley -  name - unique name for Vec
305e77ac854SMatthew G. Knepley 
306e77ac854SMatthew G. Knepley    Output Arguments:
307e77ac854SMatthew G. Knepley .  exists - true if the vector was previously created
308e77ac854SMatthew G. Knepley 
309e77ac854SMatthew G. Knepley    Level: developer
310e77ac854SMatthew G. Knepley 
311e77ac854SMatthew G. Knepley    Note: If a Vec with the given name does not exist, it is created.
312e77ac854SMatthew G. Knepley 
313e77ac854SMatthew G. Knepley .seealso: DMGetNamedGlobalVector(),DMRestoreNamedLocalVector()
314e77ac854SMatthew G. Knepley @*/
315e77ac854SMatthew G. Knepley PetscErrorCode DMHasNamedGlobalVector(DM dm,const char *name,PetscBool *exists)
316e77ac854SMatthew G. Knepley {
317e77ac854SMatthew G. Knepley   PetscErrorCode ierr;
318e77ac854SMatthew G. Knepley   DMNamedVecLink link;
319e77ac854SMatthew G. Knepley 
320e77ac854SMatthew G. Knepley   PetscFunctionBegin;
321e77ac854SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
322e77ac854SMatthew G. Knepley   PetscValidCharPointer(name,2);
323e77ac854SMatthew G. Knepley   PetscValidPointer(exists,3);
324e77ac854SMatthew G. Knepley   *exists = PETSC_FALSE;
325e77ac854SMatthew G. Knepley   for (link=dm->namedglobal; link; link=link->next) {
326e77ac854SMatthew G. Knepley     PetscBool match;
327e77ac854SMatthew G. Knepley     ierr = PetscStrcmp(name,link->name,&match);CHKERRQ(ierr);
328e77ac854SMatthew G. Knepley     if (match) {
329e77ac854SMatthew G. Knepley       *exists = PETSC_TRUE;
330e77ac854SMatthew G. Knepley       break;
331e77ac854SMatthew G. Knepley     }
332e77ac854SMatthew G. Knepley   }
333e77ac854SMatthew G. Knepley   PetscFunctionReturn(0);
334e77ac854SMatthew G. Knepley }
335e77ac854SMatthew G. Knepley 
336e77ac854SMatthew G. Knepley #undef __FUNCT__
33768260fa0SJed Brown #define __FUNCT__ "DMGetNamedGlobalVector"
33868260fa0SJed Brown /*@C
33968260fa0SJed Brown    DMGetNamedGlobalVector - get access to a named, persistent global vector
34068260fa0SJed Brown 
34168260fa0SJed Brown    Collective on DM
34268260fa0SJed Brown 
34368260fa0SJed Brown    Input Arguments:
34468260fa0SJed Brown +  dm - DM to hold named vectors
34568260fa0SJed Brown -  name - unique name for Vec
34668260fa0SJed Brown 
34768260fa0SJed Brown    Output Arguments:
34868260fa0SJed Brown .  X - named Vec
34968260fa0SJed Brown 
35068260fa0SJed Brown    Level: developer
35168260fa0SJed Brown 
35268260fa0SJed Brown    Note: If a Vec with the given name does not exist, it is created.
35368260fa0SJed Brown 
35468260fa0SJed Brown .seealso: DMRestoreNamedGlobalVector()
35568260fa0SJed Brown @*/
35668260fa0SJed Brown PetscErrorCode DMGetNamedGlobalVector(DM dm,const char *name,Vec *X)
35768260fa0SJed Brown {
35868260fa0SJed Brown   PetscErrorCode ierr;
35968260fa0SJed Brown   DMNamedVecLink link;
36068260fa0SJed Brown 
36168260fa0SJed Brown   PetscFunctionBegin;
36268260fa0SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36368260fa0SJed Brown   PetscValidCharPointer(name,2);
36468260fa0SJed Brown   PetscValidPointer(X,3);
36568260fa0SJed Brown   for (link=dm->namedglobal; link; link=link->next) {
36668260fa0SJed Brown     PetscBool match;
36768260fa0SJed Brown     ierr = PetscStrcmp(name,link->name,&match);CHKERRQ(ierr);
36868260fa0SJed Brown     if (match) {
369ce94432eSBarry Smith       if (link->status != DMVEC_STATUS_IN) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Vec name '%s' already checked out",name);
37068260fa0SJed Brown       goto found;
37168260fa0SJed Brown     }
37268260fa0SJed Brown   }
37368260fa0SJed Brown 
37468260fa0SJed Brown   /* Create the Vec */
375854ce69bSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
37668260fa0SJed Brown   ierr            = PetscStrallocpy(name,&link->name);CHKERRQ(ierr);
37768260fa0SJed Brown   ierr            = DMCreateGlobalVector(dm,&link->X);CHKERRQ(ierr);
37868260fa0SJed Brown   link->next      = dm->namedglobal;
37968260fa0SJed Brown   dm->namedglobal = link;
38068260fa0SJed Brown 
38168260fa0SJed Brown found:
38268260fa0SJed Brown   *X           = link->X;
38368260fa0SJed Brown   link->status = DMVEC_STATUS_OUT;
38468260fa0SJed Brown   PetscFunctionReturn(0);
38568260fa0SJed Brown }
38668260fa0SJed Brown 
38768260fa0SJed Brown #undef __FUNCT__
38868260fa0SJed Brown #define __FUNCT__ "DMRestoreNamedGlobalVector"
38968260fa0SJed Brown /*@C
39068260fa0SJed Brown    DMRestoreNamedGlobalVector - restore access to a named, persistent global vector
39168260fa0SJed Brown 
39268260fa0SJed Brown    Collective on DM
39368260fa0SJed Brown 
39468260fa0SJed Brown    Input Arguments:
39568260fa0SJed Brown +  dm - DM on which the vector was gotten
39668260fa0SJed Brown .  name - name under which the vector was gotten
39768260fa0SJed Brown -  X - Vec to restore
39868260fa0SJed Brown 
39968260fa0SJed Brown    Output Arguments:
40068260fa0SJed Brown 
40168260fa0SJed Brown    Level: developer
40268260fa0SJed Brown 
40368260fa0SJed Brown .seealso: DMGetNamedGlobalVector()
40468260fa0SJed Brown @*/
40568260fa0SJed Brown PetscErrorCode DMRestoreNamedGlobalVector(DM dm,const char *name,Vec *X)
40668260fa0SJed Brown {
40768260fa0SJed Brown   PetscErrorCode ierr;
40868260fa0SJed Brown   DMNamedVecLink link;
40968260fa0SJed Brown 
41068260fa0SJed Brown   PetscFunctionBegin;
41168260fa0SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41268260fa0SJed Brown   PetscValidCharPointer(name,2);
41368260fa0SJed Brown   PetscValidPointer(X,3);
41468260fa0SJed Brown   PetscValidHeaderSpecific(*X,VEC_CLASSID,3);
41568260fa0SJed Brown   for (link=dm->namedglobal; link; link=link->next) {
41668260fa0SJed Brown     PetscBool match;
41768260fa0SJed Brown     ierr = PetscStrcmp(name,link->name,&match);CHKERRQ(ierr);
41868260fa0SJed Brown     if (match) {
419ce94432eSBarry Smith       if (link->status != DMVEC_STATUS_OUT) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Vec name '%s' was not checked out",name);
420ce94432eSBarry Smith       if (link->X != *X) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_INCOMP,"Attempt to restore Vec name '%s', but Vec does not match the cache",name);
42168260fa0SJed Brown       link->status = DMVEC_STATUS_IN;
4220298fd71SBarry Smith       *X           = NULL;
42368260fa0SJed Brown       PetscFunctionReturn(0);
42468260fa0SJed Brown     }
42568260fa0SJed Brown   }
426ce94432eSBarry Smith   SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_INCOMP,"Could not find Vec name '%s' to restore",name);
42768260fa0SJed Brown   PetscFunctionReturn(0);
42868260fa0SJed Brown }
4292348bcf4SPeter Brune 
4302348bcf4SPeter Brune #undef __FUNCT__
431e77ac854SMatthew G. Knepley #define __FUNCT__ "DMHasNamedLocalVector"
432e77ac854SMatthew G. Knepley /*@C
433e77ac854SMatthew G. Knepley    DMHasNamedLocalVector - check for a named, persistent local vector
434e77ac854SMatthew G. Knepley 
435e77ac854SMatthew G. Knepley    Not Collective
436e77ac854SMatthew G. Knepley 
437e77ac854SMatthew G. Knepley    Input Arguments:
438e77ac854SMatthew G. Knepley +  dm - DM to hold named vectors
439e77ac854SMatthew G. Knepley -  name - unique name for Vec
440e77ac854SMatthew G. Knepley 
441e77ac854SMatthew G. Knepley    Output Arguments:
442e77ac854SMatthew G. Knepley .  exists - true if the vector was previously created
443e77ac854SMatthew G. Knepley 
444e77ac854SMatthew G. Knepley    Level: developer
445e77ac854SMatthew G. Knepley 
446e77ac854SMatthew G. Knepley    Note: If a Vec with the given name does not exist, it is created.
447e77ac854SMatthew G. Knepley 
448e77ac854SMatthew G. Knepley .seealso: DMGetNamedGlobalVector(),DMRestoreNamedLocalVector()
449e77ac854SMatthew G. Knepley @*/
450e77ac854SMatthew G. Knepley PetscErrorCode DMHasNamedLocalVector(DM dm,const char *name,PetscBool *exists)
451e77ac854SMatthew G. Knepley {
452e77ac854SMatthew G. Knepley   PetscErrorCode ierr;
453e77ac854SMatthew G. Knepley   DMNamedVecLink link;
454e77ac854SMatthew G. Knepley 
455e77ac854SMatthew G. Knepley   PetscFunctionBegin;
456e77ac854SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
457e77ac854SMatthew G. Knepley   PetscValidCharPointer(name,2);
458e77ac854SMatthew G. Knepley   PetscValidPointer(exists,3);
459e77ac854SMatthew G. Knepley   *exists = PETSC_FALSE;
460e77ac854SMatthew G. Knepley   for (link=dm->namedlocal; link; link=link->next) {
461e77ac854SMatthew G. Knepley     PetscBool match;
462e77ac854SMatthew G. Knepley     ierr = PetscStrcmp(name,link->name,&match);CHKERRQ(ierr);
463e77ac854SMatthew G. Knepley     if (match) {
464e77ac854SMatthew G. Knepley       *exists = PETSC_TRUE;
465e77ac854SMatthew G. Knepley       break;
466e77ac854SMatthew G. Knepley     }
467e77ac854SMatthew G. Knepley   }
468e77ac854SMatthew G. Knepley   PetscFunctionReturn(0);
469e77ac854SMatthew G. Knepley }
470e77ac854SMatthew G. Knepley 
471e77ac854SMatthew G. Knepley #undef __FUNCT__
4722348bcf4SPeter Brune #define __FUNCT__ "DMGetNamedLocalVector"
4732348bcf4SPeter Brune /*@C
4742348bcf4SPeter Brune    DMGetNamedLocalVector - get access to a named, persistent local vector
4752348bcf4SPeter Brune 
4762348bcf4SPeter Brune    Not Collective
4772348bcf4SPeter Brune 
4782348bcf4SPeter Brune    Input Arguments:
4792348bcf4SPeter Brune +  dm - DM to hold named vectors
4802348bcf4SPeter Brune -  name - unique name for Vec
4812348bcf4SPeter Brune 
4822348bcf4SPeter Brune    Output Arguments:
4832348bcf4SPeter Brune .  X - named Vec
4842348bcf4SPeter Brune 
4852348bcf4SPeter Brune    Level: developer
4862348bcf4SPeter Brune 
4872348bcf4SPeter Brune    Note: If a Vec with the given name does not exist, it is created.
4882348bcf4SPeter Brune 
4892348bcf4SPeter Brune .seealso: DMGetNamedGlobalVector(),DMRestoreNamedLocalVector()
4902348bcf4SPeter Brune @*/
4912348bcf4SPeter Brune PetscErrorCode DMGetNamedLocalVector(DM dm,const char *name,Vec *X)
4922348bcf4SPeter Brune {
4932348bcf4SPeter Brune   PetscErrorCode ierr;
4942348bcf4SPeter Brune   DMNamedVecLink link;
4952348bcf4SPeter Brune 
4962348bcf4SPeter Brune   PetscFunctionBegin;
4972348bcf4SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4982348bcf4SPeter Brune   PetscValidCharPointer(name,2);
4992348bcf4SPeter Brune   PetscValidPointer(X,3);
5002348bcf4SPeter Brune   for (link=dm->namedlocal; link; link=link->next) {
5012348bcf4SPeter Brune     PetscBool match;
5022348bcf4SPeter Brune     ierr = PetscStrcmp(name,link->name,&match);CHKERRQ(ierr);
5032348bcf4SPeter Brune     if (match) {
504ce94432eSBarry Smith       if (link->status != DMVEC_STATUS_IN) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Vec name '%s' already checked out",name);
5052348bcf4SPeter Brune       goto found;
5062348bcf4SPeter Brune     }
5072348bcf4SPeter Brune   }
5082348bcf4SPeter Brune 
5092348bcf4SPeter Brune   /* Create the Vec */
510854ce69bSBarry Smith   ierr           = PetscNew(&link);CHKERRQ(ierr);
5112348bcf4SPeter Brune   ierr           = PetscStrallocpy(name,&link->name);CHKERRQ(ierr);
5122348bcf4SPeter Brune   ierr           = DMCreateLocalVector(dm,&link->X);CHKERRQ(ierr);
5132348bcf4SPeter Brune   link->next     = dm->namedlocal;
5142348bcf4SPeter Brune   dm->namedlocal = link;
5152348bcf4SPeter Brune 
5162348bcf4SPeter Brune found:
5172348bcf4SPeter Brune   *X           = link->X;
5182348bcf4SPeter Brune   link->status = DMVEC_STATUS_OUT;
5192348bcf4SPeter Brune   PetscFunctionReturn(0);
5202348bcf4SPeter Brune }
5212348bcf4SPeter Brune 
5222348bcf4SPeter Brune #undef __FUNCT__
5232348bcf4SPeter Brune #define __FUNCT__ "DMRestoreNamedLocalVector"
5242348bcf4SPeter Brune /*@C
5252348bcf4SPeter Brune    DMRestoreNamedLocalVector - restore access to a named, persistent local vector
5262348bcf4SPeter Brune 
5272348bcf4SPeter Brune    Not Collective
5282348bcf4SPeter Brune 
5292348bcf4SPeter Brune    Input Arguments:
5302348bcf4SPeter Brune +  dm - DM on which the vector was gotten
5312348bcf4SPeter Brune .  name - name under which the vector was gotten
5322348bcf4SPeter Brune -  X - Vec to restore
5332348bcf4SPeter Brune 
5342348bcf4SPeter Brune    Output Arguments:
5352348bcf4SPeter Brune 
5362348bcf4SPeter Brune    Level: developer
5372348bcf4SPeter Brune 
5382348bcf4SPeter Brune .seealso: DMRestoreNamedGlobalVector(),DMGetNamedLocalVector()
5392348bcf4SPeter Brune @*/
5402348bcf4SPeter Brune PetscErrorCode DMRestoreNamedLocalVector(DM dm,const char *name,Vec *X)
5412348bcf4SPeter Brune {
5422348bcf4SPeter Brune   PetscErrorCode ierr;
5432348bcf4SPeter Brune   DMNamedVecLink link;
5442348bcf4SPeter Brune 
5452348bcf4SPeter Brune   PetscFunctionBegin;
5462348bcf4SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5472348bcf4SPeter Brune   PetscValidCharPointer(name,2);
5482348bcf4SPeter Brune   PetscValidPointer(X,3);
5492348bcf4SPeter Brune   PetscValidHeaderSpecific(*X,VEC_CLASSID,3);
5502348bcf4SPeter Brune   for (link=dm->namedlocal; link; link=link->next) {
5512348bcf4SPeter Brune     PetscBool match;
5522348bcf4SPeter Brune     ierr = PetscStrcmp(name,link->name,&match);CHKERRQ(ierr);
5532348bcf4SPeter Brune     if (match) {
554ce94432eSBarry Smith       if (link->status != DMVEC_STATUS_OUT) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Vec name '%s' was not checked out",name);
555ce94432eSBarry Smith       if (link->X != *X) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_INCOMP,"Attempt to restore Vec name '%s', but Vec does not match the cache",name);
5562348bcf4SPeter Brune       link->status = DMVEC_STATUS_IN;
5570298fd71SBarry Smith       *X           = NULL;
5582348bcf4SPeter Brune       PetscFunctionReturn(0);
5592348bcf4SPeter Brune     }
5602348bcf4SPeter Brune   }
561ce94432eSBarry Smith   SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_INCOMP,"Could not find Vec name '%s' to restore",name);
5622348bcf4SPeter Brune   PetscFunctionReturn(0);
5632348bcf4SPeter Brune }
564