xref: /petsc/src/dm/interface/dm.c (revision c688c0463f6c25494d0d243b2a43b2b3454e025c)
108da532bSDmitry Karpeev #include <petscsnes.h>
2b45d2f2cSJed Brown #include <petsc-private/dmimpl.h>     /*I      "petscdm.h"     I*/
347c6ae99SBarry Smith 
4732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
567a56275SMatthew G Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal;
667a56275SMatthew G Knepley 
747c6ae99SBarry Smith #undef __FUNCT__
8a4121054SBarry Smith #define __FUNCT__ "DMCreate"
9a4121054SBarry Smith /*@
10de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
11a4121054SBarry Smith 
12a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
13a4121054SBarry Smith    error when you try to use the vector.
14a4121054SBarry Smith 
15a4121054SBarry Smith   Collective on MPI_Comm
16a4121054SBarry Smith 
17a4121054SBarry Smith   Input Parameter:
18a4121054SBarry Smith . comm - The communicator for the DM object
19a4121054SBarry Smith 
20a4121054SBarry Smith   Output Parameter:
21a4121054SBarry Smith . dm - The DM object
22a4121054SBarry Smith 
23a4121054SBarry Smith   Level: beginner
24a4121054SBarry Smith 
25a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE
26a4121054SBarry Smith @*/
277087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
28a4121054SBarry Smith {
29a4121054SBarry Smith   DM             v;
30a4121054SBarry Smith   PetscErrorCode ierr;
31a4121054SBarry Smith 
32a4121054SBarry Smith   PetscFunctionBegin;
331411c6eeSJed Brown   PetscValidPointer(dm,2);
341411c6eeSJed Brown   *dm = PETSC_NULL;
35a4121054SBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
36b84caa0eSBarry Smith   ierr = VecInitializePackage(PETSC_NULL);CHKERRQ(ierr);
37b84caa0eSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
38a4121054SBarry Smith   ierr = DMInitializePackage(PETSC_NULL);CHKERRQ(ierr);
39a4121054SBarry Smith #endif
40a4121054SBarry Smith 
413194b578SJed Brown   ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, -1, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
42a4121054SBarry Smith   ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr);
431411c6eeSJed Brown 
44e7c4fc90SDmitry Karpeev 
451411c6eeSJed Brown   v->ltogmap      = PETSC_NULL;
461411c6eeSJed Brown   v->ltogmapb     = PETSC_NULL;
471411c6eeSJed Brown   v->bs           = 1;
48171400e9SBarry Smith   v->coloringtype = IS_COLORING_GLOBAL;
49970e74d5SMatthew G Knepley   v->lf           = PETSC_NULL;
50970e74d5SMatthew G Knepley   v->lj           = PETSC_NULL;
5188ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5288ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
5388ed4aceSMatthew G Knepley   v->defaultSection       = PETSC_NULL;
5488ed4aceSMatthew G Knepley   v->defaultGlobalSection = PETSC_NULL;
55435a35e8SMatthew G Knepley   {
56435a35e8SMatthew G Knepley     PetscInt i;
57435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
58435a35e8SMatthew G Knepley       v->nullspaceConstructors[i] = PETSC_NULL;
59435a35e8SMatthew G Knepley     }
60435a35e8SMatthew G Knepley   }
61af122d2aSMatthew G Knepley   v->numFields = 0;
62af122d2aSMatthew G Knepley   v->fields    = PETSC_NULL;
631411c6eeSJed Brown 
641411c6eeSJed Brown   *dm = v;
65a4121054SBarry Smith   PetscFunctionReturn(0);
66a4121054SBarry Smith }
67a4121054SBarry Smith 
68a4121054SBarry Smith #undef __FUNCT__
699a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
709a42bb27SBarry Smith /*@C
71564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
729a42bb27SBarry Smith 
73aa219208SBarry Smith    Logically Collective on DMDA
749a42bb27SBarry Smith 
759a42bb27SBarry Smith    Input Parameter:
769a42bb27SBarry Smith +  da - initial distributed array
778154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
789a42bb27SBarry Smith 
799a42bb27SBarry Smith    Options Database:
80dd85299cSBarry Smith .   -dm_vec_type ctype
819a42bb27SBarry Smith 
829a42bb27SBarry Smith    Level: intermediate
839a42bb27SBarry Smith 
84aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
859a42bb27SBarry Smith @*/
8619fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
879a42bb27SBarry Smith {
889a42bb27SBarry Smith   PetscErrorCode ierr;
899a42bb27SBarry Smith 
909a42bb27SBarry Smith   PetscFunctionBegin;
919a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
929a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
9319fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
949a42bb27SBarry Smith   PetscFunctionReturn(0);
959a42bb27SBarry Smith }
969a42bb27SBarry Smith 
979a42bb27SBarry Smith #undef __FUNCT__
985f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
995f1ad066SMatthew G Knepley /*@
1005f1ad066SMatthew G Knepley   VecSetDM - Gets the DM defining the data layout of the vector
1015f1ad066SMatthew G Knepley 
1025f1ad066SMatthew G Knepley   Not collective
1035f1ad066SMatthew G Knepley 
1045f1ad066SMatthew G Knepley   Input Parameter:
1055f1ad066SMatthew G Knepley . v - The Vec
1065f1ad066SMatthew G Knepley 
1075f1ad066SMatthew G Knepley   Output Parameter:
1085f1ad066SMatthew G Knepley . dm - The DM
1095f1ad066SMatthew G Knepley 
1105f1ad066SMatthew G Knepley   Level: intermediate
1115f1ad066SMatthew G Knepley 
1125f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
1135f1ad066SMatthew G Knepley @*/
1145f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
1155f1ad066SMatthew G Knepley {
1165f1ad066SMatthew G Knepley   PetscErrorCode ierr;
1175f1ad066SMatthew G Knepley 
1185f1ad066SMatthew G Knepley   PetscFunctionBegin;
1195f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1205f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
1215f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject *) dm);CHKERRQ(ierr);
1225f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
1235f1ad066SMatthew G Knepley }
1245f1ad066SMatthew G Knepley 
1255f1ad066SMatthew G Knepley #undef __FUNCT__
1265f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
1275f1ad066SMatthew G Knepley /*@
1285f1ad066SMatthew G Knepley   VecSetDM - Sets the DM defining the data layout of the vector
1295f1ad066SMatthew G Knepley 
1305f1ad066SMatthew G Knepley   Not collective
1315f1ad066SMatthew G Knepley 
1325f1ad066SMatthew G Knepley   Input Parameters:
1335f1ad066SMatthew G Knepley + v - The Vec
1345f1ad066SMatthew G Knepley - dm - The DM
1355f1ad066SMatthew G Knepley 
1365f1ad066SMatthew G Knepley   Level: intermediate
1375f1ad066SMatthew G Knepley 
1385f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
1395f1ad066SMatthew G Knepley @*/
1405f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
1415f1ad066SMatthew G Knepley {
1425f1ad066SMatthew G Knepley   PetscErrorCode ierr;
1435f1ad066SMatthew G Knepley 
1445f1ad066SMatthew G Knepley   PetscFunctionBegin;
1455f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1465f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
1475f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
1485f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
1495f1ad066SMatthew G Knepley }
1505f1ad066SMatthew G Knepley 
1515f1ad066SMatthew G Knepley #undef __FUNCT__
152521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
153521d9a4cSLisandro Dalcin /*@C
154521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
155521d9a4cSLisandro Dalcin 
156521d9a4cSLisandro Dalcin    Logically Collective on DM
157521d9a4cSLisandro Dalcin 
158521d9a4cSLisandro Dalcin    Input Parameter:
159521d9a4cSLisandro Dalcin +  dm - the DM context
160521d9a4cSLisandro Dalcin .  ctype - the matrix type
161521d9a4cSLisandro Dalcin 
162521d9a4cSLisandro Dalcin    Options Database:
163521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
164521d9a4cSLisandro Dalcin 
165521d9a4cSLisandro Dalcin    Level: intermediate
166521d9a4cSLisandro Dalcin 
167521d9a4cSLisandro Dalcin .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType
168521d9a4cSLisandro Dalcin @*/
16919fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
170521d9a4cSLisandro Dalcin {
171521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
172521d9a4cSLisandro Dalcin   PetscFunctionBegin;
173521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
174521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
17519fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
176521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
177521d9a4cSLisandro Dalcin }
178521d9a4cSLisandro Dalcin 
179521d9a4cSLisandro Dalcin #undef __FUNCT__
180*c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
181*c688c046SMatthew G Knepley /*@
182*c688c046SMatthew G Knepley   MatSetDM - Gets the DM defining the data layout of the matrix
183*c688c046SMatthew G Knepley 
184*c688c046SMatthew G Knepley   Not collective
185*c688c046SMatthew G Knepley 
186*c688c046SMatthew G Knepley   Input Parameter:
187*c688c046SMatthew G Knepley . A - The Mat
188*c688c046SMatthew G Knepley 
189*c688c046SMatthew G Knepley   Output Parameter:
190*c688c046SMatthew G Knepley . dm - The DM
191*c688c046SMatthew G Knepley 
192*c688c046SMatthew G Knepley   Level: intermediate
193*c688c046SMatthew G Knepley 
194*c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
195*c688c046SMatthew G Knepley @*/
196*c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
197*c688c046SMatthew G Knepley {
198*c688c046SMatthew G Knepley   PetscErrorCode ierr;
199*c688c046SMatthew G Knepley 
200*c688c046SMatthew G Knepley   PetscFunctionBegin;
201*c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
202*c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
203*c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject *) dm);CHKERRQ(ierr);
204*c688c046SMatthew G Knepley   PetscFunctionReturn(0);
205*c688c046SMatthew G Knepley }
206*c688c046SMatthew G Knepley 
207*c688c046SMatthew G Knepley #undef __FUNCT__
208*c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
209*c688c046SMatthew G Knepley /*@
210*c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
211*c688c046SMatthew G Knepley 
212*c688c046SMatthew G Knepley   Not collective
213*c688c046SMatthew G Knepley 
214*c688c046SMatthew G Knepley   Input Parameters:
215*c688c046SMatthew G Knepley + A - The Mat
216*c688c046SMatthew G Knepley - dm - The DM
217*c688c046SMatthew G Knepley 
218*c688c046SMatthew G Knepley   Level: intermediate
219*c688c046SMatthew G Knepley 
220*c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
221*c688c046SMatthew G Knepley @*/
222*c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
223*c688c046SMatthew G Knepley {
224*c688c046SMatthew G Knepley   PetscErrorCode ierr;
225*c688c046SMatthew G Knepley 
226*c688c046SMatthew G Knepley   PetscFunctionBegin;
227*c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
228*c688c046SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
229*c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
230*c688c046SMatthew G Knepley   PetscFunctionReturn(0);
231*c688c046SMatthew G Knepley }
232*c688c046SMatthew G Knepley 
233*c688c046SMatthew G Knepley #undef __FUNCT__
2349a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
2359a42bb27SBarry Smith /*@C
2369a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
237aa219208SBarry Smith    DMDA options in the database.
2389a42bb27SBarry Smith 
239aa219208SBarry Smith    Logically Collective on DMDA
2409a42bb27SBarry Smith 
2419a42bb27SBarry Smith    Input Parameter:
242aa219208SBarry Smith +  da - the DMDA context
2439a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
2449a42bb27SBarry Smith 
2459a42bb27SBarry Smith    Notes:
2469a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2479a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2489a42bb27SBarry Smith 
2499a42bb27SBarry Smith    Level: advanced
2509a42bb27SBarry Smith 
251aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
2529a42bb27SBarry Smith 
2539a42bb27SBarry Smith .seealso: DMSetFromOptions()
2549a42bb27SBarry Smith @*/
2557087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
2569a42bb27SBarry Smith {
2579a42bb27SBarry Smith   PetscErrorCode ierr;
2589a42bb27SBarry Smith 
2599a42bb27SBarry Smith   PetscFunctionBegin;
2609a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2619a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
2629a42bb27SBarry Smith   PetscFunctionReturn(0);
2639a42bb27SBarry Smith }
2649a42bb27SBarry Smith 
2659a42bb27SBarry Smith #undef __FUNCT__
26647c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
26747c6ae99SBarry Smith /*@
268aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
26947c6ae99SBarry Smith 
27047c6ae99SBarry Smith     Collective on DM
27147c6ae99SBarry Smith 
27247c6ae99SBarry Smith     Input Parameter:
27347c6ae99SBarry Smith .   dm - the DM object to destroy
27447c6ae99SBarry Smith 
27547c6ae99SBarry Smith     Level: developer
27647c6ae99SBarry Smith 
277e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
27847c6ae99SBarry Smith 
27947c6ae99SBarry Smith @*/
280fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
28147c6ae99SBarry Smith {
282af122d2aSMatthew G Knepley   PetscInt       i, cnt = 0, f;
283dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
28447c6ae99SBarry Smith   PetscErrorCode ierr;
28547c6ae99SBarry Smith 
28647c6ae99SBarry Smith   PetscFunctionBegin;
2876bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
2886bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
28987e657c6SBarry Smith 
29087e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
291732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
2926bf464f9SBarry Smith     if ((*dm)->localin[i])  {cnt++;}
2936bf464f9SBarry Smith     if ((*dm)->globalin[i]) {cnt++;}
294732e2eb9SMatthew G Knepley   }
295dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
29671cd77b2SBarry Smith   if ((*dm)->x) {
297*c688c046SMatthew G Knepley     DM obj;
298*c688c046SMatthew G Knepley     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
299*c688c046SMatthew G Knepley     if (obj == *dm) cnt++;
30071cd77b2SBarry Smith   }
301732e2eb9SMatthew G Knepley 
3026bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
303732e2eb9SMatthew G Knepley   /*
304732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
305732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
306732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
307732e2eb9SMatthew G Knepley   */
3086bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
3096bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
310732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
3116bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
3126bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
313732e2eb9SMatthew G Knepley   }
314dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */
315dfe15315SJed Brown     nnext = nlink->next;
316dfe15315SJed 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);
317dfe15315SJed Brown     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
318dfe15315SJed Brown     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
319dfe15315SJed Brown     ierr = PetscFree(nlink);CHKERRQ(ierr);
320dfe15315SJed Brown   }
321dfe15315SJed Brown   (*dm)->namedglobal = PETSC_NULL;
3221a266240SBarry Smith 
323b17ce1afSJed Brown   /* Destroy the list of hooks */
324c833c3b5SJed Brown   {
325c833c3b5SJed Brown     DMCoarsenHookLink link,next;
326b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
327b17ce1afSJed Brown       next = link->next;
328b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
329b17ce1afSJed Brown     }
330b17ce1afSJed Brown     (*dm)->coarsenhook = PETSC_NULL;
331c833c3b5SJed Brown   }
332c833c3b5SJed Brown   {
333c833c3b5SJed Brown     DMRefineHookLink link,next;
334c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
335c833c3b5SJed Brown       next = link->next;
336c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
337c833c3b5SJed Brown     }
338c833c3b5SJed Brown     (*dm)->refinehook = PETSC_NULL;
339c833c3b5SJed Brown   }
340aa1993deSMatthew G Knepley   /* Destroy the work arrays */
341aa1993deSMatthew G Knepley   {
342aa1993deSMatthew G Knepley     DMWorkLink link,next;
343aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
344aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
345aa1993deSMatthew G Knepley       next = link->next;
346aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
347aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
348aa1993deSMatthew G Knepley     }
349aa1993deSMatthew G Knepley     (*dm)->workin = PETSC_NULL;
350aa1993deSMatthew G Knepley   }
351b17ce1afSJed Brown 
3521a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
3531a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
3541a266240SBarry Smith   }
35587e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
35671cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
3574dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
3586bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
3596bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
3606bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
361073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
36288ed4aceSMatthew G Knepley 
36388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
36488ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);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;
51847c6ae99SBarry Smith 
51947c6ae99SBarry Smith   PetscFunctionBegin;
520171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5213014e516SBarry Smith  if (!v) {
5223014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
5233014e516SBarry Smith   }
5240c010503SBarry Smith   if (dm->ops->view) {
5250c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
52647c6ae99SBarry Smith   }
52747c6ae99SBarry Smith   PetscFunctionReturn(0);
52847c6ae99SBarry Smith }
52947c6ae99SBarry Smith 
53047c6ae99SBarry Smith #undef __FUNCT__
53147c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
53247c6ae99SBarry Smith /*@
533aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
53447c6ae99SBarry Smith 
53547c6ae99SBarry Smith     Collective on DM
53647c6ae99SBarry Smith 
53747c6ae99SBarry Smith     Input Parameter:
53847c6ae99SBarry Smith .   dm - the DM object
53947c6ae99SBarry Smith 
54047c6ae99SBarry Smith     Output Parameter:
54147c6ae99SBarry Smith .   vec - the global vector
54247c6ae99SBarry Smith 
543073dac72SJed Brown     Level: beginner
54447c6ae99SBarry Smith 
545e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
54647c6ae99SBarry Smith 
54747c6ae99SBarry Smith @*/
5487087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
54947c6ae99SBarry Smith {
55047c6ae99SBarry Smith   PetscErrorCode ierr;
55147c6ae99SBarry Smith 
55247c6ae99SBarry Smith   PetscFunctionBegin;
553171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55488ed4aceSMatthew G Knepley   if (dm->defaultSection) {
55588ed4aceSMatthew G Knepley     PetscSection gSection;
5561f588964SMatthew G Knepley     PetscInt     localSize, blockSize = -1, pStart, pEnd, p;
55788ed4aceSMatthew G Knepley 
55888ed4aceSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
5591f588964SMatthew G Knepley     ierr = PetscSectionGetChart(dm->defaultSection, &pStart, &pEnd);CHKERRQ(ierr);
5601f588964SMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
5611f588964SMatthew G Knepley       PetscInt dof, cdof;
5621f588964SMatthew G Knepley 
5631f588964SMatthew G Knepley       ierr = PetscSectionGetDof(dm->defaultSection, p, &dof);CHKERRQ(ierr);
5641f588964SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(dm->defaultSection, p, &cdof);CHKERRQ(ierr);
5651f588964SMatthew G Knepley       if ((blockSize < 0) && (dof > 0)) blockSize = dof-cdof;
5661f588964SMatthew G Knepley       if ((dof > 0) && (dof-cdof != blockSize)) {
5671f588964SMatthew G Knepley         blockSize = 1;
5681f588964SMatthew G Knepley         break;
5691f588964SMatthew G Knepley       }
5701f588964SMatthew G Knepley     }
57188ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstrainedStorageSize(dm->defaultGlobalSection, &localSize);CHKERRQ(ierr);
5721f588964SMatthew G Knepley     if (localSize%blockSize) SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONG, "Mismatch between blocksize %d and local storage size %d", blockSize, localSize);
57388ed4aceSMatthew G Knepley     ierr = VecCreate(((PetscObject) dm)->comm, vec);CHKERRQ(ierr);
57488ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, PETSC_DETERMINE);CHKERRQ(ierr);
5751f588964SMatthew G Knepley     ierr = VecSetBlockSize(*vec, blockSize);CHKERRQ(ierr);
57688ed4aceSMatthew G Knepley     /* ierr = VecSetType(*vec, dm->vectype);CHKERRQ(ierr); */
57788ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
578*c688c046SMatthew G Knepley     ierr = VecSetDM(*vec, dm);CHKERRQ(ierr);
57988ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMapping(*vec, dm->ltogmap);CHKERRQ(ierr); */
58088ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMappingBlock(*vec, dm->ltogmapb);CHKERRQ(ierr); */
58188ed4aceSMatthew G Knepley     /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
58288ed4aceSMatthew G Knepley   } else {
58347c6ae99SBarry Smith     ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
58488ed4aceSMatthew G Knepley   }
58547c6ae99SBarry Smith   PetscFunctionReturn(0);
58647c6ae99SBarry Smith }
58747c6ae99SBarry Smith 
58847c6ae99SBarry Smith #undef __FUNCT__
58947c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
59047c6ae99SBarry Smith /*@
591aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
59247c6ae99SBarry Smith 
59347c6ae99SBarry Smith     Not Collective
59447c6ae99SBarry Smith 
59547c6ae99SBarry Smith     Input Parameter:
59647c6ae99SBarry Smith .   dm - the DM object
59747c6ae99SBarry Smith 
59847c6ae99SBarry Smith     Output Parameter:
59947c6ae99SBarry Smith .   vec - the local vector
60047c6ae99SBarry Smith 
601073dac72SJed Brown     Level: beginner
60247c6ae99SBarry Smith 
603e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
60447c6ae99SBarry Smith 
60547c6ae99SBarry Smith @*/
6067087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
60747c6ae99SBarry Smith {
60847c6ae99SBarry Smith   PetscErrorCode ierr;
60947c6ae99SBarry Smith 
61047c6ae99SBarry Smith   PetscFunctionBegin;
611171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61288ed4aceSMatthew G Knepley   if (dm->defaultSection) {
6131f588964SMatthew G Knepley     PetscInt localSize, blockSize = -1, pStart, pEnd, p;
61488ed4aceSMatthew G Knepley 
6151f588964SMatthew G Knepley     ierr = PetscSectionGetChart(dm->defaultSection, &pStart, &pEnd);CHKERRQ(ierr);
6161f588964SMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
6171f588964SMatthew G Knepley       PetscInt dof;
6181f588964SMatthew G Knepley 
6191f588964SMatthew G Knepley       ierr = PetscSectionGetDof(dm->defaultSection, p, &dof);CHKERRQ(ierr);
6201f588964SMatthew G Knepley       if ((blockSize < 0) && (dof > 0)) blockSize = dof;
6211f588964SMatthew G Knepley       if ((dof > 0) && (dof != blockSize)) {
6221f588964SMatthew G Knepley         blockSize = 1;
6231f588964SMatthew G Knepley         break;
6241f588964SMatthew G Knepley       }
6251f588964SMatthew G Knepley     }
62688ed4aceSMatthew G Knepley     ierr = PetscSectionGetStorageSize(dm->defaultSection, &localSize);CHKERRQ(ierr);
62788ed4aceSMatthew G Knepley     ierr = VecCreate(PETSC_COMM_SELF, vec);CHKERRQ(ierr);
62888ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, localSize);CHKERRQ(ierr);
6291f588964SMatthew G Knepley     ierr = VecSetBlockSize(*vec, blockSize);CHKERRQ(ierr);
63088ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
631*c688c046SMatthew G Knepley     ierr = VecSetDM(*vec, dm);CHKERRQ(ierr);
63288ed4aceSMatthew G Knepley   } else {
63347c6ae99SBarry Smith     ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
63488ed4aceSMatthew G Knepley   }
63547c6ae99SBarry Smith   PetscFunctionReturn(0);
63647c6ae99SBarry Smith }
63747c6ae99SBarry Smith 
63847c6ae99SBarry Smith #undef __FUNCT__
6391411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
6401411c6eeSJed Brown /*@
6411411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
6421411c6eeSJed Brown 
6431411c6eeSJed Brown    Collective on DM
6441411c6eeSJed Brown 
6451411c6eeSJed Brown    Input Parameter:
6461411c6eeSJed Brown .  dm - the DM that provides the mapping
6471411c6eeSJed Brown 
6481411c6eeSJed Brown    Output Parameter:
6491411c6eeSJed Brown .  ltog - the mapping
6501411c6eeSJed Brown 
6511411c6eeSJed Brown    Level: intermediate
6521411c6eeSJed Brown 
6531411c6eeSJed Brown    Notes:
6541411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
6551411c6eeSJed Brown    MatSetLocalToGlobalMapping().
6561411c6eeSJed Brown 
6571411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
6581411c6eeSJed Brown @*/
6597087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
6601411c6eeSJed Brown {
6611411c6eeSJed Brown   PetscErrorCode ierr;
6621411c6eeSJed Brown 
6631411c6eeSJed Brown   PetscFunctionBegin;
6641411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6651411c6eeSJed Brown   PetscValidPointer(ltog,2);
6661411c6eeSJed Brown   if (!dm->ltogmap) {
66737d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
66837d0c07bSMatthew G Knepley 
66937d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
67037d0c07bSMatthew G Knepley     if (section) {
67137d0c07bSMatthew G Knepley       PetscInt      *ltog;
67237d0c07bSMatthew G Knepley       PetscInt       pStart, pEnd, size, p, l;
67337d0c07bSMatthew G Knepley 
67437d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
67537d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
67637d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
67737d0c07bSMatthew G Knepley       ierr = PetscMalloc(size * sizeof(PetscInt), &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
67837d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
67937d0c07bSMatthew G Knepley         PetscInt dof, off, c;
68037d0c07bSMatthew G Knepley 
68137d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
68237d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
68337d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
68437d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
68537d0c07bSMatthew G Knepley           ltog[l] = off+c;
68637d0c07bSMatthew G Knepley         }
68737d0c07bSMatthew G Knepley       }
68837d0c07bSMatthew G Knepley       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
68937d0c07bSMatthew G Knepley       ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr);
69037d0c07bSMatthew G Knepley     } else {
6911411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
6921411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
6931411c6eeSJed Brown     }
69437d0c07bSMatthew G Knepley   }
6951411c6eeSJed Brown   *ltog = dm->ltogmap;
6961411c6eeSJed Brown   PetscFunctionReturn(0);
6971411c6eeSJed Brown }
6981411c6eeSJed Brown 
6991411c6eeSJed Brown #undef __FUNCT__
7001411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
7011411c6eeSJed Brown /*@
7021411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
7031411c6eeSJed Brown 
7041411c6eeSJed Brown    Collective on DM
7051411c6eeSJed Brown 
7061411c6eeSJed Brown    Input Parameter:
7071411c6eeSJed Brown .  da - the distributed array that provides the mapping
7081411c6eeSJed Brown 
7091411c6eeSJed Brown    Output Parameter:
7101411c6eeSJed Brown .  ltog - the block mapping
7111411c6eeSJed Brown 
7121411c6eeSJed Brown    Level: intermediate
7131411c6eeSJed Brown 
7141411c6eeSJed Brown    Notes:
7151411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
7161411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
7171411c6eeSJed Brown 
7181411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
7191411c6eeSJed Brown @*/
7207087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
7211411c6eeSJed Brown {
7221411c6eeSJed Brown   PetscErrorCode ierr;
7231411c6eeSJed Brown 
7241411c6eeSJed Brown   PetscFunctionBegin;
7251411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7261411c6eeSJed Brown   PetscValidPointer(ltog,2);
7271411c6eeSJed Brown   if (!dm->ltogmapb) {
7281411c6eeSJed Brown     PetscInt bs;
7291411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
7301411c6eeSJed Brown     if (bs > 1) {
7311411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
7321411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
7331411c6eeSJed Brown     } else {
7341411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
7351411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
7361411c6eeSJed Brown     }
7371411c6eeSJed Brown   }
7381411c6eeSJed Brown   *ltog = dm->ltogmapb;
7391411c6eeSJed Brown   PetscFunctionReturn(0);
7401411c6eeSJed Brown }
7411411c6eeSJed Brown 
7421411c6eeSJed Brown #undef __FUNCT__
7431411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
7441411c6eeSJed Brown /*@
7451411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
7461411c6eeSJed Brown 
7471411c6eeSJed Brown    Not Collective
7481411c6eeSJed Brown 
7491411c6eeSJed Brown    Input Parameter:
7501411c6eeSJed Brown .  dm - the DM with block structure
7511411c6eeSJed Brown 
7521411c6eeSJed Brown    Output Parameter:
7531411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
7541411c6eeSJed Brown 
7551411c6eeSJed Brown    Level: intermediate
7561411c6eeSJed Brown 
7571411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
7581411c6eeSJed Brown @*/
7597087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
7601411c6eeSJed Brown {
7611411c6eeSJed Brown   PetscFunctionBegin;
7621411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7631411c6eeSJed Brown   PetscValidPointer(bs,2);
7641411c6eeSJed 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");
7651411c6eeSJed Brown   *bs = dm->bs;
7661411c6eeSJed Brown   PetscFunctionReturn(0);
7671411c6eeSJed Brown }
7681411c6eeSJed Brown 
7691411c6eeSJed Brown #undef __FUNCT__
770e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
77147c6ae99SBarry Smith /*@
772e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
77347c6ae99SBarry Smith 
77447c6ae99SBarry Smith     Collective on DM
77547c6ae99SBarry Smith 
77647c6ae99SBarry Smith     Input Parameter:
77747c6ae99SBarry Smith +   dm1 - the DM object
77847c6ae99SBarry Smith -   dm2 - the second, finer DM object
77947c6ae99SBarry Smith 
78047c6ae99SBarry Smith     Output Parameter:
78147c6ae99SBarry Smith +  mat - the interpolation
78247c6ae99SBarry Smith -  vec - the scaling (optional)
78347c6ae99SBarry Smith 
78447c6ae99SBarry Smith     Level: developer
78547c6ae99SBarry Smith 
78685afcc9aSBarry 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
78785afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
788d52bd9f3SBarry Smith 
7891f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
790d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
79185afcc9aSBarry Smith 
79285afcc9aSBarry Smith 
793e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
79447c6ae99SBarry Smith 
79547c6ae99SBarry Smith @*/
796e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
79747c6ae99SBarry Smith {
79847c6ae99SBarry Smith   PetscErrorCode ierr;
79947c6ae99SBarry Smith 
80047c6ae99SBarry Smith   PetscFunctionBegin;
801171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
802171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
80325296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
80447c6ae99SBarry Smith   PetscFunctionReturn(0);
80547c6ae99SBarry Smith }
80647c6ae99SBarry Smith 
80747c6ae99SBarry Smith #undef __FUNCT__
808e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
80947c6ae99SBarry Smith /*@
810e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
81147c6ae99SBarry Smith 
81247c6ae99SBarry Smith     Collective on DM
81347c6ae99SBarry Smith 
81447c6ae99SBarry Smith     Input Parameter:
81547c6ae99SBarry Smith +   dm1 - the DM object
81647c6ae99SBarry Smith -   dm2 - the second, finer DM object
81747c6ae99SBarry Smith 
81847c6ae99SBarry Smith     Output Parameter:
81947c6ae99SBarry Smith .   ctx - the injection
82047c6ae99SBarry Smith 
82147c6ae99SBarry Smith     Level: developer
82247c6ae99SBarry Smith 
82385afcc9aSBarry 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
82485afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
82585afcc9aSBarry Smith 
826e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
82747c6ae99SBarry Smith 
82847c6ae99SBarry Smith @*/
829e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
83047c6ae99SBarry Smith {
83147c6ae99SBarry Smith   PetscErrorCode ierr;
83247c6ae99SBarry Smith 
83347c6ae99SBarry Smith   PetscFunctionBegin;
834171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
835171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
83647c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
83747c6ae99SBarry Smith   PetscFunctionReturn(0);
83847c6ae99SBarry Smith }
83947c6ae99SBarry Smith 
84047c6ae99SBarry Smith #undef __FUNCT__
841e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
842d1e2c406SBarry Smith /*@C
843e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
84447c6ae99SBarry Smith 
84547c6ae99SBarry Smith     Collective on DM
84647c6ae99SBarry Smith 
84747c6ae99SBarry Smith     Input Parameter:
84847c6ae99SBarry Smith +   dm - the DM object
84947c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
85047c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
85147c6ae99SBarry Smith 
85247c6ae99SBarry Smith     Output Parameter:
85347c6ae99SBarry Smith .   coloring - the coloring
85447c6ae99SBarry Smith 
85547c6ae99SBarry Smith     Level: developer
85647c6ae99SBarry Smith 
857e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
85847c6ae99SBarry Smith 
859aab9d709SJed Brown @*/
86019fd82e9SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,MatType mtype,ISColoring *coloring)
86147c6ae99SBarry Smith {
86247c6ae99SBarry Smith   PetscErrorCode ierr;
86347c6ae99SBarry Smith 
86447c6ae99SBarry Smith   PetscFunctionBegin;
865171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
86647c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
86747c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
86847c6ae99SBarry Smith   PetscFunctionReturn(0);
86947c6ae99SBarry Smith }
87047c6ae99SBarry Smith 
87147c6ae99SBarry Smith #undef __FUNCT__
872950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
87347c6ae99SBarry Smith /*@C
874950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
87547c6ae99SBarry Smith 
87647c6ae99SBarry Smith     Collective on DM
87747c6ae99SBarry Smith 
87847c6ae99SBarry Smith     Input Parameter:
87947c6ae99SBarry Smith +   dm - the DM object
88047c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
88194013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
88247c6ae99SBarry Smith 
88347c6ae99SBarry Smith     Output Parameter:
88447c6ae99SBarry Smith .   mat - the empty Jacobian
88547c6ae99SBarry Smith 
886073dac72SJed Brown     Level: beginner
88747c6ae99SBarry Smith 
88894013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
88994013140SBarry Smith        do not need to do it yourself.
89094013140SBarry Smith 
89194013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
892aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
89394013140SBarry Smith 
89494013140SBarry 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
89594013140SBarry Smith        internally by PETSc.
89694013140SBarry Smith 
89794013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
898aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
89994013140SBarry Smith 
900e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
90147c6ae99SBarry Smith 
902aab9d709SJed Brown @*/
90319fd82e9SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,MatType mtype,Mat *mat)
90447c6ae99SBarry Smith {
90547c6ae99SBarry Smith   PetscErrorCode ierr;
90647c6ae99SBarry Smith 
90747c6ae99SBarry Smith   PetscFunctionBegin;
908171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
909235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
910235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
911235683edSBarry Smith #endif
912c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
913c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
914073dac72SJed Brown   if (dm->mattype) {
91525296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
916073dac72SJed Brown   } else {
91725296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
918c7b7c8a4SJed Brown   }
91947c6ae99SBarry Smith   PetscFunctionReturn(0);
92047c6ae99SBarry Smith }
92147c6ae99SBarry Smith 
92247c6ae99SBarry Smith #undef __FUNCT__
923732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
924732e2eb9SMatthew G Knepley /*@
925950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
926732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
927732e2eb9SMatthew G Knepley 
928732e2eb9SMatthew G Knepley   Logically Collective on DMDA
929732e2eb9SMatthew G Knepley 
930732e2eb9SMatthew G Knepley   Input Parameter:
931732e2eb9SMatthew G Knepley + dm - the DM
932732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
933732e2eb9SMatthew G Knepley 
934732e2eb9SMatthew G Knepley   Level: developer
935950540a4SJed Brown .seealso DMCreateMatrix()
936732e2eb9SMatthew G Knepley @*/
937732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
938732e2eb9SMatthew G Knepley {
939732e2eb9SMatthew G Knepley   PetscFunctionBegin;
940732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
941732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
942732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
943732e2eb9SMatthew G Knepley }
944732e2eb9SMatthew G Knepley 
945732e2eb9SMatthew G Knepley #undef __FUNCT__
946a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
947a89ea682SMatthew G Knepley /*@C
948aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
949a89ea682SMatthew G Knepley 
950a89ea682SMatthew G Knepley   Not Collective
951a89ea682SMatthew G Knepley 
952a89ea682SMatthew G Knepley   Input Parameters:
953a89ea682SMatthew G Knepley + dm - the DM object
954aa1993deSMatthew G Knepley . count - The minium size
955aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
956a89ea682SMatthew G Knepley 
957a89ea682SMatthew G Knepley   Output Parameter:
958a89ea682SMatthew G Knepley . array - the work array
959a89ea682SMatthew G Knepley 
960a89ea682SMatthew G Knepley   Level: developer
961a89ea682SMatthew G Knepley 
962a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
963a89ea682SMatthew G Knepley @*/
964aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
965a89ea682SMatthew G Knepley {
966a89ea682SMatthew G Knepley   PetscErrorCode ierr;
967aa1993deSMatthew G Knepley   DMWorkLink link;
968aa1993deSMatthew G Knepley   size_t size;
969a89ea682SMatthew G Knepley 
970a89ea682SMatthew G Knepley   PetscFunctionBegin;
971a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
972aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
973aa1993deSMatthew G Knepley   if (dm->workin) {
974aa1993deSMatthew G Knepley     link = dm->workin;
975aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
976aa1993deSMatthew G Knepley   } else {
977aa1993deSMatthew G Knepley     ierr = PetscNewLog(dm,struct _DMWorkLink,&link);CHKERRQ(ierr);
978a89ea682SMatthew G Knepley   }
979aa1993deSMatthew G Knepley   ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr);
980aa1993deSMatthew G Knepley   if (size*count > link->bytes) {
981aa1993deSMatthew G Knepley     ierr = PetscFree(link->mem);CHKERRQ(ierr);
982aa1993deSMatthew G Knepley     ierr = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr);
983aa1993deSMatthew G Knepley     link->bytes = size*count;
984aa1993deSMatthew G Knepley   }
985aa1993deSMatthew G Knepley   link->next = dm->workout;
986aa1993deSMatthew G Knepley   dm->workout = link;
987aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
988a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
989a89ea682SMatthew G Knepley }
990a89ea682SMatthew G Knepley 
991aa1993deSMatthew G Knepley #undef __FUNCT__
992aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
993aa1993deSMatthew G Knepley /*@C
994aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
995aa1993deSMatthew G Knepley 
996aa1993deSMatthew G Knepley   Not Collective
997aa1993deSMatthew G Knepley 
998aa1993deSMatthew G Knepley   Input Parameters:
999aa1993deSMatthew G Knepley + dm - the DM object
1000aa1993deSMatthew G Knepley . count - The minium size
1001aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1002aa1993deSMatthew G Knepley 
1003aa1993deSMatthew G Knepley   Output Parameter:
1004aa1993deSMatthew G Knepley . array - the work array
1005aa1993deSMatthew G Knepley 
1006aa1993deSMatthew G Knepley   Level: developer
1007aa1993deSMatthew G Knepley 
1008aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1009aa1993deSMatthew G Knepley @*/
1010aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1011aa1993deSMatthew G Knepley {
1012aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1013aa1993deSMatthew G Knepley 
1014aa1993deSMatthew G Knepley   PetscFunctionBegin;
1015aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1016aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1017aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1018aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1019aa1993deSMatthew G Knepley       *p = link->next;
1020aa1993deSMatthew G Knepley       link->next = dm->workin;
1021aa1993deSMatthew G Knepley       dm->workin = link;
1022aa1993deSMatthew G Knepley       *(void**)mem = PETSC_NULL;
1023aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1024aa1993deSMatthew G Knepley     }
1025aa1993deSMatthew G Knepley   }
1026aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1027aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
1028aa1993deSMatthew G Knepley }
1029e7c4fc90SDmitry Karpeev 
1030e7c4fc90SDmitry Karpeev #undef __FUNCT__
1031435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1032435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1033435a35e8SMatthew G Knepley {
1034435a35e8SMatthew G Knepley   PetscFunctionBegin;
1035435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1036435a35e8SMatthew G Knepley   if (field >= 10) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1037435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1038435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1039435a35e8SMatthew G Knepley }
1040435a35e8SMatthew G Knepley 
1041435a35e8SMatthew G Knepley #undef __FUNCT__
10424d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
10434f3b5142SJed Brown /*@C
10444d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
10454d343eeaSMatthew G Knepley 
10464d343eeaSMatthew G Knepley   Not collective
10474d343eeaSMatthew G Knepley 
10484d343eeaSMatthew G Knepley   Input Parameter:
10494d343eeaSMatthew G Knepley . dm - the DM object
10504d343eeaSMatthew G Knepley 
10514d343eeaSMatthew G Knepley   Output Parameters:
105221c9b008SJed Brown + numFields  - The number of fields (or PETSC_NULL if not requested)
105337d0c07bSMatthew G Knepley . fieldNames - The name for each field (or PETSC_NULL if not requested)
105421c9b008SJed Brown - fields     - The global indices for each field (or PETSC_NULL if not requested)
10554d343eeaSMatthew G Knepley 
10564d343eeaSMatthew G Knepley   Level: intermediate
10574d343eeaSMatthew G Knepley 
105821c9b008SJed Brown   Notes:
105921c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
106021c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
106121c9b008SJed Brown   PetscFree().
106221c9b008SJed Brown 
10634d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
10644d343eeaSMatthew G Knepley @*/
106537d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
10664d343eeaSMatthew G Knepley {
106737d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
10684d343eeaSMatthew G Knepley   PetscErrorCode ierr;
10694d343eeaSMatthew G Knepley 
10704d343eeaSMatthew G Knepley   PetscFunctionBegin;
10714d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
107269ca1f37SDmitry Karpeev   if (numFields) {
107369ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
107469ca1f37SDmitry Karpeev     *numFields = 0;
107569ca1f37SDmitry Karpeev   }
107637d0c07bSMatthew G Knepley   if (fieldNames) {
107737d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
107837d0c07bSMatthew G Knepley     *fieldNames = PETSC_NULL;
107969ca1f37SDmitry Karpeev   }
108069ca1f37SDmitry Karpeev   if (fields) {
108169ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
108269ca1f37SDmitry Karpeev     *fields = PETSC_NULL;
108369ca1f37SDmitry Karpeev   }
108437d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
108537d0c07bSMatthew G Knepley   if (section) {
108637d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
108737d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
108837d0c07bSMatthew G Knepley 
108937d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
109037d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
109137d0c07bSMatthew G Knepley     ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt *,&fieldIndices);CHKERRQ(ierr);
109237d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
109337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
109437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
109537d0c07bSMatthew G Knepley     }
109637d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
109737d0c07bSMatthew G Knepley       PetscInt gdof;
109837d0c07bSMatthew G Knepley 
109937d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
110037d0c07bSMatthew G Knepley       if (gdof > 0) {
110137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
110237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
110337d0c07bSMatthew G Knepley 
110437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
110537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
110637d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
110737d0c07bSMatthew G Knepley         }
110837d0c07bSMatthew G Knepley       }
110937d0c07bSMatthew G Knepley     }
111037d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
111137d0c07bSMatthew G Knepley       ierr = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr);
111237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
111337d0c07bSMatthew G Knepley     }
111437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
111537d0c07bSMatthew G Knepley       PetscInt gdof, goff;
111637d0c07bSMatthew G Knepley 
111737d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
111837d0c07bSMatthew G Knepley       if (gdof > 0) {
111937d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
112037d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
112137d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
112237d0c07bSMatthew G Knepley 
112337d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
112437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
112537d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
112637d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
112737d0c07bSMatthew G Knepley           }
112837d0c07bSMatthew G Knepley         }
112937d0c07bSMatthew G Knepley       }
113037d0c07bSMatthew G Knepley     }
113137d0c07bSMatthew G Knepley     if (numFields) {*numFields = nF;}
113237d0c07bSMatthew G Knepley     if (fieldNames) {
113337d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(char *), fieldNames);CHKERRQ(ierr);
113437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
113537d0c07bSMatthew G Knepley         const char *fieldName;
113637d0c07bSMatthew G Knepley 
113737d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
113837d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*fieldNames)[f]);CHKERRQ(ierr);
113937d0c07bSMatthew G Knepley       }
114037d0c07bSMatthew G Knepley     }
114137d0c07bSMatthew G Knepley     if (fields) {
114237d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr);
114337d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
114437d0c07bSMatthew G Knepley         ierr = ISCreateGeneral(((PetscObject) dm)->comm, fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
114537d0c07bSMatthew G Knepley       }
114637d0c07bSMatthew G Knepley     }
114737d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
114837d0c07bSMatthew G Knepley   } else {
114937d0c07bSMatthew G Knepley     if (dm->ops->createfieldis) {ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);}
115069ca1f37SDmitry Karpeev   }
11514d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
11524d343eeaSMatthew G Knepley }
11534d343eeaSMatthew G Knepley 
1154a89ea682SMatthew G Knepley #undef __FUNCT__
115516621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecompositionDM"
1156e7c4fc90SDmitry Karpeev /*@C
115716621825SDmitry Karpeev   DMCreateFieldDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into fields.
115816621825SDmitry Karpeev 
115916621825SDmitry Karpeev   Not Collective
116016621825SDmitry Karpeev 
116116621825SDmitry Karpeev   Input Parameters:
116216621825SDmitry Karpeev + dm   - the DM object
116316621825SDmitry Karpeev - name - the name of the field decomposition
116416621825SDmitry Karpeev 
116516621825SDmitry Karpeev   Output Parameter:
116616621825SDmitry Karpeev . ddm  - the field decomposition DM (PETSC_NULL, if no such decomposition is known)
116716621825SDmitry Karpeev 
116816621825SDmitry Karpeev   Level: advanced
116916621825SDmitry Karpeev 
117016621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
117116621825SDmitry Karpeev @*/
117216621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecompositionDM(DM dm, const char* name, DM *ddm)
117316621825SDmitry Karpeev {
117416621825SDmitry Karpeev   PetscErrorCode ierr;
117516621825SDmitry Karpeev 
117616621825SDmitry Karpeev   PetscFunctionBegin;
117716621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
117816621825SDmitry Karpeev   PetscValidCharPointer(name,2);
117916621825SDmitry Karpeev   PetscValidPointer(ddm,3);
118016621825SDmitry Karpeev   *ddm = PETSC_NULL;
1181731c8d9eSDmitry Karpeev   if (dm->ops->createfielddecompositiondm) {
118216621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
118316621825SDmitry Karpeev   }
118416621825SDmitry Karpeev   PetscFunctionReturn(0);
118516621825SDmitry Karpeev }
118616621825SDmitry Karpeev 
118716621825SDmitry Karpeev #undef __FUNCT__
118816621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
118916621825SDmitry Karpeev /*@C
119016621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
119116621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
119216621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1193e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1194e7c4fc90SDmitry Karpeev 
1195e7c4fc90SDmitry Karpeev   Not collective
1196e7c4fc90SDmitry Karpeev 
1197e7c4fc90SDmitry Karpeev   Input Parameter:
1198e7c4fc90SDmitry Karpeev . dm - the DM object
1199e7c4fc90SDmitry Karpeev 
1200e7c4fc90SDmitry Karpeev   Output Parameters:
120116621825SDmitry Karpeev + len       - The number of subproblems in the field decomposition (or PETSC_NULL if not requested)
120216621825SDmitry Karpeev . namelist  - The name for each field (or PETSC_NULL if not requested)
120316621825SDmitry Karpeev . islist    - The global indices for each field (or PETSC_NULL if not requested)
120416621825SDmitry Karpeev - dmlist    - The DMs for each field subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
1205e7c4fc90SDmitry Karpeev 
1206e7c4fc90SDmitry Karpeev   Level: intermediate
1207e7c4fc90SDmitry Karpeev 
1208e7c4fc90SDmitry Karpeev   Notes:
1209e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1210e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1211e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1212e7c4fc90SDmitry Karpeev 
1213e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1214e7c4fc90SDmitry Karpeev @*/
121516621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1216e7c4fc90SDmitry Karpeev {
1217e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1218e7c4fc90SDmitry Karpeev 
1219e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1220e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1221731c8d9eSDmitry Karpeev   if (len)      {PetscValidPointer(len,2);      *len      = 0;}
1222731c8d9eSDmitry Karpeev   if (namelist) {PetscValidPointer(namelist,3); *namelist = 0;}
1223731c8d9eSDmitry Karpeev   if (islist)   {PetscValidPointer(islist,4);   *islist   = 0;}
1224731c8d9eSDmitry Karpeev   if (dmlist)   {PetscValidPointer(dmlist,5);   *dmlist   = 0;}
122516621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1226435a35e8SMatthew G Knepley     PetscSection section;
1227435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1228435a35e8SMatthew G Knepley 
1229435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1230435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1231435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1232435a35e8SMatthew G Knepley       *len = numFields;
1233435a35e8SMatthew G Knepley       ierr = PetscMalloc3(numFields,char*,namelist,numFields,IS,islist,numFields,DM,dmlist);CHKERRQ(ierr);
1234435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1235435a35e8SMatthew G Knepley         const char *fieldName;
1236435a35e8SMatthew G Knepley 
1237435a35e8SMatthew G Knepley         ierr = DMCreateSubDM(dm, 1, &f, &(*islist)[f], &(*dmlist)[f]);CHKERRQ(ierr);
1238435a35e8SMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1239435a35e8SMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*namelist)[f]);CHKERRQ(ierr);
1240435a35e8SMatthew G Knepley       }
1241435a35e8SMatthew G Knepley     } else {
124269ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1243e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
1244e7c4fc90SDmitry Karpeev       if (dmlist) *dmlist = PETSC_NULL;
1245e7c4fc90SDmitry Karpeev     }
1246435a35e8SMatthew G Knepley   }
1247e7c4fc90SDmitry Karpeev   else {
124816621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist); CHKERRQ(ierr);
124916621825SDmitry Karpeev   }
125016621825SDmitry Karpeev   PetscFunctionReturn(0);
125116621825SDmitry Karpeev }
125216621825SDmitry Karpeev 
125316621825SDmitry Karpeev #undef __FUNCT__
1254435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1255435a35e8SMatthew G Knepley /*@C
1256435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1257435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1258435a35e8SMatthew G Knepley 
1259435a35e8SMatthew G Knepley   Not collective
1260435a35e8SMatthew G Knepley 
1261435a35e8SMatthew G Knepley   Input Parameters:
1262435a35e8SMatthew G Knepley + dm - the DM object
1263435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
1264435a35e8SMatthew G Knepley - len       - The number of subproblems in the decomposition (or PETSC_NULL if not requested)
1265435a35e8SMatthew G Knepley 
1266435a35e8SMatthew G Knepley   Output Parameters:
1267435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1268435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1269435a35e8SMatthew G Knepley 
1270435a35e8SMatthew G Knepley   Level: intermediate
1271435a35e8SMatthew G Knepley 
1272435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1273435a35e8SMatthew G Knepley @*/
1274435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1275435a35e8SMatthew G Knepley {
1276435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1277435a35e8SMatthew G Knepley 
1278435a35e8SMatthew G Knepley   PetscFunctionBegin;
1279435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1280435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
1281435a35e8SMatthew G Knepley   if (is) {PetscValidPointer(is,4);}
1282435a35e8SMatthew G Knepley   if (subdm) {PetscValidPointer(subdm,5);}
1283435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1284435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm); CHKERRQ(ierr);
1285435a35e8SMatthew G Knepley   } else SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1286435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1287435a35e8SMatthew G Knepley }
1288435a35e8SMatthew G Knepley 
1289435a35e8SMatthew G Knepley #undef __FUNCT__
129016621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecompositionDM"
129116621825SDmitry Karpeev /*@C
129216621825SDmitry Karpeev   DMCreateDomainDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into subdomains.
129316621825SDmitry Karpeev 
129416621825SDmitry Karpeev   Not Collective
129516621825SDmitry Karpeev 
129616621825SDmitry Karpeev   Input Parameters:
129716621825SDmitry Karpeev + dm   - the DM object
129816621825SDmitry Karpeev - name - the name of the subdomain decomposition
129916621825SDmitry Karpeev 
130016621825SDmitry Karpeev   Output Parameter:
130116621825SDmitry Karpeev . ddm  - the subdomain decomposition DM (PETSC_NULL, if no such decomposition is known)
130216621825SDmitry Karpeev 
130316621825SDmitry Karpeev   Level: advanced
130416621825SDmitry Karpeev 
130516621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
130616621825SDmitry Karpeev @*/
130716621825SDmitry Karpeev PetscErrorCode DMCreateDomainDecompositionDM(DM dm, const char* name, DM *ddm)
130816621825SDmitry Karpeev {
130916621825SDmitry Karpeev   PetscErrorCode ierr;
131016621825SDmitry Karpeev 
131116621825SDmitry Karpeev   PetscFunctionBegin;
131216621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
131316621825SDmitry Karpeev   PetscValidCharPointer(name,2);
131416621825SDmitry Karpeev   PetscValidPointer(ddm,3);
131516621825SDmitry Karpeev   *ddm = PETSC_NULL;
1316731c8d9eSDmitry Karpeev   if (dm->ops->createdomaindecompositiondm) {
131716621825SDmitry Karpeev     ierr = (*dm->ops->createdomaindecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
131816621825SDmitry Karpeev   }
131916621825SDmitry Karpeev   PetscFunctionReturn(0);
132016621825SDmitry Karpeev }
132116621825SDmitry Karpeev 
132216621825SDmitry Karpeev #undef __FUNCT__
132316621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
132416621825SDmitry Karpeev /*@C
13258d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
13268d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
13278d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
13288d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
13298d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
133016621825SDmitry Karpeev 
133116621825SDmitry Karpeev   Not collective
133216621825SDmitry Karpeev 
133316621825SDmitry Karpeev   Input Parameter:
133416621825SDmitry Karpeev . dm - the DM object
133516621825SDmitry Karpeev 
133616621825SDmitry Karpeev   Output Parameters:
133716621825SDmitry Karpeev + len         - The number of subproblems in the domain decomposition (or PETSC_NULL if not requested)
133816621825SDmitry Karpeev . namelist    - The name for each subdomain (or PETSC_NULL if not requested)
13398d4ac253SDmitry Karpeev . innerislist - The global indices for each inner subdomain (or PETSC_NULL, if not requested)
13408d4ac253SDmitry Karpeev . outerislist - The global indices for each outer subdomain (or PETSC_NULL, if not requested)
134116621825SDmitry Karpeev - dmlist      - The DMs for each subdomain subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
134216621825SDmitry Karpeev 
134316621825SDmitry Karpeev   Level: intermediate
134416621825SDmitry Karpeev 
134516621825SDmitry Karpeev   Notes:
134616621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
134716621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
134816621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
134916621825SDmitry Karpeev 
13508d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
135116621825SDmitry Karpeev @*/
13528d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
135316621825SDmitry Karpeev {
135416621825SDmitry Karpeev   PetscErrorCode ierr;
135516621825SDmitry Karpeev 
135616621825SDmitry Karpeev   PetscFunctionBegin;
135716621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1358731c8d9eSDmitry Karpeev   if (len)           {PetscValidPointer(len,2);            *len         = PETSC_NULL;}
135916621825SDmitry Karpeev   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = PETSC_NULL;}
13608d4ac253SDmitry Karpeev   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = PETSC_NULL;}
13618d4ac253SDmitry Karpeev   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = PETSC_NULL;}
13628d4ac253SDmitry Karpeev   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = PETSC_NULL;}
136316621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
13648d4ac253SDmitry Karpeev     ierr = (*dm->ops->createdomaindecomposition)(dm,len,namelist,innerislist,outerislist,dmlist); CHKERRQ(ierr);
1365e7c4fc90SDmitry Karpeev   }
1366e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1367e7c4fc90SDmitry Karpeev }
1368e7c4fc90SDmitry Karpeev 
1369731c8d9eSDmitry Karpeev #undef __FUNCT__
137047c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
137147c6ae99SBarry Smith /*@
137247c6ae99SBarry Smith   DMRefine - Refines a DM object
137347c6ae99SBarry Smith 
137447c6ae99SBarry Smith   Collective on DM
137547c6ae99SBarry Smith 
137647c6ae99SBarry Smith   Input Parameter:
137747c6ae99SBarry Smith + dm   - the DM object
137891d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
137947c6ae99SBarry Smith 
138047c6ae99SBarry Smith   Output Parameter:
1381ae0a1c52SMatthew G Knepley . dmf - the refined DM, or PETSC_NULL
1382ae0a1c52SMatthew G Knepley 
1383ae0a1c52SMatthew G Knepley   Note: If no refinement was done, the return value is PETSC_NULL
138447c6ae99SBarry Smith 
138547c6ae99SBarry Smith   Level: developer
138647c6ae99SBarry Smith 
1387e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
138847c6ae99SBarry Smith @*/
13897087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
139047c6ae99SBarry Smith {
139147c6ae99SBarry Smith   PetscErrorCode ierr;
1392c833c3b5SJed Brown   DMRefineHookLink link;
139347c6ae99SBarry Smith 
139447c6ae99SBarry Smith   PetscFunctionBegin;
1395732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
139647c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
13974057135bSMatthew G Knepley   if (*dmf) {
139843842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
1399644e2e5bSBarry Smith     (*dmf)->ops->initialguess = dm->ops->initialguess;
1400644e2e5bSBarry Smith     (*dmf)->ops->function     = dm->ops->function;
1401644e2e5bSBarry Smith     (*dmf)->ops->functionj    = dm->ops->functionj;
1402644e2e5bSBarry Smith     if (dm->ops->jacobian != DMComputeJacobianDefault) {
1403644e2e5bSBarry Smith       (*dmf)->ops->jacobian     = dm->ops->jacobian;
1404644e2e5bSBarry Smith     }
14058cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
1406644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
14070598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1408656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
1409e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1410c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
1411c833c3b5SJed Brown       if (link->refinehook) {ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);}
1412c833c3b5SJed Brown     }
1413c833c3b5SJed Brown   }
1414c833c3b5SJed Brown   PetscFunctionReturn(0);
1415c833c3b5SJed Brown }
1416c833c3b5SJed Brown 
1417c833c3b5SJed Brown #undef __FUNCT__
1418c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1419c833c3b5SJed Brown /*@
1420c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1421c833c3b5SJed Brown 
1422c833c3b5SJed Brown    Logically Collective
1423c833c3b5SJed Brown 
1424c833c3b5SJed Brown    Input Arguments:
1425c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1426c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1427c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
1428c833c3b5SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1429c833c3b5SJed Brown 
1430c833c3b5SJed Brown    Calling sequence of refinehook:
1431c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1432c833c3b5SJed Brown 
1433c833c3b5SJed Brown +  coarse - coarse level DM
1434c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1435c833c3b5SJed Brown -  ctx - optional user-defined function context
1436c833c3b5SJed Brown 
1437c833c3b5SJed Brown    Calling sequence for interphook:
1438c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1439c833c3b5SJed Brown 
1440c833c3b5SJed Brown +  coarse - coarse level DM
1441c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1442c833c3b5SJed Brown .  fine - fine level DM to update
1443c833c3b5SJed Brown -  ctx - optional user-defined function context
1444c833c3b5SJed Brown 
1445c833c3b5SJed Brown    Level: advanced
1446c833c3b5SJed Brown 
1447c833c3b5SJed Brown    Notes:
1448c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1449c833c3b5SJed Brown 
1450c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1451c833c3b5SJed Brown 
1452c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1453c833c3b5SJed Brown @*/
1454c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1455c833c3b5SJed Brown {
1456c833c3b5SJed Brown   PetscErrorCode ierr;
1457c833c3b5SJed Brown   DMRefineHookLink link,*p;
1458c833c3b5SJed Brown 
1459c833c3b5SJed Brown   PetscFunctionBegin;
1460c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1461c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1462c833c3b5SJed Brown   ierr = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1463c833c3b5SJed Brown   link->refinehook = refinehook;
1464c833c3b5SJed Brown   link->interphook = interphook;
1465c833c3b5SJed Brown   link->ctx = ctx;
1466c833c3b5SJed Brown   link->next = PETSC_NULL;
1467c833c3b5SJed Brown   *p = link;
1468c833c3b5SJed Brown   PetscFunctionReturn(0);
1469c833c3b5SJed Brown }
1470c833c3b5SJed Brown 
1471c833c3b5SJed Brown #undef __FUNCT__
1472c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1473c833c3b5SJed Brown /*@
1474c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1475c833c3b5SJed Brown 
1476c833c3b5SJed Brown    Collective if any hooks are
1477c833c3b5SJed Brown 
1478c833c3b5SJed Brown    Input Arguments:
1479c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1480c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1481c833c3b5SJed Brown -  fine - finer DM to update
1482c833c3b5SJed Brown 
1483c833c3b5SJed Brown    Level: developer
1484c833c3b5SJed Brown 
1485c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1486c833c3b5SJed Brown @*/
1487c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1488c833c3b5SJed Brown {
1489c833c3b5SJed Brown   PetscErrorCode ierr;
1490c833c3b5SJed Brown   DMRefineHookLink link;
1491c833c3b5SJed Brown 
1492c833c3b5SJed Brown   PetscFunctionBegin;
1493c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
1494c833c3b5SJed Brown     if (link->interphook) {ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);}
14954057135bSMatthew G Knepley   }
149647c6ae99SBarry Smith   PetscFunctionReturn(0);
149747c6ae99SBarry Smith }
149847c6ae99SBarry Smith 
149947c6ae99SBarry Smith #undef __FUNCT__
1500eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1501eb3f98d2SBarry Smith /*@
1502eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1503eb3f98d2SBarry Smith 
1504eb3f98d2SBarry Smith     Not Collective
1505eb3f98d2SBarry Smith 
1506eb3f98d2SBarry Smith     Input Parameter:
1507eb3f98d2SBarry Smith .   dm - the DM object
1508eb3f98d2SBarry Smith 
1509eb3f98d2SBarry Smith     Output Parameter:
1510eb3f98d2SBarry Smith .   level - number of refinements
1511eb3f98d2SBarry Smith 
1512eb3f98d2SBarry Smith     Level: developer
1513eb3f98d2SBarry Smith 
15146a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1515eb3f98d2SBarry Smith 
1516eb3f98d2SBarry Smith @*/
1517eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1518eb3f98d2SBarry Smith {
1519eb3f98d2SBarry Smith   PetscFunctionBegin;
1520eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1521eb3f98d2SBarry Smith   *level = dm->levelup;
1522eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1523eb3f98d2SBarry Smith }
1524eb3f98d2SBarry Smith 
1525eb3f98d2SBarry Smith #undef __FUNCT__
152647c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
152747c6ae99SBarry Smith /*@
152847c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
152947c6ae99SBarry Smith 
153047c6ae99SBarry Smith     Neighbor-wise Collective on DM
153147c6ae99SBarry Smith 
153247c6ae99SBarry Smith     Input Parameters:
153347c6ae99SBarry Smith +   dm - the DM object
153447c6ae99SBarry Smith .   g - the global vector
153547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
153647c6ae99SBarry Smith -   l - the local vector
153747c6ae99SBarry Smith 
153847c6ae99SBarry Smith 
153947c6ae99SBarry Smith     Level: beginner
154047c6ae99SBarry Smith 
1541e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
154247c6ae99SBarry Smith 
154347c6ae99SBarry Smith @*/
15447087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
154547c6ae99SBarry Smith {
15467128ae9fSMatthew G Knepley   PetscSF        sf;
154747c6ae99SBarry Smith   PetscErrorCode ierr;
154847c6ae99SBarry Smith 
154947c6ae99SBarry Smith   PetscFunctionBegin;
1550171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15517128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
15527128ae9fSMatthew G Knepley   if (sf) {
15537128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
15547128ae9fSMatthew G Knepley 
15557128ae9fSMatthew G Knepley     if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
15567128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
15577128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
15587128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
15597128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
15607128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
15617128ae9fSMatthew G Knepley   } else {
1562843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
15637128ae9fSMatthew G Knepley   }
156447c6ae99SBarry Smith   PetscFunctionReturn(0);
156547c6ae99SBarry Smith }
156647c6ae99SBarry Smith 
156747c6ae99SBarry Smith #undef __FUNCT__
156847c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
156947c6ae99SBarry Smith /*@
157047c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
157147c6ae99SBarry Smith 
157247c6ae99SBarry Smith     Neighbor-wise Collective on DM
157347c6ae99SBarry Smith 
157447c6ae99SBarry Smith     Input Parameters:
157547c6ae99SBarry Smith +   dm - the DM object
157647c6ae99SBarry Smith .   g - the global vector
157747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
157847c6ae99SBarry Smith -   l - the local vector
157947c6ae99SBarry Smith 
158047c6ae99SBarry Smith 
158147c6ae99SBarry Smith     Level: beginner
158247c6ae99SBarry Smith 
1583e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
158447c6ae99SBarry Smith 
158547c6ae99SBarry Smith @*/
15867087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
158747c6ae99SBarry Smith {
15887128ae9fSMatthew G Knepley   PetscSF        sf;
158947c6ae99SBarry Smith   PetscErrorCode ierr;
159061a3c1faSSatish Balay   PetscScalar    *lArray, *gArray;
159147c6ae99SBarry Smith 
159247c6ae99SBarry Smith   PetscFunctionBegin;
1593171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15947128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
15957128ae9fSMatthew G Knepley   if (sf) {
15967128ae9fSMatthew G Knepley   if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
15977128ae9fSMatthew G Knepley 
15987128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
15997128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16007128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
16017128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16027128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16037128ae9fSMatthew G Knepley   } else {
1604843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
16057128ae9fSMatthew G Knepley   }
160647c6ae99SBarry Smith   PetscFunctionReturn(0);
160747c6ae99SBarry Smith }
160847c6ae99SBarry Smith 
160947c6ae99SBarry Smith #undef __FUNCT__
16109a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
161147c6ae99SBarry Smith /*@
16129a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
16139a42bb27SBarry Smith 
16149a42bb27SBarry Smith     Neighbor-wise Collective on DM
16159a42bb27SBarry Smith 
16169a42bb27SBarry Smith     Input Parameters:
16179a42bb27SBarry Smith +   dm - the DM object
1618f6813fd5SJed Brown .   l - the local vector
16199a42bb27SBarry 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
16209a42bb27SBarry Smith            base point.
1621f6813fd5SJed Brown - - the global vector
16229a42bb27SBarry Smith 
16239a42bb27SBarry 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
16249a42bb27SBarry 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
16259a42bb27SBarry Smith            global array to the final global array with VecAXPY().
16269a42bb27SBarry Smith 
16279a42bb27SBarry Smith     Level: beginner
16289a42bb27SBarry Smith 
1629e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
16309a42bb27SBarry Smith 
16319a42bb27SBarry Smith @*/
16327087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
16339a42bb27SBarry Smith {
16347128ae9fSMatthew G Knepley   PetscSF        sf;
16359a42bb27SBarry Smith   PetscErrorCode ierr;
16369a42bb27SBarry Smith 
16379a42bb27SBarry Smith   PetscFunctionBegin;
1638171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16397128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16407128ae9fSMatthew G Knepley   if (sf) {
16417128ae9fSMatthew G Knepley     MPI_Op       op;
16427128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
16437128ae9fSMatthew G Knepley 
16447128ae9fSMatthew G Knepley     switch(mode) {
16457128ae9fSMatthew G Knepley     case INSERT_VALUES:
16467128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
16477128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
16487128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
16497128ae9fSMatthew G Knepley #else
16507128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
16517128ae9fSMatthew G Knepley #endif
16527128ae9fSMatthew G Knepley     case ADD_VALUES:
16537128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
16547128ae9fSMatthew G Knepley       op = MPI_SUM; break;
16557128ae9fSMatthew G Knepley   default:
16567128ae9fSMatthew G Knepley     SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16577128ae9fSMatthew G Knepley     }
16587128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16597128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16607128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
16617128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16627128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16637128ae9fSMatthew G Knepley   } else {
1664843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
16657128ae9fSMatthew G Knepley   }
16669a42bb27SBarry Smith   PetscFunctionReturn(0);
16679a42bb27SBarry Smith }
16689a42bb27SBarry Smith 
16699a42bb27SBarry Smith #undef __FUNCT__
16709a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
16719a42bb27SBarry Smith /*@
16729a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
167347c6ae99SBarry Smith 
167447c6ae99SBarry Smith     Neighbor-wise Collective on DM
167547c6ae99SBarry Smith 
167647c6ae99SBarry Smith     Input Parameters:
167747c6ae99SBarry Smith +   dm - the DM object
1678f6813fd5SJed Brown .   l - the local vector
167947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1680f6813fd5SJed Brown -   g - the global vector
168147c6ae99SBarry Smith 
168247c6ae99SBarry Smith 
168347c6ae99SBarry Smith     Level: beginner
168447c6ae99SBarry Smith 
1685e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
168647c6ae99SBarry Smith 
168747c6ae99SBarry Smith @*/
16887087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
168947c6ae99SBarry Smith {
16907128ae9fSMatthew G Knepley   PetscSF        sf;
169147c6ae99SBarry Smith   PetscErrorCode ierr;
169247c6ae99SBarry Smith 
169347c6ae99SBarry Smith   PetscFunctionBegin;
1694171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16957128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16967128ae9fSMatthew G Knepley   if (sf) {
16977128ae9fSMatthew G Knepley     MPI_Op       op;
16987128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
16997128ae9fSMatthew G Knepley 
17007128ae9fSMatthew G Knepley     switch(mode) {
17017128ae9fSMatthew G Knepley     case INSERT_VALUES:
17027128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
17037128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
17047128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
17057128ae9fSMatthew G Knepley #else
17067128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
17077128ae9fSMatthew G Knepley #endif
17087128ae9fSMatthew G Knepley     case ADD_VALUES:
17097128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
17107128ae9fSMatthew G Knepley       op = MPI_SUM; break;
17117128ae9fSMatthew G Knepley     default:
17127128ae9fSMatthew G Knepley       SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17137128ae9fSMatthew G Knepley     }
17147128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17157128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17167128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
17177128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17187128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17197128ae9fSMatthew G Knepley   } else {
1720843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
17217128ae9fSMatthew G Knepley   }
172247c6ae99SBarry Smith   PetscFunctionReturn(0);
172347c6ae99SBarry Smith }
172447c6ae99SBarry Smith 
172547c6ae99SBarry Smith #undef __FUNCT__
172647c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
172747c6ae99SBarry Smith /*@
172847c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
172947c6ae99SBarry Smith 
173047c6ae99SBarry Smith     Collective on DM
173147c6ae99SBarry Smith 
173247c6ae99SBarry Smith     Input Parameter:
173347c6ae99SBarry Smith +   dm - the DM object
173447c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
173547c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
173647c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
173747c6ae99SBarry Smith 
173847c6ae99SBarry Smith     Level: developer
173947c6ae99SBarry Smith 
1740e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
174147c6ae99SBarry Smith          DMSetFunction()
174247c6ae99SBarry Smith 
174347c6ae99SBarry Smith @*/
17447087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
174547c6ae99SBarry Smith {
174647c6ae99SBarry Smith   PetscErrorCode ierr;
1747171400e9SBarry Smith 
174847c6ae99SBarry Smith   PetscFunctionBegin;
1749171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
175047c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
175147c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
175247c6ae99SBarry Smith   if (A != B) {
175347c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
175447c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
175547c6ae99SBarry Smith   }
175647c6ae99SBarry Smith   PetscFunctionReturn(0);
175747c6ae99SBarry Smith }
175847c6ae99SBarry Smith 
175947c6ae99SBarry Smith #undef __FUNCT__
176047c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
176147c6ae99SBarry Smith /*@
176247c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
176347c6ae99SBarry Smith 
176447c6ae99SBarry Smith     Collective on DM
176547c6ae99SBarry Smith 
176647c6ae99SBarry Smith     Input Parameter:
176747c6ae99SBarry Smith +   dm - the DM object
176891d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
176947c6ae99SBarry Smith 
177047c6ae99SBarry Smith     Output Parameter:
177147c6ae99SBarry Smith .   dmc - the coarsened DM
177247c6ae99SBarry Smith 
177347c6ae99SBarry Smith     Level: developer
177447c6ae99SBarry Smith 
1775e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
177647c6ae99SBarry Smith 
177747c6ae99SBarry Smith @*/
17787087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
177947c6ae99SBarry Smith {
178047c6ae99SBarry Smith   PetscErrorCode ierr;
1781b17ce1afSJed Brown   DMCoarsenHookLink link;
178247c6ae99SBarry Smith 
178347c6ae99SBarry Smith   PetscFunctionBegin;
1784171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
178547c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
178643842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
178747c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
178847c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
178947c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
179047c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
179147c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
179247c6ae99SBarry Smith   }
17938cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1794644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
17950598a293SJed Brown   (*dmc)->levelup   = dm->levelup;
1796656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
1797e4b4b23bSJed Brown   ierr = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
1798b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1799b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1800b17ce1afSJed Brown   }
1801b17ce1afSJed Brown   PetscFunctionReturn(0);
1802b17ce1afSJed Brown }
1803b17ce1afSJed Brown 
1804b17ce1afSJed Brown #undef __FUNCT__
1805b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1806b17ce1afSJed Brown /*@
1807b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1808b17ce1afSJed Brown 
1809b17ce1afSJed Brown    Logically Collective
1810b17ce1afSJed Brown 
1811b17ce1afSJed Brown    Input Arguments:
1812b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1813b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1814b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
1815b17ce1afSJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1816b17ce1afSJed Brown 
1817b17ce1afSJed Brown    Calling sequence of coarsenhook:
1818b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1819b17ce1afSJed Brown 
1820b17ce1afSJed Brown +  fine - fine level DM
1821b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1822b17ce1afSJed Brown -  ctx - optional user-defined function context
1823b17ce1afSJed Brown 
1824b17ce1afSJed Brown    Calling sequence for restricthook:
1825c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1826b17ce1afSJed Brown 
1827b17ce1afSJed Brown +  fine - fine level DM
1828b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1829c833c3b5SJed Brown .  rscale - scaling vector for restriction
1830c833c3b5SJed Brown .  inject - matrix restricting by injection
1831b17ce1afSJed Brown .  coarse - coarse level DM to update
1832b17ce1afSJed Brown -  ctx - optional user-defined function context
1833b17ce1afSJed Brown 
1834b17ce1afSJed Brown    Level: advanced
1835b17ce1afSJed Brown 
1836b17ce1afSJed Brown    Notes:
1837b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1838b17ce1afSJed Brown 
1839b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1840b17ce1afSJed Brown 
1841b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1842b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1843b17ce1afSJed Brown 
1844c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1845b17ce1afSJed Brown @*/
1846b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1847b17ce1afSJed Brown {
1848b17ce1afSJed Brown   PetscErrorCode ierr;
1849b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1850b17ce1afSJed Brown 
1851b17ce1afSJed Brown   PetscFunctionBegin;
1852b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
18536bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1854b17ce1afSJed Brown   ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1855b17ce1afSJed Brown   link->coarsenhook = coarsenhook;
1856b17ce1afSJed Brown   link->restricthook = restricthook;
1857b17ce1afSJed Brown   link->ctx = ctx;
18586cab3a1bSJed Brown   link->next = PETSC_NULL;
1859b17ce1afSJed Brown   *p = link;
1860b17ce1afSJed Brown   PetscFunctionReturn(0);
1861b17ce1afSJed Brown }
1862b17ce1afSJed Brown 
1863b17ce1afSJed Brown #undef __FUNCT__
1864b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1865b17ce1afSJed Brown /*@
1866b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1867b17ce1afSJed Brown 
1868b17ce1afSJed Brown    Collective if any hooks are
1869b17ce1afSJed Brown 
1870b17ce1afSJed Brown    Input Arguments:
1871b17ce1afSJed Brown +  fine - finer DM to use as a base
1872b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1873b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1874b17ce1afSJed Brown -  coarse - coarer DM to update
1875b17ce1afSJed Brown 
1876b17ce1afSJed Brown    Level: developer
1877b17ce1afSJed Brown 
1878b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1879b17ce1afSJed Brown @*/
1880b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1881b17ce1afSJed Brown {
1882b17ce1afSJed Brown   PetscErrorCode ierr;
1883b17ce1afSJed Brown   DMCoarsenHookLink link;
1884b17ce1afSJed Brown 
1885b17ce1afSJed Brown   PetscFunctionBegin;
1886b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
1887b17ce1afSJed Brown     if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);}
1888b17ce1afSJed Brown   }
188947c6ae99SBarry Smith   PetscFunctionReturn(0);
189047c6ae99SBarry Smith }
189147c6ae99SBarry Smith 
189247c6ae99SBarry Smith #undef __FUNCT__
18935dbd56e3SPeter Brune #define __FUNCT__ "DMBlockRestrictHookAdd"
18945dbd56e3SPeter Brune /*@
18955dbd56e3SPeter Brune    DMBlockRestrictHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
18965dbd56e3SPeter Brune 
18975dbd56e3SPeter Brune    Logically Collective
18985dbd56e3SPeter Brune 
18995dbd56e3SPeter Brune    Input Arguments:
19005dbd56e3SPeter Brune +  global - global DM
19015dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
19025dbd56e3SPeter Brune -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
19035dbd56e3SPeter Brune 
19045dbd56e3SPeter Brune    Calling sequence for restricthook:
19055dbd56e3SPeter Brune $    restricthook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
19065dbd56e3SPeter Brune 
19075dbd56e3SPeter Brune +  global - global DM
19085dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
19095dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
19105dbd56e3SPeter Brune .  block  - block DM (may just be a shell if the global DM is passed in correctly)
19115dbd56e3SPeter Brune -  ctx - optional user-defined function context
19125dbd56e3SPeter Brune 
19135dbd56e3SPeter Brune    Level: advanced
19145dbd56e3SPeter Brune 
19155dbd56e3SPeter Brune    Notes:
19165dbd56e3SPeter Brune    This function is only needed if auxiliary data needs to be set up on coarse grids.
19175dbd56e3SPeter Brune 
19185dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
19195dbd56e3SPeter Brune 
19205dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
19215dbd56e3SPeter Brune    extract the finest level information from its context (instead of from the SNES).
19225dbd56e3SPeter Brune 
19235dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19245dbd56e3SPeter Brune @*/
19255dbd56e3SPeter Brune PetscErrorCode DMBlockRestrictHookAdd(DM global,PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
19265dbd56e3SPeter Brune {
19275dbd56e3SPeter Brune   PetscErrorCode ierr;
19285dbd56e3SPeter Brune   DMBlockRestrictHookLink link,*p;
19295dbd56e3SPeter Brune 
19305dbd56e3SPeter Brune   PetscFunctionBegin;
19315dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
19325dbd56e3SPeter Brune   for (p=&global->blockrestricthook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
19335dbd56e3SPeter Brune   ierr = PetscMalloc(sizeof(struct _DMBlockRestrictHookLink),&link);CHKERRQ(ierr);
19345dbd56e3SPeter Brune   link->restricthook = restricthook;
19355dbd56e3SPeter Brune   link->ctx = ctx;
19365dbd56e3SPeter Brune   link->next = PETSC_NULL;
19375dbd56e3SPeter Brune   *p = link;
19385dbd56e3SPeter Brune   PetscFunctionReturn(0);
19395dbd56e3SPeter Brune }
19405dbd56e3SPeter Brune 
19415dbd56e3SPeter Brune #undef __FUNCT__
19425dbd56e3SPeter Brune #define __FUNCT__ "DMBlockRestrict"
19435dbd56e3SPeter Brune /*@
19445dbd56e3SPeter Brune    DMBlockRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMBlockRestrictHookAdd()
19455dbd56e3SPeter Brune 
19465dbd56e3SPeter Brune    Collective if any hooks are
19475dbd56e3SPeter Brune 
19485dbd56e3SPeter Brune    Input Arguments:
19495dbd56e3SPeter Brune +  fine - finer DM to use as a base
19505dbd56e3SPeter Brune .  restrct - restriction matrix, apply using MatRestrict()
19515dbd56e3SPeter Brune .  inject - injection matrix, also use MatRestrict()
19525dbd56e3SPeter Brune -  coarse - coarer DM to update
19535dbd56e3SPeter Brune 
19545dbd56e3SPeter Brune    Level: developer
19555dbd56e3SPeter Brune 
19565dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
19575dbd56e3SPeter Brune @*/
19585dbd56e3SPeter Brune PetscErrorCode DMBlockRestrict(DM global,VecScatter in,VecScatter out,DM block)
19595dbd56e3SPeter Brune {
19605dbd56e3SPeter Brune   PetscErrorCode ierr;
19615dbd56e3SPeter Brune   DMBlockRestrictHookLink link;
19625dbd56e3SPeter Brune 
19635dbd56e3SPeter Brune   PetscFunctionBegin;
19645dbd56e3SPeter Brune   for (link=global->blockrestricthook; link; link=link->next) {
19655dbd56e3SPeter Brune     if (link->restricthook) {ierr = (*link->restricthook)(global,in,out,block,link->ctx);CHKERRQ(ierr);}
19665dbd56e3SPeter Brune   }
19675dbd56e3SPeter Brune   PetscFunctionReturn(0);
19685dbd56e3SPeter Brune }
19695dbd56e3SPeter Brune 
19705dbd56e3SPeter Brune #undef __FUNCT__
19715fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
19725fe1f584SPeter Brune /*@
19736a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
19745fe1f584SPeter Brune 
19755fe1f584SPeter Brune     Not Collective
19765fe1f584SPeter Brune 
19775fe1f584SPeter Brune     Input Parameter:
19785fe1f584SPeter Brune .   dm - the DM object
19795fe1f584SPeter Brune 
19805fe1f584SPeter Brune     Output Parameter:
19816a7d9d85SPeter Brune .   level - number of coarsenings
19825fe1f584SPeter Brune 
19835fe1f584SPeter Brune     Level: developer
19845fe1f584SPeter Brune 
19856a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
19865fe1f584SPeter Brune 
19875fe1f584SPeter Brune @*/
19885fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
19895fe1f584SPeter Brune {
19905fe1f584SPeter Brune   PetscFunctionBegin;
19915fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19925fe1f584SPeter Brune   *level = dm->leveldown;
19935fe1f584SPeter Brune   PetscFunctionReturn(0);
19945fe1f584SPeter Brune }
19955fe1f584SPeter Brune 
19965fe1f584SPeter Brune 
19975fe1f584SPeter Brune 
19985fe1f584SPeter Brune #undef __FUNCT__
199947c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
200047c6ae99SBarry Smith /*@C
200147c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
200247c6ae99SBarry Smith 
200347c6ae99SBarry Smith     Collective on DM
200447c6ae99SBarry Smith 
200547c6ae99SBarry Smith     Input Parameter:
200647c6ae99SBarry Smith +   dm - the DM object
200747c6ae99SBarry Smith -   nlevels - the number of levels of refinement
200847c6ae99SBarry Smith 
200947c6ae99SBarry Smith     Output Parameter:
201047c6ae99SBarry Smith .   dmf - the refined DM hierarchy
201147c6ae99SBarry Smith 
201247c6ae99SBarry Smith     Level: developer
201347c6ae99SBarry Smith 
2014e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
201547c6ae99SBarry Smith 
201647c6ae99SBarry Smith @*/
20177087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
201847c6ae99SBarry Smith {
201947c6ae99SBarry Smith   PetscErrorCode ierr;
202047c6ae99SBarry Smith 
202147c6ae99SBarry Smith   PetscFunctionBegin;
2022171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
202347c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
202447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
202547c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
202647c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
202747c6ae99SBarry Smith   } else if (dm->ops->refine) {
202847c6ae99SBarry Smith     PetscInt i;
202947c6ae99SBarry Smith 
203047c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
203147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
203247c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
203347c6ae99SBarry Smith     }
203447c6ae99SBarry Smith   } else {
203547c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
203647c6ae99SBarry Smith   }
203747c6ae99SBarry Smith   PetscFunctionReturn(0);
203847c6ae99SBarry Smith }
203947c6ae99SBarry Smith 
204047c6ae99SBarry Smith #undef __FUNCT__
204147c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
204247c6ae99SBarry Smith /*@C
204347c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
204447c6ae99SBarry Smith 
204547c6ae99SBarry Smith     Collective on DM
204647c6ae99SBarry Smith 
204747c6ae99SBarry Smith     Input Parameter:
204847c6ae99SBarry Smith +   dm - the DM object
204947c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
205047c6ae99SBarry Smith 
205147c6ae99SBarry Smith     Output Parameter:
205247c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
205347c6ae99SBarry Smith 
205447c6ae99SBarry Smith     Level: developer
205547c6ae99SBarry Smith 
2056e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
205747c6ae99SBarry Smith 
205847c6ae99SBarry Smith @*/
20597087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
206047c6ae99SBarry Smith {
206147c6ae99SBarry Smith   PetscErrorCode ierr;
206247c6ae99SBarry Smith 
206347c6ae99SBarry Smith   PetscFunctionBegin;
2064171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
206547c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
206647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
206747c6ae99SBarry Smith   PetscValidPointer(dmc,3);
206847c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
206947c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
207047c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
207147c6ae99SBarry Smith     PetscInt i;
207247c6ae99SBarry Smith 
207347c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
207447c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
207547c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
207647c6ae99SBarry Smith     }
207747c6ae99SBarry Smith   } else {
207847c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
207947c6ae99SBarry Smith   }
208047c6ae99SBarry Smith   PetscFunctionReturn(0);
208147c6ae99SBarry Smith }
208247c6ae99SBarry Smith 
208347c6ae99SBarry Smith #undef __FUNCT__
2084e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
208547c6ae99SBarry Smith /*@
2086e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
208747c6ae99SBarry Smith    grids associated with two DMs.
208847c6ae99SBarry Smith 
208947c6ae99SBarry Smith    Collective on DM
209047c6ae99SBarry Smith 
209147c6ae99SBarry Smith    Input Parameters:
209247c6ae99SBarry Smith +  dmc - the coarse grid DM
209347c6ae99SBarry Smith -  dmf - the fine grid DM
209447c6ae99SBarry Smith 
209547c6ae99SBarry Smith    Output Parameters:
209647c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
209747c6ae99SBarry Smith 
209847c6ae99SBarry Smith    Level: intermediate
209947c6ae99SBarry Smith 
210047c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
210147c6ae99SBarry Smith 
2102e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
210347c6ae99SBarry Smith @*/
2104e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
210547c6ae99SBarry Smith {
210647c6ae99SBarry Smith   PetscErrorCode ierr;
210747c6ae99SBarry Smith 
210847c6ae99SBarry Smith   PetscFunctionBegin;
2109171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2110171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
211147c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
211247c6ae99SBarry Smith   PetscFunctionReturn(0);
211347c6ae99SBarry Smith }
211447c6ae99SBarry Smith 
211547c6ae99SBarry Smith #undef __FUNCT__
21161a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
21171a266240SBarry Smith /*@C
21181a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
21191a266240SBarry Smith 
21201a266240SBarry Smith     Not Collective
21211a266240SBarry Smith 
21221a266240SBarry Smith     Input Parameters:
21231a266240SBarry Smith +   dm - the DM object
21241a266240SBarry Smith -   destroy - the destroy function
21251a266240SBarry Smith 
21261a266240SBarry Smith     Level: intermediate
21271a266240SBarry Smith 
2128e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
21291a266240SBarry Smith 
2130f07f9ceaSJed Brown @*/
21311a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
21321a266240SBarry Smith {
21331a266240SBarry Smith   PetscFunctionBegin;
2134171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21351a266240SBarry Smith   dm->ctxdestroy = destroy;
21361a266240SBarry Smith   PetscFunctionReturn(0);
21371a266240SBarry Smith }
21381a266240SBarry Smith 
21391a266240SBarry Smith #undef __FUNCT__
21401b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2141b07ff414SBarry Smith /*@
21421b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
214347c6ae99SBarry Smith 
214447c6ae99SBarry Smith     Not Collective
214547c6ae99SBarry Smith 
214647c6ae99SBarry Smith     Input Parameters:
214747c6ae99SBarry Smith +   dm - the DM object
214847c6ae99SBarry Smith -   ctx - the user context
214947c6ae99SBarry Smith 
215047c6ae99SBarry Smith     Level: intermediate
215147c6ae99SBarry Smith 
2152e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
215347c6ae99SBarry Smith 
215447c6ae99SBarry Smith @*/
21551b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
215647c6ae99SBarry Smith {
215747c6ae99SBarry Smith   PetscFunctionBegin;
2158171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
215947c6ae99SBarry Smith   dm->ctx = ctx;
216047c6ae99SBarry Smith   PetscFunctionReturn(0);
216147c6ae99SBarry Smith }
216247c6ae99SBarry Smith 
216347c6ae99SBarry Smith #undef __FUNCT__
21641b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
216547c6ae99SBarry Smith /*@
21661b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
216747c6ae99SBarry Smith 
216847c6ae99SBarry Smith     Not Collective
216947c6ae99SBarry Smith 
217047c6ae99SBarry Smith     Input Parameter:
217147c6ae99SBarry Smith .   dm - the DM object
217247c6ae99SBarry Smith 
217347c6ae99SBarry Smith     Output Parameter:
217447c6ae99SBarry Smith .   ctx - the user context
217547c6ae99SBarry Smith 
217647c6ae99SBarry Smith     Level: intermediate
217747c6ae99SBarry Smith 
2178e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
217947c6ae99SBarry Smith 
218047c6ae99SBarry Smith @*/
21811b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
218247c6ae99SBarry Smith {
218347c6ae99SBarry Smith   PetscFunctionBegin;
2184171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21851b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
218647c6ae99SBarry Smith   PetscFunctionReturn(0);
218747c6ae99SBarry Smith }
218847c6ae99SBarry Smith 
218947c6ae99SBarry Smith #undef __FUNCT__
219047c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
21917e833e3aSBarry Smith /*@C
219247c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
219347c6ae99SBarry Smith 
219447c6ae99SBarry Smith     Logically Collective on DM
219547c6ae99SBarry Smith 
219647c6ae99SBarry Smith     Input Parameter:
219747c6ae99SBarry Smith +   dm - the DM object to destroy
219847c6ae99SBarry Smith -   f - the function to compute the initial guess
219947c6ae99SBarry Smith 
220047c6ae99SBarry Smith     Level: intermediate
220147c6ae99SBarry Smith 
2202e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
220347c6ae99SBarry Smith 
2204f07f9ceaSJed Brown @*/
22057087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
220647c6ae99SBarry Smith {
220747c6ae99SBarry Smith   PetscFunctionBegin;
2208171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
220947c6ae99SBarry Smith   dm->ops->initialguess = f;
221047c6ae99SBarry Smith   PetscFunctionReturn(0);
221147c6ae99SBarry Smith }
221247c6ae99SBarry Smith 
221347c6ae99SBarry Smith #undef __FUNCT__
221447c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
22157e833e3aSBarry Smith /*@C
221647c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
221747c6ae99SBarry Smith 
221847c6ae99SBarry Smith     Logically Collective on DM
221947c6ae99SBarry Smith 
222047c6ae99SBarry Smith     Input Parameter:
222147c6ae99SBarry Smith +   dm - the DM object
222247c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
222347c6ae99SBarry Smith 
222447c6ae99SBarry Smith     Level: intermediate
222547c6ae99SBarry Smith 
222647c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
222747c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
222847c6ae99SBarry Smith 
2229e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
223047c6ae99SBarry Smith          DMSetJacobian()
223147c6ae99SBarry Smith 
2232f07f9ceaSJed Brown @*/
22337087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
223447c6ae99SBarry Smith {
223547c6ae99SBarry Smith   PetscFunctionBegin;
2236171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
223747c6ae99SBarry Smith   dm->ops->function = f;
223847c6ae99SBarry Smith   if (f) {
223947c6ae99SBarry Smith     dm->ops->functionj = f;
224047c6ae99SBarry Smith   }
224147c6ae99SBarry Smith   PetscFunctionReturn(0);
224247c6ae99SBarry Smith }
224347c6ae99SBarry Smith 
224447c6ae99SBarry Smith #undef __FUNCT__
224547c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
22467e833e3aSBarry Smith /*@C
224747c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
224847c6ae99SBarry Smith 
224947c6ae99SBarry Smith     Logically Collective on DM
225047c6ae99SBarry Smith 
225147c6ae99SBarry Smith     Input Parameter:
225247c6ae99SBarry Smith +   dm - the DM object to destroy
225347c6ae99SBarry Smith -   f - the function to compute the matrix entries
225447c6ae99SBarry Smith 
225547c6ae99SBarry Smith     Level: intermediate
225647c6ae99SBarry Smith 
2257e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
225847c6ae99SBarry Smith          DMSetFunction()
225947c6ae99SBarry Smith 
2260f07f9ceaSJed Brown @*/
22617087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
226247c6ae99SBarry Smith {
226347c6ae99SBarry Smith   PetscFunctionBegin;
2264171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
226547c6ae99SBarry Smith   dm->ops->jacobian = f;
226647c6ae99SBarry Smith   PetscFunctionReturn(0);
226747c6ae99SBarry Smith }
226847c6ae99SBarry Smith 
226947c6ae99SBarry Smith #undef __FUNCT__
227008da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
227108da532bSDmitry Karpeev /*@C
227208da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
227308da532bSDmitry Karpeev 
227408da532bSDmitry Karpeev     Logically Collective on DM
227508da532bSDmitry Karpeev 
227608da532bSDmitry Karpeev     Input Parameter:
227708da532bSDmitry Karpeev +   dm - the DM object
227808da532bSDmitry Karpeev -   f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set)
227908da532bSDmitry Karpeev 
228008da532bSDmitry Karpeev     Level: intermediate
228108da532bSDmitry Karpeev 
2282e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
228308da532bSDmitry Karpeev          DMSetJacobian()
228408da532bSDmitry Karpeev 
228508da532bSDmitry Karpeev @*/
228608da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
228708da532bSDmitry Karpeev {
228808da532bSDmitry Karpeev   PetscFunctionBegin;
228908da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
229008da532bSDmitry Karpeev   PetscFunctionReturn(0);
229108da532bSDmitry Karpeev }
229208da532bSDmitry Karpeev 
229308da532bSDmitry Karpeev #undef __FUNCT__
229408da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
229508da532bSDmitry Karpeev /*@
229608da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
229708da532bSDmitry Karpeev 
229808da532bSDmitry Karpeev     Not Collective
229908da532bSDmitry Karpeev 
230008da532bSDmitry Karpeev     Input Parameter:
230108da532bSDmitry Karpeev .   dm - the DM object to destroy
230208da532bSDmitry Karpeev 
230308da532bSDmitry Karpeev     Output Parameter:
230408da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
230508da532bSDmitry Karpeev 
230608da532bSDmitry Karpeev     Level: developer
230708da532bSDmitry Karpeev 
2308e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
230908da532bSDmitry Karpeev 
231008da532bSDmitry Karpeev @*/
231108da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
231208da532bSDmitry Karpeev {
231308da532bSDmitry Karpeev   PetscFunctionBegin;
231408da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
231508da532bSDmitry Karpeev   PetscFunctionReturn(0);
231608da532bSDmitry Karpeev }
231708da532bSDmitry Karpeev 
231808da532bSDmitry Karpeev #undef __FUNCT__
231908da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
232008da532bSDmitry Karpeev /*@C
232108da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
232208da532bSDmitry Karpeev 
232308da532bSDmitry Karpeev     Logically Collective on DM
232408da532bSDmitry Karpeev 
232508da532bSDmitry Karpeev     Input Parameters:
232608da532bSDmitry Karpeev +   dm - the DM object to destroy
232708da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
232808da532bSDmitry Karpeev 
232908da532bSDmitry Karpeev     Output parameters:
233008da532bSDmitry Karpeev +   xl - lower bound
233108da532bSDmitry Karpeev -   xu - upper bound
233208da532bSDmitry Karpeev 
233308da532bSDmitry Karpeev     Level: intermediate
233408da532bSDmitry Karpeev 
2335e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
233608da532bSDmitry Karpeev          DMSetFunction(), DMSetVariableBounds()
233708da532bSDmitry Karpeev 
233808da532bSDmitry Karpeev @*/
233908da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
234008da532bSDmitry Karpeev {
234108da532bSDmitry Karpeev   PetscErrorCode ierr;
234208da532bSDmitry Karpeev   PetscFunctionBegin;
234308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
234408da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
234508da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
234608da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu); CHKERRQ(ierr);
234708da532bSDmitry Karpeev   }
2348a201590fSDmitry Karpeev   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
234908da532bSDmitry Karpeev   PetscFunctionReturn(0);
235008da532bSDmitry Karpeev }
235108da532bSDmitry Karpeev 
235208da532bSDmitry Karpeev #undef __FUNCT__
235347c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
235447c6ae99SBarry Smith /*@
235547c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
235647c6ae99SBarry Smith 
235747c6ae99SBarry Smith     Collective on DM
235847c6ae99SBarry Smith 
235947c6ae99SBarry Smith     Input Parameter:
236047c6ae99SBarry Smith +   dm - the DM object to destroy
236147c6ae99SBarry Smith -   x - the vector to hold the initial guess values
236247c6ae99SBarry Smith 
236347c6ae99SBarry Smith     Level: developer
236447c6ae99SBarry Smith 
2365e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
236647c6ae99SBarry Smith 
236747c6ae99SBarry Smith @*/
23687087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
236947c6ae99SBarry Smith {
237047c6ae99SBarry Smith   PetscErrorCode ierr;
237147c6ae99SBarry Smith   PetscFunctionBegin;
2372171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
237347c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
237447c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
237547c6ae99SBarry Smith   PetscFunctionReturn(0);
237647c6ae99SBarry Smith }
237747c6ae99SBarry Smith 
237847c6ae99SBarry Smith #undef __FUNCT__
237947c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
238047c6ae99SBarry Smith /*@
238147c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
238247c6ae99SBarry Smith 
238347c6ae99SBarry Smith     Not Collective
238447c6ae99SBarry Smith 
238547c6ae99SBarry Smith     Input Parameter:
238647c6ae99SBarry Smith .   dm - the DM object to destroy
238747c6ae99SBarry Smith 
238847c6ae99SBarry Smith     Output Parameter:
238947c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
239047c6ae99SBarry Smith 
239147c6ae99SBarry Smith     Level: developer
239247c6ae99SBarry Smith 
2393e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
239447c6ae99SBarry Smith 
239547c6ae99SBarry Smith @*/
23967087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
239747c6ae99SBarry Smith {
239847c6ae99SBarry Smith   PetscFunctionBegin;
2399171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
240047c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
240147c6ae99SBarry Smith   PetscFunctionReturn(0);
240247c6ae99SBarry Smith }
240347c6ae99SBarry Smith 
240447c6ae99SBarry Smith #undef __FUNCT__
240547c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
240647c6ae99SBarry Smith /*@
240747c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
240847c6ae99SBarry Smith 
240947c6ae99SBarry Smith     Not Collective
241047c6ae99SBarry Smith 
241147c6ae99SBarry Smith     Input Parameter:
241247c6ae99SBarry Smith .   dm - the DM object to destroy
241347c6ae99SBarry Smith 
241447c6ae99SBarry Smith     Output Parameter:
241547c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
241647c6ae99SBarry Smith 
241747c6ae99SBarry Smith     Level: developer
241847c6ae99SBarry Smith 
2419e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
242047c6ae99SBarry Smith 
242147c6ae99SBarry Smith @*/
24227087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
242347c6ae99SBarry Smith {
242447c6ae99SBarry Smith   PetscFunctionBegin;
2425171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
242647c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
242747c6ae99SBarry Smith   PetscFunctionReturn(0);
242847c6ae99SBarry Smith }
242947c6ae99SBarry Smith 
243047c6ae99SBarry Smith #undef __FUNCT__
243147c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
243247c6ae99SBarry Smith /*@
243347c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
243447c6ae99SBarry Smith 
243547c6ae99SBarry Smith     Not Collective
243647c6ae99SBarry Smith 
243747c6ae99SBarry Smith     Input Parameter:
243847c6ae99SBarry Smith .   dm - the DM object to destroy
243947c6ae99SBarry Smith 
244047c6ae99SBarry Smith     Output Parameter:
244147c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
244247c6ae99SBarry Smith 
244347c6ae99SBarry Smith     Level: developer
244447c6ae99SBarry Smith 
2445e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
244647c6ae99SBarry Smith 
244747c6ae99SBarry Smith @*/
24487087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
244947c6ae99SBarry Smith {
245047c6ae99SBarry Smith   PetscFunctionBegin;
2451171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
245247c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
245347c6ae99SBarry Smith   PetscFunctionReturn(0);
245447c6ae99SBarry Smith }
245547c6ae99SBarry Smith 
245647c6ae99SBarry Smith #undef __FUNCT__
2457b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2458b0ae01b7SPeter Brune /*@
2459b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2460b0ae01b7SPeter Brune 
2461b0ae01b7SPeter Brune     Not Collective
2462b0ae01b7SPeter Brune 
2463b0ae01b7SPeter Brune     Input Parameter:
2464b0ae01b7SPeter Brune .   dm - the DM object
2465b0ae01b7SPeter Brune 
2466b0ae01b7SPeter Brune     Output Parameter:
2467b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2468b0ae01b7SPeter Brune 
2469b0ae01b7SPeter Brune     Level: developer
2470b0ae01b7SPeter Brune 
2471b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2472b0ae01b7SPeter Brune 
2473b0ae01b7SPeter Brune @*/
2474b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2475b0ae01b7SPeter Brune {
2476b0ae01b7SPeter Brune   PetscFunctionBegin;
2477b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2478b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2479b0ae01b7SPeter Brune }
2480b0ae01b7SPeter Brune 
2481b0ae01b7SPeter Brune #undef  __FUNCT__
248208da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2483748fac09SDmitry Karpeev /*@C
248408da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
248508da532bSDmitry Karpeev 
248608da532bSDmitry Karpeev     Collective on DM
248708da532bSDmitry Karpeev 
248808da532bSDmitry Karpeev     Input Parameter:
248908da532bSDmitry Karpeev +   dm - the DM object
2490e88d7f4bSDmitry Karpeev -   x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems.
249108da532bSDmitry Karpeev 
249208da532bSDmitry Karpeev     Level: developer
249308da532bSDmitry Karpeev 
2494e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
249508da532bSDmitry Karpeev          DMSetFunction(), DMSetJacobian(), DMSetVariableBounds()
249608da532bSDmitry Karpeev 
249708da532bSDmitry Karpeev @*/
249808da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
249908da532bSDmitry Karpeev {
250008da532bSDmitry Karpeev   PetscErrorCode ierr;
250108da532bSDmitry Karpeev   PetscFunctionBegin;
250208da532bSDmitry Karpeev   if (x) {
250308da532bSDmitry Karpeev     if (!dm->x) {
250408da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
250508da532bSDmitry Karpeev     }
250608da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
250708da532bSDmitry Karpeev   }
250808da532bSDmitry Karpeev   else if (dm->x) {
250908da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);  CHKERRQ(ierr);
251008da532bSDmitry Karpeev   }
251108da532bSDmitry Karpeev   PetscFunctionReturn(0);
251208da532bSDmitry Karpeev }
251308da532bSDmitry Karpeev 
251408da532bSDmitry Karpeev 
251508da532bSDmitry Karpeev #undef __FUNCT__
251647c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
251747c6ae99SBarry Smith /*@
251847c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
251947c6ae99SBarry Smith 
252047c6ae99SBarry Smith     Collective on DM
252147c6ae99SBarry Smith 
252247c6ae99SBarry Smith     Input Parameter:
252347c6ae99SBarry Smith +   dm - the DM object to destroy
252447c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
252547c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
252647c6ae99SBarry Smith 
252747c6ae99SBarry Smith     Level: developer
252847c6ae99SBarry Smith 
2529e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
253047c6ae99SBarry Smith          DMSetJacobian()
253147c6ae99SBarry Smith 
253247c6ae99SBarry Smith @*/
25337087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
253447c6ae99SBarry Smith {
253547c6ae99SBarry Smith   PetscErrorCode ierr;
253647c6ae99SBarry Smith   PetscFunctionBegin;
2537171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
253847c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
2539644e2e5bSBarry Smith   PetscStackPush("DM user function");
254047c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
2541644e2e5bSBarry Smith   PetscStackPop;
254247c6ae99SBarry Smith   PetscFunctionReturn(0);
254347c6ae99SBarry Smith }
254447c6ae99SBarry Smith 
254547c6ae99SBarry Smith 
254608da532bSDmitry Karpeev 
254747c6ae99SBarry Smith #undef __FUNCT__
254847c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
254947c6ae99SBarry Smith /*@
255047c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
255147c6ae99SBarry Smith 
255247c6ae99SBarry Smith     Collective on DM
255347c6ae99SBarry Smith 
255447c6ae99SBarry Smith     Input Parameter:
255547c6ae99SBarry Smith +   dm - the DM object
2556cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
255747c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
255847c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
255947c6ae99SBarry Smith 
256047c6ae99SBarry Smith     Level: developer
256147c6ae99SBarry Smith 
2562e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
256347c6ae99SBarry Smith          DMSetFunction()
256447c6ae99SBarry Smith 
256547c6ae99SBarry Smith @*/
25667087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
256747c6ae99SBarry Smith {
256847c6ae99SBarry Smith   PetscErrorCode ierr;
256947c6ae99SBarry Smith 
257047c6ae99SBarry Smith   PetscFunctionBegin;
2571171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
257247c6ae99SBarry Smith   if (!dm->ops->jacobian) {
257347c6ae99SBarry Smith     ISColoring     coloring;
257447c6ae99SBarry Smith     MatFDColoring  fd;
257519fd82e9SBarry Smith     MatType        mtype;
257647c6ae99SBarry Smith 
25772c9966d7SBarry Smith     ierr = PetscObjectGetType((PetscObject)B,&mtype);CHKERRQ(ierr);
25782c9966d7SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,mtype,&coloring);CHKERRQ(ierr);
257947c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
2580fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
258147c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
25820bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
25830bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
258471cd77b2SBarry Smith 
258547c6ae99SBarry Smith     dm->fd = fd;
258647c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
25872533e041SBarry Smith 
258871cd77b2SBarry Smith     /* don't know why this is needed */
258971cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
259047c6ae99SBarry Smith   }
259147c6ae99SBarry Smith   if (!x) x = dm->x;
259247c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
2593cab2e9ccSBarry Smith 
259471cd77b2SBarry 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 */
2595649052a6SBarry Smith   if (x) {
2596cab2e9ccSBarry Smith     if (!dm->x) {
259771cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
2598cab2e9ccSBarry Smith     }
2599cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
2600649052a6SBarry Smith   }
2601a8248277SBarry Smith   if (A != B) {
2602a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2603a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2604a8248277SBarry Smith   }
260547c6ae99SBarry Smith   PetscFunctionReturn(0);
260647c6ae99SBarry Smith }
2607264ace61SBarry Smith 
2608cab2e9ccSBarry Smith 
2609264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
2610264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
2611264ace61SBarry Smith 
2612264ace61SBarry Smith #undef __FUNCT__
2613264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2614264ace61SBarry Smith /*@C
2615264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2616264ace61SBarry Smith 
2617264ace61SBarry Smith   Collective on DM
2618264ace61SBarry Smith 
2619264ace61SBarry Smith   Input Parameters:
2620264ace61SBarry Smith + dm     - The DM object
2621264ace61SBarry Smith - method - The name of the DM type
2622264ace61SBarry Smith 
2623264ace61SBarry Smith   Options Database Key:
2624264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2625264ace61SBarry Smith 
2626264ace61SBarry Smith   Notes:
2627e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2628264ace61SBarry Smith 
2629264ace61SBarry Smith   Level: intermediate
2630264ace61SBarry Smith 
2631264ace61SBarry Smith .keywords: DM, set, type
2632264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2633264ace61SBarry Smith @*/
263419fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2635264ace61SBarry Smith {
2636264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2637264ace61SBarry Smith   PetscBool      match;
2638264ace61SBarry Smith   PetscErrorCode ierr;
2639264ace61SBarry Smith 
2640264ace61SBarry Smith   PetscFunctionBegin;
2641264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2642251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2643264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2644264ace61SBarry Smith 
2645264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
26464b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2647264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2648264ace61SBarry Smith 
2649264ace61SBarry Smith   if (dm->ops->destroy) {
2650264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
2651b5c23020SJed Brown     dm->ops->destroy = PETSC_NULL;
2652264ace61SBarry Smith   }
2653264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2654264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2655264ace61SBarry Smith   PetscFunctionReturn(0);
2656264ace61SBarry Smith }
2657264ace61SBarry Smith 
2658264ace61SBarry Smith #undef __FUNCT__
2659264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2660264ace61SBarry Smith /*@C
2661264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2662264ace61SBarry Smith 
2663264ace61SBarry Smith   Not Collective
2664264ace61SBarry Smith 
2665264ace61SBarry Smith   Input Parameter:
2666264ace61SBarry Smith . dm  - The DM
2667264ace61SBarry Smith 
2668264ace61SBarry Smith   Output Parameter:
2669264ace61SBarry Smith . type - The DM type name
2670264ace61SBarry Smith 
2671264ace61SBarry Smith   Level: intermediate
2672264ace61SBarry Smith 
2673264ace61SBarry Smith .keywords: DM, get, type, name
2674264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2675264ace61SBarry Smith @*/
267619fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2677264ace61SBarry Smith {
2678264ace61SBarry Smith   PetscErrorCode ierr;
2679264ace61SBarry Smith 
2680264ace61SBarry Smith   PetscFunctionBegin;
2681264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2682264ace61SBarry Smith   PetscValidCharPointer(type,2);
2683264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2684264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
2685264ace61SBarry Smith   }
2686264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2687264ace61SBarry Smith   PetscFunctionReturn(0);
2688264ace61SBarry Smith }
2689264ace61SBarry Smith 
269067a56275SMatthew G Knepley #undef __FUNCT__
269167a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
269267a56275SMatthew G Knepley /*@C
269367a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
269467a56275SMatthew G Knepley 
269567a56275SMatthew G Knepley   Collective on DM
269667a56275SMatthew G Knepley 
269767a56275SMatthew G Knepley   Input Parameters:
269867a56275SMatthew G Knepley + dm - the DM
269967a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
270067a56275SMatthew G Knepley 
270167a56275SMatthew G Knepley   Output Parameter:
270267a56275SMatthew G Knepley . M - pointer to new DM
270367a56275SMatthew G Knepley 
270467a56275SMatthew G Knepley   Notes:
270567a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
270667a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
270767a56275SMatthew G Knepley   of the input DM.
270867a56275SMatthew G Knepley 
270967a56275SMatthew G Knepley   Level: intermediate
271067a56275SMatthew G Knepley 
271167a56275SMatthew G Knepley .seealso: DMCreate()
271267a56275SMatthew G Knepley @*/
271319fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
271467a56275SMatthew G Knepley {
271567a56275SMatthew G Knepley   DM             B;
271667a56275SMatthew G Knepley   char           convname[256];
271767a56275SMatthew G Knepley   PetscBool      sametype, issame;
271867a56275SMatthew G Knepley   PetscErrorCode ierr;
271967a56275SMatthew G Knepley 
272067a56275SMatthew G Knepley   PetscFunctionBegin;
272167a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
272267a56275SMatthew G Knepley   PetscValidType(dm,1);
272367a56275SMatthew G Knepley   PetscValidPointer(M,3);
2724251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
272567a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
272667a56275SMatthew G Knepley   {
272719fd82e9SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = PETSC_NULL;
272867a56275SMatthew G Knepley 
272967a56275SMatthew G Knepley     /*
273067a56275SMatthew G Knepley        Order of precedence:
273167a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
273267a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
273367a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
273467a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
273567a56275SMatthew G Knepley        5) Use a really basic converter.
273667a56275SMatthew G Knepley     */
273767a56275SMatthew G Knepley 
273867a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
273967a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
274067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
274167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
274267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
274367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
274467a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
274567a56275SMatthew G Knepley     if (conv) goto foundconv;
274667a56275SMatthew G Knepley 
274767a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
274867a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
274967a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
275067a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
275167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
275267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
275367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
275467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
275567a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
275667a56275SMatthew G Knepley     if (conv) {
2757fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
275867a56275SMatthew G Knepley       goto foundconv;
275967a56275SMatthew G Knepley     }
276067a56275SMatthew G Knepley 
276167a56275SMatthew G Knepley #if 0
276267a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
276367a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2764fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
276567a56275SMatthew G Knepley     if (conv) goto foundconv;
276667a56275SMatthew G Knepley 
276767a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
276867a56275SMatthew G Knepley     if (dm->ops->convert) {
276967a56275SMatthew G Knepley       conv = dm->ops->convert;
277067a56275SMatthew G Knepley     }
277167a56275SMatthew G Knepley     if (conv) goto foundconv;
277267a56275SMatthew G Knepley #endif
277367a56275SMatthew G Knepley 
277467a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
277567a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
277667a56275SMatthew G Knepley 
277767a56275SMatthew G Knepley     foundconv:
277867a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
277967a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
278067a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
278167a56275SMatthew G Knepley   }
278267a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
278367a56275SMatthew G Knepley   PetscFunctionReturn(0);
278467a56275SMatthew G Knepley }
2785264ace61SBarry Smith 
2786264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2787264ace61SBarry Smith 
2788264ace61SBarry Smith #undef __FUNCT__
2789264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2790264ace61SBarry Smith /*@C
2791264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
2792264ace61SBarry Smith 
2793264ace61SBarry Smith   Level: advanced
2794264ace61SBarry Smith @*/
27957087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
2796264ace61SBarry Smith {
2797264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
2798264ace61SBarry Smith   PetscErrorCode ierr;
2799264ace61SBarry Smith 
2800264ace61SBarry Smith   PetscFunctionBegin;
2801264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
2802264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
2803264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
2804264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
2805264ace61SBarry Smith   PetscFunctionReturn(0);
2806264ace61SBarry Smith }
2807264ace61SBarry Smith 
2808264ace61SBarry Smith 
2809264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2810264ace61SBarry Smith #undef __FUNCT__
2811264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
2812264ace61SBarry Smith /*@C
2813264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
2814264ace61SBarry Smith 
2815264ace61SBarry Smith    Not Collective
2816264ace61SBarry Smith 
2817264ace61SBarry Smith    Level: advanced
2818264ace61SBarry Smith 
2819264ace61SBarry Smith .keywords: DM, register, destroy
2820264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
2821264ace61SBarry Smith @*/
28227087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
2823264ace61SBarry Smith {
2824264ace61SBarry Smith   PetscErrorCode ierr;
2825264ace61SBarry Smith 
2826264ace61SBarry Smith   PetscFunctionBegin;
2827264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
2828264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
2829264ace61SBarry Smith   PetscFunctionReturn(0);
2830264ace61SBarry Smith }
283123f975d1SBarry Smith 
283223f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2833c6db04a5SJed Brown #include <mex.h>
283423f975d1SBarry Smith 
28353014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
283623f975d1SBarry Smith 
283723f975d1SBarry Smith #undef __FUNCT__
283823f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
283923f975d1SBarry Smith /*
284023f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
284123f975d1SBarry Smith                          DMSetFunctionMatlab().
284223f975d1SBarry Smith 
284323f975d1SBarry Smith    For linear problems x is null
284423f975d1SBarry Smith 
284523f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
284623f975d1SBarry Smith */
28477087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
284823f975d1SBarry Smith {
284923f975d1SBarry Smith   PetscErrorCode    ierr;
285023f975d1SBarry Smith   DMMatlabContext   *sctx;
285123f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
285223f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
285323f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
285423f975d1SBarry Smith 
285523f975d1SBarry Smith   PetscFunctionBegin;
285623f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
285723f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
285823f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
285923f975d1SBarry Smith 
286023f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
28611b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
286223f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
286323f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
28643014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
286523f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
286623f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
286723f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
286823f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
2869b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
287023f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
287123f975d1SBarry Smith   mxDestroyArray(prhs[0]);
287223f975d1SBarry Smith   mxDestroyArray(prhs[1]);
287323f975d1SBarry Smith   mxDestroyArray(prhs[2]);
287423f975d1SBarry Smith   mxDestroyArray(prhs[3]);
287523f975d1SBarry Smith   mxDestroyArray(plhs[0]);
287623f975d1SBarry Smith   PetscFunctionReturn(0);
287723f975d1SBarry Smith }
287823f975d1SBarry Smith 
287923f975d1SBarry Smith 
288023f975d1SBarry Smith #undef __FUNCT__
288123f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
288223f975d1SBarry Smith /*
288323f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
288423f975d1SBarry Smith 
288523f975d1SBarry Smith */
28867087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
288723f975d1SBarry Smith {
288823f975d1SBarry Smith   PetscErrorCode    ierr;
288923f975d1SBarry Smith   DMMatlabContext   *sctx;
289023f975d1SBarry Smith 
289123f975d1SBarry Smith   PetscFunctionBegin;
2892171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
289323f975d1SBarry Smith   /* currently sctx is memory bleed */
28941b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
28953014e516SBarry Smith   if (!sctx) {
289623f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
28973014e516SBarry Smith   }
289823f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
28991b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
290023f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
290123f975d1SBarry Smith   PetscFunctionReturn(0);
290223f975d1SBarry Smith }
29033014e516SBarry Smith 
29043014e516SBarry Smith #undef __FUNCT__
29053014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
29063014e516SBarry Smith /*
29073014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
29083014e516SBarry Smith                          DMSetJacobianMatlab().
29093014e516SBarry Smith 
29103014e516SBarry Smith    For linear problems x is null
29113014e516SBarry Smith 
29123014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
29133014e516SBarry Smith */
29147087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
29153014e516SBarry Smith {
29163014e516SBarry Smith   PetscErrorCode    ierr;
29173014e516SBarry Smith   DMMatlabContext   *sctx;
29183014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
29193014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
29203014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
29213014e516SBarry Smith 
29223014e516SBarry Smith   PetscFunctionBegin;
29233014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29243014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
29253014e516SBarry Smith 
2926e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
29271b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
29283014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
29293014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
29303014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
29313014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
29323014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
29333014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
29343014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
29353014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
29363014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
2937b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
2938c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
2939c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
29403014e516SBarry Smith   mxDestroyArray(prhs[0]);
29413014e516SBarry Smith   mxDestroyArray(prhs[1]);
29423014e516SBarry Smith   mxDestroyArray(prhs[2]);
29433014e516SBarry Smith   mxDestroyArray(prhs[3]);
29443014e516SBarry Smith   mxDestroyArray(prhs[4]);
29453014e516SBarry Smith   mxDestroyArray(plhs[0]);
29463014e516SBarry Smith   mxDestroyArray(plhs[1]);
29473014e516SBarry Smith   PetscFunctionReturn(0);
29483014e516SBarry Smith }
29493014e516SBarry Smith 
29503014e516SBarry Smith 
29513014e516SBarry Smith #undef __FUNCT__
29523014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
29533014e516SBarry Smith /*
29543014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
29553014e516SBarry Smith 
29563014e516SBarry Smith */
29577087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
29583014e516SBarry Smith {
29593014e516SBarry Smith   PetscErrorCode    ierr;
29603014e516SBarry Smith   DMMatlabContext   *sctx;
29613014e516SBarry Smith 
29623014e516SBarry Smith   PetscFunctionBegin;
2963171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29643014e516SBarry Smith   /* currently sctx is memory bleed */
29651b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
29663014e516SBarry Smith   if (!sctx) {
29673014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
29683014e516SBarry Smith   }
29693014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
29701b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
29713014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
29723014e516SBarry Smith   PetscFunctionReturn(0);
29733014e516SBarry Smith }
297423f975d1SBarry Smith #endif
2975b859378eSBarry Smith 
2976b859378eSBarry Smith #undef __FUNCT__
2977b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2978b859378eSBarry Smith /*@C
2979b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
2980b859378eSBarry Smith   with DMView().
2981b859378eSBarry Smith 
2982b859378eSBarry Smith   Collective on PetscViewer
2983b859378eSBarry Smith 
2984b859378eSBarry Smith   Input Parameters:
2985b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2986b859378eSBarry Smith            some related function before a call to DMLoad().
2987b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2988b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2989b859378eSBarry Smith 
2990b859378eSBarry Smith    Level: intermediate
2991b859378eSBarry Smith 
2992b859378eSBarry Smith   Notes:
2993b859378eSBarry Smith   Defaults to the DM DA.
2994b859378eSBarry Smith 
2995b859378eSBarry Smith   Notes for advanced users:
2996b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2997b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2998b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2999b859378eSBarry Smith   format is
3000b859378eSBarry Smith .vb
3001b859378eSBarry Smith      has not yet been determined
3002b859378eSBarry Smith .ve
3003b859378eSBarry Smith 
3004b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
3005b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
3006b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
3007b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
3008b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
3009b859378eSBarry Smith 
3010b859378eSBarry Smith   Concepts: vector^loading from file
3011b859378eSBarry Smith 
3012b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3013b859378eSBarry Smith @*/
3014b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3015b859378eSBarry Smith {
3016b859378eSBarry Smith   PetscErrorCode ierr;
3017b859378eSBarry Smith 
3018b859378eSBarry Smith   PetscFunctionBegin;
3019b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3020b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3021b859378eSBarry Smith 
3022b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
3023b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
3024b859378eSBarry Smith   }
3025b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
3026b859378eSBarry Smith   PetscFunctionReturn(0);
3027b859378eSBarry Smith }
3028b859378eSBarry Smith 
30297da65231SMatthew G Knepley /******************************** FEM Support **********************************/
30307da65231SMatthew G Knepley 
30317da65231SMatthew G Knepley #undef __FUNCT__
30327da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
30337da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
30341d47ebbbSSatish Balay   PetscInt       f;
30351b30c384SMatthew G Knepley   PetscErrorCode ierr;
30361b30c384SMatthew G Knepley 
30377da65231SMatthew G Knepley   PetscFunctionBegin;
303874778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
30391d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
304074778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
30417da65231SMatthew G Knepley   }
30427da65231SMatthew G Knepley   PetscFunctionReturn(0);
30437da65231SMatthew G Knepley }
30447da65231SMatthew G Knepley 
30457da65231SMatthew G Knepley #undef __FUNCT__
30467da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
30477da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
30481b30c384SMatthew G Knepley   PetscInt       f, g;
30497da65231SMatthew G Knepley   PetscErrorCode ierr;
30507da65231SMatthew G Knepley 
30517da65231SMatthew G Knepley   PetscFunctionBegin;
305274778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
30531d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
305474778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
30551d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
305674778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
30577da65231SMatthew G Knepley     }
305874778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
30597da65231SMatthew G Knepley   }
30607da65231SMatthew G Knepley   PetscFunctionReturn(0);
30617da65231SMatthew G Knepley }
3062e7c4fc90SDmitry Karpeev 
3063970e74d5SMatthew G Knepley #undef __FUNCT__
3064970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalFunction"
3065970e74d5SMatthew G Knepley /*@C
3066970e74d5SMatthew G Knepley   DMGetLocalFunction - Get the local residual function from this DM
3067970e74d5SMatthew G Knepley 
3068970e74d5SMatthew G Knepley   Not collective
3069970e74d5SMatthew G Knepley 
3070970e74d5SMatthew G Knepley   Input Parameter:
3071970e74d5SMatthew G Knepley . dm - The DM
3072970e74d5SMatthew G Knepley 
3073970e74d5SMatthew G Knepley   Output Parameter:
3074970e74d5SMatthew G Knepley . lf - The local residual function
3075970e74d5SMatthew G Knepley 
3076970e74d5SMatthew G Knepley    Calling sequence of lf:
3077970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
3078970e74d5SMatthew G Knepley 
3079970e74d5SMatthew G Knepley +  snes - the SNES context
3080970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
3081970e74d5SMatthew G Knepley .  f - local vector to put residual in
3082970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
3083970e74d5SMatthew G Knepley 
3084970e74d5SMatthew G Knepley   Level: intermediate
3085970e74d5SMatthew G Knepley 
3086970e74d5SMatthew G Knepley .seealso DMSetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
3087970e74d5SMatthew G Knepley @*/
3088970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalFunction(DM dm, PetscErrorCode (**lf)(DM, Vec, Vec, void *))
3089970e74d5SMatthew G Knepley {
3090970e74d5SMatthew G Knepley   PetscFunctionBegin;
3091970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3092970e74d5SMatthew G Knepley   if (lf) *lf = dm->lf;
3093970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
3094970e74d5SMatthew G Knepley }
3095970e74d5SMatthew G Knepley 
3096970e74d5SMatthew G Knepley #undef __FUNCT__
3097970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalFunction"
3098970e74d5SMatthew G Knepley /*@C
3099970e74d5SMatthew G Knepley   DMSetLocalFunction - Set the local residual function from this DM
3100970e74d5SMatthew G Knepley 
3101970e74d5SMatthew G Knepley   Not collective
3102970e74d5SMatthew G Knepley 
3103970e74d5SMatthew G Knepley   Input Parameters:
3104970e74d5SMatthew G Knepley + dm - The DM
3105970e74d5SMatthew G Knepley - lf - The local residual function
3106970e74d5SMatthew G Knepley 
3107970e74d5SMatthew G Knepley    Calling sequence of lf:
3108970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
3109970e74d5SMatthew G Knepley 
3110970e74d5SMatthew G Knepley +  snes - the SNES context
3111970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
3112970e74d5SMatthew G Knepley .  f - local vector to put residual in
3113970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
3114970e74d5SMatthew G Knepley 
3115970e74d5SMatthew G Knepley   Level: intermediate
3116970e74d5SMatthew G Knepley 
3117970e74d5SMatthew G Knepley .seealso DMGetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
3118970e74d5SMatthew G Knepley @*/
3119970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalFunction(DM dm, PetscErrorCode (*lf)(DM, Vec, Vec, void *))
3120970e74d5SMatthew G Knepley {
3121970e74d5SMatthew G Knepley   PetscFunctionBegin;
3122970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3123970e74d5SMatthew G Knepley   dm->lf = lf;
3124970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
3125970e74d5SMatthew G Knepley }
3126970e74d5SMatthew G Knepley 
3127970e74d5SMatthew G Knepley #undef __FUNCT__
3128970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalJacobian"
3129970e74d5SMatthew G Knepley /*@C
3130970e74d5SMatthew G Knepley   DMGetLocalJacobian - Get the local Jacobian function from this DM
3131970e74d5SMatthew G Knepley 
3132970e74d5SMatthew G Knepley   Not collective
3133970e74d5SMatthew G Knepley 
3134970e74d5SMatthew G Knepley   Input Parameter:
3135970e74d5SMatthew G Knepley . dm - The DM
3136970e74d5SMatthew G Knepley 
3137970e74d5SMatthew G Knepley   Output Parameter:
3138970e74d5SMatthew G Knepley . lj - The local Jacobian function
3139970e74d5SMatthew G Knepley 
3140970e74d5SMatthew G Knepley    Calling sequence of lj:
3141970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
3142970e74d5SMatthew G Knepley 
3143970e74d5SMatthew G Knepley +  snes - the SNES context
3144970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
3145970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
3146970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
3147970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
3148970e74d5SMatthew G Knepley 
3149970e74d5SMatthew G Knepley   Level: intermediate
3150970e74d5SMatthew G Knepley 
3151970e74d5SMatthew G Knepley .seealso DMSetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
3152970e74d5SMatthew G Knepley @*/
3153970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalJacobian(DM dm, PetscErrorCode (**lj)(DM, Vec, Mat, Mat, void *))
3154970e74d5SMatthew G Knepley {
3155970e74d5SMatthew G Knepley   PetscFunctionBegin;
3156970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3157970e74d5SMatthew G Knepley   if (lj) *lj = dm->lj;
3158970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
3159970e74d5SMatthew G Knepley }
3160970e74d5SMatthew G Knepley 
3161970e74d5SMatthew G Knepley #undef __FUNCT__
3162970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalJacobian"
3163970e74d5SMatthew G Knepley /*@C
3164970e74d5SMatthew G Knepley   DMSetLocalJacobian - Set the local Jacobian function from this DM
3165970e74d5SMatthew G Knepley 
3166970e74d5SMatthew G Knepley   Not collective
3167970e74d5SMatthew G Knepley 
3168970e74d5SMatthew G Knepley   Input Parameters:
3169970e74d5SMatthew G Knepley + dm - The DM
3170970e74d5SMatthew G Knepley - lj - The local Jacobian function
3171970e74d5SMatthew G Knepley 
3172970e74d5SMatthew G Knepley    Calling sequence of lj:
3173970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
3174970e74d5SMatthew G Knepley 
3175970e74d5SMatthew G Knepley +  snes - the SNES context
3176970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
3177970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
3178970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
3179970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
3180970e74d5SMatthew G Knepley 
3181970e74d5SMatthew G Knepley   Level: intermediate
3182970e74d5SMatthew G Knepley 
3183970e74d5SMatthew G Knepley .seealso DMGetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
3184970e74d5SMatthew G Knepley @*/
3185970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalJacobian(DM dm, PetscErrorCode (*lj)(DM, Vec, Mat,  Mat, void *))
3186970e74d5SMatthew G Knepley {
3187970e74d5SMatthew G Knepley   PetscFunctionBegin;
3188970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3189970e74d5SMatthew G Knepley   dm->lj = lj;
3190970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
3191970e74d5SMatthew G Knepley }
319288ed4aceSMatthew G Knepley 
319388ed4aceSMatthew G Knepley #undef __FUNCT__
319488ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
319588ed4aceSMatthew G Knepley /*@
319688ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
319788ed4aceSMatthew G Knepley 
319888ed4aceSMatthew G Knepley   Input Parameter:
319988ed4aceSMatthew G Knepley . dm - The DM
320088ed4aceSMatthew G Knepley 
320188ed4aceSMatthew G Knepley   Output Parameter:
320288ed4aceSMatthew G Knepley . section - The PetscSection
320388ed4aceSMatthew G Knepley 
320488ed4aceSMatthew G Knepley   Level: intermediate
320588ed4aceSMatthew G Knepley 
320688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
320788ed4aceSMatthew G Knepley 
320888ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
320988ed4aceSMatthew G Knepley @*/
321088ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) {
321188ed4aceSMatthew G Knepley   PetscFunctionBegin;
321288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
321388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
321488ed4aceSMatthew G Knepley   *section = dm->defaultSection;
321588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
321688ed4aceSMatthew G Knepley }
321788ed4aceSMatthew G Knepley 
321888ed4aceSMatthew G Knepley #undef __FUNCT__
321988ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
322088ed4aceSMatthew G Knepley /*@
322188ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
322288ed4aceSMatthew G Knepley 
322388ed4aceSMatthew G Knepley   Input Parameters:
322488ed4aceSMatthew G Knepley + dm - The DM
322588ed4aceSMatthew G Knepley - section - The PetscSection
322688ed4aceSMatthew G Knepley 
322788ed4aceSMatthew G Knepley   Level: intermediate
322888ed4aceSMatthew G Knepley 
322988ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
323088ed4aceSMatthew G Knepley 
323188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
323288ed4aceSMatthew G Knepley @*/
323388ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) {
3234af122d2aSMatthew G Knepley   PetscInt       numFields;
3235af122d2aSMatthew G Knepley   PetscInt       f;
323688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
323788ed4aceSMatthew G Knepley 
323888ed4aceSMatthew G Knepley   PetscFunctionBegin;
323988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
324088ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
324188ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
324288ed4aceSMatthew G Knepley   dm->defaultSection = section;
3243af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
3244af122d2aSMatthew G Knepley   if (numFields) {
3245af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3246af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
3247af122d2aSMatthew G Knepley       const char *name;
3248af122d2aSMatthew G Knepley 
3249af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
3250af122d2aSMatthew G Knepley       ierr = PetscObjectSetName(dm->fields[f], name);CHKERRQ(ierr);
3251af122d2aSMatthew G Knepley     }
3252af122d2aSMatthew G Knepley   }
325388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
325488ed4aceSMatthew G Knepley }
325588ed4aceSMatthew G Knepley 
325688ed4aceSMatthew G Knepley #undef __FUNCT__
325788ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
325888ed4aceSMatthew G Knepley /*@
325988ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
326088ed4aceSMatthew G Knepley 
326188ed4aceSMatthew G Knepley   Input Parameter:
326288ed4aceSMatthew G Knepley . dm - The DM
326388ed4aceSMatthew G Knepley 
326488ed4aceSMatthew G Knepley   Output Parameter:
326588ed4aceSMatthew G Knepley . section - The PetscSection
326688ed4aceSMatthew G Knepley 
326788ed4aceSMatthew G Knepley   Level: intermediate
326888ed4aceSMatthew G Knepley 
326988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
327088ed4aceSMatthew G Knepley 
327188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
327288ed4aceSMatthew G Knepley @*/
327388ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) {
327488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
327588ed4aceSMatthew G Knepley 
327688ed4aceSMatthew G Knepley   PetscFunctionBegin;
327788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
327888ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
327988ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
328040e8a239SMatthew 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");
3281b21d0597SMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
328288ed4aceSMatthew G Knepley   }
328388ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
328488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
328588ed4aceSMatthew G Knepley }
328688ed4aceSMatthew G Knepley 
328788ed4aceSMatthew G Knepley #undef __FUNCT__
3288b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3289b21d0597SMatthew G Knepley /*@
3290b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3291b21d0597SMatthew G Knepley 
3292b21d0597SMatthew G Knepley   Input Parameters:
3293b21d0597SMatthew G Knepley + dm - The DM
3294b21d0597SMatthew G Knepley - section - The PetscSection
3295b21d0597SMatthew G Knepley 
3296b21d0597SMatthew G Knepley   Level: intermediate
3297b21d0597SMatthew G Knepley 
3298b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3299b21d0597SMatthew G Knepley 
3300b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3301b21d0597SMatthew G Knepley @*/
3302b21d0597SMatthew G Knepley PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section) {
3303b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3304b21d0597SMatthew G Knepley 
3305b21d0597SMatthew G Knepley   PetscFunctionBegin;
3306b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3307b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3308b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3309b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3310b21d0597SMatthew G Knepley }
3311b21d0597SMatthew G Knepley 
3312b21d0597SMatthew G Knepley #undef __FUNCT__
331388ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
331488ed4aceSMatthew G Knepley /*@
331588ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
331688ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
331788ed4aceSMatthew G Knepley 
331888ed4aceSMatthew G Knepley   Input Parameter:
331988ed4aceSMatthew G Knepley . dm - The DM
332088ed4aceSMatthew G Knepley 
332188ed4aceSMatthew G Knepley   Output Parameter:
332288ed4aceSMatthew G Knepley . sf - The PetscSF
332388ed4aceSMatthew G Knepley 
332488ed4aceSMatthew G Knepley   Level: intermediate
332588ed4aceSMatthew G Knepley 
332688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
332788ed4aceSMatthew G Knepley 
332888ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
332988ed4aceSMatthew G Knepley @*/
333088ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) {
333188ed4aceSMatthew G Knepley   PetscInt       nroots;
333288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
333388ed4aceSMatthew G Knepley 
333488ed4aceSMatthew G Knepley   PetscFunctionBegin;
333588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
333688ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
333788ed4aceSMatthew G Knepley   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
333888ed4aceSMatthew G Knepley   if (nroots < 0) {
333988ed4aceSMatthew G Knepley     PetscSection section, gSection;
334088ed4aceSMatthew G Knepley 
334188ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
334231ea6d37SMatthew G Knepley     if (section) {
334388ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
334488ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
334531ea6d37SMatthew G Knepley     } else {
334631ea6d37SMatthew G Knepley       *sf = PETSC_NULL;
334731ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
334831ea6d37SMatthew G Knepley     }
334988ed4aceSMatthew G Knepley   }
335088ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
335188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
335288ed4aceSMatthew G Knepley }
335388ed4aceSMatthew G Knepley 
335488ed4aceSMatthew G Knepley #undef __FUNCT__
335588ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
335688ed4aceSMatthew G Knepley /*@
335788ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
335888ed4aceSMatthew G Knepley 
335988ed4aceSMatthew G Knepley   Input Parameters:
336088ed4aceSMatthew G Knepley + dm - The DM
336188ed4aceSMatthew G Knepley - sf - The PetscSF
336288ed4aceSMatthew G Knepley 
336388ed4aceSMatthew G Knepley   Level: intermediate
336488ed4aceSMatthew G Knepley 
336588ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
336688ed4aceSMatthew G Knepley 
336788ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
336888ed4aceSMatthew G Knepley @*/
336988ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) {
337088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
337188ed4aceSMatthew G Knepley 
337288ed4aceSMatthew G Knepley   PetscFunctionBegin;
337388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
337488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
337588ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
337688ed4aceSMatthew G Knepley   dm->defaultSF = sf;
337788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
337888ed4aceSMatthew G Knepley }
337988ed4aceSMatthew G Knepley 
338088ed4aceSMatthew G Knepley #undef __FUNCT__
338188ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
338288ed4aceSMatthew G Knepley /*@C
338388ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
338488ed4aceSMatthew G Knepley   describing the data layout.
338588ed4aceSMatthew G Knepley 
338688ed4aceSMatthew G Knepley   Input Parameters:
338788ed4aceSMatthew G Knepley + dm - The DM
338888ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
338988ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
339088ed4aceSMatthew G Knepley 
339188ed4aceSMatthew G Knepley   Level: intermediate
339288ed4aceSMatthew G Knepley 
339388ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
339488ed4aceSMatthew G Knepley @*/
339588ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
339688ed4aceSMatthew G Knepley {
339788ed4aceSMatthew G Knepley   MPI_Comm        comm = ((PetscObject) dm)->comm;
339888ed4aceSMatthew G Knepley   PetscLayout     layout;
339988ed4aceSMatthew G Knepley   const PetscInt *ranges;
340088ed4aceSMatthew G Knepley   PetscInt       *local;
340188ed4aceSMatthew G Knepley   PetscSFNode    *remote;
340288ed4aceSMatthew G Knepley   PetscInt        pStart, pEnd, p, nroots, nleaves, l;
340388ed4aceSMatthew G Knepley   PetscMPIInt     size, rank;
340488ed4aceSMatthew G Knepley   PetscErrorCode  ierr;
340588ed4aceSMatthew G Knepley 
340688ed4aceSMatthew G Knepley   PetscFunctionBegin;
340788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
340888ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
340988ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
341088ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
341188ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
341288ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
341388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
341488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
341588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
341688ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
341788ed4aceSMatthew G Knepley   for (p = pStart, nleaves = 0; p < pEnd; ++p) {
34186636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
341988ed4aceSMatthew G Knepley 
34206636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
34216636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
34226636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
342388ed4aceSMatthew G Knepley   }
342488ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
342588ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
342688ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
34271f588964SMatthew G Knepley     const PetscInt *cind;
34286636e97aSMatthew G Knepley     PetscInt        dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
342988ed4aceSMatthew G Knepley 
343088ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
343188ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
343288ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
343388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
343488ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
34356636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
343688ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
34376636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
34386636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
34396636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
34406636e97aSMatthew 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", size, p, dof-cdof, dof);
34416636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
34426636e97aSMatthew G Knepley     }
344388ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
344488ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
344588ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
344688ed4aceSMatthew G Knepley     }
344788ed4aceSMatthew G Knepley     if (gdof < 0) {
34486636e97aSMatthew G Knepley       for(d = 0; d < gsize; ++d, ++l) {
344988ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
345088ed4aceSMatthew G Knepley 
345188ed4aceSMatthew G Knepley         for (r = 0; r < size; ++r) {
345288ed4aceSMatthew G Knepley           if ((offset >= ranges[r]) && (offset < ranges[r+1])) break;
345388ed4aceSMatthew G Knepley         }
345488ed4aceSMatthew G Knepley         remote[l].rank  = r;
345588ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
345688ed4aceSMatthew G Knepley       }
345788ed4aceSMatthew G Knepley     } else {
34586636e97aSMatthew G Knepley       for(d = 0; d < gsize; ++d, ++l) {
345988ed4aceSMatthew G Knepley         remote[l].rank  = rank;
346088ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
346188ed4aceSMatthew G Knepley       }
346288ed4aceSMatthew G Knepley     }
346388ed4aceSMatthew G Knepley   }
34646636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
346588ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
346688ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
346788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
346888ed4aceSMatthew G Knepley }
3469af122d2aSMatthew G Knepley 
3470af122d2aSMatthew G Knepley #undef __FUNCT__
3471b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3472b21d0597SMatthew G Knepley /*@
3473b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3474b21d0597SMatthew G Knepley 
3475b21d0597SMatthew G Knepley   Input Parameter:
3476b21d0597SMatthew G Knepley . dm - The DM
3477b21d0597SMatthew G Knepley 
3478b21d0597SMatthew G Knepley   Output Parameter:
3479b21d0597SMatthew G Knepley . sf - The PetscSF
3480b21d0597SMatthew G Knepley 
3481b21d0597SMatthew G Knepley   Level: intermediate
3482b21d0597SMatthew G Knepley 
3483b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3484b21d0597SMatthew G Knepley 
3485b21d0597SMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3486b21d0597SMatthew G Knepley @*/
3487b21d0597SMatthew G Knepley PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf) {
3488b21d0597SMatthew G Knepley   PetscFunctionBegin;
3489b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3490b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3491b21d0597SMatthew G Knepley   *sf = dm->sf;
3492b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3493b21d0597SMatthew G Knepley }
3494b21d0597SMatthew G Knepley 
3495b21d0597SMatthew G Knepley #undef __FUNCT__
3496af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetNumFields"
3497af122d2aSMatthew G Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
3498af122d2aSMatthew G Knepley {
3499af122d2aSMatthew G Knepley   PetscFunctionBegin;
3500af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3501af122d2aSMatthew G Knepley   PetscValidPointer(numFields, 2);
3502af122d2aSMatthew G Knepley   *numFields = dm->numFields;
3503af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3504af122d2aSMatthew G Knepley }
3505af122d2aSMatthew G Knepley 
3506af122d2aSMatthew G Knepley #undef __FUNCT__
3507af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3508af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3509af122d2aSMatthew G Knepley {
3510af122d2aSMatthew G Knepley   PetscInt       f;
3511af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3512af122d2aSMatthew G Knepley 
3513af122d2aSMatthew G Knepley   PetscFunctionBegin;
3514af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3515af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3516af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&dm->fields[f]);CHKERRQ(ierr);
3517af122d2aSMatthew G Knepley   }
3518af122d2aSMatthew G Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
3519af122d2aSMatthew G Knepley   dm->numFields = numFields;
3520af122d2aSMatthew G Knepley   ierr = PetscMalloc(dm->numFields * sizeof(PetscObject), &dm->fields);CHKERRQ(ierr);
3521af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3522af122d2aSMatthew G Knepley     ierr = PetscContainerCreate(((PetscObject) dm)->comm, (PetscContainer *) &dm->fields[f]);CHKERRQ(ierr);
3523af122d2aSMatthew G Knepley   }
3524af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3525af122d2aSMatthew G Knepley }
3526af122d2aSMatthew G Knepley 
3527af122d2aSMatthew G Knepley #undef __FUNCT__
3528af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3529af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3530af122d2aSMatthew G Knepley {
3531af122d2aSMatthew G Knepley   PetscFunctionBegin;
3532af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3533af122d2aSMatthew G Knepley   PetscValidPointer(field, 2);
3534af122d2aSMatthew G Knepley   if (!dm->fields) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "Fields have not been setup in this DM. Call DMSetNumFields()");
3535af122d2aSMatthew 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);
3536af122d2aSMatthew G Knepley   *field = dm->fields[f];
3537af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3538af122d2aSMatthew G Knepley }
35396636e97aSMatthew G Knepley 
35406636e97aSMatthew G Knepley #undef __FUNCT__
35416636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
35426636e97aSMatthew G Knepley /*@
35436636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
35446636e97aSMatthew G Knepley 
35456636e97aSMatthew G Knepley   Collective on DM
35466636e97aSMatthew G Knepley 
35476636e97aSMatthew G Knepley   Input Parameters:
35486636e97aSMatthew G Knepley + dm - the DM
35496636e97aSMatthew G Knepley - c - coordinate vector
35506636e97aSMatthew G Knepley 
35516636e97aSMatthew G Knepley   Note:
35526636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
35536636e97aSMatthew G Knepley 
35546636e97aSMatthew G Knepley   Level: intermediate
35556636e97aSMatthew G Knepley 
35566636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
35576636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
35586636e97aSMatthew G Knepley @*/
35596636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
35606636e97aSMatthew G Knepley {
35616636e97aSMatthew G Knepley   PetscErrorCode ierr;
35626636e97aSMatthew G Knepley 
35636636e97aSMatthew G Knepley   PetscFunctionBegin;
35646636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35656636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
35666636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
35676636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
35686636e97aSMatthew G Knepley   dm->coordinates = c;
35696636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
35706636e97aSMatthew G Knepley   PetscFunctionReturn(0);
35716636e97aSMatthew G Knepley }
35726636e97aSMatthew G Knepley 
35736636e97aSMatthew G Knepley #undef __FUNCT__
35746636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
35756636e97aSMatthew G Knepley /*@
35766636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
35776636e97aSMatthew G Knepley 
35786636e97aSMatthew G Knepley   Collective on DM
35796636e97aSMatthew G Knepley 
35806636e97aSMatthew G Knepley    Input Parameters:
35816636e97aSMatthew G Knepley +  dm - the DM
35826636e97aSMatthew G Knepley -  c - coordinate vector
35836636e97aSMatthew G Knepley 
35846636e97aSMatthew G Knepley   Note:
35856636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
35866636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
35876636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
35886636e97aSMatthew G Knepley 
35896636e97aSMatthew G Knepley   Level: intermediate
35906636e97aSMatthew G Knepley 
35916636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
35926636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
35936636e97aSMatthew G Knepley @*/
35946636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
35956636e97aSMatthew G Knepley {
35966636e97aSMatthew G Knepley   PetscErrorCode ierr;
35976636e97aSMatthew G Knepley 
35986636e97aSMatthew G Knepley   PetscFunctionBegin;
35996636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36006636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
36016636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
36026636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
36036636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
36046636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
36056636e97aSMatthew G Knepley   PetscFunctionReturn(0);
36066636e97aSMatthew G Knepley }
36076636e97aSMatthew G Knepley 
36086636e97aSMatthew G Knepley #undef __FUNCT__
36096636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
36106636e97aSMatthew G Knepley /*@
36116636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
36126636e97aSMatthew G Knepley 
36136636e97aSMatthew G Knepley   Not Collective
36146636e97aSMatthew G Knepley 
36156636e97aSMatthew G Knepley   Input Parameter:
36166636e97aSMatthew G Knepley . dm - the DM
36176636e97aSMatthew G Knepley 
36186636e97aSMatthew G Knepley   Output Parameter:
36196636e97aSMatthew G Knepley . c - global coordinate vector
36206636e97aSMatthew G Knepley 
36216636e97aSMatthew G Knepley   Note:
36226636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
36236636e97aSMatthew G Knepley 
36246636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
36256636e97aSMatthew G Knepley 
36266636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
36276636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
36286636e97aSMatthew G Knepley 
36296636e97aSMatthew G Knepley   Level: intermediate
36306636e97aSMatthew G Knepley 
36316636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
36326636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
36336636e97aSMatthew G Knepley @*/
36346636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
36356636e97aSMatthew G Knepley {
36366636e97aSMatthew G Knepley   PetscErrorCode ierr;
36376636e97aSMatthew G Knepley 
36386636e97aSMatthew G Knepley   PetscFunctionBegin;
36396636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36406636e97aSMatthew G Knepley   PetscValidPointer(c,2);
36411f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
36426636e97aSMatthew G Knepley     DM cdm;
36436636e97aSMatthew G Knepley 
36446636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
36456636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
36466636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
36476636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
36486636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
36496636e97aSMatthew G Knepley   }
36506636e97aSMatthew G Knepley   *c = dm->coordinates;
36516636e97aSMatthew G Knepley   PetscFunctionReturn(0);
36526636e97aSMatthew G Knepley }
36536636e97aSMatthew G Knepley 
36546636e97aSMatthew G Knepley #undef __FUNCT__
36556636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
36566636e97aSMatthew G Knepley /*@
36576636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
36586636e97aSMatthew G Knepley 
36596636e97aSMatthew G Knepley   Collective on DM
36606636e97aSMatthew G Knepley 
36616636e97aSMatthew G Knepley   Input Parameter:
36626636e97aSMatthew G Knepley . dm - the DM
36636636e97aSMatthew G Knepley 
36646636e97aSMatthew G Knepley   Output Parameter:
36656636e97aSMatthew G Knepley . c - coordinate vector
36666636e97aSMatthew G Knepley 
36676636e97aSMatthew G Knepley   Note:
36686636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
36696636e97aSMatthew G Knepley 
36706636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
36716636e97aSMatthew G Knepley 
36726636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
36736636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
36746636e97aSMatthew G Knepley 
36756636e97aSMatthew G Knepley   Level: intermediate
36766636e97aSMatthew G Knepley 
36776636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
36786636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
36796636e97aSMatthew G Knepley @*/
36806636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
36816636e97aSMatthew G Knepley {
36826636e97aSMatthew G Knepley   PetscErrorCode ierr;
36836636e97aSMatthew G Knepley 
36846636e97aSMatthew G Knepley   PetscFunctionBegin;
36856636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36866636e97aSMatthew G Knepley   PetscValidPointer(c,2);
36871f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
36886636e97aSMatthew G Knepley     DM cdm;
36896636e97aSMatthew G Knepley 
36906636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
36916636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
36926636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
36936636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
36946636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
36956636e97aSMatthew G Knepley   }
36966636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
36976636e97aSMatthew G Knepley   PetscFunctionReturn(0);
36986636e97aSMatthew G Knepley }
36996636e97aSMatthew G Knepley 
37006636e97aSMatthew G Knepley #undef __FUNCT__
37016636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
37026636e97aSMatthew G Knepley /*@
37036636e97aSMatthew G Knepley   DMGetCoordinateDM - Gets the DM that scatters between global and local coordinates
37046636e97aSMatthew G Knepley 
37056636e97aSMatthew G Knepley   Collective on DM
37066636e97aSMatthew G Knepley 
37076636e97aSMatthew G Knepley   Input Parameter:
37086636e97aSMatthew G Knepley . dm - the DM
37096636e97aSMatthew G Knepley 
37106636e97aSMatthew G Knepley   Output Parameter:
37116636e97aSMatthew G Knepley . cdm - coordinate DM
37126636e97aSMatthew G Knepley 
37136636e97aSMatthew G Knepley   Level: intermediate
37146636e97aSMatthew G Knepley 
37156636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
37166636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
37176636e97aSMatthew G Knepley @*/
37186636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
37196636e97aSMatthew G Knepley {
37206636e97aSMatthew G Knepley   PetscErrorCode ierr;
37216636e97aSMatthew G Knepley 
37226636e97aSMatthew G Knepley   PetscFunctionBegin;
37236636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
37246636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
37256636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
37266636e97aSMatthew G Knepley     if (!dm->ops->createcoordinatedm) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "Unable to create coordinates for this DM");
37276636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
37286636e97aSMatthew G Knepley   }
37296636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
37306636e97aSMatthew G Knepley   PetscFunctionReturn(0);
37316636e97aSMatthew G Knepley }
3732