xref: /petsc/src/binding/petsc4py/src/petsc4py/PETSc/AO.pyx (revision 552edb6364df478b294b3111f33a8f37ca096b20)
1# --------------------------------------------------------------------
2
3class AOType(object):
4    """The application ordering types."""
5    BASIC          = S_(AOBASIC)
6    ADVANCED       = S_(AOADVANCED)
7    MAPPING        = S_(AOMAPPING)
8    MEMORYSCALABLE = S_(AOMEMORYSCALABLE)
9
10# --------------------------------------------------------------------
11
12
13cdef class AO(Object):
14    """Application ordering object."""
15    Type = AOType
16
17    def __cinit__(self):
18        self.obj = <PetscObject*> &self.ao
19        self.ao = NULL
20
21    def view(self, Viewer viewer=None) -> None:
22        """Display the application ordering.
23
24        Collective.
25
26        Parameters
27        ----------
28        viewer
29            A `Viewer` to display the ordering.
30
31        See Also
32        --------
33        petsc.AOView
34
35        """
36        cdef PetscViewer cviewer = NULL
37        if viewer is not None: cviewer = viewer.vwr
38        CHKERR(AOView(self.ao, cviewer))
39
40    def destroy(self) -> Self:
41        """Destroy the application ordering.
42
43        Collective.
44
45        See Also
46        --------
47        petsc.AODestroy
48
49        """
50        CHKERR(AODestroy(&self.ao))
51        return self
52
53    def createBasic(
54        self,
55        app: Sequence[int] | IS,
56        petsc: Sequence[int] | IS | None = None,
57        comm: Comm | None = None) -> Self:
58        """Return a basic application ordering using two orderings.
59
60        Collective.
61
62        The arrays/indices ``app`` and ``petsc`` must contain all the integers
63        ``0`` to ``len(app)-1`` with no duplicates; that is there cannot be any
64        "holes" in the indices. Use ``createMapping`` if you wish to have
65        "holes" in the indices.
66
67        Parameters
68        ----------
69        app
70            The application ordering.
71        petsc
72            Another ordering (may be `None` to indicate the natural ordering,
73            that is 0, 1, 2, 3, ...).
74        comm
75            MPI communicator, defaults to `Sys.getDefaultComm`.
76
77        See Also
78        --------
79        createMemoryScalable, createMapping, petsc.AOCreateBasicIS
80        petsc.AOCreateBasic
81
82        """
83        cdef PetscIS isapp = NULL, ispetsc = NULL
84        cdef PetscInt napp = 0, *idxapp = NULL,
85        cdef PetscInt npetsc = 0, *idxpetsc = NULL
86        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
87        cdef PetscAO newao = NULL
88        if isinstance(app, IS):
89            isapp = (<IS>app).iset
90            if petsc is not None:
91                ispetsc = (<IS?>petsc).iset
92            CHKERR(AOCreateBasicIS(isapp, ispetsc, &newao))
93        else:
94            app = iarray_i(app, &napp, &idxapp)
95            if petsc is not None:
96                petsc = iarray_i(petsc, &npetsc, &idxpetsc)
97                assert napp == npetsc, "incompatible array sizes"
98            CHKERR(AOCreateBasic(ccomm, napp, idxapp, idxpetsc, &newao))
99        CHKERR(PetscCLEAR(self.obj)); self.ao = newao
100        return self
101
102    def createMemoryScalable(
103        self,
104        app: Sequence[int] | IS,
105        petsc: Sequence[int] | IS | None = None,
106        comm: Comm | None = None) -> Self:
107        """Return a memory scalable application ordering using two orderings.
108
109        Collective.
110
111        The arrays/indices ``app`` and ``petsc`` must contain all the integers
112        ``0`` to ``len(app)-1`` with no duplicates; that is there cannot be any
113        "holes" in the indices. Use ``createMapping`` if you wish to have
114        "holes" in the indices.
115
116        Comparing with ``createBasic``, this routine trades memory with message
117        communication.
118
119        Parameters
120        ----------
121        app
122            The application ordering.
123        petsc
124            Another ordering (may be `None` to indicate the natural ordering,
125            that is 0, 1, 2, 3, ...).
126        comm
127            MPI communicator, defaults to `Sys.getDefaultComm`.
128
129        See Also
130        --------
131        createBasic, createMapping, petsc.AOCreateMemoryScalableIS
132        petsc.AOCreateMemoryScalable
133
134        """
135        cdef PetscIS isapp = NULL, ispetsc = NULL
136        cdef PetscInt napp = 0, *idxapp = NULL,
137        cdef PetscInt npetsc = 0, *idxpetsc = NULL
138        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
139        cdef PetscAO newao = NULL
140        if isinstance(app, IS):
141            isapp = (<IS>app).iset
142            if petsc is not None:
143                ispetsc = (<IS?>petsc).iset
144            CHKERR(AOCreateMemoryScalableIS(isapp, ispetsc, &newao))
145        else:
146            app = iarray_i(app, &napp, &idxapp)
147            if petsc is not None:
148                petsc = iarray_i(petsc, &npetsc, &idxpetsc)
149                assert napp == npetsc, "incompatible array sizes"
150            CHKERR(AOCreateMemoryScalable(ccomm, napp, idxapp, idxpetsc, &newao))
151        CHKERR(PetscCLEAR(self.obj)); self.ao = newao
152        return self
153
154    def createMapping(
155        self,
156        app: Sequence[int] | IS,
157        petsc: Sequence[int] | IS | None = None,
158        comm: Comm | None = None) -> Self:
159        """Return an application mapping using two orderings.
160
161        Collective.
162
163        The arrays ``app`` and ``petsc`` need NOT contain all the integers
164        ``0`` to ``len(app)-1``, that is there CAN be "holes" in the indices.
165        Use ``createBasic`` if they do not have holes for better performance.
166
167        Parameters
168        ----------
169        app
170            The application ordering.
171        petsc
172            Another ordering. May be `None` to indicate the identity ordering.
173        comm
174            MPI communicator, defaults to `Sys.getDefaultComm`.
175
176        See Also
177        --------
178        createBasic, petsc.AOCreateMappingIS, petsc.AOCreateMapping
179
180        """
181        cdef PetscIS isapp = NULL, ispetsc = NULL
182        cdef PetscInt napp = 0, *idxapp = NULL,
183        cdef PetscInt npetsc = 0, *idxpetsc = NULL
184        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
185        cdef PetscAO newao = NULL
186        if isinstance(app, IS):
187            isapp = (<IS>app).iset
188            if petsc is not None:
189                ispetsc = (<IS?>petsc).iset
190            CHKERR(AOCreateMappingIS(isapp, ispetsc, &newao))
191        else:
192            app = iarray_i(app, &napp, &idxapp)
193            if petsc is not None:
194                petsc = iarray_i(petsc, &npetsc, &idxpetsc)
195                assert napp == npetsc, "incompatible array sizes"
196            CHKERR(AOCreateMapping(ccomm, napp, idxapp, idxpetsc, &newao))
197        CHKERR(PetscCLEAR(self.obj)); self.ao = newao
198        return self
199
200    def getType(self) -> str:
201        """Return the application ordering type.
202
203        Not collective.
204
205        See Also
206        --------
207        petsc.AOGetType
208
209        """
210        cdef PetscAOType cval = NULL
211        CHKERR(AOGetType(self.ao, &cval))
212        return bytes2str(cval)
213
214    def app2petsc(self, indices: Sequence[int] | IS) -> Sequence[int] | IS:
215        """Map an application-defined ordering to the PETSc ordering.
216
217        Collective.
218
219        Any integers in ``indices`` that are negative are left unchanged. This
220        allows one to convert, for example, neighbor lists that use negative
221        entries to indicate nonexistent neighbors due to boundary conditions,
222        etc.
223
224        Integers that are out of range are mapped to -1.
225
226        If ``IS`` is used, it cannot be of type stride or block.
227
228        Parameters
229        ----------
230        indices
231            The indices; to be replaced with their mapped values.
232
233        See Also
234        --------
235        petsc2app, petsc.AOApplicationToPetscIS, petsc.AOApplicationToPetsc
236
237        """
238        cdef PetscIS iset = NULL
239        cdef PetscInt nidx = 0, *idx = NULL
240        if isinstance(indices, IS):
241            iset = (<IS>indices).iset
242            CHKERR(AOApplicationToPetscIS(self.ao, iset))
243        else:
244            indices = oarray_i(indices, &nidx, &idx)
245            CHKERR(AOApplicationToPetsc(self.ao, nidx, idx))
246        return indices
247
248    def petsc2app(self, indices: Sequence[int] | IS) -> Sequence[int] | IS:
249        """Map a PETSc ordering to the application-defined ordering.
250
251        Collective.
252
253        Any integers in ``indices`` that are negative are left unchanged. This
254        allows one to convert, for example, neighbor lists that use negative
255        entries to indicate nonexistent neighbors due to boundary conditions,
256        etc.
257
258        Integers that are out of range are mapped to -1.
259
260        If ``IS`` is used, it cannot be of type stride or block.
261
262        Parameters
263        ----------
264        indices
265            The indices; to be replaced with their mapped values.
266
267        See Also
268        --------
269        app2petsc, petsc.AOPetscToApplicationIS, petsc.AOPetscToApplication
270
271        """
272        cdef PetscIS iset = NULL
273        cdef PetscInt nidx = 0, *idx = NULL
274        if isinstance(indices, IS):
275            iset = (<IS>indices).iset
276            CHKERR(AOPetscToApplicationIS(self.ao, iset))
277        else:
278            indices = oarray_i(indices, &nidx, &idx)
279            CHKERR(AOPetscToApplication(self.ao, nidx, idx))
280        return indices
281
282# --------------------------------------------------------------------
283
284del AOType
285
286# --------------------------------------------------------------------
287