xref: /petsc/src/binding/petsc4py/src/petsc4py/PETSc/DMShell.pyx (revision 552edb6364df478b294b3111f33a8f37ca096b20)
1cdef class DMShell(DM):
2    """A shell DM object, used to manage user-defined problem data."""
3
4    def create(self, comm: Comm | None = None) -> Self:
5        """Creates a shell DM object.
6
7        Collective.
8
9        Parameters
10        ----------
11        comm
12            MPI communicator, defaults to `Sys.getDefaultComm`.
13
14        See Also
15        --------
16        petsc.DMShellCreate
17
18        """
19        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
20        cdef PetscDM newdm = NULL
21        CHKERR(DMShellCreate(ccomm, &newdm))
22        CHKERR(PetscCLEAR(self.obj)); self.dm = newdm
23        return self
24
25    def setMatrix(self, Mat mat) -> None:
26        """Set a template matrix.
27
28        Collective.
29
30        Parameters
31        ----------
32        mat
33            The template matrix.
34
35        See Also
36        --------
37        petsc.DMShellSetMatrix
38
39        """
40        CHKERR(DMShellSetMatrix(self.dm, mat.mat))
41
42    def setGlobalVector(self, Vec gv) -> None:
43        """Set a template global vector.
44
45        Logically collective.
46
47        Parameters
48        ----------
49        gv
50            Template vector.
51
52        See Also
53        --------
54        setLocalVector, petsc.DMShellSetGlobalVector
55
56        """
57        CHKERR(DMShellSetGlobalVector(self.dm, gv.vec))
58
59    def setLocalVector(self, Vec lv) -> None:
60        """Set a template local vector.
61
62        Logically collective.
63
64        Parameters
65        ----------
66        lv
67            Template vector.
68
69        See Also
70        --------
71        setGlobalVector, petsc.DMShellSetLocalVector
72
73        """
74        CHKERR(DMShellSetLocalVector(self.dm, lv.vec))
75
76    def setCreateGlobalVector(
77        self,
78        create_gvec: Callable[[DM], Vec] | None,
79        args: tuple[Any, ...] | None = None,
80        kargs: dict[str, Any] | None = None) -> None:
81        """Set the routine to create a global vector.
82
83        Logically collective.
84
85        Parameters
86        ----------
87        create_gvec
88            The creation routine.
89        args
90            Additional positional arguments for ``create_gvec``.
91        kargs
92            Additional keyword arguments for ``create_gvec``.
93
94        See Also
95        --------
96        setCreateLocalVector, petsc.DMShellSetCreateGlobalVector
97
98        """
99        if create_gvec is not None:
100            if args  is None: args = ()
101            if kargs is None: kargs = {}
102            context = (create_gvec, args, kargs)
103            self.set_attr('__create_global_vector__', context)
104            CHKERR(DMShellSetCreateGlobalVector(self.dm, DMSHELL_CreateGlobalVector))
105        else:
106            CHKERR(DMShellSetCreateGlobalVector(self.dm, NULL))
107
108    def setCreateLocalVector(
109        self,
110        create_lvec: Callable[[DM], Vec] | None,
111        args: tuple[Any, ...] | None = None,
112        kargs: dict[str, Any] | None = None) -> None:
113        """Set the routine to create a local vector.
114
115        Logically collective.
116
117        Parameters
118        ----------
119        create_lvec
120            The creation routine.
121        args
122            Additional positional arguments for ``create_lvec``.
123        kargs
124            Additional keyword arguments for ``create_lvec``.
125
126        See Also
127        --------
128        setCreateGlobalVector, petsc.DMShellSetCreateLocalVector
129
130        """
131        if create_lvec is not None:
132            if args  is None: args = ()
133            if kargs is None: kargs = {}
134            context = (create_lvec, args, kargs)
135            self.set_attr('__create_local_vector__', context)
136            CHKERR(DMShellSetCreateLocalVector(self.dm, DMSHELL_CreateLocalVector))
137        else:
138            CHKERR(DMShellSetCreateLocalVector(self.dm, NULL))
139
140    def setGlobalToLocal(
141        self,
142        begin: Callable[[DM, Vec, InsertMode, Vec], None] | None,
143        end: Callable[[DM, Vec, InsertMode, Vec], None] | None,
144        begin_args: tuple[Any, ...] | None = None,
145        begin_kargs: dict[str, Any] | None = None,
146        end_args: tuple[Any, ...] | None = None,
147        end_kargs: dict[str, Any] | None = None) -> None:
148        """Set the routines used to perform a global to local scatter.
149
150        Logically collective.
151
152        Parameters
153        ----------
154        dm
155            The `DMShell`.
156        begin
157            The routine which begins the global to local scatter.
158        end
159            The routine which ends the global to local scatter.
160        begin_args
161            Additional positional arguments for ``begin``.
162        begin_kargs
163            Additional keyword arguments for ``begin``.
164        end_args
165            Additional positional arguments for ``end``.
166        end_kargs
167            Additional keyword arguments for ``end``.
168
169        See Also
170        --------
171        petsc.DMShellSetGlobalToLocal
172
173        """
174        cdef PetscDMShellXToYFunction cbegin = NULL, cend = NULL
175        if begin is not None:
176            if begin_args  is None: begin_args = ()
177            if begin_kargs is None: begin_kargs = {}
178            context = (begin, begin_args, begin_kargs)
179            self.set_attr('__g2l_begin__', context)
180            cbegin = &DMSHELL_GlobalToLocalBegin
181        if end is not None:
182            if end_args  is None: end_args = ()
183            if end_kargs is None: end_kargs = {}
184            context = (end, end_args, end_kargs)
185            self.set_attr('__g2l_end__', context)
186            cend = &DMSHELL_GlobalToLocalEnd
187        CHKERR(DMShellSetGlobalToLocal(self.dm, cbegin, cend))
188
189    def setGlobalToLocalVecScatter(self, Scatter gtol) -> None:
190        """Set a `Scatter` context for global to local communication.
191
192        Logically collective.
193
194        Parameters
195        ----------
196        gtol
197            The global to local `Scatter` context.
198
199        See Also
200        --------
201        petsc.DMShellSetGlobalToLocalVecScatter
202
203        """
204        CHKERR(DMShellSetGlobalToLocalVecScatter(self.dm, gtol.sct))
205
206    def setLocalToGlobal(
207        self,
208        begin: Callable[[DM, Vec, InsertMode, Vec], None] | None,
209        end: Callable[[DM, Vec, InsertMode, Vec], None] | None,
210        begin_args: tuple[Any, ...] | None = None,
211        begin_kargs: dict[str, Any] | None = None,
212        end_args: tuple[Any, ...] | None = None,
213        end_kargs: dict[str, Any] | None = None) -> None:
214        """Set the routines used to perform a local to global scatter.
215
216        Logically collective.
217
218        Parameters
219        ----------
220        begin
221            The routine which begins the local to global scatter.
222        end
223            The routine which ends the local to global scatter.
224        begin_args
225            Additional positional arguments for ``begin``.
226        begin_kargs
227            Additional keyword arguments for ``begin``.
228        end_args
229            Additional positional arguments for ``end``.
230        end_kargs
231            Additional keyword arguments for ``end``.
232
233        See Also
234        --------
235        petsc.DMShellSetLocalToGlobal
236
237        """
238        cdef PetscDMShellXToYFunction cbegin = NULL, cend = NULL
239        if begin is not None:
240            if begin_args  is None: begin_args = ()
241            if begin_kargs is None: begin_kargs = {}
242            context = (begin, begin_args, begin_kargs)
243            self.set_attr('__l2g_begin__', context)
244            cbegin = &DMSHELL_LocalToGlobalBegin
245        if end is not None:
246            if end_args  is None: end_args = ()
247            if end_kargs is None: end_kargs = {}
248            context = (end, end_args, end_kargs)
249            self.set_attr('__l2g_end__', context)
250            cend = &DMSHELL_LocalToGlobalEnd
251        CHKERR(DMShellSetLocalToGlobal(self.dm, cbegin, cend))
252
253    def setLocalToGlobalVecScatter(self, Scatter ltog) -> None:
254        """Set a `Scatter` context for local to global communication.
255
256        Logically collective.
257
258        Parameters
259        ----------
260        ltog
261            The local to global `Scatter` context.
262
263        See Also
264        --------
265        petsc.DMShellSetLocalToGlobalVecScatter
266
267        """
268        CHKERR(DMShellSetLocalToGlobalVecScatter(self.dm, ltog.sct))
269
270    def setLocalToLocal(
271        self,
272        begin: Callable[[DM, Vec, InsertMode, Vec], None] | None,
273        end: Callable[[DM, Vec, InsertMode, Vec], None] | None,
274        begin_args: tuple[Any, ...] | None = None,
275        begin_kargs: dict[str, Any] | None = None,
276        end_args: tuple[Any, ...] | None = None,
277        end_kargs: dict[str, Any] | None = None) -> None:
278        """Set the routines used to perform a local to local scatter.
279
280        Logically collective.
281
282        Parameters
283        ----------
284        begin
285            The routine which begins the local to local scatter.
286        end
287            The routine which ends the local to local scatter.
288        begin_args
289            Additional positional arguments for ``begin``.
290        begin_kargs
291            Additional keyword arguments for ``begin``.
292        end_args
293            Additional positional arguments for ``end``.
294        end_kargs
295            Additional keyword arguments for ``end``.
296
297        See Also
298        --------
299        petsc.DMShellSetLocalToLocal
300
301        """
302        cdef PetscDMShellXToYFunction cbegin = NULL, cend = NULL
303        cbegin = NULL
304        cend = NULL
305        if begin is not None:
306            if begin_args  is None: begin_args = ()
307            if begin_kargs is None: begin_kargs = {}
308            context = (begin, begin_args, begin_kargs)
309            self.set_attr('__l2l_begin__', context)
310            cbegin = &DMSHELL_LocalToLocalBegin
311        if end is not None:
312            if end_args  is None: end_args = ()
313            if end_kargs is None: end_kargs = {}
314            context = (end, end_args, end_kargs)
315            self.set_attr('__l2l_end__', context)
316            cend = &DMSHELL_LocalToLocalEnd
317        CHKERR(DMShellSetLocalToLocal(self.dm, cbegin, cend))
318
319    def setLocalToLocalVecScatter(self, Scatter ltol) -> None:
320        """Set a ``Scatter`` context for local to local communication.
321
322        Logically collective.
323
324        Parameters
325        ----------
326        ltol
327            The local to local ``Scatter`` context.
328
329        See Also
330        --------
331        petsc.DMShellSetLocalToLocalVecScatter
332
333        """
334        CHKERR(DMShellSetLocalToLocalVecScatter(self.dm, ltol.sct))
335
336    def setCreateMatrix(
337        self,
338        create_matrix: Callable[[DM], Mat] | None,
339        args: tuple[Any, ...] | None = None,
340        kargs: dict[str, Any] | None = None) -> None:
341        """Set the routine to create a matrix.
342
343        Logically collective.
344
345        Parameters
346        ----------
347        create_matrix
348            The function to create a matrix.
349        args
350            Additional positional arguments for ``create_matrix``.
351        kargs
352            Additional keyword arguments for ``create_matrix``.
353
354        See Also
355        --------
356        petsc.DMShellSetCreateMatrix
357
358        """
359        if create_matrix is not None:
360            if args  is None: args  = ()
361            if kargs is None: kargs = {}
362            context = (create_matrix, args, kargs)
363            self.set_attr('__create_matrix__', context)
364            CHKERR(DMShellSetCreateMatrix(self.dm, DMSHELL_CreateMatrix))
365        else:
366            CHKERR(DMShellSetCreateMatrix(self.dm, NULL))
367
368    def setCoarsen(
369        self,
370        coarsen: Callable[[DM, Comm], DM] | None,
371        args: tuple[Any, ...] | None = None,
372        kargs: dict[str, Any] | None = None) -> None:
373        """Set the routine used to coarsen the `DMShell`.
374
375        Logically collective.
376
377        Parameters
378        ----------
379        coarsen
380            The routine which coarsens the DM.
381        args
382            Additional positional arguments for ``coarsen``.
383        kargs
384            Additional keyword arguments for ``coarsen``.
385
386        See Also
387        --------
388        setRefine, petsc.DMShellSetCoarsen
389
390        """
391        if coarsen is not None:
392            if args  is None: args  = ()
393            if kargs is None: kargs = {}
394            context = (coarsen, args, kargs)
395            self.set_attr('__coarsen__', context)
396            CHKERR(DMShellSetCoarsen(self.dm, DMSHELL_Coarsen))
397        else:
398            CHKERR(DMShellSetCoarsen(self.dm, NULL))
399
400    def setRefine(
401        self,
402        refine: Callable[[DM, Comm], DM] | None,
403        args: tuple[Any, ...] | None = None,
404        kargs: dict[str, Any] | None = None) -> None:
405        """Set the routine used to refine the `DMShell`.
406
407        Logically collective.
408
409        Parameters
410        ----------
411        refine
412            The routine which refines the DM.
413        args
414            Additional positional arguments for ``refine``.
415        kargs
416            Additional keyword arguments for ``refine``.
417
418        See Also
419        --------
420        setCoarsen, petsc.DMShellSetRefine
421
422        """
423        if refine is not None:
424            if args  is None: args  = ()
425            if kargs is None: kargs = {}
426            context = (refine, args, kargs)
427            self.set_attr('__refine__', context)
428            CHKERR(DMShellSetRefine(self.dm, DMSHELL_Refine))
429        else:
430            CHKERR(DMShellSetRefine(self.dm, NULL))
431
432    def setCreateInterpolation(
433        self,
434        create_interpolation: Callable[[DM, DM], tuple[Mat, Vec]] | None,
435        args: tuple[Any, ...] | None = None,
436        kargs: dict[str, Any] | None = None) -> None:
437        """Set the routine used to create the interpolation operator.
438
439        Logically collective.
440
441        Parameters
442        ----------
443        create_interpolation
444            The routine to create the interpolation.
445        args
446            Additional positional arguments for ``create_interpolation``.
447        kargs
448            Additional keyword arguments for ``create_interpolation``.
449
450        See Also
451        --------
452        petsc.DMShellSetCreateInterpolation
453
454        """
455        if create_interpolation is not None:
456            if args  is None: args  = ()
457            if kargs is None: kargs = {}
458            context = (create_interpolation, args, kargs)
459            self.set_attr('__create_interpolation__', context)
460            CHKERR(DMShellSetCreateInterpolation(self.dm, DMSHELL_CreateInterpolation))
461        else:
462            CHKERR(DMShellSetCreateInterpolation(self.dm, NULL))
463
464    def setCreateInjection(
465        self,
466        create_injection: Callable[[DM, DM], Mat] | None,
467        args: tuple[Any, ...] | None = None,
468        kargs: dict[str, Any] | None = None) -> None:
469        """Set the routine used to create the injection operator.
470
471        Logically collective.
472
473        Parameters
474        ----------
475        create_injection
476            The routine to create the injection.
477        args
478            Additional positional arguments for ``create_injection``.
479        kargs
480            Additional keyword arguments for ``create_injection``.
481
482        See Also
483        --------
484        petsc.DMShellSetCreateInjection
485
486        """
487        if create_injection is not None:
488            if args  is None: args  = ()
489            if kargs is None: kargs = {}
490            context = (create_injection, args, kargs)
491            self.set_attr('__create_injection__', context)
492            CHKERR(DMShellSetCreateInjection(self.dm, DMSHELL_CreateInjection))
493        else:
494            CHKERR(DMShellSetCreateInjection(self.dm, NULL))
495
496    def setCreateRestriction(
497        self,
498        create_restriction: Callable[[DM, DM], Mat] | None,
499        args: tuple[Any, ...] | None = None,
500        kargs: dict[str, Any] | None = None) -> None:
501        """Set the routine used to create the restriction operator.
502
503        Logically collective.
504
505        Parameters
506        ----------
507        create_restriction
508            The routine to create the restriction
509        args
510            Additional positional arguments for ``create_restriction``.
511        kargs
512            Additional keyword arguments for ``create_restriction``.
513
514        See Also
515        --------
516        petsc.DMShellSetCreateRestriction
517
518        """
519        if create_restriction is not None:
520            if args  is None: args  = ()
521            if kargs is None: kargs = {}
522            context = (create_restriction, args, kargs)
523            self.set_attr('__create_restriction__', context)
524            CHKERR(DMShellSetCreateRestriction(self.dm, DMSHELL_CreateRestriction))
525        else:
526            CHKERR(DMShellSetCreateRestriction(self.dm, NULL))
527
528    def setCreateFieldDecomposition(
529        self,
530        decomp: Callable[[DM], tuple[list[str] | None, list[IS] | None, list[DM] | None]] | None,
531        args: tuple[Any, ...] | None = None,
532        kargs: dict[str, Any] | None = None) -> None:
533        """Set the routine used to create a field decomposition.
534
535        Logically collective.
536
537        Parameters
538        ----------
539        decomp
540            The routine to create the decomposition.
541        args
542            Additional positional arguments for ``decomp``.
543        kargs
544            Additional keyword arguments for ``decomp``.
545
546        See Also
547        --------
548        petsc.DMShellSetCreateFieldDecomposition
549
550        """
551        if decomp is not None:
552            if args  is None: args = ()
553            if kargs is None: kargs = {}
554            context = (decomp, args, kargs)
555            self.set_attr('__create_field_decomp__', context)
556            CHKERR(DMShellSetCreateFieldDecomposition(self.dm, DMSHELL_CreateFieldDecomposition))
557        else:
558            CHKERR(DMShellSetCreateFieldDecomposition(self.dm, NULL))
559
560    def setCreateDomainDecomposition(
561        self,
562        decomp: Callable[[DM], tuple[list[str] | None, list[IS] | None, list[IS] | None, list[DM] | None]] | None,
563        args: tuple[Any, ...] | None = None,
564        kargs: dict[str, Any] | None = None) -> None:
565        """Set the routine used to create a domain decomposition.
566
567        Logically collective.
568
569        Parameters
570        ----------
571        decomp
572            The routine to create the decomposition.
573        args
574            Additional positional arguments for ``decomp``.
575        kargs
576            Additional keyword arguments for ``decomp``.
577
578        See Also
579        --------
580        petsc.DMShellSetCreateDomainDecomposition
581
582        """
583        if decomp is not None:
584            if args  is None: args = ()
585            if kargs is None: kargs = {}
586            context = (decomp, args, kargs)
587            self.set_attr('__create_domain_decomp__', context)
588            CHKERR(DMShellSetCreateDomainDecomposition(self.dm, DMSHELL_CreateDomainDecomposition))
589        else:
590            CHKERR(DMShellSetCreateDomainDecomposition(self.dm, NULL))
591
592    def setCreateDomainDecompositionScatters(
593        self,
594        scatter: Callable[[DM, list[DM]], tuple[list[Scatter], list[Scatter], list[Scatter]]] | None,
595        args: tuple[Any, ...] | None = None,
596        kargs: dict[str, Any] | None = None) -> None:
597        """Set the routine used to create the scatter contexts for domain decomposition.
598
599        Logically collective.
600
601        Parameters
602        ----------
603        scatter
604            The routine to create the scatters.
605        args
606            Additional positional arguments for ``scatter``.
607        kargs
608            Additional keyword arguments for ``scatter``.
609
610        See Also
611        --------
612        petsc.DMShellSetCreateDomainDecompositionScatters
613
614        """
615        if scatter is not None:
616            if args  is None: args = ()
617            if kargs is None: kargs = {}
618            context = (scatter, args, kargs)
619            self.set_attr('__create_domain_decomp_scatters__', context)
620            CHKERR(DMShellSetCreateDomainDecompositionScatters(self.dm, DMSHELL_CreateDomainDecompositionScatters))
621        else:
622            CHKERR(DMShellSetCreateDomainDecompositionScatters(self.dm, NULL))
623
624    def setCreateSubDM(
625        self,
626        create_subdm: Callable[[DM, Sequence[int]], tuple[IS, DM]] | None,
627        args: tuple[Any, ...] | None = None,
628        kargs: dict[str, Any] | None = None) -> None:
629        """Set the routine used to create a sub DM from the `DMShell`.
630
631        Logically collective.
632
633        Parameters
634        ----------
635        subdm
636            The routine to create the decomposition.
637        args
638            Additional positional arguments for ``subdm``.
639        kargs
640            Additional keyword arguments for ``subdm``.
641
642        See Also
643        --------
644        petsc.DMShellSetCreateSubDM
645
646        """
647        if create_subdm is not None:
648            if args  is None: args = ()
649            if kargs is None: kargs = {}
650            context = (create_subdm, args, kargs)
651            self.set_attr('__create_subdm__', context)
652            CHKERR(DMShellSetCreateSubDM(self.dm, DMSHELL_CreateSubDM))
653        else:
654            CHKERR(DMShellSetCreateSubDM(self.dm, NULL))
655