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