1 #include <petsc/private/petscfeimpl.h> /*I "petscfe.h" I*/ 2 3 static PetscErrorCode PetscSpaceTensorCreateSubspace(PetscSpace space, PetscInt Nvs, PetscSpace *subspace) 4 { 5 PetscInt degree; 6 const char *prefix; 7 PetscErrorCode ierr; 8 9 PetscFunctionBegin; 10 ierr = PetscSpaceGetDegree(space, °ree, NULL);CHKERRQ(ierr); 11 ierr = PetscObjectGetOptionsPrefix((PetscObject)space, &prefix);CHKERRQ(ierr); 12 ierr = PetscSpaceCreate(PetscObjectComm((PetscObject)space), subspace);CHKERRQ(ierr); 13 ierr = PetscSpaceSetType(*subspace, PETSCSPACEPOLYNOMIAL);CHKERRQ(ierr); 14 ierr = PetscSpaceSetNumVariables(*subspace, Nvs);CHKERRQ(ierr); 15 ierr = PetscSpaceSetNumComponents(*subspace, 1);CHKERRQ(ierr); 16 ierr = PetscSpaceSetDegree(*subspace, degree, PETSC_DETERMINE);CHKERRQ(ierr); 17 ierr = PetscObjectSetOptionsPrefix((PetscObject)*subspace, prefix);CHKERRQ(ierr); 18 ierr = PetscObjectAppendOptionsPrefix((PetscObject)*subspace, "subspace_");CHKERRQ(ierr); 19 PetscFunctionReturn(0); 20 } 21 22 static PetscErrorCode PetscSpaceSetFromOptions_Tensor(PetscOptionItems *PetscOptionsObject,PetscSpace sp) 23 { 24 PetscSpace_Tensor *tens = (PetscSpace_Tensor *) sp->data; 25 PetscInt Ns, Nc, i, Nv, deg; 26 PetscBool uniform = PETSC_TRUE; 27 PetscErrorCode ierr; 28 29 PetscFunctionBegin; 30 ierr = PetscSpaceGetNumVariables(sp, &Nv);CHKERRQ(ierr); 31 if (!Nv) PetscFunctionReturn(0); 32 ierr = PetscSpaceGetNumComponents(sp, &Nc);CHKERRQ(ierr); 33 ierr = PetscSpaceTensorGetNumSubspaces(sp, &Ns);CHKERRQ(ierr); 34 ierr = PetscSpaceGetDegree(sp, °, NULL);CHKERRQ(ierr); 35 if (Ns > 1) { 36 PetscSpace s0; 37 38 ierr = PetscSpaceTensorGetSubspace(sp, 0, &s0);CHKERRQ(ierr); 39 for (i = 1; i < Ns; i++) { 40 PetscSpace si; 41 42 ierr = PetscSpaceTensorGetSubspace(sp, i, &si);CHKERRQ(ierr); 43 if (si != s0) {uniform = PETSC_FALSE; break;} 44 } 45 } 46 Ns = (Ns == PETSC_DEFAULT) ? PetscMax(Nv,1) : Ns; 47 ierr = PetscOptionsHead(PetscOptionsObject,"PetscSpace tensor options");CHKERRQ(ierr); 48 ierr = PetscOptionsInt("-petscspace_tensor_spaces", "The number of subspaces", "PetscSpaceTensorSetNumSubspaces", Ns, &Ns, NULL);CHKERRQ(ierr); 49 ierr = PetscOptionsBool("-petscspace_tensor_uniform", "Subspaces are identical", "PetscSpaceTensorSetFromOptions", uniform, &uniform, NULL);CHKERRQ(ierr); 50 ierr = PetscOptionsTail();CHKERRQ(ierr); 51 if (Ns < 0 || (Nv > 0 && Ns == 0)) SETERRQ1(PetscObjectComm((PetscObject)sp),PETSC_ERR_ARG_OUTOFRANGE,"Cannot have a tensor space made up of %D spaces\n",Ns); 52 if (Nv > 0 && Ns > Nv) SETERRQ2(PetscObjectComm((PetscObject)sp),PETSC_ERR_ARG_OUTOFRANGE,"Cannot have a tensor space with %D subspaces over %D variables\n", Ns, Nv); 53 if (Ns != tens->numTensSpaces) {ierr = PetscSpaceTensorSetNumSubspaces(sp, Ns);CHKERRQ(ierr);} 54 if (uniform) { 55 PetscInt Nvs = Nv / Ns; 56 PetscSpace subspace; 57 58 if (Nv % Ns) SETERRQ2(PetscObjectComm((PetscObject)sp),PETSC_ERR_ARG_WRONG,"Cannot use %D uniform subspaces for %D variable space\n", Ns, Nv); 59 ierr = PetscSpaceTensorGetSubspace(sp, 0, &subspace);CHKERRQ(ierr); 60 if (!subspace) {ierr = PetscSpaceTensorCreateSubspace(sp, Nvs, &subspace);CHKERRQ(ierr);} 61 else {ierr = PetscObjectReference((PetscObject)subspace);CHKERRQ(ierr);} 62 ierr = PetscSpaceSetFromOptions(subspace);CHKERRQ(ierr); 63 for (i = 0; i < Ns; i++) {ierr = PetscSpaceTensorSetSubspace(sp, i, subspace);CHKERRQ(ierr);} 64 ierr = PetscSpaceDestroy(&subspace);CHKERRQ(ierr); 65 } else { 66 for (i = 0; i < Ns; i++) { 67 PetscSpace subspace; 68 69 ierr = PetscSpaceTensorGetSubspace(sp, i, &subspace);CHKERRQ(ierr); 70 if (!subspace) { 71 char tprefix[128]; 72 73 ierr = PetscSpaceTensorCreateSubspace(sp, 1, &subspace);CHKERRQ(ierr); 74 ierr = PetscSNPrintf(tprefix, 128, "%d_",(int)i);CHKERRQ(ierr); 75 ierr = PetscObjectAppendOptionsPrefix((PetscObject)subspace, tprefix);CHKERRQ(ierr); 76 } else { 77 ierr = PetscObjectReference((PetscObject)subspace);CHKERRQ(ierr); 78 } 79 ierr = PetscSpaceSetFromOptions(subspace);CHKERRQ(ierr); 80 ierr = PetscSpaceTensorSetSubspace(sp, i, subspace);CHKERRQ(ierr); 81 ierr = PetscSpaceDestroy(&subspace);CHKERRQ(ierr); 82 } 83 } 84 PetscFunctionReturn(0); 85 } 86 87 static PetscErrorCode PetscSpaceTensorView_Ascii(PetscSpace sp, PetscViewer v) 88 { 89 PetscSpace_Tensor *tens = (PetscSpace_Tensor *) sp->data; 90 PetscBool uniform = PETSC_TRUE; 91 PetscInt Ns = tens->numTensSpaces, i, n; 92 PetscErrorCode ierr; 93 94 PetscFunctionBegin; 95 for (i = 1; i < Ns; i++) { 96 if (tens->tensspaces[i] != tens->tensspaces[0]) {uniform = PETSC_FALSE; break;} 97 } 98 if (uniform) {ierr = PetscViewerASCIIPrintf(v, "Tensor space of %D subspaces (all identical)\n", Ns);CHKERRQ(ierr);} 99 else {ierr = PetscViewerASCIIPrintf(v, "Tensor space of %D subspaces\n", Ns);CHKERRQ(ierr);} 100 n = uniform ? 1 : Ns; 101 for (i = 0; i < n; i++) { 102 ierr = PetscViewerASCIIPushTab(v);CHKERRQ(ierr); 103 ierr = PetscSpaceView(tens->tensspaces[i], v);CHKERRQ(ierr); 104 ierr = PetscViewerASCIIPopTab(v);CHKERRQ(ierr); 105 } 106 PetscFunctionReturn(0); 107 } 108 109 static PetscErrorCode PetscSpaceView_Tensor(PetscSpace sp, PetscViewer viewer) 110 { 111 PetscBool iascii; 112 PetscErrorCode ierr; 113 114 PetscFunctionBegin; 115 ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr); 116 if (iascii) {ierr = PetscSpaceTensorView_Ascii(sp, viewer);CHKERRQ(ierr);} 117 PetscFunctionReturn(0); 118 } 119 120 static PetscErrorCode PetscSpaceSetUp_Tensor(PetscSpace sp) 121 { 122 PetscSpace_Tensor *tens = (PetscSpace_Tensor *) sp->data; 123 PetscInt Nv, Ns, i; 124 PetscBool uniform = PETSC_TRUE; 125 PetscInt deg, maxDeg; 126 PetscErrorCode ierr; 127 128 PetscFunctionBegin; 129 if (tens->setupCalled) PetscFunctionReturn(0); 130 ierr = PetscSpaceGetNumVariables(sp, &Nv);CHKERRQ(ierr); 131 ierr = PetscSpaceTensorGetNumSubspaces(sp, &Ns);CHKERRQ(ierr); 132 if (Ns == PETSC_DEFAULT) { 133 Ns = Nv; 134 ierr = PetscSpaceTensorSetNumSubspaces(sp, Ns);CHKERRQ(ierr); 135 } 136 if (!Ns) { 137 if (Nv) SETERRQ(PetscObjectComm((PetscObject)sp), PETSC_ERR_ARG_OUTOFRANGE, "Cannot have zero subspaces"); 138 } else { 139 PetscSpace s0; 140 141 if (Nv > 0 && Ns > Nv) SETERRQ2(PetscObjectComm((PetscObject)sp),PETSC_ERR_ARG_OUTOFRANGE,"Cannot have a tensor space with %D subspaces over %D variables\n", Ns, Nv); 142 ierr = PetscSpaceTensorGetSubspace(sp, 0, &s0);CHKERRQ(ierr); 143 for (i = 1; i < Ns; i++) { 144 PetscSpace si; 145 146 ierr = PetscSpaceTensorGetSubspace(sp, i, &si);CHKERRQ(ierr); 147 if (si != s0) {uniform = PETSC_FALSE; break;} 148 } 149 if (uniform) { 150 PetscInt Nvs = Nv / Ns; 151 152 if (Nv % Ns) SETERRQ2(PetscObjectComm((PetscObject)sp),PETSC_ERR_ARG_WRONG,"Cannot use %D uniform subspaces for %D variable space\n", Ns, Nv); 153 if (!s0) {ierr = PetscSpaceTensorCreateSubspace(sp, Nvs, &s0);CHKERRQ(ierr);} 154 else {ierr = PetscObjectReference((PetscObject) s0);CHKERRQ(ierr);} 155 ierr = PetscSpaceSetUp(s0);CHKERRQ(ierr); 156 for (i = 0; i < Ns; i++) {ierr = PetscSpaceTensorSetSubspace(sp, i, s0);CHKERRQ(ierr);} 157 ierr = PetscSpaceDestroy(&s0);CHKERRQ(ierr); 158 } else { 159 for (i = 0 ; i < Ns; i++) { 160 PetscSpace si; 161 162 ierr = PetscSpaceTensorGetSubspace(sp, i, &si);CHKERRQ(ierr); 163 if (!si) {ierr = PetscSpaceTensorCreateSubspace(sp, 1, &si);CHKERRQ(ierr);} 164 else {ierr = PetscObjectReference((PetscObject) si);CHKERRQ(ierr);} 165 ierr = PetscSpaceSetUp(si);CHKERRQ(ierr); 166 ierr = PetscSpaceTensorSetSubspace(sp, i, si);CHKERRQ(ierr); 167 ierr = PetscSpaceDestroy(&si);CHKERRQ(ierr); 168 } 169 } 170 } 171 deg = PETSC_MAX_INT; 172 maxDeg = 0; 173 for (i = 0; i < Ns; i++) { 174 PetscSpace si; 175 PetscInt iDeg, iMaxDeg; 176 177 ierr = PetscSpaceTensorGetSubspace(sp, i, &si);CHKERRQ(ierr); 178 ierr = PetscSpaceGetDegree(si, &iDeg, &iMaxDeg);CHKERRQ(ierr); 179 deg = PetscMin(deg, iDeg); 180 maxDeg += iMaxDeg; 181 } 182 sp->degree = deg; 183 sp->maxDegree = maxDeg; 184 tens->uniform = uniform; 185 tens->setupCalled = PETSC_TRUE; 186 PetscFunctionReturn(0); 187 } 188 189 static PetscErrorCode PetscSpaceDestroy_Tensor(PetscSpace sp) 190 { 191 PetscSpace_Tensor *tens = (PetscSpace_Tensor *) sp->data; 192 PetscInt Ns, i; 193 PetscErrorCode ierr; 194 195 PetscFunctionBegin; 196 Ns = tens->numTensSpaces; 197 if (tens->heightsubspaces) { 198 PetscInt d; 199 200 /* sp->Nv is the spatial dimension, so it is equal to the number 201 * of subspaces on higher co-dimension points */ 202 for (d = 0; d < sp->Nv; ++d) { 203 ierr = PetscSpaceDestroy(&tens->heightsubspaces[d]);CHKERRQ(ierr); 204 } 205 } 206 ierr = PetscFree(tens->heightsubspaces);CHKERRQ(ierr); 207 for (i = 0; i < Ns; i++) {ierr = PetscSpaceDestroy(&tens->tensspaces[i]);CHKERRQ(ierr);} 208 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscSpaceTensorSetSubspace_C", NULL);CHKERRQ(ierr); 209 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscSpaceTensorGetSubspace_C", NULL);CHKERRQ(ierr); 210 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscSpaceTensorSetNumSubspaces_C", NULL);CHKERRQ(ierr); 211 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscSpaceTensorGetNumSubspaces_C", NULL);CHKERRQ(ierr); 212 ierr = PetscFree(tens->tensspaces);CHKERRQ(ierr); 213 ierr = PetscFree(tens);CHKERRQ(ierr); 214 PetscFunctionReturn(0); 215 } 216 217 static PetscErrorCode PetscSpaceGetDimension_Tensor(PetscSpace sp, PetscInt *dim) 218 { 219 PetscSpace_Tensor *tens = (PetscSpace_Tensor *) sp->data; 220 PetscInt i, Ns, Nc, d; 221 PetscErrorCode ierr; 222 223 PetscFunctionBegin; 224 ierr = PetscSpaceSetUp(sp);CHKERRQ(ierr); 225 Ns = tens->numTensSpaces; 226 Nc = sp->Nc; 227 d = 1; 228 for (i = 0; i < Ns; i++) { 229 PetscInt id; 230 231 ierr = PetscSpaceGetDimension(tens->tensspaces[i], &id);CHKERRQ(ierr); 232 d *= id; 233 } 234 d *= Nc; 235 *dim = d; 236 PetscFunctionReturn(0); 237 } 238 239 static PetscErrorCode PetscSpaceEvaluate_Tensor(PetscSpace sp, PetscInt npoints, const PetscReal points[], PetscReal B[], PetscReal D[], PetscReal H[]) 240 { 241 PetscSpace_Tensor *tens = (PetscSpace_Tensor *) sp->data; 242 DM dm = sp->dm; 243 PetscInt Nc = sp->Nc; 244 PetscInt Nv = sp->Nv; 245 PetscInt Ns; 246 PetscReal *lpoints, *sB = NULL, *sD = NULL, *sH = NULL; 247 PetscInt c, pdim, d, e, der, der2, i, l, si, p, s, step; 248 PetscErrorCode ierr; 249 250 PetscFunctionBegin; 251 if (!tens->setupCalled) {ierr = PetscSpaceSetUp(sp);CHKERRQ(ierr);} 252 Ns = tens->numTensSpaces; 253 ierr = PetscSpaceGetDimension(sp,&pdim);CHKERRQ(ierr); 254 pdim /= Nc; 255 ierr = DMGetWorkArray(dm, npoints*Nv, MPIU_REAL, &lpoints);CHKERRQ(ierr); 256 if (B || D || H) {ierr = DMGetWorkArray(dm, npoints*pdim, MPIU_REAL, &sB);CHKERRQ(ierr);} 257 if (D || H) {ierr = DMGetWorkArray(dm, npoints*pdim*Nv, MPIU_REAL, &sD);CHKERRQ(ierr);} 258 if (H) {ierr = DMGetWorkArray(dm, npoints*pdim*Nv*Nv, MPIU_REAL, &sH);CHKERRQ(ierr);} 259 if (B) { 260 for (i = 0; i < npoints*pdim*Nc*Nc; i++) B[i] = 0.; 261 for (i = 0; i < npoints*pdim; i++) B[i * Nc*Nc] = 1.; 262 } 263 if (D) { 264 for (i = 0; i < npoints*pdim*Nc*Nc*Nv; i++) D[i] = 0.; 265 for (i = 0; i < npoints*pdim; i++) { 266 for (l = 0; l < Nv; l++) { 267 D[i * Nc*Nc*Nv + l] = 1.; 268 } 269 } 270 } 271 if (H) { 272 for (i = 0; i < npoints*pdim*Nc*Nc*Nv*Nv; i++) D[i] = 0.; 273 for (i = 0; i < npoints*pdim; i++) { 274 for (l = 0; l < Nv*Nv; l++) { 275 H[i * Nc*Nc*Nv*Nv + l] = 1.; 276 } 277 } 278 } 279 for (s = 0, d = 0, step = 1; s < Ns; s++) { 280 PetscInt sNv, spdim; 281 PetscInt skip, j, k; 282 283 ierr = PetscSpaceGetNumVariables(tens->tensspaces[s], &sNv);CHKERRQ(ierr); 284 ierr = PetscSpaceGetDimension(tens->tensspaces[s], &spdim);CHKERRQ(ierr); 285 if ((pdim % step) || (pdim % spdim)) SETERRQ6(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Bad tensor loop: Nv %d, Ns %D, pdim %D, s %D, step %D, spdim %D", Nv, Ns, pdim, s, step, spdim); 286 skip = pdim / (step * spdim); 287 for (p = 0; p < npoints; ++p) { 288 for (i = 0; i < sNv; i++) { 289 lpoints[p * sNv + i] = points[p*Nv + d + i]; 290 } 291 } 292 ierr = PetscSpaceEvaluate(tens->tensspaces[s], npoints, lpoints, sB, sD, sH);CHKERRQ(ierr); 293 if (B) { 294 for (p = 0; p < npoints; p++) { 295 for (k = 0; k < skip; k++) { 296 for (si = 0; si < spdim; si++) { 297 for (j = 0; j < step; j++) { 298 i = (k * spdim + si) * step + j; 299 B[(pdim * p + i) * Nc * Nc] *= sB[spdim * p + si]; 300 } 301 } 302 } 303 } 304 } 305 if (D) { 306 for (p = 0; p < npoints; p++) { 307 for (k = 0; k < skip; k++) { 308 for (si = 0; si < spdim; si++) { 309 for (j = 0; j < step; j++) { 310 i = (k * spdim + si) * step + j; 311 for (der = 0; der < Nv; der++) { 312 if (der >= d && der < d + sNv) { 313 D[(pdim * p + i) * Nc*Nc*Nv + der] *= sD[(spdim * p + si) * sNv + der - d]; 314 } else { 315 D[(pdim * p + i) * Nc*Nc*Nv + der] *= sB[spdim * p + si]; 316 } 317 } 318 } 319 } 320 } 321 } 322 } 323 if (H) { 324 for (p = 0; p < npoints; p++) { 325 for (k = 0; k < skip; k++) { 326 for (si = 0; si < spdim; si++) { 327 for (j = 0; j < step; j++) { 328 i = (k * spdim + si) * step + j; 329 for (der = 0; der < Nv; der++) { 330 for (der2 = 0; der2 < Nv; der2++) { 331 if (der >= d && der < d + sNv && der2 >= d && der2 < d + sNv) { 332 H[((pdim * p + i) * Nc*Nc*Nv + der) * Nv + der2] *= sH[((spdim * p + si) * sNv + der - d) * sNv + der2 - d]; 333 } else if (der >= d && der < d + sNv) { 334 H[((pdim * p + i) * Nc*Nc*Nv + der) * Nv + der2] *= sD[(spdim * p + si) * sNv + der - d]; 335 } else if (der2 >= d && der2 < d + sNv) { 336 H[((pdim * p + i) * Nc*Nc*Nv + der) * Nv + der2] *= sD[(spdim * p + si) * sNv + der2 - d]; 337 } else { 338 H[((pdim * p + i) * Nc*Nc*Nv + der) * Nv + der2] *= sB[spdim * p + si]; 339 } 340 } 341 } 342 } 343 } 344 } 345 } 346 } 347 d += sNv; 348 step *= spdim; 349 } 350 if (B && Nc > 1) { 351 /* Make direct sum basis for multicomponent space */ 352 for (p = 0; p < npoints; ++p) { 353 for (i = 0; i < pdim; ++i) { 354 for (c = 1; c < Nc; ++c) { 355 B[(p*pdim*Nc + i*Nc + c)*Nc + c] = B[(p*pdim + i)*Nc*Nc]; 356 } 357 } 358 } 359 } 360 if (D && Nc > 1) { 361 /* Make direct sum basis for multicomponent space */ 362 for (p = 0; p < npoints; ++p) { 363 for (i = 0; i < pdim; ++i) { 364 for (c = 1; c < Nc; ++c) { 365 for (d = 0; d < Nv; ++d) { 366 D[((p*pdim*Nc + i*Nc + c)*Nc + c)*Nv + d] = D[(p*pdim + i)*Nc*Nc*Nv + d]; 367 } 368 } 369 } 370 } 371 } 372 if (H && Nc > 1) { 373 /* Make direct sum basis for multicomponent space */ 374 for (p = 0; p < npoints; ++p) { 375 for (i = 0; i < pdim; ++i) { 376 for (c = 1; c < Nc; ++c) { 377 for (d = 0; d < Nv; ++d) { 378 for (e = 0; e < Nv; ++e) { 379 H[(((p*pdim*Nc + i*Nc + c)*Nc + c)*Nv + d)*Nv + e] = H[((p*pdim + i)*Nc*Nc*Nv + d)*Nv + e]; 380 } 381 } 382 } 383 } 384 } 385 } 386 if (H) {ierr = DMRestoreWorkArray(dm, npoints*pdim*Nv*Nv, MPIU_REAL, &sH);CHKERRQ(ierr);} 387 if (D || H) {ierr = DMRestoreWorkArray(dm, npoints*pdim*Nv, MPIU_REAL, &sD);CHKERRQ(ierr);} 388 if (B || D || H) {ierr = DMRestoreWorkArray(dm, npoints*pdim, MPIU_REAL, &sB);CHKERRQ(ierr);} 389 ierr = DMRestoreWorkArray(dm, npoints*Nv, MPIU_REAL, &lpoints);CHKERRQ(ierr); 390 PetscFunctionReturn(0); 391 } 392 393 PetscErrorCode PetscSpaceTensorSetNumSubspaces(PetscSpace sp, PetscInt numTensSpaces) 394 { 395 PetscErrorCode ierr; 396 397 PetscFunctionBegin; 398 PetscValidHeaderSpecific(sp, PETSCSPACE_CLASSID, 1); 399 ierr = PetscTryMethod(sp,"PetscSpaceTensorSetNumSubspaces_C",(PetscSpace,PetscInt),(sp,numTensSpaces));CHKERRQ(ierr); 400 PetscFunctionReturn(0); 401 } 402 403 PetscErrorCode PetscSpaceTensorGetNumSubspaces(PetscSpace sp, PetscInt *numTensSpaces) 404 { 405 PetscErrorCode ierr; 406 407 PetscFunctionBegin; 408 PetscValidHeaderSpecific(sp, PETSCSPACE_CLASSID, 1); 409 PetscValidIntPointer(numTensSpaces, 2); 410 ierr = PetscTryMethod(sp,"PetscSpaceTensorGetNumSubspaces_C",(PetscSpace,PetscInt*),(sp,numTensSpaces));CHKERRQ(ierr); 411 PetscFunctionReturn(0); 412 } 413 414 PetscErrorCode PetscSpaceTensorSetSubspace(PetscSpace sp, PetscInt s, PetscSpace subsp) 415 { 416 PetscErrorCode ierr; 417 418 PetscFunctionBegin; 419 PetscValidHeaderSpecific(sp, PETSCSPACE_CLASSID, 1); 420 if (subsp) PetscValidHeaderSpecific(subsp, PETSCSPACE_CLASSID, 3); 421 ierr = PetscTryMethod(sp,"PetscSpaceTensorSetSubspace_C",(PetscSpace,PetscInt,PetscSpace),(sp,s,subsp));CHKERRQ(ierr); 422 PetscFunctionReturn(0); 423 } 424 425 PetscErrorCode PetscSpaceTensorGetSubspace(PetscSpace sp, PetscInt s, PetscSpace *subsp) 426 { 427 PetscErrorCode ierr; 428 429 PetscFunctionBegin; 430 PetscValidHeaderSpecific(sp, PETSCSPACE_CLASSID, 1); 431 PetscValidPointer(subsp, 3); 432 ierr = PetscTryMethod(sp,"PetscSpaceTensorGetSubspace_C",(PetscSpace,PetscInt,PetscSpace*),(sp,s,subsp));CHKERRQ(ierr); 433 PetscFunctionReturn(0); 434 } 435 436 static PetscErrorCode PetscSpaceTensorSetNumSubspaces_Tensor(PetscSpace space, PetscInt numTensSpaces) 437 { 438 PetscSpace_Tensor *tens = (PetscSpace_Tensor *) space->data; 439 PetscInt Ns; 440 PetscErrorCode ierr; 441 442 PetscFunctionBegin; 443 if (tens->setupCalled) SETERRQ(PetscObjectComm((PetscObject)space),PETSC_ERR_ARG_WRONGSTATE,"Cannot change number of subspaces after setup called\n"); 444 Ns = tens->numTensSpaces; 445 if (numTensSpaces == Ns) PetscFunctionReturn(0); 446 if (Ns >= 0) { 447 PetscInt s; 448 449 for (s = 0; s < Ns; s++) {ierr = PetscSpaceDestroy(&tens->tensspaces[s]);CHKERRQ(ierr);} 450 ierr = PetscFree(tens->tensspaces);CHKERRQ(ierr); 451 } 452 Ns = tens->numTensSpaces = numTensSpaces; 453 ierr = PetscCalloc1(Ns, &tens->tensspaces);CHKERRQ(ierr); 454 PetscFunctionReturn(0); 455 } 456 457 static PetscErrorCode PetscSpaceTensorGetNumSubspaces_Tensor(PetscSpace space, PetscInt *numTensSpaces) 458 { 459 PetscSpace_Tensor *tens = (PetscSpace_Tensor *) space->data; 460 461 PetscFunctionBegin; 462 *numTensSpaces = tens->numTensSpaces; 463 PetscFunctionReturn(0); 464 } 465 466 static PetscErrorCode PetscSpaceTensorSetSubspace_Tensor(PetscSpace space, PetscInt s, PetscSpace subspace) 467 { 468 PetscSpace_Tensor *tens = (PetscSpace_Tensor *) space->data; 469 PetscInt Ns; 470 PetscErrorCode ierr; 471 472 PetscFunctionBegin; 473 if (tens->setupCalled) SETERRQ(PetscObjectComm((PetscObject)space),PETSC_ERR_ARG_WRONGSTATE,"Cannot change subspace after setup called\n"); 474 Ns = tens->numTensSpaces; 475 if (Ns < 0) SETERRQ(PetscObjectComm((PetscObject)space),PETSC_ERR_ARG_WRONGSTATE,"Must call PetscSpaceTensorSetNumSubspaces() first\n"); 476 if (s < 0 || s >= Ns) SETERRQ1(PetscObjectComm((PetscObject)space),PETSC_ERR_ARG_OUTOFRANGE,"Invalid subspace number %D\n",subspace); 477 ierr = PetscObjectReference((PetscObject)subspace);CHKERRQ(ierr); 478 ierr = PetscSpaceDestroy(&tens->tensspaces[s]);CHKERRQ(ierr); 479 tens->tensspaces[s] = subspace; 480 PetscFunctionReturn(0); 481 } 482 483 static PetscErrorCode PetscSpaceGetHeightSubspace_Tensor(PetscSpace sp, PetscInt height, PetscSpace *subsp) 484 { 485 PetscSpace_Tensor *tens = (PetscSpace_Tensor *) sp->data; 486 PetscInt Nc, dim, order, i; 487 PetscSpace bsp; 488 PetscErrorCode ierr; 489 490 PetscFunctionBegin; 491 if (!tens->uniform) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Can only get a generic height subspace of a uniform tensor space: this tensor space is not uniform.\n"); 492 ierr = PetscSpaceGetNumComponents(sp, &Nc);CHKERRQ(ierr); 493 ierr = PetscSpaceGetNumVariables(sp, &dim);CHKERRQ(ierr); 494 ierr = PetscSpaceGetDegree(sp, &order, NULL);CHKERRQ(ierr); 495 if (height > dim || height < 0) {SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Asked for space at height %D for dimension %D space", height, dim);} 496 if (!tens->heightsubspaces) {ierr = PetscCalloc1(dim, &tens->heightsubspaces);CHKERRQ(ierr);} 497 if (height <= dim) { 498 if (!tens->heightsubspaces[height-1]) { 499 PetscSpace sub; 500 501 ierr = PetscSpaceTensorGetSubspace(sp, 0, &bsp);CHKERRQ(ierr); 502 ierr = PetscSpaceCreate(PetscObjectComm((PetscObject) sp), &sub);CHKERRQ(ierr); 503 ierr = PetscSpaceSetType(sub, PETSCSPACETENSOR);CHKERRQ(ierr); 504 ierr = PetscSpaceSetNumComponents(sub, Nc);CHKERRQ(ierr); 505 ierr = PetscSpaceSetDegree(sub, order, PETSC_DETERMINE);CHKERRQ(ierr); 506 ierr = PetscSpaceSetNumVariables(sub, dim-height);CHKERRQ(ierr); 507 ierr = PetscSpaceTensorSetNumSubspaces(sub, dim-height);CHKERRQ(ierr); 508 for (i = 0; i < dim - height; i++) { 509 ierr = PetscSpaceTensorSetSubspace(sub, i, bsp);CHKERRQ(ierr); 510 } 511 ierr = PetscSpaceSetUp(sub);CHKERRQ(ierr); 512 tens->heightsubspaces[height-1] = sub; 513 } 514 *subsp = tens->heightsubspaces[height-1]; 515 } else { 516 *subsp = NULL; 517 } 518 PetscFunctionReturn(0); 519 } 520 521 static PetscErrorCode PetscSpaceTensorGetSubspace_Tensor(PetscSpace space, PetscInt s, PetscSpace *subspace) 522 { 523 PetscSpace_Tensor *tens = (PetscSpace_Tensor *) space->data; 524 PetscInt Ns; 525 526 PetscFunctionBegin; 527 Ns = tens->numTensSpaces; 528 if (Ns < 0) SETERRQ(PetscObjectComm((PetscObject)space),PETSC_ERR_ARG_WRONGSTATE,"Must call PetscSpaceTensorSetNumSubspaces() first\n"); 529 if (s < 0 || s >= Ns) SETERRQ1(PetscObjectComm((PetscObject)space),PETSC_ERR_ARG_OUTOFRANGE,"Invalid subspace number %D\n",subspace); 530 *subspace = tens->tensspaces[s]; 531 PetscFunctionReturn(0); 532 } 533 534 static PetscErrorCode PetscSpaceInitialize_Tensor(PetscSpace sp) 535 { 536 PetscErrorCode ierr; 537 538 PetscFunctionBegin; 539 sp->ops->setfromoptions = PetscSpaceSetFromOptions_Tensor; 540 sp->ops->setup = PetscSpaceSetUp_Tensor; 541 sp->ops->view = PetscSpaceView_Tensor; 542 sp->ops->destroy = PetscSpaceDestroy_Tensor; 543 sp->ops->getdimension = PetscSpaceGetDimension_Tensor; 544 sp->ops->evaluate = PetscSpaceEvaluate_Tensor; 545 sp->ops->getheightsubspace = PetscSpaceGetHeightSubspace_Tensor; 546 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscSpaceTensorGetNumSubspaces_C", PetscSpaceTensorGetNumSubspaces_Tensor);CHKERRQ(ierr); 547 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscSpaceTensorSetNumSubspaces_C", PetscSpaceTensorSetNumSubspaces_Tensor);CHKERRQ(ierr); 548 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscSpaceTensorGetSubspace_C", PetscSpaceTensorGetSubspace_Tensor);CHKERRQ(ierr); 549 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscSpaceTensorSetSubspace_C", PetscSpaceTensorSetSubspace_Tensor);CHKERRQ(ierr); 550 PetscFunctionReturn(0); 551 } 552 553 /*MC 554 PETSCSPACETENSOR = "tensor" - A PetscSpace object that encapsulates a tensor product space. 555 Subspaces are scalar spaces (num of componenents = 1), so the components 556 of a vector-valued tensor space are assumed to be identical. 557 558 Level: intermediate 559 560 .seealso: PetscSpaceType, PetscSpaceCreate(), PetscSpaceSetType() 561 M*/ 562 563 PETSC_EXTERN PetscErrorCode PetscSpaceCreate_Tensor(PetscSpace sp) 564 { 565 PetscSpace_Tensor *tens; 566 PetscErrorCode ierr; 567 568 PetscFunctionBegin; 569 PetscValidHeaderSpecific(sp, PETSCSPACE_CLASSID, 1); 570 ierr = PetscNewLog(sp,&tens);CHKERRQ(ierr); 571 sp->data = tens; 572 573 tens->numTensSpaces = PETSC_DEFAULT; 574 575 ierr = PetscSpaceInitialize_Tensor(sp);CHKERRQ(ierr); 576 PetscFunctionReturn(0); 577 } 578 579