xref: /petsc/src/dm/interface/dm.c (revision 0c010503e280889cfcd2455cff2d09d8471c4db0)
1 #define PETSCDM_DLL
2 
3 #include "private/dmimpl.h"     /*I      "petscda.h"     I*/
4 
5 /*
6    Provides an interface for functionality needed by the DAMG routines.
7    Currently this interface is supported by the DA and DMComposite objects
8 
9    Note: this is actually no such thing as a DM object, rather it is
10    the common set of functions shared by DA and DMComposite.
11 
12 */
13 
14 #undef __FUNCT__
15 #define __FUNCT__ "DMDestroy"
16 /*@
17     DMDestroy - Destroys a vector packer or DA.
18 
19     Collective on DM
20 
21     Input Parameter:
22 .   dm - the DM object to destroy
23 
24     Level: developer
25 
26 .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
27 
28 @*/
29 PetscErrorCode PETSCDM_DLLEXPORT DMDestroy(DM dm)
30 {
31   PetscErrorCode ierr;
32 
33   PetscFunctionBegin;
34   ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
35   PetscFunctionReturn(0);
36 }
37 
38 #undef __FUNCT__
39 #define __FUNCT__ "DMView"
40 /*@
41     DMView - Views a vector packer or DA.
42 
43     Collective on DM
44 
45     Input Parameter:
46 +   dm - the DM object to view
47 -   v - the viewer
48 
49     Level: developer
50 
51 .seealso DMDestroy(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
52 
53 @*/
54 PetscErrorCode PETSCDM_DLLEXPORT DMView(DM dm,PetscViewer v)
55 {
56   PetscErrorCode ierr;
57 
58   PetscFunctionBegin;
59   if (dm->ops->view) {
60     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
61   }
62   PetscFunctionReturn(0);
63 }
64 
65 #undef __FUNCT__
66 #define __FUNCT__ "DMCreateGlobalVector"
67 /*@
68     DMCreateGlobalVector - Creates a global vector from a DA or DMComposite object
69 
70     Collective on DM
71 
72     Input Parameter:
73 .   dm - the DM object
74 
75     Output Parameter:
76 .   vec - the global vector
77 
78     Level: developer
79 
80 .seealso DMDestroy(), DMView(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
81 
82 @*/
83 PetscErrorCode PETSCDM_DLLEXPORT DMCreateGlobalVector(DM dm,Vec *vec)
84 {
85   PetscErrorCode ierr;
86 
87   PetscFunctionBegin;
88   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
89   PetscFunctionReturn(0);
90 }
91 
92 #undef __FUNCT__
93 #define __FUNCT__ "DMCreateLocalVector"
94 /*@
95     DMCreateLocalVector - Creates a local vector from a DA or DMComposite object
96 
97     Not Collective
98 
99     Input Parameter:
100 .   dm - the DM object
101 
102     Output Parameter:
103 .   vec - the local vector
104 
105     Level: developer
106 
107 .seealso DMDestroy(), DMView(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
108 
109 @*/
110 PetscErrorCode PETSCDM_DLLEXPORT DMCreateLocalVector(DM dm,Vec *vec)
111 {
112   PetscErrorCode ierr;
113 
114   PetscFunctionBegin;
115   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
116   PetscFunctionReturn(0);
117 }
118 
119 #undef __FUNCT__
120 #define __FUNCT__ "DMGetInterpolation"
121 /*@
122     DMGetInterpolation - Gets interpolation matrix between two DA or DMComposite objects
123 
124     Collective on DM
125 
126     Input Parameter:
127 +   dm1 - the DM object
128 -   dm2 - the second, finer DM object
129 
130     Output Parameter:
131 +  mat - the interpolation
132 -  vec - the scaling (optional)
133 
134     Level: developer
135 
136 .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetColoring(), DMGetMatrix()
137 
138 @*/
139 PetscErrorCode PETSCDM_DLLEXPORT DMGetInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
140 {
141   PetscErrorCode ierr;
142 
143   PetscFunctionBegin;
144   ierr = (*dm1->ops->getinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
145   PetscFunctionReturn(0);
146 }
147 
148 #undef __FUNCT__
149 #define __FUNCT__ "DMGetInjection"
150 /*@
151     DMGetInjection - Gets injection matrix between two DA or DMComposite objects
152 
153     Collective on DM
154 
155     Input Parameter:
156 +   dm1 - the DM object
157 -   dm2 - the second, finer DM object
158 
159     Output Parameter:
160 .   ctx - the injection
161 
162     Level: developer
163 
164 .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetColoring(), DMGetMatrix(), DMGetInterpolation()
165 
166 @*/
167 PetscErrorCode PETSCDM_DLLEXPORT DMGetInjection(DM dm1,DM dm2,VecScatter *ctx)
168 {
169   PetscErrorCode ierr;
170 
171   PetscFunctionBegin;
172   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
173   PetscFunctionReturn(0);
174 }
175 
176 #undef __FUNCT__
177 #define __FUNCT__ "DMGetColoring"
178 /*@
179     DMGetColoring - Gets coloring for a DA or DMComposite
180 
181     Collective on DM
182 
183     Input Parameter:
184 +   dm - the DM object
185 .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
186 -   matype - either MATAIJ or MATBAIJ
187 
188     Output Parameter:
189 .   coloring - the coloring
190 
191     Level: developer
192 
193 .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetMatrix()
194 
195 @*/
196 PetscErrorCode PETSCDM_DLLEXPORT DMGetColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
197 {
198   PetscErrorCode ierr;
199 
200   PetscFunctionBegin;
201   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
202   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
203   PetscFunctionReturn(0);
204 }
205 
206 #undef __FUNCT__
207 #define __FUNCT__ "DMGetMatrix"
208 /*@C
209     DMGetMatrix - Gets empty Jacobian for a DA or DMComposite
210 
211     Collective on DM
212 
213     Input Parameter:
214 +   dm - the DM object
215 -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
216             any type which inherits from one of these (such as MATAIJ, MATLUSOL, etc.).
217 
218     Output Parameter:
219 .   mat - the empty Jacobian
220 
221     Level: developer
222 
223 .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetMatrix()
224 
225 @*/
226 PetscErrorCode PETSCDM_DLLEXPORT DMGetMatrix(DM dm, const MatType mtype,Mat *mat)
227 {
228   PetscErrorCode ierr;
229 
230   PetscFunctionBegin;
231   ierr = (*dm->ops->getmatrix)(dm,mtype,mat);CHKERRQ(ierr);
232   PetscFunctionReturn(0);
233 }
234 
235 #undef __FUNCT__
236 #define __FUNCT__ "DMRefine"
237 /*@
238     DMRefine - Refines a DM object
239 
240     Collective on DM
241 
242     Input Parameter:
243 +   dm - the DM object
244 -   comm - the communicator to contain the new DM object (or PETSC_NULL)
245 
246     Output Parameter:
247 .   dmf - the refined DM
248 
249     Level: developer
250 
251 .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
252 
253 @*/
254 PetscErrorCode PETSCDM_DLLEXPORT DMRefine(DM dm,MPI_Comm comm,DM *dmf)
255 {
256   PetscErrorCode ierr;
257 
258   PetscFunctionBegin;
259   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
260   PetscFunctionReturn(0);
261 }
262 
263 #undef __FUNCT__
264 #define __FUNCT__ "DMGlobalToLocalBegin"
265 /*@
266     DMGlobalToLocalBegin - Begins updating local vectors from global vector
267 
268     Neighbor-wise Collective on DM
269 
270     Input Parameters:
271 +   dm - the DM object
272 .   g - the global vector
273 .   mode - INSERT_VALUES or ADD_VALUES
274 -   l - the local vector
275 
276 
277     Level: beginner
278 
279 .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobal()
280 
281 @*/
282 PetscErrorCode PETSCDM_DLLEXPORT DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
283 {
284   PetscErrorCode ierr;
285 
286   PetscFunctionBegin;
287   ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode,l);CHKERRQ(ierr);
288   PetscFunctionReturn(0);
289 }
290 
291 #undef __FUNCT__
292 #define __FUNCT__ "DMGlobalToLocalEnd"
293 /*@
294     DMGlobalToLocalEnd - Ends updating local vectors from global vector
295 
296     Neighbor-wise Collective on DM
297 
298     Input Parameters:
299 +   dm - the DM object
300 .   g - the global vector
301 .   mode - INSERT_VALUES or ADD_VALUES
302 -   l - the local vector
303 
304 
305     Level: beginner
306 
307 .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobal()
308 
309 @*/
310 PetscErrorCode PETSCDM_DLLEXPORT DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
311 {
312   PetscErrorCode ierr;
313 
314   PetscFunctionBegin;
315   ierr = (*dm->ops->globaltolocalend)(dm,g,mode,l);CHKERRQ(ierr);
316   PetscFunctionReturn(0);
317 }
318 
319 #undef __FUNCT__
320 #define __FUNCT__ "DMLocalToGlobal"
321 /*@
322     DMLocalToGlobal - updates global vectors from local vectors
323 
324     Neighbor-wise Collective on DM
325 
326     Input Parameters:
327 +   dm - the DM object
328 .   g - the global vector
329 .   mode - INSERT_VALUES or ADD_VALUES
330 -   l - the local vector
331 
332 
333     Level: beginner
334 
335 .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
336 
337 @*/
338 PetscErrorCode PETSCDM_DLLEXPORT DMLocalToGlobal(DM dm,Vec g,InsertMode mode,Vec l)
339 {
340   PetscErrorCode ierr;
341 
342   PetscFunctionBegin;
343   ierr = (*dm->ops->localtoglobal)(dm,g,mode,l);CHKERRQ(ierr);
344   PetscFunctionReturn(0);
345 }
346 
347 #undef __FUNCT__
348 #define __FUNCT__ "DMComputeJacobianDefault"
349 /*@
350     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
351 
352     Collective on DM
353 
354     Input Parameter:
355 +   dm - the DM object
356 .   x - location to compute Jacobian at; may be ignored for linear problems
357 .   A - matrix that defines the operator for the linear solve
358 -   B - the matrix used to construct the preconditioner
359 
360     Level: developer
361 
362 .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
363          DMSetFunction()
364 
365 @*/
366 PetscErrorCode PETSCDM_DLLEXPORT DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
367 {
368   PetscErrorCode ierr;
369   PetscFunctionBegin;
370   *stflag = SAME_NONZERO_PATTERN;
371   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
372   if (A != B) {
373     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
374     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
375   }
376   PetscFunctionReturn(0);
377 }
378 
379 #undef __FUNCT__
380 #define __FUNCT__ "DMCoarsen"
381 /*@
382     DMCoarsen - Coarsens a DM object
383 
384     Collective on DM
385 
386     Input Parameter:
387 +   dm - the DM object
388 -   comm - the communicator to contain the new DM object (or PETSC_NULL)
389 
390     Output Parameter:
391 .   dmc - the coarsened DM
392 
393     Level: developer
394 
395 .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
396 
397 @*/
398 PetscErrorCode PETSCDM_DLLEXPORT DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
399 {
400   PetscErrorCode ierr;
401 
402   PetscFunctionBegin;
403   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
404   (*dmc)->ops->initialguess = dm->ops->initialguess;
405   (*dmc)->ops->function     = dm->ops->function;
406   (*dmc)->ops->functionj    = dm->ops->functionj;
407   if (dm->ops->jacobian != DMComputeJacobianDefault) {
408     (*dmc)->ops->jacobian     = dm->ops->jacobian;
409   }
410   PetscFunctionReturn(0);
411 }
412 
413 #undef __FUNCT__
414 #define __FUNCT__ "DMRefineHierarchy"
415 /*@C
416     DMRefineHierarchy - Refines a DM object, all levels at once
417 
418     Collective on DM
419 
420     Input Parameter:
421 +   dm - the DM object
422 -   nlevels - the number of levels of refinement
423 
424     Output Parameter:
425 .   dmf - the refined DM hierarchy
426 
427     Level: developer
428 
429 .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
430 
431 @*/
432 PetscErrorCode PETSCDM_DLLEXPORT DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
433 {
434   PetscErrorCode ierr;
435 
436   PetscFunctionBegin;
437   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
438   if (nlevels == 0) PetscFunctionReturn(0);
439   if (dm->ops->refinehierarchy) {
440     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
441   } else if (dm->ops->refine) {
442     PetscInt i;
443 
444     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
445     for (i=1; i<nlevels; i++) {
446       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
447     }
448   } else {
449     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
450   }
451   PetscFunctionReturn(0);
452 }
453 
454 #undef __FUNCT__
455 #define __FUNCT__ "DMCoarsenHierarchy"
456 /*@C
457     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
458 
459     Collective on DM
460 
461     Input Parameter:
462 +   dm - the DM object
463 -   nlevels - the number of levels of coarsening
464 
465     Output Parameter:
466 .   dmc - the coarsened DM hierarchy
467 
468     Level: developer
469 
470 .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
471 
472 @*/
473 PetscErrorCode PETSCDM_DLLEXPORT DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
474 {
475   PetscErrorCode ierr;
476 
477   PetscFunctionBegin;
478   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
479   if (nlevels == 0) PetscFunctionReturn(0);
480   PetscValidPointer(dmc,3);
481   if (dm->ops->coarsenhierarchy) {
482     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
483   } else if (dm->ops->coarsen) {
484     PetscInt i;
485 
486     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
487     for (i=1; i<nlevels; i++) {
488       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
489     }
490   } else {
491     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
492   }
493   PetscFunctionReturn(0);
494 }
495 
496 #undef __FUNCT__
497 #define __FUNCT__ "DMGetAggregates"
498 /*@
499    DMGetAggregates - Gets the aggregates that map between
500    grids associated with two DMs.
501 
502    Collective on DM
503 
504    Input Parameters:
505 +  dmc - the coarse grid DM
506 -  dmf - the fine grid DM
507 
508    Output Parameters:
509 .  rest - the restriction matrix (transpose of the projection matrix)
510 
511    Level: intermediate
512 
513 .keywords: interpolation, restriction, multigrid
514 
515 .seealso: DMRefine(), DMGetInjection(), DMGetInterpolation()
516 @*/
517 PetscErrorCode PETSCDM_DLLEXPORT DMGetAggregates(DM dmc, DM dmf, Mat *rest)
518 {
519   PetscErrorCode ierr;
520 
521   PetscFunctionBegin;
522   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
523   PetscFunctionReturn(0);
524 }
525 
526 #undef __FUNCT__
527 #define __FUNCT__ "DMSetContext"
528 /*@
529     DMSetContext - Set a user context into a DM object
530 
531     Not Collective
532 
533     Input Parameters:
534 +   dm - the DM object
535 -   ctx - the user context
536 
537     Level: intermediate
538 
539 .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext()
540 
541 @*/
542 PetscErrorCode PETSCDM_DLLEXPORT DMSetContext(DM dm,void *ctx)
543 {
544   PetscFunctionBegin;
545   dm->ctx = ctx;
546   PetscFunctionReturn(0);
547 }
548 
549 #undef __FUNCT__
550 #define __FUNCT__ "DMGetContext"
551 /*@
552     DMGetContext - Gets a user context from a DM object
553 
554     Not Collective
555 
556     Input Parameter:
557 .   dm - the DM object
558 
559     Output Parameter:
560 .   ctx - the user context
561 
562     Level: intermediate
563 
564 .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext()
565 
566 @*/
567 PetscErrorCode PETSCDM_DLLEXPORT DMGetContext(DM dm,void **ctx)
568 {
569   PetscFunctionBegin;
570   *ctx = dm->ctx;
571   PetscFunctionReturn(0);
572 }
573 
574 #undef __FUNCT__
575 #define __FUNCT__ "DMSetInitialGuess"
576 /*@
577     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
578 
579     Logically Collective on DM
580 
581     Input Parameter:
582 +   dm - the DM object to destroy
583 -   f - the function to compute the initial guess
584 
585     Level: intermediate
586 
587 .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetFunction(), DMSetJacobian()
588 
589 @*/
590 PetscErrorCode PETSCDM_DLLEXPORT DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
591 {
592   PetscFunctionBegin;
593   dm->ops->initialguess = f;
594   PetscFunctionReturn(0);
595 }
596 
597 #undef __FUNCT__
598 #define __FUNCT__ "DMSetFunction"
599 /*@
600     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
601 
602     Logically Collective on DM
603 
604     Input Parameter:
605 +   dm - the DM object
606 -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
607 
608     Level: intermediate
609 
610     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
611            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
612 
613 .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
614          DMSetJacobian()
615 
616 @*/
617 PetscErrorCode PETSCDM_DLLEXPORT DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
618 {
619   PetscFunctionBegin;
620   dm->ops->function = f;
621   if (f) {
622     dm->ops->functionj = f;
623   }
624   PetscFunctionReturn(0);
625 }
626 
627 #undef __FUNCT__
628 #define __FUNCT__ "DMSetJacobian"
629 /*@
630     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
631 
632     Logically Collective on DM
633 
634     Input Parameter:
635 +   dm - the DM object to destroy
636 -   f - the function to compute the matrix entries
637 
638     Level: intermediate
639 
640 .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
641          DMSetFunction()
642 
643 @*/
644 PetscErrorCode PETSCDM_DLLEXPORT DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
645 {
646   PetscFunctionBegin;
647   dm->ops->jacobian = f;
648   PetscFunctionReturn(0);
649 }
650 
651 #undef __FUNCT__
652 #define __FUNCT__ "DMComputeInitialGuess"
653 /*@
654     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
655 
656     Collective on DM
657 
658     Input Parameter:
659 +   dm - the DM object to destroy
660 -   x - the vector to hold the initial guess values
661 
662     Level: developer
663 
664 .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetRhs(), DMSetMat()
665 
666 @*/
667 PetscErrorCode PETSCDM_DLLEXPORT DMComputeInitialGuess(DM dm,Vec x)
668 {
669   PetscErrorCode ierr;
670   PetscFunctionBegin;
671   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
672   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
673   PetscFunctionReturn(0);
674 }
675 
676 #undef __FUNCT__
677 #define __FUNCT__ "DMHasInitialGuess"
678 /*@
679     DMHasInitialGuess - does the DM object have an initial guess function
680 
681     Not Collective
682 
683     Input Parameter:
684 .   dm - the DM object to destroy
685 
686     Output Parameter:
687 .   flg - PETSC_TRUE if function exists
688 
689     Level: developer
690 
691 .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetFunction(), DMSetJacobian()
692 
693 @*/
694 PetscErrorCode PETSCDM_DLLEXPORT DMHasInitialGuess(DM dm,PetscBool  *flg)
695 {
696   PetscFunctionBegin;
697   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
698   PetscFunctionReturn(0);
699 }
700 
701 #undef __FUNCT__
702 #define __FUNCT__ "DMHasFunction"
703 /*@
704     DMHasFunction - does the DM object have a function
705 
706     Not Collective
707 
708     Input Parameter:
709 .   dm - the DM object to destroy
710 
711     Output Parameter:
712 .   flg - PETSC_TRUE if function exists
713 
714     Level: developer
715 
716 .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetFunction(), DMSetJacobian()
717 
718 @*/
719 PetscErrorCode PETSCDM_DLLEXPORT DMHasFunction(DM dm,PetscBool  *flg)
720 {
721   PetscFunctionBegin;
722   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
723   PetscFunctionReturn(0);
724 }
725 
726 #undef __FUNCT__
727 #define __FUNCT__ "DMHasJacobian"
728 /*@
729     DMHasJacobian - does the DM object have a matrix function
730 
731     Not Collective
732 
733     Input Parameter:
734 .   dm - the DM object to destroy
735 
736     Output Parameter:
737 .   flg - PETSC_TRUE if function exists
738 
739     Level: developer
740 
741 .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetFunction(), DMSetJacobian()
742 
743 @*/
744 PetscErrorCode PETSCDM_DLLEXPORT DMHasJacobian(DM dm,PetscBool  *flg)
745 {
746   PetscFunctionBegin;
747   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
748   PetscFunctionReturn(0);
749 }
750 
751 #undef __FUNCT__
752 #define __FUNCT__ "DMComputeFunction"
753 /*@
754     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
755 
756     Collective on DM
757 
758     Input Parameter:
759 +   dm - the DM object to destroy
760 .   x - the location where the function is evaluationed, may be ignored for linear problems
761 -   b - the vector to hold the right hand side entries
762 
763     Level: developer
764 
765 .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
766          DMSetJacobian()
767 
768 @*/
769 PetscErrorCode PETSCDM_DLLEXPORT DMComputeFunction(DM dm,Vec x,Vec b)
770 {
771   PetscErrorCode ierr;
772   PetscFunctionBegin;
773   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
774   if (!x) x = dm->x;
775   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
776   PetscFunctionReturn(0);
777 }
778 
779 
780 #undef __FUNCT__
781 #define __FUNCT__ "DMComputeJacobian"
782 /*@
783     DMComputeJacobian - compute the matrix entries for the solver
784 
785     Collective on DM
786 
787     Input Parameter:
788 +   dm - the DM object
789 .   x - location to compute Jacobian at; may be ignored for linear problems
790 .   A - matrix that defines the operator for the linear solve
791 -   B - the matrix used to construct the preconditioner
792 
793     Level: developer
794 
795 .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
796          DMSetFunction()
797 
798 @*/
799 PetscErrorCode PETSCDM_DLLEXPORT DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
800 {
801   PetscErrorCode ierr;
802 
803   PetscFunctionBegin;
804   if (!dm->ops->jacobian) {
805     ISColoring    coloring;
806     MatFDColoring fd;
807 
808     ierr = DMGetColoring(dm,IS_COLORING_GHOSTED,MATAIJ,&coloring);CHKERRQ(ierr);
809     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
810     ierr = ISColoringDestroy(coloring);CHKERRQ(ierr);
811     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
812     dm->fd = fd;
813     dm->ops->jacobian = DMComputeJacobianDefault;
814 
815     if (!dm->x) {
816       ierr = MatGetVecs(B,&dm->x,PETSC_NULL);CHKERRQ(ierr);
817     }
818   }
819   if (!x) x = dm->x;
820   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
821   PetscFunctionReturn(0);
822 }
823