xref: /petsc/src/binding/petsc4py/src/petsc4py/PETSc/DMUtils.pyx (revision 552edb6364df478b294b3111f33a8f37ca096b20)
1
2cdef class DMInterpolation:
3    """Interpolation on a mesh."""
4
5    cdef PetscDMInterpolation dminterp
6
7    def __cinit__(self):
8        self.dminterp = NULL
9
10    def __dealloc__(self):
11        self.destroy()
12
13    def create(self, comm: Comm | None = None) -> Self:
14        """Create a `DMInterpolation` context.
15
16        Collective.
17
18        Parameters
19        ----------
20        comm
21            MPI communicator, defaults to `COMM_SELF`.
22
23        See Also
24        --------
25        destroy, petsc.DMInterpolationCreate
26
27        """
28        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_SELF)
29        cdef PetscDMInterpolation newdminterp = NULL
30        CHKERR(DMInterpolationCreate(ccomm, &newdminterp))
31        CHKERR(DMInterpolationDestroy(&self.dminterp))
32        self.dminterp = newdminterp
33        return self
34
35    def destroy(self) -> Self:
36        """Destroy the `DMInterpolation` context.
37
38        Collective.
39
40        See Also
41        --------
42        create, petsc.DMInterpolationDestroy
43
44        """
45        CHKERR(DMInterpolationDestroy(&self.dminterp))
46        return self
47
48    def evaluate(self, DM dm, Vec x, Vec v=None) -> Vec:
49        """Calculate interpolated field values at the interpolation points.
50
51        Collective.
52
53        Parameters
54        ----------
55        dm
56            The `DM`.
57        x
58            The local vector containing the field to be interpolated.
59        v
60            A vector capable of holding the interpolated field values.
61
62        See Also
63        --------
64        petsc.DMInterpolationEvaluate
65
66        """
67        if v is None:
68            v = Vec()
69        if v.vec == NULL:
70            CHKERR(DMInterpolationGetVector(self.dminterp, &v.vec))
71        CHKERR(DMInterpolationEvaluate(self.dminterp, dm.dm, x.vec, v.vec))
72        return v
73
74    def getCoordinates(self) -> Vec:
75        """Return the coordinates of each interpolation point.
76
77        Collective.
78
79        The local vector entries correspond to interpolation points lying on
80        this process, according to the associated DM.
81
82        See Also
83        --------
84        petsc.DMInterpolationGetCoordinates
85
86        """
87        cdef Vec coords = Vec()
88        CHKERR(DMInterpolationGetCoordinates(self.dminterp, &coords.vec))
89        CHKERR(PetscINCREF(coords.obj))
90        return coords
91
92    def getDim(self) -> int:
93        """Return the spatial dimension of the interpolation context.
94
95        Not collective.
96
97        See Also
98        --------
99        setDim, petsc.DMInterpolationGetDim
100
101        """
102        cdef PetscInt cdim = 0
103        CHKERR(DMInterpolationGetDim(self.dminterp, &cdim))
104        return toInt(cdim)
105
106    def getDof(self) -> int:
107        """Return the number of fields interpolated at a point.
108
109        Not collective.
110
111        See Also
112        --------
113        setDof, petsc.DMInterpolationGetDof
114
115        """
116        cdef PetscInt cdof = 0
117        CHKERR(DMInterpolationGetDof(self.dminterp, &cdof))
118        return toInt(cdof)
119
120    def setDim(self, dim: int) -> None:
121        """Set the spatial dimension for the interpolation context.
122
123        Not collective.
124
125        Parameters
126        ----------
127        dim
128            The spatial dimension.
129
130        See Also
131        --------
132        getDim, petsc.DMInterpolationSetDim
133
134        """
135        cdef PetscInt cdim = asInt(dim)
136        CHKERR(DMInterpolationSetDim(self.dminterp, cdim))
137
138    def setDof(self, dof: int) -> None:
139        """Set the number of fields interpolated at a point.
140
141        Not collective.
142
143        Parameters
144        ----------
145        dof
146            The number of fields.
147
148        See Also
149        --------
150        getDof, petsc.DMInterpolationSetDof
151
152        """
153        cdef PetscInt cdof = asInt(dof)
154        CHKERR(DMInterpolationSetDof(self.dminterp, cdof))
155
156    def setUp(
157        self,
158        DM dm,
159        redundantPoints: bool = False,
160        ignoreOutsideDomain: bool = False) -> None:
161        """Compute spatial indices for point location during interpolation.
162
163        Collective.
164
165        Parameters
166        ----------
167        dm
168            The DM for the function space used for interpolation.
169        redundantPoints
170            If `True`, all processes are passing in the same array of points.
171            Otherwise, points need to be communicated among processes.
172        ignoreOutsideDomain
173            Ignore points outside of the domain if `True`; otherwise, return an
174            error.
175
176        See Also
177        --------
178        petsc.DMInterpolationSetUp
179
180        """
181        cdef PetscBool credundantPoints = asBool(redundantPoints)
182        cdef PetscBool cignoreOutsideDomain = asBool(ignoreOutsideDomain)
183        CHKERR(DMInterpolationSetUp(self.dminterp, dm.dm, credundantPoints, cignoreOutsideDomain))
184
185    def getVector(self) -> Vec:
186        """Return a `Vec` which can hold all the interpolated field values.
187
188        Collective.
189
190        This vector should be returned using `restoreVector`.
191
192        See Also
193        --------
194        restoreVector, petsc.DMInterpolationGetVector
195
196        """
197        cdef Vec vec = Vec()
198        CHKERR(DMInterpolationGetVector(self.dminterp, &vec.vec))
199        return vec
200
201    def restoreVector(self, Vec vec) -> None:
202        """Restore a Vec which can hold all the interpolated field values.
203
204        Collective.
205
206        Parameters
207        ----------
208        vec
209            A vector capable of holding the interpolated field values.
210
211        See Also
212        --------
213        getVector, petsc.DMInterpolationRestoreVector
214
215        """
216        CHKERR(DMInterpolationRestoreVector(self.dminterp, &vec.vec))
217