xref: /petsc/src/binding/petsc4py/src/petsc4py/PETSc/DS.pyx (revision 552edb6364df478b294b3111f33a8f37ca096b20)
1# --------------------------------------------------------------------
2
3class DSType(object):
4    """The Discrete System types."""
5    BASIC = S_(PETSCDSBASIC)
6
7# --------------------------------------------------------------------
8
9
10cdef class DS(Object):
11    """Discrete System object."""
12
13    Type = DSType
14
15    #
16
17    def __cinit__(self):
18        self.obj = <PetscObject*> &self.ds
19        self.ds  = NULL
20
21    def view(self, Viewer viewer=None) -> None:
22        """View a discrete system.
23
24        Collective.
25
26        Parameters
27        ----------
28        viewer
29            A `Viewer` to display the system.
30
31        See Also
32        --------
33        petsc.PetscDSView
34
35        """
36        cdef PetscViewer vwr = NULL
37        if viewer is not None: vwr = viewer.vwr
38        CHKERR(PetscDSView(self.ds, vwr))
39
40    def destroy(self) -> Self:
41        """Destroy the discrete system.
42
43        Collective.
44
45        See Also
46        --------
47        create, petsc.PetscDSDestroy
48
49        """
50        CHKERR(PetscDSDestroy(&self.ds))
51        return self
52
53    def create(self, comm: Comm | None = None) -> Self:
54        """Create an empty DS.
55
56        Collective.
57
58        The type can then be set with `setType`.
59
60        Parameters
61        ----------
62        comm
63            MPI communicator, defaults to `Sys.getDefaultComm`.
64
65        See Also
66        --------
67        setType, destroy, petsc.PetscDSCreate
68
69        """
70        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
71        cdef PetscDS newds = NULL
72        CHKERR(PetscDSCreate(ccomm, &newds))
73        CHKERR(PetscCLEAR(self.obj)); self.ds = newds
74        return self
75
76    def setType(self, ds_type: Type | str) -> None:
77        """Build a particular type of a discrete system.
78
79        Collective.
80
81        Parameters
82        ----------
83        ds_type
84            The type of the discrete system.
85
86        See Also
87        --------
88        getType, petsc.PetscDSSetType
89
90        """
91        cdef PetscDSType cval = NULL
92        ds_type = str2bytes(ds_type, &cval)
93        CHKERR(PetscDSSetType(self.ds, cval))
94
95    def getType(self) -> str:
96        """Return the type of the discrete system.
97
98        Not collective.
99
100        See Also
101        --------
102        setType, petsc.PetscDSGetType
103
104        """
105        cdef PetscDSType cval = NULL
106        CHKERR(PetscDSGetType(self.ds, &cval))
107        return bytes2str(cval)
108
109    def setFromOptions(self) -> None:
110        """Set parameters in a `DS` from the options database.
111
112        Collective.
113
114        See Also
115        --------
116        petsc_options, petsc.PetscDSSetFromOptions
117
118        """
119        CHKERR(PetscDSSetFromOptions(self.ds))
120
121    def setUp(self) -> Self:
122        """Construct data structures for the discrete system.
123
124        Collective.
125
126        See Also
127        --------
128        petsc.PetscDSSetUp
129
130        """
131        CHKERR(PetscDSSetUp(self.ds))
132        return self
133
134    #
135
136    def getSpatialDimension(self) -> int:
137        """Return the spatial dimension of the DS.
138
139        Not collective.
140
141        The spatial dimension of the `DS` is the topological dimension of the
142        discretizations.
143
144        See Also
145        --------
146        petsc.PetscDSGetSpatialDimension
147
148        """
149        cdef PetscInt dim = 0
150        CHKERR(PetscDSGetSpatialDimension(self.ds, &dim))
151        return toInt(dim)
152
153    def getCoordinateDimension(self) -> int:
154        """Return the coordinate dimension of the DS.
155
156        Not collective.
157
158        The coordinate dimension of the `DS` is the dimension of the space into
159        which the discretiaztions are embedded.
160
161        See Also
162        --------
163        petsc.PetscDSGetCoordinateDimension
164
165        """
166        cdef PetscInt dim = 0
167        CHKERR(PetscDSGetCoordinateDimension(self.ds, &dim))
168        return toInt(dim)
169
170    def getNumFields(self) -> int:
171        """Return the number of fields in the DS.
172
173        Not collective.
174
175        See Also
176        --------
177        petsc.PetscDSGetNumFields
178
179        """
180        cdef PetscInt nf = 0
181        CHKERR(PetscDSGetNumFields(self.ds, &nf))
182        return toInt(nf)
183
184    def getFieldIndex(self, Object disc) -> int:
185        """Return the index of the given field.
186
187        Not collective.
188
189        Parameters
190        ----------
191        disc
192            The discretization object.
193
194        See Also
195        --------
196        petsc.PetscDSGetFieldIndex
197
198        """
199        cdef PetscInt field = 0
200        CHKERR(PetscDSGetFieldIndex(self.ds, disc.obj[0], &field))
201        return toInt(field)
202
203    def getTotalDimensions(self) -> int:
204        """Return the total size of the approximation space for this system.
205
206        Not collective.
207
208        See Also
209        --------
210        petsc.PetscDSGetTotalDimension
211
212        """
213        cdef PetscInt tdim = 0
214        CHKERR(PetscDSGetTotalDimension(self.ds, &tdim))
215        return toInt(tdim)
216
217    def getTotalComponents(self) -> int:
218        """Return the total number of components in this system.
219
220        Not collective.
221
222        See Also
223        --------
224        petsc.PetscDSGetTotalComponents
225
226        """
227        cdef PetscInt tcmp = 0
228        CHKERR(PetscDSGetTotalComponents(self.ds, &tcmp))
229        return toInt(tcmp)
230
231    def getDimensions(self) -> ArrayInt:
232        """Return the size of the space for each field on an evaluation point.
233
234        Not collective.
235
236        See Also
237        --------
238        petsc.PetscDSGetDimensions
239
240        """
241        cdef PetscInt nf = 0, *dims = NULL
242        CHKERR(PetscDSGetNumFields(self.ds, &nf))
243        CHKERR(PetscDSGetDimensions(self.ds, &dims))
244        return array_i(nf, dims)
245
246    def getComponents(self) -> ArrayInt:
247        """Return the number of components for each field on an evaluation point.
248
249        Not collective.
250
251        See Also
252        --------
253        petsc.PetscDSGetComponents
254
255        """
256        cdef PetscInt nf = 0, *cmps = NULL
257        CHKERR(PetscDSGetNumFields(self.ds, &nf))
258        CHKERR(PetscDSGetComponents(self.ds, &cmps))
259        return array_i(nf, cmps)
260
261    def setDiscretisation(self, f: int, disc: Object) -> None:
262        """Set the discretization object for the given field.
263
264        Not collective.
265
266        Parameters
267        ----------
268        f
269            The field number.
270        disc
271            The discretization object.
272
273        See Also
274        --------
275        petsc.PetscDSSetDiscretization
276
277        """
278        cdef PetscInt cf = asInt(f)
279        cdef FE fe = disc
280        CHKERR(PetscDSSetDiscretization(self.ds, cf, <PetscObject> fe.fe))
281
282# --------------------------------------------------------------------
283
284del DSType
285
286# --------------------------------------------------------------------
287