xref: /petsc/src/binding/petsc4py/src/petsc4py/PETSc/Viewer.pyx (revision 6d8694c4fbab79f9439f1ad13c0386ba7ee1ca4b)
1# --------------------------------------------------------------------
2
3class ViewerType(object):
4    """Viewer type."""
5    SOCKET      = S_(PETSCVIEWERSOCKET)
6    ASCII       = S_(PETSCVIEWERASCII)
7    BINARY      = S_(PETSCVIEWERBINARY)
8    STRING      = S_(PETSCVIEWERSTRING)
9    DRAW        = S_(PETSCVIEWERDRAW)
10    VU          = S_(PETSCVIEWERVU)
11    MATHEMATICA = S_(PETSCVIEWERMATHEMATICA)
12    HDF5        = S_(PETSCVIEWERHDF5)
13    VTK         = S_(PETSCVIEWERVTK)
14    MATLAB      = S_(PETSCVIEWERMATLAB)
15    SAWS        = S_(PETSCVIEWERSAWS)
16    GLVIS       = S_(PETSCVIEWERGLVIS)
17    ADIOS       = S_(PETSCVIEWERADIOS)
18    EXODUSII    = S_(PETSCVIEWEREXODUSII)
19    PYTHON      = S_(PETSCVIEWERPYTHON)
20    PYVISTA     = S_(PETSCVIEWERPYVISTA)
21
22
23class ViewerFormat(object):
24    """Viewer format."""
25    DEFAULT           = PETSC_VIEWER_DEFAULT
26    ASCII_MATLAB      = PETSC_VIEWER_ASCII_MATLAB
27    ASCII_MATHEMATICA = PETSC_VIEWER_ASCII_MATHEMATICA
28    ASCII_IMPL        = PETSC_VIEWER_ASCII_IMPL
29    ASCII_INFO        = PETSC_VIEWER_ASCII_INFO
30    ASCII_INFO_DETAIL = PETSC_VIEWER_ASCII_INFO_DETAIL
31    ASCII_COMMON      = PETSC_VIEWER_ASCII_COMMON
32    ASCII_SYMMODU     = PETSC_VIEWER_ASCII_SYMMODU
33    ASCII_INDEX       = PETSC_VIEWER_ASCII_INDEX
34    ASCII_DENSE       = PETSC_VIEWER_ASCII_DENSE
35    ASCII_MATRIXMARKET= PETSC_VIEWER_ASCII_MATRIXMARKET
36    ASCII_PCICE       = PETSC_VIEWER_ASCII_PCICE
37    ASCII_PYTHON      = PETSC_VIEWER_ASCII_PYTHON
38    ASCII_FACTOR_INFO = PETSC_VIEWER_ASCII_FACTOR_INFO
39    ASCII_LATEX       = PETSC_VIEWER_ASCII_LATEX
40    ASCII_XML         = PETSC_VIEWER_ASCII_XML
41    ASCII_GLVIS       = PETSC_VIEWER_ASCII_GLVIS
42    ASCII_CSV         = PETSC_VIEWER_ASCII_CSV
43    DRAW_BASIC        = PETSC_VIEWER_DRAW_BASIC
44    DRAW_LG           = PETSC_VIEWER_DRAW_LG
45    DRAW_LG_XRANGE    = PETSC_VIEWER_DRAW_LG_XRANGE
46    DRAW_CONTOUR      = PETSC_VIEWER_DRAW_CONTOUR
47    DRAW_PORTS        = PETSC_VIEWER_DRAW_PORTS
48    VTK_VTS           = PETSC_VIEWER_VTK_VTS
49    VTK_VTR           = PETSC_VIEWER_VTK_VTR
50    VTK_VTU           = PETSC_VIEWER_VTK_VTU
51    BINARY_MATLAB     = PETSC_VIEWER_BINARY_MATLAB
52    NATIVE            = PETSC_VIEWER_NATIVE
53    HDF5_PETSC        = PETSC_VIEWER_HDF5_PETSC
54    HDF5_VIZ          = PETSC_VIEWER_HDF5_VIZ
55    HDF5_XDMF         = PETSC_VIEWER_HDF5_XDMF
56    HDF5_MAT          = PETSC_VIEWER_HDF5_MAT
57    NOFORMAT          = PETSC_VIEWER_NOFORMAT
58    LOAD_BALANCE      = PETSC_VIEWER_LOAD_BALANCE
59    FAILED            = PETSC_VIEWER_FAILED
60
61
62class ViewerFileMode(object):
63    """Viewer file mode."""
64    # native
65    READ          = PETSC_FILE_MODE_READ
66    WRITE         = PETSC_FILE_MODE_WRITE
67    APPEND        = PETSC_FILE_MODE_APPEND
68    UPDATE        = PETSC_FILE_MODE_UPDATE
69    APPEND_UPDATE = PETSC_FILE_MODE_APPEND_UPDATE
70    # aliases
71    R, W, A, U = READ, WRITE, APPEND, UPDATE
72    AU = UA    = APPEND_UPDATE
73
74
75class ViewerDrawSize(object):
76    """Window size."""
77    # native
78    FULL_SIZE    = PETSC_DRAW_FULL_SIZE
79    HALF_SIZE    = PETSC_DRAW_HALF_SIZE
80    THIRD_SIZE   = PETSC_DRAW_THIRD_SIZE
81    QUARTER_SIZE = PETSC_DRAW_QUARTER_SIZE
82    # aliases
83    FULL    = FULL_SIZE
84    HALF    = HALF_SIZE
85    THIRD   = THIRD_SIZE
86    QUARTER = QUARTER_SIZE
87
88# --------------------------------------------------------------------
89
90
91cdef class Viewer(Object):
92    """Viewer object.
93
94    Viewer is described in the `PETSc manual <petsc:sec_viewers>`.
95
96    Viewers can be called as functions where the argument specified
97    is the PETSc object to be viewed. See the example below.
98
99    Examples
100    --------
101    >>> from petsc4py import PETSc
102    >>> u = PETSc.Vec().createWithArray([1,2])
103    >>> v = PETSc.Viewer()
104    >>> v(u)
105    Vec Object: 1 MPI process
106      type: seq
107    1.
108    2.
109
110    See Also
111    --------
112    petsc.PetscViewer
113
114    """
115
116    Type   = ViewerType
117    Format = ViewerFormat
118    FileMode = ViewerFileMode
119    DrawSize = ViewerDrawSize
120
121    # backward compatibility
122    Mode = ViewerFileMode
123    Size = ViewerFileMode
124
125    #
126
127    def __cinit__(self):
128        self.obj = <PetscObject*> &self.vwr
129        self.vwr = NULL
130
131    def __call__(self, Object obj) -> None:
132        """View a generic object."""
133        assert obj.obj != NULL
134        CHKERR(PetscObjectView(obj.obj[0], self.vwr))
135
136    #
137
138    def view(self, obj: Viewer | Object | None = None) -> None:
139        """View the viewer.
140
141        Collective.
142
143        Parameters
144        ----------
145        obj
146            A `Viewer` instance or `None` for the default viewer.
147            If none of the above applies, it assumes ``obj`` is an instance of `Object`
148            and it calls the generic view for ``obj``.
149
150        Notes
151        -----
152
153        See Also
154        --------
155        petsc.PetscViewerView
156
157        """
158        if obj is None:
159            CHKERR(PetscViewerView(self.vwr, NULL))
160        elif isinstance(obj, Viewer):
161            CHKERR(PetscViewerView(self.vwr, (<Viewer?>obj).vwr))
162        else:
163            assert (<Object?>obj).obj != NULL
164            CHKERR(PetscObjectView((<Object?>obj).obj[0], self.vwr))
165
166    def destroy(self) -> Self:
167        """Destroy the viewer.
168
169        Collective.
170
171        See Also
172        --------
173        petsc.PetscViewerDestroy
174
175        """
176        CHKERR(PetscViewerDestroy(&self.vwr))
177        return self
178
179    def create(self, comm: Comm | None = None) -> Self:
180        """Create a viewer.
181
182        Collective.
183
184        Parameters
185        ----------
186        comm
187            MPI communicator, defaults to `Sys.getDefaultComm`.
188
189        See Also
190        --------
191        Sys.getDefaultComm, petsc.PetscViewerCreate
192
193        """
194        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
195        cdef PetscViewer newvwr = NULL
196        CHKERR(PetscViewerCreate(ccomm, &newvwr))
197        CHKERR(PetscCLEAR(self.obj)); self.vwr = newvwr
198        return self
199
200    def createASCII(
201        self,
202        name: str,
203        mode: FileMode | str | None = None,
204        comm: Comm | None = None,
205       ) -> Self:
206        """Create a viewer of type `Type.ASCII`.
207
208        Collective.
209
210        Parameters
211        ----------
212        name
213            The filename associated with the viewer.
214        mode
215            The mode type.
216        comm
217            MPI communicator, defaults to `Sys.getDefaultComm`.
218
219        See Also
220        --------
221        create, setType, setFileMode, setFileName, Sys.getDefaultComm
222        setASCIITab, addASCIITab, subtractASCIITab, getASCIITab
223
224        """
225        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
226        cdef const char *cname = NULL
227        name = str2bytes(name, &cname)
228        cdef PetscFileMode cmode = PETSC_FILE_MODE_WRITE
229        if mode is not None: cmode = filemode(mode)
230        cdef PetscViewer newvwr = NULL
231        CHKERR(PetscViewerCreate(ccomm, &newvwr))
232        CHKERR(PetscCLEAR(self.obj)); self.vwr = newvwr
233        CHKERR(PetscViewerSetType(self.vwr, PETSCVIEWERASCII))
234        CHKERR(PetscViewerFileSetMode(self.vwr, cmode))
235        CHKERR(PetscViewerFileSetName(self.vwr, cname))
236        return self
237
238    def createBinary(
239        self,
240        name: str,
241        mode: FileMode | str | None = None,
242        comm: Comm | None = None,
243       ) -> Self:
244        """Create a viewer of type `Type.BINARY`.
245
246        Collective.
247
248        Parameters
249        ----------
250        name
251            The filename associated with the viewer.
252        mode
253            The mode type.
254        comm
255            MPI communicator, defaults to `Sys.getDefaultComm`.
256
257        See Also
258        --------
259        create, setType, setFileMode, setFileName, Sys.getDefaultComm
260
261        """
262        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
263        cdef const char *cname = NULL
264        name = str2bytes(name, &cname)
265        cdef PetscFileMode cmode = filemode(mode)
266        cdef PetscViewer newvwr = NULL
267        CHKERR(PetscViewerBinaryOpen(ccomm, cname, cmode, &newvwr))
268        CHKERR(PetscCLEAR(self.obj)); self.vwr = newvwr
269        return self
270
271    def createMPIIO(
272        self,
273        name: str,
274        mode: FileMode | str | None = None,
275        comm: Comm | None = None,
276       ) -> Self:
277        """Create a viewer of type `Type.BINARY` supporting MPI-IO.
278
279        Collective.
280
281        Parameters
282        ----------
283        name
284            The filename associated with the viewer.
285        mode
286            The mode type.
287        comm
288            MPI communicator, defaults to `Sys.getDefaultComm`.
289
290        See Also
291        --------
292        create, setType, setFileMode, setFileName, Sys.getDefaultComm
293
294        """
295        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
296        cdef const char *cname = NULL
297        name = str2bytes(name, &cname)
298        cdef PetscFileMode cmode = filemode(mode)
299        cdef PetscViewer newvwr = NULL
300        CHKERR(PetscViewerCreate(ccomm, &newvwr))
301        CHKERR(PetscCLEAR(self.obj)); self.vwr = newvwr
302        CHKERR(PetscViewerSetType(self.vwr, PETSCVIEWERBINARY))
303        CHKERR(PetscViewerBinarySetUseMPIIO(self.vwr, PETSC_TRUE))
304        CHKERR(PetscViewerFileSetMode(self.vwr, cmode))
305        CHKERR(PetscViewerFileSetName(self.vwr, cname))
306        return self
307
308    def createVTK(
309        self,
310        name: str,
311        mode: FileMode | str | None = None,
312        comm: Comm | None = None,
313       ) -> Self:
314        """Create a viewer of type `Type.VTK`.
315
316        Collective.
317
318        Parameters
319        ----------
320        name
321            The filename associated with the viewer.
322        mode
323            The mode type.
324        comm
325            MPI communicator, defaults to `Sys.getDefaultComm`.
326
327        See Also
328        --------
329        create, setType, setFileMode, setFileName, Sys.getDefaultComm
330
331        """
332        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
333        cdef const char *cname = NULL
334        name = str2bytes(name, &cname)
335        cdef PetscFileMode cmode = filemode(mode)
336        cdef PetscViewer newvwr = NULL
337        CHKERR(PetscViewerCreate(ccomm, &newvwr))
338        CHKERR(PetscCLEAR(self.obj)); self.vwr = newvwr
339        CHKERR(PetscViewerSetType(self.vwr, PETSCVIEWERVTK))
340        CHKERR(PetscViewerFileSetMode(self.vwr, cmode))
341        CHKERR(PetscViewerFileSetName(self.vwr, cname))
342        return self
343
344    def createHDF5(
345        self,
346        name: str,
347        mode: FileMode | str | None = None,
348        comm: Comm | None = None,
349       ) -> Self:
350        """Create a viewer of type `Type.HDF5`.
351
352        Collective.
353
354        Parameters
355        ----------
356        name
357            The filename associated with the viewer.
358        mode
359            The mode type.
360        comm
361            MPI communicator, defaults to `Sys.getDefaultComm`.
362
363        See Also
364        --------
365        create, setType, setFileMode, setFileName, Sys.getDefaultComm
366
367        """
368        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
369        cdef const char *cname = NULL
370        name = str2bytes(name, &cname)
371        cdef PetscFileMode cmode = filemode(mode)
372        cdef PetscViewer newvwr = NULL
373        CHKERR(PetscViewerCreate(ccomm, &newvwr))
374        CHKERR(PetscCLEAR(self.obj)); self.vwr = newvwr
375        CHKERR(PetscViewerSetType(self.vwr, PETSCVIEWERHDF5))
376        CHKERR(PetscViewerFileSetMode(self.vwr, cmode))
377        CHKERR(PetscViewerFileSetName(self.vwr, cname))
378        return self
379
380    def createDraw(
381        self,
382        display: str | None = None,
383        title: str | None = None,
384        position: tuple[int, int] | None = None,
385        size: tuple[int, int] | int | None = None,
386        comm: Comm | None = None,
387       ) -> Self:
388        """Create a `Type.DRAW` viewer.
389
390        Collective.
391
392        Parameters
393        ----------
394        display
395            The X display to use or `None` for the local machine.
396        title
397            The window title or `None` for no title.
398        position
399            Screen coordinates of the upper left corner, or `None` for default.
400        size
401            Window size or `None` for default.
402        comm
403            MPI communicator, defaults to `Sys.getDefaultComm`.
404
405        See Also
406        --------
407        Sys.getDefaultComm, petsc.PetscViewerDrawOpen
408
409        """
410        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
411        cdef const char *cdisplay = NULL
412        cdef const char *ctitle = NULL
413        display = str2bytes(display, &cdisplay)
414        title = str2bytes(title, &ctitle)
415        cdef int x, y, h, w
416        x = y = h = w = PETSC_DECIDE
417        if position not in (None, PETSC_DECIDE):
418            x, y = position
419        if size not in (None, PETSC_DECIDE):
420            try:
421                w, h = size
422            except TypeError:
423                w = h = size
424        cdef PetscViewer newvwr = NULL
425        CHKERR(PetscViewerDrawOpen(ccomm, cdisplay, ctitle,
426                                   x, y, w, h, &newvwr))
427        CHKERR(PetscCLEAR(self.obj)); self.vwr = newvwr
428        return self
429
430    def setType(self, vwr_type: Type | str) -> None:
431        """Set the type of the viewer.
432
433        Logically collective.
434
435        Parameters
436        ----------
437        vwr_type
438            The type of the viewer.
439
440        See Also
441        --------
442        getType, petsc.PetscViewerSetType
443
444        """
445        cdef PetscViewerType cval = NULL
446        vwr_type = str2bytes(vwr_type, &cval)
447        CHKERR(PetscViewerSetType(self.vwr, cval))
448
449    def getType(self) -> str:
450        """Return the type of the viewer.
451
452        Not collective.
453
454        See Also
455        --------
456        setType, petsc.PetscViewerGetType
457
458        """
459        cdef PetscViewerType cval = NULL
460        CHKERR(PetscViewerGetType(self.vwr, &cval))
461        return bytes2str(cval)
462
463    def setFromOptions(self) -> None:
464        """Configure the object from the options database.
465
466        Collective.
467
468        See Also
469        --------
470        petsc_options, petsc.PetscViewerSetFromOptions
471
472        """
473        CHKERR(PetscViewerSetFromOptions(self.vwr))
474
475    def setUp(self) -> Self:
476        """Set up the internal data structures for using the viewer.
477
478        Collective.
479
480        See Also
481        --------
482        petsc.PetscViewerSetUp
483
484        """
485        CHKERR(PetscViewerSetUp(self.vwr))
486        return self
487
488    def getFormat(self) -> Format:
489        """Return the format of the viewer.
490
491        Not collective.
492
493        See Also
494        --------
495        pushFormat, popFormat, petsc.PetscViewerGetFormat
496
497        """
498        cdef PetscViewerFormat format = PETSC_VIEWER_DEFAULT
499        CHKERR(PetscViewerGetFormat(self.vwr, &format))
500        return format
501
502    def pushFormat(self, format: Format) -> None:
503        """Push format to the viewer.
504
505        Collective.
506
507        See Also
508        --------
509        popFormat, petsc.PetscViewerPushFormat
510
511        """
512        CHKERR(PetscViewerPushFormat(self.vwr, format))
513
514    def popFormat(self) -> None:
515        """Pop format from the viewer.
516
517        Collective.
518
519        See Also
520        --------
521        pushFormat, petsc.PetscViewerPopFormat
522
523        """
524        CHKERR(PetscViewerPopFormat(self.vwr))
525
526    def getSubViewer(self, comm: Comm | None = None) -> Viewer:
527        """Return a viewer defined on a subcommunicator.
528
529        Collective.
530
531        Parameters
532        ----------
533        comm
534            The subcommunicator. If `None`, uses `COMM_SELF`.
535
536        Notes
537        -----
538        Users must call `restoreSubViewer` when done.
539
540        See Also
541        --------
542        restoreSubViewer, petsc.PetscViewerGetSubViewer
543
544        """
545        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_SELF)
546        cdef Viewer sub = Viewer()
547        CHKERR(PetscViewerGetSubViewer(self.vwr, ccomm, &sub.vwr))
548        return sub
549
550    def restoreSubViewer(self, Viewer sub) -> None:
551        """Restore a viewer defined on a subcommunicator.
552
553        Collective.
554
555        Parameters
556        ----------
557        sub
558            The subviewer obtained from `getSubViewer`.
559
560        See Also
561        --------
562        getSubViewer, petsc.PetscViewerRestoreSubViewer
563
564        """
565        cdef MPI_Comm ccomm = def_Comm(sub.getComm(), PETSC_COMM_SELF)
566        CHKERR(PetscViewerRestoreSubViewer(self.vwr, ccomm, &sub.vwr))
567
568    @classmethod
569    def STDOUT(cls, comm: Comm | None = None) -> Viewer:
570        """Return the standard output viewer associated with the communicator.
571
572        Collective.
573
574        Parameters
575        ----------
576        comm
577            MPI communicator, defaults to `Sys.getDefaultComm`.
578
579        """
580        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
581        cdef Viewer viewer = Viewer()
582        viewer.vwr = PETSC_VIEWER_STDOUT_(ccomm)
583        CHKERR(PetscINCREF(viewer.obj))
584        return viewer
585
586    @classmethod
587    def STDERR(cls, comm: Comm | None = None) -> Viewer:
588        """Return the standard error viewer associated with the communicator.
589
590        Collective.
591
592        Parameters
593        ----------
594        comm
595            MPI communicator, defaults to `Sys.getDefaultComm`.
596
597        """
598        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
599        cdef Viewer viewer = Viewer()
600        viewer.vwr = PETSC_VIEWER_STDERR_(ccomm)
601        CHKERR(PetscINCREF(viewer.obj))
602        return viewer
603
604    @classmethod
605    def ASCII(cls, name : str, comm: Comm | None = None) -> Viewer:
606        """Return an ASCII viewer associated with the communicator.
607
608        Collective.
609
610        Parameters
611        ----------
612        name
613            The filename.
614        comm
615            MPI communicator, defaults to `Sys.getDefaultComm`.
616
617        """
618        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
619        cdef const char *cname = NULL
620        name = str2bytes(name, &cname)
621        cdef Viewer viewer = Viewer()
622        CHKERR(PetscViewerASCIIOpen(ccomm, cname, &viewer.vwr))
623        return viewer
624
625    @classmethod
626    def BINARY(cls, comm: Comm | None = None) -> Viewer:
627        """Return the default `Type.BINARY` viewer associated with the communicator.
628
629        Collective.
630
631        Parameters
632        ----------
633        comm
634            MPI communicator, defaults to `Sys.getDefaultComm`.
635
636        """
637        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
638        cdef Viewer viewer = Viewer()
639        viewer.vwr = PETSC_VIEWER_BINARY_(ccomm)
640        CHKERR(PetscINCREF(viewer.obj))
641        return viewer
642
643    @classmethod
644    def DRAW(cls, comm: Comm | None = None) -> Viewer:
645        """Return the default `Type.DRAW` viewer associated with the communicator.
646
647        Collective.
648
649        Parameters
650        ----------
651        comm
652            MPI communicator, defaults to `Sys.getDefaultComm`.
653
654        """
655        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
656        cdef Viewer viewer = Viewer()
657        viewer.vwr = PETSC_VIEWER_DRAW_(ccomm)
658        CHKERR(PetscINCREF(viewer.obj))
659        return viewer
660
661    # --- ASCII viewers ---
662
663    def setASCIITab(self, tabs : int) -> None:
664        """Set ASCII tab level.
665
666        Collective.
667
668        See Also
669        --------
670        getASCIITab, petsc.PetscViewerASCIISetTab
671
672        """
673        cdef PetscInt ctabs = asInt(tabs)
674        CHKERR(PetscViewerASCIISetTab(self.vwr, ctabs))
675
676    def getASCIITab(self) -> int:
677        """Return the ASCII tab level.
678
679        Not collective.
680
681        See Also
682        --------
683        setASCIITab, petsc.PetscViewerASCIIGetTab
684
685        """
686        cdef PetscInt tabs = 0
687        CHKERR(PetscViewerASCIIGetTab(self.vwr, &tabs))
688        return toInt(tabs)
689
690    def addASCIITab(self, tabs: int) -> None:
691        """Increment the ASCII tab level.
692
693        Collective.
694
695        See Also
696        --------
697        petsc.PetscViewerASCIIAddTab
698
699        """
700        cdef PetscInt ctabs = asInt(tabs)
701        CHKERR(PetscViewerASCIIAddTab(self.vwr, ctabs))
702
703    def subtractASCIITab(self, tabs: int) -> None:
704        """Decrement the ASCII tab level.
705
706        Collective.
707
708        See Also
709        --------
710        petsc.PetscViewerASCIISubtractTab
711
712        """
713        cdef PetscInt ctabs = asInt(tabs)
714        CHKERR(PetscViewerASCIISubtractTab(self.vwr, ctabs))
715
716    def pushASCIISynchronized(self) -> None:
717        """Allow ASCII synchronized calls.
718
719        Collective.
720
721        See Also
722        --------
723        printfASCIISynchronized, popASCIISynchronized
724        petsc.PetscViewerASCIIPushSynchronized
725
726        """
727        CHKERR(PetscViewerASCIIPushSynchronized(self.vwr))
728
729    def popASCIISynchronized(self) -> None:
730        """Disallow ASCII synchronized calls.
731
732        Collective.
733
734        See Also
735        --------
736        printfASCIISynchronized, pushASCIISynchronized
737        petsc.PetscViewerASCIIPopSynchronized
738
739        """
740        CHKERR(PetscViewerASCIIPopSynchronized(self.vwr))
741
742    def pushASCIITab(self) -> None:
743        """Push an additional tab level.
744
745        Collective.
746
747        See Also
748        --------
749        popASCIITab, petsc.PetscViewerASCIIPushTab
750
751        """
752        CHKERR(PetscViewerASCIIPushTab(self.vwr))
753
754    def popASCIITab(self) -> None:
755        """Pop an additional tab level pushed via `pushASCIITab`.
756
757        Collective.
758
759        See Also
760        --------
761        pushASCIITab, petsc.PetscViewerASCIIPopTab
762
763        """
764        CHKERR(PetscViewerASCIIPopTab(self.vwr))
765
766    def useASCIITabs(self, flag: bool) -> None:
767        """Enable/disable the use of ASCII tabs.
768
769        Collective.
770
771        See Also
772        --------
773        petsc.PetscViewerASCIIUseTabs
774
775        """
776        cdef PetscBool flg = asBool(flag)
777        CHKERR(PetscViewerASCIIUseTabs(self.vwr, flg))
778
779    def printfASCII(self, msg: str) -> None:
780        """Print a message.
781
782        Collective.
783
784        See Also
785        --------
786        petsc.PetscViewerASCIIPrintf
787
788        """
789        cdef const char *cmsg = NULL
790        msg = str2bytes(msg, &cmsg)
791        CHKERR(PetscViewerASCIIPrintf(self.vwr, '%s', cmsg))
792
793    def printfASCIISynchronized(self, msg: str) -> None:
794        """Print a synchronized message.
795
796        Collective.
797
798        See Also
799        --------
800        pushASCIISynchronized, petsc.PetscViewerASCIISynchronizedPrintf
801
802        """
803        cdef const char *cmsg = NULL
804        msg = str2bytes(msg, &cmsg)
805        CHKERR(PetscViewerASCIISynchronizedPrintf(self.vwr, '%s', cmsg))
806
807    # --- methods specific to file viewers ---
808
809    def flush(self) -> None:
810        """Flush the viewer.
811
812        Collective.
813
814        See Also
815        --------
816        petsc.PetscViewerFlush
817
818        """
819        CHKERR(PetscViewerFlush(self.vwr))
820
821    def setFileMode(self, mode: FileMode | str) -> None:
822        """Set file mode.
823
824        Collective.
825
826        See Also
827        --------
828        getFileMode, petsc.PetscViewerFileSetMode
829
830        """
831        CHKERR(PetscViewerFileSetMode(self.vwr, filemode(mode)))
832
833    def getFileMode(self) -> FileMode:
834        """Return the file mode.
835
836        Not collective.
837
838        See Also
839        --------
840        setFileMode, petsc.PetscViewerFileGetMode
841
842        """
843        cdef PetscFileMode mode = PETSC_FILE_MODE_READ
844        CHKERR(PetscViewerFileGetMode(self.vwr, &mode))
845        return mode
846
847    def setFileName(self, name: str) -> None:
848        """Set file name.
849
850        Collective.
851
852        See Also
853        --------
854        getFileName, petsc.PetscViewerFileSetName
855
856        """
857        cdef const char *cval = NULL
858        name = str2bytes(name, &cval)
859        CHKERR(PetscViewerFileSetName(self.vwr, cval))
860
861    def getFileName(self) -> str:
862        """Return file name.
863
864        Not collective.
865
866        See Also
867        --------
868        setFileName, petsc.PetscViewerFileGetName
869
870        """
871        cdef const char *cval = NULL
872        CHKERR(PetscViewerFileGetName(self.vwr, &cval))
873        return bytes2str(cval)
874
875    # --- methods specific to draw viewers ---
876
877    def setDrawInfo(
878        self,
879        display: str | None = None,
880        title: str | None = None,
881        position: tuple[int, int] | None = None,
882        size: tuple[int, int] | int | None = None,
883       ) -> None:
884        """Set window information for a `Type.DRAW` viewer.
885
886        Collective.
887
888        Parameters
889        ----------
890        display
891            The X display to use or `None` for the local machine.
892        title
893            The window title or `None` for no title.
894        position
895            Screen coordinates of the upper left corner, or `None` for default.
896        size
897            Window size or `None` for default.
898
899        """
900        # FIXME missing manual page
901        # See Also
902        # --------
903        # petsc.PetscViewerDrawSetInfo
904        cdef const char *cdisplay = NULL
905        cdef const char *ctitle = NULL
906        display = str2bytes(display, &cdisplay)
907        title = str2bytes(title, &ctitle)
908        cdef int x, y, h, w
909        x = y = h = w = PETSC_DECIDE
910        if position not in (None, PETSC_DECIDE):
911            x, y = position
912        if size not in (None, PETSC_DECIDE):
913            try:
914                w, h = size
915            except TypeError:
916                w = h = size
917        CHKERR(PetscViewerDrawSetInfo(self.vwr,
918                                      cdisplay, ctitle,
919                                      x, y, w, h))
920
921    def clearDraw(self) -> None:
922        """Reset graphics.
923
924        Not collective.
925
926        See Also
927        --------
928        petsc.PetscViewerDrawClear
929
930        """
931        CHKERR(PetscViewerDrawClear(self.vwr))
932
933    def createPython(self, context: Any = None, comm: Comm | None = None) -> Self:
934        """Create a `Type.PYTHON` viewer.
935
936        Collective.
937
938        Parameters
939        ----------
940        context
941            An instance of the Python class implementing the required methods.
942        comm
943            MPI communicator, defaults to `Sys.getDefaultComm`.
944
945        See Also
946        --------
947        petsc_python_viewer, setType, setPythonContext, Type.PYTHON
948
949        """
950        # communicator and sizes
951        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
952        cdef PetscViewer newvwr = NULL
953        CHKERR(PetscViewerCreate(ccomm, &newvwr))
954        CHKERR(PetscCLEAR(self.obj)); self.vwr = newvwr
955        CHKERR(PetscViewerSetType(self.vwr, PETSCVIEWERPYTHON))
956        CHKERR(PetscViewerPythonSetContext(self.vwr, <void*>context))
957        if context:
958            CHKERR(PetscViewerSetUp(self.vwr))
959        return self
960
961    def setPythonContext(self, context: Any) -> None:
962        """Set the instance of the class implementing the required Python methods.
963
964        Logically collective.
965
966        See Also
967        --------
968        petsc_python_viewer, getPythonContext, setPythonType
969
970        """
971        CHKERR(PetscViewerPythonSetContext(self.vwr, <void*>context))
972
973    def getPythonContext(self) -> Any:
974        """Return the instance of the class implementing the required Python methods.
975
976        Not collective.
977
978        See Also
979        --------
980        petsc_python_viewer, setPythonContext
981
982        """
983        cdef void *context = NULL
984        CHKERR(PetscViewerPythonGetContext(self.vwr, &context))
985        if context == NULL: return None
986        else: return <object> context
987
988    def setPythonType(self, py_type: str) -> None:
989        """Set the fully qualified Python name of the class to be used.
990
991        Collective.
992
993        See Also
994        --------
995        petsc_python_viewer, setPythonContext, getPythonType
996        petsc.PetscViewerPythonSetType
997
998        """
999        cdef const char *cval = NULL
1000        py_type = str2bytes(py_type, &cval)
1001        CHKERR(PetscViewerPythonSetType(self.vwr, cval))
1002
1003    def getPythonType(self) -> str:
1004        """Return the fully qualified Python name of the class used by the viewer.
1005
1006        Not collective.
1007
1008        See Also
1009        --------
1010        petsc_python_viewer, setPythonContext, setPythonType
1011        petsc.PetscViewerPythonGetType
1012
1013        """
1014        cdef const char *cval = NULL
1015        CHKERR(PetscViewerPythonGetType(self.vwr, &cval))
1016        return bytes2str(cval)
1017
1018    def viewObjectPython(self, Object obj) -> None:
1019        """View a generic `Object`.
1020
1021        Collective.
1022
1023        See Also
1024        --------
1025        petsc_python_viewer, setPythonContext, setPythonType
1026        petsc.PetscViewerPythonViewObject
1027
1028        """
1029        CHKERR(PetscViewerPythonViewObject(self.vwr, obj.obj[0]))
1030
1031# --------------------------------------------------------------------
1032
1033cdef class ViewerHDF5(Viewer):
1034    """Viewer object for HDF5 file formats.
1035
1036    Viewer is described in the `PETSc manual <petsc:sec_viewers>`.
1037
1038    See Also
1039    --------
1040    Viewer
1041
1042    """
1043
1044    def create(
1045        self,
1046        name: str,
1047        mode: Viewer.FileMode | str | None = None,
1048        comm: Comm | None = None,
1049       ) -> Self:
1050        """Create a viewer of type `Type.HDF5`.
1051
1052        Collective.
1053
1054        Parameters
1055        ----------
1056        name
1057            The filename associated with the viewer.
1058        mode
1059            The mode type.
1060        comm
1061            MPI communicator, defaults to `Sys.getDefaultComm`.
1062
1063        See Also
1064        --------
1065        Viewer.createHDF5
1066
1067        """
1068        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
1069        cdef const char *cname = NULL
1070        name = str2bytes(name, &cname)
1071        cdef PetscFileMode cmode = filemode(mode)
1072        cdef PetscViewer newvwr = NULL
1073        CHKERR(PetscViewerCreate(ccomm, &newvwr))
1074        CHKERR(PetscCLEAR(self.obj)); self.vwr = newvwr
1075        CHKERR(PetscViewerSetType(self.vwr, PETSCVIEWERHDF5))
1076        CHKERR(PetscViewerFileSetMode(self.vwr, cmode))
1077        CHKERR(PetscViewerFileSetName(self.vwr, cname))
1078        return self
1079
1080    def pushTimestepping(self) -> None:
1081        """Activate the timestepping mode.
1082
1083        Logically collective.
1084
1085        See Also
1086        --------
1087        popTimestepping, petsc.PetscViewerHDF5PushTimestepping
1088
1089        """
1090        CHKERR(PetscViewerHDF5PushTimestepping(self.vwr))
1091
1092    def popTimestepping(self) -> None:
1093        """Deactivate the timestepping mode.
1094
1095        Logically collective.
1096
1097        See Also
1098        --------
1099        pushTimestepping, petsc.PetscViewerHDF5PopTimestepping
1100
1101        """
1102        CHKERR(PetscViewerHDF5PopTimestepping(self.vwr))
1103
1104    def getTimestep(self) -> int:
1105        """Return the current time step.
1106
1107        Not collective.
1108
1109        See Also
1110        --------
1111        pushTimestepping, setTimestep, incrementTimestep
1112        petsc.PetscViewerHDF5GetTimestep
1113
1114        """
1115        cdef PetscInt ctimestep = 0
1116        CHKERR(PetscViewerHDF5GetTimestep(self.vwr, &ctimestep))
1117        return toInt(ctimestep)
1118
1119    def setTimestep(self, timestep: int) -> None:
1120        """Set the current time step.
1121
1122        Logically collective.
1123
1124        See Also
1125        --------
1126        pushTimestepping, getTimestep, incrementTimestep
1127        petsc.PetscViewerHDF5SetTimestep
1128
1129        """
1130        CHKERR(PetscViewerHDF5SetTimestep(self.vwr, asInt(timestep)))
1131
1132    def incrementTimestep(self) -> None:
1133        """Increment the time step.
1134
1135        Logically collective.
1136
1137        See Also
1138        --------
1139        pushTimestepping, setTimestep, getTimestep
1140        petsc.PetscViewerHDF5IncrementTimestep
1141
1142        """
1143        CHKERR(PetscViewerHDF5IncrementTimestep(self.vwr))
1144
1145    def pushGroup(self, group: str) -> None:
1146        """Set the current group.
1147
1148        Logically collective.
1149
1150        See Also
1151        --------
1152        popGroup, getGroup, petsc.PetscViewerHDF5PushGroup
1153
1154        """
1155        cdef const char *cgroup = NULL
1156        group = str2bytes(group, &cgroup)
1157        CHKERR(PetscViewerHDF5PushGroup(self.vwr, cgroup))
1158
1159    def popGroup(self) -> None:
1160        """Pop the current group from the stack.
1161
1162        Logically collective.
1163
1164        See Also
1165        --------
1166        pushGroup, getGroup, petsc.PetscViewerHDF5PopGroup
1167
1168        """
1169        CHKERR(PetscViewerHDF5PopGroup(self.vwr))
1170
1171    def getGroup(self) -> str:
1172        """Return the current group.
1173
1174        Not collective.
1175
1176        See Also
1177        --------
1178        pushGroup, popGroup, petsc.PetscViewerHDF5GetGroup
1179
1180        """
1181        cdef const char *cgroup = NULL
1182        CHKERR(PetscViewerHDF5GetGroup(self.vwr, NULL, &cgroup))
1183        group = bytes2str(cgroup)
1184        CHKERR(PetscFree(cgroup))
1185        return group
1186
1187# --------------------------------------------------------------------
1188
1189del ViewerType
1190del ViewerFormat
1191del ViewerFileMode
1192del ViewerDrawSize
1193
1194# --------------------------------------------------------------------
1195