xref: /petsc/src/dm/interface/dm.c (revision 835c3ec7ce19e5ac5b52b3d452d0b398d46f7605)
1b45d2f2cSJed Brown #include <petsc-private/dmimpl.h>     /*I      "petscdm.h"     I*/
247c6ae99SBarry Smith 
3732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
467a56275SMatthew G Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal;
567a56275SMatthew G Knepley 
647c6ae99SBarry Smith #undef __FUNCT__
7a4121054SBarry Smith #define __FUNCT__ "DMCreate"
8a4121054SBarry Smith /*@
9de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
10a4121054SBarry Smith 
11a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
12a4121054SBarry Smith    error when you try to use the vector.
13a4121054SBarry Smith 
14a4121054SBarry Smith   Collective on MPI_Comm
15a4121054SBarry Smith 
16a4121054SBarry Smith   Input Parameter:
17a4121054SBarry Smith . comm - The communicator for the DM object
18a4121054SBarry Smith 
19a4121054SBarry Smith   Output Parameter:
20a4121054SBarry Smith . dm - The DM object
21a4121054SBarry Smith 
22a4121054SBarry Smith   Level: beginner
23a4121054SBarry Smith 
24a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE
25a4121054SBarry Smith @*/
267087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
27a4121054SBarry Smith {
28a4121054SBarry Smith   DM             v;
29a4121054SBarry Smith   PetscErrorCode ierr;
30a4121054SBarry Smith 
31a4121054SBarry Smith   PetscFunctionBegin;
321411c6eeSJed Brown   PetscValidPointer(dm,2);
331411c6eeSJed Brown   *dm = PETSC_NULL;
34a4121054SBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
35b84caa0eSBarry Smith   ierr = VecInitializePackage(PETSC_NULL);CHKERRQ(ierr);
36b84caa0eSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
37a4121054SBarry Smith   ierr = DMInitializePackage(PETSC_NULL);CHKERRQ(ierr);
38a4121054SBarry Smith #endif
39a4121054SBarry Smith 
403194b578SJed Brown   ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, -1, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
41a4121054SBarry Smith   ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr);
421411c6eeSJed Brown 
43e7c4fc90SDmitry Karpeev 
441411c6eeSJed Brown   v->ltogmap      = PETSC_NULL;
451411c6eeSJed Brown   v->ltogmapb     = PETSC_NULL;
461411c6eeSJed Brown   v->bs           = 1;
47171400e9SBarry Smith   v->coloringtype = IS_COLORING_GLOBAL;
48970e74d5SMatthew G Knepley   v->lf           = PETSC_NULL;
49970e74d5SMatthew G Knepley   v->lj           = PETSC_NULL;
5088ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5188ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
5288ed4aceSMatthew G Knepley   v->defaultSection       = PETSC_NULL;
5388ed4aceSMatthew G Knepley   v->defaultGlobalSection = PETSC_NULL;
54435a35e8SMatthew G Knepley   {
55435a35e8SMatthew G Knepley     PetscInt i;
56435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
57435a35e8SMatthew G Knepley       v->nullspaceConstructors[i] = PETSC_NULL;
58435a35e8SMatthew G Knepley     }
59435a35e8SMatthew G Knepley   }
60af122d2aSMatthew G Knepley   v->numFields = 0;
61af122d2aSMatthew G Knepley   v->fields    = PETSC_NULL;
621411c6eeSJed Brown 
631411c6eeSJed Brown   *dm = v;
64a4121054SBarry Smith   PetscFunctionReturn(0);
65a4121054SBarry Smith }
66a4121054SBarry Smith 
67a4121054SBarry Smith #undef __FUNCT__
689a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
699a42bb27SBarry Smith /*@C
70564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
719a42bb27SBarry Smith 
72aa219208SBarry Smith    Logically Collective on DMDA
739a42bb27SBarry Smith 
749a42bb27SBarry Smith    Input Parameter:
759a42bb27SBarry Smith +  da - initial distributed array
768154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
779a42bb27SBarry Smith 
789a42bb27SBarry Smith    Options Database:
79dd85299cSBarry Smith .   -dm_vec_type ctype
809a42bb27SBarry Smith 
819a42bb27SBarry Smith    Level: intermediate
829a42bb27SBarry Smith 
83aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
849a42bb27SBarry Smith @*/
8519fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
869a42bb27SBarry Smith {
879a42bb27SBarry Smith   PetscErrorCode ierr;
889a42bb27SBarry Smith 
899a42bb27SBarry Smith   PetscFunctionBegin;
909a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
919a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
9219fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
939a42bb27SBarry Smith   PetscFunctionReturn(0);
949a42bb27SBarry Smith }
959a42bb27SBarry Smith 
969a42bb27SBarry Smith #undef __FUNCT__
975f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
985f1ad066SMatthew G Knepley /*@
9934f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
1005f1ad066SMatthew G Knepley 
1015f1ad066SMatthew G Knepley   Not collective
1025f1ad066SMatthew G Knepley 
1035f1ad066SMatthew G Knepley   Input Parameter:
1045f1ad066SMatthew G Knepley . v - The Vec
1055f1ad066SMatthew G Knepley 
1065f1ad066SMatthew G Knepley   Output Parameter:
1075f1ad066SMatthew G Knepley . dm - The DM
1085f1ad066SMatthew G Knepley 
1095f1ad066SMatthew G Knepley   Level: intermediate
1105f1ad066SMatthew G Knepley 
1115f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
1125f1ad066SMatthew G Knepley @*/
1135f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
1145f1ad066SMatthew G Knepley {
1155f1ad066SMatthew G Knepley   PetscErrorCode ierr;
1165f1ad066SMatthew G Knepley 
1175f1ad066SMatthew G Knepley   PetscFunctionBegin;
1185f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1195f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
1205f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject *) dm);CHKERRQ(ierr);
1215f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
1225f1ad066SMatthew G Knepley }
1235f1ad066SMatthew G Knepley 
1245f1ad066SMatthew G Knepley #undef __FUNCT__
1255f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
1265f1ad066SMatthew G Knepley /*@
1275f1ad066SMatthew G Knepley   VecSetDM - Sets the DM defining the data layout of the vector
1285f1ad066SMatthew G Knepley 
1295f1ad066SMatthew G Knepley   Not collective
1305f1ad066SMatthew G Knepley 
1315f1ad066SMatthew G Knepley   Input Parameters:
1325f1ad066SMatthew G Knepley + v - The Vec
1335f1ad066SMatthew G Knepley - dm - The DM
1345f1ad066SMatthew G Knepley 
1355f1ad066SMatthew G Knepley   Level: intermediate
1365f1ad066SMatthew G Knepley 
1375f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
1385f1ad066SMatthew G Knepley @*/
1395f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
1405f1ad066SMatthew G Knepley {
1415f1ad066SMatthew G Knepley   PetscErrorCode ierr;
1425f1ad066SMatthew G Knepley 
1435f1ad066SMatthew G Knepley   PetscFunctionBegin;
1445f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1455f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
1465f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
1475f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
1485f1ad066SMatthew G Knepley }
1495f1ad066SMatthew G Knepley 
1505f1ad066SMatthew G Knepley #undef __FUNCT__
151521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
152521d9a4cSLisandro Dalcin /*@C
153521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
154521d9a4cSLisandro Dalcin 
155521d9a4cSLisandro Dalcin    Logically Collective on DM
156521d9a4cSLisandro Dalcin 
157521d9a4cSLisandro Dalcin    Input Parameter:
158521d9a4cSLisandro Dalcin +  dm - the DM context
159521d9a4cSLisandro Dalcin .  ctype - the matrix type
160521d9a4cSLisandro Dalcin 
161521d9a4cSLisandro Dalcin    Options Database:
162521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
163521d9a4cSLisandro Dalcin 
164521d9a4cSLisandro Dalcin    Level: intermediate
165521d9a4cSLisandro Dalcin 
166521d9a4cSLisandro Dalcin .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType
167521d9a4cSLisandro Dalcin @*/
16819fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
169521d9a4cSLisandro Dalcin {
170521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
171521d9a4cSLisandro Dalcin   PetscFunctionBegin;
172521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
173521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
17419fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
175521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
176521d9a4cSLisandro Dalcin }
177521d9a4cSLisandro Dalcin 
178521d9a4cSLisandro Dalcin #undef __FUNCT__
179c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
180c688c046SMatthew G Knepley /*@
18134f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
182c688c046SMatthew G Knepley 
183c688c046SMatthew G Knepley   Not collective
184c688c046SMatthew G Knepley 
185c688c046SMatthew G Knepley   Input Parameter:
186c688c046SMatthew G Knepley . A - The Mat
187c688c046SMatthew G Knepley 
188c688c046SMatthew G Knepley   Output Parameter:
189c688c046SMatthew G Knepley . dm - The DM
190c688c046SMatthew G Knepley 
191c688c046SMatthew G Knepley   Level: intermediate
192c688c046SMatthew G Knepley 
193c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
194c688c046SMatthew G Knepley @*/
195c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
196c688c046SMatthew G Knepley {
197c688c046SMatthew G Knepley   PetscErrorCode ierr;
198c688c046SMatthew G Knepley 
199c688c046SMatthew G Knepley   PetscFunctionBegin;
200c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
201c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
202c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject *) dm);CHKERRQ(ierr);
203c688c046SMatthew G Knepley   PetscFunctionReturn(0);
204c688c046SMatthew G Knepley }
205c688c046SMatthew G Knepley 
206c688c046SMatthew G Knepley #undef __FUNCT__
207c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
208c688c046SMatthew G Knepley /*@
209c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
210c688c046SMatthew G Knepley 
211c688c046SMatthew G Knepley   Not collective
212c688c046SMatthew G Knepley 
213c688c046SMatthew G Knepley   Input Parameters:
214c688c046SMatthew G Knepley + A - The Mat
215c688c046SMatthew G Knepley - dm - The DM
216c688c046SMatthew G Knepley 
217c688c046SMatthew G Knepley   Level: intermediate
218c688c046SMatthew G Knepley 
219c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
220c688c046SMatthew G Knepley @*/
221c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
222c688c046SMatthew G Knepley {
223c688c046SMatthew G Knepley   PetscErrorCode ierr;
224c688c046SMatthew G Knepley 
225c688c046SMatthew G Knepley   PetscFunctionBegin;
226c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
227a85f731bSMatthew G Knepley   if (dm) {PetscValidHeaderSpecific(dm,DM_CLASSID,2);}
228c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
229c688c046SMatthew G Knepley   PetscFunctionReturn(0);
230c688c046SMatthew G Knepley }
231c688c046SMatthew G Knepley 
232c688c046SMatthew G Knepley #undef __FUNCT__
2339a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
2349a42bb27SBarry Smith /*@C
2359a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
236aa219208SBarry Smith    DMDA options in the database.
2379a42bb27SBarry Smith 
238aa219208SBarry Smith    Logically Collective on DMDA
2399a42bb27SBarry Smith 
2409a42bb27SBarry Smith    Input Parameter:
241aa219208SBarry Smith +  da - the DMDA context
2429a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
2439a42bb27SBarry Smith 
2449a42bb27SBarry Smith    Notes:
2459a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2469a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2479a42bb27SBarry Smith 
2489a42bb27SBarry Smith    Level: advanced
2499a42bb27SBarry Smith 
250aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
2519a42bb27SBarry Smith 
2529a42bb27SBarry Smith .seealso: DMSetFromOptions()
2539a42bb27SBarry Smith @*/
2547087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
2559a42bb27SBarry Smith {
2569a42bb27SBarry Smith   PetscErrorCode ierr;
2579a42bb27SBarry Smith 
2589a42bb27SBarry Smith   PetscFunctionBegin;
2599a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2609a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
2619a42bb27SBarry Smith   PetscFunctionReturn(0);
2629a42bb27SBarry Smith }
2639a42bb27SBarry Smith 
2649a42bb27SBarry Smith #undef __FUNCT__
26547c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
26647c6ae99SBarry Smith /*@
267aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
26847c6ae99SBarry Smith 
26947c6ae99SBarry Smith     Collective on DM
27047c6ae99SBarry Smith 
27147c6ae99SBarry Smith     Input Parameter:
27247c6ae99SBarry Smith .   dm - the DM object to destroy
27347c6ae99SBarry Smith 
27447c6ae99SBarry Smith     Level: developer
27547c6ae99SBarry Smith 
276e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
27747c6ae99SBarry Smith 
27847c6ae99SBarry Smith @*/
279fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
28047c6ae99SBarry Smith {
281af122d2aSMatthew G Knepley   PetscInt       i, cnt = 0, f;
282dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
28347c6ae99SBarry Smith   PetscErrorCode ierr;
28447c6ae99SBarry Smith 
28547c6ae99SBarry Smith   PetscFunctionBegin;
2866bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
2876bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
28887e657c6SBarry Smith 
28987e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
290732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
2916bf464f9SBarry Smith     if ((*dm)->localin[i])  {cnt++;}
2926bf464f9SBarry Smith     if ((*dm)->globalin[i]) {cnt++;}
293732e2eb9SMatthew G Knepley   }
294dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
29571cd77b2SBarry Smith   if ((*dm)->x) {
296c688c046SMatthew G Knepley     DM obj;
297c688c046SMatthew G Knepley     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
298c688c046SMatthew G Knepley     if (obj == *dm) cnt++;
29971cd77b2SBarry Smith   }
300732e2eb9SMatthew G Knepley 
3016bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
302732e2eb9SMatthew G Knepley   /*
303732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
304732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
305732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
306732e2eb9SMatthew G Knepley   */
3076bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
3086bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
309732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
3106bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
3116bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
312732e2eb9SMatthew G Knepley   }
313dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */
314dfe15315SJed Brown     nnext = nlink->next;
315dfe15315SJed Brown     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
316dfe15315SJed Brown     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
317dfe15315SJed Brown     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
318dfe15315SJed Brown     ierr = PetscFree(nlink);CHKERRQ(ierr);
319dfe15315SJed Brown   }
320dfe15315SJed Brown   (*dm)->namedglobal = PETSC_NULL;
3211a266240SBarry Smith 
322b17ce1afSJed Brown   /* Destroy the list of hooks */
323c833c3b5SJed Brown   {
324c833c3b5SJed Brown     DMCoarsenHookLink link,next;
325b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
326b17ce1afSJed Brown       next = link->next;
327b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
328b17ce1afSJed Brown     }
329b17ce1afSJed Brown     (*dm)->coarsenhook = PETSC_NULL;
330c833c3b5SJed Brown   }
331c833c3b5SJed Brown   {
332c833c3b5SJed Brown     DMRefineHookLink link,next;
333c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
334c833c3b5SJed Brown       next = link->next;
335c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
336c833c3b5SJed Brown     }
337c833c3b5SJed Brown     (*dm)->refinehook = PETSC_NULL;
338c833c3b5SJed Brown   }
339aa1993deSMatthew G Knepley   /* Destroy the work arrays */
340aa1993deSMatthew G Knepley   {
341aa1993deSMatthew G Knepley     DMWorkLink link,next;
342aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
343aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
344aa1993deSMatthew G Knepley       next = link->next;
345aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
346aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
347aa1993deSMatthew G Knepley     }
348aa1993deSMatthew G Knepley     (*dm)->workin = PETSC_NULL;
349aa1993deSMatthew G Knepley   }
350b17ce1afSJed Brown 
3511a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
3521a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
3531a266240SBarry Smith   }
35487e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
35571cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
3564dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
3576bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
3586bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
3596bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
360073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
36188ed4aceSMatthew G Knepley 
36288ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
36388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
3648b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
36588ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
36688ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
367af122d2aSMatthew G Knepley 
3686636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
3696636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
3706636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
3716636e97aSMatthew G Knepley 
372af122d2aSMatthew G Knepley   for (f = 0; f < (*dm)->numFields; ++f) {
373af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&(*dm)->fields[f]);CHKERRQ(ierr);
374af122d2aSMatthew G Knepley   }
375af122d2aSMatthew G Knepley   ierr = PetscFree((*dm)->fields);CHKERRQ(ierr);
376732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
3776bf464f9SBarry Smith   ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr);
378732e2eb9SMatthew G Knepley 
3796bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
380435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
381732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
38247c6ae99SBarry Smith   PetscFunctionReturn(0);
38347c6ae99SBarry Smith }
38447c6ae99SBarry Smith 
38547c6ae99SBarry Smith #undef __FUNCT__
386d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
387d7bf68aeSBarry Smith /*@
388d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
389d7bf68aeSBarry Smith 
390d7bf68aeSBarry Smith     Collective on DM
391d7bf68aeSBarry Smith 
392d7bf68aeSBarry Smith     Input Parameter:
393d7bf68aeSBarry Smith .   dm - the DM object to setup
394d7bf68aeSBarry Smith 
395d7bf68aeSBarry Smith     Level: developer
396d7bf68aeSBarry Smith 
397e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
398d7bf68aeSBarry Smith 
399d7bf68aeSBarry Smith @*/
4007087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
401d7bf68aeSBarry Smith {
402d7bf68aeSBarry Smith   PetscErrorCode ierr;
403d7bf68aeSBarry Smith 
404d7bf68aeSBarry Smith   PetscFunctionBegin;
405171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4068387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
407d7bf68aeSBarry Smith   if (dm->ops->setup) {
408d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
409d7bf68aeSBarry Smith   }
4108387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
411d7bf68aeSBarry Smith   PetscFunctionReturn(0);
412d7bf68aeSBarry Smith }
413d7bf68aeSBarry Smith 
414d7bf68aeSBarry Smith #undef __FUNCT__
415d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
416d7bf68aeSBarry Smith /*@
417d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
418d7bf68aeSBarry Smith 
419d7bf68aeSBarry Smith     Collective on DM
420d7bf68aeSBarry Smith 
421d7bf68aeSBarry Smith     Input Parameter:
422d7bf68aeSBarry Smith .   dm - the DM object to set options for
423d7bf68aeSBarry Smith 
424732e2eb9SMatthew G Knepley     Options Database:
425dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
426dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
427171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
428171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
429732e2eb9SMatthew G Knepley 
430d7bf68aeSBarry Smith     Level: developer
431d7bf68aeSBarry Smith 
432e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
433d7bf68aeSBarry Smith 
434d7bf68aeSBarry Smith @*/
4357087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
436d7bf68aeSBarry Smith {
43767ad5babSMatthew G Knepley   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flg4 = PETSC_FALSE,flg;
438d7bf68aeSBarry Smith   PetscErrorCode ierr;
439f9ba7244SBarry Smith   char           typeName[256] = MATAIJ;
440d7bf68aeSBarry Smith 
441d7bf68aeSBarry Smith   PetscFunctionBegin;
442171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4433194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
44482fcb398SMatthew G Knepley     ierr = PetscOptionsBool("-dm_view", "Information on DM", "DMView", flg1, &flg1, PETSC_NULL);CHKERRQ(ierr);
445c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_detail", "Exhaustive mesh description", "DMView", flg2, &flg2, PETSC_NULL);CHKERRQ(ierr);
446c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_vtk", "Output mesh in VTK format", "DMView", flg3, &flg3, PETSC_NULL);CHKERRQ(ierr);
44767ad5babSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_latex", "Output mesh in LaTeX TikZ format", "DMView", flg4, &flg4, PETSC_NULL);CHKERRQ(ierr);
448073dac72SJed Brown     ierr = PetscOptionsBool("-dm_preallocate_only","only preallocate matrix, but do not set column indices","DMSetMatrixPreallocateOnly",dm->prealloc_only,&dm->prealloc_only,PETSC_NULL);CHKERRQ(ierr);
449f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
450f9ba7244SBarry Smith     if (flg) {
451f9ba7244SBarry Smith       ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
452f9ba7244SBarry Smith     }
4538caf3d72SBarry Smith     ierr = PetscOptionsList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype?dm->mattype:typeName,typeName,sizeof(typeName),&flg);CHKERRQ(ierr);
454073dac72SJed Brown     if (flg) {
455521d9a4cSLisandro Dalcin       ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
456073dac72SJed Brown     }
4571b89239cSHong Zhang     ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,PETSC_NULL);CHKERRQ(ierr);
458f9ba7244SBarry Smith     if (dm->ops->setfromoptions) {
459f9ba7244SBarry Smith       ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
460f9ba7244SBarry Smith     }
461f9ba7244SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
462f9ba7244SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
46382fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
46482fcb398SMatthew G Knepley   if (flg1) {
46582fcb398SMatthew G Knepley     ierr = DMView(dm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
46682fcb398SMatthew G Knepley   }
467c4721b0eSMatthew G Knepley   if (flg2) {
468c4721b0eSMatthew G Knepley     PetscViewer viewer;
469c4721b0eSMatthew G Knepley 
470c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
471c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
472c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr);
473c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
474c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
475c4721b0eSMatthew G Knepley   }
476c4721b0eSMatthew G Knepley   if (flg3) {
477c4721b0eSMatthew G Knepley     PetscViewer viewer;
478c4721b0eSMatthew G Knepley 
479c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
480c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
481c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr);
482c4721b0eSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.vtk");CHKERRQ(ierr);
483c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
484c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
485c4721b0eSMatthew G Knepley   }
48667ad5babSMatthew G Knepley   if (flg4) {
48767ad5babSMatthew G Knepley     PetscViewer viewer;
48867ad5babSMatthew G Knepley 
48967ad5babSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
49067ad5babSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
49167ad5babSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_LATEX);CHKERRQ(ierr);
49267ad5babSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.tex");CHKERRQ(ierr);
49367ad5babSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
49467ad5babSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
49567ad5babSMatthew G Knepley   }
496d7bf68aeSBarry Smith   PetscFunctionReturn(0);
497d7bf68aeSBarry Smith }
498d7bf68aeSBarry Smith 
499d7bf68aeSBarry Smith #undef __FUNCT__
50047c6ae99SBarry Smith #define __FUNCT__ "DMView"
501fc9bc008SSatish Balay /*@C
502aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
50347c6ae99SBarry Smith 
50447c6ae99SBarry Smith     Collective on DM
50547c6ae99SBarry Smith 
50647c6ae99SBarry Smith     Input Parameter:
50747c6ae99SBarry Smith +   dm - the DM object to view
50847c6ae99SBarry Smith -   v - the viewer
50947c6ae99SBarry Smith 
51047c6ae99SBarry Smith     Level: developer
51147c6ae99SBarry Smith 
512e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
51347c6ae99SBarry Smith 
51447c6ae99SBarry Smith @*/
5157087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
51647c6ae99SBarry Smith {
51747c6ae99SBarry Smith   PetscErrorCode ierr;
51832c0f0efSBarry Smith   PetscBool      isbinary;
51947c6ae99SBarry Smith 
52047c6ae99SBarry Smith   PetscFunctionBegin;
521171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5223014e516SBarry Smith   if (!v) {
5233014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
5243014e516SBarry Smith   }
52532c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
52632c0f0efSBarry Smith   if (isbinary) {
52755849f57SBarry Smith     PetscInt         classid = DM_FILE_CLASSID;
52832c0f0efSBarry Smith     MPI_Comm         comm;
52932c0f0efSBarry Smith     PetscMPIInt      rank;
53032c0f0efSBarry Smith     char             type[256];
53132c0f0efSBarry Smith 
53232c0f0efSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
53332c0f0efSBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
53432c0f0efSBarry Smith     if (!rank) {
53532c0f0efSBarry Smith       ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
53632c0f0efSBarry Smith       ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
53732c0f0efSBarry Smith       ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
53832c0f0efSBarry Smith     }
53932c0f0efSBarry Smith   }
5400c010503SBarry Smith   if (dm->ops->view) {
5410c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
54247c6ae99SBarry Smith   }
54347c6ae99SBarry Smith   PetscFunctionReturn(0);
54447c6ae99SBarry Smith }
54547c6ae99SBarry Smith 
546ba654b55SMatthew G Knepley PETSC_EXTERN PetscErrorCode VecView_Complex_Local(Vec, PetscViewer);
547ba654b55SMatthew G Knepley PETSC_EXTERN PetscErrorCode VecView_Complex(Vec, PetscViewer);
548ba654b55SMatthew G Knepley 
54947c6ae99SBarry Smith #undef __FUNCT__
55047c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
55147c6ae99SBarry Smith /*@
552aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
55347c6ae99SBarry Smith 
55447c6ae99SBarry Smith     Collective on DM
55547c6ae99SBarry Smith 
55647c6ae99SBarry Smith     Input Parameter:
55747c6ae99SBarry Smith .   dm - the DM object
55847c6ae99SBarry Smith 
55947c6ae99SBarry Smith     Output Parameter:
56047c6ae99SBarry Smith .   vec - the global vector
56147c6ae99SBarry Smith 
562073dac72SJed Brown     Level: beginner
56347c6ae99SBarry Smith 
564e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
56547c6ae99SBarry Smith 
56647c6ae99SBarry Smith @*/
5677087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
56847c6ae99SBarry Smith {
56947c6ae99SBarry Smith   PetscErrorCode ierr;
57047c6ae99SBarry Smith 
57147c6ae99SBarry Smith   PetscFunctionBegin;
572171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
57388ed4aceSMatthew G Knepley   if (dm->defaultSection) {
57488ed4aceSMatthew G Knepley     PetscSection gSection;
5751f588964SMatthew G Knepley     PetscInt     localSize, blockSize = -1, pStart, pEnd, p;
57688ed4aceSMatthew G Knepley 
57788ed4aceSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
5781f588964SMatthew G Knepley     ierr = PetscSectionGetChart(dm->defaultSection, &pStart, &pEnd);CHKERRQ(ierr);
5791f588964SMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
5801f588964SMatthew G Knepley       PetscInt dof, cdof;
5811f588964SMatthew G Knepley 
5821f588964SMatthew G Knepley       ierr = PetscSectionGetDof(dm->defaultSection, p, &dof);CHKERRQ(ierr);
5831f588964SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(dm->defaultSection, p, &cdof);CHKERRQ(ierr);
5841f588964SMatthew G Knepley       if ((blockSize < 0) && (dof > 0)) blockSize = dof-cdof;
5851f588964SMatthew G Knepley       if ((dof > 0) && (dof-cdof != blockSize)) {
5861f588964SMatthew G Knepley         blockSize = 1;
5871f588964SMatthew G Knepley         break;
5881f588964SMatthew G Knepley       }
5891f588964SMatthew G Knepley     }
59088ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstrainedStorageSize(dm->defaultGlobalSection, &localSize);CHKERRQ(ierr);
5911f588964SMatthew G Knepley     if (localSize%blockSize) SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONG, "Mismatch between blocksize %d and local storage size %d", blockSize, localSize);
59288ed4aceSMatthew G Knepley     ierr = VecCreate(((PetscObject) dm)->comm, vec);CHKERRQ(ierr);
59388ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, PETSC_DETERMINE);CHKERRQ(ierr);
5941f588964SMatthew G Knepley     ierr = VecSetBlockSize(*vec, blockSize);CHKERRQ(ierr);
59588ed4aceSMatthew G Knepley     /* ierr = VecSetType(*vec, dm->vectype);CHKERRQ(ierr); */
59688ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
597c688c046SMatthew G Knepley     ierr = VecSetDM(*vec, dm);CHKERRQ(ierr);
59888ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMapping(*vec, dm->ltogmap);CHKERRQ(ierr); */
59988ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMappingBlock(*vec, dm->ltogmapb);CHKERRQ(ierr); */
60088ed4aceSMatthew G Knepley     /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
601ba654b55SMatthew G Knepley     ierr = VecSetOperation(*vec, VECOP_VIEW, (void(*)(void)) VecView_Complex);CHKERRQ(ierr);
60288ed4aceSMatthew G Knepley   } else {
60347c6ae99SBarry Smith     ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
60488ed4aceSMatthew G Knepley   }
60547c6ae99SBarry Smith   PetscFunctionReturn(0);
60647c6ae99SBarry Smith }
60747c6ae99SBarry Smith 
60847c6ae99SBarry Smith #undef __FUNCT__
60947c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
61047c6ae99SBarry Smith /*@
611aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
61247c6ae99SBarry Smith 
61347c6ae99SBarry Smith     Not Collective
61447c6ae99SBarry Smith 
61547c6ae99SBarry Smith     Input Parameter:
61647c6ae99SBarry Smith .   dm - the DM object
61747c6ae99SBarry Smith 
61847c6ae99SBarry Smith     Output Parameter:
61947c6ae99SBarry Smith .   vec - the local vector
62047c6ae99SBarry Smith 
621073dac72SJed Brown     Level: beginner
62247c6ae99SBarry Smith 
623e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
62447c6ae99SBarry Smith 
62547c6ae99SBarry Smith @*/
6267087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
62747c6ae99SBarry Smith {
62847c6ae99SBarry Smith   PetscErrorCode ierr;
62947c6ae99SBarry Smith 
63047c6ae99SBarry Smith   PetscFunctionBegin;
631171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63288ed4aceSMatthew G Knepley   if (dm->defaultSection) {
6331f588964SMatthew G Knepley     PetscInt localSize, blockSize = -1, pStart, pEnd, p;
63488ed4aceSMatthew G Knepley 
6351f588964SMatthew G Knepley     ierr = PetscSectionGetChart(dm->defaultSection, &pStart, &pEnd);CHKERRQ(ierr);
6361f588964SMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
6371f588964SMatthew G Knepley       PetscInt dof;
6381f588964SMatthew G Knepley 
6391f588964SMatthew G Knepley       ierr = PetscSectionGetDof(dm->defaultSection, p, &dof);CHKERRQ(ierr);
6401f588964SMatthew G Knepley       if ((blockSize < 0) && (dof > 0)) blockSize = dof;
6411f588964SMatthew G Knepley       if ((dof > 0) && (dof != blockSize)) {
6421f588964SMatthew G Knepley         blockSize = 1;
6431f588964SMatthew G Knepley         break;
6441f588964SMatthew G Knepley       }
6451f588964SMatthew G Knepley     }
64688ed4aceSMatthew G Knepley     ierr = PetscSectionGetStorageSize(dm->defaultSection, &localSize);CHKERRQ(ierr);
64788ed4aceSMatthew G Knepley     ierr = VecCreate(PETSC_COMM_SELF, vec);CHKERRQ(ierr);
64888ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, localSize);CHKERRQ(ierr);
6491f588964SMatthew G Knepley     ierr = VecSetBlockSize(*vec, blockSize);CHKERRQ(ierr);
65088ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
651c688c046SMatthew G Knepley     ierr = VecSetDM(*vec, dm);CHKERRQ(ierr);
652ba654b55SMatthew G Knepley     ierr = VecSetOperation(*vec, VECOP_VIEW, (void(*)(void)) VecView_Complex_Local);CHKERRQ(ierr);
65388ed4aceSMatthew G Knepley   } else {
65447c6ae99SBarry Smith     ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
65588ed4aceSMatthew G Knepley   }
65647c6ae99SBarry Smith   PetscFunctionReturn(0);
65747c6ae99SBarry Smith }
65847c6ae99SBarry Smith 
65947c6ae99SBarry Smith #undef __FUNCT__
6601411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
6611411c6eeSJed Brown /*@
6621411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
6631411c6eeSJed Brown 
6641411c6eeSJed Brown    Collective on DM
6651411c6eeSJed Brown 
6661411c6eeSJed Brown    Input Parameter:
6671411c6eeSJed Brown .  dm - the DM that provides the mapping
6681411c6eeSJed Brown 
6691411c6eeSJed Brown    Output Parameter:
6701411c6eeSJed Brown .  ltog - the mapping
6711411c6eeSJed Brown 
6721411c6eeSJed Brown    Level: intermediate
6731411c6eeSJed Brown 
6741411c6eeSJed Brown    Notes:
6751411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
6761411c6eeSJed Brown    MatSetLocalToGlobalMapping().
6771411c6eeSJed Brown 
6781411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
6791411c6eeSJed Brown @*/
6807087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
6811411c6eeSJed Brown {
6821411c6eeSJed Brown   PetscErrorCode ierr;
6831411c6eeSJed Brown 
6841411c6eeSJed Brown   PetscFunctionBegin;
6851411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6861411c6eeSJed Brown   PetscValidPointer(ltog,2);
6871411c6eeSJed Brown   if (!dm->ltogmap) {
68837d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
68937d0c07bSMatthew G Knepley 
69037d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
69137d0c07bSMatthew G Knepley     if (section) {
69237d0c07bSMatthew G Knepley       PetscInt      *ltog;
69337d0c07bSMatthew G Knepley       PetscInt       pStart, pEnd, size, p, l;
69437d0c07bSMatthew G Knepley 
69537d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
69637d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
69737d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
69837d0c07bSMatthew G Knepley       ierr = PetscMalloc(size * sizeof(PetscInt), &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
69937d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
70037d0c07bSMatthew G Knepley         PetscInt dof, off, c;
70137d0c07bSMatthew G Knepley 
70237d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
70337d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
70437d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
70537d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
70637d0c07bSMatthew G Knepley           ltog[l] = off+c;
70737d0c07bSMatthew G Knepley         }
70837d0c07bSMatthew G Knepley       }
70937d0c07bSMatthew G Knepley       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
71037d0c07bSMatthew G Knepley       ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr);
71137d0c07bSMatthew G Knepley     } else {
7121411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
7131411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
7141411c6eeSJed Brown     }
71537d0c07bSMatthew G Knepley   }
7161411c6eeSJed Brown   *ltog = dm->ltogmap;
7171411c6eeSJed Brown   PetscFunctionReturn(0);
7181411c6eeSJed Brown }
7191411c6eeSJed Brown 
7201411c6eeSJed Brown #undef __FUNCT__
7211411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
7221411c6eeSJed Brown /*@
7231411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
7241411c6eeSJed Brown 
7251411c6eeSJed Brown    Collective on DM
7261411c6eeSJed Brown 
7271411c6eeSJed Brown    Input Parameter:
7281411c6eeSJed Brown .  da - the distributed array that provides the mapping
7291411c6eeSJed Brown 
7301411c6eeSJed Brown    Output Parameter:
7311411c6eeSJed Brown .  ltog - the block mapping
7321411c6eeSJed Brown 
7331411c6eeSJed Brown    Level: intermediate
7341411c6eeSJed Brown 
7351411c6eeSJed Brown    Notes:
7361411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
7371411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
7381411c6eeSJed Brown 
7391411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
7401411c6eeSJed Brown @*/
7417087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
7421411c6eeSJed Brown {
7431411c6eeSJed Brown   PetscErrorCode ierr;
7441411c6eeSJed Brown 
7451411c6eeSJed Brown   PetscFunctionBegin;
7461411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7471411c6eeSJed Brown   PetscValidPointer(ltog,2);
7481411c6eeSJed Brown   if (!dm->ltogmapb) {
7491411c6eeSJed Brown     PetscInt bs;
7501411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
7511411c6eeSJed Brown     if (bs > 1) {
7521411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
7531411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
7541411c6eeSJed Brown     } else {
7551411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
7561411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
7571411c6eeSJed Brown     }
7581411c6eeSJed Brown   }
7591411c6eeSJed Brown   *ltog = dm->ltogmapb;
7601411c6eeSJed Brown   PetscFunctionReturn(0);
7611411c6eeSJed Brown }
7621411c6eeSJed Brown 
7631411c6eeSJed Brown #undef __FUNCT__
7641411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
7651411c6eeSJed Brown /*@
7661411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
7671411c6eeSJed Brown 
7681411c6eeSJed Brown    Not Collective
7691411c6eeSJed Brown 
7701411c6eeSJed Brown    Input Parameter:
7711411c6eeSJed Brown .  dm - the DM with block structure
7721411c6eeSJed Brown 
7731411c6eeSJed Brown    Output Parameter:
7741411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
7751411c6eeSJed Brown 
7761411c6eeSJed Brown    Level: intermediate
7771411c6eeSJed Brown 
7781411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
7791411c6eeSJed Brown @*/
7807087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
7811411c6eeSJed Brown {
7821411c6eeSJed Brown   PetscFunctionBegin;
7831411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7841411c6eeSJed Brown   PetscValidPointer(bs,2);
7851411c6eeSJed Brown   if (dm->bs < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet");
7861411c6eeSJed Brown   *bs = dm->bs;
7871411c6eeSJed Brown   PetscFunctionReturn(0);
7881411c6eeSJed Brown }
7891411c6eeSJed Brown 
7901411c6eeSJed Brown #undef __FUNCT__
791e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
79247c6ae99SBarry Smith /*@
793e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
79447c6ae99SBarry Smith 
79547c6ae99SBarry Smith     Collective on DM
79647c6ae99SBarry Smith 
79747c6ae99SBarry Smith     Input Parameter:
79847c6ae99SBarry Smith +   dm1 - the DM object
79947c6ae99SBarry Smith -   dm2 - the second, finer DM object
80047c6ae99SBarry Smith 
80147c6ae99SBarry Smith     Output Parameter:
80247c6ae99SBarry Smith +  mat - the interpolation
80347c6ae99SBarry Smith -  vec - the scaling (optional)
80447c6ae99SBarry Smith 
80547c6ae99SBarry Smith     Level: developer
80647c6ae99SBarry Smith 
80785afcc9aSBarry Smith     Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
80885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
809d52bd9f3SBarry Smith 
8101f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
811d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
81285afcc9aSBarry Smith 
81385afcc9aSBarry Smith 
814e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
81547c6ae99SBarry Smith 
81647c6ae99SBarry Smith @*/
817e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
81847c6ae99SBarry Smith {
81947c6ae99SBarry Smith   PetscErrorCode ierr;
82047c6ae99SBarry Smith 
82147c6ae99SBarry Smith   PetscFunctionBegin;
822171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
823171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
82425296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
82547c6ae99SBarry Smith   PetscFunctionReturn(0);
82647c6ae99SBarry Smith }
82747c6ae99SBarry Smith 
82847c6ae99SBarry Smith #undef __FUNCT__
829e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
83047c6ae99SBarry Smith /*@
831e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
83247c6ae99SBarry Smith 
83347c6ae99SBarry Smith     Collective on DM
83447c6ae99SBarry Smith 
83547c6ae99SBarry Smith     Input Parameter:
83647c6ae99SBarry Smith +   dm1 - the DM object
83747c6ae99SBarry Smith -   dm2 - the second, finer DM object
83847c6ae99SBarry Smith 
83947c6ae99SBarry Smith     Output Parameter:
84047c6ae99SBarry Smith .   ctx - the injection
84147c6ae99SBarry Smith 
84247c6ae99SBarry Smith     Level: developer
84347c6ae99SBarry Smith 
84485afcc9aSBarry Smith    Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
84585afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
84685afcc9aSBarry Smith 
847e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
84847c6ae99SBarry Smith 
84947c6ae99SBarry Smith @*/
850e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
85147c6ae99SBarry Smith {
85247c6ae99SBarry Smith   PetscErrorCode ierr;
85347c6ae99SBarry Smith 
85447c6ae99SBarry Smith   PetscFunctionBegin;
855171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
856171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
85747c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
85847c6ae99SBarry Smith   PetscFunctionReturn(0);
85947c6ae99SBarry Smith }
86047c6ae99SBarry Smith 
86147c6ae99SBarry Smith #undef __FUNCT__
862e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
863d1e2c406SBarry Smith /*@C
864e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
86547c6ae99SBarry Smith 
86647c6ae99SBarry Smith     Collective on DM
86747c6ae99SBarry Smith 
86847c6ae99SBarry Smith     Input Parameter:
86947c6ae99SBarry Smith +   dm - the DM object
87047c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
87147c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
87247c6ae99SBarry Smith 
87347c6ae99SBarry Smith     Output Parameter:
87447c6ae99SBarry Smith .   coloring - the coloring
87547c6ae99SBarry Smith 
87647c6ae99SBarry Smith     Level: developer
87747c6ae99SBarry Smith 
878e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
87947c6ae99SBarry Smith 
880aab9d709SJed Brown @*/
88119fd82e9SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,MatType mtype,ISColoring *coloring)
88247c6ae99SBarry Smith {
88347c6ae99SBarry Smith   PetscErrorCode ierr;
88447c6ae99SBarry Smith 
88547c6ae99SBarry Smith   PetscFunctionBegin;
886171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
88747c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
88847c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
88947c6ae99SBarry Smith   PetscFunctionReturn(0);
89047c6ae99SBarry Smith }
89147c6ae99SBarry Smith 
89247c6ae99SBarry Smith #undef __FUNCT__
893950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
89447c6ae99SBarry Smith /*@C
895950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
89647c6ae99SBarry Smith 
89747c6ae99SBarry Smith     Collective on DM
89847c6ae99SBarry Smith 
89947c6ae99SBarry Smith     Input Parameter:
90047c6ae99SBarry Smith +   dm - the DM object
90147c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
90294013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
90347c6ae99SBarry Smith 
90447c6ae99SBarry Smith     Output Parameter:
90547c6ae99SBarry Smith .   mat - the empty Jacobian
90647c6ae99SBarry Smith 
907073dac72SJed Brown     Level: beginner
90847c6ae99SBarry Smith 
90994013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
91094013140SBarry Smith        do not need to do it yourself.
91194013140SBarry Smith 
91294013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
913aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
91494013140SBarry Smith 
91594013140SBarry Smith        For structured grid problems, when you call MatView() on this matrix it is displayed using the global natural ordering, NOT in the ordering used
91694013140SBarry Smith        internally by PETSc.
91794013140SBarry Smith 
91894013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
919aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
92094013140SBarry Smith 
921e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
92247c6ae99SBarry Smith 
923aab9d709SJed Brown @*/
92419fd82e9SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,MatType mtype,Mat *mat)
92547c6ae99SBarry Smith {
92647c6ae99SBarry Smith   PetscErrorCode ierr;
92747c6ae99SBarry Smith 
92847c6ae99SBarry Smith   PetscFunctionBegin;
929171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
930235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
931235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
932235683edSBarry Smith #endif
933c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
934c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
935073dac72SJed Brown   if (dm->mattype) {
93625296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
937073dac72SJed Brown   } else {
93825296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
939c7b7c8a4SJed Brown   }
94047c6ae99SBarry Smith   PetscFunctionReturn(0);
94147c6ae99SBarry Smith }
94247c6ae99SBarry Smith 
94347c6ae99SBarry Smith #undef __FUNCT__
944732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
945732e2eb9SMatthew G Knepley /*@
946950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
947732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
948732e2eb9SMatthew G Knepley 
949732e2eb9SMatthew G Knepley   Logically Collective on DMDA
950732e2eb9SMatthew G Knepley 
951732e2eb9SMatthew G Knepley   Input Parameter:
952732e2eb9SMatthew G Knepley + dm - the DM
953732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
954732e2eb9SMatthew G Knepley 
955732e2eb9SMatthew G Knepley   Level: developer
956950540a4SJed Brown .seealso DMCreateMatrix()
957732e2eb9SMatthew G Knepley @*/
958732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
959732e2eb9SMatthew G Knepley {
960732e2eb9SMatthew G Knepley   PetscFunctionBegin;
961732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
962732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
963732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
964732e2eb9SMatthew G Knepley }
965732e2eb9SMatthew G Knepley 
966732e2eb9SMatthew G Knepley #undef __FUNCT__
967a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
968a89ea682SMatthew G Knepley /*@C
969aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
970a89ea682SMatthew G Knepley 
971a89ea682SMatthew G Knepley   Not Collective
972a89ea682SMatthew G Knepley 
973a89ea682SMatthew G Knepley   Input Parameters:
974a89ea682SMatthew G Knepley + dm - the DM object
975aa1993deSMatthew G Knepley . count - The minium size
976aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
977a89ea682SMatthew G Knepley 
978a89ea682SMatthew G Knepley   Output Parameter:
979a89ea682SMatthew G Knepley . array - the work array
980a89ea682SMatthew G Knepley 
981a89ea682SMatthew G Knepley   Level: developer
982a89ea682SMatthew G Knepley 
983a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
984a89ea682SMatthew G Knepley @*/
985aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
986a89ea682SMatthew G Knepley {
987a89ea682SMatthew G Knepley   PetscErrorCode ierr;
988aa1993deSMatthew G Knepley   DMWorkLink link;
989aa1993deSMatthew G Knepley   size_t size;
990a89ea682SMatthew G Knepley 
991a89ea682SMatthew G Knepley   PetscFunctionBegin;
992a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
993aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
994aa1993deSMatthew G Knepley   if (dm->workin) {
995aa1993deSMatthew G Knepley     link = dm->workin;
996aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
997aa1993deSMatthew G Knepley   } else {
998aa1993deSMatthew G Knepley     ierr = PetscNewLog(dm,struct _DMWorkLink,&link);CHKERRQ(ierr);
999a89ea682SMatthew G Knepley   }
1000aa1993deSMatthew G Knepley   ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr);
1001aa1993deSMatthew G Knepley   if (size*count > link->bytes) {
1002aa1993deSMatthew G Knepley     ierr = PetscFree(link->mem);CHKERRQ(ierr);
1003aa1993deSMatthew G Knepley     ierr = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr);
1004aa1993deSMatthew G Knepley     link->bytes = size*count;
1005aa1993deSMatthew G Knepley   }
1006aa1993deSMatthew G Knepley   link->next = dm->workout;
1007aa1993deSMatthew G Knepley   dm->workout = link;
1008aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1009a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1010a89ea682SMatthew G Knepley }
1011a89ea682SMatthew G Knepley 
1012aa1993deSMatthew G Knepley #undef __FUNCT__
1013aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1014aa1993deSMatthew G Knepley /*@C
1015aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1016aa1993deSMatthew G Knepley 
1017aa1993deSMatthew G Knepley   Not Collective
1018aa1993deSMatthew G Knepley 
1019aa1993deSMatthew G Knepley   Input Parameters:
1020aa1993deSMatthew G Knepley + dm - the DM object
1021aa1993deSMatthew G Knepley . count - The minium size
1022aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1023aa1993deSMatthew G Knepley 
1024aa1993deSMatthew G Knepley   Output Parameter:
1025aa1993deSMatthew G Knepley . array - the work array
1026aa1993deSMatthew G Knepley 
1027aa1993deSMatthew G Knepley   Level: developer
1028aa1993deSMatthew G Knepley 
1029aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1030aa1993deSMatthew G Knepley @*/
1031aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1032aa1993deSMatthew G Knepley {
1033aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1034aa1993deSMatthew G Knepley 
1035aa1993deSMatthew G Knepley   PetscFunctionBegin;
1036aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1037aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1038aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1039aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1040aa1993deSMatthew G Knepley       *p = link->next;
1041aa1993deSMatthew G Knepley       link->next = dm->workin;
1042aa1993deSMatthew G Knepley       dm->workin = link;
1043aa1993deSMatthew G Knepley       *(void**)mem = PETSC_NULL;
1044aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1045aa1993deSMatthew G Knepley     }
1046aa1993deSMatthew G Knepley   }
1047aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1048aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
1049aa1993deSMatthew G Knepley }
1050e7c4fc90SDmitry Karpeev 
1051e7c4fc90SDmitry Karpeev #undef __FUNCT__
1052435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1053435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1054435a35e8SMatthew G Knepley {
1055435a35e8SMatthew G Knepley   PetscFunctionBegin;
1056435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1057435a35e8SMatthew G Knepley   if (field >= 10) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1058435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1059435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1060435a35e8SMatthew G Knepley }
1061435a35e8SMatthew G Knepley 
1062435a35e8SMatthew G Knepley #undef __FUNCT__
10634d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
10644f3b5142SJed Brown /*@C
10654d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
10664d343eeaSMatthew G Knepley 
10674d343eeaSMatthew G Knepley   Not collective
10684d343eeaSMatthew G Knepley 
10694d343eeaSMatthew G Knepley   Input Parameter:
10704d343eeaSMatthew G Knepley . dm - the DM object
10714d343eeaSMatthew G Knepley 
10724d343eeaSMatthew G Knepley   Output Parameters:
107321c9b008SJed Brown + numFields  - The number of fields (or PETSC_NULL if not requested)
107437d0c07bSMatthew G Knepley . fieldNames - The name for each field (or PETSC_NULL if not requested)
107521c9b008SJed Brown - fields     - The global indices for each field (or PETSC_NULL if not requested)
10764d343eeaSMatthew G Knepley 
10774d343eeaSMatthew G Knepley   Level: intermediate
10784d343eeaSMatthew G Knepley 
107921c9b008SJed Brown   Notes:
108021c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
108121c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
108221c9b008SJed Brown   PetscFree().
108321c9b008SJed Brown 
10844d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
10854d343eeaSMatthew G Knepley @*/
108637d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
10874d343eeaSMatthew G Knepley {
108837d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
10894d343eeaSMatthew G Knepley   PetscErrorCode ierr;
10904d343eeaSMatthew G Knepley 
10914d343eeaSMatthew G Knepley   PetscFunctionBegin;
10924d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
109369ca1f37SDmitry Karpeev   if (numFields) {
109469ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
109569ca1f37SDmitry Karpeev     *numFields = 0;
109669ca1f37SDmitry Karpeev   }
109737d0c07bSMatthew G Knepley   if (fieldNames) {
109837d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
109937d0c07bSMatthew G Knepley     *fieldNames = PETSC_NULL;
110069ca1f37SDmitry Karpeev   }
110169ca1f37SDmitry Karpeev   if (fields) {
110269ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
110369ca1f37SDmitry Karpeev     *fields = PETSC_NULL;
110469ca1f37SDmitry Karpeev   }
110537d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
110637d0c07bSMatthew G Knepley   if (section) {
110737d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
110837d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
110937d0c07bSMatthew G Knepley 
111037d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
111137d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
111237d0c07bSMatthew G Knepley     ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt *,&fieldIndices);CHKERRQ(ierr);
111337d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
111437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
111537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
111637d0c07bSMatthew G Knepley     }
111737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
111837d0c07bSMatthew G Knepley       PetscInt gdof;
111937d0c07bSMatthew G Knepley 
112037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
112137d0c07bSMatthew G Knepley       if (gdof > 0) {
112237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
112337d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
112437d0c07bSMatthew G Knepley 
112537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
112637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
112737d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
112837d0c07bSMatthew G Knepley         }
112937d0c07bSMatthew G Knepley       }
113037d0c07bSMatthew G Knepley     }
113137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
113237d0c07bSMatthew G Knepley       ierr = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr);
113337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
113437d0c07bSMatthew G Knepley     }
113537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
113637d0c07bSMatthew G Knepley       PetscInt gdof, goff;
113737d0c07bSMatthew G Knepley 
113837d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
113937d0c07bSMatthew G Knepley       if (gdof > 0) {
114037d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
114137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
114237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
114337d0c07bSMatthew G Knepley 
114437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
114537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
114637d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
114737d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
114837d0c07bSMatthew G Knepley           }
114937d0c07bSMatthew G Knepley         }
115037d0c07bSMatthew G Knepley       }
115137d0c07bSMatthew G Knepley     }
115237d0c07bSMatthew G Knepley     if (numFields) {*numFields = nF;}
115337d0c07bSMatthew G Knepley     if (fieldNames) {
115437d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(char *), fieldNames);CHKERRQ(ierr);
115537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
115637d0c07bSMatthew G Knepley         const char *fieldName;
115737d0c07bSMatthew G Knepley 
115837d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
115937d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*fieldNames)[f]);CHKERRQ(ierr);
116037d0c07bSMatthew G Knepley       }
116137d0c07bSMatthew G Knepley     }
116237d0c07bSMatthew G Knepley     if (fields) {
116337d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr);
116437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
116537d0c07bSMatthew G Knepley         ierr = ISCreateGeneral(((PetscObject) dm)->comm, fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
116637d0c07bSMatthew G Knepley       }
116737d0c07bSMatthew G Knepley     }
116837d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
116937d0c07bSMatthew G Knepley   } else {
117037d0c07bSMatthew G Knepley     if (dm->ops->createfieldis) {ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);}
117169ca1f37SDmitry Karpeev   }
11724d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
11734d343eeaSMatthew G Knepley }
11744d343eeaSMatthew G Knepley 
1175a89ea682SMatthew G Knepley #undef __FUNCT__
117616621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecompositionDM"
1177e7c4fc90SDmitry Karpeev /*@C
117816621825SDmitry Karpeev   DMCreateFieldDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into fields.
117916621825SDmitry Karpeev 
118016621825SDmitry Karpeev   Not Collective
118116621825SDmitry Karpeev 
118216621825SDmitry Karpeev   Input Parameters:
118316621825SDmitry Karpeev + dm   - the DM object
118416621825SDmitry Karpeev - name - the name of the field decomposition
118516621825SDmitry Karpeev 
118616621825SDmitry Karpeev   Output Parameter:
118716621825SDmitry Karpeev . ddm  - the field decomposition DM (PETSC_NULL, if no such decomposition is known)
118816621825SDmitry Karpeev 
118916621825SDmitry Karpeev   Level: advanced
119016621825SDmitry Karpeev 
119116621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
119216621825SDmitry Karpeev @*/
119316621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecompositionDM(DM dm, const char* name, DM *ddm)
119416621825SDmitry Karpeev {
119516621825SDmitry Karpeev   PetscErrorCode ierr;
119616621825SDmitry Karpeev 
119716621825SDmitry Karpeev   PetscFunctionBegin;
119816621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
119916621825SDmitry Karpeev   PetscValidCharPointer(name,2);
120016621825SDmitry Karpeev   PetscValidPointer(ddm,3);
120116621825SDmitry Karpeev   *ddm = PETSC_NULL;
1202731c8d9eSDmitry Karpeev   if (dm->ops->createfielddecompositiondm) {
120316621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
120416621825SDmitry Karpeev   }
120516621825SDmitry Karpeev   PetscFunctionReturn(0);
120616621825SDmitry Karpeev }
120716621825SDmitry Karpeev 
120816621825SDmitry Karpeev #undef __FUNCT__
120916621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
121016621825SDmitry Karpeev /*@C
121116621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
121216621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
121316621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1214e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1215e7c4fc90SDmitry Karpeev 
1216e7c4fc90SDmitry Karpeev   Not collective
1217e7c4fc90SDmitry Karpeev 
1218e7c4fc90SDmitry Karpeev   Input Parameter:
1219e7c4fc90SDmitry Karpeev . dm - the DM object
1220e7c4fc90SDmitry Karpeev 
1221e7c4fc90SDmitry Karpeev   Output Parameters:
122216621825SDmitry Karpeev + len       - The number of subproblems in the field decomposition (or PETSC_NULL if not requested)
122316621825SDmitry Karpeev . namelist  - The name for each field (or PETSC_NULL if not requested)
122416621825SDmitry Karpeev . islist    - The global indices for each field (or PETSC_NULL if not requested)
122516621825SDmitry Karpeev - dmlist    - The DMs for each field subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
1226e7c4fc90SDmitry Karpeev 
1227e7c4fc90SDmitry Karpeev   Level: intermediate
1228e7c4fc90SDmitry Karpeev 
1229e7c4fc90SDmitry Karpeev   Notes:
1230e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1231e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1232e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1233e7c4fc90SDmitry Karpeev 
1234e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1235e7c4fc90SDmitry Karpeev @*/
123616621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1237e7c4fc90SDmitry Karpeev {
1238e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1239e7c4fc90SDmitry Karpeev 
1240e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1241e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1242731c8d9eSDmitry Karpeev   if (len)      {PetscValidPointer(len,2);      *len      = 0;}
1243731c8d9eSDmitry Karpeev   if (namelist) {PetscValidPointer(namelist,3); *namelist = 0;}
1244731c8d9eSDmitry Karpeev   if (islist)   {PetscValidPointer(islist,4);   *islist   = 0;}
1245731c8d9eSDmitry Karpeev   if (dmlist)   {PetscValidPointer(dmlist,5);   *dmlist   = 0;}
124616621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1247435a35e8SMatthew G Knepley     PetscSection section;
1248435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1249435a35e8SMatthew G Knepley 
1250435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1251435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1252435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1253435a35e8SMatthew G Knepley       *len = numFields;
1254435a35e8SMatthew G Knepley       ierr = PetscMalloc3(numFields,char*,namelist,numFields,IS,islist,numFields,DM,dmlist);CHKERRQ(ierr);
1255435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1256435a35e8SMatthew G Knepley         const char *fieldName;
1257435a35e8SMatthew G Knepley 
1258435a35e8SMatthew G Knepley         ierr = DMCreateSubDM(dm, 1, &f, &(*islist)[f], &(*dmlist)[f]);CHKERRQ(ierr);
1259435a35e8SMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1260435a35e8SMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*namelist)[f]);CHKERRQ(ierr);
1261435a35e8SMatthew G Knepley       }
1262435a35e8SMatthew G Knepley     } else {
126369ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1264e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
1265e7c4fc90SDmitry Karpeev       if (dmlist) *dmlist = PETSC_NULL;
1266e7c4fc90SDmitry Karpeev     }
1267435a35e8SMatthew G Knepley   }
1268e7c4fc90SDmitry Karpeev   else {
126916621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist); CHKERRQ(ierr);
127016621825SDmitry Karpeev   }
127116621825SDmitry Karpeev   PetscFunctionReturn(0);
127216621825SDmitry Karpeev }
127316621825SDmitry Karpeev 
127416621825SDmitry Karpeev #undef __FUNCT__
1275435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1276435a35e8SMatthew G Knepley /*@C
1277435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1278435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1279435a35e8SMatthew G Knepley 
1280435a35e8SMatthew G Knepley   Not collective
1281435a35e8SMatthew G Knepley 
1282435a35e8SMatthew G Knepley   Input Parameters:
1283435a35e8SMatthew G Knepley + dm - the DM object
1284435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
1285435a35e8SMatthew G Knepley - len       - The number of subproblems in the decomposition (or PETSC_NULL if not requested)
1286435a35e8SMatthew G Knepley 
1287435a35e8SMatthew G Knepley   Output Parameters:
1288435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1289435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1290435a35e8SMatthew G Knepley 
1291435a35e8SMatthew G Knepley   Level: intermediate
1292435a35e8SMatthew G Knepley 
1293435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1294435a35e8SMatthew G Knepley @*/
1295435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1296435a35e8SMatthew G Knepley {
1297435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1298435a35e8SMatthew G Knepley 
1299435a35e8SMatthew G Knepley   PetscFunctionBegin;
1300435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1301435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
1302435a35e8SMatthew G Knepley   if (is) {PetscValidPointer(is,4);}
1303435a35e8SMatthew G Knepley   if (subdm) {PetscValidPointer(subdm,5);}
1304435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1305435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm); CHKERRQ(ierr);
1306435a35e8SMatthew G Knepley   } else SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1307435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1308435a35e8SMatthew G Knepley }
1309435a35e8SMatthew G Knepley 
1310435a35e8SMatthew G Knepley #undef __FUNCT__
131116621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecompositionDM"
131216621825SDmitry Karpeev /*@C
131316621825SDmitry Karpeev   DMCreateDomainDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into subdomains.
131416621825SDmitry Karpeev 
131516621825SDmitry Karpeev   Not Collective
131616621825SDmitry Karpeev 
131716621825SDmitry Karpeev   Input Parameters:
131816621825SDmitry Karpeev + dm   - the DM object
131916621825SDmitry Karpeev - name - the name of the subdomain decomposition
132016621825SDmitry Karpeev 
132116621825SDmitry Karpeev   Output Parameter:
132216621825SDmitry Karpeev . ddm  - the subdomain decomposition DM (PETSC_NULL, if no such decomposition is known)
132316621825SDmitry Karpeev 
132416621825SDmitry Karpeev   Level: advanced
132516621825SDmitry Karpeev 
132616621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
132716621825SDmitry Karpeev @*/
132816621825SDmitry Karpeev PetscErrorCode DMCreateDomainDecompositionDM(DM dm, const char* name, DM *ddm)
132916621825SDmitry Karpeev {
133016621825SDmitry Karpeev   PetscErrorCode ierr;
133116621825SDmitry Karpeev 
133216621825SDmitry Karpeev   PetscFunctionBegin;
133316621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
133416621825SDmitry Karpeev   PetscValidCharPointer(name,2);
133516621825SDmitry Karpeev   PetscValidPointer(ddm,3);
133616621825SDmitry Karpeev   *ddm = PETSC_NULL;
1337731c8d9eSDmitry Karpeev   if (dm->ops->createdomaindecompositiondm) {
133816621825SDmitry Karpeev     ierr = (*dm->ops->createdomaindecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
133916621825SDmitry Karpeev   }
134016621825SDmitry Karpeev   PetscFunctionReturn(0);
134116621825SDmitry Karpeev }
134216621825SDmitry Karpeev 
134316621825SDmitry Karpeev #undef __FUNCT__
134416621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
134516621825SDmitry Karpeev /*@C
13468d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
13478d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
13488d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
13498d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
13508d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
135116621825SDmitry Karpeev 
135216621825SDmitry Karpeev   Not collective
135316621825SDmitry Karpeev 
135416621825SDmitry Karpeev   Input Parameter:
135516621825SDmitry Karpeev . dm - the DM object
135616621825SDmitry Karpeev 
135716621825SDmitry Karpeev   Output Parameters:
135816621825SDmitry Karpeev + len         - The number of subproblems in the domain decomposition (or PETSC_NULL if not requested)
135916621825SDmitry Karpeev . namelist    - The name for each subdomain (or PETSC_NULL if not requested)
13608d4ac253SDmitry Karpeev . innerislist - The global indices for each inner subdomain (or PETSC_NULL, if not requested)
13618d4ac253SDmitry Karpeev . outerislist - The global indices for each outer subdomain (or PETSC_NULL, if not requested)
136216621825SDmitry Karpeev - dmlist      - The DMs for each subdomain subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
136316621825SDmitry Karpeev 
136416621825SDmitry Karpeev   Level: intermediate
136516621825SDmitry Karpeev 
136616621825SDmitry Karpeev   Notes:
136716621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
136816621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
136916621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
137016621825SDmitry Karpeev 
13718d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
137216621825SDmitry Karpeev @*/
13738d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
137416621825SDmitry Karpeev {
137516621825SDmitry Karpeev   PetscErrorCode ierr;
137616621825SDmitry Karpeev 
137716621825SDmitry Karpeev   PetscFunctionBegin;
137816621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1379731c8d9eSDmitry Karpeev   if (len)           {PetscValidPointer(len,2);            *len         = PETSC_NULL;}
138016621825SDmitry Karpeev   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = PETSC_NULL;}
13818d4ac253SDmitry Karpeev   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = PETSC_NULL;}
13828d4ac253SDmitry Karpeev   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = PETSC_NULL;}
13838d4ac253SDmitry Karpeev   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = PETSC_NULL;}
138416621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
13858d4ac253SDmitry Karpeev     ierr = (*dm->ops->createdomaindecomposition)(dm,len,namelist,innerislist,outerislist,dmlist); CHKERRQ(ierr);
1386e7c4fc90SDmitry Karpeev   }
1387e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1388e7c4fc90SDmitry Karpeev }
1389e7c4fc90SDmitry Karpeev 
1390731c8d9eSDmitry Karpeev #undef __FUNCT__
139147c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
139247c6ae99SBarry Smith /*@
139347c6ae99SBarry Smith   DMRefine - Refines a DM object
139447c6ae99SBarry Smith 
139547c6ae99SBarry Smith   Collective on DM
139647c6ae99SBarry Smith 
139747c6ae99SBarry Smith   Input Parameter:
139847c6ae99SBarry Smith + dm   - the DM object
139991d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
140047c6ae99SBarry Smith 
140147c6ae99SBarry Smith   Output Parameter:
1402ae0a1c52SMatthew G Knepley . dmf - the refined DM, or PETSC_NULL
1403ae0a1c52SMatthew G Knepley 
1404ae0a1c52SMatthew G Knepley   Note: If no refinement was done, the return value is PETSC_NULL
140547c6ae99SBarry Smith 
140647c6ae99SBarry Smith   Level: developer
140747c6ae99SBarry Smith 
1408e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
140947c6ae99SBarry Smith @*/
14107087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
141147c6ae99SBarry Smith {
141247c6ae99SBarry Smith   PetscErrorCode ierr;
1413c833c3b5SJed Brown   DMRefineHookLink link;
141447c6ae99SBarry Smith 
141547c6ae99SBarry Smith   PetscFunctionBegin;
1416732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
141747c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
14184057135bSMatthew G Knepley   if (*dmf) {
141943842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
1420644e2e5bSBarry Smith     (*dmf)->ops->initialguess = dm->ops->initialguess;
1421644e2e5bSBarry Smith     (*dmf)->ops->function     = dm->ops->function;
1422644e2e5bSBarry Smith     (*dmf)->ops->functionj    = dm->ops->functionj;
1423644e2e5bSBarry Smith     if (dm->ops->jacobian != DMComputeJacobianDefault) {
1424644e2e5bSBarry Smith       (*dmf)->ops->jacobian     = dm->ops->jacobian;
1425644e2e5bSBarry Smith     }
14268cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
1427644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
14280598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1429656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
1430e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1431c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
1432c833c3b5SJed Brown       if (link->refinehook) {ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);}
1433c833c3b5SJed Brown     }
1434c833c3b5SJed Brown   }
1435c833c3b5SJed Brown   PetscFunctionReturn(0);
1436c833c3b5SJed Brown }
1437c833c3b5SJed Brown 
1438c833c3b5SJed Brown #undef __FUNCT__
1439c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1440c833c3b5SJed Brown /*@
1441c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1442c833c3b5SJed Brown 
1443c833c3b5SJed Brown    Logically Collective
1444c833c3b5SJed Brown 
1445c833c3b5SJed Brown    Input Arguments:
1446c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1447c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1448c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
1449c833c3b5SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1450c833c3b5SJed Brown 
1451c833c3b5SJed Brown    Calling sequence of refinehook:
1452c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1453c833c3b5SJed Brown 
1454c833c3b5SJed Brown +  coarse - coarse level DM
1455c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1456c833c3b5SJed Brown -  ctx - optional user-defined function context
1457c833c3b5SJed Brown 
1458c833c3b5SJed Brown    Calling sequence for interphook:
1459c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1460c833c3b5SJed Brown 
1461c833c3b5SJed Brown +  coarse - coarse level DM
1462c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1463c833c3b5SJed Brown .  fine - fine level DM to update
1464c833c3b5SJed Brown -  ctx - optional user-defined function context
1465c833c3b5SJed Brown 
1466c833c3b5SJed Brown    Level: advanced
1467c833c3b5SJed Brown 
1468c833c3b5SJed Brown    Notes:
1469c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1470c833c3b5SJed Brown 
1471c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1472c833c3b5SJed Brown 
1473c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1474c833c3b5SJed Brown @*/
1475c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1476c833c3b5SJed Brown {
1477c833c3b5SJed Brown   PetscErrorCode ierr;
1478c833c3b5SJed Brown   DMRefineHookLink link,*p;
1479c833c3b5SJed Brown 
1480c833c3b5SJed Brown   PetscFunctionBegin;
1481c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1482c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1483c833c3b5SJed Brown   ierr = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1484c833c3b5SJed Brown   link->refinehook = refinehook;
1485c833c3b5SJed Brown   link->interphook = interphook;
1486c833c3b5SJed Brown   link->ctx = ctx;
1487c833c3b5SJed Brown   link->next = PETSC_NULL;
1488c833c3b5SJed Brown   *p = link;
1489c833c3b5SJed Brown   PetscFunctionReturn(0);
1490c833c3b5SJed Brown }
1491c833c3b5SJed Brown 
1492c833c3b5SJed Brown #undef __FUNCT__
1493c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1494c833c3b5SJed Brown /*@
1495c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1496c833c3b5SJed Brown 
1497c833c3b5SJed Brown    Collective if any hooks are
1498c833c3b5SJed Brown 
1499c833c3b5SJed Brown    Input Arguments:
1500c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1501c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1502c833c3b5SJed Brown -  fine - finer DM to update
1503c833c3b5SJed Brown 
1504c833c3b5SJed Brown    Level: developer
1505c833c3b5SJed Brown 
1506c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1507c833c3b5SJed Brown @*/
1508c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1509c833c3b5SJed Brown {
1510c833c3b5SJed Brown   PetscErrorCode ierr;
1511c833c3b5SJed Brown   DMRefineHookLink link;
1512c833c3b5SJed Brown 
1513c833c3b5SJed Brown   PetscFunctionBegin;
1514c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
1515c833c3b5SJed Brown     if (link->interphook) {ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);}
15164057135bSMatthew G Knepley   }
151747c6ae99SBarry Smith   PetscFunctionReturn(0);
151847c6ae99SBarry Smith }
151947c6ae99SBarry Smith 
152047c6ae99SBarry Smith #undef __FUNCT__
1521eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1522eb3f98d2SBarry Smith /*@
1523eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1524eb3f98d2SBarry Smith 
1525eb3f98d2SBarry Smith     Not Collective
1526eb3f98d2SBarry Smith 
1527eb3f98d2SBarry Smith     Input Parameter:
1528eb3f98d2SBarry Smith .   dm - the DM object
1529eb3f98d2SBarry Smith 
1530eb3f98d2SBarry Smith     Output Parameter:
1531eb3f98d2SBarry Smith .   level - number of refinements
1532eb3f98d2SBarry Smith 
1533eb3f98d2SBarry Smith     Level: developer
1534eb3f98d2SBarry Smith 
15356a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1536eb3f98d2SBarry Smith 
1537eb3f98d2SBarry Smith @*/
1538eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1539eb3f98d2SBarry Smith {
1540eb3f98d2SBarry Smith   PetscFunctionBegin;
1541eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1542eb3f98d2SBarry Smith   *level = dm->levelup;
1543eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1544eb3f98d2SBarry Smith }
1545eb3f98d2SBarry Smith 
1546eb3f98d2SBarry Smith #undef __FUNCT__
154747c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
154847c6ae99SBarry Smith /*@
154947c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
155047c6ae99SBarry Smith 
155147c6ae99SBarry Smith     Neighbor-wise Collective on DM
155247c6ae99SBarry Smith 
155347c6ae99SBarry Smith     Input Parameters:
155447c6ae99SBarry Smith +   dm - the DM object
155547c6ae99SBarry Smith .   g - the global vector
155647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
155747c6ae99SBarry Smith -   l - the local vector
155847c6ae99SBarry Smith 
155947c6ae99SBarry Smith 
156047c6ae99SBarry Smith     Level: beginner
156147c6ae99SBarry Smith 
1562e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
156347c6ae99SBarry Smith 
156447c6ae99SBarry Smith @*/
15657087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
156647c6ae99SBarry Smith {
15677128ae9fSMatthew G Knepley   PetscSF        sf;
156847c6ae99SBarry Smith   PetscErrorCode ierr;
156947c6ae99SBarry Smith 
157047c6ae99SBarry Smith   PetscFunctionBegin;
1571171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15727128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
15737128ae9fSMatthew G Knepley   if (sf) {
15747128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
15757128ae9fSMatthew G Knepley 
15767128ae9fSMatthew G Knepley     if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
15777128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
15787128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
15797128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
15807128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
15817128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
15827128ae9fSMatthew G Knepley   } else {
1583843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
15847128ae9fSMatthew G Knepley   }
158547c6ae99SBarry Smith   PetscFunctionReturn(0);
158647c6ae99SBarry Smith }
158747c6ae99SBarry Smith 
158847c6ae99SBarry Smith #undef __FUNCT__
158947c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
159047c6ae99SBarry Smith /*@
159147c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
159247c6ae99SBarry Smith 
159347c6ae99SBarry Smith     Neighbor-wise Collective on DM
159447c6ae99SBarry Smith 
159547c6ae99SBarry Smith     Input Parameters:
159647c6ae99SBarry Smith +   dm - the DM object
159747c6ae99SBarry Smith .   g - the global vector
159847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
159947c6ae99SBarry Smith -   l - the local vector
160047c6ae99SBarry Smith 
160147c6ae99SBarry Smith 
160247c6ae99SBarry Smith     Level: beginner
160347c6ae99SBarry Smith 
1604e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
160547c6ae99SBarry Smith 
160647c6ae99SBarry Smith @*/
16077087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
160847c6ae99SBarry Smith {
16097128ae9fSMatthew G Knepley   PetscSF        sf;
161047c6ae99SBarry Smith   PetscErrorCode ierr;
161161a3c1faSSatish Balay   PetscScalar    *lArray, *gArray;
161247c6ae99SBarry Smith 
161347c6ae99SBarry Smith   PetscFunctionBegin;
1614171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16157128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16167128ae9fSMatthew G Knepley   if (sf) {
16177128ae9fSMatthew G Knepley   if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16187128ae9fSMatthew G Knepley 
16197128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16207128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16217128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
16227128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16237128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16247128ae9fSMatthew G Knepley   } else {
1625843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
16267128ae9fSMatthew G Knepley   }
162747c6ae99SBarry Smith   PetscFunctionReturn(0);
162847c6ae99SBarry Smith }
162947c6ae99SBarry Smith 
163047c6ae99SBarry Smith #undef __FUNCT__
16319a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
163247c6ae99SBarry Smith /*@
16339a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
16349a42bb27SBarry Smith 
16359a42bb27SBarry Smith     Neighbor-wise Collective on DM
16369a42bb27SBarry Smith 
16379a42bb27SBarry Smith     Input Parameters:
16389a42bb27SBarry Smith +   dm - the DM object
1639f6813fd5SJed Brown .   l - the local vector
16409a42bb27SBarry Smith .   mode - if INSERT_VALUES then no parallel communication is used, if ADD_VALUES then all ghost points from the same base point accumulate into that
16419a42bb27SBarry Smith            base point.
1642f6813fd5SJed Brown - - the global vector
16439a42bb27SBarry Smith 
16449a42bb27SBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation. If you would like to simply add the non-ghosted values in the local
16459a42bb27SBarry Smith            array into the global array you need to either (1) zero the ghosted locations and use ADD_VALUES or (2) use INSERT_VALUES into a work global array and then add the work
16469a42bb27SBarry Smith            global array to the final global array with VecAXPY().
16479a42bb27SBarry Smith 
16489a42bb27SBarry Smith     Level: beginner
16499a42bb27SBarry Smith 
1650e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
16519a42bb27SBarry Smith 
16529a42bb27SBarry Smith @*/
16537087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
16549a42bb27SBarry Smith {
16557128ae9fSMatthew G Knepley   PetscSF        sf;
16569a42bb27SBarry Smith   PetscErrorCode ierr;
16579a42bb27SBarry Smith 
16589a42bb27SBarry Smith   PetscFunctionBegin;
1659171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16607128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16617128ae9fSMatthew G Knepley   if (sf) {
16627128ae9fSMatthew G Knepley     MPI_Op       op;
16637128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
16647128ae9fSMatthew G Knepley 
16657128ae9fSMatthew G Knepley     switch(mode) {
16667128ae9fSMatthew G Knepley     case INSERT_VALUES:
16677128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
16687128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
16697128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
16707128ae9fSMatthew G Knepley #else
16717128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
16727128ae9fSMatthew G Knepley #endif
16737128ae9fSMatthew G Knepley     case ADD_VALUES:
16747128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
16757128ae9fSMatthew G Knepley       op = MPI_SUM; break;
16767128ae9fSMatthew G Knepley   default:
16777128ae9fSMatthew G Knepley     SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16787128ae9fSMatthew G Knepley     }
16797128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16807128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16817128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
16827128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16837128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16847128ae9fSMatthew G Knepley   } else {
1685843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
16867128ae9fSMatthew G Knepley   }
16879a42bb27SBarry Smith   PetscFunctionReturn(0);
16889a42bb27SBarry Smith }
16899a42bb27SBarry Smith 
16909a42bb27SBarry Smith #undef __FUNCT__
16919a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
16929a42bb27SBarry Smith /*@
16939a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
169447c6ae99SBarry Smith 
169547c6ae99SBarry Smith     Neighbor-wise Collective on DM
169647c6ae99SBarry Smith 
169747c6ae99SBarry Smith     Input Parameters:
169847c6ae99SBarry Smith +   dm - the DM object
1699f6813fd5SJed Brown .   l - the local vector
170047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1701f6813fd5SJed Brown -   g - the global vector
170247c6ae99SBarry Smith 
170347c6ae99SBarry Smith 
170447c6ae99SBarry Smith     Level: beginner
170547c6ae99SBarry Smith 
1706e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
170747c6ae99SBarry Smith 
170847c6ae99SBarry Smith @*/
17097087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
171047c6ae99SBarry Smith {
17117128ae9fSMatthew G Knepley   PetscSF        sf;
171247c6ae99SBarry Smith   PetscErrorCode ierr;
171347c6ae99SBarry Smith 
171447c6ae99SBarry Smith   PetscFunctionBegin;
1715171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17167128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17177128ae9fSMatthew G Knepley   if (sf) {
17187128ae9fSMatthew G Knepley     MPI_Op       op;
17197128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
17207128ae9fSMatthew G Knepley 
17217128ae9fSMatthew G Knepley     switch(mode) {
17227128ae9fSMatthew G Knepley     case INSERT_VALUES:
17237128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
17247128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
17257128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
17267128ae9fSMatthew G Knepley #else
17277128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
17287128ae9fSMatthew G Knepley #endif
17297128ae9fSMatthew G Knepley     case ADD_VALUES:
17307128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
17317128ae9fSMatthew G Knepley       op = MPI_SUM; break;
17327128ae9fSMatthew G Knepley     default:
17337128ae9fSMatthew G Knepley       SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17347128ae9fSMatthew G Knepley     }
17357128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17367128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17377128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
17387128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17397128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17407128ae9fSMatthew G Knepley   } else {
1741843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
17427128ae9fSMatthew G Knepley   }
174347c6ae99SBarry Smith   PetscFunctionReturn(0);
174447c6ae99SBarry Smith }
174547c6ae99SBarry Smith 
174647c6ae99SBarry Smith #undef __FUNCT__
174747c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
174847c6ae99SBarry Smith /*@
174947c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
175047c6ae99SBarry Smith 
175147c6ae99SBarry Smith     Collective on DM
175247c6ae99SBarry Smith 
175347c6ae99SBarry Smith     Input Parameter:
175447c6ae99SBarry Smith +   dm - the DM object
175547c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
175647c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
175747c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
175847c6ae99SBarry Smith 
175947c6ae99SBarry Smith     Level: developer
176047c6ae99SBarry Smith 
1761*835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
176247c6ae99SBarry Smith          DMSetFunction()
176347c6ae99SBarry Smith 
176447c6ae99SBarry Smith @*/
17657087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
176647c6ae99SBarry Smith {
176747c6ae99SBarry Smith   PetscErrorCode ierr;
1768171400e9SBarry Smith 
176947c6ae99SBarry Smith   PetscFunctionBegin;
1770171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
177147c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
177247c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
177347c6ae99SBarry Smith   if (A != B) {
177447c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
177547c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
177647c6ae99SBarry Smith   }
177747c6ae99SBarry Smith   PetscFunctionReturn(0);
177847c6ae99SBarry Smith }
177947c6ae99SBarry Smith 
178047c6ae99SBarry Smith #undef __FUNCT__
178147c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
178247c6ae99SBarry Smith /*@
178347c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
178447c6ae99SBarry Smith 
178547c6ae99SBarry Smith     Collective on DM
178647c6ae99SBarry Smith 
178747c6ae99SBarry Smith     Input Parameter:
178847c6ae99SBarry Smith +   dm - the DM object
178991d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
179047c6ae99SBarry Smith 
179147c6ae99SBarry Smith     Output Parameter:
179247c6ae99SBarry Smith .   dmc - the coarsened DM
179347c6ae99SBarry Smith 
179447c6ae99SBarry Smith     Level: developer
179547c6ae99SBarry Smith 
1796e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
179747c6ae99SBarry Smith 
179847c6ae99SBarry Smith @*/
17997087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
180047c6ae99SBarry Smith {
180147c6ae99SBarry Smith   PetscErrorCode ierr;
1802b17ce1afSJed Brown   DMCoarsenHookLink link;
180347c6ae99SBarry Smith 
180447c6ae99SBarry Smith   PetscFunctionBegin;
1805171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
180647c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
180743842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
180847c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
180947c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
181047c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
181147c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
181247c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
181347c6ae99SBarry Smith   }
18148cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1815644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
18160598a293SJed Brown   (*dmc)->levelup   = dm->levelup;
1817656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
1818e4b4b23bSJed Brown   ierr = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
1819b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1820b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1821b17ce1afSJed Brown   }
1822b17ce1afSJed Brown   PetscFunctionReturn(0);
1823b17ce1afSJed Brown }
1824b17ce1afSJed Brown 
1825b17ce1afSJed Brown #undef __FUNCT__
1826b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1827b17ce1afSJed Brown /*@
1828b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1829b17ce1afSJed Brown 
1830b17ce1afSJed Brown    Logically Collective
1831b17ce1afSJed Brown 
1832b17ce1afSJed Brown    Input Arguments:
1833b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1834b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1835b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
1836b17ce1afSJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1837b17ce1afSJed Brown 
1838b17ce1afSJed Brown    Calling sequence of coarsenhook:
1839b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1840b17ce1afSJed Brown 
1841b17ce1afSJed Brown +  fine - fine level DM
1842b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1843b17ce1afSJed Brown -  ctx - optional user-defined function context
1844b17ce1afSJed Brown 
1845b17ce1afSJed Brown    Calling sequence for restricthook:
1846c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1847b17ce1afSJed Brown 
1848b17ce1afSJed Brown +  fine - fine level DM
1849b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1850c833c3b5SJed Brown .  rscale - scaling vector for restriction
1851c833c3b5SJed Brown .  inject - matrix restricting by injection
1852b17ce1afSJed Brown .  coarse - coarse level DM to update
1853b17ce1afSJed Brown -  ctx - optional user-defined function context
1854b17ce1afSJed Brown 
1855b17ce1afSJed Brown    Level: advanced
1856b17ce1afSJed Brown 
1857b17ce1afSJed Brown    Notes:
1858b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1859b17ce1afSJed Brown 
1860b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1861b17ce1afSJed Brown 
1862b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1863b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1864b17ce1afSJed Brown 
1865c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1866b17ce1afSJed Brown @*/
1867b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1868b17ce1afSJed Brown {
1869b17ce1afSJed Brown   PetscErrorCode ierr;
1870b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1871b17ce1afSJed Brown 
1872b17ce1afSJed Brown   PetscFunctionBegin;
1873b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
18746bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1875b17ce1afSJed Brown   ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1876b17ce1afSJed Brown   link->coarsenhook = coarsenhook;
1877b17ce1afSJed Brown   link->restricthook = restricthook;
1878b17ce1afSJed Brown   link->ctx = ctx;
18796cab3a1bSJed Brown   link->next = PETSC_NULL;
1880b17ce1afSJed Brown   *p = link;
1881b17ce1afSJed Brown   PetscFunctionReturn(0);
1882b17ce1afSJed Brown }
1883b17ce1afSJed Brown 
1884b17ce1afSJed Brown #undef __FUNCT__
1885b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1886b17ce1afSJed Brown /*@
1887b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1888b17ce1afSJed Brown 
1889b17ce1afSJed Brown    Collective if any hooks are
1890b17ce1afSJed Brown 
1891b17ce1afSJed Brown    Input Arguments:
1892b17ce1afSJed Brown +  fine - finer DM to use as a base
1893b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1894b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1895b17ce1afSJed Brown -  coarse - coarer DM to update
1896b17ce1afSJed Brown 
1897b17ce1afSJed Brown    Level: developer
1898b17ce1afSJed Brown 
1899b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1900b17ce1afSJed Brown @*/
1901b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1902b17ce1afSJed Brown {
1903b17ce1afSJed Brown   PetscErrorCode ierr;
1904b17ce1afSJed Brown   DMCoarsenHookLink link;
1905b17ce1afSJed Brown 
1906b17ce1afSJed Brown   PetscFunctionBegin;
1907b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
1908b17ce1afSJed Brown     if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);}
1909b17ce1afSJed Brown   }
191047c6ae99SBarry Smith   PetscFunctionReturn(0);
191147c6ae99SBarry Smith }
191247c6ae99SBarry Smith 
191347c6ae99SBarry Smith #undef __FUNCT__
19145dbd56e3SPeter Brune #define __FUNCT__ "DMBlockRestrictHookAdd"
19155dbd56e3SPeter Brune /*@
19165dbd56e3SPeter Brune    DMBlockRestrictHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
19175dbd56e3SPeter Brune 
19185dbd56e3SPeter Brune    Logically Collective
19195dbd56e3SPeter Brune 
19205dbd56e3SPeter Brune    Input Arguments:
19215dbd56e3SPeter Brune +  global - global DM
19225dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
19235dbd56e3SPeter Brune -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
19245dbd56e3SPeter Brune 
19255dbd56e3SPeter Brune    Calling sequence for restricthook:
19265dbd56e3SPeter Brune $    restricthook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
19275dbd56e3SPeter Brune 
19285dbd56e3SPeter Brune +  global - global DM
19295dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
19305dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
19315dbd56e3SPeter Brune .  block  - block DM (may just be a shell if the global DM is passed in correctly)
19325dbd56e3SPeter Brune -  ctx - optional user-defined function context
19335dbd56e3SPeter Brune 
19345dbd56e3SPeter Brune    Level: advanced
19355dbd56e3SPeter Brune 
19365dbd56e3SPeter Brune    Notes:
19375dbd56e3SPeter Brune    This function is only needed if auxiliary data needs to be set up on coarse grids.
19385dbd56e3SPeter Brune 
19395dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
19405dbd56e3SPeter Brune 
19415dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
19425dbd56e3SPeter Brune    extract the finest level information from its context (instead of from the SNES).
19435dbd56e3SPeter Brune 
19445dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19455dbd56e3SPeter Brune @*/
19465dbd56e3SPeter Brune PetscErrorCode DMBlockRestrictHookAdd(DM global,PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
19475dbd56e3SPeter Brune {
19485dbd56e3SPeter Brune   PetscErrorCode ierr;
19495dbd56e3SPeter Brune   DMBlockRestrictHookLink link,*p;
19505dbd56e3SPeter Brune 
19515dbd56e3SPeter Brune   PetscFunctionBegin;
19525dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
19535dbd56e3SPeter Brune   for (p=&global->blockrestricthook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
19545dbd56e3SPeter Brune   ierr = PetscMalloc(sizeof(struct _DMBlockRestrictHookLink),&link);CHKERRQ(ierr);
19555dbd56e3SPeter Brune   link->restricthook = restricthook;
19565dbd56e3SPeter Brune   link->ctx = ctx;
19575dbd56e3SPeter Brune   link->next = PETSC_NULL;
19585dbd56e3SPeter Brune   *p = link;
19595dbd56e3SPeter Brune   PetscFunctionReturn(0);
19605dbd56e3SPeter Brune }
19615dbd56e3SPeter Brune 
19625dbd56e3SPeter Brune #undef __FUNCT__
19635dbd56e3SPeter Brune #define __FUNCT__ "DMBlockRestrict"
19645dbd56e3SPeter Brune /*@
19655dbd56e3SPeter Brune    DMBlockRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMBlockRestrictHookAdd()
19665dbd56e3SPeter Brune 
19675dbd56e3SPeter Brune    Collective if any hooks are
19685dbd56e3SPeter Brune 
19695dbd56e3SPeter Brune    Input Arguments:
19705dbd56e3SPeter Brune +  fine - finer DM to use as a base
19715dbd56e3SPeter Brune .  restrct - restriction matrix, apply using MatRestrict()
19725dbd56e3SPeter Brune .  inject - injection matrix, also use MatRestrict()
19735dbd56e3SPeter Brune -  coarse - coarer DM to update
19745dbd56e3SPeter Brune 
19755dbd56e3SPeter Brune    Level: developer
19765dbd56e3SPeter Brune 
19775dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
19785dbd56e3SPeter Brune @*/
19795dbd56e3SPeter Brune PetscErrorCode DMBlockRestrict(DM global,VecScatter in,VecScatter out,DM block)
19805dbd56e3SPeter Brune {
19815dbd56e3SPeter Brune   PetscErrorCode ierr;
19825dbd56e3SPeter Brune   DMBlockRestrictHookLink link;
19835dbd56e3SPeter Brune 
19845dbd56e3SPeter Brune   PetscFunctionBegin;
19855dbd56e3SPeter Brune   for (link=global->blockrestricthook; link; link=link->next) {
19865dbd56e3SPeter Brune     if (link->restricthook) {ierr = (*link->restricthook)(global,in,out,block,link->ctx);CHKERRQ(ierr);}
19875dbd56e3SPeter Brune   }
19885dbd56e3SPeter Brune   PetscFunctionReturn(0);
19895dbd56e3SPeter Brune }
19905dbd56e3SPeter Brune 
19915dbd56e3SPeter Brune #undef __FUNCT__
19925fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
19935fe1f584SPeter Brune /*@
19946a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
19955fe1f584SPeter Brune 
19965fe1f584SPeter Brune     Not Collective
19975fe1f584SPeter Brune 
19985fe1f584SPeter Brune     Input Parameter:
19995fe1f584SPeter Brune .   dm - the DM object
20005fe1f584SPeter Brune 
20015fe1f584SPeter Brune     Output Parameter:
20026a7d9d85SPeter Brune .   level - number of coarsenings
20035fe1f584SPeter Brune 
20045fe1f584SPeter Brune     Level: developer
20055fe1f584SPeter Brune 
20066a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
20075fe1f584SPeter Brune 
20085fe1f584SPeter Brune @*/
20095fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
20105fe1f584SPeter Brune {
20115fe1f584SPeter Brune   PetscFunctionBegin;
20125fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20135fe1f584SPeter Brune   *level = dm->leveldown;
20145fe1f584SPeter Brune   PetscFunctionReturn(0);
20155fe1f584SPeter Brune }
20165fe1f584SPeter Brune 
20175fe1f584SPeter Brune 
20185fe1f584SPeter Brune 
20195fe1f584SPeter Brune #undef __FUNCT__
202047c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
202147c6ae99SBarry Smith /*@C
202247c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
202347c6ae99SBarry Smith 
202447c6ae99SBarry Smith     Collective on DM
202547c6ae99SBarry Smith 
202647c6ae99SBarry Smith     Input Parameter:
202747c6ae99SBarry Smith +   dm - the DM object
202847c6ae99SBarry Smith -   nlevels - the number of levels of refinement
202947c6ae99SBarry Smith 
203047c6ae99SBarry Smith     Output Parameter:
203147c6ae99SBarry Smith .   dmf - the refined DM hierarchy
203247c6ae99SBarry Smith 
203347c6ae99SBarry Smith     Level: developer
203447c6ae99SBarry Smith 
2035e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
203647c6ae99SBarry Smith 
203747c6ae99SBarry Smith @*/
20387087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
203947c6ae99SBarry Smith {
204047c6ae99SBarry Smith   PetscErrorCode ierr;
204147c6ae99SBarry Smith 
204247c6ae99SBarry Smith   PetscFunctionBegin;
2043171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
204447c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
204547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
204647c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
204747c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
204847c6ae99SBarry Smith   } else if (dm->ops->refine) {
204947c6ae99SBarry Smith     PetscInt i;
205047c6ae99SBarry Smith 
205147c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
205247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
205347c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
205447c6ae99SBarry Smith     }
205547c6ae99SBarry Smith   } else {
205647c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
205747c6ae99SBarry Smith   }
205847c6ae99SBarry Smith   PetscFunctionReturn(0);
205947c6ae99SBarry Smith }
206047c6ae99SBarry Smith 
206147c6ae99SBarry Smith #undef __FUNCT__
206247c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
206347c6ae99SBarry Smith /*@C
206447c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
206547c6ae99SBarry Smith 
206647c6ae99SBarry Smith     Collective on DM
206747c6ae99SBarry Smith 
206847c6ae99SBarry Smith     Input Parameter:
206947c6ae99SBarry Smith +   dm - the DM object
207047c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
207147c6ae99SBarry Smith 
207247c6ae99SBarry Smith     Output Parameter:
207347c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
207447c6ae99SBarry Smith 
207547c6ae99SBarry Smith     Level: developer
207647c6ae99SBarry Smith 
2077e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
207847c6ae99SBarry Smith 
207947c6ae99SBarry Smith @*/
20807087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
208147c6ae99SBarry Smith {
208247c6ae99SBarry Smith   PetscErrorCode ierr;
208347c6ae99SBarry Smith 
208447c6ae99SBarry Smith   PetscFunctionBegin;
2085171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
208647c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
208747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
208847c6ae99SBarry Smith   PetscValidPointer(dmc,3);
208947c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
209047c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
209147c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
209247c6ae99SBarry Smith     PetscInt i;
209347c6ae99SBarry Smith 
209447c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
209547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
209647c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
209747c6ae99SBarry Smith     }
209847c6ae99SBarry Smith   } else {
209947c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
210047c6ae99SBarry Smith   }
210147c6ae99SBarry Smith   PetscFunctionReturn(0);
210247c6ae99SBarry Smith }
210347c6ae99SBarry Smith 
210447c6ae99SBarry Smith #undef __FUNCT__
2105e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
210647c6ae99SBarry Smith /*@
2107e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
210847c6ae99SBarry Smith    grids associated with two DMs.
210947c6ae99SBarry Smith 
211047c6ae99SBarry Smith    Collective on DM
211147c6ae99SBarry Smith 
211247c6ae99SBarry Smith    Input Parameters:
211347c6ae99SBarry Smith +  dmc - the coarse grid DM
211447c6ae99SBarry Smith -  dmf - the fine grid DM
211547c6ae99SBarry Smith 
211647c6ae99SBarry Smith    Output Parameters:
211747c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
211847c6ae99SBarry Smith 
211947c6ae99SBarry Smith    Level: intermediate
212047c6ae99SBarry Smith 
212147c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
212247c6ae99SBarry Smith 
2123e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
212447c6ae99SBarry Smith @*/
2125e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
212647c6ae99SBarry Smith {
212747c6ae99SBarry Smith   PetscErrorCode ierr;
212847c6ae99SBarry Smith 
212947c6ae99SBarry Smith   PetscFunctionBegin;
2130171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2131171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
213247c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
213347c6ae99SBarry Smith   PetscFunctionReturn(0);
213447c6ae99SBarry Smith }
213547c6ae99SBarry Smith 
213647c6ae99SBarry Smith #undef __FUNCT__
21371a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
21381a266240SBarry Smith /*@C
21391a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
21401a266240SBarry Smith 
21411a266240SBarry Smith     Not Collective
21421a266240SBarry Smith 
21431a266240SBarry Smith     Input Parameters:
21441a266240SBarry Smith +   dm - the DM object
21451a266240SBarry Smith -   destroy - the destroy function
21461a266240SBarry Smith 
21471a266240SBarry Smith     Level: intermediate
21481a266240SBarry Smith 
2149e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
21501a266240SBarry Smith 
2151f07f9ceaSJed Brown @*/
21521a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
21531a266240SBarry Smith {
21541a266240SBarry Smith   PetscFunctionBegin;
2155171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21561a266240SBarry Smith   dm->ctxdestroy = destroy;
21571a266240SBarry Smith   PetscFunctionReturn(0);
21581a266240SBarry Smith }
21591a266240SBarry Smith 
21601a266240SBarry Smith #undef __FUNCT__
21611b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2162b07ff414SBarry Smith /*@
21631b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
216447c6ae99SBarry Smith 
216547c6ae99SBarry Smith     Not Collective
216647c6ae99SBarry Smith 
216747c6ae99SBarry Smith     Input Parameters:
216847c6ae99SBarry Smith +   dm - the DM object
216947c6ae99SBarry Smith -   ctx - the user context
217047c6ae99SBarry Smith 
217147c6ae99SBarry Smith     Level: intermediate
217247c6ae99SBarry Smith 
2173e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
217447c6ae99SBarry Smith 
217547c6ae99SBarry Smith @*/
21761b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
217747c6ae99SBarry Smith {
217847c6ae99SBarry Smith   PetscFunctionBegin;
2179171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
218047c6ae99SBarry Smith   dm->ctx = ctx;
218147c6ae99SBarry Smith   PetscFunctionReturn(0);
218247c6ae99SBarry Smith }
218347c6ae99SBarry Smith 
218447c6ae99SBarry Smith #undef __FUNCT__
21851b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
218647c6ae99SBarry Smith /*@
21871b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
218847c6ae99SBarry Smith 
218947c6ae99SBarry Smith     Not Collective
219047c6ae99SBarry Smith 
219147c6ae99SBarry Smith     Input Parameter:
219247c6ae99SBarry Smith .   dm - the DM object
219347c6ae99SBarry Smith 
219447c6ae99SBarry Smith     Output Parameter:
219547c6ae99SBarry Smith .   ctx - the user context
219647c6ae99SBarry Smith 
219747c6ae99SBarry Smith     Level: intermediate
219847c6ae99SBarry Smith 
2199e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
220047c6ae99SBarry Smith 
220147c6ae99SBarry Smith @*/
22021b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
220347c6ae99SBarry Smith {
220447c6ae99SBarry Smith   PetscFunctionBegin;
2205171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22061b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
220747c6ae99SBarry Smith   PetscFunctionReturn(0);
220847c6ae99SBarry Smith }
220947c6ae99SBarry Smith 
221047c6ae99SBarry Smith #undef __FUNCT__
221147c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
22127e833e3aSBarry Smith /*@C
221347c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
221447c6ae99SBarry Smith 
221547c6ae99SBarry Smith     Logically Collective on DM
221647c6ae99SBarry Smith 
221747c6ae99SBarry Smith     Input Parameter:
221847c6ae99SBarry Smith +   dm - the DM object
221947c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
222047c6ae99SBarry Smith 
222147c6ae99SBarry Smith     Level: intermediate
222247c6ae99SBarry Smith 
222347c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
222447c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
222547c6ae99SBarry Smith 
2226*835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
222747c6ae99SBarry Smith          DMSetJacobian()
222847c6ae99SBarry Smith 
2229f07f9ceaSJed Brown @*/
22307087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
223147c6ae99SBarry Smith {
223247c6ae99SBarry Smith   PetscFunctionBegin;
2233171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
223447c6ae99SBarry Smith   dm->ops->function = f;
223547c6ae99SBarry Smith   if (f) {
223647c6ae99SBarry Smith     dm->ops->functionj = f;
223747c6ae99SBarry Smith   }
223847c6ae99SBarry Smith   PetscFunctionReturn(0);
223947c6ae99SBarry Smith }
224047c6ae99SBarry Smith 
224147c6ae99SBarry Smith #undef __FUNCT__
224247c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
22437e833e3aSBarry Smith /*@C
224447c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
224547c6ae99SBarry Smith 
224647c6ae99SBarry Smith     Logically Collective on DM
224747c6ae99SBarry Smith 
224847c6ae99SBarry Smith     Input Parameter:
224947c6ae99SBarry Smith +   dm - the DM object to destroy
225047c6ae99SBarry Smith -   f - the function to compute the matrix entries
225147c6ae99SBarry Smith 
225247c6ae99SBarry Smith     Level: intermediate
225347c6ae99SBarry Smith 
2254*835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
225547c6ae99SBarry Smith          DMSetFunction()
225647c6ae99SBarry Smith 
2257f07f9ceaSJed Brown @*/
22587087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
225947c6ae99SBarry Smith {
226047c6ae99SBarry Smith   PetscFunctionBegin;
2261171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
226247c6ae99SBarry Smith   dm->ops->jacobian = f;
226347c6ae99SBarry Smith   PetscFunctionReturn(0);
226447c6ae99SBarry Smith }
226547c6ae99SBarry Smith 
226647c6ae99SBarry Smith #undef __FUNCT__
226708da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
226808da532bSDmitry Karpeev /*@C
226908da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
227008da532bSDmitry Karpeev 
227108da532bSDmitry Karpeev     Logically Collective on DM
227208da532bSDmitry Karpeev 
227308da532bSDmitry Karpeev     Input Parameter:
227408da532bSDmitry Karpeev +   dm - the DM object
227508da532bSDmitry Karpeev -   f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set)
227608da532bSDmitry Karpeev 
227708da532bSDmitry Karpeev     Level: intermediate
227808da532bSDmitry Karpeev 
2279*835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
228008da532bSDmitry Karpeev          DMSetJacobian()
228108da532bSDmitry Karpeev 
228208da532bSDmitry Karpeev @*/
228308da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
228408da532bSDmitry Karpeev {
228508da532bSDmitry Karpeev   PetscFunctionBegin;
228608da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
228708da532bSDmitry Karpeev   PetscFunctionReturn(0);
228808da532bSDmitry Karpeev }
228908da532bSDmitry Karpeev 
229008da532bSDmitry Karpeev #undef __FUNCT__
229108da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
229208da532bSDmitry Karpeev /*@
229308da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
229408da532bSDmitry Karpeev 
229508da532bSDmitry Karpeev     Not Collective
229608da532bSDmitry Karpeev 
229708da532bSDmitry Karpeev     Input Parameter:
229808da532bSDmitry Karpeev .   dm - the DM object to destroy
229908da532bSDmitry Karpeev 
230008da532bSDmitry Karpeev     Output Parameter:
230108da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
230208da532bSDmitry Karpeev 
230308da532bSDmitry Karpeev     Level: developer
230408da532bSDmitry Karpeev 
2305e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
230608da532bSDmitry Karpeev 
230708da532bSDmitry Karpeev @*/
230808da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
230908da532bSDmitry Karpeev {
231008da532bSDmitry Karpeev   PetscFunctionBegin;
231108da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
231208da532bSDmitry Karpeev   PetscFunctionReturn(0);
231308da532bSDmitry Karpeev }
231408da532bSDmitry Karpeev 
231508da532bSDmitry Karpeev #undef __FUNCT__
231608da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
231708da532bSDmitry Karpeev /*@C
231808da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
231908da532bSDmitry Karpeev 
232008da532bSDmitry Karpeev     Logically Collective on DM
232108da532bSDmitry Karpeev 
232208da532bSDmitry Karpeev     Input Parameters:
232308da532bSDmitry Karpeev +   dm - the DM object to destroy
232408da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
232508da532bSDmitry Karpeev 
232608da532bSDmitry Karpeev     Output parameters:
232708da532bSDmitry Karpeev +   xl - lower bound
232808da532bSDmitry Karpeev -   xu - upper bound
232908da532bSDmitry Karpeev 
233008da532bSDmitry Karpeev     Level: intermediate
233108da532bSDmitry Karpeev 
2332*835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
233308da532bSDmitry Karpeev          DMSetFunction(), DMSetVariableBounds()
233408da532bSDmitry Karpeev 
233508da532bSDmitry Karpeev @*/
233608da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
233708da532bSDmitry Karpeev {
233808da532bSDmitry Karpeev   PetscErrorCode ierr;
233908da532bSDmitry Karpeev   PetscFunctionBegin;
234008da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
234108da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
234208da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
234308da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu); CHKERRQ(ierr);
234408da532bSDmitry Karpeev   }
2345a201590fSDmitry Karpeev   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
234608da532bSDmitry Karpeev   PetscFunctionReturn(0);
234708da532bSDmitry Karpeev }
234808da532bSDmitry Karpeev 
234908da532bSDmitry Karpeev #undef __FUNCT__
235047c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
235147c6ae99SBarry Smith /*@
235247c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
235347c6ae99SBarry Smith 
235447c6ae99SBarry Smith     Not Collective
235547c6ae99SBarry Smith 
235647c6ae99SBarry Smith     Input Parameter:
235747c6ae99SBarry Smith .   dm - the DM object to destroy
235847c6ae99SBarry Smith 
235947c6ae99SBarry Smith     Output Parameter:
236047c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
236147c6ae99SBarry Smith 
236247c6ae99SBarry Smith     Level: developer
236347c6ae99SBarry Smith 
2364e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
236547c6ae99SBarry Smith 
236647c6ae99SBarry Smith @*/
23677087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
236847c6ae99SBarry Smith {
236947c6ae99SBarry Smith   PetscFunctionBegin;
2370171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
237147c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
237247c6ae99SBarry Smith   PetscFunctionReturn(0);
237347c6ae99SBarry Smith }
237447c6ae99SBarry Smith 
237547c6ae99SBarry Smith #undef __FUNCT__
237647c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
237747c6ae99SBarry Smith /*@
237847c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
237947c6ae99SBarry Smith 
238047c6ae99SBarry Smith     Not Collective
238147c6ae99SBarry Smith 
238247c6ae99SBarry Smith     Input Parameter:
238347c6ae99SBarry Smith .   dm - the DM object to destroy
238447c6ae99SBarry Smith 
238547c6ae99SBarry Smith     Output Parameter:
238647c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
238747c6ae99SBarry Smith 
238847c6ae99SBarry Smith     Level: developer
238947c6ae99SBarry Smith 
2390e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
239147c6ae99SBarry Smith 
239247c6ae99SBarry Smith @*/
23937087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
239447c6ae99SBarry Smith {
239547c6ae99SBarry Smith   PetscFunctionBegin;
2396171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
239747c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
239847c6ae99SBarry Smith   PetscFunctionReturn(0);
239947c6ae99SBarry Smith }
240047c6ae99SBarry Smith 
240147c6ae99SBarry Smith #undef __FUNCT__
2402b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2403b0ae01b7SPeter Brune /*@
2404b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2405b0ae01b7SPeter Brune 
2406b0ae01b7SPeter Brune     Not Collective
2407b0ae01b7SPeter Brune 
2408b0ae01b7SPeter Brune     Input Parameter:
2409b0ae01b7SPeter Brune .   dm - the DM object
2410b0ae01b7SPeter Brune 
2411b0ae01b7SPeter Brune     Output Parameter:
2412b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2413b0ae01b7SPeter Brune 
2414b0ae01b7SPeter Brune     Level: developer
2415b0ae01b7SPeter Brune 
2416b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2417b0ae01b7SPeter Brune 
2418b0ae01b7SPeter Brune @*/
2419b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2420b0ae01b7SPeter Brune {
2421b0ae01b7SPeter Brune   PetscFunctionBegin;
2422b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2423b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2424b0ae01b7SPeter Brune }
2425b0ae01b7SPeter Brune 
2426b0ae01b7SPeter Brune #undef  __FUNCT__
242708da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2428748fac09SDmitry Karpeev /*@C
242908da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
243008da532bSDmitry Karpeev 
243108da532bSDmitry Karpeev     Collective on DM
243208da532bSDmitry Karpeev 
243308da532bSDmitry Karpeev     Input Parameter:
243408da532bSDmitry Karpeev +   dm - the DM object
2435e88d7f4bSDmitry Karpeev -   x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems.
243608da532bSDmitry Karpeev 
243708da532bSDmitry Karpeev     Level: developer
243808da532bSDmitry Karpeev 
2439*835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
244008da532bSDmitry Karpeev          DMSetFunction(), DMSetJacobian(), DMSetVariableBounds()
244108da532bSDmitry Karpeev 
244208da532bSDmitry Karpeev @*/
244308da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
244408da532bSDmitry Karpeev {
244508da532bSDmitry Karpeev   PetscErrorCode ierr;
244608da532bSDmitry Karpeev   PetscFunctionBegin;
244708da532bSDmitry Karpeev   if (x) {
244808da532bSDmitry Karpeev     if (!dm->x) {
244908da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
245008da532bSDmitry Karpeev     }
245108da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
245208da532bSDmitry Karpeev   }
245308da532bSDmitry Karpeev   else if (dm->x) {
245408da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);  CHKERRQ(ierr);
245508da532bSDmitry Karpeev   }
245608da532bSDmitry Karpeev   PetscFunctionReturn(0);
245708da532bSDmitry Karpeev }
245808da532bSDmitry Karpeev 
245908da532bSDmitry Karpeev 
246008da532bSDmitry Karpeev #undef __FUNCT__
246147c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
246247c6ae99SBarry Smith /*@
246347c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
246447c6ae99SBarry Smith 
246547c6ae99SBarry Smith     Collective on DM
246647c6ae99SBarry Smith 
246747c6ae99SBarry Smith     Input Parameter:
246847c6ae99SBarry Smith +   dm - the DM object to destroy
246947c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
247047c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
247147c6ae99SBarry Smith 
247247c6ae99SBarry Smith     Level: developer
247347c6ae99SBarry Smith 
2474*835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
247547c6ae99SBarry Smith          DMSetJacobian()
247647c6ae99SBarry Smith 
247747c6ae99SBarry Smith @*/
24787087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
247947c6ae99SBarry Smith {
248047c6ae99SBarry Smith   PetscErrorCode ierr;
248147c6ae99SBarry Smith   PetscFunctionBegin;
2482171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
248347c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
2484644e2e5bSBarry Smith   PetscStackPush("DM user function");
248547c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
2486644e2e5bSBarry Smith   PetscStackPop;
248747c6ae99SBarry Smith   PetscFunctionReturn(0);
248847c6ae99SBarry Smith }
248947c6ae99SBarry Smith 
249047c6ae99SBarry Smith 
249108da532bSDmitry Karpeev 
249247c6ae99SBarry Smith #undef __FUNCT__
249347c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
249447c6ae99SBarry Smith /*@
249547c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
249647c6ae99SBarry Smith 
249747c6ae99SBarry Smith     Collective on DM
249847c6ae99SBarry Smith 
249947c6ae99SBarry Smith     Input Parameter:
250047c6ae99SBarry Smith +   dm - the DM object
2501cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
250247c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
250347c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
250447c6ae99SBarry Smith 
250547c6ae99SBarry Smith     Level: developer
250647c6ae99SBarry Smith 
2507*835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
250847c6ae99SBarry Smith          DMSetFunction()
250947c6ae99SBarry Smith 
251047c6ae99SBarry Smith @*/
25117087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
251247c6ae99SBarry Smith {
251347c6ae99SBarry Smith   PetscErrorCode ierr;
251447c6ae99SBarry Smith 
251547c6ae99SBarry Smith   PetscFunctionBegin;
2516171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
251747c6ae99SBarry Smith   if (!dm->ops->jacobian) {
251847c6ae99SBarry Smith     ISColoring     coloring;
251947c6ae99SBarry Smith     MatFDColoring  fd;
252019fd82e9SBarry Smith     MatType        mtype;
252147c6ae99SBarry Smith 
25222c9966d7SBarry Smith     ierr = PetscObjectGetType((PetscObject)B,&mtype);CHKERRQ(ierr);
25232c9966d7SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,mtype,&coloring);CHKERRQ(ierr);
252447c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
2525fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
252647c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
25270bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
25280bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
252971cd77b2SBarry Smith 
253047c6ae99SBarry Smith     dm->fd = fd;
253147c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
25322533e041SBarry Smith 
253371cd77b2SBarry Smith     /* don't know why this is needed */
253471cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
253547c6ae99SBarry Smith   }
253647c6ae99SBarry Smith   if (!x) x = dm->x;
253747c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
2538cab2e9ccSBarry Smith 
253971cd77b2SBarry Smith   /* if matrix depends on x; i.e. nonlinear problem, keep copy of input vector since needed by multigrid methods to generate coarse grid matrices */
2540649052a6SBarry Smith   if (x) {
2541cab2e9ccSBarry Smith     if (!dm->x) {
254271cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
2543cab2e9ccSBarry Smith     }
2544cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
2545649052a6SBarry Smith   }
2546a8248277SBarry Smith   if (A != B) {
2547a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2548a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2549a8248277SBarry Smith   }
255047c6ae99SBarry Smith   PetscFunctionReturn(0);
255147c6ae99SBarry Smith }
2552264ace61SBarry Smith 
2553cab2e9ccSBarry Smith 
2554264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
2555264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
2556264ace61SBarry Smith 
2557264ace61SBarry Smith #undef __FUNCT__
2558264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2559264ace61SBarry Smith /*@C
2560264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2561264ace61SBarry Smith 
2562264ace61SBarry Smith   Collective on DM
2563264ace61SBarry Smith 
2564264ace61SBarry Smith   Input Parameters:
2565264ace61SBarry Smith + dm     - The DM object
2566264ace61SBarry Smith - method - The name of the DM type
2567264ace61SBarry Smith 
2568264ace61SBarry Smith   Options Database Key:
2569264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2570264ace61SBarry Smith 
2571264ace61SBarry Smith   Notes:
2572e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2573264ace61SBarry Smith 
2574264ace61SBarry Smith   Level: intermediate
2575264ace61SBarry Smith 
2576264ace61SBarry Smith .keywords: DM, set, type
2577264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2578264ace61SBarry Smith @*/
257919fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2580264ace61SBarry Smith {
2581264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2582264ace61SBarry Smith   PetscBool      match;
2583264ace61SBarry Smith   PetscErrorCode ierr;
2584264ace61SBarry Smith 
2585264ace61SBarry Smith   PetscFunctionBegin;
2586264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2587251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2588264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2589264ace61SBarry Smith 
2590264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
25914b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
259232c0f0efSBarry Smith   if (!r) SETERRQ1(((PetscObject)dm)->comm,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2593264ace61SBarry Smith 
2594264ace61SBarry Smith   if (dm->ops->destroy) {
2595264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
2596b5c23020SJed Brown     dm->ops->destroy = PETSC_NULL;
2597264ace61SBarry Smith   }
2598264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2599264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2600264ace61SBarry Smith   PetscFunctionReturn(0);
2601264ace61SBarry Smith }
2602264ace61SBarry Smith 
2603264ace61SBarry Smith #undef __FUNCT__
2604264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2605264ace61SBarry Smith /*@C
2606264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2607264ace61SBarry Smith 
2608264ace61SBarry Smith   Not Collective
2609264ace61SBarry Smith 
2610264ace61SBarry Smith   Input Parameter:
2611264ace61SBarry Smith . dm  - The DM
2612264ace61SBarry Smith 
2613264ace61SBarry Smith   Output Parameter:
2614264ace61SBarry Smith . type - The DM type name
2615264ace61SBarry Smith 
2616264ace61SBarry Smith   Level: intermediate
2617264ace61SBarry Smith 
2618264ace61SBarry Smith .keywords: DM, get, type, name
2619264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2620264ace61SBarry Smith @*/
262119fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2622264ace61SBarry Smith {
2623264ace61SBarry Smith   PetscErrorCode ierr;
2624264ace61SBarry Smith 
2625264ace61SBarry Smith   PetscFunctionBegin;
2626264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2627264ace61SBarry Smith   PetscValidCharPointer(type,2);
2628264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2629264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
2630264ace61SBarry Smith   }
2631264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2632264ace61SBarry Smith   PetscFunctionReturn(0);
2633264ace61SBarry Smith }
2634264ace61SBarry Smith 
263567a56275SMatthew G Knepley #undef __FUNCT__
263667a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
263767a56275SMatthew G Knepley /*@C
263867a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
263967a56275SMatthew G Knepley 
264067a56275SMatthew G Knepley   Collective on DM
264167a56275SMatthew G Knepley 
264267a56275SMatthew G Knepley   Input Parameters:
264367a56275SMatthew G Knepley + dm - the DM
264467a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
264567a56275SMatthew G Knepley 
264667a56275SMatthew G Knepley   Output Parameter:
264767a56275SMatthew G Knepley . M - pointer to new DM
264867a56275SMatthew G Knepley 
264967a56275SMatthew G Knepley   Notes:
265067a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
265167a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
265267a56275SMatthew G Knepley   of the input DM.
265367a56275SMatthew G Knepley 
265467a56275SMatthew G Knepley   Level: intermediate
265567a56275SMatthew G Knepley 
265667a56275SMatthew G Knepley .seealso: DMCreate()
265767a56275SMatthew G Knepley @*/
265819fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
265967a56275SMatthew G Knepley {
266067a56275SMatthew G Knepley   DM             B;
266167a56275SMatthew G Knepley   char           convname[256];
266267a56275SMatthew G Knepley   PetscBool      sametype, issame;
266367a56275SMatthew G Knepley   PetscErrorCode ierr;
266467a56275SMatthew G Knepley 
266567a56275SMatthew G Knepley   PetscFunctionBegin;
266667a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
266767a56275SMatthew G Knepley   PetscValidType(dm,1);
266867a56275SMatthew G Knepley   PetscValidPointer(M,3);
2669251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
267067a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
267167a56275SMatthew G Knepley   {
267219fd82e9SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = PETSC_NULL;
267367a56275SMatthew G Knepley 
267467a56275SMatthew G Knepley     /*
267567a56275SMatthew G Knepley        Order of precedence:
267667a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
267767a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
267867a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
267967a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
268067a56275SMatthew G Knepley        5) Use a really basic converter.
268167a56275SMatthew G Knepley     */
268267a56275SMatthew G Knepley 
268367a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
268467a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
268567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
268667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
268767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
268867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
268967a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
269067a56275SMatthew G Knepley     if (conv) goto foundconv;
269167a56275SMatthew G Knepley 
269267a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
269367a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
269467a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
269567a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
269667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
269767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
269867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
269967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
270067a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
270167a56275SMatthew G Knepley     if (conv) {
2702fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
270367a56275SMatthew G Knepley       goto foundconv;
270467a56275SMatthew G Knepley     }
270567a56275SMatthew G Knepley 
270667a56275SMatthew G Knepley #if 0
270767a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
270867a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2709fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
271067a56275SMatthew G Knepley     if (conv) goto foundconv;
271167a56275SMatthew G Knepley 
271267a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
271367a56275SMatthew G Knepley     if (dm->ops->convert) {
271467a56275SMatthew G Knepley       conv = dm->ops->convert;
271567a56275SMatthew G Knepley     }
271667a56275SMatthew G Knepley     if (conv) goto foundconv;
271767a56275SMatthew G Knepley #endif
271867a56275SMatthew G Knepley 
271967a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
272067a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
272167a56275SMatthew G Knepley 
272267a56275SMatthew G Knepley     foundconv:
272367a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
272467a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
272567a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
272667a56275SMatthew G Knepley   }
272767a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
272867a56275SMatthew G Knepley   PetscFunctionReturn(0);
272967a56275SMatthew G Knepley }
2730264ace61SBarry Smith 
2731264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2732264ace61SBarry Smith 
2733264ace61SBarry Smith #undef __FUNCT__
2734264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2735264ace61SBarry Smith /*@C
2736264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
2737264ace61SBarry Smith 
2738264ace61SBarry Smith   Level: advanced
2739264ace61SBarry Smith @*/
27407087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
2741264ace61SBarry Smith {
2742264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
2743264ace61SBarry Smith   PetscErrorCode ierr;
2744264ace61SBarry Smith 
2745264ace61SBarry Smith   PetscFunctionBegin;
2746264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
2747264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
2748264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
2749264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
2750264ace61SBarry Smith   PetscFunctionReturn(0);
2751264ace61SBarry Smith }
2752264ace61SBarry Smith 
2753264ace61SBarry Smith 
2754264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2755264ace61SBarry Smith #undef __FUNCT__
2756264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
2757264ace61SBarry Smith /*@C
2758264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
2759264ace61SBarry Smith 
2760264ace61SBarry Smith    Not Collective
2761264ace61SBarry Smith 
2762264ace61SBarry Smith    Level: advanced
2763264ace61SBarry Smith 
2764264ace61SBarry Smith .keywords: DM, register, destroy
2765264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
2766264ace61SBarry Smith @*/
27677087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
2768264ace61SBarry Smith {
2769264ace61SBarry Smith   PetscErrorCode ierr;
2770264ace61SBarry Smith 
2771264ace61SBarry Smith   PetscFunctionBegin;
2772264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
2773264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
2774264ace61SBarry Smith   PetscFunctionReturn(0);
2775264ace61SBarry Smith }
277623f975d1SBarry Smith 
277723f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2778c6db04a5SJed Brown #include <mex.h>
277923f975d1SBarry Smith 
27803014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
278123f975d1SBarry Smith 
278223f975d1SBarry Smith #undef __FUNCT__
278323f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
278423f975d1SBarry Smith /*
278523f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
278623f975d1SBarry Smith                          DMSetFunctionMatlab().
278723f975d1SBarry Smith 
278823f975d1SBarry Smith    For linear problems x is null
278923f975d1SBarry Smith 
279023f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
279123f975d1SBarry Smith */
27927087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
279323f975d1SBarry Smith {
279423f975d1SBarry Smith   PetscErrorCode    ierr;
279523f975d1SBarry Smith   DMMatlabContext   *sctx;
279623f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
279723f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
279823f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
279923f975d1SBarry Smith 
280023f975d1SBarry Smith   PetscFunctionBegin;
280123f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
280223f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
280323f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
280423f975d1SBarry Smith 
280523f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
28061b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
280723f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
280823f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
28093014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
281023f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
281123f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
281223f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
281323f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
2814b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
281523f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
281623f975d1SBarry Smith   mxDestroyArray(prhs[0]);
281723f975d1SBarry Smith   mxDestroyArray(prhs[1]);
281823f975d1SBarry Smith   mxDestroyArray(prhs[2]);
281923f975d1SBarry Smith   mxDestroyArray(prhs[3]);
282023f975d1SBarry Smith   mxDestroyArray(plhs[0]);
282123f975d1SBarry Smith   PetscFunctionReturn(0);
282223f975d1SBarry Smith }
282323f975d1SBarry Smith 
282423f975d1SBarry Smith 
282523f975d1SBarry Smith #undef __FUNCT__
282623f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
282723f975d1SBarry Smith /*
282823f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
282923f975d1SBarry Smith 
283023f975d1SBarry Smith */
28317087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
283223f975d1SBarry Smith {
283323f975d1SBarry Smith   PetscErrorCode    ierr;
283423f975d1SBarry Smith   DMMatlabContext   *sctx;
283523f975d1SBarry Smith 
283623f975d1SBarry Smith   PetscFunctionBegin;
2837171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
283823f975d1SBarry Smith   /* currently sctx is memory bleed */
28391b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
28403014e516SBarry Smith   if (!sctx) {
284123f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
28423014e516SBarry Smith   }
284323f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
28441b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
284523f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
284623f975d1SBarry Smith   PetscFunctionReturn(0);
284723f975d1SBarry Smith }
28483014e516SBarry Smith 
28493014e516SBarry Smith #undef __FUNCT__
28503014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
28513014e516SBarry Smith /*
28523014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
28533014e516SBarry Smith                          DMSetJacobianMatlab().
28543014e516SBarry Smith 
28553014e516SBarry Smith    For linear problems x is null
28563014e516SBarry Smith 
28573014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
28583014e516SBarry Smith */
28597087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
28603014e516SBarry Smith {
28613014e516SBarry Smith   PetscErrorCode    ierr;
28623014e516SBarry Smith   DMMatlabContext   *sctx;
28633014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
28643014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
28653014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
28663014e516SBarry Smith 
28673014e516SBarry Smith   PetscFunctionBegin;
28683014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28693014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
28703014e516SBarry Smith 
2871e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
28721b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
28733014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
28743014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
28753014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
28763014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
28773014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
28783014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
28793014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
28803014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
28813014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
2882b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
2883c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
2884c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
28853014e516SBarry Smith   mxDestroyArray(prhs[0]);
28863014e516SBarry Smith   mxDestroyArray(prhs[1]);
28873014e516SBarry Smith   mxDestroyArray(prhs[2]);
28883014e516SBarry Smith   mxDestroyArray(prhs[3]);
28893014e516SBarry Smith   mxDestroyArray(prhs[4]);
28903014e516SBarry Smith   mxDestroyArray(plhs[0]);
28913014e516SBarry Smith   mxDestroyArray(plhs[1]);
28923014e516SBarry Smith   PetscFunctionReturn(0);
28933014e516SBarry Smith }
28943014e516SBarry Smith 
28953014e516SBarry Smith 
28963014e516SBarry Smith #undef __FUNCT__
28973014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
28983014e516SBarry Smith /*
28993014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
29003014e516SBarry Smith 
29013014e516SBarry Smith */
29027087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
29033014e516SBarry Smith {
29043014e516SBarry Smith   PetscErrorCode    ierr;
29053014e516SBarry Smith   DMMatlabContext   *sctx;
29063014e516SBarry Smith 
29073014e516SBarry Smith   PetscFunctionBegin;
2908171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29093014e516SBarry Smith   /* currently sctx is memory bleed */
29101b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
29113014e516SBarry Smith   if (!sctx) {
29123014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
29133014e516SBarry Smith   }
29143014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
29151b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
29163014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
29173014e516SBarry Smith   PetscFunctionReturn(0);
29183014e516SBarry Smith }
291923f975d1SBarry Smith #endif
2920b859378eSBarry Smith 
2921b859378eSBarry Smith #undef __FUNCT__
2922b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2923b859378eSBarry Smith /*@C
292455849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2925b859378eSBarry Smith 
2926b859378eSBarry Smith   Collective on PetscViewer
2927b859378eSBarry Smith 
2928b859378eSBarry Smith   Input Parameters:
2929b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2930b859378eSBarry Smith            some related function before a call to DMLoad().
2931b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2932b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2933b859378eSBarry Smith 
2934b859378eSBarry Smith    Level: intermediate
2935b859378eSBarry Smith 
2936b859378eSBarry Smith   Notes:
293755849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2938b859378eSBarry Smith 
2939b859378eSBarry Smith   Notes for advanced users:
2940b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2941b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2942b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2943b859378eSBarry Smith   format is
2944b859378eSBarry Smith .vb
2945b859378eSBarry Smith      has not yet been determined
2946b859378eSBarry Smith .ve
2947b859378eSBarry Smith 
2948b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2949b859378eSBarry Smith @*/
2950b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2951b859378eSBarry Smith {
2952b859378eSBarry Smith   PetscErrorCode ierr;
295332c0f0efSBarry Smith   PetscBool      isbinary;
295455849f57SBarry Smith   PetscInt       classid;
295532c0f0efSBarry Smith   char           type[256];
2956b859378eSBarry Smith 
2957b859378eSBarry Smith   PetscFunctionBegin;
2958b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2959b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
296032c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
296132c0f0efSBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
2962b859378eSBarry Smith 
296332c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
296432c0f0efSBarry Smith   if (classid != DM_FILE_CLASSID) SETERRQ(((PetscObject)newdm)->comm,PETSC_ERR_ARG_WRONG,"Not DM next in file");
296532c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
296632c0f0efSBarry Smith   ierr = DMSetType(newdm, type);CHKERRQ(ierr);
2967b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
2968b859378eSBarry Smith   PetscFunctionReturn(0);
2969b859378eSBarry Smith }
2970b859378eSBarry Smith 
29717da65231SMatthew G Knepley /******************************** FEM Support **********************************/
29727da65231SMatthew G Knepley 
29737da65231SMatthew G Knepley #undef __FUNCT__
29747da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
29757da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
29761d47ebbbSSatish Balay   PetscInt       f;
29771b30c384SMatthew G Knepley   PetscErrorCode ierr;
29781b30c384SMatthew G Knepley 
29797da65231SMatthew G Knepley   PetscFunctionBegin;
298074778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
29811d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
298274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
29837da65231SMatthew G Knepley   }
29847da65231SMatthew G Knepley   PetscFunctionReturn(0);
29857da65231SMatthew G Knepley }
29867da65231SMatthew G Knepley 
29877da65231SMatthew G Knepley #undef __FUNCT__
29887da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
29897da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
29901b30c384SMatthew G Knepley   PetscInt       f, g;
29917da65231SMatthew G Knepley   PetscErrorCode ierr;
29927da65231SMatthew G Knepley 
29937da65231SMatthew G Knepley   PetscFunctionBegin;
299474778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
29951d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
299674778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
29971d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
299874778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
29997da65231SMatthew G Knepley     }
300074778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
30017da65231SMatthew G Knepley   }
30027da65231SMatthew G Knepley   PetscFunctionReturn(0);
30037da65231SMatthew G Knepley }
3004e7c4fc90SDmitry Karpeev 
3005970e74d5SMatthew G Knepley #undef __FUNCT__
3006970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalFunction"
3007970e74d5SMatthew G Knepley /*@C
3008970e74d5SMatthew G Knepley   DMGetLocalFunction - Get the local residual function from this DM
3009970e74d5SMatthew G Knepley 
3010970e74d5SMatthew G Knepley   Not collective
3011970e74d5SMatthew G Knepley 
3012970e74d5SMatthew G Knepley   Input Parameter:
3013970e74d5SMatthew G Knepley . dm - The DM
3014970e74d5SMatthew G Knepley 
3015970e74d5SMatthew G Knepley   Output Parameter:
3016970e74d5SMatthew G Knepley . lf - The local residual function
3017970e74d5SMatthew G Knepley 
3018970e74d5SMatthew G Knepley    Calling sequence of lf:
3019970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
3020970e74d5SMatthew G Knepley 
3021970e74d5SMatthew G Knepley +  snes - the SNES context
3022970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
3023970e74d5SMatthew G Knepley .  f - local vector to put residual in
3024970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
3025970e74d5SMatthew G Knepley 
3026970e74d5SMatthew G Knepley   Level: intermediate
3027970e74d5SMatthew G Knepley 
3028970e74d5SMatthew G Knepley .seealso DMSetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
3029970e74d5SMatthew G Knepley @*/
3030970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalFunction(DM dm, PetscErrorCode (**lf)(DM, Vec, Vec, void *))
3031970e74d5SMatthew G Knepley {
3032970e74d5SMatthew G Knepley   PetscFunctionBegin;
3033970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3034970e74d5SMatthew G Knepley   if (lf) *lf = dm->lf;
3035970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
3036970e74d5SMatthew G Knepley }
3037970e74d5SMatthew G Knepley 
3038970e74d5SMatthew G Knepley #undef __FUNCT__
3039970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalFunction"
3040970e74d5SMatthew G Knepley /*@C
3041970e74d5SMatthew G Knepley   DMSetLocalFunction - Set the local residual function from this DM
3042970e74d5SMatthew G Knepley 
3043970e74d5SMatthew G Knepley   Not collective
3044970e74d5SMatthew G Knepley 
3045970e74d5SMatthew G Knepley   Input Parameters:
3046970e74d5SMatthew G Knepley + dm - The DM
3047970e74d5SMatthew G Knepley - lf - The local residual function
3048970e74d5SMatthew G Knepley 
3049970e74d5SMatthew G Knepley    Calling sequence of lf:
3050970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
3051970e74d5SMatthew G Knepley 
3052970e74d5SMatthew G Knepley +  snes - the SNES context
3053970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
3054970e74d5SMatthew G Knepley .  f - local vector to put residual in
3055970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
3056970e74d5SMatthew G Knepley 
3057970e74d5SMatthew G Knepley   Level: intermediate
3058970e74d5SMatthew G Knepley 
3059970e74d5SMatthew G Knepley .seealso DMGetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
3060970e74d5SMatthew G Knepley @*/
3061970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalFunction(DM dm, PetscErrorCode (*lf)(DM, Vec, Vec, void *))
3062970e74d5SMatthew G Knepley {
3063970e74d5SMatthew G Knepley   PetscFunctionBegin;
3064970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3065970e74d5SMatthew G Knepley   dm->lf = lf;
3066970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
3067970e74d5SMatthew G Knepley }
3068970e74d5SMatthew G Knepley 
3069970e74d5SMatthew G Knepley #undef __FUNCT__
3070970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalJacobian"
3071970e74d5SMatthew G Knepley /*@C
3072970e74d5SMatthew G Knepley   DMGetLocalJacobian - Get the local Jacobian function from this DM
3073970e74d5SMatthew G Knepley 
3074970e74d5SMatthew G Knepley   Not collective
3075970e74d5SMatthew G Knepley 
3076970e74d5SMatthew G Knepley   Input Parameter:
3077970e74d5SMatthew G Knepley . dm - The DM
3078970e74d5SMatthew G Knepley 
3079970e74d5SMatthew G Knepley   Output Parameter:
3080970e74d5SMatthew G Knepley . lj - The local Jacobian function
3081970e74d5SMatthew G Knepley 
3082970e74d5SMatthew G Knepley    Calling sequence of lj:
3083970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
3084970e74d5SMatthew G Knepley 
3085970e74d5SMatthew G Knepley +  snes - the SNES context
3086970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
3087970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
3088970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
3089970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
3090970e74d5SMatthew G Knepley 
3091970e74d5SMatthew G Knepley   Level: intermediate
3092970e74d5SMatthew G Knepley 
3093970e74d5SMatthew G Knepley .seealso DMSetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
3094970e74d5SMatthew G Knepley @*/
3095970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalJacobian(DM dm, PetscErrorCode (**lj)(DM, Vec, Mat, Mat, void *))
3096970e74d5SMatthew G Knepley {
3097970e74d5SMatthew G Knepley   PetscFunctionBegin;
3098970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3099970e74d5SMatthew G Knepley   if (lj) *lj = dm->lj;
3100970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
3101970e74d5SMatthew G Knepley }
3102970e74d5SMatthew G Knepley 
3103970e74d5SMatthew G Knepley #undef __FUNCT__
3104970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalJacobian"
3105970e74d5SMatthew G Knepley /*@C
3106970e74d5SMatthew G Knepley   DMSetLocalJacobian - Set the local Jacobian function from this DM
3107970e74d5SMatthew G Knepley 
3108970e74d5SMatthew G Knepley   Not collective
3109970e74d5SMatthew G Knepley 
3110970e74d5SMatthew G Knepley   Input Parameters:
3111970e74d5SMatthew G Knepley + dm - The DM
3112970e74d5SMatthew G Knepley - lj - The local Jacobian function
3113970e74d5SMatthew G Knepley 
3114970e74d5SMatthew G Knepley    Calling sequence of lj:
3115970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
3116970e74d5SMatthew G Knepley 
3117970e74d5SMatthew G Knepley +  snes - the SNES context
3118970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
3119970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
3120970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
3121970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
3122970e74d5SMatthew G Knepley 
3123970e74d5SMatthew G Knepley   Level: intermediate
3124970e74d5SMatthew G Knepley 
3125970e74d5SMatthew G Knepley .seealso DMGetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
3126970e74d5SMatthew G Knepley @*/
3127970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalJacobian(DM dm, PetscErrorCode (*lj)(DM, Vec, Mat,  Mat, void *))
3128970e74d5SMatthew G Knepley {
3129970e74d5SMatthew G Knepley   PetscFunctionBegin;
3130970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3131970e74d5SMatthew G Knepley   dm->lj = lj;
3132970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
3133970e74d5SMatthew G Knepley }
313488ed4aceSMatthew G Knepley 
313588ed4aceSMatthew G Knepley #undef __FUNCT__
313688ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
313788ed4aceSMatthew G Knepley /*@
313888ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
313988ed4aceSMatthew G Knepley 
314088ed4aceSMatthew G Knepley   Input Parameter:
314188ed4aceSMatthew G Knepley . dm - The DM
314288ed4aceSMatthew G Knepley 
314388ed4aceSMatthew G Knepley   Output Parameter:
314488ed4aceSMatthew G Knepley . section - The PetscSection
314588ed4aceSMatthew G Knepley 
314688ed4aceSMatthew G Knepley   Level: intermediate
314788ed4aceSMatthew G Knepley 
314888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
314988ed4aceSMatthew G Knepley 
315088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
315188ed4aceSMatthew G Knepley @*/
315288ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) {
315388ed4aceSMatthew G Knepley   PetscFunctionBegin;
315488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
315588ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
315688ed4aceSMatthew G Knepley   *section = dm->defaultSection;
315788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
315888ed4aceSMatthew G Knepley }
315988ed4aceSMatthew G Knepley 
316088ed4aceSMatthew G Knepley #undef __FUNCT__
316188ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
316288ed4aceSMatthew G Knepley /*@
316388ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
316488ed4aceSMatthew G Knepley 
316588ed4aceSMatthew G Knepley   Input Parameters:
316688ed4aceSMatthew G Knepley + dm - The DM
316788ed4aceSMatthew G Knepley - section - The PetscSection
316888ed4aceSMatthew G Knepley 
316988ed4aceSMatthew G Knepley   Level: intermediate
317088ed4aceSMatthew G Knepley 
317188ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
317288ed4aceSMatthew G Knepley 
317388ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
317488ed4aceSMatthew G Knepley @*/
317588ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) {
3176af122d2aSMatthew G Knepley   PetscInt       numFields;
3177af122d2aSMatthew G Knepley   PetscInt       f;
317888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
317988ed4aceSMatthew G Knepley 
318088ed4aceSMatthew G Knepley   PetscFunctionBegin;
318188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
318288ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
318388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
318488ed4aceSMatthew G Knepley   dm->defaultSection = section;
3185af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
3186af122d2aSMatthew G Knepley   if (numFields) {
3187af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3188af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
3189af122d2aSMatthew G Knepley       const char *name;
3190af122d2aSMatthew G Knepley 
3191af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
3192af122d2aSMatthew G Knepley       ierr = PetscObjectSetName(dm->fields[f], name);CHKERRQ(ierr);
3193af122d2aSMatthew G Knepley     }
3194af122d2aSMatthew G Knepley   }
319588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
319688ed4aceSMatthew G Knepley }
319788ed4aceSMatthew G Knepley 
319888ed4aceSMatthew G Knepley #undef __FUNCT__
319988ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
320088ed4aceSMatthew G Knepley /*@
320188ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
320288ed4aceSMatthew G Knepley 
32038b1ab98fSJed Brown   Collective on DM
32048b1ab98fSJed Brown 
320588ed4aceSMatthew G Knepley   Input Parameter:
320688ed4aceSMatthew G Knepley . dm - The DM
320788ed4aceSMatthew G Knepley 
320888ed4aceSMatthew G Knepley   Output Parameter:
320988ed4aceSMatthew G Knepley . section - The PetscSection
321088ed4aceSMatthew G Knepley 
321188ed4aceSMatthew G Knepley   Level: intermediate
321288ed4aceSMatthew G Knepley 
321388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
321488ed4aceSMatthew G Knepley 
321588ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
321688ed4aceSMatthew G Knepley @*/
321788ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) {
321888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
321988ed4aceSMatthew G Knepley 
322088ed4aceSMatthew G Knepley   PetscFunctionBegin;
322188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
322288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
322388ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
322440e8a239SMatthew G Knepley     if (!dm->defaultSection || !dm->sf) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection and PetscSF in order to create a global PetscSection");
3225b21d0597SMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
32268b1ab98fSJed Brown     ierr = PetscSectionGetValueLayout(((PetscObject)dm)->comm,dm->defaultGlobalSection,&dm->map);CHKERRQ(ierr);
322788ed4aceSMatthew G Knepley   }
322888ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
322988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
323088ed4aceSMatthew G Knepley }
323188ed4aceSMatthew G Knepley 
323288ed4aceSMatthew G Knepley #undef __FUNCT__
3233b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3234b21d0597SMatthew G Knepley /*@
3235b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3236b21d0597SMatthew G Knepley 
3237b21d0597SMatthew G Knepley   Input Parameters:
3238b21d0597SMatthew G Knepley + dm - The DM
3239b21d0597SMatthew G Knepley - section - The PetscSection
3240b21d0597SMatthew G Knepley 
3241b21d0597SMatthew G Knepley   Level: intermediate
3242b21d0597SMatthew G Knepley 
3243b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3244b21d0597SMatthew G Knepley 
3245b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3246b21d0597SMatthew G Knepley @*/
3247b21d0597SMatthew G Knepley PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section) {
3248b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3249b21d0597SMatthew G Knepley 
3250b21d0597SMatthew G Knepley   PetscFunctionBegin;
3251b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3252b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3253b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3254b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3255b21d0597SMatthew G Knepley }
3256b21d0597SMatthew G Knepley 
3257b21d0597SMatthew G Knepley #undef __FUNCT__
325888ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
325988ed4aceSMatthew G Knepley /*@
326088ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
326188ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
326288ed4aceSMatthew G Knepley 
326388ed4aceSMatthew G Knepley   Input Parameter:
326488ed4aceSMatthew G Knepley . dm - The DM
326588ed4aceSMatthew G Knepley 
326688ed4aceSMatthew G Knepley   Output Parameter:
326788ed4aceSMatthew G Knepley . sf - The PetscSF
326888ed4aceSMatthew G Knepley 
326988ed4aceSMatthew G Knepley   Level: intermediate
327088ed4aceSMatthew G Knepley 
327188ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
327288ed4aceSMatthew G Knepley 
327388ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
327488ed4aceSMatthew G Knepley @*/
327588ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) {
327688ed4aceSMatthew G Knepley   PetscInt       nroots;
327788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
327888ed4aceSMatthew G Knepley 
327988ed4aceSMatthew G Knepley   PetscFunctionBegin;
328088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
328188ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
328288ed4aceSMatthew G Knepley   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
328388ed4aceSMatthew G Knepley   if (nroots < 0) {
328488ed4aceSMatthew G Knepley     PetscSection section, gSection;
328588ed4aceSMatthew G Knepley 
328688ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
328731ea6d37SMatthew G Knepley     if (section) {
328888ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
328988ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
329031ea6d37SMatthew G Knepley     } else {
329131ea6d37SMatthew G Knepley       *sf = PETSC_NULL;
329231ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
329331ea6d37SMatthew G Knepley     }
329488ed4aceSMatthew G Knepley   }
329588ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
329688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
329788ed4aceSMatthew G Knepley }
329888ed4aceSMatthew G Knepley 
329988ed4aceSMatthew G Knepley #undef __FUNCT__
330088ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
330188ed4aceSMatthew G Knepley /*@
330288ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
330388ed4aceSMatthew G Knepley 
330488ed4aceSMatthew G Knepley   Input Parameters:
330588ed4aceSMatthew G Knepley + dm - The DM
330688ed4aceSMatthew G Knepley - sf - The PetscSF
330788ed4aceSMatthew G Knepley 
330888ed4aceSMatthew G Knepley   Level: intermediate
330988ed4aceSMatthew G Knepley 
331088ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
331188ed4aceSMatthew G Knepley 
331288ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
331388ed4aceSMatthew G Knepley @*/
331488ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) {
331588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
331688ed4aceSMatthew G Knepley 
331788ed4aceSMatthew G Knepley   PetscFunctionBegin;
331888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
331988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
332088ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
332188ed4aceSMatthew G Knepley   dm->defaultSF = sf;
332288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
332388ed4aceSMatthew G Knepley }
332488ed4aceSMatthew G Knepley 
332588ed4aceSMatthew G Knepley #undef __FUNCT__
332688ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
332788ed4aceSMatthew G Knepley /*@C
332888ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
332988ed4aceSMatthew G Knepley   describing the data layout.
333088ed4aceSMatthew G Knepley 
333188ed4aceSMatthew G Knepley   Input Parameters:
333288ed4aceSMatthew G Knepley + dm - The DM
333388ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
333488ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
333588ed4aceSMatthew G Knepley 
333688ed4aceSMatthew G Knepley   Level: intermediate
333788ed4aceSMatthew G Knepley 
333888ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
333988ed4aceSMatthew G Knepley @*/
334088ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
334188ed4aceSMatthew G Knepley {
334288ed4aceSMatthew G Knepley   MPI_Comm        comm = ((PetscObject) dm)->comm;
334388ed4aceSMatthew G Knepley   PetscLayout     layout;
334488ed4aceSMatthew G Knepley   const PetscInt *ranges;
334588ed4aceSMatthew G Knepley   PetscInt       *local;
334688ed4aceSMatthew G Knepley   PetscSFNode    *remote;
334788ed4aceSMatthew G Knepley   PetscInt        pStart, pEnd, p, nroots, nleaves, l;
334888ed4aceSMatthew G Knepley   PetscMPIInt     size, rank;
334988ed4aceSMatthew G Knepley   PetscErrorCode  ierr;
335088ed4aceSMatthew G Knepley 
335188ed4aceSMatthew G Knepley   PetscFunctionBegin;
335288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
335388ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
335488ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
335588ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
335688ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
335788ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
335888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
335988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
336088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
336188ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
336288ed4aceSMatthew G Knepley   for (p = pStart, nleaves = 0; p < pEnd; ++p) {
33636636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
336488ed4aceSMatthew G Knepley 
33656636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
33666636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
33676636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
336888ed4aceSMatthew G Knepley   }
336988ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
337088ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
337188ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
33721f588964SMatthew G Knepley     const PetscInt *cind;
33736636e97aSMatthew G Knepley     PetscInt        dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
337488ed4aceSMatthew G Knepley 
337588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
337688ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
337788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
337888ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
337988ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
33806636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
338188ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
33826636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
33836636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
33846636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3385057b4bcdSMatthew G Knepley       if (gsize != dof) SETERRQ4(comm, PETSC_ERR_ARG_WRONG, "Global dof %d for point %d is neither the constrained size %d, nor the unconstrained %d", gsize, p, dof-cdof, dof);
33866636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
33876636e97aSMatthew G Knepley     }
338888ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
338988ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
339088ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
339188ed4aceSMatthew G Knepley     }
339288ed4aceSMatthew G Knepley     if (gdof < 0) {
33936636e97aSMatthew G Knepley       for(d = 0; d < gsize; ++d, ++l) {
339488ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
339588ed4aceSMatthew G Knepley 
339631d3f06eSJed Brown         ierr = PetscFindInt(offset,size,ranges,&r);CHKERRQ(ierr);
339731d3f06eSJed Brown         if (r < 0) r = -(r+2);
339888ed4aceSMatthew G Knepley         remote[l].rank  = r;
339988ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
340088ed4aceSMatthew G Knepley       }
340188ed4aceSMatthew G Knepley     } else {
34026636e97aSMatthew G Knepley       for(d = 0; d < gsize; ++d, ++l) {
340388ed4aceSMatthew G Knepley         remote[l].rank  = rank;
340488ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
340588ed4aceSMatthew G Knepley       }
340688ed4aceSMatthew G Knepley     }
340788ed4aceSMatthew G Knepley   }
34086636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
340988ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
341088ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
341188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
341288ed4aceSMatthew G Knepley }
3413af122d2aSMatthew G Knepley 
3414af122d2aSMatthew G Knepley #undef __FUNCT__
3415b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3416b21d0597SMatthew G Knepley /*@
3417b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3418b21d0597SMatthew G Knepley 
3419b21d0597SMatthew G Knepley   Input Parameter:
3420b21d0597SMatthew G Knepley . dm - The DM
3421b21d0597SMatthew G Knepley 
3422b21d0597SMatthew G Knepley   Output Parameter:
3423b21d0597SMatthew G Knepley . sf - The PetscSF
3424b21d0597SMatthew G Knepley 
3425b21d0597SMatthew G Knepley   Level: intermediate
3426b21d0597SMatthew G Knepley 
3427b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3428b21d0597SMatthew G Knepley 
3429057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3430b21d0597SMatthew G Knepley @*/
3431b21d0597SMatthew G Knepley PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf) {
3432b21d0597SMatthew G Knepley   PetscFunctionBegin;
3433b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3434b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3435b21d0597SMatthew G Knepley   *sf = dm->sf;
3436b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3437b21d0597SMatthew G Knepley }
3438b21d0597SMatthew G Knepley 
3439b21d0597SMatthew G Knepley #undef __FUNCT__
3440057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3441057b4bcdSMatthew G Knepley /*@
3442057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3443057b4bcdSMatthew G Knepley 
3444057b4bcdSMatthew G Knepley   Input Parameters:
3445057b4bcdSMatthew G Knepley + dm - The DM
3446057b4bcdSMatthew G Knepley - sf - The PetscSF
3447057b4bcdSMatthew G Knepley 
3448057b4bcdSMatthew G Knepley   Level: intermediate
3449057b4bcdSMatthew G Knepley 
3450057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3451057b4bcdSMatthew G Knepley @*/
3452057b4bcdSMatthew G Knepley PetscErrorCode DMSetPointSF(DM dm, PetscSF sf) {
3453057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3454057b4bcdSMatthew G Knepley 
3455057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3456057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3457057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3458057b4bcdSMatthew G Knepley   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3459057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3460057b4bcdSMatthew G Knepley   dm->sf = sf;
3461057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3462057b4bcdSMatthew G Knepley }
3463057b4bcdSMatthew G Knepley 
3464057b4bcdSMatthew G Knepley #undef __FUNCT__
3465af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetNumFields"
3466af122d2aSMatthew G Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
3467af122d2aSMatthew G Knepley {
3468af122d2aSMatthew G Knepley   PetscFunctionBegin;
3469af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3470af122d2aSMatthew G Knepley   PetscValidPointer(numFields, 2);
3471af122d2aSMatthew G Knepley   *numFields = dm->numFields;
3472af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3473af122d2aSMatthew G Knepley }
3474af122d2aSMatthew G Knepley 
3475af122d2aSMatthew G Knepley #undef __FUNCT__
3476af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3477af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3478af122d2aSMatthew G Knepley {
3479af122d2aSMatthew G Knepley   PetscInt       f;
3480af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3481af122d2aSMatthew G Knepley 
3482af122d2aSMatthew G Knepley   PetscFunctionBegin;
3483af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3484af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3485af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&dm->fields[f]);CHKERRQ(ierr);
3486af122d2aSMatthew G Knepley   }
3487af122d2aSMatthew G Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
3488af122d2aSMatthew G Knepley   dm->numFields = numFields;
3489af122d2aSMatthew G Knepley   ierr = PetscMalloc(dm->numFields * sizeof(PetscObject), &dm->fields);CHKERRQ(ierr);
3490af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3491af122d2aSMatthew G Knepley     ierr = PetscContainerCreate(((PetscObject) dm)->comm, (PetscContainer *) &dm->fields[f]);CHKERRQ(ierr);
3492af122d2aSMatthew G Knepley   }
3493af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3494af122d2aSMatthew G Knepley }
3495af122d2aSMatthew G Knepley 
3496af122d2aSMatthew G Knepley #undef __FUNCT__
3497af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3498af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3499af122d2aSMatthew G Knepley {
3500af122d2aSMatthew G Knepley   PetscFunctionBegin;
3501af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3502af122d2aSMatthew G Knepley   PetscValidPointer(field, 2);
3503af122d2aSMatthew G Knepley   if (!dm->fields) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "Fields have not been setup in this DM. Call DMSetNumFields()");
3504af122d2aSMatthew G Knepley   if ((f < 0) || (f >= dm->numFields)) SETERRQ3(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Field %d should be in [%d,%d)", f, 0, dm->numFields);
3505af122d2aSMatthew G Knepley   *field = dm->fields[f];
3506af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3507af122d2aSMatthew G Knepley }
35086636e97aSMatthew G Knepley 
35096636e97aSMatthew G Knepley #undef __FUNCT__
35106636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
35116636e97aSMatthew G Knepley /*@
35126636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
35136636e97aSMatthew G Knepley 
35146636e97aSMatthew G Knepley   Collective on DM
35156636e97aSMatthew G Knepley 
35166636e97aSMatthew G Knepley   Input Parameters:
35176636e97aSMatthew G Knepley + dm - the DM
35186636e97aSMatthew G Knepley - c - coordinate vector
35196636e97aSMatthew G Knepley 
35206636e97aSMatthew G Knepley   Note:
35216636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
35226636e97aSMatthew G Knepley 
35236636e97aSMatthew G Knepley   Level: intermediate
35246636e97aSMatthew G Knepley 
35256636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
35266636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
35276636e97aSMatthew G Knepley @*/
35286636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
35296636e97aSMatthew G Knepley {
35306636e97aSMatthew G Knepley   PetscErrorCode ierr;
35316636e97aSMatthew G Knepley 
35326636e97aSMatthew G Knepley   PetscFunctionBegin;
35336636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35346636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
35356636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
35366636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
35376636e97aSMatthew G Knepley   dm->coordinates = c;
35386636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
35396636e97aSMatthew G Knepley   PetscFunctionReturn(0);
35406636e97aSMatthew G Knepley }
35416636e97aSMatthew G Knepley 
35426636e97aSMatthew G Knepley #undef __FUNCT__
35436636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
35446636e97aSMatthew G Knepley /*@
35456636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
35466636e97aSMatthew G Knepley 
35476636e97aSMatthew G Knepley   Collective on DM
35486636e97aSMatthew G Knepley 
35496636e97aSMatthew G Knepley    Input Parameters:
35506636e97aSMatthew G Knepley +  dm - the DM
35516636e97aSMatthew G Knepley -  c - coordinate vector
35526636e97aSMatthew G Knepley 
35536636e97aSMatthew G Knepley   Note:
35546636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
35556636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
35566636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
35576636e97aSMatthew G Knepley 
35586636e97aSMatthew G Knepley   Level: intermediate
35596636e97aSMatthew G Knepley 
35606636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
35616636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
35626636e97aSMatthew G Knepley @*/
35636636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
35646636e97aSMatthew G Knepley {
35656636e97aSMatthew G Knepley   PetscErrorCode ierr;
35666636e97aSMatthew G Knepley 
35676636e97aSMatthew G Knepley   PetscFunctionBegin;
35686636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35696636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
35706636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
35716636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
35726636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
35736636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
35746636e97aSMatthew G Knepley   PetscFunctionReturn(0);
35756636e97aSMatthew G Knepley }
35766636e97aSMatthew G Knepley 
35776636e97aSMatthew G Knepley #undef __FUNCT__
35786636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
35796636e97aSMatthew G Knepley /*@
35806636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
35816636e97aSMatthew G Knepley 
35826636e97aSMatthew G Knepley   Not Collective
35836636e97aSMatthew G Knepley 
35846636e97aSMatthew G Knepley   Input Parameter:
35856636e97aSMatthew G Knepley . dm - the DM
35866636e97aSMatthew G Knepley 
35876636e97aSMatthew G Knepley   Output Parameter:
35886636e97aSMatthew G Knepley . c - global coordinate vector
35896636e97aSMatthew G Knepley 
35906636e97aSMatthew G Knepley   Note:
35916636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
35926636e97aSMatthew G Knepley 
35936636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
35946636e97aSMatthew G Knepley 
35956636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
35966636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
35976636e97aSMatthew G Knepley 
35986636e97aSMatthew G Knepley   Level: intermediate
35996636e97aSMatthew G Knepley 
36006636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
36016636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
36026636e97aSMatthew G Knepley @*/
36036636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
36046636e97aSMatthew G Knepley {
36056636e97aSMatthew G Knepley   PetscErrorCode ierr;
36066636e97aSMatthew G Knepley 
36076636e97aSMatthew G Knepley   PetscFunctionBegin;
36086636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36096636e97aSMatthew G Knepley   PetscValidPointer(c,2);
36101f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
36116636e97aSMatthew G Knepley     DM cdm;
36126636e97aSMatthew G Knepley 
36136636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
36146636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
36156636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
36166636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
36176636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
36186636e97aSMatthew G Knepley   }
36196636e97aSMatthew G Knepley   *c = dm->coordinates;
36206636e97aSMatthew G Knepley   PetscFunctionReturn(0);
36216636e97aSMatthew G Knepley }
36226636e97aSMatthew G Knepley 
36236636e97aSMatthew G Knepley #undef __FUNCT__
36246636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
36256636e97aSMatthew G Knepley /*@
36266636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
36276636e97aSMatthew G Knepley 
36286636e97aSMatthew G Knepley   Collective on DM
36296636e97aSMatthew G Knepley 
36306636e97aSMatthew G Knepley   Input Parameter:
36316636e97aSMatthew G Knepley . dm - the DM
36326636e97aSMatthew G Knepley 
36336636e97aSMatthew G Knepley   Output Parameter:
36346636e97aSMatthew G Knepley . c - coordinate vector
36356636e97aSMatthew G Knepley 
36366636e97aSMatthew G Knepley   Note:
36376636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
36386636e97aSMatthew G Knepley 
36396636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
36406636e97aSMatthew G Knepley 
36416636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
36426636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
36436636e97aSMatthew G Knepley 
36446636e97aSMatthew G Knepley   Level: intermediate
36456636e97aSMatthew G Knepley 
36466636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
36476636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
36486636e97aSMatthew G Knepley @*/
36496636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
36506636e97aSMatthew G Knepley {
36516636e97aSMatthew G Knepley   PetscErrorCode ierr;
36526636e97aSMatthew G Knepley 
36536636e97aSMatthew G Knepley   PetscFunctionBegin;
36546636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36556636e97aSMatthew G Knepley   PetscValidPointer(c,2);
36561f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
36576636e97aSMatthew G Knepley     DM cdm;
36586636e97aSMatthew G Knepley 
36596636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
36606636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
36616636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
36626636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
36636636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
36646636e97aSMatthew G Knepley   }
36656636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
36666636e97aSMatthew G Knepley   PetscFunctionReturn(0);
36676636e97aSMatthew G Knepley }
36686636e97aSMatthew G Knepley 
36696636e97aSMatthew G Knepley #undef __FUNCT__
36706636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
36716636e97aSMatthew G Knepley /*@
36726636e97aSMatthew G Knepley   DMGetCoordinateDM - Gets the DM that scatters between global and local coordinates
36736636e97aSMatthew G Knepley 
36746636e97aSMatthew G Knepley   Collective on DM
36756636e97aSMatthew G Knepley 
36766636e97aSMatthew G Knepley   Input Parameter:
36776636e97aSMatthew G Knepley . dm - the DM
36786636e97aSMatthew G Knepley 
36796636e97aSMatthew G Knepley   Output Parameter:
36806636e97aSMatthew G Knepley . cdm - coordinate DM
36816636e97aSMatthew G Knepley 
36826636e97aSMatthew G Knepley   Level: intermediate
36836636e97aSMatthew G Knepley 
36846636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
36856636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
36866636e97aSMatthew G Knepley @*/
36876636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
36886636e97aSMatthew G Knepley {
36896636e97aSMatthew G Knepley   PetscErrorCode ierr;
36906636e97aSMatthew G Knepley 
36916636e97aSMatthew G Knepley   PetscFunctionBegin;
36926636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36936636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
36946636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
36956636e97aSMatthew G Knepley     if (!dm->ops->createcoordinatedm) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "Unable to create coordinates for this DM");
36966636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
36976636e97aSMatthew G Knepley   }
36986636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
36996636e97aSMatthew G Knepley   PetscFunctionReturn(0);
37006636e97aSMatthew G Knepley }
3701